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