2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2007 Andreas Steffen
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #ifndef LDAP_DEPRECATED
20 #define LDAP_DEPRECATED 1
21 #endif /* LDAP_DEPRECATED */
29 #include "ldap_fetcher.h"
31 #define DEFAULT_TIMEOUT 10
33 typedef struct private_ldap_fetcher_t private_ldap_fetcher_t
;
36 * Private Data of a ldap_fetcher_t object.
38 struct private_ldap_fetcher_t
{
42 ldap_fetcher_t
public;
45 * timeout to use for fetches
51 * Parses the result returned by an ldap query
53 static bool parse(LDAP
*ldap
, LDAPMessage
*result
, chunk_t
*response
)
55 LDAPMessage
*entry
= ldap_first_entry(ldap
, result
);
60 BerElement
*ber
= NULL
;
63 attr
= ldap_first_attribute(ldap
, entry
, &ber
);
66 struct berval
**values
= ldap_get_values_len(ldap
, entry
, attr
);
72 *response
= chunk_alloc(values
[0]->bv_len
);
73 memcpy(response
->ptr
, values
[0]->bv_val
, response
->len
);
78 DBG1("LDAP response contains no values");
80 ldap_value_free_len(values
);
84 DBG1("getting LDAP values failed: %s",
85 ldap_err2string(ldap_result2error(ldap
, entry
, 0)));
91 DBG1("finding LDAP attributes failed: %s",
92 ldap_err2string(ldap_result2error(ldap
, entry
, 0)));
98 DBG1("finding first LDAP entry failed: %s",
99 ldap_err2string(ldap_result2error(ldap
, entry
, 0)));
105 static status_t
fetch(private_ldap_fetcher_t
*this, char *url
,
106 chunk_t
*result
, va_list args
)
112 int ldap_version
= LDAP_VERSION3
;
113 struct timeval timeout
;
114 status_t status
= FAILED
;
116 if (!strneq(url
, "ldap", 4))
118 return NOT_SUPPORTED
;
120 if (ldap_url_parse(url
, &lurl
) != LDAP_SUCCESS
)
122 return NOT_SUPPORTED
;
124 ldap
= ldap_init(lurl
->lud_host
, lurl
->lud_port
);
127 DBG1("LDAP initialization failed: %s", strerror(errno
));
128 ldap_free_urldesc(lurl
);
132 timeout
.tv_sec
= this->timeout
;
135 ldap_set_option(ldap
, LDAP_OPT_PROTOCOL_VERSION
, &ldap_version
);
136 ldap_set_option(ldap
, LDAP_OPT_NETWORK_TIMEOUT
, &timeout
);
138 DBG1("sending LDAP request to '%s'...", url
);
140 res
= ldap_simple_bind_s(ldap
, NULL
, NULL
);
141 if (res
== LDAP_SUCCESS
)
143 res
= ldap_search_st(ldap
, lurl
->lud_dn
, lurl
->lud_scope
,
144 lurl
->lud_filter
, lurl
->lud_attrs
,
147 if (res
== LDAP_SUCCESS
)
149 if (parse(ldap
, msg
, result
))
157 DBG1("LDAP search failed: %s", ldap_err2string(res
));
162 DBG1("LDAP bind to '%s' failed: %s", url
, ldap_err2string(res
));
165 ldap_free_urldesc(lurl
);
171 * Implementation of fetcher_t.set_option.
173 static bool set_option(private_ldap_fetcher_t
*this, fetcher_option_t option
, ...)
177 va_start(args
, option
);
182 this->timeout
= va_arg(args
, u_int
);
191 * Implements ldap_fetcher_t.destroy
193 static void destroy(private_ldap_fetcher_t
*this)
199 * Described in header.
201 ldap_fetcher_t
*ldap_fetcher_create()
203 private_ldap_fetcher_t
*this = malloc_thing(private_ldap_fetcher_t
);
205 this->public.interface
.fetch
= (status_t(*)(fetcher_t
*,char*,chunk_t
*))fetch
;
206 this->public.interface
.set_option
= (bool(*)(fetcher_t
*, fetcher_option_t option
, ...))set_option
;
207 this->public.interface
.destroy
= (void (*)(fetcher_t
*))destroy
;
209 this->timeout
= DEFAULT_TIMEOUT
;
211 return &this->public;