VirtualBox

source: vbox/trunk/src/libs/curl-7.83.1/lib/ldap.c@ 97122

最後變更 在這個檔案從97122是 95312,由 vboxsync 提交於 3 年 前

libs/{curl,libxml2}: OSE export fixes, bugref:8515

  • 屬性 svn:eol-style 設為 native
檔案大小: 28.3 KB
 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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.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
23#include "curl_setup.h"
24
25#if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
26
27/*
28 * Notice that USE_OPENLDAP is only a source code selection switch. When
29 * libcurl is built with USE_OPENLDAP defined the libcurl source code that
30 * gets compiled is the code from openldap.c, otherwise the code that gets
31 * compiled is the code from ldap.c.
32 *
33 * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
34 * might be required for compilation and runtime. In order to use ancient
35 * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
36 */
37
38#ifdef USE_WIN32_LDAP /* Use Windows LDAP implementation. */
39# include <winldap.h>
40# ifndef LDAP_VENDOR_NAME
41# error Your Platform SDK is NOT sufficient for LDAP support! \
42 Update your Platform SDK, or disable LDAP support!
43# else
44# include <winber.h>
45# endif
46#else
47# define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
48# ifdef HAVE_LBER_H
49# include <lber.h>
50# endif
51# include <ldap.h>
52# if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
53# include <ldap_ssl.h>
54# endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
55#endif
56
57#include "urldata.h"
58#include <curl/curl.h>
59#include "sendf.h"
60#include "escape.h"
61#include "progress.h"
62#include "transfer.h"
63#include "strcase.h"
64#include "strtok.h"
65#include "curl_ldap.h"
66#include "curl_multibyte.h"
67#include "curl_base64.h"
68#include "connect.h"
69/* The last 3 #include files should be in this order */
70#include "curl_printf.h"
71#include "curl_memory.h"
72#include "memdebug.h"
73
74#ifndef HAVE_LDAP_URL_PARSE
75
76/* Use our own implementation. */
77
78struct ldap_urldesc {
79 char *lud_host;
80 int lud_port;
81#if defined(USE_WIN32_LDAP)
82 TCHAR *lud_dn;
83 TCHAR **lud_attrs;
84#else
85 char *lud_dn;
86 char **lud_attrs;
87#endif
88 int lud_scope;
89#if defined(USE_WIN32_LDAP)
90 TCHAR *lud_filter;
91#else
92 char *lud_filter;
93#endif
94 char **lud_exts;
95 size_t lud_attrs_dups; /* how many were dup'ed, this field is not in the
96 "real" struct so can only be used in code
97 without HAVE_LDAP_URL_PARSE defined */
98};
99
100#undef LDAPURLDesc
101#define LDAPURLDesc struct ldap_urldesc
102
103static int _ldap_url_parse(struct Curl_easy *data,
104 const struct connectdata *conn,
105 LDAPURLDesc **ludp);
106static void _ldap_free_urldesc(LDAPURLDesc *ludp);
107
108#undef ldap_free_urldesc
109#define ldap_free_urldesc _ldap_free_urldesc
110#endif
111
112#ifdef DEBUG_LDAP
113 #define LDAP_TRACE(x) do { \
114 _ldap_trace("%u: ", __LINE__); \
115 _ldap_trace x; \
116 } while(0)
117
118 static void _ldap_trace(const char *fmt, ...);
119#else
120 #define LDAP_TRACE(x) Curl_nop_stmt
121#endif
122
123#if defined(USE_WIN32_LDAP) && defined(ldap_err2string)
124/* Use ansi error strings in UNICODE builds */
125#undef ldap_err2string
126#define ldap_err2string ldap_err2stringA
127#endif
128
129
130static CURLcode ldap_do(struct Curl_easy *data, bool *done);
131
132/*
133 * LDAP protocol handler.
134 */
135
136const struct Curl_handler Curl_handler_ldap = {
137 "LDAP", /* scheme */
138 ZERO_NULL, /* setup_connection */
139 ldap_do, /* 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 ZERO_NULL, /* attach connection */
153 PORT_LDAP, /* defport */
154 CURLPROTO_LDAP, /* protocol */
155 CURLPROTO_LDAP, /* family */
156 PROTOPT_NONE /* flags */
157};
158
159#ifdef HAVE_LDAP_SSL
160/*
161 * LDAPS protocol handler.
162 */
163
164const struct Curl_handler Curl_handler_ldaps = {
165 "LDAPS", /* scheme */
166 ZERO_NULL, /* setup_connection */
167 ldap_do, /* do_it */
168 ZERO_NULL, /* done */
169 ZERO_NULL, /* do_more */
170 ZERO_NULL, /* connect_it */
171 ZERO_NULL, /* connecting */
172 ZERO_NULL, /* doing */
173 ZERO_NULL, /* proto_getsock */
174 ZERO_NULL, /* doing_getsock */
175 ZERO_NULL, /* domore_getsock */
176 ZERO_NULL, /* perform_getsock */
177 ZERO_NULL, /* disconnect */
178 ZERO_NULL, /* readwrite */
179 ZERO_NULL, /* connection_check */
180 ZERO_NULL, /* attach connection */
181 PORT_LDAPS, /* defport */
182 CURLPROTO_LDAPS, /* protocol */
183 CURLPROTO_LDAP, /* family */
184 PROTOPT_SSL /* flags */
185};
186#endif
187
188#if defined(USE_WIN32_LDAP)
189
190#if defined(USE_WINDOWS_SSPI)
191static int ldap_win_bind_auth(LDAP *server, const char *user,
192 const char *passwd, unsigned long authflags)
193{
194 ULONG method = 0;
195 SEC_WINNT_AUTH_IDENTITY cred;
196 int rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
197
198 memset(&cred, 0, sizeof(cred));
199
200#if defined(USE_SPNEGO)
201 if(authflags & CURLAUTH_NEGOTIATE) {
202 method = LDAP_AUTH_NEGOTIATE;
203 }
204 else
205#endif
206#if defined(USE_NTLM)
207 if(authflags & CURLAUTH_NTLM) {
208 method = LDAP_AUTH_NTLM;
209 }
210 else
211#endif
212#if !defined(CURL_DISABLE_CRYPTO_AUTH)
213 if(authflags & CURLAUTH_DIGEST) {
214 method = LDAP_AUTH_DIGEST;
215 }
216 else
217#endif
218 {
219 /* required anyway if one of upper preprocessor definitions enabled */
220 }
221
222 if(method && user && passwd) {
223 rc = Curl_create_sspi_identity(user, passwd, &cred);
224 if(!rc) {
225 rc = ldap_bind_s(server, NULL, (TCHAR *)&cred, method);
226 Curl_sspi_free_identity(&cred);
227 }
228 }
229 else {
230 /* proceed with current user credentials */
231 method = LDAP_AUTH_NEGOTIATE;
232 rc = ldap_bind_s(server, NULL, NULL, method);
233 }
234 return rc;
235}
236#endif /* #if defined(USE_WINDOWS_SSPI) */
237
238static int ldap_win_bind(struct Curl_easy *data, LDAP *server,
239 const char *user, const char *passwd)
240{
241 int rc = LDAP_INVALID_CREDENTIALS;
242
243 PTCHAR inuser = NULL;
244 PTCHAR inpass = NULL;
245
246 if(user && passwd && (data->set.httpauth & CURLAUTH_BASIC)) {
247 inuser = curlx_convert_UTF8_to_tchar((char *) user);
248 inpass = curlx_convert_UTF8_to_tchar((char *) passwd);
249
250 rc = ldap_simple_bind_s(server, inuser, inpass);
251
252 curlx_unicodefree(inuser);
253 curlx_unicodefree(inpass);
254 }
255#if defined(USE_WINDOWS_SSPI)
256 else {
257 rc = ldap_win_bind_auth(server, user, passwd, data->set.httpauth);
258 }
259#endif
260
261 return rc;
262}
263#endif /* #if defined(USE_WIN32_LDAP) */
264
265#if defined(USE_WIN32_LDAP)
266#define FREE_ON_WINLDAP(x) curlx_unicodefree(x)
267#else
268#define FREE_ON_WINLDAP(x)
269#endif
270
271
272static CURLcode ldap_do(struct Curl_easy *data, bool *done)
273{
274 CURLcode result = CURLE_OK;
275 int rc = 0;
276 LDAP *server = NULL;
277 LDAPURLDesc *ludp = NULL;
278 LDAPMessage *ldapmsg = NULL;
279 LDAPMessage *entryIterator;
280 int num = 0;
281 struct connectdata *conn = data->conn;
282 int ldap_proto = LDAP_VERSION3;
283 int ldap_ssl = 0;
284 char *val_b64 = NULL;
285 size_t val_b64_sz = 0;
286 curl_off_t dlsize = 0;
287#ifdef LDAP_OPT_NETWORK_TIMEOUT
288 struct timeval ldap_timeout = {10, 0}; /* 10 sec connection/search timeout */
289#endif
290#if defined(USE_WIN32_LDAP)
291 TCHAR *host = NULL;
292#else
293 char *host = NULL;
294#endif
295 char *user = NULL;
296 char *passwd = NULL;
297
298 *done = TRUE; /* unconditionally */
299 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d",
300 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
301 infof(data, "LDAP local: %s", data->state.url);
302
303#ifdef HAVE_LDAP_URL_PARSE
304 rc = ldap_url_parse(data->state.url, &ludp);
305#else
306 rc = _ldap_url_parse(data, conn, &ludp);
307#endif
308 if(rc) {
309 failf(data, "Bad LDAP URL: %s", ldap_err2string(rc));
310 result = CURLE_URL_MALFORMAT;
311 goto quit;
312 }
313
314 /* Get the URL scheme (either ldap or ldaps) */
315 if(conn->given->flags & PROTOPT_SSL)
316 ldap_ssl = 1;
317 infof(data, "LDAP local: trying to establish %s connection",
318 ldap_ssl ? "encrypted" : "cleartext");
319
320#if defined(USE_WIN32_LDAP)
321 host = curlx_convert_UTF8_to_tchar(conn->host.name);
322 if(!host) {
323 result = CURLE_OUT_OF_MEMORY;
324
325 goto quit;
326 }
327#else
328 host = conn->host.name;
329#endif
330
331 if(data->state.aptr.user) {
332 user = conn->user;
333 passwd = conn->passwd;
334 }
335
336#ifdef LDAP_OPT_NETWORK_TIMEOUT
337 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
338#endif
339 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
340
341 if(ldap_ssl) {
342#ifdef HAVE_LDAP_SSL
343#ifdef USE_WIN32_LDAP
344 /* Win32 LDAP SDK doesn't support insecure mode without CA! */
345 server = ldap_sslinit(host, (int)conn->port, 1);
346 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
347#else
348 int ldap_option;
349 char *ldap_ca = conn->ssl_config.CAfile;
350#if defined(CURL_HAS_NOVELL_LDAPSDK)
351 rc = ldapssl_client_init(NULL, NULL);
352 if(rc != LDAP_SUCCESS) {
353 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
354 result = CURLE_SSL_CERTPROBLEM;
355 goto quit;
356 }
357 if(conn->ssl_config.verifypeer) {
358 /* Novell SDK supports DER or BASE64 files. */
359 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
360 if((data->set.ssl.cert_type) &&
361 (strcasecompare(data->set.ssl.cert_type, "DER")))
362 cert_type = LDAPSSL_CERT_FILETYPE_DER;
363 if(!ldap_ca) {
364 failf(data, "LDAP local: ERROR %s CA cert not set",
365 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
366 result = CURLE_SSL_CERTPROBLEM;
367 goto quit;
368 }
369 infof(data, "LDAP local: using %s CA cert '%s'",
370 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
371 ldap_ca);
372 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
373 if(rc != LDAP_SUCCESS) {
374 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
375 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
376 ldap_err2string(rc));
377 result = CURLE_SSL_CERTPROBLEM;
378 goto quit;
379 }
380 ldap_option = LDAPSSL_VERIFY_SERVER;
381 }
382 else
383 ldap_option = LDAPSSL_VERIFY_NONE;
384 rc = ldapssl_set_verify_mode(ldap_option);
385 if(rc != LDAP_SUCCESS) {
386 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
387 ldap_err2string(rc));
388 result = CURLE_SSL_CERTPROBLEM;
389 goto quit;
390 }
391 server = ldapssl_init(host, (int)conn->port, 1);
392 if(!server) {
393 failf(data, "LDAP local: Cannot connect to %s:%ld",
394 conn->host.dispname, conn->port);
395 result = CURLE_COULDNT_CONNECT;
396 goto quit;
397 }
398#elif defined(LDAP_OPT_X_TLS)
399 if(conn->ssl_config.verifypeer) {
400 /* OpenLDAP SDK supports BASE64 files. */
401 if((data->set.ssl.cert_type) &&
402 (!strcasecompare(data->set.ssl.cert_type, "PEM"))) {
403 failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type");
404 result = CURLE_SSL_CERTPROBLEM;
405 goto quit;
406 }
407 if(!ldap_ca) {
408 failf(data, "LDAP local: ERROR PEM CA cert not set");
409 result = CURLE_SSL_CERTPROBLEM;
410 goto quit;
411 }
412 infof(data, "LDAP local: using PEM CA cert: %s", ldap_ca);
413 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
414 if(rc != LDAP_SUCCESS) {
415 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
416 ldap_err2string(rc));
417 result = CURLE_SSL_CERTPROBLEM;
418 goto quit;
419 }
420 ldap_option = LDAP_OPT_X_TLS_DEMAND;
421 }
422 else
423 ldap_option = LDAP_OPT_X_TLS_NEVER;
424
425 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
426 if(rc != LDAP_SUCCESS) {
427 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
428 ldap_err2string(rc));
429 result = CURLE_SSL_CERTPROBLEM;
430 goto quit;
431 }
432 server = ldap_init(host, (int)conn->port);
433 if(!server) {
434 failf(data, "LDAP local: Cannot connect to %s:%ld",
435 conn->host.dispname, conn->port);
436 result = CURLE_COULDNT_CONNECT;
437 goto quit;
438 }
439 ldap_option = LDAP_OPT_X_TLS_HARD;
440 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
441 if(rc != LDAP_SUCCESS) {
442 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
443 ldap_err2string(rc));
444 result = CURLE_SSL_CERTPROBLEM;
445 goto quit;
446 }
447/*
448 rc = ldap_start_tls_s(server, NULL, NULL);
449 if(rc != LDAP_SUCCESS) {
450 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
451 ldap_err2string(rc));
452 result = CURLE_SSL_CERTPROBLEM;
453 goto quit;
454 }
455*/
456#else
457 /* we should probably never come up to here since configure
458 should check in first place if we can support LDAP SSL/TLS */
459 failf(data, "LDAP local: SSL/TLS not supported with this version "
460 "of the OpenLDAP toolkit\n");
461 result = CURLE_SSL_CERTPROBLEM;
462 goto quit;
463#endif
464#endif
465#endif /* CURL_LDAP_USE_SSL */
466 }
467 else if(data->set.use_ssl > CURLUSESSL_TRY) {
468 failf(data, "LDAP local: explicit TLS not supported");
469 result = CURLE_NOT_BUILT_IN;
470 goto quit;
471 }
472 else {
473 server = ldap_init(host, (int)conn->port);
474 if(!server) {
475 failf(data, "LDAP local: Cannot connect to %s:%ld",
476 conn->host.dispname, conn->port);
477 result = CURLE_COULDNT_CONNECT;
478 goto quit;
479 }
480 }
481#ifdef USE_WIN32_LDAP
482 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
483 rc = ldap_win_bind(data, server, user, passwd);
484#else
485 rc = ldap_simple_bind_s(server, user, passwd);
486#endif
487 if(!ldap_ssl && rc) {
488 ldap_proto = LDAP_VERSION2;
489 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
490#ifdef USE_WIN32_LDAP
491 rc = ldap_win_bind(data, server, user, passwd);
492#else
493 rc = ldap_simple_bind_s(server, user, passwd);
494#endif
495 }
496 if(rc) {
497#ifdef USE_WIN32_LDAP
498 failf(data, "LDAP local: bind via ldap_win_bind %s",
499 ldap_err2string(rc));
500#else
501 failf(data, "LDAP local: bind via ldap_simple_bind_s %s",
502 ldap_err2string(rc));
503#endif
504 result = CURLE_LDAP_CANNOT_BIND;
505 goto quit;
506 }
507
508 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
509 ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
510
511 if(rc && rc != LDAP_SIZELIMIT_EXCEEDED) {
512 failf(data, "LDAP remote: %s", ldap_err2string(rc));
513 result = CURLE_LDAP_SEARCH_FAILED;
514 goto quit;
515 }
516
517 for(num = 0, entryIterator = ldap_first_entry(server, ldapmsg);
518 entryIterator;
519 entryIterator = ldap_next_entry(server, entryIterator), num++) {
520 BerElement *ber = NULL;
521#if defined(USE_WIN32_LDAP)
522 TCHAR *attribute;
523#else
524 char *attribute;
525#endif
526 int i;
527
528 /* Get the DN and write it to the client */
529 {
530 char *name;
531 size_t name_len;
532#if defined(USE_WIN32_LDAP)
533 TCHAR *dn = ldap_get_dn(server, entryIterator);
534 name = curlx_convert_tchar_to_UTF8(dn);
535 if(!name) {
536 ldap_memfree(dn);
537
538 result = CURLE_OUT_OF_MEMORY;
539
540 goto quit;
541 }
542#else
543 char *dn = name = ldap_get_dn(server, entryIterator);
544#endif
545 name_len = strlen(name);
546
547 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"DN: ", 4);
548 if(result) {
549 FREE_ON_WINLDAP(name);
550 ldap_memfree(dn);
551 goto quit;
552 }
553
554 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) name,
555 name_len);
556 if(result) {
557 FREE_ON_WINLDAP(name);
558 ldap_memfree(dn);
559 goto quit;
560 }
561
562 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
563 if(result) {
564 FREE_ON_WINLDAP(name);
565 ldap_memfree(dn);
566
567 goto quit;
568 }
569
570 dlsize += name_len + 5;
571
572 FREE_ON_WINLDAP(name);
573 ldap_memfree(dn);
574 }
575
576 /* Get the attributes and write them to the client */
577 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
578 attribute;
579 attribute = ldap_next_attribute(server, entryIterator, ber)) {
580 BerValue **vals;
581 size_t attr_len;
582#if defined(USE_WIN32_LDAP)
583 char *attr = curlx_convert_tchar_to_UTF8(attribute);
584 if(!attr) {
585 if(ber)
586 ber_free(ber, 0);
587
588 result = CURLE_OUT_OF_MEMORY;
589
590 goto quit;
591 }
592#else
593 char *attr = attribute;
594#endif
595 attr_len = strlen(attr);
596
597 vals = ldap_get_values_len(server, entryIterator, attribute);
598 if(vals) {
599 for(i = 0; (vals[i] != NULL); i++) {
600 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\t", 1);
601 if(result) {
602 ldap_value_free_len(vals);
603 FREE_ON_WINLDAP(attr);
604 ldap_memfree(attribute);
605 if(ber)
606 ber_free(ber, 0);
607
608 goto quit;
609 }
610
611 result = Curl_client_write(data, CLIENTWRITE_BODY,
612 (char *) attr, attr_len);
613 if(result) {
614 ldap_value_free_len(vals);
615 FREE_ON_WINLDAP(attr);
616 ldap_memfree(attribute);
617 if(ber)
618 ber_free(ber, 0);
619
620 goto quit;
621 }
622
623 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
624 if(result) {
625 ldap_value_free_len(vals);
626 FREE_ON_WINLDAP(attr);
627 ldap_memfree(attribute);
628 if(ber)
629 ber_free(ber, 0);
630
631 goto quit;
632 }
633
634 dlsize += attr_len + 3;
635
636 if((attr_len > 7) &&
637 (strcmp(";binary", (char *) attr + (attr_len - 7)) == 0)) {
638 /* Binary attribute, encode to base64. */
639 result = Curl_base64_encode(vals[i]->bv_val, vals[i]->bv_len,
640 &val_b64, &val_b64_sz);
641 if(result) {
642 ldap_value_free_len(vals);
643 FREE_ON_WINLDAP(attr);
644 ldap_memfree(attribute);
645 if(ber)
646 ber_free(ber, 0);
647
648 goto quit;
649 }
650
651 if(val_b64_sz > 0) {
652 result = Curl_client_write(data, CLIENTWRITE_BODY, val_b64,
653 val_b64_sz);
654 free(val_b64);
655 if(result) {
656 ldap_value_free_len(vals);
657 FREE_ON_WINLDAP(attr);
658 ldap_memfree(attribute);
659 if(ber)
660 ber_free(ber, 0);
661
662 goto quit;
663 }
664
665 dlsize += val_b64_sz;
666 }
667 }
668 else {
669 result = Curl_client_write(data, CLIENTWRITE_BODY, vals[i]->bv_val,
670 vals[i]->bv_len);
671 if(result) {
672 ldap_value_free_len(vals);
673 FREE_ON_WINLDAP(attr);
674 ldap_memfree(attribute);
675 if(ber)
676 ber_free(ber, 0);
677
678 goto quit;
679 }
680
681 dlsize += vals[i]->bv_len;
682 }
683
684 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
685 if(result) {
686 ldap_value_free_len(vals);
687 FREE_ON_WINLDAP(attr);
688 ldap_memfree(attribute);
689 if(ber)
690 ber_free(ber, 0);
691
692 goto quit;
693 }
694
695 dlsize++;
696 }
697
698 /* Free memory used to store values */
699 ldap_value_free_len(vals);
700 }
701
702 /* Free the attribute as we are done with it */
703 FREE_ON_WINLDAP(attr);
704 ldap_memfree(attribute);
705
706 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
707 if(result)
708 goto quit;
709 dlsize++;
710 Curl_pgrsSetDownloadCounter(data, dlsize);
711 }
712
713 if(ber)
714 ber_free(ber, 0);
715 }
716
717quit:
718 if(ldapmsg) {
719 ldap_msgfree(ldapmsg);
720 LDAP_TRACE(("Received %d entries\n", num));
721 }
722 if(rc == LDAP_SIZELIMIT_EXCEEDED)
723 infof(data, "There are more than %d entries", num);
724 if(ludp)
725 ldap_free_urldesc(ludp);
726 if(server)
727 ldap_unbind_s(server);
728#if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
729 if(ldap_ssl)
730 ldapssl_client_deinit();
731#endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
732
733 FREE_ON_WINLDAP(host);
734
735 /* no data to transfer */
736 Curl_setup_transfer(data, -1, -1, FALSE, -1);
737 connclose(conn, "LDAP connection always disable re-use");
738
739 return result;
740}
741
742#ifdef DEBUG_LDAP
743static void _ldap_trace(const char *fmt, ...)
744{
745 static int do_trace = -1;
746 va_list args;
747
748 if(do_trace == -1) {
749 const char *env = getenv("CURL_TRACE");
750 do_trace = (env && strtol(env, NULL, 10) > 0);
751 }
752 if(!do_trace)
753 return;
754
755 va_start(args, fmt);
756 vfprintf(stderr, fmt, args);
757 va_end(args);
758}
759#endif
760
761#ifndef HAVE_LDAP_URL_PARSE
762
763/*
764 * Return scope-value for a scope-string.
765 */
766static int str2scope(const char *p)
767{
768 if(strcasecompare(p, "one"))
769 return LDAP_SCOPE_ONELEVEL;
770 if(strcasecompare(p, "onetree"))
771 return LDAP_SCOPE_ONELEVEL;
772 if(strcasecompare(p, "base"))
773 return LDAP_SCOPE_BASE;
774 if(strcasecompare(p, "sub"))
775 return LDAP_SCOPE_SUBTREE;
776 if(strcasecompare(p, "subtree"))
777 return LDAP_SCOPE_SUBTREE;
778 return (-1);
779}
780
781/*
782 * Split 'str' into strings separated by commas.
783 * Note: out[] points into 'str'.
784 */
785static bool split_str(char *str, char ***out, size_t *count)
786{
787 char **res;
788 char *lasts;
789 char *s;
790 size_t i;
791 size_t items = 1;
792
793 s = strchr(str, ',');
794 while(s) {
795 items++;
796 s = strchr(++s, ',');
797 }
798
799 res = calloc(items, sizeof(char *));
800 if(!res)
801 return FALSE;
802
803 for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
804 s = strtok_r(NULL, ",", &lasts), i++)
805 res[i] = s;
806
807 *out = res;
808 *count = items;
809
810 return TRUE;
811}
812
813/*
814 * Break apart the pieces of an LDAP URL.
815 * Syntax:
816 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
817 *
818 * <hostname> already known from 'conn->host.name'.
819 * <port> already known from 'conn->remote_port'.
820 * extract the rest from 'data->state.path+1'. All fields are optional.
821 * e.g.
822 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
823 * yields ludp->lud_dn = "".
824 *
825 * Defined in RFC4516 section 2.
826 */
827static int _ldap_url_parse2(struct Curl_easy *data,
828 const struct connectdata *conn, LDAPURLDesc *ludp)
829{
830 int rc = LDAP_SUCCESS;
831 char *p;
832 char *path;
833 char *q = NULL;
834 char *query = NULL;
835 size_t i;
836
837 if(!data ||
838 !data->state.up.path ||
839 data->state.up.path[0] != '/' ||
840 !strncasecompare("LDAP", data->state.up.scheme, 4))
841 return LDAP_INVALID_SYNTAX;
842
843 ludp->lud_scope = LDAP_SCOPE_BASE;
844 ludp->lud_port = conn->remote_port;
845 ludp->lud_host = conn->host.name;
846
847 /* Duplicate the path */
848 p = path = strdup(data->state.up.path + 1);
849 if(!path)
850 return LDAP_NO_MEMORY;
851
852 /* Duplicate the query if present */
853 if(data->state.up.query) {
854 q = query = strdup(data->state.up.query);
855 if(!query) {
856 free(path);
857 return LDAP_NO_MEMORY;
858 }
859 }
860
861 /* Parse the DN (Distinguished Name) */
862 if(*p) {
863 char *dn = p;
864 char *unescaped;
865 CURLcode result;
866
867 LDAP_TRACE(("DN '%s'\n", dn));
868
869 /* Unescape the DN */
870 result = Curl_urldecode(dn, 0, &unescaped, NULL, REJECT_ZERO);
871 if(result) {
872 rc = LDAP_NO_MEMORY;
873
874 goto quit;
875 }
876
877#if defined(USE_WIN32_LDAP)
878 /* Convert the unescaped string to a tchar */
879 ludp->lud_dn = curlx_convert_UTF8_to_tchar(unescaped);
880
881 /* Free the unescaped string as we are done with it */
882 free(unescaped);
883
884 if(!ludp->lud_dn) {
885 rc = LDAP_NO_MEMORY;
886
887 goto quit;
888 }
889#else
890 ludp->lud_dn = unescaped;
891#endif
892 }
893
894 p = q;
895 if(!p)
896 goto quit;
897
898 /* Parse the attributes. skip "??" */
899 q = strchr(p, '?');
900 if(q)
901 *q++ = '\0';
902
903 if(*p) {
904 char **attributes;
905 size_t count = 0;
906
907 /* Split the string into an array of attributes */
908 if(!split_str(p, &attributes, &count)) {
909 rc = LDAP_NO_MEMORY;
910
911 goto quit;
912 }
913
914 /* Allocate our array (+1 for the NULL entry) */
915#if defined(USE_WIN32_LDAP)
916 ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
917#else
918 ludp->lud_attrs = calloc(count + 1, sizeof(char *));
919#endif
920 if(!ludp->lud_attrs) {
921 free(attributes);
922
923 rc = LDAP_NO_MEMORY;
924
925 goto quit;
926 }
927
928 for(i = 0; i < count; i++) {
929 char *unescaped;
930 CURLcode result;
931
932 LDAP_TRACE(("attr[%zu] '%s'\n", i, attributes[i]));
933
934 /* Unescape the attribute */
935 result = Curl_urldecode(attributes[i], 0, &unescaped, NULL,
936 REJECT_ZERO);
937 if(result) {
938 free(attributes);
939
940 rc = LDAP_NO_MEMORY;
941
942 goto quit;
943 }
944
945#if defined(USE_WIN32_LDAP)
946 /* Convert the unescaped string to a tchar */
947 ludp->lud_attrs[i] = curlx_convert_UTF8_to_tchar(unescaped);
948
949 /* Free the unescaped string as we are done with it */
950 free(unescaped);
951
952 if(!ludp->lud_attrs[i]) {
953 free(attributes);
954
955 rc = LDAP_NO_MEMORY;
956
957 goto quit;
958 }
959#else
960 ludp->lud_attrs[i] = unescaped;
961#endif
962
963 ludp->lud_attrs_dups++;
964 }
965
966 free(attributes);
967 }
968
969 p = q;
970 if(!p)
971 goto quit;
972
973 /* Parse the scope. skip "??" */
974 q = strchr(p, '?');
975 if(q)
976 *q++ = '\0';
977
978 if(*p) {
979 ludp->lud_scope = str2scope(p);
980 if(ludp->lud_scope == -1) {
981 rc = LDAP_INVALID_SYNTAX;
982
983 goto quit;
984 }
985 LDAP_TRACE(("scope %d\n", ludp->lud_scope));
986 }
987
988 p = q;
989 if(!p)
990 goto quit;
991
992 /* Parse the filter */
993 q = strchr(p, '?');
994 if(q)
995 *q++ = '\0';
996
997 if(*p) {
998 char *filter = p;
999 char *unescaped;
1000 CURLcode result;
1001
1002 LDAP_TRACE(("filter '%s'\n", filter));
1003
1004 /* Unescape the filter */
1005 result = Curl_urldecode(filter, 0, &unescaped, NULL, REJECT_ZERO);
1006 if(result) {
1007 rc = LDAP_NO_MEMORY;
1008
1009 goto quit;
1010 }
1011
1012#if defined(USE_WIN32_LDAP)
1013 /* Convert the unescaped string to a tchar */
1014 ludp->lud_filter = curlx_convert_UTF8_to_tchar(unescaped);
1015
1016 /* Free the unescaped string as we are done with it */
1017 free(unescaped);
1018
1019 if(!ludp->lud_filter) {
1020 rc = LDAP_NO_MEMORY;
1021
1022 goto quit;
1023 }
1024#else
1025 ludp->lud_filter = unescaped;
1026#endif
1027 }
1028
1029 p = q;
1030 if(p && !*p) {
1031 rc = LDAP_INVALID_SYNTAX;
1032
1033 goto quit;
1034 }
1035
1036quit:
1037 free(path);
1038 free(query);
1039
1040 return rc;
1041}
1042
1043static int _ldap_url_parse(struct Curl_easy *data,
1044 const struct connectdata *conn,
1045 LDAPURLDesc **ludpp)
1046{
1047 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
1048 int rc;
1049
1050 *ludpp = NULL;
1051 if(!ludp)
1052 return LDAP_NO_MEMORY;
1053
1054 rc = _ldap_url_parse2(data, conn, ludp);
1055 if(rc != LDAP_SUCCESS) {
1056 _ldap_free_urldesc(ludp);
1057 ludp = NULL;
1058 }
1059 *ludpp = ludp;
1060 return (rc);
1061}
1062
1063static void _ldap_free_urldesc(LDAPURLDesc *ludp)
1064{
1065 if(!ludp)
1066 return;
1067
1068#if defined(USE_WIN32_LDAP)
1069 curlx_unicodefree(ludp->lud_dn);
1070 curlx_unicodefree(ludp->lud_filter);
1071#else
1072 free(ludp->lud_dn);
1073 free(ludp->lud_filter);
1074#endif
1075
1076 if(ludp->lud_attrs) {
1077 size_t i;
1078 for(i = 0; i < ludp->lud_attrs_dups; i++) {
1079#if defined(USE_WIN32_LDAP)
1080 curlx_unicodefree(ludp->lud_attrs[i]);
1081#else
1082 free(ludp->lud_attrs[i]);
1083#endif
1084 }
1085 free(ludp->lud_attrs);
1086 }
1087
1088 free(ludp);
1089}
1090#endif /* !HAVE_LDAP_URL_PARSE */
1091#endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette