death to register
[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(struct cmd_syndesc *as, void * arock)
32 {
33     DBM *kdb;
34     datum key, data;
35     kalog_elt rdata;
36     afs_int32 cnt = 0;
37     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                     char hoststr[16];
71                     hostIP = afs_inet_ntoa_r(rdata.host, hoststr);
72                     printf("%s: last operation from host %s at %s", 
73                            (char *)key.dptr, hostIP, 
74                            ctime(&rdata.last_use));
75                 }
76             } else {
77                 printf("\t%s\n", (char *)key.dptr);
78             }
79         }
80         printf("%d entries were found\n", cnt);
81     } else {
82         for (; ti; ti = ti->next) {
83             key.dsize = strlen(ti->data) + 1;
84             key.dptr = ti->data;
85             data = dbm_fetch(kdb, key);
86             if (!data.dptr) {
87                 fprintf(stderr, "%s: no entry exists\n", ti->data);
88                 continue;
89             }
90             if (data.dsize != sizeof(kalog_elt)) {
91                 fprintf(stderr, "%s: data came out corrupt\n", ti->data);
92                 continue;
93             }
94             memcpy(&rdata, data.dptr, sizeof(kalog_elt));
95             printf("%s: last operation from host %x at %s", ti->data,
96                    rdata.host, ctime(&rdata.last_use));
97         }
98     }
99     dbm_close(kdb);
100     return 0;
101 }
102
103
104 #include "AFS_component_version_number.c"
105
106 int
107 main(int argc, char **argv)
108 {
109     struct cmd_syndesc *ts;
110     afs_int32 code;
111     char dbmfile_help[AFSDIR_PATH_MAX];
112
113     sprintf(dbmfile_help, "dbmfile to use (default %s)",
114             AFSDIR_SERVER_KALOGDB_FILEPATH);
115     dbmfile = AFSDIR_SERVER_KALOGDB_FILEPATH;
116     ts = cmd_CreateSyntax(NULL, cmdproc, NULL, "Dump contents of dbm database");
117     cmd_AddParm(ts, "-dbmfile", CMD_SINGLE, CMD_OPTIONAL, dbmfile_help);
118     cmd_AddParm(ts, "-key", CMD_SINGLE, CMD_OPTIONAL,
119                 "extract entries that match specified key");
120     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL, "print long info for each entry");
121     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
122     code = cmd_Dispatch(argc, argv);
123     return code;
124 }
125 #else
126
127 #include "AFS_component_version_number.c"
128
129 int
130 main(void)
131 {
132     printf("kdb not supported\n");
133     return 1;
134 }
135 #endif