ldap.c 29 KB

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