ldap.c 21 KB

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