dso_dlfcn.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* dso_dlfcn.c -*- mode:C; c-file-style: "eay" -*- */
  2. /* Written by Geoff Thorpe (geoff@geoffthorpe.net) 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. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/dso.h>
  61. #ifndef DSO_DLFCN
  62. DSO_METHOD *DSO_METHOD_dlfcn(void)
  63. {
  64. return NULL;
  65. }
  66. #else
  67. #ifdef HAVE_DLFCN_H
  68. #include <dlfcn.h>
  69. #endif
  70. /* Part of the hack in "dlfcn_load" ... */
  71. #define DSO_MAX_TRANSLATED_SIZE 256
  72. static int dlfcn_load(DSO *dso);
  73. static int dlfcn_unload(DSO *dso);
  74. static void *dlfcn_bind_var(DSO *dso, const char *symname);
  75. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname);
  76. #if 0
  77. static int dlfcn_unbind(DSO *dso, char *symname, void *symptr);
  78. static int dlfcn_init(DSO *dso);
  79. static int dlfcn_finish(DSO *dso);
  80. static long dlfcn_ctrl(DSO *dso, int cmd, long larg, void *parg);
  81. #endif
  82. static char *dlfcn_name_converter(DSO *dso, const char *filename);
  83. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  84. const char *filespec2);
  85. static DSO_METHOD dso_meth_dlfcn = {
  86. "OpenSSL 'dlfcn' shared library method",
  87. dlfcn_load,
  88. dlfcn_unload,
  89. dlfcn_bind_var,
  90. dlfcn_bind_func,
  91. /* For now, "unbind" doesn't exist */
  92. #if 0
  93. NULL, /* unbind_var */
  94. NULL, /* unbind_func */
  95. #endif
  96. NULL, /* ctrl */
  97. dlfcn_name_converter,
  98. dlfcn_merger,
  99. NULL, /* init */
  100. NULL /* finish */
  101. };
  102. DSO_METHOD *DSO_METHOD_dlfcn(void)
  103. {
  104. return(&dso_meth_dlfcn);
  105. }
  106. /* Prior to using the dlopen() function, we should decide on the flag
  107. * we send. There's a few different ways of doing this and it's a
  108. * messy venn-diagram to match up which platforms support what. So
  109. * as we don't have autoconf yet, I'm implementing a hack that could
  110. * be hacked further relatively easily to deal with cases as we find
  111. * them. Initially this is to cope with OpenBSD. */
  112. #if defined(__OpenBSD__) || defined(__NetBSD__)
  113. # ifdef DL_LAZY
  114. # define DLOPEN_FLAG DL_LAZY
  115. # else
  116. # ifdef RTLD_NOW
  117. # define DLOPEN_FLAG RTLD_NOW
  118. # else
  119. # define DLOPEN_FLAG 0
  120. # endif
  121. # endif
  122. #else
  123. # ifdef OPENSSL_SYS_SUNOS
  124. # define DLOPEN_FLAG 1
  125. # else
  126. # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */
  127. # endif
  128. #endif
  129. /* For this DSO_METHOD, our meth_data STACK will contain;
  130. * (i) the handle (void*) returned from dlopen().
  131. */
  132. static int dlfcn_load(DSO *dso)
  133. {
  134. void *ptr = NULL;
  135. /* See applicable comments in dso_dl.c */
  136. char *filename = DSO_convert_filename(dso, NULL);
  137. int flags = DLOPEN_FLAG;
  138. if(filename == NULL)
  139. {
  140. DSOerr(DSO_F_DLFCN_LOAD,DSO_R_NO_FILENAME);
  141. goto err;
  142. }
  143. #ifdef RTLD_GLOBAL
  144. if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
  145. flags |= RTLD_GLOBAL;
  146. #endif
  147. ptr = dlopen(filename, flags);
  148. if(ptr == NULL)
  149. {
  150. DSOerr(DSO_F_DLFCN_LOAD,DSO_R_LOAD_FAILED);
  151. ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
  152. goto err;
  153. }
  154. if(!sk_push(dso->meth_data, (char *)ptr))
  155. {
  156. DSOerr(DSO_F_DLFCN_LOAD,DSO_R_STACK_ERROR);
  157. goto err;
  158. }
  159. /* Success */
  160. dso->loaded_filename = filename;
  161. return(1);
  162. err:
  163. /* Cleanup! */
  164. if(filename != NULL)
  165. OPENSSL_free(filename);
  166. if(ptr != NULL)
  167. dlclose(ptr);
  168. return(0);
  169. }
  170. static int dlfcn_unload(DSO *dso)
  171. {
  172. void *ptr;
  173. if(dso == NULL)
  174. {
  175. DSOerr(DSO_F_DLFCN_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
  176. return(0);
  177. }
  178. if(sk_num(dso->meth_data) < 1)
  179. return(1);
  180. ptr = (void *)sk_pop(dso->meth_data);
  181. if(ptr == NULL)
  182. {
  183. DSOerr(DSO_F_DLFCN_UNLOAD,DSO_R_NULL_HANDLE);
  184. /* Should push the value back onto the stack in
  185. * case of a retry. */
  186. sk_push(dso->meth_data, (char *)ptr);
  187. return(0);
  188. }
  189. /* For now I'm not aware of any errors associated with dlclose() */
  190. dlclose(ptr);
  191. return(1);
  192. }
  193. static void *dlfcn_bind_var(DSO *dso, const char *symname)
  194. {
  195. void *ptr, *sym;
  196. if((dso == NULL) || (symname == NULL))
  197. {
  198. DSOerr(DSO_F_DLFCN_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
  199. return(NULL);
  200. }
  201. if(sk_num(dso->meth_data) < 1)
  202. {
  203. DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_STACK_ERROR);
  204. return(NULL);
  205. }
  206. ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  207. if(ptr == NULL)
  208. {
  209. DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_NULL_HANDLE);
  210. return(NULL);
  211. }
  212. sym = dlsym(ptr, symname);
  213. if(sym == NULL)
  214. {
  215. DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_SYM_FAILURE);
  216. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  217. return(NULL);
  218. }
  219. return(sym);
  220. }
  221. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
  222. {
  223. void *ptr;
  224. DSO_FUNC_TYPE sym, *tsym = &sym;
  225. if((dso == NULL) || (symname == NULL))
  226. {
  227. DSOerr(DSO_F_DLFCN_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
  228. return(NULL);
  229. }
  230. if(sk_num(dso->meth_data) < 1)
  231. {
  232. DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_STACK_ERROR);
  233. return(NULL);
  234. }
  235. ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  236. if(ptr == NULL)
  237. {
  238. DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_NULL_HANDLE);
  239. return(NULL);
  240. }
  241. *(void **)(tsym) = dlsym(ptr, symname);
  242. if(sym == NULL)
  243. {
  244. DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_SYM_FAILURE);
  245. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  246. return(NULL);
  247. }
  248. return(sym);
  249. }
  250. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  251. const char *filespec2)
  252. {
  253. char *merged;
  254. if(!filespec1 && !filespec2)
  255. {
  256. DSOerr(DSO_F_DLFCN_MERGER,
  257. ERR_R_PASSED_NULL_PARAMETER);
  258. return(NULL);
  259. }
  260. /* If the first file specification is a rooted path, it rules.
  261. same goes if the second file specification is missing. */
  262. if (!filespec2 || filespec1[0] == '/')
  263. {
  264. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  265. if(!merged)
  266. {
  267. DSOerr(DSO_F_DLFCN_MERGER,
  268. ERR_R_MALLOC_FAILURE);
  269. return(NULL);
  270. }
  271. strcpy(merged, filespec1);
  272. }
  273. /* If the first file specification is missing, the second one rules. */
  274. else if (!filespec1)
  275. {
  276. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  277. if(!merged)
  278. {
  279. DSOerr(DSO_F_DLFCN_MERGER,
  280. ERR_R_MALLOC_FAILURE);
  281. return(NULL);
  282. }
  283. strcpy(merged, filespec2);
  284. }
  285. else
  286. /* This part isn't as trivial as it looks. It assumes that
  287. the second file specification really is a directory, and
  288. makes no checks whatsoever. Therefore, the result becomes
  289. the concatenation of filespec2 followed by a slash followed
  290. by filespec1. */
  291. {
  292. int spec2len, len;
  293. spec2len = (filespec2 ? strlen(filespec2) : 0);
  294. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  295. if(filespec2 && filespec2[spec2len - 1] == '/')
  296. {
  297. spec2len--;
  298. len--;
  299. }
  300. merged = OPENSSL_malloc(len + 2);
  301. if(!merged)
  302. {
  303. DSOerr(DSO_F_DLFCN_MERGER,
  304. ERR_R_MALLOC_FAILURE);
  305. return(NULL);
  306. }
  307. strcpy(merged, filespec2);
  308. merged[spec2len] = '/';
  309. strcpy(&merged[spec2len + 1], filespec1);
  310. }
  311. return(merged);
  312. }
  313. static char *dlfcn_name_converter(DSO *dso, const char *filename)
  314. {
  315. char *translated;
  316. int len, rsize, transform;
  317. len = strlen(filename);
  318. rsize = len + 1;
  319. transform = (strstr(filename, "/") == NULL);
  320. if(transform)
  321. {
  322. /* We will convert this to "%s.so" or "lib%s.so" */
  323. rsize += 3; /* The length of ".so" */
  324. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  325. rsize += 3; /* The length of "lib" */
  326. }
  327. translated = OPENSSL_malloc(rsize);
  328. if(translated == NULL)
  329. {
  330. DSOerr(DSO_F_DLFCN_NAME_CONVERTER,
  331. DSO_R_NAME_TRANSLATION_FAILED);
  332. return(NULL);
  333. }
  334. if(transform)
  335. {
  336. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  337. sprintf(translated, "lib%s.so", filename);
  338. else
  339. sprintf(translated, "%s.so", filename);
  340. }
  341. else
  342. sprintf(translated, "%s", filename);
  343. return(translated);
  344. }
  345. #endif /* DSO_DLFCN */