aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/kdb/db2/adb_openclose.c
blob: 9a506e9d44a01e897746aedd9ddc9cec93600a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
 *
 * $Header$
 */

#include        <k5-int.h>
#include        <sys/file.h>
#include        <fcntl.h>
#include        <unistd.h>
#include        "policy_db.h"
#include        <stdlib.h>
#include        <db.h>

struct _locklist {
    osa_adb_lock_ent lockinfo;
    struct _locklist *next;
};

krb5_error_code
osa_adb_create_db(char *filename, char *lockfilename, int magic)
{
    int lf;
    DB *db;
    BTREEINFO btinfo;

    memset(&btinfo, 0, sizeof(btinfo));
    btinfo.flags = 0;
    btinfo.cachesize = 0;
    btinfo.psize = 4096;
    btinfo.lorder = 0;
    btinfo.minkeypage = 0;
    btinfo.compare = NULL;
    btinfo.prefix = NULL;
    db = dbopen(filename, O_RDWR | O_CREAT | O_EXCL, 0600, DB_BTREE, &btinfo);
    if (db == NULL)
        return errno;
    if (db->close(db) < 0)
        return errno;

    /* only create the lock file if we successfully created the db */
    lf = THREEPARAMOPEN(lockfilename, O_RDWR | O_CREAT | O_EXCL, 0600);
    if (lf == -1)
        return errno;
    (void) close(lf);

    return OSA_ADB_OK;
}

krb5_error_code
osa_adb_destroy_db(char *filename, char *lockfilename, int magic)
{
    /* the admin databases do not contain security-critical data */
    if (unlink(filename) < 0 ||
        unlink(lockfilename) < 0)
        return errno;
    return OSA_ADB_OK;
}

krb5_error_code
osa_adb_init_db(osa_adb_db_t *dbp, char *filename, char *lockfilename,
                int magic)
{
    osa_adb_db_t db;
    static struct _locklist *locklist = NULL;
    struct _locklist *lockp;
    krb5_error_code code;

    if (dbp == NULL || filename == NULL)
        return EINVAL;

    db = (osa_adb_princ_t) malloc(sizeof(osa_adb_db_ent));
    if (db == NULL)
        return ENOMEM;

    memset(db, 0, sizeof(*db));
    db->info.hash = NULL;
    db->info.bsize = 256;
    db->info.ffactor = 8;
    db->info.nelem = 25000;
    db->info.lorder = 0;

    db->btinfo.flags = 0;
    db->btinfo.cachesize = 0;
    db->btinfo.psize = 4096;
    db->btinfo.lorder = 0;
    db->btinfo.minkeypage = 0;
    db->btinfo.compare = NULL;
    db->btinfo.prefix = NULL;
    /*
     * A process is allowed to open the same database multiple times
     * and access it via different handles.  If the handles use
     * distinct lockinfo structures, things get confused: lock(A),
     * lock(B), release(B) will result in the kernel unlocking the
     * lock file but handle A will still think the file is locked.
     * Therefore, all handles using the same lock file must share a
     * single lockinfo structure.
     *
     * It is not sufficient to have a single lockinfo structure,
     * however, because a single process may also wish to open
     * multiple different databases simultaneously, with different
     * lock files.  This code used to use a single static lockinfo
     * structure, which means that the second database opened used
     * the first database's lock file.  This was Bad.
     *
     * We now maintain a linked list of lockinfo structures, keyed by
     * lockfilename.  An entry is added when this function is called
     * with a new lockfilename, and all subsequent calls with that
     * lockfilename use the existing entry, updating the refcnt.
     * When the database is closed with fini_db(), the refcnt is
     * decremented, and when it is zero the lockinfo structure is
     * freed and reset.  The entry in the linked list, however, is
     * never removed; it will just be reinitialized the next time
     * init_db is called with the right lockfilename.
     */

    /* find or create the lockinfo structure for lockfilename */
    lockp = locklist;
    while (lockp) {
        if (strcmp(lockp->lockinfo.filename, lockfilename) == 0)
            break;
        else
            lockp = lockp->next;
    }
    if (lockp == NULL) {
        /* doesn't exist, create it, add to list */
        lockp = (struct _locklist *) malloc(sizeof(*lockp));
        if (lockp == NULL) {
            free(db);
            return ENOMEM;
        }
        memset(lockp, 0, sizeof(*lockp));
        lockp->lockinfo.filename = strdup(lockfilename);
        if (lockp->lockinfo.filename == NULL) {
            free(lockp);
            free(db);
            return ENOMEM;
        }
        lockp->next = locklist;
        locklist = lockp;
    }

    /* now initialize lockp->lockinfo if necessary */
    if (lockp->lockinfo.lockfile == NULL) {
        if ((code = krb5int_init_context_kdc(&lockp->lockinfo.context))) {
            free(db);
            return((krb5_error_code) code);
        }

        /*
         * needs be open read/write so that write locking can work with
         * POSIX systems
         */
        if ((lockp->lockinfo.lockfile = fopen(lockfilename, "r+")) == NULL) {
            /*
             * maybe someone took away write permission so we could only
             * get shared locks?
             */
            if ((lockp->lockinfo.lockfile = fopen(lockfilename, "r"))
                == NULL) {
                free(db);
                return OSA_ADB_NOLOCKFILE;
            }
        }
        set_cloexec_file(lockp->lockinfo.lockfile);
        lockp->lockinfo.lockmode = lockp->lockinfo.lockcnt = 0;
    }

    /* lockp is set, lockinfo is initialized, update the reference count */
    db->lock = &lockp->lockinfo;
    db->lock->refcnt++;

    db->opencnt = 0;
    db->filename = strdup(filename);
    db->magic = magic;

    *dbp = db;

    return OSA_ADB_OK;
}

