dso_vms.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* dso_vms.c */
  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. #ifdef OPENSSL_SYS_VMS
  62. #pragma message disable DOLLARID
  63. #include <lib$routines.h>
  64. #include <libfisdef.h>
  65. #include <stsdef.h>
  66. #include <descrip.h>
  67. #include <starlet.h>
  68. #endif
  69. #include "cryptlib.h"
  70. #include <openssl/dso.h>
  71. #ifndef OPENSSL_SYS_VMS
  72. DSO_METHOD *DSO_METHOD_vms(void)
  73. {
  74. return NULL;
  75. }
  76. #else
  77. #pragma message disable DOLLARID
  78. static int vms_load(DSO *dso);
  79. static int vms_unload(DSO *dso);
  80. static void *vms_bind_var(DSO *dso, const char *symname);
  81. static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
  82. #if 0
  83. static int vms_unbind_var(DSO *dso, char *symname, void *symptr);
  84. static int vms_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
  85. static int vms_init(DSO *dso);
  86. static int vms_finish(DSO *dso);
  87. static long vms_ctrl(DSO *dso, int cmd, long larg, void *parg);
  88. #endif
  89. static char *vms_name_converter(DSO *dso);
  90. static DSO_METHOD dso_meth_vms = {
  91. "OpenSSL 'VMS' shared library method",
  92. vms_load,
  93. NULL, /* unload */
  94. vms_bind_var,
  95. vms_bind_func,
  96. /* For now, "unbind" doesn't exist */
  97. #if 0
  98. NULL, /* unbind_var */
  99. NULL, /* unbind_func */
  100. #endif
  101. NULL, /* ctrl */
  102. vms_name_converter,
  103. NULL, /* init */
  104. NULL /* finish */
  105. };
  106. /* On VMS, the only "handle" is the file name. LIB$FIND_IMAGE_SYMBOL depends
  107. * on the reference to the file name being the same for all calls regarding
  108. * one shared image, so we'll just store it in an instance of the following
  109. * structure and put a pointer to that instance in the meth_data stack.
  110. */
  111. typedef struct dso_internal_st
  112. {
  113. /* This should contain the name only, no directory,
  114. * no extension, nothing but a name. */
  115. struct dsc$descriptor_s filename_dsc;
  116. char filename[FILENAME_MAX+1];
  117. /* This contains whatever is not in filename, if needed.
  118. * Normally not defined. */
  119. struct dsc$descriptor_s imagename_dsc;
  120. char imagename[FILENAME_MAX+1];
  121. } DSO_VMS_INTERNAL;
  122. DSO_METHOD *DSO_METHOD_vms(void)
  123. {
  124. return(&dso_meth_vms);
  125. }
  126. static int vms_load(DSO *dso)
  127. {
  128. void *ptr = NULL;
  129. /* See applicable comments in dso_dl.c */
  130. char *filename = DSO_convert_filename(dso, NULL);
  131. DSO_VMS_INTERNAL *p;
  132. const char *sp1, *sp2; /* Search result */
  133. if(filename == NULL)
  134. {
  135. DSOerr(DSO_F_DLFCN_LOAD,DSO_R_NO_FILENAME);
  136. goto err;
  137. }
  138. /* A file specification may look like this:
  139. *
  140. * node::dev:[dir-spec]name.type;ver
  141. *
  142. * or (for compatibility with TOPS-20):
  143. *
  144. * node::dev:<dir-spec>name.type;ver
  145. *
  146. * and the dir-spec uses '.' as separator. Also, a dir-spec
  147. * may consist of several parts, with mixed use of [] and <>:
  148. *
  149. * [dir1.]<dir2>
  150. *
  151. * We need to split the file specification into the name and
  152. * the rest (both before and after the name itself).
  153. */
  154. /* Start with trying to find the end of a dir-spec, and save the
  155. position of the byte after in sp1 */
  156. sp1 = strrchr(filename, ']');
  157. sp2 = strrchr(filename, '>');
  158. if (sp1 == NULL) sp1 = sp2;
  159. if (sp2 != NULL && sp2 > sp1) sp1 = sp2;
  160. if (sp1 == NULL) sp1 = strrchr(filename, ':');
  161. if (sp1 == NULL)
  162. sp1 = filename;
  163. else
  164. sp1++; /* The byte after the found character */
  165. /* Now, let's see if there's a type, and save the position in sp2 */
  166. sp2 = strchr(sp1, '.');
  167. /* If we found it, that's where we'll cut. Otherwise, look for a
  168. version number and save the position in sp2 */
  169. if (sp2 == NULL) sp2 = strchr(sp1, ';');
  170. /* If there was still nothing to find, set sp2 to point at the end of
  171. the string */
  172. if (sp2 == NULL) sp2 = sp1 + strlen(sp1);
  173. /* Check that we won't get buffer overflows */
  174. if (sp2 - sp1 > FILENAME_MAX
  175. || (sp1 - filename) + strlen(sp2) > FILENAME_MAX)
  176. {
  177. DSOerr(DSO_F_VMS_LOAD,DSO_R_FILENAME_TOO_BIG);
  178. goto err;
  179. }
  180. p = (DSO_VMS_INTERNAL *)OPENSSL_malloc(sizeof(DSO_VMS_INTERNAL));
  181. if(p == NULL)
  182. {
  183. DSOerr(DSO_F_VMS_LOAD,ERR_R_MALLOC_FAILURE);
  184. goto err;
  185. }
  186. strncpy(p->filename, sp1, sp2-sp1);
  187. p->filename[sp2-sp1] = '\0';
  188. strncpy(p->imagename, filename, sp1-filename);
  189. p->imagename[sp1-filename] = '\0';
  190. strcat(p->imagename, sp2);
  191. p->filename_dsc.dsc$w_length = strlen(p->filename);
  192. p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  193. p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
  194. p->filename_dsc.dsc$a_pointer = p->filename;
  195. p->imagename_dsc.dsc$w_length = strlen(p->imagename);
  196. p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  197. p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
  198. p->imagename_dsc.dsc$a_pointer = p->imagename;
  199. if(!sk_push(dso->meth_data, (char *)p))
  200. {
  201. DSOerr(DSO_F_VMS_LOAD,DSO_R_STACK_ERROR);
  202. goto err;
  203. }
  204. /* Success (for now, we lie. We actually do not know...) */
  205. dso->loaded_filename = filename;
  206. return(1);
  207. err:
  208. /* Cleanup! */
  209. if(p != NULL)
  210. OPENSSL_free(p);
  211. if(filename != NULL)
  212. OPENSSL_free(filename);
  213. return(0);
  214. }
  215. /* Note that this doesn't actually unload the shared image, as there is no
  216. * such thing in VMS. Next time it get loaded again, a new copy will
  217. * actually be loaded.
  218. */
  219. static int vms_unload(DSO *dso)
  220. {
  221. DSO_VMS_INTERNAL *p;
  222. if(dso == NULL)
  223. {
  224. DSOerr(DSO_F_VMS_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
  225. return(0);
  226. }
  227. if(sk_num(dso->meth_data) < 1)
  228. return(1);
  229. p = (DSO_VMS_INTERNAL *)sk_pop(dso->meth_data);
  230. if(p == NULL)
  231. {
  232. DSOerr(DSO_F_VMS_UNLOAD,DSO_R_NULL_HANDLE);
  233. return(0);
  234. }
  235. /* Cleanup */
  236. OPENSSL_free(p);
  237. return(1);
  238. }
  239. /* We must do this in a separate function because of the way the exception
  240. handler works (it makes this function return */
  241. static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
  242. struct dsc$descriptor_s *symname_dsc, void **sym,
  243. unsigned long flags)
  244. {
  245. /* Make sure that signals are caught and returned instead of
  246. aborting the program. The exception handler gets unestablished
  247. automatically on return from this function. */
  248. lib$establish(lib$sig_to_ret);
  249. if(ptr->imagename_dsc.dsc$w_length)
  250. return lib$find_image_symbol(&ptr->filename_dsc,
  251. symname_dsc, sym,
  252. &ptr->imagename_dsc, flags);
  253. else
  254. return lib$find_image_symbol(&ptr->filename_dsc,
  255. symname_dsc, sym,
  256. 0, flags);
  257. }
  258. void vms_bind_sym(DSO *dso, const char *symname, void **sym)
  259. {
  260. DSO_VMS_INTERNAL *ptr;
  261. int status;
  262. int flags = LIB$M_FIS_MIXEDCASE;
  263. struct dsc$descriptor_s symname_dsc;
  264. *sym = NULL;
  265. symname_dsc.dsc$w_length = strlen(symname);
  266. symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  267. symname_dsc.dsc$b_class = DSC$K_CLASS_S;
  268. symname_dsc.dsc$a_pointer = (char *)symname; /* The cast is needed */
  269. if((dso == NULL) || (symname == NULL))
  270. {
  271. DSOerr(DSO_F_VMS_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
  272. return;
  273. }
  274. if(sk_num(dso->meth_data) < 1)
  275. {
  276. DSOerr(DSO_F_VMS_BIND_VAR,DSO_R_STACK_ERROR);
  277. return;
  278. }
  279. ptr = (DSO_VMS_INTERNAL *)sk_value(dso->meth_data,
  280. sk_num(dso->meth_data) - 1);
  281. if(ptr == NULL)
  282. {
  283. DSOerr(DSO_F_VMS_BIND_VAR,DSO_R_NULL_HANDLE);
  284. return;
  285. }
  286. if(dso->flags & DSO_FLAG_UPCASE_SYMBOL) flags = 0;
  287. status = do_find_symbol(ptr, &symname_dsc, sym, flags);
  288. if(!$VMS_STATUS_SUCCESS(status))
  289. {
  290. unsigned short length;
  291. char errstring[257];
  292. struct dsc$descriptor_s errstring_dsc;
  293. errstring_dsc.dsc$w_length = sizeof(errstring);
  294. errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  295. errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
  296. errstring_dsc.dsc$a_pointer = errstring;
  297. *sym = NULL;
  298. status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
  299. if (!$VMS_STATUS_SUCCESS(status))
  300. lib$signal(status); /* This is really bad. Abort! */
  301. else
  302. {
  303. errstring[length] = '\0';
  304. DSOerr(DSO_F_VMS_BIND_VAR,DSO_R_SYM_FAILURE);
  305. if (ptr->imagename_dsc.dsc$w_length)
  306. ERR_add_error_data(9,
  307. "Symbol ", symname,
  308. " in ", ptr->filename,
  309. " (", ptr->imagename, ")",
  310. ": ", errstring);
  311. else
  312. ERR_add_error_data(6,
  313. "Symbol ", symname,
  314. " in ", ptr->filename,
  315. ": ", errstring);
  316. }
  317. return;
  318. }
  319. return;
  320. }
  321. static void *vms_bind_var(DSO *dso, const char *symname)
  322. {
  323. void *sym = 0;
  324. vms_bind_sym(dso, symname, &sym);
  325. return sym;
  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_name_converter(DSO *dso, const char *filename)
  334. {
  335. return(filename);
  336. }
  337. #endif /* OPENSSL_SYS_VMS */