ldap.c 28 KB

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