ldap-client.h revision 3b840237852271bfb8e6f4258c3656edc72e9a79
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi#ifndef LDAP_CLIENT_H
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi#define LDAP_CLIENT_H
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomienum ldap_scope {
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi LDAP_SEARCH_SCOPE_BASE = 0x0000,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi LDAP_SEARCH_SCOPE_ONE = 0x0001,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi LDAP_SEARCH_SCOPE_SUBTREE = 0x0002
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi};
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_client;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_result;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_search_iterator;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_entry;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi/* Called when the LDAP result has finished. The callback must verify first
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi if the result is valid or not by calling ldap_result_has_failed() or
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi ldap_result_get_error(). The result is freed automatically after this
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi callback finishes. */
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomitypedef void ldap_result_callback_t(struct ldap_result *result, void *context);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_client_settings {
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *uri;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *bind_dn;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *password;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const struct ssl_iostream_settings *ssl_set;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi unsigned int timeout_secs;
3b840237852271bfb8e6f4258c3656edc72e9a79Aki Tuomi unsigned int max_idle_time_secs;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi unsigned int debug;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi};
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_search_input {
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *base_dn;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *filter;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *const *attributes;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi enum ldap_scope scope;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi unsigned int size_limit;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi unsigned int timeout_secs;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi};
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_compare_input {
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *dn;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *attr;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const char *value;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi unsigned int timeout_secs;
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi};
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi/* Initialize LDAP. Returns 0 on success, or -1 and error_r if initialization
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi failed with the given settings. */
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomiint ldap_client_init(const struct ldap_client_settings *set,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi struct ldap_client **client_r, const char **error_r);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomivoid ldap_client_deinit(struct ldap_client **client);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomivoid ldap_client_switch_ioloop(struct ldap_client *client);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomivoid ldap_search_start(struct ldap_client *client,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const struct ldap_search_input *input,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi ldap_result_callback_t *callback,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi void *context);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi#define ldap_search_start(client, input, callback, context) \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi ldap_search_start(client, input + \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi CALLBACK_TYPECHECK(callback, void (*)( \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi struct ldap_result *, typeof(context))), \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi (ldap_result_callback_t *)callback, context)
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi/* Returns TRUE if the LDAP query failed and result must not be used further. */
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomibool ldap_result_has_failed(struct ldap_result *result);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi/* Returns the error string if the query had failed, or NULL if it hasn't. */
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomiconst char *ldap_result_get_error(struct ldap_result *result);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomistruct ldap_search_iterator* ldap_search_iterator_init(struct ldap_result *result);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomiconst struct ldap_entry *ldap_search_iterator_next(struct ldap_search_iterator *iter);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomivoid ldap_search_iterator_deinit(struct ldap_search_iterator **iter);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomivoid ldap_compare_start(struct ldap_client *client,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi const struct ldap_compare_input *input,
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi ldap_result_callback_t *callback, void *context);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi#define ldap_compare_start(client, input, callback, context) \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi ldap_compare_start(client, input + \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi CALLBACK_TYPECHECK(callback, void (*)( \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi struct ldap_result *, typeof(context))), \
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi (ldap_result_callback_t *)callback, context)
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi/* Returns TRUE if the comparison matched, FALSE if not. */
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomibool ldap_compare_result(struct ldap_result *result);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomiconst char *ldap_entry_dn(const struct ldap_entry *entry);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomiconst char *const *ldap_entry_get_attributes(const struct ldap_entry *entry);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomiconst char *const *ldap_entry_get_attribute(const struct ldap_entry *entry, const char *attribute);
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi
844129da33797fba15314372a0dfab4cfe8162e8Aki Tuomi#endif