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