afsconfig-and-rcsid-all-around-20010705
[openafs.git] / src / kauth / kdb.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include <fcntl.h>
16 #include <sys/types.h>
17 #include <time.h>
18 #include <stdio.h>
19 #ifndef AFS_NT40_ENV
20 #include <strings.h>
21 #endif
22 #include <afs/cmd.h>
23 #include "kauth.h"
24 #include "kalog.h"
25 #include <afs/afsutil.h>
26
27 #ifdef AUTH_DBM_LOG
28
29 char *dbmfile;
30
31 static cmdproc(
32   register struct cmd_syndesc *as,
33   afs_int32 arock)
34 {
35     DBM *kdb;
36     datum key, data;
37     kalog_elt rdata;
38     register afs_int32 code = 0, cnt = 0;
39     register struct cmd_item *ti;
40
41     if (as->parms[0].items) {
42         dbmfile = as->parms[0].items->data;
43     } 
44     kdb = dbm_open(dbmfile, O_RDONLY, KALOG_DB_MODE);
45     if (!kdb) {
46         perror(dbmfile);
47         exit(1);
48     }
49     if (!(ti = as->parms[1].items)) {
50         printf("Printing all entries found in %s\n", dbmfile);
51         for (key = dbm_firstkey(kdb); key.dptr;
52              key = afs_dbm_nextkey(kdb, key), cnt++) {
53             printf("\t%s\n", key.dptr);
54         }
55         printf("%d entries were found\n", cnt);
56     } else {
57         for (; ti; ti = ti->next) {
58             key.dsize = strlen(ti->data) + 1;
59             key.dptr = ti->data;
60             data = dbm_fetch(kdb, key);
61             if (!data.dptr) {
62                 fprintf(stderr, "%s: no entry exists\n", ti->data);
63                 continue;
64             }
65             if (data.dsize != sizeof(kalog_elt)) {
66                 fprintf(stderr, "%s: data came out corrupt\n", ti->data);
67                 continue;
68             }
69             bcopy(data.dptr, &rdata, sizeof(kalog_elt));
70             printf("%s: last operation from host %x at %s", ti->data, rdata.host, 
71                    ctime(&rdata.last_use));
72         }
73     }
74     dbm_close(kdb);
75 }
76
77
78 #include "AFS_component_version_number.c"
79
80 int main(
81   int argc,
82   char **argv)
83 {
84     register struct cmd_syndesc *ts;
85     register afs_int32 code;
86     char dbmfile_help[AFSDIR_PATH_MAX];
87
88     sprintf(dbmfile_help, "dbmfile to use (default %s)",  AFSDIR_SERVER_KALOGDB_FILEPATH);
89     dbmfile = AFSDIR_SERVER_KALOGDB_FILEPATH;
90     ts = cmd_CreateSyntax((char *) 0, cmdproc, 0, "Dump contents of dbm database");
91     cmd_AddParm(ts, "-dbmfile", CMD_SINGLE, CMD_OPTIONAL, dbmfile_help);
92     cmd_AddParm(ts, "-key", CMD_SINGLE, CMD_OPTIONAL, "extract entries that match specified key");
93     code = cmd_Dispatch(argc, argv);
94     return code;
95 }
96 #else
97
98 #include "AFS_component_version_number.c"
99
100 int main(void)
101 {
102     printf("kdb not supported\n");
103 }
104 #endif