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