dso_dl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* dso_dl.c -*- mode:C; c-file-style: "eay" -*- */
  2. /*
  3. * Written by Richard Levitte (richard@levitte.org) 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_DL
  63. DSO_METHOD *DSO_METHOD_dl(void)
  64. {
  65. return NULL;
  66. }
  67. #else
  68. # include <dl.h>
  69. /* Part of the hack in "dl_load" ... */
  70. # define DSO_MAX_TRANSLATED_SIZE 256
  71. static int dl_load(DSO *dso);
  72. static int dl_unload(DSO *dso);
  73. static void *dl_bind_var(DSO *dso, const char *symname);
  74. static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname);
  75. # if 0
  76. static int dl_unbind_var(DSO *dso, char *symname, void *symptr);
  77. static int dl_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
  78. static int dl_init(DSO *dso);
  79. static int dl_finish(DSO *dso);
  80. static int dl_ctrl(DSO *dso, int cmd, long larg, void *parg);
  81. # endif
  82. static char *dl_name_converter(DSO *dso, const char *filename);
  83. static char *dl_merger(DSO *dso, const char *filespec1,
  84. const char *filespec2);
  85. static DSO_METHOD dso_meth_dl = {
  86. "OpenSSL 'dl' shared library method",
  87. dl_load,
  88. dl_unload,
  89. dl_bind_var,
  90. dl_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. dl_name_converter,
  98. dl_merger,
  99. NULL, /* init */
  100. NULL /* finish */
  101. };
  102. DSO_METHOD *DSO_METHOD_dl(void)
  103. {
  104. return (&dso_meth_dl);
  105. }
  106. /*
  107. * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
  108. * (shl_t) returned from shl_load(). NB: I checked on HPUX11 and shl_t is
  109. * itself a pointer type so the cast is safe.
  110. */
  111. static int dl_load(DSO *dso)
  112. {
  113. shl_t ptr = NULL;
  114. /*
  115. * We don't do any fancy retries or anything, just take the method's (or
  116. * DSO's if it has the callback set) best translation of the
  117. * platform-independant filename and try once with that.
  118. */
  119. char *filename = DSO_convert_filename(dso, NULL);
  120. if (filename == NULL) {
  121. DSOerr(DSO_F_DL_LOAD, DSO_R_NO_FILENAME);
  122. goto err;
  123. }
  124. ptr = shl_load(filename, BIND_IMMEDIATE |
  125. (dso->flags & DSO_FLAG_NO_NAME_TRANSLATION ? 0 :
  126. DYNAMIC_PATH), 0L);
  127. if (ptr == NULL) {
  128. DSOerr(DSO_F_DL_LOAD, DSO_R_LOAD_FAILED);
  129. ERR_add_error_data(4, "filename(", filename, "): ", strerror(errno));
  130. goto err;
  131. }
  132. if (!sk_push(dso->meth_data, (char *)ptr)) {
  133. DSOerr(DSO_F_DL_LOAD, DSO_R_STACK_ERROR);
  134. goto err;
  135. }
  136. /*
  137. * Success, stick the converted filename we've loaded under into the DSO
  138. * (it also serves as the indicator that we are currently loaded).
  139. */
  140. dso->loaded_filename = filename;
  141. return (1);
  142. err:
  143. /* Cleanup! */
  144. if (filename != NULL)
  145. OPENSSL_free(filename);
  146. if (ptr != NULL)
  147. shl_unload(ptr);
  148. return (0);
  149. }
  150. static int dl_unload(DSO *dso)
  151. {
  152. shl_t ptr;
  153. if (dso == NULL) {
  154. DSOerr(DSO_F_DL_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  155. return (0);
  156. }
  157. if (sk_num(dso->meth_data) < 1)
  158. return (1);
  159. /* Is this statement legal? */
  160. ptr = (shl_t) sk_pop(dso->meth_data);
  161. if (ptr == NULL) {
  162. DSOerr(DSO_F_DL_UNLOAD, DSO_R_NULL_HANDLE);
  163. /*
  164. * Should push the value back onto the stack in case of a retry.
  165. */
  166. sk_push(dso->meth_data, (char *)ptr);
  167. return (0);
  168. }
  169. shl_unload(ptr);
  170. return (1);
  171. }
  172. static void *dl_bind_var(DSO *dso, const char *symname)
  173. {
  174. shl_t ptr;
  175. void *sym;
  176. if ((dso == NULL) || (symname == NULL)) {
  177. DSOerr(DSO_F_DL_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  178. return (NULL);
  179. }
  180. if (sk_num(dso->meth_data) < 1) {
  181. DSOerr(DSO_F_DL_BIND_VAR, DSO_R_STACK_ERROR);
  182. return (NULL);
  183. }
  184. ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  185. if (ptr == NULL) {
  186. DSOerr(DSO_F_DL_BIND_VAR, DSO_R_NULL_HANDLE);
  187. return (NULL);
  188. }
  189. if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
  190. DSOerr(DSO_F_DL_BIND_VAR, DSO_R_SYM_FAILURE);
  191. ERR_add_error_data(4, "symname(", symname, "): ", strerror(errno));
  192. return (NULL);
  193. }
  194. return (sym);
  195. }
  196. static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
  197. {
  198. shl_t ptr;
  199. void *sym;
  200. if ((dso == NULL) || (symname == NULL)) {
  201. DSOerr(DSO_F_DL_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  202. return (NULL);
  203. }
  204. if (sk_num(dso->meth_data) < 1) {
  205. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_STACK_ERROR);
  206. return (NULL);
  207. }
  208. ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  209. if (ptr == NULL) {
  210. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_NULL_HANDLE);
  211. return (NULL);
  212. }
  213. if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
  214. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_SYM_FAILURE);
  215. ERR_add_error_data(4, "symname(", symname, "): ", strerror(errno));
  216. return (NULL);
  217. }
  218. return ((DSO_FUNC_TYPE)sym);
  219. }
  220. static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
  221. {
  222. char *merged;
  223. if (!filespec1 && !filespec2) {
  224. DSOerr(DSO_F_DL_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  225. return (NULL);
  226. }
  227. /*
  228. * If the first file specification is a rooted path, it rules. same goes
  229. * if the second file specification is missing.
  230. */
  231. if (!filespec2 || filespec1[0] == '/') {
  232. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  233. if (!merged) {
  234. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  235. return (NULL);
  236. }
  237. strcpy(merged, filespec1);
  238. }
  239. /*
  240. * If the first file specification is missing, the second one rules.
  241. */
  242. else if (!filespec1) {
  243. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  244. if (!merged) {
  245. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  246. return (NULL);
  247. }
  248. strcpy(merged, filespec2);
  249. } else
  250. /*
  251. * This part isn't as trivial as it looks. It assumes that the
  252. * second file specification really is a directory, and makes no
  253. * checks whatsoever. Therefore, the result becomes the
  254. * concatenation of filespec2 followed by a slash followed by
  255. * filespec1.
  256. */
  257. {
  258. int spec2len, len;
  259. spec2len = (filespec2 ? strlen(filespec2) : 0);
  260. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  261. if (filespec2 && filespec2[spec2len - 1] == '/') {
  262. spec2len--;
  263. len--;
  264. }
  265. merged = OPENSSL_malloc(len + 2);
  266. if (!merged) {
  267. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  268. return (NULL);
  269. }
  270. strcpy(merged, filespec2);
  271. merged[spec2len] = '/';
  272. strcpy(&merged[spec2len + 1], filespec1);
  273. }
  274. return (merged);
  275. }
  276. /*
  277. * This function is identical to the one in dso_dlfcn.c, but as it is highly
  278. * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at
  279. * the same time, there's no great duplicating the code. Figuring out an
  280. * elegant way to share one copy of the code would be more difficult and
  281. * would not leave the implementations independant.
  282. */
  283. # if defined(__hpux)
  284. static const char extension[] = ".sl";
  285. # else
  286. static const char extension[] = ".so";
  287. # endif
  288. static char *dl_name_converter(DSO *dso, const char *filename)
  289. {
  290. char *translated;
  291. int len, rsize, transform;
  292. len = strlen(filename);
  293. rsize = len + 1;
  294. transform = (strstr(filename, "/") == NULL);
  295. {
  296. /* We will convert this to "%s.s?" or "lib%s.s?" */
  297. rsize += strlen(extension); /* The length of ".s?" */
  298. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  299. rsize += 3; /* The length of "lib" */
  300. }
  301. translated = OPENSSL_malloc(rsize);
  302. if (translated == NULL) {
  303. DSOerr(DSO_F_DL_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  304. return (NULL);
  305. }
  306. if (transform) {
  307. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  308. sprintf(translated, "lib%s%s", filename, extension);
  309. else
  310. sprintf(translated, "%s%s", filename, extension);
  311. } else
  312. sprintf(translated, "%s", filename);
  313. return (translated);
  314. }
  315. #endif /* DSO_DL */