ldap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, 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. ***************************************************************************/
  22. #include "setup.h"
  23. #if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
  24. /*
  25. * Notice that USE_OPENLDAP is only a source code selection switch. When
  26. * libcurl is built with USE_OPENLDAP defined the libcurl source code that
  27. * gets compiled is the code from openldap.c, otherwise the code that gets
  28. * compiled is the code from ldap.c.
  29. *
  30. * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
  31. * might be required for compilation and runtime. In order to use ancient
  32. * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
  33. */
  34. /* -- WIN32 approved -- */
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdarg.h>
  38. #include <stdlib.h>
  39. #include <ctype.h>
  40. #include <errno.h>
  41. #ifdef CURL_LDAP_WIN /* Use Windows LDAP implementation. */
  42. # include <winldap.h>
  43. # ifndef LDAP_VENDOR_NAME
  44. # error Your Platform SDK is NOT sufficient for LDAP support! Update your Platform SDK, or disable LDAP support!
  45. # else
  46. # include <winber.h>
  47. # endif
  48. #else
  49. # define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
  50. # ifdef HAVE_LBER_H
  51. # include <lber.h>
  52. # endif
  53. # include <ldap.h>
  54. # if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
  55. # include <ldap_ssl.h>
  56. # endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
  57. #endif
  58. #ifdef HAVE_UNISTD_H
  59. # include <unistd.h>
  60. #endif
  61. #include "urldata.h"
  62. #include <curl/curl.h>
  63. #include "sendf.h"
  64. #include "escape.h"
  65. #include "progress.h"
  66. #include "transfer.h"
  67. #include "strequal.h"
  68. #include "strtok.h"
  69. #include "curl_ldap.h"
  70. #include "curl_memory.h"
  71. #include "curl_base64.h"
  72. #include "rawstr.h"
  73. #define _MPRINTF_REPLACE /* use our functions only */
  74. #include <curl/mprintf.h>
  75. #include "memdebug.h"
  76. #ifndef HAVE_LDAP_URL_PARSE
  77. /* Use our own implementation. */
  78. typedef struct {
  79. char *lud_host;
  80. int lud_port;
  81. char *lud_dn;
  82. char **lud_attrs;
  83. int lud_scope;
  84. char *lud_filter;
  85. char **lud_exts;
  86. } CURL_LDAPURLDesc;
  87. #undef LDAPURLDesc
  88. #define LDAPURLDesc CURL_LDAPURLDesc
  89. static int _ldap_url_parse (const struct connectdata *conn,
  90. LDAPURLDesc **ludp);
  91. static void _ldap_free_urldesc (LDAPURLDesc *ludp);
  92. #undef ldap_free_urldesc
  93. #define ldap_free_urldesc _ldap_free_urldesc
  94. #endif
  95. #ifdef DEBUG_LDAP
  96. #define LDAP_TRACE(x) do { \
  97. _ldap_trace ("%u: ", __LINE__); \
  98. _ldap_trace x; \
  99. } while(0)
  100. static void _ldap_trace (const char *fmt, ...);
  101. #else
  102. #define LDAP_TRACE(x) ((void)0)
  103. #endif
  104. static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
  105. /*
  106. * LDAP protocol handler.
  107. */
  108. const struct Curl_handler Curl_handler_ldap = {
  109. "LDAP", /* scheme */
  110. ZERO_NULL, /* setup_connection */
  111. Curl_ldap, /* do_it */
  112. ZERO_NULL, /* done */
  113. ZERO_NULL, /* do_more */
  114. ZERO_NULL, /* connect_it */
  115. ZERO_NULL, /* connecting */
  116. ZERO_NULL, /* doing */
  117. ZERO_NULL, /* proto_getsock */
  118. ZERO_NULL, /* doing_getsock */
  119. ZERO_NULL, /* perform_getsock */
  120. ZERO_NULL, /* disconnect */
  121. PORT_LDAP, /* defport */
  122. PROT_LDAP /* protocol */
  123. };
  124. #ifdef HAVE_LDAP_SSL
  125. /*
  126. * LDAPS protocol handler.
  127. */
  128. const struct Curl_handler Curl_handler_ldaps = {
  129. "LDAPS", /* scheme */
  130. ZERO_NULL, /* setup_connection */
  131. Curl_ldap, /* do_it */
  132. ZERO_NULL, /* done */
  133. ZERO_NULL, /* do_more */
  134. ZERO_NULL, /* connect_it */
  135. ZERO_NULL, /* connecting */
  136. ZERO_NULL, /* doing */
  137. ZERO_NULL, /* proto_getsock */
  138. ZERO_NULL, /* doing_getsock */
  139. ZERO_NULL, /* perform_getsock */
  140. ZERO_NULL, /* disconnect */
  141. PORT_LDAPS, /* defport */
  142. PROT_LDAP | PROT_SSL /* protocol */
  143. };
  144. #endif
  145. static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
  146. {
  147. CURLcode status = CURLE_OK;
  148. int rc = 0;
  149. LDAP *server = NULL;
  150. LDAPURLDesc *ludp = NULL;
  151. LDAPMessage *result = NULL;
  152. LDAPMessage *entryIterator;
  153. int num = 0;
  154. struct SessionHandle *data=conn->data;
  155. int ldap_proto = LDAP_VERSION3;
  156. int ldap_ssl = 0;
  157. char *val_b64;
  158. size_t val_b64_sz;
  159. curl_off_t dlsize=0;
  160. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  161. struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
  162. #endif
  163. *done = TRUE; /* unconditionally */
  164. infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
  165. LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
  166. infof(data, "LDAP local: %s\n", data->change.url);
  167. #ifdef HAVE_LDAP_URL_PARSE
  168. rc = ldap_url_parse(data->change.url, &ludp);
  169. #else
  170. rc = _ldap_url_parse(conn, &ludp);
  171. #endif
  172. if(rc != 0) {
  173. failf(data, "LDAP local: %s", ldap_err2string(rc));
  174. status = CURLE_LDAP_INVALID_URL;
  175. goto quit;
  176. }
  177. /* Get the URL scheme ( either ldap or ldaps ) */
  178. if(conn->protocol & PROT_SSL)
  179. ldap_ssl = 1;
  180. infof(data, "LDAP local: trying to establish %s connection\n",
  181. ldap_ssl ? "encrypted" : "cleartext");
  182. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  183. ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
  184. #endif
  185. ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  186. if(ldap_ssl) {
  187. #ifdef HAVE_LDAP_SSL
  188. #ifdef CURL_LDAP_WIN
  189. /* Win32 LDAP SDK doesnt support insecure mode without CA! */
  190. server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
  191. ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
  192. #else
  193. int ldap_option;
  194. char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
  195. #if defined(CURL_HAS_NOVELL_LDAPSDK)
  196. rc = ldapssl_client_init(NULL, NULL);
  197. if(rc != LDAP_SUCCESS) {
  198. failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
  199. status = CURLE_SSL_CERTPROBLEM;
  200. goto quit;
  201. }
  202. if(data->set.ssl.verifypeer) {
  203. /* Novell SDK supports DER or BASE64 files. */
  204. int cert_type = LDAPSSL_CERT_FILETYPE_B64;
  205. if((data->set.str[STRING_CERT_TYPE]) &&
  206. (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
  207. cert_type = LDAPSSL_CERT_FILETYPE_DER;
  208. if(!ldap_ca) {
  209. failf(data, "LDAP local: ERROR %s CA cert not set!",
  210. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
  211. status = CURLE_SSL_CERTPROBLEM;
  212. goto quit;
  213. }
  214. infof(data, "LDAP local: using %s CA cert '%s'\n",
  215. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
  216. ldap_ca);
  217. rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
  218. if(rc != LDAP_SUCCESS) {
  219. failf(data, "LDAP local: ERROR setting %s CA cert: %s",
  220. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
  221. ldap_err2string(rc));
  222. status = CURLE_SSL_CERTPROBLEM;
  223. goto quit;
  224. }
  225. ldap_option = LDAPSSL_VERIFY_SERVER;
  226. } else {
  227. ldap_option = LDAPSSL_VERIFY_NONE;
  228. }
  229. rc = ldapssl_set_verify_mode(ldap_option);
  230. if(rc != LDAP_SUCCESS) {
  231. failf(data, "LDAP local: ERROR setting cert verify mode: %s",
  232. ldap_err2string(rc));
  233. status = CURLE_SSL_CERTPROBLEM;
  234. goto quit;
  235. }
  236. server = ldapssl_init(conn->host.name, (int)conn->port, 1);
  237. if(server == NULL) {
  238. failf(data, "LDAP local: Cannot connect to %s:%hu",
  239. conn->host.name, conn->port);
  240. status = CURLE_COULDNT_CONNECT;
  241. goto quit;
  242. }
  243. #elif defined(LDAP_OPT_X_TLS)
  244. if(data->set.ssl.verifypeer) {
  245. /* OpenLDAP SDK supports BASE64 files. */
  246. if((data->set.str[STRING_CERT_TYPE]) &&
  247. (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
  248. failf(data, "LDAP local: ERROR OpenLDAP does only support PEM cert-type!");
  249. status = CURLE_SSL_CERTPROBLEM;
  250. goto quit;
  251. }
  252. if(!ldap_ca) {
  253. failf(data, "LDAP local: ERROR PEM CA cert not set!");
  254. status = CURLE_SSL_CERTPROBLEM;
  255. goto quit;
  256. }
  257. infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
  258. rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
  259. if(rc != LDAP_SUCCESS) {
  260. failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
  261. ldap_err2string(rc));
  262. status = CURLE_SSL_CERTPROBLEM;
  263. goto quit;
  264. }
  265. ldap_option = LDAP_OPT_X_TLS_DEMAND;
  266. } else {
  267. ldap_option = LDAP_OPT_X_TLS_NEVER;
  268. }
  269. rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
  270. if(rc != LDAP_SUCCESS) {
  271. failf(data, "LDAP local: ERROR setting cert verify mode: %s",
  272. ldap_err2string(rc));
  273. status = CURLE_SSL_CERTPROBLEM;
  274. goto quit;
  275. }
  276. server = ldap_init(conn->host.name, (int)conn->port);
  277. if(server == NULL) {
  278. failf(data, "LDAP local: Cannot connect to %s:%hu",
  279. conn->host.name, conn->port);
  280. status = CURLE_COULDNT_CONNECT;
  281. goto quit;
  282. }
  283. ldap_option = LDAP_OPT_X_TLS_HARD;
  284. rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
  285. if(rc != LDAP_SUCCESS) {
  286. failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
  287. ldap_err2string(rc));
  288. status = CURLE_SSL_CERTPROBLEM;
  289. goto quit;
  290. }
  291. /*
  292. rc = ldap_start_tls_s(server, NULL, NULL);
  293. if(rc != LDAP_SUCCESS) {
  294. failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
  295. ldap_err2string(rc));
  296. status = CURLE_SSL_CERTPROBLEM;
  297. goto quit;
  298. }
  299. */
  300. #else
  301. /* we should probably never come up to here since configure
  302. should check in first place if we can support LDAP SSL/TLS */
  303. failf(data, "LDAP local: SSL/TLS not supported with this version "
  304. "of the OpenLDAP toolkit\n");
  305. status = CURLE_SSL_CERTPROBLEM;
  306. goto quit;
  307. #endif
  308. #endif
  309. #endif /* CURL_LDAP_USE_SSL */
  310. } else {
  311. server = ldap_init(conn->host.name, (int)conn->port);
  312. if(server == NULL) {
  313. failf(data, "LDAP local: Cannot connect to %s:%hu",
  314. conn->host.name, conn->port);
  315. status = CURLE_COULDNT_CONNECT;
  316. goto quit;
  317. }
  318. }
  319. #ifdef CURL_LDAP_WIN
  320. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  321. #endif
  322. rc = ldap_simple_bind_s(server,
  323. conn->bits.user_passwd ? conn->user : NULL,
  324. conn->bits.user_passwd ? conn->passwd : NULL);
  325. if(!ldap_ssl && rc != 0) {
  326. ldap_proto = LDAP_VERSION2;
  327. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  328. rc = ldap_simple_bind_s(server,
  329. conn->bits.user_passwd ? conn->user : NULL,
  330. conn->bits.user_passwd ? conn->passwd : NULL);
  331. }
  332. if(rc != 0) {
  333. failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
  334. status = CURLE_LDAP_CANNOT_BIND;
  335. goto quit;
  336. }
  337. rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
  338. ludp->lud_filter, ludp->lud_attrs, 0, &result);
  339. if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
  340. failf(data, "LDAP remote: %s", ldap_err2string(rc));
  341. status = CURLE_LDAP_SEARCH_FAILED;
  342. goto quit;
  343. }
  344. for(num = 0, entryIterator = ldap_first_entry(server, result);
  345. entryIterator;
  346. entryIterator = ldap_next_entry(server, entryIterator), num++)
  347. {
  348. BerElement *ber = NULL;
  349. char *attribute; /*! suspicious that this isn't 'const' */
  350. char *dn = ldap_get_dn(server, entryIterator);
  351. int i;
  352. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
  353. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
  354. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  355. dlsize += strlen(dn)+5;
  356. for (attribute = ldap_first_attribute(server, entryIterator, &ber);
  357. attribute;
  358. attribute = ldap_next_attribute(server, entryIterator, ber))
  359. {
  360. BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
  361. if(vals != NULL)
  362. {
  363. for (i = 0; (vals[i] != NULL); i++)
  364. {
  365. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
  366. Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
  367. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
  368. dlsize += strlen(attribute)+3;
  369. if((strlen(attribute) > 7) &&
  370. (strcmp(";binary",
  371. (char *)attribute +
  372. (strlen((char *)attribute) - 7)) == 0)) {
  373. /* Binary attribute, encode to base64. */
  374. val_b64_sz = Curl_base64_encode(data,
  375. vals[i]->bv_val,
  376. vals[i]->bv_len,
  377. &val_b64);
  378. if(val_b64_sz > 0) {
  379. Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
  380. free(val_b64);
  381. dlsize += val_b64_sz;
  382. }
  383. }
  384. else {
  385. Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
  386. vals[i]->bv_len);
  387. dlsize += vals[i]->bv_len;
  388. }
  389. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
  390. dlsize++;
  391. }
  392. /* Free memory used to store values */
  393. ldap_value_free_len(vals);
  394. }
  395. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  396. dlsize++;
  397. Curl_pgrsSetDownloadCounter(data, dlsize);
  398. ldap_memfree(attribute);
  399. }
  400. ldap_memfree(dn);
  401. if(ber)
  402. ber_free(ber, 0);
  403. }
  404. quit:
  405. if(result) {
  406. ldap_msgfree(result);
  407. LDAP_TRACE (("Received %d entries\n", num));
  408. }
  409. if(rc == LDAP_SIZELIMIT_EXCEEDED)
  410. infof(data, "There are more than %d entries\n", num);
  411. if(ludp)
  412. ldap_free_urldesc(ludp);
  413. if(server)
  414. ldap_unbind_s(server);
  415. #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
  416. if(ldap_ssl)
  417. ldapssl_client_deinit();
  418. #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
  419. /* no data to transfer */
  420. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  421. conn->bits.close = TRUE;
  422. return status;
  423. }
  424. #ifdef DEBUG_LDAP
  425. static void _ldap_trace (const char *fmt, ...)
  426. {
  427. static int do_trace = -1;
  428. va_list args;
  429. if(do_trace == -1) {
  430. const char *env = getenv("CURL_TRACE");
  431. do_trace = (env && strtol(env, NULL, 10) > 0);
  432. }
  433. if(!do_trace)
  434. return;
  435. va_start (args, fmt);
  436. vfprintf (stderr, fmt, args);
  437. va_end (args);
  438. }
  439. #endif
  440. #ifndef HAVE_LDAP_URL_PARSE
  441. /*
  442. * Return scope-value for a scope-string.
  443. */
  444. static int str2scope (const char *p)
  445. {
  446. if(strequal(p, "one"))
  447. return LDAP_SCOPE_ONELEVEL;
  448. if(strequal(p, "onetree"))
  449. return LDAP_SCOPE_ONELEVEL;
  450. if(strequal(p, "base"))
  451. return LDAP_SCOPE_BASE;
  452. if(strequal(p, "sub"))
  453. return LDAP_SCOPE_SUBTREE;
  454. if(strequal( p, "subtree"))
  455. return LDAP_SCOPE_SUBTREE;
  456. return (-1);
  457. }
  458. /*
  459. * Split 'str' into strings separated by commas.
  460. * Note: res[] points into 'str'.
  461. */
  462. static char **split_str (char *str)
  463. {
  464. char **res, *lasts, *s;
  465. int i;
  466. for (i = 2, s = strchr(str,','); s; i++)
  467. s = strchr(++s,',');
  468. res = calloc(i, sizeof(char*));
  469. if(!res)
  470. return NULL;
  471. for (i = 0, s = strtok_r(str, ",", &lasts); s;
  472. s = strtok_r(NULL, ",", &lasts), i++)
  473. res[i] = s;
  474. return res;
  475. }
  476. /*
  477. * Unescape the LDAP-URL components
  478. */
  479. static bool unescape_elements (void *data, LDAPURLDesc *ludp)
  480. {
  481. int i;
  482. if(ludp->lud_filter) {
  483. ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
  484. if(!ludp->lud_filter)
  485. return (FALSE);
  486. }
  487. for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
  488. ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
  489. if(!ludp->lud_attrs[i])
  490. return (FALSE);
  491. }
  492. for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
  493. ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
  494. if(!ludp->lud_exts[i])
  495. return (FALSE);
  496. }
  497. if(ludp->lud_dn) {
  498. char *dn = ludp->lud_dn;
  499. char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
  500. free(dn);
  501. ludp->lud_dn = new_dn;
  502. if(!new_dn)
  503. return (FALSE);
  504. }
  505. return (TRUE);
  506. }
  507. /*
  508. * Break apart the pieces of an LDAP URL.
  509. * Syntax:
  510. * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
  511. *
  512. * <hostname> already known from 'conn->host.name'.
  513. * <port> already known from 'conn->remote_port'.
  514. * extract the rest from 'conn->data->state.path+1'. All fields are optional.
  515. * e.g.
  516. * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
  517. * yields ludp->lud_dn = "".
  518. *
  519. * Defined in RFC4516 section 2.
  520. */
  521. static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
  522. {
  523. char *p, *q;
  524. int i;
  525. if(!conn->data ||
  526. !conn->data->state.path ||
  527. conn->data->state.path[0] != '/' ||
  528. !checkprefix("LDAP", conn->data->change.url))
  529. return LDAP_INVALID_SYNTAX;
  530. ludp->lud_scope = LDAP_SCOPE_BASE;
  531. ludp->lud_port = conn->remote_port;
  532. ludp->lud_host = conn->host.name;
  533. /* parse DN (Distinguished Name).
  534. */
  535. ludp->lud_dn = strdup(conn->data->state.path+1);
  536. if(!ludp->lud_dn)
  537. return LDAP_NO_MEMORY;
  538. p = strchr(ludp->lud_dn, '?');
  539. LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
  540. strlen(ludp->lud_dn), ludp->lud_dn));
  541. if(!p)
  542. goto success;
  543. *p++ = '\0';
  544. /* parse attributes. skip "??".
  545. */
  546. q = strchr(p, '?');
  547. if(q)
  548. *q++ = '\0';
  549. if(*p && *p != '?') {
  550. ludp->lud_attrs = split_str(p);
  551. if(!ludp->lud_attrs)
  552. return LDAP_NO_MEMORY;
  553. for (i = 0; ludp->lud_attrs[i]; i++)
  554. LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
  555. }
  556. p = q;
  557. if(!p)
  558. goto success;
  559. /* parse scope. skip "??"
  560. */
  561. q = strchr(p, '?');
  562. if(q)
  563. *q++ = '\0';
  564. if(*p && *p != '?') {
  565. ludp->lud_scope = str2scope(p);
  566. if(ludp->lud_scope == -1)
  567. return LDAP_INVALID_SYNTAX;
  568. LDAP_TRACE (("scope %d\n", ludp->lud_scope));
  569. }
  570. p = q;
  571. if(!p)
  572. goto success;
  573. /* parse filter
  574. */
  575. q = strchr(p, '?');
  576. if(q)
  577. *q++ = '\0';
  578. if(!*p)
  579. return LDAP_INVALID_SYNTAX;
  580. ludp->lud_filter = p;
  581. LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
  582. p = q;
  583. if(!p)
  584. goto success;
  585. /* parse extensions
  586. */
  587. ludp->lud_exts = split_str(p);
  588. if(!ludp->lud_exts)
  589. return LDAP_NO_MEMORY;
  590. for (i = 0; ludp->lud_exts[i]; i++)
  591. LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
  592. success:
  593. if(!unescape_elements(conn->data, ludp))
  594. return LDAP_NO_MEMORY;
  595. return LDAP_SUCCESS;
  596. }
  597. static int _ldap_url_parse (const struct connectdata *conn,
  598. LDAPURLDesc **ludpp)
  599. {
  600. LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
  601. int rc;
  602. *ludpp = NULL;
  603. if(!ludp)
  604. return LDAP_NO_MEMORY;
  605. rc = _ldap_url_parse2 (conn, ludp);
  606. if(rc != LDAP_SUCCESS) {
  607. _ldap_free_urldesc(ludp);
  608. ludp = NULL;
  609. }
  610. *ludpp = ludp;
  611. return (rc);
  612. }
  613. static void _ldap_free_urldesc (LDAPURLDesc *ludp)
  614. {
  615. int i;
  616. if(!ludp)
  617. return;
  618. if(ludp->lud_dn)
  619. free(ludp->lud_dn);
  620. if(ludp->lud_filter)
  621. free(ludp->lud_filter);
  622. if(ludp->lud_attrs) {
  623. for (i = 0; ludp->lud_attrs[i]; i++)
  624. free(ludp->lud_attrs[i]);
  625. free(ludp->lud_attrs);
  626. }
  627. if(ludp->lud_exts) {
  628. for (i = 0; ludp->lud_exts[i]; i++)
  629. free(ludp->lud_exts[i]);
  630. free(ludp->lud_exts);
  631. }
  632. free (ludp);
  633. }
  634. #endif /* !HAVE_LDAP_URL_PARSE */
  635. #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */