ldap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_LDAP
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #ifdef HAVE_SYS_TYPES_H
  32. #include <sys/types.h>
  33. #endif
  34. #ifdef HAVE_SYS_STAT_H
  35. #include <sys/stat.h>
  36. #endif
  37. #ifdef NEED_MALLOC_H
  38. #include <malloc.h>
  39. #endif
  40. #include <errno.h>
  41. #if defined(WIN32)
  42. # include <winldap.h>
  43. #endif
  44. #ifdef HAVE_UNISTD_H
  45. # include <unistd.h>
  46. #endif
  47. #ifdef HAVE_DLFCN_H
  48. # include <dlfcn.h>
  49. #endif
  50. #include "urldata.h"
  51. #include <curl/curl.h>
  52. #include "sendf.h"
  53. #include "escape.h"
  54. #include "transfer.h"
  55. #include "strequal.h"
  56. #include "strtok.h"
  57. #include "ldap.h"
  58. #include "memory.h"
  59. #include "base64.h"
  60. #define _MPRINTF_REPLACE /* use our functions only */
  61. #include <curl/mprintf.h>
  62. #include "memdebug.h"
  63. /* WLdap32.dll functions are *not* stdcall. Must call these via __cdecl
  64. * pointers in case libcurl was compiled as fastcall (cl -Gr). Watcom
  65. * uses fastcall by default.
  66. */
  67. #if !defined(WIN32) && !defined(__cdecl)
  68. #define __cdecl
  69. #endif
  70. #ifndef LDAP_SIZELIMIT_EXCEEDED
  71. #define LDAP_SIZELIMIT_EXCEEDED 4
  72. #endif
  73. #ifndef LDAP_VERSION2
  74. #define LDAP_VERSION2 2
  75. #endif
  76. #ifndef LDAP_VERSION3
  77. #define LDAP_VERSION3 3
  78. #endif
  79. #ifndef LDAP_OPT_PROTOCOL_VERSION
  80. #define LDAP_OPT_PROTOCOL_VERSION 0x0011
  81. #endif
  82. #define DLOPEN_MODE RTLD_LAZY /*! assume all dlopen() implementations have
  83. this */
  84. #if defined(RTLD_LAZY_GLOBAL) /* It turns out some systems use this: */
  85. # undef DLOPEN_MODE
  86. # define DLOPEN_MODE RTLD_LAZY_GLOBAL
  87. #elif defined(RTLD_GLOBAL)
  88. # undef DLOPEN_MODE
  89. # define DLOPEN_MODE (RTLD_LAZY | RTLD_GLOBAL)
  90. #endif
  91. #define DYNA_GET_FUNCTION(type, fnc) do { \
  92. (fnc) = (type)DynaGetFunction(#fnc); \
  93. if ((fnc) == NULL) \
  94. return CURLE_FUNCTION_NOT_FOUND; \
  95. } while (0)
  96. /*! CygWin etc. configure could set these, but we don't want it.
  97. * Must use WLdap32.dll code.
  98. */
  99. #if defined(WIN32)
  100. #undef HAVE_DLOPEN
  101. #undef HAVE_LIBDL
  102. #endif
  103. /*
  104. * We use this ZERO_NULL to avoid picky compiler warnings,
  105. * when assigning a NULL pointer to a function pointer var.
  106. */
  107. #define ZERO_NULL 0
  108. typedef void * (*dynafunc)(void *input);
  109. /***********************************************************************
  110. */
  111. #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL) || defined(WIN32)
  112. static void *libldap = NULL;
  113. #if defined(DL_LBER_FILE)
  114. static void *liblber = NULL;
  115. #endif
  116. #endif
  117. struct bv {
  118. unsigned long bv_len;
  119. char *bv_val;
  120. };
  121. static int DynaOpen(const char **mod_name)
  122. {
  123. #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
  124. if (libldap == NULL) {
  125. /*
  126. * libldap.so can normally resolve its dependency on liblber.so
  127. * automatically, but in broken installation it does not so
  128. * handle it here by opening liblber.so as global.
  129. */
  130. #ifdef DL_LBER_FILE
  131. *mod_name = DL_LBER_FILE;
  132. liblber = dlopen(*mod_name, DLOPEN_MODE);
  133. if (!liblber)
  134. return 0;
  135. #endif
  136. /* Assume loading libldap.so will fail if loading of liblber.so failed
  137. */
  138. *mod_name = DL_LDAP_FILE;
  139. libldap = dlopen(*mod_name, RTLD_LAZY);
  140. }
  141. return (libldap != NULL);
  142. #elif defined(WIN32)
  143. *mod_name = DL_LDAP_FILE;
  144. if (!libldap)
  145. libldap = (void*)LoadLibrary(*mod_name);
  146. return (libldap != NULL);
  147. #else
  148. *mod_name = "";
  149. return (0);
  150. #endif
  151. }
  152. static void DynaClose(void)
  153. {
  154. #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
  155. if (libldap) {
  156. dlclose(libldap);
  157. libldap=NULL;
  158. }
  159. #ifdef DL_LBER_FILE
  160. if (liblber) {
  161. dlclose(liblber);
  162. liblber=NULL;
  163. }
  164. #endif
  165. #elif defined(WIN32)
  166. if (libldap) {
  167. FreeLibrary ((HMODULE)libldap);
  168. libldap = NULL;
  169. }
  170. #endif
  171. }
  172. static dynafunc DynaGetFunction(const char *name)
  173. {
  174. dynafunc func = (dynafunc)ZERO_NULL;
  175. #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
  176. if (libldap) {
  177. /* This typecast magic below was brought by Joe Halpin. In ISO C, you
  178. * cannot typecast a data pointer to a function pointer, but that's
  179. * exactly what we need to do here to avoid compiler warnings on picky
  180. * compilers! */
  181. *(void**) (&func) = dlsym(libldap, name);
  182. }
  183. #elif defined(WIN32)
  184. if (libldap) {
  185. func = (dynafunc)GetProcAddress((HINSTANCE)libldap, name);
  186. }
  187. #else
  188. (void) name;
  189. #endif
  190. return func;
  191. }
  192. /***********************************************************************
  193. */
  194. typedef struct ldap_url_desc {
  195. struct ldap_url_desc *lud_next;
  196. char *lud_scheme;
  197. char *lud_host;
  198. int lud_port;
  199. char *lud_dn;
  200. char **lud_attrs;
  201. int lud_scope;
  202. char *lud_filter;
  203. char **lud_exts;
  204. int lud_crit_exts;
  205. } LDAPURLDesc;
  206. #ifdef WIN32
  207. static int _ldap_url_parse (const struct connectdata *conn,
  208. LDAPURLDesc **ludp);
  209. static void _ldap_free_urldesc (LDAPURLDesc *ludp);
  210. static void (*ldap_free_urldesc)(LDAPURLDesc *) = _ldap_free_urldesc;
  211. #endif
  212. #ifdef DEBUG_LDAP
  213. #define LDAP_TRACE(x) do { \
  214. _ldap_trace ("%u: ", __LINE__); \
  215. _ldap_trace x; \
  216. } while (0)
  217. static void _ldap_trace (const char *fmt, ...);
  218. #else
  219. #define LDAP_TRACE(x) ((void)0)
  220. #endif
  221. CURLcode Curl_ldap(struct connectdata *conn, bool *done)
  222. {
  223. CURLcode status = CURLE_OK;
  224. int rc = 0;
  225. #ifndef WIN32
  226. int (*ldap_url_parse)(char *, LDAPURLDesc **);
  227. void (*ldap_free_urldesc)(void *);
  228. #endif
  229. void *(__cdecl *ldap_init)(char *, int);
  230. int (__cdecl *ldap_simple_bind_s)(void *, char *, char *);
  231. int (__cdecl *ldap_unbind_s)(void *);
  232. int (__cdecl *ldap_search_s)(void *, char *, int, char *, char **,
  233. int, void **);
  234. void *(__cdecl *ldap_first_entry)(void *, void *);
  235. void *(__cdecl *ldap_next_entry)(void *, void *);
  236. char *(__cdecl *ldap_err2string)(int);
  237. char *(__cdecl *ldap_get_dn)(void *, void *);
  238. char *(__cdecl *ldap_first_attribute)(void *, void *, void **);
  239. char *(__cdecl *ldap_next_attribute)(void *, void *, void *);
  240. void **(__cdecl *ldap_get_values_len)(void *, void *, const char *);
  241. void (__cdecl *ldap_value_free_len)(void **);
  242. void (__cdecl *ldap_memfree)(void *);
  243. void (__cdecl *ber_free)(void *, int);
  244. int (__cdecl *ldap_set_option)(void *, int, void *);
  245. void *server;
  246. LDAPURLDesc *ludp = NULL;
  247. const char *mod_name;
  248. void *result;
  249. void *entryIterator; /*! type should be 'LDAPMessage *' */
  250. int num = 0;
  251. struct SessionHandle *data=conn->data;
  252. int ldap_proto;
  253. char *val_b64;
  254. size_t val_b64_sz;
  255. *done = TRUE; /* unconditionally */
  256. infof(data, "LDAP local: %s\n", data->change.url);
  257. if (!DynaOpen(&mod_name)) {
  258. failf(data, "The %s LDAP library/libraries couldn't be opened", mod_name);
  259. return CURLE_LIBRARY_NOT_FOUND;
  260. }
  261. /* The types are needed because ANSI C distinguishes between
  262. * pointer-to-object (data) and pointer-to-function.
  263. */
  264. DYNA_GET_FUNCTION(void *(__cdecl *)(char *, int), ldap_init);
  265. DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, char *),
  266. ldap_simple_bind_s);
  267. DYNA_GET_FUNCTION(int (__cdecl *)(void *), ldap_unbind_s);
  268. #ifndef WIN32
  269. DYNA_GET_FUNCTION(int (*)(char *, LDAPURLDesc **), ldap_url_parse);
  270. DYNA_GET_FUNCTION(void (*)(void *), ldap_free_urldesc);
  271. #endif
  272. DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, int, char *, char **, int,
  273. void **), ldap_search_s);
  274. DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_first_entry);
  275. DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_next_entry);
  276. DYNA_GET_FUNCTION(char *(__cdecl *)(int), ldap_err2string);
  277. DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *), ldap_get_dn);
  278. DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void **),
  279. ldap_first_attribute);
  280. DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void *),
  281. ldap_next_attribute);
  282. DYNA_GET_FUNCTION(void **(__cdecl *)(void *, void *, const char *),
  283. ldap_get_values_len);
  284. DYNA_GET_FUNCTION(void (__cdecl *)(void **), ldap_value_free_len);
  285. DYNA_GET_FUNCTION(void (__cdecl *)(void *), ldap_memfree);
  286. DYNA_GET_FUNCTION(void (__cdecl *)(void *, int), ber_free);
  287. DYNA_GET_FUNCTION(int (__cdecl *)(void *, int, void *), ldap_set_option);
  288. server = (*ldap_init)(conn->host.name, (int)conn->port);
  289. if (server == NULL) {
  290. failf(data, "LDAP local: Cannot connect to %s:%d",
  291. conn->host.name, conn->port);
  292. status = CURLE_COULDNT_CONNECT;
  293. goto quit;
  294. }
  295. ldap_proto = LDAP_VERSION3;
  296. (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  297. rc = (*ldap_simple_bind_s)(server,
  298. conn->bits.user_passwd ? conn->user : NULL,
  299. conn->bits.user_passwd ? conn->passwd : NULL);
  300. if (rc != 0) {
  301. ldap_proto = LDAP_VERSION2;
  302. (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  303. rc = (*ldap_simple_bind_s)(server,
  304. conn->bits.user_passwd ? conn->user : NULL,
  305. conn->bits.user_passwd ? conn->passwd : NULL);
  306. }
  307. if (rc != 0) {
  308. failf(data, "LDAP local: %s", (*ldap_err2string)(rc));
  309. status = CURLE_LDAP_CANNOT_BIND;
  310. goto quit;
  311. }
  312. #ifdef WIN32
  313. rc = _ldap_url_parse(conn, &ludp);
  314. #else
  315. rc = (*ldap_url_parse)(data->change.url, &ludp);
  316. #endif
  317. if (rc != 0) {
  318. failf(data, "LDAP local: %s", (*ldap_err2string)(rc));
  319. status = CURLE_LDAP_INVALID_URL;
  320. goto quit;
  321. }
  322. rc = (*ldap_search_s)(server, ludp->lud_dn, ludp->lud_scope,
  323. ludp->lud_filter, ludp->lud_attrs, 0, &result);
  324. if (rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
  325. failf(data, "LDAP remote: %s", (*ldap_err2string)(rc));
  326. status = CURLE_LDAP_SEARCH_FAILED;
  327. goto quit;
  328. }
  329. for(num = 0, entryIterator = (*ldap_first_entry)(server, result);
  330. entryIterator;
  331. entryIterator = (*ldap_next_entry)(server, entryIterator), num++)
  332. {
  333. void *ber = NULL; /*! is really 'BerElement **' */
  334. void *attribute; /*! suspicious that this isn't 'const' */
  335. char *dn = (*ldap_get_dn)(server, entryIterator);
  336. int i;
  337. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
  338. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
  339. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  340. for (attribute = (*ldap_first_attribute)(server, entryIterator, &ber);
  341. attribute;
  342. attribute = (*ldap_next_attribute)(server, entryIterator, ber))
  343. {
  344. struct bv **vals = (struct bv **)
  345. (*ldap_get_values_len)(server, entryIterator, attribute);
  346. if (vals != NULL)
  347. {
  348. for (i = 0; (vals[i] != NULL); i++)
  349. {
  350. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
  351. Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
  352. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
  353. if ((strlen(attribute) > 7) &&
  354. (strcmp(";binary",
  355. (char *)attribute +
  356. (strlen((char *)attribute) - 7)) == 0)) {
  357. /* Binary attribute, encode to base64. */
  358. val_b64_sz = Curl_base64_encode(conn->data,
  359. vals[i]->bv_val,
  360. vals[i]->bv_len,
  361. &val_b64);
  362. if (val_b64_sz > 0) {
  363. Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
  364. free(val_b64);
  365. }
  366. } else
  367. Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
  368. vals[i]->bv_len);
  369. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
  370. }
  371. /* Free memory used to store values */
  372. (*ldap_value_free_len)((void **)vals);
  373. }
  374. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  375. (*ldap_memfree)(attribute);
  376. }
  377. (*ldap_memfree)(dn);
  378. if (ber)
  379. (*ber_free)(ber, 0);
  380. }
  381. quit:
  382. LDAP_TRACE (("Received %d entries\n", num));
  383. if (rc == LDAP_SIZELIMIT_EXCEEDED)
  384. infof(data, "There are more than %d entries\n", num);
  385. if (ludp)
  386. (*ldap_free_urldesc)(ludp);
  387. if (server)
  388. (*ldap_unbind_s)(server);
  389. DynaClose();
  390. /* no data to transfer */
  391. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  392. conn->bits.close = TRUE;
  393. return status;
  394. }
  395. #ifdef DEBUG_LDAP
  396. static void _ldap_trace (const char *fmt, ...)
  397. {
  398. static int do_trace = -1;
  399. va_list args;
  400. if (do_trace == -1) {
  401. const char *env = getenv("CURL_TRACE");
  402. do_trace = (env && atoi(env) > 0);
  403. }
  404. if (!do_trace)
  405. return;
  406. va_start (args, fmt);
  407. vfprintf (stderr, fmt, args);
  408. va_end (args);
  409. }
  410. #endif
  411. #ifdef WIN32
  412. /*
  413. * Return scope-value for a scope-string.
  414. */
  415. static int str2scope (const char *p)
  416. {
  417. if (!stricmp(p, "one"))
  418. return LDAP_SCOPE_ONELEVEL;
  419. if (!stricmp(p, "onetree"))
  420. return LDAP_SCOPE_ONELEVEL;
  421. if (!stricmp(p, "base"))
  422. return LDAP_SCOPE_BASE;
  423. if (!stricmp(p, "sub"))
  424. return LDAP_SCOPE_SUBTREE;
  425. if (!stricmp( p, "subtree"))
  426. return LDAP_SCOPE_SUBTREE;
  427. return (-1);
  428. }
  429. /*
  430. * Split 'str' into strings separated by commas.
  431. * Note: res[] points into 'str'.
  432. */
  433. static char **split_str (char *str)
  434. {
  435. char **res, *lasts, *s;
  436. int i;
  437. for (i = 2, s = strchr(str,','); s; i++)
  438. s = strchr(++s,',');
  439. res = calloc(i, sizeof(char*));
  440. if (!res)
  441. return NULL;
  442. for (i = 0, s = strtok_r(str, ",", &lasts); s;
  443. s = strtok_r(NULL, ",", &lasts), i++)
  444. res[i] = s;
  445. return res;
  446. }
  447. /*
  448. * Unescape the LDAP-URL components
  449. */
  450. static bool unescape_elements (void *data, LDAPURLDesc *ludp)
  451. {
  452. int i;
  453. if (ludp->lud_filter) {
  454. ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
  455. if (!ludp->lud_filter)
  456. return (FALSE);
  457. }
  458. for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
  459. ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
  460. if (!ludp->lud_attrs[i])
  461. return (FALSE);
  462. }
  463. for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
  464. ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
  465. if (!ludp->lud_exts[i])
  466. return (FALSE);
  467. }
  468. if (ludp->lud_dn) {
  469. char *dn = ludp->lud_dn;
  470. char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
  471. free(dn);
  472. ludp->lud_dn = new_dn;
  473. if (!new_dn)
  474. return (FALSE);
  475. }
  476. return (TRUE);
  477. }
  478. /*
  479. * Break apart the pieces of an LDAP URL.
  480. * Syntax:
  481. * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
  482. *
  483. * <hostname> already known from 'conn->host.name'.
  484. * <port> already known from 'conn->remote_port'.
  485. * extract the rest from 'conn->data->reqdata.path+1'. All fields are optional.
  486. * e.g.
  487. * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
  488. * yields ludp->lud_dn = "".
  489. *
  490. * Ref. http://developer.netscape.com/docs/manuals/dirsdk/csdk30/url.htm#2831915
  491. */
  492. static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
  493. {
  494. char *p, *q;
  495. int i;
  496. if (!conn->data ||
  497. !conn->data->reqdata.path ||
  498. conn->data->reqdata.path[0] != '/' ||
  499. !checkprefix(conn->protostr, conn->data->change.url))
  500. return LDAP_INVALID_SYNTAX;
  501. ludp->lud_scope = LDAP_SCOPE_BASE;
  502. ludp->lud_port = conn->remote_port;
  503. ludp->lud_host = conn->host.name;
  504. /* parse DN (Distinguished Name).
  505. */
  506. ludp->lud_dn = strdup(conn->data->reqdata.path+1);
  507. if (!ludp->lud_dn)
  508. return LDAP_NO_MEMORY;
  509. p = strchr(ludp->lud_dn, '?');
  510. LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
  511. strlen(ludp->lud_dn), ludp->lud_dn));
  512. if (!p)
  513. goto success;
  514. *p++ = '\0';
  515. /* parse attributes. skip "??".
  516. */
  517. q = strchr(p, '?');
  518. if (q)
  519. *q++ = '\0';
  520. if (*p && *p != '?') {
  521. ludp->lud_attrs = split_str(p);
  522. if (!ludp->lud_attrs)
  523. return LDAP_NO_MEMORY;
  524. for (i = 0; ludp->lud_attrs[i]; i++)
  525. LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
  526. }
  527. p = q;
  528. if (!p)
  529. goto success;
  530. /* parse scope. skip "??"
  531. */
  532. q = strchr(p, '?');
  533. if (q)
  534. *q++ = '\0';
  535. if (*p && *p != '?') {
  536. ludp->lud_scope = str2scope(p);
  537. if (ludp->lud_scope == -1)
  538. return LDAP_INVALID_SYNTAX;
  539. LDAP_TRACE (("scope %d\n", ludp->lud_scope));
  540. }
  541. p = q;
  542. if (!p)
  543. goto success;
  544. /* parse filter
  545. */
  546. q = strchr(p, '?');
  547. if (q)
  548. *q++ = '\0';
  549. if (!*p)
  550. return LDAP_INVALID_SYNTAX;
  551. ludp->lud_filter = p;
  552. LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
  553. p = q;
  554. if (!p)
  555. goto success;
  556. /* parse extensions
  557. */
  558. ludp->lud_exts = split_str(p);
  559. if (!ludp->lud_exts)
  560. return LDAP_NO_MEMORY;
  561. for (i = 0; ludp->lud_exts[i]; i++)
  562. LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
  563. success:
  564. if (!unescape_elements(conn->data, ludp))
  565. return LDAP_NO_MEMORY;
  566. return LDAP_SUCCESS;
  567. }
  568. static int _ldap_url_parse (const struct connectdata *conn,
  569. LDAPURLDesc **ludpp)
  570. {
  571. LDAPURLDesc *ludp = calloc(sizeof(*ludp), 1);
  572. int rc;
  573. *ludpp = NULL;
  574. if (!ludp)
  575. return LDAP_NO_MEMORY;
  576. rc = _ldap_url_parse2 (conn, ludp);
  577. if (rc != LDAP_SUCCESS) {
  578. _ldap_free_urldesc(ludp);
  579. ludp = NULL;
  580. }
  581. *ludpp = ludp;
  582. return (rc);
  583. }
  584. static void _ldap_free_urldesc (LDAPURLDesc *ludp)
  585. {
  586. int i;
  587. if (!ludp)
  588. return;
  589. if (ludp->lud_dn)
  590. free(ludp->lud_dn);
  591. if (ludp->lud_filter)
  592. free(ludp->lud_filter);
  593. if (ludp->lud_attrs) {
  594. for (i = 0; ludp->lud_attrs[i]; i++)
  595. free(ludp->lud_attrs[i]);
  596. free(ludp->lud_attrs);
  597. }
  598. if (ludp->lud_exts) {
  599. for (i = 0; ludp->lud_exts[i]; i++)
  600. free(ludp->lud_exts[i]);
  601. free(ludp->lud_exts);
  602. }
  603. free (ludp);
  604. }
  605. #endif /* WIN32 */
  606. #endif /* CURL_DISABLE_LDAP */