#if !defined(lint) && !defined(__CODECENTER__)
static char *rcsid = "$Header$";
#endif
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#include <k5-int.h>
#include "policy_db.h"
#include <stdlib.h>
#include <db.h>
#define MAX_LOCK_TRIES 5
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;
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)
{
if (unlink(filename) < 0 ||
unlink(lockfilename) < 0)
return errno;
return OSA_ADB_OK;
}
krb5_error_code osa_adb_rename_db(char *filefrom, char *lockfrom,
char *fileto, char *lockto, int magic)
{
osa_adb_db_t fromdb, todb;
krb5_error_code ret;
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, KRB5_DB_LOCKMODE_PERMANENT))) {
(void) osa_adb_fini_db(fromdb, magic);
(void) osa_adb_fini_db(todb, magic);
return ret;
}
if ((ret = osa_adb_get_lock(todb, KRB5_DB_LOCKMODE_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;
}
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;
}
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;
lockp = locklist;
while (lockp) {
if (strcmp(lockp->lockinfo.filename, lockfilename) == 0)
break;
else
lockp = lockp->next;
}
if (lockp == NULL) {
lockp = (struct _locklist *) malloc(sizeof(*lockp));
if (lockp == NULL) {
free(db);
return ENOMEM;
}
memset(lockp, 0, sizeof(*lockp));
lockp->next = locklist;
locklist = lockp;
}
if (lockp->lockinfo.lockfile == NULL) {
if ((code = krb5int_init_context_kdc(&lockp->lockinfo.context))) {
free(db);
return((krb5_error_code) code);
}
lockp->lockinfo.filename = strdup(lockfilename);
if ((lockp->lockinfo.lockfile = fopen(lockfilename, "r+")) == NULL) {
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;
}
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) {
return OSA_ADB_FAILURE;
} else {
db->lock->refcnt--;
}
if (db->lock->refcnt == 0) {
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 tries, gotlock, perm, krb5_mode, ret = 0;
if (db->lock->lockmode >= mode) {
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);
}
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 == KRB5_DB_LOCKMODE_EXCLUSIVE)
return OSA_ADB_NOEXCL_PERM;
sleep(1);
}
if (ret == EACCES || ret == EAGAIN || ret == EWOULDBLOCK)
return OSA_ADB_CANTLOCK_DB;
else if (ret != 0)
return ret;
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;
}
if (perm) {
if (unlink(db->lock->filename) < 0) {
ret = errno;
(void) krb5_lock_file(db->lock->context,
fileno(db->lock->lockfile),
KRB5_LOCKMODE_UNLOCK);
return ret;
}
(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)
return OSA_ADB_NOTLOCKED;
if (--db->lock->lockcnt == 0) {
if (db->lock->lockmode == KRB5_DB_LOCKMODE_PERMANENT) {
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)
goto open_ok;
switch (errno) {
#ifdef EFTYPE
case EFTYPE:
#endif
case EINVAL:
db->db = dbopen(db->filename, O_RDWR, 0600, DB_HASH, &db->info);
if (db->db != NULL)
goto open_ok;
default:
(void) osa_adb_release_lock(db);
if (errno == EINVAL)
return OSA_ADB_BAD_DB;
return 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));
}