5d2ce8f3bb74eeaf90af901f98a6df5dcb3ae037
[ambi/samba-autobuild/.git] / source4 / heimdal / lib / hdb / hdb.c
1 /*
2  * Copyright (c) 1997 - 2004 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: hdb.c,v 1.61 2006/04/24 20:57:58 lha Exp $");
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 _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         if(k->key.keytype == enctype){
83             *key = k;
84             return 0;
85         }
86     return KRB5_PROG_ETYPE_NOSUPP; /* XXX */
87 }
88
89 krb5_error_code
90 hdb_enctype2key(krb5_context context, 
91                 hdb_entry *e, 
92                 krb5_enctype enctype, 
93                 Key **key)
94 {
95     *key = NULL;
96     return hdb_next_enctype2key(context, e, enctype, key);
97 }
98
99 void
100 hdb_free_key(Key *key)
101 {
102     memset(key->key.keyvalue.data, 
103            0,
104            key->key.keyvalue.length);
105     free_Key(key);
106     free(key);
107 }
108
109
110 krb5_error_code
111 hdb_lock(int fd, int operation)
112 {
113     int i, code = 0;
114
115     for(i = 0; i < 3; i++){
116         code = flock(fd, (operation == HDB_RLOCK ? LOCK_SH : LOCK_EX) | LOCK_NB);
117         if(code == 0 || errno != EWOULDBLOCK)
118             break;
119         sleep(1);
120     }
121     if(code == 0)
122         return 0;
123     if(errno == EWOULDBLOCK)
124         return HDB_ERR_DB_INUSE;
125     return HDB_ERR_CANT_LOCK_DB;
126 }
127
128 krb5_error_code
129 hdb_unlock(int fd)
130 {
131     int code;
132     code = flock(fd, LOCK_UN);
133     if(code)
134         return 4711 /* XXX */;
135     return 0;
136 }
137
138 void
139 hdb_free_entry(krb5_context context, hdb_entry_ex *ent)
140 {
141     int i;
142
143     if (ent->free_entry)
144         (*ent->free_entry)(context, ent);
145
146     for(i = 0; i < ent->entry.keys.len; ++i) {
147         Key *k = &ent->entry.keys.val[i];
148
149         memset (k->key.keyvalue.data, 0, k->key.keyvalue.length);
150     }
151     free_hdb_entry(&ent->entry);
152 }
153
154 krb5_error_code
155 hdb_foreach(krb5_context context,
156             HDB *db,
157             unsigned flags,
158             hdb_foreach_func_t func,
159             void *data)
160 {
161     krb5_error_code ret;
162     hdb_entry_ex entry;
163     ret = db->hdb_firstkey(context, db, flags, &entry);
164     while(ret == 0){
165         ret = (*func)(context, db, &entry, data);
166         hdb_free_entry(context, &entry);
167         if(ret == 0)
168             ret = db->hdb_nextkey(context, db, flags, &entry);
169     }
170     if(ret == HDB_ERR_NOENTRY)
171         ret = 0;
172     return ret;
173 }
174
175 krb5_error_code
176 hdb_check_db_format(krb5_context context, HDB *db)
177 {
178     krb5_data tag;
179     krb5_data version;
180     krb5_error_code ret, ret2;
181     unsigned ver;
182     int foo;
183
184     ret = db->hdb_lock(context, db, HDB_RLOCK);
185     if (ret)
186         return ret;
187
188     tag.data = HDB_DB_FORMAT_ENTRY;
189     tag.length = strlen(tag.data);
190     ret = (*db->hdb__get)(context, db, tag, &version);
191     ret2 = db->hdb_unlock(context, db);
192     if(ret)
193         return ret;
194     if (ret2)
195         return ret2;
196     foo = sscanf(version.data, "%u", &ver);
197     krb5_data_free (&version);
198     if (foo != 1)
199         return HDB_ERR_BADVERSION;
200     if(ver != HDB_DB_FORMAT)
201         return HDB_ERR_BADVERSION;
202     return 0;
203 }
204
205 krb5_error_code
206 hdb_init_db(krb5_context context, HDB *db)
207 {
208     krb5_error_code ret, ret2;
209     krb5_data tag;
210     krb5_data version;
211     char ver[32];
212     
213     ret = hdb_check_db_format(context, db);
214     if(ret != HDB_ERR_NOENTRY)
215         return ret;
216     
217     ret = db->hdb_lock(context, db, HDB_WLOCK);
218     if (ret)
219         return ret;
220
221     tag.data = HDB_DB_FORMAT_ENTRY;
222     tag.length = strlen(tag.data);
223     snprintf(ver, sizeof(ver), "%u", HDB_DB_FORMAT);
224     version.data = ver;
225     version.length = strlen(version.data) + 1; /* zero terminated */
226     ret = (*db->hdb__put)(context, db, 0, tag, version);
227     ret2 = db->hdb_unlock(context, db);
228     if (ret)
229         return ret;
230     return ret2;
231 }
232
233 #ifdef HAVE_DLOPEN
234
235  /*
236  * Load a dynamic backend from /usr/heimdal/lib/hdb_NAME.so,
237  * looking for the hdb_NAME_create symbol.
238  */
239
240 static const struct hdb_method *
241 find_dynamic_method (krb5_context context,
242                      const char *filename, 
243                      const char **rest)
244 {
245     static struct hdb_method method;
246     struct hdb_so_method *mso;
247     char *prefix, *path, *symbol;
248     const char *p;
249     void *dl;
250     size_t len;
251     
252     p = strchr(filename, ':');
253
254     /* if no prefix, don't know what module to load, just ignore it */
255     if (p == NULL)
256         return NULL;
257
258     len = p - filename;
259     *rest = filename + len + 1;
260     
261     prefix = strndup(filename, len);
262     if (prefix == NULL)
263         krb5_errx(context, 1, "out of memory");
264     
265     if (asprintf(&path, LIBDIR "/hdb_%s.so", prefix) == -1)
266         krb5_errx(context, 1, "out of memory");
267
268 #ifndef RTLD_NOW
269 #define RTLD_NOW 0
270 #endif
271 #ifndef RTLD_GLOBAL
272 #define RTLD_GLOBAL 0
273 #endif
274
275     dl = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
276     if (dl == NULL) {
277         krb5_warnx(context, "error trying to load dynamic module %s: %s\n",
278                    path, dlerror());
279         free(prefix);
280         free(path);
281         return NULL;
282     }
283     
284     if (asprintf(&symbol, "hdb_%s_interface", prefix) == -1)
285         krb5_errx(context, 1, "out of memory");
286         
287     mso = dlsym(dl, symbol);
288     if (mso == NULL) {
289         krb5_warnx(context, "error finding symbol %s in %s: %s\n", 
290                    symbol, path, dlerror());
291         dlclose(dl);
292         free(symbol);
293         free(prefix);
294         free(path);
295         return NULL;
296     }
297     free(path);
298     free(symbol);
299
300     if (mso->version != HDB_INTERFACE_VERSION) {
301         krb5_warnx(context, 
302                    "error wrong version in shared module %s "
303                    "version: %d should have been %d\n", 
304                    prefix, mso->version, HDB_INTERFACE_VERSION);
305         dlclose(dl);
306         free(prefix);
307         return NULL;
308     }
309
310     if (mso->create == NULL) {
311         krb5_errx(context, 1,
312                   "no entry point function in shared mod %s ",
313                    prefix);
314         dlclose(dl);
315         free(prefix);
316         return NULL;
317     }
318
319     method.create = mso->create;
320     method.prefix = prefix;
321
322     return &method;
323 }
324 #endif /* HAVE_DLOPEN */
325
326 /*
327  * find the relevant method for `filename', returning a pointer to the
328  * rest in `rest'.
329  * return NULL if there's no such method.
330  */
331
332 static const struct hdb_method *
333 find_method (const char *filename, const char **rest)
334 {
335     const struct hdb_method *h;
336
337     for (h = methods; h->prefix != NULL; ++h) {
338         if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0) {
339             *rest = filename + strlen(h->prefix);
340             return h;
341         }
342     }
343 #if defined(HAVE_DB1) || defined(HAVE_DB3) || defined(HAVE_NDBM)
344     if (strncmp(filename, "/", 1) == 0
345         || strncmp(filename, "./", 2) == 0
346         || strncmp(filename, "../", 3) == 0)
347     {
348         *rest = filename;
349         return &dbmetod;
350     }
351 #endif
352
353     return NULL;
354 }
355
356 krb5_error_code
357 hdb_list_builtin(krb5_context context, char **list)
358 {
359     const struct hdb_method *h;
360     size_t len = 0;
361     char *buf = NULL;
362
363     for (h = methods; h->prefix != NULL; ++h) {
364         if (h->prefix[0] == '\0')
365             continue;
366         len += strlen(h->prefix) + 2;
367     }
368
369     len += 1;
370     buf = malloc(len);
371     if (buf == NULL) {
372         krb5_set_error_string(context, "malloc: out of memory");
373         return ENOMEM;
374     }
375     buf[0] = '\0';
376
377     for (h = methods; h->prefix != NULL; ++h) {
378         if (h != methods)
379             strlcat(buf, ", ", len);
380         strlcat(buf, h->prefix, len);
381     }
382     *list = buf;
383     return 0;
384 }
385
386 krb5_error_code
387 hdb_create(krb5_context context, HDB **db, const char *filename)
388 {
389     const struct hdb_method *h;
390     const char *residual;
391
392     if(filename == NULL)
393         filename = HDB_DEFAULT_DB;
394     krb5_add_et_list(context, initialize_hdb_error_table_r);
395     h = find_method (filename, &residual);
396 #ifdef HAVE_DLOPEN
397     if (h == NULL)
398         h = find_dynamic_method (context, filename, &residual);
399 #endif
400     if (h == NULL)
401         krb5_errx(context, 1, "No database support! (hdb_create(%s))", filename);
402     return (*h->create)(context, db, residual);
403 }