dso_dlfcn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* dso_dlfcn.c -*- mode:C; c-file-style: "eay" -*- */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/dso.h>
  62. #ifndef DSO_DLFCN
  63. DSO_METHOD *DSO_METHOD_dlfcn(void)
  64. {
  65. return NULL;
  66. }
  67. #else
  68. # ifdef HAVE_DLFCN_H
  69. # include <dlfcn.h>
  70. # endif
  71. /* Part of the hack in "dlfcn_load" ... */
  72. # define DSO_MAX_TRANSLATED_SIZE 256
  73. static int dlfcn_load(DSO *dso);
  74. static int dlfcn_unload(DSO *dso);
  75. static void *dlfcn_bind_var(DSO *dso, const char *symname);
  76. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname);
  77. # if 0
  78. static int dlfcn_unbind(DSO *dso, char *symname, void *symptr);
  79. static int dlfcn_init(DSO *dso);
  80. static int dlfcn_finish(DSO *dso);
  81. static long dlfcn_ctrl(DSO *dso, int cmd, long larg, void *parg);
  82. # endif
  83. static char *dlfcn_name_converter(DSO *dso, const char *filename);
  84. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  85. const char *filespec2);
  86. static DSO_METHOD dso_meth_dlfcn = {
  87. "OpenSSL 'dlfcn' shared library method",
  88. dlfcn_load,
  89. dlfcn_unload,
  90. dlfcn_bind_var,
  91. dlfcn_bind_func,
  92. /* For now, "unbind" doesn't exist */
  93. # if 0
  94. NULL, /* unbind_var */
  95. NULL, /* unbind_func */
  96. # endif
  97. NULL, /* ctrl */
  98. dlfcn_name_converter,
  99. dlfcn_merger,
  100. NULL, /* init */
  101. NULL /* finish */
  102. };
  103. DSO_METHOD *DSO_METHOD_dlfcn(void)
  104. {
  105. return (&dso_meth_dlfcn);
  106. }
  107. /*
  108. * Prior to using the dlopen() function, we should decide on the flag we
  109. * send. There's a few different ways of doing this and it's a messy
  110. * venn-diagram to match up which platforms support what. So as we don't have
  111. * autoconf yet, I'm implementing a hack that could be hacked further
  112. * relatively easily to deal with cases as we find them. Initially this is to
  113. * cope with OpenBSD.
  114. */
  115. # if defined(__OpenBSD__) || defined(__NetBSD__)
  116. # ifdef DL_LAZY
  117. # define DLOPEN_FLAG DL_LAZY
  118. # else
  119. # ifdef RTLD_NOW
  120. # define DLOPEN_FLAG RTLD_NOW
  121. # else
  122. # define DLOPEN_FLAG 0
  123. # endif
  124. # endif
  125. # else
  126. # ifdef OPENSSL_SYS_SUNOS
  127. # define DLOPEN_FLAG 1
  128. # else
  129. # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */
  130. # endif
  131. # endif
  132. /*
  133. * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
  134. * (void*) returned from dlopen().
  135. */
  136. static int dlfcn_load(DSO *dso)
  137. {
  138. void *ptr = NULL;
  139. /* See applicable comments in dso_dl.c */
  140. char *filename = DSO_convert_filename(dso, NULL);
  141. int flags = DLOPEN_FLAG;
  142. if (filename == NULL) {
  143. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_NO_FILENAME);
  144. goto err;
  145. }
  146. # ifdef RTLD_GLOBAL
  147. if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
  148. flags |= RTLD_GLOBAL;
  149. # endif
  150. ptr = dlopen(filename, flags);
  151. if (ptr == NULL) {
  152. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_LOAD_FAILED);
  153. ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
  154. goto err;
  155. }
  156. if (!sk_push(dso->meth_data, (char *)ptr)) {
  157. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_STACK_ERROR);
  158. goto err;
  159. }
  160. /* Success */
  161. dso->loaded_filename = filename;
  162. return (1);
  163. err:
  164. /* Cleanup! */
  165. if (filename != NULL)
  166. OPENSSL_free(filename);
  167. if (ptr != NULL)
  168. dlclose(ptr);
  169. return (0);
  170. }
  171. static int dlfcn_unload(DSO *dso)
  172. {
  173. void *ptr;
  174. if (dso == NULL) {
  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. DSOerr(DSO_F_DLFCN_UNLOAD, DSO_R_NULL_HANDLE);
  183. /*
  184. * Should push the value back onto the stack in case of a retry.
  185. */
  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. DSOerr(DSO_F_DLFCN_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  198. return (NULL);
  199. }
  200. if (sk_num(dso->meth_data) < 1) {
  201. DSOerr(DSO_F_DLFCN_BIND_VAR, DSO_R_STACK_ERROR);
  202. return (NULL);
  203. }
  204. ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  205. if (ptr == NULL) {
  206. DSOerr(DSO_F_DLFCN_BIND_VAR, DSO_R_NULL_HANDLE);
  207. return (NULL);
  208. }
  209. sym = dlsym(ptr, symname);
  210. if (sym == NULL) {
  211. DSOerr(DSO_F_DLFCN_BIND_VAR, DSO_R_SYM_FAILURE);
  212. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  213. return (NULL);
  214. }
  215. return (sym);
  216. }
  217. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
  218. {
  219. void *ptr;
  220. union {
  221. DSO_FUNC_TYPE sym;
  222. void *dlret;
  223. } u;
  224. if ((dso == NULL) || (symname == NULL)) {
  225. DSOerr(DSO_F_DLFCN_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  226. return (NULL);
  227. }
  228. if (sk_num(dso->meth_data) < 1) {
  229. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_STACK_ERROR);
  230. return (NULL);
  231. }
  232. ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  233. if (ptr == NULL) {
  234. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_NULL_HANDLE);
  235. return (NULL);
  236. }
  237. u.dlret = dlsym(ptr, symname);
  238. if (u.dlret == NULL) {
  239. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_SYM_FAILURE);
  240. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  241. return (NULL);
  242. }
  243. return u.sym;
  244. }
  245. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  246. const char *filespec2)
  247. {
  248. char *merged;
  249. if (!filespec1 && !filespec2) {
  250. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  251. return (NULL);
  252. }
  253. /*
  254. * If the first file specification is a rooted path, it rules. same goes
  255. * if the second file specification is missing.
  256. */
  257. if (!filespec2 || filespec1[0] == '/') {
  258. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  259. if (!merged) {
  260. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  261. return (NULL);
  262. }
  263. strcpy(merged, filespec1);
  264. }
  265. /*
  266. * If the first file specification is missing, the second one rules.
  267. */
  268. else if (!filespec1) {
  269. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  270. if (!merged) {
  271. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  272. return (NULL);
  273. }
  274. strcpy(merged, filespec2);
  275. } else
  276. /*
  277. * This part isn't as trivial as it looks. It assumes that the
  278. * second file specification really is a directory, and makes no
  279. * checks whatsoever. Therefore, the result becomes the
  280. * concatenation of filespec2 followed by a slash followed by
  281. * filespec1.
  282. */
  283. {
  284. int spec2len, len;
  285. spec2len = (filespec2 ? strlen(filespec2) : 0);
  286. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  287. if (filespec2 && filespec2[spec2len - 1] == '/') {
  288. spec2len--;
  289. len--;
  290. }
  291. merged = OPENSSL_malloc(len + 2);
  292. if (!merged) {
  293. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  294. return (NULL);
  295. }
  296. strcpy(merged, filespec2);
  297. merged[spec2len] = '/';
  298. strcpy(&merged[spec2len + 1], filespec1);
  299. }
  300. return (merged);
  301. }
  302. # ifdef OPENSSL_SYS_MACOSX
  303. # define DSO_ext ".dylib"
  304. # define DSO_extlen 6
  305. # else
  306. # define DSO_ext ".so"
  307. # define DSO_extlen 3
  308. # endif
  309. static char *dlfcn_name_converter(DSO *dso, const char *filename)
  310. {
  311. char *translated;
  312. int len, rsize, transform;
  313. len = strlen(filename);
  314. rsize = len + 1;
  315. transform = (strstr(filename, "/") == NULL);
  316. if (transform) {
  317. /* We will convert this to "%s.so" or "lib%s.so" etc */
  318. rsize += DSO_extlen; /* The length of ".so" */
  319. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  320. rsize += 3; /* The length of "lib" */
  321. }
  322. translated = OPENSSL_malloc(rsize);
  323. if (translated == NULL) {
  324. DSOerr(DSO_F_DLFCN_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  325. return (NULL);
  326. }
  327. if (transform) {
  328. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  329. sprintf(translated, "lib%s" DSO_ext, filename);
  330. else
  331. sprintf(translated, "%s" DSO_ext, filename);
  332. } else
  333. sprintf(translated, "%s", filename);
  334. return (translated);
  335. }
  336. #endif /* DSO_DLFCN */