dso_vms.c 15.0 KB
Newer Older
1
/* dso_vms.c -*- mode:C; c-file-style: "eay" -*- */
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
 * project 2000.
 */
/* ====================================================================
 * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
62 63
#include "cryptlib.h"
#include <openssl/dso.h>
64
#ifdef OPENSSL_SYS_VMS
65
#pragma message disable DOLLARID
66
#include <rms.h>
67 68 69 70
#include <lib$routines.h>
#include <stsdef.h>
#include <descrip.h>
#include <starlet.h>
71
#endif
72 73 74 75 76 77 78 79 80 81 82 83
#include "vms_rms.h"

/* Some compiler options may mask the declaration of "_malloc32". */
#if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
# if __INITIAL_POINTER_SIZE == 64
#  pragma pointer_size save
#  pragma pointer_size 32
    void * _malloc32  (__size_t);
#  pragma pointer_size restore
# endif /* __INITIAL_POINTER_SIZE == 64 */
#endif /* __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE */

84

85
#ifndef OPENSSL_SYS_VMS
86 87 88 89 90 91 92
DSO_METHOD *DSO_METHOD_vms(void)
	{
	return NULL;
	}
#else
#pragma message disable DOLLARID

93
static int vms_load(DSO *dso);
94 95 96 97 98 99 100 101 102
static int vms_unload(DSO *dso);
static void *vms_bind_var(DSO *dso, const char *symname);
static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
#if 0
static int vms_unbind_var(DSO *dso, char *symname, void *symptr);
static int vms_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
static int vms_init(DSO *dso);
static int vms_finish(DSO *dso);
static long vms_ctrl(DSO *dso, int cmd, long larg, void *parg);
103
#endif
104
static char *vms_name_converter(DSO *dso, const char *filename);
105 106
static char *vms_merger(DSO *dso, const char *filespec1,
	const char *filespec2);
107 108 109 110 111 112 113 114 115 116 117 118

static DSO_METHOD dso_meth_vms = {
	"OpenSSL 'VMS' shared library method",
	vms_load,
	NULL, /* unload */
	vms_bind_var,
	vms_bind_func,
/* For now, "unbind" doesn't exist */
#if 0
	NULL, /* unbind_var */
	NULL, /* unbind_func */
#endif
119
	NULL, /* ctrl */
120
	vms_name_converter,
121
	vms_merger,
122 123 124 125 126 127 128 129 130 131 132 133 134 135
	NULL, /* init */
	NULL  /* finish */
	};

/* On VMS, the only "handle" is the file name.  LIB$FIND_IMAGE_SYMBOL depends
 * on the reference to the file name being the same for all calls regarding
 * one shared image, so we'll just store it in an instance of the following
 * structure and put a pointer to that instance in the meth_data stack.
 */
typedef struct dso_internal_st
	{
	/* This should contain the name only, no directory,
	 * no extension, nothing but a name. */
	struct dsc$descriptor_s filename_dsc;
136
	char filename[ NAMX_MAXRSS+ 1];
137 138 139
	/* This contains whatever is not in filename, if needed.
	 * Normally not defined. */
	struct dsc$descriptor_s imagename_dsc;
140
	char imagename[ NAMX_MAXRSS+ 1];
141 142 143 144 145 146 147
	} DSO_VMS_INTERNAL;

DSO_METHOD *DSO_METHOD_vms(void)
	{
	return(&dso_meth_vms);
	}

