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