1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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"
70 #define _MPRINTF_REPLACE /* use our functions only */
71 #include <curl/mprintf.h>
75 #ifndef HAVE_LDAP_URL_PARSE
77 /* Use our own implementation. */
90 #define LDAPURLDesc CURL_LDAPURLDesc
92 static int _ldap_url_parse (const struct connectdata *conn,
94 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
96 #undef ldap_free_urldesc
97 #define ldap_free_urldesc _ldap_free_urldesc
101 #define LDAP_TRACE(x) do { \
102 _ldap_trace ("%u: ", __LINE__); \
106 static void _ldap_trace (const char *fmt, ...);
108 #define LDAP_TRACE(x) Curl_nop_stmt
112 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
115 * LDAP protocol handler.
118 const struct Curl_handler Curl_handler_ldap = {
120 ZERO_NULL, /* setup_connection */
121 Curl_ldap, /* do_it */
122 ZERO_NULL, /* done */
123 ZERO_NULL, /* do_more */
124 ZERO_NULL, /* connect_it */
125 ZERO_NULL, /* connecting */
126 ZERO_NULL, /* doing */
127 ZERO_NULL, /* proto_getsock */
128 ZERO_NULL, /* doing_getsock */
129 ZERO_NULL, /* domore_getsock */
130 ZERO_NULL, /* perform_getsock */
131 ZERO_NULL, /* disconnect */
132 ZERO_NULL, /* readwrite */
133 PORT_LDAP, /* defport */
134 CURLPROTO_LDAP, /* protocol */
135 PROTOPT_NONE /* flags */
140 * LDAPS protocol handler.
143 const struct Curl_handler Curl_handler_ldaps = {
144 "LDAPS", /* scheme */
145 ZERO_NULL, /* setup_connection */
146 Curl_ldap, /* do_it */
147 ZERO_NULL, /* done */
148 ZERO_NULL, /* do_more */
149 ZERO_NULL, /* connect_it */
150 ZERO_NULL, /* connecting */
151 ZERO_NULL, /* doing */
152 ZERO_NULL, /* proto_getsock */
153 ZERO_NULL, /* doing_getsock */
154 ZERO_NULL, /* domore_getsock */
155 ZERO_NULL, /* perform_getsock */
156 ZERO_NULL, /* disconnect */
157 ZERO_NULL, /* readwrite */
158 PORT_LDAPS, /* defport */
159 CURLPROTO_LDAP | CURLPROTO_LDAPS, /* protocol */
160 PROTOPT_SSL /* flags */
165 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
167 CURLcode status = CURLE_OK;
170 LDAPURLDesc *ludp = NULL;
171 LDAPMessage *result = NULL;
172 LDAPMessage *entryIterator;
174 struct SessionHandle *data=conn->data;
175 int ldap_proto = LDAP_VERSION3;
177 char *val_b64 = NULL;
178 size_t val_b64_sz = 0;
179 curl_off_t dlsize = 0;
180 #ifdef LDAP_OPT_NETWORK_TIMEOUT
181 struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
184 *done = TRUE; /* unconditionally */
185 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
186 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
187 infof(data, "LDAP local: %s\n", data->change.url);
189 #ifdef HAVE_LDAP_URL_PARSE
190 rc = ldap_url_parse(data->change.url, &ludp);
192 rc = _ldap_url_parse(conn, &ludp);
195 failf(data, "LDAP local: %s", ldap_err2string(rc));
196 status = CURLE_LDAP_INVALID_URL;
200 /* Get the URL scheme ( either ldap or ldaps ) */
201 if(conn->given->flags & PROTOPT_SSL)
203 infof(data, "LDAP local: trying to establish %s connection\n",
204 ldap_ssl ? "encrypted" : "cleartext");
206 #ifdef LDAP_OPT_NETWORK_TIMEOUT
207 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
209 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
214 /* Win32 LDAP SDK doesn't support insecure mode without CA! */
215 server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
216 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
219 char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
220 #if defined(CURL_HAS_NOVELL_LDAPSDK)
221 rc = ldapssl_client_init(NULL, NULL);
222 if(rc != LDAP_SUCCESS) {
223 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
224 status = CURLE_SSL_CERTPROBLEM;
227 if(data->set.ssl.verifypeer) {
228 /* Novell SDK supports DER or BASE64 files. */
229 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
230 if((data->set.str[STRING_CERT_TYPE]) &&
231 (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
232 cert_type = LDAPSSL_CERT_FILETYPE_DER;
234 failf(data, "LDAP local: ERROR %s CA cert not set!",
235 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
236 status = CURLE_SSL_CERTPROBLEM;
239 infof(data, "LDAP local: using %s CA cert '%s'\n",
240 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
242 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
243 if(rc != LDAP_SUCCESS) {
244 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
245 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
246 ldap_err2string(rc));
247 status = CURLE_SSL_CERTPROBLEM;
250 ldap_option = LDAPSSL_VERIFY_SERVER;
253 ldap_option = LDAPSSL_VERIFY_NONE;
254 rc = ldapssl_set_verify_mode(ldap_option);
255 if(rc != LDAP_SUCCESS) {
256 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
257 ldap_err2string(rc));
258 status = CURLE_SSL_CERTPROBLEM;
261 server = ldapssl_init(conn->host.name, (int)conn->port, 1);
263 failf(data, "LDAP local: Cannot connect to %s:%ld",
264 conn->host.name, conn->port);
265 status = CURLE_COULDNT_CONNECT;
268 #elif defined(LDAP_OPT_X_TLS)
269 if(data->set.ssl.verifypeer) {
270 /* OpenLDAP SDK supports BASE64 files. */
271 if((data->set.str[STRING_CERT_TYPE]) &&
272 (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
273 failf(data, "LDAP local: ERROR OpenLDAP only supports PEM cert-type!");
274 status = CURLE_SSL_CERTPROBLEM;
278 failf(data, "LDAP local: ERROR PEM CA cert not set!");
279 status = CURLE_SSL_CERTPROBLEM;
282 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
283 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
284 if(rc != LDAP_SUCCESS) {
285 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
286 ldap_err2string(rc));
287 status = CURLE_SSL_CERTPROBLEM;
290 ldap_option = LDAP_OPT_X_TLS_DEMAND;
293 ldap_option = LDAP_OPT_X_TLS_NEVER;
295 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
296 if(rc != LDAP_SUCCESS) {
297 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
298 ldap_err2string(rc));
299 status = CURLE_SSL_CERTPROBLEM;
302 server = ldap_init(conn->host.name, (int)conn->port);
304 failf(data, "LDAP local: Cannot connect to %s:%ld",
305 conn->host.name, conn->port);
306 status = CURLE_COULDNT_CONNECT;
309 ldap_option = LDAP_OPT_X_TLS_HARD;
310 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
311 if(rc != LDAP_SUCCESS) {
312 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
313 ldap_err2string(rc));
314 status = CURLE_SSL_CERTPROBLEM;
318 rc = ldap_start_tls_s(server, NULL, NULL);
319 if(rc != LDAP_SUCCESS) {
320 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
321 ldap_err2string(rc));
322 status = CURLE_SSL_CERTPROBLEM;
327 /* we should probably never come up to here since configure
328 should check in first place if we can support LDAP SSL/TLS */
329 failf(data, "LDAP local: SSL/TLS not supported with this version "
330 "of the OpenLDAP toolkit\n");
331 status = CURLE_SSL_CERTPROBLEM;
335 #endif /* CURL_LDAP_USE_SSL */
338 server = ldap_init(conn->host.name, (int)conn->port);
340 failf(data, "LDAP local: Cannot connect to %s:%ld",
341 conn->host.name, conn->port);
342 status = CURLE_COULDNT_CONNECT;
347 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
350 rc = ldap_simple_bind_s(server,
351 conn->bits.user_passwd ? conn->user : NULL,
352 conn->bits.user_passwd ? conn->passwd : NULL);
353 if(!ldap_ssl && rc != 0) {
354 ldap_proto = LDAP_VERSION2;
355 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
356 rc = ldap_simple_bind_s(server,
357 conn->bits.user_passwd ? conn->user : NULL,
358 conn->bits.user_passwd ? conn->passwd : NULL);
361 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
362 status = CURLE_LDAP_CANNOT_BIND;
366 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
367 ludp->lud_filter, ludp->lud_attrs, 0, &result);
369 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
370 failf(data, "LDAP remote: %s", ldap_err2string(rc));
371 status = CURLE_LDAP_SEARCH_FAILED;
375 for(num = 0, entryIterator = ldap_first_entry(server, result);
377 entryIterator = ldap_next_entry(server, entryIterator), num++) {
378 BerElement *ber = NULL;
379 char *attribute; /*! suspicious that this isn't 'const' */
380 char *dn = ldap_get_dn(server, entryIterator);
383 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
384 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
385 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
387 dlsize += strlen(dn)+5;
389 for(attribute = ldap_first_attribute(server, entryIterator, &ber);
391 attribute = ldap_next_attribute(server, entryIterator, ber)) {
392 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
395 for(i = 0; (vals[i] != NULL); i++) {
396 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
397 Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
398 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
399 dlsize += strlen(attribute)+3;
401 if((strlen(attribute) > 7) &&
404 (strlen((char *)attribute) - 7)) == 0)) {
405 /* Binary attribute, encode to base64. */
406 CURLcode error = Curl_base64_encode(data,
412 ldap_value_free_len(vals);
413 ldap_memfree(attribute);
421 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
423 dlsize += val_b64_sz;
427 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
429 dlsize += vals[i]->bv_len;
431 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
435 /* Free memory used to store values */
436 ldap_value_free_len(vals);
438 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
440 Curl_pgrsSetDownloadCounter(data, dlsize);
441 ldap_memfree(attribute);
450 ldap_msgfree(result);
451 LDAP_TRACE (("Received %d entries\n", num));
453 if(rc == LDAP_SIZELIMIT_EXCEEDED)
454 infof(data, "There are more than %d entries\n", num);
456 ldap_free_urldesc(ludp);
458 ldap_unbind_s(server);
459 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
461 ldapssl_client_deinit();
462 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
464 /* no data to transfer */
465 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
466 conn->bits.close = TRUE;
472 static void _ldap_trace (const char *fmt, ...)
474 static int do_trace = -1;
478 const char *env = getenv("CURL_TRACE");
479 do_trace = (env && strtol(env, NULL, 10) > 0);
484 va_start (args, fmt);
485 vfprintf (stderr, fmt, args);
490 #ifndef HAVE_LDAP_URL_PARSE
493 * Return scope-value for a scope-string.
495 static int str2scope (const char *p)
497 if(strequal(p, "one"))
498 return LDAP_SCOPE_ONELEVEL;
499 if(strequal(p, "onetree"))
500 return LDAP_SCOPE_ONELEVEL;
501 if(strequal(p, "base"))
502 return LDAP_SCOPE_BASE;
503 if(strequal(p, "sub"))
504 return LDAP_SCOPE_SUBTREE;
505 if(strequal( p, "subtree"))
506 return LDAP_SCOPE_SUBTREE;
511 * Split 'str' into strings separated by commas.
512 * Note: res[] points into 'str'.
514 static char **split_str (char *str)
516 char **res, *lasts, *s;
519 for(i = 2, s = strchr(str,','); s; i++)
522 res = calloc(i, sizeof(char*));
526 for(i = 0, s = strtok_r(str, ",", &lasts); s;
527 s = strtok_r(NULL, ",", &lasts), i++)
533 * Unescape the LDAP-URL components
535 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
539 if(ludp->lud_filter) {
540 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
541 if(!ludp->lud_filter)
545 for(i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
546 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
547 if(!ludp->lud_attrs[i])
551 for(i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
552 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
553 if(!ludp->lud_exts[i])
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;
642 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
655 return LDAP_INVALID_SYNTAX;
657 ludp->lud_filter = p;
658 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
666 ludp->lud_exts = split_str(p);
668 return LDAP_NO_MEMORY;
670 for(i = 0; ludp->lud_exts[i]; i++)
671 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
674 if(!unescape_elements(conn->data, ludp))
675 return LDAP_NO_MEMORY;
679 static int _ldap_url_parse (const struct connectdata *conn,
682 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
687 return LDAP_NO_MEMORY;
689 rc = _ldap_url_parse2 (conn, ludp);
690 if(rc != LDAP_SUCCESS) {
691 _ldap_free_urldesc(ludp);
698 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
709 free(ludp->lud_filter);
711 if(ludp->lud_attrs) {
712 for(i = 0; ludp->lud_attrs[i]; i++)
713 free(ludp->lud_attrs[i]);
714 free(ludp->lud_attrs);
718 for(i = 0; ludp->lud_exts[i]; i++)
719 free(ludp->lud_exts[i]);
720 free(ludp->lud_exts);
724 #endif /* !HAVE_LDAP_URL_PARSE */
725 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */