]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dso/dso_vms.c
Run util/openssl-format-source -v -c .
[thirdparty/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 #ifdef OPENSSL_SYS_VMS
66 # pragma message disable DOLLARID
67 # include <rms.h>
68 # include <lib$routines.h>
69 # include <stsdef.h>
70 # include <descrip.h>
71 # include <starlet.h>
72 #endif
73
74 #ifndef OPENSSL_SYS_VMS
75 DSO_METHOD *DSO_METHOD_vms(void)
76 {
77 return NULL;
78 }
79 #else
80 # pragma message disable DOLLARID
81
82 static int vms_load(DSO *dso);
83 static int vms_unload(DSO *dso);
84 static void *vms_bind_var(DSO *dso, const char *symname);
85 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
86 # if 0
87 static int vms_unbind_var(DSO *dso, char *symname, void *symptr);
88 static int vms_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
89 static int vms_init(DSO *dso);
90 static int vms_finish(DSO *dso);
91 static long vms_ctrl(DSO *dso, int cmd, long larg, void *parg);
92 # endif
93 static char *vms_name_converter(DSO *dso, const char *filename);
94 static char *vms_merger(DSO *dso, const char *filespec1,
95 const char *filespec2);
96
97 static DSO_METHOD dso_meth_vms = {
98 "OpenSSL 'VMS' shared library method",
99 vms_load,
100 NULL, /* unload */
101 vms_bind_var,
102 vms_bind_func,
103 /* For now, "unbind" doesn't exist */
104 # if 0
105 NULL, /* unbind_var */
106 NULL, /* unbind_func */
107 # endif
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[FILENAME_MAX + 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[FILENAME_MAX + 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 DSO_VMS_INTERNAL *p;
147 const char *sp1, *sp2; /* Search result */
148
149 if (filename == NULL) {
150 DSOerr(DSO_F_VMS_LOAD, DSO_R_NO_FILENAME);
151 goto err;
152 }
153
154 /*-
155 * A file specification may look like this:
156 *
157 * node::dev:[dir-spec]name.type;ver
158 *
159 * or (for compatibility with TOPS-20):
160 *
161 * node::dev:<dir-spec>name.type;ver
162 *
163 * and the dir-spec uses '.' as separator. Also, a dir-spec
164 * may consist of several parts, with mixed use of [] and <>:
165 *
166 * [dir1.]<dir2>
167 *
168 * We need to split the file specification into the name and
169 * the rest (both before and after the name itself).
170 */
171 /*
172 * Start with trying to find the end of a dir-spec, and save the position
173 * of the byte after in sp1
174 */
175 sp1 = strrchr(filename, ']');
176 sp2 = strrchr(filename, '>');
177 if (sp1 == NULL)
178 sp1 = sp2;
179 if (sp2 != NULL && sp2 > sp1)
180 sp1 = sp2;
181 if (sp1 == NULL)
182 sp1 = strrchr(filename, ':');
183 if (sp1 == NULL)
184 sp1 = filename;
185 else
186 sp1++; /* The byte after the found character */
187 /* Now, let's see if there's a type, and save the position in sp2 */
188 sp2 = strchr(sp1, '.');
189 /*
190 * If we found it, that's where we'll cut. Otherwise, look for a version
191 * number and save the position in sp2
192 */
193 if (sp2 == NULL)
194 sp2 = strchr(sp1, ';');
195 /*
196 * If there was still nothing to find, set sp2 to point at the end of the
197 * string
198 */
199 if (sp2 == NULL)
200 sp2 = sp1 + strlen(sp1);
201
202 /* Check that we won't get buffer overflows */
203 if (sp2 - sp1 > FILENAME_MAX
204 || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
205 DSOerr(DSO_F_VMS_LOAD, DSO_R_FILENAME_TOO_BIG);
206 goto err;
207 }
208
209 p = (DSO_VMS_INTERNAL *)OPENSSL_malloc(sizeof(DSO_VMS_INTERNAL));
210 if (p == NULL) {
211 DSOerr(DSO_F_VMS_LOAD, ERR_R_MALLOC_FAILURE);
212 goto err;
213 }
214
215 strncpy(p->filename, sp1, sp2 - sp1);
216 p->filename[sp2 - sp1] = '\0';
217
218 strncpy(p->imagename, filename, sp1 - filename);
219 p->imagename[sp1 - filename] = '\0';
220 strcat(p->imagename, sp2);
221
222 p->filename_dsc.dsc$w_length = strlen(p->filename);
223 p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
224 p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
225 p->filename_dsc.dsc$a_pointer = p->filename;
226 p->imagename_dsc.dsc$w_length = strlen(p->imagename);
227 p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
228 p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
229 p->imagename_dsc.dsc$a_pointer = p->imagename;
230
231 if (!sk_push(dso->meth_data, (char *)p)) {
232 DSOerr(DSO_F_VMS_LOAD, DSO_R_STACK_ERROR);
233 goto err;
234 }
235
236 /* Success (for now, we lie. We actually do not know...) */
237 dso->loaded_filename = filename;
238 return (1);
239 err:
240 /* Cleanup! */
241 if (p != NULL)
242 OPENSSL_free(p);
243 if (filename != NULL)
244 OPENSSL_free(filename);
245 return (0);
246 }
247
248 /*
249 * Note that this doesn't actually unload the shared image, as there is no
250 * such thing in VMS. Next time it get loaded again, a new copy will
251 * actually be loaded.
252 */
253 static int vms_unload(DSO *dso)
254 {
255 DSO_VMS_INTERNAL *p;
256 if (dso == NULL) {
257 DSOerr(DSO_F_VMS_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
258 return (0);
259 }
260 if (sk_num(dso->meth_data) < 1)
261 return (1);
262 p = (DSO_VMS_INTERNAL *)sk_pop(dso->meth_data);
263 if (p == NULL) {
264 DSOerr(DSO_F_VMS_UNLOAD, DSO_R_NULL_HANDLE);
265 return (0);
266 }
267 /* Cleanup */
268 OPENSSL_free(p);
269 return (1);
270 }
271
272 /*
273 * We must do this in a separate function because of the way the exception
274 * handler works (it makes this function return
275 */
276 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
277 struct dsc$descriptor_s *symname_dsc, void **sym,
278 unsigned long flags)
279 {
280 /*
281 * Make sure that signals are caught and returned instead of aborting the
282 * program. The exception handler gets unestablished automatically on
283 * return from this function.
284 */
285 lib$establish(lib$sig_to_ret);
286
287 if (ptr->imagename_dsc.dsc$w_length)
288 return lib$find_image_symbol(&ptr->filename_dsc,
289 symname_dsc, sym,
290 &ptr->imagename_dsc, flags);
291 else
292 return lib$find_image_symbol(&ptr->filename_dsc,
293 symname_dsc, sym, 0, flags);
294 }
295
296 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
297 {
298 DSO_VMS_INTERNAL *ptr;
299 int status;
300 # if 0
301 int flags = (1 << 4); /* LIB$M_FIS_MIXEDCASE, but this symbol isn't
302 * defined in VMS older than 7.0 or so */
303 # else
304 int flags = 0;
305 # endif
306 struct dsc$descriptor_s symname_dsc;
307 *sym = NULL;
308
309 symname_dsc.dsc$w_length = strlen(symname);
310 symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
311 symname_dsc.dsc$b_class = DSC$K_CLASS_S;
312 symname_dsc.dsc$a_pointer = (char *)symname; /* The cast is needed */
313
314 if ((dso == NULL) || (symname == NULL)) {
315 DSOerr(DSO_F_VMS_BIND_SYM, ERR_R_PASSED_NULL_PARAMETER);
316 return;
317 }
318 if (sk_num(dso->meth_data) < 1) {
319 DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_STACK_ERROR);
320 return;
321 }
322 ptr = (DSO_VMS_INTERNAL *)sk_value(dso->meth_data,
323 sk_num(dso->meth_data) - 1);
324 if (ptr == NULL) {
325 DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_NULL_HANDLE);
326 return;
327 }
328
329 if (dso->flags & DSO_FLAG_UPCASE_SYMBOL)
330 flags = 0;
331
332 status = do_find_symbol(ptr, &symname_dsc, sym, flags);
333
334 if (!$VMS_STATUS_SUCCESS(status)) {
335 unsigned short length;
336 char errstring[257];
337 struct dsc$descriptor_s errstring_dsc;
338
339 errstring_dsc.dsc$w_length = sizeof(errstring);
340 errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
341 errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
342 errstring_dsc.dsc$a_pointer = errstring;
343
344 *sym = NULL;
345
346 status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
347
348 if (!$VMS_STATUS_SUCCESS(status))
349 lib$signal(status); /* This is really bad. Abort! */
350 else {
351 errstring[length] = '\0';
352
353 DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_SYM_FAILURE);
354 if (ptr->imagename_dsc.dsc$w_length)
355 ERR_add_error_data(9,
356 "Symbol ", symname,
357 " in ", ptr->filename,
358 " (", ptr->imagename, ")",
359 ": ", errstring);
360 else
361 ERR_add_error_data(6,
362 "Symbol ", symname,
363 " in ", ptr->filename, ": ", errstring);
364 }
365 return;
366 }
367 return;
368 }
369
370 static void *vms_bind_var(DSO *dso, const char *symname)
371 {
372 void *sym = 0;
373 vms_bind_sym(dso, symname, &sym);
374 return sym;
375 }
376
377 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
378 {
379 DSO_FUNC_TYPE sym = 0;
380 vms_bind_sym(dso, symname, (void **)&sym);
381 return sym;
382 }
383
384 static char *vms_merger(DSO *dso, const char *filespec1,
385 const char *filespec2)
386 {
387 int status;
388 int filespec1len, filespec2len;
389 struct FAB fab;
390 # ifdef NAML$C_MAXRSS
391 struct NAML nam;
392 char esa[NAML$C_MAXRSS];
393 # else
394 struct NAM nam;
395 char esa[NAM$C_MAXRSS];
396 # endif
397 char *merged;
398
399 if (!filespec1)
400 filespec1 = "";
401 if (!filespec2)
402 filespec2 = "";
403 filespec1len = strlen(filespec1);
404 filespec2len = strlen(filespec2);
405
406 fab = cc$rms_fab;
407 # ifdef NAML$C_MAXRSS
408 nam = cc$rms_naml;
409 # else
410 nam = cc$rms_nam;
411 # endif
412
413 fab.fab$l_fna = (char *)filespec1;
414 fab.fab$b_fns = filespec1len;
415 fab.fab$l_dna = (char *)filespec2;
416 fab.fab$b_dns = filespec2len;
417 # ifdef NAML$C_MAXRSS
418 if (filespec1len > NAM$C_MAXRSS) {
419 fab.fab$l_fna = 0;
420 fab.fab$b_fns = 0;
421 nam.naml$l_long_filename = (char *)filespec1;
422 nam.naml$l_long_filename_size = filespec1len;
423 }
424 if (filespec2len > NAM$C_MAXRSS) {
425 fab.fab$l_dna = 0;
426 fab.fab$b_dns = 0;
427 nam.naml$l_long_defname = (char *)filespec2;
428 nam.naml$l_long_defname_size = filespec2len;
429 }
430 nam.naml$l_esa = esa;
431 nam.naml$b_ess = NAM$C_MAXRSS;
432 nam.naml$l_long_expand = esa;
433 nam.naml$l_long_expand_alloc = sizeof(esa);
434 nam.naml$b_nop = NAM$M_SYNCHK | NAM$M_PWD;
435 nam.naml$v_no_short_upcase = 1;
436 fab.fab$l_naml = &nam;
437 # else
438 nam.nam$l_esa = esa;
439 nam.nam$b_ess = NAM$C_MAXRSS;
440 nam.nam$b_nop = NAM$M_SYNCHK | NAM$M_PWD;
441 fab.fab$l_nam = &nam;
442 # endif
443
444 status = sys$parse(&fab, 0, 0);
445
446 if (!$VMS_STATUS_SUCCESS(status)) {
447 unsigned short length;
448 char errstring[257];
449 struct dsc$descriptor_s errstring_dsc;
450
451 errstring_dsc.dsc$w_length = sizeof(errstring);
452 errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
453 errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
454 errstring_dsc.dsc$a_pointer = errstring;
455
456 status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
457
458 if (!$VMS_STATUS_SUCCESS(status))
459 lib$signal(status); /* This is really bad. Abort! */
460 else {
461 errstring[length] = '\0';
462
463 DSOerr(DSO_F_VMS_MERGER, DSO_R_FAILURE);
464 ERR_add_error_data(7,
465 "filespec \"", filespec1, "\", ",
466 "defaults \"", filespec2, "\": ", errstring);
467 }
468 return (NULL);
469 }
470 # ifdef NAML$C_MAXRSS
471 if (nam.naml$l_long_expand_size) {
472 merged = OPENSSL_malloc(nam.naml$l_long_expand_size + 1);
473 if (!merged)
474 goto malloc_err;
475 strncpy(merged, nam.naml$l_long_expand, nam.naml$l_long_expand_size);
476 merged[nam.naml$l_long_expand_size] = '\0';
477 } else {
478 merged = OPENSSL_malloc(nam.naml$b_esl + 1);
479 if (!merged)
480 goto malloc_err;
481 strncpy(merged, nam.naml$l_esa, nam.naml$b_esl);
482 merged[nam.naml$b_esl] = '\0';
483 }
484 # else
485 merged = OPENSSL_malloc(nam.nam$b_esl + 1);
486 if (!merged)
487 goto malloc_err;
488 strncpy(merged, nam.nam$l_esa, nam.nam$b_esl);
489 merged[nam.nam$b_esl] = '\0';
490 # endif
491 return (merged);
492 malloc_err:
493 DSOerr(DSO_F_VMS_MERGER, ERR_R_MALLOC_FAILURE);
494 }
495
496 static char *vms_name_converter(DSO *dso, const char *filename)
497 {
498 int len = strlen(filename);
499 char *not_translated = OPENSSL_malloc(len + 1);
500 strcpy(not_translated, filename);
501 return (not_translated);
502 }
503
504 #endif /* OPENSSL_SYS_VMS */