2
0

dso_dl.c 11 KB

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