ldap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_LDAP
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #ifdef CURL_LDAP_HYBRID /* If W$ definitions are needed. */
  33. # include <windows.h>
  34. /* Remember we are NOT in a W$ compiler! */
  35. # undef WIN32
  36. # undef _WIN32
  37. # undef __WIN32__
  38. #endif
  39. #ifdef CURL_LDAP_WIN /* Use W$ LDAP implementation. */
  40. # include <winldap.h>
  41. # ifndef LDAP_VENDOR_NAME
  42. # error Your Platform SDK is NOT sufficient for LDAP support! Update your Platform SDK, or disable LDAP support!
  43. # else
  44. # include <winber.h>
  45. # endif
  46. #else
  47. #define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
  48. #ifdef HAVE_LBER_H
  49. # include <lber.h>
  50. #endif
  51. # include <ldap.h>
  52. #if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
  53. # include <ldap_ssl.h>
  54. #endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
  55. #endif
  56. #ifdef HAVE_UNISTD_H
  57. # include <unistd.h>
  58. #endif
  59. #include "urldata.h"
  60. #include <curl/curl.h>
  61. #include "sendf.h"
  62. #include "escape.h"
  63. #include "progress.h"
  64. #include "transfer.h"
  65. #include "strequal.h"
  66. #include "strtok.h"
  67. #include "curl_ldap.h"
  68. #include "curl_memory.h"
  69. #include "curl_base64.h"
  70. #include "rawstr.h"
  71. #define _MPRINTF_REPLACE /* use our functions only */
  72. #include <curl/mprintf.h>
  73. #include "memdebug.h"
  74. #ifndef HAVE_LDAP_URL_PARSE
  75. /* Use our own implementation. */
  76. typedef struct {
  77. char *lud_host;
  78. int lud_port;
  79. char *lud_dn;
  80. char **lud_attrs;
  81. int lud_scope;
  82. char *lud_filter;
  83. char **lud_exts;
  84. } CURL_LDAPURLDesc;
  85. #undef LDAPURLDesc
  86. #define LDAPURLDesc CURL_LDAPURLDesc
  87. static int _ldap_url_parse (const struct connectdata *conn,
  88. LDAPURLDesc **ludp);
  89. static void _ldap_free_urldesc (LDAPURLDesc *ludp);
  90. #undef ldap_free_urldesc
  91. #define ldap_free_urldesc _ldap_free_urldesc
  92. #endif
  93. #ifdef DEBUG_LDAP
  94. #define LDAP_TRACE(x) do { \
  95. _ldap_trace ("%u: ", __LINE__); \
  96. _ldap_trace x; \
  97. } while(0)
  98. static void _ldap_trace (const char *fmt, ...);
  99. #else
  100. #define LDAP_TRACE(x) ((void)0)
  101. #endif
  102. static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
  103. /*
  104. * LDAP protocol handler.
  105. */
  106. const struct Curl_handler Curl_handler_ldap = {
  107. "LDAP", /* scheme */
  108. ZERO_NULL, /* setup_connection */
  109. Curl_ldap, /* do_it */
  110. ZERO_NULL, /* done */
  111. ZERO_NULL, /* do_more */
  112. ZERO_NULL, /* connect_it */
  113. ZERO_NULL, /* connecting */
  114. ZERO_NULL, /* doing */
  115. ZERO_NULL, /* proto_getsock */
  116. ZERO_NULL, /* doing_getsock */
  117. ZERO_NULL, /* perform_getsock */
  118. ZERO_NULL, /* disconnect */
  119. PORT_LDAP, /* defport */
  120. PROT_LDAP /* protocol */
  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, /* perform_getsock */
  138. ZERO_NULL, /* disconnect */
  139. PORT_LDAPS, /* defport */
  140. PROT_LDAP | PROT_SSL /* protocol */
  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;
  156. size_t val_b64_sz;
  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->protocol & PROT_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 doesnt 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. } else {
  225. ldap_option = LDAPSSL_VERIFY_NONE;
  226. }
  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:%d",
  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 does only support 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. } else {
  265. ldap_option = LDAP_OPT_X_TLS_NEVER;
  266. }
  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:%d",
  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. } else {
  309. server = ldap_init(conn->host.name, (int)conn->port);
  310. if(server == NULL) {
  311. failf(data, "LDAP local: Cannot connect to %s:%d",
  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. {
  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. {
  358. BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
  359. if(vals != NULL)
  360. {
  361. for (i = 0; (vals[i] != NULL); i++)
  362. {
  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. val_b64_sz = Curl_base64_encode(data,
  373. vals[i]->bv_val,
  374. vals[i]->bv_len,
  375. &val_b64);
  376. if(val_b64_sz > 0) {
  377. Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
  378. free(val_b64);
  379. dlsize += val_b64_sz;
  380. }
  381. }
  382. else {
  383. Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
  384. vals[i]->bv_len);
  385. dlsize += vals[i]->bv_len;
  386. }
  387. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
  388. dlsize++;
  389. }
  390. /* Free memory used to store values */
  391. ldap_value_free_len(vals);
  392. }
  393. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  394. dlsize++;
  395. Curl_pgrsSetDownloadCounter(data, dlsize);
  396. ldap_memfree(attribute);
  397. }
  398. ldap_memfree(dn);
  399. if(ber)
  400. ber_free(ber, 0);
  401. }
  402. quit:
  403. if(result) {
  404. ldap_msgfree(result);
  405. LDAP_TRACE (("Received %d entries\n", num));
  406. }
  407. if(rc == LDAP_SIZELIMIT_EXCEEDED)
  408. infof(data, "There are more than %d entries\n", num);
  409. if(ludp)
  410. ldap_free_urldesc(ludp);
  411. if(server)
  412. ldap_unbind_s(server);
  413. #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
  414. if(ldap_ssl)
  415. ldapssl_client_deinit();
  416. #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
  417. /* no data to transfer */
  418. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  419. conn->bits.close = TRUE;
  420. return status;
  421. }
  422. #ifdef DEBUG_LDAP
  423. static void _ldap_trace (const char *fmt, ...)
  424. {
  425. static int do_trace = -1;
  426. va_list args;
  427. if(do_trace == -1) {
  428. const char *env = getenv("CURL_TRACE");
  429. do_trace = (env && atoi(env) > 0);
  430. }
  431. if(!do_trace)
  432. return;
  433. va_start (args, fmt);
  434. vfprintf (stderr, fmt, args);
  435. va_end (args);
  436. }
  437. #endif
  438. #ifndef HAVE_LDAP_URL_PARSE
  439. /*
  440. * Return scope-value for a scope-string.
  441. */
  442. static int str2scope (const char *p)
  443. {
  444. if(strequal(p, "one"))
  445. return LDAP_SCOPE_ONELEVEL;
  446. if(strequal(p, "onetree"))
  447. return LDAP_SCOPE_ONELEVEL;
  448. if(strequal(p, "base"))
  449. return LDAP_SCOPE_BASE;
  450. if(strequal(p, "sub"))
  451. return LDAP_SCOPE_SUBTREE;
  452. if(strequal( p, "subtree"))
  453. return LDAP_SCOPE_SUBTREE;
  454. return (-1);
  455. }
  456. /*
  457. * Split 'str' into strings separated by commas.
  458. * Note: res[] points into 'str'.
  459. */
  460. static char **split_str (char *str)
  461. {
  462. char **res, *lasts, *s;
  463. int i;
  464. for (i = 2, s = strchr(str,','); s; i++)
  465. s = strchr(++s,',');
  466. res = calloc(i, sizeof(char*));
  467. if(!res)
  468. return NULL;
  469. for (i = 0, s = strtok_r(str, ",", &lasts); s;
  470. s = strtok_r(NULL, ",", &lasts), i++)
  471. res[i] = s;
  472. return res;
  473. }
  474. /*
  475. * Unescape the LDAP-URL components
  476. */
  477. static bool unescape_elements (void *data, LDAPURLDesc *ludp)
  478. {
  479. int i;
  480. if(ludp->lud_filter) {
  481. ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
  482. if(!ludp->lud_filter)
  483. return (FALSE);
  484. }
  485. for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
  486. ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
  487. if(!ludp->lud_attrs[i])
  488. return (FALSE);
  489. }
  490. for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
  491. ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
  492. if(!ludp->lud_exts[i])
  493. return (FALSE);
  494. }
  495. if(ludp->lud_dn) {
  496. char *dn = ludp->lud_dn;
  497. char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
  498. free(dn);
  499. ludp->lud_dn = new_dn;
  500. if(!new_dn)
  501. return (FALSE);
  502. }
  503. return (TRUE);
  504. }
  505. /*
  506. * Break apart the pieces of an LDAP URL.
  507. * Syntax:
  508. * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
  509. *
  510. * <hostname> already known from 'conn->host.name'.
  511. * <port> already known from 'conn->remote_port'.
  512. * extract the rest from 'conn->data->state.path+1'. All fields are optional.
  513. * e.g.
  514. * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
  515. * yields ludp->lud_dn = "".
  516. *
  517. * Defined in RFC4516 section 2.
  518. */
  519. static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
  520. {
  521. char *p, *q;
  522. int i;
  523. if(!conn->data ||
  524. !conn->data->state.path ||
  525. conn->data->state.path[0] != '/' ||
  526. !checkprefix("LDAP", conn->data->change.url))
  527. return LDAP_INVALID_SYNTAX;
  528. ludp->lud_scope = LDAP_SCOPE_BASE;
  529. ludp->lud_port = conn->remote_port;
  530. ludp->lud_host = conn->host.name;
  531. /* parse DN (Distinguished Name).
  532. */
  533. ludp->lud_dn = strdup(conn->data->state.path+1);
  534. if(!ludp->lud_dn)
  535. return LDAP_NO_MEMORY;
  536. p = strchr(ludp->lud_dn, '?');
  537. LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
  538. strlen(ludp->lud_dn), ludp->lud_dn));
  539. if(!p)
  540. goto success;
  541. *p++ = '\0';
  542. /* parse attributes. skip "??".
  543. */
  544. q = strchr(p, '?');
  545. if(q)
  546. *q++ = '\0';
  547. if(*p && *p != '?') {
  548. ludp->lud_attrs = split_str(p);
  549. if(!ludp->lud_attrs)
  550. return LDAP_NO_MEMORY;
  551. for (i = 0; ludp->lud_attrs[i]; i++)
  552. LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
  553. }
  554. p = q;
  555. if(!p)
  556. goto success;
  557. /* parse scope. skip "??"
  558. */
  559. q = strchr(p, '?');
  560. if(q)
  561. *q++ = '\0';
  562. if(*p && *p != '?') {
  563. ludp->lud_scope = str2scope(p);
  564. if(ludp->lud_scope == -1)
  565. return LDAP_INVALID_SYNTAX;
  566. LDAP_TRACE (("scope %d\n", ludp->lud_scope));
  567. }
  568. p = q;
  569. if(!p)
  570. goto success;
  571. /* parse filter
  572. */
  573. q = strchr(p, '?');
  574. if(q)
  575. *q++ = '\0';
  576. if(!*p)
  577. return LDAP_INVALID_SYNTAX;
  578. ludp->lud_filter = p;
  579. LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
  580. p = q;
  581. if(!p)
  582. goto success;
  583. /* parse extensions
  584. */
  585. ludp->lud_exts = split_str(p);
  586. if(!ludp->lud_exts)
  587. return LDAP_NO_MEMORY;
  588. for (i = 0; ludp->lud_exts[i]; i++)
  589. LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
  590. success:
  591. if(!unescape_elements(conn->data, ludp))
  592. return LDAP_NO_MEMORY;
  593. return LDAP_SUCCESS;
  594. }
  595. static int _ldap_url_parse (const struct connectdata *conn,
  596. LDAPURLDesc **ludpp)
  597. {
  598. LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
  599. int rc;
  600. *ludpp = NULL;
  601. if(!ludp)
  602. return LDAP_NO_MEMORY;
  603. rc = _ldap_url_parse2 (conn, ludp);
  604. if(rc != LDAP_SUCCESS) {
  605. _ldap_free_urldesc(ludp);
  606. ludp = NULL;
  607. }
  608. *ludpp = ludp;
  609. return (rc);
  610. }
  611. static void _ldap_free_urldesc (LDAPURLDesc *ludp)
  612. {
  613. int i;
  614. if(!ludp)
  615. return;
  616. if(ludp->lud_dn)
  617. free(ludp->lud_dn);
  618. if(ludp->lud_filter)
  619. free(ludp->lud_filter);
  620. if(ludp->lud_attrs) {
  621. for (i = 0; ludp->lud_attrs[i]; i++)
  622. free(ludp->lud_attrs[i]);
  623. free(ludp->lud_attrs);
  624. }
  625. if(ludp->lud_exts) {
  626. for (i = 0; ludp->lud_exts[i]; i++)
  627. free(ludp->lud_exts[i]);
  628. free(ludp->lud_exts);
  629. }
  630. free (ludp);
  631. }
  632. #endif /* !HAVE_LDAP_URL_PARSE */
  633. #endif /* CURL_DISABLE_LDAP */