ldap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. size_t lud_attrs_dups; /* how many were dup'ed, this field is not in the
  78. "real" struct so can only be used in code
  79. without HAVE_LDAP_URL_PARSE defined */
  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, /* domore_getsock */
  114. ZERO_NULL, /* perform_getsock */
  115. ZERO_NULL, /* disconnect */
  116. ZERO_NULL, /* readwrite */
  117. PORT_LDAP, /* defport */
  118. CURLPROTO_LDAP, /* protocol */
  119. PROTOPT_NONE /* flags */
  120. };
  121. #ifdef HAVE_LDAP_SSL
  122. /*
  123. * LDAPS protocol handler.
  124. */
  125. const struct Curl_handler Curl_handler_ldaps = {
  126. "LDAPS", /* scheme */
  127. ZERO_NULL, /* setup_connection */
  128. Curl_ldap, /* do_it */
  129. ZERO_NULL, /* done */
  130. ZERO_NULL, /* do_more */
  131. ZERO_NULL, /* connect_it */
  132. ZERO_NULL, /* connecting */
  133. ZERO_NULL, /* doing */
  134. ZERO_NULL, /* proto_getsock */
  135. ZERO_NULL, /* doing_getsock */
  136. ZERO_NULL, /* domore_getsock */
  137. ZERO_NULL, /* perform_getsock */
  138. ZERO_NULL, /* disconnect */
  139. ZERO_NULL, /* readwrite */
  140. PORT_LDAPS, /* defport */
  141. CURLPROTO_LDAP | CURLPROTO_LDAPS, /* protocol */
  142. PROTOPT_SSL /* flags */
  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 = NULL;
  158. size_t val_b64_sz = 0;
  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->given->flags & PROTOPT_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 doesn't 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. }
  227. else
  228. ldap_option = LDAPSSL_VERIFY_NONE;
  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:%ld",
  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 only supports 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. }
  267. else
  268. ldap_option = LDAP_OPT_X_TLS_NEVER;
  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:%ld",
  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. }
  311. else {
  312. server = ldap_init(conn->host.name, (int)conn->port);
  313. if(server == NULL) {
  314. failf(data, "LDAP local: Cannot connect to %s:%ld",
  315. conn->host.name, conn->port);
  316. status = CURLE_COULDNT_CONNECT;
  317. goto quit;
  318. }
  319. }
  320. #ifdef CURL_LDAP_WIN
  321. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  322. #endif
  323. rc = ldap_simple_bind_s(server,
  324. conn->bits.user_passwd ? conn->user : NULL,
  325. conn->bits.user_passwd ? conn->passwd : NULL);
  326. if(!ldap_ssl && rc != 0) {
  327. ldap_proto = LDAP_VERSION2;
  328. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  329. rc = ldap_simple_bind_s(server,
  330. conn->bits.user_passwd ? conn->user : NULL,
  331. conn->bits.user_passwd ? conn->passwd : NULL);
  332. }
  333. if(rc != 0) {
  334. failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
  335. status = CURLE_LDAP_CANNOT_BIND;
  336. goto quit;
  337. }
  338. rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
  339. ludp->lud_filter, ludp->lud_attrs, 0, &result);
  340. if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
  341. failf(data, "LDAP remote: %s", ldap_err2string(rc));
  342. status = CURLE_LDAP_SEARCH_FAILED;
  343. goto quit;
  344. }
  345. for(num = 0, entryIterator = ldap_first_entry(server, result);
  346. entryIterator;
  347. entryIterator = ldap_next_entry(server, entryIterator), num++) {
  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. BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
  360. if(vals != NULL) {
  361. for(i = 0; (vals[i] != NULL); i++) {
  362. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
  363. Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
  364. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
  365. dlsize += strlen(attribute)+3;
  366. if((strlen(attribute) > 7) &&
  367. (strcmp(";binary",
  368. (char *)attribute +
  369. (strlen((char *)attribute) - 7)) == 0)) {
  370. /* Binary attribute, encode to base64. */
  371. CURLcode error = Curl_base64_encode(data,
  372. vals[i]->bv_val,
  373. vals[i]->bv_len,
  374. &val_b64,
  375. &val_b64_sz);
  376. if(error) {
  377. ldap_value_free_len(vals);
  378. ldap_memfree(attribute);
  379. ldap_memfree(dn);
  380. if(ber)
  381. ber_free(ber, 0);
  382. status = error;
  383. goto quit;
  384. }
  385. if(val_b64_sz > 0) {
  386. Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
  387. free(val_b64);
  388. dlsize += val_b64_sz;
  389. }
  390. }
  391. else {
  392. Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
  393. vals[i]->bv_len);
  394. dlsize += vals[i]->bv_len;
  395. }
  396. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
  397. dlsize++;
  398. }
  399. /* Free memory used to store values */
  400. ldap_value_free_len(vals);
  401. }
  402. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  403. dlsize++;
  404. Curl_pgrsSetDownloadCounter(data, dlsize);
  405. ldap_memfree(attribute);
  406. }
  407. ldap_memfree(dn);
  408. if(ber)
  409. ber_free(ber, 0);
  410. }
  411. quit:
  412. if(result) {
  413. ldap_msgfree(result);
  414. LDAP_TRACE (("Received %d entries\n", num));
  415. }
  416. if(rc == LDAP_SIZELIMIT_EXCEEDED)
  417. infof(data, "There are more than %d entries\n", num);
  418. if(ludp)
  419. ldap_free_urldesc(ludp);
  420. if(server)
  421. ldap_unbind_s(server);
  422. #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
  423. if(ldap_ssl)
  424. ldapssl_client_deinit();
  425. #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
  426. /* no data to transfer */
  427. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  428. conn->bits.close = TRUE;
  429. return status;
  430. }
  431. #ifdef DEBUG_LDAP
  432. static void _ldap_trace (const char *fmt, ...)
  433. {
  434. static int do_trace = -1;
  435. va_list args;
  436. if(do_trace == -1) {
  437. const char *env = getenv("CURL_TRACE");
  438. do_trace = (env && strtol(env, NULL, 10) > 0);
  439. }
  440. if(!do_trace)
  441. return;
  442. va_start (args, fmt);
  443. vfprintf (stderr, fmt, args);
  444. va_end (args);
  445. }
  446. #endif
  447. #ifndef HAVE_LDAP_URL_PARSE
  448. /*
  449. * Return scope-value for a scope-string.
  450. */
  451. static int str2scope (const char *p)
  452. {
  453. if(strequal(p, "one"))
  454. return LDAP_SCOPE_ONELEVEL;
  455. if(strequal(p, "onetree"))
  456. return LDAP_SCOPE_ONELEVEL;
  457. if(strequal(p, "base"))
  458. return LDAP_SCOPE_BASE;
  459. if(strequal(p, "sub"))
  460. return LDAP_SCOPE_SUBTREE;
  461. if(strequal( p, "subtree"))
  462. return LDAP_SCOPE_SUBTREE;
  463. return (-1);
  464. }
  465. /*
  466. * Split 'str' into strings separated by commas.
  467. * Note: res[] points into 'str'.
  468. */
  469. static char **split_str (char *str)
  470. {
  471. char **res, *lasts, *s;
  472. int i;
  473. for(i = 2, s = strchr(str,','); s; i++)
  474. s = strchr(++s,',');
  475. res = calloc(i, sizeof(char*));
  476. if(!res)
  477. return NULL;
  478. for(i = 0, s = strtok_r(str, ",", &lasts); s;
  479. s = strtok_r(NULL, ",", &lasts), i++)
  480. res[i] = s;
  481. return res;
  482. }
  483. /*
  484. * Unescape the LDAP-URL components
  485. */
  486. static bool unescape_elements (void *data, LDAPURLDesc *ludp)
  487. {
  488. int i;
  489. if(ludp->lud_filter) {
  490. ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
  491. if(!ludp->lud_filter)
  492. return FALSE;
  493. }
  494. for(i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
  495. ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i],
  496. 0, NULL);
  497. if(!ludp->lud_attrs[i])
  498. return FALSE;
  499. ludp->lud_attrs_dups++;
  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. }
  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. }
  586. ludp->lud_filter = p;
  587. LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
  588. success:
  589. if(!unescape_elements(conn->data, ludp))
  590. return LDAP_NO_MEMORY;
  591. return LDAP_SUCCESS;
  592. }
  593. static int _ldap_url_parse (const struct connectdata *conn,
  594. LDAPURLDesc **ludpp)
  595. {
  596. LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
  597. int rc;
  598. *ludpp = NULL;
  599. if(!ludp)
  600. return LDAP_NO_MEMORY;
  601. rc = _ldap_url_parse2 (conn, ludp);
  602. if(rc != LDAP_SUCCESS) {
  603. _ldap_free_urldesc(ludp);
  604. ludp = NULL;
  605. }
  606. *ludpp = ludp;
  607. return (rc);
  608. }
  609. static void _ldap_free_urldesc (LDAPURLDesc *ludp)
  610. {
  611. size_t i;
  612. if(!ludp)
  613. return;
  614. if(ludp->lud_dn)
  615. free(ludp->lud_dn);
  616. if(ludp->lud_filter)
  617. free(ludp->lud_filter);
  618. if(ludp->lud_attrs) {
  619. for(i = 0; i < ludp->lud_attrs_dups; i++)
  620. free(ludp->lud_attrs[i]);
  621. free(ludp->lud_attrs);
  622. }
  623. free (ludp);
  624. }
  625. #endif /* !HAVE_LDAP_URL_PARSE */
  626. #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */