2
0

dso_vms.c 16 KB

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