Make the DSO code for VMS work again. First attempt.
[openssl.git] / crypto / dso / dso_vms.c
1 /* dso_vms.c */
2 /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3  * project 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 #ifdef VMS
63 #pragma message disable DOLLARID
64 #include <lib$routines.h>
65 #include <libfisdef.h>
66 #include <stsdef.h>
67 #include <descrip.h>
68 #include <starlet.h>
69 #endif
70 #include "cryptlib.h"
71 #include <openssl/dso.h>
72
73 #ifndef VMS
74 DSO_METHOD *DSO_METHOD_vms(void)
75         {
76         return NULL;
77         }
78 #else
79 #pragma message disable DOLLARID
80
81 static int vms_load(DSO *dso);
82 static int vms_unload(DSO *dso);
83 static void *vms_bind_var(DSO *dso, const char *symname);
84 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
85 #if 0
86 static int vms_unbind_var(DSO *dso, char *symname, void *symptr);
87 static int vms_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
88 static int vms_init(DSO *dso);
89 static int vms_finish(DSO *dso);
90 static long vms_ctrl(DSO *dso, int cmd, long larg, void *parg);
91 #endif
92 static char *vms_name_converter(DSO *dso);
93
94 static DSO_METHOD dso_meth_vms = {
95         "OpenSSL 'VMS' shared library method",
96         vms_load,
97         NULL, /* unload */
98         vms_bind_var,
99         vms_bind_func,
100 /* For now, "unbind" doesn't exist */
101 #if 0
102         NULL, /* unbind_var */
103         NULL, /* unbind_func */
104 #endif
105         NULL, /* ctrl */
106         vms_name_converter,
107         NULL, /* init */
108         NULL  /* finish */
109         };
110
111 /* On VMS, the only "handle" is the file name.  LIB$FIND_IMAGE_SYMBOL depends
112  * on the reference to the file name being the same for all calls regarding
113  * one shared image, so we'll just store it in an instance of the following
114  * structure and put a pointer to that instance in the meth_data stack.
115  */
116 typedef struct dso_internal_st
117         {
118         /* This should contain the name only, no directory,
119          * no extension, nothing but a name. */
120         struct dsc$descriptor_s filename_dsc;
121         char filename[FILENAME_MAX+1];
122         /* This contains whatever is not in filename, if needed.
123          * Normally not defined. */
124         struct dsc$descriptor_s imagename_dsc;
125         char imagename[FILENAME_MAX+1];
126         } DSO_VMS_INTERNAL;
127
128
129 DSO_METHOD *DSO_METHOD_vms(void)
130         {
131         return(&dso_meth_vms);
132         }
133
134 static int vms_load(DSO *dso)
135         {
136         void *ptr = NULL;
137         /* See applicable comments in dso_dl.c */
138         char *filename = DSO_convert_filename(dso, NULL);
139         DSO_VMS_INTERNAL *p;
140         const char *sp1, *sp2;  /* Search result */
141
142         if(filename == NULL)
143                 {
144                 DSOerr(DSO_F_DLFCN_LOAD,DSO_R_NO_FILENAME);
145                 goto err;
146                 }
147
148         /* A file specification may look like this:
149          *
150          *      node::dev:[dir-spec]name.type;ver
151          *
152          * or (for compatibility with TOPS-20):
153          *
154          *      node::dev:<dir-spec>name.type;ver
155          *
156          * and the dir-spec uses '.' as separator.  Also, a dir-spec
157          * may consist of several parts, with mixed use of [] and <>:
158          *
159          *      [dir1.]<dir2>
160          *
161          * We need to split the file specification into the name and
162          * the rest (both before and after the name itself).
163          */
164         /* Start with trying to find the end of a dir-spec, and save the
165            position of the byte after in sp1 */
166         sp1 = strrchr(filename, ']');
167         sp2 = strrchr(filename, '>');
168         if (sp1 == NULL) sp1 = sp2;
169         if (sp2 != NULL && sp2 > sp1) sp1 = sp2;
170         if (sp1 == NULL) sp1 = strrchr(filename, ':');
171         if (sp1 == NULL)
172                 sp1 = filename;
173         else
174                 sp1++;          /* The byte after the found character */
175         /* Now, let's see if there's a type, and save the position in sp2 */
176         sp2 = strchr(sp1, '.');
177         /* If we found it, that's where we'll cut.  Otherwise, look for a
178            version number and save the position in sp2 */
179         if (sp2 == NULL) sp2 = strchr(sp1, ';');
180         /* If there was still nothing to find, set sp2 to point at the end of
181            the string */
182         if (sp2 == NULL) sp2 = sp1 + strlen(sp1);
183
184         /* Check that we won't get buffer overflows */
185         if (sp2 - sp1 > FILENAME_MAX
186                 || (sp1 - filename) + strlen(sp2) > FILENAME_MAX)
187                 {
188                 DSOerr(DSO_F_VMS_LOAD,DSO_R_FILENAME_TOO_BIG);
189                 goto err;
190                 }
191
192         p = (DSO_VMS_INTERNAL *)OPENSSL_malloc(sizeof(DSO_VMS_INTERNAL));
193         if(p == NULL)
194                 {
195                 DSOerr(DSO_F_VMS_LOAD,ERR_R_MALLOC_FAILURE);
196                 goto err;
197                 }
198
199         strncpy(p->filename, sp1, sp2-sp1);
200         p->filename[sp2-sp1] = '\0';
201
202         strncpy(p->imagename, filename, sp1-filename);
203         p->imagename[sp1-filename] = '\0';
204         strcat(p->imagename, sp2);
205
206         p->filename_dsc.dsc$w_length = strlen(p->filename);
207         p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
208         p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
209         p->filename_dsc.dsc$a_pointer = p->filename;
210         p->imagename_dsc.dsc$w_length = strlen(p->imagename);
211         p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
212         p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
213         p->imagename_dsc.dsc$a_pointer = p->imagename;
214
215         if(!sk_push(dso->meth_data, (char *)p))
216                 {
217                 DSOerr(DSO_F_VMS_LOAD,DSO_R_STACK_ERROR);
218                 goto err;
219                 }
220
221         /* Success (for now, we lie.  We actually do not know...) */
222         dso->loaded_filename = filename;
223         return(1);
224 err:
225         /* Cleanup! */
226         if(p != NULL)
227                 OPENSSL_free(p);
228         if(filename != NULL)
229                 OPENSSL_free(filename);
230         return(0);
231         }
232
233 /* Note that this doesn't actually unload the shared image, as there is no
234  * such thing in VMS.  Next time it get loaded again, a new copy will
235  * actually be loaded.
236  */
237 static int vms_unload(DSO *dso)
238         {
239         DSO_VMS_INTERNAL *p;
240         if(dso == NULL)
241                 {
242                 DSOerr(DSO_F_VMS_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
243                 return(0);
244                 }
245         if(sk_num(dso->meth_data) < 1)
246                 return(1);
247         p = (DSO_VMS_INTERNAL *)sk_pop(dso->meth_data);
248         if(p == NULL)
249                 {
250                 DSOerr(DSO_F_VMS_UNLOAD,DSO_R_NULL_HANDLE);
251                 return(0);
252                 }
253         /* Cleanup */
254         OPENSSL_free(p);
255         return(1);
256         }
257
258 /* We must do this in a separate function because of the way the exception
259    handler works (it makes this function return */
260 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
261         struct dsc$descriptor_s *symname_dsc, void **sym,
262         unsigned long flags)
263         {
264         /* Make sure that signals are caught and returned instead of
265            aborting the program.  The exception handler gets unestablished
266            automatically on return from this function.  */
267         lib$establish(lib$sig_to_ret);
268
269         if(ptr->imagename_dsc.dsc$w_length)
270                 return lib$find_image_symbol(&ptr->filename_dsc,
271                         symname_dsc, sym,
272                         &ptr->imagename_dsc, flags);
273         else
274                 return lib$find_image_symbol(&ptr->filename_dsc,
275                         symname_dsc, sym,
276                         0, flags);
277         }
278
279 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
280         {
281         DSO_VMS_INTERNAL *ptr;
282         int status;
283         int flags = LIB$M_FIS_MIXEDCASE;
284         struct dsc$descriptor_s symname_dsc;
285         *sym = NULL;
286
287         symname_dsc.dsc$w_length = strlen(symname);
288         symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
289         symname_dsc.dsc$b_class = DSC$K_CLASS_S;
290         symname_dsc.dsc$a_pointer = (char *)symname; /* The cast is needed */
291
292         if((dso == NULL) || (symname == NULL))
293                 {
294                 DSOerr(DSO_F_VMS_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
295                 return;
296                 }
297         if(sk_num(dso->meth_data) < 1)
298                 {
299                 DSOerr(DSO_F_VMS_BIND_VAR,DSO_R_STACK_ERROR);
300                 return;
301                 }
302         ptr = (DSO_VMS_INTERNAL *)sk_value(dso->meth_data,
303                 sk_num(dso->meth_data) - 1);
304         if(ptr == NULL)
305                 {
306                 DSOerr(DSO_F_VMS_BIND_VAR,DSO_R_NULL_HANDLE);
307                 return;
308                 }
309
310         if(dso->flags & DSO_FLAG_UPCASE_SYMBOL) flags = 0;
311
312         status = do_find_symbol(ptr, &symname_dsc, sym, flags);
313
314         if(!$VMS_STATUS_SUCCESS(status))
315                 {
316                 unsigned short length;
317                 char errstring[257];
318                 struct dsc$descriptor_s errstring_dsc;
319
320                 errstring_dsc.dsc$w_length = sizeof(errstring);
321                 errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
322                 errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
323                 errstring_dsc.dsc$a_pointer = errstring;
324
325                 *sym = NULL;
326
327                 status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
328
329                 if (!$VMS_STATUS_SUCCESS(status))
330                         lib$signal(status); /* This is really bad.  Abort!  */
331                 else
332                         {
333                         errstring[length] = '\0';
334
335                         DSOerr(DSO_F_VMS_BIND_VAR,DSO_R_SYM_FAILURE);
336                         if (ptr->imagename_dsc.dsc$w_length)
337                                 ERR_add_error_data(9,
338                                         "Symbol ", symname,
339                                         " in ", ptr->filename,
340                                         " (", ptr->imagename, ")",
341                                         ": ", errstring);
342                         else
343                                 ERR_add_error_data(6,
344                                         "Symbol ", symname,
345                                         " in ", ptr->filename,
346                                         ": ", errstring);
347                         }
348                 return;
349                 }
350         return;
351         }
352
353 static void *vms_bind_var(DSO *dso, const char *symname)
354         {
355         void *sym = 0;
356         vms_bind_sym(dso, symname, &sym);
357         return sym;
358         }
359
360 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
361         {
362         DSO_FUNC_TYPE sym = 0;
363         vms_bind_sym(dso, symname, (void **)&sym);
364         return sym;
365         }
366
367 static char *vms_name_converter(DSO *dso, const char *filename)
368         {
369         return(filename);
370         }
371
372 #endif /* VMS */