148
static int vms_load(DSO *dso)
149
	{
150 151 152
	void *ptr = NULL;
	/* See applicable comments in dso_dl.c */
	char *filename = DSO_convert_filename(dso, NULL);
153 154 155 156 157 158 159 160 161 162

/* Ensure 32-bit pointer for "p", and appropriate malloc() function. */
#if __INITIAL_POINTER_SIZE == 64
# define DSO_MALLOC _malloc32
# pragma pointer_size save
# pragma pointer_size 32
#else /* __INITIAL_POINTER_SIZE == 64 */
# define DSO_MALLOC OPENSSL_malloc
#endif /* __INITIAL_POINTER_SIZE == 64 [else] */

163
	DSO_VMS_INTERNAL *p;
164 165 166 167 168

#if __INITIAL_POINTER_SIZE == 64
# pragma pointer_size restore
#endif /* __INITIAL_POINTER_SIZE == 64 */

169 170
	const char *sp1, *sp2;	/* Search result */

171 172
	if(filename == NULL)
		{
173
		DSOerr(DSO_F_VMS_LOAD,DSO_R_NO_FILENAME);
174 175 176
		goto err;
		}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	/* A file specification may look like this:
	 *
	 *	node::dev:[dir-spec]name.type;ver
	 *
	 * or (for compatibility with TOPS-20):
	 *
	 *	node::dev:<dir-spec>name.type;ver
	 *
	 * and the dir-spec uses '.' as separator.  Also, a dir-spec
	 * may consist of several parts, with mixed use of [] and <>:
	 *
	 *	[dir1.]<dir2>
	 *
	 * We need to split the file specification into the name and
	 * the rest (both before and after the name itself).
	 */
	/* Start with trying to find the end of a dir-spec, and save the
	   position of the byte after in sp1 */
	sp1 = strrchr(filename, ']');
	sp2 = strrchr(filename, '>');
	if (sp1 == NULL) sp1 = sp2;
	if (sp2 != NULL && sp2 > sp1) sp1 = sp2;
	if (sp1 == NULL) sp1 = strrchr(filename, ':');
	if (sp1 == NULL)
		sp1 = filename;
	else
		sp1++;		/* The byte after the found character */
	/* Now, let's see if there's a type, and save the position in sp2 */
	sp2 = strchr(sp1, '.');
	/* If we found it, that's where we'll cut.  Otherwise, look for a
	   version number and save the position in sp2 */
	if (sp2 == NULL) sp2 = strchr(sp1, ';');
	/* If there was still nothing to find, set sp2 to point at the end of
	   the string */
	if (sp2 == NULL) sp2 = sp1 + strlen(sp1);

	/* Check that we won't get buffer overflows */
	if (sp2 - sp1 > FILENAME_MAX
		|| (sp1 - filename) + strlen(sp2) > FILENAME_MAX)
		{
		DSOerr(DSO_F_VMS_LOAD,DSO_R_FILENAME_TOO_BIG);
218
		goto err;
219 220
		}

221
	p = DSO_MALLOC(sizeof(DSO_VMS_INTERNAL));
222 223 224
	if(p == NULL)
		{
		DSOerr(DSO_F_VMS_LOAD,ERR_R_MALLOC_FAILURE);
225
		goto err;
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		}

	strncpy(p->filename, sp1, sp2-sp1);
	p->filename[sp2-sp1] = '\0';

	strncpy(p->imagename, filename, sp1-filename);
	p->imagename[sp1-filename] = '\0';
	strcat(p->imagename, sp2);

	p->filename_dsc.dsc$w_length = strlen(p->filename);
	p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
	p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
	p->filename_dsc.dsc$a_pointer = p->filename;
	p->imagename_dsc.dsc$w_length = strlen(p->imagename);
	p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
	p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
	p->imagename_dsc.dsc$a_pointer = p->imagename;

244
	if(!sk_void_push(dso->meth_data, (char *)p))
245 246
		{
		DSOerr(DSO_F_VMS_LOAD,DSO_R_STACK_ERROR);
247
		goto err;
248
		}
249 250 251

	/* Success (for now, we lie.  We actually do not know...) */
	dso->loaded_filename = filename;
252
	return(1);
253 254 255 256 257 258 259
err:
	/* Cleanup! */
	if(p != NULL)
		OPENSSL_free(p);
	if(filename != NULL)
		OPENSSL_free(filename);
	return(0);
260 261 262 263 264 265 266 267 268 269 270 271 272 273
	}

/* Note that this doesn't actually unload the shared image, as there is no
 * such thing in VMS.  Next time it get loaded again, a new copy will
 * actually be loaded.
 */
static int vms_unload(DSO *dso)
	{
	DSO_VMS_INTERNAL *p;
	if(dso == NULL)
		{
		DSOerr(DSO_F_VMS_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
		return(0);
		}
274
	if(sk_void_num(dso->meth_data) < 1)
275
		return(1);
276
	p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	if(p == NULL)
		{
		DSOerr(DSO_F_VMS_UNLOAD,DSO_R_NULL_HANDLE);
		return(0);
		}
	/* Cleanup */
	OPENSSL_free(p);
	return(1);
	}

/* We must do this in a separate function because of the way the exception
   handler works (it makes this function return */
static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
	struct dsc$descriptor_s *symname_dsc, void **sym,
	unsigned long flags)
	{
	/* Make sure that signals are caught and returned instead of
	   aborting the program.  The exception handler gets unestablished
	   automatically on return from this function.  */
	lib$establish(lib$sig_to_ret);

	if(ptr->imagename_dsc.dsc$w_length)
		return lib$find_image_symbol(&ptr->filename_dsc,
			symname_dsc, sym,
			&ptr->imagename_dsc, flags);
	else
		return lib$find_image_symbol(&ptr->filename_dsc,
			symname_dsc, sym,
			0, flags);
	}

