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