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