ldap.c 29 KB

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