ldap.c 29 KB

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