Debian Codegraph @

lib/hdb/db.c

raw
1 /*
2 * Copyright (c) 1997 - 2001 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 #if defined(HAVE_DB1)
37
38 #if defined(HAVE_DB_185_H)
39 #include <db_185.h>
40 #elif defined(HAVE_DB_H)
41 #include <db.h>
42 #endif
43
44 typedef struct {
45 HDB hdb; /* generic members */
46 int lock_fd; /* DB-specific */
47 int do_sync; /* DB-specific */
48 } DB1_HDB;
49
50 static krb5_error_code
51 DB_close(krb5_context context, HDB *db)
52 {
53 DB1_HDB *db1 = (DB1_HDB *)db;
54 DB *d = (DB*)db->hdb_db;
55
56 heim_assert(d != 0, "Closing already closed HDB");
57
58 (*d->close)(d);
59 db->hdb_db = 0;
60
61 if (db1->lock_fd >= 0) {
62 close(db1->lock_fd);
63 db1->lock_fd = -1;
64 }
65
66 return 0;
67 }
68
69 static krb5_error_code
70 DB_destroy(krb5_context context, HDB *db)
71 {
72 krb5_error_code ret;
73
74 ret = hdb_clear_master_key (context, db);
75 free(db->hdb_name);
76 free(db);
77 return ret;
78 }
79
80 static krb5_error_code
81 DB_set_sync(krb5_context context, HDB *db, int on)
82 {
83 DB1_HDB *db1 = (DB1_HDB *)db;
84 DB *d = (DB*)db->hdb_db;
85 krb5_error_code ret = 0;
86
87 db1->do_sync = on;
88 if (on) {
89 ret = (*d->sync)(d, 0);
90 if (ret == -1) {
91 ret = errno;
92 krb5_set_error_message(context, ret, "Database %s put sync error: %s",
93 db->hdb_name, strerror(ret));
94 }
95 }
96 return ret;
97 }
98
99 static krb5_error_code
100 DB_lock(krb5_context context, HDB *db, int operation)
101 {
102
103 return 0;
104 }
105
106 static krb5_error_code
107 DB_unlock(krb5_context context, HDB *db)
108 {
109
110 return 0;
111 }
112
113
114 static krb5_error_code
115 DB_seq(krb5_context context, HDB *db,
116 unsigned flags, hdb_entry_ex *entry, int flag)
117 {
118 DB *d = (DB*)db->hdb_db;
119 DBT key, value;
120 krb5_data key_data, data;
121 int code;
122
123 code = (*d->seq)(d, &key, &value, flag);
124 if(code == -1) {
125 code = errno;
126 krb5_set_error_message(context, code, "Database %s seq error: %s",
127 db->hdb_name, strerror(code));
128 return code;
129 }
130 if(code == 1) {
131 krb5_clear_error_message(context);
132 return HDB_ERR_NOENTRY;
133 }
134
135 key_data.data = key.data;
136 key_data.length = key.size;
137 data.data = value.data;
138 data.length = value.size;
139 memset(entry, 0, sizeof(*entry));
140 if (hdb_value2entry(context, &data, &entry->entry))
141 return DB_seq(context, db, flags, entry, R_NEXT);
142 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
143 code = hdb_unseal_keys (context, db, &entry->entry);
144 if (code)
145 hdb_free_entry (context, entry);
146 }
147 if (code == 0 && entry->entry.principal == NULL) {
148 entry->entry.principal = malloc(sizeof(*entry->entry.principal));
149 if (entry->entry.principal == NULL) {
150 code = ENOMEM;
151 krb5_set_error_message(context, code, "malloc: out of memory");
152 hdb_free_entry (context, entry);
153 } else {
154 hdb_key2principal(context, &key_data, entry->entry.principal);
155 }
156 }
157 return code;
158 }
159
160
161 static krb5_error_code
162 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
163 {
164 return DB_seq(context, db, flags, entry, R_FIRST);
165 }
166
167
168 static krb5_error_code
169 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
170 {
171 return DB_seq(context, db, flags, entry, R_NEXT);
172 }
173
174 static krb5_error_code
175 DB_rename(krb5_context context, HDB *db, const char *new_name)
176 {
177 int ret;
178 char *old, *new;
179
180 if (strncmp(new_name, "db:", sizeof("db:") - 1) == 0)
181 new_name += sizeof("db:") - 1;
182 else if (strncmp(new_name, "db1:", sizeof("db1:") - 1) == 0)
183 new_name += sizeof("db1:") - 1;
184 asprintf(&old, "%s.db", db->hdb_name);
185 asprintf(&new, "%s.db", new_name);
186 ret = rename(old, new);
187 free(old);
188 free(new);
189 if(ret)
190 return errno;
191
192 free(db->hdb_name);
193 db->hdb_name = strdup(new_name);
194 return 0;
195 }
196
197 static krb5_error_code
198 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
199 {
200 DB *d = (DB*)db->hdb_db;
201 DBT k, v;
202 int code;
203
204 k.data = key.data;
205 k.size = key.length;
206 code = (*d->get)(d, &k, &v, 0);
207 if(code < 0) {
208 code = errno;
209 krb5_set_error_message(context, code, "Database %s get error: %s",
210 db->hdb_name, strerror(code));
211 return code;
212 }
213 if(code == 1) {
214 krb5_clear_error_message(context);
215 return HDB_ERR_NOENTRY;
216 }
217
218 krb5_data_copy(reply, v.data, v.size);
219 return 0;
220 }
221
222 static krb5_error_code
223 DB__put(krb5_context context, HDB *db, int replace,
224 krb5_data key, krb5_data value)
225 {
226 DB1_HDB *db1 = (DB1_HDB *)db;
227 DB *d = (DB*)db->hdb_db;
228 DBT k, v;
229 int code;
230
231 k.data = key.data;
232 k.size = key.length;
233 v.data = value.data;
234 v.size = value.length;
235 krb5_clear_error_message(context);
236 code = (*d->put)(d, &k, &v, replace ? 0 : R_NOOVERWRITE);
237 if(code < 0) {
238 code = errno;
239 krb5_set_error_message(context, code, "Database %s put error: %s",
240 db->hdb_name, strerror(code));
241 return code;
242 }
243 if(code == 1) {
244 return HDB_ERR_EXISTS;
245 }
246
247 return db->hdb_set_sync(context, db, db1->do_sync);
248 }
249
250 static krb5_error_code
251 DB__del(krb5_context context, HDB *db, krb5_data key)
252 {
253 DB1_HDB *db1 = (DB1_HDB *)db;
254 DB *d = (DB*)db->hdb_db;
255 DBT k;
256 krb5_error_code code;
257 k.data = key.data;
258 k.size = key.length;
259 krb5_clear_error_message(context);
260 code = (*d->del)(d, &k, 0);
261 if (code == 1)
262 return HDB_ERR_NOENTRY;
263 if (code < 0) {
264 code = errno;
265 krb5_set_error_message(context, code, "Database %s del error: %s",
266 db->hdb_name, strerror(code));
267 return code;
268 }
269 return db->hdb_set_sync(context, db, db1->do_sync);
270 }
271
272 static DB *
273 _open_db(char *fn, int flags, int mode, int *fd)
274 {
275 #ifndef O_EXLOCK
276 int op;
277 int ret;
278
279 *fd = open(fn, flags, mode);
280 if (*fd == -1)
281 return NULL;
282
283 if ((flags & O_ACCMODE) == O_RDONLY)
284 op = LOCK_SH;
285 else
286 op = LOCK_EX;
287
288 ret = flock(*fd, op);
289 if (ret == -1) {
290 int saved_errno;
291
292 saved_errno = errno;
293 close(*fd);
294 errno = saved_errno;
295 return NULL;
296 }
297 #else
298 if ((flags & O_ACCMODE) == O_RDONLY)
299 flags |= O_SHLOCK;
300 else
301 flags |= O_EXLOCK;
302 #endif
303
304 return dbopen(fn, flags, mode, DB_BTREE, NULL);
305 }
306
307 static krb5_error_code
308 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
309 {
310 DB1_HDB *db1 = (DB1_HDB *)db;
311 char *fn;
312 krb5_error_code ret;
313
314 asprintf(&fn, "%s.db", db->hdb_name);
315 if (fn == NULL) {
316 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
317 return ENOMEM;
318 }
319 db->hdb_db = _open_db(fn, flags, mode, &db1->lock_fd);
320 free(fn);
321 /* try to open without .db extension */
322 if(db->hdb_db == NULL && errno == ENOENT)
323 db->hdb_db = _open_db(db->hdb_name, flags, mode, &db1->lock_fd);
324 if(db->hdb_db == NULL) {
325 krb5_set_error_message(context, errno, "dbopen (%s): %s",
326 db->hdb_name, strerror(errno));
327 return errno;
328 }
329 if((flags & O_ACCMODE) == O_RDONLY)
330 ret = hdb_check_db_format(context, db);
331 else
332 ret = hdb_init_db(context, db);
333 if(ret == HDB_ERR_NOENTRY) {
334 krb5_clear_error_message(context);
335 return 0;
336 }
337 if (ret) {
338 DB_close(context, db);
339 krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
340 (flags & O_ACCMODE) == O_RDONLY ?
341 "checking format of" : "initialize",
342 db->hdb_name);
343 }
344 return ret;
345 }
346
347 krb5_error_code
348 hdb_db1_create(krb5_context context, HDB **db,
349 const char *filename)
350 {
351 DB1_HDB **db1 = (DB1_HDB **)db;
352 *db = calloc(1, sizeof(**db1)); /* Allocate space for the larger db1 */
353 if (*db == NULL) {
354 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
355 return ENOMEM;
356 }
357
358 (*db)->hdb_db = NULL;
359 (*db)->hdb_name = strdup(filename);
360 if ((*db)->hdb_name == NULL) {
361 free(*db);
362 *db = NULL;
363 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
364 return ENOMEM;
365 }
366 (*db)->hdb_master_key_set = 0;
367 (*db)->hdb_openp = 0;
368 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
369 (*db)->hdb_open = DB_open;
370 (*db)->hdb_close = DB_close;
371 (*db)->hdb_fetch_kvno = _hdb_fetch_kvno;
372 (*db)->hdb_store = _hdb_store;
373 (*db)->hdb_remove = _hdb_remove;
374 (*db)->hdb_firstkey = DB_firstkey;
375 (*db)->hdb_nextkey= DB_nextkey;
376 (*db)->hdb_lock = DB_lock;
377 (*db)->hdb_unlock = DB_unlock;
378 (*db)->hdb_rename = DB_rename;
379 (*db)->hdb__get = DB__get;
380 (*db)->hdb__put = DB__put;
381 (*db)->hdb__del = DB__del;
382 (*db)->hdb_destroy = DB_destroy;
383 (*db)->hdb_set_sync = DB_set_sync;
384
385 (*db1)->lock_fd = -1;
386 (*db1)->do_sync = 1;
387 return 0;
388 }
389
390 #endif /* defined(HAVE_DB1) */