1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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 -- */
38 #ifdef CURL_LDAP_HYBRID /* If W$ definitions are needed. */
40 /* Remember we are NOT in a W$ compiler! */
46 #ifdef CURL_LDAP_WIN /* Use W$ 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 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>
75 #include "curl_ldap.h"
79 #define _MPRINTF_REPLACE /* use our functions only */
80 #include <curl/mprintf.h>
84 #ifndef HAVE_LDAP_URL_PARSE
86 /* Use our own implementation. */
99 #define LDAPURLDesc CURL_LDAPURLDesc
101 static int _ldap_url_parse (const struct connectdata *conn,
103 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
105 #undef ldap_free_urldesc
106 #define ldap_free_urldesc _ldap_free_urldesc
110 #define LDAP_TRACE(x) do { \
111 _ldap_trace ("%u: ", __LINE__); \
115 static void _ldap_trace (const char *fmt, ...);
117 #define LDAP_TRACE(x) ((void)0)
121 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
124 * LDAP protocol handler.
127 const struct Curl_handler Curl_handler_ldap = {
129 ZERO_NULL, /* setup_connection */
130 Curl_ldap, /* do_it */
131 ZERO_NULL, /* done */
132 ZERO_NULL, /* do_more */
133 ZERO_NULL, /* connect_it */
134 ZERO_NULL, /* connecting */
135 ZERO_NULL, /* doing */
136 ZERO_NULL, /* proto_getsock */
137 ZERO_NULL, /* doing_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, /* disconnect */
160 PORT_LDAPS, /* defport */
161 PROT_LDAP | PROT_SSL /* protocol */
166 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
168 CURLcode status = CURLE_OK;
171 LDAPURLDesc *ludp = NULL;
172 LDAPMessage *result = NULL;
173 LDAPMessage *entryIterator;
175 struct SessionHandle *data=conn->data;
176 int ldap_proto = LDAP_VERSION3;
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(strequal(conn->protostr, "LDAPS"))
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 doesnt 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 (strequal(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;
252 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:%d",
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 (!strequal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
273 failf(data, "LDAP local: ERROR OpenLDAP does only support 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;
292 ldap_option = LDAP_OPT_X_TLS_NEVER;
294 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
295 if(rc != LDAP_SUCCESS) {
296 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
297 ldap_err2string(rc));
298 status = CURLE_SSL_CERTPROBLEM;
301 server = ldap_init(conn->host.name, (int)conn->port);
303 failf(data, "LDAP local: Cannot connect to %s:%d",
304 conn->host.name, conn->port);
305 status = CURLE_COULDNT_CONNECT;
308 ldap_option = LDAP_OPT_X_TLS_HARD;
309 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
310 if(rc != LDAP_SUCCESS) {
311 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
312 ldap_err2string(rc));
313 status = CURLE_SSL_CERTPROBLEM;
317 rc = ldap_start_tls_s(server, NULL, NULL);
318 if(rc != LDAP_SUCCESS) {
319 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
320 ldap_err2string(rc));
321 status = CURLE_SSL_CERTPROBLEM;
326 /* we should probably never come up to here since configure
327 should check in first place if we can support LDAP SSL/TLS */
328 failf(data, "LDAP local: SSL/TLS not supported with this version "
329 "of the OpenLDAP toolkit\n");
330 status = CURLE_SSL_CERTPROBLEM;
334 #endif /* CURL_LDAP_USE_SSL */
336 server = ldap_init(conn->host.name, (int)conn->port);
338 failf(data, "LDAP local: Cannot connect to %s:%d",
339 conn->host.name, conn->port);
340 status = CURLE_COULDNT_CONNECT;
345 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
348 rc = ldap_simple_bind_s(server,
349 conn->bits.user_passwd ? conn->user : NULL,
350 conn->bits.user_passwd ? conn->passwd : NULL);
351 if(!ldap_ssl && rc != 0) {
352 ldap_proto = LDAP_VERSION2;
353 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
354 rc = ldap_simple_bind_s(server,
355 conn->bits.user_passwd ? conn->user : NULL,
356 conn->bits.user_passwd ? conn->passwd : NULL);
359 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
360 status = CURLE_LDAP_CANNOT_BIND;
364 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
365 ludp->lud_filter, ludp->lud_attrs, 0, &result);
367 if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
368 failf(data, "LDAP remote: %s", ldap_err2string(rc));
369 status = CURLE_LDAP_SEARCH_FAILED;
373 for(num = 0, entryIterator = ldap_first_entry(server, result);
375 entryIterator = ldap_next_entry(server, entryIterator), num++)
377 BerElement *ber = NULL;
378 char *attribute; /*! suspicious that this isn't 'const' */
379 char *dn = ldap_get_dn(server, entryIterator);
382 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
383 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
384 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
386 for (attribute = ldap_first_attribute(server, entryIterator, &ber);
388 attribute = ldap_next_attribute(server, entryIterator, ber))
390 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
394 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 if((strlen(attribute) > 7) &&
402 (strlen((char *)attribute) - 7)) == 0)) {
403 /* Binary attribute, encode to base64. */
404 val_b64_sz = Curl_base64_encode(conn->data,
409 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
413 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
415 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
418 /* Free memory used to store values */
419 ldap_value_free_len(vals);
421 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
423 ldap_memfree(attribute);
432 ldap_msgfree(result);
433 LDAP_TRACE (("Received %d entries\n", num));
435 if(rc == LDAP_SIZELIMIT_EXCEEDED)
436 infof(data, "There are more than %d entries\n", num);
438 ldap_free_urldesc(ludp);
440 ldap_unbind_s(server);
441 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
443 ldapssl_client_deinit();
444 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
446 /* no data to transfer */
447 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
448 conn->bits.close = TRUE;
454 static void _ldap_trace (const char *fmt, ...)
456 static int do_trace = -1;
460 const char *env = getenv("CURL_TRACE");
461 do_trace = (env && atoi(env) > 0);
466 va_start (args, fmt);
467 vfprintf (stderr, fmt, args);
472 #ifndef HAVE_LDAP_URL_PARSE
475 * Return scope-value for a scope-string.
477 static int str2scope (const char *p)
479 if(!stricmp(p, "one"))
480 return LDAP_SCOPE_ONELEVEL;
481 if(!stricmp(p, "onetree"))
482 return LDAP_SCOPE_ONELEVEL;
483 if(!stricmp(p, "base"))
484 return LDAP_SCOPE_BASE;
485 if(!stricmp(p, "sub"))
486 return LDAP_SCOPE_SUBTREE;
487 if(!stricmp( p, "subtree"))
488 return LDAP_SCOPE_SUBTREE;
493 * Split 'str' into strings separated by commas.
494 * Note: res[] points into 'str'.
496 static char **split_str (char *str)
498 char **res, *lasts, *s;
501 for (i = 2, s = strchr(str,','); s; i++)
504 res = calloc(i, sizeof(char*));
508 for (i = 0, s = strtok_r(str, ",", &lasts); s;
509 s = strtok_r(NULL, ",", &lasts), i++)
515 * Unescape the LDAP-URL components
517 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
521 if(ludp->lud_filter) {
522 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
523 if(!ludp->lud_filter)
527 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
528 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
529 if(!ludp->lud_attrs[i])
533 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
534 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
535 if(!ludp->lud_exts[i])
540 char *dn = ludp->lud_dn;
541 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
544 ludp->lud_dn = new_dn;
552 * Break apart the pieces of an LDAP URL.
554 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
556 * <hostname> already known from 'conn->host.name'.
557 * <port> already known from 'conn->remote_port'.
558 * extract the rest from 'conn->data->state.path+1'. All fields are optional.
560 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
561 * yields ludp->lud_dn = "".
563 * Ref. http://developer.netscape.com/docs/manuals/dirsdk/csdk30/url.htm#2831915
565 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
571 !conn->data->state.path ||
572 conn->data->state.path[0] != '/' ||
573 !checkprefix(conn->protostr, conn->data->change.url))
574 return LDAP_INVALID_SYNTAX;
576 ludp->lud_scope = LDAP_SCOPE_BASE;
577 ludp->lud_port = conn->remote_port;
578 ludp->lud_host = conn->host.name;
580 /* parse DN (Distinguished Name).
582 ludp->lud_dn = strdup(conn->data->state.path+1);
584 return LDAP_NO_MEMORY;
586 p = strchr(ludp->lud_dn, '?');
587 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
588 strlen(ludp->lud_dn), ludp->lud_dn));
595 /* parse attributes. skip "??".
601 if(*p && *p != '?') {
602 ludp->lud_attrs = split_str(p);
604 return LDAP_NO_MEMORY;
606 for (i = 0; ludp->lud_attrs[i]; i++)
607 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
614 /* parse scope. skip "??"
620 if(*p && *p != '?') {
621 ludp->lud_scope = str2scope(p);
622 if(ludp->lud_scope == -1)
623 return LDAP_INVALID_SYNTAX;
624 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
637 return LDAP_INVALID_SYNTAX;
639 ludp->lud_filter = p;
640 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
648 ludp->lud_exts = split_str(p);
650 return LDAP_NO_MEMORY;
652 for (i = 0; ludp->lud_exts[i]; i++)
653 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
656 if(!unescape_elements(conn->data, ludp))
657 return LDAP_NO_MEMORY;
661 static int _ldap_url_parse (const struct connectdata *conn,
664 LDAPURLDesc *ludp = calloc(sizeof(*ludp), 1);
669 return LDAP_NO_MEMORY;
671 rc = _ldap_url_parse2 (conn, ludp);
672 if(rc != LDAP_SUCCESS) {
673 _ldap_free_urldesc(ludp);
680 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
691 free(ludp->lud_filter);
693 if(ludp->lud_attrs) {
694 for (i = 0; ludp->lud_attrs[i]; i++)
695 free(ludp->lud_attrs[i]);
696 free(ludp->lud_attrs);
700 for (i = 0; ludp->lud_exts[i]; i++)
701 free(ludp->lud_exts[i]);
702 free(ludp->lud_exts);
706 #endif /* !HAVE_LDAP_URL_PARSE */
707 #endif /* CURL_DISABLE_LDAP */