dso_vms.c 17 KB

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