2
0

dso_vms.c 15 KB

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