1*2139Sjp161948 /* dso_dl.c -*- mode:C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
30Sstevel@tonic-gate * project 2000.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/dso.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate #ifndef DSO_DL
DSO_METHOD_dl(void)640Sstevel@tonic-gate DSO_METHOD *DSO_METHOD_dl(void)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate return NULL;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate #else
690Sstevel@tonic-gate
700Sstevel@tonic-gate #include <dl.h>
710Sstevel@tonic-gate
720Sstevel@tonic-gate /* Part of the hack in "dl_load" ... */
730Sstevel@tonic-gate #define DSO_MAX_TRANSLATED_SIZE 256
740Sstevel@tonic-gate
750Sstevel@tonic-gate static int dl_load(DSO *dso);
760Sstevel@tonic-gate static int dl_unload(DSO *dso);
770Sstevel@tonic-gate static void *dl_bind_var(DSO *dso, const char *symname);
780Sstevel@tonic-gate static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname);
790Sstevel@tonic-gate #if 0
800Sstevel@tonic-gate static int dl_unbind_var(DSO *dso, char *symname, void *symptr);
810Sstevel@tonic-gate static int dl_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
820Sstevel@tonic-gate static int dl_init(DSO *dso);
830Sstevel@tonic-gate static int dl_finish(DSO *dso);
840Sstevel@tonic-gate static int dl_ctrl(DSO *dso, int cmd, long larg, void *parg);
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate static char *dl_name_converter(DSO *dso, const char *filename);
87*2139Sjp161948 static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2);
880Sstevel@tonic-gate
890Sstevel@tonic-gate static DSO_METHOD dso_meth_dl = {
900Sstevel@tonic-gate "OpenSSL 'dl' shared library method",
910Sstevel@tonic-gate dl_load,
920Sstevel@tonic-gate dl_unload,
930Sstevel@tonic-gate dl_bind_var,
940Sstevel@tonic-gate dl_bind_func,
950Sstevel@tonic-gate /* For now, "unbind" doesn't exist */
960Sstevel@tonic-gate #if 0
970Sstevel@tonic-gate NULL, /* unbind_var */
980Sstevel@tonic-gate NULL, /* unbind_func */
990Sstevel@tonic-gate #endif
1000Sstevel@tonic-gate NULL, /* ctrl */
1010Sstevel@tonic-gate dl_name_converter,
102*2139Sjp161948 dl_merger,
1030Sstevel@tonic-gate NULL, /* init */
1040Sstevel@tonic-gate NULL /* finish */
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate
DSO_METHOD_dl(void)1070Sstevel@tonic-gate DSO_METHOD *DSO_METHOD_dl(void)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate return(&dso_meth_dl);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /* For this DSO_METHOD, our meth_data STACK will contain;
1130Sstevel@tonic-gate * (i) the handle (shl_t) returned from shl_load().
1140Sstevel@tonic-gate * NB: I checked on HPUX11 and shl_t is itself a pointer
1150Sstevel@tonic-gate * type so the cast is safe.
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate
dl_load(DSO * dso)1180Sstevel@tonic-gate static int dl_load(DSO *dso)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate shl_t ptr = NULL;
1210Sstevel@tonic-gate /* We don't do any fancy retries or anything, just take the method's
1220Sstevel@tonic-gate * (or DSO's if it has the callback set) best translation of the
1230Sstevel@tonic-gate * platform-independant filename and try once with that. */
1240Sstevel@tonic-gate char *filename= DSO_convert_filename(dso, NULL);
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if(filename == NULL)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate DSOerr(DSO_F_DL_LOAD,DSO_R_NO_FILENAME);
1290Sstevel@tonic-gate goto err;
1300Sstevel@tonic-gate }
131*2139Sjp161948 ptr = shl_load(filename, BIND_IMMEDIATE |
132*2139Sjp161948 (dso->flags&DSO_FLAG_NO_NAME_TRANSLATION?0:DYNAMIC_PATH), 0L);
1330Sstevel@tonic-gate if(ptr == NULL)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate DSOerr(DSO_F_DL_LOAD,DSO_R_LOAD_FAILED);
1360Sstevel@tonic-gate ERR_add_error_data(4, "filename(", filename, "): ",
1370Sstevel@tonic-gate strerror(errno));
1380Sstevel@tonic-gate goto err;
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate if(!sk_push(dso->meth_data, (char *)ptr))
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate DSOerr(DSO_F_DL_LOAD,DSO_R_STACK_ERROR);
1430Sstevel@tonic-gate goto err;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate /* Success, stick the converted filename we've loaded under into the DSO
1460Sstevel@tonic-gate * (it also serves as the indicator that we are currently loaded). */
1470Sstevel@tonic-gate dso->loaded_filename = filename;
1480Sstevel@tonic-gate return(1);
1490Sstevel@tonic-gate err:
1500Sstevel@tonic-gate /* Cleanup! */
1510Sstevel@tonic-gate if(filename != NULL)
1520Sstevel@tonic-gate OPENSSL_free(filename);
1530Sstevel@tonic-gate if(ptr != NULL)
1540Sstevel@tonic-gate shl_unload(ptr);
1550Sstevel@tonic-gate return(0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
dl_unload(DSO * dso)1580Sstevel@tonic-gate static int dl_unload(DSO *dso)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate shl_t ptr;
1610Sstevel@tonic-gate if(dso == NULL)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate DSOerr(DSO_F_DL_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
1640Sstevel@tonic-gate return(0);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate if(sk_num(dso->meth_data) < 1)
1670Sstevel@tonic-gate return(1);
1680Sstevel@tonic-gate /* Is this statement legal? */
1690Sstevel@tonic-gate ptr = (shl_t)sk_pop(dso->meth_data);
1700Sstevel@tonic-gate if(ptr == NULL)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate DSOerr(DSO_F_DL_UNLOAD,DSO_R_NULL_HANDLE);
1730Sstevel@tonic-gate /* Should push the value back onto the stack in
1740Sstevel@tonic-gate * case of a retry. */
1750Sstevel@tonic-gate sk_push(dso->meth_data, (char *)ptr);
1760Sstevel@tonic-gate return(0);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate shl_unload(ptr);
1790Sstevel@tonic-gate return(1);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
dl_bind_var(DSO * dso,const char * symname)1820Sstevel@tonic-gate static void *dl_bind_var(DSO *dso, const char *symname)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate shl_t ptr;
1850Sstevel@tonic-gate void *sym;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate if((dso == NULL) || (symname == NULL))
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
1900Sstevel@tonic-gate return(NULL);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate if(sk_num(dso->meth_data) < 1)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_VAR,DSO_R_STACK_ERROR);
1950Sstevel@tonic-gate return(NULL);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate ptr = (shl_t)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
1980Sstevel@tonic-gate if(ptr == NULL)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_VAR,DSO_R_NULL_HANDLE);
2010Sstevel@tonic-gate return(NULL);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_VAR,DSO_R_SYM_FAILURE);
2060Sstevel@tonic-gate ERR_add_error_data(4, "symname(", symname, "): ",
2070Sstevel@tonic-gate strerror(errno));
2080Sstevel@tonic-gate return(NULL);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate return(sym);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
dl_bind_func(DSO * dso,const char * symname)2130Sstevel@tonic-gate static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate shl_t ptr;
2160Sstevel@tonic-gate void *sym;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if((dso == NULL) || (symname == NULL))
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
2210Sstevel@tonic-gate return(NULL);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate if(sk_num(dso->meth_data) < 1)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_FUNC,DSO_R_STACK_ERROR);
2260Sstevel@tonic-gate return(NULL);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate ptr = (shl_t)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
2290Sstevel@tonic-gate if(ptr == NULL)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_FUNC,DSO_R_NULL_HANDLE);
2320Sstevel@tonic-gate return(NULL);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate DSOerr(DSO_F_DL_BIND_FUNC,DSO_R_SYM_FAILURE);
2370Sstevel@tonic-gate ERR_add_error_data(4, "symname(", symname, "): ",
2380Sstevel@tonic-gate strerror(errno));
2390Sstevel@tonic-gate return(NULL);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate return((DSO_FUNC_TYPE)sym);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
dl_merger(DSO * dso,const char * filespec1,const char * filespec2)244*2139Sjp161948 static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
245*2139Sjp161948 {
246*2139Sjp161948 char *merged;
247*2139Sjp161948
248*2139Sjp161948 if(!filespec1 && !filespec2)
249*2139Sjp161948 {
250*2139Sjp161948 DSOerr(DSO_F_DL_MERGER,
251*2139Sjp161948 ERR_R_PASSED_NULL_PARAMETER);
252*2139Sjp161948 return(NULL);
253*2139Sjp161948 }
254*2139Sjp161948 /* If the first file specification is a rooted path, it rules.
255*2139Sjp161948 same goes if the second file specification is missing. */
256*2139Sjp161948 if (!filespec2 || filespec1[0] == '/')
257*2139Sjp161948 {
258*2139Sjp161948 merged = OPENSSL_malloc(strlen(filespec1) + 1);
259*2139Sjp161948 if(!merged)
260*2139Sjp161948 {
261*2139Sjp161948 DSOerr(DSO_F_DL_MERGER,
262*2139Sjp161948 ERR_R_MALLOC_FAILURE);
263*2139Sjp161948 return(NULL);
264*2139Sjp161948 }
265*2139Sjp161948 strcpy(merged, filespec1);
266*2139Sjp161948 }
267*2139Sjp161948 /* If the first file specification is missing, the second one rules. */
268*2139Sjp161948 else if (!filespec1)
269*2139Sjp161948 {
270*2139Sjp161948 merged = OPENSSL_malloc(strlen(filespec2) + 1);
271*2139Sjp161948 if(!merged)
272*2139Sjp161948 {
273*2139Sjp161948 DSOerr(DSO_F_DL_MERGER,
274*2139Sjp161948 ERR_R_MALLOC_FAILURE);
275*2139Sjp161948 return(NULL);
276*2139Sjp161948 }
277*2139Sjp161948 strcpy(merged, filespec2);
278*2139Sjp161948 }
279*2139Sjp161948 else
280*2139Sjp161948 /* This part isn't as trivial as it looks. It assumes that
281*2139Sjp161948 the second file specification really is a directory, and
282*2139Sjp161948 makes no checks whatsoever. Therefore, the result becomes
283*2139Sjp161948 the concatenation of filespec2 followed by a slash followed
284*2139Sjp161948 by filespec1. */
285*2139Sjp161948 {
286*2139Sjp161948 int spec2len, len;
287*2139Sjp161948
288*2139Sjp161948 spec2len = (filespec2 ? strlen(filespec2) : 0);
289*2139Sjp161948 len = spec2len + (filespec1 ? strlen(filespec1) : 0);
290*2139Sjp161948
291*2139Sjp161948 if(filespec2 && filespec2[spec2len - 1] == '/')
292*2139Sjp161948 {
293*2139Sjp161948 spec2len--;
294*2139Sjp161948 len--;
295*2139Sjp161948 }
296*2139Sjp161948 merged = OPENSSL_malloc(len + 2);
297*2139Sjp161948 if(!merged)
298*2139Sjp161948 {
299*2139Sjp161948 DSOerr(DSO_F_DL_MERGER,
300*2139Sjp161948 ERR_R_MALLOC_FAILURE);
301*2139Sjp161948 return(NULL);
302*2139Sjp161948 }
303*2139Sjp161948 strcpy(merged, filespec2);
304*2139Sjp161948 merged[spec2len] = '/';
305*2139Sjp161948 strcpy(&merged[spec2len + 1], filespec1);
306*2139Sjp161948 }
307*2139Sjp161948 return(merged);
308*2139Sjp161948 }
309*2139Sjp161948
3100Sstevel@tonic-gate /* This function is identical to the one in dso_dlfcn.c, but as it is highly
3110Sstevel@tonic-gate * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at the
3120Sstevel@tonic-gate * same time, there's no great duplicating the code. Figuring out an elegant
3130Sstevel@tonic-gate * way to share one copy of the code would be more difficult and would not
3140Sstevel@tonic-gate * leave the implementations independant. */
3150Sstevel@tonic-gate #if defined(__hpux)
3160Sstevel@tonic-gate static const char extension[] = ".sl";
3170Sstevel@tonic-gate #else
3180Sstevel@tonic-gate static const char extension[] = ".so";
3190Sstevel@tonic-gate #endif
dl_name_converter(DSO * dso,const char * filename)3200Sstevel@tonic-gate static char *dl_name_converter(DSO *dso, const char *filename)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate char *translated;
3230Sstevel@tonic-gate int len, rsize, transform;
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate len = strlen(filename);
3260Sstevel@tonic-gate rsize = len + 1;
3270Sstevel@tonic-gate transform = (strstr(filename, "/") == NULL);
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate /* We will convert this to "%s.s?" or "lib%s.s?" */
3300Sstevel@tonic-gate rsize += strlen(extension);/* The length of ".s?" */
3310Sstevel@tonic-gate if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
3320Sstevel@tonic-gate rsize += 3; /* The length of "lib" */
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate translated = OPENSSL_malloc(rsize);
3350Sstevel@tonic-gate if(translated == NULL)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate DSOerr(DSO_F_DL_NAME_CONVERTER,
3380Sstevel@tonic-gate DSO_R_NAME_TRANSLATION_FAILED);
3390Sstevel@tonic-gate return(NULL);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate if(transform)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
3440Sstevel@tonic-gate sprintf(translated, "lib%s%s", filename, extension);
3450Sstevel@tonic-gate else
3460Sstevel@tonic-gate sprintf(translated, "%s%s", filename, extension);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate else
3490Sstevel@tonic-gate sprintf(translated, "%s", filename);
3500Sstevel@tonic-gate return(translated);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate #endif /* DSO_DL */
354