dso_vms.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* dso_vms.c -*- mode:C; c-file-style: "eay" -*- */
  2. /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <errno.h>
  61. #include "cryptlib.h"
  62. #include <openssl/dso.h>
  63. #ifdef OPENSSL_SYS_VMS
  64. #pragma message disable DOLLARID
  65. #include <rms.h>
  66. #include <lib$routines.h>
  67. #include <stsdef.h>
  68. #include <descrip.h>
  69. #include <starlet.h>
  70. #endif
  71. #ifndef OPENSSL_SYS_VMS
  72. DSO_METHOD *DSO_METHOD_vms(void)
  73. {
  74. return NULL;
  75. }
  76. #else
  77. #pragma message disable DOLLARID
  78. static int vms_load(DSO *dso);
  79. static int vms_unload(DSO *dso);
  80. static void *vms_bind_var(DSO *dso, const char *symname);
  81. static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
  82. #if 0
  83. static int vms_unbind_var(DSO *dso, char *symname, void *symptr);
  84. static int vms_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
  85. static int vms_init(DSO *dso);
  86. static int vms_finish(DSO *dso);
  87. static long vms_ctrl(DSO *dso, int cmd, long larg, void *parg);
  88. #endif
  89. static char *vms_name_converter(DSO *dso, const char *filename);
  90. static char *vms_merger(DSO *dso, const char *filespec1,
  91. const char *filespec2);
  92. static DSO_METHOD dso_meth_vms = {
  93. "OpenSSL 'VMS' shared library method",
  94. vms_load,
  95. NULL, /* unload */
  96. vms_bind_var,
  97. vms_bind_func,
  98. /* For now, "unbind" doesn't exist */
  99. #if 0
  100. NULL, /* unbind_var */
  101. NULL, /* unbind_func */
  102. #endif
  103. NULL, /* ctrl */
  104. vms_name_converter,
  105. vms_merger,
  106. NULL, /* init */
  107. NULL /* finish */
  108. };
  109. /* On VMS, the only "handle" is the file name. LIB$FIND_IMAGE_SYMBOL depends
  110. * on the reference to the file name being the same for all calls regarding
  111. * one shared image, so we'll just store it in an instance of the following
  112. * structure and put a pointer to that instance in the meth_data stack.
  113. */
  114. typedef struct dso_internal_st
  115. {
  116. /* This should contain the name only, no directory,
  117. * no extension, nothing but a name. */
  118. struct dsc$descriptor_s filename_dsc;
  119. char filename[FILENAME_MAX+1];
  120. /* This contains whatever is not in filename, if needed.
  121. * Normally not defined. */
  122. struct dsc$descriptor_s imagename_dsc;
  123. char imagename[FILENAME_MAX+1];
  124. } DSO_VMS_INTERNAL;
  125. DSO_METHOD *DSO_METHOD_vms(void)
  126. {
  127. return(&dso_meth_vms);
  128. }
  129. static int vms_load(DSO *dso)
  130. {
  131. void *ptr = NULL;
  132. /* See applicable comments in dso_dl.c */
  133. char *filename = DSO_convert_filename(dso, NULL);
  134. DSO_VMS_INTERNAL *p;
  135. const char *sp1, *sp2; /* Search result */
  136. if(filename == NULL)
  137. {
  138. DSOerr(DSO_F_VMS_LOAD,DSO_R_NO_FILENAME);
  139. goto err;
  140. }
  141. /* A file specification may look like this:
  142. *
  143. * node::dev:[dir-spec]name.type;ver
  144. *
  145. * or (for compatibility with TOPS-20):
  146. *
  147. * node::dev:<dir-spec>name.type;ver
  148. *
  149. * and the dir-spec uses '.' as separator. Also, a dir-spec
  150. * may consist of several parts, with mixed use of [] and <>:
  151. *
  152. * [dir1.]<dir2>
  153. *
  154. * We need to split the file specification into the name and
  155. * the rest (both before and after the name itself).
  156. */
  157. /* Start with trying to find the end of a dir-spec, and save the
  158. position of the byte after in sp1 */
  159. sp1 = strrchr(filename, ']');
  160. sp2 = strrchr(filename, '>');
  161. if (sp1 == NULL) sp1 = sp2;
  162. if (sp2 != NULL && sp2 > sp1) sp1 = sp2;
  163. if (sp1 == NULL) sp1 = strrchr(filename, ':');
  164. if (sp1 == NULL)
  165. sp1 = filename;
  166. else
  167. sp1++; /* The byte after the found character */
  168. /* Now, let's see if there's a type, and save the position in sp2 */
  169. sp2 = strchr(sp1, '.');
  170. /* If we found it, that's where we'll cut. Otherwise, look for a
  171. version number and save the position in sp2 */
  172. if (sp2 == NULL) sp2 = strchr(sp1, ';');
  173. /* If there was still nothing to find, set sp2 to point at the end of
  174. the string */
  175. if (sp2 == NULL) sp2 = sp1 + strlen(sp1);
  176. /* Check that we won't get buffer overflows */
  177. if (sp2 - sp1 > FILENAME_MAX
  178. || (sp1 - filename) + strlen(sp2) > FILENAME_MAX)
  179. {
  180. DSOerr(DSO_F_VMS_LOAD,DSO_R_FILENAME_TOO_BIG);
  181. goto err;
  182. }
  183. p = (DSO_VMS_INTERNAL *)OPENSSL_malloc(sizeof(DSO_VMS_INTERNAL));
  184. if(p == NULL)
  185. {
  186. DSOerr(DSO_F_VMS_LOAD,ERR_R_MALLOC_FAILURE);
  187. goto err;
  188. }
  189. strncpy(p->filename, sp1, sp2-sp1);
  190. p->filename[sp2-sp1] = '\0';
  191. strncpy(p->imagename, filename, sp1-filename);
  192. p->imagename[sp1-filename] = '\0';
  193. strcat(p->imagename, sp2);
  194. p->filename_dsc.dsc$w_length = strlen(p->filename);
  195. p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  196. p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
  197. p->filename_dsc.dsc$a_pointer = p->filename;
  198. p->imagename_dsc.dsc$w_length = strlen(p->imagename);
  199. p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  200. p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
  201. p->imagename_dsc.dsc$a_pointer = p->imagename;
  202. if(!sk_push(dso->meth_data, (char *)p))
  203. {
  204. DSOerr(DSO_F_VMS_LOAD,DSO_R_STACK_ERROR);
  205. goto err;
  206. }
  207. /* Success (for now, we lie. We actually do not know...) */
  208. dso->loaded_filename = filename;
  209. return(1);
  210. err:
  211. /* Cleanup! */
  212. if(p != NULL)
  213. OPENSSL_free(p);
  214. if(filename != NULL)
  215. OPENSSL_free(filename);
  216. return(0);
  217. }
  218. /* Note that this doesn't actually unload the shared image, as there is no
  219. * such thing in VMS. Next time it get loaded again, a new copy will
  220. * actually be loaded.
  221. */
  222. static int vms_unload(DSO *dso)
  223. {
  224. DSO_VMS_INTERNAL *p;
  225. if(dso == NULL)
  226. {
  227. DSOerr(DSO_F_VMS_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
  228. return(0);
  229. }
  230. if(sk_num(dso->meth_data) < 1)
  231. return(1);
  232. p = (DSO_VMS_INTERNAL *)sk_pop(dso->meth_data);
  233. if(p == NULL)
  234. {
  235. DSOerr(DSO_F_VMS_UNLOAD,DSO_R_NULL_HANDLE);
  236. return(0);
  237. }
  238. /* Cleanup */
  239. OPENSSL_free(p);
  240. return(1);
  241. }
  242. /* We must do this in a separate function because of the way the exception
  243. handler works (it makes this function return */
  244. static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
  245. struct dsc$descriptor_s *symname_dsc, void **sym,
  246. unsigned long flags)
  247. {
  248. /* Make sure that signals are caught and returned instead of
  249. aborting the program. The exception handler gets unestablished
  250. automatically on return from this function. */
  251. lib$establish(lib$sig_to_ret);
  252. if(ptr->imagename_dsc.dsc$w_length)
  253. return lib$find_image_symbol(&ptr->filename_dsc,
  254. symname_dsc, sym,
  255. &ptr->imagename_dsc, flags);
  256. else
  257. return lib$find_image_symbol(&ptr->filename_dsc,
  258. symname_dsc, sym,
  259. 0, flags);
  260. }
  261. void vms_bind_sym(DSO *dso, const char *symname, void **sym)
  262. {
  263. DSO_VMS_INTERNAL *ptr;
  264. int status;
  265. #if 0
  266. int flags = (1<<4); /* LIB$M_FIS_MIXEDCASE, but this symbol isn't
  267. defined in VMS older than 7.0 or so */
  268. #else
  269. int flags = 0;
  270. #endif
  271. struct dsc$descriptor_s symname_dsc;
  272. *sym = NULL;
  273. symname_dsc.dsc$w_length = strlen(symname);
  274. symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  275. symname_dsc.dsc$b_class = DSC$K_CLASS_S;
  276. symname_dsc.dsc$a_pointer = (char *)symname; /* The cast is needed */
  277. if((dso == NULL) || (symname == NULL))
  278. {
  279. DSOerr(DSO_F_VMS_BIND_SYM,ERR_R_PASSED_NULL_PARAMETER);
  280. return;
  281. }
  282. if(sk_num(dso->meth_data) < 1)
  283. {
  284. DSOerr(DSO_F_VMS_BIND_SYM,DSO_R_STACK_ERROR);
  285. return;
  286. }
  287. ptr = (DSO_VMS_INTERNAL *)sk_value(dso->meth_data,
  288. sk_num(dso->meth_data) - 1);
  289. if(ptr == NULL)
  290. {
  291. DSOerr(DSO_F_VMS_BIND_SYM,DSO_R_NULL_HANDLE);
  292. return;
  293. }
  294. if(dso->flags & DSO_FLAG_UPCASE_SYMBOL) flags = 0;
  295. status = do_find_symbol(ptr, &symname_dsc, sym, flags);
  296. if(!$VMS_STATUS_SUCCESS(status))
  297. {
  298. unsigned short length;
  299. char errstring[257];
  300. struct dsc$descriptor_s errstring_dsc;
  301. errstring_dsc.dsc$w_length = sizeof(errstring);
  302. errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  303. errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
  304. errstring_dsc.dsc$a_pointer = errstring;
  305. *sym = NULL;
  306. status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
  307. if (!$VMS_STATUS_SUCCESS(status))
  308. lib$signal(status); /* This is really bad. Abort! */
  309. else
  310. {
  311. errstring[length] = '\0';
  312. DSOerr(DSO_F_VMS_BIND_SYM,DSO_R_SYM_FAILURE);
  313. if (ptr->imagename_dsc.dsc$w_length)
  314. ERR_add_error_data(9,
  315. "Symbol ", symname,
  316. " in ", ptr->filename,
  317. " (", ptr->imagename, ")",
  318. ": ", errstring);
  319. else
  320. ERR_add_error_data(6,
  321. "Symbol ", symname,
  322. " in ", ptr->filename,
  323. ": ", errstring);
  324. }
  325. return;
  326. }
  327. return;
  328. }
  329. static void *vms_bind_var(DSO *dso, const char *symname)
  330. {
  331. void *sym = 0;
  332. vms_bind_sym(dso, symname, &sym);
  333. return sym;
  334. }
  335. static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
  336. {
  337. DSO_FUNC_TYPE sym = 0;
  338. vms_bind_sym(dso, symname, (void **)&sym);
  339. return sym;
  340. }
  341. static char *vms_merger(DSO *dso, const char *filespec1, const char *filespec2)
  342. {
  343. int status;
  344. int filespec1len, filespec2len;
  345. struct FAB fab;
  346. #ifdef NAML$C_MAXRSS
  347. struct NAML nam;
  348. char esa[NAML$C_MAXRSS];
  349. #else
  350. struct NAM nam;
  351. char esa[NAM$C_MAXRSS];
  352. #endif
  353. char *merged;
  354. if (!filespec1) filespec1 = "";
  355. if (!filespec2) filespec2 = "";
  356. filespec1len = strlen(filespec1);
  357. filespec2len = strlen(filespec2);
  358. fab = cc$rms_fab;
  359. #ifdef NAML$C_MAXRSS
  360. nam = cc$rms_naml;
  361. #else
  362. nam = cc$rms_nam;
  363. #endif
  364. fab.fab$l_fna = (char *)filespec1;
  365. fab.fab$b_fns = filespec1len;
  366. fab.fab$l_dna = (char *)filespec2;
  367. fab.fab$b_dns = filespec2len;
  368. #ifdef NAML$C_MAXRSS
  369. if (filespec1len > NAM$C_MAXRSS)
  370. {
  371. fab.fab$l_fna = 0;
  372. fab.fab$b_fns = 0;
  373. nam.naml$l_long_filename = (char *)filespec1;
  374. nam.naml$l_long_filename_size = filespec1len;
  375. }
  376. if (filespec2len > NAM$C_MAXRSS)
  377. {
  378. fab.fab$l_dna = 0;
  379. fab.fab$b_dns = 0;
  380. nam.naml$l_long_defname = (char *)filespec2;
  381. nam.naml$l_long_defname_size = filespec2len;
  382. }
  383. nam.naml$l_esa = esa;
  384. nam.naml$b_ess = NAM$C_MAXRSS;
  385. nam.naml$l_long_expand = esa;
  386. nam.naml$l_long_expand_alloc = sizeof(esa);
  387. nam.naml$b_nop = NAM$M_SYNCHK | NAM$M_PWD;
  388. nam.naml$v_no_short_upcase = 1;
  389. fab.fab$l_naml = &nam;
  390. #else
  391. nam.nam$l_esa = esa;
  392. nam.nam$b_ess = NAM$C_MAXRSS;
  393. nam.nam$b_nop = NAM$M_SYNCHK | NAM$M_PWD;
  394. fab.fab$l_nam = &nam;
  395. #endif
  396. status = sys$parse(&fab, 0, 0);
  397. if(!$VMS_STATUS_SUCCESS(status))
  398. {
  399. unsigned short length;
  400. char errstring[257];
  401. struct dsc$descriptor_s errstring_dsc;
  402. errstring_dsc.dsc$w_length = sizeof(errstring);
  403. errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  404. errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
  405. errstring_dsc.dsc$a_pointer = errstring;
  406. status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
  407. if (!$VMS_STATUS_SUCCESS(status))
  408. lib$signal(status); /* This is really bad. Abort! */
  409. else
  410. {
  411. errstring[length] = '\0';
  412. DSOerr(DSO_F_VMS_MERGER,DSO_R_FAILURE);
  413. ERR_add_error_data(7,
  414. "filespec \"", filespec1, "\", ",
  415. "defaults \"", filespec2, "\": ",
  416. errstring);
  417. }
  418. return(NULL);
  419. }
  420. #ifdef NAML$C_MAXRSS
  421. if (nam.naml$l_long_expand_size)
  422. {
  423. merged = OPENSSL_malloc(nam.naml$l_long_expand_size + 1);
  424. if(!merged)
  425. goto malloc_err;
  426. strncpy(merged, nam.naml$l_long_expand,
  427. nam.naml$l_long_expand_size);
  428. merged[nam.naml$l_long_expand_size] = '\0';
  429. }
  430. else
  431. {
  432. merged = OPENSSL_malloc(nam.naml$b_esl + 1);
  433. if(!merged)
  434. goto malloc_err;
  435. strncpy(merged, nam.naml$l_esa,
  436. nam.naml$b_esl);
  437. merged[nam.naml$b_esl] = '\0';
  438. }
  439. #else
  440. merged = OPENSSL_malloc(nam.nam$b_esl + 1);
  441. if(!merged)
  442. goto malloc_err;
  443. strncpy(merged, nam.nam$l_esa,
  444. nam.nam$b_esl);
  445. merged[nam.nam$b_esl] = '\0';
  446. #endif
  447. return(merged);
  448. malloc_err:
  449. DSOerr(DSO_F_VMS_MERGER,
  450. ERR_R_MALLOC_FAILURE);
  451. }
  452. static char *vms_name_converter(DSO *dso, const char *filename)
  453. {
  454. int len = strlen(filename);
  455. char *not_translated = OPENSSL_malloc(len+1);
  456. strcpy(not_translated,filename);
  457. return(not_translated);
  458. }
  459. #endif /* OPENSSL_SYS_VMS */