308
void vms_bind_sym(DSO *dso, const char *symname, void **sym)
309 310 311
	{
	DSO_VMS_INTERNAL *ptr;
	int status;
312
#if 0
313 314
	int flags = (1<<4); /* LIB$M_FIS_MIXEDCASE, but this symbol isn't
                               defined in VMS older than 7.0 or so */
315 316 317
#else
	int flags = 0;
#endif
318 319
	struct dsc$descriptor_s symname_dsc;

320 321 322 323 324 325 326 327 328 329 330 331 332
/* Arrange 32-bit pointer to (copied) string storage, if needed. */
#if __INITIAL_POINTER_SIZE == 64
# define SYMNAME symname_32p
# pragma pointer_size save
# pragma pointer_size 32
	char *symname_32p;
# pragma pointer_size restore
	char symname_32[ NAMX_MAXRSS+ 1];
#else /* __INITIAL_POINTER_SIZE == 64 */
# define SYMNAME ((char *) symname)
#endif /* __INITIAL_POINTER_SIZE == 64 [else] */

	*sym = NULL;
333 334 335

	if((dso == NULL) || (symname == NULL))
		{
336
		DSOerr(DSO_F_VMS_BIND_SYM,ERR_R_PASSED_NULL_PARAMETER);
337
		return;
338
		}
339 340 341 342 343 344 345 346 347 348 349 350

#if __INITIAL_POINTER_SIZE == 64
	/* Copy the symbol name to storage with a 32-bit pointer. */
	symname_32p = symname_32;
	strcpy( symname_32p, symname);
#endif /* __INITIAL_POINTER_SIZE == 64 [else] */

	symname_dsc.dsc$w_length = strlen(SYMNAME);
	symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
	symname_dsc.dsc$b_class = DSC$K_CLASS_S;
	symname_dsc.dsc$a_pointer = SYMNAME;

351
	if(sk_void_num(dso->meth_data) < 1)
352
		{
353
		DSOerr(DSO_F_VMS_BIND_SYM,DSO_R_STACK_ERROR);
354
		return;
355
		}
356 357
	ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
		sk_void_num(dso->meth_data) - 1);
358 359
	if(ptr == NULL)
		{
360
		DSOerr(DSO_F_VMS_BIND_SYM,DSO_R_NULL_HANDLE);
361
		return;
362 363 364 365
		}

	if(dso->flags & DSO_FLAG_UPCASE_SYMBOL) flags = 0;

366
	status = do_find_symbol(ptr, &symname_dsc, sym, flags);
367 368 369 370 371 372 373 374 375 376 377 378

	if(!$VMS_STATUS_SUCCESS(status))
		{
		unsigned short length;
		char errstring[257];
		struct dsc$descriptor_s errstring_dsc;

		errstring_dsc.dsc$w_length = sizeof(errstring);
		errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
		errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
		errstring_dsc.dsc$a_pointer = errstring;

379 380
		*sym = NULL;

381 382 383 384 385 386 387 388
		status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);

		if (!$VMS_STATUS_SUCCESS(status))
			lib$signal(status); /* This is really bad.  Abort!  */
		else
			{
			errstring[length] = '\0';

389
			DSOerr(DSO_F_VMS_BIND_SYM,DSO_R_SYM_FAILURE);
390 391 392 393 394 395 396 397 398 399 400 401
			if (ptr->imagename_dsc.dsc$w_length)
				ERR_add_error_data(9,
					"Symbol ", symname,
					" in ", ptr->filename,
					" (", ptr->imagename, ")",
					": ", errstring);
			else
				ERR_add_error_data(6,
					"Symbol ", symname,
					" in ", ptr->filename,
					": ", errstring);
			}
402
		return;
403
		}
404
	return;
405 406 407 408
	}

