ldap.c 28 KB

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