ldapdb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #ifndef DONT_USE_LDAP_SSL
  42. #define USE_LDAP_SSL
  43. #endif
  44. #include <string.h>
  45. #include <malloc.h>
  46. #include <nspr.h>
  47. #include <prthread.h>
  48. #include <prmon.h>
  49. #include "ldaputil/errors.h"
  50. #include "ldaputil/certmap.h"
  51. #include "ldaputil/ldapdb.h"
  52. #ifdef USE_LDAP_SSL
  53. /* removed for new ns security integration
  54. #include <sec.h>
  55. #include <key.h>
  56. #include "cert.h"
  57. */
  58. #include <ssl.h>
  59. #include "ldap_ssl.h"
  60. #endif
  61. #include "ldaputili.h"
  62. #define LDAPDB_PREFIX_WITH_SLASHES "ldapdb://"
  63. #define LDAPDB_PREFIX_WITH_SLASHES_LEN 9
  64. uintn tsdindex;
  65. static void ldb_crit_init (LDAPDatabase_t *ldb)
  66. {
  67. ldb->crit = PR_NewMonitor();
  68. }
  69. static void ldb_crit_enter (LDAPDatabase_t *ldb)
  70. {
  71. PR_EnterMonitor(ldb->crit);
  72. }
  73. static void ldb_crit_exit (LDAPDatabase_t *ldb)
  74. {
  75. PR_ExitMonitor(ldb->crit);
  76. }
  77. struct ldap_error {
  78. int le_errno;
  79. char *le_matched;
  80. char *le_errmsg;
  81. };
  82. static void set_ld_error( int err, char *matched, char *errmsg, void *dummy )
  83. {
  84. struct ldap_error *le;
  85. if (!(le = (struct ldap_error *) PR_GetThreadPrivate(tsdindex))) {
  86. le = (struct ldap_error *) malloc(sizeof(struct ldap_error));
  87. memset((void *)le, 0, sizeof(struct ldap_error));
  88. PR_SetThreadPrivate(tsdindex, (void *)le);
  89. }
  90. le->le_errno = err;
  91. if ( le->le_matched != NULL ) {
  92. ldap_memfree( le->le_matched );
  93. }
  94. le->le_matched = matched;
  95. if ( le->le_errmsg != NULL ) {
  96. ldap_memfree( le->le_errmsg );
  97. }
  98. le->le_errmsg = errmsg;
  99. }
  100. static int get_ld_error( char **matched, char **errmsg, void *dummy )
  101. {
  102. struct ldap_error *le;
  103. le = (struct ldap_error *) PR_GetThreadPrivate( tsdindex);
  104. if ( matched != NULL ) {
  105. *matched = le->le_matched;
  106. }
  107. if ( errmsg != NULL ) {
  108. *errmsg = le->le_errmsg;
  109. }
  110. return( le->le_errno );
  111. }
  112. static void set_errno( int err )
  113. {
  114. PR_SetError( err, 0);
  115. }
  116. static int get_errno( void )
  117. {
  118. return( PR_GetError() );
  119. }
  120. #ifdef LDAP_OPT_DNS_FN_PTRS /* not supported in older LDAP SDKs */
  121. static LDAPHostEnt *
  122. ldapu_copyPRHostEnt2LDAPHostEnt( LDAPHostEnt *ldhp, PRHostEnt *prhp )
  123. {
  124. ldhp->ldaphe_name = prhp->h_name;
  125. ldhp->ldaphe_aliases = prhp->h_aliases;
  126. ldhp->ldaphe_addrtype = prhp->h_addrtype;
  127. ldhp->ldaphe_length = prhp->h_length;
  128. ldhp->ldaphe_addr_list = prhp->h_addr_list;
  129. return( ldhp );
  130. }
  131. static LDAPHostEnt *
  132. ldapu_gethostbyname( const char *name, LDAPHostEnt *result,
  133. char *buffer, int buflen, int *statusp, void *extradata )
  134. {
  135. PRHostEnt prhent;
  136. if( !statusp || ( *statusp = (int)PR_GetHostByName( name, buffer,
  137. buflen, &prhent )) == PR_FAILURE ) {
  138. return( NULL );
  139. }
  140. return( ldapu_copyPRHostEnt2LDAPHostEnt( result, &prhent ));
  141. }
  142. static LDAPHostEnt *
  143. ldapu_gethostbyaddr( const char *addr, int length, int type,
  144. LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
  145. void *extradata )
  146. {
  147. return( (LDAPHostEnt *)PR_GetError() );
  148. }
  149. #endif /* LDAP_OPT_DNS_FN_PTRS */
  150. static void unescape_ldap_basedn (char *str)
  151. {
  152. if (strchr(str, '%')) {
  153. register int x = 0, y = 0;
  154. int l = strlen(str);
  155. char digit;
  156. while(x < l) {
  157. if((str[x] == '%') && (x < (l - 2))) {
  158. ++x;
  159. digit = (str[x] >= 'A' ?
  160. ((str[x] & 0xdf) - 'A')+10 : (str[x] - '0'));
  161. digit *= 16;
  162. ++x;
  163. digit += (str[x] >= 'A' ?
  164. ((str[x] & 0xdf) - 'A')+10 : (str[x] - '0'));
  165. str[y] = digit;
  166. }
  167. else {
  168. str[y] = str[x];
  169. }
  170. x++;
  171. y++;
  172. }
  173. str[y] = '\0';
  174. }
  175. }
  176. /*
  177. * extract_path_and_basedn:
  178. * Description:
  179. * Parses the ldapdb url and returns pathname to the lcache.conf file
  180. * and basedn. The caller must free the memory allocated for the
  181. * returned path and the basedn.
  182. * Arguments:
  183. * url URL (must begin with ldapdb://)
  184. * path Pathname to the lcache.conf file
  185. * basedn basedn for the ldapdb.
  186. * Return Values: (same as ldap_find)
  187. * LDAPU_SUCCESS if the URL is parsed successfully
  188. * <rv> if error, one of the LDAPU_ errors.
  189. */
  190. static int extract_path_and_basedn(const char *url_in, char **path_out,
  191. char **basedn_out)
  192. {
  193. char *url = strdup(url_in);
  194. char *basedn;
  195. char *path;
  196. *path_out = 0;
  197. *basedn_out = 0;
  198. if (!url) return LDAPU_ERR_OUT_OF_MEMORY;
  199. if (strncmp(url, LDAPDB_URL_PREFIX, LDAPDB_URL_PREFIX_LEN)) {
  200. free(url);
  201. return LDAPU_ERR_URL_INVALID_PREFIX;
  202. }
  203. path = url + LDAPDB_URL_PREFIX_LEN;
  204. if (strncmp(path, "//", 2)) {
  205. free(url);
  206. return LDAPU_ERR_URL_INVALID_PREFIX;
  207. }
  208. path += 2;
  209. /* Find base DN -- empty string is OK */
  210. if ((basedn = strrchr(path, '/')) == NULL) {
  211. free(url);
  212. return LDAPU_ERR_URL_NO_BASEDN;
  213. }
  214. *basedn++ = '\0'; /* terminate the path */
  215. unescape_ldap_basedn(basedn);
  216. *basedn_out = strdup(basedn);
  217. *path_out = strdup(path);
  218. free(url);
  219. return (*basedn_out && *path_out) ? LDAPU_SUCCESS : LDAPU_ERR_OUT_OF_MEMORY;
  220. }
  221. NSAPI_PUBLIC int ldapu_ldapdb_url_parse (const char *url,
  222. LDAPDatabase_t **ldb_out)
  223. {
  224. char *path = 0;
  225. char *basedn = 0;
  226. LDAPDatabase_t *ldb = 0;
  227. int rv;
  228. rv = extract_path_and_basedn(url, &path, &basedn);
  229. if (rv != LDAPU_SUCCESS) {
  230. if (path) free(path);
  231. if (basedn) free(basedn);
  232. return rv;
  233. }
  234. ldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  235. if (!ldb) {
  236. if (path) free(path);
  237. if (basedn) free(basedn);
  238. return LDAPU_ERR_OUT_OF_MEMORY;
  239. }
  240. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  241. ldb->basedn = basedn; /* extract_path_and_basedn has allocated */
  242. ldb->host = path; /* memory for us -- don't make a copy */
  243. ldb_crit_init(ldb);
  244. *ldb_out = ldb;
  245. return LDAPU_SUCCESS;
  246. }
  247. /*
  248. * ldapu_url_parse:
  249. * Description:
  250. * Parses the ldapdb or ldap url and returns a LDAPDatabase_t struct
  251. * Arguments:
  252. * url URL (must begin with ldapdb://)
  253. * binddn DN to use to bind to ldap server.
  254. * bindpw Password to use to bind to ldap server.
  255. * ldb a LDAPDatabase_t struct filled from parsing
  256. * the url.
  257. * Return Values: (same as ldap_find)
  258. * LDAPU_SUCCESS if the URL is parsed successfully
  259. * <rv> if error, one of the LDAPU_ errors.
  260. */
  261. NSAPI_PUBLIC int ldapu_url_parse (const char *url, const char *binddn,
  262. const char *bindpw,
  263. LDAPDatabase_t **ldb_out)
  264. {
  265. LDAPDatabase_t *ldb;
  266. LDAPURLDesc *ludp = 0;
  267. int rv;
  268. *ldb_out = 0;
  269. if (!strncmp(url, LDAPDB_PREFIX_WITH_SLASHES,
  270. LDAPDB_PREFIX_WITH_SLASHES_LEN))
  271. {
  272. return ldapu_ldapdb_url_parse(url, ldb_out);
  273. }
  274. /* call ldapsdk's parse function */
  275. rv = ldap_url_parse((char *)url, &ludp);
  276. if (rv != LDAP_SUCCESS) {
  277. if (ludp) ldap_free_urldesc(ludp);
  278. return LDAPU_ERR_URL_PARSE_FAILED;
  279. }
  280. ldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  281. if (!ldb) {
  282. ldap_free_urldesc(ludp);
  283. return LDAPU_ERR_OUT_OF_MEMORY;
  284. }
  285. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  286. ldb->host = ludp->lud_host ? strdup(ludp->lud_host) : 0;
  287. ldb->use_ssl = ludp->lud_options & LDAP_URL_OPT_SECURE;
  288. ldb->port = ludp->lud_port ? ludp->lud_port : ldb->use_ssl ? 636 : 389;
  289. ldb->basedn = ludp->lud_dn ? strdup(ludp->lud_dn) : 0;
  290. ldb_crit_init(ldb);
  291. ldap_free_urldesc(ludp);
  292. if (binddn) ldb->binddn = strdup(binddn);
  293. if (bindpw) ldb->bindpw = strdup(bindpw);
  294. /* success */
  295. *ldb_out = ldb;
  296. return LDAPU_SUCCESS;
  297. }
  298. NSAPI_PUBLIC void ldapu_free_LDAPDatabase_t (LDAPDatabase_t *ldb)
  299. {
  300. if (ldb->host) free(ldb->host);
  301. if (ldb->basedn) free(ldb->basedn);
  302. if (ldb->filter) free(ldb->filter);
  303. if (ldb->binddn) free(ldb->binddn);
  304. if (ldb->bindpw) free(ldb->bindpw);
  305. if (ldb->ld) ldapu_unbind(ldb->ld);
  306. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  307. free(ldb);
  308. }
  309. NSAPI_PUBLIC LDAPDatabase_t *ldapu_copy_LDAPDatabase_t (const LDAPDatabase_t *ldb)
  310. {
  311. LDAPDatabase_t *nldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  312. if (!nldb) return 0;
  313. memset((void *)nldb, 0, sizeof(LDAPDatabase_t));
  314. nldb->use_ssl = ldb->use_ssl;
  315. if (ldb->host) nldb->host = strdup(ldb->host);
  316. nldb->port = ldb->port;
  317. if (ldb->basedn) nldb->basedn = strdup(ldb->basedn);
  318. nldb->scope = ldb->scope;
  319. if (ldb->filter) nldb->filter = strdup(ldb->filter);
  320. nldb->ld = 0;
  321. if (ldb->binddn) nldb->binddn = strdup(ldb->binddn);
  322. if (ldb->bindpw) nldb->bindpw = strdup(ldb->bindpw);
  323. nldb->bound = 0;
  324. ldb_crit_init(nldb);
  325. return nldb;
  326. }
  327. NSAPI_PUBLIC int ldapu_is_local_db (const LDAPDatabase_t *ldb)
  328. {
  329. return ldb->port ? 0 : 1;
  330. }
  331. static int LDAP_CALL LDAP_CALLBACK
  332. ldapu_rebind_proc (LDAP *ld, char **whop, char **passwdp,
  333. int *authmethodp, int freeit, void *arg)
  334. {
  335. if (freeit == 0) {
  336. LDAPDatabase_t *ldb = (LDAPDatabase_t *)arg;
  337. *whop = ldb->binddn;
  338. *passwdp = ldb->bindpw;
  339. *authmethodp = LDAP_AUTH_SIMPLE;
  340. }
  341. return LDAP_SUCCESS;
  342. }
  343. NSAPI_PUBLIC int ldapu_ldap_init(LDAPDatabase_t *ldb)
  344. {
  345. LDAP *ld = 0;
  346. ldb_crit_enter(ldb);
  347. #ifdef USE_LDAP_SSL
  348. /* Note. This assume the security related initialization is done */
  349. /* The step needed is :
  350. PR_Init
  351. RNG_SystemInfoForRNG
  352. RNG_RNGInit
  353. CERT_OpenCertDBFilename
  354. CERT_SetDefaultCertDB
  355. SECMOD_init
  356. And because ldapssl_init depends on security initialization, it is
  357. no good for non-ssl init
  358. */
  359. if (ldb->use_ssl)
  360. ld = ldapssl_init(ldb->host, ldb->port, ldb->use_ssl);
  361. else ldap_init(ldb->host, ldb->port);
  362. #else
  363. ld = ldapu_init(ldb->host, ldb->port);
  364. #endif
  365. if (ld == NULL) {
  366. DBG_PRINT1("ldapu_ldap_init: Failed to initialize connection");
  367. ldb_crit_exit(ldb);
  368. return LDAPU_ERR_LDAP_INIT_FAILED;
  369. }
  370. {
  371. struct ldap_thread_fns tfns;
  372. PR_NewThreadPrivateIndex(&tsdindex, NULL);
  373. /* set mutex pointers */
  374. memset( &tfns, '\0', sizeof(struct ldap_thread_fns) );
  375. tfns.ltf_mutex_alloc = (void *(*)(void))PR_NewMonitor;
  376. tfns.ltf_mutex_free = (void (*)(void *))PR_DestroyMonitor;
  377. tfns.ltf_mutex_lock = (int (*)(void *)) PR_EnterMonitor;
  378. tfns.ltf_mutex_unlock = (int (*)(void *)) PR_ExitMonitor;
  379. tfns.ltf_get_errno = get_errno;
  380. tfns.ltf_set_errno = set_errno;
  381. tfns.ltf_get_lderrno = get_ld_error;
  382. tfns.ltf_set_lderrno = set_ld_error;
  383. /* set ld_errno pointers */
  384. if ( ldapu_set_option( ld, LDAP_OPT_THREAD_FN_PTRS, (void *) &tfns )
  385. != 0 ) {
  386. ldb_crit_exit(ldb);
  387. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  388. }
  389. }
  390. #ifdef LDAP_OPT_DNS_FN_PTRS /* not supported in older LDAP SDKs */
  391. {
  392. /* install DNS functions */
  393. struct ldap_dns_fns dnsfns;
  394. memset( &dnsfns, '\0', sizeof(struct ldap_dns_fns) );
  395. dnsfns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
  396. dnsfns.lddnsfn_gethostbyname = ldapu_gethostbyname;
  397. dnsfns.lddnsfn_gethostbyaddr = ldapu_gethostbyaddr;
  398. if ( ldapu_set_option( ld, LDAP_OPT_DNS_FN_PTRS, (void *)&dnsfns )
  399. != 0 ) {
  400. ldb_crit_exit(ldb);
  401. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  402. }
  403. }
  404. #endif /* LDAP_OPT_DNS_FN_PTRS */
  405. if (ldapu_is_local_db(ldb)) {
  406. /* No more Local db support, force error! */
  407. return LDAPU_ERR_LCACHE_INIT_FAILED;
  408. #if 0
  409. int optval = 1;
  410. if (lcache_init(ld, ldb->host) != 0) {
  411. ldb_crit_exit(ldb);
  412. return LDAPU_ERR_LCACHE_INIT_FAILED;
  413. }
  414. if (ldap_set_option(ld, LDAP_OPT_CACHE_ENABLE, &optval) != 0) {
  415. ldb_crit_exit(ldb);
  416. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  417. }
  418. optval = LDAP_CACHE_LOCALDB;
  419. if (ldap_set_option(ld, LDAP_OPT_CACHE_STRATEGY, &optval) != 0) {
  420. ldb_crit_exit(ldb);
  421. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  422. }
  423. #endif
  424. }
  425. else if (ldb->binddn && *ldb->binddn) {
  426. /* Set the rebind proc */
  427. /* Rebind proc is used when chasing a referral */
  428. ldap_set_rebind_proc(ld, ldapu_rebind_proc, (void *)ldb);
  429. }
  430. ldb->ld = ld;
  431. ldb_crit_exit(ldb);
  432. return LDAPU_SUCCESS;
  433. }
  434. NSAPI_PUBLIC int ldapu_ldap_rebind (LDAPDatabase_t *ldb)
  435. {
  436. int retry;
  437. int rv = LDAPU_FAILED;
  438. ldb_crit_enter(ldb);
  439. if (ldb->ld) {
  440. retry = (ldb->bound != -1 ? 1 : 0); /* avoid recursion */
  441. #ifdef USE_LDAP_SSL
  442. if (ldb->use_ssl && !CERT_GetDefaultCertDB()) {
  443. /* default cert database has not been initialized */
  444. rv = LDAPU_ERR_NO_DEFAULT_CERTDB;
  445. }
  446. else
  447. #endif
  448. {
  449. rv = ldap_simple_bind_s(ldb->ld, ldb->binddn, ldb->bindpw);
  450. }
  451. /* retry once if the LDAP server is down */
  452. if (rv == LDAP_SERVER_DOWN && retry) {
  453. ldb->bound = -1; /* to avoid recursion */
  454. rv = ldapu_ldap_reinit_and_rebind(ldb);
  455. }
  456. if (rv == LDAPU_SUCCESS) ldb->bound = 1;
  457. }
  458. ldb_crit_exit(ldb);
  459. return rv;
  460. }
  461. NSAPI_PUBLIC int ldapu_ldap_init_and_bind (LDAPDatabase_t *ldb)
  462. {
  463. int rv = LDAPU_SUCCESS;
  464. ldb_crit_enter(ldb);
  465. if (!ldb->ld) {
  466. rv = ldapu_ldap_init(ldb);
  467. /* ldb->bound may be set to -1 to avoid recursion */
  468. if (ldb->bound == 1) ldb->bound = 0;
  469. }
  470. /* bind as binddn & bindpw if not bound already */
  471. if (rv == LDAPU_SUCCESS && ldb->bound != 1) {
  472. rv = ldapu_ldap_rebind (ldb);
  473. }
  474. ldb_crit_exit(ldb);
  475. return rv;
  476. }
  477. NSAPI_PUBLIC int ldapu_ldap_reinit_and_rebind (LDAPDatabase_t *ldb)
  478. {
  479. int rv;
  480. ldb_crit_enter(ldb);
  481. if (ldb->ld) {
  482. ldapu_unbind(ldb->ld);
  483. ldb->ld = 0;
  484. }
  485. rv = ldapu_ldap_init_and_bind(ldb);
  486. ldb_crit_exit(ldb);
  487. return rv;
  488. }