ldap.c 26 KB

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