1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
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.
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.
40 /* Wincrypt must be included before anything that could include OpenSSL. */
41 #if defined(USE_WIN32_CRYPTO)
43 /* Undefine wincrypt conflicting symbols for BoringSSL. */
45 #undef X509_EXTENSIONS
46 #undef PKCS7_ISSUER_AND_SERIAL
47 #undef PKCS7_SIGNER_INFO
52 #ifdef USE_WIN32_LDAP /* Use Windows LDAP implementation. */
54 # ifndef LDAP_VENDOR_NAME
55 # error Your Platform SDK is NOT sufficient for LDAP support! \
56 Update your Platform SDK, or disable LDAP support!
61 # define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
66 # if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
67 # include <ldap_ssl.h>
68 # endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
72 #include <curl/curl.h>
79 #include "curl_ldap.h"
80 #include "curl_multibyte.h"
81 #include "curl_base64.h"
83 /* The last 3 #include files should be in this order */
84 #include "curl_printf.h"
85 #include "curl_memory.h"
88 #ifndef HAVE_LDAP_URL_PARSE
90 /* Use our own implementation. */
95 #if defined(USE_WIN32_LDAP)
103 #if defined(USE_WIN32_LDAP)
109 size_t lud_attrs_dups; /* how many were dup'ed, this field is not in the
110 "real" struct so can only be used in code
111 without HAVE_LDAP_URL_PARSE defined */
115 #define LDAPURLDesc struct ldap_urldesc
117 static int _ldap_url_parse(struct Curl_easy *data,
118 const struct connectdata *conn,
120 static void _ldap_free_urldesc(LDAPURLDesc *ludp);
122 #undef ldap_free_urldesc
123 #define ldap_free_urldesc _ldap_free_urldesc
127 #define LDAP_TRACE(x) do { \
128 _ldap_trace("%u: ", __LINE__); \
132 static void _ldap_trace(const char *fmt, ...);
134 #define LDAP_TRACE(x) Curl_nop_stmt
137 #if defined(USE_WIN32_LDAP) && defined(ldap_err2string)
138 /* Use ansi error strings in UNICODE builds */
139 #undef ldap_err2string
140 #define ldap_err2string ldap_err2stringA
144 static CURLcode ldap_do(struct Curl_easy *data, bool *done);
147 * LDAP protocol handler.
150 const struct Curl_handler Curl_handler_ldap = {
152 ZERO_NULL, /* setup_connection */
154 ZERO_NULL, /* done */
155 ZERO_NULL, /* do_more */
156 ZERO_NULL, /* connect_it */
157 ZERO_NULL, /* connecting */
158 ZERO_NULL, /* doing */
159 ZERO_NULL, /* proto_getsock */
160 ZERO_NULL, /* doing_getsock */
161 ZERO_NULL, /* domore_getsock */
162 ZERO_NULL, /* perform_getsock */
163 ZERO_NULL, /* disconnect */
164 ZERO_NULL, /* readwrite */
165 ZERO_NULL, /* connection_check */
166 ZERO_NULL, /* attach connection */
167 PORT_LDAP, /* defport */
168 CURLPROTO_LDAP, /* protocol */
169 CURLPROTO_LDAP, /* family */
170 PROTOPT_NONE /* flags */
175 * LDAPS protocol handler.
178 const struct Curl_handler Curl_handler_ldaps = {
179 "LDAPS", /* scheme */
180 ZERO_NULL, /* setup_connection */
182 ZERO_NULL, /* done */
183 ZERO_NULL, /* do_more */
184 ZERO_NULL, /* connect_it */
185 ZERO_NULL, /* connecting */
186 ZERO_NULL, /* doing */
187 ZERO_NULL, /* proto_getsock */
188 ZERO_NULL, /* doing_getsock */
189 ZERO_NULL, /* domore_getsock */
190 ZERO_NULL, /* perform_getsock */
191 ZERO_NULL, /* disconnect */
192 ZERO_NULL, /* readwrite */
193 ZERO_NULL, /* connection_check */
194 ZERO_NULL, /* attach connection */
195 PORT_LDAPS, /* defport */
196 CURLPROTO_LDAPS, /* protocol */
197 CURLPROTO_LDAP, /* family */
198 PROTOPT_SSL /* flags */
202 #if defined(USE_WIN32_LDAP)
204 #if defined(USE_WINDOWS_SSPI)
205 static int ldap_win_bind_auth(LDAP *server, const char *user,
206 const char *passwd, unsigned long authflags)
209 SEC_WINNT_AUTH_IDENTITY cred;
210 int rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
212 memset(&cred, 0, sizeof(cred));
214 #if defined(USE_SPNEGO)
215 if(authflags & CURLAUTH_NEGOTIATE) {
216 method = LDAP_AUTH_NEGOTIATE;
220 #if defined(USE_NTLM)
221 if(authflags & CURLAUTH_NTLM) {
222 method = LDAP_AUTH_NTLM;
226 #if !defined(CURL_DISABLE_CRYPTO_AUTH)
227 if(authflags & CURLAUTH_DIGEST) {
228 method = LDAP_AUTH_DIGEST;
233 /* required anyway if one of upper preprocessor definitions enabled */
236 if(method && user && passwd) {
237 rc = Curl_create_sspi_identity(user, passwd, &cred);
239 rc = ldap_bind_s(server, NULL, (TCHAR *)&cred, method);
240 Curl_sspi_free_identity(&cred);
244 /* proceed with current user credentials */
245 method = LDAP_AUTH_NEGOTIATE;
246 rc = ldap_bind_s(server, NULL, NULL, method);
250 #endif /* #if defined(USE_WINDOWS_SSPI) */
252 static int ldap_win_bind(struct Curl_easy *data, LDAP *server,
253 const char *user, const char *passwd)
255 int rc = LDAP_INVALID_CREDENTIALS;
257 PTCHAR inuser = NULL;
258 PTCHAR inpass = NULL;
260 if(user && passwd && (data->set.httpauth & CURLAUTH_BASIC)) {
261 inuser = curlx_convert_UTF8_to_tchar((char *) user);
262 inpass = curlx_convert_UTF8_to_tchar((char *) passwd);
264 rc = ldap_simple_bind_s(server, inuser, inpass);
266 curlx_unicodefree(inuser);
267 curlx_unicodefree(inpass);
269 #if defined(USE_WINDOWS_SSPI)
271 rc = ldap_win_bind_auth(server, user, passwd, data->set.httpauth);
277 #endif /* #if defined(USE_WIN32_LDAP) */
279 #if defined(USE_WIN32_LDAP)
280 #define FREE_ON_WINLDAP(x) curlx_unicodefree(x)
282 #define FREE_ON_WINLDAP(x)
286 static CURLcode ldap_do(struct Curl_easy *data, bool *done)
288 CURLcode result = CURLE_OK;
291 LDAPURLDesc *ludp = NULL;
292 LDAPMessage *ldapmsg = NULL;
293 LDAPMessage *entryIterator;
295 struct connectdata *conn = data->conn;
296 int ldap_proto = LDAP_VERSION3;
298 char *val_b64 = NULL;
299 size_t val_b64_sz = 0;
300 curl_off_t dlsize = 0;
301 #ifdef LDAP_OPT_NETWORK_TIMEOUT
302 struct timeval ldap_timeout = {10, 0}; /* 10 sec connection/search timeout */
304 #if defined(USE_WIN32_LDAP)
312 *done = TRUE; /* unconditionally */
313 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d",
314 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
315 infof(data, "LDAP local: %s", data->state.url);
317 #ifdef HAVE_LDAP_URL_PARSE
318 rc = ldap_url_parse(data->state.url, &ludp);
320 rc = _ldap_url_parse(data, conn, &ludp);
323 failf(data, "Bad LDAP URL: %s", ldap_err2string(rc));
324 result = CURLE_URL_MALFORMAT;
328 /* Get the URL scheme (either ldap or ldaps) */
329 if(conn->given->flags & PROTOPT_SSL)
331 infof(data, "LDAP local: trying to establish %s connection",
332 ldap_ssl ? "encrypted" : "cleartext");
334 #if defined(USE_WIN32_LDAP)
335 host = curlx_convert_UTF8_to_tchar(conn->host.name);
337 result = CURLE_OUT_OF_MEMORY;
342 host = conn->host.name;
345 if(data->state.aptr.user) {
347 passwd = conn->passwd;
350 #ifdef LDAP_OPT_NETWORK_TIMEOUT
351 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
353 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
357 #ifdef USE_WIN32_LDAP
358 /* Win32 LDAP SDK doesn't support insecure mode without CA! */
359 server = ldap_sslinit(host, conn->port, 1);
360 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
363 char *ldap_ca = conn->ssl_config.CAfile;
364 #if defined(CURL_HAS_NOVELL_LDAPSDK)
365 rc = ldapssl_client_init(NULL, NULL);
366 if(rc != LDAP_SUCCESS) {
367 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
368 result = CURLE_SSL_CERTPROBLEM;
371 if(conn->ssl_config.verifypeer) {
372 /* Novell SDK supports DER or BASE64 files. */
373 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
374 if((data->set.ssl.cert_type) &&
375 (strcasecompare(data->set.ssl.cert_type, "DER")))
376 cert_type = LDAPSSL_CERT_FILETYPE_DER;
378 failf(data, "LDAP local: ERROR %s CA cert not set",
379 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
380 result = CURLE_SSL_CERTPROBLEM;
383 infof(data, "LDAP local: using %s CA cert '%s'",
384 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
386 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
387 if(rc != LDAP_SUCCESS) {
388 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
389 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
390 ldap_err2string(rc));
391 result = CURLE_SSL_CERTPROBLEM;
394 ldap_option = LDAPSSL_VERIFY_SERVER;
397 ldap_option = LDAPSSL_VERIFY_NONE;
398 rc = ldapssl_set_verify_mode(ldap_option);
399 if(rc != LDAP_SUCCESS) {
400 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
401 ldap_err2string(rc));
402 result = CURLE_SSL_CERTPROBLEM;
405 server = ldapssl_init(host, conn->port, 1);
407 failf(data, "LDAP local: Cannot connect to %s:%u",
408 conn->host.dispname, conn->port);
409 result = CURLE_COULDNT_CONNECT;
412 #elif defined(LDAP_OPT_X_TLS)
413 if(conn->ssl_config.verifypeer) {
414 /* OpenLDAP SDK supports BASE64 files. */
415 if((data->set.ssl.cert_type) &&
416 (!strcasecompare(data->set.ssl.cert_type, "PEM"))) {
417 failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type");
418 result = CURLE_SSL_CERTPROBLEM;
422 failf(data, "LDAP local: ERROR PEM CA cert not set");
423 result = CURLE_SSL_CERTPROBLEM;
426 infof(data, "LDAP local: using PEM CA cert: %s", ldap_ca);
427 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
428 if(rc != LDAP_SUCCESS) {
429 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
430 ldap_err2string(rc));
431 result = CURLE_SSL_CERTPROBLEM;
434 ldap_option = LDAP_OPT_X_TLS_DEMAND;
437 ldap_option = LDAP_OPT_X_TLS_NEVER;
439 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
440 if(rc != LDAP_SUCCESS) {
441 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
442 ldap_err2string(rc));
443 result = CURLE_SSL_CERTPROBLEM;
446 server = ldap_init(host, conn->port);
448 failf(data, "LDAP local: Cannot connect to %s:%u",
449 conn->host.dispname, conn->port);
450 result = CURLE_COULDNT_CONNECT;
453 ldap_option = LDAP_OPT_X_TLS_HARD;
454 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
455 if(rc != LDAP_SUCCESS) {
456 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
457 ldap_err2string(rc));
458 result = CURLE_SSL_CERTPROBLEM;
462 rc = ldap_start_tls_s(server, NULL, NULL);
463 if(rc != LDAP_SUCCESS) {
464 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
465 ldap_err2string(rc));
466 result = CURLE_SSL_CERTPROBLEM;
471 /* we should probably never come up to here since configure
472 should check in first place if we can support LDAP SSL/TLS */
473 failf(data, "LDAP local: SSL/TLS not supported with this version "
474 "of the OpenLDAP toolkit\n");
475 result = CURLE_SSL_CERTPROBLEM;
479 #endif /* CURL_LDAP_USE_SSL */
481 else if(data->set.use_ssl > CURLUSESSL_TRY) {
482 failf(data, "LDAP local: explicit TLS not supported");
483 result = CURLE_NOT_BUILT_IN;
487 server = ldap_init(host, conn->port);
489 failf(data, "LDAP local: Cannot connect to %s:%u",
490 conn->host.dispname, conn->port);
491 result = CURLE_COULDNT_CONNECT;
495 #ifdef USE_WIN32_LDAP
496 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
497 rc = ldap_win_bind(data, server, user, passwd);
499 rc = ldap_simple_bind_s(server, user, passwd);
501 if(!ldap_ssl && rc) {
502 ldap_proto = LDAP_VERSION2;
503 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
504 #ifdef USE_WIN32_LDAP
505 rc = ldap_win_bind(data, server, user, passwd);
507 rc = ldap_simple_bind_s(server, user, passwd);
511 #ifdef USE_WIN32_LDAP
512 failf(data, "LDAP local: bind via ldap_win_bind %s",
513 ldap_err2string(rc));
515 failf(data, "LDAP local: bind via ldap_simple_bind_s %s",
516 ldap_err2string(rc));
518 result = CURLE_LDAP_CANNOT_BIND;
522 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
523 ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
525 if(rc && rc != LDAP_SIZELIMIT_EXCEEDED) {
526 failf(data, "LDAP remote: %s", ldap_err2string(rc));
527 result = CURLE_LDAP_SEARCH_FAILED;
531 for(num = 0, entryIterator = ldap_first_entry(server, ldapmsg);
533 entryIterator = ldap_next_entry(server, entryIterator), num++) {
534 BerElement *ber = NULL;
535 #if defined(USE_WIN32_LDAP)
542 /* Get the DN and write it to the client */
546 #if defined(USE_WIN32_LDAP)
547 TCHAR *dn = ldap_get_dn(server, entryIterator);
548 name = curlx_convert_tchar_to_UTF8(dn);
552 result = CURLE_OUT_OF_MEMORY;
557 char *dn = name = ldap_get_dn(server, entryIterator);
559 name_len = strlen(name);
561 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"DN: ", 4);
563 FREE_ON_WINLDAP(name);
568 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) name,
571 FREE_ON_WINLDAP(name);
576 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
578 FREE_ON_WINLDAP(name);
584 dlsize += name_len + 5;
586 FREE_ON_WINLDAP(name);
590 /* Get the attributes and write them to the client */
591 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
593 attribute = ldap_next_attribute(server, entryIterator, ber)) {
596 #if defined(USE_WIN32_LDAP)
597 char *attr = curlx_convert_tchar_to_UTF8(attribute);
602 result = CURLE_OUT_OF_MEMORY;
607 char *attr = attribute;
609 attr_len = strlen(attr);
611 vals = ldap_get_values_len(server, entryIterator, attribute);
613 for(i = 0; (vals[i] != NULL); i++) {
614 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\t", 1);
616 ldap_value_free_len(vals);
617 FREE_ON_WINLDAP(attr);
618 ldap_memfree(attribute);
625 result = Curl_client_write(data, CLIENTWRITE_BODY,
626 (char *) attr, attr_len);
628 ldap_value_free_len(vals);
629 FREE_ON_WINLDAP(attr);
630 ldap_memfree(attribute);
637 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
639 ldap_value_free_len(vals);
640 FREE_ON_WINLDAP(attr);
641 ldap_memfree(attribute);
648 dlsize += attr_len + 3;
651 (strcmp(";binary", (char *) attr + (attr_len - 7)) == 0)) {
652 /* Binary attribute, encode to base64. */
653 result = Curl_base64_encode(vals[i]->bv_val, vals[i]->bv_len,
654 &val_b64, &val_b64_sz);
656 ldap_value_free_len(vals);
657 FREE_ON_WINLDAP(attr);
658 ldap_memfree(attribute);
666 result = Curl_client_write(data, CLIENTWRITE_BODY, val_b64,
670 ldap_value_free_len(vals);
671 FREE_ON_WINLDAP(attr);
672 ldap_memfree(attribute);
679 dlsize += val_b64_sz;
683 result = Curl_client_write(data, CLIENTWRITE_BODY, vals[i]->bv_val,
686 ldap_value_free_len(vals);
687 FREE_ON_WINLDAP(attr);
688 ldap_memfree(attribute);
695 dlsize += vals[i]->bv_len;
698 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
700 ldap_value_free_len(vals);
701 FREE_ON_WINLDAP(attr);
702 ldap_memfree(attribute);
712 /* Free memory used to store values */
713 ldap_value_free_len(vals);
716 /* Free the attribute as we are done with it */
717 FREE_ON_WINLDAP(attr);
718 ldap_memfree(attribute);
720 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
724 Curl_pgrsSetDownloadCounter(data, dlsize);
733 ldap_msgfree(ldapmsg);
734 LDAP_TRACE(("Received %d entries\n", num));
736 if(rc == LDAP_SIZELIMIT_EXCEEDED)
737 infof(data, "There are more than %d entries", num);
739 ldap_free_urldesc(ludp);
741 ldap_unbind_s(server);
742 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
744 ldapssl_client_deinit();
745 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
747 FREE_ON_WINLDAP(host);
749 /* no data to transfer */
750 Curl_setup_transfer(data, -1, -1, FALSE, -1);
751 connclose(conn, "LDAP connection always disable re-use");
757 static void _ldap_trace(const char *fmt, ...)
759 static int do_trace = -1;
763 const char *env = getenv("CURL_TRACE");
764 do_trace = (env && strtol(env, NULL, 10) > 0);
770 vfprintf(stderr, fmt, args);
775 #ifndef HAVE_LDAP_URL_PARSE
778 * Return scope-value for a scope-string.
780 static int str2scope(const char *p)
782 if(strcasecompare(p, "one"))
783 return LDAP_SCOPE_ONELEVEL;
784 if(strcasecompare(p, "onetree"))
785 return LDAP_SCOPE_ONELEVEL;
786 if(strcasecompare(p, "base"))
787 return LDAP_SCOPE_BASE;
788 if(strcasecompare(p, "sub"))
789 return LDAP_SCOPE_SUBTREE;
790 if(strcasecompare(p, "subtree"))
791 return LDAP_SCOPE_SUBTREE;
796 * Split 'str' into strings separated by commas.
797 * Note: out[] points into 'str'.
799 static bool split_str(char *str, char ***out, size_t *count)
807 s = strchr(str, ',');
810 s = strchr(++s, ',');
813 res = calloc(items, sizeof(char *));
817 for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
818 s = strtok_r(NULL, ",", &lasts), i++)
828 * Break apart the pieces of an LDAP URL.
830 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
832 * <hostname> already known from 'conn->host.name'.
833 * <port> already known from 'conn->remote_port'.
834 * extract the rest from 'data->state.path+1'. All fields are optional.
836 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
837 * yields ludp->lud_dn = "".
839 * Defined in RFC4516 section 2.
841 static int _ldap_url_parse2(struct Curl_easy *data,
842 const struct connectdata *conn, LDAPURLDesc *ludp)
844 int rc = LDAP_SUCCESS;
852 !data->state.up.path ||
853 data->state.up.path[0] != '/' ||
854 !strncasecompare("LDAP", data->state.up.scheme, 4))
855 return LDAP_INVALID_SYNTAX;
857 ludp->lud_scope = LDAP_SCOPE_BASE;
858 ludp->lud_port = conn->remote_port;
859 ludp->lud_host = conn->host.name;
861 /* Duplicate the path */
862 p = path = strdup(data->state.up.path + 1);
864 return LDAP_NO_MEMORY;
866 /* Duplicate the query if present */
867 if(data->state.up.query) {
868 q = query = strdup(data->state.up.query);
871 return LDAP_NO_MEMORY;
875 /* Parse the DN (Distinguished Name) */
881 LDAP_TRACE(("DN '%s'\n", dn));
883 /* Unescape the DN */
884 result = Curl_urldecode(dn, 0, &unescaped, NULL, REJECT_ZERO);
891 #if defined(USE_WIN32_LDAP)
892 /* Convert the unescaped string to a tchar */
893 ludp->lud_dn = curlx_convert_UTF8_to_tchar(unescaped);
895 /* Free the unescaped string as we are done with it */
904 ludp->lud_dn = unescaped;
912 /* Parse the attributes. skip "??" */
921 /* Split the string into an array of attributes */
922 if(!split_str(p, &attributes, &count)) {
928 /* Allocate our array (+1 for the NULL entry) */
929 #if defined(USE_WIN32_LDAP)
930 ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
932 ludp->lud_attrs = calloc(count + 1, sizeof(char *));
934 if(!ludp->lud_attrs) {
942 for(i = 0; i < count; i++) {
946 LDAP_TRACE(("attr[%zu] '%s'\n", i, attributes[i]));
948 /* Unescape the attribute */
949 result = Curl_urldecode(attributes[i], 0, &unescaped, NULL,
959 #if defined(USE_WIN32_LDAP)
960 /* Convert the unescaped string to a tchar */
961 ludp->lud_attrs[i] = curlx_convert_UTF8_to_tchar(unescaped);
963 /* Free the unescaped string as we are done with it */
966 if(!ludp->lud_attrs[i]) {
974 ludp->lud_attrs[i] = unescaped;
977 ludp->lud_attrs_dups++;
987 /* Parse the scope. skip "??" */
993 ludp->lud_scope = str2scope(p);
994 if(ludp->lud_scope == -1) {
995 rc = LDAP_INVALID_SYNTAX;
999 LDAP_TRACE(("scope %d\n", ludp->lud_scope));
1006 /* Parse the filter */
1016 LDAP_TRACE(("filter '%s'\n", filter));
1018 /* Unescape the filter */
1019 result = Curl_urldecode(filter, 0, &unescaped, NULL, REJECT_ZERO);
1021 rc = LDAP_NO_MEMORY;
1026 #if defined(USE_WIN32_LDAP)
1027 /* Convert the unescaped string to a tchar */
1028 ludp->lud_filter = curlx_convert_UTF8_to_tchar(unescaped);
1030 /* Free the unescaped string as we are done with it */
1033 if(!ludp->lud_filter) {
1034 rc = LDAP_NO_MEMORY;
1039 ludp->lud_filter = unescaped;
1045 rc = LDAP_INVALID_SYNTAX;
1057 static int _ldap_url_parse(struct Curl_easy *data,
1058 const struct connectdata *conn,
1059 LDAPURLDesc **ludpp)
1061 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
1066 return LDAP_NO_MEMORY;
1068 rc = _ldap_url_parse2(data, conn, ludp);
1069 if(rc != LDAP_SUCCESS) {
1070 _ldap_free_urldesc(ludp);
1077 static void _ldap_free_urldesc(LDAPURLDesc *ludp)
1082 #if defined(USE_WIN32_LDAP)
1083 curlx_unicodefree(ludp->lud_dn);
1084 curlx_unicodefree(ludp->lud_filter);
1087 free(ludp->lud_filter);
1090 if(ludp->lud_attrs) {
1092 for(i = 0; i < ludp->lud_attrs_dups; i++) {
1093 #if defined(USE_WIN32_LDAP)
1094 curlx_unicodefree(ludp->lud_attrs[i]);
1096 free(ludp->lud_attrs[i]);
1099 free(ludp->lud_attrs);
1104 #endif /* !HAVE_LDAP_URL_PARSE */
1105 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */