ldap.c 26 KB

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