ldap.c 28 KB

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