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