1994-09-30 14:50:09 +00:00
|
|
|
/*
|
1995-05-30 06:41:30 +00:00
|
|
|
* Copyright 1988 by the Massachusetts Institute of Technology.
|
1994-09-30 14:50:09 +00:00
|
|
|
* For copying and distribution information, please see the file
|
1995-05-30 06:41:30 +00:00
|
|
|
* <Copyright.MIT>.
|
1994-09-30 14:50:09 +00:00
|
|
|
*
|
|
|
|
* from: krb_dbm.c,v 4.9 89/04/18 16:15:13 wesommer Exp $
|
1995-08-25 22:52:32 +00:00
|
|
|
* $Id: krb_dbm.c,v 1.4 1995/08/03 17:15:42 mark Exp $
|
|
|
|
*/
|
1995-09-07 21:39:00 +00:00
|
|
|
|
1995-08-25 22:52:32 +00:00
|
|
|
#if 0
|
1994-09-30 14:50:09 +00:00
|
|
|
#ifndef lint
|
|
|
|
static char rcsid[] =
|
1995-08-25 22:52:32 +00:00
|
|
|
"$Id: krb_dbm.c,v 1.4 1995/08/03 17:15:42 mark Exp $";
|
1994-09-30 14:50:09 +00:00
|
|
|
#endif lint
|
1995-08-25 22:52:32 +00:00
|
|
|
#endif
|
1994-09-30 14:50:09 +00:00
|
|
|
|
1995-08-02 23:08:18 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__)
|
1995-09-07 21:39:00 +00:00
|
|
|
#define _NDBM_
|
1995-08-25 22:52:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__)
|
1995-09-07 21:39:00 +00:00
|
|
|
#define _DBM_
|
1994-09-30 14:50:09 +00:00
|
|
|
#endif
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
#include <unistd.h>
|
1994-09-30 14:50:09 +00:00
|
|
|
#include <stdio.h>
|
1995-08-25 22:52:32 +00:00
|
|
|
#include <stdlib.h>
|
1994-09-30 14:50:09 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <des.h>
|
|
|
|
#include <sys/file.h>
|
1995-09-07 21:39:00 +00:00
|
|
|
#ifdef _NDBM_
|
1994-09-30 14:50:09 +00:00
|
|
|
#include <ndbm.h>
|
1995-09-07 21:39:00 +00:00
|
|
|
#else /*_NDBM_*/
|
1994-09-30 14:50:09 +00:00
|
|
|
#include <dbm.h>
|
1995-09-07 21:39:00 +00:00
|
|
|
#endif /*_NDBM_*/
|
1994-09-30 14:50:09 +00:00
|
|
|
/* before krb_db.h */
|
|
|
|
#include <krb.h>
|
|
|
|
#include <krb_db.h>
|
|
|
|
|
1995-08-02 23:08:18 +00:00
|
|
|
#ifdef dbm_pagfno
|
1995-09-07 21:39:00 +00:00
|
|
|
#define DB
|
1995-08-02 23:08:18 +00:00
|
|
|
#endif
|
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
#define KERB_DB_MAX_RETRY 5
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
extern int debug;
|
|
|
|
extern long kerb_debug;
|
|
|
|
extern char *progname;
|
|
|
|
#endif
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static init = 0;
|
1994-09-30 14:50:09 +00:00
|
|
|
static char default_db_name[] = DBM_FILE;
|
|
|
|
static char *current_db_name = default_db_name;
|
1995-09-07 21:39:00 +00:00
|
|
|
static void encode_princ_key(datum *key, char *name, char *instance);
|
|
|
|
static void decode_princ_key(datum *key, char *name, char *instance);
|
|
|
|
static void encode_princ_contents(datum *contents, Principal *principal);
|
|
|
|
static void decode_princ_contents(datum *contents, Principal *principal);
|
|
|
|
static void kerb_dbl_fini(void);
|
|
|
|
static int kerb_dbl_lock(int mode);
|
|
|
|
static void kerb_dbl_unlock(void);
|
|
|
|
static long kerb_start_update(char *db_name);
|
|
|
|
static long kerb_end_update(char *db_name, long age);
|
1994-09-30 14:50:09 +00:00
|
|
|
|
|
|
|
static struct timeval timestamp;/* current time of request */
|
|
|
|
static int non_blocking = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This module contains all of the code which directly interfaces to
|
|
|
|
* the underlying representation of the Kerberos database; this
|
|
|
|
* implementation uses a DBM or NDBM indexed "file" (actually
|
|
|
|
* implemented as two separate files) to store the relations, plus a
|
|
|
|
* third file as a semaphore to allow the database to be replaced out
|
|
|
|
* from underneath the KDC server.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Locking:
|
1995-05-30 06:41:30 +00:00
|
|
|
*
|
1994-09-30 14:50:09 +00:00
|
|
|
* There are two distinct locking protocols used. One is designed to
|
|
|
|
* lock against processes (the admin_server, for one) which make
|
|
|
|
* incremental changes to the database; the other is designed to lock
|
|
|
|
* against utilities (kdb_util, kpropd) which replace the entire
|
|
|
|
* database in one fell swoop.
|
|
|
|
*
|
1995-05-30 06:41:30 +00:00
|
|
|
* The first locking protocol is implemented using flock() in the
|
1994-09-30 14:50:09 +00:00
|
|
|
* krb_dbl_lock() and krb_dbl_unlock routines.
|
|
|
|
*
|
|
|
|
* The second locking protocol is necessary because DBM "files" are
|
|
|
|
* actually implemented as two separate files, and it is impossible to
|
|
|
|
* atomically rename two files simultaneously. It assumes that the
|
|
|
|
* database is replaced only very infrequently in comparison to the time
|
|
|
|
* needed to do a database read operation.
|
|
|
|
*
|
|
|
|
* A third file is used as a "version" semaphore; the modification
|
|
|
|
* time of this file is the "version number" of the database.
|
|
|
|
* At the start of a read operation, the reader checks the version
|
|
|
|
* number; at the end of the read operation, it checks again. If the
|
|
|
|
* version number changed, or if the semaphore was nonexistant at
|
|
|
|
* either time, the reader sleeps for a second to let things
|
|
|
|
* stabilize, and then tries again; if it does not succeed after
|
|
|
|
* KERB_DB_MAX_RETRY attempts, it gives up.
|
1995-05-30 06:41:30 +00:00
|
|
|
*
|
1994-09-30 14:50:09 +00:00
|
|
|
* On update, the semaphore file is deleted (if it exists) before any
|
|
|
|
* update takes place; at the end of the update, it is replaced, with
|
|
|
|
* a version number strictly greater than the version number which
|
|
|
|
* existed at the start of the update.
|
1995-05-30 06:41:30 +00:00
|
|
|
*
|
1994-09-30 14:50:09 +00:00
|
|
|
* If the system crashes in the middle of an update, the semaphore
|
|
|
|
* file is not automatically created on reboot; this is a feature, not
|
|
|
|
* a bug, since the database may be inconsistant. Note that the
|
|
|
|
* absence of a semaphore file does not prevent another _update_ from
|
|
|
|
* taking place later. Database replacements take place automatically
|
|
|
|
* only on slave servers; a crash in the middle of an update will be
|
|
|
|
* fixed by the next slave propagation. A crash in the middle of an
|
|
|
|
* update on the master would be somewhat more serious, but this would
|
|
|
|
* likely be noticed by an administrator, who could fix the problem and
|
|
|
|
* retry the operation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Macros to convert ndbm names to dbm names.
|
|
|
|
* Note that dbm_nextkey() cannot be simply converted using a macro, since
|
|
|
|
* it is invoked giving the database, and nextkey() needs the previous key.
|
|
|
|
*
|
|
|
|
* Instead, all routines call "dbm_next" instead.
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
#ifndef _NDBM_
|
1994-09-30 14:50:09 +00:00
|
|
|
typedef char DBM;
|
|
|
|
|
|
|
|
#define dbm_open(file, flags, mode) ((dbminit(file) == 0)?"":((char *)0))
|
|
|
|
#define dbm_fetch(db, key) fetch(key)
|
|
|
|
#define dbm_store(db, key, content, flag) store(key, content)
|
|
|
|
#define dbm_firstkey(db) firstkey()
|
|
|
|
#define dbm_next(db,key) nextkey(key)
|
|
|
|
#define dbm_close(db) dbmclose()
|
|
|
|
#else
|
|
|
|
#define dbm_next(db,key) dbm_nextkey(db)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Utility routine: generate name of database file.
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static char *gen_dbsuffix(db_name, sfx)
|
|
|
|
char *db_name;
|
|
|
|
char *sfx;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
char *dbsuffix;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
if (sfx == NULL)
|
|
|
|
sfx = ".ok";
|
|
|
|
|
|
|
|
dbsuffix = malloc (strlen(db_name) + strlen(sfx) + 1);
|
|
|
|
strcpy(dbsuffix, db_name);
|
|
|
|
strcat(dbsuffix, sfx);
|
|
|
|
return dbsuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialization for data base routines.
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_init()
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
init = 1;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gracefully shut down database--must be called by ANY program that does
|
1995-05-30 06:41:30 +00:00
|
|
|
* a kerb_db_init
|
1994-09-30 14:50:09 +00:00
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
void
|
|
|
|
kerb_db_fini()
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the "name" of the current database to some alternate value.
|
|
|
|
*
|
|
|
|
* Passing a null pointer as "name" will set back to the default.
|
|
|
|
* If the alternate database doesn't exist, nothing is changed.
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_set_name(name)
|
|
|
|
char *name;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
DBM *db;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
name = default_db_name;
|
|
|
|
db = dbm_open(name, 0, 0);
|
|
|
|
if (db == NULL)
|
|
|
|
return errno;
|
|
|
|
dbm_close(db);
|
|
|
|
kerb_dbl_fini();
|
|
|
|
current_db_name = name;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the last modification time of the database.
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
long
|
|
|
|
kerb_get_db_age()
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *okname;
|
|
|
|
long age;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
okname = gen_dbsuffix(current_db_name, ".ok");
|
|
|
|
|
|
|
|
if (stat (okname, &st) < 0)
|
|
|
|
age = 0;
|
|
|
|
else
|
|
|
|
age = st.st_mtime;
|
|
|
|
|
|
|
|
free (okname);
|
|
|
|
return age;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the semaphore file; indicates that database is currently
|
|
|
|
* under renovation.
|
|
|
|
*
|
|
|
|
* This is only for use when moving the database out from underneath
|
|
|
|
* the server (for example, during slave updates).
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static long
|
|
|
|
kerb_start_update(db_name)
|
|
|
|
char *db_name;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
char *okname = gen_dbsuffix(db_name, ".ok");
|
|
|
|
long age = kerb_get_db_age();
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
if (unlink(okname) < 0
|
|
|
|
&& errno != ENOENT) {
|
|
|
|
age = -1;
|
|
|
|
}
|
|
|
|
free (okname);
|
|
|
|
return age;
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static long
|
|
|
|
kerb_end_update(db_name, age)
|
|
|
|
char *db_name;
|
|
|
|
long age;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int retval = 0;
|
|
|
|
char *new_okname = gen_dbsuffix(db_name, ".ok#");
|
|
|
|
char *okname = gen_dbsuffix(db_name, ".ok");
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
fd = open (new_okname, O_CREAT|O_RDWR|O_TRUNC, 0600);
|
|
|
|
if (fd < 0)
|
|
|
|
retval = errno;
|
|
|
|
else {
|
|
|
|
struct stat st;
|
|
|
|
struct timeval tv[2];
|
|
|
|
/* make sure that semaphore is "after" previous value. */
|
|
|
|
if (fstat (fd, &st) == 0
|
|
|
|
&& st.st_mtime <= age) {
|
|
|
|
tv[0].tv_sec = st.st_atime;
|
|
|
|
tv[0].tv_usec = 0;
|
|
|
|
tv[1].tv_sec = age;
|
|
|
|
tv[1].tv_usec = 0;
|
|
|
|
/* set times.. */
|
|
|
|
utimes (new_okname, tv);
|
|
|
|
fsync(fd);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if (rename (new_okname, okname) < 0)
|
|
|
|
retval = errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (new_okname);
|
|
|
|
free (okname);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static long
|
|
|
|
kerb_start_read()
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
return kerb_get_db_age();
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static long
|
|
|
|
kerb_end_read(age)
|
|
|
|
u_long age;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
if (kerb_get_db_age() != age || age == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the database, assuming it's not there.
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_create(db_name)
|
|
|
|
char *db_name;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
char *okname = gen_dbsuffix(db_name, ".ok");
|
|
|
|
int fd;
|
|
|
|
register int ret = 0;
|
1995-09-07 21:39:00 +00:00
|
|
|
#ifdef _NDBM_
|
1994-09-30 14:50:09 +00:00
|
|
|
DBM *db;
|
|
|
|
|
|
|
|
db = dbm_open(db_name, O_RDWR|O_CREAT|O_EXCL, 0600);
|
|
|
|
if (db == NULL)
|
|
|
|
ret = errno;
|
|
|
|
else
|
|
|
|
dbm_close(db);
|
|
|
|
#else
|
|
|
|
char *dirname = gen_dbsuffix(db_name, ".dir");
|
|
|
|
char *pagname = gen_dbsuffix(db_name, ".pag");
|
|
|
|
|
|
|
|
fd = open(dirname, O_RDWR|O_CREAT|O_EXCL, 0600);
|
|
|
|
if (fd < 0)
|
|
|
|
ret = errno;
|
|
|
|
else {
|
|
|
|
close(fd);
|
|
|
|
fd = open (pagname, O_RDWR|O_CREAT|O_EXCL, 0600);
|
|
|
|
if (fd < 0)
|
|
|
|
ret = errno;
|
|
|
|
else
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if (dbminit(db_name) < 0)
|
|
|
|
ret = errno;
|
|
|
|
#endif
|
|
|
|
if (ret == 0) {
|
|
|
|
fd = open (okname, O_CREAT|O_RDWR|O_TRUNC, 0600);
|
|
|
|
if (fd < 0)
|
|
|
|
ret = errno;
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "Atomically" rename the database in a way that locks out read
|
|
|
|
* access in the middle of the rename.
|
|
|
|
*
|
|
|
|
* Not perfect; if we crash in the middle of an update, we don't
|
|
|
|
* necessarily know to complete the transaction the rename, but...
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_rename(from, to)
|
|
|
|
char *from;
|
|
|
|
char *to;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
1995-09-07 21:39:00 +00:00
|
|
|
#ifdef _DBM_
|
1995-08-02 23:08:18 +00:00
|
|
|
char *fromdb = gen_dbsuffix (from, ".db");
|
|
|
|
char *todb = gen_dbsuffix (to, ".db");
|
|
|
|
#else
|
1994-09-30 14:50:09 +00:00
|
|
|
char *fromdir = gen_dbsuffix (from, ".dir");
|
|
|
|
char *todir = gen_dbsuffix (to, ".dir");
|
|
|
|
char *frompag = gen_dbsuffix (from , ".pag");
|
|
|
|
char *topag = gen_dbsuffix (to, ".pag");
|
1995-01-25 19:45:25 +00:00
|
|
|
#endif
|
1994-09-30 14:50:09 +00:00
|
|
|
char *fromok = gen_dbsuffix(from, ".ok");
|
|
|
|
long trans = kerb_start_update(to);
|
1995-09-07 21:39:00 +00:00
|
|
|
int ok = 0;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
#ifdef _DBM_
|
1995-08-02 23:08:18 +00:00
|
|
|
if (rename (fromdb, todb) == 0) {
|
|
|
|
#else
|
1994-09-30 14:50:09 +00:00
|
|
|
if ((rename (fromdir, todir) == 0)
|
|
|
|
&& (rename (frompag, topag) == 0)) {
|
1995-01-25 19:45:25 +00:00
|
|
|
#endif
|
1994-09-30 14:50:09 +00:00
|
|
|
(void) unlink (fromok);
|
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (fromok);
|
1995-09-07 21:39:00 +00:00
|
|
|
#ifdef _DBM_
|
1995-08-02 23:08:18 +00:00
|
|
|
free (fromdb);
|
|
|
|
free (todb);
|
|
|
|
#else
|
1994-09-30 14:50:09 +00:00
|
|
|
free (fromdir);
|
|
|
|
free (todir);
|
|
|
|
free (frompag);
|
|
|
|
free (topag);
|
1995-01-25 19:45:25 +00:00
|
|
|
#endif
|
1994-09-30 14:50:09 +00:00
|
|
|
if (ok)
|
|
|
|
return kerb_end_update(to, trans);
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* look up a principal in the data base returns number of principals
|
1995-05-30 06:41:30 +00:00
|
|
|
* found , and whether there were more than requested.
|
1994-09-30 14:50:09 +00:00
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_get_principal(name, inst, principal, max, more)
|
|
|
|
char *name; /* could have wild card */
|
|
|
|
char *inst; /* could have wild card */
|
|
|
|
Principal *principal;
|
|
|
|
unsigned int max; /* max number of name structs to return */
|
|
|
|
int *more; /* where there more than 'max' tuples? */
|
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
int found = 0, code;
|
|
|
|
extern int errorproc();
|
|
|
|
int wildp, wildi;
|
|
|
|
datum key, contents;
|
|
|
|
char testname[ANAME_SZ], testinst[INST_SZ];
|
|
|
|
u_long trans;
|
|
|
|
int try;
|
|
|
|
DBM *db;
|
|
|
|
|
|
|
|
if (!init)
|
|
|
|
kerb_db_init(); /* initialize database routines */
|
|
|
|
|
|
|
|
for (try = 0; try < KERB_DB_MAX_RETRY; try++) {
|
|
|
|
trans = kerb_start_read();
|
|
|
|
|
|
|
|
if ((code = kerb_dbl_lock(KERB_DBL_SHARED)) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
db = dbm_open(current_db_name, O_RDONLY, 0600);
|
|
|
|
|
|
|
|
*more = 0;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (kerb_debug & 2)
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: db_get_principal for %s %s max = %d",
|
|
|
|
progname, name, inst, max);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
wildp = !strcmp(name, "*");
|
|
|
|
wildi = !strcmp(inst, "*");
|
|
|
|
|
|
|
|
if (!wildi && !wildp) { /* nothing's wild */
|
|
|
|
encode_princ_key(&key, name, inst);
|
|
|
|
contents = dbm_fetch(db, key);
|
|
|
|
if (contents.dptr == NULL) {
|
|
|
|
found = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
decode_princ_contents(&contents, principal);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (kerb_debug & 1) {
|
|
|
|
fprintf(stderr, "\t found %s %s p_n length %d t_n length %d\n",
|
|
|
|
principal->name, principal->instance,
|
|
|
|
strlen(principal->name),
|
|
|
|
strlen(principal->instance));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
found = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* process wild cards by looping through entire database */
|
|
|
|
|
|
|
|
for (key = dbm_firstkey(db); key.dptr != NULL;
|
|
|
|
key = dbm_next(db, key)) {
|
|
|
|
decode_princ_key(&key, testname, testinst);
|
|
|
|
if ((wildp || !strcmp(testname, name)) &&
|
|
|
|
(wildi || !strcmp(testinst, inst))) { /* have a match */
|
|
|
|
if (found >= max) {
|
|
|
|
*more = 1;
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
found++;
|
|
|
|
contents = dbm_fetch(db, key);
|
|
|
|
decode_princ_contents(&contents, principal);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (kerb_debug & 1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\tfound %s %s p_n length %d t_n length %d\n",
|
|
|
|
principal->name, principal->instance,
|
|
|
|
strlen(principal->name),
|
|
|
|
strlen(principal->instance));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
principal++; /* point to next */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
kerb_dbl_unlock(); /* unlock read lock */
|
|
|
|
dbm_close(db);
|
|
|
|
if (kerb_end_read(trans) == 0)
|
|
|
|
break;
|
|
|
|
found = -1;
|
|
|
|
if (!non_blocking)
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update a name in the data base. Returns number of names
|
|
|
|
* successfully updated.
|
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_put_principal(principal, max)
|
|
|
|
Principal *principal;
|
|
|
|
unsigned int max; /* number of principal structs to
|
|
|
|
* update */
|
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
int found = 0, code;
|
|
|
|
u_long i;
|
|
|
|
extern int errorproc();
|
|
|
|
datum key, contents;
|
|
|
|
DBM *db;
|
|
|
|
|
|
|
|
gettimeofday(×tamp, NULL);
|
|
|
|
|
|
|
|
if (!init)
|
|
|
|
kerb_db_init();
|
|
|
|
|
|
|
|
if ((code = kerb_dbl_lock(KERB_DBL_EXCLUSIVE)) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
db = dbm_open(current_db_name, O_RDWR, 0600);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (kerb_debug & 2)
|
|
|
|
fprintf(stderr, "%s: kerb_db_put_principal max = %d",
|
|
|
|
progname, max);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* for each one, stuff temps, and do replace/append */
|
|
|
|
for (i = 0; i < max; i++) {
|
|
|
|
encode_princ_contents(&contents, principal);
|
|
|
|
encode_princ_key(&key, principal->name, principal->instance);
|
|
|
|
dbm_store(db, key, contents, DBM_REPLACE);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (kerb_debug & 1) {
|
|
|
|
fprintf(stderr, "\n put %s %s\n",
|
|
|
|
principal->name, principal->instance);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
found++;
|
|
|
|
principal++; /* bump to next struct */
|
|
|
|
}
|
|
|
|
|
|
|
|
dbm_close(db);
|
|
|
|
kerb_dbl_unlock(); /* unlock database */
|
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1995-09-07 21:39:00 +00:00
|
|
|
encode_princ_key(key, name, instance)
|
|
|
|
datum *key;
|
|
|
|
char *name, *instance;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
static char keystring[ANAME_SZ + INST_SZ];
|
|
|
|
|
|
|
|
bzero(keystring, ANAME_SZ + INST_SZ);
|
|
|
|
strncpy(keystring, name, ANAME_SZ);
|
|
|
|
strncpy(&keystring[ANAME_SZ], instance, INST_SZ);
|
|
|
|
key->dptr = keystring;
|
|
|
|
key->dsize = ANAME_SZ + INST_SZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1995-09-07 21:39:00 +00:00
|
|
|
decode_princ_key(key, name, instance)
|
|
|
|
datum *key;
|
|
|
|
char *name, *instance;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
strncpy(name, key->dptr, ANAME_SZ);
|
|
|
|
strncpy(instance, key->dptr + ANAME_SZ, INST_SZ);
|
|
|
|
name[ANAME_SZ - 1] = '\0';
|
|
|
|
instance[INST_SZ - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1995-09-07 21:39:00 +00:00
|
|
|
encode_princ_contents(contents, principal)
|
|
|
|
datum *contents;
|
|
|
|
Principal *principal;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
contents->dsize = sizeof(*principal);
|
|
|
|
contents->dptr = (char *) principal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1995-09-07 21:39:00 +00:00
|
|
|
decode_princ_contents(contents, principal)
|
|
|
|
datum *contents;
|
|
|
|
Principal *principal;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
bcopy(contents->dptr, (char *) principal, sizeof(*principal));
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
void
|
|
|
|
kerb_db_get_stat(s)
|
|
|
|
DB_stat *s;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
gettimeofday(×tamp, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
s->cpu = 0;
|
|
|
|
s->elapsed = 0;
|
|
|
|
s->dio = 0;
|
|
|
|
s->pfault = 0;
|
|
|
|
s->t_stamp = timestamp.tv_sec;
|
|
|
|
s->n_retrieve = 0;
|
|
|
|
s->n_replace = 0;
|
|
|
|
s->n_append = 0;
|
|
|
|
s->n_get_stat = 0;
|
|
|
|
s->n_put_stat = 0;
|
|
|
|
/* update local copy too */
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
void
|
|
|
|
kerb_db_put_stat(s)
|
|
|
|
DB_stat *s;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
void
|
|
|
|
delta_stat(a, b, c)
|
|
|
|
DB_stat *a, *b, *c;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
/* c = a - b then b = a for the next time */
|
|
|
|
|
|
|
|
c->cpu = a->cpu - b->cpu;
|
|
|
|
c->elapsed = a->elapsed - b->elapsed;
|
|
|
|
c->dio = a->dio - b->dio;
|
|
|
|
c->pfault = a->pfault - b->pfault;
|
|
|
|
c->t_stamp = a->t_stamp - b->t_stamp;
|
|
|
|
c->n_retrieve = a->n_retrieve - b->n_retrieve;
|
|
|
|
c->n_replace = a->n_replace - b->n_replace;
|
|
|
|
c->n_append = a->n_append - b->n_append;
|
|
|
|
c->n_get_stat = a->n_get_stat - b->n_get_stat;
|
|
|
|
c->n_put_stat = a->n_put_stat - b->n_put_stat;
|
|
|
|
|
|
|
|
bcopy(a, b, sizeof(DB_stat));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* look up a dba in the data base returns number of dbas found , and
|
1995-05-30 06:41:30 +00:00
|
|
|
* whether there were more than requested.
|
1994-09-30 14:50:09 +00:00
|
|
|
*/
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_get_dba(dba_name, dba_inst, dba, max, more)
|
|
|
|
char *dba_name; /* could have wild card */
|
|
|
|
char *dba_inst; /* could have wild card */
|
|
|
|
Dba *dba;
|
|
|
|
unsigned int max; /* max number of name structs to return */
|
|
|
|
int *more; /* where there more than 'max' tuples? */
|
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
*more = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_iterate (func, arg)
|
|
|
|
int (*func)();
|
|
|
|
char *arg; /* void *, really */
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
datum key, contents;
|
|
|
|
Principal *principal;
|
|
|
|
int code;
|
|
|
|
DBM *db;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
kerb_db_init(); /* initialize and open the database */
|
|
|
|
if ((code = kerb_dbl_lock(KERB_DBL_SHARED)) != 0)
|
|
|
|
return code;
|
|
|
|
|
|
|
|
db = dbm_open(current_db_name, O_RDONLY, 0600);
|
|
|
|
|
|
|
|
for (key = dbm_firstkey (db); key.dptr != NULL; key = dbm_next(db, key)) {
|
|
|
|
contents = dbm_fetch (db, key);
|
|
|
|
/* XXX may not be properly aligned */
|
|
|
|
principal = (Principal *) contents.dptr;
|
|
|
|
if ((code = (*func)(arg, principal)) != 0)
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
dbm_close(db);
|
|
|
|
kerb_dbl_unlock();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dblfd = -1;
|
|
|
|
static int mylock = 0;
|
|
|
|
static int inited = 0;
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static void
|
|
|
|
kerb_dbl_init()
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
if (!inited) {
|
|
|
|
char *filename = gen_dbsuffix (current_db_name, ".ok");
|
|
|
|
if ((dblfd = open(filename, 0)) < 0) {
|
|
|
|
fprintf(stderr, "kerb_dbl_init: couldn't open %s\n", filename);
|
|
|
|
fflush(stderr);
|
|
|
|
perror("open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
free(filename);
|
|
|
|
inited++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static void
|
|
|
|
kerb_dbl_fini()
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
close(dblfd);
|
|
|
|
dblfd = -1;
|
|
|
|
inited = 0;
|
|
|
|
mylock = 0;
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static int
|
|
|
|
kerb_dbl_lock(mode)
|
|
|
|
int mode;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
int flock_mode;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-09-30 14:50:09 +00:00
|
|
|
if (!inited)
|
|
|
|
kerb_dbl_init();
|
|
|
|
if (mylock) { /* Detect lock call when lock already
|
|
|
|
* locked */
|
|
|
|
fprintf(stderr, "Kerberos locking error (mylock)\n");
|
|
|
|
fflush(stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
switch (mode) {
|
|
|
|
case KERB_DBL_EXCLUSIVE:
|
|
|
|
flock_mode = LOCK_EX;
|
|
|
|
break;
|
|
|
|
case KERB_DBL_SHARED:
|
|
|
|
flock_mode = LOCK_SH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "invalid lock mode %d\n", mode);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (non_blocking)
|
|
|
|
flock_mode |= LOCK_NB;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
|
|
|
if (flock(dblfd, flock_mode) < 0)
|
1994-09-30 14:50:09 +00:00
|
|
|
return errno;
|
|
|
|
mylock++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
static void
|
|
|
|
kerb_dbl_unlock()
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
if (!mylock) { /* lock already unlocked */
|
|
|
|
fprintf(stderr, "Kerberos database lock not locked when unlocking.\n");
|
|
|
|
fflush(stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (flock(dblfd, LOCK_UN) < 0) {
|
|
|
|
fprintf(stderr, "Kerberos database lock error. (unlocking)\n");
|
|
|
|
fflush(stderr);
|
|
|
|
perror("flock");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
mylock = 0;
|
|
|
|
}
|
|
|
|
|
1995-09-07 21:39:00 +00:00
|
|
|
int
|
|
|
|
kerb_db_set_lockmode(mode)
|
|
|
|
int mode;
|
1994-09-30 14:50:09 +00:00
|
|
|
{
|
|
|
|
int old = non_blocking;
|
|
|
|
non_blocking = mode;
|
|
|
|
return old;
|
|
|
|
}
|