ldapdb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. /* old code did this which was clearly wrong:
  148. return( (LDAPHostEnt *)PR_GetError() );
  149. which leads me to believe this is not used */
  150. return( NULL );
  151. }
  152. #endif /* LDAP_OPT_DNS_FN_PTRS */
  153. static void unescape_ldap_basedn (char *str)
  154. {
  155. if (strchr(str, '%')) {
  156. register int x = 0, y = 0;
  157. int l = strlen(str);
  158. char digit;
  159. while(x < l) {
  160. if((str[x] == '%') && (x < (l - 2))) {
  161. ++x;
  162. digit = (str[x] >= 'A' ?
  163. ((str[x] & 0xdf) - 'A')+10 : (str[x] - '0'));
  164. digit *= 16;
  165. ++x;
  166. digit += (str[x] >= 'A' ?
  167. ((str[x] & 0xdf) - 'A')+10 : (str[x] - '0'));
  168. str[y] = digit;
  169. }
  170. else {
  171. str[y] = str[x];
  172. }
  173. x++;
  174. y++;
  175. }
  176. str[y] = '\0';
  177. }
  178. }
  179. /*
  180. * extract_path_and_basedn:
  181. * Description:
  182. * Parses the ldapdb url and returns pathname to the lcache.conf file
  183. * and basedn. The caller must free the memory allocated for the
  184. * returned path and the basedn.
  185. * Arguments:
  186. * url URL (must begin with ldapdb://)
  187. * path Pathname to the lcache.conf file
  188. * basedn basedn for the ldapdb.
  189. * Return Values: (same as ldap_find)
  190. * LDAPU_SUCCESS if the URL is parsed successfully
  191. * <rv> if error, one of the LDAPU_ errors.
  192. */
  193. static int extract_path_and_basedn(const char *url_in, char **path_out,
  194. char **basedn_out)
  195. {
  196. char *url = strdup(url_in);
  197. char *basedn;
  198. char *path;
  199. *path_out = 0;
  200. *basedn_out = 0;
  201. if (!url) return LDAPU_ERR_OUT_OF_MEMORY;
  202. if (strncmp(url, LDAPDB_URL_PREFIX, LDAPDB_URL_PREFIX_LEN)) {
  203. free(url);
  204. return LDAPU_ERR_URL_INVALID_PREFIX;
  205. }
  206. path = url + LDAPDB_URL_PREFIX_LEN;
  207. if (strncmp(path, "//", 2)) {
  208. free(url);
  209. return LDAPU_ERR_URL_INVALID_PREFIX;
  210. }
  211. path += 2;
  212. /* Find base DN -- empty string is OK */
  213. if ((basedn = strrchr(path, '/')) == NULL) {
  214. free(url);
  215. return LDAPU_ERR_URL_NO_BASEDN;
  216. }
  217. *basedn++ = '\0'; /* terminate the path */
  218. unescape_ldap_basedn(basedn);
  219. *basedn_out = strdup(basedn);
  220. *path_out = strdup(path);
  221. free(url);
  222. return (*basedn_out && *path_out) ? LDAPU_SUCCESS : LDAPU_ERR_OUT_OF_MEMORY;
  223. }
  224. NSAPI_PUBLIC int ldapu_ldapdb_url_parse (const char *url,
  225. LDAPDatabase_t **ldb_out)
  226. {
  227. char *path = 0;
  228. char *basedn = 0;
  229. LDAPDatabase_t *ldb = 0;
  230. int rv;
  231. rv = extract_path_and_basedn(url, &path, &basedn);
  232. if (rv != LDAPU_SUCCESS) {
  233. if (path) free(path);
  234. if (basedn) free(basedn);
  235. return rv;
  236. }
  237. ldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  238. if (!ldb) {
  239. if (path) free(path);
  240. if (basedn) free(basedn);
  241. return LDAPU_ERR_OUT_OF_MEMORY;
  242. }
  243. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  244. ldb->basedn = basedn; /* extract_path_and_basedn has allocated */
  245. ldb->host = path; /* memory for us -- don't make a copy */
  246. ldb_crit_init(ldb);
  247. *ldb_out = ldb;
  248. return LDAPU_SUCCESS;
  249. }
  250. /*
  251. * ldapu_url_parse:
  252. * Description:
  253. * Parses the ldapdb or ldap url and returns a LDAPDatabase_t struct
  254. * Arguments:
  255. * url URL (must begin with ldapdb://)
  256. * binddn DN to use to bind to ldap server.
  257. * bindpw Password to use to bind to ldap server.
  258. * ldb a LDAPDatabase_t struct filled from parsing
  259. * the url.
  260. * Return Values: (same as ldap_find)
  261. * LDAPU_SUCCESS if the URL is parsed successfully
  262. * <rv> if error, one of the LDAPU_ errors.
  263. */
  264. NSAPI_PUBLIC int ldapu_url_parse (const char *url, const char *binddn,
  265. const char *bindpw,
  266. LDAPDatabase_t **ldb_out)
  267. {
  268. LDAPDatabase_t *ldb;
  269. LDAPURLDesc *ludp = 0;
  270. int rv;
  271. *ldb_out = 0;
  272. if (!strncmp(url, LDAPDB_PREFIX_WITH_SLASHES,
  273. LDAPDB_PREFIX_WITH_SLASHES_LEN))
  274. {
  275. return ldapu_ldapdb_url_parse(url, ldb_out);
  276. }
  277. /* call ldapsdk's parse function */
  278. rv = ldap_url_parse((char *)url, &ludp);
  279. if (rv != LDAP_SUCCESS) {
  280. if (ludp) ldap_free_urldesc(ludp);
  281. return LDAPU_ERR_URL_PARSE_FAILED;
  282. }
  283. ldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  284. if (!ldb) {
  285. ldap_free_urldesc(ludp);
  286. return LDAPU_ERR_OUT_OF_MEMORY;
  287. }
  288. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  289. ldb->host = ludp->lud_host ? strdup(ludp->lud_host) : 0;
  290. ldb->use_ssl = ludp->lud_options & LDAP_URL_OPT_SECURE;
  291. ldb->port = ludp->lud_port ? ludp->lud_port : ldb->use_ssl ? 636 : 389;
  292. ldb->basedn = ludp->lud_dn ? strdup(ludp->lud_dn) : 0;
  293. ldb_crit_init(ldb);
  294. ldap_free_urldesc(ludp);
  295. if (binddn) ldb->binddn = strdup(binddn);
  296. if (bindpw) ldb->bindpw = strdup(bindpw);
  297. /* success */
  298. *ldb_out = ldb;
  299. return LDAPU_SUCCESS;
  300. }
  301. NSAPI_PUBLIC void ldapu_free_LDAPDatabase_t (LDAPDatabase_t *ldb)
  302. {
  303. if (ldb->host) free(ldb->host);
  304. if (ldb->basedn) free(ldb->basedn);
  305. if (ldb->filter) free(ldb->filter);
  306. if (ldb->binddn) free(ldb->binddn);
  307. if (ldb->bindpw) free(ldb->bindpw);
  308. if (ldb->ld) ldapu_unbind(ldb->ld);
  309. memset((void *)ldb, 0, sizeof(LDAPDatabase_t));
  310. free(ldb);
  311. }
  312. NSAPI_PUBLIC LDAPDatabase_t *ldapu_copy_LDAPDatabase_t (const LDAPDatabase_t *ldb)
  313. {
  314. LDAPDatabase_t *nldb = (LDAPDatabase_t *)malloc(sizeof(LDAPDatabase_t));
  315. if (!nldb) return 0;
  316. memset((void *)nldb, 0, sizeof(LDAPDatabase_t));
  317. nldb->use_ssl = ldb->use_ssl;
  318. if (ldb->host) nldb->host = strdup(ldb->host);
  319. nldb->port = ldb->port;
  320. if (ldb->basedn) nldb->basedn = strdup(ldb->basedn);
  321. nldb->scope = ldb->scope;
  322. if (ldb->filter) nldb->filter = strdup(ldb->filter);
  323. nldb->ld = 0;
  324. if (ldb->binddn) nldb->binddn = strdup(ldb->binddn);
  325. if (ldb->bindpw) nldb->bindpw = strdup(ldb->bindpw);
  326. nldb->bound = 0;
  327. ldb_crit_init(nldb);
  328. return nldb;
  329. }
  330. NSAPI_PUBLIC int ldapu_is_local_db (const LDAPDatabase_t *ldb)
  331. {
  332. return ldb->port ? 0 : 1;
  333. }
  334. static int LDAP_CALL LDAP_CALLBACK
  335. ldapu_rebind_proc (LDAP *ld, char **whop, char **passwdp,
  336. int *authmethodp, int freeit, void *arg)
  337. {
  338. if (freeit == 0) {
  339. LDAPDatabase_t *ldb = (LDAPDatabase_t *)arg;
  340. *whop = ldb->binddn;
  341. *passwdp = ldb->bindpw;
  342. *authmethodp = LDAP_AUTH_SIMPLE;
  343. }
  344. return LDAP_SUCCESS;
  345. }
  346. NSAPI_PUBLIC int ldapu_ldap_init(LDAPDatabase_t *ldb)
  347. {
  348. LDAP *ld = 0;
  349. ldb_crit_enter(ldb);
  350. #ifdef USE_LDAP_SSL
  351. /* Note. This assume the security related initialization is done */
  352. /* The step needed is :
  353. PR_Init
  354. RNG_SystemInfoForRNG
  355. RNG_RNGInit
  356. CERT_OpenCertDBFilename
  357. CERT_SetDefaultCertDB
  358. SECMOD_init
  359. And because ldapssl_init depends on security initialization, it is
  360. no good for non-ssl init
  361. */
  362. if (ldb->use_ssl)
  363. ld = ldapssl_init(ldb->host, ldb->port, ldb->use_ssl);
  364. else ldap_init(ldb->host, ldb->port);
  365. #else
  366. ld = ldapu_init(ldb->host, ldb->port);
  367. #endif
  368. if (ld == NULL) {
  369. DBG_PRINT1("ldapu_ldap_init: Failed to initialize connection");
  370. ldb_crit_exit(ldb);
  371. return LDAPU_ERR_LDAP_INIT_FAILED;
  372. }
  373. {
  374. struct ldap_thread_fns tfns;
  375. PR_NewThreadPrivateIndex(&tsdindex, NULL);
  376. /* set mutex pointers */
  377. memset( &tfns, '\0', sizeof(struct ldap_thread_fns) );
  378. tfns.ltf_mutex_alloc = (void *(*)(void))PR_NewMonitor;
  379. tfns.ltf_mutex_free = (void (*)(void *))PR_DestroyMonitor;
  380. tfns.ltf_mutex_lock = (int (*)(void *)) PR_EnterMonitor;
  381. tfns.ltf_mutex_unlock = (int (*)(void *)) PR_ExitMonitor;
  382. tfns.ltf_get_errno = get_errno;
  383. tfns.ltf_set_errno = set_errno;
  384. tfns.ltf_get_lderrno = get_ld_error;
  385. tfns.ltf_set_lderrno = set_ld_error;
  386. /* set ld_errno pointers */
  387. if ( ldapu_set_option( ld, LDAP_OPT_THREAD_FN_PTRS, (void *) &tfns )
  388. != 0 ) {
  389. ldb_crit_exit(ldb);
  390. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  391. }
  392. }
  393. #ifdef LDAP_OPT_DNS_FN_PTRS /* not supported in older LDAP SDKs */
  394. {
  395. /* install DNS functions */
  396. struct ldap_dns_fns dnsfns;
  397. memset( &dnsfns, '\0', sizeof(struct ldap_dns_fns) );
  398. dnsfns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
  399. dnsfns.lddnsfn_gethostbyname = ldapu_gethostbyname;
  400. dnsfns.lddnsfn_gethostbyaddr = ldapu_gethostbyaddr;
  401. if ( ldapu_set_option( ld, LDAP_OPT_DNS_FN_PTRS, (void *)&dnsfns )
  402. != 0 ) {
  403. ldb_crit_exit(ldb);
  404. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  405. }
  406. }
  407. #endif /* LDAP_OPT_DNS_FN_PTRS */
  408. if (ldapu_is_local_db(ldb)) {
  409. /* No more Local db support, force error! */
  410. return LDAPU_ERR_LCACHE_INIT_FAILED;
  411. #if 0
  412. int optval = 1;
  413. if (lcache_init(ld, ldb->host) != 0) {
  414. ldb_crit_exit(ldb);
  415. return LDAPU_ERR_LCACHE_INIT_FAILED;
  416. }
  417. if (ldap_set_option(ld, LDAP_OPT_CACHE_ENABLE, &optval) != 0) {
  418. ldb_crit_exit(ldb);
  419. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  420. }
  421. optval = LDAP_CACHE_LOCALDB;
  422. if (ldap_set_option(ld, LDAP_OPT_CACHE_STRATEGY, &optval) != 0) {
  423. ldb_crit_exit(ldb);
  424. return LDAPU_ERR_LDAP_SET_OPTION_FAILED;
  425. }
  426. #endif
  427. }
  428. else if (ldb->binddn && *ldb->binddn) {
  429. /* Set the rebind proc */
  430. /* Rebind proc is used when chasing a referral */
  431. ldap_set_rebind_proc(ld, ldapu_rebind_proc, (void *)ldb);
  432. }
  433. ldb->ld = ld;
  434. ldb_crit_exit(ldb);
  435. return LDAPU_SUCCESS;
  436. }
  437. NSAPI_PUBLIC int ldapu_ldap_rebind (LDAPDatabase_t *ldb)
  438. {
  439. int retry;
  440. int rv = LDAPU_FAILED;
  441. ldb_crit_enter(ldb);
  442. if (ldb->ld) {
  443. retry = (ldb->bound != -1 ? 1 : 0); /* avoid recursion */
  444. #ifdef USE_LDAP_SSL
  445. if (ldb->use_ssl && !CERT_GetDefaultCertDB()) {
  446. /* default cert database has not been initialized */
  447. rv = LDAPU_ERR_NO_DEFAULT_CERTDB;
  448. }
  449. else
  450. #endif
  451. {
  452. rv = ldap_simple_bind_s(ldb->ld, ldb->binddn, ldb->bindpw);
  453. }
  454. /* retry once if the LDAP server is down */
  455. if (rv == LDAP_SERVER_DOWN && retry) {
  456. ldb->bound = -1; /* to avoid recursion */
  457. rv = ldapu_ldap_reinit_and_rebind(ldb);
  458. }
  459. if (rv == LDAPU_SUCCESS) ldb->bound = 1;
  460. }
  461. ldb_crit_exit(ldb);
  462. return rv;
  463. }
  464. NSAPI_PUBLIC int ldapu_ldap_init_and_bind (LDAPDatabase_t *ldb)
  465. {
  466. int rv = LDAPU_SUCCESS;
  467. ldb_crit_enter(ldb);
  468. if (!ldb->ld) {
  469. rv = ldapu_ldap_init(ldb);
  470. /* ldb->bound may be set to -1 to avoid recursion */
  471. if (ldb->bound == 1) ldb->bound = 0;
  472. }
  473. /* bind as binddn & bindpw if not bound already */
  474. if (rv == LDAPU_SUCCESS && ldb->bound != 1) {
  475. rv = ldapu_ldap_rebind (ldb);
  476. }
  477. ldb_crit_exit(ldb);
  478. return rv;
  479. }
  480. NSAPI_PUBLIC int ldapu_ldap_reinit_and_rebind (LDAPDatabase_t *ldb)
  481. {
  482. int rv;
  483. ldb_crit_enter(ldb);
  484. if (ldb->ld) {
  485. ldapu_unbind(ldb->ld);
  486. ldb->ld = 0;
  487. }
  488. rv = ldapu_ldap_init_and_bind(ldb);
  489. ldb_crit_exit(ldb);
  490. return rv;
  491. }