ldap.c 27 KB

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