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
11 #include <afsconfig.h>
12 #include <afs/param.h>
18 * (3) Define a structure, idused, instead of an
19 * array of long integers, idmap, to count group
20 * memberships. These structures are on a linked
21 * list, with each structure containing IDCOUNT
23 * (4) Add new functions to processs the structure
25 * zeromap(), idcount(), inccount().
26 * (5) Add code, primarily in WalkNextChain():
27 * 1. Test id's, allowing groups within groups.
28 * 2. Count the membership list for supergroups,
29 * and follow the continuation chain for
31 * (6) Add fprintf statements for various error
36 #include <sys/types.h>
39 #include <WINNT/afsevent.h>
43 #include <netinet/in.h>
50 #include <afs/cellconfig.h>
51 #include <afs/afsutil.h>
54 #include <afs/com_err.h>
61 struct prheader cheader;
64 char *whoami = "db_verify";
65 #define UBIK_HEADERSIZE 64
68 printheader(struct prheader *h)
70 printf("Version = %d\n", ntohl(h->version));
71 printf("Header Size = %d\n", ntohl(h->headerSize));
72 printf("Free Ptr = 0x%x\n", ntohl(h->freePtr));
73 printf("EOF Ptr = 0x%x\n", ntohl(h->eofPtr));
74 printf("Max Group ID = %d\n", ntohl(h->maxGroup));
75 printf("Max User ID = %d\n", ntohl(h->maxID));
76 printf("Max Foreign ID = %d\n", ntohl(h->maxForeign));
77 /* printf("Max Sub/Super ID = %d\n", ntohl(h->maxInst)); */
78 printf("Orphaned groups = %d\n", ntohl(h->orphan));
79 printf("User Count = %d\n", ntohl(h->usercount));
80 printf("Group Count = %d\n", ntohl(h->groupcount));
81 /* printf("Foreign Count = %d\n", ntohl(h->foreigncount)); NYI */
82 /* printf("Sub/super Count = %d\n", ntohl(h->instcount)); NYI */
83 printf("Name Hash = %d buckets\n", HASHSIZE);
84 printf("ID Hash = %d buckets\n", HASHSIZE);
89 pr_Read(afs_int32 pos, void *buff, afs_int32 len)
93 code = lseek(fd, UBIK_HEADERSIZE + pos, 0);
97 code = read(fd, buff, len);
107 * Initializes the a transaction on the database and reads the header into
108 * the static variable cheader. If successful it returns a read-locked
109 * transaction. If ubik reports that cached database info should be up to date
110 * the cheader structure is not re-read from the ubik.
118 code = pr_Read(0, (char *)&cheader, sizeof(cheader));
120 afs_com_err(whoami, code, "couldn't read header");
123 /* Check and see if database exists and is approximately OK. */
124 if (ntohl(cheader.headerSize) != sizeof(cheader)
125 || ntohl(cheader.eofPtr) == 0) {
128 afs_com_err(whoami, PRDBBAD, "header is bad");
137 /* returns hash bucket for x */
138 return ((abs(x)) % HASHSIZE);
142 NameHash(register char *aname)
144 /* returns hash bucket for aname */
145 register unsigned int hash = 0;
147 /* stolen directly from the HashString function in the vol package */
148 for (i = strlen(aname), aname += i - 1; i--; aname--)
149 hash = (hash * 31) + (*(unsigned char *)aname - 31);
150 return (hash % HASHSIZE);
153 #define MAP_NAMEHASH 1
155 #define MAP_HASHES (MAP_NAMEHASH | MAP_IDHASH)
158 #define MAP_OWNED 0x10
159 #define MAP_RECREATE 0x20
162 int nEntries; /* number of database entries */
163 int anon; /* found anonymous Id */
164 afs_int32 maxId; /* user */
165 afs_int32 minId; /* group */
166 afs_int32 maxForId; /* foreign user id */
167 #if defined(SUPERGROUPS)
171 afs_int32 idcount[IDCOUNT];
172 struct idused *idnext;
175 int idRange; /* number of ids in map */
176 afs_int32 *idmap; /* map of all id's: midId is origin */
177 #endif /* SUPERGROUPS */
178 int nusers; /* counts of each type */
183 int maxOwnerLength; /* longest owner chain */
184 int maxContLength; /* longest chain of cont. blks */
185 int orphanLength; /* length of orphan list */
186 int freeLength; /* length of free list */
191 FILE *recreate; /* stream for recreate instructions */
194 #if defined(SUPERGROUPS)
195 void zeromap(struct idused *idmap);
196 void inccount(struct idused **idmapp, int id);
197 int idcount(struct idused **idmapp, int id);
201 readUbikHeader(struct misc_data *misc)
204 struct ubik_hdr uheader;
206 offset = lseek(fd, 0, 0);
208 printf("error: lseek to 0 failed: %d %d\n", offset, errno);
212 /* now read the info */
213 r = read(fd, &uheader, sizeof(uheader));
214 if (r != sizeof(uheader)) {
215 printf("error: read of %lu bytes failed: %d %d\n", sizeof(uheader), r,
220 uheader.magic = ntohl(uheader.magic);
221 uheader.size = ntohl(uheader.size);
222 uheader.version.epoch = ntohl(uheader.version.epoch);
223 uheader.version.counter = ntohl(uheader.version.counter);
225 if (misc->listuheader) {
226 printf("Ubik Header\n");
227 printf(" Magic = 0x%x\n", uheader.magic);
228 printf(" Size = %u\n", uheader.size);
229 printf(" Version.epoch = %u\n", uheader.version.epoch);
230 printf(" Version.counter = %u\n", uheader.version.counter);
233 if (uheader.size != UBIK_HEADERSIZE)
234 printf("Ubik header size is %u (should be %u)\n", uheader.size,
236 if (uheader.magic != UBIK_MAGIC)
237 printf("Ubik header magic is 0x%x (should be 0x%x)\n", uheader.magic,
244 ConvertDiskAddress(afs_uint32 ea, int *eiP)
250 if (ea < sizeof(cheader))
252 if (ea >= ntohl(cheader.eofPtr))
254 ea -= sizeof(cheader);
255 i = ea / sizeof(struct prentry);
256 if (i * sizeof(struct prentry) != ea)
258 /* if ((i < 0) || (i >= misc->nEntries)) return PRDBADDR; */
264 PrintEntryError(struct misc_data *misc, afs_int32 ea, struct prentry *e, int indent)
267 pr_PrintEntry(stderr, /*net order */ 0, ea, e, indent);
272 WalkHashTable(afs_int32 hashtable[], /* hash table to walk */
273 int hashType, /* hash function to use */
274 char map[], /* one byte per db entry */
275 struct misc_data *misc) /* stuff to keep track of */
278 int hi; /* index in hash table */
279 afs_int32 ea; /* entry's db addr */
280 int ei; /* entry's index */
281 char bit; /* bits to check for in map */
290 for (hi = 0; hi < HASHSIZE; hi++) {
292 next_ea = ntohl(hashtable[hi]);
294 code = ConvertDiskAddress(next_ea, &ei);
296 fprintf(stderr, "Bad chain address %d\n", next_ea);
298 fprintf(stderr, "Last entry in chain:\n");
299 if (PrintEntryError(misc, ea, &e, 2))
302 fprintf(stderr, "Skipping remainder of hash bucket %d\n", hi);
306 code = pr_Read(ea, (char *)&e, sizeof(e));
312 if (((e.flags & htonl((PRGRP | PRINST))) == 0)
313 && (strchr(e.name, '@'))) {
315 if (id > misc->maxForId)
318 if (id == ANONYMOUSID)
320 else if (id > misc->maxId)
322 if (id < misc->minId)
328 next_ea = ntohl(e.nextName);
329 hash = NameHash(e.name);
332 next_ea = ntohl(e.nextID);
336 fprintf(stderr, "unknown hash table type %d\n", hashType);
342 "Entry found twice in hash table: bucket %d\n", hi);
344 fprintf(stderr, "also in wrong bucket: should be in %d\n",
346 if (PrintEntryError(misc, ea, &e, 2))
352 flags = ntohl(e.flags);
353 switch (flags & PRTYPE) {
355 fprintf(stderr, "ENTRY IS FREE");
358 fprintf(stderr, "ENTRY IS CONTINUATION");
366 fprintf(stderr, "ENTRY IS unexpected type (flags=0x%x)\n",
370 fprintf(stderr, "ENTRY IS OF unknown type (flags=0x%x)\n",
376 fprintf(stderr, "entry hashed in bucket %d should be %d\n",
379 if (PrintEntryError(misc, ea, &e, 2))
389 WalkNextChain(char map[], /* one byte per db entry */
390 struct misc_data *misc, /* stuff to keep track of */
391 afs_int32 ea, struct prentry *e)
396 struct prentry c; /* continuation entry */
397 afs_int32 na; /* next thread */
400 int count = 0; /* number of members, set to > 9999 if */
401 /* list ends early */
404 int length; /* length of chain */
405 #if defined(SUPERGROUPS)
406 int sgcount; /* number of sgentrys */
408 #define g (((struct prentryg *)e))
412 head = ntohl(e->next);
415 #if defined(SUPERGROUPS)
417 sghead = ntohl(g->next);
419 for (i = 0; i < PRSIZE; i++) {
420 afs_int32 id = ntohl(e->entries[i]);
426 /* in case the ids are large, convert to pure sign. */
435 #if defined(SUPERGROUPS)
436 if (id_s > 0 && eid_s > 0) {
438 "User can't be member of user in membership list\n");
439 if (PrintEntryError(misc, ea, e, 2))
444 if (id_s * eid_s > 0) { /* sign should be different */
446 "Bad user/group dicotomy in membership list\n");
447 if (PrintEntryError(misc, ea, e, 2))
451 #endif /* SUPERGROUPS */
452 /* count each user as a group, and each group a user is in */
453 #if defined(SUPERGROUPS)
454 if (!(id < 0 && eid < 0) && (id != ANONYMOUSID))
455 inccount(&misc->idmap, id);
457 if ((id >= misc->minId) && (id <= misc->maxId)
458 && (id != ANONYMOUSID))
459 misc->idmap[id - misc->minId]++;
460 #endif /* SUPERGROUPS */
466 #if defined(SUPERGROUPS)
467 sghead = ntohl(g->nextsg);
468 if ((e->flags & htonl(PRGRP))) {
469 for (i = 0; i < SGSIZE; ++i) {
470 afs_int32 id = ntohl(g->supergroup[i]);
476 "User can't be member of supergroup list\n");
477 if (PrintEntryError(misc, ea, e, 2))
482 inccount(&misc->idmap, id);
486 #endif /* SUPERGROUPS */
488 head = ntohl(cheader.freePtr);
489 #if defined(SUPERGROUPS)
495 #if defined(SUPERGROUPS)
497 for (na = sghead; na; na = ntohl(c.next)) {
498 code = ConvertDiskAddress(na, &ni);
500 fprintf(stderr, "Bad SGcontinuation ptr %d", na);
501 if (PrintEntryError(misc, ea, e, 2))
504 fprintf(stderr, "last block: \n");
505 if (PrintEntryError(misc, na, &c, 4))
510 code = pr_Read(na, (char *)&c, sizeof(c));
516 fprintf(stderr, "Continuation entry reused\n");
517 if (PrintEntryError(misc, ea, e, 2))
519 if (PrintEntryError(misc, na, &c, 4))
525 if ((ntohl(c.id) != eid)) {
526 fprintf(stderr, "Continuation id mismatch\n");
527 if (PrintEntryError(misc, ea, e, 2))
529 if (PrintEntryError(misc, na, &c, 4))
535 /* update membership count */
536 for (i = 0; i < COSIZE; i++) {
537 afs_int32 id = ntohl(c.entries[i]);
543 /* in case the ids are large, convert to pure sign. */
554 "User can't be member of supergroup list\n");
555 if (PrintEntryError(misc, ea, e, 2))
557 if (PrintEntryError(misc, na, &c, 4))
561 /* count each user as a group, and each group a user is in */
562 if ((id != ANONYMOUSID))
563 inccount(&misc->idmap, id);
570 if (length > misc->maxContLength)
571 misc->maxContLength = length;
572 #endif /* SUPERGROUPS */
574 for (na = head; na; na = ntohl(c.next)) {
575 code = ConvertDiskAddress(na, &ni);
577 fprintf(stderr, "Bad continuation ptr %d", na);
579 fprintf(stderr, "walking free list");
580 else if (PrintEntryError(misc, ea, e, 2))
583 fprintf(stderr, "last block: \n");
584 if (PrintEntryError(misc, na, &c, 4))
589 code = pr_Read(na, (char *)&c, sizeof(c));
595 fprintf(stderr, "Continuation entry reused\n");
597 fprintf(stderr, "walking free list");
598 else if (PrintEntryError(misc, ea, e, 2))
600 if (PrintEntryError(misc, na, &c, 4))
606 if (e && (ntohl(c.id) != eid)) {
607 fprintf(stderr, "Continuation id mismatch\n");
609 fprintf(stderr, "walking free list");
610 else if (PrintEntryError(misc, ea, e, 2))
612 if (PrintEntryError(misc, na, &c, 4))
618 /* update membership count */
620 for (i = 0; i < COSIZE; i++) {
621 afs_int32 id = ntohl(c.entries[i]);
627 /* in case the ids are large, convert to pure sign. */
636 #if defined(SUPERGROUPS)
637 if (id_s > 0 && eid_s > 0) {
639 "User can't be member of user in membership list\n");
640 if (PrintEntryError(misc, ea, e, 2))
642 if (PrintEntryError(misc, na, &c, 4))
647 if (id_s * eid_s > 0) { /* sign should be different */
649 "Bad user/group dicotomy in membership list\n");
650 if (PrintEntryError(misc, ea, e, 2))
652 if (PrintEntryError(misc, na, &c, 4))
656 #endif /* SUPERGROUPS */
657 /* count each user as a group, and each group a user is in */
658 #if defined(SUPERGROUPS)
659 if (!(id < 0 && eid < 0) && (id != ANONYMOUSID))
660 inccount(&misc->idmap, id);
662 if ((id >= misc->minId) && (id <= misc->maxId)
663 && (id != ANONYMOUSID))
664 misc->idmap[id - misc->minId]++;
665 #endif /* SUPERGROUPS */
672 if (e && noErrors && (count != ntohl(e->count))) {
673 #if defined(SUPERGROUPS)
675 fprintf(stderr, "Membership list ends early\n");
678 fprintf(stderr, "Membership list ends early\n");
679 #endif /* SUPERGROUPS */
680 fprintf(stderr, "Count was %d should be %d\n", count,
682 if (PrintEntryError(misc, ea, e, 2))
684 #if defined(SUPERGROUPS)
687 if (e && (e->flags & htonl(PRGRP)) && (sgcount != ntohl(g->countsg))) {
688 fprintf(stderr, "SGCount was %d should be %d\n", sgcount,
690 if (PrintEntryError(misc, ea, e, 2))
696 if (length > misc->maxContLength)
697 misc->maxContLength = length;
699 misc->freeLength = length;
702 #if defined(SUPERGROUPS)
708 WalkOwnedChain(char map[], /* one byte per db entry */
709 struct misc_data *misc, /* stuff to keep track of */
710 afs_int32 ea, struct prentry *e)
714 struct prentry c; /* continuation entry */
715 afs_int32 na; /* next thread */
718 int length; /* length of chain */
721 head = ntohl(e->owned);
724 head = ntohl(cheader.orphan);
727 for (na = head; na; na = ntohl(c.nextOwned)) {
728 code = ConvertDiskAddress(na, &ni);
730 fprintf(stderr, "Bad owned list ptr %d", na);
732 fprintf(stderr, "walking orphan list");
733 else if (PrintEntryError(misc, ea, e, 2))
736 fprintf(stderr, "last block: \n");
737 if (PrintEntryError(misc, na, &c, 4))
742 code = pr_Read(na, (char *)&c, sizeof(c));
747 if (map[ni] & MAP_OWNED) {
748 fprintf(stderr, "Entry on multiple owner chains\n");
750 fprintf(stderr, "walking orphan list");
751 else if (PrintEntryError(misc, ea, e, 2))
753 if (PrintEntryError(misc, na, &c, 4))
757 map[ni] |= MAP_OWNED;
758 if ((map[ni] & MAP_HASHES) != MAP_HASHES) {
759 fprintf(stderr, "Owned entry not hashed properly\n");
762 fprintf(stderr, "walking orphan list");
763 else if (PrintEntryError(misc, ea, e, 2))
765 if (PrintEntryError(misc, na, &c, 4))
770 if (ntohl(c.owner) != eid) {
771 fprintf(stderr, "Owner id mismatch\n");
774 } else /* orphan */ if (c.owner) {
775 fprintf(stderr, "Orphan group owner not zero\n");
781 if (length > misc->maxOwnerLength)
782 misc->maxOwnerLength = length;
784 misc->orphanLength = length;
790 WalkChains(char map[], /* one byte per db entry */
791 struct misc_data *misc) /* stuff to keep track of */
795 afs_int32 ea; /* entry's db addr */
800 /* check all entries found in hash table walks */
801 for (ei = 0; ei < misc->nEntries; ei++)
802 if (map[ei] & MAP_HASHES) {
803 ea = ei * sizeof(struct prentry) + sizeof(cheader);
804 code = pr_Read(ea, (char *)&e, sizeof(e));
808 if ((map[ei] & MAP_HASHES) != MAP_HASHES) {
809 fprintf(stderr, "entry not in both hashtables\n");
810 if ((map[ei] & MAP_NAMEHASH) != MAP_NAMEHASH)
811 fprintf(stderr, "--> entry not in Name hashtable\n");
812 if ((map[ei] & MAP_IDHASH) != MAP_IDHASH)
813 fprintf(stderr, "--> entry not in ID hashtable\n");
816 if (PrintEntryError(misc, ea, &e, 2))
823 type = ntohl(e.flags) & PRTYPE;
827 fprintf(stderr, "Group id not negative\n");
830 /* special case sysadmin: it owns itself */
831 if (id == SYSADMINID) {
832 if (ntohl(e.owner) != SYSADMINID) {
834 "System:administrators doesn't own itself\n");
838 code = WalkOwnedChain(map, misc, ea, &e);
841 code = WalkNextChain(map, misc, ea, &e);
848 #if defined(SUPERGROUPS)
849 fprintf(stderr, "User id not positive\n");
851 fprintf(stderr, "User id negative\n");
856 /* Users are owned by sysadmin, but sysadmin doesn't have an owner
857 * chain. Check this then set the owned bit. */
858 if (ntohl(e.owner) != SYSADMINID) {
860 "User not owned by system:administrators\n");
864 fprintf(stderr, "User has owned pointer\n");
867 map[ei] |= MAP_OWNED;
869 code = WalkOwnedChain(map, misc, ea, &e);
872 code = WalkNextChain(map, misc, ea, &e);
875 if (strchr(e.name, '@') == 0) {
876 misc->nusers++; /* Not a foreign user */
878 misc->nforeigns++; /* A foreign user */
888 "ENTRY IS unexpected type [PRFOREIGN] (flags=0x%x)\n",
895 fprintf(stderr, "entry with unexpected type");
904 GC(char map[], struct misc_data *misc)
912 for (ei = 0; ei < misc->nEntries; ei++) {
913 ea = ei * sizeof(struct prentry) + sizeof(cheader);
914 code = pr_Read(ea, (char *)&e, sizeof(e));
919 fprintf(stderr, "Unreferenced entry:");
920 if (PrintEntryError(misc, ea, &e, 2))
923 /* all users and groups should be owned, and their membership counts
925 else if ((m & MAP_HASHES) == MAP_HASHES) {
928 if (!(m & MAP_OWNED)) {
929 fprintf(stderr, "Entry not on any owner chain:\n");
930 if (PrintEntryError(misc, ea, &e, 2))
934 #if defined(SUPERGROUPS)
935 if ((id != ANONYMOUSID)
936 && ((refCount = idcount(&misc->idmap, id)) != ntohl(e.count)))
938 if ((id >= misc->minId) && (id <= misc->maxId)
939 && (id != ANONYMOUSID)
940 && ((refCount = misc->idmap[id - misc->minId]) !=
942 #endif /* SUPERGROUPS */
946 "Entry membership count is inconsistent: %d entries refer to this one\n",
948 if (PrintEntryError(misc, ea, &e, 2))
951 /* get continuation blocks too */
952 for (na = ntohl(e.next); na; na = ntohl(e.next)) {
954 code = ConvertDiskAddress(na, &ni);
957 code = pr_Read(na, (char *)&e, sizeof(e));
960 if (PrintEntryError(misc, na, &e, 4))
973 if (strpbrk(s, " \t")) {
974 qs = (char *)malloc(strlen(s) + 3);
984 DumpRecreate(char map[], struct misc_data *misc)
994 int builtinUsers = 0;
995 int createLow = 0; /* users uncreate from here */
996 #if defined(SUPERGROUPS)
997 struct idused *idmap; /* map of all id's */
999 afs_int32 *idmap; /* map of all id's */
1004 rc = misc->recreate;
1005 idmap = misc->idmap;
1006 #if defined(SUPERGROUPS)
1009 memset(idmap, 0, misc->idRange * sizeof(misc->idmap[0]));
1013 for (ei = createLow; ei < misc->nEntries; ei++) {
1014 if ((map[ei] & MAP_HASHES) && (map[ei] & MAP_RECREATE) == 0) {
1019 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1020 code = pr_Read(ea, (char *)&e, sizeof(e));
1024 if (misc->listentries)
1025 pr_PrintEntry(stdout, 0 /*not in host order */ , ea, &e,
1029 flags = ntohl(e.flags);
1030 owner = ntohl(e.owner);
1031 name = QuoteName(e.name);
1033 if (!strcmp(e.name, "system:administrators")
1034 || !strcmp(e.name, "system:anyuser")
1035 || !strcmp(e.name, "system:authuser")
1036 || !strcmp(e.name, "system:backup")
1037 || !strcmp(e.name, "anonymous")) {
1042 /* check for duplicate id. This may still lead to duplicate
1044 #if defined(SUPERGROUPS)
1045 if (idcount(&idmap, id))
1047 if (idmap[id - misc->minId])
1050 fprintf(stderr, "Skipping entry with duplicate id %di\n",
1055 /* If owner doesn't exist skip for now, unless we're our own
1056 * owner. If so, a special case allows a group to own itself
1057 * if caller is sysadmin. This leaves only owner cycles to
1060 if ((owner < misc->minId) || (owner > misc->maxId)) {
1061 if (owner == ANONYMOUSID)
1063 "Warning: id %di is owned by ANONYMOUS; using sysadmin instead\n",
1067 "Bogus owner (%d) of id %di; using sysadmin instead\n",
1072 fprintf(stderr, "Warning: group %s is self owning\n",
1074 } else if (owner == 0) {
1076 "Warning: orphan group %s will become self owning.\n",
1080 #if defined(SUPERGROUPS)
1081 else if (!idcount(&idmap, owner))
1084 else if (idmap[owner - misc->minId] == 0)
1089 fprintf(rc, "cr %s %d %d\n", name, id, owner);
1091 gq = uq = access = mask = 0;
1092 if (flags & PRACCESS) {
1093 access = (flags >> PRIVATE_SHIFT);
1094 mask |= PR_SF_ALLBITS;
1096 if (flags & PRQUOTA) {
1097 gq = ntohl(e.ngroups);
1098 uq = ntohl(e.nusers);
1099 mask |= PR_SF_NGROUPS | PR_SF_NUSERS;
1102 fprintf(rc, "sf %d %x %x %d %d\n", id, mask, access, gq,
1106 map[ei] |= MAP_RECREATE;
1107 #if defined(SUPERGROUPS)
1108 if (id != ANONYMOUSID)
1109 inccount(&idmap, id);
1111 if (id != ANONYMOUSID)
1112 idmap[id - misc->minId]++;
1116 /* bump low water mark if possible */
1117 if (ei == createLow)
1124 /* Now create the entries with circular owner dependencies and make them
1125 * own themselves. This is the only way to create them with the correct
1127 for (ei = 0; ei < misc->nEntries; ei++)
1128 if (((map[ei] & MAP_HASHES) == MAP_HASHES)
1129 && (map[ei] & MAP_RECREATE) == 0) {
1130 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1131 code = pr_Read(ea, (char *)&e, sizeof(e));
1136 name = QuoteName(e.name);
1137 fprintf(stderr, "Warning: group %s in self owning cycle\n", name);
1139 fprintf(rc, "cr %s %d %d\n", name, id, id);
1140 #if defined(SUPERGROUPS)
1141 inccount(&idmap, id);
1143 idmap[id - misc->minId]++;
1146 for (ei = 0; ei < misc->nEntries; ei++)
1147 if (((map[ei] & MAP_HASHES) == MAP_HASHES)
1148 && (map[ei] & MAP_RECREATE) == 0) {
1149 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1150 code = pr_Read(ea, (char *)&e, sizeof(e));
1154 owner = ntohl(e.owner);
1155 #if defined(SUPERGROUPS)
1156 if (!idcount(&idmap, owner))
1158 if (idmap[owner - misc->minId] == 0)
1162 "Skipping chown of '%s' to non-existant owner %di\n",
1165 fprintf(rc, "ce %d \"\" %d 0\n", ntohl(e.id), e.owner);
1171 /* Reconstruct membership information based on the groups' user lists. */
1172 for (ei = 0; ei < misc->nEntries; ei++) {
1173 if ((map[ei] & MAP_HASHES) == MAP_HASHES) {
1174 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1175 code = pr_Read(ea, (char *)&e, sizeof(e));
1180 flags = ntohl(e.flags);
1182 if ((id < 0) && (flags & PRGRP)) {
1185 #if defined(SUPERGROUPS)
1189 for (i = 0; i < PRSIZE; i++) {
1190 afs_int32 uid = ntohl(e.entries[i]);
1195 #if !defined(SUPERGROUPS)
1198 fprintf(rc, "au %d %d\n", uid, id);
1200 #if !defined(SUPERGROUPS)
1202 fprintf(stderr, "Skipping %di in group %di\n", uid,
1206 #if defined(SUPERGROUPS)
1207 #define g (*((struct prentryg *)&e))
1208 ng = ntohl(g.nextsg);
1209 for (i = 0; i < SGSIZE; i++) {
1210 afs_int32 uid = ntohl(g.supergroup[i]);
1215 fprintf(rc, "au %d %d\n", uid, id);
1220 code = pr_Read(ng, (char *)&c, sizeof(c));
1224 if ((id == ntohl(c.id)) && (c.flags & htonl(PRCONT))) {
1225 for (i = 0; i < COSIZE; i++) {
1226 afs_int32 uid = ntohl(c.entries[i]);
1231 fprintf(rc, "au %d %d\n", uid, id);
1235 fprintf(stderr, "Skipping continuation block at %d\n",
1242 #endif /* SUPERGROUPS */
1246 code = pr_Read(na, (char *)&c, sizeof(c));
1250 if ((id == ntohl(c.id)) && (c.flags & htonl(PRCONT))) {
1251 for (i = 0; i < COSIZE; i++) {
1252 afs_int32 uid = ntohl(c.entries[i]);
1257 #if !defined(SUPERGROUPS)
1260 fprintf(rc, "au %d %d\n", uid, id);
1262 #if !defined(SUPERGROUPS)
1264 fprintf(stderr, "Skipping %di in group %di\n",
1269 fprintf(stderr, "Skipping continuation block at %d\n",
1275 if (count != ntohl(e.count))
1277 "Group membership count problem found %d should be %d\n",
1278 count, ntohl(e.count));
1279 } else if ((id < 0) || (flags & PRGRP)) {
1280 fprintf(stderr, "Skipping group %di\n", id);
1288 CheckPrDatabase(struct misc_data *misc) /* info & statistics */
1293 char *map; /* map of each entry in db */
1295 eof = ntohl(cheader.eofPtr);
1296 eof -= sizeof(cheader);
1297 n = eof / sizeof(struct prentry);
1298 if ((eof < 0) || (n * sizeof(struct prentry) != eof)) {
1300 afs_com_err(whoami, code, "eof ptr no good: eof=%d, sizeof(prentry)=%d",
1301 eof, sizeof(struct prentry));
1306 printf("Database has %d entries\n", n);
1307 map = (char *)malloc(n);
1311 if (misc->verbose) {
1312 printf("\nChecking name hash table\n");
1315 code = WalkHashTable(cheader.nameHash, MAP_NAMEHASH, map, misc);
1317 afs_com_err(whoami, code, "walking name hash");
1320 if (misc->verbose) {
1321 printf("\nChecking id hash table\n");
1324 code = WalkHashTable(cheader.idHash, MAP_IDHASH, map, misc);
1326 afs_com_err(whoami, code, "walking id hash");
1330 /* hash walk calculates min and max id */
1331 #if defined(SUPERGROUPS)
1334 n = ((misc->maxId > misc->maxForId) ? misc->maxId : misc->maxForId);
1335 misc->idRange = n - misc->minId + 1;
1336 misc->idmap = (afs_int32 *) malloc(misc->idRange * sizeof(afs_int32));
1338 afs_com_err(whoami, 0, "Unable to malloc space for max ids of %d",
1343 memset(misc->idmap, 0, misc->idRange * sizeof(misc->idmap[0]));
1344 #endif /* SUPERGROUPS */
1346 if (misc->verbose) {
1347 printf("\nChecking entry chains\n");
1350 code = WalkChains(map, misc);
1352 afs_com_err(whoami, code, "walking chains");
1355 if (misc->verbose) {
1356 printf("\nChecking free list\n");
1359 code = WalkNextChain(map, misc, 0, 0);
1361 afs_com_err(whoami, code, "walking free list");
1364 if (misc->verbose) {
1365 printf("\nChecking orphans list\n");
1368 code = WalkOwnedChain(map, misc, 0, 0);
1370 afs_com_err(whoami, code, "walking orphan list");
1374 if (misc->verbose) {
1375 printf("\nChecking for unreferenced entries\n");
1378 code = GC(map, misc);
1380 afs_com_err(whoami, code, "looking for unreferenced entries");
1384 DumpRecreate(map, misc); /* check for owner cycles */
1386 fclose(misc->recreate);
1388 if (misc->anon != 2) /* once for each hash table */
1389 fprintf(stderr, "Problems with ANON=%d\n", misc->anon);
1390 if (misc->ncells || misc->ninsts)
1391 fprintf(stderr, "Unexpected entry type\n");
1392 if (misc->nusers != ntohl(cheader.usercount)) {
1394 "User count inconsistent: should be %d, header claims: %d\n",
1395 misc->nusers, ntohl(cheader.usercount));
1397 if (misc->ngroups != ntohl(cheader.groupcount)) {
1399 "Group count inconsistent: should be %d, header claims: %d\n",
1400 misc->ngroups, ntohl(cheader.groupcount));
1402 if (misc->maxId > ntohl(cheader.maxID))
1404 "Database's max user Id (%d) is smaller than largest user's Id (%d).\n",
1405 ntohl(cheader.maxID), misc->maxId);
1406 if (misc->minId < ntohl(cheader.maxGroup))
1408 "Database's max group Id (%d) is smaller than largest group's Id (%d).\n",
1409 ntohl(cheader.maxGroup), misc->minId);
1411 if (misc->verbose) {
1412 printf("\nMaxId = %d, MinId = %d, MaxForeignId = %d\n", misc->maxId,
1413 misc->minId, misc->maxForId);
1415 ("Free list is %d entries in length, %d groups on orphan list\n",
1416 misc->freeLength, misc->orphanLength);
1418 ("The longest owner list is %d, the longest continuation block chain is %d\n",
1419 misc->maxOwnerLength, misc->maxContLength);
1420 printf("%d users ; %d foreign users ; and %d groups\n", misc->nusers,
1421 misc->nforeigns, misc->ngroups);
1428 #include "AFS_component_version_number.c"
1431 WorkerBee(struct cmd_syndesc *as, void *arock)
1435 struct misc_data misc; /* info & statistics */
1437 initialize_PT_error_table();
1438 initialize_U_error_table();
1440 pr_dbaseName = AFSDIR_SERVER_PRDB_FILEPATH;
1441 memset(&misc, 0, sizeof(misc));
1443 pr_dbaseName = as->parms[0].items->data; /* -database */
1444 misc.listuheader = (as->parms[1].items ? 1 : 0); /* -uheader */
1445 misc.listpheader = (as->parms[2].items ? 1 : 0); /* -pheader */
1446 misc.listentries = (as->parms[3].items ? 1 : 0); /* -entries */
1447 misc.verbose = (as->parms[4].items ? 1 : 0); /* -verbose */
1448 recreateFile = (as->parms[5].items ? as->parms[5].items->data : NULL); /* -rebuild */
1450 fd = open(pr_dbaseName, O_RDONLY, 0);
1452 afs_com_err(whoami, errno, "Open failed on db %s", pr_dbaseName);
1456 /* Read the ubik header */
1457 if (misc.listuheader) {
1458 readUbikHeader(&misc);
1461 code = ReadHeader();
1464 if (misc.listpheader)
1465 printheader(&cheader);
1468 misc.recreate = fopen(recreateFile, "w");
1469 if (misc.recreate == 0) {
1470 afs_com_err(whoami, errno,
1471 "can't create file for recreation instructions: %s",
1476 code = CheckPrDatabase(&misc);
1478 afs_com_err(whoami, code, "Checking prserver database");
1485 main(int argc, char *argv[])
1487 struct cmd_syndesc *ts;
1491 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "PRDB check");
1492 cmd_AddParm(ts, "-database", CMD_SINGLE, CMD_REQUIRED, "ptdb_file");
1493 cmd_AddParm(ts, "-uheader", CMD_FLAG, CMD_OPTIONAL,
1494 "Display UBIK header");
1495 cmd_AddParm(ts, "-pheader", CMD_FLAG, CMD_OPTIONAL,
1496 "Display KADB header");
1497 cmd_AddParm(ts, "-entries", CMD_FLAG, CMD_OPTIONAL, "Display entries");
1498 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
1499 cmd_AddParm(ts, "-rebuild", CMD_SINGLE, CMD_OPTIONAL | CMD_HIDE,
1502 return cmd_Dispatch(argc, argv);
1506 #if defined(SUPERGROUPS)
1508 /* new routines to deal with very large ID numbers */
1511 zeromap(struct idused *idmap)
1514 memset((char *)idmap->idcount, 0, sizeof idmap->idcount);
1515 idmap = idmap->idnext;
1520 inccount(struct idused **idmapp, int id)
1522 struct idused *idmap;
1524 if (IDCOUNT & (IDCOUNT - 1)) {
1525 fprintf(stderr, "IDCOUNT must be power of 2!\n");
1528 while (idmap = *idmapp) {
1529 if (idmap->idstart == (id & ~(IDCOUNT - 1)))
1531 idmapp = &idmap->idnext;
1534 idmap = (struct idused *)malloc(sizeof *idmap);
1539 memset((char *)idmap, 0, sizeof idmap);
1540 idmap->idstart = id & ~(IDCOUNT - 1);
1541 idmap->idnext = *idmapp;
1544 ++idmap->idcount[id & (IDCOUNT - 1)];
1548 idcount(struct idused **idmapp, int id)
1550 struct idused *idmap;
1552 if (IDCOUNT & (IDCOUNT - 1)) {
1553 fprintf(stderr, "IDCOUNT must be power of 2!\n");
1556 while (idmap = *idmapp) {
1557 if (idmap->idstart == (id & ~(IDCOUNT - 1))) {
1558 return idmap->idcount[id & (IDCOUNT - 1)];
1560 idmapp = &idmap->idnext;
1564 #endif /* SUPERGROUPS */