2
0

ldap.c 29 KB

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