ldap.c 28 KB

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