Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-test
[kai/samba.git] / source4 / heimdal / lib / hdb / hdb.c
1 /*
2  * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "hdb_locl.h"
35
36 RCSID("$Id$");
37
38 #ifdef HAVE_DLFCN_H
39 #include <dlfcn.h>
40 #endif
41
42 struct hdb_method {
43     const char *prefix;
44     krb5_error_code (*create)(krb5_context, HDB **, const char *filename);
45 };
46
47 static struct hdb_method methods[] = {
48 #if HAVE_DB1 || HAVE_DB3
49     {"db:",     hdb_db_create},
50 #endif
51 #if HAVE_NDBM
52     {"ndbm:",   hdb_ndbm_create},
53 #endif
54 #if defined(OPENLDAP) && !defined(OPENLDAP_MODULE)
55     {"ldap:",   hdb_ldap_create},
56     {"ldapi:",  hdb_ldapi_create},
57 #endif
58 #ifdef HAVE_LDB /* Used for integrated samba build */
59     {"ldb:",    hdb_ldb_create},
60 #endif
61     {NULL,      NULL}
62 };
63
64 #if HAVE_DB1 || HAVE_DB3
65 static struct hdb_method dbmetod = {"", hdb_db_create };
66 #elif defined(HAVE_NDBM)
67 static struct hdb_method dbmetod = {"", hdb_ndbm_create };
68 #endif
69
70
71 krb5_error_code
72 hdb_next_enctype2key(krb5_context context,
73                      const hdb_entry *e,
74                      krb5_enctype enctype,
75                      Key **key)
76 {
77     Key *k;
78     
79     for (k = *key ? (*key) + 1 : e->keys.val;
80          k < e->keys.val + e->keys.len; 
81          k++) 
82     {
83         if(k->key.keytype == enctype){
84             *key = k;
85             return 0;
86         }
87     }
88     krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
89                            "No next enctype %d for hdb-entry", 
90                           (int)enctype);
91     return KRB5_PROG_ETYPE_NOSUPP; /* XXX */
92 }
93
94 krb5_error_code
95 hdb_enctype2key(krb5_context context, 
96                 hdb_entry *e, 
97                 krb5_enctype enctype, 
98                 Key **key)
99 {
100     *key = NULL;
101     return hdb_next_enctype2key(context, e, enctype, key);
102 }
103
104 void
105 hdb_free_key(Key *key)
106 {
107     memset(key->key.keyvalue.data, 
108            0,
109            key->key.keyvalue.length);
110     free_Key(key);
111     free(key);
112 }
113
114
115 krb5_error_code
116 hdb_lock(int fd, int operation)
117 {
118     int i, code = 0;
119
120     for(i = 0; i < 3; i++){
121         code = flock(fd, (operation == HDB_RLOCK ? LOCK_SH : LOCK_EX) | LOCK_NB);
122         if(code == 0 || errno != EWOULDBLOCK)
123             break;
124         sleep(1);
125     }
126     if(code == 0)
127         return 0;
128     if(errno == EWOULDBLOCK)
129         return HDB_ERR_DB_INUSE;
130     return HDB_ERR_CANT_LOCK_DB;
131 }
132
133 krb5_error_code
134 hdb_unlock(int fd)
135 {
136     int code;
137     code = flock(fd, LOCK_UN);
138     if(code)
139         return 4711 /* XXX */;
140     return 0;
141 }
142
143 void
144 hdb_free_entry(krb5_context context, hdb_entry_ex *ent)
145 {
146     int i;
147
148     if (ent->free_entry)
149         (*ent->free_entry)(context, ent);
150
151     for(i = 0; i < ent->entry.keys.len; ++i) {
152         Key *k = &ent->entry.keys.val[i];
153
154         memset (k->key.keyvalue.data, 0, k->key.keyvalue.length);
155     }
156     free_hdb_entry(&ent->entry);
157 }
158
159 krb5_error_code
160 hdb_foreach(krb5_context context,
161             HDB *db,
162             unsigned flags,
163             hdb_foreach_func_t func,
164             void *data)
165 {
166     krb5_error_code ret;
167     hdb_entry_ex entry;
168     ret = db->hdb_firstkey(context, db, flags, &entry);
169     if (ret == 0)
170         krb5_clear_error_string(context);
171     while(ret == 0){
172         ret = (*func)(context, db, &entry, data);
173         hdb_free_entry(context, &entry);
174         if(ret == 0)
175             ret = db->hdb_nextkey(context, db, flags, &entry);
176     }
177     if(ret == HDB_ERR_NOENTRY)
178         ret = 0;
179     return ret;
180 }
181
182 krb5_error_code
183 hdb_check_db_format(krb5_context context, HDB *db)
184 {
185     krb5_data tag;
186     krb5_data version;
187     krb5_error_code ret, ret2;
188     unsigned ver;
189     int foo;
190
191     ret = db->hdb_lock(context, db, HDB_RLOCK);
192     if (ret)
193         return ret;
194
195     tag.data = HDB_DB_FORMAT_ENTRY;
196     tag.length = strlen(tag.data);
197     ret = (*db->hdb__get)(context, db, tag, &version);
198     ret2 = db->hdb_unlock(context, db);
199     if(ret)
200         return ret;
201     if (ret2)
202         return ret2;
203     foo = sscanf(version.data, "%u", &ver);
204     krb5_data_free (&version);
205     if (foo != 1)
206         return HDB_ERR_BADVERSION;
207     if(ver != HDB_DB_FORMAT)
208         return HDB_ERR_BADVERSION;
209     return 0;
210 }
211
212 krb5_error_code
213 hdb_init_db(krb5_context context, HDB *db)
214 {
215     krb5_error_code ret, ret2;
216     krb5_data tag;
217     krb5_data version;
218     char ver[32];
219     
220     ret = hdb_check_db_format(context, db);
221     if(ret != HDB_ERR_NOENTRY)
222         return ret;
223     
224     ret = db->hdb_lock(context, db, HDB_WLOCK);
225     if (ret)
226         return ret;
227
228     tag.data = HDB_DB_FORMAT_ENTRY;
229     tag.length = strlen(tag.data);
230     snprintf(ver, sizeof(ver), "%u", HDB_DB_FORMAT);
231     version.data = ver;
232     version.length = strlen(version.data) + 1; /* zero terminated */
233     ret = (*db->hdb__put)(context, db, 0, tag, version);
234     ret2 = db->hdb_unlock(context, db);
235     if (ret) {
236         if (ret2)
237             krb5_clear_error_string(context);
238         return ret;
239     }
240     return ret2;
241 }
242
243 #ifdef HAVE_DLOPEN
244
245  /*
246  * Load a dynamic backend from /usr/heimdal/lib/hdb_NAME.so,
247  * looking for the hdb_NAME_create symbol.
248  */
249
250 static const struct hdb_method *
251 find_dynamic_method (krb5_context context,
252                      const char *filename, 
253                      const char **rest)
254 {
255     static struct hdb_method method;
256     struct hdb_so_method *mso;
257     char *prefix, *path, *symbol;
258     const char *p;
259     void *dl;
260     size_t len;
261     
262     p = strchr(filename, ':');
263
264     /* if no prefix, don't know what module to load, just ignore it */
265     if (p == NULL)
266         return NULL;
267
268     len = p - filename;
269     *rest = filename + len + 1;
270     
271     prefix = strndup(filename, len);
272     if (prefix == NULL)
273         krb5_errx(context, 1, "out of memory");
274     
275     if (asprintf(&path, LIBDIR "/hdb_%s.so", prefix) == -1)
276         krb5_errx(context, 1, "out of memory");
277
278 #ifndef RTLD_NOW
279 #define RTLD_NOW 0
280 #endif
281 #ifndef RTLD_GLOBAL
282 #define RTLD_GLOBAL 0
283 #endif
284
285     dl = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
286     if (dl == NULL) {
287         krb5_warnx(context, "error trying to load dynamic module %s: %s\n",
288                    path, dlerror());
289         free(prefix);
290         free(path);
291         return NULL;
292     }
293     
294     if (asprintf(&symbol, "hdb_%s_interface", prefix) == -1)
295         krb5_errx(context, 1, "out of memory");
296         
297     mso = dlsym(dl, symbol);
298     if (mso == NULL) {
299         krb5_warnx(context, "error finding symbol %s in %s: %s\n", 
300                    symbol, path, dlerror());
301         dlclose(dl);
302         free(symbol);
303         free(prefix);
304         free(path);
305         return NULL;
306     }
307     free(path);
308     free(symbol);
309
310     if (mso->version != HDB_INTERFACE_VERSION) {
311         krb5_warnx(context, 
312                    "error wrong version in shared module %s "
313                    "version: %d should have been %d\n", 
314                    prefix, mso->version, HDB_INTERFACE_VERSION);
315         dlclose(dl);
316         free(prefix);
317         return NULL;
318     }
319
320     if (mso->create == NULL) {
321         krb5_errx(context, 1,
322                   "no entry point function in shared mod %s ",
323                    prefix);
324         dlclose(dl);
325         free(prefix);
326         return NULL;
327     }
328
329     method.create = mso->create;
330     method.prefix = prefix;
331
332     return &method;
333 }
334 #endif /* HAVE_DLOPEN */
335
336 /*
337  * find the relevant method for `filename', returning a pointer to the
338  * rest in `rest'.
339  * return NULL if there's no such method.
340  */
341
342 static const struct hdb_method *
343 find_method (const char *filename, const char **rest)
344 {
345     const struct hdb_method *h;
346
347     for (h = methods; h->prefix != NULL; ++h) {
348         if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0) {
349             *rest = filename + strlen(h->prefix);
350             return h;
351         }
352     }
353 #if defined(HAVE_DB1) || defined(HAVE_DB3) || defined(HAVE_NDBM)
354     if (strncmp(filename, "/", 1) == 0
355         || strncmp(filename, "./", 2) == 0
356         || strncmp(filename, "../", 3) == 0)
357     {
358         *rest = filename;
359         return &dbmetod;
360     }
361 #endif
362
363     return NULL;
364 }
365
366 krb5_error_code
367 hdb_list_builtin(krb5_context context, char **list)
368 {
369     const struct hdb_method *h;
370     size_t len = 0;
371     char *buf = NULL;
372
373     for (h = methods; h->prefix != NULL; ++h) {
374         if (h->prefix[0] == '\0')
375             continue;
376         len += strlen(h->prefix) + 2;
377     }
378
379     len += 1;
380     buf = malloc(len);
381     if (buf == NULL) {
382         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
383         return ENOMEM;
384     }
385     buf[0] = '\0';
386
387     for (h = methods; h->prefix != NULL; ++h) {
388         if (h != methods)
389             strlcat(buf, ", ", len);
390         strlcat(buf, h->prefix, len);
391     }
392     *list = buf;
393     return 0;
394 }
395
396 krb5_error_code
397 hdb_create(krb5_context context, HDB **db, const char *filename)
398 {
399     const struct hdb_method *h;
400     const char *residual;
401
402     if(filename == NULL)
403         filename = HDB_DEFAULT_DB;
404     krb5_add_et_list(context, initialize_hdb_error_table_r);
405     h = find_method (filename, &residual);
406 #ifdef HAVE_DLOPEN
407     if (h == NULL)
408         h = find_dynamic_method (context, filename, &residual);
409 #endif
410     if (h == NULL)
411         krb5_errx(context, 1, "No database support for %s", filename);
412     return (*h->create)(context, db, residual);
413 }