Stop raising ERR_R_MALLOC_FAILURE in most places
[openssl.git] / crypto / dso / dso_vms.c
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 # 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 static void *dso_malloc(__size_t num, const char *file, int line)
31 {
32     void *ret = _malloc32(num);
33     if (ret == NULL && (file != NULL || line != 0)) {
34         ERR_new();
35         ERR_set_debug(file, line, NULL);
36         ERR_set_error(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE, NULL);
37     }
38     return ret;
39 }
40 #   define DSO_MALLOC(num) dso_malloc((num), OPENSSL_FILE, OPENSSL_LINE)
41 #   pragma pointer_size restore
42 #  else                         /* __INITIAL_POINTER_SIZE == 64 */
43 #   define DSO_MALLOC OPENSSL_malloc
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
92 DSO_METHOD *DSO_METHOD_openssl(void)
93 {
94     return &dso_meth_vms;
95 }
96
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
110     DSO_VMS_INTERNAL *p = NULL;
111
112 #  if __INITIAL_POINTER_SIZE == 64
113 #   pragma pointer_size restore
114 #  endif                        /* __INITIAL_POINTER_SIZE == 64 */
115 # endif                         /* __INITIAL_POINTER_SIZE && defined
116                                  * _ANSI_C_SOURCE */
117
118     const char *sp1, *sp2;      /* Search result */
119     const char *ext = NULL;     /* possible extension to add */
120
121     if (filename == NULL) {
122         ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
123         goto err;
124     }
125
126     /*-
127      * A file specification may look like this:
128      *
129      *      node::dev:[dir-spec]name.type;ver
130      *
131      * or (for compatibility with TOPS-20):
132      *
133      *      node::dev:<dir-spec>name.type;ver
134      *
135      * and the dir-spec uses '.' as separator.  Also, a dir-spec
136      * may consist of several parts, with mixed use of [] and <>:
137      *
138      *      [dir1.]<dir2>
139      *
140      * We need to split the file specification into the name and
141      * the rest (both before and after the name itself).
142      */
143     /*
144      * Start with trying to find the end of a dir-spec, and save the position
145      * of the byte after in sp1
146      */
147     sp1 = strrchr(filename, ']');
148     sp2 = strrchr(filename, '>');
149     if (sp1 == NULL)
150         sp1 = sp2;
151     if (sp2 != NULL && sp2 > sp1)
152         sp1 = sp2;
153     if (sp1 == NULL)
154         sp1 = strrchr(filename, ':');
155     if (sp1 == NULL)
156         sp1 = filename;
157     else
158         sp1++;                  /* The byte after the found character */
159     /* Now, let's see if there's a type, and save the position in sp2 */
160     sp2 = strchr(sp1, '.');
161     /*
162      * If there is a period and the next character is a semi-colon,
163      * we need to add an extension
164      */
165     if (sp2 != NULL && sp2[1] == ';')
166         ext = ".EXE";
167     /*
168      * If we found it, that's where we'll cut.  Otherwise, look for a version
169      * number and save the position in sp2
170      */
171     if (sp2 == NULL) {
172         sp2 = strchr(sp1, ';');
173         ext = ".EXE";
174     }
175     /*
176      * If there was still nothing to find, set sp2 to point at the end of the
177      * string
178      */
179     if (sp2 == NULL)
180         sp2 = sp1 + strlen(sp1);
181
182     /* Check that we won't get buffer overflows */
183     if (sp2 - sp1 > FILENAME_MAX
184         || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
185         ERR_raise(ERR_LIB_DSO, DSO_R_FILENAME_TOO_BIG);
186         goto err;
187     }
188
189     p = DSO_MALLOC(sizeof(*p));
190     if (p == NULL)
191         goto err;
192
193     strncpy(p->filename, sp1, sp2 - sp1);
194     p->filename[sp2 - sp1] = '\0';
195
196     strncpy(p->imagename, filename, sp1 - filename);
197     p->imagename[sp1 - filename] = '\0';
198     if (ext) {
199         strcat(p->imagename, ext);
200         if (*sp2 == '.')
201             sp2++;
202     }
203     strcat(p->imagename, sp2);
204
205     p->filename_dsc.dsc$w_length = strlen(p->filename);
206     p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
207     p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
208     p->filename_dsc.dsc$a_pointer = p->filename;
209     p->imagename_dsc.dsc$w_length = strlen(p->imagename);
210     p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
211     p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
212     p->imagename_dsc.dsc$a_pointer = p->imagename;
213
214     if (!sk_void_push(dso->meth_data, (char *)p)) {
215         ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
216         goto err;
217     }
218
219     /* Success (for now, we lie.  We actually do not know...) */
220     dso->loaded_filename = filename;
221     return 1;
222  err:
223     /* Cleanup! */
224     OPENSSL_free(p);
225     OPENSSL_free(filename);
226     return 0;
227 }
228
229 /*
230  * Note that this doesn't actually unload the shared image, as there is no
231  * such thing in VMS.  Next time it get loaded again, a new copy will
232  * actually be loaded.
233  */
234 static int vms_unload(DSO *dso)
235 {
236     DSO_VMS_INTERNAL *p;
237     if (dso == NULL) {
238         ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
239         return 0;
240     }
241     if (sk_void_num(dso->meth_data) < 1)
242         return 1;
243     p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
244     if (p == NULL) {
245         ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
246         return 0;
247     }
248     /* Cleanup */
249     OPENSSL_free(p);
250     return 1;
251 }
252
253 /*
254  * We must do this in a separate function because of the way the exception
255  * handler works (it makes this function return
256  */
257 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
258                           struct dsc$descriptor_s *symname_dsc, void **sym,
259                           unsigned long flags)
260 {
261     /*
262      * Make sure that signals are caught and returned instead of aborting the
263      * program.  The exception handler gets unestablished automatically on
264      * return from this function.
265      */
266     lib$establish(lib$sig_to_ret);
267
268     if (ptr->imagename_dsc.dsc$w_length)
269         return lib$find_image_symbol(&ptr->filename_dsc,
270                                      symname_dsc, sym,
271                                      &ptr->imagename_dsc, flags);
272     else
273         return lib$find_image_symbol(&ptr->filename_dsc,
274                                      symname_dsc, sym, 0, flags);
275 }
276
277 # ifndef LIB$M_FIS_MIXEDCASE
278 #  define LIB$M_FIS_MIXEDCASE (1 << 4);
279 # endif
280 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
281 {
282     DSO_VMS_INTERNAL *ptr;
283     int status = 0;
284     struct dsc$descriptor_s symname_dsc;
285
286 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
287 # if __INITIAL_POINTER_SIZE == 64
288 #  define SYMNAME symname_32p
289 #  pragma pointer_size save
290 #  pragma pointer_size 32
291     char *symname_32p;
292 #  pragma pointer_size restore
293     char symname_32[NAMX_MAXRSS + 1];
294 # else                          /* __INITIAL_POINTER_SIZE == 64 */
295 #  define SYMNAME ((char *) symname)
296 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
297
298     *sym = NULL;
299
300     if ((dso == NULL) || (symname == NULL)) {
301         ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
302         return;
303     }
304 # if __INITIAL_POINTER_SIZE == 64
305     /* Copy the symbol name to storage with a 32-bit pointer. */
306     symname_32p = symname_32;
307     strcpy(symname_32p, symname);
308 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
309
310     symname_dsc.dsc$w_length = strlen(SYMNAME);
311     symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
312     symname_dsc.dsc$b_class = DSC$K_CLASS_S;
313     symname_dsc.dsc$a_pointer = SYMNAME;
314
315     if (sk_void_num(dso->meth_data) < 1) {
316         ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
317         return;
318     }
319     ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
320                                             sk_void_num(dso->meth_data) - 1);
321     if (ptr == NULL) {
322         ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
323         return;
324     }
325
326     status = do_find_symbol(ptr, &symname_dsc, sym, LIB$M_FIS_MIXEDCASE);
327
328     if (!$VMS_STATUS_SUCCESS(status))
329         status = do_find_symbol(ptr, &symname_dsc, sym, 0);
330
331     if (!$VMS_STATUS_SUCCESS(status)) {
332         unsigned short length;
333         char errstring[257];
334         struct dsc$descriptor_s errstring_dsc;
335
336         errstring_dsc.dsc$w_length = sizeof(errstring);
337         errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
338         errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
339         errstring_dsc.dsc$a_pointer = errstring;
340
341         *sym = NULL;
342
343         status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
344
345         if (!$VMS_STATUS_SUCCESS(status))
346             lib$signal(status); /* This is really bad.  Abort! */
347         else {
348             errstring[length] = '\0';
349
350             if (ptr->imagename_dsc.dsc$w_length)
351                 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
352                                "Symbol %s in %s (%s): %s",
353                                symname, ptr->filename, ptr->imagename,
354                                errstring);
355             else
356                 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
357                                "Symbol %s in %s: %s",
358                                symname, ptr->filename, errstring);
359         }
360         return;
361     }
362     return;
363 }
364
365 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
366 {
367     DSO_FUNC_TYPE sym = 0;
368     vms_bind_sym(dso, symname, (void **)&sym);
369     return sym;
370 }
371
372 static char *vms_merger(DSO *dso, const char *filespec1,
373                         const char *filespec2)
374 {
375     int status;
376     int filespec1len, filespec2len;
377     struct FAB fab;
378     struct NAMX_STRUCT nam;
379     char esa[NAMX_MAXRSS + 1];
380     char *merged;
381
382 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
383 # if __INITIAL_POINTER_SIZE == 64
384 #  define FILESPEC1 filespec1_32p;
385 #  define FILESPEC2 filespec2_32p;
386 #  pragma pointer_size save
387 #  pragma pointer_size 32
388     char *filespec1_32p;
389     char *filespec2_32p;
390 #  pragma pointer_size restore
391     char filespec1_32[NAMX_MAXRSS + 1];
392     char filespec2_32[NAMX_MAXRSS + 1];
393 # else                          /* __INITIAL_POINTER_SIZE == 64 */
394 #  define FILESPEC1 ((char *) filespec1)
395 #  define FILESPEC2 ((char *) filespec2)
396 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
397
398     if (!filespec1)
399         filespec1 = "";
400     if (!filespec2)
401         filespec2 = "";
402     filespec1len = strlen(filespec1);
403     filespec2len = strlen(filespec2);
404
405 # if __INITIAL_POINTER_SIZE == 64
406     /* Copy the file names to storage with a 32-bit pointer. */
407     filespec1_32p = filespec1_32;
408     filespec2_32p = filespec2_32;
409     strcpy(filespec1_32p, filespec1);
410     strcpy(filespec2_32p, filespec2);
411 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
412
413     fab = cc$rms_fab;
414     nam = CC_RMS_NAMX;
415
416     FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
417     FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNS = filespec1len;
418     FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
419     FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNS = filespec2len;
420     NAMX_DNA_FNA_SET(fab)
421
422         nam.NAMX_ESA = esa;
423     nam.NAMX_ESS = NAMX_MAXRSS;
424     nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
425     SET_NAMX_NO_SHORT_UPCASE(nam);
426
427     fab.FAB_NAMX = &nam;
428
429     status = sys$parse(&fab, 0, 0);
430
431     if (!$VMS_STATUS_SUCCESS(status)) {
432         unsigned short length;
433         char errstring[257];
434         struct dsc$descriptor_s errstring_dsc;
435
436         errstring_dsc.dsc$w_length = sizeof(errstring);
437         errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
438         errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
439         errstring_dsc.dsc$a_pointer = errstring;
440
441         status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
442
443         if (!$VMS_STATUS_SUCCESS(status))
444             lib$signal(status); /* This is really bad.  Abort! */
445         else {
446             errstring[length] = '\0';
447
448             ERR_raise_data(ERR_LIB_DSO, DSO_R_FAILURE,
449                            "filespec \"%s\", default \"%s\": %s",
450                            filespec1, filespec2, errstring);
451         }
452         return NULL;
453     }
454
455     merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
456     if (merged == NULL)
457         return NULL;
458     strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
459     merged[nam.NAMX_ESL] = '\0';
460     return merged;
461 }
462
463 static char *vms_name_converter(DSO *dso, const char *filename)
464 {
465     char *translated;
466     int len, transform;
467     const char *p;
468
469     len = strlen(filename);
470
471     p = strchr(filename, ':');
472     if (p != NULL) {
473         transform = 0;
474     } else {
475         p = filename;
476         transform = (strrchr(p, '>') == NULL && strrchr(p, ']') == NULL);
477     }
478
479     if (transform) {
480         int rsize = len + sizeof(DSO_EXTENSION);
481
482         if ((translated = OPENSSL_malloc(rsize)) != NULL) {
483             p = strrchr(filename, ';');
484             if (p != NULL)
485                 len = p - filename;
486             strncpy(translated, filename, len);
487             translated[len] = '\0';
488             strcat(translated, DSO_EXTENSION);
489             if (p != NULL)
490                 strcat(translated, p);
491         }
492     } else {
493         translated = OPENSSL_strdup(filename);
494     }
495     return translated;
496 }
497
498 #endif                          /* OPENSSL_SYS_VMS */