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>
60 struct prheader cheader;
63 char *whoami = "db_verify";
64 #define UBIK_HEADERSIZE 64
67 printheader(struct prheader *h)
69 printf("Version = %d\n", ntohl(h->version));
70 printf("Header Size = %d\n", ntohl(h->headerSize));
71 printf("Free Ptr = 0x%x\n", ntohl(h->freePtr));
72 printf("EOF Ptr = 0x%x\n", ntohl(h->eofPtr));
73 printf("Max Group ID = %d\n", ntohl(h->maxGroup));
74 printf("Max User ID = %d\n", ntohl(h->maxID));
75 printf("Max Foreign ID = %d\n", ntohl(h->maxForeign));
76 /* printf("Max Sub/Super ID = %d\n", ntohl(h->maxInst)); */
77 printf("Orphaned groups = %d\n", ntohl(h->orphan));
78 printf("User Count = %d\n", ntohl(h->usercount));
79 printf("Group Count = %d\n", ntohl(h->groupcount));
80 /* printf("Foreign Count = %d\n", ntohl(h->foreigncount)); NYI */
81 /* printf("Sub/super Count = %d\n", ntohl(h->instcount)); NYI */
82 printf("Name Hash = %d buckets\n", HASHSIZE);
83 printf("ID Hash = %d buckets\n", HASHSIZE);
88 pr_Read(afs_int32 pos, char *buff, afs_int32 len)
92 code = lseek(fd, UBIK_HEADERSIZE + pos, 0);
96 code = read(fd, buff, len);
106 * Initializes the a transaction on the database and reads the header into
107 * the static variable cheader. If successful it returns a read-locked
108 * transaction. If ubik reports that cached database info should be up to date
109 * the cheader structure is not re-read from the ubik.
117 code = pr_Read(0, (char *)&cheader, sizeof(cheader));
119 afs_com_err(whoami, code, "couldn't read header");
122 /* Check and see if database exists and is approximately OK. */
123 if (ntohl(cheader.headerSize) != sizeof(cheader)
124 || ntohl(cheader.eofPtr) == 0) {
127 afs_com_err(whoami, PRDBBAD, "header is bad");
136 /* returns hash bucket for x */
137 return ((abs(x)) % HASHSIZE);
141 NameHash(register unsigned char *aname)
143 /* returns hash bucket for aname */
144 register unsigned int hash = 0;
146 /* stolen directly from the HashString function in the vol package */
147 for (i = strlen(aname), aname += i - 1; i--; aname--)
148 hash = (hash * 31) + (*aname - 31);
149 return (hash % HASHSIZE);
152 #define MAP_NAMEHASH 1
154 #define MAP_HASHES (MAP_NAMEHASH | MAP_IDHASH)
157 #define MAP_OWNED 0x10
158 #define MAP_RECREATE 0x20
161 int nEntries; /* number of database entries */
162 int anon; /* found anonymous Id */
163 afs_int32 maxId; /* user */
164 afs_int32 minId; /* group */
165 afs_int32 maxForId; /* foreign user id */
166 #if defined(SUPERGROUPS)
170 afs_int32 idcount[IDCOUNT];
171 struct idused *idnext;
174 int idRange; /* number of ids in map */
175 afs_int32 *idmap; /* map of all id's: midId is origin */
176 #endif /* SUPERGROUPS */
177 int nusers; /* counts of each type */
182 int maxOwnerLength; /* longest owner chain */
183 int maxContLength; /* longest chain of cont. blks */
184 int orphanLength; /* length of orphan list */
185 int freeLength; /* length of free list */
190 FILE *recreate; /* stream for recreate instructions */
193 #if defined(SUPERGROUPS)
194 void zeromap(struct idused *idmap);
195 void inccount(struct idused **idmapp, int id);
196 int idcount(struct idused **idmapp, int id);
200 readUbikHeader(struct misc_data *misc)
203 struct ubik_hdr uheader;
205 offset = lseek(fd, 0, 0);
207 printf("error: lseek to 0 failed: %d %d\n", offset, errno);
211 /* now read the info */
212 r = read(fd, &uheader, sizeof(uheader));
213 if (r != sizeof(uheader)) {
214 printf("error: read of %d bytes failed: %d %d\n", sizeof(uheader), r,
219 uheader.magic = ntohl(uheader.magic);
220 uheader.size = ntohl(uheader.size);
221 uheader.version.epoch = ntohl(uheader.version.epoch);
222 uheader.version.counter = ntohl(uheader.version.counter);
224 if (misc->listuheader) {
225 printf("Ubik Header\n");
226 printf(" Magic = 0x%x\n", uheader.magic);
227 printf(" Size = %u\n", uheader.size);
228 printf(" Version.epoch = %u\n", uheader.version.epoch);
229 printf(" Version.counter = %u\n", uheader.version.counter);
232 if (uheader.size != UBIK_HEADERSIZE)
233 printf("Ubik header size is %u (should be %u)\n", uheader.size,
235 if (uheader.magic != UBIK_MAGIC)
236 printf("Ubik header magic is 0x%x (should be 0x%x)\n", uheader.magic,
243 ConvertDiskAddress(afs_uint32 ea, int *eiP)
249 if (ea < sizeof(cheader))
251 if (ea >= ntohl(cheader.eofPtr))
253 ea -= sizeof(cheader);
254 i = ea / sizeof(struct prentry);
255 if (i * sizeof(struct prentry) != ea)
257 /* if ((i < 0) || (i >= misc->nEntries)) return PRDBADDR; */
263 PrintEntryError(struct misc_data *misc, afs_int32 ea, struct prentry *e, int indent)
266 pr_PrintEntry(stderr, /*net order */ 0, ea, e, indent);
271 WalkHashTable(afs_int32 hashtable[], /* hash table to walk */
272 int hashType, /* hash function to use */
273 char map[], /* one byte per db entry */
274 struct misc_data *misc) /* stuff to keep track of */
277 int hi; /* index in hash table */
278 afs_int32 ea; /* entry's db addr */
279 int ei; /* entry's index */
280 char bit; /* bits to check for in map */
289 for (hi = 0; hi < HASHSIZE; hi++) {
291 next_ea = ntohl(hashtable[hi]);
293 code = ConvertDiskAddress(next_ea, &ei);
295 fprintf(stderr, "Bad chain address %d\n", next_ea);
297 fprintf(stderr, "Last entry in chain:\n");
298 if (PrintEntryError(misc, ea, &e, 2))
301 fprintf(stderr, "Skipping remainder of hash bucket %d\n", hi);
305 code = pr_Read(ea, (char *)&e, sizeof(e));
311 if (((e.flags & htonl((PRGRP | PRINST))) == 0)
312 && (strchr(e.name, '@'))) {
314 if (id > misc->maxForId)
317 if (id == ANONYMOUSID)
319 else if (id > misc->maxId)
321 if (id < misc->minId)
327 next_ea = ntohl(e.nextName);
328 hash = NameHash(e.name);
331 next_ea = ntohl(e.nextID);
335 fprintf(stderr, "unknown hash table type %d\n", hashType);
341 "Entry found twice in hash table: bucket %d\n", hi);
343 fprintf(stderr, "also in wrong bucket: should be in %d\n",
345 if (PrintEntryError(misc, ea, &e, 2))
351 flags = ntohl(e.flags);
352 switch (flags & PRTYPE) {
354 fprintf(stderr, "ENTRY IS FREE");
357 fprintf(stderr, "ENTRY IS CONTINUATION");
365 fprintf(stderr, "ENTRY IS unexpected type (flags=0x%x)\n",
369 fprintf(stderr, "ENTRY IS OF unknown type (flags=0x%x)\n",
375 fprintf(stderr, "entry hashed in bucket %d should be %d\n",
378 if (PrintEntryError(misc, ea, &e, 2))
388 WalkNextChain(char map[], /* one byte per db entry */
389 struct misc_data *misc, /* stuff to keep track of */
390 afs_int32 ea, struct prentry *e)
395 struct prentry c; /* continuation entry */
396 afs_int32 na; /* next thread */
399 int count; /* number of members */
402 int length; /* length of chain */
403 #if defined(SUPERGROUPS)
404 int sgcount; /* number of sgentrys */
406 #define g (((struct prentryg *)e))
410 head = ntohl(e->next);
413 count = 0; /* set to >9999 if list ends early */
414 #if defined(SUPERGROUPS)
416 sghead = ntohl(g->next);
418 for (i = 0; i < PRSIZE; i++) {
419 afs_int32 id = ntohl(e->entries[i]);
425 /* in case the ids are large, convert to pure sign. */
434 #if defined(SUPERGROUPS)
435 if (id_s > 0 && eid_s > 0) {
437 "User can't be member of user in membership list\n");
438 if (PrintEntryError(misc, ea, e, 2))
443 if (id_s * eid_s > 0) { /* sign should be different */
445 "Bad user/group dicotomy in membership list\n");
446 if (PrintEntryError(misc, ea, e, 2))
450 #endif /* SUPERGROUPS */
451 /* count each user as a group, and each group a user is in */
452 #if defined(SUPERGROUPS)
453 if (!(id < 0 && eid < 0) && (id != ANONYMOUSID))
454 inccount(&misc->idmap, id);
456 if ((id >= misc->minId) && (id <= misc->maxId)
457 && (id != ANONYMOUSID))
458 misc->idmap[id - misc->minId]++;
459 #endif /* SUPERGROUPS */
465 #if defined(SUPERGROUPS)
466 sghead = ntohl(g->nextsg);
467 if ((e->flags & htonl(PRGRP))) {
468 for (i = 0; i < SGSIZE; ++i) {
469 afs_int32 id = ntohl(g->supergroup[i]);
475 "User can't be member of supergroup list\n");
476 if (PrintEntryError(misc, ea, e, 2))
481 inccount(&misc->idmap, id);
485 #endif /* SUPERGROUPS */
487 head = ntohl(cheader.freePtr);
488 #if defined(SUPERGROUPS)
494 #if defined(SUPERGROUPS)
496 for (na = sghead; na; na = ntohl(c.next)) {
497 code = ConvertDiskAddress(na, &ni);
499 fprintf(stderr, "Bad SGcontinuation ptr %d", na);
500 if (PrintEntryError(misc, ea, e, 2))
503 fprintf(stderr, "last block: \n");
504 if (PrintEntryError(misc, na, &c, 4))
509 code = pr_Read(na, (char *)&c, sizeof(c));
515 fprintf(stderr, "Continuation entry reused\n");
516 if (PrintEntryError(misc, ea, e, 2))
518 if (PrintEntryError(misc, na, &c, 4))
524 if ((ntohl(c.id) != eid)) {
525 fprintf(stderr, "Continuation id mismatch\n");
526 if (PrintEntryError(misc, ea, e, 2))
528 if (PrintEntryError(misc, na, &c, 4))
534 /* update membership count */
535 for (i = 0; i < COSIZE; i++) {
536 afs_int32 id = ntohl(c.entries[i]);
542 /* in case the ids are large, convert to pure sign. */
553 "User can't be member of supergroup list\n");
554 if (PrintEntryError(misc, ea, e, 2))
556 if (PrintEntryError(misc, na, &c, 4))
560 /* count each user as a group, and each group a user is in */
561 if ((id != ANONYMOUSID))
562 inccount(&misc->idmap, id);
569 if (length > misc->maxContLength)
570 misc->maxContLength = length;
571 #endif /* SUPERGROUPS */
573 for (na = head; na; na = ntohl(c.next)) {
574 code = ConvertDiskAddress(na, &ni);
576 fprintf(stderr, "Bad continuation ptr %d", na);
578 fprintf(stderr, "walking free list");
579 else if (PrintEntryError(misc, ea, e, 2))
582 fprintf(stderr, "last block: \n");
583 if (PrintEntryError(misc, na, &c, 4))
588 code = pr_Read(na, (char *)&c, sizeof(c));
594 fprintf(stderr, "Continuation entry reused\n");
596 fprintf(stderr, "walking free list");
597 else if (PrintEntryError(misc, ea, e, 2))
599 if (PrintEntryError(misc, na, &c, 4))
605 if (e && (ntohl(c.id) != eid)) {
606 fprintf(stderr, "Continuation id mismatch\n");
608 fprintf(stderr, "walking free list");
609 else if (PrintEntryError(misc, ea, e, 2))
611 if (PrintEntryError(misc, na, &c, 4))
617 /* update membership count */
619 for (i = 0; i < COSIZE; i++) {
620 afs_int32 id = ntohl(c.entries[i]);
626 /* in case the ids are large, convert to pure sign. */
635 #if defined(SUPERGROUPS)
636 if (id_s > 0 && eid_s > 0) {
638 "User can't be member of user in membership list\n");
639 if (PrintEntryError(misc, ea, e, 2))
641 if (PrintEntryError(misc, na, &c, 4))
646 if (id_s * eid_s > 0) { /* sign should be different */
648 "Bad user/group dicotomy in membership list\n");
649 if (PrintEntryError(misc, ea, e, 2))
651 if (PrintEntryError(misc, na, &c, 4))
655 #endif /* SUPERGROUPS */
656 /* count each user as a group, and each group a user is in */
657 #if defined(SUPERGROUPS)
658 if (!(id < 0 && eid < 0) && (id != ANONYMOUSID))
659 inccount(&misc->idmap, id);
661 if ((id >= misc->minId) && (id <= misc->maxId)
662 && (id != ANONYMOUSID))
663 misc->idmap[id - misc->minId]++;
664 #endif /* SUPERGROUPS */
671 if (e && noErrors && (count != ntohl(e->count))) {
672 #if defined(SUPERGROUPS)
674 fprintf(stderr, "Membership list ends early\n");
677 fprintf(stderr, "Membership list ends early\n");
678 #endif /* SUPERGROUPS */
679 fprintf(stderr, "Count was %d should be %d\n", count,
681 if (PrintEntryError(misc, ea, e, 2))
683 #if defined(SUPERGROUPS)
686 if (e && (e->flags & htonl(PRGRP)) && (sgcount != ntohl(g->countsg))) {
687 fprintf(stderr, "SGCount was %d should be %d\n", sgcount,
689 if (PrintEntryError(misc, ea, e, 2))
695 if (length > misc->maxContLength)
696 misc->maxContLength = length;
698 misc->freeLength = length;
701 #if defined(SUPERGROUPS)
707 WalkOwnedChain(char map[], /* one byte per db entry */
708 struct misc_data *misc, /* stuff to keep track of */
709 afs_int32 ea, struct prentry *e)
713 struct prentry c; /* continuation entry */
714 afs_int32 na; /* next thread */
717 int length; /* length of chain */
720 head = ntohl(e->owned);
723 head = ntohl(cheader.orphan);
726 for (na = head; na; na = ntohl(c.nextOwned)) {
727 code = ConvertDiskAddress(na, &ni);
729 fprintf(stderr, "Bad owned list ptr %d", na);
731 fprintf(stderr, "walking orphan list");
732 else if (PrintEntryError(misc, ea, e, 2))
735 fprintf(stderr, "last block: \n");
736 if (PrintEntryError(misc, na, &c, 4))
741 code = pr_Read(na, (char *)&c, sizeof(c));
746 if (map[ni] & MAP_OWNED) {
747 fprintf(stderr, "Entry on multiple owner chains\n");
749 fprintf(stderr, "walking orphan list");
750 else if (PrintEntryError(misc, ea, e, 2))
752 if (PrintEntryError(misc, na, &c, 4))
756 map[ni] |= MAP_OWNED;
757 if ((map[ni] & MAP_HASHES) != MAP_HASHES) {
758 fprintf(stderr, "Owned entry not hashed properly\n");
761 fprintf(stderr, "walking orphan list");
762 else if (PrintEntryError(misc, ea, e, 2))
764 if (PrintEntryError(misc, na, &c, 4))
769 if (ntohl(c.owner) != eid) {
770 fprintf(stderr, "Owner id mismatch\n");
773 } else /* orphan */ if (c.owner) {
774 fprintf(stderr, "Orphan group owner not zero\n");
780 if (length > misc->maxOwnerLength)
781 misc->maxOwnerLength = length;
783 misc->orphanLength = length;
789 WalkChains(char map[], /* one byte per db entry */
790 struct misc_data *misc) /* stuff to keep track of */
794 afs_int32 ea; /* entry's db addr */
799 /* check all entries found in hash table walks */
800 for (ei = 0; ei < misc->nEntries; ei++)
801 if (map[ei] & MAP_HASHES) {
802 ea = ei * sizeof(struct prentry) + sizeof(cheader);
803 code = pr_Read(ea, (char *)&e, sizeof(e));
807 if ((map[ei] & MAP_HASHES) != MAP_HASHES) {
808 fprintf(stderr, "entry not in both hashtables\n");
809 if ((map[ei] & MAP_NAMEHASH) != MAP_NAMEHASH)
810 fprintf(stderr, "--> entry not in Name hashtable\n");
811 if ((map[ei] & MAP_IDHASH) != MAP_IDHASH)
812 fprintf(stderr, "--> entry not in ID hashtable\n");
815 if (PrintEntryError(misc, ea, &e, 2))
822 type = ntohl(e.flags) & PRTYPE;
826 fprintf(stderr, "Group id not negative\n");
829 /* special case sysadmin: it owns itself */
830 if (id == SYSADMINID) {
831 if (ntohl(e.owner) != SYSADMINID) {
833 "System:administrators doesn't own itself\n");
837 code = WalkOwnedChain(map, misc, ea, &e);
840 code = WalkNextChain(map, misc, ea, &e);
847 #if defined(SUPERGROUPS)
848 fprintf(stderr, "User id not positive\n");
850 fprintf(stderr, "User id negative\n");
855 /* Users are owned by sysadmin, but sysadmin doesn't have an owner
856 * chain. Check this then set the owned bit. */
857 if (ntohl(e.owner) != SYSADMINID) {
859 "User not owned by system:administrators\n");
863 fprintf(stderr, "User has owned pointer\n");
866 map[ei] |= MAP_OWNED;
868 code = WalkOwnedChain(map, misc, ea, &e);
871 code = WalkNextChain(map, misc, ea, &e);
874 if (strchr(e.name, '@') == 0) {
875 misc->nusers++; /* Not a foreign user */
877 misc->nforeigns++; /* A foreign user */
887 "ENTRY IS unexpected type [PRFOREIGN] (flags=0x%x)\n",
894 fprintf(stderr, "entry with unexpected type");
903 GC(char map[], struct misc_data *misc)
911 for (ei = 0; ei < misc->nEntries; ei++) {
912 ea = ei * sizeof(struct prentry) + sizeof(cheader);
913 code = pr_Read(ea, (char *)&e, sizeof(e));
918 fprintf(stderr, "Unreferenced entry:");
919 if (PrintEntryError(misc, ea, &e, 2))
922 /* all users and groups should be owned, and their membership counts
924 else if ((m & MAP_HASHES) == MAP_HASHES) {
927 if (!(m & MAP_OWNED)) {
928 fprintf(stderr, "Entry not on any owner chain:\n");
929 if (PrintEntryError(misc, ea, &e, 2))
933 #if defined(SUPERGROUPS)
934 if ((id != ANONYMOUSID)
935 && ((refCount = idcount(&misc->idmap, id)) != ntohl(e.count)))
937 if ((id >= misc->minId) && (id <= misc->maxId)
938 && (id != ANONYMOUSID)
939 && ((refCount = misc->idmap[id - misc->minId]) !=
941 #endif /* SUPERGROUPS */
945 "Entry membership count is inconsistent: %d entries refer to this one\n",
947 if (PrintEntryError(misc, ea, &e, 2))
950 /* get continuation blocks too */
951 for (na = ntohl(e.next); na; na = ntohl(e.next)) {
953 code = ConvertDiskAddress(na, &ni);
956 code = pr_Read(na, (char *)&e, sizeof(e));
959 if (PrintEntryError(misc, na, &e, 4))
972 if (strpbrk(s, " \t")) {
973 qs = (char *)malloc(strlen(s) + 3);
983 DumpRecreate(char map[], struct misc_data *misc)
993 int builtinUsers = 0;
994 int createLow = 0; /* users uncreate from here */
995 #if defined(SUPERGROUPS)
996 struct idused *idmap; /* map of all id's */
998 afs_int32 *idmap; /* map of all id's */
1003 rc = misc->recreate;
1004 idmap = misc->idmap;
1005 #if defined(SUPERGROUPS)
1008 memset(idmap, 0, misc->idRange * sizeof(misc->idmap[0]));
1012 for (ei = createLow; ei < misc->nEntries; ei++) {
1013 if ((map[ei] & MAP_HASHES) && (map[ei] & MAP_RECREATE) == 0) {
1018 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1019 code = pr_Read(ea, (char *)&e, sizeof(e));
1023 if (misc->listentries)
1024 pr_PrintEntry(stdout, 0 /*not in host order */ , ea, &e,
1028 flags = ntohl(e.flags);
1029 owner = ntohl(e.owner);
1030 name = QuoteName(e.name);
1032 if (!strcmp(e.name, "system:administrators")
1033 || !strcmp(e.name, "system:anyuser")
1034 || !strcmp(e.name, "system:authuser")
1035 || !strcmp(e.name, "system:backup")
1036 || !strcmp(e.name, "anonymous")) {
1041 /* check for duplicate id. This may still lead to duplicate
1043 #if defined(SUPERGROUPS)
1044 if (idcount(&idmap, id))
1046 if (idmap[id - misc->minId])
1049 fprintf(stderr, "Skipping entry with duplicate id %di\n",
1054 /* If owner doesn't exist skip for now, unless we're our own
1055 * owner. If so, a special case allows a group to own itself
1056 * if caller is sysadmin. This leaves only owner cycles to
1059 if ((owner < misc->minId) || (owner > misc->maxId)) {
1060 if (owner == ANONYMOUSID)
1062 "Warning: id %di is owned by ANONYMOUS; using sysadmin instead\n",
1066 "Bogus owner (%d) of id %di; using sysadmin instead\n",
1071 fprintf(stderr, "Warning: group %s is self owning\n",
1073 } else if (owner == 0) {
1075 "Warning: orphan group %s will become self owning.\n",
1079 #if defined(SUPERGROUPS)
1080 else if (!idcount(&idmap, owner))
1083 else if (idmap[owner - misc->minId] == 0)
1088 fprintf(rc, "cr %s %d %d\n", name, id, owner);
1090 gq = uq = access = mask = 0;
1091 if (flags & PRACCESS) {
1092 access = (flags >> PRIVATE_SHIFT);
1093 mask |= PR_SF_ALLBITS;
1095 if (flags & PRQUOTA) {
1096 gq = ntohl(e.ngroups);
1097 uq = ntohl(e.nusers);
1098 mask |= PR_SF_NGROUPS | PR_SF_NUSERS;
1101 fprintf(rc, "sf %d %x %x %d %d\n", id, mask, access, gq,
1105 map[ei] |= MAP_RECREATE;
1106 #if defined(SUPERGROUPS)
1107 if (id != ANONYMOUSID)
1108 inccount(&idmap, id);
1110 if (id != ANONYMOUSID)
1111 idmap[id - misc->minId]++;
1115 /* bump low water mark if possible */
1116 if (ei == createLow)
1123 /* Now create the entries with circular owner dependencies and make them
1124 * own themselves. This is the only way to create them with the correct
1126 for (ei = 0; ei < misc->nEntries; ei++)
1127 if (((map[ei] & MAP_HASHES) == MAP_HASHES)
1128 && (map[ei] & MAP_RECREATE) == 0) {
1129 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1130 code = pr_Read(ea, (char *)&e, sizeof(e));
1135 name = QuoteName(e.name);
1136 fprintf(stderr, "Warning: group %s in self owning cycle\n", name);
1138 fprintf(rc, "cr %s %d %d\n", name, id, id);
1139 #if defined(SUPERGROUPS)
1140 inccount(&idmap, id);
1142 idmap[id - misc->minId]++;
1145 for (ei = 0; ei < misc->nEntries; ei++)
1146 if (((map[ei] & MAP_HASHES) == MAP_HASHES)
1147 && (map[ei] & MAP_RECREATE) == 0) {
1148 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1149 code = pr_Read(ea, (char *)&e, sizeof(e));
1153 owner = ntohl(e.owner);
1154 #if defined(SUPERGROUPS)
1155 if (!idcount(&idmap, owner))
1157 if (idmap[owner - misc->minId] == 0)
1161 "Skipping chown of '%s' to non-existant owner %di\n",
1164 fprintf(rc, "ce %d \"\" %d 0\n", ntohl(e.id), e.owner);
1170 /* Reconstruct membership information based on the groups' user lists. */
1171 for (ei = 0; ei < misc->nEntries; ei++) {
1172 if ((map[ei] & MAP_HASHES) == MAP_HASHES) {
1173 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1174 code = pr_Read(ea, (char *)&e, sizeof(e));
1179 flags = ntohl(e.flags);
1181 if ((id < 0) && (flags & PRGRP)) {
1184 #if defined(SUPERGROUPS)
1188 for (i = 0; i < PRSIZE; i++) {
1189 afs_int32 uid = ntohl(e.entries[i]);
1194 #if !defined(SUPERGROUPS)
1197 fprintf(rc, "au %d %d\n", uid, id);
1199 #if !defined(SUPERGROUPS)
1201 fprintf(stderr, "Skipping %di in group %di\n", uid,
1205 #if defined(SUPERGROUPS)
1206 #define g (*((struct prentryg *)&e))
1207 ng = ntohl(g.nextsg);
1208 for (i = 0; i < SGSIZE; i++) {
1209 afs_int32 uid = ntohl(g.supergroup[i]);
1214 fprintf(rc, "au %d %d\n", uid, id);
1219 code = pr_Read(ng, (char *)&c, sizeof(c));
1223 if ((id == ntohl(c.id)) && (c.flags & htonl(PRCONT))) {
1224 for (i = 0; i < COSIZE; i++) {
1225 afs_int32 uid = ntohl(c.entries[i]);
1230 fprintf(rc, "au %d %d\n", uid, id);
1234 fprintf(stderr, "Skipping continuation block at %d\n",
1241 #endif /* SUPERGROUPS */
1245 code = pr_Read(na, (char *)&c, sizeof(c));
1249 if ((id == ntohl(c.id)) && (c.flags & htonl(PRCONT))) {
1250 for (i = 0; i < COSIZE; i++) {
1251 afs_int32 uid = ntohl(c.entries[i]);
1256 #if !defined(SUPERGROUPS)
1259 fprintf(rc, "au %d %d\n", uid, id);
1261 #if !defined(SUPERGROUPS)
1263 fprintf(stderr, "Skipping %di in group %di\n",
1268 fprintf(stderr, "Skipping continuation block at %d\n",
1274 if (count != ntohl(e.count))
1276 "Group membership count problem found %d should be %d\n",
1277 count, ntohl(e.count));
1278 } else if ((id < 0) || (flags & PRGRP)) {
1279 fprintf(stderr, "Skipping group %di\n", id);
1287 CheckPrDatabase(struct misc_data *misc) /* info & statistics */
1292 char *map; /* map of each entry in db */
1294 eof = ntohl(cheader.eofPtr);
1295 eof -= sizeof(cheader);
1296 n = eof / sizeof(struct prentry);
1297 if ((eof < 0) || (n * sizeof(struct prentry) != eof)) {
1299 afs_com_err(whoami, code, "eof ptr no good: eof=%d, sizeof(prentry)=%d",
1300 eof, sizeof(struct prentry));
1305 printf("Database has %d entries\n", n);
1306 map = (char *)malloc(n);
1310 if (misc->verbose) {
1311 printf("\nChecking name hash table\n");
1314 code = WalkHashTable(cheader.nameHash, MAP_NAMEHASH, map, misc);
1316 afs_com_err(whoami, code, "walking name hash");
1319 if (misc->verbose) {
1320 printf("\nChecking id hash table\n");
1323 code = WalkHashTable(cheader.idHash, MAP_IDHASH, map, misc);
1325 afs_com_err(whoami, code, "walking id hash");
1329 /* hash walk calculates min and max id */
1330 #if defined(SUPERGROUPS)
1333 n = ((misc->maxId > misc->maxForId) ? misc->maxId : misc->maxForId);
1334 misc->idRange = n - misc->minId + 1;
1335 misc->idmap = (afs_int32 *) malloc(misc->idRange * sizeof(afs_int32));
1337 afs_com_err(whoami, 0, "Unable to malloc space for max ids of %d",
1342 memset(misc->idmap, 0, misc->idRange * sizeof(misc->idmap[0]));
1343 #endif /* SUPERGROUPS */
1345 if (misc->verbose) {
1346 printf("\nChecking entry chains\n");
1349 code = WalkChains(map, misc);
1351 afs_com_err(whoami, code, "walking chains");
1354 if (misc->verbose) {
1355 printf("\nChecking free list\n");
1358 code = WalkNextChain(map, misc, 0, 0);
1360 afs_com_err(whoami, code, "walking free list");
1363 if (misc->verbose) {
1364 printf("\nChecking orphans list\n");
1367 code = WalkOwnedChain(map, misc, 0, 0);
1369 afs_com_err(whoami, code, "walking orphan list");
1373 if (misc->verbose) {
1374 printf("\nChecking for unreferenced entries\n");
1377 code = GC(map, misc);
1379 afs_com_err(whoami, code, "looking for unreferenced entries");
1383 DumpRecreate(map, misc); /* check for owner cycles */
1385 fclose(misc->recreate);
1387 if (misc->anon != 2) /* once for each hash table */
1388 fprintf(stderr, "Problems with ANON=%d\n", misc->anon);
1389 if (misc->ncells || misc->ninsts)
1390 fprintf(stderr, "Unexpected entry type\n");
1391 if (misc->nusers != ntohl(cheader.usercount)) {
1393 "User count inconsistent: should be %d, header claims: %d\n",
1394 misc->nusers, ntohl(cheader.usercount));
1396 if (misc->ngroups != ntohl(cheader.groupcount)) {
1398 "Group count inconsistent: should be %d, header claims: %d\n",
1399 misc->ngroups, ntohl(cheader.groupcount));
1401 if (misc->maxId > ntohl(cheader.maxID))
1403 "Database's max user Id (%d) is smaller than largest user's Id (%d).\n",
1404 ntohl(cheader.maxID), misc->maxId);
1405 if (misc->minId < ntohl(cheader.maxGroup))
1407 "Database's max group Id (%d) is smaller than largest group's Id (%d).\n",
1408 ntohl(cheader.maxGroup), misc->minId);
1410 if (misc->verbose) {
1411 printf("\nMaxId = %d, MinId = %d, MaxForeignId = %d\n", misc->maxId,
1412 misc->minId, misc->maxForId);
1414 ("Free list is %d entries in length, %d groups on orphan list\n",
1415 misc->freeLength, misc->orphanLength);
1417 ("The longest owner list is %d, the longest continuation block chain is %d\n",
1418 misc->maxOwnerLength, misc->maxContLength);
1419 printf("%d users ; %d foreign users ; and %d groups\n", misc->nusers,
1420 misc->nforeigns, misc->ngroups);
1427 #include "AFS_component_version_number.c"
1430 WorkerBee(struct cmd_syndesc *as, char *arock)
1434 struct misc_data misc; /* info & statistics */
1436 initialize_PT_error_table();
1437 initialize_U_error_table();
1439 pr_dbaseName = AFSDIR_SERVER_PRDB_FILEPATH;
1440 memset(&misc, 0, sizeof(misc));
1442 pr_dbaseName = as->parms[0].items->data; /* -database */
1443 misc.listuheader = (as->parms[1].items ? 1 : 0); /* -uheader */
1444 misc.listpheader = (as->parms[2].items ? 1 : 0); /* -pheader */
1445 misc.listentries = (as->parms[3].items ? 1 : 0); /* -entries */
1446 misc.verbose = (as->parms[4].items ? 1 : 0); /* -verbose */
1447 recreateFile = (as->parms[5].items ? as->parms[5].items->data : NULL); /* -rebuild */
1449 fd = open(pr_dbaseName, O_RDONLY, 0);
1451 afs_com_err(whoami, errno, "Open failed on db %s", pr_dbaseName);
1455 /* Read the ubik header */
1456 if (misc.listuheader) {
1457 readUbikHeader(&misc);
1460 code = ReadHeader();
1463 if (misc.listpheader)
1464 printheader(&cheader);
1467 misc.recreate = fopen(recreateFile, "w");
1468 if (misc.recreate == 0) {
1469 afs_com_err(whoami, errno,
1470 "can't create file for recreation instructions: %s",
1475 code = CheckPrDatabase(&misc);
1477 afs_com_err(whoami, code, "Checking prserver database");
1484 main(int argc, char *argv[])
1486 struct cmd_syndesc *ts;
1490 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "PRDB check");
1491 cmd_AddParm(ts, "-database", CMD_SINGLE, CMD_REQUIRED, "ptdb_file");
1492 cmd_AddParm(ts, "-uheader", CMD_FLAG, CMD_OPTIONAL,
1493 "Display UBIK header");
1494 cmd_AddParm(ts, "-pheader", CMD_FLAG, CMD_OPTIONAL,
1495 "Display KADB header");
1496 cmd_AddParm(ts, "-entries", CMD_FLAG, CMD_OPTIONAL, "Display entries");
1497 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
1498 cmd_AddParm(ts, "-rebuild", CMD_SINGLE, CMD_OPTIONAL | CMD_HIDE,
1501 return cmd_Dispatch(argc, argv);
1505 #if defined(SUPERGROUPS)
1507 /* new routines to deal with very large ID numbers */
1510 zeromap(struct idused *idmap)
1513 memset((char *)idmap->idcount, 0, sizeof idmap->idcount);
1514 idmap = idmap->idnext;
1519 inccount(struct idused **idmapp, int id)
1521 struct idused *idmap;
1523 if (IDCOUNT & (IDCOUNT - 1)) {
1524 fprintf(stderr, "IDCOUNT must be power of 2!\n");
1527 while (idmap = *idmapp) {
1528 if (idmap->idstart == (id & ~(IDCOUNT - 1)))
1530 idmapp = &idmap->idnext;
1533 idmap = (struct idused *)malloc(sizeof *idmap);
1538 memset((char *)idmap, 0, sizeof idmap);
1539 idmap->idstart = id & ~(IDCOUNT - 1);
1540 idmap->idnext = *idmapp;
1543 ++idmap->idcount[id & (IDCOUNT - 1)];
1547 idcount(struct idused **idmapp, int id)
1549 struct idused *idmap;
1551 if (IDCOUNT & (IDCOUNT - 1)) {
1552 fprintf(stderr, "IDCOUNT must be power of 2!\n");
1555 while (idmap = *idmapp) {
1556 if (idmap->idstart == (id & ~(IDCOUNT - 1))) {
1557 return idmap->idcount[id & (IDCOUNT - 1)];
1559 idmapp = &idmap->idnext;
1563 #endif /* SUPERGROUPS */