blob: 89047bcbdaa8e217acbfc3b20d019f54c5dce43d [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Alex Deymo486467e2017-12-19 19:04:07 +01008 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
Lucas Eckels9bd90e62012-08-06 15:07:02 -070025#if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
26
27/*
28 * Notice that USE_OPENLDAP is only a source code selection switch. When
29 * libcurl is built with USE_OPENLDAP defined the libcurl source code that
30 * gets compiled is the code from openldap.c, otherwise the code that gets
31 * compiled is the code from ldap.c.
32 *
33 * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
34 * might be required for compilation and runtime. In order to use ancient
35 * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
36 */
37
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070038#ifdef USE_WIN32_LDAP /* Use Windows LDAP implementation. */
Kristian Monsen5ab50182010-05-14 18:53:44 +010039# include <winldap.h>
40# ifndef LDAP_VENDOR_NAME
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070041# error Your Platform SDK is NOT sufficient for LDAP support! \
42 Update your Platform SDK, or disable LDAP support!
Kristian Monsen5ab50182010-05-14 18:53:44 +010043# else
44# include <winber.h>
45# endif
46#else
Lucas Eckels9bd90e62012-08-06 15:07:02 -070047# define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
48# ifdef HAVE_LBER_H
49# include <lber.h>
50# endif
Kristian Monsen5ab50182010-05-14 18:53:44 +010051# include <ldap.h>
Lucas Eckels9bd90e62012-08-06 15:07:02 -070052# if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
53# include <ldap_ssl.h>
54# endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
Kristian Monsen5ab50182010-05-14 18:53:44 +010055#endif
56
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070057/* These are macros in both <wincrypt.h> (in above <winldap.h>) and typedefs
58 * in BoringSSL's <openssl/x509.h>
59 */
60#ifdef HAVE_BORINGSSL
61# undef X509_NAME
62# undef X509_CERT_PAIR
63# undef X509_EXTENSIONS
Kristian Monsen5ab50182010-05-14 18:53:44 +010064#endif
65
66#include "urldata.h"
67#include <curl/curl.h>
68#include "sendf.h"
69#include "escape.h"
70#include "progress.h"
71#include "transfer.h"
Elliott Hughescee03382017-06-23 12:17:18 -070072#include "strcase.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010073#include "strtok.h"
74#include "curl_ldap.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070075#include "curl_multibyte.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010076#include "curl_base64.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070077#include "connect.h"
Alex Deymod15eaac2016-06-28 14:49:26 -070078/* The last 3 #include files should be in this order */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070079#include "curl_printf.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070080#include "curl_memory.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010081#include "memdebug.h"
82
83#ifndef HAVE_LDAP_URL_PARSE
84
85/* Use our own implementation. */
86
87typedef struct {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070088 char *lud_host;
89 int lud_port;
90#if defined(USE_WIN32_LDAP)
91 TCHAR *lud_dn;
92 TCHAR **lud_attrs;
93#else
94 char *lud_dn;
95 char **lud_attrs;
96#endif
97 int lud_scope;
98#if defined(USE_WIN32_LDAP)
99 TCHAR *lud_filter;
100#else
101 char *lud_filter;
102#endif
103 char **lud_exts;
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 */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100107} CURL_LDAPURLDesc;
108
109#undef LDAPURLDesc
110#define LDAPURLDesc CURL_LDAPURLDesc
111
Elliott Hughes82be86d2017-09-20 17:00:17 -0700112static int _ldap_url_parse(const struct connectdata *conn,
113 LDAPURLDesc **ludp);
114static void _ldap_free_urldesc(LDAPURLDesc *ludp);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100115
116#undef ldap_free_urldesc
117#define ldap_free_urldesc _ldap_free_urldesc
118#endif
119
120#ifdef DEBUG_LDAP
121 #define LDAP_TRACE(x) do { \
Elliott Hughes82be86d2017-09-20 17:00:17 -0700122 _ldap_trace("%u: ", __LINE__); \
Kristian Monsen5ab50182010-05-14 18:53:44 +0100123 _ldap_trace x; \
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700124 } WHILE_FALSE
Kristian Monsen5ab50182010-05-14 18:53:44 +0100125
Elliott Hughes82be86d2017-09-20 17:00:17 -0700126 static void _ldap_trace(const char *fmt, ...);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100127#else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700128 #define LDAP_TRACE(x) Curl_nop_stmt
Kristian Monsen5ab50182010-05-14 18:53:44 +0100129#endif
130
131
132static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
133
134/*
135 * LDAP protocol handler.
136 */
137
138const struct Curl_handler Curl_handler_ldap = {
139 "LDAP", /* scheme */
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 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700149 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100150 ZERO_NULL, /* perform_getsock */
151 ZERO_NULL, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700152 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700153 ZERO_NULL, /* connection_check */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100154 PORT_LDAP, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700155 CURLPROTO_LDAP, /* protocol */
156 PROTOPT_NONE /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100157};
158
159#ifdef HAVE_LDAP_SSL
160/*
161 * LDAPS protocol handler.
162 */
163
164const struct Curl_handler Curl_handler_ldaps = {
165 "LDAPS", /* scheme */
166 ZERO_NULL, /* setup_connection */
167 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 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700175 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100176 ZERO_NULL, /* perform_getsock */
177 ZERO_NULL, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700178 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700179 ZERO_NULL, /* connection_check */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100180 PORT_LDAPS, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700181 CURLPROTO_LDAPS, /* protocol */
182 PROTOPT_SSL /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100183};
184#endif
185
Elliott Hughes82be86d2017-09-20 17:00:17 -0700186#if defined(USE_WIN32_LDAP)
187
188#if defined(USE_WINDOWS_SSPI)
189static int ldap_win_bind_auth(LDAP *server, const char *user,
190 const char *passwd, unsigned long authflags)
191{
192 ULONG method = 0;
Alex Deymo486467e2017-12-19 19:04:07 +0100193 SEC_WINNT_AUTH_IDENTITY cred;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700194 int rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
195
Alex Deymo486467e2017-12-19 19:04:07 +0100196 memset(&cred, 0, sizeof(cred));
197
Elliott Hughes82be86d2017-09-20 17:00:17 -0700198#if defined(USE_SPNEGO)
199 if(authflags & CURLAUTH_NEGOTIATE) {
200 method = LDAP_AUTH_NEGOTIATE;
201 }
202 else
203#endif
204#if defined(USE_NTLM)
205 if(authflags & CURLAUTH_NTLM) {
206 method = LDAP_AUTH_NTLM;
207 }
208 else
209#endif
210#if !defined(CURL_DISABLE_CRYPTO_AUTH)
211 if(authflags & CURLAUTH_DIGEST) {
212 method = LDAP_AUTH_DIGEST;
213 }
214 else
215#endif
216 {
217 /* required anyway if one of upper preprocessor definitions enabled */
218 }
219
220 if(method && user && passwd) {
221 rc = Curl_create_sspi_identity(user, passwd, &cred);
222 if(!rc) {
223 rc = ldap_bind_s(server, NULL, (TCHAR *)&cred, method);
224 Curl_sspi_free_identity(&cred);
225 }
226 }
227 else {
228 /* proceed with current user credentials */
229 method = LDAP_AUTH_NEGOTIATE;
230 rc = ldap_bind_s(server, NULL, NULL, method);
231 }
232 return rc;
233}
234#endif /* #if defined(USE_WINDOWS_SSPI) */
235
236static int ldap_win_bind(struct connectdata *conn, LDAP *server,
237 const char *user, const char *passwd)
238{
239 int rc = LDAP_INVALID_CREDENTIALS;
240
241 PTCHAR inuser = NULL;
242 PTCHAR inpass = NULL;
243
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);
247
248 rc = ldap_simple_bind_s(server, inuser, inpass);
249
250 Curl_unicodefree(inuser);
251 Curl_unicodefree(inpass);
252 }
253#if defined(USE_WINDOWS_SSPI)
254 else {
255 rc = ldap_win_bind_auth(server, user, passwd, conn->data->set.httpauth);
256 }
257#endif
258
259 return rc;
260}
261#endif /* #if defined(USE_WIN32_LDAP) */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100262
263static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
264{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700265 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100266 int rc = 0;
267 LDAP *server = NULL;
268 LDAPURLDesc *ludp = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700269 LDAPMessage *ldapmsg = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100270 LDAPMessage *entryIterator;
271 int num = 0;
Alex Deymo486467e2017-12-19 19:04:07 +0100272 struct Curl_easy *data = conn->data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100273 int ldap_proto = LDAP_VERSION3;
274 int ldap_ssl = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700275 char *val_b64 = NULL;
276 size_t val_b64_sz = 0;
277 curl_off_t dlsize = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100278#ifdef LDAP_OPT_NETWORK_TIMEOUT
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700279 struct timeval ldap_timeout = {10, 0}; /* 10 sec connection/search timeout */
280#endif
281#if defined(USE_WIN32_LDAP)
282 TCHAR *host = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700283#else
284 char *host = NULL;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700285#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700286 char *user = NULL;
287 char *passwd = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100288
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);
293
294#ifdef HAVE_LDAP_URL_PARSE
295 rc = ldap_url_parse(data->change.url, &ludp);
296#else
297 rc = _ldap_url_parse(conn, &ludp);
298#endif
299 if(rc != 0) {
300 failf(data, "LDAP local: %s", ldap_err2string(rc));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700301 result = CURLE_LDAP_INVALID_URL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100302 goto quit;
303 }
304
Alex Deymod15eaac2016-06-28 14:49:26 -0700305 /* Get the URL scheme (either ldap or ldaps) */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700306 if(conn->given->flags & PROTOPT_SSL)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100307 ldap_ssl = 1;
308 infof(data, "LDAP local: trying to establish %s connection\n",
309 ldap_ssl ? "encrypted" : "cleartext");
310
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700311#if defined(USE_WIN32_LDAP)
312 host = Curl_convert_UTF8_to_tchar(conn->host.name);
313 if(!host) {
314 result = CURLE_OUT_OF_MEMORY;
315
316 goto quit;
317 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700318#else
319 host = conn->host.name;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700320#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700321
322 if(conn->bits.user_passwd) {
323 user = conn->user;
324 passwd = conn->passwd;
325 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700326
Kristian Monsen5ab50182010-05-14 18:53:44 +0100327#ifdef LDAP_OPT_NETWORK_TIMEOUT
328 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
329#endif
330 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
331
332 if(ldap_ssl) {
333#ifdef HAVE_LDAP_SSL
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700334#ifdef USE_WIN32_LDAP
335 /* Win32 LDAP SDK doesn't support insecure mode without CA! */
336 server = ldap_sslinit(host, (int)conn->port, 1);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100337 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
338#else
339 int ldap_option;
Elliott Hughescee03382017-06-23 12:17:18 -0700340 char *ldap_ca = conn->ssl_config.CAfile;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100341#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));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700345 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100346 goto quit;
347 }
Elliott Hughescee03382017-06-23 12:17:18 -0700348 if(conn->ssl_config.verifypeer) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100349 /* Novell SDK supports DER or BASE64 files. */
350 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
Elliott Hughescee03382017-06-23 12:17:18 -0700351 if((data->set.ssl.cert_type) &&
352 (strcasecompare(data->set.ssl.cert_type, "DER")))
Kristian Monsen5ab50182010-05-14 18:53:44 +0100353 cert_type = LDAPSSL_CERT_FILETYPE_DER;
354 if(!ldap_ca) {
355 failf(data, "LDAP local: ERROR %s CA cert not set!",
356 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700357 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100358 goto quit;
359 }
360 infof(data, "LDAP local: using %s CA cert '%s'\n",
361 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
362 ldap_ca);
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));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700368 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100369 goto quit;
370 }
371 ldap_option = LDAPSSL_VERIFY_SERVER;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100372 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700373 else
374 ldap_option = LDAPSSL_VERIFY_NONE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100375 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));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700379 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100380 goto quit;
381 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700382 server = ldapssl_init(host, (int)conn->port, 1);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100383 if(server == NULL) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700384 failf(data, "LDAP local: Cannot connect to %s:%ld",
385 conn->host.dispname, conn->port);
386 result = CURLE_COULDNT_CONNECT;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100387 goto quit;
388 }
389#elif defined(LDAP_OPT_X_TLS)
Elliott Hughescee03382017-06-23 12:17:18 -0700390 if(conn->ssl_config.verifypeer) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100391 /* OpenLDAP SDK supports BASE64 files. */
Elliott Hughescee03382017-06-23 12:17:18 -0700392 if((data->set.ssl.cert_type) &&
393 (!strcasecompare(data->set.ssl.cert_type, "PEM"))) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700394 failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type!");
395 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100396 goto quit;
397 }
398 if(!ldap_ca) {
399 failf(data, "LDAP local: ERROR PEM CA cert not set!");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700400 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100401 goto quit;
402 }
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));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700408 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100409 goto quit;
410 }
411 ldap_option = LDAP_OPT_X_TLS_DEMAND;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100412 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700413 else
414 ldap_option = LDAP_OPT_X_TLS_NEVER;
415
Kristian Monsen5ab50182010-05-14 18:53:44 +0100416 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));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700420 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100421 goto quit;
422 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700423 server = ldap_init(host, (int)conn->port);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100424 if(server == NULL) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700425 failf(data, "LDAP local: Cannot connect to %s:%ld",
426 conn->host.dispname, conn->port);
427 result = CURLE_COULDNT_CONNECT;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100428 goto quit;
429 }
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));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700435 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100436 goto quit;
437 }
438/*
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));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700443 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100444 goto quit;
445 }
446*/
447#else
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");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700452 result = CURLE_SSL_CERTPROBLEM;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100453 goto quit;
454#endif
455#endif
456#endif /* CURL_LDAP_USE_SSL */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700457 }
458 else {
459 server = ldap_init(host, (int)conn->port);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100460 if(server == NULL) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700461 failf(data, "LDAP local: Cannot connect to %s:%ld",
462 conn->host.dispname, conn->port);
463 result = CURLE_COULDNT_CONNECT;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100464 goto quit;
465 }
466 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700467#ifdef USE_WIN32_LDAP
Kristian Monsen5ab50182010-05-14 18:53:44 +0100468 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
469#endif
470
Elliott Hughes82be86d2017-09-20 17:00:17 -0700471#ifdef USE_WIN32_LDAP
472 rc = ldap_win_bind(conn, server, user, passwd);
473#else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700474 rc = ldap_simple_bind_s(server, user, passwd);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700475#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100476 if(!ldap_ssl && rc != 0) {
477 ldap_proto = LDAP_VERSION2;
478 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700479#ifdef USE_WIN32_LDAP
480 rc = ldap_win_bind(conn, server, user, passwd);
481#else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700482 rc = ldap_simple_bind_s(server, user, passwd);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700483#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100484 }
485 if(rc != 0) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700486 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
487 result = CURLE_LDAP_CANNOT_BIND;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100488 goto quit;
489 }
490
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700491 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
492 ludp->lud_filter, ludp->lud_attrs, 0, &ldapmsg);
493
494 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
495 failf(data, "LDAP remote: %s", ldap_err2string(rc));
496 result = CURLE_LDAP_SEARCH_FAILED;
497 goto quit;
498 }
499
500 for(num = 0, entryIterator = ldap_first_entry(server, ldapmsg);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100501 entryIterator;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700502 entryIterator = ldap_next_entry(server, entryIterator), num++) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100503 BerElement *ber = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700504#if defined(USE_WIN32_LDAP)
505 TCHAR *attribute;
506#else
Kristian Monsen5ab50182010-05-14 18:53:44 +0100507 char *attribute; /*! suspicious that this isn't 'const' */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700508#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100509 int i;
510
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700511 /* Get the DN and write it to the client */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100512 {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700513 char *name;
514 size_t name_len;
515#if defined(USE_WIN32_LDAP)
516 TCHAR *dn = ldap_get_dn(server, entryIterator);
517 name = Curl_convert_tchar_to_UTF8(dn);
518 if(!name) {
519 ldap_memfree(dn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100520
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700521 result = CURLE_OUT_OF_MEMORY;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100522
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700523 goto quit;
524 }
525#else
526 char *dn = name = ldap_get_dn(server, entryIterator);
527#endif
528 name_len = strlen(name);
529
530 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
531 if(result) {
532#if defined(USE_WIN32_LDAP)
533 Curl_unicodefree(name);
534#endif
535 ldap_memfree(dn);
536
537 goto quit;
538 }
539
540 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *) name,
541 name_len);
542 if(result) {
543#if defined(USE_WIN32_LDAP)
544 Curl_unicodefree(name);
545#endif
546 ldap_memfree(dn);
547
548 goto quit;
549 }
550
551 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
552 if(result) {
553#if defined(USE_WIN32_LDAP)
554 Curl_unicodefree(name);
555#endif
556 ldap_memfree(dn);
557
558 goto quit;
559 }
560
561 dlsize += name_len + 5;
562
563#if defined(USE_WIN32_LDAP)
564 Curl_unicodefree(name);
565#endif
566 ldap_memfree(dn);
567 }
568
569 /* Get the attributes and write them to the client */
570 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
571 attribute;
572 attribute = ldap_next_attribute(server, entryIterator, ber)) {
573 BerValue **vals;
574 size_t attr_len;
575#if defined(USE_WIN32_LDAP)
576 char *attr = Curl_convert_tchar_to_UTF8(attribute);
577 if(!attr) {
578 if(ber)
579 ber_free(ber, 0);
580
581 result = CURLE_OUT_OF_MEMORY;
582
583 goto quit;
584 }
585#else
586 char *attr = attribute;
587#endif
588 attr_len = strlen(attr);
589
590 vals = ldap_get_values_len(server, entryIterator, attribute);
591 if(vals != NULL) {
592 for(i = 0; (vals[i] != NULL); i++) {
593 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
594 if(result) {
595 ldap_value_free_len(vals);
596#if defined(USE_WIN32_LDAP)
597 Curl_unicodefree(attr);
598#endif
599 ldap_memfree(attribute);
600 if(ber)
601 ber_free(ber, 0);
602
603 goto quit;
604 }
605
606 result = Curl_client_write(conn, CLIENTWRITE_BODY,
607 (char *) attr, attr_len);
608 if(result) {
609 ldap_value_free_len(vals);
610#if defined(USE_WIN32_LDAP)
611 Curl_unicodefree(attr);
612#endif
613 ldap_memfree(attribute);
614 if(ber)
615 ber_free(ber, 0);
616
617 goto quit;
618 }
619
620 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
621 if(result) {
622 ldap_value_free_len(vals);
623#if defined(USE_WIN32_LDAP)
624 Curl_unicodefree(attr);
625#endif
626 ldap_memfree(attribute);
627 if(ber)
628 ber_free(ber, 0);
629
630 goto quit;
631 }
632
633 dlsize += attr_len + 3;
634
635 if((attr_len > 7) &&
636 (strcmp(";binary", (char *) attr + (attr_len - 7)) == 0)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100637 /* Binary attribute, encode to base64. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700638 result = Curl_base64_encode(data,
639 vals[i]->bv_val,
640 vals[i]->bv_len,
641 &val_b64,
642 &val_b64_sz);
643 if(result) {
644 ldap_value_free_len(vals);
645#if defined(USE_WIN32_LDAP)
646 Curl_unicodefree(attr);
647#endif
648 ldap_memfree(attribute);
649 if(ber)
650 ber_free(ber, 0);
651
652 goto quit;
653 }
654
Kristian Monsen5ab50182010-05-14 18:53:44 +0100655 if(val_b64_sz > 0) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700656 result = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
657 val_b64_sz);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100658 free(val_b64);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700659 if(result) {
660 ldap_value_free_len(vals);
661#if defined(USE_WIN32_LDAP)
662 Curl_unicodefree(attr);
663#endif
664 ldap_memfree(attribute);
665 if(ber)
666 ber_free(ber, 0);
667
668 goto quit;
669 }
670
Kristian Monsen5ab50182010-05-14 18:53:44 +0100671 dlsize += val_b64_sz;
672 }
673 }
674 else {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700675 result = Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
676 vals[i]->bv_len);
677 if(result) {
678 ldap_value_free_len(vals);
679#if defined(USE_WIN32_LDAP)
680 Curl_unicodefree(attr);
681#endif
682 ldap_memfree(attribute);
683 if(ber)
684 ber_free(ber, 0);
685
686 goto quit;
687 }
688
Kristian Monsen5ab50182010-05-14 18:53:44 +0100689 dlsize += vals[i]->bv_len;
690 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700691
692 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
693 if(result) {
694 ldap_value_free_len(vals);
695#if defined(USE_WIN32_LDAP)
696 Curl_unicodefree(attr);
697#endif
698 ldap_memfree(attribute);
699 if(ber)
700 ber_free(ber, 0);
701
702 goto quit;
703 }
704
Kristian Monsen5ab50182010-05-14 18:53:44 +0100705 dlsize++;
706 }
707
708 /* Free memory used to store values */
709 ldap_value_free_len(vals);
710 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700711
712 /* Free the attribute as we are done with it */
713#if defined(USE_WIN32_LDAP)
714 Curl_unicodefree(attr);
715#endif
716 ldap_memfree(attribute);
717
718 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
719 if(result)
720 goto quit;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100721 dlsize++;
722 Curl_pgrsSetDownloadCounter(data, dlsize);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100723 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700724
Kristian Monsen5ab50182010-05-14 18:53:44 +0100725 if(ber)
726 ber_free(ber, 0);
727 }
728
729quit:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700730 if(ldapmsg) {
731 ldap_msgfree(ldapmsg);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700732 LDAP_TRACE(("Received %d entries\n", num));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100733 }
734 if(rc == LDAP_SIZELIMIT_EXCEEDED)
735 infof(data, "There are more than %d entries\n", num);
736 if(ludp)
737 ldap_free_urldesc(ludp);
738 if(server)
739 ldap_unbind_s(server);
740#if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
741 if(ldap_ssl)
742 ldapssl_client_deinit();
743#endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
744
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700745#if defined(USE_WIN32_LDAP)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700746 Curl_unicodefree(host);
747#endif
748
Kristian Monsen5ab50182010-05-14 18:53:44 +0100749 /* no data to transfer */
750 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700751 connclose(conn, "LDAP connection always disable re-use");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100752
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700753 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100754}
755
756#ifdef DEBUG_LDAP
Elliott Hughes82be86d2017-09-20 17:00:17 -0700757static void _ldap_trace(const char *fmt, ...)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100758{
759 static int do_trace = -1;
760 va_list args;
761
762 if(do_trace == -1) {
763 const char *env = getenv("CURL_TRACE");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700764 do_trace = (env && strtol(env, NULL, 10) > 0);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100765 }
766 if(!do_trace)
767 return;
768
Elliott Hughes82be86d2017-09-20 17:00:17 -0700769 va_start(args, fmt);
770 vfprintf(stderr, fmt, args);
771 va_end(args);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100772}
773#endif
774
775#ifndef HAVE_LDAP_URL_PARSE
776
777/*
778 * Return scope-value for a scope-string.
779 */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700780static int str2scope(const char *p)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100781{
Elliott Hughescee03382017-06-23 12:17:18 -0700782 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;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100792 return (-1);
793}
794
795/*
796 * Split 'str' into strings separated by commas.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700797 * Note: out[] points into 'str'.
Kristian Monsen5ab50182010-05-14 18:53:44 +0100798 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700799static bool split_str(char *str, char ***out, size_t *count)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100800{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700801 char **res;
802 char *lasts;
803 char *s;
804 size_t i;
805 size_t items = 1;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100806
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700807 s = strchr(str, ',');
808 while(s) {
809 items++;
810 s = strchr(++s, ',');
811 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100812
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700813 res = calloc(items, sizeof(char *));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100814 if(!res)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700815 return FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100816
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700817 for(i = 0, s = strtok_r(str, ",", &lasts); s && i < items;
818 s = strtok_r(NULL, ",", &lasts), i++)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100819 res[i] = s;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100820
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700821 *out = res;
822 *count = items;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100823
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700824 return TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100825}
826
827/*
828 * Break apart the pieces of an LDAP URL.
829 * Syntax:
830 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
831 *
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.
835 * e.g.
836 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
837 * yields ludp->lud_dn = "".
838 *
839 * Defined in RFC4516 section 2.
840 */
Elliott Hughescee03382017-06-23 12:17:18 -0700841static int _ldap_url_parse2(const struct connectdata *conn, LDAPURLDesc *ludp)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100842{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700843 int rc = LDAP_SUCCESS;
844 char *path;
845 char *p;
846 char *q;
847 size_t i;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100848
849 if(!conn->data ||
Elliott Hughescee03382017-06-23 12:17:18 -0700850 !conn->data->state.path ||
851 conn->data->state.path[0] != '/' ||
852 !checkprefix("LDAP", conn->data->change.url))
Kristian Monsen5ab50182010-05-14 18:53:44 +0100853 return LDAP_INVALID_SYNTAX;
854
855 ludp->lud_scope = LDAP_SCOPE_BASE;
856 ludp->lud_port = conn->remote_port;
857 ludp->lud_host = conn->host.name;
858
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700859 /* Duplicate the path */
860 p = path = strdup(conn->data->state.path + 1);
861 if(!path)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100862 return LDAP_NO_MEMORY;
863
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700864 /* Parse the DN (Distinguished Name) */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100865 q = strchr(p, '?');
866 if(q)
867 *q++ = '\0';
868
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700869 if(*p) {
870 char *dn = p;
871 char *unescaped;
Elliott Hughescee03382017-06-23 12:17:18 -0700872 CURLcode result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100873
Elliott Hughes82be86d2017-09-20 17:00:17 -0700874 LDAP_TRACE(("DN '%s'\n", dn));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700875
876 /* Unescape the DN */
Elliott Hughescee03382017-06-23 12:17:18 -0700877 result = Curl_urldecode(conn->data, dn, 0, &unescaped, NULL, FALSE);
878 if(result) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700879 rc = LDAP_NO_MEMORY;
880
881 goto quit;
882 }
883
884#if defined(USE_WIN32_LDAP)
885 /* Convert the unescaped string to a tchar */
886 ludp->lud_dn = Curl_convert_UTF8_to_tchar(unescaped);
887
888 /* Free the unescaped string as we are done with it */
889 Curl_unicodefree(unescaped);
890
891 if(!ludp->lud_dn) {
892 rc = LDAP_NO_MEMORY;
893
894 goto quit;
895 }
896#else
897 ludp->lud_dn = unescaped;
898#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100899 }
900
901 p = q;
902 if(!p)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700903 goto quit;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100904
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700905 /* Parse the attributes. skip "??" */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100906 q = strchr(p, '?');
907 if(q)
908 *q++ = '\0';
909
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700910 if(*p) {
911 char **attributes;
912 size_t count = 0;
913
914 /* Split the string into an array of attributes */
915 if(!split_str(p, &attributes, &count)) {
916 rc = LDAP_NO_MEMORY;
917
918 goto quit;
919 }
920
921 /* Allocate our array (+1 for the NULL entry) */
922#if defined(USE_WIN32_LDAP)
923 ludp->lud_attrs = calloc(count + 1, sizeof(TCHAR *));
924#else
925 ludp->lud_attrs = calloc(count + 1, sizeof(char *));
926#endif
927 if(!ludp->lud_attrs) {
928 free(attributes);
929
930 rc = LDAP_NO_MEMORY;
931
932 goto quit;
933 }
934
935 for(i = 0; i < count; i++) {
936 char *unescaped;
Elliott Hughescee03382017-06-23 12:17:18 -0700937 CURLcode result;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700938
Elliott Hughes82be86d2017-09-20 17:00:17 -0700939 LDAP_TRACE(("attr[%d] '%s'\n", i, attributes[i]));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700940
941 /* Unescape the attribute */
Elliott Hughescee03382017-06-23 12:17:18 -0700942 result = Curl_urldecode(conn->data, attributes[i], 0, &unescaped, NULL,
943 FALSE);
944 if(result) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700945 free(attributes);
946
947 rc = LDAP_NO_MEMORY;
948
949 goto quit;
950 }
951
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);
955
956 /* Free the unescaped string as we are done with it */
957 Curl_unicodefree(unescaped);
958
959 if(!ludp->lud_attrs[i]) {
960 free(attributes);
961
962 rc = LDAP_NO_MEMORY;
963
964 goto quit;
965 }
966#else
967 ludp->lud_attrs[i] = unescaped;
968#endif
969
970 ludp->lud_attrs_dups++;
971 }
972
973 free(attributes);
974 }
975
976 p = q;
977 if(!p)
978 goto quit;
979
980 /* Parse the scope. skip "??" */
981 q = strchr(p, '?');
982 if(q)
983 *q++ = '\0';
984
985 if(*p) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100986 ludp->lud_scope = str2scope(p);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700987 if(ludp->lud_scope == -1) {
988 rc = LDAP_INVALID_SYNTAX;
989
990 goto quit;
991 }
Elliott Hughes82be86d2017-09-20 17:00:17 -0700992 LDAP_TRACE(("scope %d\n", ludp->lud_scope));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100993 }
994
995 p = q;
996 if(!p)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700997 goto quit;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100998
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700999 /* Parse the filter */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001000 q = strchr(p, '?');
1001 if(q)
1002 *q++ = '\0';
Kristian Monsen5ab50182010-05-14 18:53:44 +01001003
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001004 if(*p) {
1005 char *filter = p;
1006 char *unescaped;
Elliott Hughescee03382017-06-23 12:17:18 -07001007 CURLcode result;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001008
Elliott Hughes82be86d2017-09-20 17:00:17 -07001009 LDAP_TRACE(("filter '%s'\n", filter));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001010
1011 /* Unescape the filter */
Elliott Hughescee03382017-06-23 12:17:18 -07001012 result = Curl_urldecode(conn->data, filter, 0, &unescaped, NULL, FALSE);
1013 if(result) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001014 rc = LDAP_NO_MEMORY;
1015
1016 goto quit;
1017 }
1018
1019#if defined(USE_WIN32_LDAP)
1020 /* Convert the unescaped string to a tchar */
1021 ludp->lud_filter = Curl_convert_UTF8_to_tchar(unescaped);
1022
1023 /* Free the unescaped string as we are done with it */
1024 Curl_unicodefree(unescaped);
1025
1026 if(!ludp->lud_filter) {
1027 rc = LDAP_NO_MEMORY;
1028
1029 goto quit;
1030 }
1031#else
1032 ludp->lud_filter = unescaped;
1033#endif
1034 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001035
1036 p = q;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001037 if(p && !*p) {
1038 rc = LDAP_INVALID_SYNTAX;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001039
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001040 goto quit;
1041 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001042
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001043quit:
1044 free(path);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001045
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001046 return rc;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001047}
1048
Elliott Hughescee03382017-06-23 12:17:18 -07001049static int _ldap_url_parse(const struct connectdata *conn,
1050 LDAPURLDesc **ludpp)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001051{
1052 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
1053 int rc;
1054
1055 *ludpp = NULL;
1056 if(!ludp)
1057 return LDAP_NO_MEMORY;
1058
Elliott Hughescee03382017-06-23 12:17:18 -07001059 rc = _ldap_url_parse2(conn, ludp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001060 if(rc != LDAP_SUCCESS) {
1061 _ldap_free_urldesc(ludp);
1062 ludp = NULL;
1063 }
1064 *ludpp = ludp;
1065 return (rc);
1066}
1067
Elliott Hughescee03382017-06-23 12:17:18 -07001068static void _ldap_free_urldesc(LDAPURLDesc *ludp)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001069{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001070 size_t i;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001071
1072 if(!ludp)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001073 return;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001074
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001075 free(ludp->lud_dn);
1076 free(ludp->lud_filter);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001077
1078 if(ludp->lud_attrs) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001079 for(i = 0; i < ludp->lud_attrs_dups; i++)
1080 free(ludp->lud_attrs[i]);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001081 free(ludp->lud_attrs);
1082 }
1083
Elliott Hughes82be86d2017-09-20 17:00:17 -07001084 free(ludp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001085}
1086#endif /* !HAVE_LDAP_URL_PARSE */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001087#endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */