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 ***************************************************************************/
23 #include "curl_setup.h"
25 #if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
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.
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.
38 #ifdef USE_WIN32_LDAP /* Use Windows LDAP implementation. */
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!
47 # define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
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 */
58 #include <curl/curl.h>
65 #include "curl_ldap.h"
66 #include "curl_multibyte.h"
67 #include "curl_base64.h"
69 /* The last 3 #include files should be in this order */
70 #include "curl_printf.h"
71 #include "curl_memory.h"
74 #ifndef HAVE_LDAP_URL_PARSE
76 /* Use our own implementation. */
81 #if defined(USE_WIN32_LDAP)
89 #if defined(USE_WIN32_LDAP)
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 */
101 #define LDAPURLDesc struct ldap_urldesc
103 static int _ldap_url_parse(struct Curl_easy *data,
104 const struct connectdata *conn,
106 static void _ldap_free_urldesc(LDAPURLDesc *ludp);
108 #undef ldap_free_urldesc
109 #define ldap_free_urldesc _ldap_free_urldesc
113 #define LDAP_TRACE(x) do { \
114 _ldap_trace("%u: ", __LINE__); \
118 static void _ldap_trace(const char *fmt, ...);
120 #define LDAP_TRACE(x) Curl_nop_stmt
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
130 static CURLcode ldap_do(struct Curl_easy *data, bool *done);
133 * LDAP protocol handler.
136 const struct Curl_handler Curl_handler_ldap = {
138 ZERO_NULL, /* setup_connection */
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 */
161 * LDAPS protocol handler.
164 const struct Curl_handler Curl_handler_ldaps = {
165 "LDAPS", /* scheme */
166 ZERO_NULL, /* setup_connection */
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 */
188 #if defined(USE_WIN32_LDAP)
190 #if defined(USE_WINDOWS_SSPI)
191 static int ldap_win_bind_auth(LDAP *server, const char *user,
192 const char *passwd, unsigned long authflags)
195 SEC_WINNT_AUTH_IDENTITY cred;
196 int rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
198 memset(&cred, 0, sizeof(cred));
200 #if defined(USE_SPNEGO)
201 if(authflags & CURLAUTH_NEGOTIATE) {
202 method = LDAP_AUTH_NEGOTIATE;
206 #if defined(USE_NTLM)
207 if(authflags & CURLAUTH_NTLM) {
208 method = LDAP_AUTH_NTLM;
212 #if !defined(CURL_DISABLE_CRYPTO_AUTH)
213 if(authflags & CURLAUTH_DIGEST) {
214 method = LDAP_AUTH_DIGEST;
219 /* required anyway if one of upper preprocessor definitions enabled */
222 if(method && user && passwd) {
223 rc = Curl_create_sspi_identity(user, passwd, &cred);
225 rc = ldap_bind_s(server, NULL, (TCHAR *)&cred, method);
226 Curl_sspi_free_identity(&cred);
230 /* proceed with current user credentials */
231 method = LDAP_AUTH_NEGOTIATE;
232 rc = ldap_bind_s(server, NULL, NULL, method);
236 #endif /* #if defined(USE_WINDOWS_SSPI) */
238 static int ldap_win_bind(struct Curl_easy *data, LDAP *server,
239 const char *user, const char *passwd)
241 int rc = LDAP_INVALID_CREDENTIALS;
243 PTCHAR inuser = NULL;
244 PTCHAR inpass = NULL;
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);
250 rc = ldap_simple_bind_s(server, inuser, inpass);
252 curlx_unicodefree(inuser);
253 curlx_unicodefree(inpass);
255 #if defined(USE_WINDOWS_SSPI)
257 rc = ldap_win_bind_auth(server, user, passwd, data->set.httpauth);
263 #endif /* #if defined(USE_WIN32_LDAP) */
265 #if defined(USE_WIN32_LDAP)
266 #define FREE_ON_WINLDAP(x) curlx_unicodefree(x)
268 #define FREE_ON_WINLDAP(x)
272 static CURLcode ldap_do(struct Curl_easy *data, bool *done)
274 CURLcode result = CURLE_OK;
277 LDAPURLDesc *ludp = NULL;
278 LDAPMessage *ldapmsg = NULL;
279 LDAPMessage *entryIterator;
281 struct connectdata *conn = data->conn;
282 int ldap_proto = LDAP_VERSION3;
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 */
290 #if defined(USE_WIN32_LDAP)
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);
303 #ifdef HAVE_LDAP_URL_PARSE
304 rc = ldap_url_parse(data->state.url, &ludp);
306 rc = _ldap_url_parse(data, conn, &ludp);
309 failf(data, "Bad LDAP URL: %s", ldap_err2string(rc));
310 result = CURLE_URL_MALFORMAT;
314 /* Get the URL scheme (either ldap or ldaps) */
315 if(conn->given->flags & PROTOPT_SSL)
317 infof(data, "LDAP local: trying to establish %s connection",
318 ldap_ssl ? "encrypted" : "cleartext");
320 #if defined(USE_WIN32_LDAP)
321 host = curlx_convert_UTF8_to_tchar(conn->host.name);
323 result = CURLE_OUT_OF_MEMORY;
328 host = conn->host.name;
331 if(data->state.aptr.user) {
333 passwd = conn->passwd;
336 #ifdef LDAP_OPT_NETWORK_TIMEOUT
337 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
339 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
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);
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;
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;
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;
369 infof(data, "LDAP local: using %s CA cert '%s'",
370 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
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;
380 ldap_option = LDAPSSL_VERIFY_SERVER;
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;
391 server = ldapssl_init(host, (int)conn->port, 1);
393 failf(data, "LDAP local: Cannot connect to %s:%ld",
394 conn->host.dispname, conn->port);
395 result = CURLE_COULDNT_CONNECT;
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;
408 failf(data, "LDAP local: ERROR PEM CA cert not set");
409 result = CURLE_SSL_CERTPROBLEM;
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;
420 ldap_option = LDAP_OPT_X_TLS_DEMAND;
423 ldap_option = LDAP_OPT_X_TLS_NEVER;
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;
432 server = ldap_init(host, (int)conn->port);
434 failf(data, "LDAP local: Cannot connect to %s:%ld",
435 conn->host.dispname, conn->port);
436 result = CURLE_COULDNT_CONNECT;
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;
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;
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;
465 #endif /* CURL_LDAP_USE_SSL */
467 else if(data->set.use_ssl > CURLUSESSL_TRY) {
468 failf(data, "LDAP local: explicit TLS not supported");
469 result = CURLE_NOT_BUILT_IN;
473 server = ldap_init(host, (int)conn->port);
475 failf(data, "LDAP local: Cannot connect to %s:%ld",
476 conn->host.dispname, conn->port);
477 result = CURLE_COULDNT_CONNECT;
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);
485 rc = ldap_simple_bind_s(server, user, passwd);
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);
493 rc = ldap_simple_bind_s(server, user, passwd);
497 #ifdef USE_WIN32_LDAP
498 failf(data, "LDAP local: bind via ldap_win_bind %s",
499 ldap_err2string(rc));
501 failf(data, "LDAP local: bind via ldap_simple_bind_s %s",
502 ldap_err2string(rc));
504 result = CURLE_LDAP_CANNOT_BIND;
508 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
509 ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
511 if(rc && rc != LDAP_SIZELIMIT_EXCEEDED) {
512 failf(data, "LDAP remote: %s", ldap_err2string(rc));
513 result = CURLE_LDAP_SEARCH_FAILED;
517 for(num = 0, entryIterator = ldap_first_entry(server, ldapmsg);
519 entryIterator = ldap_next_entry(server, entryIterator), num++) {
520 BerElement *ber = NULL;
521 #if defined(USE_WIN32_LDAP)
528 /* Get the DN and write it to the client */
532 #if defined(USE_WIN32_LDAP)
533 TCHAR *dn = ldap_get_dn(server, entryIterator);
534 name = curlx_convert_tchar_to_UTF8(dn);
538 result = CURLE_OUT_OF_MEMORY;
543 char *dn = name = ldap_get_dn(server, entryIterator);
545 name_len = strlen(name);
547 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"DN: ", 4);
549 FREE_ON_WINLDAP(name);
554 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) name,
557 FREE_ON_WINLDAP(name);
562 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
564 FREE_ON_WINLDAP(name);
570 dlsize += name_len + 5;
572 FREE_ON_WINLDAP(name);
576 /* Get the attributes and write them to the client */
577 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
579 attribute = ldap_next_attribute(server, entryIterator, ber)) {
582 #if defined(USE_WIN32_LDAP)
583 char *attr = curlx_convert_tchar_to_UTF8(attribute);
588 result = CURLE_OUT_OF_MEMORY;
593 char *attr = attribute;
595 attr_len = strlen(attr);
597 vals = ldap_get_values_len(server, entryIterator, attribute);
599 for(i = 0; (vals[i] != NULL); i++) {
600 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\t", 1);
602 ldap_value_free_len(vals);
603 FREE_ON_WINLDAP(attr);
604 ldap_memfree(attribute);
611 result = Curl_client_write(data, CLIENTWRITE_BODY,
612 (char *) attr, attr_len);
614 ldap_value_free_len(vals);
615 FREE_ON_WINLDAP(attr);
616 ldap_memfree(attribute);
623 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
625 ldap_value_free_len(vals);
626 FREE_ON_WINLDAP(attr);
627 ldap_memfree(attribute);
634 dlsize += attr_len + 3;
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);
642 ldap_value_free_len(vals);
643 FREE_ON_WINLDAP(attr);
644 ldap_memfree(attribute);
652 result = Curl_client_write(data, CLIENTWRITE_BODY, val_b64,
656 ldap_value_free_len(vals);
657 FREE_ON_WINLDAP(attr);
658 ldap_memfree(attribute);
665 dlsize += val_b64_sz;
669 result = Curl_client_write(data, CLIENTWRITE_BODY, vals[i]->bv_val,
672 ldap_value_free_len(vals);
673 FREE_ON_WINLDAP(attr);
674 ldap_memfree(attribute);
681 dlsize += vals[i]->bv_len;
684 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
686 ldap_value_free_len(vals);
687 FREE_ON_WINLDAP(attr);
688 ldap_memfree(attribute);
698 /* Free memory used to store values */
699 ldap_value_free_len(vals);
702 /* Free the attribute as we are done with it */
703 FREE_ON_WINLDAP(attr);
704 ldap_memfree(attribute);
706 result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
710 Curl_pgrsSetDownloadCounter(data, dlsize);
719 ldap_msgfree(ldapmsg);
720 LDAP_TRACE(("Received %d entries\n", num));
722 if(rc == LDAP_SIZELIMIT_EXCEEDED)
723 infof(data, "There are more than %d entries", num);
725 ldap_free_urldesc(ludp);
727 ldap_unbind_s(server);
728 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
730 ldapssl_client_deinit();
731 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
733 FREE_ON_WINLDAP(host);
735 /* no data to transfer */
736 Curl_setup_transfer(data, -1, -1, FALSE, -1);
737 connclose(conn, "LDAP connection always disable re-use");
743 static void _ldap_trace(const char *fmt, ...)
745 static int do_trace = -1;
749 const char *env = getenv("CURL_TRACE");
750 do_trace = (env && strtol(env, NULL, 10) > 0);
756 vfprintf(stderr, fmt, args);
761 #ifndef HAVE_LDAP_URL_PARSE
764 * Return scope-value for a scope-string.
766 static int str2scope(const char *p)
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;
782 * Split 'str' into strings separated by commas.
783 * Note: out[] points into 'str'.
785 static bool split_str(char *str, char ***out, size_t *count)
793 s = strchr(str, ',');
796 s = strchr(++s, ',');
799 res = calloc(items, sizeof(char *));
803 for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
804 s = strtok_r(NULL, ",", &lasts), i++)
814 * Break apart the pieces of an LDAP URL.
816 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
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.
822 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
823 * yields ludp->lud_dn = "".
825 * Defined in RFC4516 section 2.
827 static int _ldap_url_parse2(struct Curl_easy *data,
828 const struct connectdata *conn, LDAPURLDesc *ludp)
830 int rc = LDAP_SUCCESS;
838 !data->state.up.path ||
839 data->state.up.path[0] != '/' ||
840 !strncasecompare("LDAP", data->state.up.scheme, 4))
841 return LDAP_INVALID_SYNTAX;
843 ludp->lud_scope = LDAP_SCOPE_BASE;
844 ludp->lud_port = conn->remote_port;
845 ludp->lud_host = conn->host.name;
847 /* Duplicate the path */
848 p = path = strdup(data->state.up.path + 1);
850 return LDAP_NO_MEMORY;
852 /* Duplicate the query if present */
853 if(data->state.up.query) {
854 q = query = strdup(data->state.up.query);
857 return LDAP_NO_MEMORY;
861 /* Parse the DN (Distinguished Name) */
867 LDAP_TRACE(("DN '%s'\n", dn));
869 /* Unescape the DN */
870 result = Curl_urldecode(dn, 0, &unescaped, NULL, REJECT_ZERO);
877 #if defined(USE_WIN32_LDAP)
878 /* Convert the unescaped string to a tchar */
879 ludp->lud_dn = curlx_convert_UTF8_to_tchar(unescaped);
881 /* Free the unescaped string as we are done with it */
890 ludp->lud_dn = unescaped;
898 /* Parse the attributes. skip "??" */
907 /* Split the string into an array of attributes */
908 if(!split_str(p, &attributes, &count)) {
914 /* Allocate our array (+1 for the NULL entry) */
915 #if defined(USE_WIN32_LDAP)
916 ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
918 ludp->lud_attrs = calloc(count + 1, sizeof(char *));
920 if(!ludp->lud_attrs) {
928 for(i = 0; i < count; i++) {
932 LDAP_TRACE(("attr[%zu] '%s'\n", i, attributes[i]));
934 /* Unescape the attribute */
935 result = Curl_urldecode(attributes[i], 0, &unescaped, NULL,
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);
949 /* Free the unescaped string as we are done with it */
952 if(!ludp->lud_attrs[i]) {
960 ludp->lud_attrs[i] = unescaped;
963 ludp->lud_attrs_dups++;
973 /* Parse the scope. skip "??" */
979 ludp->lud_scope = str2scope(p);
980 if(ludp->lud_scope == -1) {
981 rc = LDAP_INVALID_SYNTAX;
985 LDAP_TRACE(("scope %d\n", ludp->lud_scope));
992 /* Parse the filter */
1002 LDAP_TRACE(("filter '%s'\n", filter));
1004 /* Unescape the filter */
1005 result = Curl_urldecode(filter, 0, &unescaped, NULL, REJECT_ZERO);
1007 rc = LDAP_NO_MEMORY;
1012 #if defined(USE_WIN32_LDAP)
1013 /* Convert the unescaped string to a tchar */
1014 ludp->lud_filter = curlx_convert_UTF8_to_tchar(unescaped);
1016 /* Free the unescaped string as we are done with it */
1019 if(!ludp->lud_filter) {
1020 rc = LDAP_NO_MEMORY;
1025 ludp->lud_filter = unescaped;
1031 rc = LDAP_INVALID_SYNTAX;
1043 static int _ldap_url_parse(struct Curl_easy *data,
1044 const struct connectdata *conn,
1045 LDAPURLDesc **ludpp)
1047 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
1052 return LDAP_NO_MEMORY;
1054 rc = _ldap_url_parse2(data, conn, ludp);
1055 if(rc != LDAP_SUCCESS) {
1056 _ldap_free_urldesc(ludp);
1063 static void _ldap_free_urldesc(LDAPURLDesc *ludp)
1068 #if defined(USE_WIN32_LDAP)
1069 curlx_unicodefree(ludp->lud_dn);
1070 curlx_unicodefree(ludp->lud_filter);
1073 free(ludp->lud_filter);
1076 if(ludp->lud_attrs) {
1078 for(i = 0; i < ludp->lud_attrs_dups; i++) {
1079 #if defined(USE_WIN32_LDAP)
1080 curlx_unicodefree(ludp->lud_attrs[i]);
1082 free(ludp->lud_attrs[i]);
1085 free(ludp->lud_attrs);
1090 #endif /* !HAVE_LDAP_URL_PARSE */
1091 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */