Account lockout
[krb5.git] / src / plugins / kdb / ldap / libkdb_ldap / kdb_ldap.h
1 /*
2  * lib/kdb/kdb_ldap/kdb_ldap.h
3  *
4  * Copyright (c) 2004-2005, Novell, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
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.
17  *
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.
29  */
30
31 /* */
32 #ifndef _KDB_LDAP_H
33 #define _KDB_LDAP_H 1
34
35 /* We want the interfaces marked "deprecated" in OpenLDAP.  */
36 #define LDAP_DEPRECATED 1
37 #include <ldap.h>
38
39 /* Check for acceptable versions.
40
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
45    tested.  */
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.
50 #  endif
51 # endif
52 #endif /* BUILD_WITH_BROKEN_LDAP */
53
54 #include <k5-thread.h>
55 #include <kdb5.h>
56 #include "k5-int.h"
57 #include "ldap_krbcontainer.h"
58 #include "ldap_realm.h"
59
60 extern struct timeval timelimit;
61
62 #define  SERV_COUNT                  100
63 #define  DEFAULT_CONNS_PER_SERVER    5
64 #define  REALM_READ_REFRESH_INTERVAL (5 * 60)
65
66 #ifdef HAVE_EDIRECTORY
67 #define  SECURITY_CONTAINER "cn=Security"
68 #define  KERBEROS_CONTAINER "cn=Kerberos,cn=Security"
69 #endif
70
71 #if !defined(LDAP_OPT_RESULT_CODE) && defined(LDAP_OPT_ERROR_NUMBER)
72 #define LDAP_OPT_RESULT_CODE LDAP_OPT_ERROR_NUMBER
73 #endif
74
75 #define MAXINTLEN  10
76
77 #define IGNORE_STATUS              0
78 #define CHECK_STATUS               1
79
80 #define SETUP_CONTEXT() if (context == NULL || context->dal_handle == NULL \
81             || context->dal_handle->db_context == NULL) { \
82         return EINVAL; \
83     } \
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;
88
89 #define GET_HANDLE()  ld = NULL; \
90     st = krb5_ldap_request_handle_from_pool(ldap_context, &ldap_server_handle); \
91     if (st != 0) { \
92         prepend_err_str(context, "LDAP handle unavailable: ", KRB5_KDB_ACCESS_ERROR, st); \
93         st = KRB5_KDB_ACCESS_ERROR; \
94         goto cleanup; \
95     } \
96     ld = ldap_server_handle->ldap_handle;
97
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);
100
101 #define LDAP_SEARCH(base, scope, filter, attrs)   LDAP_SEARCH_1(base, scope, filter, attrs, CHECK_STATUS)
102
103 #define LDAP_SEARCH_1(base, scope, filter, attrs, status_check)        \
104       do { \
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; \
110           } \
111       }while (translate_ldap_error(st, OP_SEARCH) == KRB5_KDB_ACCESS_ERROR && tempst == 0); \
112       \
113       if (status_check != IGNORE_STATUS) { \
114         if (tempst != 0) { \
115             prepend_err_str(context, "LDAP handle unavailable: ", KRB5_KDB_ACCESS_ERROR, st); \
116             st = KRB5_KDB_ACCESS_ERROR; \
117             goto cleanup; \
118         } \
119         if (st != LDAP_SUCCESS) { \
120              st = set_ldap_error(context, st, OP_SEARCH); \
121              goto cleanup; \
122         } \
123       }
124
125
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); \
130             } \
131             prepend_err_str(context, str, st, st); \
132             goto cleanup; \
133          }
134
135 #define CHECK_NULL(ptr) if (ptr == NULL) { \
136                             st = ENOMEM; \
137                             goto cleanup; \
138                         }
139
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))
144
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"
148
149 #define  KDB_TL_USER_INFO      0x7ffe
150
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
158
159
160 #define CHECK_LDAP_HANDLE(lcontext)     if (!(ldap_context \
161                                               && ldap_context->server_info_list)) { \
162                                           return KRB5_KDB_DBNOTINITED; \
163                                         }
164      
165 #define HNDL_LOCK(lcontext) k5_mutex_lock(&lcontext->hndl_lock)
166 #define HNDL_UNLOCK(lcontext) k5_mutex_unlock(&lcontext->hndl_lock)
167
168 /* To be used later */
169 typedef struct _krb5_ldap_certificates{
170     char *certificate;
171     int  certtype;
172 }krb5_ldap_certificates;
173
174 /* ldap server info structure */
175
176 typedef enum _server_type {PRIMARY, SECONDARY} krb5_ldap_server_type;
177
178 typedef enum _server_status {OFF, ON, NOTSET} krb5_ldap_server_status;
179
180 typedef struct _krb5_ldap_server_info krb5_ldap_server_info;
181
182 typedef struct  _krb5_ldap_server_handle {
183     int                              msgid;
184     LDAP                             *ldap_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;
189
190 struct _krb5_ldap_server_info {
191     krb5_ldap_server_type        server_type;
192     krb5_ldap_server_status      server_status;
193     krb5_ui_4                    num_conns;
194     krb5_ldap_server_handle      *ldap_server_handles;
195     time_t                       downtime;
196     char                        *server_name;
197 #ifdef HAVE_EDIRECTORY
198     char                        *root_certificate_file;
199 #endif
200     int                          modify_increment;
201     struct _krb5_ldap_server_info *next;
202 };
203
204
205 /* ldap server structure */
206
207 typedef enum {SERVICE_DN_TYPE_SERVER, SERVICE_DN_TYPE_CLIENT} krb5_ldap_servicetype; 
208
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;
213   char                          *conf_section;
214   char                          *bind_dn;
215   char                          *bind_pwd;
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 */
226 } krb5_ldap_context;
227
228
229 typedef struct {
230   int           nkey;
231   struct berval **keys;
232 }KEY;
233
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))
237
238
239 /* misc functions */
240
241 krb5_error_code
242 krb5_ldap_db_init(krb5_context, krb5_ldap_context *);
243
244 krb5_error_code
245 krb5_ldap_db_single_init(krb5_ldap_context *);
246
247 krb5_error_code
248 krb5_ldap_rebind(krb5_ldap_context *, krb5_ldap_server_handle **);
249
250 krb5_error_code
251 krb5_ldap_db_get_age(krb5_context, char *, time_t *);
252
253 krb5_error_code 
254 krb5_ldap_lib_init(void);
255
256 krb5_error_code 
257 krb5_ldap_lib_cleanup(void);
258
259 void * 
260 krb5_ldap_alloc( krb5_context kcontext,  void *ptr, size_t size );
261
262 void 
263 krb5_ldap_free( krb5_context kcontext, void *ptr );
264 krb5_error_code
265 krb5_ldap_get_mkey(krb5_context, krb5_keyblock **);
266
267 krb5_error_code
268 krb5_ldap_set_mkey(krb5_context, char *, krb5_keyblock *);
269
270 krb5_error_code
271 krb5_ldap_get_mkey_list (krb5_context context, krb5_keylist_node **key_list);
272
273 krb5_error_code
274 krb5_ldap_set_mkey_list(krb5_context, krb5_keylist_node *);
275
276 krb5_error_code
277 krb5_ldap_create(krb5_context , char *, char **);
278
279 krb5_error_code 
280 krb5_ldap_open( krb5_context , char *,
281                 char **db_args,
282                 int mode );
283 krb5_error_code 
284 krb5_ldap_close( krb5_context );
285
286 krb5_error_code
287 krb5_ldap_free_ldap_context(krb5_ldap_context *);
288
289 krb5_error_code
290 krb5_ldap_read_startup_information(krb5_context );
291
292 int
293 has_sasl_external_mech(krb5_context, char *);
294
295 int
296 has_modify_increment(krb5_context, char *);
297
298 krb5_error_code
299 krb5_ldap_free_server_context_params(krb5_ldap_context *ldap_context);
300
301
302 /* DAL functions */
303
304
305 krb5_error_code
306 krb5_ldap_set_option( krb5_context, int, void * );
307
308 krb5_error_code
309 krb5_ldap_lock( krb5_context, int );
310
311 krb5_error_code
312 krb5_ldap_unlock( krb5_context );
313
314 krb5_error_code
315 krb5_ldap_supported_realms( krb5_context, char ** );
316
317 krb5_error_code
318 krb5_ldap_free_supported_realms( krb5_context, char ** );
319
320 const char *
321 krb5_ldap_errcode_2_string( krb5_context, long );
322
323 void
324 krb5_ldap_release_errcode_string (krb5_context, const char *);
325
326 #ifndef HAVE_LDAP_INITIALIZE
327 int
328 ldap_initialize(LDAP **, char *);
329 #endif
330 #ifndef HAVE_LDAP_UNBIND_EXT_S
331 int
332 ldap_unbind_ext_s(LDAP *, LDAPControl **, LDAPControl **);
333 #endif
334
335 /* lockout.c */
336 krb5_error_code
337 krb5_ldap_lockout_check_policy(krb5_context context,
338                                krb5_db_entry *entry,
339                                krb5_timestamp stamp);
340
341 krb5_error_code
342 krb5_ldap_lockout_audit(krb5_context context,
343                         krb5_db_entry *entry,
344                         krb5_timestamp stamp,
345                         krb5_error_code status);
346
347 /* kdb_ext.c */
348 krb5_error_code
349 krb5_ldap_invoke(krb5_context context,
350                  unsigned int method,
351                  const krb5_data *req,
352                  krb5_data *rep);
353
354 #endif