dlopen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* dlopen.c--Unix dlopen() dynamic loader interface
  2. * Rob Siemborski
  3. * Rob Earhart
  4. */
  5. /*
  6. * Copyright (c) 1998-2016 Carnegie Mellon University. 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. The name "Carnegie Mellon University" must not be used to
  21. * endorse or promote products derived from this software without
  22. * prior written permission. For permission or any other legal
  23. * details, please contact
  24. * Carnegie Mellon University
  25. * Center for Technology Transfer and Enterprise Creation
  26. * 4615 Forbes Avenue
  27. * Suite 302
  28. * Pittsburgh, PA 15213
  29. * (412) 268-7393, fax: (412) 268-7395
  30. * innovation@andrew.cmu.edu
  31. *
  32. * 4. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by Computing Services
  35. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  36. *
  37. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  38. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  39. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  40. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  41. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  42. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  43. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  44. */
  45. #include <config.h>
  46. #ifdef HAVE_DLFCN_H
  47. #include <dlfcn.h>
  48. #endif
  49. #include <stdlib.h>
  50. #include <errno.h>
  51. #include <stdio.h>
  52. #include <limits.h>
  53. #include <sasl.h>
  54. #include "saslint.h"
  55. #ifndef PIC
  56. #include <saslplug.h>
  57. #include "staticopen.h"
  58. #endif
  59. #ifdef DO_DLOPEN
  60. #if HAVE_DIRENT_H
  61. # include <dirent.h>
  62. # define NAMLEN(dirent) strlen((dirent)->d_name)
  63. #else /* HAVE_DIRENT_H */
  64. # define dirent direct
  65. # define NAMLEN(dirent) (dirent)->d_namlen
  66. # if HAVE_SYS_NDIR_H
  67. # include <sys/ndir.h>
  68. # endif
  69. # if HAVE_SYS_DIR_H
  70. # include <sys/dir.h>
  71. # endif
  72. # if HAVE_NDIR_H
  73. # include <ndir.h>
  74. # endif
  75. #endif /* ! HAVE_DIRENT_H */
  76. #ifndef NAME_MAX
  77. # ifdef _POSIX_NAME_MAX
  78. # define NAME_MAX _POSIX_NAME_MAX
  79. # else
  80. # define NAME_MAX 16
  81. # endif
  82. #endif
  83. #if NAME_MAX < 8
  84. # define NAME_MAX 8
  85. #endif
  86. #ifdef __hpux
  87. #ifndef HAVE_DLFCN_H
  88. #include <dl.h>
  89. typedef shl_t * dll_handle;
  90. typedef void * dll_func;
  91. dll_handle
  92. dlopen(char *fname, int mode)
  93. {
  94. shl_t h = shl_load(fname, BIND_DEFERRED, 0L);
  95. shl_t *hp = NULL;
  96. if (h) {
  97. hp = (shl_t *)malloc(sizeof (shl_t));
  98. if (!hp) {
  99. shl_unload(h);
  100. } else {
  101. *hp = h;
  102. }
  103. }
  104. return (dll_handle)hp;
  105. }
  106. int
  107. dlclose(dll_handle hp)
  108. {
  109. shl_t h;
  110. if (hp != NULL) {
  111. h = *((shl_t *)hp);
  112. free(hp);
  113. return shl_unload(h);
  114. } else {
  115. /* Return error */
  116. return -1;
  117. }
  118. }
  119. dll_func
  120. dlsym(dll_handle h, char *n)
  121. {
  122. dll_func handle;
  123. if (shl_findsym ((shl_t *)h, n, TYPE_PROCEDURE, &handle))
  124. return NULL;
  125. return (dll_func)handle;
  126. }
  127. char *dlerror()
  128. {
  129. if (errno != 0) {
  130. return strerror(errno);
  131. }
  132. return "Generic shared library error";
  133. }
  134. #endif /* HAVE_DLFCN_H */
  135. #ifdef __ia64
  136. #define SO_SUFFIX ".so"
  137. #else
  138. #define SO_SUFFIX ".sl"
  139. #endif /* __ia64 */
  140. #else /* __hpux */
  141. #define SO_SUFFIX ".so"
  142. #endif
  143. #define LA_SUFFIX ".la"
  144. typedef struct lib_list
  145. {
  146. struct lib_list *next;
  147. void *library;
  148. } lib_list_t;
  149. static lib_list_t *lib_list_head = NULL;
  150. #endif /* DO_DLOPEN */
  151. int _sasl_locate_entry(void *library, const char *entryname,
  152. void **entry_point)
  153. {
  154. #ifdef DO_DLOPEN
  155. /* note that we still check for known problem systems in
  156. * case we are cross-compiling */
  157. #if defined(DLSYM_NEEDS_UNDERSCORE) || (defined(__OpenBSD__) && !defined(__ELF__))
  158. char adj_entryname[1024];
  159. #else
  160. #define adj_entryname entryname
  161. #endif
  162. if(!entryname) {
  163. _sasl_log(NULL, SASL_LOG_ERR,
  164. "no entryname in _sasl_locate_entry");
  165. return SASL_BADPARAM;
  166. }
  167. if(!library) {
  168. _sasl_log(NULL, SASL_LOG_ERR,
  169. "no library in _sasl_locate_entry");
  170. return SASL_BADPARAM;
  171. }
  172. if(!entry_point) {
  173. _sasl_log(NULL, SASL_LOG_ERR,
  174. "no entrypoint output pointer in _sasl_locate_entry");
  175. return SASL_BADPARAM;
  176. }
  177. #if defined(DLSYM_NEEDS_UNDERSCORE) || (defined(__OpenBSD__) && !defined(__ELF__))
  178. snprintf(adj_entryname, sizeof adj_entryname, "_%s", entryname);
  179. #endif
  180. *entry_point = NULL;
  181. *entry_point = dlsym(library, adj_entryname);
  182. if (*entry_point == NULL) {
  183. #if 0 /* This message appears to confuse people */
  184. _sasl_log(NULL, SASL_LOG_DEBUG,
  185. "unable to get entry point %s: %s", adj_entryname,
  186. dlerror());
  187. #endif
  188. return SASL_FAIL;
  189. }
  190. return SASL_OK;
  191. #else
  192. return SASL_FAIL;
  193. #endif /* DO_DLOPEN */
  194. }
  195. #ifdef DO_DLOPEN
  196. static int _sasl_plugin_load(char *plugin, void *library,
  197. const char *entryname,
  198. int (*add_plugin)(const char *, void *))
  199. {
  200. void *entry_point;
  201. int result;
  202. result = _sasl_locate_entry(library, entryname, &entry_point);
  203. if(result == SASL_OK) {
  204. result = add_plugin(plugin, entry_point);
  205. if(result != SASL_OK)
  206. _sasl_log(NULL, SASL_LOG_DEBUG,
  207. "_sasl_plugin_load failed on %s for plugin: %s\n",
  208. entryname, plugin);
  209. }
  210. return result;
  211. }
  212. /* this returns the file to actually open.
  213. * out should be a buffer of size PATH_MAX
  214. * and may be the same as in. */
  215. /* We'll use a static buffer for speed unless someone complains */
  216. #define MAX_LINE 2048
  217. static int _parse_la(const char *prefix, const char *in, char *out)
  218. {
  219. FILE *file;
  220. size_t length;
  221. char line[MAX_LINE];
  222. char *ntmp = NULL;
  223. if(!in || !out || !prefix || out == in) return SASL_BADPARAM;
  224. /* Set this so we can detect failure */
  225. *out = '\0';
  226. length = strlen(in);
  227. if (strcmp(in + (length - strlen(LA_SUFFIX)), LA_SUFFIX)) {
  228. if(!strcmp(in + (length - strlen(SO_SUFFIX)),SO_SUFFIX)) {
  229. /* check for a .la file */
  230. if (strlen(prefix) + strlen(in) + strlen(LA_SUFFIX) + 1 >= MAX_LINE)
  231. return SASL_BADPARAM;
  232. strcpy(line, prefix);
  233. strcat(line, in);
  234. length = strlen(line);
  235. *(line + (length - strlen(SO_SUFFIX))) = '\0';
  236. strcat(line, LA_SUFFIX);
  237. file = fopen(line, "r");
  238. if(file) {
  239. /* We'll get it on the .la open */
  240. fclose(file);
  241. return SASL_FAIL;
  242. }
  243. }
  244. if (strlen(prefix) + strlen(in) + 1 >= PATH_MAX)
  245. return SASL_BADPARAM;
  246. strcpy(out, prefix);
  247. strcat(out, in);
  248. return SASL_OK;
  249. }
  250. if (strlen(prefix) + strlen(in) + 1 >= MAX_LINE)
  251. return SASL_BADPARAM;
  252. strcpy(line, prefix);
  253. strcat(line, in);
  254. file = fopen(line, "r");
  255. if(!file) {
  256. _sasl_log(NULL, SASL_LOG_WARN,
  257. "unable to open LA file: %s", line);
  258. return SASL_FAIL;
  259. }
  260. while(!feof(file)) {
  261. if(!fgets(line, MAX_LINE, file)) break;
  262. if(line[strlen(line) - 1] != '\n') {
  263. _sasl_log(NULL, SASL_LOG_WARN,
  264. "LA file has too long of a line: %s", in);
  265. fclose(file);
  266. return SASL_BUFOVER;
  267. }
  268. if(line[0] == '\n' || line[0] == '#') continue;
  269. if(!strncmp(line, "dlname=", sizeof("dlname=") - 1)) {
  270. /* We found the line with the name in it */
  271. char *end;
  272. char *start;
  273. size_t len;
  274. end = strrchr(line, '\'');
  275. if(!end) continue;
  276. start = &line[sizeof("dlname=")-1];
  277. len = strlen(start);
  278. if(len > 3 && start[0] == '\'') {
  279. ntmp=&start[1];
  280. *end='\0';
  281. /* Do we have dlname="" ? */
  282. if(ntmp == end) {
  283. _sasl_log(NULL, SASL_LOG_DEBUG,
  284. "dlname is empty in .la file: %s", in);
  285. fclose(file);
  286. return SASL_FAIL;
  287. }
  288. strcpy(out, prefix);
  289. strcat(out, ntmp);
  290. }
  291. break;
  292. }
  293. }
  294. if(ferror(file) || feof(file)) {
  295. _sasl_log(NULL, SASL_LOG_WARN,
  296. "Error reading .la: %s\n", in);
  297. fclose(file);
  298. return SASL_FAIL;
  299. }
  300. fclose(file);
  301. if(!(*out)) {
  302. _sasl_log(NULL, SASL_LOG_WARN,
  303. "Could not find a dlname line in .la file: %s", in);
  304. return SASL_FAIL;
  305. }
  306. return SASL_OK;
  307. }
  308. #endif /* DO_DLOPEN */
  309. /* loads a plugin library */
  310. int _sasl_get_plugin(const char *file,
  311. const sasl_callback_t *verifyfile_cb,
  312. void **libraryptr)
  313. {
  314. #ifdef DO_DLOPEN
  315. int r = 0;
  316. int flag;
  317. void *library;
  318. lib_list_t *newhead;
  319. r = ((sasl_verifyfile_t *)(verifyfile_cb->proc))
  320. (verifyfile_cb->context, file, SASL_VRFY_PLUGIN);
  321. if (r != SASL_OK) return r;
  322. #ifdef RTLD_NOW
  323. flag = RTLD_NOW;
  324. #else
  325. flag = 0;
  326. #endif
  327. newhead = sasl_ALLOC(sizeof(lib_list_t));
  328. if(!newhead) return SASL_NOMEM;
  329. if (!(library = dlopen(file, flag))) {
  330. _sasl_log(NULL, SASL_LOG_ERR,
  331. "unable to dlopen %s: %s", file, dlerror());
  332. sasl_FREE(newhead);
  333. return SASL_FAIL;
  334. }
  335. newhead->library = library;
  336. newhead->next = lib_list_head;
  337. lib_list_head = newhead;
  338. *libraryptr = library;
  339. return SASL_OK;
  340. #else
  341. return SASL_FAIL;
  342. #endif /* DO_DLOPEN */
  343. }
  344. /* gets the list of mechanisms */
  345. int _sasl_load_plugins(const add_plugin_list_t *entrypoints,
  346. const sasl_callback_t *getpath_cb,
  347. const sasl_callback_t *verifyfile_cb)
  348. {
  349. int result;
  350. const add_plugin_list_t *cur_ep;
  351. #ifdef DO_DLOPEN
  352. char str[PATH_MAX], tmp[PATH_MAX+2], prefix[PATH_MAX+2];
  353. /* 1 for '/' 1 for trailing '\0' */
  354. char c;
  355. int pos;
  356. const char *path=NULL;
  357. int position;
  358. DIR *dp;
  359. struct dirent *dir;
  360. #endif
  361. #ifndef PIC
  362. add_plugin_t *add_plugin;
  363. _sasl_plug_type type;
  364. _sasl_plug_rec *p;
  365. #endif
  366. if (! entrypoints
  367. || ! getpath_cb
  368. || getpath_cb->id != SASL_CB_GETPATH
  369. || ! getpath_cb->proc
  370. || ! verifyfile_cb
  371. || verifyfile_cb->id != SASL_CB_VERIFYFILE
  372. || ! verifyfile_cb->proc)
  373. return SASL_BADPARAM;
  374. #ifndef PIC
  375. /* do all the static plugins first */
  376. for(cur_ep = entrypoints; cur_ep->entryname; cur_ep++) {
  377. /* What type of plugin are we looking for? */
  378. if(!strcmp(cur_ep->entryname, "sasl_server_plug_init")) {
  379. type = SERVER;
  380. add_plugin = (add_plugin_t *)sasl_server_add_plugin;
  381. } else if (!strcmp(cur_ep->entryname, "sasl_client_plug_init")) {
  382. type = CLIENT;
  383. add_plugin = (add_plugin_t *)sasl_client_add_plugin;
  384. } else if (!strcmp(cur_ep->entryname, "sasl_auxprop_plug_init")) {
  385. type = AUXPROP;
  386. add_plugin = (add_plugin_t *)sasl_auxprop_add_plugin;
  387. } else if (!strcmp(cur_ep->entryname, "sasl_canonuser_init")) {
  388. type = CANONUSER;
  389. add_plugin = (add_plugin_t *)sasl_canonuser_add_plugin;
  390. } else {
  391. /* What are we looking for then? */
  392. return SASL_FAIL;
  393. }
  394. for (p=_sasl_static_plugins; p->type; p++) {
  395. if(type == p->type)
  396. result = add_plugin(p->name, p->plug);
  397. }
  398. }
  399. #endif /* !PIC */
  400. /* only do the following if:
  401. *
  402. * we support dlopen()
  403. * AND we are not staticly compiled
  404. * OR we are staticly compiled and TRY_DLOPEN_WHEN_STATIC is defined
  405. */
  406. #if defined(DO_DLOPEN) && (defined(PIC) || (!defined(PIC) && defined(TRY_DLOPEN_WHEN_STATIC)))
  407. /* get the path to the plugins */
  408. result = ((sasl_getpath_t *)(getpath_cb->proc))(getpath_cb->context,
  409. &path);
  410. if (result != SASL_OK) return result;
  411. if (! path) return SASL_FAIL;
  412. if (strlen(path) >= PATH_MAX) { /* no you can't buffer overrun */
  413. return SASL_FAIL;
  414. }
  415. position=0;
  416. do {
  417. pos=0;
  418. do {
  419. c=path[position];
  420. position++;
  421. str[pos]=c;
  422. pos++;
  423. } while ((c!=':') && (c!='=') && (c!=0));
  424. str[pos-1]='\0';
  425. strcpy(prefix,str);
  426. strcat(prefix,"/");
  427. if ((dp=opendir(str)) !=NULL) /* ignore errors */
  428. {
  429. while ((dir=readdir(dp)) != NULL)
  430. {
  431. size_t length;
  432. void *library;
  433. char *c;
  434. char plugname[PATH_MAX];
  435. char name[PATH_MAX];
  436. length = NAMLEN(dir);
  437. if (length < 4)
  438. continue; /* can not possibly be what we're looking for */
  439. if (length + pos>=PATH_MAX) continue; /* too big */
  440. if (strcmp(dir->d_name + (length - strlen(SO_SUFFIX)),
  441. SO_SUFFIX)
  442. && strcmp(dir->d_name + (length - strlen(LA_SUFFIX)),
  443. LA_SUFFIX))
  444. continue;
  445. memcpy(name,dir->d_name,length);
  446. name[length]='\0';
  447. result = _parse_la(prefix, name, tmp);
  448. if(result != SASL_OK)
  449. continue;
  450. /* skip "lib" and cut off suffix --
  451. this only need be approximate */
  452. strcpy(plugname, name + 3);
  453. c = strchr(plugname, (int)'.');
  454. if(c) *c = '\0';
  455. result = _sasl_get_plugin(tmp, verifyfile_cb, &library);
  456. if(result != SASL_OK)
  457. continue;
  458. for(cur_ep = entrypoints; cur_ep->entryname; cur_ep++) {
  459. _sasl_plugin_load(plugname, library, cur_ep->entryname,
  460. cur_ep->add_plugin);
  461. /* If this fails, it's not the end of the world */
  462. }
  463. }
  464. closedir(dp);
  465. } else {
  466. _sasl_log(NULL, SASL_LOG_DEBUG,
  467. "looking for plugins in '%s', failed to open directory, error: %s",
  468. str,
  469. strerror(errno));
  470. }
  471. } while ((c!='=') && (c!=0));
  472. #endif /* defined(DO_DLOPEN) && (!defined(PIC) || (defined(PIC) && defined(TRY_DLOPEN_WHEN_STATIC))) */
  473. return SASL_OK;
  474. }
  475. int
  476. _sasl_done_with_plugins(void)
  477. {
  478. #ifdef DO_DLOPEN
  479. lib_list_t *libptr, *libptr_next;
  480. for(libptr = lib_list_head; libptr; libptr = libptr_next) {
  481. libptr_next = libptr->next;
  482. if(libptr->library)
  483. dlclose(libptr->library);
  484. sasl_FREE(libptr);
  485. }
  486. lib_list_head = NULL;
  487. #endif /* DO_DLOPEN */
  488. return SASL_OK;
  489. }