ldap.c 29 KB

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