Prototype kalog_Init
[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
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <time.h>
17 #include <stdio.h>
18 #ifndef AFS_NT40_ENV
19 #include <strings.h>
20 #endif
21 #include <afs/cmd.h>
22 #include "kauth.h"
23 #include "kalog.h"
24 #include <afs/afsutil.h>
25
26 #ifdef AUTH_DBM_LOG
27
28 const char *dbmfile;
29
30 static int
31 cmdproc(register struct cmd_syndesc *as, void * arock)
32 {
33     DBM *kdb;
34     datum key, data;
35     kalog_elt rdata;
36     afs_int32 cnt = 0;
37     register struct cmd_item *ti;
38
39     if (as->parms[0].items) {
40         dbmfile = as->parms[0].items->data;
41     }
42     kdb = dbm_open(dbmfile, O_RDONLY, KALOG_DB_MODE);
43     if (!kdb) {
44         perror(dbmfile);
45         exit(1);
46     }
47     if (!(ti = as->parms[1].items)) {
48         printf("Printing all entries found in %s\n", dbmfile);
49         for (key = dbm_firstkey(kdb); key.dptr;
50              key = afs_dbm_nextkey(kdb, key), cnt++) {
51             if (as->parms[2].items) {
52                 data = dbm_fetch(kdb, key);
53                 if (!data.dptr) {
54                     fprintf(stderr, "%s: no entry exists\n", ti->data);
55                     continue;
56                 }
57                 if (data.dsize != sizeof(kalog_elt)) {
58                     fprintf(stderr, "%s: data came out corrupt\n", ti->data);
59                     continue;
60                 }
61                 memcpy(&rdata, data.dptr, sizeof(kalog_elt));
62                 if (! as->parms[3].items) {
63                     char *hostName;
64                     hostName = hostutil_GetNameByINet(rdata.host);
65                     printf("%s: last operation from host %s at %s", 
66                            (char *)key.dptr, hostName, 
67                            ctime(&rdata.last_use));
68                 } else {
69                     char *hostIP;
70                     hostIP = afs_inet_ntoa(rdata.host);
71                     printf("%s: last operation from host %s at %s", 
72                            (char *)key.dptr, hostIP, 
73                            ctime(&rdata.last_use));
74                 }
75             } else {
76                 printf("\t%s\n", (char *)key.dptr);
77             }
78         }
79         printf("%d entries were found\n", cnt);
80     } else {
81         for (; ti; ti = ti->next) {
82             key.dsize = strlen(ti->data) + 1;
83             key.dptr = ti->data;
84             data = dbm_fetch(kdb, key);
85             if (!data.dptr) {
86                 fprintf(stderr, "%s: no entry exists\n", ti->data);
87                 continue;
88             }
89             if (data.dsize != sizeof(kalog_elt)) {
90                 fprintf(stderr, "%s: data came out corrupt\n", ti->data);
91                 continue;
92             }
93             memcpy(&rdata, data.dptr, sizeof(kalog_elt));
94             printf("%s: last operation from host %x at %s", ti->data,
95                    rdata.host, ctime(&rdata.last_use));
96         }
97     }
98     dbm_close(kdb);
99     return 0;
100 }
101
102
103 #include "AFS_component_version_number.c"
104
105 int
106 main(int argc, char **argv)
107 {
108     register struct cmd_syndesc *ts;
109     register afs_int32 code;
110     char dbmfile_help[AFSDIR_PATH_MAX];
111
112     sprintf(dbmfile_help, "dbmfile to use (default %s)",
113             AFSDIR_SERVER_KALOGDB_FILEPATH);
114     dbmfile = AFSDIR_SERVER_KALOGDB_FILEPATH;
115     ts = cmd_CreateSyntax(NULL, cmdproc, NULL, "Dump contents of dbm database");
116     cmd_AddParm(ts, "-dbmfile", CMD_SINGLE, CMD_OPTIONAL, dbmfile_help);
117     cmd_AddParm(ts, "-key", CMD_SINGLE, CMD_OPTIONAL,
118                 "extract entries that match specified key");
119     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL, "print long info for each entry");
120     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
121     code = cmd_Dispatch(argc, argv);
122     return code;
123 }
124 #else
125
126 #include "AFS_component_version_number.c"
127
128 int
129 main(void)
130 {
131     printf("kdb not supported\n");
132     return 1;
133 }
134 #endif