ldap.c 28 KB

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