2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #ifdef OPENSSL_SYS_VMS
14 # pragma message disable DOLLARID
17 # include <lib$routines.h>
18 # include <libfisdef.h>
22 # include "../vms_rms.h"
24 /* Some compiler options may mask the declaration of "_malloc32". */
25 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
26 # if __INITIAL_POINTER_SIZE == 64
27 # pragma pointer_size save
28 # pragma pointer_size 32
29 void *_malloc32(__size_t);
30 # pragma pointer_size restore
31 # endif /* __INITIAL_POINTER_SIZE == 64 */
32 # endif /* __INITIAL_POINTER_SIZE && defined
35 # pragma message disable DOLLARID
37 static int vms_load(DSO *dso);
38 static int vms_unload(DSO *dso);
39 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
40 static char *vms_name_converter(DSO *dso, const char *filename);
41 static char *vms_merger(DSO *dso, const char *filespec1,
42 const char *filespec2);
44 static DSO_METHOD dso_meth_vms = {
45 "OpenSSL 'VMS' shared library method",
54 NULL, /* pathbyaddr */
55 NULL /* globallookup */
59 * On VMS, the only "handle" is the file name. LIB$FIND_IMAGE_SYMBOL depends
60 * on the reference to the file name being the same for all calls regarding
61 * one shared image, so we'll just store it in an instance of the following
62 * structure and put a pointer to that instance in the meth_data stack.
64 typedef struct dso_internal_st {
66 * This should contain the name only, no directory, no extension, nothing
69 struct dsc$descriptor_s filename_dsc;
70 char filename[NAMX_MAXRSS + 1];
72 * This contains whatever is not in filename, if needed. Normally not
75 struct dsc$descriptor_s imagename_dsc;
76 char imagename[NAMX_MAXRSS + 1];
79 DSO_METHOD *DSO_METHOD_openssl(void)
84 static int vms_load(DSO *dso)
87 /* See applicable comments in dso_dl.c */
88 char *filename = DSO_convert_filename(dso, NULL);
90 /* Ensure 32-bit pointer for "p", and appropriate malloc() function. */
91 # if __INITIAL_POINTER_SIZE == 64
92 # define DSO_MALLOC _malloc32
93 # pragma pointer_size save
94 # pragma pointer_size 32
95 # else /* __INITIAL_POINTER_SIZE == 64 */
96 # define DSO_MALLOC OPENSSL_malloc
97 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
99 DSO_VMS_INTERNAL *p = NULL;
101 # if __INITIAL_POINTER_SIZE == 64
102 # pragma pointer_size restore
103 # endif /* __INITIAL_POINTER_SIZE == 64 */
105 const char *sp1, *sp2; /* Search result */
106 const char *ext = NULL; /* possible extension to add */
108 if (filename == NULL) {
109 DSOerr(DSO_F_VMS_LOAD, DSO_R_NO_FILENAME);
114 * A file specification may look like this:
116 * node::dev:[dir-spec]name.type;ver
118 * or (for compatibility with TOPS-20):
120 * node::dev:<dir-spec>name.type;ver
122 * and the dir-spec uses '.' as separator. Also, a dir-spec
123 * may consist of several parts, with mixed use of [] and <>:
127 * We need to split the file specification into the name and
128 * the rest (both before and after the name itself).
131 * Start with trying to find the end of a dir-spec, and save the position
132 * of the byte after in sp1
134 sp1 = strrchr(filename, ']');
135 sp2 = strrchr(filename, '>');
138 if (sp2 != NULL && sp2 > sp1)
141 sp1 = strrchr(filename, ':');
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, '.');
149 * If there is a period and the next character is a semi-colon,
150 * we need to add an extension
152 if (sp2 != NULL && sp2[1] == ';')
155 * If we found it, that's where we'll cut. Otherwise, look for a version
156 * number and save the position in sp2
159 sp2 = strchr(sp1, ';');
163 * If there was still nothing to find, set sp2 to point at the end of the
167 sp2 = sp1 + strlen(sp1);
169 /* Check that we won't get buffer overflows */
170 if (sp2 - sp1 > FILENAME_MAX
171 || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
172 DSOerr(DSO_F_VMS_LOAD, DSO_R_FILENAME_TOO_BIG);
176 p = DSO_MALLOC(sizeof(*p));
178 DSOerr(DSO_F_VMS_LOAD, ERR_R_MALLOC_FAILURE);
182 strncpy(p->filename, sp1, sp2 - sp1);
183 p->filename[sp2 - sp1] = '\0';
185 strncpy(p->imagename, filename, sp1 - filename);
186 p->imagename[sp1 - filename] = '\0';
188 strcat(p->imagename, ext);
192 strcat(p->imagename, sp2);
194 p->filename_dsc.dsc$w_length = strlen(p->filename);
195 p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
196 p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
197 p->filename_dsc.dsc$a_pointer = p->filename;
198 p->imagename_dsc.dsc$w_length = strlen(p->imagename);
199 p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
200 p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
201 p->imagename_dsc.dsc$a_pointer = p->imagename;
203 if (!sk_void_push(dso->meth_data, (char *)p)) {
204 DSOerr(DSO_F_VMS_LOAD, DSO_R_STACK_ERROR);
208 /* Success (for now, we lie. We actually do not know...) */
209 dso->loaded_filename = filename;
214 OPENSSL_free(filename);
219 * Note that this doesn't actually unload the shared image, as there is no
220 * such thing in VMS. Next time it get loaded again, a new copy will
221 * actually be loaded.
223 static int vms_unload(DSO *dso)
227 DSOerr(DSO_F_VMS_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
230 if (sk_void_num(dso->meth_data) < 1)
232 p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
234 DSOerr(DSO_F_VMS_UNLOAD, DSO_R_NULL_HANDLE);
243 * We must do this in a separate function because of the way the exception
244 * handler works (it makes this function return
246 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
247 struct dsc$descriptor_s *symname_dsc, void **sym,
251 * Make sure that signals are caught and returned instead of aborting the
252 * program. The exception handler gets unestablished automatically on
253 * return from this function.
255 lib$establish(lib$sig_to_ret);
257 if (ptr->imagename_dsc.dsc$w_length)
258 return lib$find_image_symbol(&ptr->filename_dsc,
260 &ptr->imagename_dsc, flags);
262 return lib$find_image_symbol(&ptr->filename_dsc,
263 symname_dsc, sym, 0, flags);
266 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
268 DSO_VMS_INTERNAL *ptr;
270 # ifdef LIB$M_FIS_MIXEDCASE
271 int flags = LIB$M_FIS_MIXEDCASE;
273 int flags = (1 << 4);
275 struct dsc$descriptor_s symname_dsc;
277 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
278 # if __INITIAL_POINTER_SIZE == 64
279 # define SYMNAME symname_32p
280 # pragma pointer_size save
281 # pragma pointer_size 32
283 # pragma pointer_size restore
284 char symname_32[NAMX_MAXRSS + 1];
285 # else /* __INITIAL_POINTER_SIZE == 64 */
286 # define SYMNAME ((char *) symname)
287 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
291 if ((dso == NULL) || (symname == NULL)) {
292 DSOerr(DSO_F_VMS_BIND_SYM, ERR_R_PASSED_NULL_PARAMETER);
295 # if __INITIAL_POINTER_SIZE == 64
296 /* Copy the symbol name to storage with a 32-bit pointer. */
297 symname_32p = symname_32;
298 strcpy(symname_32p, symname);
299 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
301 symname_dsc.dsc$w_length = strlen(SYMNAME);
302 symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
303 symname_dsc.dsc$b_class = DSC$K_CLASS_S;
304 symname_dsc.dsc$a_pointer = SYMNAME;
306 if (sk_void_num(dso->meth_data) < 1) {
307 DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_STACK_ERROR);
310 ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
311 sk_void_num(dso->meth_data) - 1);
313 DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_NULL_HANDLE);
317 if (dso->flags & DSO_FLAG_UPCASE_SYMBOL)
320 status = do_find_symbol(ptr, &symname_dsc, sym, flags);
322 if (!$VMS_STATUS_SUCCESS(status)) {
323 unsigned short length;
325 struct dsc$descriptor_s errstring_dsc;
327 errstring_dsc.dsc$w_length = sizeof(errstring);
328 errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
329 errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
330 errstring_dsc.dsc$a_pointer = errstring;
334 status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
336 if (!$VMS_STATUS_SUCCESS(status))
337 lib$signal(status); /* This is really bad. Abort! */
339 errstring[length] = '\0';
341 DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_SYM_FAILURE);
342 if (ptr->imagename_dsc.dsc$w_length)
343 ERR_add_error_data(9,
345 " in ", ptr->filename,
346 " (", ptr->imagename, ")",
349 ERR_add_error_data(6,
351 " in ", ptr->filename, ": ", errstring);
358 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
360 DSO_FUNC_TYPE sym = 0;
361 vms_bind_sym(dso, symname, (void **)&sym);
365 static char *vms_merger(DSO *dso, const char *filespec1,
366 const char *filespec2)
369 int filespec1len, filespec2len;
371 struct NAMX_STRUCT nam;
372 char esa[NAMX_MAXRSS + 1];
375 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
376 # if __INITIAL_POINTER_SIZE == 64
377 # define FILESPEC1 filespec1_32p;
378 # define FILESPEC2 filespec2_32p;
379 # pragma pointer_size save
380 # pragma pointer_size 32
383 # pragma pointer_size restore
384 char filespec1_32[NAMX_MAXRSS + 1];
385 char filespec2_32[NAMX_MAXRSS + 1];
386 # else /* __INITIAL_POINTER_SIZE == 64 */
387 # define FILESPEC1 ((char *) filespec1)
388 # define FILESPEC2 ((char *) filespec2)
389 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
395 filespec1len = strlen(filespec1);
396 filespec2len = strlen(filespec2);
398 # if __INITIAL_POINTER_SIZE == 64
399 /* Copy the file names to storage with a 32-bit pointer. */
400 filespec1_32p = filespec1_32;
401 filespec2_32p = filespec2_32;
402 strcpy(filespec1_32p, filespec1);
403 strcpy(filespec2_32p, filespec2);
404 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
409 FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
410 FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNS = filespec1len;
411 FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
412 FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNS = filespec2len;
413 NAMX_DNA_FNA_SET(fab)
416 nam.NAMX_ESS = NAMX_MAXRSS;
417 nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
418 SET_NAMX_NO_SHORT_UPCASE(nam);
422 status = sys$parse(&fab, 0, 0);
424 if (!$VMS_STATUS_SUCCESS(status)) {
425 unsigned short length;
427 struct dsc$descriptor_s errstring_dsc;
429 errstring_dsc.dsc$w_length = sizeof(errstring);
430 errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
431 errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
432 errstring_dsc.dsc$a_pointer = errstring;
434 status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
436 if (!$VMS_STATUS_SUCCESS(status))
437 lib$signal(status); /* This is really bad. Abort! */
439 errstring[length] = '\0';
441 DSOerr(DSO_F_VMS_MERGER, DSO_R_FAILURE);
442 ERR_add_error_data(7,
443 "filespec \"", filespec1, "\", ",
444 "defaults \"", filespec2, "\": ", errstring);
449 merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
452 strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
453 merged[nam.NAMX_ESL] = '\0';
456 DSOerr(DSO_F_VMS_MERGER, ERR_R_MALLOC_FAILURE);
459 static char *vms_name_converter(DSO *dso, const char *filename)
461 int len = strlen(filename);
462 char *not_translated = OPENSSL_malloc(len + 1);
463 if (not_translated != NULL)
464 strcpy(not_translated, filename);
465 return (not_translated);
468 #endif /* OPENSSL_SYS_VMS */