2
0

dso_vms.c 15 KB

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