2
0

ldap.c 30 KB

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