1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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 CURL_LDAP_WIN /* 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_memory.h"
67 #include "curl_base64.h"
71 #define _MPRINTF_REPLACE /* use our functions only */
72 #include <curl/mprintf.h>
76 #ifndef HAVE_LDAP_URL_PARSE
78 /* Use our own implementation. */
88 size_t lud_attrs_dups; /* how many were dup'ed, this field is not in the
89 "real" struct so can only be used in code
90 without HAVE_LDAP_URL_PARSE defined */
94 #define LDAPURLDesc CURL_LDAPURLDesc
96 static int _ldap_url_parse (const struct connectdata *conn,
98 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
100 #undef ldap_free_urldesc
101 #define ldap_free_urldesc _ldap_free_urldesc
105 #define LDAP_TRACE(x) do { \
106 _ldap_trace ("%u: ", __LINE__); \
110 static void _ldap_trace (const char *fmt, ...);
112 #define LDAP_TRACE(x) Curl_nop_stmt
116 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
119 * LDAP protocol handler.
122 const struct Curl_handler Curl_handler_ldap = {
124 ZERO_NULL, /* setup_connection */
125 Curl_ldap, /* do_it */
126 ZERO_NULL, /* done */
127 ZERO_NULL, /* do_more */
128 ZERO_NULL, /* connect_it */
129 ZERO_NULL, /* connecting */
130 ZERO_NULL, /* doing */
131 ZERO_NULL, /* proto_getsock */
132 ZERO_NULL, /* doing_getsock */
133 ZERO_NULL, /* domore_getsock */
134 ZERO_NULL, /* perform_getsock */
135 ZERO_NULL, /* disconnect */
136 ZERO_NULL, /* readwrite */
137 PORT_LDAP, /* defport */
138 CURLPROTO_LDAP, /* protocol */
139 PROTOPT_NONE /* flags */
144 * LDAPS protocol handler.
147 const struct Curl_handler Curl_handler_ldaps = {
148 "LDAPS", /* scheme */
149 ZERO_NULL, /* setup_connection */
150 Curl_ldap, /* do_it */
151 ZERO_NULL, /* done */
152 ZERO_NULL, /* do_more */
153 ZERO_NULL, /* connect_it */
154 ZERO_NULL, /* connecting */
155 ZERO_NULL, /* doing */
156 ZERO_NULL, /* proto_getsock */
157 ZERO_NULL, /* doing_getsock */
158 ZERO_NULL, /* domore_getsock */
159 ZERO_NULL, /* perform_getsock */
160 ZERO_NULL, /* disconnect */
161 ZERO_NULL, /* readwrite */
162 PORT_LDAPS, /* defport */
163 CURLPROTO_LDAPS, /* protocol */
164 PROTOPT_SSL /* flags */
169 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
171 CURLcode status = CURLE_OK;
174 LDAPURLDesc *ludp = NULL;
175 LDAPMessage *result = NULL;
176 LDAPMessage *entryIterator;
178 struct SessionHandle *data=conn->data;
179 int ldap_proto = LDAP_VERSION3;
181 char *val_b64 = NULL;
182 size_t val_b64_sz = 0;
183 curl_off_t dlsize = 0;
184 #ifdef LDAP_OPT_NETWORK_TIMEOUT
185 struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
188 *done = TRUE; /* unconditionally */
189 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
190 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
191 infof(data, "LDAP local: %s\n", data->change.url);
193 #ifdef HAVE_LDAP_URL_PARSE
194 rc = ldap_url_parse(data->change.url, &ludp);
196 rc = _ldap_url_parse(conn, &ludp);
199 failf(data, "LDAP local: %s", ldap_err2string(rc));
200 status = CURLE_LDAP_INVALID_URL;
204 /* Get the URL scheme ( either ldap or ldaps ) */
205 if(conn->given->flags & PROTOPT_SSL)
207 infof(data, "LDAP local: trying to establish %s connection\n",
208 ldap_ssl ? "encrypted" : "cleartext");
210 #ifdef LDAP_OPT_NETWORK_TIMEOUT
211 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
213 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
218 /* Win32 LDAP SDK doesn't support insecure mode without CA! */
219 server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
220 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
223 char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
224 #if defined(CURL_HAS_NOVELL_LDAPSDK)
225 rc = ldapssl_client_init(NULL, NULL);
226 if(rc != LDAP_SUCCESS) {
227 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
228 status = CURLE_SSL_CERTPROBLEM;
231 if(data->set.ssl.verifypeer) {
232 /* Novell SDK supports DER or BASE64 files. */
233 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
234 if((data->set.str[STRING_CERT_TYPE]) &&
235 (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
236 cert_type = LDAPSSL_CERT_FILETYPE_DER;
238 failf(data, "LDAP local: ERROR %s CA cert not set!",
239 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
240 status = CURLE_SSL_CERTPROBLEM;
243 infof(data, "LDAP local: using %s CA cert '%s'\n",
244 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
246 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
247 if(rc != LDAP_SUCCESS) {
248 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
249 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
250 ldap_err2string(rc));
251 status = CURLE_SSL_CERTPROBLEM;
254 ldap_option = LDAPSSL_VERIFY_SERVER;
257 ldap_option = LDAPSSL_VERIFY_NONE;
258 rc = ldapssl_set_verify_mode(ldap_option);
259 if(rc != LDAP_SUCCESS) {
260 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
261 ldap_err2string(rc));
262 status = CURLE_SSL_CERTPROBLEM;
265 server = ldapssl_init(conn->host.name, (int)conn->port, 1);
267 failf(data, "LDAP local: Cannot connect to %s:%ld",
268 conn->host.name, conn->port);
269 status = CURLE_COULDNT_CONNECT;
272 #elif defined(LDAP_OPT_X_TLS)
273 if(data->set.ssl.verifypeer) {
274 /* OpenLDAP SDK supports BASE64 files. */
275 if((data->set.str[STRING_CERT_TYPE]) &&
276 (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
277 failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type!");
278 status = CURLE_SSL_CERTPROBLEM;
282 failf(data, "LDAP local: ERROR PEM CA cert not set!");
283 status = CURLE_SSL_CERTPROBLEM;
286 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
287 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
288 if(rc != LDAP_SUCCESS) {
289 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
290 ldap_err2string(rc));
291 status = CURLE_SSL_CERTPROBLEM;
294 ldap_option = LDAP_OPT_X_TLS_DEMAND;
297 ldap_option = LDAP_OPT_X_TLS_NEVER;
299 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
300 if(rc != LDAP_SUCCESS) {
301 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
302 ldap_err2string(rc));
303 status = CURLE_SSL_CERTPROBLEM;
306 server = ldap_init(conn->host.name, (int)conn->port);
308 failf(data, "LDAP local: Cannot connect to %s:%ld",
309 conn->host.name, conn->port);
310 status = CURLE_COULDNT_CONNECT;
313 ldap_option = LDAP_OPT_X_TLS_HARD;
314 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
315 if(rc != LDAP_SUCCESS) {
316 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
317 ldap_err2string(rc));
318 status = CURLE_SSL_CERTPROBLEM;
322 rc = ldap_start_tls_s(server, NULL, NULL);
323 if(rc != LDAP_SUCCESS) {
324 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
325 ldap_err2string(rc));
326 status = CURLE_SSL_CERTPROBLEM;
331 /* we should probably never come up to here since configure
332 should check in first place if we can support LDAP SSL/TLS */
333 failf(data, "LDAP local: SSL/TLS not supported with this version "
334 "of the OpenLDAP toolkit\n");
335 status = CURLE_SSL_CERTPROBLEM;
339 #endif /* CURL_LDAP_USE_SSL */
342 server = ldap_init(conn->host.name, (int)conn->port);
344 failf(data, "LDAP local: Cannot connect to %s:%ld",
345 conn->host.name, conn->port);
346 status = CURLE_COULDNT_CONNECT;
351 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
354 rc = ldap_simple_bind_s(server,
355 conn->bits.user_passwd ? conn->user : NULL,
356 conn->bits.user_passwd ? conn->passwd : NULL);
357 if(!ldap_ssl && rc != 0) {
358 ldap_proto = LDAP_VERSION2;
359 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
360 rc = ldap_simple_bind_s(server,
361 conn->bits.user_passwd ? conn->user : NULL,
362 conn->bits.user_passwd ? conn->passwd : NULL);
365 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
366 status = CURLE_LDAP_CANNOT_BIND;
370 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
371 ludp->lud_filter, ludp->lud_attrs, 0, &result);
373 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
374 failf(data, "LDAP remote: %s", ldap_err2string(rc));
375 status = CURLE_LDAP_SEARCH_FAILED;
379 for(num = 0, entryIterator = ldap_first_entry(server, result);
381 entryIterator = ldap_next_entry(server, entryIterator), num++) {
382 BerElement *ber = NULL;
383 char *attribute; /*! suspicious that this isn't 'const' */
384 char *dn = ldap_get_dn(server, entryIterator);
387 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
388 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
389 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
391 dlsize += strlen(dn)+5;
393 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
395 attribute = ldap_next_attribute(server, entryIterator, ber)) {
396 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
399 for(i = 0; (vals[i] != NULL); i++) {
400 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
401 Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
402 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
403 dlsize += strlen(attribute)+3;
405 if((strlen(attribute) > 7) &&
408 (strlen((char *)attribute) - 7)) == 0)) {
409 /* Binary attribute, encode to base64. */
410 CURLcode error = Curl_base64_encode(data,
416 ldap_value_free_len(vals);
417 ldap_memfree(attribute);
425 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
427 dlsize += val_b64_sz;
431 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
433 dlsize += vals[i]->bv_len;
435 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
439 /* Free memory used to store values */
440 ldap_value_free_len(vals);
442 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
444 Curl_pgrsSetDownloadCounter(data, dlsize);
445 ldap_memfree(attribute);
454 ldap_msgfree(result);
455 LDAP_TRACE (("Received %d entries\n", num));
457 if(rc == LDAP_SIZELIMIT_EXCEEDED)
458 infof(data, "There are more than %d entries\n", num);
460 ldap_free_urldesc(ludp);
462 ldap_unbind_s(server);
463 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
465 ldapssl_client_deinit();
466 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
468 /* no data to transfer */
469 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
470 connclose(conn, "LDAP connection always disable re-use");
476 static void _ldap_trace (const char *fmt, ...)
478 static int do_trace = -1;
482 const char *env = getenv("CURL_TRACE");
483 do_trace = (env && strtol(env, NULL, 10) > 0);
488 va_start (args, fmt);
489 vfprintf (stderr, fmt, args);
494 #ifndef HAVE_LDAP_URL_PARSE
497 * Return scope-value for a scope-string.
499 static int str2scope (const char *p)
501 if(strequal(p, "one"))
502 return LDAP_SCOPE_ONELEVEL;
503 if(strequal(p, "onetree"))
504 return LDAP_SCOPE_ONELEVEL;
505 if(strequal(p, "base"))
506 return LDAP_SCOPE_BASE;
507 if(strequal(p, "sub"))
508 return LDAP_SCOPE_SUBTREE;
509 if(strequal( p, "subtree"))
510 return LDAP_SCOPE_SUBTREE;
515 * Split 'str' into strings separated by commas.
516 * Note: res[] points into 'str'.
518 static char **split_str (char *str)
520 char **res, *lasts, *s;
523 for(i = 2, s = strchr(str,','); s; i++)
526 res = calloc(i, sizeof(char*));
530 for(i = 0, s = strtok_r(str, ",", &lasts); s;
531 s = strtok_r(NULL, ",", &lasts), i++)
537 * Unescape the LDAP-URL components
539 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
543 if(ludp->lud_filter) {
544 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
545 if(!ludp->lud_filter)
549 for(i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
550 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i],
552 if(!ludp->lud_attrs[i])
554 ludp->lud_attrs_dups++;
558 char *dn = ludp->lud_dn;
559 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
562 ludp->lud_dn = new_dn;
570 * Break apart the pieces of an LDAP URL.
572 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
574 * <hostname> already known from 'conn->host.name'.
575 * <port> already known from 'conn->remote_port'.
576 * extract the rest from 'conn->data->state.path+1'. All fields are optional.
578 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
579 * yields ludp->lud_dn = "".
581 * Defined in RFC4516 section 2.
583 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
589 !conn->data->state.path ||
590 conn->data->state.path[0] != '/' ||
591 !checkprefix("LDAP", conn->data->change.url))
592 return LDAP_INVALID_SYNTAX;
594 ludp->lud_scope = LDAP_SCOPE_BASE;
595 ludp->lud_port = conn->remote_port;
596 ludp->lud_host = conn->host.name;
598 /* parse DN (Distinguished Name).
600 ludp->lud_dn = strdup(conn->data->state.path+1);
602 return LDAP_NO_MEMORY;
604 p = strchr(ludp->lud_dn, '?');
605 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
606 strlen(ludp->lud_dn), ludp->lud_dn));
613 /* parse attributes. skip "??".
619 if(*p && *p != '?') {
620 ludp->lud_attrs = split_str(p);
622 return LDAP_NO_MEMORY;
624 for(i = 0; ludp->lud_attrs[i]; i++)
625 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
632 /* parse scope. skip "??"
638 if(*p && *p != '?') {
639 ludp->lud_scope = str2scope(p);
640 if(ludp->lud_scope == -1) {
641 return LDAP_INVALID_SYNTAX;
643 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
656 return LDAP_INVALID_SYNTAX;
659 ludp->lud_filter = p;
660 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
663 if(!unescape_elements(conn->data, ludp))
664 return LDAP_NO_MEMORY;
668 static int _ldap_url_parse (const struct connectdata *conn,
671 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
676 return LDAP_NO_MEMORY;
678 rc = _ldap_url_parse2 (conn, ludp);
679 if(rc != LDAP_SUCCESS) {
680 _ldap_free_urldesc(ludp);
687 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
698 free(ludp->lud_filter);
700 if(ludp->lud_attrs) {
701 for(i = 0; i < ludp->lud_attrs_dups; i++)
702 free(ludp->lud_attrs[i]);
703 free(ludp->lud_attrs);
708 #endif /* !HAVE_LDAP_URL_PARSE */
709 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */