ldap.c 21 KB

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