dso_vms.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
  3. * 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 "internal/cryptlib.h"
  62. #include <openssl/dso.h>
  63. #ifndef OPENSSL_SYS_VMS
  64. DSO_METHOD *DSO_METHOD_vms(void)
  65. {
  66. return NULL;
  67. }
  68. #else
  69. # pragma message disable DOLLARID
  70. # include <rms.h>
  71. # include <lib$routines.h>
  72. # include <stsdef.h>
  73. # include <descrip.h>
  74. # include <starlet.h>
  75. # include "../vms_rms.h"
  76. /* Some compiler options may mask the declaration of "_malloc32". */
  77. # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
  78. # if __INITIAL_POINTER_SIZE == 64
  79. # pragma pointer_size save
  80. # pragma pointer_size 32
  81. void *_malloc32(__size_t);
  82. # pragma pointer_size restore
  83. # endif /* __INITIAL_POINTER_SIZE == 64 */
  84. # endif /* __INITIAL_POINTER_SIZE && defined
  85. * _ANSI_C_SOURCE */
  86. # pragma message disable DOLLARID
  87. static int vms_load(DSO *dso);
  88. static int vms_unload(DSO *dso);
  89. static void *vms_bind_var(DSO *dso, const char *symname);
  90. static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
  91. static char *vms_name_converter(DSO *dso, const char *filename);
  92. static char *vms_merger(DSO *dso, const char *filespec1,
  93. const char *filespec2);
  94. static DSO_METHOD dso_meth_vms = {
  95. "OpenSSL 'VMS' shared library method",
  96. vms_load,
  97. NULL, /* unload */
  98. vms_bind_var,
  99. vms_bind_func,
  100. NULL, /* ctrl */
  101. vms_name_converter,
  102. vms_merger,
  103. NULL, /* init */
  104. NULL /* finish */
  105. };
  106. /*
  107. * On VMS, the only "handle" is the file name. LIB$FIND_IMAGE_SYMBOL depends
  108. * on the reference to the file name being the same for all calls regarding
  109. * one shared image, so we'll just store it in an instance of the following
  110. * structure and put a pointer to that instance in the meth_data stack.
  111. */
  112. typedef struct dso_internal_st {
  113. /*
  114. * This should contain the name only, no directory, no extension, nothing
  115. * but a name.
  116. */
  117. struct dsc$descriptor_s filename_dsc;
  118. char filename[NAMX_MAXRSS + 1];
  119. /*
  120. * This contains whatever is not in filename, if needed. Normally not
  121. * defined.
  122. */
  123. struct dsc$descriptor_s imagename_dsc;
  124. char imagename[NAMX_MAXRSS + 1];
  125. } DSO_VMS_INTERNAL;
  126. DSO_METHOD *DSO_METHOD_vms(void)
  127. {
  128. return (&dso_meth_vms);
  129. }
  130. static int vms_load(DSO *dso)
  131. {
  132. void *ptr = NULL;
  133. /* See applicable comments in dso_dl.c */
  134. char *filename = DSO_convert_filename(dso, NULL);
  135. /* Ensure 32-bit pointer for "p", and appropriate malloc() function. */
  136. # if __INITIAL_POINTER_SIZE == 64
  137. # define DSO_MALLOC _malloc32
  138. # pragma pointer_size save
  139. # pragma pointer_size 32
  140. # else /* __INITIAL_POINTER_SIZE == 64 */
  141. # define DSO_MALLOC OPENSSL_malloc
  142. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  143. DSO_VMS_INTERNAL *p = NULL;
  144. # if __INITIAL_POINTER_SIZE == 64
  145. # pragma pointer_size restore
  146. # endif /* __INITIAL_POINTER_SIZE == 64 */
  147. const char *sp1, *sp2; /* Search result */
  148. const char *ext = NULL; /* possible extension to add */
  149. if (filename == NULL) {
  150. DSOerr(DSO_F_VMS_LOAD, DSO_R_NO_FILENAME);
  151. goto err;
  152. }
  153. /*-
  154. * A file specification may look like this:
  155. *
  156. * node::dev:[dir-spec]name.type;ver
  157. *
  158. * or (for compatibility with TOPS-20):
  159. *
  160. * node::dev:<dir-spec>name.type;ver
  161. *
  162. * and the dir-spec uses '.' as separator. Also, a dir-spec
  163. * may consist of several parts, with mixed use of [] and <>:
  164. *
  165. * [dir1.]<dir2>
  166. *
  167. * We need to split the file specification into the name and
  168. * the rest (both before and after the name itself).
  169. */
  170. /*
  171. * Start with trying to find the end of a dir-spec, and save the position
  172. * of the byte after in sp1
  173. */
  174. sp1 = strrchr(filename, ']');
  175. sp2 = strrchr(filename, '>');
  176. if (sp1 == NULL)
  177. sp1 = sp2;
  178. if (sp2 != NULL && sp2 > sp1)
  179. sp1 = sp2;
  180. if (sp1 == NULL)
  181. sp1 = strrchr(filename, ':');
  182. if (sp1 == NULL)
  183. sp1 = filename;
  184. else
  185. sp1++; /* The byte after the found character */
  186. /* Now, let's see if there's a type, and save the position in sp2 */
  187. sp2 = strchr(sp1, '.');
  188. /*
  189. * If there is a period and the next character is a semi-colon,
  190. * we need to add an extension
  191. */
  192. if (sp2 != NULL && sp2[1] == ';')
  193. ext = ".EXE";
  194. /*
  195. * If we found it, that's where we'll cut. Otherwise, look for a version
  196. * number and save the position in sp2
  197. */
  198. if (sp2 == NULL) {
  199. sp2 = strchr(sp1, ';');
  200. ext = ".EXE";
  201. }
  202. /*
  203. * If there was still nothing to find, set sp2 to point at the end of the
  204. * string
  205. */
  206. if (sp2 == NULL)
  207. sp2 = sp1 + strlen(sp1);
  208. /* Check that we won't get buffer overflows */
  209. if (sp2 - sp1 > FILENAME_MAX
  210. || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
  211. DSOerr(DSO_F_VMS_LOAD, DSO_R_FILENAME_TOO_BIG);
  212. goto err;
  213. }
  214. p = DSO_MALLOC(sizeof(*p));
  215. if (p == NULL) {
  216. DSOerr(DSO_F_VMS_LOAD, ERR_R_MALLOC_FAILURE);
  217. goto err;
  218. }
  219. strncpy(p->filename, sp1, sp2 - sp1);
  220. p->filename[sp2 - sp1] = '\0';
  221. strncpy(p->imagename, filename, sp1 - filename);
  222. p->imagename[sp1 - filename] = '\0';
  223. if (ext) {
  224. strcat(p->imagename, ext);
  225. if (*sp2 == '.')
  226. sp2++;
  227. }
  228. strcat(p->imagename, sp2);
  229. p->filename_dsc.dsc$w_length = strlen(p->filename);
  230. p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  231. p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
  232. p->filename_dsc.dsc$a_pointer = p->filename;
  233. p->imagename_dsc.dsc$w_length = strlen(p->imagename);
  234. p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  235. p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
  236. p->imagename_dsc.dsc$a_pointer = p->imagename;
  237. if (!sk_void_push(dso->meth_data, (char *)p)) {
  238. DSOerr(DSO_F_VMS_LOAD, DSO_R_STACK_ERROR);
  239. goto err;
  240. }
  241. /* Success (for now, we lie. We actually do not know...) */
  242. dso->loaded_filename = filename;
  243. return (1);
  244. err:
  245. /* Cleanup! */
  246. OPENSSL_free(p);
  247. OPENSSL_free(filename);
  248. return (0);
  249. }
  250. /*
  251. * Note that this doesn't actually unload the shared image, as there is no
  252. * such thing in VMS. Next time it get loaded again, a new copy will
  253. * actually be loaded.
  254. */
  255. static int vms_unload(DSO *dso)
  256. {
  257. DSO_VMS_INTERNAL *p;
  258. if (dso == NULL) {
  259. DSOerr(DSO_F_VMS_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  260. return (0);
  261. }
  262. if (sk_void_num(dso->meth_data) < 1)
  263. return (1);
  264. p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
  265. if (p == NULL) {
  266. DSOerr(DSO_F_VMS_UNLOAD, DSO_R_NULL_HANDLE);
  267. return (0);
  268. }
  269. /* Cleanup */
  270. OPENSSL_free(p);
  271. return (1);
  272. }
  273. /*
  274. * We must do this in a separate function because of the way the exception
  275. * handler works (it makes this function return
  276. */
  277. static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
  278. struct dsc$descriptor_s *symname_dsc, void **sym,
  279. unsigned long flags)
  280. {
  281. /*
  282. * Make sure that signals are caught and returned instead of aborting the
  283. * program. The exception handler gets unestablished automatically on
  284. * return from this function.
  285. */
  286. lib$establish(lib$sig_to_ret);
  287. if (ptr->imagename_dsc.dsc$w_length)
  288. return lib$find_image_symbol(&ptr->filename_dsc,
  289. symname_dsc, sym,
  290. &ptr->imagename_dsc, flags);
  291. else
  292. return lib$find_image_symbol(&ptr->filename_dsc,
  293. symname_dsc, sym, 0, flags);
  294. }
  295. void vms_bind_sym(DSO *dso, const char *symname, void **sym)
  296. {
  297. DSO_VMS_INTERNAL *ptr;
  298. int status;
  299. # if 0
  300. int flags = (1 << 4); /* LIB$M_FIS_MIXEDCASE, but this symbol isn't
  301. * defined in VMS older than 7.0 or so */
  302. # else
  303. int flags = 0;
  304. # endif
  305. struct dsc$descriptor_s symname_dsc;
  306. /* Arrange 32-bit pointer to (copied) string storage, if needed. */
  307. # if __INITIAL_POINTER_SIZE == 64
  308. # define SYMNAME symname_32p
  309. # pragma pointer_size save
  310. # pragma pointer_size 32
  311. char *symname_32p;
  312. # pragma pointer_size restore
  313. char symname_32[NAMX_MAXRSS + 1];
  314. # else /* __INITIAL_POINTER_SIZE == 64 */
  315. # define SYMNAME ((char *) symname)
  316. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  317. *sym = NULL;
  318. if ((dso == NULL) || (symname == NULL)) {
  319. DSOerr(DSO_F_VMS_BIND_SYM, ERR_R_PASSED_NULL_PARAMETER);
  320. return;
  321. }
  322. # if __INITIAL_POINTER_SIZE == 64
  323. /* Copy the symbol name to storage with a 32-bit pointer. */
  324. symname_32p = symname_32;
  325. strcpy(symname_32p, symname);
  326. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  327. symname_dsc.dsc$w_length = strlen(SYMNAME);
  328. symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  329. symname_dsc.dsc$b_class = DSC$K_CLASS_S;
  330. symname_dsc.dsc$a_pointer = SYMNAME;
  331. if (sk_void_num(dso->meth_data) < 1) {
  332. DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_STACK_ERROR);
  333. return;
  334. }
  335. ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
  336. sk_void_num(dso->meth_data) - 1);
  337. if (ptr == NULL) {
  338. DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_NULL_HANDLE);
  339. return;
  340. }
  341. if (dso->flags & DSO_FLAG_UPCASE_SYMBOL)
  342. flags = 0;
  343. status = do_find_symbol(ptr, &symname_dsc, sym, flags);
  344. if (!$VMS_STATUS_SUCCESS(status)) {
  345. unsigned short length;
  346. char errstring[257];
  347. struct dsc$descriptor_s errstring_dsc;
  348. errstring_dsc.dsc$w_length = sizeof(errstring);
  349. errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  350. errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
  351. errstring_dsc.dsc$a_pointer = errstring;
  352. *sym = NULL;
  353. status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
  354. if (!$VMS_STATUS_SUCCESS(status))
  355. lib$signal(status); /* This is really bad. Abort! */
  356. else {
  357. errstring[length] = '\0';
  358. DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_SYM_FAILURE);
  359. if (ptr->imagename_dsc.dsc$w_length)
  360. ERR_add_error_data(9,
  361. "Symbol ", symname,
  362. " in ", ptr->filename,
  363. " (", ptr->imagename, ")",
  364. ": ", errstring);
  365. else
  366. ERR_add_error_data(6,
  367. "Symbol ", symname,
  368. " in ", ptr->filename, ": ", errstring);
  369. }
  370. return;
  371. }
  372. return;
  373. }
  374. static void *vms_bind_var(DSO *dso, const char *symname)
  375. {
  376. void *sym = 0;
  377. vms_bind_sym(dso, symname, &sym);
  378. return sym;
  379. }
  380. static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
  381. {
  382. DSO_FUNC_TYPE sym = 0;
  383. vms_bind_sym(dso, symname, (void **)&sym);
  384. return sym;
  385. }
  386. static char *vms_merger(DSO *dso, const char *filespec1,
  387. const char *filespec2)
  388. {
  389. int status;
  390. int filespec1len, filespec2len;
  391. struct FAB fab;
  392. struct NAMX_STRUCT nam;
  393. char esa[NAMX_MAXRSS + 1];
  394. char *merged;
  395. /* Arrange 32-bit pointer to (copied) string storage, if needed. */
  396. # if __INITIAL_POINTER_SIZE == 64
  397. # define FILESPEC1 filespec1_32p;
  398. # define FILESPEC2 filespec2_32p;
  399. # pragma pointer_size save
  400. # pragma pointer_size 32
  401. char *filespec1_32p;
  402. char *filespec2_32p;
  403. # pragma pointer_size restore
  404. char filespec1_32[NAMX_MAXRSS + 1];
  405. char filespec2_32[NAMX_MAXRSS + 1];
  406. # else /* __INITIAL_POINTER_SIZE == 64 */
  407. # define FILESPEC1 ((char *) filespec1)
  408. # define FILESPEC2 ((char *) filespec2)
  409. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  410. if (!filespec1)
  411. filespec1 = "";
  412. if (!filespec2)
  413. filespec2 = "";
  414. filespec1len = strlen(filespec1);
  415. filespec2len = strlen(filespec2);
  416. # if __INITIAL_POINTER_SIZE == 64
  417. /* Copy the file names to storage with a 32-bit pointer. */
  418. filespec1_32p = filespec1_32;
  419. filespec2_32p = filespec2_32;
  420. strcpy(filespec1_32p, filespec1);
  421. strcpy(filespec2_32p, filespec2);
  422. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  423. fab = cc$rms_fab;
  424. nam = CC_RMS_NAMX;
  425. FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
  426. FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNS = filespec1len;
  427. FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
  428. FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNS = filespec2len;
  429. NAMX_DNA_FNA_SET(fab)
  430. nam.NAMX_ESA = esa;
  431. nam.NAMX_ESS = NAMX_MAXRSS;
  432. nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
  433. SET_NAMX_NO_SHORT_UPCASE(nam);
  434. fab.FAB_NAMX = &nam;
  435. status = sys$parse(&fab, 0, 0);
  436. if (!$VMS_STATUS_SUCCESS(status)) {
  437. unsigned short length;
  438. char errstring[257];
  439. struct dsc$descriptor_s errstring_dsc;
  440. errstring_dsc.dsc$w_length = sizeof(errstring);
  441. errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  442. errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
  443. errstring_dsc.dsc$a_pointer = errstring;
  444. status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
  445. if (!$VMS_STATUS_SUCCESS(status))
  446. lib$signal(status); /* This is really bad. Abort! */
  447. else {
  448. errstring[length] = '\0';
  449. DSOerr(DSO_F_VMS_MERGER, DSO_R_FAILURE);
  450. ERR_add_error_data(7,
  451. "filespec \"", filespec1, "\", ",
  452. "defaults \"", filespec2, "\": ", errstring);
  453. }
  454. return (NULL);
  455. }
  456. merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
  457. if (merged == NULL)
  458. goto malloc_err;
  459. strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
  460. merged[nam.NAMX_ESL] = '\0';
  461. return (merged);
  462. malloc_err:
  463. DSOerr(DSO_F_VMS_MERGER, ERR_R_MALLOC_FAILURE);
  464. }
  465. static char *vms_name_converter(DSO *dso, const char *filename)
  466. {
  467. int len = strlen(filename);
  468. char *not_translated = OPENSSL_malloc(len + 1);
  469. if (not_translated != NULL)
  470. strcpy(not_translated, filename);
  471. return (not_translated);
  472. }
  473. #endif /* OPENSSL_SYS_VMS */