14d885df15cd91e5ab76693eb6677eacbf3595ee
[openssl.git] / crypto / dso / dso_vms.c
1 /* dso_vms.c -*- mode:C; c-file-style: "eay" -*- */
2 /*
3  * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
4  * 2000.
5  */
6 /* ====================================================================
7  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <stdio.h>
61 #include <string.h>
62 #include <errno.h>
63 #include "cryptlib.h"
64 #include <openssl/dso.h>
65
66 #ifndef OPENSSL_SYS_VMS
67 DSO_METHOD *DSO_METHOD_vms(void)
68 {
69     return NULL;
70 }
71 #else
72
73 # pragma message disable DOLLARID
74 # include <rms.h>
75 # include <lib$routines.h>
76 # include <stsdef.h>
77 # include <descrip.h>
78 # include <starlet.h>
79 # include "vms_rms.h"
80
81 /* Some compiler options may mask the declaration of "_malloc32". */
82 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
83 #  if __INITIAL_POINTER_SIZE == 64
84 #   pragma pointer_size save
85 #   pragma pointer_size 32
86 void *_malloc32(__size_t);
87 #   pragma pointer_size restore
88 #  endif                        /* __INITIAL_POINTER_SIZE == 64 */
89 # endif                         /* __INITIAL_POINTER_SIZE && defined
90                                  * _ANSI_C_SOURCE */
91
92 # pragma message disable DOLLARID
93
94 static int vms_load(DSO *dso);
95 static int vms_unload(DSO *dso);
96 static void *vms_bind_var(DSO *dso, const char *symname);
97 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
98 # if 0
99 static int vms_unbind_var(DSO *dso, char *symname, void *symptr);
100 static int vms_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
101 static int vms_init(DSO *dso);
102 static int vms_finish(DSO *dso);
103 static long vms_ctrl(DSO *dso, int cmd, long larg, void *parg);
104 # endif
105 static char *vms_name_converter(DSO *dso, const char *filename);
106 static char *vms_merger(DSO *dso, const char *filespec1,
107                         const char *filespec2);
108
109 static DSO_METHOD dso_meth_vms = {
110     "OpenSSL 'VMS' shared library method",
111     vms_load,
112     NULL,                       /* unload */
113     vms_bind_var,
114     vms_bind_func,
115 /* For now, "unbind" doesn't exist */
116 # if 0
117     NULL,                       /* unbind_var */
118     NULL,                       /* unbind_func */
119 # endif
120     NULL,                       /* ctrl */
121     vms_name_converter,
122     vms_merger,
123     NULL,                       /* init */
124     NULL                        /* finish */
125 };
126
127 /*
128  * On VMS, the only "handle" is the file name.  LIB$FIND_IMAGE_SYMBOL depends
129  * on the reference to the file name being the same for all calls regarding
130  * one shared image, so we'll just store it in an instance of the following
131  * structure and put a pointer to that instance in the meth_data stack.
132  */
133 typedef struct dso_internal_st {
134     /*
135      * This should contain the name only, no directory, no extension, nothing
136      * but a name.
137      */
138     struct dsc$descriptor_s filename_dsc;
139     char filename[NAMX_MAXRSS + 1];
140     /*
141      * This contains whatever is not in filename, if needed. Normally not
142      * defined.
143      */
144     struct dsc$descriptor_s imagename_dsc;
145     char imagename[NAMX_MAXRSS + 1];
146 } DSO_VMS_INTERNAL;
147
148 DSO_METHOD *DSO_METHOD_vms(void)
149 {
150     return (&dso_meth_vms);
151 }
152
153 static int vms_load(DSO *dso)
154 {
155     void *ptr = NULL;
156     /* See applicable comments in dso_dl.c */
157     char *filename = DSO_convert_filename(dso, NULL);
158
159 /* Ensure 32-bit pointer for "p", and appropriate malloc() function. */
160 # if __INITIAL_POINTER_SIZE == 64
161 #  define DSO_MALLOC _malloc32
162 #  pragma pointer_size save
163 #  pragma pointer_size 32
164 # else                          /* __INITIAL_POINTER_SIZE == 64 */
165 #  define DSO_MALLOC OPENSSL_malloc
166 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
167
168     DSO_VMS_INTERNAL *p = NULL;
169
170 # if __INITIAL_POINTER_SIZE == 64
171 #  pragma pointer_size restore
172 # endif                         /* __INITIAL_POINTER_SIZE == 64 */
173
174     const char *sp1, *sp2;      /* Search result */
175
176     if (filename == NULL) {
177         DSOerr(DSO_F_VMS_LOAD, DSO_R_NO_FILENAME);
178         goto err;
179     }
180
181     /*-
182      * A file specification may look like this:
183      *
184      *      node::dev:[dir-spec]name.type;ver
185      *
186      * or (for compatibility with TOPS-20):
187      *
188      *      node::dev:<dir-spec>name.type;ver
189      *
190      * and the dir-spec uses '.' as separator.  Also, a dir-spec
191      * may consist of several parts, with mixed use of [] and <>:
192      *
193      *      [dir1.]<dir2>
194      *
195      * We need to split the file specification into the name and
196      * the rest (both before and after the name itself).
197      */
198     /*
199      * Start with trying to find the end of a dir-spec, and save the position
200      * of the byte after in sp1
201      */
202     sp1 = strrchr(filename, ']');
203     sp2 = strrchr(filename, '>');
204     if (sp1 == NULL)
205         sp1 = sp2;
206     if (sp2 != NULL && sp2 > sp1)
207         sp1 = sp2;
208     if (sp1 == NULL)
209         sp1 = strrchr(filename, ':');
210     if (sp1 == NULL)
211         sp1 = filename;
212     else
213         sp1++;                  /* The byte after the found character */
214     /* Now, let's see if there's a type, and save the position in sp2 */
215     sp2 = strchr(sp1, '.');
216     /*
217      * If we found it, that's where we'll cut.  Otherwise, look for a version
218      * number and save the position in sp2
219      */
220     if (sp2 == NULL)
221         sp2 = strchr(sp1, ';');
222     /*
223      * If there was still nothing to find, set sp2 to point at the end of the
224      * string
225      */
226     if (sp2 == NULL)
227         sp2 = sp1 + strlen(sp1);
228
229     /* Check that we won't get buffer overflows */
230     if (sp2 - sp1 > FILENAME_MAX
231         || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
232         DSOerr(DSO_F_VMS_LOAD, DSO_R_FILENAME_TOO_BIG);
233         goto err;
234     }
235
236     p = DSO_MALLOC(sizeof(DSO_VMS_INTERNAL));
237     if (p == NULL) {
238         DSOerr(DSO_F_VMS_LOAD, ERR_R_MALLOC_FAILURE);
239         goto err;
240     }
241
242     strncpy(p->filename, sp1, sp2 - sp1);
243     p->filename[sp2 - sp1] = '\0';
244
245     strncpy(p->imagename, filename, sp1 - filename);
246     p->imagename[sp1 - filename] = '\0';
247     strcat(p->imagename, sp2);
248
249     p->filename_dsc.dsc$w_length = strlen(p->filename);
250     p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
251     p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
252     p->filename_dsc.dsc$a_pointer = p->filename;
253     p->imagename_dsc.dsc$w_length = strlen(p->imagename);
254     p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
255     p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
256     p->imagename_dsc.dsc$a_pointer = p->imagename;
257
258     if (!sk_void_push(dso->meth_data, (char *)p)) {
259         DSOerr(DSO_F_VMS_LOAD, DSO_R_STACK_ERROR);
260         goto err;
261     }
262
263     /* Success (for now, we lie.  We actually do not know...) */
264     dso->loaded_filename = filename;
265     return (1);
266  err:
267     /* Cleanup! */
268     if (p != NULL)
269         OPENSSL_free(p);
270     if (filename != NULL)
271         OPENSSL_free(filename);
272     return (0);
273 }
274
275 /*
276  * Note that this doesn't actually unload the shared image, as there is no
277  * such thing in VMS.  Next time it get loaded again, a new copy will
278  * actually be loaded.
279  */
280 static int vms_unload(DSO *dso)
281 {
282     DSO_VMS_INTERNAL *p;
283     if (dso == NULL) {
284         DSOerr(DSO_F_VMS_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
285         return (0);
286     }
287     if (sk_void_num(dso->meth_data) < 1)
288         return (1);
289     p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
290     if (p == NULL) {
291         DSOerr(DSO_F_VMS_UNLOAD, DSO_R_NULL_HANDLE);
292         return (0);
293     }
294     /* Cleanup */
295     OPENSSL_free(p);
296     return (1);
297 }
298
299 /*
300  * We must do this in a separate function because of the way the exception
301  * handler works (it makes this function return
302  */
303 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
304                           struct dsc$descriptor_s *symname_dsc, void **sym,
305                           unsigned long flags)
306 {
307     /*
308      * Make sure that signals are caught and returned instead of aborting the
309      * program.  The exception handler gets unestablished automatically on
310      * return from this function.
311      */
312     lib$establish(lib$sig_to_ret);
313
314     if (ptr->imagename_dsc.dsc$w_length)
315         return lib$find_image_symbol(&ptr->filename_dsc,
316                                      symname_dsc, sym,
317                                      &ptr->imagename_dsc, flags);
318     else
319         return lib$find_image_symbol(&ptr->filename_dsc,
320                                      symname_dsc, sym, 0, flags);
321 }
322
323 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
324 {
325     DSO_VMS_INTERNAL *ptr;
326     int status;
327 # if 0
328     int flags = (1 << 4);       /* LIB$M_FIS_MIXEDCASE, but this symbol isn't
329                                  * defined in VMS older than 7.0 or so */
330 # else
331     int flags = 0;
332 # endif
333     struct dsc$descriptor_s symname_dsc;
334
335 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
336 # if __INITIAL_POINTER_SIZE == 64
337 #  define SYMNAME symname_32p
338 #  pragma pointer_size save
339 #  pragma pointer_size 32
340     char *symname_32p;
341 #  pragma pointer_size restore
342     char symname_32[NAMX_MAXRSS + 1];
343 # else                          /* __INITIAL_POINTER_SIZE == 64 */
344 #  define SYMNAME ((char *) symname)
345 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
346
347     *sym = NULL;
348
349     if ((dso == NULL) || (symname == NULL)) {
350         DSOerr(DSO_F_VMS_BIND_SYM, ERR_R_PASSED_NULL_PARAMETER);
351         return;
352     }
353 # if __INITIAL_POINTER_SIZE == 64
354     /* Copy the symbol name to storage with a 32-bit pointer. */
355     symname_32p = symname_32;
356     strcpy(symname_32p, symname);
357 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
358
359     symname_dsc.dsc$w_length = strlen(SYMNAME);
360     symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
361     symname_dsc.dsc$b_class = DSC$K_CLASS_S;
362     symname_dsc.dsc$a_pointer = SYMNAME;
363
364     if (sk_void_num(dso->meth_data) < 1) {
365         DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_STACK_ERROR);
366         return;
367     }
368     ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
369                                             sk_void_num(dso->meth_data) - 1);
370     if (ptr == NULL) {
371         DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_NULL_HANDLE);
372         return;
373     }
374
375     if (dso->flags & DSO_FLAG_UPCASE_SYMBOL)
376         flags = 0;
377
378     status = do_find_symbol(ptr, &symname_dsc, sym, flags);
379
380     if (!$VMS_STATUS_SUCCESS(status)) {
381         unsigned short length;
382         char errstring[257];
383         struct dsc$descriptor_s errstring_dsc;
384
385         errstring_dsc.dsc$w_length = sizeof(errstring);
386         errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
387         errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
388         errstring_dsc.dsc$a_pointer = errstring;
389
390         *sym = NULL;
391
392         status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
393
394         if (!$VMS_STATUS_SUCCESS(status))
395             lib$signal(status); /* This is really bad.  Abort! */
396         else {
397             errstring[length] = '\0';
398
399             DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_SYM_FAILURE);
400             if (ptr->imagename_dsc.dsc$w_length)
401                 ERR_add_error_data(9,
402                                    "Symbol ", symname,
403                                    " in ", ptr->filename,
404                                    " (", ptr->imagename, ")",
405                                    ": ", errstring);
406             else
407                 ERR_add_error_data(6,
408                                    "Symbol ", symname,
409                                    " in ", ptr->filename, ": ", errstring);
410         }
411         return;
412     }
413     return;
414 }
415
416 static void *vms_bind_var(DSO *dso, const char *symname)
417 {
418     void *sym = 0;
419     vms_bind_sym(dso, symname, &sym);
420     return sym;
421 }
422
423 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
424 {
425     DSO_FUNC_TYPE sym = 0;
426     vms_bind_sym(dso, symname, (void **)&sym);
427     return sym;
428 }
429
430 static char *vms_merger(DSO *dso, const char *filespec1,
431                         const char *filespec2)
432 {
433     int status;
434     int filespec1len, filespec2len;
435     struct FAB fab;
436     struct NAMX_STRUCT nam;
437     char esa[NAMX_MAXRSS + 1];
438     char *merged;
439
440 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
441 # if __INITIAL_POINTER_SIZE == 64
442 #  define FILESPEC1 filespec1_32p;
443 #  define FILESPEC2 filespec2_32p;
444 #  pragma pointer_size save
445 #  pragma pointer_size 32
446     char *filespec1_32p;
447     char *filespec2_32p;
448 #  pragma pointer_size restore
449     char filespec1_32[NAMX_MAXRSS + 1];
450     char filespec2_32[NAMX_MAXRSS + 1];
451 # else                          /* __INITIAL_POINTER_SIZE == 64 */
452 #  define FILESPEC1 ((char *) filespec1)
453 #  define FILESPEC2 ((char *) filespec2)
454 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
455
456     if (!filespec1)
457         filespec1 = "";
458     if (!filespec2)
459         filespec2 = "";
460     filespec1len = strlen(filespec1);
461     filespec2len = strlen(filespec2);
462
463 # if __INITIAL_POINTER_SIZE == 64
464     /* Copy the file names to storage with a 32-bit pointer. */
465     filespec1_32p = filespec1_32;
466     filespec2_32p = filespec2_32;
467     strcpy(filespec1_32p, filespec1);
468     strcpy(filespec2_32p, filespec2);
469 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
470
471     fab = cc$rms_fab;
472     nam = CC_RMS_NAMX;
473
474     FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
475     FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNS = filespec1len;
476     FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
477     FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNS = filespec2len;
478     NAMX_DNA_FNA_SET(fab)
479
480         nam.NAMX_ESA = esa;
481     nam.NAMX_ESS = NAMX_MAXRSS;
482     nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
483     SET_NAMX_NO_SHORT_UPCASE(nam);
484
485     fab.FAB_NAMX = &nam;
486
487     status = sys$parse(&fab, 0, 0);
488
489     if (!$VMS_STATUS_SUCCESS(status)) {
490         unsigned short length;
491         char errstring[257];
492         struct dsc$descriptor_s errstring_dsc;
493
494         errstring_dsc.dsc$w_length = sizeof(errstring);
495         errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
496         errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
497         errstring_dsc.dsc$a_pointer = errstring;
498
499         status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
500
501         if (!$VMS_STATUS_SUCCESS(status))
502             lib$signal(status); /* This is really bad.  Abort! */
503         else {
504             errstring[length] = '\0';
505
506             DSOerr(DSO_F_VMS_MERGER, DSO_R_FAILURE);
507             ERR_add_error_data(7,
508                                "filespec \"", filespec1, "\", ",
509                                "defaults \"", filespec2, "\": ", errstring);
510         }
511         return (NULL);
512     }
513
514     merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
515     if (!merged)
516         goto malloc_err;
517     strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
518     merged[nam.NAMX_ESL] = '\0';
519     return (merged);
520  malloc_err:
521     DSOerr(DSO_F_VMS_MERGER, ERR_R_MALLOC_FAILURE);
522 }
523
524 static char *vms_name_converter(DSO *dso, const char *filename)
525 {
526     int len = strlen(filename);
527     char *not_translated = OPENSSL_malloc(len + 1);
528     strcpy(not_translated, filename);
529     return (not_translated);
530 }
531
532 #endif                          /* OPENSSL_SYS_VMS */