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