2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <sys/types.h>
24 #include <netinet/in.h>
33 #include <afs/com_err.h>
39 #define UBIK_HEADERSIZE 64
40 #define UBIK_BUFFERSIZE 1024
42 char *whoami = "kadb_check";
48 int listuheader, listkheader, listentries, verbose;
54 struct ubik_hdr uheader;
56 offset = lseek(fd, 0, 0);
58 printf("error: lseek to 0 failed: %d %d\n", offset, errno);
62 /* now read the info */
63 r = read(fd, &uheader, sizeof(uheader));
64 if (r != sizeof(uheader)) {
65 printf("error: read of %d bytes failed: %d %d\n", sizeof(uheader), r,
70 uheader.magic = ntohl(uheader.magic);
71 uheader.size = ntohl(uheader.size);
72 uheader.version.epoch = ntohl(uheader.version.epoch);
73 uheader.version.counter = ntohl(uheader.version.counter);
76 printf("Ubik Header\n");
77 printf(" Magic = 0x%x\n", uheader.magic);
78 printf(" Size = %u\n", uheader.size);
79 printf(" Version.epoch = %u\n", uheader.version.epoch);
80 printf(" Version.counter = %u\n", uheader.version.counter);
83 if (uheader.size != UBIK_HEADERSIZE)
84 printf("Ubik header size is %u (should be %u)\n", uheader.size,
86 if (uheader.magic != UBIK_MAGIC)
87 printf("Ubik header magic is 0x%x (should be 0x%x)\n", uheader.magic,
95 struct kaheader *header;
97 printf("Version = %d\n", header->version);
98 printf("HeaderSize = %d\n", header->headerSize);
99 printf("Free Ptr = %u\n", header->freePtr);
100 printf("EOF Ptr = %u\n", header->eofPtr);
101 printf("Kvno Ptr = %u\n", header->kvnoPtr);
102 printf("SpecialKeysVersion changed = %d\n", header->specialKeysVersion);
103 printf("# admin accounts = %d\n", header->admin_accounts);
104 printf("HashSize = %d\n", header->hashsize);
105 printf("Check Version = %d\n", header->checkVersion);
106 printf("stats.minorVersion = %d\n", header->stats.minor_version);
107 printf("stats.AllocBlock calls = %d\n", header->stats.allocs);
108 printf("stats.FreeBlock calls = %d\n", header->stats.frees);
109 printf("stats.cpw commands = %d\n", header->stats.cpws);
113 PrintEntry(index, entry)
115 struct kaentry *entry;
121 time_t modification_time = entry->modification_time;
122 time_t change_password_time = entry->change_password_time;
123 time_t max_ticket_lifetime = entry->max_ticket_lifetime;
127 i = (index - sizeof(struct kaheader)) / sizeof(struct kaentry);
129 printf("Entry %5d (%u):\n", i, index);
131 if (entry->flags & KAFNORMAL) {
132 printf(" Name = %s", entry->userID.name);
133 if (strlen(entry->userID.instance) > 0) {
134 printf(".%s", entry->userID.instance);
140 if (entry->flags & KAFNORMAL)
142 if (entry->flags & KAFADMIN)
144 if (entry->flags & KAFNOTGS)
146 if (entry->flags & KAFNOSEAL)
148 if (entry->flags & KAFNOCPW)
151 if (entry->flags & KAFNEWASSOC)
153 if (entry->flags & KAFFREE)
155 if (entry->flags & KAFOLDKEYS)
157 if (entry->flags & KAFSPECIAL)
159 if (entry->flags & KAFASSOCROOT)
160 printf("ROOT-ASSOC ");
161 if (entry->flags & KAFASSOC)
165 printf(" Next = %u\n", entry->next);
167 if (entry->flags & KAFFREE)
169 if (entry->flags & KAFOLDKEYS)
172 tt = entry->user_expiration;
173 tm_p = localtime(&tt);
175 strftime(Time, 100, "%m/%d/%Y %H:%M", tm_p);
177 printf(" User Expiration = %s\n",
178 (entry->user_expiration == 0xffffffff) ? "never" : Time);
180 printf(" Password Expiration = %u days %s\n",
181 entry->misc_auth_bytes[EXPIRES],
182 (entry->misc_auth_bytes[EXPIRES] ? "" : "(never)"));
184 printf(" Password Attempts before lock = ");
185 if (!entry->misc_auth_bytes[ATTEMPTS])
186 printf("unlimited\n");
188 printf("%d\n", entry->misc_auth_bytes[ATTEMPTS]);
190 printf(" Password lockout time = ");
191 if (!entry->misc_auth_bytes[LOCKTIME])
192 printf("unlimited\n");
194 printf("%.1f min\n", (entry->misc_auth_bytes[LOCKTIME] * 8.5));
196 printf(" Is entry locked = %s\n",
197 (entry->misc_auth_bytes[REUSEFLAGS] ==
198 KA_ISLOCKED) ? "yes" : "no");
200 printf(" Permit password reuse = %s\n",
201 (!entry->pwsums[0] && !entry->pwsums[1]) ? "yes" : "no");
203 printf(" Mod Time = %u: %s", entry->modification_time,
204 ctime(&modification_time));
205 printf(" Mod ID = %u\n", entry->modification_id);
206 printf(" Change Password Time = %u: %s", entry->change_password_time,
207 ctime(&change_password_time));
208 printf(" Ticket lifetime = %u: %s", entry->max_ticket_lifetime,
209 ctime(&max_ticket_lifetime));
210 printf(" Key Version = %d\n", entry->key_version);
213 ka_PrintBytes((char *)&entry->key, sizeof(entry->key));
216 /* What about asServer structs and such and misc_ath_bytes */
219 /* ntohEntry - convert back to host-order */
220 ntohEntry(struct kaentry *entryp)
222 entryp->flags = ntohl(entryp->flags);
223 entryp->next = ntohl(entryp->next);
224 entryp->user_expiration = ntohl(entryp->user_expiration);
225 entryp->modification_time = ntohl(entryp->modification_time);
226 entryp->modification_id = ntohl(entryp->modification_id);
227 entryp->change_password_time = ntohl(entryp->change_password_time);
228 entryp->max_ticket_lifetime = ntohl(entryp->max_ticket_lifetime);
229 entryp->key_version = ntohl(entryp->key_version);
230 entryp->misc.asServer.nOldKeys = ntohl(entryp->misc.asServer.nOldKeys);
231 entryp->misc.asServer.oldKeys = ntohl(entryp->misc.asServer.oldKeys);
237 struct kaentry *entryp;
239 char name[32], inst[32];
241 ka_ConvertBytes(name, sizeof(name), entryp->userID.name,
242 strlen(entryp->userID.name));
243 ka_ConvertBytes(inst, sizeof(inst), entryp->userID.instance,
244 strlen(entryp->userID.instance));
246 if (strlen(entryp->userID.instance)) {
247 sprintf(principal, "%s.%s", name, inst);
249 strcpy(principal, name);
257 struct kaentry *entryp;
263 /* Special entries are not rebuilt */
264 if (entryp->flags & KAFSPECIAL)
267 fprintf(out, "create -name %s", EntryName(entryp));
269 ka_ConvertBytes(key, sizeof(key), (char *)&entryp->key,
270 sizeof(entryp->key));
271 fprintf(out, " -initial_password foo\n");
274 if (entryp->flags & KAFADMIN)
275 strcat(flags, "+ADMIN");
276 if (entryp->flags & KAFNOTGS)
277 strcat(flags, "+NOTGS");
278 if (entryp->flags & KAFNOSEAL)
279 strcat(flags, "+NOSEAL");
280 if (entryp->flags & KAFNOCPW)
281 strcat(flags, "+NOCPW");
283 fprintf(out, "setfields -name %s", principal);
284 if (strcmp(flags, "") != 0)
285 fprintf(out, " -flags %s", &flags[1]);
286 if (entryp->user_expiration != 0xffffffff) {
287 time_t tt = entryp->user_expiration;
288 strftime(Time, 50, "%m/%d/%Y %H:%M",localtime(&tt));
289 fprintf(out, " -expiration '%s'", Time);
291 fprintf(out, " -lifetime %u", entryp->max_ticket_lifetime);
292 if (entryp->misc_auth_bytes[EXPIRES])
293 fprintf(out, " -pwexpires %u", entryp->misc_auth_bytes[EXPIRES]);
294 if (entryp->pwsums[0] || entryp->pwsums[1])
295 fprintf(out, " -reuse no");
296 if (entryp->misc_auth_bytes[ATTEMPTS])
297 fprintf(out, " -attempts %u", entryp->misc_auth_bytes[ATTEMPTS]);
298 if (entryp->misc_auth_bytes[LOCKTIME])
299 fprintf(out, " -locktime %d",
300 (int)(entryp->misc_auth_bytes[LOCKTIME] * 8.5));
303 fprintf(out, "setkey -name %s -new_key %s -kvno %d\n", principal, key,
304 ntohl(entryp->key_version));
308 struct kaheader *header;
310 afs_int32 i, code = 0;
312 header->version = ntohl(header->version);
313 header->headerSize = ntohl(header->headerSize);
314 header->freePtr = ntohl(header->freePtr);
315 header->eofPtr = ntohl(header->eofPtr);
316 header->kvnoPtr = ntohl(header->kvnoPtr);
317 header->stats.minor_version = ntohl(header->stats.minor_version);
318 header->stats.allocs = ntohl(header->stats.allocs);
319 header->stats.frees = ntohl(header->stats.frees);
320 header->stats.cpws = ntohl(header->stats.cpws);
321 header->admin_accounts = ntohl(header->admin_accounts);
322 header->specialKeysVersion = ntohl(header->specialKeysVersion);
323 header->hashsize = ntohl(header->hashsize);
324 for (i = 0; i < HASHSIZE; i++) {
325 header->nameHash[i] = ntohl(header->nameHash[i]);
327 header->checkVersion = ntohl(header->checkVersion);
329 if (header->version != header->checkVersion) {
331 fprintf(stderr, "HEADER VERSION MISMATCH: initial %d, final %d\n",
332 header->version, header->checkVersion);
334 if (header->headerSize != sizeof(struct kaheader)) {
337 "HEADER SIZE WRONG: file indicates %d, should be %d\n",
338 header->headerSize, sizeof(struct kaheader));
340 if (header->hashsize != HASHSIZE) {
342 fprintf(stderr, "HASH SIZE WRONG: file indicates %d, should be %d\n",
343 header->hashsize, HASHSIZE);
345 if ((header->kvnoPtr && ((header->kvnoPtr < header->headerSize)
346 || (header->eofPtr < header->freePtr)))
347 || (header->freePtr && ((header->freePtr < header->headerSize)
348 || (header->eofPtr < header->kvnoPtr)))) {
351 "DATABASE POINTERS BAD: header size = %d, freePtr = %d, kvnoPtr = %d, eofPtr = %d\n",
352 header->headerSize, header->freePtr, header->kvnoPtr,
357 * fprintf(stderr, "DB Version %d, %d possible entries\n", header->version,
358 * (header->eofPtr-header->headerSize) / sizeof(struct kaentry));
365 struct kaentry *entryp;
369 char *aname = entryp->userID.name;
370 char *ainstance = entryp->userID.instance;
372 /* stolen directly from the HashString function in the vol package */
374 for (i = strlen(aname), aname += i - 1; i--; aname--)
375 hash = (hash * 31) + (*((unsigned char *)aname) - 31);
376 for (i = strlen(ainstance), ainstance += i - 1; i--; ainstance--)
377 hash = (hash * 31) + (*((unsigned char *)ainstance) - 31);
378 return (hash % HASHSIZE);
381 readDB(offset, buffer, size)
388 offset += UBIK_HEADERSIZE;
389 code = lseek(fd, offset, SEEK_SET);
390 if (code != offset) {
391 afs_com_err(whoami, errno, "skipping Ubik header");
394 code = read(fd, buffer, size);
396 afs_com_err(whoami, errno, "reading db got %d bytes", code);
401 #include "AFS_component_version_number.c"
404 struct cmd_syndesc *as;
412 struct kaheader header;
413 int nentries, i, j, count;
415 struct kaentry entry;
417 dbFile = as->parms[0].items->data; /* -database */
418 listuheader = (as->parms[1].items ? 1 : 0); /* -uheader */
419 listkheader = (as->parms[2].items ? 1 : 0); /* -kheader */
420 listentries = (as->parms[3].items ? 1 : 0); /* -entries */
421 verbose = (as->parms[4].items ? 1 : 0); /* -verbose */
422 outFile = (as->parms[5].items ? as->parms[5].items->data : NULL); /* -rebuild */
425 out = fopen(outFile, "w");
427 afs_com_err(whoami, errno, "opening output file %s", outFile);
433 fd = open(dbFile, O_RDONLY, 0);
435 afs_com_err(whoami, errno, "opening database file %s", dbFile);
438 code = fstat(fd, &info);
440 afs_com_err(whoami, errno, "stat'ing file %s", dbFile);
443 if ((info.st_size - UBIK_HEADERSIZE) % UBIK_BUFFERSIZE)
445 "DATABASE SIZE INCONSISTENT: was %d, should be (n*%d + %d), for integral n\n",
446 info.st_size, UBIK_BUFFERSIZE, UBIK_HEADERSIZE);
450 readDB(0, &header, sizeof(header));
451 code = CheckHeader(&header);
453 PrintHeader(&header);
457 (UBIK_HEADERSIZE + header.headerSize)) / sizeof(struct kaentry);
458 entrys = (int *)malloc(nentries * sizeof(int));
459 memset(entrys, 0, nentries * sizeof(int));
461 for (i = 0, index = sizeof(header); i < nentries;
462 i++, index += sizeof(struct kaentry)) {
463 readDB(index, &entry, sizeof(entry));
465 if (index >= header.eofPtr) {
467 } else if (listentries) {
468 PrintEntry(index, &entry);
471 if (entry.flags & KAFNORMAL) {
472 entrys[i] |= 0x1; /* user entry */
474 if (strlen(entry.userID.name) == 0) {
476 printf("Entry %d has zero length name\n", i);
479 if (!des_check_key_parity(&entry.key)
480 || des_is_weak_key(&entry.key)) {
481 fprintf(stderr, "Entry %d, %s, has bad key\n", i,
487 RebuildEntry(&entry);
490 } else if (entry.flags & KAFFREE) {
491 entrys[i] |= 0x2; /* free entry */
493 } else if (entry.flags & KAFOLDKEYS) {
494 entrys[i] |= 0x4; /* old keys block */
495 /* Should check the structure of the oldkeys block? */
498 if (index < header.eofPtr) {
499 fprintf(stderr, "Entry %d is unrecognizable\n", i);
504 /* Follow the hash chains */
505 for (j = 0; j < HASHSIZE; j++) {
506 for (index = header.nameHash[j]; index; index = entry.next) {
507 readDB(index, &entry, sizeof(entry));
509 /* check to see if the name is hashed correctly */
510 i = NameHash(&entry);
513 "Entry %d, %s, found in hash chain %d (should be %d)\n",
515 sizeof(struct kaheader)) / sizeof(struct kaentry)),
516 EntryName(&entry), j, i);
519 /* Is it on another hash chain or circular hash chain */
520 i = (index - header.headerSize) / sizeof(entry);
521 if (entrys[i] & 0x10) {
523 "Entry %d, %s, hash index %d, was found on another hash chain\n",
524 i, EntryName(&entry), j);
526 fprintf(stderr, "Skipping rest of hash chain %d\n", j);
528 fprintf(stderr, "No next entry in hash chain %d\n", j);
532 entrys[i] |= 0x10; /* On hash chain */
536 /* Follow the free pointers */
538 for (index = header.freePtr; index; index = entry.next) {
539 readDB(index, &entry, sizeof(entry));
541 /* Is it on another chain or circular free chain */
542 i = (index - header.headerSize) / sizeof(entry);
543 if (entrys[i] & 0x20) {
544 fprintf(stderr, "Entry %d, %s, already found on free chain\n", i,
546 fprintf(stderr, "Skipping rest of free chain\n");
550 entrys[i] |= 0x20; /* On free chain */
555 printf("Found %d free entries\n", count);
557 /* Follow the oldkey blocks */
559 for (index = header.kvnoPtr; index; index = entry.next) {
560 readDB(index, &entry, sizeof(entry));
562 /* Is it on another chain or circular free chain */
563 i = (index - header.headerSize) / sizeof(entry);
564 if (entrys[i] & 0x40) {
565 fprintf(stderr, "Entry %d, %s, already found on olkeys chain\n",
566 i, EntryName(&entry));
567 fprintf(stderr, "Skipping rest of oldkeys chain\n");
571 entrys[i] |= 0x40; /* On free chain */
576 printf("Found %d oldkey blocks\n", count);
578 /* Now recheck all the blocks and see if they are allocated correctly
579 * 0x1 --> User Entry 0x10 --> On hash chain
580 * 0x2 --> Free Entry 0x20 --> On Free chain
581 * 0x4 --> OldKeys Entry 0x40 --> On Oldkeys chain
584 for (i = 0; i < nentries; i++) {
586 if (j & 0x1) { /* user entry */
588 badEntry(j, i); /* on hash chain? */
590 badEntry(j, i); /* anything else? */
591 } else if (j & 0x2) { /* free entry */
593 badEntry(j, i); /* on free chain? */
595 badEntry(j, i); /* anything else? */
596 } else if (j & 0x4) { /* oldkeys entry */
598 badEntry(j, i); /* on oldkeys chain? */
600 badEntry(j, i); /* anything else? */
601 } else if (j & 0x8) { /* past eof */
603 badEntry(j, i); /* anything else? */
605 badEntry(j, i); /* anything else? */
616 struct kaentry entry;
618 offset = i * sizeof(struct kaentry) + sizeof(struct kaheader);
619 readDB(offset, &entry, sizeof(entry));
621 fprintf(stderr, "Entry %d, %s, hash index %d, is bad: [", i,
622 EntryName(&entry), NameHash(&entry));
624 fprintf(stderr, " UserEntry");
626 fprintf(stderr, " FreeEntry");
628 fprintf(stderr, " OldkeysEntry");
630 fprintf(stderr, " PastEOF");
632 fprintf(stderr, " <NULL>");
633 fprintf(stderr, " ] [");
635 fprintf(stderr, " UserChain");
637 fprintf(stderr, " FreeChain");
639 fprintf(stderr, " OldkeysChain");
641 fprintf(stderr, " <NULL>");
642 fprintf(stderr, " ]\n");
649 struct cmd_syndesc *ts;
653 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "KADB check");
654 cmd_AddParm(ts, "-database", CMD_SINGLE, CMD_REQUIRED, "kadb_file");
655 cmd_AddParm(ts, "-uheader", CMD_FLAG, CMD_OPTIONAL,
656 "Display UBIK header");
657 cmd_AddParm(ts, "-kheader", CMD_FLAG, CMD_OPTIONAL,
658 "Display KADB header");
659 cmd_AddParm(ts, "-entries", CMD_FLAG, CMD_OPTIONAL, "Display entries");
660 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
661 cmd_AddParm(ts, "-rebuild", CMD_SINGLE, CMD_OPTIONAL, "out_file");
663 return cmd_Dispatch(argc, argv);