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