ldap.c 28 KB

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