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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/*
* Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
*
* $Header$
*/
#if !defined(lint) && !defined(__CODECENTER__)
static char *rcsid = "$Header$";
#endif
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#include "adb.h"
#include <stdlib.h>
#define MAX_LOCK_TRIES 5
struct _locklist {
osa_adb_lock_ent lockinfo;
struct _locklist *next;
};
osa_adb_ret_t osa_adb_create_db(char *filename, char *lockfilename,
int magic)
{
int lf;
DB *db;
HASHINFO info;
memset(&info, 0, sizeof(info));
info.hash = NULL;
info.bsize = 256;
info.ffactor = 8;
info.nelem = 25000;
info.lorder = 0;
db = dbopen(filename, O_RDWR | O_CREAT | O_EXCL, 0600, DB_HASH, &info);
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;
}
osa_adb_ret_t 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;
}
osa_adb_ret_t osa_adb_rename_db(char *filefrom, char *lockfrom,
char *fileto, char *lockto, int magic)
{
osa_adb_db_t fromdb, todb;
osa_adb_ret_t ret;
/* make sure todb exists */
if ((ret = osa_adb_create_db(fileto, lockto, magic)) &&
ret != EEXIST)
return ret;
if (ret = osa_adb_init_db(&fromdb, filefrom, lockfrom, magic))
return ret;
if (ret = osa_adb_init_db(&todb, fileto, lockto, magic)) {
(void) osa_adb_fini_db(fromdb, magic);
return ret;
}
if (ret = osa_adb_get_lock(fromdb, OSA_ADB_PERMANENT)) {
(void) osa_adb_fini_db(fromdb, magic);
(void) osa_adb_fini_db(todb, magic);
return ret;
}
if (ret = osa_adb_get_lock(todb, OSA_ADB_PERMANENT)) {
(void) osa_adb_fini_db(fromdb, magic);
(void) osa_adb_fini_db(todb, magic);
return ret;
}
if (rename(filefrom, fileto) < 0) {
(void) osa_adb_fini_db(fromdb, magic);
(void) osa_adb_fini_db(todb, magic);
return errno;
}
/*
* Do not release the lock on fromdb because it is being renamed
* out of existence; no one can ever use it again.
*/
if (ret = osa_adb_release_lock(todb)) {
(void) osa_adb_fini_db(fromdb, magic);
(void) osa_adb_fini_db(todb, magic);
return ret;
}
(void) osa_adb_fini_db(fromdb, magic);
(void) osa_adb_fini_db(todb, magic);
return 0;
}
osa_adb_ret_t 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;
/*
* 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->next = locklist;
locklist = lockp;
}
/* now initialize lockp->lockinfo if necessary */
if (lockp->lockinfo.lockfile == NULL) {
if (code = krb5_init_context(&lockp->lockinfo.context)) {
free(db);
return((osa_adb_ret_t) code);
}
/*
* needs be open read/write so that write locking can work with
* POSIX systems
*/
lockp->lockinfo.filename = strdup(lockfilename);
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;
}
}
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->filename = strdup(filename);
db->magic = magic;
*dbp = db;
return OSA_ADB_OK;
}
osa_adb_ret_t 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 != OSA_ADB_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;
}
osa_adb_ret_t osa_adb_get_lock(osa_adb_db_t db, int mode)
{
int tries, gotlock, perm, krb5_mode, ret;
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 OSA_ADB_PERMANENT:
perm = 1;
case OSA_ADB_EXCLUSIVE:
krb5_mode = KRB5_LOCKMODE_EXCLUSIVE;
break;
case OSA_ADB_SHARED:
krb5_mode = KRB5_LOCKMODE_SHARED;
break;
default:
return(EINVAL);
}
for (gotlock = tries = 0; tries < MAX_LOCK_TRIES; tries++) {
if ((ret = krb5_lock_file(db->lock->context,
fileno(db->lock->lockfile),
krb5_mode|KRB5_LOCKMODE_DONTBLOCK)) == 0) {
gotlock++;
break;
} else if (ret == EBADF && mode == OSA_ADB_EXCLUSIVE)
/* tried to exclusive-lock something we don't have */
/* write access to */
return OSA_ADB_NOEXCL_PERM;
sleep(1);
}
/* test for all the likely "can't get lock" error codes */
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) {
int ret;
/* 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;
}
osa_adb_ret_t 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 == OSA_ADB_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 ((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;
}
osa_adb_ret_t 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;
db->db = dbopen(db->filename, O_RDWR, 0600, DB_HASH, &db->info);
if (db->db == NULL) {
(void) osa_adb_release_lock(db);
if(errno == EINVAL)
return OSA_ADB_BAD_DB;
return errno;
}
return OSA_ADB_OK;
}
osa_adb_ret_t osa_adb_close_and_unlock(osa_adb_princ_t db)
{
int ret;
if(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));
}
|