2 * lib/kdb/kdb_ldap/kdb_ldap.h
4 * Copyright (c) 2004-2005, Novell, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * The copyright holder's name is not used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
35 /* We want the interfaces marked "deprecated" in OpenLDAP. */
36 #define LDAP_DEPRECATED 1
39 /* Check for acceptable versions.
41 OpenLDAP version 2.2.6 is known to have some kind of problem that
42 is tickled by the use of multiple handles in this code. Version
43 2.2.19 in Mac OS 10.4.7 seems to be buggy as well. Version 2.2.24
44 doesn't have this problem. Other in-between versions have not been
46 #ifndef BUILD_WITH_BROKEN_LDAP
47 # if defined(LDAP_API_FEATURE_X_OPENLDAP)
48 # if LDAP_VENDOR_VERSION < 20224
49 # error This code triggers bugs in old OpenLDAP implementations. Please update to 2.2.24 or later.
52 #endif /* BUILD_WITH_BROKEN_LDAP */
54 #include <k5-thread.h>
57 #include "ldap_krbcontainer.h"
58 #include "ldap_realm.h"
60 extern struct timeval timelimit;
62 #define SERV_COUNT 100
63 #define DEFAULT_CONNS_PER_SERVER 5
64 #define REALM_READ_REFRESH_INTERVAL (5 * 60)
66 #ifdef HAVE_EDIRECTORY
67 #define SECURITY_CONTAINER "cn=Security"
68 #define KERBEROS_CONTAINER "cn=Kerberos,cn=Security"
71 #if !defined(LDAP_OPT_RESULT_CODE) && defined(LDAP_OPT_ERROR_NUMBER)
72 #define LDAP_OPT_RESULT_CODE LDAP_OPT_ERROR_NUMBER
77 #define IGNORE_STATUS 0
78 #define CHECK_STATUS 1
80 #define SETUP_CONTEXT() if (context == NULL || context->dal_handle == NULL \
81 || context->dal_handle->db_context == NULL) { \
84 dal_handle = context->dal_handle; \
85 ldap_context = (krb5_ldap_context *) dal_handle->db_context; \
86 if (ldap_context == NULL || ldap_context->server_info_list == NULL) \
87 return KRB5_KDB_DBNOTINITED;
89 #define GET_HANDLE() ld = NULL; \
90 st = krb5_ldap_request_handle_from_pool(ldap_context, &ldap_server_handle); \
92 prepend_err_str(context, "LDAP handle unavailable: ", KRB5_KDB_ACCESS_ERROR, st); \
93 st = KRB5_KDB_ACCESS_ERROR; \
96 ld = ldap_server_handle->ldap_handle;
98 extern int set_ldap_error (krb5_context ctx, int st, int op);
99 extern void prepend_err_str (krb5_context ctx, const char *s, krb5_error_code err, krb5_error_code oerr);
101 #define LDAP_SEARCH(base, scope, filter, attrs) LDAP_SEARCH_1(base, scope, filter, attrs, CHECK_STATUS)
103 #define LDAP_SEARCH_1(base, scope, filter, attrs, status_check) \
105 st = ldap_search_ext_s(ld, base, scope, filter, attrs, 0, NULL, NULL, &timelimit, LDAP_NO_LIMIT, &result); \
106 if (translate_ldap_error(st, OP_SEARCH) == KRB5_KDB_ACCESS_ERROR) { \
107 tempst = krb5_ldap_rebind(ldap_context, &ldap_server_handle); \
108 if (ldap_server_handle) \
109 ld = ldap_server_handle->ldap_handle; \
111 }while (translate_ldap_error(st, OP_SEARCH) == KRB5_KDB_ACCESS_ERROR && tempst == 0); \
113 if (status_check != IGNORE_STATUS) { \
115 prepend_err_str(context, "LDAP handle unavailable: ", KRB5_KDB_ACCESS_ERROR, st); \
116 st = KRB5_KDB_ACCESS_ERROR; \
119 if (st != LDAP_SUCCESS) { \
120 st = set_ldap_error(context, st, OP_SEARCH); \
126 #define CHECK_CLASS_VALIDITY(st, mask, str) \
127 if (st != 0 || mask == 0) { \
128 if (st == 0 && mask == 0) { \
129 st = set_ldap_error(context, LDAP_OBJECT_CLASS_VIOLATION, OP_SEARCH); \
131 prepend_err_str(context, str, st, st); \
135 #define CHECK_NULL(ptr) if (ptr == NULL) { \
140 #define STORE16_INT(ptr, val) store_16_be(val, ptr)
141 #define STORE32_INT(ptr, val) store_32_be(val, ptr)
142 #define UNSTORE16_INT(ptr, val) (val = load_16_be(ptr))
143 #define UNSTORE32_INT(ptr, val) (val = load_32_be(ptr))
145 #define KRB5_CONF_KDC_BIND_DN "ldap_kdc_dn"
146 #define KRB5_CONF_ADMIN_BIND_DN "ldap_kadmind_dn"
147 #define KRB5_CONF_PWD_BIND_DN "ldap_passwd_dn"
149 #define KDB_TL_USER_INFO 0x7ffe
151 #define KDB_TL_PRINCTYPE 0x01
152 #define KDB_TL_PRINCCOUNT 0x02
153 #define KDB_TL_USERDN 0x03
154 #define KDB_TL_KEYINFO 0x04
155 #define KDB_TL_MASK 0x05
156 #define KDB_TL_CONTAINERDN 0x06
157 #define KDB_TL_LINKDN 0x07
160 #define CHECK_LDAP_HANDLE(lcontext) if (!(ldap_context \
161 && ldap_context->server_info_list)) { \
162 return KRB5_KDB_DBNOTINITED; \
165 #define HNDL_LOCK(lcontext) k5_mutex_lock(&lcontext->hndl_lock)
166 #define HNDL_UNLOCK(lcontext) k5_mutex_unlock(&lcontext->hndl_lock)
168 /* To be used later */
169 typedef struct _krb5_ldap_certificates{
172 }krb5_ldap_certificates;
174 /* ldap server info structure */
176 typedef enum _server_type {PRIMARY, SECONDARY} krb5_ldap_server_type;
178 typedef enum _server_status {OFF, ON, NOTSET} krb5_ldap_server_status;
180 typedef struct _krb5_ldap_server_info krb5_ldap_server_info;
182 typedef struct _krb5_ldap_server_handle {
185 krb5_boolean server_info_update_pending;
186 krb5_ldap_server_info *server_info;
187 struct _krb5_ldap_server_handle *next;
188 } krb5_ldap_server_handle;
190 struct _krb5_ldap_server_info {
191 krb5_ldap_server_type server_type;
192 krb5_ldap_server_status server_status;
194 krb5_ldap_server_handle *ldap_server_handles;
197 #ifdef HAVE_EDIRECTORY
198 char *root_certificate_file;
200 int modify_increment;
201 struct _krb5_ldap_server_info *next;
205 /* ldap server structure */
207 typedef enum {SERVICE_DN_TYPE_SERVER, SERVICE_DN_TYPE_CLIENT} krb5_ldap_servicetype;
209 typedef struct _krb5_ldap_context {
210 krb5_ldap_servicetype service_type;
211 krb5_ldap_server_info **server_info_list;
212 krb5_ui_4 max_server_conns;
216 char *service_password_file;
217 char *root_certificate_file;
218 char *service_cert_path;
219 char *service_cert_pass;
220 krb5_ldap_certificates **certificates;
221 krb5_ui_4 cert_count; /* certificate count */
222 k5_mutex_t hndl_lock;
223 krb5_ldap_krbcontainer_params *krbcontainer;
224 krb5_ldap_realm_params *lrparams;
225 krb5_context kcontext; /* to set the error code and message */
231 struct berval **keys;
234 #define k5ldap_inited(c) (c && c->db_context \
235 && ((kdb5_dal_handle*)c->db_context)->db_context \
236 && ((krb5_ldap_context *) ((kdb5_dal_handle*)c->db_context)->db_context))
242 krb5_ldap_db_init(krb5_context, krb5_ldap_context *);
245 krb5_ldap_db_single_init(krb5_ldap_context *);
248 krb5_ldap_rebind(krb5_ldap_context *, krb5_ldap_server_handle **);
251 krb5_ldap_db_get_age(krb5_context, char *, time_t *);
254 krb5_ldap_lib_init(void);
257 krb5_ldap_lib_cleanup(void);
260 krb5_ldap_alloc( krb5_context kcontext, void *ptr, size_t size );
263 krb5_ldap_free( krb5_context kcontext, void *ptr );
265 krb5_ldap_get_mkey(krb5_context, krb5_keyblock **);
268 krb5_ldap_set_mkey(krb5_context, char *, krb5_keyblock *);
271 krb5_ldap_get_mkey_list (krb5_context context, krb5_keylist_node **key_list);
274 krb5_ldap_set_mkey_list(krb5_context, krb5_keylist_node *);
277 krb5_ldap_create(krb5_context , char *, char **);
280 krb5_ldap_open( krb5_context , char *,
284 krb5_ldap_close( krb5_context );
287 krb5_ldap_free_ldap_context(krb5_ldap_context *);
290 krb5_ldap_read_startup_information(krb5_context );
293 has_sasl_external_mech(krb5_context, char *);
296 has_modify_increment(krb5_context, char *);
299 krb5_ldap_free_server_context_params(krb5_ldap_context *ldap_context);
306 krb5_ldap_set_option( krb5_context, int, void * );
309 krb5_ldap_lock( krb5_context, int );
312 krb5_ldap_unlock( krb5_context );
315 krb5_ldap_supported_realms( krb5_context, char ** );
318 krb5_ldap_free_supported_realms( krb5_context, char ** );
321 krb5_ldap_errcode_2_string( krb5_context, long );
324 krb5_ldap_release_errcode_string (krb5_context, const char *);
326 #ifndef HAVE_LDAP_INITIALIZE
328 ldap_initialize(LDAP **, char *);
330 #ifndef HAVE_LDAP_UNBIND_EXT_S
332 ldap_unbind_ext_s(LDAP *, LDAPControl **, LDAPControl **);
337 krb5_ldap_lockout_check_policy(krb5_context context,
338 krb5_db_entry *entry,
339 krb5_timestamp stamp);
342 krb5_ldap_lockout_audit(krb5_context context,
343 krb5_db_entry *entry,
344 krb5_timestamp stamp,
345 krb5_error_code status);
349 krb5_ldap_invoke(krb5_context context,
351 const krb5_data *req,