ldap.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
  24. /*
  25. * Notice that USE_OPENLDAP is only a source code selection switch. When
  26. * libcurl is built with USE_OPENLDAP defined the libcurl source code that
  27. * gets compiled is the code from openldap.c, otherwise the code that gets
  28. * compiled is the code from ldap.c.
  29. *
  30. * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
  31. * might be required for compilation and runtime. In order to use ancient
  32. * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
  33. */
  34. #ifdef USE_WIN32_LDAP /* Use Windows LDAP implementation. */
  35. # include <winldap.h>
  36. # ifndef LDAP_VENDOR_NAME
  37. # error Your Platform SDK is NOT sufficient for LDAP support! \
  38. Update your Platform SDK, or disable LDAP support!
  39. # else
  40. # include <winber.h>
  41. # endif
  42. #else
  43. # define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
  44. # ifdef HAVE_LBER_H
  45. # include <lber.h>
  46. # endif
  47. # include <ldap.h>
  48. # if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
  49. # include <ldap_ssl.h>
  50. # endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
  51. #endif
  52. #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. #ifdef USE_WIN32_LDAP
  437. failf(data, "LDAP local: bind via ldap_win_bind %s",
  438. ldap_err2string(rc));
  439. #else
  440. failf(data, "LDAP local: bind via ldap_simple_bind_s %s",
  441. ldap_err2string(rc));
  442. #endif
  443. result = CURLE_LDAP_CANNOT_BIND;
  444. goto quit;
  445. }
  446. rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
  447. ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
  448. if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
  449. failf(data, "LDAP remote: %s", ldap_err2string(rc));
  450. result = CURLE_LDAP_SEARCH_FAILED;
  451. goto quit;
  452. }
  453. for(num = 0, entryIterator = ldap_first_entry(server, ldapmsg);
  454. entryIterator;
  455. entryIterator = ldap_next_entry(server, entryIterator), num++) {
  456. BerElement *ber = NULL;
  457. #if defined(USE_WIN32_LDAP)
  458. TCHAR *attribute;
  459. #else
  460. char *attribute; /*! suspicious that this isn't 'const' */
  461. #endif
  462. int i;
  463. /* Get the DN and write it to the client */
  464. {
  465. char *name;
  466. size_t name_len;
  467. #if defined(USE_WIN32_LDAP)
  468. TCHAR *dn = ldap_get_dn(server, entryIterator);
  469. name = Curl_convert_tchar_to_UTF8(dn);
  470. if(!name) {
  471. ldap_memfree(dn);
  472. result = CURLE_OUT_OF_MEMORY;
  473. goto quit;
  474. }
  475. #else
  476. char *dn = name = ldap_get_dn(server, entryIterator);
  477. #endif
  478. name_len = strlen(name);
  479. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
  480. if(result) {
  481. #if defined(USE_WIN32_LDAP)
  482. Curl_unicodefree(name);
  483. #endif
  484. ldap_memfree(dn);
  485. goto quit;
  486. }
  487. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *) name,
  488. name_len);
  489. if(result) {
  490. #if defined(USE_WIN32_LDAP)
  491. Curl_unicodefree(name);
  492. #endif
  493. ldap_memfree(dn);
  494. goto quit;
  495. }
  496. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  497. if(result) {
  498. #if defined(USE_WIN32_LDAP)
  499. Curl_unicodefree(name);
  500. #endif
  501. ldap_memfree(dn);
  502. goto quit;
  503. }
  504. dlsize += name_len + 5;
  505. #if defined(USE_WIN32_LDAP)
  506. Curl_unicodefree(name);
  507. #endif
  508. ldap_memfree(dn);
  509. }
  510. /* Get the attributes and write them to the client */
  511. for(attribute = ldap_first_attribute(server, entryIterator, &ber);
  512. attribute;
  513. attribute = ldap_next_attribute(server, entryIterator, ber)) {
  514. BerValue **vals;
  515. size_t attr_len;
  516. #if defined(USE_WIN32_LDAP)
  517. char *attr = Curl_convert_tchar_to_UTF8(attribute);
  518. if(!attr) {
  519. if(ber)
  520. ber_free(ber, 0);
  521. result = CURLE_OUT_OF_MEMORY;
  522. goto quit;
  523. }
  524. #else
  525. char *attr = attribute;
  526. #endif
  527. attr_len = strlen(attr);
  528. vals = ldap_get_values_len(server, entryIterator, attribute);
  529. if(vals != NULL) {
  530. for(i = 0; (vals[i] != NULL); i++) {
  531. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
  532. if(result) {
  533. ldap_value_free_len(vals);
  534. #if defined(USE_WIN32_LDAP)
  535. Curl_unicodefree(attr);
  536. #endif
  537. ldap_memfree(attribute);
  538. if(ber)
  539. ber_free(ber, 0);
  540. goto quit;
  541. }
  542. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  543. (char *) attr, attr_len);
  544. if(result) {
  545. ldap_value_free_len(vals);
  546. #if defined(USE_WIN32_LDAP)
  547. Curl_unicodefree(attr);
  548. #endif
  549. ldap_memfree(attribute);
  550. if(ber)
  551. ber_free(ber, 0);
  552. goto quit;
  553. }
  554. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
  555. if(result) {
  556. ldap_value_free_len(vals);
  557. #if defined(USE_WIN32_LDAP)
  558. Curl_unicodefree(attr);
  559. #endif
  560. ldap_memfree(attribute);
  561. if(ber)
  562. ber_free(ber, 0);
  563. goto quit;
  564. }
  565. dlsize += attr_len + 3;
  566. if((attr_len > 7) &&
  567. (strcmp(";binary", (char *) attr + (attr_len - 7)) == 0)) {
  568. /* Binary attribute, encode to base64. */
  569. result = Curl_base64_encode(data,
  570. vals[i]->bv_val,
  571. vals[i]->bv_len,
  572. &val_b64,
  573. &val_b64_sz);
  574. if(result) {
  575. ldap_value_free_len(vals);
  576. #if defined(USE_WIN32_LDAP)
  577. Curl_unicodefree(attr);
  578. #endif
  579. ldap_memfree(attribute);
  580. if(ber)
  581. ber_free(ber, 0);
  582. goto quit;
  583. }
  584. if(val_b64_sz > 0) {
  585. result = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
  586. val_b64_sz);
  587. free(val_b64);
  588. if(result) {
  589. ldap_value_free_len(vals);
  590. #if defined(USE_WIN32_LDAP)
  591. Curl_unicodefree(attr);
  592. #endif
  593. ldap_memfree(attribute);
  594. if(ber)
  595. ber_free(ber, 0);
  596. goto quit;
  597. }
  598. dlsize += val_b64_sz;
  599. }
  600. }
  601. else {
  602. result = Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
  603. vals[i]->bv_len);
  604. if(result) {
  605. ldap_value_free_len(vals);
  606. #if defined(USE_WIN32_LDAP)
  607. Curl_unicodefree(attr);
  608. #endif
  609. ldap_memfree(attribute);
  610. if(ber)
  611. ber_free(ber, 0);
  612. goto quit;
  613. }
  614. dlsize += vals[i]->bv_len;
  615. }
  616. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  617. if(result) {
  618. ldap_value_free_len(vals);
  619. #if defined(USE_WIN32_LDAP)
  620. Curl_unicodefree(attr);
  621. #endif
  622. ldap_memfree(attribute);
  623. if(ber)
  624. ber_free(ber, 0);
  625. goto quit;
  626. }
  627. dlsize++;
  628. }
  629. /* Free memory used to store values */
  630. ldap_value_free_len(vals);
  631. }
  632. /* Free the attribute as we are done with it */
  633. #if defined(USE_WIN32_LDAP)
  634. Curl_unicodefree(attr);
  635. #endif
  636. ldap_memfree(attribute);
  637. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  638. if(result)
  639. goto quit;
  640. dlsize++;
  641. Curl_pgrsSetDownloadCounter(data, dlsize);
  642. }
  643. if(ber)
  644. ber_free(ber, 0);
  645. }
  646. quit:
  647. if(ldapmsg) {
  648. ldap_msgfree(ldapmsg);
  649. LDAP_TRACE(("Received %d entries\n", num));
  650. }
  651. if(rc == LDAP_SIZELIMIT_EXCEEDED)
  652. infof(data, "There are more than %d entries\n", num);
  653. if(ludp)
  654. ldap_free_urldesc(ludp);
  655. if(server)
  656. ldap_unbind_s(server);
  657. #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
  658. if(ldap_ssl)
  659. ldapssl_client_deinit();
  660. #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
  661. #if defined(USE_WIN32_LDAP)
  662. Curl_unicodefree(host);
  663. #endif
  664. /* no data to transfer */
  665. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  666. connclose(conn, "LDAP connection always disable re-use");
  667. return result;
  668. }
  669. #ifdef DEBUG_LDAP
  670. static void _ldap_trace(const char *fmt, ...)
  671. {
  672. static int do_trace = -1;
  673. va_list args;
  674. if(do_trace == -1) {
  675. const char *env = getenv("CURL_TRACE");
  676. do_trace = (env && strtol(env, NULL, 10) > 0);
  677. }
  678. if(!do_trace)
  679. return;
  680. va_start(args, fmt);
  681. vfprintf(stderr, fmt, args);
  682. va_end(args);
  683. }
  684. #endif
  685. #ifndef HAVE_LDAP_URL_PARSE
  686. /*
  687. * Return scope-value for a scope-string.
  688. */
  689. static int str2scope(const char *p)
  690. {
  691. if(strcasecompare(p, "one"))
  692. return LDAP_SCOPE_ONELEVEL;
  693. if(strcasecompare(p, "onetree"))
  694. return LDAP_SCOPE_ONELEVEL;
  695. if(strcasecompare(p, "base"))
  696. return LDAP_SCOPE_BASE;
  697. if(strcasecompare(p, "sub"))
  698. return LDAP_SCOPE_SUBTREE;
  699. if(strcasecompare(p, "subtree"))
  700. return LDAP_SCOPE_SUBTREE;
  701. return (-1);
  702. }
  703. /*
  704. * Split 'str' into strings separated by commas.
  705. * Note: out[] points into 'str'.
  706. */
  707. static bool split_str(char *str, char ***out, size_t *count)
  708. {
  709. char **res;
  710. char *lasts;
  711. char *s;
  712. size_t i;
  713. size_t items = 1;
  714. s = strchr(str, ',');
  715. while(s) {
  716. items++;
  717. s = strchr(++s, ',');
  718. }
  719. res = calloc(items, sizeof(char *));
  720. if(!res)
  721. return FALSE;
  722. for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
  723. s = strtok_r(NULL, ",", &lasts), i++)
  724. res[i] = s;
  725. *out = res;
  726. *count = items;
  727. return TRUE;
  728. }
  729. /*
  730. * Break apart the pieces of an LDAP URL.
  731. * Syntax:
  732. * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
  733. *
  734. * <hostname> already known from 'conn->host.name'.
  735. * <port> already known from 'conn->remote_port'.
  736. * extract the rest from 'conn->data->state.path+1'. All fields are optional.
  737. * e.g.
  738. * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
  739. * yields ludp->lud_dn = "".
  740. *
  741. * Defined in RFC4516 section 2.
  742. */
  743. static int _ldap_url_parse2(const struct connectdata *conn, LDAPURLDesc *ludp)
  744. {
  745. int rc = LDAP_SUCCESS;
  746. char *path;
  747. char *p;
  748. char *q;
  749. size_t i;
  750. if(!conn->data ||
  751. !conn->data->state.up.path ||
  752. conn->data->state.up.path[0] != '/' ||
  753. !strcasecompare("LDAP", conn->data->state.up.scheme))
  754. return LDAP_INVALID_SYNTAX;
  755. ludp->lud_scope = LDAP_SCOPE_BASE;
  756. ludp->lud_port = conn->remote_port;
  757. ludp->lud_host = conn->host.name;
  758. /* Duplicate the path */
  759. p = path = strdup(conn->data->state.up.path + 1);
  760. if(!path)
  761. return LDAP_NO_MEMORY;
  762. /* Parse the DN (Distinguished Name) */
  763. q = strchr(p, '?');
  764. if(q)
  765. *q++ = '\0';
  766. if(*p) {
  767. char *dn = p;
  768. char *unescaped;
  769. CURLcode result;
  770. LDAP_TRACE(("DN '%s'\n", dn));
  771. /* Unescape the DN */
  772. result = Curl_urldecode(conn->data, dn, 0, &unescaped, NULL, FALSE);
  773. if(result) {
  774. rc = LDAP_NO_MEMORY;
  775. goto quit;
  776. }
  777. #if defined(USE_WIN32_LDAP)
  778. /* Convert the unescaped string to a tchar */
  779. ludp->lud_dn = Curl_convert_UTF8_to_tchar(unescaped);
  780. /* Free the unescaped string as we are done with it */
  781. Curl_unicodefree(unescaped);
  782. if(!ludp->lud_dn) {
  783. rc = LDAP_NO_MEMORY;
  784. goto quit;
  785. }
  786. #else
  787. ludp->lud_dn = unescaped;
  788. #endif
  789. }
  790. p = q;
  791. if(!p)
  792. goto quit;
  793. /* Parse the attributes. skip "??" */
  794. q = strchr(p, '?');
  795. if(q)
  796. *q++ = '\0';
  797. if(*p) {
  798. char **attributes;
  799. size_t count = 0;
  800. /* Split the string into an array of attributes */
  801. if(!split_str(p, &attributes, &count)) {
  802. rc = LDAP_NO_MEMORY;
  803. goto quit;
  804. }
  805. /* Allocate our array (+1 for the NULL entry) */
  806. #if defined(USE_WIN32_LDAP)
  807. ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
  808. #else
  809. ludp->lud_attrs = calloc(count + 1, sizeof(char *));
  810. #endif
  811. if(!ludp->lud_attrs) {
  812. free(attributes);
  813. rc = LDAP_NO_MEMORY;
  814. goto quit;
  815. }
  816. for(i = 0; i < count; i++) {
  817. char *unescaped;
  818. CURLcode result;
  819. LDAP_TRACE(("attr[%d] '%s'\n", i, attributes[i]));
  820. /* Unescape the attribute */
  821. result = Curl_urldecode(conn->data, attributes[i], 0, &unescaped, NULL,
  822. FALSE);
  823. if(result) {
  824. free(attributes);
  825. rc = LDAP_NO_MEMORY;
  826. goto quit;
  827. }
  828. #if defined(USE_WIN32_LDAP)
  829. /* Convert the unescaped string to a tchar */
  830. ludp->lud_attrs[i] = Curl_convert_UTF8_to_tchar(unescaped);
  831. /* Free the unescaped string as we are done with it */
  832. Curl_unicodefree(unescaped);
  833. if(!ludp->lud_attrs[i]) {
  834. free(attributes);
  835. rc = LDAP_NO_MEMORY;
  836. goto quit;
  837. }
  838. #else
  839. ludp->lud_attrs[i] = unescaped;
  840. #endif
  841. ludp->lud_attrs_dups++;
  842. }
  843. free(attributes);
  844. }
  845. p = q;
  846. if(!p)
  847. goto quit;
  848. /* Parse the scope. skip "??" */
  849. q = strchr(p, '?');
  850. if(q)
  851. *q++ = '\0';
  852. if(*p) {
  853. ludp->lud_scope = str2scope(p);
  854. if(ludp->lud_scope == -1) {
  855. rc = LDAP_INVALID_SYNTAX;
  856. goto quit;
  857. }
  858. LDAP_TRACE(("scope %d\n", ludp->lud_scope));
  859. }
  860. p = q;
  861. if(!p)
  862. goto quit;
  863. /* Parse the filter */
  864. q = strchr(p, '?');
  865. if(q)
  866. *q++ = '\0';
  867. if(*p) {
  868. char *filter = p;
  869. char *unescaped;
  870. CURLcode result;
  871. LDAP_TRACE(("filter '%s'\n", filter));
  872. /* Unescape the filter */
  873. result = Curl_urldecode(conn->data, filter, 0, &unescaped, NULL, FALSE);
  874. if(result) {
  875. rc = LDAP_NO_MEMORY;
  876. goto quit;
  877. }
  878. #if defined(USE_WIN32_LDAP)
  879. /* Convert the unescaped string to a tchar */
  880. ludp->lud_filter = Curl_convert_UTF8_to_tchar(unescaped);
  881. /* Free the unescaped string as we are done with it */
  882. Curl_unicodefree(unescaped);
  883. if(!ludp->lud_filter) {
  884. rc = LDAP_NO_MEMORY;
  885. goto quit;
  886. }
  887. #else
  888. ludp->lud_filter = unescaped;
  889. #endif
  890. }
  891. p = q;
  892. if(p && !*p) {
  893. rc = LDAP_INVALID_SYNTAX;
  894. goto quit;
  895. }
  896. quit:
  897. free(path);
  898. return rc;
  899. }
  900. static int _ldap_url_parse(const struct connectdata *conn,
  901. LDAPURLDesc **ludpp)
  902. {
  903. LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
  904. int rc;
  905. *ludpp = NULL;
  906. if(!ludp)
  907. return LDAP_NO_MEMORY;
  908. rc = _ldap_url_parse2(conn, ludp);
  909. if(rc != LDAP_SUCCESS) {
  910. _ldap_free_urldesc(ludp);
  911. ludp = NULL;
  912. }
  913. *ludpp = ludp;
  914. return (rc);
  915. }
  916. static void _ldap_free_urldesc(LDAPURLDesc *ludp)
  917. {
  918. size_t i;
  919. if(!ludp)
  920. return;
  921. free(ludp->lud_dn);
  922. free(ludp->lud_filter);
  923. if(ludp->lud_attrs) {
  924. for(i = 0; i < ludp->lud_attrs_dups; i++)
  925. free(ludp->lud_attrs[i]);
  926. free(ludp->lud_attrs);
  927. }
  928. free(ludp);
  929. }
  930. #endif /* !HAVE_LDAP_URL_PARSE */
  931. #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */