1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* plugins/kdb/db2/kdb_db2.c */
4 * Copyright 1997,2006,2007-2009 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
29 * Copyright (C) 1998 by the FundsXpress, INC.
31 * All rights reserved.
33 * Export of this software from the United States of America may require
34 * a specific license from the United States Government. It is the
35 * responsibility of any person or organization contemplating export to
36 * obtain such a license before exporting.
38 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
39 * distribute this software and its documentation for any purpose and
40 * without fee is hereby granted, provided that the above copyright
41 * notice appear in all copies and that both that copyright notice and
42 * this permission notice appear in supporting documentation, and that
43 * the name of FundsXpress. not be used in advertising or publicity pertaining
44 * to distribution of the software without specific, written prior
45 * permission. FundsXpress makes no representations about the suitability of
46 * this software for any purpose. It is provided "as is" without express
47 * or implied warranty.
49 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
51 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
67 #include "policy_db.h"
69 #define KDB_DB2_DATABASE_NAME "database_name"
72 #define SUFFIX_LOCK ".ok"
73 #define SUFFIX_POLICY ".kadm5"
74 #define SUFFIX_POLICY_LOCK ".kadm5.lock"
79 * There are two distinct locking protocols used. One is designed to
80 * lock against processes (the admin_server, for one) which make
81 * incremental changes to the database; the other is designed to lock
82 * against utilities (kdb5_edit, kpropd, kdb5_convert) which replace the
83 * entire database in one fell swoop.
85 * The first locking protocol is implemented using flock() in the
86 * krb_dbl_lock() and krb_dbl_unlock routines.
88 * The second locking protocol is necessary because DBM "files" are
89 * actually implemented as two separate files, and it is impossible to
90 * atomically rename two files simultaneously. It assumes that the
91 * database is replaced only very infrequently in comparison to the time
92 * needed to do a database read operation.
94 * A third file is used as a "version" semaphore; the modification
95 * time of this file is the "version number" of the database.
96 * At the start of a read operation, the reader checks the version
97 * number; at the end of the read operation, it checks again. If the
98 * version number changed, or if the semaphore was nonexistant at
99 * either time, the reader sleeps for a second to let things
100 * stabilize, and then tries again; if it does not succeed after
101 * KRB5_DBM_MAX_RETRY attempts, it gives up.
103 * On update, the semaphore file is deleted (if it exists) before any
104 * update takes place; at the end of the update, it is replaced, with
105 * a version number strictly greater than the version number which
106 * existed at the start of the update.
108 * If the system crashes in the middle of an update, the semaphore
109 * file is not automatically created on reboot; this is a feature, not
110 * a bug, since the database may be inconsistant. Note that the
111 * absence of a semaphore file does not prevent another _update_ from
112 * taking place later. Database replacements take place automatically
113 * only on slave servers; a crash in the middle of an update will be
114 * fixed by the next slave propagation. A crash in the middle of an
115 * update on the master would be somewhat more serious, but this would
116 * likely be noticed by an administrator, who could fix the problem and
117 * retry the operation.
120 /* Evaluate to true if the krb5_context c contains an initialized db2
122 #define inited(c) ((c)->dal_handle->db_context && \
123 ((krb5_db2_context *)(c)->dal_handle->db_context)-> \
126 static krb5_error_code
127 get_db_opt(char *input, char **opt, char **val)
129 char *pos = strchr(input, '=');
132 *val = strdup(input);
137 *opt = malloc((pos - input) + 1);
138 *val = strdup(pos + 1);
139 if (!*opt || !*val) {
146 memcpy(*opt, input, pos - input);
147 (*opt)[pos - input] = '\0';
153 /* Restore dbctx to the uninitialized state. */
155 ctx_clear(krb5_db2_context *dbc)
158 * Free any dynamically allocated memory. File descriptors and locks
159 * are the caller's problem.
161 free(dbc->db_lf_name);
164 * Clear the structure and reset the defaults.
166 memset(dbc, 0, sizeof(krb5_db2_context));
168 dbc->db_lf_name = NULL;
169 dbc->db_lf_file = -1;
171 dbc->db_nb_locks = FALSE;
175 /* Set *dbc_out to the db2 database context for context. If one does not
176 * exist, create one in the uninitialized state. */
177 static krb5_error_code
178 ctx_get(krb5_context context, krb5_db2_context **dbc_out)
180 krb5_db2_context *dbc;
181 kdb5_dal_handle *dal_handle;
183 dal_handle = context->dal_handle;
185 if (dal_handle->db_context == NULL) {
186 dbc = (krb5_db2_context *) malloc(sizeof(krb5_db2_context));
190 memset(dbc, 0, sizeof(krb5_db2_context));
192 dal_handle->db_context = dbc;
195 *dbc_out = dal_handle->db_context;
199 /* Using db_args and the profile, initialize the configurable parameters of the
200 * DB context inside context. */
201 static krb5_error_code
202 configure_context(krb5_context context, char *conf_section, char **db_args)
204 krb5_error_code status;
205 krb5_db2_context *dbc;
206 char **t_ptr, *opt = NULL, *val = NULL, *pval = NULL;
207 profile_t profile = KRB5_DB_GET_PROFILE(context);
210 status = ctx_get(context, &dbc);
214 /* Allow unlockiter to be overridden by command line db_args. */
215 status = profile_get_boolean(profile, KDB_MODULE_SECTION, conf_section,
216 KRB5_CONF_UNLOCKITER, FALSE, &bval);
219 dbc->unlockiter = bval;
221 for (t_ptr = db_args; t_ptr && *t_ptr; t_ptr++) {
224 status = get_db_opt(*t_ptr, &opt, &val);
225 if (opt && !strcmp(opt, "dbname")) {
226 dbc->db_name = strdup(val);
227 if (dbc->db_name == NULL) {
232 else if (!opt && !strcmp(val, "temporary")) {
234 } else if (!opt && !strcmp(val, "merge_nra")) {
236 } else if (opt && !strcmp(opt, "hash")) {
237 dbc->hashfirst = TRUE;
238 } else if (!opt && !strcmp(val, "unlockiter")) {
239 dbc->unlockiter = TRUE;
240 } else if (!opt && !strcmp(val, "lockiter")) {
241 dbc->unlockiter = FALSE;
244 k5_setmsg(context, status,
245 _("Unsupported argument \"%s\" for db2"),
251 if (dbc->db_name == NULL) {
252 /* Check for database_name in the db_module section. */
253 status = profile_get_string(profile, KDB_MODULE_SECTION, conf_section,
254 KDB_DB2_DATABASE_NAME, NULL, &pval);
255 if (status == 0 && pval == NULL) {
256 /* For compatibility, check for database_name in the realm. */
257 status = profile_get_string(profile, KDB_REALM_SECTION,
258 KRB5_DB_GET_REALM(context),
259 KDB_DB2_DATABASE_NAME,
260 DEFAULT_KDB_FILE, &pval);
264 dbc->db_name = strdup(pval);
267 status = profile_get_boolean(profile, KDB_MODULE_SECTION, conf_section,
268 KRB5_CONF_DISABLE_LAST_SUCCESS, FALSE, &bval);
271 dbc->disable_last_success = bval;
273 status = profile_get_boolean(profile, KDB_MODULE_SECTION, conf_section,
274 KRB5_CONF_DISABLE_LOCKOUT, FALSE, &bval);
277 dbc->disable_lockout = bval;
282 profile_release_string(pval);
287 * Set *out to one of the filenames used for the DB described by dbc. sfx
288 * should be one of SUFFIX_DB, SUFFIX_LOCK, SUFFIX_POLICY, or
289 * SUFFIX_POLICY_LOCK.
291 static krb5_error_code
292 ctx_dbsuffix(krb5_db2_context *dbc, const char *sfx, char **out)
298 tilde = dbc->tempdb ? "~" : "";
299 if (asprintf(&result, "%s%s%s", dbc->db_name, tilde, sfx) < 0)
305 /* Generate all four files corresponding to dbc. */
306 static krb5_error_code
307 ctx_allfiles(krb5_db2_context *dbc, char **dbname_out, char **lockname_out,
308 char **polname_out, char **plockname_out)
310 char *a = NULL, *b = NULL, *c = NULL, *d = NULL;
312 *dbname_out = *lockname_out = *polname_out = *plockname_out = NULL;
313 if (ctx_dbsuffix(dbc, SUFFIX_DB, &a))
315 if (ctx_dbsuffix(dbc, SUFFIX_LOCK, &b))
317 if (ctx_dbsuffix(dbc, SUFFIX_POLICY, &c))
319 if (ctx_dbsuffix(dbc, SUFFIX_POLICY_LOCK, &d))
335 * Open the DB2 database described by dbc, using the specified flags and mode,
336 * and return the resulting handle. Try both hash and btree database types;
337 * dbc->hashfirst determines which is attempted first. If dbc->hashfirst
338 * indicated the wrong type, update it to indicate the correct type.
340 static krb5_error_code
341 open_db(krb5_context context, krb5_db2_context *dbc, int flags, int mode,
358 if (ctx_dbsuffix(dbc, SUFFIX_DB, &fname) != 0)
368 /* Try our best guess at the database type. */
369 db = dbopen(fname, flags, mode,
370 dbc->hashfirst ? DB_HASH : DB_BTREE,
371 dbc->hashfirst ? (void *) &hashi : (void *) &bti);
373 if (db == NULL && IS_EFTYPE(errno)) {
374 db = dbopen(fname, flags, mode,
375 dbc->hashfirst ? DB_BTREE : DB_HASH,
376 dbc->hashfirst ? (void *) &bti : (void *) &hashi);
377 /* If that worked, update our guess for next time. */
379 dbc->hashfirst = !dbc->hashfirst;
382 /* Don't try unlocked iteration with a hash database. */
383 if (db != NULL && dbc->hashfirst)
384 dbc->unlockiter = FALSE;
387 k5_prependmsg(context, errno, _("Cannot open DB2 database '%s'"),
393 return (db == NULL) ? errno : 0;
396 static krb5_error_code
397 ctx_unlock(krb5_context context, krb5_db2_context *dbc)
399 krb5_error_code retval, retval2;
402 retval = osa_adb_release_lock(dbc->policy_db);
404 if (!dbc->db_locks_held) /* lock already unlocked */
405 return KRB5_KDB_NOTLOCKED;
408 if (--(dbc->db_locks_held) == 0) {
411 dbc->db_lock_mode = 0;
413 retval2 = krb5_lock_file(context, dbc->db_lf_file,
414 KRB5_LOCKMODE_UNLOCK);
419 /* We may be unlocking because osa_adb_get_lock() failed. */
420 if (retval == OSA_ADB_NOTLOCKED)
425 static krb5_error_code
426 ctx_lock(krb5_context context, krb5_db2_context *dbc, int lockmode)
428 krb5_error_code retval;
431 if (lockmode == KRB5_DB_LOCKMODE_PERMANENT ||
432 lockmode == KRB5_DB_LOCKMODE_EXCLUSIVE)
433 kmode = KRB5_LOCKMODE_EXCLUSIVE;
434 else if (lockmode == KRB5_DB_LOCKMODE_SHARED)
435 kmode = KRB5_LOCKMODE_SHARED;
439 if (dbc->db_locks_held == 0 || dbc->db_lock_mode < kmode) {
440 /* Acquire or upgrade the lock. */
441 retval = krb5_lock_file(context, dbc->db_lf_file, kmode);
442 /* Check if we tried to lock something not open for write. */
443 if (retval == EBADF && kmode == KRB5_LOCKMODE_EXCLUSIVE)
444 return KRB5_KDB_CANTLOCK_DB;
445 else if (retval == EACCES || retval == EAGAIN || retval == EWOULDBLOCK)
446 return KRB5_KDB_CANTLOCK_DB;
450 /* Open the DB (or re-open it for read/write). */
452 dbc->db->close(dbc->db);
453 retval = open_db(context, dbc,
454 kmode == KRB5_LOCKMODE_SHARED ? O_RDONLY : O_RDWR,
457 dbc->db_locks_held = 0;
458 dbc->db_lock_mode = 0;
459 (void) osa_adb_release_lock(dbc->policy_db);
460 (void) krb5_lock_file(context, dbc->db_lf_file,
461 KRB5_LOCKMODE_UNLOCK);
465 dbc->db_lock_mode = kmode;
467 dbc->db_locks_held++;
469 /* Acquire or upgrade the policy lock. */
470 retval = osa_adb_get_lock(dbc->policy_db, lockmode);
472 (void) ctx_unlock(context, dbc);
473 if (retval == OSA_ADB_NOEXCL_PERM || retval == OSA_ADB_CANTLOCK_DB ||
474 retval == OSA_ADB_NOLOCKFILE)
475 retval = KRB5_KDB_CANTLOCK_DB;
480 /* Initialize the lock file and policy database fields of dbc. The db_name and
481 * tempdb fields must already be set. */
482 static krb5_error_code
483 ctx_init(krb5_db2_context *dbc)
485 krb5_error_code retval;
486 char *polname = NULL, *plockname = NULL;
488 retval = ctx_dbsuffix(dbc, SUFFIX_LOCK, &dbc->db_lf_name);
493 * should be opened read/write so that write locking can work with
496 if ((dbc->db_lf_file = open(dbc->db_lf_name, O_RDWR, 0666)) < 0) {
497 if ((dbc->db_lf_file = open(dbc->db_lf_name, O_RDONLY, 0666)) < 0) {
502 set_cloexec_fd(dbc->db_lf_file);
505 retval = ctx_dbsuffix(dbc, SUFFIX_POLICY, &polname);
508 retval = ctx_dbsuffix(dbc, SUFFIX_POLICY_LOCK, &plockname);
511 retval = osa_adb_init_db(&dbc->policy_db, polname, plockname,
512 OSA_ADB_POLICY_DB_MAGIC);
523 ctx_fini(krb5_db2_context *dbc)
525 if (dbc->db_lf_file != -1)
526 (void) close(dbc->db_lf_file);
528 (void) osa_adb_fini_db(dbc->policy_db, OSA_ADB_POLICY_DB_MAGIC);
534 krb5_db2_fini(krb5_context context)
536 if (context->dal_handle->db_context != NULL) {
537 ctx_fini(context->dal_handle->db_context);
538 context->dal_handle->db_context = NULL;
543 /* Return successfully if the db2 name set in context can be opened. */
544 static krb5_error_code
545 check_openable(krb5_context context)
547 krb5_error_code retval;
549 krb5_db2_context *dbc;
551 dbc = context->dal_handle->db_context;
552 retval = open_db(context, dbc, O_RDONLY, 0, &db);
560 * Return the last modification time of the database.
562 * Think about using fstat.
566 krb5_db2_get_age(krb5_context context, char *db_name, time_t *age)
568 krb5_db2_context *dbc;
571 if (!inited(context))
572 return (KRB5_KDB_DBNOTINITED);
573 dbc = context->dal_handle->db_context;
575 if (fstat(dbc->db_lf_file, &st) < 0)
582 /* Try to update the timestamp on dbc's lockfile. */
584 ctx_update_age(krb5_db2_context *dbc)
588 struct utimbuf utbuf;
590 now = time((time_t *) NULL);
591 if (fstat(dbc->db_lf_file, &st) != 0)
593 if (st.st_mtime >= now) {
594 utbuf.actime = st.st_mtime + 1;
595 utbuf.modtime = st.st_mtime + 1;
596 (void) utime(dbc->db_lf_name, &utbuf);
598 (void) utime(dbc->db_lf_name, (struct utimbuf *) NULL);
602 krb5_db2_lock(krb5_context context, int lockmode)
604 if (!inited(context))
605 return KRB5_KDB_DBNOTINITED;
606 return ctx_lock(context, context->dal_handle->db_context, lockmode);
610 krb5_db2_unlock(krb5_context context)
612 if (!inited(context))
613 return KRB5_KDB_DBNOTINITED;
614 return ctx_unlock(context, context->dal_handle->db_context);
617 /* Zero out and unlink filename. */
618 static krb5_error_code
619 destroy_file(char *filename)
622 int dowrite, j, nb, fd, retval;
624 char buf[BUFSIZ], zbuf[BUFSIZ];
626 fd = open(filename, O_RDWR, 0);
630 /* fstat() will probably not fail unless using a remote filesystem
631 * (which is inappropriate for the kerberos database) so this check
632 * is mostly paranoia. */
633 if (fstat(fd, &statb) == -1)
636 * Stroll through the file, reading in BUFSIZ chunks. If everything
637 * is zero, then we're done for that block, otherwise, zero the block.
638 * We would like to just blast through everything, but some DB
639 * implementations make holey files and writing data to the holes
640 * causes actual blocks to be allocated which is no good, since
641 * we're just about to unlink it anyways.
643 memset(zbuf, 0, BUFSIZ);
645 while (pos < statb.st_size) {
647 nb = read(fd, buf, BUFSIZ);
650 for (j = 0; j < nb; j++) {
651 if (buf[j] != '\0') {
659 lseek(fd, pos, SEEK_SET);
660 nb = write(fd, zbuf, j);
666 /* ??? Is fsync really needed? I don't know of any non-networked
667 * filesystem which will discard queued writes to disk if a file
668 * is deleted after it is closed. --jfc */
674 if (unlink(filename))
684 /* Initialize dbc by locking and creating the DB. If the DB already exists,
685 * clear it out if dbc->tempdb is set; otherwise return EEXIST. */
686 static krb5_error_code
687 ctx_create_db(krb5_context context, krb5_db2_context *dbc)
689 krb5_error_code retval = 0;
690 char *dbname = NULL, *polname = NULL, *plockname = NULL;
692 retval = ctx_allfiles(dbc, &dbname, &dbc->db_lf_name, &polname,
697 dbc->db_lf_file = open(dbc->db_lf_name, O_CREAT | O_RDWR | O_TRUNC,
699 if (dbc->db_lf_file < 0) {
703 retval = krb5_lock_file(context, dbc->db_lf_file, KRB5_LOCKMODE_EXCLUSIVE);
706 set_cloexec_fd(dbc->db_lf_file);
707 dbc->db_lock_mode = KRB5_LOCKMODE_EXCLUSIVE;
708 dbc->db_locks_held = 1;
711 /* Temporary DBs are locked for their whole lifetime. Since we have
712 * the lock, any remnant files can be safely destroyed. */
713 (void) destroy_file(dbname);
714 (void) unlink(polname);
715 (void) unlink(plockname);
718 retval = open_db(context, dbc, O_RDWR | O_CREAT | O_EXCL, 0600, &dbc->db);
722 /* Create the policy database, initialize a handle to it, and lock it. */
723 retval = osa_adb_create_db(polname, plockname, OSA_ADB_POLICY_DB_MAGIC);
726 retval = osa_adb_init_db(&dbc->policy_db, polname, plockname,
727 OSA_ADB_POLICY_DB_MAGIC);
730 retval = osa_adb_get_lock(dbc->policy_db, KRB5_DB_LOCKMODE_EXCLUSIVE);
739 dbc->db->close(dbc->db);
740 if (dbc->db_locks_held > 0) {
741 (void) krb5_lock_file(context, dbc->db_lf_file,
742 KRB5_LOCKMODE_UNLOCK);
744 if (dbc->db_lf_file >= 0)
745 close(dbc->db_lf_file);
755 krb5_db2_get_principal(krb5_context context, krb5_const_principal searchfor,
756 unsigned int flags, krb5_db_entry **entry)
758 krb5_db2_context *dbc;
759 krb5_error_code retval;
762 krb5_data keydata, contdata;
766 if (!inited(context))
767 return KRB5_KDB_DBNOTINITED;
769 dbc = context->dal_handle->db_context;
771 retval = ctx_lock(context, dbc, KRB5_LOCKMODE_SHARED);
775 /* XXX deal with wildcard lookups */
776 retval = krb5_encode_princ_dbkey(context, &keydata, searchfor);
779 key.data = keydata.data;
780 key.size = keydata.length;
783 dbret = (*db->get)(db, &key, &contents, 0);
785 krb5_free_data_contents(context, &keydata);
788 retval = KRB5_KDB_NOENTRY;
794 contdata.data = contents.data;
795 contdata.length = contents.size;
796 retval = krb5_decode_princ_entry(context, &contdata, entry);
801 (void) krb5_db2_unlock(context); /* unlock read lock */
806 krb5_db2_put_principal(krb5_context context, krb5_db_entry *entry,
812 krb5_data contdata, keydata;
813 krb5_error_code retval;
814 krb5_db2_context *dbc;
816 krb5_clear_error_message (context);
818 /* DB2 does not support db_args DB arguments for principal */
819 k5_setmsg(context, EINVAL, _("Unsupported argument \"%s\" for db2"),
824 if (!inited(context))
825 return KRB5_KDB_DBNOTINITED;
827 dbc = context->dal_handle->db_context;
828 if ((retval = ctx_lock(context, dbc, KRB5_LOCKMODE_EXCLUSIVE)))
833 retval = krb5_encode_princ_entry(context, &contdata, entry);
836 contents.data = contdata.data;
837 contents.size = contdata.length;
838 retval = krb5_encode_princ_dbkey(context, &keydata, entry->princ);
840 krb5_free_data_contents(context, &contdata);
844 key.data = keydata.data;
845 key.size = keydata.length;
846 dbret = (*db->put)(db, &key, &contents, 0);
847 retval = dbret ? errno : 0;
848 krb5_free_data_contents(context, &keydata);
849 krb5_free_data_contents(context, &contdata);
853 (void) krb5_db2_unlock(context); /* unlock database */
858 krb5_db2_delete_principal(krb5_context context, krb5_const_principal searchfor)
860 krb5_error_code retval;
861 krb5_db_entry *entry;
862 krb5_db2_context *dbc;
865 krb5_data keydata, contdata;
868 if (!inited(context))
869 return KRB5_KDB_DBNOTINITED;
871 dbc = context->dal_handle->db_context;
872 if ((retval = ctx_lock(context, dbc, KRB5_LOCKMODE_EXCLUSIVE)))
875 if ((retval = krb5_encode_princ_dbkey(context, &keydata, searchfor)))
877 key.data = keydata.data;
878 key.size = keydata.length;
881 dbret = (*db->get) (db, &key, &contents, 0);
885 retval = KRB5_KDB_NOENTRY;
893 contdata.data = contents.data;
894 contdata.length = contents.size;
895 retval = krb5_decode_princ_entry(context, &contdata, &entry);
899 /* Clear encrypted key contents */
900 for (i = 0; i < entry->n_key_data; i++) {
901 if (entry->key_data[i].key_data_length[0]) {
902 memset(entry->key_data[i].key_data_contents[0], 0,
903 (unsigned) entry->key_data[i].key_data_length[0]);
907 retval = krb5_encode_princ_entry(context, &contdata, entry);
908 krb5_db_free_principal(context, entry);
912 contents.data = contdata.data;
913 contents.size = contdata.length;
914 dbret = (*db->put) (db, &key, &contents, 0);
915 retval = dbret ? errno : 0;
916 krb5_free_data_contents(context, &contdata);
919 dbret = (*db->del) (db, &key, 0);
920 retval = dbret ? errno : 0;
922 krb5_free_data_contents(context, &keydata);
926 (void) krb5_db2_unlock(context); /* unlock write lock */
930 typedef krb5_error_code (*ctx_iterate_cb)(krb5_pointer, krb5_db_entry *);
932 /* Cursor structure for ctx_iterate() */
933 typedef struct iter_curs {
937 unsigned int startflag;
938 unsigned int stepflag;
940 krb5_db2_context *dbc;
942 krb5_boolean islocked;
945 /* Lock DB handle of curs, updating curs->islocked. */
946 static krb5_error_code
947 curs_lock(iter_curs *curs)
949 krb5_error_code retval;
951 retval = ctx_lock(curs->ctx, curs->dbc, curs->lockmode);
954 curs->islocked = TRUE;
958 /* Unlock DB handle of curs, updating curs->islocked. */
960 curs_unlock(iter_curs *curs)
962 ctx_unlock(curs->ctx, curs->dbc);
963 curs->islocked = FALSE;
966 /* Set up curs and lock DB. */
967 static krb5_error_code
968 curs_init(iter_curs *curs, krb5_context ctx, krb5_db2_context *dbc,
969 krb5_flags iterflags)
971 int isrecurse = iterflags & KRB5_DB_ITER_RECURSE;
972 unsigned int prevflag = R_PREV;
973 unsigned int nextflag = R_NEXT;
975 curs->keycopy.size = 0;
976 curs->keycopy.data = NULL;
977 curs->islocked = FALSE;
981 if (iterflags & KRB5_DB_ITER_WRITE)
982 curs->lockmode = KRB5_LOCKMODE_EXCLUSIVE;
984 curs->lockmode = KRB5_LOCKMODE_SHARED;
988 if (dbc->hashfirst) {
989 k5_setmsg(ctx, EINVAL, _("Recursive iteration is not supported "
990 "for hash databases"));
996 k5_setmsg(ctx, EINVAL, _("Recursive iteration not supported "
997 "in this version of libdb"));
1001 if (iterflags & KRB5_DB_ITER_REV) {
1002 curs->startflag = R_LAST;
1003 curs->stepflag = prevflag;
1005 curs->startflag = R_FIRST;
1006 curs->stepflag = nextflag;
1008 return curs_lock(curs);
1011 /* Get initial entry. */
1013 curs_start(iter_curs *curs)
1015 DB *db = curs->dbc->db;
1017 return db->seq(db, &curs->key, &curs->data, curs->startflag);
1020 /* Save iteration state so DB can be unlocked/closed. */
1021 static krb5_error_code
1022 curs_save(iter_curs *curs)
1024 if (!curs->dbc->unlockiter)
1027 curs->keycopy.data = malloc(curs->key.size);
1028 if (curs->keycopy.data == NULL)
1031 curs->keycopy.size = curs->key.size;
1032 memcpy(curs->keycopy.data, curs->key.data, curs->key.size);
1036 /* Free allocated cursor resources */
1038 curs_free(iter_curs *curs)
1040 free(curs->keycopy.data);
1041 curs->keycopy.size = 0;
1042 curs->keycopy.data = NULL;
1045 /* Move one step of iteration (forwards or backwards as requested). Free
1046 * curs->keycopy as a side effect, if needed. */
1048 curs_step(iter_curs *curs)
1051 krb5_db2_context *dbc = curs->dbc;
1053 if (dbc->unlockiter) {
1054 /* Reacquire libdb cursor using saved copy of key. */
1055 curs->key = curs->keycopy;
1056 dbret = dbc->db->seq(dbc->db, &curs->key, &curs->data, R_CURSOR);
1061 return dbc->db->seq(dbc->db, &curs->key, &curs->data, curs->stepflag);
1064 /* Run one invocation of the callback, unlocking the mutex and possibly the DB
1065 * around the invocation. */
1066 static krb5_error_code
1067 curs_run_cb(iter_curs *curs, ctx_iterate_cb func, krb5_pointer func_arg)
1069 krb5_db2_context *dbc = curs->dbc;
1070 krb5_error_code retval, lockerr;
1071 krb5_db_entry *entry;
1072 krb5_context ctx = curs->ctx;
1075 contdata = make_data(curs->data.data, curs->data.size);
1076 retval = krb5_decode_princ_entry(ctx, &contdata, &entry);
1079 /* Save libdb key across possible DB closure. */
1080 retval = curs_save(curs);
1084 if (dbc->unlockiter)
1087 k5_mutex_unlock(krb5_db2_mutex);
1088 retval = (*func)(func_arg, entry);
1089 krb5_db_free_principal(ctx, entry);
1090 k5_mutex_lock(krb5_db2_mutex);
1091 if (dbc->unlockiter) {
1092 lockerr = curs_lock(curs);
1099 /* Free cursor resources and unlock the DB if needed. */
1101 curs_fini(iter_curs *curs)
1108 static krb5_error_code
1109 ctx_iterate(krb5_context context, krb5_db2_context *dbc,
1110 ctx_iterate_cb func, krb5_pointer func_arg, krb5_flags iterflags)
1112 krb5_error_code retval;
1116 retval = curs_init(&curs, context, dbc, iterflags);
1119 dbret = curs_start(&curs);
1120 while (dbret == 0) {
1121 retval = curs_run_cb(&curs, func, func_arg);
1124 dbret = curs_step(&curs);
1140 krb5_db2_iterate(krb5_context context, char *match_expr, ctx_iterate_cb func,
1141 krb5_pointer func_arg, krb5_flags iterflags)
1143 if (!inited(context))
1144 return KRB5_KDB_DBNOTINITED;
1145 return ctx_iterate(context, context->dal_handle->db_context, func,
1146 func_arg, iterflags);
1150 krb5_db2_set_lockmode(krb5_context context, krb5_boolean mode)
1153 krb5_db2_context *dbc;
1155 dbc = context->dal_handle->db_context;
1158 old = dbc->db_nb_locks;
1159 dbc->db_nb_locks = mode;
1174 krb5_db2_lib_cleanup()
1176 /* right now, no cleanup required */
1181 krb5_db2_open(krb5_context context, char *conf_section, char **db_args,
1184 krb5_error_code status = 0;
1186 krb5_clear_error_message(context);
1187 if (inited(context))
1190 status = configure_context(context, conf_section, db_args);
1194 status = check_openable(context);
1198 return ctx_init(context->dal_handle->db_context);
1202 krb5_db2_create(krb5_context context, char *conf_section, char **db_args)
1204 krb5_error_code status = 0;
1205 krb5_db2_context *dbc;
1207 krb5_clear_error_message(context);
1208 if (inited(context))
1211 status = configure_context(context, conf_section, db_args);
1215 dbc = context->dal_handle->db_context;
1216 status = ctx_create_db(context, dbc);
1221 krb5_db2_unlock(context);
1227 krb5_db2_destroy(krb5_context context, char *conf_section, char **db_args)
1229 krb5_error_code status;
1230 krb5_db2_context *dbc;
1231 char *dbname = NULL, *lockname = NULL, *polname = NULL, *plockname = NULL;
1233 if (inited(context)) {
1234 status = krb5_db2_fini(context);
1239 krb5_clear_error_message(context);
1240 status = configure_context(context, conf_section, db_args);
1244 status = check_openable(context);
1248 dbc = context->dal_handle->db_context;
1250 status = ctx_allfiles(dbc, &dbname, &lockname, &polname, &plockname);
1253 status = destroy_file(dbname);
1256 status = unlink(lockname);
1259 status = osa_adb_destroy_db(polname, plockname, OSA_ADB_POLICY_DB_MAGIC);
1263 status = krb5_db2_fini(context);
1273 /* policy functions */
1275 krb5_db2_create_policy(krb5_context context, osa_policy_ent_t policy)
1277 krb5_db2_context *dbc = context->dal_handle->db_context;
1279 return osa_adb_create_policy(dbc->policy_db, policy);
1283 krb5_db2_get_policy(krb5_context context,
1284 char *name, osa_policy_ent_t *policy)
1286 krb5_db2_context *dbc = context->dal_handle->db_context;
1288 return osa_adb_get_policy(dbc->policy_db, name, policy);
1292 krb5_db2_put_policy(krb5_context context, osa_policy_ent_t policy)
1294 krb5_db2_context *dbc = context->dal_handle->db_context;
1296 return osa_adb_put_policy(dbc->policy_db, policy);
1300 krb5_db2_iter_policy(krb5_context context,
1302 osa_adb_iter_policy_func func, void *data)
1304 krb5_db2_context *dbc = context->dal_handle->db_context;
1306 return osa_adb_iter_policy(dbc->policy_db, func, data);
1310 krb5_db2_delete_policy(krb5_context context, char *policy)
1312 krb5_db2_context *dbc = context->dal_handle->db_context;
1314 return osa_adb_destroy_policy(dbc->policy_db, policy);
1318 krb5_db2_free_policy(krb5_context context, osa_policy_ent_t entry)
1320 osa_free_policy_ent(entry);
1325 * Merge non-replicated attributes from src into dst, setting
1326 * changed to non-zero if dst was changed.
1328 * Non-replicated attributes are: last_success, last_failed,
1329 * fail_auth_count, and any negative TL data values.
1331 static krb5_error_code
1332 krb5_db2_merge_principal(krb5_context context,
1339 if (dst->last_success != src->last_success) {
1340 dst->last_success = src->last_success;
1344 if (dst->last_failed != src->last_failed) {
1345 dst->last_failed = src->last_failed;
1349 if (dst->fail_auth_count != src->fail_auth_count) {
1350 dst->fail_auth_count = src->fail_auth_count;
1357 struct nra_context {
1358 krb5_context kcontext;
1359 krb5_db2_context *db_context;
1363 * Iteration callback merges non-replicated attributes from
1366 static krb5_error_code
1367 krb5_db2_merge_nra_iterator(krb5_pointer ptr, krb5_db_entry *entry)
1369 struct nra_context *nra = (struct nra_context *)ptr;
1370 kdb5_dal_handle *dal_handle = nra->kcontext->dal_handle;
1371 krb5_error_code retval;
1373 krb5_db_entry *s_entry;
1374 krb5_db2_context *dst_db;
1376 memset(&s_entry, 0, sizeof(s_entry));
1378 dst_db = dal_handle->db_context;
1379 dal_handle->db_context = nra->db_context;
1381 /* look up the new principal in the old DB */
1382 retval = krb5_db2_get_principal(nra->kcontext, entry->princ, 0, &s_entry);
1384 /* principal may be newly created, so ignore */
1385 dal_handle->db_context = dst_db;
1389 /* merge non-replicated attributes from the old entry in */
1390 krb5_db2_merge_principal(nra->kcontext, s_entry, entry, &changed);
1392 dal_handle->db_context = dst_db;
1394 /* if necessary, commit the modified new entry to the new DB */
1396 retval = krb5_db2_put_principal(nra->kcontext, entry, NULL);
1401 krb5_db_free_principal(nra->kcontext, s_entry);
1406 * Merge non-replicated attributes (that is, lockout-related
1407 * attributes and negative TL data types) from the real database
1408 * into the temporary one.
1410 static krb5_error_code
1411 ctx_merge_nra(krb5_context context, krb5_db2_context *dbc_temp,
1412 krb5_db2_context *dbc_real)
1414 struct nra_context nra;
1416 nra.kcontext = context;
1417 nra.db_context = dbc_real;
1418 return ctx_iterate(context, dbc_temp, krb5_db2_merge_nra_iterator, &nra, 0);
1422 * In the filesystem, promote the temporary database described by dbc_temp to
1423 * the real database described by dbc_real. Both must be exclusively locked.
1425 static krb5_error_code
1426 ctx_promote(krb5_context context, krb5_db2_context *dbc_temp,
1427 krb5_db2_context *dbc_real)
1429 krb5_error_code retval;
1430 char *tdb = NULL, *tlock = NULL, *tpol = NULL, *tplock = NULL;
1431 char *rdb = NULL, *rlock = NULL, *rpol = NULL, *rplock = NULL;
1433 /* Generate all filenames of interest (including a few we don't need). */
1434 retval = ctx_allfiles(dbc_temp, &tdb, &tlock, &tpol, &tplock);
1437 retval = ctx_allfiles(dbc_real, &rdb, &rlock, &rpol, &rplock);
1441 /* Rename the principal and policy databases into place. */
1442 if (rename(tdb, rdb)) {
1446 if (rename(tpol, rpol)) {
1451 ctx_update_age(dbc_real);
1453 /* Release and remove the temporary DB lockfiles. */
1454 (void) unlink(tlock);
1455 (void) unlink(tplock);
1470 krb5_db2_promote_db(krb5_context context, char *conf_section, char **db_args)
1472 krb5_error_code retval;
1473 krb5_boolean merge_nra = FALSE, real_locked = FALSE;
1474 krb5_db2_context *dbc_temp, *dbc_real = NULL;
1477 /* context must be initialized with an exclusively locked temp DB. */
1478 if (!inited(context))
1479 return KRB5_KDB_DBNOTINITED;
1480 dbc_temp = context->dal_handle->db_context;
1481 if (dbc_temp->db_lock_mode != KRB5_LOCKMODE_EXCLUSIVE)
1482 return KRB5_KDB_NOTLOCKED;
1483 if (!dbc_temp->tempdb)
1486 /* Check db_args for whether we should merge non-replicated attributes. */
1487 for (db_argp = db_args; *db_argp; db_argp++) {
1488 if (!strcmp(*db_argp, "merge_nra")) {
1494 /* Make a db2 context for the real DB. */
1495 dbc_real = k5alloc(sizeof(*dbc_real), &retval);
1496 if (dbc_real == NULL)
1498 ctx_clear(dbc_real);
1500 /* Try creating the real DB. */
1501 dbc_real->db_name = strdup(dbc_temp->db_name);
1502 if (dbc_real->db_name == NULL)
1504 dbc_real->tempdb = FALSE;
1505 retval = ctx_create_db(context, dbc_real);
1506 if (retval == EEXIST) {
1507 /* The real database already exists, so open and lock it. */
1508 dbc_real->db_name = strdup(dbc_temp->db_name);
1509 if (dbc_real->db_name == NULL)
1511 dbc_real->tempdb = FALSE;
1512 retval = ctx_init(dbc_real);
1515 retval = ctx_lock(context, dbc_real, KRB5_DB_LOCKMODE_EXCLUSIVE);
1523 retval = ctx_merge_nra(context, dbc_temp, dbc_real);
1528 /* Perform filesystem manipulations for the promotion. */
1529 retval = ctx_promote(context, dbc_temp, dbc_real);
1533 /* Unlock and finalize context since the temp DB is gone. */
1534 (void) krb5_db2_unlock(context);
1535 krb5_db2_fini(context);
1539 (void) ctx_unlock(context, dbc_real);
1546 krb5_db2_check_policy_as(krb5_context kcontext, krb5_kdc_req *request,
1547 krb5_db_entry *client, krb5_db_entry *server,
1548 krb5_timestamp kdc_time, const char **status,
1549 krb5_pa_data ***e_data)
1551 krb5_error_code retval;
1553 retval = krb5_db2_lockout_check_policy(kcontext, client, kdc_time);
1554 if (retval == KRB5KDC_ERR_CLIENT_REVOKED)
1555 *status = "LOCKED_OUT";
1560 krb5_db2_audit_as_req(krb5_context kcontext, krb5_kdc_req *request,
1561 krb5_db_entry *client, krb5_db_entry *server,
1562 krb5_timestamp authtime, krb5_error_code error_code)
1564 (void) krb5_db2_lockout_audit(kcontext, client, authtime, error_code);