ldapdb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. #ifndef DONT_USE_LDAP_SSL
  39. #define USE_LDAP_SSL
  40. #endif
  41. #include <string.h>
  42. #include <malloc.h>
  43. #ifdef LDAPDB_THREAD_SAFE
  44. #include <nspr.h>
  45. #include <prthread.h>
  46. #include <prmon.h>
  47. /* removed for new ns security integration
  48. #include <xp_error.h>
  49. */
  50. #endif /* LDAPDB_THREAD_SAFE */
  51. #include "ldaputil/errors.h"
  52. #include "ldaputil/certmap.h"
  53. #include "ldaputil/ldapdb.h"
  54. #ifdef USE_LDAP_SSL
  55. /* removed for new ns security integration
  56. #include <sec.h>
  57. #include <key.h>
  58. #include "cert.h"
  59. */
  60. #include <ssl.h>
  61. #include "ldap_ssl.h"
  62. #endif
  63. #include "ldaputili.h"
  64. #define LDAPDB_PREFIX_WITH_SLASHES "ldapdb://"
  65. #define LDAPDB_PREFIX_WITH_SLASHES_LEN 9
  66. #ifndef LDAPDB_THREAD_SAFE
  67. #define ldb_crit_init(x)
  68. #define ldb_crit_enter(x)
  69. #define ldb_crit_exit(x)
  70. #else
  71. #ifdef NSPR20
  72. uintn tsdindex;
  73. #else
  74. int32 tsdindex;
  75. #endif
  76. static void ldb_crit_init (LDAPDatabase_t *ldb)
  77. {
  78. ldb->crit = PR_NewMonitor();
  79. }
  80. static void ldb_crit_enter (LDAPDatabase_t *ldb)
  81. {
  82. PR_EnterMonitor(ldb->crit);
  83. }
  84. static void ldb_crit_exit (LDAPDatabase_t *ldb)
  85. {
  86. PR_ExitMonitor(ldb->crit);
  87. }
  88. struct ldap_error {
  89. int le_errno;
  90. char *le_matched;
  91. char *le_errmsg;
  92. };
  93. static void set_ld_error( int err, char *matched, char *errmsg, void *dummy )
  94. {
  95. struct ldap_error *le;
  96. #ifdef NSPR20
  97. if (!(le = (struct ldap_error *) PR_GetThreadPrivate(tsdindex))) {
  98. le = (struct ldap_error *) malloc(sizeof(struct ldap_error));
  99. memset((void *)le, 0, sizeof(struct ldap_error));
  100. PR_SetThreadPrivate(tsdindex, (void *)le);
  101. }
  102. #else
  103. le = (struct ldap_error *) PR_GetThreadPrivate( PR_CurrentThread(), tsdindex );
  104. #endif
  105. le->le_errno = err;
  106. if ( le->le_matched != NULL ) {
  107. ldap_memfree( le->le_matched );
  108. }
  109. le->le_matched = matched;
  110. if ( le->le_errmsg != NULL ) {
  111. ldap_memfree( le->le_errmsg );
  112. }
  113. le->le_errmsg = errmsg;
  114. }
  115. static int get_ld_error( char **matched, char **errmsg, void *dummy )
  116. {
  117. struct ldap_error *le;
  118. #ifdef NSPR20
  119. le = (struct ldap_error *) PR_GetThreadPrivate( tsdindex);
  120. #else
  121. le = (struct ldap_error *) PR_GetThreadPrivate( PR_CurrentThread(), tsdindex );
  122. #endif
  123. if ( matched != NULL ) {
  124. *matched = le->le_matched;
  125. }
  126. if ( errmsg != NULL ) {
  127. *errmsg = le->le_errmsg;
  128. }
  129. return( le->le_errno );
  130. }
  131. static void set_errno( int err )
  132. {
  133. PR_SetError( err, 0);
  134. }
  135. static int get_errno( void )
  136. {
  137. return( PR_GetError() );
  138. }
  139. #ifdef LDAP_OPT_DNS_FN_PTRS /* not supported in older LDAP SDKs */
  140. static LDAPHostEnt *
  141. ldapu_copyPRHostEnt2LDAPHostEnt( LDAPHostEnt *ldhp, PRHostEnt *prhp )
  142. {
  143. ldhp->ldaphe_name = prhp->h_name;
  144. ldhp->ldaphe_aliases = prhp->h_aliases;
  145. ldhp->ldaphe_addrtype = prhp->h_addrtype;
  146. ldhp->ldaphe_length = prhp->h_length;
  147. ldhp->ldaphe_addr_list = prhp->h_addr_list;
  148. return( ldhp );
  149. }
  150. static LDAPHostEnt *
  151. ldapu_gethostbyname( const char *name, LDAPHostEnt *result,
  152. char *buffer, int buflen, int *statusp, void *extradata )
  153. {
  154. PRHostEnt prhent;
  155. if( !statusp || ( *statusp = (int)PR_GetHostByName( name, buffer,
  156. buflen, &prhent )) == PR_FAILURE ) {
  157. return( NULL );
  158. }
  159. return( ldapu_copyPRHostEnt2LDAPHostEnt( result, &prhent ));
  160. }
  161. static LDAPHostEnt *
  162. ldapu_gethostbyaddr( const char *addr, int length, int type,
  163. LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
  164. void *extradata )
  165. {
  166. return( (LDAPHostEnt *)PR_GetError() );
  167. }
  168. #endif /* LDAP_OPT_DNS_FN_PTRS */
  169. #endif /* LDAPDB_THREAD_SAFE */
  170. static void unescape_ldap_basedn (char *str)
  171. {
  172. if (strchr(str, '%')) {
  173. register int x = 0, y = 0;
  174. int l = strlen(str);
  175. char digit;
  176. while(x < l) {
  177. if((str[x] == '%') && (x < (l - 2))) {
  178. ++x;
  179. digit = (str[x] >= 'A' ?
  180. ((str[x] & 0xdf) - 'A')+10 : (str[x] - '0'));
  181. digit *= 16;
  182. ++x;
  183. digit += (str[x] >= 'A' ?
  184. ((str[x] & 0xdf) - 'A')+10 : (str[x] - '0'));
  185. str[y] = digit;
  186. }
  187. else {
  188. str[y] = str[x];
  189. }
  190. x++;
  191. y++;
  192. }
  193. str[y] = '\0';
  194. }
  195. }
  196. /*
  197. * extract_path_and_basedn:
  198. * Description:
  199. * Parses the ldapdb url and returns pathname to the lcache.conf file
  200. * and basedn. The caller must free the memory allocated for the
  201. * returned path and the basedn.
  202. * Arguments:
  203. * url URL (must begin with ldapdb://)
  204. * path Pathname to the lcache.conf file
  205. * basedn basedn for the ldapdb.
  206. * Return Values: (same as ldap_find)
  207. * LDAPU_SUCCESS if the URL is parsed successfully
  208. * <rv> if error, one of the LDAPU_ errors.
  209. */
  210. static int extract_path_and_basedn(const char *url_in, char **path_out,
  211. char **basedn_out)
  212. {
  213. char *url = strdup(url_in);
  214. char *basedn;
  215. char *path;
  216. *path_out = 0;
  217. *basedn_out = 0;
  218. if (!url) return LDAPU_ERR_OUT_OF_MEMORY;
  219. if (strncmp(url, LDAPDB_URL_PREFIX, LDAPDB_URL_PREFIX_LEN)) {
  220. free(url);
  221. return LDAPU_ERR_URL_INVALID_PREFIX;
  222. }
  223. path = url + LDAPDB_URL_PREFIX_LEN;
  224. if (strncmp(path, "//", 2)) {
  225. free(url);
  226. return LDAPU_ERR_URL_INVALID_PREFIX;
  227. }
  228. path += 2;
  229. /* Find base DN -- empty string is OK */
  230. if ((basedn = strrchr(path, '/')) == NULL) {
  231. free(url);
  232. return LDAPU_ERR_URL_NO_BASEDN;
  233. }
  234. *basedn++ = '\0'; /* terminate the path */
  235. unescape_ldap_basedn(basedn);
  236. *basedn_out = strdup(basedn);
  237. *path_out = strdup(path);
  238. free(url);
  239. return (*basedn_out && *path_out) ? LDAPU_SUCCESS : LDAPU_ERR_OUT_OF_MEMORY;
  240. }
  241. NSAPI_PUBLIC int ldapu_ldapdb_url_parse (const char *url,
  242. LDAPDatabase_t **ldb_out)
  243. {
  244. char *path = 0;
  245. char *basedn = 0;
  246. LDAPDatabase_t *ldb = 0;
  247. int rv;
  248. rv = extract_path_and_basedn(url, &path, &basedn);
  249. if (rv != LDAPU_SUCCESS) {
  250. if (path) free(path);
  251. if (basedn) free(basedn);
  252. return rv;
  253. }
  254. ldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  255. if (!ldb) {
  256. if (path) free(path);
  257. if (basedn) free(basedn);
  258. return LDAPU_ERR_OUT_OF_MEMORY;
  259. }
  260. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  261. ldb->basedn = basedn; /* extract_path_and_basedn has allocated */
  262. ldb->host = path; /* memory for us -- don't make a copy */
  263. ldb_crit_init(ldb);
  264. *ldb_out = ldb;
  265. return LDAPU_SUCCESS;
  266. }
  267. /*
  268. * ldapu_url_parse:
  269. * Description:
  270. * Parses the ldapdb or ldap url and returns a LDAPDatabase_t struct
  271. * Arguments:
  272. * url URL (must begin with ldapdb://)
  273. * binddn DN to use to bind to ldap server.
  274. * bindpw Password to use to bind to ldap server.
  275. * ldb a LDAPDatabase_t struct filled from parsing
  276. * the url.
  277. * Return Values: (same as ldap_find)
  278. * LDAPU_SUCCESS if the URL is parsed successfully
  279. * <rv> if error, one of the LDAPU_ errors.
  280. */
  281. NSAPI_PUBLIC int ldapu_url_parse (const char *url, const char *binddn,
  282. const char *bindpw,
  283. LDAPDatabase_t **ldb_out)
  284. {
  285. LDAPDatabase_t *ldb;
  286. LDAPURLDesc *ludp = 0;
  287. int rv;
  288. *ldb_out = 0;
  289. if (!strncmp(url, LDAPDB_PREFIX_WITH_SLASHES,
  290. LDAPDB_PREFIX_WITH_SLASHES_LEN))
  291. {
  292. return ldapu_ldapdb_url_parse(url, ldb_out);
  293. }
  294. /* call ldapsdk's parse function */
  295. rv = ldap_url_parse((char *)url, &ludp);
  296. if (rv != LDAP_SUCCESS) {
  297. if (ludp) ldap_free_urldesc(ludp);
  298. return LDAPU_ERR_URL_PARSE_FAILED;
  299. }
  300. ldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  301. if (!ldb) {
  302. ldap_free_urldesc(ludp);
  303. return LDAPU_ERR_OUT_OF_MEMORY;
  304. }
  305. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  306. ldb->host = ludp->lud_host ? strdup(ludp->lud_host) : 0;
  307. ldb->use_ssl = ludp->lud_options & LDAP_URL_OPT_SECURE;
  308. ldb->port = ludp->lud_port ? ludp->lud_port : ldb->use_ssl ? 636 : 389;
  309. ldb->basedn = ludp->lud_dn ? strdup(ludp->lud_dn) : 0;
  310. ldb_crit_init(ldb);
  311. ldap_free_urldesc(ludp);
  312. if (binddn) ldb->binddn = strdup(binddn);
  313. if (bindpw) ldb->bindpw = strdup(bindpw);
  314. /* success */
  315. *ldb_out = ldb;
  316. return LDAPU_SUCCESS;
  317. }
  318. NSAPI_PUBLIC void ldapu_free_LDAPDatabase_t (LDAPDatabase_t *ldb)
  319. {
  320. if (ldb->host) free(ldb->host);
  321. if (ldb->basedn) free(ldb->basedn);
  322. if (ldb->filter) free(ldb->filter);
  323. if (ldb->binddn) free(ldb->binddn);
  324. if (ldb->bindpw) free(ldb->bindpw);
  325. if (ldb->ld) ldapu_unbind(ldb->ld);
  326. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  327. free(ldb);
  328. }
  329. NSAPI_PUBLIC LDAPDatabase_t *ldapu_copy_LDAPDatabase_t (const LDAPDatabase_t *ldb)
  330. {
  331. LDAPDatabase_t *nldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  332. if (!nldb) return 0;
  333. memset((void *)nldb, 0, sizeof(LDAPDatabase_t));
  334. nldb->use_ssl = ldb->use_ssl;
  335. if (ldb->host) nldb->host = strdup(ldb->host);
  336. nldb->port = ldb->port;
  337. if (ldb->basedn) nldb->basedn = strdup(ldb->basedn);
  338. nldb->scope = ldb->scope;
  339. if (ldb->filter) nldb->filter = strdup(ldb->filter);
  340. nldb->ld = 0;
  341. if (ldb->binddn) nldb->binddn = strdup(ldb->binddn);
  342. if (ldb->bindpw) nldb->bindpw = strdup(ldb->bindpw);
  343. nldb->bound = 0;
  344. ldb_crit_init(nldb);
  345. return nldb;
  346. }
  347. NSAPI_PUBLIC int ldapu_is_local_db (const LDAPDatabase_t *ldb)
  348. {
  349. return ldb->port ? 0 : 1;
  350. }
  351. static int LDAP_CALL LDAP_CALLBACK
  352. ldapu_rebind_proc (LDAP *ld, char **whop, char **passwdp,
  353. int *authmethodp, int freeit, void *arg)
  354. {
  355. if (freeit == 0) {
  356. LDAPDatabase_t *ldb = (LDAPDatabase_t *)arg;
  357. *whop = ldb->binddn;
  358. *passwdp = ldb->bindpw;
  359. *authmethodp = LDAP_AUTH_SIMPLE;
  360. }
  361. return LDAP_SUCCESS;
  362. }
  363. NSAPI_PUBLIC int ldapu_ldap_init(LDAPDatabase_t *ldb)
  364. {
  365. LDAP *ld = 0;
  366. ldb_crit_enter(ldb);
  367. #ifdef USE_LDAP_SSL
  368. /* Note. This assume the security related initialization is done */
  369. /* The step needed is :
  370. PR_Init
  371. RNG_SystemInfoForRNG
  372. RNG_RNGInit
  373. CERT_OpenCertDBFilename
  374. CERT_SetDefaultCertDB
  375. SECMOD_init
  376. And because ldapssl_init depends on security initialization, it is
  377. no good for non-ssl init
  378. */
  379. if (ldb->use_ssl)
  380. ld = ldapssl_init(ldb->host, ldb->port, ldb->use_ssl);
  381. else ldap_init(ldb->host, ldb->port);
  382. #else
  383. ld = ldapu_init(ldb->host, ldb->port);
  384. #endif
  385. if (ld == NULL) {
  386. DBG_PRINT1("ldapu_ldap_init: Failed to initialize connection");
  387. ldb_crit_exit(ldb);
  388. return LDAPU_ERR_LDAP_INIT_FAILED;
  389. }
  390. #ifdef LDAPDB_THREAD_SAFE
  391. {
  392. struct ldap_thread_fns tfns;
  393. #ifdef NSPR20
  394. PR_NewThreadPrivateIndex(&tsdindex, NULL);
  395. #else
  396. tsdindex = PR_NewThreadPrivateID();
  397. #endif
  398. /* set mutex pointers */
  399. memset( &tfns, '\0', sizeof(struct ldap_thread_fns) );
  400. tfns.ltf_mutex_alloc = (void *(*)(void))PR_NewMonitor;
  401. tfns.ltf_mutex_free = (void (*)(void *))PR_DestroyMonitor;
  402. tfns.ltf_mutex_lock = (int (*)(void *)) PR_EnterMonitor;
  403. tfns.ltf_mutex_unlock = (int (*)(void *)) PR_ExitMonitor;
  404. tfns.ltf_get_errno = get_errno;
  405. tfns.ltf_set_errno = set_errno;
  406. tfns.ltf_get_lderrno = get_ld_error;
  407. tfns.ltf_set_lderrno = set_ld_error;
  408. /* set ld_errno pointers */
  409. if ( ldapu_set_option( ld, LDAP_OPT_THREAD_FN_PTRS, (void *) &tfns )
  410. != 0 ) {
  411. ldb_crit_exit(ldb);
  412. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  413. }
  414. }
  415. #ifdef LDAP_OPT_DNS_FN_PTRS /* not supported in older LDAP SDKs */
  416. {
  417. /* install DNS functions */
  418. struct ldap_dns_fns dnsfns;
  419. memset( &dnsfns, '\0', sizeof(struct ldap_dns_fns) );
  420. dnsfns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
  421. dnsfns.lddnsfn_gethostbyname = ldapu_gethostbyname;
  422. dnsfns.lddnsfn_gethostbyaddr = ldapu_gethostbyaddr;
  423. if ( ldapu_set_option( ld, LDAP_OPT_DNS_FN_PTRS, (void *)&dnsfns )
  424. != 0 ) {
  425. ldb_crit_exit(ldb);
  426. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  427. }
  428. }
  429. #endif /* LDAP_OPT_DNS_FN_PTRS */
  430. #endif /* LDAPDB_THREAD_SAFE */
  431. if (ldapu_is_local_db(ldb)) {
  432. /* No more Local db support, force error! */
  433. return LDAPU_ERR_LCACHE_INIT_FAILED;
  434. #if 0
  435. int optval = 1;
  436. if (lcache_init(ld, ldb->host) != 0) {
  437. ldb_crit_exit(ldb);
  438. return LDAPU_ERR_LCACHE_INIT_FAILED;
  439. }
  440. if (ldap_set_option(ld, LDAP_OPT_CACHE_ENABLE, &optval) != 0) {
  441. ldb_crit_exit(ldb);
  442. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  443. }
  444. optval = LDAP_CACHE_LOCALDB;
  445. if (ldap_set_option(ld, LDAP_OPT_CACHE_STRATEGY, &optval) != 0) {
  446. ldb_crit_exit(ldb);
  447. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  448. }
  449. #endif
  450. }
  451. else if (ldb->binddn && *ldb->binddn) {
  452. /* Set the rebind proc */
  453. /* Rebind proc is used when chasing a referral */
  454. ldap_set_rebind_proc(ld, ldapu_rebind_proc, (void *)ldb);
  455. }
  456. ldb->ld = ld;
  457. ldb_crit_exit(ldb);
  458. return LDAPU_SUCCESS;
  459. }
  460. NSAPI_PUBLIC int ldapu_ldap_rebind (LDAPDatabase_t *ldb)
  461. {
  462. int retry;
  463. int rv = LDAPU_FAILED;
  464. ldb_crit_enter(ldb);
  465. if (ldb->ld) {
  466. retry = (ldb->bound != -1 ? 1 : 0); /* avoid recursion */
  467. #ifdef USE_LDAP_SSL
  468. if (ldb->use_ssl && !CERT_GetDefaultCertDB()) {
  469. /* default cert database has not been initialized */
  470. rv = LDAPU_ERR_NO_DEFAULT_CERTDB;
  471. }
  472. else
  473. #endif
  474. {
  475. rv = ldap_simple_bind_s(ldb->ld, ldb->binddn, ldb->bindpw);
  476. }
  477. /* retry once if the LDAP server is down */
  478. if (rv == LDAP_SERVER_DOWN && retry) {
  479. ldb->bound = -1; /* to avoid recursion */
  480. rv = ldapu_ldap_reinit_and_rebind(ldb);
  481. }
  482. if (rv == LDAPU_SUCCESS) ldb->bound = 1;
  483. }
  484. ldb_crit_exit(ldb);
  485. return rv;
  486. }
  487. NSAPI_PUBLIC int ldapu_ldap_init_and_bind (LDAPDatabase_t *ldb)
  488. {
  489. int rv = LDAPU_SUCCESS;
  490. ldb_crit_enter(ldb);
  491. if (!ldb->ld) {
  492. rv = ldapu_ldap_init(ldb);
  493. /* ldb->bound may be set to -1 to avoid recursion */
  494. if (ldb->bound == 1) ldb->bound = 0;
  495. }
  496. /* bind as binddn & bindpw if not bound already */
  497. if (rv == LDAPU_SUCCESS && ldb->bound != 1) {
  498. rv = ldapu_ldap_rebind (ldb);
  499. }
  500. ldb_crit_exit(ldb);
  501. return rv;
  502. }
  503. NSAPI_PUBLIC int ldapu_ldap_reinit_and_rebind (LDAPDatabase_t *ldb)
  504. {
  505. int rv;
  506. ldb_crit_enter(ldb);
  507. if (ldb->ld) {
  508. ldapu_unbind(ldb->ld);
  509. ldb->ld = 0;
  510. }
  511. rv = ldapu_ldap_init_and_bind(ldb);
  512. ldb_crit_exit(ldb);
  513. return rv;
  514. }