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>
39 #include <afs/com_err.h>
45 #define UBIK_HEADERSIZE 64
46 #define UBIK_BUFFERSIZE 1024
48 char *whoami = "kadb_check";
54 int listuheader, listkheader, listentries, verbose;
60 struct ubik_hdr uheader;
62 offset = lseek(fd, 0, 0);
64 printf("error: lseek to 0 failed: %d %d\n", offset, errno);
68 /* now read the info */
69 r = read(fd, &uheader, sizeof(uheader));
70 if (r != sizeof(uheader)) {
71 printf("error: read of %d bytes failed: %d %d\n", sizeof(uheader), r,
76 uheader.magic = ntohl(uheader.magic);
77 uheader.size = ntohl(uheader.size);
78 uheader.version.epoch = ntohl(uheader.version.epoch);
79 uheader.version.counter = ntohl(uheader.version.counter);
82 printf("Ubik Header\n");
83 printf(" Magic = 0x%x\n", uheader.magic);
84 printf(" Size = %u\n", uheader.size);
85 printf(" Version.epoch = %u\n", uheader.version.epoch);
86 printf(" Version.counter = %u\n", uheader.version.counter);
89 if (uheader.size != UBIK_HEADERSIZE)
90 printf("Ubik header size is %u (should be %u)\n", uheader.size,
92 if (uheader.magic != UBIK_MAGIC)
93 printf("Ubik header magic is 0x%x (should be 0x%x)\n", uheader.magic,
101 struct kaheader *header;
103 printf("Version = %d\n", header->version);
104 printf("HeaderSize = %d\n", header->headerSize);
105 printf("Free Ptr = %u\n", header->freePtr);
106 printf("EOF Ptr = %u\n", header->eofPtr);
107 printf("Kvno Ptr = %u\n", header->kvnoPtr);
108 printf("SpecialKeysVersion changed = %d\n", header->specialKeysVersion);
109 printf("# admin accounts = %d\n", header->admin_accounts);
110 printf("HashSize = %d\n", header->hashsize);
111 printf("Check Version = %d\n", header->checkVersion);
112 printf("stats.minorVersion = %d\n", header->stats.minor_version);
113 printf("stats.AllocBlock calls = %d\n", header->stats.allocs);
114 printf("stats.FreeBlock calls = %d\n", header->stats.frees);
115 printf("stats.cpw commands = %d\n", header->stats.cpws);
119 PrintEntry(index, entry)
121 struct kaentry *entry;
129 i = (index - sizeof(struct kaheader)) / sizeof(struct kaentry);
131 printf("Entry %5d (%u):\n", i, index);
133 if (entry->flags & KAFNORMAL) {
134 printf(" Name = %s", entry->userID.name);
135 if (strlen(entry->userID.instance) > 0) {
136 printf(".%s", entry->userID.instance);
142 if (entry->flags & KAFNORMAL)
144 if (entry->flags & KAFADMIN)
146 if (entry->flags & KAFNOTGS)
148 if (entry->flags & KAFNOSEAL)
150 if (entry->flags & KAFNOCPW)
153 if (entry->flags & KAFNEWASSOC)
155 if (entry->flags & KAFFREE)
157 if (entry->flags & KAFOLDKEYS)
159 if (entry->flags & KAFSPECIAL)
161 if (entry->flags & KAFASSOCROOT)
162 printf("ROOT-ASSOC ");
163 if (entry->flags & KAFASSOC)
167 printf(" Next = %u\n", entry->next);
169 if (entry->flags & KAFFREE)
171 if (entry->flags & KAFOLDKEYS)
174 tm_p = localtime((time_t *) & entry->user_expiration);
176 strftime(Time, 100, "%m/%d/%Y %H:%M", tm_p);
178 printf(" User Expiration = %s\n",
179 (entry->user_expiration == 0xffffffff) ? "never" : Time);
181 printf(" Password Expiration = %u days %s\n",
182 entry->misc_auth_bytes[EXPIRES],
183 (entry->misc_auth_bytes[EXPIRES] ? "" : "(never)"));
185 printf(" Password Attempts before lock = ");
186 if (!entry->misc_auth_bytes[ATTEMPTS])
187 printf("unlimited\n");
189 printf("%d\n", entry->misc_auth_bytes[ATTEMPTS]);
191 printf(" Password lockout time = ");
192 if (!entry->misc_auth_bytes[LOCKTIME])
193 printf("unlimited\n");
195 printf("%.1f min\n", (entry->misc_auth_bytes[LOCKTIME] * 8.5));
197 printf(" Is entry locked = %s\n",
198 (entry->misc_auth_bytes[REUSEFLAGS] ==
199 KA_ISLOCKED) ? "yes" : "no");
201 printf(" Permit password reuse = %s\n",
202 (!entry->pwsums[0] && !entry->pwsums[1]) ? "yes" : "no");
204 printf(" Mod Time = %u: %s", entry->modification_time,
205 ctime((time_t *) & entry->modification_time));
206 printf(" Mod ID = %u\n", entry->modification_id);
207 printf(" Change Password Time = %u: %s", entry->change_password_time,
208 ctime((time_t *) & entry->change_password_time));
209 printf(" Ticket lifetime = %u: %s", entry->max_ticket_lifetime,
210 ctime((time_t *) & entry->max_ticket_lifetime));
211 printf(" Key Version = %d\n", entry->key_version);
214 ka_PrintBytes((char *)&entry->key, sizeof(entry->key));
217 /* What about asServer structs and such and misc_ath_bytes */
220 /* ntohEntry - convert back to host-order */
221 ntohEntry(struct kaentry *entryp)
223 entryp->flags = ntohl(entryp->flags);
224 entryp->next = ntohl(entryp->next);
225 entryp->user_expiration = ntohl(entryp->user_expiration);
226 entryp->modification_time = ntohl(entryp->modification_time);
227 entryp->modification_id = ntohl(entryp->modification_id);
228 entryp->change_password_time = ntohl(entryp->change_password_time);
229 entryp->max_ticket_lifetime = ntohl(entryp->max_ticket_lifetime);
230 entryp->key_version = ntohl(entryp->key_version);
231 entryp->misc.asServer.nOldKeys = ntohl(entryp->misc.asServer.nOldKeys);
232 entryp->misc.asServer.oldKeys = ntohl(entryp->misc.asServer.oldKeys);
238 struct kaentry *entryp;
240 char name[32], inst[32];
242 ka_ConvertBytes(name, sizeof(name), entryp->userID.name,
243 strlen(entryp->userID.name));
244 ka_ConvertBytes(inst, sizeof(inst), entryp->userID.instance,
245 strlen(entryp->userID.instance));
247 if (strlen(entryp->userID.instance)) {
248 sprintf(principal, "%s.%s", name, inst);
250 strcpy(principal, name);
258 struct kaentry *entryp;
264 /* Special entries are not rebuilt */
265 if (entryp->flags & KAFSPECIAL)
268 fprintf(out, "create -name %s", EntryName(entryp));
270 ka_ConvertBytes(key, sizeof(key), (char *)&entryp->key,
271 sizeof(entryp->key));
272 fprintf(out, " -initial_password foo\n");
275 if (entryp->flags & KAFADMIN)
276 strcat(flags, "+ADMIN");
277 if (entryp->flags & KAFNOTGS)
278 strcat(flags, "+NOTGS");
279 if (entryp->flags & KAFNOSEAL)
280 strcat(flags, "+NOSEAL");
281 if (entryp->flags & KAFNOCPW)
282 strcat(flags, "+NOCPW");
284 fprintf(out, "setfields -name %s", principal);
285 if (strcmp(flags, "") != 0)
286 fprintf(out, " -flags %s", &flags[1]);
287 if (entryp->user_expiration != 0xffffffff) {
288 strftime(Time, 50, "%m/%d/%Y %H:%M",
289 localtime((time_t *) & entryp->user_expiration));
290 fprintf(out, " -expiration '%s'", Time);
292 fprintf(out, " -lifetime %u", entryp->max_ticket_lifetime);
293 if (entryp->misc_auth_bytes[EXPIRES])
294 fprintf(out, " -pwexpires %u", entryp->misc_auth_bytes[EXPIRES]);
295 if (entryp->pwsums[0] || entryp->pwsums[1])
296 fprintf(out, " -reuse no");
297 if (entryp->misc_auth_bytes[ATTEMPTS])
298 fprintf(out, " -attempts %u", entryp->misc_auth_bytes[ATTEMPTS]);
299 if (entryp->misc_auth_bytes[LOCKTIME])
300 fprintf(out, " -locktime %d",
301 (int)(entryp->misc_auth_bytes[LOCKTIME] * 8.5));
304 fprintf(out, "setkey -name %s -new_key %s -kvno %d\n", principal, key,
305 ntohl(entryp->key_version));
309 struct kaheader *header;
311 afs_int32 i, code = 0;
313 header->version = ntohl(header->version);
314 header->headerSize = ntohl(header->headerSize);
315 header->freePtr = ntohl(header->freePtr);
316 header->eofPtr = ntohl(header->eofPtr);
317 header->kvnoPtr = ntohl(header->kvnoPtr);
318 header->stats.minor_version = ntohl(header->stats.minor_version);
319 header->stats.allocs = ntohl(header->stats.allocs);
320 header->stats.frees = ntohl(header->stats.frees);
321 header->stats.cpws = ntohl(header->stats.cpws);
322 header->admin_accounts = ntohl(header->admin_accounts);
323 header->specialKeysVersion = ntohl(header->specialKeysVersion);
324 header->hashsize = ntohl(header->hashsize);
325 for (i = 0; i < HASHSIZE; i++) {
326 header->nameHash[i] = ntohl(header->nameHash[i]);
328 header->checkVersion = ntohl(header->checkVersion);
330 if (header->version != header->checkVersion) {
332 fprintf(stderr, "HEADER VERSION MISMATCH: initial %d, final %d\n",
333 header->version, header->checkVersion);
335 if (header->headerSize != sizeof(struct kaheader)) {
338 "HEADER SIZE WRONG: file indicates %d, should be %d\n",
339 header->headerSize, sizeof(struct kaheader));
341 if (header->hashsize != HASHSIZE) {
343 fprintf(stderr, "HASH SIZE WRONG: file indicates %d, should be %d\n",
344 header->hashsize, HASHSIZE);
346 if ((header->kvnoPtr && ((header->kvnoPtr < header->headerSize)
347 || (header->eofPtr < header->freePtr)))
348 || (header->freePtr && ((header->freePtr < header->headerSize)
349 || (header->eofPtr < header->kvnoPtr)))) {
352 "DATABASE POINTERS BAD: header size = %d, freePtr = %d, kvnoPtr = %d, eofPtr = %d\n",
353 header->headerSize, header->freePtr, header->kvnoPtr,
358 * fprintf(stderr, "DB Version %d, %d possible entries\n", header->version,
359 * (header->eofPtr-header->headerSize) / sizeof(struct kaentry));
366 struct kaentry *entryp;
370 char *aname = entryp->userID.name;
371 char *ainstance = entryp->userID.instance;
373 /* stolen directly from the HashString function in the vol package */
375 for (i = strlen(aname), aname += i - 1; i--; aname--)
376 hash = (hash * 31) + (*((unsigned char *)aname) - 31);
377 for (i = strlen(ainstance), ainstance += i - 1; i--; ainstance--)
378 hash = (hash * 31) + (*((unsigned char *)ainstance) - 31);
379 return (hash % HASHSIZE);
382 readDB(offset, buffer, size)
389 offset += UBIK_HEADERSIZE;
390 code = lseek(fd, offset, SEEK_SET);
391 if (code != offset) {
392 com_err(whoami, errno, "skipping Ubik header");
395 code = read(fd, buffer, size);
397 com_err(whoami, errno, "reading db got %d bytes", code);
402 #include "AFS_component_version_number.c"
405 struct cmd_syndesc *as;
413 struct kaheader header;
414 int nentries, i, j, count;
416 struct kaentry entry;
418 dbFile = as->parms[0].items->data; /* -database */
419 listuheader = (as->parms[1].items ? 1 : 0); /* -uheader */
420 listkheader = (as->parms[2].items ? 1 : 0); /* -kheader */
421 listentries = (as->parms[3].items ? 1 : 0); /* -entries */
422 verbose = (as->parms[4].items ? 1 : 0); /* -verbose */
423 outFile = (as->parms[5].items ? as->parms[5].items->data : NULL); /* -rebuild */
426 out = fopen(outFile, "w");
428 com_err(whoami, errno, "opening output file %s", outFile);
434 fd = open(dbFile, O_RDONLY, 0);
436 com_err(whoami, errno, "opening database file %s", dbFile);
439 code = fstat(fd, &info);
441 com_err(whoami, errno, "stat'ing file %s", dbFile);
444 if ((info.st_size - UBIK_HEADERSIZE) % UBIK_BUFFERSIZE)
446 "DATABASE SIZE INCONSISTENT: was %d, should be (n*%d + %d), for integral n\n",
447 info.st_size, UBIK_BUFFERSIZE, UBIK_HEADERSIZE);
451 readDB(0, &header, sizeof(header));
452 code = CheckHeader(&header);
454 PrintHeader(&header);
458 (UBIK_HEADERSIZE + header.headerSize)) / sizeof(struct kaentry);
459 entrys = (int *)malloc(nentries * sizeof(int));
460 memset(entrys, 0, nentries * sizeof(int));
462 for (i = 0, index = sizeof(header); i < nentries;
463 i++, index += sizeof(struct kaentry)) {
464 readDB(index, &entry, sizeof(entry));
466 if (index >= header.eofPtr) {
468 } else if (listentries) {
469 PrintEntry(index, &entry);
472 if (entry.flags & KAFNORMAL) {
473 entrys[i] |= 0x1; /* user entry */
475 if (strlen(entry.userID.name) == 0) {
477 printf("Entry %d has zero length name\n", i);
480 if (!des_check_key_parity(&entry.key)
481 || des_is_weak_key(&entry.key)) {
482 fprintf(stderr, "Entry %d, %s, has bad key\n", i,
488 RebuildEntry(&entry);
491 } else if (entry.flags & KAFFREE) {
492 entrys[i] |= 0x2; /* free entry */
494 } else if (entry.flags & KAFOLDKEYS) {
495 entrys[i] |= 0x4; /* old keys block */
496 /* Should check the structure of the oldkeys block? */
499 if (index < header.eofPtr) {
500 fprintf(stderr, "Entry %d is unrecognizable\n", i);
505 /* Follow the hash chains */
506 for (j = 0; j < HASHSIZE; j++) {
507 for (index = header.nameHash[j]; index; index = entry.next) {
508 readDB(index, &entry, sizeof(entry));
510 /* check to see if the name is hashed correctly */
511 i = NameHash(&entry);
514 "Entry %d, %s, found in hash chain %d (should be %d)\n",
516 sizeof(struct kaheader)) / sizeof(struct kaentry)),
517 EntryName(&entry), j, i);
520 /* Is it on another hash chain or circular hash chain */
521 i = (index - header.headerSize) / sizeof(entry);
522 if (entrys[i] & 0x10) {
524 "Entry %d, %s, hash index %d, was found on another hash chain\n",
525 i, EntryName(&entry), j);
527 fprintf(stderr, "Skipping rest of hash chain %d\n", j);
529 fprintf(stderr, "No next entry in hash chain %d\n", j);
533 entrys[i] |= 0x10; /* On hash chain */
537 /* Follow the free pointers */
539 for (index = header.freePtr; index; index = entry.next) {
540 readDB(index, &entry, sizeof(entry));
542 /* Is it on another chain or circular free chain */
543 i = (index - header.headerSize) / sizeof(entry);
544 if (entrys[i] & 0x20) {
545 fprintf(stderr, "Entry %d, %s, already found on free chain\n", i,
547 fprintf(stderr, "Skipping rest of free chain\n");
551 entrys[i] |= 0x20; /* On free chain */
556 printf("Found %d free entries\n", count);
558 /* Follow the oldkey blocks */
560 for (index = header.kvnoPtr; index; index = entry.next) {
561 readDB(index, &entry, sizeof(entry));
563 /* Is it on another chain or circular free chain */
564 i = (index - header.headerSize) / sizeof(entry);
565 if (entrys[i] & 0x40) {
566 fprintf(stderr, "Entry %d, %s, already found on olkeys chain\n",
567 i, EntryName(&entry));
568 fprintf(stderr, "Skipping rest of oldkeys chain\n");
572 entrys[i] |= 0x40; /* On free chain */
577 printf("Found %d oldkey blocks\n", count);
579 /* Now recheck all the blocks and see if they are allocated correctly
580 * 0x1 --> User Entry 0x10 --> On hash chain
581 * 0x2 --> Free Entry 0x20 --> On Free chain
582 * 0x4 --> OldKeys Entry 0x40 --> On Oldkeys chain
585 for (i = 0; i < nentries; i++) {
587 if (j & 0x1) { /* user entry */
589 badEntry(j, i); /* on hash chain? */
591 badEntry(j, i); /* anything else? */
592 } else if (j & 0x2) { /* free entry */
594 badEntry(j, i); /* on free chain? */
596 badEntry(j, i); /* anything else? */
597 } else if (j & 0x4) { /* oldkeys entry */
599 badEntry(j, i); /* on oldkeys chain? */
601 badEntry(j, i); /* anything else? */
602 } else if (j & 0x8) { /* past eof */
604 badEntry(j, i); /* anything else? */
606 badEntry(j, i); /* anything else? */
617 struct kaentry entry;
619 offset = i * sizeof(struct kaentry) + sizeof(struct kaheader);
620 readDB(offset, &entry, sizeof(entry));
622 fprintf(stderr, "Entry %d, %s, hash index %d, is bad: [", i,
623 EntryName(&entry), NameHash(&entry));
625 fprintf(stderr, " UserEntry");
627 fprintf(stderr, " FreeEntry");
629 fprintf(stderr, " OldkeysEntry");
631 fprintf(stderr, " PastEOF");
633 fprintf(stderr, " <NULL>");
634 fprintf(stderr, " ] [");
636 fprintf(stderr, " UserChain");
638 fprintf(stderr, " FreeChain");
640 fprintf(stderr, " OldkeysChain");
642 fprintf(stderr, " <NULL>");
643 fprintf(stderr, " ]\n");
650 struct cmd_syndesc *ts;
654 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "KADB check");
655 cmd_AddParm(ts, "-database", CMD_SINGLE, CMD_REQUIRED, "kadb_file");
656 cmd_AddParm(ts, "-uheader", CMD_FLAG, CMD_OPTIONAL,
657 "Display UBIK header");
658 cmd_AddParm(ts, "-kheader", CMD_FLAG, CMD_OPTIONAL,
659 "Display KADB header");
660 cmd_AddParm(ts, "-entries", CMD_FLAG, CMD_OPTIONAL, "Display entries");
661 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
662 cmd_AddParm(ts, "-rebuild", CMD_SINGLE, CMD_OPTIONAL, "out_file");
664 return cmd_Dispatch(argc, argv);