1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 ***************************************************************************/
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 /* -- WIN32 approved -- */
46 #ifdef CURL_LDAP_WIN /* Use Windows LDAP implementation. */
48 # ifndef LDAP_VENDOR_NAME
49 # error Your Platform SDK is NOT sufficient for LDAP support! Update your Platform SDK, or disable LDAP support!
54 # define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
59 # if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
60 # include <ldap_ssl.h>
61 # endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
69 #include <curl/curl.h>
76 #include "curl_ldap.h"
77 #include "curl_memory.h"
78 #include "curl_base64.h"
81 #define _MPRINTF_REPLACE /* use our functions only */
82 #include <curl/mprintf.h>
86 #ifndef HAVE_LDAP_URL_PARSE
88 /* Use our own implementation. */
101 #define LDAPURLDesc CURL_LDAPURLDesc
103 static int _ldap_url_parse (const struct connectdata *conn,
105 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
107 #undef ldap_free_urldesc
108 #define ldap_free_urldesc _ldap_free_urldesc
112 #define LDAP_TRACE(x) do { \
113 _ldap_trace ("%u: ", __LINE__); \
117 static void _ldap_trace (const char *fmt, ...);
119 #define LDAP_TRACE(x) ((void)0)
123 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
126 * LDAP protocol handler.
129 const struct Curl_handler Curl_handler_ldap = {
131 ZERO_NULL, /* setup_connection */
132 Curl_ldap, /* do_it */
133 ZERO_NULL, /* done */
134 ZERO_NULL, /* do_more */
135 ZERO_NULL, /* connect_it */
136 ZERO_NULL, /* connecting */
137 ZERO_NULL, /* doing */
138 ZERO_NULL, /* proto_getsock */
139 ZERO_NULL, /* doing_getsock */
140 ZERO_NULL, /* perform_getsock */
141 ZERO_NULL, /* disconnect */
142 PORT_LDAP, /* defport */
143 PROT_LDAP /* protocol */
148 * LDAPS protocol handler.
151 const struct Curl_handler Curl_handler_ldaps = {
152 "LDAPS", /* scheme */
153 ZERO_NULL, /* setup_connection */
154 Curl_ldap, /* do_it */
155 ZERO_NULL, /* done */
156 ZERO_NULL, /* do_more */
157 ZERO_NULL, /* connect_it */
158 ZERO_NULL, /* connecting */
159 ZERO_NULL, /* doing */
160 ZERO_NULL, /* proto_getsock */
161 ZERO_NULL, /* doing_getsock */
162 ZERO_NULL, /* perform_getsock */
163 ZERO_NULL, /* disconnect */
164 PORT_LDAPS, /* defport */
165 PROT_LDAP | PROT_SSL /* protocol */
170 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
172 CURLcode status = CURLE_OK;
175 LDAPURLDesc *ludp = NULL;
176 LDAPMessage *result = NULL;
177 LDAPMessage *entryIterator;
179 struct SessionHandle *data=conn->data;
180 int ldap_proto = LDAP_VERSION3;
185 #ifdef LDAP_OPT_NETWORK_TIMEOUT
186 struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
189 *done = TRUE; /* unconditionally */
190 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
191 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
192 infof(data, "LDAP local: %s\n", data->change.url);
194 #ifdef HAVE_LDAP_URL_PARSE
195 rc = ldap_url_parse(data->change.url, &ludp);
197 rc = _ldap_url_parse(conn, &ludp);
200 failf(data, "LDAP local: %s", ldap_err2string(rc));
201 status = CURLE_LDAP_INVALID_URL;
205 /* Get the URL scheme ( either ldap or ldaps ) */
206 if(conn->protocol & PROT_SSL)
208 infof(data, "LDAP local: trying to establish %s connection\n",
209 ldap_ssl ? "encrypted" : "cleartext");
211 #ifdef LDAP_OPT_NETWORK_TIMEOUT
212 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
214 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
219 /* Win32 LDAP SDK doesnt support insecure mode without CA! */
220 server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
221 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
224 char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
225 #if defined(CURL_HAS_NOVELL_LDAPSDK)
226 rc = ldapssl_client_init(NULL, NULL);
227 if(rc != LDAP_SUCCESS) {
228 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
229 status = CURLE_SSL_CERTPROBLEM;
232 if(data->set.ssl.verifypeer) {
233 /* Novell SDK supports DER or BASE64 files. */
234 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
235 if((data->set.str[STRING_CERT_TYPE]) &&
236 (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
237 cert_type = LDAPSSL_CERT_FILETYPE_DER;
239 failf(data, "LDAP local: ERROR %s CA cert not set!",
240 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
241 status = CURLE_SSL_CERTPROBLEM;
244 infof(data, "LDAP local: using %s CA cert '%s'\n",
245 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
247 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
248 if(rc != LDAP_SUCCESS) {
249 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
250 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
251 ldap_err2string(rc));
252 status = CURLE_SSL_CERTPROBLEM;
255 ldap_option = LDAPSSL_VERIFY_SERVER;
257 ldap_option = LDAPSSL_VERIFY_NONE;
259 rc = ldapssl_set_verify_mode(ldap_option);
260 if(rc != LDAP_SUCCESS) {
261 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
262 ldap_err2string(rc));
263 status = CURLE_SSL_CERTPROBLEM;
266 server = ldapssl_init(conn->host.name, (int)conn->port, 1);
268 failf(data, "LDAP local: Cannot connect to %s:%hu",
269 conn->host.name, conn->port);
270 status = CURLE_COULDNT_CONNECT;
273 #elif defined(LDAP_OPT_X_TLS)
274 if(data->set.ssl.verifypeer) {
275 /* OpenLDAP SDK supports BASE64 files. */
276 if((data->set.str[STRING_CERT_TYPE]) &&
277 (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
278 failf(data, "LDAP local: ERROR OpenLDAP does only support PEM cert-type!");
279 status = CURLE_SSL_CERTPROBLEM;
283 failf(data, "LDAP local: ERROR PEM CA cert not set!");
284 status = CURLE_SSL_CERTPROBLEM;
287 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
288 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
289 if(rc != LDAP_SUCCESS) {
290 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
291 ldap_err2string(rc));
292 status = CURLE_SSL_CERTPROBLEM;
295 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:%hu",
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 */
341 server = ldap_init(conn->host.name, (int)conn->port);
343 failf(data, "LDAP local: Cannot connect to %s:%hu",
344 conn->host.name, conn->port);
345 status = CURLE_COULDNT_CONNECT;
350 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
353 rc = ldap_simple_bind_s(server,
354 conn->bits.user_passwd ? conn->user : NULL,
355 conn->bits.user_passwd ? conn->passwd : NULL);
356 if(!ldap_ssl && rc != 0) {
357 ldap_proto = LDAP_VERSION2;
358 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
359 rc = ldap_simple_bind_s(server,
360 conn->bits.user_passwd ? conn->user : NULL,
361 conn->bits.user_passwd ? conn->passwd : NULL);
364 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
365 status = CURLE_LDAP_CANNOT_BIND;
369 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
370 ludp->lud_filter, ludp->lud_attrs, 0, &result);
372 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
373 failf(data, "LDAP remote: %s", ldap_err2string(rc));
374 status = CURLE_LDAP_SEARCH_FAILED;
378 for(num = 0, entryIterator = ldap_first_entry(server, result);
380 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))
397 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
401 for (i = 0; (vals[i] != NULL); i++)
403 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
404 Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
405 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
406 dlsize += strlen(attribute)+3;
408 if((strlen(attribute) > 7) &&
411 (strlen((char *)attribute) - 7)) == 0)) {
412 /* Binary attribute, encode to base64. */
413 val_b64_sz = Curl_base64_encode(data,
418 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
420 dlsize += val_b64_sz;
424 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
426 dlsize += vals[i]->bv_len;
428 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
432 /* Free memory used to store values */
433 ldap_value_free_len(vals);
435 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
437 Curl_pgrsSetDownloadCounter(data, dlsize);
438 ldap_memfree(attribute);
447 ldap_msgfree(result);
448 LDAP_TRACE (("Received %d entries\n", num));
450 if(rc == LDAP_SIZELIMIT_EXCEEDED)
451 infof(data, "There are more than %d entries\n", num);
453 ldap_free_urldesc(ludp);
455 ldap_unbind_s(server);
456 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
458 ldapssl_client_deinit();
459 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
461 /* no data to transfer */
462 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
463 conn->bits.close = TRUE;
469 static void _ldap_trace (const char *fmt, ...)
471 static int do_trace = -1;
475 const char *env = getenv("CURL_TRACE");
476 do_trace = (env && strtol(env, NULL, 10) > 0);
481 va_start (args, fmt);
482 vfprintf (stderr, fmt, args);
487 #ifndef HAVE_LDAP_URL_PARSE
490 * Return scope-value for a scope-string.
492 static int str2scope (const char *p)
494 if(strequal(p, "one"))
495 return LDAP_SCOPE_ONELEVEL;
496 if(strequal(p, "onetree"))
497 return LDAP_SCOPE_ONELEVEL;
498 if(strequal(p, "base"))
499 return LDAP_SCOPE_BASE;
500 if(strequal(p, "sub"))
501 return LDAP_SCOPE_SUBTREE;
502 if(strequal( p, "subtree"))
503 return LDAP_SCOPE_SUBTREE;
508 * Split 'str' into strings separated by commas.
509 * Note: res[] points into 'str'.
511 static char **split_str (char *str)
513 char **res, *lasts, *s;
516 for (i = 2, s = strchr(str,','); s; i++)
519 res = calloc(i, sizeof(char*));
523 for (i = 0, s = strtok_r(str, ",", &lasts); s;
524 s = strtok_r(NULL, ",", &lasts), i++)
530 * Unescape the LDAP-URL components
532 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
536 if(ludp->lud_filter) {
537 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
538 if(!ludp->lud_filter)
542 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
543 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
544 if(!ludp->lud_attrs[i])
548 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
549 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
550 if(!ludp->lud_exts[i])
555 char *dn = ludp->lud_dn;
556 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
559 ludp->lud_dn = new_dn;
567 * Break apart the pieces of an LDAP URL.
569 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
571 * <hostname> already known from 'conn->host.name'.
572 * <port> already known from 'conn->remote_port'.
573 * extract the rest from 'conn->data->state.path+1'. All fields are optional.
575 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
576 * yields ludp->lud_dn = "".
578 * Defined in RFC4516 section 2.
580 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
586 !conn->data->state.path ||
587 conn->data->state.path[0] != '/' ||
588 !checkprefix("LDAP", conn->data->change.url))
589 return LDAP_INVALID_SYNTAX;
591 ludp->lud_scope = LDAP_SCOPE_BASE;
592 ludp->lud_port = conn->remote_port;
593 ludp->lud_host = conn->host.name;
595 /* parse DN (Distinguished Name).
597 ludp->lud_dn = strdup(conn->data->state.path+1);
599 return LDAP_NO_MEMORY;
601 p = strchr(ludp->lud_dn, '?');
602 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
603 strlen(ludp->lud_dn), ludp->lud_dn));
610 /* parse attributes. skip "??".
616 if(*p && *p != '?') {
617 ludp->lud_attrs = split_str(p);
619 return LDAP_NO_MEMORY;
621 for (i = 0; ludp->lud_attrs[i]; i++)
622 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
629 /* parse scope. skip "??"
635 if(*p && *p != '?') {
636 ludp->lud_scope = str2scope(p);
637 if(ludp->lud_scope == -1)
638 return LDAP_INVALID_SYNTAX;
639 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
652 return LDAP_INVALID_SYNTAX;
654 ludp->lud_filter = p;
655 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
663 ludp->lud_exts = split_str(p);
665 return LDAP_NO_MEMORY;
667 for (i = 0; ludp->lud_exts[i]; i++)
668 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
671 if(!unescape_elements(conn->data, ludp))
672 return LDAP_NO_MEMORY;
676 static int _ldap_url_parse (const struct connectdata *conn,
679 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
684 return LDAP_NO_MEMORY;
686 rc = _ldap_url_parse2 (conn, ludp);
687 if(rc != LDAP_SUCCESS) {
688 _ldap_free_urldesc(ludp);
695 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
706 free(ludp->lud_filter);
708 if(ludp->lud_attrs) {
709 for (i = 0; ludp->lud_attrs[i]; i++)
710 free(ludp->lud_attrs[i]);
711 free(ludp->lud_attrs);
715 for (i = 0; ludp->lud_exts[i]; i++)
716 free(ludp->lud_exts[i]);
717 free(ludp->lud_exts);
721 #endif /* !HAVE_LDAP_URL_PARSE */
722 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */