1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2009, 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.
22 ***************************************************************************/
26 #ifndef CURL_DISABLE_LDAP
27 /* -- WIN32 approved -- */
35 #ifdef CURL_LDAP_HYBRID /* If W$ definitions are needed. */
37 /* Remember we are NOT in a W$ compiler! */
43 #ifdef CURL_LDAP_WIN /* Use W$ LDAP implementation. */
45 # ifndef LDAP_VENDOR_NAME
46 # error Your Platform SDK is NOT sufficient for LDAP support! Update your Platform SDK, or disable LDAP support!
51 #define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
56 #if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
57 # include <ldap_ssl.h>
58 #endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
66 #include <curl/curl.h>
73 #include "curl_ldap.h"
74 #include "curl_memory.h"
75 #include "curl_base64.h"
78 #define _MPRINTF_REPLACE /* use our functions only */
79 #include <curl/mprintf.h>
83 #ifndef HAVE_LDAP_URL_PARSE
85 /* Use our own implementation. */
98 #define LDAPURLDesc CURL_LDAPURLDesc
100 static int _ldap_url_parse (const struct connectdata *conn,
102 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
104 #undef ldap_free_urldesc
105 #define ldap_free_urldesc _ldap_free_urldesc
109 #define LDAP_TRACE(x) do { \
110 _ldap_trace ("%u: ", __LINE__); \
114 static void _ldap_trace (const char *fmt, ...);
116 #define LDAP_TRACE(x) ((void)0)
120 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
123 * LDAP protocol handler.
126 const struct Curl_handler Curl_handler_ldap = {
128 ZERO_NULL, /* setup_connection */
129 Curl_ldap, /* do_it */
130 ZERO_NULL, /* done */
131 ZERO_NULL, /* do_more */
132 ZERO_NULL, /* connect_it */
133 ZERO_NULL, /* connecting */
134 ZERO_NULL, /* doing */
135 ZERO_NULL, /* proto_getsock */
136 ZERO_NULL, /* doing_getsock */
137 ZERO_NULL, /* perform_getsock */
138 ZERO_NULL, /* disconnect */
139 PORT_LDAP, /* defport */
140 PROT_LDAP /* protocol */
145 * LDAPS protocol handler.
148 const struct Curl_handler Curl_handler_ldaps = {
149 "LDAPS", /* scheme */
150 ZERO_NULL, /* setup_connection */
151 Curl_ldap, /* do_it */
152 ZERO_NULL, /* done */
153 ZERO_NULL, /* do_more */
154 ZERO_NULL, /* connect_it */
155 ZERO_NULL, /* connecting */
156 ZERO_NULL, /* doing */
157 ZERO_NULL, /* proto_getsock */
158 ZERO_NULL, /* doing_getsock */
159 ZERO_NULL, /* perform_getsock */
160 ZERO_NULL, /* disconnect */
161 PORT_LDAPS, /* defport */
162 PROT_LDAP | PROT_SSL /* protocol */
167 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
169 CURLcode status = CURLE_OK;
172 LDAPURLDesc *ludp = NULL;
173 LDAPMessage *result = NULL;
174 LDAPMessage *entryIterator;
176 struct SessionHandle *data=conn->data;
177 int ldap_proto = LDAP_VERSION3;
182 #ifdef LDAP_OPT_NETWORK_TIMEOUT
183 struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
186 *done = TRUE; /* unconditionally */
187 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
188 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
189 infof(data, "LDAP local: %s\n", data->change.url);
191 #ifdef HAVE_LDAP_URL_PARSE
192 rc = ldap_url_parse(data->change.url, &ludp);
194 rc = _ldap_url_parse(conn, &ludp);
197 failf(data, "LDAP local: %s", ldap_err2string(rc));
198 status = CURLE_LDAP_INVALID_URL;
202 /* Get the URL scheme ( either ldap or ldaps ) */
203 if(conn->protocol & PROT_SSL)
205 infof(data, "LDAP local: trying to establish %s connection\n",
206 ldap_ssl ? "encrypted" : "cleartext");
208 #ifdef LDAP_OPT_NETWORK_TIMEOUT
209 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
211 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
216 /* Win32 LDAP SDK doesnt support insecure mode without CA! */
217 server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
218 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
221 char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
222 #if defined(CURL_HAS_NOVELL_LDAPSDK)
223 rc = ldapssl_client_init(NULL, NULL);
224 if(rc != LDAP_SUCCESS) {
225 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
226 status = CURLE_SSL_CERTPROBLEM;
229 if(data->set.ssl.verifypeer) {
230 /* Novell SDK supports DER or BASE64 files. */
231 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
232 if((data->set.str[STRING_CERT_TYPE]) &&
233 (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
234 cert_type = LDAPSSL_CERT_FILETYPE_DER;
236 failf(data, "LDAP local: ERROR %s CA cert not set!",
237 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
238 status = CURLE_SSL_CERTPROBLEM;
241 infof(data, "LDAP local: using %s CA cert '%s'\n",
242 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
244 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
245 if(rc != LDAP_SUCCESS) {
246 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
247 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
248 ldap_err2string(rc));
249 status = CURLE_SSL_CERTPROBLEM;
252 ldap_option = LDAPSSL_VERIFY_SERVER;
254 ldap_option = LDAPSSL_VERIFY_NONE;
256 rc = ldapssl_set_verify_mode(ldap_option);
257 if(rc != LDAP_SUCCESS) {
258 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
259 ldap_err2string(rc));
260 status = CURLE_SSL_CERTPROBLEM;
263 server = ldapssl_init(conn->host.name, (int)conn->port, 1);
265 failf(data, "LDAP local: Cannot connect to %s:%d",
266 conn->host.name, conn->port);
267 status = CURLE_COULDNT_CONNECT;
270 #elif defined(LDAP_OPT_X_TLS)
271 if(data->set.ssl.verifypeer) {
272 /* OpenLDAP SDK supports BASE64 files. */
273 if((data->set.str[STRING_CERT_TYPE]) &&
274 (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
275 failf(data, "LDAP local: ERROR OpenLDAP does only support PEM cert-type!");
276 status = CURLE_SSL_CERTPROBLEM;
280 failf(data, "LDAP local: ERROR PEM CA cert not set!");
281 status = CURLE_SSL_CERTPROBLEM;
284 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
285 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
286 if(rc != LDAP_SUCCESS) {
287 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
288 ldap_err2string(rc));
289 status = CURLE_SSL_CERTPROBLEM;
292 ldap_option = LDAP_OPT_X_TLS_DEMAND;
294 ldap_option = LDAP_OPT_X_TLS_NEVER;
296 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
297 if(rc != LDAP_SUCCESS) {
298 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
299 ldap_err2string(rc));
300 status = CURLE_SSL_CERTPROBLEM;
303 server = ldap_init(conn->host.name, (int)conn->port);
305 failf(data, "LDAP local: Cannot connect to %s:%d",
306 conn->host.name, conn->port);
307 status = CURLE_COULDNT_CONNECT;
310 ldap_option = LDAP_OPT_X_TLS_HARD;
311 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
312 if(rc != LDAP_SUCCESS) {
313 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
314 ldap_err2string(rc));
315 status = CURLE_SSL_CERTPROBLEM;
319 rc = ldap_start_tls_s(server, NULL, NULL);
320 if(rc != LDAP_SUCCESS) {
321 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
322 ldap_err2string(rc));
323 status = CURLE_SSL_CERTPROBLEM;
328 /* we should probably never come up to here since configure
329 should check in first place if we can support LDAP SSL/TLS */
330 failf(data, "LDAP local: SSL/TLS not supported with this version "
331 "of the OpenLDAP toolkit\n");
332 status = CURLE_SSL_CERTPROBLEM;
336 #endif /* CURL_LDAP_USE_SSL */
338 server = ldap_init(conn->host.name, (int)conn->port);
340 failf(data, "LDAP local: Cannot connect to %s:%d",
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++)
379 BerElement *ber = NULL;
380 char *attribute; /*! suspicious that this isn't 'const' */
381 char *dn = ldap_get_dn(server, entryIterator);
384 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
385 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
386 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
388 dlsize += strlen(dn)+5;
390 for (attribute = ldap_first_attribute(server, entryIterator, &ber);
392 attribute = ldap_next_attribute(server, entryIterator, ber))
394 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
398 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 val_b64_sz = Curl_base64_encode(data,
415 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
417 dlsize += val_b64_sz;
421 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
423 dlsize += vals[i]->bv_len;
425 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
429 /* Free memory used to store values */
430 ldap_value_free_len(vals);
432 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
434 Curl_pgrsSetDownloadCounter(data, dlsize);
435 ldap_memfree(attribute);
444 ldap_msgfree(result);
445 LDAP_TRACE (("Received %d entries\n", num));
447 if(rc == LDAP_SIZELIMIT_EXCEEDED)
448 infof(data, "There are more than %d entries\n", num);
450 ldap_free_urldesc(ludp);
452 ldap_unbind_s(server);
453 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
455 ldapssl_client_deinit();
456 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
458 /* no data to transfer */
459 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
460 conn->bits.close = TRUE;
466 static void _ldap_trace (const char *fmt, ...)
468 static int do_trace = -1;
472 const char *env = getenv("CURL_TRACE");
473 do_trace = (env && atoi(env) > 0);
478 va_start (args, fmt);
479 vfprintf (stderr, fmt, args);
484 #ifndef HAVE_LDAP_URL_PARSE
487 * Return scope-value for a scope-string.
489 static int str2scope (const char *p)
491 if(strequal(p, "one"))
492 return LDAP_SCOPE_ONELEVEL;
493 if(strequal(p, "onetree"))
494 return LDAP_SCOPE_ONELEVEL;
495 if(strequal(p, "base"))
496 return LDAP_SCOPE_BASE;
497 if(strequal(p, "sub"))
498 return LDAP_SCOPE_SUBTREE;
499 if(strequal( p, "subtree"))
500 return LDAP_SCOPE_SUBTREE;
505 * Split 'str' into strings separated by commas.
506 * Note: res[] points into 'str'.
508 static char **split_str (char *str)
510 char **res, *lasts, *s;
513 for (i = 2, s = strchr(str,','); s; i++)
516 res = calloc(i, sizeof(char*));
520 for (i = 0, s = strtok_r(str, ",", &lasts); s;
521 s = strtok_r(NULL, ",", &lasts), i++)
527 * Unescape the LDAP-URL components
529 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
533 if(ludp->lud_filter) {
534 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
535 if(!ludp->lud_filter)
539 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
540 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
541 if(!ludp->lud_attrs[i])
545 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
546 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
547 if(!ludp->lud_exts[i])
552 char *dn = ludp->lud_dn;
553 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
556 ludp->lud_dn = new_dn;
564 * Break apart the pieces of an LDAP URL.
566 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
568 * <hostname> already known from 'conn->host.name'.
569 * <port> already known from 'conn->remote_port'.
570 * extract the rest from 'conn->data->state.path+1'. All fields are optional.
572 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
573 * yields ludp->lud_dn = "".
575 * Defined in RFC4516 section 2.
577 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
583 !conn->data->state.path ||
584 conn->data->state.path[0] != '/' ||
585 !checkprefix("LDAP", conn->data->change.url))
586 return LDAP_INVALID_SYNTAX;
588 ludp->lud_scope = LDAP_SCOPE_BASE;
589 ludp->lud_port = conn->remote_port;
590 ludp->lud_host = conn->host.name;
592 /* parse DN (Distinguished Name).
594 ludp->lud_dn = strdup(conn->data->state.path+1);
596 return LDAP_NO_MEMORY;
598 p = strchr(ludp->lud_dn, '?');
599 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
600 strlen(ludp->lud_dn), ludp->lud_dn));
607 /* parse attributes. skip "??".
613 if(*p && *p != '?') {
614 ludp->lud_attrs = split_str(p);
616 return LDAP_NO_MEMORY;
618 for (i = 0; ludp->lud_attrs[i]; i++)
619 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
626 /* parse scope. skip "??"
632 if(*p && *p != '?') {
633 ludp->lud_scope = str2scope(p);
634 if(ludp->lud_scope == -1)
635 return LDAP_INVALID_SYNTAX;
636 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
649 return LDAP_INVALID_SYNTAX;
651 ludp->lud_filter = p;
652 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
660 ludp->lud_exts = split_str(p);
662 return LDAP_NO_MEMORY;
664 for (i = 0; ludp->lud_exts[i]; i++)
665 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
668 if(!unescape_elements(conn->data, ludp))
669 return LDAP_NO_MEMORY;
673 static int _ldap_url_parse (const struct connectdata *conn,
676 LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
681 return LDAP_NO_MEMORY;
683 rc = _ldap_url_parse2 (conn, ludp);
684 if(rc != LDAP_SUCCESS) {
685 _ldap_free_urldesc(ludp);
692 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
703 free(ludp->lud_filter);
705 if(ludp->lud_attrs) {
706 for (i = 0; ludp->lud_attrs[i]; i++)
707 free(ludp->lud_attrs[i]);
708 free(ludp->lud_attrs);
712 for (i = 0; ludp->lud_exts[i]; i++)
713 free(ludp->lud_exts[i]);
714 free(ludp->lud_exts);
718 #endif /* !HAVE_LDAP_URL_PARSE */
719 #endif /* CURL_DISABLE_LDAP */