ldap.c 28 KB

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