kdb_db2.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
*
* Copyright 1997 by the Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
*/
/*
* Copyright (C) 1998 by the FundsXpress, INC.
*
* All rights reserved.
*
* Export of this software from the United States of America may require
* a specific license from the United States Government. It is the
* responsibility of any person or organization contemplating export to
* obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of FundsXpress. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. FundsXpress makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "k5-int.h"
#include "kdb_log.h"
#include <db.h>
#include <stdio.h>
#include <errno.h>
#include <utime.h>
#define OLD_COMPAT_VERSION_1
#ifdef OLD_COMPAT_VERSION_1
#include "kdb_compat.h"
#endif
#include "kdb_db2.h"
static char *gen_dbsuffix
PROTOTYPE((char *, char * ));
PROTOTYPE((krb5_context, int));
static char default_db_name[] = DEFAULT_KDB_FILE;
/*
* Locking:
*
* 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 (kdb5_edit, kpropd, kdb5_convert) which replace the
* entire database in one fell swoop.
*
* The first locking protocol is implemented using flock() in the
* 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
* KRB5_DBM_MAX_RETRY attempts, it gives up.
*
* 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.
*
* 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.
*/
/*
* Routines to deal with context.
*/
#define k5db2_inited(c) (c && c->db_context && \
/*
* Restore the default context.
*/
static void
{
/*
* Free any dynamically allocated memory. File descriptors and locks
* are the caller's problem.
*/
if (dbctx->db_lf_name)
/*
* Clear the structure and reset the defaults.
*/
}
static krb5_error_code
{
return ENOMEM;
else {
}
}
return(0);
}
/*
* Utility routine: generate name of database file.
*/
static char *
char *db_name;
char *sfx;
{
char *dbsuffix;
return((char *) NULL);
if (!dbsuffix)
return(0);
return dbsuffix;
}
static DB *
char *fname;
int flags;
int mode;
{
bti.minkeypage = 0;
return db;
switch (errno) {
#ifdef EFTYPE
case EFTYPE:
#endif
case EINVAL:
default:
return db;
}
}
static krb5_error_code
int hashfirst;
{
if (k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
return 0;
}
/*
* initialization for data base routines.
*/
{
if (k5db2_inited(context))
return 0;
/* Check for presence of our context, if not present, allocate one. */
return(retval);
return ENOMEM;
/*
* POSIX systems
*/
goto err_out;
}
}
goto err_out;
return 0;
return (retval);
}
/*
* gracefully shut down database--must be called by ANY program that does
* a krb5_db2_db_init
*/
{
krb5_error_code retval = 0;
if (k5db2_inited(context)) {
else
retval = 0;
}
if (db_ctx) {
}
return retval;
}
{
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
return 0;
}
{
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
return 0;
}
/*
*/
{
if (!k5db2_inited(context))
return(KRB5_KDB_DBNOTINITED);
return 0;
}
krb5_keyblock **key;
{
if (!k5db2_inited(context))
return(KRB5_KDB_DBNOTINITED);
return 0;
}
/*
* 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.
*
* XXX rethink this
*/
char *name;
{
if (k5db2_inited(context))
return KRB5_KDB_DBINITED;
/* Check for presence of our context, if not present, allocate one. */
return(kret);
return errno;
return 0;
}
/*
* Return the last modification time of the database.
*
* Think about using fstat.
*/
char *db_name;
{
if (!k5db2_inited(context))
return(KRB5_KDB_DBNOTINITED);
*age = -1;
else
return 0;
}
/*
* 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).
*/
static krb5_error_code
{
return 0;
}
static krb5_error_code
{
if (!k5db2_inited(context))
return(KRB5_KDB_DBNOTINITED);
retval = 0;
}
else {
}
}
else
if (!retval) {
else
}
return(retval);
}
int mode;
{
int krb5_lock_mode;
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
/* No need to upgrade lock, just return */
db_ctx->db_locks_held++;
return(0);
}
return KRB5_KDB_BADLOCKMODE;
if (db_ctx->db_nb_locks)
else
switch (retval) {
case EBADF:
if (mode == KRB5_LOCKMODE_EXCLUSIVE)
return KRB5_KDB_CANTLOCK_DB;
default:
return retval;
case 0:
break;
}
goto lock_error;
0600);
if (db) {
} else {
goto lock_error;
}
db_ctx->db_locks_held++;
return 0;
db_ctx->db_lock_mode = 0;
db_ctx->db_locks_held = 0;
(void) krb5_db2_db_unlock(context);
return retval;
}
{
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
return KRB5_KDB_NOTLOCKED;
if (--(db_ctx->db_locks_held) == 0) {
db_ctx->db_lock_mode = 0;
return(retval);
}
return 0;
}
/*
* Create the database, assuming it's not there.
*/
char *db_name;
{
register krb5_error_code retval = 0;
char *okname;
int fd;
return(retval);
switch (flags) {
case KRB5_KDB_CREATE_HASH:
return retval;
break;
case KRB5_KDB_CREATE_BTREE:
case 0:
return retval;
break;
default:
return KRB5_KDB_BAD_CREATEFLAGS;
}
else
if (retval == 0) {
if (!okname)
else {
if (fd < 0)
else
}
}
return retval;
}
/*
* Destroy the database. Zero's out all of the files, just to be sure.
*/
char *dbname;
char *suffix;
{
char *filename;
int dowrite;
if (filename == 0)
return ENOMEM;
return errno;
}
/* fstat() will probably not fail unless using a remote filesystem
(which is inappropriate for the kerberos database) so this check
is mostly paranoia. */
return retval;
}
/*
* Stroll through the file, reading in BUFSIZ chunks. If everything
* is zero, then we're done for that block, otherwise, zero the block.
* We would like to just blast through everything, but some DB
* implementations make holey files and writing data to the holes
* causes actual blocks to be allocated which is no good, since
* we're just about to unlink it anyways.
*/
i = 0;
dowrite = 0;
if (nb < 0) {
return retval;
}
for (j=0; j<nb; j++) {
if (buf[j] != '\0') {
dowrite = 1;
break;
}
}
if (dowrite) {
if (nb < 0) {
return retval;
}
}
i += nb;
}
/* ??? Is fsync really needed? I don't know of any non-networked
filesystem which will discard queued writes to disk if a file
is deleted after it is closed. --jfc */
#ifndef NOFSYNC
#endif
return(errno);
}
return(0);
}
/*
* have some tomfoolery to undergo here. If we're operating under no
* database context, then we initialize with the default. If the caller
* wishes a different context (e.g. different dispatch table), it's their
* responsibility to call kdb5_db_set_dbops() before this call. That will
* set up the right dispatch table values (e.g. name extensions).
*
* Not quite valid due to ripping out of dbops...
*/
char *dbname;
{
tmpcontext = 0;
if (!context->db_context) {
tmpcontext = 1;
return(retval1);
}
if (tmpcontext) {
}
else
return 0;
}
/*
* "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...
*
* have to go through the same stuff that we went through up in db_destroy.
*/
char *from;
char *to;
{
char *fromok;
return retval;
/*
* Create the database if it does not already exist; the
* files must exist because krb5_db2_db_lock, called below,
* will fail otherwise.
*/
goto errout;
}
else
/*
* Set the database to the target, so that other processes sharing
* the target will stop their activity, and notice the new database.
*/
if (retval)
goto errout;
goto errout;
}
if (db_ctx->db_lf_file < 0) {
goto errout;
}
if (retval)
goto errout;
goto errout;
}
goto errfromok;
goto errfromok;
goto errfromok;
}
goto errfromok;
}
if (context->db_context) {
if (db_ctx->db_lf_file >= 0) {
}
}
return retval;
}
/*
* look up a principal in the data base.
* returns number of entries found, and whether there were
* more than requested.
*/
{
*nentries = 0;
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
if (db_ctx->db_nb_locks)
return(retval);
sleep(1);
continue;
}
break;
}
if (try == KRB5_DB2_MAX_RETRY)
return KRB5_KDB_DB_INUSE;
/* XXX deal with wildcard lookups */
if (retval)
goto cleanup;
switch (dbret) {
case 1:
retval = 0;
case -1:
default:
*nentries = 0;
goto cleanup;
case 0:
if (!retval)
*nentries = 1;
break;
}
return retval;
}
/*
Free stuff returned by krb5_db2_db_get_principal.
*/
void
int nentries;
{
register int i;
for (i = 0; i < nentries; i++)
return;
}
/*
Stores the *"nentries" entry structures pointed to by "entries" in the
database.
*"nentries" is updated upon return to reflect the number of records
acutally stored; the first *"nstored" records will have been stored in the
database (even if an error occurs).
*/
register int *nentries; /* number of entry structs to update */
{
int i, n, dbret;
char *princ_name = NULL;
n = *nentries;
*nentries = 0;
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
return retval;
}
/*
* We need the lock since ulog_conv_2logentry() does a get
*/
if (!(upd = (kdb_incr_update_t *)
malloc(sizeof (kdb_incr_update_t)*n))) {
goto err_lock;
}
goto err_lock;
}
}
goto err_lock;
}
for (i = 0; i < n; i++) {
/*
* We'll be sharing the same locks as db for logging
*/
&princ_name)))
goto err_lock;
goto err_lock;
}
if (retval)
break;
if (retval) {
break;
}
if (retval)
break;
/*
* We need to make sure the db record is synced before we mark
* it as committed via finish_update.
*/
if (dbret) {
goto err_lock;
}
upd++;
}
entries++; /* bump to next struct */
}
(void)krb5_db2_db_end_update(context);
ulog_free_entries(fupd, n);
*nentries = i;
return(retval);
}
/*
* delete a principal from the data base.
* returns number of entries removed
*/
int *nentries; /* how many found & deleted */
{
int i, dbret;
char *princ_name = NULL;
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
return(retval);
return(retval);
}
goto cleanup;
switch (dbret) {
case 1:
case -1:
default:
*nentries = 0;
goto cleankey;
case 0:
;
}
/*
* We'll be sharing the same locks as db for logging
*/
(void) krb5_db2_db_unlock(context);
return retval;
}
(void) krb5_db2_db_unlock(context);
return retval;
}
}
if (retval)
goto cleankey;
*nentries = 1;
/* Clear encrypted key contents */
for (i = 0; i < entry.n_key_data; i++) {
}
}
if (retval)
goto cleankey;
if (retval)
goto cleankey;
/*
* We need to commit our update upon success
*/
if (!retval)
(void) krb5_db2_db_end_update(context);
return retval;
}
{
int dbret;
if (!k5db2_inited(context))
return KRB5_KDB_DBNOTINITED;
if (retval)
return retval;
while (dbret == 0) {
if (retval)
break;
if (retval)
break;
}
switch (dbret) {
case 1:
case 0:
break;
case -1:
default:
}
(void) krb5_db2_db_unlock(context);
return retval;
}
{
}
return old;
}
/*
* Context serialization operations.
*
* Ick, this is really gross. --- tlyu
*/
/*
* kdb5_context_size() - Determine size required to serialize.
*/
static krb5_error_code
{
/*
* The database context requires at minimum:
* krb5_int32 for KV5M_DB_CONTEXT
* krb5_int32 for db_inited
* krb5_int32 for database lockfile non-blocking flag
* krb5_int32 for database lockfile lock count
* krb5_int32 for database lockfile lock mode
* krb5_int32 for length of database name.
* krb5_int32 for KV5M_DB_CONTEXT
*/
kret = 0;
}
return(kret);
}
/*
* kdb5_context_externalize() - Externalize the database context.
*/
static krb5_error_code
krb5_octet **buffer;
{
krb5_octet *bp;
required = 0;
/* Write magic number */
/* Write inited flag */
/* Write blocking lock lockmode */
/* Write lock count */
(void) krb5_ser_pack_int32((krb5_int32)
dbctx->db_locks_held : 0,
/* Write lock mode */
(void) krb5_ser_pack_int32((krb5_int32)
dbctx->db_lock_mode : 0,
/* Write length of database name */
/* Write trailer */
kret = 0;
}
}
return(kret);
}
/*
* kdb5_context_internalize() - Internalize the database context.
*/
static krb5_error_code
krb5_octet **buffer;
{
krb5_octet *bp;
char *dbname;
/* Read our magic number */
ibuf = 0;
if (ibuf == KV5M_DB_CONTEXT) {
if (iflag) {
if (dbnamelen &&
if (!kret)
}
if (!kret &&
(void) krb5_db2_db_set_lockmode(tmpctx, 0);
if (lockmode)
}
if (dbname)
}
if (!kret)
if (kret) {
if (dbctx)
}
else
}
}
if (!kret) {
}
return(kret);
}
/* Dispatch entry */
static const krb5_ser_entry kdb5_context_ser_entry = {
KV5M_DB_CONTEXT, /* Type */
kdb5_context_size, /* Sizer routine */
kdb5_context_externalize, /* Externalize routine */
kdb5_context_internalize /* Externalize routine */
};
/*
* Register serializer.
*/
{
}