ldap.c 28 KB

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