2
0

ldap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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, ...);
  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, /* readwrite */
  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, /* readwrite */
  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_CRYPTO_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. curl_off_t dlsize = 0;
  282. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  283. struct timeval ldap_timeout = {10, 0}; /* 10 sec connection/search timeout */
  284. #endif
  285. #if defined(USE_WIN32_LDAP)
  286. TCHAR *host = NULL;
  287. #else
  288. char *host = NULL;
  289. #endif
  290. char *user = NULL;
  291. char *passwd = NULL;
  292. *done = TRUE; /* unconditionally */
  293. infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d",
  294. LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
  295. infof(data, "LDAP local: %s", data->state.url);
  296. #ifdef HAVE_LDAP_URL_PARSE
  297. rc = ldap_url_parse(data->state.url, &ludp);
  298. #else
  299. rc = _ldap_url_parse(data, conn, &ludp);
  300. #endif
  301. if(rc) {
  302. failf(data, "Bad LDAP URL: %s", ldap_err2string(rc));
  303. result = CURLE_URL_MALFORMAT;
  304. goto quit;
  305. }
  306. /* Get the URL scheme (either ldap or ldaps) */
  307. if(conn->given->flags & PROTOPT_SSL)
  308. ldap_ssl = 1;
  309. infof(data, "LDAP local: trying to establish %s connection",
  310. ldap_ssl ? "encrypted" : "cleartext");
  311. #if defined(USE_WIN32_LDAP)
  312. host = curlx_convert_UTF8_to_tchar(conn->host.name);
  313. if(!host) {
  314. result = CURLE_OUT_OF_MEMORY;
  315. goto quit;
  316. }
  317. #else
  318. host = conn->host.name;
  319. #endif
  320. if(data->state.aptr.user) {
  321. user = conn->user;
  322. passwd = conn->passwd;
  323. }
  324. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  325. ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
  326. #endif
  327. ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  328. if(ldap_ssl) {
  329. #ifdef HAVE_LDAP_SSL
  330. #ifdef USE_WIN32_LDAP
  331. /* Win32 LDAP SDK doesn't support insecure mode without CA! */
  332. server = ldap_sslinit(host, conn->port, 1);
  333. ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
  334. #else
  335. int ldap_option;
  336. char *ldap_ca = conn->ssl_config.CAfile;
  337. #if defined(CURL_HAS_NOVELL_LDAPSDK)
  338. rc = ldapssl_client_init(NULL, NULL);
  339. if(rc != LDAP_SUCCESS) {
  340. failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
  341. result = CURLE_SSL_CERTPROBLEM;
  342. goto quit;
  343. }
  344. if(conn->ssl_config.verifypeer) {
  345. /* Novell SDK supports DER or BASE64 files. */
  346. int cert_type = LDAPSSL_CERT_FILETYPE_B64;
  347. if((data->set.ssl.cert_type) &&
  348. (strcasecompare(data->set.ssl.cert_type, "DER")))
  349. cert_type = LDAPSSL_CERT_FILETYPE_DER;
  350. if(!ldap_ca) {
  351. failf(data, "LDAP local: ERROR %s CA cert not set",
  352. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
  353. result = CURLE_SSL_CERTPROBLEM;
  354. goto quit;
  355. }
  356. infof(data, "LDAP local: using %s CA cert '%s'",
  357. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
  358. ldap_ca);
  359. rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
  360. if(rc != LDAP_SUCCESS) {
  361. failf(data, "LDAP local: ERROR setting %s CA cert: %s",
  362. (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
  363. ldap_err2string(rc));
  364. result = CURLE_SSL_CERTPROBLEM;
  365. goto quit;
  366. }
  367. ldap_option = LDAPSSL_VERIFY_SERVER;
  368. }
  369. else
  370. ldap_option = LDAPSSL_VERIFY_NONE;
  371. rc = ldapssl_set_verify_mode(ldap_option);
  372. if(rc != LDAP_SUCCESS) {
  373. failf(data, "LDAP local: ERROR setting cert verify mode: %s",
  374. ldap_err2string(rc));
  375. result = CURLE_SSL_CERTPROBLEM;
  376. goto quit;
  377. }
  378. server = ldapssl_init(host, conn->port, 1);
  379. if(!server) {
  380. failf(data, "LDAP local: Cannot connect to %s:%u",
  381. conn->host.dispname, conn->port);
  382. result = CURLE_COULDNT_CONNECT;
  383. goto quit;
  384. }
  385. #elif defined(LDAP_OPT_X_TLS)
  386. if(conn->ssl_config.verifypeer) {
  387. /* OpenLDAP SDK supports BASE64 files. */
  388. if((data->set.ssl.cert_type) &&
  389. (!strcasecompare(data->set.ssl.cert_type, "PEM"))) {
  390. failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type");
  391. result = CURLE_SSL_CERTPROBLEM;
  392. goto quit;
  393. }
  394. if(!ldap_ca) {
  395. failf(data, "LDAP local: ERROR PEM CA cert not set");
  396. result = CURLE_SSL_CERTPROBLEM;
  397. goto quit;
  398. }
  399. infof(data, "LDAP local: using PEM CA cert: %s", ldap_ca);
  400. rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
  401. if(rc != LDAP_SUCCESS) {
  402. failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
  403. ldap_err2string(rc));
  404. result = CURLE_SSL_CERTPROBLEM;
  405. goto quit;
  406. }
  407. ldap_option = LDAP_OPT_X_TLS_DEMAND;
  408. }
  409. else
  410. ldap_option = LDAP_OPT_X_TLS_NEVER;
  411. rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
  412. if(rc != LDAP_SUCCESS) {
  413. failf(data, "LDAP local: ERROR setting cert verify mode: %s",
  414. ldap_err2string(rc));
  415. result = CURLE_SSL_CERTPROBLEM;
  416. goto quit;
  417. }
  418. server = ldap_init(host, conn->port);
  419. if(!server) {
  420. failf(data, "LDAP local: Cannot connect to %s:%u",
  421. conn->host.dispname, conn->port);
  422. result = CURLE_COULDNT_CONNECT;
  423. goto quit;
  424. }
  425. ldap_option = LDAP_OPT_X_TLS_HARD;
  426. rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
  427. if(rc != LDAP_SUCCESS) {
  428. failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
  429. ldap_err2string(rc));
  430. result = CURLE_SSL_CERTPROBLEM;
  431. goto quit;
  432. }
  433. /*
  434. rc = ldap_start_tls_s(server, NULL, NULL);
  435. if(rc != LDAP_SUCCESS) {
  436. failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
  437. ldap_err2string(rc));
  438. result = CURLE_SSL_CERTPROBLEM;
  439. goto quit;
  440. }
  441. */
  442. #else
  443. /* we should probably never come up to here since configure
  444. should check in first place if we can support LDAP SSL/TLS */
  445. failf(data, "LDAP local: SSL/TLS not supported with this version "
  446. "of the OpenLDAP toolkit\n");
  447. result = CURLE_SSL_CERTPROBLEM;
  448. goto quit;
  449. #endif
  450. #endif
  451. #endif /* CURL_LDAP_USE_SSL */
  452. }
  453. else if(data->set.use_ssl > CURLUSESSL_TRY) {
  454. failf(data, "LDAP local: explicit TLS not supported");
  455. result = CURLE_NOT_BUILT_IN;
  456. goto quit;
  457. }
  458. else {
  459. server = ldap_init(host, conn->port);
  460. if(!server) {
  461. failf(data, "LDAP local: Cannot connect to %s:%u",
  462. conn->host.dispname, conn->port);
  463. result = CURLE_COULDNT_CONNECT;
  464. goto quit;
  465. }
  466. }
  467. #ifdef USE_WIN32_LDAP
  468. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  469. rc = ldap_win_bind(data, server, user, passwd);
  470. #else
  471. rc = ldap_simple_bind_s(server, user, passwd);
  472. #endif
  473. if(!ldap_ssl && rc) {
  474. ldap_proto = LDAP_VERSION2;
  475. ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
  476. #ifdef USE_WIN32_LDAP
  477. rc = ldap_win_bind(data, server, user, passwd);
  478. #else
  479. rc = ldap_simple_bind_s(server, user, passwd);
  480. #endif
  481. }
  482. if(rc) {
  483. #ifdef USE_WIN32_LDAP
  484. failf(data, "LDAP local: bind via ldap_win_bind %s",
  485. ldap_err2string(rc));
  486. #else
  487. failf(data, "LDAP local: bind via ldap_simple_bind_s %s",
  488. ldap_err2string(rc));
  489. #endif
  490. result = CURLE_LDAP_CANNOT_BIND;
  491. goto quit;
  492. }
  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. dlsize += name_len + 5;
  545. FREE_ON_WINLDAP(name);
  546. ldap_memfree(dn);
  547. }
  548. /* Get the attributes and write them to the client */
  549. for(attribute = ldap_first_attribute(server, entryIterator, &ber);
  550. attribute;
  551. attribute = ldap_next_attribute(server, entryIterator, ber)) {
  552. BerValue **vals;
  553. size_t attr_len;
  554. #if defined(USE_WIN32_LDAP)
  555. char *attr = curlx_convert_tchar_to_UTF8(attribute);
  556. if(!attr) {
  557. if(ber)
  558. ber_free(ber, 0);
  559. result = CURLE_OUT_OF_MEMORY;
  560. goto quit;
  561. }
  562. #else
  563. char *attr = attribute;
  564. #endif
  565. attr_len = strlen(attr);
  566. vals = ldap_get_values_len(server, entryIterator, attribute);
  567. if(vals) {
  568. for(i = 0; (vals[i] != NULL); i++) {
  569. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\t", 1);
  570. if(result) {
  571. ldap_value_free_len(vals);
  572. FREE_ON_WINLDAP(attr);
  573. ldap_memfree(attribute);
  574. if(ber)
  575. ber_free(ber, 0);
  576. goto quit;
  577. }
  578. result = Curl_client_write(data, CLIENTWRITE_BODY, attr, attr_len);
  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. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
  588. if(result) {
  589. ldap_value_free_len(vals);
  590. FREE_ON_WINLDAP(attr);
  591. ldap_memfree(attribute);
  592. if(ber)
  593. ber_free(ber, 0);
  594. goto quit;
  595. }
  596. dlsize += attr_len + 3;
  597. if((attr_len > 7) &&
  598. (strcmp(";binary", attr + (attr_len - 7)) == 0)) {
  599. /* Binary attribute, encode to base64. */
  600. result = Curl_base64_encode(vals[i]->bv_val, vals[i]->bv_len,
  601. &val_b64, &val_b64_sz);
  602. if(result) {
  603. ldap_value_free_len(vals);
  604. FREE_ON_WINLDAP(attr);
  605. ldap_memfree(attribute);
  606. if(ber)
  607. ber_free(ber, 0);
  608. goto quit;
  609. }
  610. if(val_b64_sz > 0) {
  611. result = Curl_client_write(data, CLIENTWRITE_BODY, val_b64,
  612. val_b64_sz);
  613. free(val_b64);
  614. if(result) {
  615. ldap_value_free_len(vals);
  616. FREE_ON_WINLDAP(attr);
  617. ldap_memfree(attribute);
  618. if(ber)
  619. ber_free(ber, 0);
  620. goto quit;
  621. }
  622. dlsize += val_b64_sz;
  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. dlsize += vals[i]->bv_len;
  637. }
  638. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
  639. if(result) {
  640. ldap_value_free_len(vals);
  641. FREE_ON_WINLDAP(attr);
  642. ldap_memfree(attribute);
  643. if(ber)
  644. ber_free(ber, 0);
  645. goto quit;
  646. }
  647. dlsize++;
  648. }
  649. /* Free memory used to store values */
  650. ldap_value_free_len(vals);
  651. }
  652. /* Free the attribute as we are done with it */
  653. FREE_ON_WINLDAP(attr);
  654. ldap_memfree(attribute);
  655. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
  656. if(result)
  657. goto quit;
  658. dlsize++;
  659. Curl_pgrsSetDownloadCounter(data, dlsize);
  660. }
  661. if(ber)
  662. ber_free(ber, 0);
  663. }
  664. quit:
  665. if(ldapmsg) {
  666. ldap_msgfree(ldapmsg);
  667. LDAP_TRACE(("Received %d entries\n", num));
  668. }
  669. if(rc == LDAP_SIZELIMIT_EXCEEDED)
  670. infof(data, "There are more than %d entries", num);
  671. if(ludp)
  672. ldap_free_urldesc(ludp);
  673. if(server)
  674. ldap_unbind_s(server);
  675. #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
  676. if(ldap_ssl)
  677. ldapssl_client_deinit();
  678. #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
  679. FREE_ON_WINLDAP(host);
  680. /* no data to transfer */
  681. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  682. connclose(conn, "LDAP connection always disable re-use");
  683. return result;
  684. }
  685. #ifdef DEBUG_LDAP
  686. static void _ldap_trace(const char *fmt, ...)
  687. {
  688. static int do_trace = -1;
  689. va_list args;
  690. if(do_trace == -1) {
  691. const char *env = getenv("CURL_TRACE");
  692. do_trace = (env && strtol(env, NULL, 10) > 0);
  693. }
  694. if(!do_trace)
  695. return;
  696. va_start(args, fmt);
  697. vfprintf(stderr, fmt, args);
  698. va_end(args);
  699. }
  700. #endif
  701. #ifndef HAVE_LDAP_URL_PARSE
  702. /*
  703. * Return scope-value for a scope-string.
  704. */
  705. static int str2scope(const char *p)
  706. {
  707. if(strcasecompare(p, "one"))
  708. return LDAP_SCOPE_ONELEVEL;
  709. if(strcasecompare(p, "onetree"))
  710. return LDAP_SCOPE_ONELEVEL;
  711. if(strcasecompare(p, "base"))
  712. return LDAP_SCOPE_BASE;
  713. if(strcasecompare(p, "sub"))
  714. return LDAP_SCOPE_SUBTREE;
  715. if(strcasecompare(p, "subtree"))
  716. return LDAP_SCOPE_SUBTREE;
  717. return (-1);
  718. }
  719. /*
  720. * Split 'str' into strings separated by commas.
  721. * Note: out[] points into 'str'.
  722. */
  723. static bool split_str(char *str, char ***out, size_t *count)
  724. {
  725. char **res;
  726. char *lasts;
  727. char *s;
  728. size_t i;
  729. size_t items = 1;
  730. s = strchr(str, ',');
  731. while(s) {
  732. items++;
  733. s = strchr(++s, ',');
  734. }
  735. res = calloc(items, sizeof(char *));
  736. if(!res)
  737. return FALSE;
  738. for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
  739. s = strtok_r(NULL, ",", &lasts), i++)
  740. res[i] = s;
  741. *out = res;
  742. *count = items;
  743. return TRUE;
  744. }
  745. /*
  746. * Break apart the pieces of an LDAP URL.
  747. * Syntax:
  748. * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
  749. *
  750. * <hostname> already known from 'conn->host.name'.
  751. * <port> already known from 'conn->remote_port'.
  752. * extract the rest from 'data->state.path+1'. All fields are optional.
  753. * e.g.
  754. * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
  755. * yields ludp->lud_dn = "".
  756. *
  757. * Defined in RFC4516 section 2.
  758. */
  759. static int _ldap_url_parse2(struct Curl_easy *data,
  760. const struct connectdata *conn, LDAPURLDesc *ludp)
  761. {
  762. int rc = LDAP_SUCCESS;
  763. char *p;
  764. char *path;
  765. char *q = NULL;
  766. char *query = NULL;
  767. size_t i;
  768. if(!data ||
  769. !data->state.up.path ||
  770. data->state.up.path[0] != '/' ||
  771. !strncasecompare("LDAP", data->state.up.scheme, 4))
  772. return LDAP_INVALID_SYNTAX;
  773. ludp->lud_scope = LDAP_SCOPE_BASE;
  774. ludp->lud_port = conn->remote_port;
  775. ludp->lud_host = conn->host.name;
  776. /* Duplicate the path */
  777. p = path = strdup(data->state.up.path + 1);
  778. if(!path)
  779. return LDAP_NO_MEMORY;
  780. /* Duplicate the query if present */
  781. if(data->state.up.query) {
  782. q = query = strdup(data->state.up.query);
  783. if(!query) {
  784. free(path);
  785. return LDAP_NO_MEMORY;
  786. }
  787. }
  788. /* Parse the DN (Distinguished Name) */
  789. if(*p) {
  790. char *dn = p;
  791. char *unescaped;
  792. CURLcode result;
  793. LDAP_TRACE(("DN '%s'\n", dn));
  794. /* Unescape the DN */
  795. result = Curl_urldecode(dn, 0, &unescaped, NULL, REJECT_ZERO);
  796. if(result) {
  797. rc = LDAP_NO_MEMORY;
  798. goto quit;
  799. }
  800. #if defined(USE_WIN32_LDAP)
  801. /* Convert the unescaped string to a tchar */
  802. ludp->lud_dn = curlx_convert_UTF8_to_tchar(unescaped);
  803. /* Free the unescaped string as we are done with it */
  804. free(unescaped);
  805. if(!ludp->lud_dn) {
  806. rc = LDAP_NO_MEMORY;
  807. goto quit;
  808. }
  809. #else
  810. ludp->lud_dn = unescaped;
  811. #endif
  812. }
  813. p = q;
  814. if(!p)
  815. goto quit;
  816. /* Parse the attributes. skip "??" */
  817. q = strchr(p, '?');
  818. if(q)
  819. *q++ = '\0';
  820. if(*p) {
  821. char **attributes;
  822. size_t count = 0;
  823. /* Split the string into an array of attributes */
  824. if(!split_str(p, &attributes, &count)) {
  825. rc = LDAP_NO_MEMORY;
  826. goto quit;
  827. }
  828. /* Allocate our array (+1 for the NULL entry) */
  829. #if defined(USE_WIN32_LDAP)
  830. ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
  831. #else
  832. ludp->lud_attrs = calloc(count + 1, sizeof(char *));
  833. #endif
  834. if(!ludp->lud_attrs) {
  835. free(attributes);
  836. rc = LDAP_NO_MEMORY;
  837. goto quit;
  838. }
  839. for(i = 0; i < count; i++) {
  840. char *unescaped;
  841. CURLcode result;
  842. LDAP_TRACE(("attr[%zu] '%s'\n", i, attributes[i]));
  843. /* Unescape the attribute */
  844. result = Curl_urldecode(attributes[i], 0, &unescaped, NULL,
  845. REJECT_ZERO);
  846. if(result) {
  847. free(attributes);
  848. rc = LDAP_NO_MEMORY;
  849. goto quit;
  850. }
  851. #if defined(USE_WIN32_LDAP)
  852. /* Convert the unescaped string to a tchar */
  853. ludp->lud_attrs[i] = curlx_convert_UTF8_to_tchar(unescaped);
  854. /* Free the unescaped string as we are done with it */
  855. free(unescaped);
  856. if(!ludp->lud_attrs[i]) {
  857. free(attributes);
  858. rc = LDAP_NO_MEMORY;
  859. goto quit;
  860. }
  861. #else
  862. ludp->lud_attrs[i] = unescaped;
  863. #endif
  864. ludp->lud_attrs_dups++;
  865. }
  866. free(attributes);
  867. }
  868. p = q;
  869. if(!p)
  870. goto quit;
  871. /* Parse the scope. skip "??" */
  872. q = strchr(p, '?');
  873. if(q)
  874. *q++ = '\0';
  875. if(*p) {
  876. ludp->lud_scope = str2scope(p);
  877. if(ludp->lud_scope == -1) {
  878. rc = LDAP_INVALID_SYNTAX;
  879. goto quit;
  880. }
  881. LDAP_TRACE(("scope %d\n", ludp->lud_scope));
  882. }
  883. p = q;
  884. if(!p)
  885. goto quit;
  886. /* Parse the filter */
  887. q = strchr(p, '?');
  888. if(q)
  889. *q++ = '\0';
  890. if(*p) {
  891. char *filter = p;
  892. char *unescaped;
  893. CURLcode result;
  894. LDAP_TRACE(("filter '%s'\n", filter));
  895. /* Unescape the filter */
  896. result = Curl_urldecode(filter, 0, &unescaped, NULL, REJECT_ZERO);
  897. if(result) {
  898. rc = LDAP_NO_MEMORY;
  899. goto quit;
  900. }
  901. #if defined(USE_WIN32_LDAP)
  902. /* Convert the unescaped string to a tchar */
  903. ludp->lud_filter = curlx_convert_UTF8_to_tchar(unescaped);
  904. /* Free the unescaped string as we are done with it */
  905. free(unescaped);
  906. if(!ludp->lud_filter) {
  907. rc = LDAP_NO_MEMORY;
  908. goto quit;
  909. }
  910. #else
  911. ludp->lud_filter = unescaped;
  912. #endif
  913. }
  914. p = q;
  915. if(p && !*p) {
  916. rc = LDAP_INVALID_SYNTAX;
  917. goto quit;
  918. }
  919. quit:
  920. free(path);
  921. free(query);
  922. return rc;
  923. }
  924. static int _ldap_url_parse(struct Curl_easy *data,
  925. const struct connectdata *conn,
  926. LDAPURLDesc **ludpp)
  927. {
  928. LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
  929. int rc;
  930. *ludpp = NULL;
  931. if(!ludp)
  932. return LDAP_NO_MEMORY;
  933. rc = _ldap_url_parse2(data, conn, ludp);
  934. if(rc != LDAP_SUCCESS) {
  935. _ldap_free_urldesc(ludp);
  936. ludp = NULL;
  937. }
  938. *ludpp = ludp;
  939. return (rc);
  940. }
  941. static void _ldap_free_urldesc(LDAPURLDesc *ludp)
  942. {
  943. if(!ludp)
  944. return;
  945. #if defined(USE_WIN32_LDAP)
  946. curlx_unicodefree(ludp->lud_dn);
  947. curlx_unicodefree(ludp->lud_filter);
  948. #else
  949. free(ludp->lud_dn);
  950. free(ludp->lud_filter);
  951. #endif
  952. if(ludp->lud_attrs) {
  953. size_t i;
  954. for(i = 0; i < ludp->lud_attrs_dups; i++) {
  955. #if defined(USE_WIN32_LDAP)
  956. curlx_unicodefree(ludp->lud_attrs[i]);
  957. #else
  958. free(ludp->lud_attrs[i]);
  959. #endif
  960. }
  961. free(ludp->lud_attrs);
  962. }
  963. free(ludp);
  964. }
  965. #endif /* !HAVE_LDAP_URL_PARSE */
  966. #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */