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_HYBRID /* If W$ definitions are needed. */
48 /* Remember we are NOT in a W$ compiler! */
54 #ifdef CURL_LDAP_WIN /* Use W$ LDAP implementation. */
56 # ifndef LDAP_VENDOR_NAME
57 # error Your Platform SDK is NOT sufficient for LDAP support! Update your Platform SDK, or disable LDAP support!
62 #define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
67 #if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
68 # include <ldap_ssl.h>
69 #endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
77 #include <curl/curl.h>
84 #include "curl_ldap.h"
85 #include "curl_memory.h"
86 #include "curl_base64.h"
89 #define _MPRINTF_REPLACE /* use our functions only */
90 #include <curl/mprintf.h>
94 #ifndef HAVE_LDAP_URL_PARSE
96 /* Use our own implementation. */
109 #define LDAPURLDesc CURL_LDAPURLDesc
111 static int _ldap_url_parse (const struct connectdata *conn,
113 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
115 #undef ldap_free_urldesc
116 #define ldap_free_urldesc _ldap_free_urldesc
120 #define LDAP_TRACE(x) do { \
121 _ldap_trace ("%u: ", __LINE__); \
125 static void _ldap_trace (const char *fmt, ...);
127 #define LDAP_TRACE(x) ((void)0)
131 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
134 * LDAP protocol handler.
137 const struct Curl_handler Curl_handler_ldap = {
139 ZERO_NULL, /* setup_connection */
140 Curl_ldap, /* do_it */
141 ZERO_NULL, /* done */
142 ZERO_NULL, /* do_more */
143 ZERO_NULL, /* connect_it */
144 ZERO_NULL, /* connecting */
145 ZERO_NULL, /* doing */
146 ZERO_NULL, /* proto_getsock */
147 ZERO_NULL, /* doing_getsock */
148 ZERO_NULL, /* perform_getsock */
149 ZERO_NULL, /* disconnect */
150 PORT_LDAP, /* defport */
151 PROT_LDAP /* protocol */
156 * LDAPS protocol handler.
159 const struct Curl_handler Curl_handler_ldaps = {
160 "LDAPS", /* scheme */
161 ZERO_NULL, /* setup_connection */
162 Curl_ldap, /* do_it */
163 ZERO_NULL, /* done */
164 ZERO_NULL, /* do_more */
165 ZERO_NULL, /* connect_it */
166 ZERO_NULL, /* connecting */
167 ZERO_NULL, /* doing */
168 ZERO_NULL, /* proto_getsock */
169 ZERO_NULL, /* doing_getsock */
170 ZERO_NULL, /* perform_getsock */
171 ZERO_NULL, /* disconnect */
172 PORT_LDAPS, /* defport */
173 PROT_LDAP | PROT_SSL /* protocol */
178 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
180 CURLcode status = CURLE_OK;
183 LDAPURLDesc *ludp = NULL;
184 LDAPMessage *result = NULL;
185 LDAPMessage *entryIterator;
187 struct SessionHandle *data=conn->data;
188 int ldap_proto = LDAP_VERSION3;
193 #ifdef LDAP_OPT_NETWORK_TIMEOUT
194 struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
197 *done = TRUE; /* unconditionally */
198 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
199 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
200 infof(data, "LDAP local: %s\n", data->change.url);
202 #ifdef HAVE_LDAP_URL_PARSE
203 rc = ldap_url_parse(data->change.url, &ludp);
205 rc = _ldap_url_parse(conn, &ludp);
208 failf(data, "LDAP local: %s", ldap_err2string(rc));
209 status = CURLE_LDAP_INVALID_URL;
213 /* Get the URL scheme ( either ldap or ldaps ) */
214 if(conn->protocol & PROT_SSL)
216 infof(data, "LDAP local: trying to establish %s connection\n",
217 ldap_ssl ? "encrypted" : "cleartext");
219 #ifdef LDAP_OPT_NETWORK_TIMEOUT
220 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
222 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
227 /* Win32 LDAP SDK doesnt support insecure mode without CA! */
228 server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
229 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
232 char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
233 #if defined(CURL_HAS_NOVELL_LDAPSDK)
234 rc = ldapssl_client_init(NULL, NULL);
235 if(rc != LDAP_SUCCESS) {
236 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
237 status = CURLE_SSL_CERTPROBLEM;
240 if(data->set.ssl.verifypeer) {
241 /* Novell SDK supports DER or BASE64 files. */
242 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
243 if((data->set.str[STRING_CERT_TYPE]) &&
244 (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
245 cert_type = LDAPSSL_CERT_FILETYPE_DER;
247 failf(data, "LDAP local: ERROR %s CA cert not set!",
248 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
249 status = CURLE_SSL_CERTPROBLEM;
252 infof(data, "LDAP local: using %s CA cert '%s'\n",
253 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
255 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
256 if(rc != LDAP_SUCCESS) {
257 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
258 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
259 ldap_err2string(rc));
260 status = CURLE_SSL_CERTPROBLEM;
263 ldap_option = LDAPSSL_VERIFY_SERVER;
265 ldap_option = LDAPSSL_VERIFY_NONE;
267 rc = ldapssl_set_verify_mode(ldap_option);
268 if(rc != LDAP_SUCCESS) {
269 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
270 ldap_err2string(rc));
271 status = CURLE_SSL_CERTPROBLEM;
274 server = ldapssl_init(conn->host.name, (int)conn->port, 1);
276 failf(data, "LDAP local: Cannot connect to %s:%hu",
277 conn->host.name, conn->port);
278 status = CURLE_COULDNT_CONNECT;
281 #elif defined(LDAP_OPT_X_TLS)
282 if(data->set.ssl.verifypeer) {
283 /* OpenLDAP SDK supports BASE64 files. */
284 if((data->set.str[STRING_CERT_TYPE]) &&
285 (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
286 failf(data, "LDAP local: ERROR OpenLDAP does only support PEM cert-type!");
287 status = CURLE_SSL_CERTPROBLEM;
291 failf(data, "LDAP local: ERROR PEM CA cert not set!");
292 status = CURLE_SSL_CERTPROBLEM;
295 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
296 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
297 if(rc != LDAP_SUCCESS) {
298 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
299 ldap_err2string(rc));
300 status = CURLE_SSL_CERTPROBLEM;
303 ldap_option = LDAP_OPT_X_TLS_DEMAND;
305 ldap_option = LDAP_OPT_X_TLS_NEVER;
307 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
308 if(rc != LDAP_SUCCESS) {
309 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
310 ldap_err2string(rc));
311 status = CURLE_SSL_CERTPROBLEM;
314 server = ldap_init(conn->host.name, (int)conn->port);
316 failf(data, "LDAP local: Cannot connect to %s:%hu",
317 conn->host.name, conn->port);
318 status = CURLE_COULDNT_CONNECT;
321 ldap_option = LDAP_OPT_X_TLS_HARD;
322 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
323 if(rc != LDAP_SUCCESS) {
324 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
325 ldap_err2string(rc));
326 status = CURLE_SSL_CERTPROBLEM;
330 rc = ldap_start_tls_s(server, NULL, NULL);
331 if(rc != LDAP_SUCCESS) {
332 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
333 ldap_err2string(rc));
334 status = CURLE_SSL_CERTPROBLEM;
339 /* we should probably never come up to here since configure
340 should check in first place if we can support LDAP SSL/TLS */
341 failf(data, "LDAP local: SSL/TLS not supported with this version "
342 "of the OpenLDAP toolkit\n");
343 status = CURLE_SSL_CERTPROBLEM;
347 #endif /* CURL_LDAP_USE_SSL */
349 server = ldap_init(conn->host.name, (int)conn->port);
351 failf(data, "LDAP local: Cannot connect to %s:%hu",
352 conn->host.name, conn->port);
353 status = CURLE_COULDNT_CONNECT;
358 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
361 rc = ldap_simple_bind_s(server,
362 conn->bits.user_passwd ? conn->user : NULL,
363 conn->bits.user_passwd ? conn->passwd : NULL);
364 if(!ldap_ssl && rc != 0) {
365 ldap_proto = LDAP_VERSION2;
366 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
367 rc = ldap_simple_bind_s(server,
368 conn->bits.user_passwd ? conn->user : NULL,
369 conn->bits.user_passwd ? conn->passwd : NULL);
372 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
373 status = CURLE_LDAP_CANNOT_BIND;
377 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
378 ludp->lud_filter, ludp->lud_attrs, 0, &result);
380 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
381 failf(data, "LDAP remote: %s", ldap_err2string(rc));
382 status = CURLE_LDAP_SEARCH_FAILED;
386 for(num = 0, entryIterator = ldap_first_entry(server, result);
388 entryIterator = ldap_next_entry(server, entryIterator), num++)
390 BerElement *ber = NULL;
391 char *attribute; /*! suspicious that this isn't 'const' */
392 char *dn = ldap_get_dn(server, entryIterator);
395 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
396 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
397 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
399 dlsize += strlen(dn)+5;
401 for (attribute = ldap_first_attribute(server, entryIterator, &ber);
403 attribute = ldap_next_attribute(server, entryIterator, ber))
405 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
409 for (i = 0; (vals[i] != NULL); i++)
411 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
412 Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
413 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
414 dlsize += strlen(attribute)+3;
416 if((strlen(attribute) > 7) &&
419 (strlen((char *)attribute) - 7)) == 0)) {
420 /* Binary attribute, encode to base64. */
421 val_b64_sz = Curl_base64_encode(data,
426 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
428 dlsize += val_b64_sz;
432 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
434 dlsize += vals[i]->bv_len;
436 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
440 /* Free memory used to store values */
441 ldap_value_free_len(vals);
443 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
445 Curl_pgrsSetDownloadCounter(data, dlsize);
446 ldap_memfree(attribute);
455 ldap_msgfree(result);
456 LDAP_TRACE (("Received %d entries\n", num));
458 if(rc == LDAP_SIZELIMIT_EXCEEDED)
459 infof(data, "There are more than %d entries\n", num);
461 ldap_free_urldesc(ludp);
463 ldap_unbind_s(server);
464 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
466 ldapssl_client_deinit();
467 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
469 /* no data to transfer */
470 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
471 conn->bits.close = TRUE;
477 static void _ldap_trace (const char *fmt, ...)
479 static int do_trace = -1;
483 const char *env = getenv("CURL_TRACE");
484 do_trace = (env && atoi(env) > 0);
489 va_start (args, fmt);
490 vfprintf (stderr, fmt, args);
495 #ifndef HAVE_LDAP_URL_PARSE
498 * Return scope-value for a scope-string.
500 static int str2scope (const char *p)
502 if(strequal(p, "one"))
503 return LDAP_SCOPE_ONELEVEL;
504 if(strequal(p, "onetree"))
505 return LDAP_SCOPE_ONELEVEL;
506 if(strequal(p, "base"))
507 return LDAP_SCOPE_BASE;
508 if(strequal(p, "sub"))
509 return LDAP_SCOPE_SUBTREE;
510 if(strequal( p, "subtree"))
511 return LDAP_SCOPE_SUBTREE;
516 * Split 'str' into strings separated by commas.
517 * Note: res[] points into 'str'.
519 static char **split_str (char *str)
521 char **res, *lasts, *s;
524 for (i = 2, s = strchr(str,','); s; i++)
527 res = calloc(i, sizeof(char*));
531 for (i = 0, s = strtok_r(str, ",", &lasts); s;
532 s = strtok_r(NULL, ",", &lasts), i++)
538 * Unescape the LDAP-URL components
540 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
544 if(ludp->lud_filter) {
545 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
546 if(!ludp->lud_filter)
550 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
551 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
552 if(!ludp->lud_attrs[i])
556 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
557 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
558 if(!ludp->lud_exts[i])
563 char *dn = ludp->lud_dn;
564 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
567 ludp->lud_dn = new_dn;
575 * Break apart the pieces of an LDAP URL.
577 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
579 * <hostname> already known from 'conn->host.name'.
580 * <port> already known from 'conn->remote_port'.
581 * extract the rest from 'conn->data->state.path+1'. All fields are optional.
583 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
584 * yields ludp->lud_dn = "".
586 * Defined in RFC4516 section 2.
588 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
594 !conn->data->state.path ||
595 conn->data->state.path[0] != '/' ||
596 !checkprefix("LDAP", conn->data->change.url))
597 return LDAP_INVALID_SYNTAX;
599 ludp->lud_scope = LDAP_SCOPE_BASE;
600 ludp->lud_port = conn->remote_port;
601 ludp->lud_host = conn->host.name;
603 /* parse DN (Distinguished Name).
605 ludp->lud_dn = strdup(conn->data->state.path+1);
607 return LDAP_NO_MEMORY;
609 p = strchr(ludp->lud_dn, '?');
610 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
611 strlen(ludp->lud_dn), ludp->lud_dn));
618 /* parse attributes. skip "??".
624 if(*p && *p != '?') {
625 ludp->lud_attrs = split_str(p);
627 return LDAP_NO_MEMORY;
629 for (i = 0; ludp->lud_attrs[i]; i++)
630 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
637 /* parse scope. skip "??"
643 if(*p && *p != '?') {
644 ludp->lud_scope = str2scope(p);
645 if(ludp->lud_scope == -1)
646 return LDAP_INVALID_SYNTAX;
647 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
660 return LDAP_INVALID_SYNTAX;
662 ludp->lud_filter = p;
663 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
671 ludp->lud_exts = split_str(p);
673 return LDAP_NO_MEMORY;
675 for (i = 0; ludp->lud_exts[i]; i++)
676 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
679 if(!unescape_elements(conn->data, ludp))
680 return LDAP_NO_MEMORY;
684 static int _ldap_url_parse (const struct connectdata *conn,
687 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
692 return LDAP_NO_MEMORY;
694 rc = _ldap_url_parse2 (conn, ludp);
695 if(rc != LDAP_SUCCESS) {
696 _ldap_free_urldesc(ludp);
703 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
714 free(ludp->lud_filter);
716 if(ludp->lud_attrs) {
717 for (i = 0; ludp->lud_attrs[i]; i++)
718 free(ludp->lud_attrs[i]);
719 free(ludp->lud_attrs);
723 for (i = 0; ludp->lud_exts[i]; i++)
724 free(ludp->lud_exts[i]);
725 free(ludp->lud_exts);
729 #endif /* !HAVE_LDAP_URL_PARSE */
730 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */