1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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.haxx.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 */
57 /* These are macros in both <wincrypt.h> (in above <winldap.h>) and typedefs
58 * in BoringSSL's <openssl/x509.h>
62 # undef X509_CERT_PAIR
63 # undef X509_EXTENSIONS
67 #include <curl/curl.h>
74 #include "curl_ldap.h"
75 #include "curl_multibyte.h"
76 #include "curl_base64.h"
78 /* The last 3 #include files should be in this order */
79 #include "curl_printf.h"
80 #include "curl_memory.h"
83 #ifndef HAVE_LDAP_URL_PARSE
85 /* Use our own implementation. */
90 #if defined(USE_WIN32_LDAP)
98 #if defined(USE_WIN32_LDAP)
104 size_t lud_attrs_dups; /* how many were dup'ed, this field is not in the
105 "real" struct so can only be used in code
106 without HAVE_LDAP_URL_PARSE defined */
110 #define LDAPURLDesc CURL_LDAPURLDesc
112 static int _ldap_url_parse(const struct connectdata *conn,
114 static void _ldap_free_urldesc(LDAPURLDesc *ludp);
116 #undef ldap_free_urldesc
117 #define ldap_free_urldesc _ldap_free_urldesc
121 #define LDAP_TRACE(x) do { \
122 _ldap_trace("%u: ", __LINE__); \
126 static void _ldap_trace(const char *fmt, ...);
128 #define LDAP_TRACE(x) Curl_nop_stmt
132 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
135 * LDAP protocol handler.
138 const struct Curl_handler Curl_handler_ldap = {
140 ZERO_NULL, /* setup_connection */
141 Curl_ldap, /* do_it */
142 ZERO_NULL, /* done */
143 ZERO_NULL, /* do_more */
144 ZERO_NULL, /* connect_it */
145 ZERO_NULL, /* connecting */
146 ZERO_NULL, /* doing */
147 ZERO_NULL, /* proto_getsock */
148 ZERO_NULL, /* doing_getsock */
149 ZERO_NULL, /* domore_getsock */
150 ZERO_NULL, /* perform_getsock */
151 ZERO_NULL, /* disconnect */
152 ZERO_NULL, /* readwrite */
153 ZERO_NULL, /* connection_check */
154 PORT_LDAP, /* defport */
155 CURLPROTO_LDAP, /* protocol */
156 PROTOPT_NONE /* flags */
161 * LDAPS protocol handler.
164 const struct Curl_handler Curl_handler_ldaps = {
165 "LDAPS", /* scheme */
166 ZERO_NULL, /* setup_connection */
167 Curl_ldap, /* 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 PORT_LDAPS, /* defport */
181 CURLPROTO_LDAPS, /* protocol */
182 PROTOPT_SSL /* flags */
186 #if defined(USE_WIN32_LDAP)
188 #if defined(USE_WINDOWS_SSPI)
189 static int ldap_win_bind_auth(LDAP *server, const char *user,
190 const char *passwd, unsigned long authflags)
193 SEC_WINNT_AUTH_IDENTITY cred;
194 int rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
196 memset(&cred, 0, sizeof(cred));
198 #if defined(USE_SPNEGO)
199 if(authflags & CURLAUTH_NEGOTIATE) {
200 method = LDAP_AUTH_NEGOTIATE;
204 #if defined(USE_NTLM)
205 if(authflags & CURLAUTH_NTLM) {
206 method = LDAP_AUTH_NTLM;
210 #if !defined(CURL_DISABLE_CRYPTO_AUTH)
211 if(authflags & CURLAUTH_DIGEST) {
212 method = LDAP_AUTH_DIGEST;
217 /* required anyway if one of upper preprocessor definitions enabled */
220 if(method && user && passwd) {
221 rc = Curl_create_sspi_identity(user, passwd, &cred);
223 rc = ldap_bind_s(server, NULL, (TCHAR *)&cred, method);
224 Curl_sspi_free_identity(&cred);
228 /* proceed with current user credentials */
229 method = LDAP_AUTH_NEGOTIATE;
230 rc = ldap_bind_s(server, NULL, NULL, method);
234 #endif /* #if defined(USE_WINDOWS_SSPI) */
236 static int ldap_win_bind(struct connectdata *conn, LDAP *server,
237 const char *user, const char *passwd)
239 int rc = LDAP_INVALID_CREDENTIALS;
241 PTCHAR inuser = NULL;
242 PTCHAR inpass = NULL;
244 if(user && passwd && (conn->data->set.httpauth & CURLAUTH_BASIC)) {
245 inuser = Curl_convert_UTF8_to_tchar((char *) user);
246 inpass = Curl_convert_UTF8_to_tchar((char *) passwd);
248 rc = ldap_simple_bind_s(server, inuser, inpass);
250 Curl_unicodefree(inuser);
251 Curl_unicodefree(inpass);
253 #if defined(USE_WINDOWS_SSPI)
255 rc = ldap_win_bind_auth(server, user, passwd, conn->data->set.httpauth);
261 #endif /* #if defined(USE_WIN32_LDAP) */
263 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
265 CURLcode result = CURLE_OK;
268 LDAPURLDesc *ludp = NULL;
269 LDAPMessage *ldapmsg = NULL;
270 LDAPMessage *entryIterator;
272 struct Curl_easy *data = conn->data;
273 int ldap_proto = LDAP_VERSION3;
275 char *val_b64 = NULL;
276 size_t val_b64_sz = 0;
277 curl_off_t dlsize = 0;
278 #ifdef LDAP_OPT_NETWORK_TIMEOUT
279 struct timeval ldap_timeout = {10, 0}; /* 10 sec connection/search timeout */
281 #if defined(USE_WIN32_LDAP)
289 *done = TRUE; /* unconditionally */
290 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
291 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
292 infof(data, "LDAP local: %s\n", data->change.url);
294 #ifdef HAVE_LDAP_URL_PARSE
295 rc = ldap_url_parse(data->change.url, &ludp);
297 rc = _ldap_url_parse(conn, &ludp);
300 failf(data, "LDAP local: %s", ldap_err2string(rc));
301 result = CURLE_LDAP_INVALID_URL;
305 /* Get the URL scheme (either ldap or ldaps) */
306 if(conn->given->flags & PROTOPT_SSL)
308 infof(data, "LDAP local: trying to establish %s connection\n",
309 ldap_ssl ? "encrypted" : "cleartext");
311 #if defined(USE_WIN32_LDAP)
312 host = Curl_convert_UTF8_to_tchar(conn->host.name);
314 result = CURLE_OUT_OF_MEMORY;
319 host = conn->host.name;
322 if(conn->bits.user_passwd) {
324 passwd = conn->passwd;
327 #ifdef LDAP_OPT_NETWORK_TIMEOUT
328 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
330 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
334 #ifdef USE_WIN32_LDAP
335 /* Win32 LDAP SDK doesn't support insecure mode without CA! */
336 server = ldap_sslinit(host, (int)conn->port, 1);
337 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
340 char *ldap_ca = conn->ssl_config.CAfile;
341 #if defined(CURL_HAS_NOVELL_LDAPSDK)
342 rc = ldapssl_client_init(NULL, NULL);
343 if(rc != LDAP_SUCCESS) {
344 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
345 result = CURLE_SSL_CERTPROBLEM;
348 if(conn->ssl_config.verifypeer) {
349 /* Novell SDK supports DER or BASE64 files. */
350 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
351 if((data->set.ssl.cert_type) &&
352 (strcasecompare(data->set.ssl.cert_type, "DER")))
353 cert_type = LDAPSSL_CERT_FILETYPE_DER;
355 failf(data, "LDAP local: ERROR %s CA cert not set!",
356 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
357 result = CURLE_SSL_CERTPROBLEM;
360 infof(data, "LDAP local: using %s CA cert '%s'\n",
361 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
363 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
364 if(rc != LDAP_SUCCESS) {
365 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
366 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
367 ldap_err2string(rc));
368 result = CURLE_SSL_CERTPROBLEM;
371 ldap_option = LDAPSSL_VERIFY_SERVER;
374 ldap_option = LDAPSSL_VERIFY_NONE;
375 rc = ldapssl_set_verify_mode(ldap_option);
376 if(rc != LDAP_SUCCESS) {
377 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
378 ldap_err2string(rc));
379 result = CURLE_SSL_CERTPROBLEM;
382 server = ldapssl_init(host, (int)conn->port, 1);
384 failf(data, "LDAP local: Cannot connect to %s:%ld",
385 conn->host.dispname, conn->port);
386 result = CURLE_COULDNT_CONNECT;
389 #elif defined(LDAP_OPT_X_TLS)
390 if(conn->ssl_config.verifypeer) {
391 /* OpenLDAP SDK supports BASE64 files. */
392 if((data->set.ssl.cert_type) &&
393 (!strcasecompare(data->set.ssl.cert_type, "PEM"))) {
394 failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type!");
395 result = CURLE_SSL_CERTPROBLEM;
399 failf(data, "LDAP local: ERROR PEM CA cert not set!");
400 result = CURLE_SSL_CERTPROBLEM;
403 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
404 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
405 if(rc != LDAP_SUCCESS) {
406 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
407 ldap_err2string(rc));
408 result = CURLE_SSL_CERTPROBLEM;
411 ldap_option = LDAP_OPT_X_TLS_DEMAND;
414 ldap_option = LDAP_OPT_X_TLS_NEVER;
416 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
417 if(rc != LDAP_SUCCESS) {
418 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
419 ldap_err2string(rc));
420 result = CURLE_SSL_CERTPROBLEM;
423 server = ldap_init(host, (int)conn->port);
425 failf(data, "LDAP local: Cannot connect to %s:%ld",
426 conn->host.dispname, conn->port);
427 result = CURLE_COULDNT_CONNECT;
430 ldap_option = LDAP_OPT_X_TLS_HARD;
431 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
432 if(rc != LDAP_SUCCESS) {
433 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
434 ldap_err2string(rc));
435 result = CURLE_SSL_CERTPROBLEM;
439 rc = ldap_start_tls_s(server, NULL, NULL);
440 if(rc != LDAP_SUCCESS) {
441 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
442 ldap_err2string(rc));
443 result = CURLE_SSL_CERTPROBLEM;
448 /* we should probably never come up to here since configure
449 should check in first place if we can support LDAP SSL/TLS */
450 failf(data, "LDAP local: SSL/TLS not supported with this version "
451 "of the OpenLDAP toolkit\n");
452 result = CURLE_SSL_CERTPROBLEM;
456 #endif /* CURL_LDAP_USE_SSL */
459 server = ldap_init(host, (int)conn->port);
461 failf(data, "LDAP local: Cannot connect to %s:%ld",
462 conn->host.dispname, conn->port);
463 result = CURLE_COULDNT_CONNECT;
467 #ifdef USE_WIN32_LDAP
468 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
471 #ifdef USE_WIN32_LDAP
472 rc = ldap_win_bind(conn, server, user, passwd);
474 rc = ldap_simple_bind_s(server, user, passwd);
476 if(!ldap_ssl && rc != 0) {
477 ldap_proto = LDAP_VERSION2;
478 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
479 #ifdef USE_WIN32_LDAP
480 rc = ldap_win_bind(conn, server, user, passwd);
482 rc = ldap_simple_bind_s(server, user, passwd);
486 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
487 result = CURLE_LDAP_CANNOT_BIND;
491 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
492 ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
494 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
495 failf(data, "LDAP remote: %s", ldap_err2string(rc));
496 result = CURLE_LDAP_SEARCH_FAILED;
500 for(num = 0, entryIterator = ldap_first_entry(server, ldapmsg);
502 entryIterator = ldap_next_entry(server, entryIterator), num++) {
503 BerElement *ber = NULL;
504 #if defined(USE_WIN32_LDAP)
507 char *attribute; /*! suspicious that this isn't 'const' */
511 /* Get the DN and write it to the client */
515 #if defined(USE_WIN32_LDAP)
516 TCHAR *dn = ldap_get_dn(server, entryIterator);
517 name = Curl_convert_tchar_to_UTF8(dn);
521 result = CURLE_OUT_OF_MEMORY;
526 char *dn = name = ldap_get_dn(server, entryIterator);
528 name_len = strlen(name);
530 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
532 #if defined(USE_WIN32_LDAP)
533 Curl_unicodefree(name);
540 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *) name,
543 #if defined(USE_WIN32_LDAP)
544 Curl_unicodefree(name);
551 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
553 #if defined(USE_WIN32_LDAP)
554 Curl_unicodefree(name);
561 dlsize += name_len + 5;
563 #if defined(USE_WIN32_LDAP)
564 Curl_unicodefree(name);
569 /* Get the attributes and write them to the client */
570 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
572 attribute = ldap_next_attribute(server, entryIterator, ber)) {
575 #if defined(USE_WIN32_LDAP)
576 char *attr = Curl_convert_tchar_to_UTF8(attribute);
581 result = CURLE_OUT_OF_MEMORY;
586 char *attr = attribute;
588 attr_len = strlen(attr);
590 vals = ldap_get_values_len(server, entryIterator, attribute);
592 for(i = 0; (vals[i] != NULL); i++) {
593 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
595 ldap_value_free_len(vals);
596 #if defined(USE_WIN32_LDAP)
597 Curl_unicodefree(attr);
599 ldap_memfree(attribute);
606 result = Curl_client_write(conn, CLIENTWRITE_BODY,
607 (char *) attr, attr_len);
609 ldap_value_free_len(vals);
610 #if defined(USE_WIN32_LDAP)
611 Curl_unicodefree(attr);
613 ldap_memfree(attribute);
620 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
622 ldap_value_free_len(vals);
623 #if defined(USE_WIN32_LDAP)
624 Curl_unicodefree(attr);
626 ldap_memfree(attribute);
633 dlsize += attr_len + 3;
636 (strcmp(";binary", (char *) attr + (attr_len - 7)) == 0)) {
637 /* Binary attribute, encode to base64. */
638 result = Curl_base64_encode(data,
644 ldap_value_free_len(vals);
645 #if defined(USE_WIN32_LDAP)
646 Curl_unicodefree(attr);
648 ldap_memfree(attribute);
656 result = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
660 ldap_value_free_len(vals);
661 #if defined(USE_WIN32_LDAP)
662 Curl_unicodefree(attr);
664 ldap_memfree(attribute);
671 dlsize += val_b64_sz;
675 result = Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
678 ldap_value_free_len(vals);
679 #if defined(USE_WIN32_LDAP)
680 Curl_unicodefree(attr);
682 ldap_memfree(attribute);
689 dlsize += vals[i]->bv_len;
692 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
694 ldap_value_free_len(vals);
695 #if defined(USE_WIN32_LDAP)
696 Curl_unicodefree(attr);
698 ldap_memfree(attribute);
708 /* Free memory used to store values */
709 ldap_value_free_len(vals);
712 /* Free the attribute as we are done with it */
713 #if defined(USE_WIN32_LDAP)
714 Curl_unicodefree(attr);
716 ldap_memfree(attribute);
718 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
722 Curl_pgrsSetDownloadCounter(data, dlsize);
731 ldap_msgfree(ldapmsg);
732 LDAP_TRACE(("Received %d entries\n", num));
734 if(rc == LDAP_SIZELIMIT_EXCEEDED)
735 infof(data, "There are more than %d entries\n", num);
737 ldap_free_urldesc(ludp);
739 ldap_unbind_s(server);
740 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
742 ldapssl_client_deinit();
743 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
745 #if defined(USE_WIN32_LDAP)
746 Curl_unicodefree(host);
749 /* no data to transfer */
750 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
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 'conn->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(const struct connectdata *conn, LDAPURLDesc *ludp)
843 int rc = LDAP_SUCCESS;
850 !conn->data->state.path ||
851 conn->data->state.path[0] != '/' ||
852 !checkprefix("LDAP", conn->data->change.url))
853 return LDAP_INVALID_SYNTAX;
855 ludp->lud_scope = LDAP_SCOPE_BASE;
856 ludp->lud_port = conn->remote_port;
857 ludp->lud_host = conn->host.name;
859 /* Duplicate the path */
860 p = path = strdup(conn->data->state.path + 1);
862 return LDAP_NO_MEMORY;
864 /* Parse the DN (Distinguished Name) */
874 LDAP_TRACE(("DN '%s'\n", dn));
876 /* Unescape the DN */
877 result = Curl_urldecode(conn->data, dn, 0, &unescaped, NULL, FALSE);
884 #if defined(USE_WIN32_LDAP)
885 /* Convert the unescaped string to a tchar */
886 ludp->lud_dn = Curl_convert_UTF8_to_tchar(unescaped);
888 /* Free the unescaped string as we are done with it */
889 Curl_unicodefree(unescaped);
897 ludp->lud_dn = unescaped;
905 /* Parse the attributes. skip "??" */
914 /* Split the string into an array of attributes */
915 if(!split_str(p, &attributes, &count)) {
921 /* Allocate our array (+1 for the NULL entry) */
922 #if defined(USE_WIN32_LDAP)
923 ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
925 ludp->lud_attrs = calloc(count + 1, sizeof(char *));
927 if(!ludp->lud_attrs) {
935 for(i = 0; i < count; i++) {
939 LDAP_TRACE(("attr[%d] '%s'\n", i, attributes[i]));
941 /* Unescape the attribute */
942 result = Curl_urldecode(conn->data, attributes[i], 0, &unescaped, NULL,
952 #if defined(USE_WIN32_LDAP)
953 /* Convert the unescaped string to a tchar */
954 ludp->lud_attrs[i] = Curl_convert_UTF8_to_tchar(unescaped);
956 /* Free the unescaped string as we are done with it */
957 Curl_unicodefree(unescaped);
959 if(!ludp->lud_attrs[i]) {
967 ludp->lud_attrs[i] = unescaped;
970 ludp->lud_attrs_dups++;
980 /* Parse the scope. skip "??" */
986 ludp->lud_scope = str2scope(p);
987 if(ludp->lud_scope == -1) {
988 rc = LDAP_INVALID_SYNTAX;
992 LDAP_TRACE(("scope %d\n", ludp->lud_scope));
999 /* Parse the filter */
1009 LDAP_TRACE(("filter '%s'\n", filter));
1011 /* Unescape the filter */
1012 result = Curl_urldecode(conn->data, filter, 0, &unescaped, NULL, FALSE);
1014 rc = LDAP_NO_MEMORY;
1019 #if defined(USE_WIN32_LDAP)
1020 /* Convert the unescaped string to a tchar */
1021 ludp->lud_filter = Curl_convert_UTF8_to_tchar(unescaped);
1023 /* Free the unescaped string as we are done with it */
1024 Curl_unicodefree(unescaped);
1026 if(!ludp->lud_filter) {
1027 rc = LDAP_NO_MEMORY;
1032 ludp->lud_filter = unescaped;
1038 rc = LDAP_INVALID_SYNTAX;
1049 static int _ldap_url_parse(const struct connectdata *conn,
1050 LDAPURLDesc **ludpp)
1052 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
1057 return LDAP_NO_MEMORY;
1059 rc = _ldap_url_parse2(conn, ludp);
1060 if(rc != LDAP_SUCCESS) {
1061 _ldap_free_urldesc(ludp);
1068 static void _ldap_free_urldesc(LDAPURLDesc *ludp)
1076 free(ludp->lud_filter);
1078 if(ludp->lud_attrs) {
1079 for(i = 0; i < ludp->lud_attrs_dups; i++)
1080 free(ludp->lud_attrs[i]);
1081 free(ludp->lud_attrs);
1086 #endif /* !HAVE_LDAP_URL_PARSE */
1087 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */