ldap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, Daniel Stenberg, <[email protected]>, 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_multibyte.h"
  63. #include "curl_base64.h"
  64. #include "rawstr.h"
  65. #include "connect.h"
  66. #define _MPRINTF_REPLACE /* use our functions only */
  67. #include <curl/mprintf.h>
  68. #include "memdebug.h"
  69. #ifndef HAVE_LDAP_URL_PARSE
  70. /* Use our own implementation. */
  71. typedef struct {
  72. char *lud_host;
  73. int lud_port;
  74. #if defined(CURL_LDAP_WIN)
  75. TCHAR *lud_dn;
  76. TCHAR **lud_attrs;
  77. #else
  78. char *lud_dn;
  79. char **lud_attrs;
  80. #endif
  81. int lud_scope;
  82. #if defined(CURL_LDAP_WIN)
  83. TCHAR *lud_filter;
  84. #else
  85. char *lud_filter;
  86. #endif
  87. char **lud_exts;
  88. size_t lud_attrs_dups; /* how many were dup'ed, this field is not in the
  89. "real" struct so can only be used in code
  90. without HAVE_LDAP_URL_PARSE defined */
  91. } CURL_LDAPURLDesc;
  92. #undef LDAPURLDesc
  93. #define LDAPURLDesc CURL_LDAPURLDesc
  94. static int _ldap_url_parse (const struct connectdata *conn,
  95. LDAPURLDesc **ludp);
  96. static void _ldap_free_urldesc (LDAPURLDesc *ludp);
  97. #undef ldap_free_urldesc
  98. #define ldap_free_urldesc _ldap_free_urldesc
  99. #endif
  100. #ifdef DEBUG_LDAP
  101. #define LDAP_TRACE(x) do { \
  102. _ldap_trace ("%u: ", __LINE__); \
  103. _ldap_trace x; \
  104. } WHILE_FALSE
  105. static void _ldap_trace (const char *fmt, ...);
  106. #else
  107. #define LDAP_TRACE(x) Curl_nop_stmt
  108. #endif
  109. static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
  110. /*
  111. * LDAP protocol handler.
  112. */
  113. const struct Curl_handler Curl_handler_ldap = {
  114. "LDAP", /* scheme */
  115. ZERO_NULL, /* setup_connection */
  116. Curl_ldap, /* do_it */
  117. ZERO_NULL, /* done */
  118. ZERO_NULL, /* do_more */
  119. ZERO_NULL, /* connect_it */
  120. ZERO_NULL, /* connecting */
  121. ZERO_NULL, /* doing */
  122. ZERO_NULL, /* proto_getsock */
  123. ZERO_NULL, /* doing_getsock */
  124. ZERO_NULL, /* domore_getsock */
  125. ZERO_NULL, /* perform_getsock */
  126. ZERO_NULL, /* disconnect */
  127. ZERO_NULL, /* readwrite */
  128. PORT_LDAP, /* defport */
  129. CURLPROTO_LDAP, /* protocol */
  130. PROTOPT_NONE /* flags */
  131. };
  132. #ifdef HAVE_LDAP_SSL
  133. /*
  134. * LDAPS protocol handler.
  135. */
  136. const struct Curl_handler Curl_handler_ldaps = {
  137. "LDAPS", /* scheme */
  138. ZERO_NULL, /* setup_connection */
  139. Curl_ldap, /* do_it */
  140. ZERO_NULL, /* done */
  141. ZERO_NULL, /* do_more */
  142. ZERO_NULL, /* connect_it */
  143. ZERO_NULL, /* connecting */
  144. ZERO_NULL, /* doing */
  145. ZERO_NULL, /* proto_getsock */
  146. ZERO_NULL, /* doing_getsock */
  147. ZERO_NULL, /* domore_getsock */
  148. ZERO_NULL, /* perform_getsock */
  149. ZERO_NULL, /* disconnect */
  150. ZERO_NULL, /* readwrite */
  151. PORT_LDAPS, /* defport */
  152. CURLPROTO_LDAPS, /* protocol */
  153. PROTOPT_SSL /* flags */
  154. };
  155. #endif
  156. static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
  157. {
  158. CURLcode result = CURLE_OK;
  159. int rc = 0;
  160. LDAP *server = NULL;
  161. LDAPURLDesc *ludp = NULL;
  162. LDAPMessage *ldapmsg = NULL;
  163. LDAPMessage *entryIterator;
  164. int num = 0;
  165. struct SessionHandle *data=conn->data;
  166. int ldap_proto = LDAP_VERSION3;
  167. int ldap_ssl = 0;
  168. char *val_b64 = NULL;
  169. size_t val_b64_sz = 0;
  170. curl_off_t dlsize = 0;
  171. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  172. struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
  173. #endif
  174. #if defined(CURL_LDAP_WIN)
  175. TCHAR *host = NULL;
  176. TCHAR *user = NULL;
  177. TCHAR *passwd = NULL;
  178. #else
  179. char *host = NULL;
  180. char *user = NULL;
  181. char *passwd = NULL;
  182. #endif
  183. *done = TRUE; /* unconditionally */
  184. infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
  185. LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
  186. infof(data, "LDAP local: %s\n", data->change.url);
  187. #ifdef HAVE_LDAP_URL_PARSE
  188. rc = ldap_url_parse(data->change.url, &ludp);
  189. #else
  190. rc = _ldap_url_parse(conn, &ludp);
  191. #endif
  192. if(rc != 0) {
  193. failf(data, "LDAP local: %s", ldap_err2string(rc));
  194. result = CURLE_LDAP_INVALID_URL;
  195. goto quit;
  196. }
  197. /* Get the URL scheme ( either ldap or ldaps ) */
  198. if(conn->given->flags & PROTOPT_SSL)
  199. ldap_ssl = 1;
  200. infof(data, "LDAP local: trying to establish %s connection\n",
  201. ldap_ssl ? "encrypted" : "cleartext");
  202. #if defined(CURL_LDAP_WIN)
  203. host = Curl_convert_UTF8_to_tchar(conn->host.name);
  204. if(!host) {
  205. result = CURLE_OUT_OF_MEMORY;
  206. goto quit;
  207. }
  208. if(conn->bits.user_passwd) {
  209. user = Curl_convert_UTF8_to_tchar(conn->user);
  210. passwd = Curl_convert_UTF8_to_tchar(conn->passwd);
  211. if(!user || !passwd) {
  212. result = CURLE_OUT_OF_MEMORY;
  213. goto quit;
  214. }
  215. }
  216. #else
  217. host = conn->host.name;
  218. if(conn->bits.user_passwd) {
  219. user = conn->user;
  220. passwd = conn->passwd;
  221. }
  222. #endif
  223. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  224. ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
  225. #endif
  226. ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  227. if(ldap_ssl) {
  228. #ifdef HAVE_LDAP_SSL
  229. #ifdef CURL_LDAP_WIN
  230. /* Win32 LDAP SDK doesn't support insecure mode without CA! */
  231. server = ldap_sslinit(host, (int)conn->port, 1);
  232. ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
  233. #else
  234. int ldap_option;
  235. char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
  236. #if defined(CURL_HAS_NOVELL_LDAPSDK)
  237. rc = ldapssl_client_init(NULL, NULL);
  238. if(rc != LDAP_SUCCESS) {
  239. failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
  240. result = CURLE_SSL_CERTPROBLEM;
  241. goto quit;
  242. }
  243. if(data->set.ssl.verifypeer) {
  244. /* Novell SDK supports DER or BASE64 files. */
  245. int cert_type = LDAPSSL_CERT_FILETYPE_B64;
  246. if((data->set.str[STRING_CERT_TYPE]) &&
  247. (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
  248. cert_type = LDAPSSL_CERT_FILETYPE_DER;
  249. if(!ldap_ca) {
  250. failf(data, "LDAP local: ERROR %s CA cert not set!",
  251. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
  252. result = CURLE_SSL_CERTPROBLEM;
  253. goto quit;
  254. }
  255. infof(data, "LDAP local: using %s CA cert '%s'\n",
  256. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
  257. ldap_ca);
  258. rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
  259. if(rc != LDAP_SUCCESS) {
  260. failf(data, "LDAP local: ERROR setting %s CA cert: %s",
  261. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
  262. ldap_err2string(rc));
  263. result = CURLE_SSL_CERTPROBLEM;
  264. goto quit;
  265. }
  266. ldap_option = LDAPSSL_VERIFY_SERVER;
  267. }
  268. else
  269. ldap_option = LDAPSSL_VERIFY_NONE;
  270. rc = ldapssl_set_verify_mode(ldap_option);
  271. if(rc != LDAP_SUCCESS) {
  272. failf(data, "LDAP local: ERROR setting cert verify mode: %s",
  273. ldap_err2string(rc));
  274. result = CURLE_SSL_CERTPROBLEM;
  275. goto quit;
  276. }
  277. server = ldapssl_init(host, (int)conn->port, 1);
  278. if(server == NULL) {
  279. failf(data, "LDAP local: Cannot connect to %s:%ld",
  280. conn->host.dispname, conn->port);
  281. result = CURLE_COULDNT_CONNECT;
  282. goto quit;
  283. }
  284. #elif defined(LDAP_OPT_X_TLS)
  285. if(data->set.ssl.verifypeer) {
  286. /* OpenLDAP SDK supports BASE64 files. */
  287. if((data->set.str[STRING_CERT_TYPE]) &&
  288. (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
  289. failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type!");
  290. result = CURLE_SSL_CERTPROBLEM;
  291. goto quit;
  292. }
  293. if(!ldap_ca) {
  294. failf(data, "LDAP local: ERROR PEM CA cert not set!");
  295. result = CURLE_SSL_CERTPROBLEM;
  296. goto quit;
  297. }
  298. infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
  299. rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
  300. if(rc != LDAP_SUCCESS) {
  301. failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
  302. ldap_err2string(rc));
  303. result = CURLE_SSL_CERTPROBLEM;
  304. goto quit;
  305. }
  306. ldap_option = LDAP_OPT_X_TLS_DEMAND;
  307. }
  308. else
  309. ldap_option = LDAP_OPT_X_TLS_NEVER;
  310. rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
  311. if(rc != LDAP_SUCCESS) {
  312. failf(data, "LDAP local: ERROR setting cert verify mode: %s",
  313. ldap_err2string(rc));
  314. result = CURLE_SSL_CERTPROBLEM;
  315. goto quit;
  316. }
  317. server = ldap_init(host, (int)conn->port);
  318. if(server == NULL) {
  319. failf(data, "LDAP local: Cannot connect to %s:%ld",
  320. conn->host.dispname, conn->port);
  321. result = CURLE_COULDNT_CONNECT;
  322. goto quit;
  323. }
  324. ldap_option = LDAP_OPT_X_TLS_HARD;
  325. rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
  326. if(rc != LDAP_SUCCESS) {
  327. failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
  328. ldap_err2string(rc));
  329. result = CURLE_SSL_CERTPROBLEM;
  330. goto quit;
  331. }
  332. /*
  333. rc = ldap_start_tls_s(server, NULL, NULL);
  334. if(rc != LDAP_SUCCESS) {
  335. failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
  336. ldap_err2string(rc));
  337. result = CURLE_SSL_CERTPROBLEM;
  338. goto quit;
  339. }
  340. */
  341. #else
  342. /* we should probably never come up to here since configure
  343. should check in first place if we can support LDAP SSL/TLS */
  344. failf(data, "LDAP local: SSL/TLS not supported with this version "
  345. "of the OpenLDAP toolkit\n");
  346. result = CURLE_SSL_CERTPROBLEM;
  347. goto quit;
  348. #endif
  349. #endif
  350. #endif /* CURL_LDAP_USE_SSL */
  351. }
  352. else {
  353. server = ldap_init(host, (int)conn->port);
  354. if(server == NULL) {
  355. failf(data, "LDAP local: Cannot connect to %s:%ld",
  356. conn->host.dispname, conn->port);
  357. result = CURLE_COULDNT_CONNECT;
  358. goto quit;
  359. }
  360. }
  361. #ifdef CURL_LDAP_WIN
  362. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  363. #endif
  364. rc = ldap_simple_bind_s(server, user, passwd);
  365. if(!ldap_ssl && rc != 0) {
  366. ldap_proto = LDAP_VERSION2;
  367. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  368. rc = ldap_simple_bind_s(server, user, passwd);
  369. }
  370. if(rc != 0) {
  371. failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
  372. result = CURLE_LDAP_CANNOT_BIND;
  373. goto quit;
  374. }
  375. rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
  376. ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
  377. if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
  378. failf(data, "LDAP remote: %s", ldap_err2string(rc));
  379. result = CURLE_LDAP_SEARCH_FAILED;
  380. goto quit;
  381. }
  382. for(num = 0, entryIterator = ldap_first_entry(server, ldapmsg);
  383. entryIterator;
  384. entryIterator = ldap_next_entry(server, entryIterator), num++) {
  385. BerElement *ber = NULL;
  386. #if defined(CURL_LDAP_WIN)
  387. TCHAR *attribute;
  388. #else
  389. char *attribute; /*! suspicious that this isn't 'const' */
  390. #endif
  391. int i;
  392. /* Get the DN and write it to the client */
  393. {
  394. char *name;
  395. size_t name_len;
  396. #if defined(CURL_LDAP_WIN)
  397. TCHAR *dn = ldap_get_dn(server, entryIterator);
  398. name = Curl_convert_tchar_to_UTF8(dn);
  399. if(!name) {
  400. ldap_memfree(dn);
  401. result = CURLE_OUT_OF_MEMORY;
  402. goto quit;
  403. }
  404. #else
  405. char *dn = name = ldap_get_dn(server, entryIterator);
  406. #endif
  407. name_len = strlen(name);
  408. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
  409. if(result) {
  410. #if defined(CURL_LDAP_WIN)
  411. Curl_unicodefree(name);
  412. #endif
  413. ldap_memfree(dn);
  414. goto quit;
  415. }
  416. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *) name,
  417. name_len);
  418. if(result) {
  419. #if defined(CURL_LDAP_WIN)
  420. Curl_unicodefree(name);
  421. #endif
  422. ldap_memfree(dn);
  423. goto quit;
  424. }
  425. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  426. if(result) {
  427. #if defined(CURL_LDAP_WIN)
  428. Curl_unicodefree(name);
  429. #endif
  430. ldap_memfree(dn);
  431. goto quit;
  432. }
  433. dlsize += name_len + 5;
  434. #if defined(CURL_LDAP_WIN)
  435. Curl_unicodefree(name);
  436. #endif
  437. ldap_memfree(dn);
  438. }
  439. /* Get the attributes and write them to the client */
  440. for(attribute = ldap_first_attribute(server, entryIterator, &ber);
  441. attribute;
  442. attribute = ldap_next_attribute(server, entryIterator, ber)) {
  443. BerValue **vals;
  444. size_t attr_len;
  445. #if defined(CURL_LDAP_WIN)
  446. char *attr = Curl_convert_tchar_to_UTF8(attribute);
  447. if(!attr) {
  448. if(ber)
  449. ber_free(ber, 0);
  450. result = CURLE_OUT_OF_MEMORY;
  451. goto quit;
  452. }
  453. #else
  454. char *attr = attribute;
  455. #endif
  456. attr_len = strlen(attr);
  457. vals = ldap_get_values_len(server, entryIterator, attribute);
  458. if(vals != NULL) {
  459. for(i = 0; (vals[i] != NULL); i++) {
  460. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
  461. if(result) {
  462. ldap_value_free_len(vals);
  463. #if defined(CURL_LDAP_WIN)
  464. Curl_unicodefree(attr);
  465. #endif
  466. ldap_memfree(attribute);
  467. if(ber)
  468. ber_free(ber, 0);
  469. goto quit;
  470. }
  471. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  472. (char *) attr, attr_len);
  473. if(result) {
  474. ldap_value_free_len(vals);
  475. #if defined(CURL_LDAP_WIN)
  476. Curl_unicodefree(attr);
  477. #endif
  478. ldap_memfree(attribute);
  479. if(ber)
  480. ber_free(ber, 0);
  481. goto quit;
  482. }
  483. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
  484. if(result) {
  485. ldap_value_free_len(vals);
  486. #if defined(CURL_LDAP_WIN)
  487. Curl_unicodefree(attr);
  488. #endif
  489. ldap_memfree(attribute);
  490. if(ber)
  491. ber_free(ber, 0);
  492. goto quit;
  493. }
  494. dlsize += attr_len + 3;
  495. if((attr_len > 7) &&
  496. (strcmp(";binary", (char *) attr + (attr_len - 7)) == 0)) {
  497. /* Binary attribute, encode to base64. */
  498. result = Curl_base64_encode(data,
  499. vals[i]->bv_val,
  500. vals[i]->bv_len,
  501. &val_b64,
  502. &val_b64_sz);
  503. if(result) {
  504. ldap_value_free_len(vals);
  505. #if defined(CURL_LDAP_WIN)
  506. Curl_unicodefree(attr);
  507. #endif
  508. ldap_memfree(attribute);
  509. if(ber)
  510. ber_free(ber, 0);
  511. goto quit;
  512. }
  513. if(val_b64_sz > 0) {
  514. result = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
  515. val_b64_sz);
  516. free(val_b64);
  517. if(result) {
  518. ldap_value_free_len(vals);
  519. #if defined(CURL_LDAP_WIN)
  520. Curl_unicodefree(attr);
  521. #endif
  522. ldap_memfree(attribute);
  523. if(ber)
  524. ber_free(ber, 0);
  525. goto quit;
  526. }
  527. dlsize += val_b64_sz;
  528. }
  529. }
  530. else {
  531. result = Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
  532. vals[i]->bv_len);
  533. if(result) {
  534. ldap_value_free_len(vals);
  535. #if defined(CURL_LDAP_WIN)
  536. Curl_unicodefree(attr);
  537. #endif
  538. ldap_memfree(attribute);
  539. if(ber)
  540. ber_free(ber, 0);
  541. goto quit;
  542. }
  543. dlsize += vals[i]->bv_len;
  544. }
  545. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  546. if(result) {
  547. ldap_value_free_len(vals);
  548. #if defined(CURL_LDAP_WIN)
  549. Curl_unicodefree(attr);
  550. #endif
  551. ldap_memfree(attribute);
  552. if(ber)
  553. ber_free(ber, 0);
  554. goto quit;
  555. }
  556. dlsize++;
  557. }
  558. /* Free memory used to store values */
  559. ldap_value_free_len(vals);
  560. }
  561. /* Free the attribute as we are done with it */
  562. #if defined(CURL_LDAP_WIN)
  563. Curl_unicodefree(attr);
  564. #endif
  565. ldap_memfree(attribute);
  566. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  567. if(result)
  568. goto quit;
  569. dlsize++;
  570. Curl_pgrsSetDownloadCounter(data, dlsize);
  571. }
  572. if(ber)
  573. ber_free(ber, 0);
  574. }
  575. quit:
  576. if(ldapmsg) {
  577. ldap_msgfree(ldapmsg);
  578. LDAP_TRACE (("Received %d entries\n", num));
  579. }
  580. if(rc == LDAP_SIZELIMIT_EXCEEDED)
  581. infof(data, "There are more than %d entries\n", num);
  582. if(ludp)
  583. ldap_free_urldesc(ludp);
  584. if(server)
  585. ldap_unbind_s(server);
  586. #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
  587. if(ldap_ssl)
  588. ldapssl_client_deinit();
  589. #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
  590. #if defined(CURL_LDAP_WIN)
  591. Curl_unicodefree(passwd);
  592. Curl_unicodefree(user);
  593. Curl_unicodefree(host);
  594. #endif
  595. /* no data to transfer */
  596. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  597. connclose(conn, "LDAP connection always disable re-use");
  598. return result;
  599. }
  600. #ifdef DEBUG_LDAP
  601. static void _ldap_trace (const char *fmt, ...)
  602. {
  603. static int do_trace = -1;
  604. va_list args;
  605. if(do_trace == -1) {
  606. const char *env = getenv("CURL_TRACE");
  607. do_trace = (env && strtol(env, NULL, 10) > 0);
  608. }
  609. if(!do_trace)
  610. return;
  611. va_start (args, fmt);
  612. vfprintf (stderr, fmt, args);
  613. va_end (args);
  614. }
  615. #endif
  616. #ifndef HAVE_LDAP_URL_PARSE
  617. /*
  618. * Return scope-value for a scope-string.
  619. */
  620. static int str2scope (const char *p)
  621. {
  622. if(strequal(p, "one"))
  623. return LDAP_SCOPE_ONELEVEL;
  624. if(strequal(p, "onetree"))
  625. return LDAP_SCOPE_ONELEVEL;
  626. if(strequal(p, "base"))
  627. return LDAP_SCOPE_BASE;
  628. if(strequal(p, "sub"))
  629. return LDAP_SCOPE_SUBTREE;
  630. if(strequal( p, "subtree"))
  631. return LDAP_SCOPE_SUBTREE;
  632. return (-1);
  633. }
  634. /*
  635. * Split 'str' into strings separated by commas.
  636. * Note: out[] points into 'str'.
  637. */
  638. static bool split_str(char *str, char ***out, size_t *count)
  639. {
  640. char **res;
  641. char *lasts;
  642. char *s;
  643. size_t i;
  644. size_t items = 1;
  645. s = strchr(str, ',');
  646. while(s) {
  647. items++;
  648. s = strchr(++s, ',');
  649. }
  650. res = calloc(items, sizeof(char *));
  651. if(!res)
  652. return FALSE;
  653. for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
  654. s = strtok_r(NULL, ",", &lasts), i++)
  655. res[i] = s;
  656. *out = res;
  657. *count = items;
  658. return TRUE;
  659. }
  660. /*
  661. * Break apart the pieces of an LDAP URL.
  662. * Syntax:
  663. * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
  664. *
  665. * <hostname> already known from 'conn->host.name'.
  666. * <port> already known from 'conn->remote_port'.
  667. * extract the rest from 'conn->data->state.path+1'. All fields are optional.
  668. * e.g.
  669. * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
  670. * yields ludp->lud_dn = "".
  671. *
  672. * Defined in RFC4516 section 2.
  673. */
  674. static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
  675. {
  676. int rc = LDAP_SUCCESS;
  677. char *path;
  678. char *p;
  679. char *q;
  680. size_t i;
  681. if(!conn->data ||
  682. !conn->data->state.path ||
  683. conn->data->state.path[0] != '/' ||
  684. !checkprefix("LDAP", conn->data->change.url))
  685. return LDAP_INVALID_SYNTAX;
  686. ludp->lud_scope = LDAP_SCOPE_BASE;
  687. ludp->lud_port = conn->remote_port;
  688. ludp->lud_host = conn->host.name;
  689. /* Duplicate the path */
  690. p = path = strdup(conn->data->state.path + 1);
  691. if(!path)
  692. return LDAP_NO_MEMORY;
  693. /* Parse the DN (Distinguished Name) */
  694. q = strchr(p, '?');
  695. if(q)
  696. *q++ = '\0';
  697. if(*p) {
  698. char *dn = p;
  699. char *unescaped;
  700. LDAP_TRACE (("DN '%s'\n", dn));
  701. /* Unescape the DN */
  702. unescaped = curl_easy_unescape(conn->data, dn, 0, NULL);
  703. if(!unescaped) {
  704. rc = LDAP_NO_MEMORY;
  705. goto quit;
  706. }
  707. #if defined(CURL_LDAP_WIN)
  708. /* Convert the unescaped string to a tchar */
  709. ludp->lud_dn = Curl_convert_UTF8_to_tchar(unescaped);
  710. /* Free the unescaped string as we are done with it */
  711. Curl_unicodefree(unescaped);
  712. if(!ludp->lud_dn) {
  713. rc = LDAP_NO_MEMORY;
  714. goto quit;
  715. }
  716. #else
  717. ludp->lud_dn = unescaped;
  718. #endif
  719. }
  720. p = q;
  721. if(!p)
  722. goto quit;
  723. /* Parse the attributes. skip "??" */
  724. q = strchr(p, '?');
  725. if(q)
  726. *q++ = '\0';
  727. if(*p) {
  728. char **attributes;
  729. size_t count = 0;
  730. /* Split the string into an array of attributes */
  731. if(!split_str(p, &attributes, &count)) {
  732. rc = LDAP_NO_MEMORY;
  733. goto quit;
  734. }
  735. /* Allocate our array (+1 for the NULL entry) */
  736. #if defined(CURL_LDAP_WIN)
  737. ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
  738. #else
  739. ludp->lud_attrs = calloc(count + 1, sizeof(char *));
  740. #endif
  741. if(!ludp->lud_attrs) {
  742. Curl_safefree(attributes);
  743. rc = LDAP_NO_MEMORY;
  744. goto quit;
  745. }
  746. for(i = 0; i < count; i++) {
  747. char *unescaped;
  748. LDAP_TRACE (("attr[%d] '%s'\n", i, attributes[i]));
  749. /* Unescape the attribute */
  750. unescaped = curl_easy_unescape(conn->data, attributes[i], 0, NULL);
  751. if(!unescaped) {
  752. Curl_safefree(attributes);
  753. rc = LDAP_NO_MEMORY;
  754. goto quit;
  755. }
  756. #if defined(CURL_LDAP_WIN)
  757. /* Convert the unescaped string to a tchar */
  758. ludp->lud_attrs[i] = Curl_convert_UTF8_to_tchar(unescaped);
  759. /* Free the unescaped string as we are done with it */
  760. Curl_unicodefree(unescaped);
  761. if(!ludp->lud_attrs[i]) {
  762. Curl_safefree(attributes);
  763. rc = LDAP_NO_MEMORY;
  764. goto quit;
  765. }
  766. #else
  767. ludp->lud_attrs[i] = unescaped;
  768. #endif
  769. ludp->lud_attrs_dups++;
  770. }
  771. Curl_safefree(attributes);
  772. }
  773. p = q;
  774. if(!p)
  775. goto quit;
  776. /* Parse the scope. skip "??" */
  777. q = strchr(p, '?');
  778. if(q)
  779. *q++ = '\0';
  780. if(*p) {
  781. ludp->lud_scope = str2scope(p);
  782. if(ludp->lud_scope == -1) {
  783. rc = LDAP_INVALID_SYNTAX;
  784. goto quit;
  785. }
  786. LDAP_TRACE (("scope %d\n", ludp->lud_scope));
  787. }
  788. p = q;
  789. if(!p)
  790. goto quit;
  791. /* Parse the filter */
  792. q = strchr(p, '?');
  793. if(q)
  794. *q++ = '\0';
  795. if(*p) {
  796. char *filter = p;
  797. char *unescaped;
  798. LDAP_TRACE (("filter '%s'\n", filter));
  799. /* Unescape the filter */
  800. unescaped = curl_easy_unescape(conn->data, filter, 0, NULL);
  801. if(!unescaped) {
  802. rc = LDAP_NO_MEMORY;
  803. goto quit;
  804. }
  805. #if defined(CURL_LDAP_WIN)
  806. /* Convert the unescaped string to a tchar */
  807. ludp->lud_filter = Curl_convert_UTF8_to_tchar(unescaped);
  808. /* Free the unescaped string as we are done with it */
  809. Curl_unicodefree(unescaped);
  810. if(!ludp->lud_filter) {
  811. rc = LDAP_NO_MEMORY;
  812. goto quit;
  813. }
  814. #else
  815. ludp->lud_filter = unescaped;
  816. #endif
  817. }
  818. p = q;
  819. if(p && !*p) {
  820. rc = LDAP_INVALID_SYNTAX;
  821. goto quit;
  822. }
  823. quit:
  824. Curl_safefree(path);
  825. return rc;
  826. }
  827. static int _ldap_url_parse (const struct connectdata *conn,
  828. LDAPURLDesc **ludpp)
  829. {
  830. LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
  831. int rc;
  832. *ludpp = NULL;
  833. if(!ludp)
  834. return LDAP_NO_MEMORY;
  835. rc = _ldap_url_parse2 (conn, ludp);
  836. if(rc != LDAP_SUCCESS) {
  837. _ldap_free_urldesc(ludp);
  838. ludp = NULL;
  839. }
  840. *ludpp = ludp;
  841. return (rc);
  842. }
  843. static void _ldap_free_urldesc (LDAPURLDesc *ludp)
  844. {
  845. size_t i;
  846. if(!ludp)
  847. return;
  848. if(ludp->lud_dn)
  849. free(ludp->lud_dn);
  850. if(ludp->lud_filter)
  851. free(ludp->lud_filter);
  852. if(ludp->lud_attrs) {
  853. for(i = 0; i < ludp->lud_attrs_dups; i++)
  854. free(ludp->lud_attrs[i]);
  855. free(ludp->lud_attrs);
  856. }
  857. free (ludp);
  858. }
  859. #endif /* !HAVE_LDAP_URL_PARSE */
  860. #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */