ldap.c 21 KB

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