static void *vms_bind_var(DSO *dso, const char *symname)
	{
409 410 411
	void *sym = 0;
	vms_bind_sym(dso, symname, &sym);
	return sym;
412 413 414 415
	}

static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
	{
416
	DSO_FUNC_TYPE sym = 0;
417
	vms_bind_sym(dso, symname, (void **)&sym);
418
	return sym;
419 420
	}

421

422 423 424 425 426
static char *vms_merger(DSO *dso, const char *filespec1, const char *filespec2)
	{
	int status;
	int filespec1len, filespec2len;
	struct FAB fab;
427 428
	struct NAMX_STRUCT nam;
	char esa[ NAMX_MAXRSS+ 1];
429 430
	char *merged;

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/* Arrange 32-bit pointer to (copied) string storage, if needed. */
#if __INITIAL_POINTER_SIZE == 64
# define FILESPEC1 filespec1_32p;
# define FILESPEC2 filespec2_32p;
# pragma pointer_size save
# pragma pointer_size 32
	char *filespec1_32p;
	char *filespec2_32p;
# pragma pointer_size restore
	char filespec1_32[ NAMX_MAXRSS+ 1];
	char filespec2_32[ NAMX_MAXRSS+ 1];
#else /* __INITIAL_POINTER_SIZE == 64 */
# define FILESPEC1 ((char *) filespec1)
# define FILESPEC2 ((char *) filespec2)
#endif /* __INITIAL_POINTER_SIZE == 64 [else] */

447 448 449 450 451
	if (!filespec1) filespec1 = "";
	if (!filespec2) filespec2 = "";
	filespec1len = strlen(filespec1);
	filespec2len = strlen(filespec2);

452 453 454 455 456 457 458 459
#if __INITIAL_POINTER_SIZE == 64
	/* Copy the file names to storage with a 32-bit pointer. */
	filespec1_32p = filespec1_32;
	filespec2_32p = filespec2_32;
	strcpy( filespec1_32p, filespec1);
	strcpy( filespec2_32p, filespec2);
#endif /* __INITIAL_POINTER_SIZE == 64 [else] */

460
	fab = cc$rms_fab;
461
	nam = CC_RMS_NAMX;
462

463 464 465 466 467 468 469 470 471 472 473 474
	FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
	FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNS = filespec1len;
	FAB_OR_NAML( fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
	FAB_OR_NAML( fab, nam).FAB_OR_NAML_DNS = filespec2len;
	NAMX_DNA_FNA_SET( fab)

	nam.NAMX_ESA = esa;
	nam.NAMX_ESS = NAMX_MAXRSS;
	nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
	SET_NAMX_NO_SHORT_UPCASE( nam);

	fab.FAB_NAMX = &nam;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

	status = sys$parse(&fab, 0, 0);

	if(!$VMS_STATUS_SUCCESS(status))
		{
		unsigned short length;
		char errstring[257];
		struct dsc$descriptor_s errstring_dsc;

		errstring_dsc.dsc$w_length = sizeof(errstring);
		errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
		errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
		errstring_dsc.dsc$a_pointer = errstring;

		status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);

		if (!$VMS_STATUS_SUCCESS(status))
			lib$signal(status); /* This is really bad.  Abort!  */
		else
			{
			errstring[length] = '\0';

			DSOerr(DSO_F_VMS_MERGER,DSO_R_FAILURE);
498 499 500 501
			ERR_add_error_data(7,
					   "filespec \"", filespec1, "\", ",
					   "defaults \"", filespec2, "\": ",
					   errstring);
502 503 504
			}
		return(NULL);
		}
505 506

	merged = OPENSSL_malloc( nam.NAMX_ESL+ 1);
507 508
	if(!merged)
		goto malloc_err;
509 510
	strncpy( merged, nam.NAMX_ESA, nam.NAMX_ESL);
	merged[ nam.NAMX_ESL] = '\0';
511 512 513 514 515 516
	return(merged);
 malloc_err:
	DSOerr(DSO_F_VMS_MERGER,
		ERR_R_MALLOC_FAILURE);
	}

517
static char *vms_name_converter(DSO *dso, const char *filename)
518
	{
519 520 521 522
        int len = strlen(filename);
        char *not_translated = OPENSSL_malloc(len+1);
        strcpy(not_translated,filename);
	return(not_translated);
523 524
	}

525
#endif /* OPENSSL_SYS_VMS */