master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
25#include "urldata.h"
26#include "curl_ldap.h"
28#if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
30#if defined(CURL_HAVE_DIAG) && defined(__APPLE__)
31#pragma GCC diagnostic push
32#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
33#endif
35/*
36 * Notice that USE_OPENLDAP is only a source code selection switch. When
37 * libcurl is built with USE_OPENLDAP defined the libcurl source code that
38 * gets compiled is the code from openldap.c, otherwise the code that gets
39 * compiled is the code from ldap.c.
40 *
41 * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
42 * might be required for compilation and runtime. In order to use ancient
43 * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
44 */
46#ifdef USE_WIN32_LDAP /* Use Windows LDAP implementation. */
47# include <winldap.h>
48/* Undefine indirect <wincrypt.h> symbols conflicting with AWS-LC/BoringSSL. */
49# undef X509_NAME
50# undef X509_EXTENSIONS
51# undef PKCS7_ISSUER_AND_SERIAL
52# undef PKCS7_SIGNER_INFO
53# undef OCSP_REQUEST
54# undef OCSP_RESPONSE
55# include <winber.h>
56#else
57# define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
58# ifdef HAVE_LBER_H
59# include <lber.h>
60# endif
61# include <ldap.h>
62# if defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H)
63# include <ldap_ssl.h>
64# endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
65#endif
67#include "cfilters.h"
68#include "sendf.h"
69#include "curl_trc.h"
70#include "escape.h"
71#include "progress.h"
72#include "transfer.h"
73#include "curlx/strparse.h"
74#include "bufref.h"
75#include "curlx/multibyte.h"
76#include "curlx/base64.h"
77#include "connect.h"
79#ifdef USE_WIN32_LDAP
80#define FREE_ON_WINLDAP(x) curlx_free(x)
81#define curl_ldap_num_t ULONG
82#else
83#define FREE_ON_WINLDAP(x) do {} while(0)
84#define curl_ldap_num_t int
85#endif
87#ifndef HAVE_LDAP_URL_PARSE
89/* Use our own implementation. */
91struct ldap_urldesc {
92 char *lud_host;
93 int lud_port;
94#ifdef USE_WIN32_LDAP
95 TCHAR *lud_dn;
96 TCHAR **lud_attrs;
97#else
98 char *lud_dn;
99 char **lud_attrs;
100#endif
101 int lud_scope;
102#ifdef USE_WIN32_LDAP
103 TCHAR *lud_filter;
104#else
105 char *lud_filter;
106#endif
107 char **lud_exts;
108 size_t lud_attrs_dups; /* how many were dup'ed, this field is not in the
109 "real" struct so can only be used in code
110 without HAVE_LDAP_URL_PARSE defined */
111};
113#undef LDAPURLDesc
114#define LDAPURLDesc struct ldap_urldesc
116static curl_ldap_num_t ldap_url_parse_low(struct Curl_easy *data,
117 const struct connectdata *conn,
118 LDAPURLDesc **ludpp);
119static void ldap_free_urldesc_low(LDAPURLDesc *ludp);
121#undef ldap_free_urldesc
122#define ldap_free_urldesc ldap_free_urldesc_low
124#endif /* !HAVE_LDAP_URL_PARSE */
126#ifdef DEBUG_LDAP
127#define LDAP_TRACE(x) \
128 do { \
129 ldap_trace_low("%u: ", __LINE__); \
130 ldap_trace_low x; \
131 } while(0)
133static void ldap_trace_low(const char *fmt, ...) CURL_PRINTF(1, 2);
134#else
135#define LDAP_TRACE(x) Curl_nop_stmt
136#endif
138#if defined(USE_WIN32_LDAP) && defined(ldap_err2string)
139/* Use ANSI error strings in Unicode builds */
140#undef ldap_err2string
141#define ldap_err2string ldap_err2stringA
142#endif
144#if defined(USE_WIN32_LDAP) && defined(_MSC_VER) && (_MSC_VER <= 1700)
145/* Workaround for warning:
146 'type cast' : conversion from 'int' to 'void *' of greater size */
147#undef LDAP_OPT_ON
148#undef LDAP_OPT_OFF
149#define LDAP_OPT_ON ((void *)(size_t)1)
150#define LDAP_OPT_OFF ((void *)(size_t)0)
151#endif
153#ifdef USE_WIN32_LDAP
155#ifdef USE_WINDOWS_SSPI
156static ULONG ldap_win_bind_auth(LDAP *server, const char *user,
157 const char *passwd, unsigned long authflags)
158{
159 ULONG method = 0;
160 SEC_WINNT_AUTH_IDENTITY_EX cred;
161 ULONG rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
163 memset(&cred, 0, sizeof(cred));
165#ifdef USE_SPNEGO
166 if(authflags & CURLAUTH_NEGOTIATE) {
167 method = LDAP_AUTH_NEGOTIATE;
168 }
169 else
170#endif
171#ifndef CURL_DISABLE_DIGEST_AUTH
172 if(authflags & CURLAUTH_DIGEST) {
173 method = LDAP_AUTH_DIGEST;
174 }
175 else
176#endif
177 {
178 /* required anyway if one of upper preprocessor definitions enabled */
179 }
181 if(method && user && passwd) {
182 CURLcode result = Curl_create_sspi_identity(user, passwd, &cred);
183 if(!result) {
184 rc = ldap_bind_s(server, NULL, (TCHAR *)&cred, method);
185 Curl_sspi_free_identity(&cred);
186 }
187 else {
188 rc = LDAP_NO_MEMORY;
189 }
190 }
191 else {
192 /* proceed with current user credentials */
193 method = LDAP_AUTH_NEGOTIATE;
194 rc = ldap_bind_s(server, NULL, NULL, method);
195 }
196 return rc;
197}
198#endif /* USE_WINDOWS_SSPI */
200static ULONG ldap_win_bind(struct Curl_easy *data, LDAP *server,
201 const char *user, const char *passwd)
202{
203 ULONG rc = LDAP_INVALID_CREDENTIALS;
205 PTCHAR inuser = NULL;
206 PTCHAR inpass = NULL;
208 if(user && passwd && (data->set.httpauth & CURLAUTH_BASIC)) {
209 inuser = curlx_convert_UTF8_to_tchar(user);
210 inpass = curlx_convert_UTF8_to_tchar(passwd);
212 rc = ldap_simple_bind_s(server, inuser, inpass);
214 curlx_free(inuser);
215 curlx_free(inpass);
216 }
217#ifdef USE_WINDOWS_SSPI
218 else {
219 rc = ldap_win_bind_auth(server, user, passwd, data->set.httpauth);
220 }
221#endif
223 return rc;
224}
225#endif /* USE_WIN32_LDAP */
227/* RFC 2849: a string that is not a SAFE-STRING (leading ':' or '<', leading or
228 trailing space, or any non-printable byte such as CR/LF) cannot go into LDIF
229 as-is. DNs and values get base64-encoded instead; an attribute type has no
230 base64 form in LDIF and is rejected by the caller. */
231static bool ldap_str_needs_base64(const char *s, size_t len)
232{
233 size_t i;
234 if(!len)
235 return FALSE;
236 if((s[0] == ':') || (s[0] == '<') || ISBLANK(s[0]) || ISBLANK(s[len - 1]))
237 return TRUE;
238 for(i = 0; i < len; i++)
239 if(!ISPRINT(s[i]))
240 return TRUE;
241 return FALSE;
242}
244static bool ldap_value_needs_base64(const char *attr, size_t attr_len,
245 const BerValue *val)
246{
247 if((attr_len > 7) && curl_strequal(";binary", attr + attr_len - 7))
248 return TRUE;
250 return ldap_str_needs_base64(val->bv_val, val->bv_len);
251}
253#ifdef USE_WIN32_LDAP
254static BOOLEAN bypass_cert_verify(PLDAP Connection,
255 PCCERT_CONTEXT *ppServerCert)
256{
257 (void)Connection;
258 CertFreeCertificateContext(*ppServerCert);
259 /* approve any certificate since the verification is set to bypass */
260 return TRUE;
261}
262#endif
264static CURLcode show_vals(struct Curl_easy *data, BerValue **vals,
265 const char *attr)
266{
267 CURLcode result = CURLE_OK;
268 int i;
269 size_t attr_len = strlen(attr);
271 /* An attribute type has no base64 form in LDIF, so a type carrying control
272 bytes (CR/LF etc.) would let a hostile server inject lines. A conformant
273 type name never contains them, so reject it. */
274 if(ldap_str_needs_base64(attr, attr_len)) {
275 failf(data, "LDAP local: attribute type contains illegal bytes");
276 return CURLE_WEIRD_SERVER_REPLY;
277 }
279 for(i = 0; vals[i] && !result; i++) {
280 result = Curl_client_write(data, CLIENTWRITE_BODY, "\t", 1);
281 if(!result)
282 result = Curl_client_write(data, CLIENTWRITE_BODY, attr, attr_len);
283 if(!result)
284 result = Curl_client_write(data, CLIENTWRITE_BODY, ":", 1);
286 if(result)
287 break;
289 if(ldap_value_needs_base64(attr, attr_len, vals[i])) {
290 char *val_b64 = NULL;
291 size_t val_b64_sz = 0;
293 /* Binary attribute, encode to base64. */
294 if(vals[i]->bv_len)
295 result = curlx_base64_encode((uint8_t *)vals[i]->bv_val,
296 vals[i]->bv_len,
297 &val_b64, &val_b64_sz);
298 if(!result)
299 result = Curl_client_write(data, CLIENTWRITE_BODY, ": ", 2);
300 if(!result && val_b64_sz)
301 result = Curl_client_write(data, CLIENTWRITE_BODY, val_b64,
302 val_b64_sz);
303 curlx_free(val_b64);
304 }
305 else {
306 result = Curl_client_write(data, CLIENTWRITE_BODY, " ", 1);
307 if(!result)
308 result = Curl_client_write(data, CLIENTWRITE_BODY,
309 vals[i]->bv_val, vals[i]->bv_len);
310 }
312 if(!result)
313 result = Curl_client_write(data, CLIENTWRITE_BODY, "\n", 1);
314 }
315 return result;
316}
318static CURLcode ldap_do(struct Curl_easy *data, bool *done)
319{
320 CURLcode result = CURLE_OK;
321 curl_ldap_num_t rc = LDAP_SUCCESS;
322 LDAP *server = NULL;
323 LDAPURLDesc *ludp = NULL;
324 LDAPMessage *ldapmsg = NULL;
325 LDAPMessage *entryIterator;
326 int num = 0;
327 struct connectdata *conn = data->conn;
328 int ldap_proto = LDAP_VERSION3;
329 bool ldap_ssl = FALSE;
330#ifdef LDAP_OPT_NETWORK_TIMEOUT
331 struct timeval ldap_timeout = { 10, 0 }; /* 10s connection/search timeout */
332#endif
333#ifdef USE_WIN32_LDAP
334 TCHAR *host = NULL;
335#else
336 char *host = NULL;
337#endif
338 const char *user = data->state.creds ? data->state.creds->user : NULL;
339 const char *passwd = data->state.creds ? data->state.creds->passwd : NULL;
340 struct ip_quadruple ipquad;
341 bool is_ipv6;
342 BerElement *ber = NULL;
344 *done = TRUE; /* unconditionally */
345 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d",
346 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
347 infof(data, "LDAP local: %s", Curl_bufref_ptr(&data->state.url));
349#ifdef HAVE_LDAP_URL_PARSE
350 rc = ldap_url_parse(Curl_bufref_ptr(&data->state.url), &ludp);
351#else
352 rc = ldap_url_parse_low(data, conn, &ludp);
353#endif
354 if(rc) {
355 failf(data, "Bad LDAP URL: %s", ldap_err2string(rc));
356 result = CURLE_URL_MALFORMAT;
357 goto quit;
358 }
360 result = Curl_conn_get_ip_info(data, conn, FIRSTSOCKET, &is_ipv6, &ipquad);
361 if(result)
362 goto quit;
364 /* Get the URL scheme (either ldap or ldaps) */
365 ldap_ssl = Curl_conn_is_ssl(conn, FIRSTSOCKET);
366 infof(data, "LDAP local: trying to establish %s connection",
367 ldap_ssl ? "encrypted" : "cleartext");
369#ifdef USE_WIN32_LDAP
370 host = curlx_convert_UTF8_to_tchar(conn->origin->hostname);
371 if(!host) {
372 result = CURLE_OUT_OF_MEMORY;
374 goto quit;
375 }
376#else
377 host = conn->origin->hostname;
378#endif
380#ifdef USE_WIN32_LDAP
381 if(ldap_ssl)
382 server = ldap_sslinit(host, (curl_ldap_num_t)ipquad.remote_port, 1);
383 else
384#endif
385 server = ldap_init(host, (curl_ldap_num_t)ipquad.remote_port);
386 if(!server) {
387 failf(data, "LDAP: cannot setup connect to %s:%u",
388 conn->origin->user_hostname, ipquad.remote_port);
389 result = CURLE_COULDNT_CONNECT;
390 goto quit;
391 }
393#ifdef LDAP_OPT_NETWORK_TIMEOUT
394 ldap_set_option(server, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
395#endif
396 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
398 /* Do not chase referrals. */
399 ldap_set_option(server, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
401 if(ldap_ssl) {
402#ifdef HAVE_LDAP_SSL
403#ifdef USE_WIN32_LDAP
404 /* Win32 LDAP uses the Windows CA store to verify certificates */
405 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
406 if(!conn->ssl_config.verifypeer) {
407 if(conn->ssl_config.verifyhost) {
408 failf(data, "LDAP local: host verification cannot be enabled when "
409 "peer verification is disabled for Windows native LDAP");
410 result = CURLE_NOT_BUILT_IN;
411 goto quit;
412 }
413 ldap_set_option(server, LDAP_OPT_SERVER_CERTIFICATE,
414 (void *)(uintptr_t)bypass_cert_verify);
415 }
416#elif defined(LDAP_OPT_X_TLS) /* !USE_WIN32_LDAP */
417 int ldap_option;
418 const char *ldap_ca = conn->ssl_config.CAfile;
419 if(conn->ssl_config.verifypeer) {
420 /* OpenLDAP SDK supports BASE64 files. */
421 if(conn->ssl_config.cert_type &&
422 !curl_strequal(conn->ssl_config.cert_type, "PEM")) {
423 failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type");
424 result = CURLE_SSL_CERTPROBLEM;
425 goto quit;
426 }
427 if(!ldap_ca) {
428 failf(data, "LDAP local: ERROR PEM CA cert not set");
429 result = CURLE_SSL_CERTPROBLEM;
430 goto quit;
431 }
432 infof(data, "LDAP local: using PEM CA cert: %s", ldap_ca);
433 rc = ldap_set_option(server, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
434 if(rc != LDAP_SUCCESS) {
435 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
436 ldap_err2string(rc));
437 result = CURLE_SSL_CERTPROBLEM;
438 goto quit;
439 }
440 ldap_option = LDAP_OPT_X_TLS_DEMAND;
441 }
442 else
443 ldap_option = LDAP_OPT_X_TLS_NEVER;
445 rc = ldap_set_option(server, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
446 if(rc != LDAP_SUCCESS) {
447 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
448 ldap_err2string(rc));
449 result = CURLE_SSL_CERTPROBLEM;
450 goto quit;
451 }
452 ldap_option = LDAP_OPT_X_TLS_HARD;
453 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
454 if(rc != LDAP_SUCCESS) {
455 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
456 ldap_err2string(rc));
457 result = CURLE_SSL_CERTPROBLEM;
458 goto quit;
459 }
460#else /* !USE_WIN32_LDAP && !LDAP_OPT_X_TLS */
461 /* we should probably never come up to here since configure
462 should check in first place if we can support LDAP SSL/TLS */
463 failf(data, "LDAP local: SSL/TLS not supported with this version "
464 "of the OpenLDAP toolkit");
465 result = CURLE_SSL_CERTPROBLEM;
466 goto quit;
467#endif
468#endif /* HAVE_LDAP_SSL */
469 }
470 else if(data->set.use_ssl > CURLUSESSL_TRY) {
471 failf(data, "LDAP local: explicit TLS not supported");
472 result = CURLE_NOT_BUILT_IN;
473 goto quit;
474 }
476#ifdef USE_WIN32_LDAP
477 rc = ldap_win_bind(data, server, user, passwd);
478#else
479 rc = ldap_simple_bind_s(server, user, passwd);
480#endif
481 if(!ldap_ssl && rc != LDAP_SUCCESS) {
482 ldap_proto = LDAP_VERSION2;
483 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
484#ifdef USE_WIN32_LDAP
485 rc = ldap_win_bind(data, server, user, passwd);
486#else
487 rc = ldap_simple_bind_s(server, user, passwd);
488#endif
489 }
490 if(rc != LDAP_SUCCESS) {
491#ifdef USE_WIN32_LDAP
492 failf(data, "LDAP local: bind via ldap_win_bind %s",
493 ldap_err2string(rc));
494#else
495 failf(data, "LDAP local: bind via ldap_simple_bind_s %s",
496 ldap_err2string(rc));
497#endif
498 result = CURLE_LDAP_CANNOT_BIND;
499 goto quit;
500 }
502 Curl_pgrsReset(data);
503 rc = ldap_search_s(server, ludp->lud_dn,
504 ludp->lud_scope,
505 ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
507 if(rc != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED) {
508 failf(data, "LDAP remote: %s", ldap_err2string(rc));
509 result = CURLE_LDAP_SEARCH_FAILED;
510 goto quit;
511 }
513 num = 0;
514 for(entryIterator = ldap_first_entry(server, ldapmsg);
515 entryIterator;
516 entryIterator = ldap_next_entry(server, entryIterator), num++) {
517#ifdef USE_WIN32_LDAP
518 TCHAR *attribute;
519#else
520 char *attribute;
521#endif
523 /* Get the DN and write it to the client */
524 {
525 char *name = NULL;
526 size_t name_len = 0;
527#ifdef USE_WIN32_LDAP
528 TCHAR *dn = ldap_get_dn(server, entryIterator);
529 if(dn)
530 name = curlx_convert_tchar_to_UTF8(dn);
531#else
532 char *dn = name = ldap_get_dn(server, entryIterator);
533#endif
534 if(!name)
535 result = dn ? CURLE_OUT_OF_MEMORY : CURLE_FAILED_INIT;
536 else {
537 name_len = strlen(name);
538 if(ldap_str_needs_base64(name, name_len)) {
539 char *dn_b64 = NULL;
540 size_t dn_b64_sz = 0;
542 /* A DN with e.g. CR/LF would inject LDIF lines, so encode it */
543 result = curlx_base64_encode((uint8_t *)name, name_len,
544 &dn_b64, &dn_b64_sz);
545 if(!result)
546 result = Curl_client_write(data, CLIENTWRITE_BODY, "DN:: ", 5);
547 if(!result)
548 result = Curl_client_write(data, CLIENTWRITE_BODY, dn_b64,
549 dn_b64_sz);
550 curlx_free(dn_b64);
551 }
552 else {
553 result = Curl_client_write(data, CLIENTWRITE_BODY, "DN: ", 4);
554 if(!result)
555 result = Curl_client_write(data, CLIENTWRITE_BODY, name, name_len);
556 }
557 if(!result)
558 result = Curl_client_write(data, CLIENTWRITE_BODY, "\n", 1);
559 }
560 FREE_ON_WINLDAP(name);
561 ldap_memfree(dn);
562 if(result)
563 goto quit;
564 }
566 /* Get the attributes and write them to the client */
567 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
568 attribute;
569 attribute = ldap_next_attribute(server, entryIterator, ber)) {
570 BerValue **vals;
571#ifdef USE_WIN32_LDAP
572 char *attr = curlx_convert_tchar_to_UTF8(attribute);
573 if(!attr) {
574 ldap_memfree(attribute);
575 result = CURLE_OUT_OF_MEMORY;
576 goto quit;
577 }
578#else
579 char *attr = attribute;
580#endif
581 vals = ldap_get_values_len(server, entryIterator, attribute);
582 if(vals) {
583 result = show_vals(data, vals, attr);
584 /* Free memory used to store values */
585 ldap_value_free_len(vals);
586 }
588 /* Free the attribute as we are done with it */
589 FREE_ON_WINLDAP(attr);
590 ldap_memfree(attribute);
592 if(!result)
593 result = Curl_client_write(data, CLIENTWRITE_BODY, "\n", 1);
594 if(result)
595 goto quit;
596 }
598 if(ber) {
599 ber_free(ber, 0);
600 ber = NULL;
601 }
602 }
604quit:
605 if(ber)
606 ber_free(ber, 0);
607 if(ldapmsg) {
608 ldap_msgfree(ldapmsg);
609 LDAP_TRACE(("Received %d entries\n", num));
610 }
611 if(rc == LDAP_SIZELIMIT_EXCEEDED)
612 infof(data, "There are more than %d entries", num);
613 if(ludp)
614 ldap_free_urldesc(ludp);
615 if(server)
616 ldap_unbind_s(server);
618 FREE_ON_WINLDAP(host);
620 /* no data to transfer */
621 Curl_xfer_setup_nop(data);
622 connclose(conn);
624 return result;
625}
627#ifdef DEBUG_LDAP
628static void ldap_trace_low(const char *fmt, ...)
629{
630 static int do_trace = -1;
631 va_list args;
633 if(do_trace == -1) {
634 const char *env = getenv("CURL_TRACE");
635 curl_off_t e = 0;
636 if(!curlx_str_number(&env, &e, INT_MAX))
637 do_trace = e > 0;
638 }
639 if(!do_trace)
640 return;
642 va_start(args, fmt);
643 curl_mvfprintf(stderr, fmt, args);
644 va_end(args);
645}
646#endif /* DEBUG_LDAP */
648#ifndef HAVE_LDAP_URL_PARSE
650/*
651 * Return scope-value for a scope-string.
652 */
653static int str2scope(const char *p)
654{
655 if(curl_strequal(p, "one"))
656 return LDAP_SCOPE_ONELEVEL;
657 if(curl_strequal(p, "onetree"))
658 return LDAP_SCOPE_ONELEVEL;
659 if(curl_strequal(p, "base"))
660 return LDAP_SCOPE_BASE;
661 if(curl_strequal(p, "sub"))
662 return LDAP_SCOPE_SUBTREE;
663 if(curl_strequal(p, "subtree"))
664 return LDAP_SCOPE_SUBTREE;
665 return -1;
666}
668/* number of entries in the attributes list */
669static size_t num_entries(const char *s)
670{
671 size_t items = 1;
673 s = strchr(s, ',');
674 while(s) {
675 items++;
676 s = strchr(s + 1, ',');
677 }
678 return items;
679}
681/*
682 * Break apart the pieces of an LDAP URL.
683 * Syntax:
684 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
685 *
686 * <hostname> already known from 'conn->origin->hostname'.
687 * <port> already known from 'conn->origin->port'.
688 * extract the rest from 'data->state.path+1'. All fields are optional.
689 * e.g.
690 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
691 * yields ludp->lud_dn = "".
692 *
693 * Defined in RFC4516 section 2.
694 */
695static curl_ldap_num_t ldap_url_parse2_low(struct Curl_easy *data,
696 const struct connectdata *conn,
697 LDAPURLDesc *ludp)
698{
699 curl_ldap_num_t rc = LDAP_SUCCESS;
700 char *p;
701 char *path;
702 char *q = NULL;
703 char *query = NULL;
704 size_t i;
706 if(!data ||
707 !data->state.up.path ||
708 data->state.up.path[0] != '/' ||
709 ((data->state.origin->scheme != &Curl_scheme_ldap) &&
710 (data->state.origin->scheme != &Curl_scheme_ldaps)))
711 return LDAP_INVALID_SYNTAX;
713 ludp->lud_scope = LDAP_SCOPE_BASE;
714 ludp->lud_port = conn->origin->port;
715 ludp->lud_host = conn->origin->hostname;
717 /* Duplicate the path */
718 p = path = curlx_strdup(data->state.up.path + 1);
719 if(!path)
720 return LDAP_NO_MEMORY;
722 /* Duplicate the query if present */
723 if(data->state.up.query) {
724 q = query = curlx_strdup(data->state.up.query);
725 if(!query) {
726 curlx_free(path);
727 return LDAP_NO_MEMORY;
728 }
729 }
731 /* Parse the DN (Distinguished Name) */
732 if(*p) {
733 char *dn = p;
734 char *unescaped;
735 CURLcode result;
737 LDAP_TRACE(("DN '%s'\n", dn));
739 /* Unescape the DN */
740 result = Curl_urldecode(dn, 0, &unescaped, NULL, REJECT_ZERO);
741 if(result) {
742 rc = LDAP_NO_MEMORY;
744 goto quit;
745 }
747#ifdef USE_WIN32_LDAP
748 /* Convert the unescaped string to a tchar */
749 ludp->lud_dn = curlx_convert_UTF8_to_tchar(unescaped);
751 /* Free the unescaped string as we are done with it */
752 curlx_free(unescaped);
754 if(!ludp->lud_dn) {
755 rc = LDAP_NO_MEMORY;
757 goto quit;
758 }
759#else
760 ludp->lud_dn = unescaped;
761#endif
762 }
764 p = q;
765 if(!p)
766 goto quit;
768 /* Parse the attributes. skip "??" */
769 q = strchr(p, '?');
770 if(q)
771 *q++ = '\0';
773 if(*p) {
774 size_t count = num_entries(p); /* at least one */
775 const char *atp = p;
777 /* Allocate our array (+1 for the NULL entry) */
778#ifdef USE_WIN32_LDAP
779 ludp->lud_attrs = curlx_calloc(count + 1, sizeof(TCHAR *));
780#else
781 ludp->lud_attrs = curlx_calloc(count + 1, sizeof(char *));
782#endif
783 if(!ludp->lud_attrs) {
784 rc = LDAP_NO_MEMORY;
785 goto quit;
786 }
788 for(i = 0; i < count; i++) {
789 char *unescaped;
790 CURLcode result;
791 struct Curl_str out;
793 if(curlx_str_until(&atp, &out, 1024, ','))
794 break;
796 LDAP_TRACE(("attr[%zu] '%.*s'\n", i, (int)out.len, out.str));
798 /* Unescape the attribute */
799 result = Curl_urldecode(out.str, out.len, &unescaped, NULL, REJECT_ZERO);
800 if(result) {
801 rc = LDAP_NO_MEMORY;
802 goto quit;
803 }
805#ifdef USE_WIN32_LDAP
806 /* Convert the unescaped string to a tchar */
807 ludp->lud_attrs[i] = curlx_convert_UTF8_to_tchar(unescaped);
809 /* Free the unescaped string as we are done with it */
810 curlx_free(unescaped);
812 if(!ludp->lud_attrs[i]) {
813 rc = LDAP_NO_MEMORY;
814 goto quit;
815 }
816#else
817 ludp->lud_attrs[i] = unescaped;
818#endif
820 ludp->lud_attrs_dups++;
821 if(curlx_str_single(&atp, ','))
822 break;
823 }
824 }
826 p = q;
827 if(!p)
828 goto quit;
830 /* Parse the scope. skip "??" */
831 q = strchr(p, '?');
832 if(q)
833 *q++ = '\0';
835 if(*p) {
836 ludp->lud_scope = str2scope(p);
837 if(ludp->lud_scope == -1) {
838 rc = LDAP_INVALID_SYNTAX;
840 goto quit;
841 }
842 LDAP_TRACE(("scope %d\n", ludp->lud_scope));
843 }
845 p = q;
846 if(!p)
847 goto quit;
849 /* Parse the filter */
850 q = strchr(p, '?');
851 if(q)
852 *q++ = '\0';
854 if(*p) {
855 char *filter = p;
856 char *unescaped;
857 CURLcode result;
859 LDAP_TRACE(("filter '%s'\n", filter));
861 /* Unescape the filter */
862 result = Curl_urldecode(filter, 0, &unescaped, NULL, REJECT_CTRL);
863 if(result) {
864 rc = LDAP_NO_MEMORY;
866 goto quit;
867 }
869#ifdef USE_WIN32_LDAP
870 /* Convert the unescaped string to a tchar */
871 ludp->lud_filter = curlx_convert_UTF8_to_tchar(unescaped);
873 /* Free the unescaped string as we are done with it */
874 curlx_free(unescaped);
876 if(!ludp->lud_filter) {
877 rc = LDAP_NO_MEMORY;
879 goto quit;
880 }
881#else
882 ludp->lud_filter = unescaped;
883#endif
884 }
886 p = q;
887 if(p && !*p) {
888 rc = LDAP_INVALID_SYNTAX;
890 goto quit;
891 }
893quit:
894 curlx_free(path);
895 curlx_free(query);
897 return rc;
898}
900static curl_ldap_num_t ldap_url_parse_low(struct Curl_easy *data,
901 const struct connectdata *conn,
902 LDAPURLDesc **ludpp)
903{
904 LDAPURLDesc *ludp = curlx_calloc(1, sizeof(*ludp));
905 curl_ldap_num_t rc;
907 *ludpp = NULL;
908 if(!ludp)
909 return LDAP_NO_MEMORY;
911 rc = ldap_url_parse2_low(data, conn, ludp);
912 if(rc != LDAP_SUCCESS) {
913 ldap_free_urldesc_low(ludp);
914 ludp = NULL;
915 }
916 *ludpp = ludp;
917 return rc;
918}
920static void ldap_free_urldesc_low(LDAPURLDesc *ludp)
921{
922 if(!ludp)
923 return;
925 curlx_free(ludp->lud_dn);
926 curlx_free(ludp->lud_filter);
928 if(ludp->lud_attrs) {
929 size_t i;
930 for(i = 0; i < ludp->lud_attrs_dups; i++) {
931 curlx_free(ludp->lud_attrs[i]);
932 }
933 curlx_free(ludp->lud_attrs);
934 }
936 curlx_free(ludp);
937}
938#endif /* !HAVE_LDAP_URL_PARSE */
940void Curl_ldap_version(char *buf, size_t bufsz)
941{
942#ifdef USE_WIN32_LDAP
943 curl_msnprintf(buf, bufsz, "WinLDAP");
944#elif defined(LDAP_OPT_X_TLS_PASSPHRASE)
945 /* Skip querying version for Apple's OpenLDAP fork. It was reported to make
946 DNS/network requests during the process, which in cases can take a long
947 time. The returned version is 2.4.28 in all known OS releases. */
948 curl_msnprintf(buf, bufsz, "AppleLDAP");
949#else
950 static const char flavor[] = "";
951 LDAPAPIInfo api;
952 api.ldapai_info_version = LDAP_API_INFO_VERSION;
954 /* Comparing against 0, as different platforms
955 disagree on the success define name */
956 if(ldap_get_option(NULL, LDAP_OPT_API_INFO, &api) == 0) {
957 unsigned int patch = (unsigned int)(api.ldapai_vendor_version % 100);
958 unsigned int major = (unsigned int)(api.ldapai_vendor_version / 10000);
959 unsigned int minor =
960 (((unsigned int)api.ldapai_vendor_version - (major * 10000))
961 - patch) / 100;
962#ifdef __OS400__
963 curl_msnprintf(buf, bufsz, "IBMLDAP/%u.%u.%u", major, minor, patch);
964 ldap_value_free(api.ldapai_extensions);
965 (void)flavor;
966#else
967 curl_msnprintf(buf, bufsz, "%s/%u.%u.%u%s",
968 api.ldapai_vendor_name, major, minor, patch, flavor);
969 ldap_memfree(api.ldapai_vendor_name);
970 ber_memvfree((void **)api.ldapai_extensions);
971#endif
972 }
973 else
974 curl_msnprintf(buf, bufsz, "LDAP/1");
975#endif /* USE_WIN32_LDAP */
976}
978/*
979 * LDAP protocol handler.
980 */
981const struct Curl_protocol Curl_protocol_ldap = {
982 ZERO_NULL, /* setup_connection */
983 ZERO_NULL, /* setup_filters */
984 ldap_do, /* do_it */
985 ZERO_NULL, /* done */
986 ZERO_NULL, /* do_more */
987 ZERO_NULL, /* connect_it */
988 ZERO_NULL, /* connecting */
989 ZERO_NULL, /* doing */
990 ZERO_NULL, /* proto_pollset */
991 ZERO_NULL, /* doing_pollset */
992 ZERO_NULL, /* domore_pollset */
993 ZERO_NULL, /* perform_pollset */
994 ZERO_NULL, /* disconnect */
995 ZERO_NULL, /* write_resp */
996 ZERO_NULL, /* write_resp_hd */
997 ZERO_NULL, /* connection_is_dead */
998 ZERO_NULL, /* attach connection */
999 ZERO_NULL, /* follow */
1000};
1002#if defined(CURL_HAVE_DIAG) && defined(__APPLE__)
1003#pragma GCC diagnostic pop
1004#endif
1006#endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */
Diagnostics4 git calls · 309 ms
CallStatusSizeTime0309 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=304 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2008.6 KiB304 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=8795 git.response.cache=MISS git.response.duration_ms=304 git.fetch.wants=fdc94599d554e4f4d315b7ac47b46ecaf1ce0b27 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=8795 git.pkt.payload.max=8774 git.sideband.band1.bytes=8773 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e14dc1397da140
- Request
- 7083d796…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format