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