ldap.c 28 KB

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