ldapdb.c 15 KB

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