krb5_error_code
osa_adb_fini_db(osa_adb_db_t db, int magic)
{
    if (db->magic != magic)
        return EINVAL;
    if (db->lock->refcnt == 0) {
        /* barry says this can't happen */
        return OSA_ADB_FAILURE;
    } else {
        db->lock->refcnt--;
    }

    if (db->lock->refcnt == 0) {
        /*
         * Don't free db->lock->filename, it is used as a key to
         * find the lockinfo entry in the linked list.  If the
         * lockfile doesn't exist, we must be closing the database
         * after trashing it.  This has to be allowed, so don't
         * generate an error.
         */
        if (db->lock->lockmode != KRB5_DB_LOCKMODE_PERMANENT)
            (void) fclose(db->lock->lockfile);
        db->lock->lockfile = NULL;
        krb5_free_context(db->lock->context);
    }

    db->magic = 0;
    free(db->filename);
    free(db);
    return OSA_ADB_OK;
}

krb5_error_code
osa_adb_get_lock(osa_adb_db_t db, int mode)
{
    int perm, krb5_mode, ret = 0;

    if (db->lock->lockmode >= mode) {
        /* No need to upgrade lock, just incr refcnt and return */
        db->lock->lockcnt++;
        return(OSA_ADB_OK);
    }

    perm = 0;
    switch (mode) {
    case KRB5_DB_LOCKMODE_PERMANENT:
        perm = 1;
    case KRB5_DB_LOCKMODE_EXCLUSIVE:
        krb5_mode = KRB5_LOCKMODE_EXCLUSIVE;
        break;
    case KRB5_DB_LOCKMODE_SHARED:
        krb5_mode = KRB5_LOCKMODE_SHARED;
        break;
    default:
        return(EINVAL);
    }

    ret = krb5_lock_file(db->lock->context, fileno(db->lock->lockfile),
                         krb5_mode);
    if (ret == EBADF && mode == KRB5_DB_LOCKMODE_EXCLUSIVE)
        return OSA_ADB_NOEXCL_PERM;
    else if (ret == EACCES || ret == EAGAIN || ret == EWOULDBLOCK)
        return OSA_ADB_CANTLOCK_DB;
    else if (ret != 0)
        return ret;

    /*
     * If the file no longer exists, someone acquired a permanent
     * lock.  If that process terminates its exclusive lock is lost,
     * but if we already had the file open we can (probably) lock it
     * even though it has been unlinked.  So we need to insist that
     * it exist.
     */
    if (access(db->lock->filename, F_OK) < 0) {
        (void) krb5_lock_file(db->lock->context,
                              fileno(db->lock->lockfile),
                              KRB5_LOCKMODE_UNLOCK);
        return OSA_ADB_NOLOCKFILE;
    }

    /* we have the shared/exclusive lock */

    if (perm) {
        if (unlink(db->lock->filename) < 0) {
            /* somehow we can't delete the file, but we already */
            /* have the lock, so release it and return */

            ret = errno;
            (void) krb5_lock_file(db->lock->context,
                                  fileno(db->lock->lockfile),
                                  KRB5_LOCKMODE_UNLOCK);

            /* maybe we should return CANTLOCK_DB.. but that would */
            /* look just like the db was already locked */
            return ret;
        }

        /* this releases our exclusive lock.. which is okay because */
        /* now no one else can get one either */
        (void) fclose(db->lock->lockfile);
    }

    db->lock->lockmode = mode;
    db->lock->lockcnt++;
    return OSA_ADB_OK;
}

krb5_error_code
osa_adb_release_lock(osa_adb_db_t db)
{
    int ret, fd;

    if (!db->lock->lockcnt)            /* lock already unlocked */
        return OSA_ADB_NOTLOCKED;

    if (--db->lock->lockcnt == 0) {
        if (db->lock->lockmode == KRB5_DB_LOCKMODE_PERMANENT) {
            /* now we need to create the file since it does not exist */
            fd = THREEPARAMOPEN(db->lock->filename,O_RDWR | O_CREAT | O_EXCL,
                                0600);
            if (fd < 0)
                return OSA_ADB_NOLOCKFILE;
            set_cloexec_fd(fd);
            if ((db->lock->lockfile = fdopen(fd, "w+")) == NULL)
                return OSA_ADB_NOLOCKFILE;
        } else if ((ret = krb5_lock_file(db->lock->context,
                                         fileno(db->lock->lockfile),
                                         KRB5_LOCKMODE_UNLOCK)))
            return ret;

        db->lock->lockmode = 0;
    }
    return OSA_ADB_OK;
}

krb5_error_code
osa_adb_open_and_lock(osa_adb_princ_t db, int locktype)
{
    int ret;

    ret = osa_adb_get_lock(db, locktype);
    if (ret != OSA_ADB_OK)
        return ret;
    if (db->opencnt)
        goto open_ok;

    db->db = dbopen(db->filename, O_RDWR, 0600, DB_BTREE, &db->btinfo);
    if (db->db == NULL && IS_EFTYPE(errno))
        db->db = dbopen(db->filename, O_RDWR, 0600, DB_HASH, &db->info);
    if (db->db == NULL) {
        (void)osa_adb_release_lock(db);
        return (errno == EINVAL) ? OSA_ADB_BAD_DB : errno;
    }

open_ok:
    db->opencnt++;
    return OSA_ADB_OK;
}

krb5_error_code
osa_adb_close_and_unlock(osa_adb_princ_t db)
{
    if (--db->opencnt)
        return osa_adb_release_lock(db);
    if(db->db != NULL && db->db->close(db->db) == -1) {
        (void) osa_adb_release_lock(db);
        return OSA_ADB_FAILURE;
    }

    db->db = NULL;

    return(osa_adb_release_lock(db));
}