1*2139Sjp161948 /* dso_dlfcn.c -*- mode:C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* Written by Geoff Thorpe (geoff@geoffthorpe.net) 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_DLFCN
DSO_METHOD_dlfcn(void)640Sstevel@tonic-gate DSO_METHOD *DSO_METHOD_dlfcn(void)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate return NULL;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate #else
690Sstevel@tonic-gate
700Sstevel@tonic-gate #ifdef HAVE_DLFCN_H
710Sstevel@tonic-gate #include <dlfcn.h>
720Sstevel@tonic-gate #endif
730Sstevel@tonic-gate
740Sstevel@tonic-gate /* Part of the hack in "dlfcn_load" ... */
750Sstevel@tonic-gate #define DSO_MAX_TRANSLATED_SIZE 256
760Sstevel@tonic-gate
770Sstevel@tonic-gate static int dlfcn_load(DSO *dso);
780Sstevel@tonic-gate static int dlfcn_unload(DSO *dso);
790Sstevel@tonic-gate static void *dlfcn_bind_var(DSO *dso, const char *symname);
800Sstevel@tonic-gate static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname);
810Sstevel@tonic-gate #if 0
820Sstevel@tonic-gate static int dlfcn_unbind(DSO *dso, char *symname, void *symptr);
830Sstevel@tonic-gate static int dlfcn_init(DSO *dso);
840Sstevel@tonic-gate static int dlfcn_finish(DSO *dso);
850Sstevel@tonic-gate static long dlfcn_ctrl(DSO *dso, int cmd, long larg, void *parg);
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate static char *dlfcn_name_converter(DSO *dso, const char *filename);
88*2139Sjp161948 static char *dlfcn_merger(DSO *dso, const char *filespec1,
89*2139Sjp161948 const char *filespec2);
900Sstevel@tonic-gate
910Sstevel@tonic-gate static DSO_METHOD dso_meth_dlfcn = {
920Sstevel@tonic-gate "OpenSSL 'dlfcn' shared library method",
930Sstevel@tonic-gate dlfcn_load,
940Sstevel@tonic-gate dlfcn_unload,
950Sstevel@tonic-gate dlfcn_bind_var,
960Sstevel@tonic-gate dlfcn_bind_func,
970Sstevel@tonic-gate /* For now, "unbind" doesn't exist */
980Sstevel@tonic-gate #if 0
990Sstevel@tonic-gate NULL, /* unbind_var */
1000Sstevel@tonic-gate NULL, /* unbind_func */
1010Sstevel@tonic-gate #endif
1020Sstevel@tonic-gate NULL, /* ctrl */
1030Sstevel@tonic-gate dlfcn_name_converter,
104*2139Sjp161948 dlfcn_merger,
1050Sstevel@tonic-gate NULL, /* init */
1060Sstevel@tonic-gate NULL /* finish */
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate
DSO_METHOD_dlfcn(void)1090Sstevel@tonic-gate DSO_METHOD *DSO_METHOD_dlfcn(void)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate return(&dso_meth_dlfcn);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /* Prior to using the dlopen() function, we should decide on the flag
1150Sstevel@tonic-gate * we send. There's a few different ways of doing this and it's a
1160Sstevel@tonic-gate * messy venn-diagram to match up which platforms support what. So
1170Sstevel@tonic-gate * as we don't have autoconf yet, I'm implementing a hack that could
1180Sstevel@tonic-gate * be hacked further relatively easily to deal with cases as we find
1190Sstevel@tonic-gate * them. Initially this is to cope with OpenBSD. */
1200Sstevel@tonic-gate #if defined(__OpenBSD__) || defined(__NetBSD__)
1210Sstevel@tonic-gate # ifdef DL_LAZY
1220Sstevel@tonic-gate # define DLOPEN_FLAG DL_LAZY
1230Sstevel@tonic-gate # else
1240Sstevel@tonic-gate # ifdef RTLD_NOW
1250Sstevel@tonic-gate # define DLOPEN_FLAG RTLD_NOW
1260Sstevel@tonic-gate # else
1270Sstevel@tonic-gate # define DLOPEN_FLAG 0
1280Sstevel@tonic-gate # endif
1290Sstevel@tonic-gate # endif
1300Sstevel@tonic-gate #else
1310Sstevel@tonic-gate # ifdef OPENSSL_SYS_SUNOS
1320Sstevel@tonic-gate # define DLOPEN_FLAG 1
1330Sstevel@tonic-gate # else
1340Sstevel@tonic-gate # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */
1350Sstevel@tonic-gate # endif
1360Sstevel@tonic-gate #endif
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /* For this DSO_METHOD, our meth_data STACK will contain;
1390Sstevel@tonic-gate * (i) the handle (void*) returned from dlopen().
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate
dlfcn_load(DSO * dso)1420Sstevel@tonic-gate static int dlfcn_load(DSO *dso)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate void *ptr = NULL;
1450Sstevel@tonic-gate /* See applicable comments in dso_dl.c */
1460Sstevel@tonic-gate char *filename = DSO_convert_filename(dso, NULL);
147*2139Sjp161948 int flags = DLOPEN_FLAG;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate if(filename == NULL)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_LOAD,DSO_R_NO_FILENAME);
1520Sstevel@tonic-gate goto err;
1530Sstevel@tonic-gate }
154*2139Sjp161948
155*2139Sjp161948 #ifdef RTLD_GLOBAL
156*2139Sjp161948 if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
157*2139Sjp161948 flags |= RTLD_GLOBAL;
158*2139Sjp161948 #endif
159*2139Sjp161948 ptr = dlopen(filename, flags);
1600Sstevel@tonic-gate if(ptr == NULL)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_LOAD,DSO_R_LOAD_FAILED);
1630Sstevel@tonic-gate ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
1640Sstevel@tonic-gate goto err;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate if(!sk_push(dso->meth_data, (char *)ptr))
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_LOAD,DSO_R_STACK_ERROR);
1690Sstevel@tonic-gate goto err;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate /* Success */
1720Sstevel@tonic-gate dso->loaded_filename = filename;
1730Sstevel@tonic-gate return(1);
1740Sstevel@tonic-gate err:
1750Sstevel@tonic-gate /* Cleanup! */
1760Sstevel@tonic-gate if(filename != NULL)
1770Sstevel@tonic-gate OPENSSL_free(filename);
1780Sstevel@tonic-gate if(ptr != NULL)
1790Sstevel@tonic-gate dlclose(ptr);
1800Sstevel@tonic-gate return(0);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
dlfcn_unload(DSO * dso)1830Sstevel@tonic-gate static int dlfcn_unload(DSO *dso)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate void *ptr;
1860Sstevel@tonic-gate if(dso == NULL)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
1890Sstevel@tonic-gate return(0);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate if(sk_num(dso->meth_data) < 1)
1920Sstevel@tonic-gate return(1);
1930Sstevel@tonic-gate ptr = (void *)sk_pop(dso->meth_data);
1940Sstevel@tonic-gate if(ptr == NULL)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_UNLOAD,DSO_R_NULL_HANDLE);
1970Sstevel@tonic-gate /* Should push the value back onto the stack in
1980Sstevel@tonic-gate * case of a retry. */
1990Sstevel@tonic-gate sk_push(dso->meth_data, (char *)ptr);
2000Sstevel@tonic-gate return(0);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate /* For now I'm not aware of any errors associated with dlclose() */
2030Sstevel@tonic-gate dlclose(ptr);
2040Sstevel@tonic-gate return(1);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
dlfcn_bind_var(DSO * dso,const char * symname)2070Sstevel@tonic-gate static void *dlfcn_bind_var(DSO *dso, const char *symname)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate void *ptr, *sym;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate if((dso == NULL) || (symname == NULL))
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
2140Sstevel@tonic-gate return(NULL);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate if(sk_num(dso->meth_data) < 1)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_STACK_ERROR);
2190Sstevel@tonic-gate return(NULL);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
2220Sstevel@tonic-gate if(ptr == NULL)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_NULL_HANDLE);
2250Sstevel@tonic-gate return(NULL);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate sym = dlsym(ptr, symname);
2280Sstevel@tonic-gate if(sym == NULL)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_SYM_FAILURE);
2310Sstevel@tonic-gate ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
2320Sstevel@tonic-gate return(NULL);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate return(sym);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
dlfcn_bind_func(DSO * dso,const char * symname)2370Sstevel@tonic-gate static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate void *ptr;
240*2139Sjp161948 DSO_FUNC_TYPE sym, *tsym = &sym;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if((dso == NULL) || (symname == NULL))
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
2450Sstevel@tonic-gate return(NULL);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate if(sk_num(dso->meth_data) < 1)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_STACK_ERROR);
2500Sstevel@tonic-gate return(NULL);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
2530Sstevel@tonic-gate if(ptr == NULL)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_NULL_HANDLE);
2560Sstevel@tonic-gate return(NULL);
2570Sstevel@tonic-gate }
258*2139Sjp161948 *(void **)(tsym) = dlsym(ptr, symname);
2590Sstevel@tonic-gate if(sym == NULL)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_SYM_FAILURE);
2620Sstevel@tonic-gate ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
2630Sstevel@tonic-gate return(NULL);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate return(sym);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
dlfcn_merger(DSO * dso,const char * filespec1,const char * filespec2)268*2139Sjp161948 static char *dlfcn_merger(DSO *dso, const char *filespec1,
269*2139Sjp161948 const char *filespec2)
270*2139Sjp161948 {
271*2139Sjp161948 char *merged;
272*2139Sjp161948
273*2139Sjp161948 if(!filespec1 && !filespec2)
274*2139Sjp161948 {
275*2139Sjp161948 DSOerr(DSO_F_DLFCN_MERGER,
276*2139Sjp161948 ERR_R_PASSED_NULL_PARAMETER);
277*2139Sjp161948 return(NULL);
278*2139Sjp161948 }
279*2139Sjp161948 /* If the first file specification is a rooted path, it rules.
280*2139Sjp161948 same goes if the second file specification is missing. */
281*2139Sjp161948 if (!filespec2 || filespec1[0] == '/')
282*2139Sjp161948 {
283*2139Sjp161948 merged = OPENSSL_malloc(strlen(filespec1) + 1);
284*2139Sjp161948 if(!merged)
285*2139Sjp161948 {
286*2139Sjp161948 DSOerr(DSO_F_DLFCN_MERGER,
287*2139Sjp161948 ERR_R_MALLOC_FAILURE);
288*2139Sjp161948 return(NULL);
289*2139Sjp161948 }
290*2139Sjp161948 strcpy(merged, filespec1);
291*2139Sjp161948 }
292*2139Sjp161948 /* If the first file specification is missing, the second one rules. */
293*2139Sjp161948 else if (!filespec1)
294*2139Sjp161948 {
295*2139Sjp161948 merged = OPENSSL_malloc(strlen(filespec2) + 1);
296*2139Sjp161948 if(!merged)
297*2139Sjp161948 {
298*2139Sjp161948 DSOerr(DSO_F_DLFCN_MERGER,
299*2139Sjp161948 ERR_R_MALLOC_FAILURE);
300*2139Sjp161948 return(NULL);
301*2139Sjp161948 }
302*2139Sjp161948 strcpy(merged, filespec2);
303*2139Sjp161948 }
304*2139Sjp161948 else
305*2139Sjp161948 /* This part isn't as trivial as it looks. It assumes that
306*2139Sjp161948 the second file specification really is a directory, and
307*2139Sjp161948 makes no checks whatsoever. Therefore, the result becomes
308*2139Sjp161948 the concatenation of filespec2 followed by a slash followed
309*2139Sjp161948 by filespec1. */
310*2139Sjp161948 {
311*2139Sjp161948 int spec2len, len;
312*2139Sjp161948
313*2139Sjp161948 spec2len = (filespec2 ? strlen(filespec2) : 0);
314*2139Sjp161948 len = spec2len + (filespec1 ? strlen(filespec1) : 0);
315*2139Sjp161948
316*2139Sjp161948 if(filespec2 && filespec2[spec2len - 1] == '/')
317*2139Sjp161948 {
318*2139Sjp161948 spec2len--;
319*2139Sjp161948 len--;
320*2139Sjp161948 }
321*2139Sjp161948 merged = OPENSSL_malloc(len + 2);
322*2139Sjp161948 if(!merged)
323*2139Sjp161948 {
324*2139Sjp161948 DSOerr(DSO_F_DLFCN_MERGER,
325*2139Sjp161948 ERR_R_MALLOC_FAILURE);
326*2139Sjp161948 return(NULL);
327*2139Sjp161948 }
328*2139Sjp161948 strcpy(merged, filespec2);
329*2139Sjp161948 merged[spec2len] = '/';
330*2139Sjp161948 strcpy(&merged[spec2len + 1], filespec1);
331*2139Sjp161948 }
332*2139Sjp161948 return(merged);
333*2139Sjp161948 }
334*2139Sjp161948
dlfcn_name_converter(DSO * dso,const char * filename)3350Sstevel@tonic-gate static char *dlfcn_name_converter(DSO *dso, const char *filename)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate char *translated;
3380Sstevel@tonic-gate int len, rsize, transform;
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate len = strlen(filename);
3410Sstevel@tonic-gate rsize = len + 1;
3420Sstevel@tonic-gate transform = (strstr(filename, "/") == NULL);
3430Sstevel@tonic-gate if(transform)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate /* We will convert this to "%s.so" or "lib%s.so" */
3460Sstevel@tonic-gate rsize += 3; /* The length of ".so" */
3470Sstevel@tonic-gate if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
3480Sstevel@tonic-gate rsize += 3; /* The length of "lib" */
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate translated = OPENSSL_malloc(rsize);
3510Sstevel@tonic-gate if(translated == NULL)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate DSOerr(DSO_F_DLFCN_NAME_CONVERTER,
3540Sstevel@tonic-gate DSO_R_NAME_TRANSLATION_FAILED);
3550Sstevel@tonic-gate return(NULL);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate if(transform)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
3600Sstevel@tonic-gate sprintf(translated, "lib%s.so", filename);
3610Sstevel@tonic-gate else
3620Sstevel@tonic-gate sprintf(translated, "%s.so", filename);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate else
3650Sstevel@tonic-gate sprintf(translated, "%s", filename);
3660Sstevel@tonic-gate return(translated);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate #endif /* DSO_DLFCN */
370