reindent-20030715
[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 <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <fcntl.h>
17 #include <sys/types.h>
18 #include <time.h>
19 #include <stdio.h>
20 #ifndef AFS_NT40_ENV
21 #include <strings.h>
22 #endif
23 #include <afs/cmd.h>
24 #include "kauth.h"
25 #include "kalog.h"
26 #include <afs/afsutil.h>
27
28 #ifdef AUTH_DBM_LOG
29
30 char *dbmfile;
31
32 static
33 cmdproc(register struct cmd_syndesc *as, 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             memcpy(&rdata, data.dptr, sizeof(kalog_elt));
70             printf("%s: last operation from host %x at %s", ti->data,
71                    rdata.host, ctime(&rdata.last_use));
72         }
73     }
74     dbm_close(kdb);
75 }
76
77
78 #include "AFS_component_version_number.c"
79
80 int
81 main(int argc, char **argv)
82 {
83     register struct cmd_syndesc *ts;
84     register afs_int32 code;
85     char dbmfile_help[AFSDIR_PATH_MAX];
86
87     sprintf(dbmfile_help, "dbmfile to use (default %s)",
88             AFSDIR_SERVER_KALOGDB_FILEPATH);
89     dbmfile = AFSDIR_SERVER_KALOGDB_FILEPATH;
90     ts = cmd_CreateSyntax(NULL, 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,
93                 "extract entries that match specified key");
94     code = cmd_Dispatch(argc, argv);
95     return code;
96 }
97 #else
98
99 #include "AFS_component_version_number.c"
100
101 int
102 main(void)
103 {
104     printf("kdb not supported\n");
105 }
106 #endif