ldap.c 20 KB

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