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. */
49 #define LDAP_DEPRECATED 1 /* Be sure ldap_init() is defined. */
51 #if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
52 # include <ldap_ssl.h>
53 #endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
61 #include <curl/curl.h>
67 #include "curl_ldap.h"
71 #define _MPRINTF_REPLACE /* use our functions only */
72 #include <curl/mprintf.h>
76 #ifndef HAVE_LDAP_URL_PARSE
78 /* Use our own implementation. */
91 #define LDAPURLDesc CURL_LDAPURLDesc
93 static int _ldap_url_parse (const struct connectdata *conn,
95 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
97 #undef ldap_free_urldesc
98 #define ldap_free_urldesc _ldap_free_urldesc
102 #define LDAP_TRACE(x) do { \
103 _ldap_trace ("%u: ", __LINE__); \
107 static void _ldap_trace (const char *fmt, ...);
109 #define LDAP_TRACE(x) ((void)0)
113 CURLcode Curl_ldap(struct connectdata *conn, bool *done)
115 CURLcode status = CURLE_OK;
118 LDAPURLDesc *ludp = NULL;
119 LDAPMessage *result = NULL;
120 LDAPMessage *entryIterator;
122 struct SessionHandle *data=conn->data;
127 #ifdef LDAP_OPT_NETWORK_TIMEOUT
128 struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
131 *done = TRUE; /* unconditionally */
132 infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
133 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
134 infof(data, "LDAP local: %s\n", data->change.url);
136 #ifdef HAVE_LDAP_URL_PARSE
137 rc = ldap_url_parse(data->change.url, &ludp);
139 rc = _ldap_url_parse(conn, &ludp);
142 failf(data, "LDAP local: %s", ldap_err2string(rc));
143 status = CURLE_LDAP_INVALID_URL;
147 /* Get the URL scheme ( either ldap or ldaps ) */
148 if (strequal(conn->protostr, "LDAPS"))
150 infof(data, "LDAP local: trying to establish %s connection\n",
151 ldap_ssl ? "encrypted" : "cleartext");
153 #ifdef LDAP_OPT_NETWORK_TIMEOUT
154 ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
156 ldap_proto = LDAP_VERSION3;
157 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
162 /* Win32 LDAP SDK doesnt support insecure mode without CA! */
163 server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
164 ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
167 char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
168 #if defined(CURL_HAS_NOVELL_LDAPSDK)
169 rc = ldapssl_client_init(NULL, NULL);
170 if (rc != LDAP_SUCCESS) {
171 failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
172 status = CURLE_SSL_CERTPROBLEM;
175 if (data->set.ssl.verifypeer) {
176 /* Novell SDK supports DER or BASE64 files. */
177 int cert_type = LDAPSSL_CERT_FILETYPE_B64;
178 if ((data->set.str[STRING_CERT_TYPE]) &&
179 (strequal(data->set.str[STRING_CERT_TYPE], "DER")))
180 cert_type = LDAPSSL_CERT_FILETYPE_DER;
182 failf(data, "LDAP local: ERROR %s CA cert not set!",
183 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
184 status = CURLE_SSL_CERTPROBLEM;
187 infof(data, "LDAP local: using %s CA cert '%s'\n",
188 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
190 rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
191 if (rc != LDAP_SUCCESS) {
192 failf(data, "LDAP local: ERROR setting %s CA cert: %s",
193 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
194 ldap_err2string(rc));
195 status = CURLE_SSL_CERTPROBLEM;
198 ldap_option = LDAPSSL_VERIFY_SERVER;
200 ldap_option = LDAPSSL_VERIFY_NONE;
202 rc = ldapssl_set_verify_mode(ldap_option);
203 if (rc != LDAP_SUCCESS) {
204 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
205 ldap_err2string(rc));
206 status = CURLE_SSL_CERTPROBLEM;
209 server = ldapssl_init(conn->host.name, (int)conn->port, 1);
210 if (server == NULL) {
211 failf(data, "LDAP local: Cannot connect to %s:%d",
212 conn->host.name, conn->port);
213 status = CURLE_COULDNT_CONNECT;
216 #elif defined(LDAP_OPT_X_TLS)
217 if (data->set.ssl.verifypeer) {
218 /* OpenLDAP SDK supports BASE64 files. */
220 failf(data, "LDAP local: ERROR PEM CA cert not set!");
221 status = CURLE_SSL_CERTPROBLEM;
224 infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
225 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
226 if (rc != LDAP_SUCCESS) {
227 failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
228 ldap_err2string(rc));
229 status = CURLE_SSL_CERTPROBLEM;
232 ldap_option = LDAP_OPT_X_TLS_DEMAND;
234 ldap_option = LDAP_OPT_X_TLS_NEVER;
236 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
237 if (rc != LDAP_SUCCESS) {
238 failf(data, "LDAP local: ERROR setting cert verify mode: %s",
239 ldap_err2string(rc));
240 status = CURLE_SSL_CERTPROBLEM;
243 server = ldap_init(conn->host.name, (int)conn->port);
244 if (server == NULL) {
245 failf(data, "LDAP local: Cannot connect to %s:%d",
246 conn->host.name, conn->port);
247 status = CURLE_COULDNT_CONNECT;
250 ldap_option = LDAP_OPT_X_TLS_HARD;
251 rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
252 if (rc != LDAP_SUCCESS) {
253 failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
254 ldap_err2string(rc));
255 status = CURLE_SSL_CERTPROBLEM;
259 rc = ldap_start_tls_s(server, NULL, NULL);
260 if (rc != LDAP_SUCCESS) {
261 failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
262 ldap_err2string(rc));
263 status = CURLE_SSL_CERTPROBLEM;
268 /* we should probably never come up to here since configure
269 should check in first place if we can support LDAP SSL/TLS */
270 failf(data, "LDAP local: SSL/TLS not supported with this version "
271 "of the OpenLDAP toolkit\n");
272 status = CURLE_SSL_CERTPROBLEM;
276 #endif /* CURL_LDAP_USE_SSL */
278 server = ldap_init(conn->host.name, (int)conn->port);
279 if (server == NULL) {
280 failf(data, "LDAP local: Cannot connect to %s:%d",
281 conn->host.name, conn->port);
282 status = CURLE_COULDNT_CONNECT;
287 rc = ldap_simple_bind_s(server,
288 conn->bits.user_passwd ? conn->user : NULL,
289 conn->bits.user_passwd ? conn->passwd : NULL);
290 if (!ldap_ssl && rc != 0) {
291 ldap_proto = LDAP_VERSION2;
292 ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
293 rc = ldap_simple_bind_s(server,
294 conn->bits.user_passwd ? conn->user : NULL,
295 conn->bits.user_passwd ? conn->passwd : NULL);
298 failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
299 status = CURLE_LDAP_CANNOT_BIND;
303 rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
304 ludp->lud_filter, ludp->lud_attrs, 0, &result);
306 if (rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
307 failf(data, "LDAP remote: %s", ldap_err2string(rc));
308 status = CURLE_LDAP_SEARCH_FAILED;
312 for(num = 0, entryIterator = ldap_first_entry(server, result);
314 entryIterator = ldap_next_entry(server, entryIterator), num++)
316 BerElement *ber = NULL;
317 char *attribute; /*! suspicious that this isn't 'const' */
318 char *dn = ldap_get_dn(server, entryIterator);
321 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
322 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
323 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
325 for (attribute = ldap_first_attribute(server, entryIterator, &ber);
327 attribute = ldap_next_attribute(server, entryIterator, ber))
329 BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
333 for (i = 0; (vals[i] != NULL); i++)
335 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
336 Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
337 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
338 if ((strlen(attribute) > 7) &&
341 (strlen((char *)attribute) - 7)) == 0)) {
342 /* Binary attribute, encode to base64. */
343 val_b64_sz = Curl_base64_encode(conn->data,
347 if (val_b64_sz > 0) {
348 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
352 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
354 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
357 /* Free memory used to store values */
358 ldap_value_free_len(vals);
360 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
362 ldap_memfree(attribute);
371 ldap_msgfree(result);
372 LDAP_TRACE (("Received %d entries\n", num));
374 if (rc == LDAP_SIZELIMIT_EXCEEDED)
375 infof(data, "There are more than %d entries\n", num);
377 ldap_free_urldesc(ludp);
379 ldap_unbind_s(server);
380 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
382 ldapssl_client_deinit();
383 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
385 /* no data to transfer */
386 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
387 conn->bits.close = TRUE;
393 static void _ldap_trace (const char *fmt, ...)
395 static int do_trace = -1;
398 if (do_trace == -1) {
399 const char *env = getenv("CURL_TRACE");
400 do_trace = (env && atoi(env) > 0);
405 va_start (args, fmt);
406 vfprintf (stderr, fmt, args);
411 #ifndef HAVE_LDAP_URL_PARSE
414 * Return scope-value for a scope-string.
416 static int str2scope (const char *p)
418 if (!stricmp(p, "one"))
419 return LDAP_SCOPE_ONELEVEL;
420 if (!stricmp(p, "onetree"))
421 return LDAP_SCOPE_ONELEVEL;
422 if (!stricmp(p, "base"))
423 return LDAP_SCOPE_BASE;
424 if (!stricmp(p, "sub"))
425 return LDAP_SCOPE_SUBTREE;
426 if (!stricmp( p, "subtree"))
427 return LDAP_SCOPE_SUBTREE;
432 * Split 'str' into strings separated by commas.
433 * Note: res[] points into 'str'.
435 static char **split_str (char *str)
437 char **res, *lasts, *s;
440 for (i = 2, s = strchr(str,','); s; i++)
443 res = calloc(i, sizeof(char*));
447 for (i = 0, s = strtok_r(str, ",", &lasts); s;
448 s = strtok_r(NULL, ",", &lasts), i++)
454 * Unescape the LDAP-URL components
456 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
460 if (ludp->lud_filter) {
461 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
462 if (!ludp->lud_filter)
466 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
467 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
468 if (!ludp->lud_attrs[i])
472 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
473 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
474 if (!ludp->lud_exts[i])
479 char *dn = ludp->lud_dn;
480 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
483 ludp->lud_dn = new_dn;
491 * Break apart the pieces of an LDAP URL.
493 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
495 * <hostname> already known from 'conn->host.name'.
496 * <port> already known from 'conn->remote_port'.
497 * extract the rest from 'conn->data->reqdata.path+1'. All fields are optional.
499 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
500 * yields ludp->lud_dn = "".
502 * Ref. http://developer.netscape.com/docs/manuals/dirsdk/csdk30/url.htm#2831915
504 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
510 !conn->data->reqdata.path ||
511 conn->data->reqdata.path[0] != '/' ||
512 !checkprefix(conn->protostr, conn->data->change.url))
513 return LDAP_INVALID_SYNTAX;
515 ludp->lud_scope = LDAP_SCOPE_BASE;
516 ludp->lud_port = conn->remote_port;
517 ludp->lud_host = conn->host.name;
519 /* parse DN (Distinguished Name).
521 ludp->lud_dn = strdup(conn->data->reqdata.path+1);
523 return LDAP_NO_MEMORY;
525 p = strchr(ludp->lud_dn, '?');
526 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
527 strlen(ludp->lud_dn), ludp->lud_dn));
534 /* parse attributes. skip "??".
540 if (*p && *p != '?') {
541 ludp->lud_attrs = split_str(p);
542 if (!ludp->lud_attrs)
543 return LDAP_NO_MEMORY;
545 for (i = 0; ludp->lud_attrs[i]; i++)
546 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
553 /* parse scope. skip "??"
559 if (*p && *p != '?') {
560 ludp->lud_scope = str2scope(p);
561 if (ludp->lud_scope == -1)
562 return LDAP_INVALID_SYNTAX;
563 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
576 return LDAP_INVALID_SYNTAX;
578 ludp->lud_filter = p;
579 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
587 ludp->lud_exts = split_str(p);
589 return LDAP_NO_MEMORY;
591 for (i = 0; ludp->lud_exts[i]; i++)
592 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
595 if (!unescape_elements(conn->data, ludp))
596 return LDAP_NO_MEMORY;
600 static int _ldap_url_parse (const struct connectdata *conn,
603 LDAPURLDesc *ludp = calloc(sizeof(*ludp), 1);
608 return LDAP_NO_MEMORY;
610 rc = _ldap_url_parse2 (conn, ludp);
611 if (rc != LDAP_SUCCESS) {
612 _ldap_free_urldesc(ludp);
619 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
629 if (ludp->lud_filter)
630 free(ludp->lud_filter);
632 if (ludp->lud_attrs) {
633 for (i = 0; ludp->lud_attrs[i]; i++)
634 free(ludp->lud_attrs[i]);
635 free(ludp->lud_attrs);
638 if (ludp->lud_exts) {
639 for (i = 0; ludp->lud_exts[i]; i++)
640 free(ludp->lud_exts[i]);
641 free(ludp->lud_exts);
645 #endif /* !HAVE_LDAP_URL_PARSE */
646 #endif /* CURL_DISABLE_LDAP */