ldap.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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, "LDAP local: %s", ldap_err2string(rc));
  274. result = CURLE_LDAP_INVALID_URL;
  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(conn->bits.user_passwd) {
  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(data,
  574. vals[i]->bv_val,
  575. vals[i]->bv_len,
  576. &val_b64,
  577. &val_b64_sz);
  578. if(result) {
  579. ldap_value_free_len(vals);
  580. FREE_ON_WINLDAP(attr);
  581. ldap_memfree(attribute);
  582. if(ber)
  583. ber_free(ber, 0);
  584. goto quit;
  585. }
  586. if(val_b64_sz > 0) {
  587. result = Curl_client_write(data, CLIENTWRITE_BODY, val_b64,
  588. val_b64_sz);
  589. free(val_b64);
  590. if(result) {
  591. ldap_value_free_len(vals);
  592. FREE_ON_WINLDAP(attr);
  593. ldap_memfree(attribute);
  594. if(ber)
  595. ber_free(ber, 0);
  596. goto quit;
  597. }
  598. dlsize += val_b64_sz;
  599. }
  600. }
  601. else {
  602. result = Curl_client_write(data, CLIENTWRITE_BODY, vals[i]->bv_val,
  603. vals[i]->bv_len);
  604. if(result) {
  605. ldap_value_free_len(vals);
  606. FREE_ON_WINLDAP(attr);
  607. ldap_memfree(attribute);
  608. if(ber)
  609. ber_free(ber, 0);
  610. goto quit;
  611. }
  612. dlsize += vals[i]->bv_len;
  613. }
  614. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
  615. if(result) {
  616. ldap_value_free_len(vals);
  617. FREE_ON_WINLDAP(attr);
  618. ldap_memfree(attribute);
  619. if(ber)
  620. ber_free(ber, 0);
  621. goto quit;
  622. }
  623. dlsize++;
  624. }
  625. /* Free memory used to store values */
  626. ldap_value_free_len(vals);
  627. }
  628. /* Free the attribute as we are done with it */
  629. FREE_ON_WINLDAP(attr);
  630. ldap_memfree(attribute);
  631. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
  632. if(result)
  633. goto quit;
  634. dlsize++;
  635. Curl_pgrsSetDownloadCounter(data, dlsize);
  636. }
  637. if(ber)
  638. ber_free(ber, 0);
  639. }
  640. quit:
  641. if(ldapmsg) {
  642. ldap_msgfree(ldapmsg);
  643. LDAP_TRACE(("Received %d entries\n", num));
  644. }
  645. if(rc == LDAP_SIZELIMIT_EXCEEDED)
  646. infof(data, "There are more than %d entries", num);
  647. if(ludp)
  648. ldap_free_urldesc(ludp);
  649. if(server)
  650. ldap_unbind_s(server);
  651. #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
  652. if(ldap_ssl)
  653. ldapssl_client_deinit();
  654. #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
  655. FREE_ON_WINLDAP(host);
  656. /* no data to transfer */
  657. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  658. connclose(conn, "LDAP connection always disable re-use");
  659. return result;
  660. }
  661. #ifdef DEBUG_LDAP
  662. static void _ldap_trace(const char *fmt, ...)
  663. {
  664. static int do_trace = -1;
  665. va_list args;
  666. if(do_trace == -1) {
  667. const char *env = getenv("CURL_TRACE");
  668. do_trace = (env && strtol(env, NULL, 10) > 0);
  669. }
  670. if(!do_trace)
  671. return;
  672. va_start(args, fmt);
  673. vfprintf(stderr, fmt, args);
  674. va_end(args);
  675. }
  676. #endif
  677. #ifndef HAVE_LDAP_URL_PARSE
  678. /*
  679. * Return scope-value for a scope-string.
  680. */
  681. static int str2scope(const char *p)
  682. {
  683. if(strcasecompare(p, "one"))
  684. return LDAP_SCOPE_ONELEVEL;
  685. if(strcasecompare(p, "onetree"))
  686. return LDAP_SCOPE_ONELEVEL;
  687. if(strcasecompare(p, "base"))
  688. return LDAP_SCOPE_BASE;
  689. if(strcasecompare(p, "sub"))
  690. return LDAP_SCOPE_SUBTREE;
  691. if(strcasecompare(p, "subtree"))
  692. return LDAP_SCOPE_SUBTREE;
  693. return (-1);
  694. }
  695. /*
  696. * Split 'str' into strings separated by commas.
  697. * Note: out[] points into 'str'.
  698. */
  699. static bool split_str(char *str, char ***out, size_t *count)
  700. {
  701. char **res;
  702. char *lasts;
  703. char *s;
  704. size_t i;
  705. size_t items = 1;
  706. s = strchr(str, ',');
  707. while(s) {
  708. items++;
  709. s = strchr(++s, ',');
  710. }
  711. res = calloc(items, sizeof(char *));
  712. if(!res)
  713. return FALSE;
  714. for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
  715. s = strtok_r(NULL, ",", &lasts), i++)
  716. res[i] = s;
  717. *out = res;
  718. *count = items;
  719. return TRUE;
  720. }
  721. /*
  722. * Break apart the pieces of an LDAP URL.
  723. * Syntax:
  724. * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
  725. *
  726. * <hostname> already known from 'conn->host.name'.
  727. * <port> already known from 'conn->remote_port'.
  728. * extract the rest from 'data->state.path+1'. All fields are optional.
  729. * e.g.
  730. * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
  731. * yields ludp->lud_dn = "".
  732. *
  733. * Defined in RFC4516 section 2.
  734. */
  735. static int _ldap_url_parse2(struct Curl_easy *data,
  736. const struct connectdata *conn, LDAPURLDesc *ludp)
  737. {
  738. int rc = LDAP_SUCCESS;
  739. char *p;
  740. char *path;
  741. char *q = NULL;
  742. char *query = NULL;
  743. size_t i;
  744. if(!data ||
  745. !data->state.up.path ||
  746. data->state.up.path[0] != '/' ||
  747. !strncasecompare("LDAP", data->state.up.scheme, 4))
  748. return LDAP_INVALID_SYNTAX;
  749. ludp->lud_scope = LDAP_SCOPE_BASE;
  750. ludp->lud_port = conn->remote_port;
  751. ludp->lud_host = conn->host.name;
  752. /* Duplicate the path */
  753. p = path = strdup(data->state.up.path + 1);
  754. if(!path)
  755. return LDAP_NO_MEMORY;
  756. /* Duplicate the query if present */
  757. if(data->state.up.query) {
  758. q = query = strdup(data->state.up.query);
  759. if(!query) {
  760. free(path);
  761. return LDAP_NO_MEMORY;
  762. }
  763. }
  764. /* Parse the DN (Distinguished Name) */
  765. if(*p) {
  766. char *dn = p;
  767. char *unescaped;
  768. CURLcode result;
  769. LDAP_TRACE(("DN '%s'\n", dn));
  770. /* Unescape the DN */
  771. result = Curl_urldecode(data, dn, 0, &unescaped, NULL, REJECT_ZERO);
  772. if(result) {
  773. rc = LDAP_NO_MEMORY;
  774. goto quit;
  775. }
  776. #if defined(USE_WIN32_LDAP)
  777. /* Convert the unescaped string to a tchar */
  778. ludp->lud_dn = curlx_convert_UTF8_to_tchar(unescaped);
  779. /* Free the unescaped string as we are done with it */
  780. free(unescaped);
  781. if(!ludp->lud_dn) {
  782. rc = LDAP_NO_MEMORY;
  783. goto quit;
  784. }
  785. #else
  786. ludp->lud_dn = unescaped;
  787. #endif
  788. }
  789. p = q;
  790. if(!p)
  791. goto quit;
  792. /* Parse the attributes. skip "??" */
  793. q = strchr(p, '?');
  794. if(q)
  795. *q++ = '\0';
  796. if(*p) {
  797. char **attributes;
  798. size_t count = 0;
  799. /* Split the string into an array of attributes */
  800. if(!split_str(p, &attributes, &count)) {
  801. rc = LDAP_NO_MEMORY;
  802. goto quit;
  803. }
  804. /* Allocate our array (+1 for the NULL entry) */
  805. #if defined(USE_WIN32_LDAP)
  806. ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
  807. #else
  808. ludp->lud_attrs = calloc(count + 1, sizeof(char *));
  809. #endif
  810. if(!ludp->lud_attrs) {
  811. free(attributes);
  812. rc = LDAP_NO_MEMORY;
  813. goto quit;
  814. }
  815. for(i = 0; i < count; i++) {
  816. char *unescaped;
  817. CURLcode result;
  818. LDAP_TRACE(("attr[%zu] '%s'\n", i, attributes[i]));
  819. /* Unescape the attribute */
  820. result = Curl_urldecode(data, attributes[i], 0, &unescaped, NULL,
  821. REJECT_ZERO);
  822. if(result) {
  823. free(attributes);
  824. rc = LDAP_NO_MEMORY;
  825. goto quit;
  826. }
  827. #if defined(USE_WIN32_LDAP)
  828. /* Convert the unescaped string to a tchar */
  829. ludp->lud_attrs[i] = curlx_convert_UTF8_to_tchar(unescaped);
  830. /* Free the unescaped string as we are done with it */
  831. free(unescaped);
  832. if(!ludp->lud_attrs[i]) {
  833. free(attributes);
  834. rc = LDAP_NO_MEMORY;
  835. goto quit;
  836. }
  837. #else
  838. ludp->lud_attrs[i] = unescaped;
  839. #endif
  840. ludp->lud_attrs_dups++;
  841. }
  842. free(attributes);
  843. }
  844. p = q;
  845. if(!p)
  846. goto quit;
  847. /* Parse the scope. skip "??" */
  848. q = strchr(p, '?');
  849. if(q)
  850. *q++ = '\0';
  851. if(*p) {
  852. ludp->lud_scope = str2scope(p);
  853. if(ludp->lud_scope == -1) {
  854. rc = LDAP_INVALID_SYNTAX;
  855. goto quit;
  856. }
  857. LDAP_TRACE(("scope %d\n", ludp->lud_scope));
  858. }
  859. p = q;
  860. if(!p)
  861. goto quit;
  862. /* Parse the filter */
  863. q = strchr(p, '?');
  864. if(q)
  865. *q++ = '\0';
  866. if(*p) {
  867. char *filter = p;
  868. char *unescaped;
  869. CURLcode result;
  870. LDAP_TRACE(("filter '%s'\n", filter));
  871. /* Unescape the filter */
  872. result = Curl_urldecode(data, filter, 0, &unescaped, NULL, REJECT_ZERO);
  873. if(result) {
  874. rc = LDAP_NO_MEMORY;
  875. goto quit;
  876. }
  877. #if defined(USE_WIN32_LDAP)
  878. /* Convert the unescaped string to a tchar */
  879. ludp->lud_filter = curlx_convert_UTF8_to_tchar(unescaped);
  880. /* Free the unescaped string as we are done with it */
  881. free(unescaped);
  882. if(!ludp->lud_filter) {
  883. rc = LDAP_NO_MEMORY;
  884. goto quit;
  885. }
  886. #else
  887. ludp->lud_filter = unescaped;
  888. #endif
  889. }
  890. p = q;
  891. if(p && !*p) {
  892. rc = LDAP_INVALID_SYNTAX;
  893. goto quit;
  894. }
  895. quit:
  896. free(path);
  897. free(query);
  898. return rc;
  899. }
  900. static int _ldap_url_parse(struct Curl_easy *data,
  901. const struct connectdata *conn,
  902. LDAPURLDesc **ludpp)
  903. {
  904. LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
  905. int rc;
  906. *ludpp = NULL;
  907. if(!ludp)
  908. return LDAP_NO_MEMORY;
  909. rc = _ldap_url_parse2(data, conn, ludp);
  910. if(rc != LDAP_SUCCESS) {
  911. _ldap_free_urldesc(ludp);
  912. ludp = NULL;
  913. }
  914. *ludpp = ludp;
  915. return (rc);
  916. }
  917. static void _ldap_free_urldesc(LDAPURLDesc *ludp)
  918. {
  919. if(!ludp)
  920. return;
  921. #if defined(USE_WIN32_LDAP)
  922. curlx_unicodefree(ludp->lud_dn);
  923. curlx_unicodefree(ludp->lud_filter);
  924. #else
  925. free(ludp->lud_dn);
  926. free(ludp->lud_filter);
  927. #endif
  928. if(ludp->lud_attrs) {
  929. size_t i;
  930. for(i = 0; i < ludp->lud_attrs_dups; i++) {
  931. #if defined(USE_WIN32_LDAP)
  932. curlx_unicodefree(ludp->lud_attrs[i]);
  933. #else
  934. free(ludp->lud_attrs[i]);
  935. #endif
  936. }
  937. free(ludp->lud_attrs);
  938. }
  939. free(ludp);
  940. }
  941. #endif /* !HAVE_LDAP_URL_PARSE */
  942. #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */