xref: /openssl/crypto/dso/dso_vms.c (revision 89d72311)
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 
10 #include "dso_local.h"
11 
12 #ifdef OPENSSL_SYS_VMS
13 
14 # pragma message disable DOLLARID
15 # include <errno.h>
16 # include <rms.h>
17 # include <lib$routines.h>
18 # include <libfisdef.h>
19 # include <stsdef.h>
20 # include <descrip.h>
21 # include <starlet.h>
22 # include "../vms_rms.h"
23 
24 /* Some compiler options may mask the declaration of "_malloc32". */
25 # define DSO_MALLOC OPENSSL_malloc
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
30 void *_malloc32(__size_t);
dso_malloc(__size_t num,const char * file,int line)31 static 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 }
41 #   undef DSO_MALLOC
42 #   define DSO_MALLOC(num) dso_malloc((num), OPENSSL_FILE, OPENSSL_LINE)
43 #   pragma pointer_size restore
44 #  endif                        /* __INITIAL_POINTER_SIZE == 64 [else] */
45 # endif                         /* __INITIAL_POINTER_SIZE && defined
46                                  * _ANSI_C_SOURCE */
47 
48 # pragma message disable DOLLARID
49 
50 static int vms_load(DSO *dso);
51 static int vms_unload(DSO *dso);
52 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
53 static char *vms_name_converter(DSO *dso, const char *filename);
54 static char *vms_merger(DSO *dso, const char *filespec1,
55                         const char *filespec2);
56 
57 static DSO_METHOD dso_meth_vms = {
58     "OpenSSL 'VMS' shared library method",
59     vms_load,
60     NULL,                       /* unload */
61     vms_bind_func,
62     NULL,                       /* ctrl */
63     vms_name_converter,
64     vms_merger,
65     NULL,                       /* init */
66     NULL,                       /* finish */
67     NULL,                       /* pathbyaddr */
68     NULL                        /* globallookup */
69 };
70 
71 /*
72  * On VMS, the only "handle" is the file name.  LIB$FIND_IMAGE_SYMBOL depends
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  */
77 typedef 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;
91 
DSO_METHOD_openssl(void)92 DSO_METHOD *DSO_METHOD_openssl(void)
93 {
94     return &dso_meth_vms;
95 }
96 
vms_load(DSO * dso)97 static int vms_load(DSO *dso)
98 {
99     void *ptr = NULL;
100     /* See applicable comments in dso_dl.c */
101     char *filename = DSO_convert_filename(dso, NULL);
102 
103 /* Ensure 32-bit pointer for "p", and appropriate malloc() function. */
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 */
109 # endif                         /* __INITIAL_POINTER_SIZE && defined
110                                  * _ANSI_C_SOURCE */
111 
112     DSO_VMS_INTERNAL *p = NULL;
113 
114 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
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 */
120 
121     const char *sp1, *sp2;      /* Search result */
122     const char *ext = NULL;     /* possible extension to add */
123 
124     if (filename == NULL) {
125         ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
126         goto err;
127     }
128 
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      */
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, '.');
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";
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      */
174     if (sp2 == NULL) {
175         sp2 = strchr(sp1, ';');
176         ext = ".EXE";
177     }
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) {
188         ERR_raise(ERR_LIB_DSO, DSO_R_FILENAME_TOO_BIG);
189         goto err;
190     }
191 
192     p = DSO_MALLOC(sizeof(*p));
193     if (p == NULL)
194         goto err;
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';
201     if (ext) {
202         strcat(p->imagename, ext);
203         if (*sp2 == '.')
204             sp2++;
205     }
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)) {
218         ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
219         goto err;
220     }
221 
222     /* Success (for now, we lie.  We actually do not know...) */
223     dso->loaded_filename = filename;
224     return 1;
225  err:
226     /* Cleanup! */
227     OPENSSL_free(p);
228     OPENSSL_free(filename);
229     return 0;
230 }
231 
232 /*
233  * Note that this doesn't actually unload the shared image, as there is no
234  * such thing in VMS.  Next time it get loaded again, a new copy will
235  * actually be loaded.
236  */
vms_unload(DSO * dso)237 static int vms_unload(DSO *dso)
238 {
239     DSO_VMS_INTERNAL *p;
240     if (dso == NULL) {
241         ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
242         return 0;
243     }
244     if (sk_void_num(dso->meth_data) < 1)
245         return 1;
246     p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
247     if (p == NULL) {
248         ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
249         return 0;
250     }
251     /* Cleanup */
252     OPENSSL_free(p);
253     return 1;
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  */
do_find_symbol(DSO_VMS_INTERNAL * ptr,struct dsc$descriptor_s * symname_dsc,void ** sym,unsigned long flags)260 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
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 }
279 
280 # ifndef LIB$M_FIS_MIXEDCASE
281 #  define LIB$M_FIS_MIXEDCASE (1 << 4);
282 # endif
vms_bind_sym(DSO * dso,const char * symname,void ** sym)283 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
284 {
285     DSO_VMS_INTERNAL *ptr;
286     int status = 0;
287     struct dsc$descriptor_s symname_dsc;
288 
289 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
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)) {
304         ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
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) {
319         ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
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) {
325         ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
326         return;
327     }
328 
329     status = do_find_symbol(ptr, &symname_dsc, sym, LIB$M_FIS_MIXEDCASE);
330 
331     if (!$VMS_STATUS_SUCCESS(status))
332         status = do_find_symbol(ptr, &symname_dsc, sym, 0);
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 
353             if (ptr->imagename_dsc.dsc$w_length)
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);
358             else
359                 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
360                                "Symbol %s in %s: %s",
361                                symname, ptr->filename, errstring);
362         }
363         return;
364     }
365     return;
366 }
367 
vms_bind_func(DSO * dso,const char * symname)368 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
369 {
370     DSO_FUNC_TYPE sym = 0;
371     vms_bind_sym(dso, symname, (void **)&sym);
372     return sym;
373 }
374 
vms_merger(DSO * dso,const char * filespec1,const char * filespec2)375 static 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;
384 
385 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
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 
451             ERR_raise_data(ERR_LIB_DSO, DSO_R_FAILURE,
452                            "filespec \"%s\", default \"%s\": %s",
453                            filespec1, filespec2, errstring);
454         }
455         return NULL;
456     }
457 
458     merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
459     if (merged == NULL)
460         return NULL;
461     strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
462     merged[nam.NAMX_ESL] = '\0';
463     return merged;
464 }
465 
vms_name_converter(DSO * dso,const char * filename)466 static char *vms_name_converter(DSO *dso, const char *filename)
467 {
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;
499 }
500 
501 #endif                          /* OPENSSL_SYS_VMS */
502