1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 CURLPROTO_LDAP, /* protocol */
144 PROTOPTS_NONE /* flags */
149 * LDAPS protocol handler.
152 const struct Curl_handler Curl_handler_ldaps = {
153 "LDAPS", /* scheme */
154 ZERO_NULL, /* setup_connection */
155 Curl_ldap, /* do_it */
156 ZERO_NULL, /* done */
157 ZERO_NULL, /* do_more */
158 ZERO_NULL, /* connect_it */
159 ZERO_NULL, /* connecting */
160 ZERO_NULL, /* doing */
161 ZERO_NULL, /* proto_getsock */
162 ZERO_NULL, /* doing_getsock */
163 ZERO_NULL, /* perform_getsock */
164 ZERO_NULL, /* disconnect */
165 PORT_LDAPS, /* defport */
166 CURLPROTO_LDAP | CURLPROTO_LDAPS, /* protocol */
167 PROTOPT_SSL /* flags */
172 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
174 CURLcode status = CURLE_OK;
177 LDAPURLDesc *ludp = NULL;
178 LDAPMessage *result = NULL;
179 LDAPMessage *entryIterator;
181 struct SessionHandle *data=conn->data;
182 int ldap_proto = LDAP_VERSION3;
187 #ifdef LDAP_OPT_NETWORK_TIMEOUT
188 struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
191 *done = TRUE; /* unconditionally */
192 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
193 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
194 infof(data, "LDAP local: %s\n", data->change.url);
196 #ifdef HAVE_LDAP_URL_PARSE
197 rc = ldap_url_parse(data->change.url, &ludp);
199 rc = _ldap_url_parse(conn, &ludp);
202 failf(data, "LDAP local: %s", ldap_err2string(rc));
203 status = CURLE_LDAP_INVALID_URL;
207 /* Get the URL scheme ( either ldap or ldaps ) */
208 if(conn->given->protocol & CURLPROTO_LDAPS)
210 infof(data, "LDAP local: trying to establish %s connection\n",
211 ldap_ssl ? "encrypted" : "cleartext");
213 #ifdef LDAP_OPT_NETWORK_TIMEOUT
214 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
216 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
221 /* Win32 LDAP SDK doesnt support insecure mode without CA! */
222 server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
223 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
226 char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
227 #if defined(CURL_HAS_NOVELL_LDAPSDK)
228 rc = ldapssl_client_init(NULL, NULL);
229 if(rc != LDAP_SUCCESS) {
230 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
231 status = CURLE_SSL_CERTPROBLEM;
234 if(data->set.ssl.verifypeer) {
235 /* Novell SDK supports DER or BASE64 files. */
236 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
237 if((data->set.str[STRING_CERT_TYPE]) &&
238 (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
239 cert_type = LDAPSSL_CERT_FILETYPE_DER;
241 failf(data, "LDAP local: ERROR %s CA cert not set!",
242 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
243 status = CURLE_SSL_CERTPROBLEM;
246 infof(data, "LDAP local: using %s CA cert '%s'\n",
247 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
249 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
250 if(rc != LDAP_SUCCESS) {
251 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
252 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
253 ldap_err2string(rc));
254 status = CURLE_SSL_CERTPROBLEM;
257 ldap_option = LDAPSSL_VERIFY_SERVER;
259 ldap_option = LDAPSSL_VERIFY_NONE;
261 rc = ldapssl_set_verify_mode(ldap_option);
262 if(rc != LDAP_SUCCESS) {
263 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
264 ldap_err2string(rc));
265 status = CURLE_SSL_CERTPROBLEM;
268 server = ldapssl_init(conn->host.name, (int)conn->port, 1);
270 failf(data, "LDAP local: Cannot connect to %s:%hu",
271 conn->host.name, conn->port);
272 status = CURLE_COULDNT_CONNECT;
275 #elif defined(LDAP_OPT_X_TLS)
276 if(data->set.ssl.verifypeer) {
277 /* OpenLDAP SDK supports BASE64 files. */
278 if((data->set.str[STRING_CERT_TYPE]) &&
279 (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
280 failf(data, "LDAP local: ERROR OpenLDAP does only support PEM cert-type!");
281 status = CURLE_SSL_CERTPROBLEM;
285 failf(data, "LDAP local: ERROR PEM CA cert not set!");
286 status = CURLE_SSL_CERTPROBLEM;
289 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
290 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
291 if(rc != LDAP_SUCCESS) {
292 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
293 ldap_err2string(rc));
294 status = CURLE_SSL_CERTPROBLEM;
297 ldap_option = LDAP_OPT_X_TLS_DEMAND;
299 ldap_option = LDAP_OPT_X_TLS_NEVER;
301 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
302 if(rc != LDAP_SUCCESS) {
303 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
304 ldap_err2string(rc));
305 status = CURLE_SSL_CERTPROBLEM;
308 server = ldap_init(conn->host.name, (int)conn->port);
310 failf(data, "LDAP local: Cannot connect to %s:%hu",
311 conn->host.name, conn->port);
312 status = CURLE_COULDNT_CONNECT;
315 ldap_option = LDAP_OPT_X_TLS_HARD;
316 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
317 if(rc != LDAP_SUCCESS) {
318 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
319 ldap_err2string(rc));
320 status = CURLE_SSL_CERTPROBLEM;
324 rc = ldap_start_tls_s(server, NULL, NULL);
325 if(rc != LDAP_SUCCESS) {
326 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
327 ldap_err2string(rc));
328 status = CURLE_SSL_CERTPROBLEM;
333 /* we should probably never come up to here since configure
334 should check in first place if we can support LDAP SSL/TLS */
335 failf(data, "LDAP local: SSL/TLS not supported with this version "
336 "of the OpenLDAP toolkit\n");
337 status = CURLE_SSL_CERTPROBLEM;
341 #endif /* CURL_LDAP_USE_SSL */
343 server = ldap_init(conn->host.name, (int)conn->port);
345 failf(data, "LDAP local: Cannot connect to %s:%hu",
346 conn->host.name, conn->port);
347 status = CURLE_COULDNT_CONNECT;
352 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
355 rc = ldap_simple_bind_s(server,
356 conn->bits.user_passwd ? conn->user : NULL,
357 conn->bits.user_passwd ? conn->passwd : NULL);
358 if(!ldap_ssl && rc != 0) {
359 ldap_proto = LDAP_VERSION2;
360 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);
366 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
367 status = CURLE_LDAP_CANNOT_BIND;
371 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
372 ludp->lud_filter, ludp->lud_attrs, 0, &result);
374 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
375 failf(data, "LDAP remote: %s", ldap_err2string(rc));
376 status = CURLE_LDAP_SEARCH_FAILED;
380 for(num = 0, entryIterator = ldap_first_entry(server, result);
382 entryIterator = ldap_next_entry(server, entryIterator), num++)
384 BerElement *ber = NULL;
385 char *attribute; /*! suspicious that this isn't 'const' */
386 char *dn = ldap_get_dn(server, entryIterator);
389 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
390 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
391 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
393 dlsize += strlen(dn)+5;
395 for (attribute = ldap_first_attribute(server, entryIterator, &ber);
397 attribute = ldap_next_attribute(server, entryIterator, ber))
399 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
403 for (i = 0; (vals[i] != NULL); i++)
405 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
406 Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
407 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
408 dlsize += strlen(attribute)+3;
410 if((strlen(attribute) > 7) &&
413 (strlen((char *)attribute) - 7)) == 0)) {
414 /* Binary attribute, encode to base64. */
415 val_b64_sz = Curl_base64_encode(data,
420 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
422 dlsize += val_b64_sz;
426 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
428 dlsize += vals[i]->bv_len;
430 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
434 /* Free memory used to store values */
435 ldap_value_free_len(vals);
437 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
439 Curl_pgrsSetDownloadCounter(data, dlsize);
440 ldap_memfree(attribute);
449 ldap_msgfree(result);
450 LDAP_TRACE (("Received %d entries\n", num));
452 if(rc == LDAP_SIZELIMIT_EXCEEDED)
453 infof(data, "There are more than %d entries\n", num);
455 ldap_free_urldesc(ludp);
457 ldap_unbind_s(server);
458 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
460 ldapssl_client_deinit();
461 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
463 /* no data to transfer */
464 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
465 conn->bits.close = TRUE;
471 static void _ldap_trace (const char *fmt, ...)
473 static int do_trace = -1;
477 const char *env = getenv("CURL_TRACE");
478 do_trace = (env && strtol(env, NULL, 10) > 0);
483 va_start (args, fmt);
484 vfprintf (stderr, fmt, args);
489 #ifndef HAVE_LDAP_URL_PARSE
492 * Return scope-value for a scope-string.
494 static int str2scope (const char *p)
496 if(strequal(p, "one"))
497 return LDAP_SCOPE_ONELEVEL;
498 if(strequal(p, "onetree"))
499 return LDAP_SCOPE_ONELEVEL;
500 if(strequal(p, "base"))
501 return LDAP_SCOPE_BASE;
502 if(strequal(p, "sub"))
503 return LDAP_SCOPE_SUBTREE;
504 if(strequal( p, "subtree"))
505 return LDAP_SCOPE_SUBTREE;
510 * Split 'str' into strings separated by commas.
511 * Note: res[] points into 'str'.
513 static char **split_str (char *str)
515 char **res, *lasts, *s;
518 for (i = 2, s = strchr(str,','); s; i++)
521 res = calloc(i, sizeof(char*));
525 for (i = 0, s = strtok_r(str, ",", &lasts); s;
526 s = strtok_r(NULL, ",", &lasts), i++)
532 * Unescape the LDAP-URL components
534 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
538 if(ludp->lud_filter) {
539 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
540 if(!ludp->lud_filter)
544 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
545 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
546 if(!ludp->lud_attrs[i])
550 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
551 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
552 if(!ludp->lud_exts[i])
557 char *dn = ludp->lud_dn;
558 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
561 ludp->lud_dn = new_dn;
569 * Break apart the pieces of an LDAP URL.
571 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
573 * <hostname> already known from 'conn->host.name'.
574 * <port> already known from 'conn->remote_port'.
575 * extract the rest from 'conn->data->state.path+1'. All fields are optional.
577 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
578 * yields ludp->lud_dn = "".
580 * Defined in RFC4516 section 2.
582 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
588 !conn->data->state.path ||
589 conn->data->state.path[0] != '/' ||
590 !checkprefix("LDAP", conn->data->change.url))
591 return LDAP_INVALID_SYNTAX;
593 ludp->lud_scope = LDAP_SCOPE_BASE;
594 ludp->lud_port = conn->remote_port;
595 ludp->lud_host = conn->host.name;
597 /* parse DN (Distinguished Name).
599 ludp->lud_dn = strdup(conn->data->state.path+1);
601 return LDAP_NO_MEMORY;
603 p = strchr(ludp->lud_dn, '?');
604 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
605 strlen(ludp->lud_dn), ludp->lud_dn));
612 /* parse attributes. skip "??".
618 if(*p && *p != '?') {
619 ludp->lud_attrs = split_str(p);
621 return LDAP_NO_MEMORY;
623 for (i = 0; ludp->lud_attrs[i]; i++)
624 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
631 /* parse scope. skip "??"
637 if(*p && *p != '?') {
638 ludp->lud_scope = str2scope(p);
639 if(ludp->lud_scope == -1)
640 return LDAP_INVALID_SYNTAX;
641 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
654 return LDAP_INVALID_SYNTAX;
656 ludp->lud_filter = p;
657 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
665 ludp->lud_exts = split_str(p);
667 return LDAP_NO_MEMORY;
669 for (i = 0; ludp->lud_exts[i]; i++)
670 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
673 if(!unescape_elements(conn->data, ludp))
674 return LDAP_NO_MEMORY;
678 static int _ldap_url_parse (const struct connectdata *conn,
681 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
686 return LDAP_NO_MEMORY;
688 rc = _ldap_url_parse2 (conn, ludp);
689 if(rc != LDAP_SUCCESS) {
690 _ldap_free_urldesc(ludp);
697 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
708 free(ludp->lud_filter);
710 if(ludp->lud_attrs) {
711 for (i = 0; ludp->lud_attrs[i]; i++)
712 free(ludp->lud_attrs[i]);
713 free(ludp->lud_attrs);
717 for (i = 0; ludp->lud_exts[i]; i++)
718 free(ludp->lud_exts[i]);
719 free(ludp->lud_exts);
723 #endif /* !HAVE_LDAP_URL_PARSE */
724 #endif /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */