dso_vms.c 14 KB

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