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