ptserver: Don't ignore ubik_Write failures
[openafs.git] / src / ptserver / db_verify.c
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 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 /*
17  *                      (3) Define a structure, idused, instead of an
18  *                          array of long integers, idmap, to count group
19  *                          memberships. These structures are on a linked
20  *                          list, with each structure containing IDCOUNT
21  *                          slots for id's.
22  *                      (4) Add new functions to processs the structure
23  *                          described above:
24  *                             zeromap(), idcount(), inccount().
25  *                      (5) Add code, primarily in WalkNextChain():
26  *                           1. Test id's, allowing groups within groups.
27  *                           2. Count the membership list for supergroups,
28  *                              and follow the continuation chain for
29  *                              supergroups.
30  *                      (6) Add fprintf statements for various error
31  *                          conditions.
32  */
33
34
35 #ifdef AFS_NT40_ENV
36 #include <WINNT/afsevent.h>
37 #else
38 #include <sys/file.h>
39 #endif
40
41 #include <afs/cellconfig.h>
42 #include <afs/afsutil.h>
43 #include <ubik.h>
44 #include <afs/cmd.h>
45 #include <afs/com_err.h>
46
47 #include "ptint.h"
48 #include "pterror.h"
49 #include "ptserver.h"
50 #include "ptuser.h"
51 #include "display.h"
52
53 struct prheader cheader;
54 int fd;
55 const char *pr_dbaseName;
56 char *whoami = "db_verify";
57 #define UBIK_HEADERSIZE 64
58
59 afs_int32
60 printheader(struct prheader *h)
61 {
62     printf("Version           = %d\n", ntohl(h->version));
63     printf("Header Size       = %d\n", ntohl(h->headerSize));
64     printf("Free Ptr          = 0x%x\n", ntohl(h->freePtr));
65     printf("EOF  Ptr          = 0x%x\n", ntohl(h->eofPtr));
66     printf("Max Group     ID  = %d\n", ntohl(h->maxGroup));
67     printf("Max User      ID  = %d\n", ntohl(h->maxID));
68     printf("Max Foreign   ID  = %d\n", ntohl(h->maxForeign));
69 /* printf("Max Sub/Super ID  = %d\n", ntohl(h->maxInst)); */
70     printf("Orphaned groups   = %d\n", ntohl(h->orphan));
71     printf("User      Count   = %d\n", ntohl(h->usercount));
72     printf("Group     Count   = %d\n", ntohl(h->groupcount));
73 /* printf("Foreign   Count   = %d\n", ntohl(h->foreigncount)); NYI */
74 /* printf("Sub/super Count   = %d\n", ntohl(h->instcount));    NYI */
75     printf("Name Hash         = %d buckets\n", HASHSIZE);
76     printf("ID   Hash         = %d buckets\n", HASHSIZE);
77     return 0;
78 }
79
80 static afs_int32
81 pr_Read(afs_int32 pos, void *buff, afs_int32 len)
82 {
83     afs_int32 code;
84
85     code = lseek(fd, UBIK_HEADERSIZE + pos, 0);
86     if (code == -1)
87         return errno;
88
89     code = read(fd, buff, len);
90     if (code != len)
91         return -1;
92     if (code == -1)
93         return errno;
94
95     return 0;
96 }
97
98 /* InitDB ()
99  *   Initializes the a transaction on the database and reads the header into
100  * the static variable cheader.  If successful it returns a read-locked
101  * transaction.  If ubik reports that cached database info should be up to date
102  * the cheader structure is not re-read from the ubik.
103  */
104
105 afs_int32
106 ReadHeader(void)
107 {
108     afs_int32 code;
109
110     code = pr_Read(0, (char *)&cheader, sizeof(cheader));
111     if (code) {
112         afs_com_err(whoami, code, "couldn't read header");
113         return code;
114     }
115     /* Check and see if database exists and is approximately OK. */
116     if (ntohl(cheader.headerSize) != sizeof(cheader)
117         || ntohl(cheader.eofPtr) == 0) {
118         if (code)
119             return code;
120         afs_com_err(whoami, PRDBBAD, "header is bad");
121         return PRDBBAD;
122     }
123     return 0;
124 }
125
126 static afs_int32
127 IDHash(afs_int32 x)
128 {
129     /* returns hash bucket for x */
130     return ((abs(x)) % HASHSIZE);
131 }
132
133 static afs_int32
134 NameHash(char *aname)
135 {
136     /* returns hash bucket for aname */
137     unsigned int hash = 0;
138     int i;
139 /* stolen directly from the HashString function in the vol package */
140     for (i = strlen(aname), aname += i - 1; i--; aname--)
141         hash = (hash * 31) + (*(unsigned char *)aname - 31);
142     return (hash % HASHSIZE);
143 }
144
145 #define MAP_NAMEHASH 1
146 #define MAP_IDHASH 2
147 #define MAP_HASHES (MAP_NAMEHASH | MAP_IDHASH)
148 #define MAP_CONT 4
149 #define MAP_FREE 8
150 #define MAP_OWNED 0x10
151 #define MAP_RECREATE 0x20
152
153 struct misc_data {
154     int nEntries;               /* number of database entries */
155     int anon;                   /* found anonymous Id */
156     afs_int32 maxId;            /* user */
157     afs_int32 minId;            /* group */
158     afs_int32 maxForId;         /* foreign user id */
159 #if defined(SUPERGROUPS)
160 #define IDCOUNT 512
161     struct idused {
162         int idstart;
163         afs_int32 idcount[IDCOUNT];
164         struct idused *idnext;
165     } *idmap;
166 #else
167     int idRange;                /* number of ids in map */
168     afs_int32 *idmap;           /* map of all id's: midId is origin */
169 #endif                          /* SUPERGROUPS */
170     int nusers;                 /* counts of each type */
171     int ngroups;
172     int nforeigns;
173     int ninsts;
174     int ncells;
175     int maxOwnerLength;         /* longest owner chain */
176     int maxContLength;          /* longest chain of cont. blks */
177     int orphanLength;           /* length of orphan list */
178     int freeLength;             /* length of free list */
179     int verbose;
180     int listuheader;
181     int listpheader;
182     int listentries;
183     FILE *recreate;             /* stream for recreate instructions */
184 };
185
186 #if defined(SUPERGROUPS)
187 void zeromap(struct idused *idmap);
188 void inccount(struct idused **idmapp, int id);
189 int idcount(struct idused **idmapp, int id);
190 #endif
191
192 int
193 readUbikHeader(struct misc_data *misc)
194 {
195     int offset, r;
196     struct ubik_hdr uheader;
197
198     offset = lseek(fd, 0, 0);
199     if (offset != 0) {
200         printf("error: lseek to 0 failed: %d %d\n", offset, errno);
201         return (-1);
202     }
203
204     /* now read the info */
205     r = read(fd, &uheader, sizeof(uheader));
206     if (r != sizeof(uheader)) {
207         printf("error: read of %" AFS_SIZET_FMT " bytes failed: %d %d\n",
208                sizeof(uheader), r, errno);
209         return (-1);
210     }
211
212     uheader.magic = ntohl(uheader.magic);
213     uheader.size = ntohs(uheader.size);
214     uheader.version.epoch = ntohl(uheader.version.epoch);
215     uheader.version.counter = ntohl(uheader.version.counter);
216
217     if (misc->listuheader) {
218         printf("Ubik Header\n");
219         printf("   Magic           = 0x%x\n", uheader.magic);
220         printf("   Size            = %u\n", uheader.size);
221         printf("   Version.epoch   = %u\n", uheader.version.epoch);
222         printf("   Version.counter = %u\n", uheader.version.counter);
223     }
224
225     if (uheader.size != UBIK_HEADERSIZE)
226         printf("Ubik header size is %u (should be %u)\n", uheader.size,
227                UBIK_HEADERSIZE);
228     if (uheader.magic != UBIK_MAGIC)
229         printf("Ubik header magic is 0x%x (should be 0x%x)\n", uheader.magic,
230                UBIK_MAGIC);
231
232     return (0);
233 }
234
235 afs_int32
236 ConvertDiskAddress(afs_uint32 ea, int *eiP)
237 {
238     int i;
239
240     *eiP = -1;
241
242     if (ea < sizeof(cheader))
243         return PRDBADDR;
244     if (ea >= ntohl(cheader.eofPtr))
245         return PRDBADDR;
246     ea -= sizeof(cheader);
247     i = ea / sizeof(struct prentry);
248     if (i * sizeof(struct prentry) != ea)
249         return PRDBADDR;
250 /*    if ((i < 0) || (i >= misc->nEntries)) return PRDBADDR; */
251     *eiP = i;
252     return 0;
253 }
254
255 int
256 PrintEntryError(struct misc_data *misc, afs_int32 ea, struct prentry *e, int indent)
257 {
258
259     pr_PrintEntry(stderr, /*net order */ 0, ea, e, indent);
260     return 0;
261 }
262
263 afs_int32
264 WalkHashTable(afs_int32 hashtable[],    /* hash table to walk */
265               int hashType,             /* hash function to use */
266               char map[],               /* one byte per db entry */
267               struct misc_data *misc)   /* stuff to keep track of */
268 {
269     afs_int32 code;
270     int hi;                     /* index in hash table */
271     afs_int32 ea;               /* entry's db addr */
272     int ei;                     /* entry's index */
273     char bit;                   /* bits to check for in map */
274     struct prentry e;
275     afs_int32 next_ea;
276     afs_int32 id;
277     afs_int32 flags;
278     afs_int32 hash;
279
280     bit = hashType;
281
282     for (hi = 0; hi < HASHSIZE; hi++) {
283         ea = 0;
284         next_ea = ntohl(hashtable[hi]);
285         while (next_ea) {
286             code = ConvertDiskAddress(next_ea, &ei);
287             if (code) {
288                 fprintf(stderr, "Bad chain address %d\n", next_ea);
289                 if (ea) {
290                     fprintf(stderr, "Last entry in chain:\n");
291                     if (PrintEntryError(misc, ea, &e, 2))
292                         return PRDBBAD;
293                 }
294                 fprintf(stderr, "Skipping remainder of hash bucket %d\n", hi);
295                 break;
296             }
297             ea = next_ea;
298             code = pr_Read(ea, (char *)&e, sizeof(e));
299             if (code)
300                 return code;
301
302             id = ntohl(e.id);
303
304             if (((e.flags & htonl((PRGRP | PRINST))) == 0)
305                 && (strchr(e.name, '@'))) {
306                 /* Foreign user */
307                 if (id > misc->maxForId)
308                     misc->maxForId = id;
309             } else {
310                 if (id == ANONYMOUSID)
311                     misc->anon++;
312                 else if (id > misc->maxId)
313                     misc->maxId = id;
314                 if (id < misc->minId)
315                     misc->minId = id;
316             }
317
318             switch (hashType) {
319             case MAP_NAMEHASH:
320                 next_ea = ntohl(e.nextName);
321                 hash = NameHash(e.name);
322                 break;
323             case MAP_IDHASH:
324                 next_ea = ntohl(e.nextID);
325                 hash = IDHash(id);
326                 break;
327             default:
328                 fprintf(stderr, "unknown hash table type %d\n", hashType);
329                 return PRBADARG;
330             }
331
332             if (map[ei] & bit) {
333                 fprintf(stderr,
334                         "Entry found twice in hash table: bucket %d\n", hi);
335                 if (hi != hash)
336                     fprintf(stderr, "also in wrong bucket: should be in %d\n",
337                             hash);
338                 if (PrintEntryError(misc, ea, &e, 2))
339                     return PRDBBAD;
340                 break;
341             }
342             map[ei] |= bit;
343
344             flags = ntohl(e.flags);
345             switch (flags & PRTYPE) {
346             case PRFREE:
347                 fprintf(stderr, "ENTRY IS FREE");
348                 goto abort;
349             case PRCONT:
350                 fprintf(stderr, "ENTRY IS CONTINUATION");
351                 goto abort;
352             case PRGRP:
353             case PRUSER:
354                 break;
355             case PRCELL:
356             case PRFOREIGN:
357             case PRINST:
358                 fprintf(stderr, "ENTRY IS unexpected type (flags=0x%x)\n",
359                         flags);
360                 break;
361             default:
362                 fprintf(stderr, "ENTRY IS OF unknown type (flags=0x%x)\n",
363                         flags);
364                 goto abort;
365             }
366
367             if (hash != hi) {
368                 fprintf(stderr, "entry hashed in bucket %d should be %d\n",
369                         hi, hash);
370               abort:
371                 if (PrintEntryError(misc, ea, &e, 2))
372                     return PRDBBAD;
373                 continue;
374             }
375         }
376     }
377     return 0;
378 }
379
380 afs_int32
381 WalkNextChain(char map[],               /* one byte per db entry */
382               struct misc_data *misc,   /* stuff to keep track of */
383               afs_int32 ea, struct prentry *e)
384 {
385     afs_int32 head;
386     int bit;
387     afs_int32 code;
388     struct prentry c;           /* continuation entry */
389     afs_int32 na;               /* next thread */
390     int ni;
391     afs_int32 eid = 0;
392     int count = 0;              /* number of members, set to > 9999 if */
393                                 /* list ends early */
394     int i;
395     int noErrors = 1;
396     int length;                 /* length of chain */
397 #if defined(SUPERGROUPS)
398     int sgcount = 0;            /* number of sgentrys */
399     afs_int32 sghead;
400 #define g (((struct prentryg *)e))
401 #endif
402
403     if (e) {
404         head = ntohl(e->next);
405         eid = ntohl(e->id);
406         bit = MAP_CONT;
407 #if defined(SUPERGROUPS)
408         sghead = ntohl(g->next);
409 #endif
410         for (i = 0; i < PRSIZE; i++) {
411             afs_int32 id = ntohl(e->entries[i]);
412             if (id == PRBADID)
413                 continue;
414             else if (id) {
415                 int eid_s, id_s;
416                 count++;
417                 /* in case the ids are large, convert to pure sign. */
418                 if (id > 0)
419                     id_s = 1;
420                 else
421                     id_s = -1;
422                 if (eid > 0)
423                     eid_s = 1;
424                 else
425                     eid_s = -1;
426 #if defined(SUPERGROUPS)
427                 if (id_s > 0 && eid_s > 0) {
428                     fprintf(stderr,
429                             "User can't be member of user in membership list\n");
430                     if (PrintEntryError(misc, ea, e, 2))
431                         return PRDBBAD;
432                     noErrors = 0;
433                 }
434 #else
435                 if (id_s * eid_s > 0) { /* sign should be different */
436                     fprintf(stderr,
437                             "Bad user/group dicotomy in membership list\n");
438                     if (PrintEntryError(misc, ea, e, 2))
439                         return PRDBBAD;
440                     noErrors = 0;
441                 }
442 #endif /* SUPERGROUPS */
443                 /* count each user as a group, and each group a user is in */
444 #if defined(SUPERGROUPS)
445                 if (!(id < 0 && eid < 0) && (id != ANONYMOUSID))
446                     inccount(&misc->idmap, id);
447 #else
448                 if ((id >= misc->minId) && (id <= misc->maxId)
449                     && (id != ANONYMOUSID))
450                     misc->idmap[id - misc->minId]++;
451 #endif /* SUPERGROUPS */
452             } else if (head)
453                 count = 9999;
454             else
455                 break;
456         }
457 #if defined(SUPERGROUPS)
458         sghead = ntohl(g->nextsg);
459         if ((e->flags & htonl(PRGRP))) {
460             for (i = 0; i < SGSIZE; ++i) {
461                 afs_int32 id = ntohl(g->supergroup[i]);
462                 if (id == PRBADID)
463                     continue;
464                 else if (id) {
465                     if (id > 0) {
466                         fprintf(stderr,
467                                 "User can't be member of supergroup list\n");
468                         if (PrintEntryError(misc, ea, e, 2))
469                             return PRDBBAD;
470                         noErrors = 0;
471                     }
472                     sgcount++;
473                     inccount(&misc->idmap, id);
474                 }
475             }
476         }
477 #endif /* SUPERGROUPS */
478     } else {
479         head = ntohl(cheader.freePtr);
480 #if defined(SUPERGROUPS)
481         sghead = 0;
482 #endif
483         bit = MAP_FREE;
484     }
485
486 #if defined(SUPERGROUPS)
487     length = 0;
488     for (na = sghead; na; na = ntohl(c.next)) {
489         code = ConvertDiskAddress(na, &ni);
490         if (code) {
491             fprintf(stderr, "Bad SGcontinuation ptr %d", na);
492             if (PrintEntryError(misc, ea, e, 2))
493                 return PRDBBAD;
494             if (na != sghead) {
495                 fprintf(stderr, "last block: \n");
496                 if (PrintEntryError(misc, na, &c, 4))
497                     return PRDBBAD;
498             }
499             return 0;
500         }
501         code = pr_Read(na, (char *)&c, sizeof(c));
502         if (code)
503             return code;
504         length++;
505
506         if (map[ni]) {
507             fprintf(stderr, "Continuation entry reused\n");
508             if (PrintEntryError(misc, ea, e, 2))
509                 return PRDBBAD;
510             if (PrintEntryError(misc, na, &c, 4))
511                 return PRDBBAD;
512             noErrors = 0;
513             break;
514         }
515         map[ni] |= bit;
516         if ((ntohl(c.id) != eid)) {
517             fprintf(stderr, "Continuation id mismatch\n");
518             if (PrintEntryError(misc, ea, e, 2))
519                 return PRDBBAD;
520             if (PrintEntryError(misc, na, &c, 4))
521                 return PRDBBAD;
522             noErrors = 0;
523             continue;
524         }
525
526         /* update membership count */
527         for (i = 0; i < COSIZE; i++) {
528             afs_int32 id = ntohl(c.entries[i]);
529             if (id == PRBADID)
530                 continue;
531             else if (id) {
532                 int id_s;
533                 sgcount++;
534                 /* in case the ids are large, convert to pure sign. */
535                 if (id > 0)
536                     id_s = 1;
537                 else
538                     id_s = -1;
539                 if (id_s > 0) {
540                     fprintf(stderr,
541                             "User can't be member of supergroup list\n");
542                     if (PrintEntryError(misc, ea, e, 2))
543                         return PRDBBAD;
544                     if (PrintEntryError(misc, na, &c, 4))
545                         return PRDBBAD;
546                     noErrors = 0;
547                 }
548                 /* count each user as a group, and each group a user is in */
549                 if ((id != ANONYMOUSID))
550                     inccount(&misc->idmap, id);
551             } else if (c.next)
552                 count = 9999;
553             else
554                 break;
555         }
556     }
557     if (length > misc->maxContLength)
558         misc->maxContLength = length;
559 #endif /* SUPERGROUPS */
560     length = 0;
561     for (na = head; na; na = ntohl(c.next)) {
562         code = ConvertDiskAddress(na, &ni);
563         if (code) {
564             fprintf(stderr, "Bad continuation ptr %d", na);
565             if (e == 0)
566                 fprintf(stderr, "walking free list");
567             else if (PrintEntryError(misc, ea, e, 2))
568                 return PRDBBAD;
569             if (na != head) {
570                 fprintf(stderr, "last block: \n");
571                 if (PrintEntryError(misc, na, &c, 4))
572                     return PRDBBAD;
573             }
574             return 0;
575         }
576         code = pr_Read(na, (char *)&c, sizeof(c));
577         if (code)
578             return code;
579         length++;
580
581         if (map[ni]) {
582             fprintf(stderr, "Continuation entry reused\n");
583             if (e == 0)
584                 fprintf(stderr, "walking free list");
585             else if (PrintEntryError(misc, ea, e, 2))
586                 return PRDBBAD;
587             if (PrintEntryError(misc, na, &c, 4))
588                 return PRDBBAD;
589             noErrors = 0;
590             break;
591         }
592         map[ni] |= bit;
593         if (e && (ntohl(c.id) != eid)) {
594             fprintf(stderr, "Continuation id mismatch\n");
595             if (e == 0)
596                 fprintf(stderr, "walking free list");
597             else if (PrintEntryError(misc, ea, e, 2))
598                 return PRDBBAD;
599             if (PrintEntryError(misc, na, &c, 4))
600                 return PRDBBAD;
601             noErrors = 0;
602             continue;
603         }
604
605         /* update membership count */
606         if (e)
607             for (i = 0; i < COSIZE; i++) {
608                 afs_int32 id = ntohl(c.entries[i]);
609                 if (id == PRBADID)
610                     continue;
611                 else if (id) {
612                     int eid_s, id_s;
613                     count++;
614                     /* in case the ids are large, convert to pure sign. */
615                     if (id > 0)
616                         id_s = 1;
617                     else
618                         id_s = -1;
619                     if (eid > 0)
620                         eid_s = 1;
621                     else
622                         eid_s = -1;
623 #if defined(SUPERGROUPS)
624                     if (id_s > 0 && eid_s > 0) {
625                         fprintf(stderr,
626                                 "User can't be member of user in membership list\n");
627                         if (PrintEntryError(misc, ea, e, 2))
628                             return PRDBBAD;
629                         if (PrintEntryError(misc, na, &c, 4))
630                             return PRDBBAD;
631                         noErrors = 0;
632                     }
633 #else
634                     if (id_s * eid_s > 0) {     /* sign should be different */
635                         fprintf(stderr,
636                                 "Bad user/group dicotomy in membership list\n");
637                         if (PrintEntryError(misc, ea, e, 2))
638                             return PRDBBAD;
639                         if (PrintEntryError(misc, na, &c, 4))
640                             return PRDBBAD;
641                         noErrors = 0;
642                     }
643 #endif /* SUPERGROUPS */
644                     /* count each user as a group, and each group a user is in */
645 #if defined(SUPERGROUPS)
646                     if (!(id < 0 && eid < 0) && (id != ANONYMOUSID))
647                         inccount(&misc->idmap, id);
648 #else
649                     if ((id >= misc->minId) && (id <= misc->maxId)
650                         && (id != ANONYMOUSID))
651                         misc->idmap[id - misc->minId]++;
652 #endif /* SUPERGROUPS */
653                 } else if (c.next)
654                     count = 9999;
655                 else
656                     break;
657             }
658     }
659     if (e && noErrors && (count != ntohl(e->count))) {
660 #if defined(SUPERGROUPS)
661         if (count >= 9999)
662             fprintf(stderr, "Membership list ends early\n");
663 #else
664         if (count > 9999)
665             fprintf(stderr, "Membership list ends early\n");
666 #endif /* SUPERGROUPS */
667         fprintf(stderr, "Count was %d should be %d\n", count,
668                 ntohl(e->count));
669         if (PrintEntryError(misc, ea, e, 2))
670             return PRDBBAD;
671 #if defined(SUPERGROUPS)
672         noErrors = 0;
673     }
674     if (e && (e->flags & htonl(PRGRP)) && (sgcount != ntohl(g->countsg))) {
675         fprintf(stderr, "SGCount was %d should be %d\n", sgcount,
676                 ntohl(g->countsg));
677         if (PrintEntryError(misc, ea, e, 2))
678             return PRDBBAD;
679 #endif
680     }
681
682     if (e) {
683         if (length > misc->maxContLength)
684             misc->maxContLength = length;
685     } else
686         misc->freeLength = length;
687
688     return 0;
689 #if defined(SUPERGROUPS)
690 #undef g
691 #endif
692 }
693
694 afs_int32
695 WalkOwnedChain(char map[],              /* one byte per db entry */
696                struct misc_data *misc,  /* stuff to keep track of */
697                afs_int32 ea, struct prentry *e)
698 {
699     afs_int32 head;
700     afs_int32 code;
701     struct prentry c;           /* continuation entry */
702     afs_int32 na;               /* next thread */
703     int ni;
704     afs_int32 eid = 0;
705     int length;                 /* length of chain */
706
707     if (e) {
708         head = ntohl(e->owned);
709         eid = ntohl(e->id);
710     } else
711         head = ntohl(cheader.orphan);
712
713     length = 0;
714     for (na = head; na; na = ntohl(c.nextOwned)) {
715         code = ConvertDiskAddress(na, &ni);
716         if (code) {
717             fprintf(stderr, "Bad owned list ptr %d", na);
718             if (e == 0)
719                 fprintf(stderr, "walking orphan list");
720             else if (PrintEntryError(misc, ea, e, 2))
721                 return PRDBBAD;
722             if (na != head) {
723                 fprintf(stderr, "last block: \n");
724                 if (PrintEntryError(misc, na, &c, 4))
725                     return PRDBBAD;
726             }
727             return 0;
728         }
729         code = pr_Read(na, (char *)&c, sizeof(c));
730         if (code)
731             return code;
732         length++;
733
734         if (map[ni] & MAP_OWNED) {
735             fprintf(stderr, "Entry on multiple owner chains\n");
736             if (e == 0)
737                 fprintf(stderr, "walking orphan list");
738             else if (PrintEntryError(misc, ea, e, 2))
739                 return PRDBBAD;
740             if (PrintEntryError(misc, na, &c, 4))
741                 return PRDBBAD;
742             break;
743         }
744         map[ni] |= MAP_OWNED;
745         if ((map[ni] & MAP_HASHES) != MAP_HASHES) {
746             fprintf(stderr, "Owned entry not hashed properly\n");
747           abort:
748             if (e == 0)
749                 fprintf(stderr, "walking orphan list");
750             else if (PrintEntryError(misc, ea, e, 2))
751                 return PRDBBAD;
752             if (PrintEntryError(misc, na, &c, 4))
753                 return PRDBBAD;
754             continue;
755         }
756         if (e) {
757             if (ntohl(c.owner) != eid) {
758                 fprintf(stderr, "Owner id mismatch\n");
759                 goto abort;
760             }
761         } else /* orphan */ if (c.owner) {
762             fprintf(stderr, "Orphan group owner not zero\n");
763             goto abort;
764         }
765     }
766
767     if (e) {
768         if (length > misc->maxOwnerLength)
769             misc->maxOwnerLength = length;
770     } else
771         misc->orphanLength = length;
772
773     return 0;
774 }
775
776 afs_int32
777 WalkChains(char map[],          /* one byte per db entry */
778            struct misc_data *misc)      /* stuff to keep track of */
779 {
780     afs_int32 code;
781     int ei;
782     afs_int32 ea;               /* entry's db addr */
783     struct prentry e;
784     afs_int32 id;
785     int type;
786
787     /* check all entries found in hash table walks */
788     for (ei = 0; ei < misc->nEntries; ei++)
789         if (map[ei] & MAP_HASHES) {
790             ea = ei * sizeof(struct prentry) + sizeof(cheader);
791             code = pr_Read(ea, (char *)&e, sizeof(e));
792             if (code)
793                 return code;
794
795             if ((map[ei] & MAP_HASHES) != MAP_HASHES) {
796                 fprintf(stderr, "entry not in both hashtables\n");
797                 if ((map[ei] & MAP_NAMEHASH) != MAP_NAMEHASH)
798                     fprintf(stderr, "--> entry not in Name hashtable\n");
799                 if ((map[ei] & MAP_IDHASH) != MAP_IDHASH)
800                     fprintf(stderr, "--> entry not in ID hashtable\n");
801
802               abort:
803                 if (PrintEntryError(misc, ea, &e, 2))
804                     return PRDBBAD;
805                 continue;
806             }
807
808             id = ntohl(e.id);
809
810             type = ntohl(e.flags) & PRTYPE;
811             switch (type) {
812             case PRGRP:
813                 if (id >= 0) {
814                     fprintf(stderr, "Group id not negative\n");
815                     goto abort;
816                 }
817                 /* special case sysadmin: it owns itself */
818                 if (id == SYSADMINID) {
819                     if (ntohl(e.owner) != SYSADMINID) {
820                         fprintf(stderr,
821                                 "System:administrators doesn't own itself\n");
822                         goto abort;
823                     }
824                 }
825                 code = WalkOwnedChain(map, misc, ea, &e);
826                 if (code)
827                     return code;
828                 code = WalkNextChain(map, misc, ea, &e);
829                 if (code)
830                     return code;
831                 misc->ngroups++;
832                 break;
833             case PRUSER:
834                 if (id <= 0) {
835 #if defined(SUPERGROUPS)
836                     fprintf(stderr, "User id not positive\n");
837 #else
838                     fprintf(stderr, "User id negative\n");
839 #endif
840                     goto abort;
841                 }
842
843                 /* Users are owned by sysadmin, but sysadmin doesn't have an owner
844                  * chain.  Check this then set the owned bit. */
845                 if (ntohl(e.owner) != SYSADMINID) {
846                     fprintf(stderr,
847                             "User not owned by system:administrators\n");
848                     goto abort;
849                 }
850                 if (e.nextOwned) {
851                     fprintf(stderr, "User has owned pointer\n");
852                     goto abort;
853                 }
854                 map[ei] |= MAP_OWNED;
855
856                 code = WalkOwnedChain(map, misc, ea, &e);
857                 if (code)
858                     return code;
859                 code = WalkNextChain(map, misc, ea, &e);
860                 if (code)
861                     return code;
862                 if (strchr(e.name, '@') == 0) {
863                     misc->nusers++;     /* Not a foreign user */
864                 } else {
865                     misc->nforeigns++;  /* A foreign user */
866                 }
867                 break;
868             case PRFREE:
869             case PRCONT:
870             case PRCELL:
871                 misc->ncells++;
872                 break;
873             case PRFOREIGN:
874                 fprintf(stderr,
875                         "ENTRY IS unexpected type [PRFOREIGN] (flags=0x%x)\n",
876                         ntohl(e.flags));
877                 break;
878             case PRINST:
879                 misc->ninsts++;
880                 break;
881             default:
882                 fprintf(stderr, "entry with unexpected type");
883                 goto abort;
884             }
885         }
886
887     return 0;
888 }
889
890 afs_int32
891 GC(char map[], struct misc_data *misc)
892 {
893     afs_int32 code;
894     int ei;
895     afs_int32 ea;
896     struct prentry e;
897     char m;
898
899     for (ei = 0; ei < misc->nEntries; ei++) {
900         ea = ei * sizeof(struct prentry) + sizeof(cheader);
901         code = pr_Read(ea, (char *)&e, sizeof(e));
902         if (code)
903             return code;
904         m = map[ei];
905         if (m == 0) {
906             fprintf(stderr, "Unreferenced entry:");
907             if (PrintEntryError(misc, ea, &e, 2))
908                 return PRDBBAD;
909         }
910         /* all users and groups should be owned, and their membership counts
911          * should be okay */
912         else if ((m & MAP_HASHES) == MAP_HASHES) {
913             afs_int32 id;
914             int refCount;
915             if (!(m & MAP_OWNED)) {
916                 fprintf(stderr, "Entry not on any owner chain:\n");
917                 if (PrintEntryError(misc, ea, &e, 2))
918                     return PRDBBAD;
919             }
920             id = ntohl(e.id);
921 #if defined(SUPERGROUPS)
922             if ((id != ANONYMOUSID)
923                 && ((refCount = idcount(&misc->idmap, id)) != ntohl(e.count)))
924 #else
925             if ((id >= misc->minId) && (id <= misc->maxId)
926                 && (id != ANONYMOUSID)
927                 && ((refCount = misc->idmap[id - misc->minId]) !=
928                     ntohl(e.count)))
929 #endif /* SUPERGROUPS */
930               {
931                 afs_int32 na;
932                 fprintf(stderr,
933                         "Entry membership count is inconsistent: %d entries refer to this one\n",
934                         refCount);
935                 if (PrintEntryError(misc, ea, &e, 2))
936                     return PRDBBAD;
937
938                 /* get continuation blocks too */
939                 for (na = ntohl(e.next); na; na = ntohl(e.next)) {
940                     int ni;
941                     code = ConvertDiskAddress(na, &ni);
942                     if (code)
943                         return code;
944                     code = pr_Read(na, (char *)&e, sizeof(e));
945                     if (code)
946                         return code;
947                     if (PrintEntryError(misc, na, &e, 4))
948                         return PRDBBAD;
949                 }
950             }
951         }
952     }
953     return 0;
954 }
955
956 char *
957 QuoteName(char *s)
958 {
959     char *qs;
960     if (strpbrk(s, " \t")) {
961         asprintf(&qs, "\"%s\"", s);
962     } else
963         qs = s;
964     return qs;
965 }
966
967 afs_int32
968 DumpRecreate(char map[], struct misc_data *misc)
969 {
970     afs_int32 code;
971     int ei;
972     afs_int32 ea;
973     struct prentry e;
974     afs_int32 id;
975     afs_int32 flags;
976     afs_int32 owner;
977     char *name;
978     int builtinUsers = 0;
979     int createLow = 0;          /* users uncreate from here */
980 #if defined(SUPERGROUPS)
981     struct idused *idmap;       /* map of all id's */
982 #else
983     afs_int32 *idmap;           /* map of all id's */
984 #endif
985     int found;
986     FILE *rc;
987
988     rc = misc->recreate;
989     idmap = misc->idmap;
990 #if defined(SUPERGROUPS)
991     zeromap(idmap);
992 #else
993     memset(idmap, 0, misc->idRange * sizeof(misc->idmap[0]));
994 #endif
995     do {
996         found = 0;
997         for (ei = createLow; ei < misc->nEntries; ei++) {
998             if ((map[ei] & MAP_HASHES) && (map[ei] & MAP_RECREATE) == 0) {
999                 afs_int32 mask;
1000                 afs_int32 access;
1001                 int gq, uq;
1002
1003                 ea = ei * sizeof(struct prentry) + sizeof(cheader);
1004                 code = pr_Read(ea, (char *)&e, sizeof(e));
1005                 if (code)
1006                     return code;
1007
1008                 if (misc->listentries)
1009                     pr_PrintEntry(stdout, 0 /*not in host order */ , ea, &e,
1010                                   0);
1011
1012                 id = ntohl(e.id);
1013                 flags = ntohl(e.flags);
1014                 owner = ntohl(e.owner);
1015                 name = QuoteName(e.name);
1016
1017                 if (!strcmp(e.name, "system:administrators")
1018                     || !strcmp(e.name, "system:anyuser")
1019                     || !strcmp(e.name, "system:authuser")
1020                     || !strcmp(e.name, "system:backup")
1021                     || !strcmp(e.name, "anonymous")) {
1022                     builtinUsers++;
1023                     goto user_done;
1024                 }
1025
1026                 /* check for duplicate id.  This may still lead to duplicate
1027                  * names. */
1028 #if defined(SUPERGROUPS)
1029                 if (idcount(&idmap, id))
1030 #else
1031                 if (idmap[id - misc->minId])
1032 #endif
1033                   {
1034                     fprintf(stderr, "Skipping entry with duplicate id %di\n",
1035                             id);
1036                     goto user_done;
1037                 }
1038
1039                 /* If owner doesn't exist skip for now, unless we're our own
1040                  * owner.  If so, a special case allows a group to own itself
1041                  * if caller is sysadmin.  This leaves only owner cycles to
1042                  * deal with. */
1043
1044                 if ((owner < misc->minId) || (owner > misc->maxId)) {
1045                     if (owner == ANONYMOUSID)
1046                         fprintf(stderr,
1047                                 "Warning: id %di is owned by ANONYMOUS; using sysadmin instead\n",
1048                                 id);
1049                     else
1050                         fprintf(stderr,
1051                                 "Bogus owner (%d) of id %di; using sysadmin instead\n",
1052                                 owner, id);
1053                     owner = SYSADMINID;
1054                 }
1055                 if (id == owner) {
1056                     fprintf(stderr, "Warning: group %s is self owning\n",
1057                             name);
1058                 } else if (owner == 0) {
1059                     fprintf(stderr,
1060                             "Warning: orphan group %s will become self owning.\n",
1061                             name);
1062                     owner = id;
1063                 }
1064 #if defined(SUPERGROUPS)
1065                 else if (!idcount(&idmap, owner))
1066                     goto user_skip;
1067 #else
1068                 else if (idmap[owner - misc->minId] == 0)
1069                     goto user_skip;
1070 #endif
1071
1072                 if (rc)
1073                     fprintf(rc, "cr %s %d %d\n", name, id, owner);
1074
1075                 gq = uq = access = mask = 0;
1076                 if (flags & PRACCESS) {
1077                     access = (flags >> PRIVATE_SHIFT);
1078                     mask |= PR_SF_ALLBITS;
1079                 }
1080                 if (flags & PRQUOTA) {
1081                     gq = ntohl(e.ngroups);
1082                     uq = ntohl(e.nusers);
1083                     mask |= PR_SF_NGROUPS | PR_SF_NUSERS;
1084                 }
1085                 if (mask && rc) {
1086                     fprintf(rc, "sf %d %x %x %d %d\n", id, mask, access, gq,
1087                             uq);
1088                 }
1089               user_done:
1090                 map[ei] |= MAP_RECREATE;
1091 #if defined(SUPERGROUPS)
1092                 if (id != ANONYMOUSID)
1093                     inccount(&idmap, id);
1094 #else
1095                 if (id != ANONYMOUSID)
1096                     idmap[id - misc->minId]++;
1097 #endif
1098                 found++;
1099             }
1100             /* bump low water mark if possible */
1101             if (ei == createLow)
1102                 createLow++;
1103           user_skip:;
1104         }
1105         misc->verbose = 0;
1106     } while (found);
1107
1108     /* Now create the entries with circular owner dependencies and make them
1109      * own themselves.  This is the only way to create them with the correct
1110      * names. */
1111     for (ei = 0; ei < misc->nEntries; ei++)
1112         if (((map[ei] & MAP_HASHES) == MAP_HASHES)
1113             && (map[ei] & MAP_RECREATE) == 0) {
1114             ea = ei * sizeof(struct prentry) + sizeof(cheader);
1115             code = pr_Read(ea, (char *)&e, sizeof(e));
1116             if (code)
1117                 return code;
1118
1119             id = ntohl(e.id);
1120             name = QuoteName(e.name);
1121             fprintf(stderr, "Warning: group %s in self owning cycle\n", name);
1122             if (rc)
1123                 fprintf(rc, "cr %s %d %d\n", name, id, id);
1124 #if defined(SUPERGROUPS)
1125             inccount(&idmap, id);
1126 #else
1127             idmap[id - misc->minId]++;
1128 #endif
1129         }
1130     for (ei = 0; ei < misc->nEntries; ei++)
1131         if (((map[ei] & MAP_HASHES) == MAP_HASHES)
1132             && (map[ei] & MAP_RECREATE) == 0) {
1133             ea = ei * sizeof(struct prentry) + sizeof(cheader);
1134             code = pr_Read(ea, (char *)&e, sizeof(e));
1135             if (code)
1136                 return code;
1137
1138             owner = ntohl(e.owner);
1139 #if defined(SUPERGROUPS)
1140             if (!idcount(&idmap, owner))
1141 #else
1142             if (idmap[owner - misc->minId] == 0)
1143 #endif
1144               {
1145                 fprintf(stderr,
1146                         "Skipping chown of '%s' to non-existant owner %di\n",
1147                         e.name, owner);
1148             } else if (rc)
1149                 fprintf(rc, "ce %d \"\" %d 0\n", ntohl(e.id), e.owner);
1150         }
1151
1152     if (rc == 0)
1153         return 0;
1154
1155     /* Reconstruct membership information based on the groups' user lists. */
1156     for (ei = 0; ei < misc->nEntries; ei++) {
1157         if ((map[ei] & MAP_HASHES) == MAP_HASHES) {
1158             ea = ei * sizeof(struct prentry) + sizeof(cheader);
1159             code = pr_Read(ea, (char *)&e, sizeof(e));
1160             if (code)
1161                 return code;
1162
1163             id = ntohl(e.id);
1164             flags = ntohl(e.flags);
1165
1166             if ((id < 0) && (flags & PRGRP)) {
1167                 int count = 0;
1168                 afs_int32 na;
1169                 int i;
1170                 for (i = 0; i < PRSIZE; i++) {
1171                     afs_int32 uid = ntohl(e.entries[i]);
1172                     if (uid == 0)
1173                         break;
1174                     if (uid == PRBADID)
1175                         continue;
1176 #if !defined(SUPERGROUPS)
1177                     if (uid > 0) {
1178 #endif
1179                         fprintf(rc, "au %d %d\n", uid, id);
1180                         count++;
1181 #if !defined(SUPERGROUPS)
1182                     } else
1183                         fprintf(stderr, "Skipping %di in group %di\n", uid,
1184                                 id);
1185 #endif
1186                 }
1187                 na = ntohl(e.next);
1188                 while (na) {
1189                     struct prentry c;
1190                     code = pr_Read(na, (char *)&c, sizeof(c));
1191                     if (code)
1192                         return code;
1193
1194                     if ((id == ntohl(c.id)) && (c.flags & htonl(PRCONT))) {
1195                         for (i = 0; i < COSIZE; i++) {
1196                             afs_int32 uid = ntohl(c.entries[i]);
1197                             if (uid == 0)
1198                                 break;
1199                             if (uid == PRBADID)
1200                                 continue;
1201 #if !defined(SUPERGROUPS)
1202                             if (uid > 0) {
1203 #endif
1204                                 fprintf(rc, "au %d %d\n", uid, id);
1205                                 count++;
1206 #if !defined(SUPERGROUPS)
1207                             } else
1208                                 fprintf(stderr, "Skipping %di in group %di\n",
1209                                         uid, id);
1210 #endif
1211                         }
1212                     } else {
1213                         fprintf(stderr, "Skipping continuation block at %d\n",
1214                                 na);
1215                         break;
1216                     }
1217                     na = ntohl(c.next);
1218                 }
1219                 if (count != ntohl(e.count))
1220                     fprintf(stderr,
1221                             "Group membership count problem found %d should be %d\n",
1222                             count, ntohl(e.count));
1223             } else if ((id < 0) || (flags & PRGRP)) {
1224                 fprintf(stderr, "Skipping group %di\n", id);
1225             }
1226         }
1227     }
1228     return 0;
1229 }
1230
1231 afs_int32
1232 CheckPrDatabase(struct misc_data *misc) /* info & statistics */
1233 {
1234     afs_int32 code;
1235     afs_int32 eof;
1236     int n;
1237     char *map;                  /* map of each entry in db */
1238
1239     eof = ntohl(cheader.eofPtr);
1240     eof -= sizeof(cheader);
1241     n = eof / sizeof(struct prentry);
1242     if ((eof < 0) || (n * sizeof(struct prentry) != eof)) {
1243         code = PRDBBAD;
1244         afs_com_err(whoami, code,
1245                     "eof ptr no good: eof=%d, sizeof(prentry)=%" AFS_SIZET_FMT,
1246                 eof, sizeof(struct prentry));
1247       abort:
1248         return code;
1249     }
1250     if (misc->verbose)
1251         printf("Database has %d entries\n", n);
1252     map = calloc(1, n);
1253     misc->nEntries = n;
1254
1255     if (misc->verbose) {
1256         printf("\nChecking name hash table\n");
1257         fflush(stdout);
1258     }
1259     code = WalkHashTable(cheader.nameHash, MAP_NAMEHASH, map, misc);
1260     if (code) {
1261         afs_com_err(whoami, code, "walking name hash");
1262         goto abort;
1263     }
1264     if (misc->verbose) {
1265         printf("\nChecking id hash table\n");
1266         fflush(stdout);
1267     }
1268     code = WalkHashTable(cheader.idHash, MAP_IDHASH, map, misc);
1269     if (code) {
1270         afs_com_err(whoami, code, "walking id hash");
1271         goto abort;
1272     }
1273
1274     /* hash walk calculates min and max id */
1275 #if defined(SUPERGROUPS)
1276     misc->idmap = 0;
1277 #else
1278     n = ((misc->maxId > misc->maxForId) ? misc->maxId : misc->maxForId);
1279     misc->idRange = n - misc->minId + 1;
1280     misc->idmap = calloc(misc->idRange, sizeof(afs_int32));
1281     if (!misc->idmap) {
1282         afs_com_err(whoami, 0, "Unable to malloc space for max ids of %d",
1283                 misc->idRange);
1284         code = -1;
1285         goto abort;
1286     }
1287 #endif /* SUPERGROUPS */
1288
1289     if (misc->verbose) {
1290         printf("\nChecking entry chains\n");
1291         fflush(stdout);
1292     }
1293     code = WalkChains(map, misc);
1294     if (code) {
1295         afs_com_err(whoami, code, "walking chains");
1296         goto abort;
1297     }
1298     if (misc->verbose) {
1299         printf("\nChecking free list\n");
1300         fflush(stdout);
1301     }
1302     code = WalkNextChain(map, misc, 0, 0);
1303     if (code) {
1304         afs_com_err(whoami, code, "walking free list");
1305         goto abort;
1306     }
1307     if (misc->verbose) {
1308         printf("\nChecking orphans list\n");
1309         fflush(stdout);
1310     }
1311     code = WalkOwnedChain(map, misc, 0, 0);
1312     if (code) {
1313         afs_com_err(whoami, code, "walking orphan list");
1314         goto abort;
1315     }
1316
1317     if (misc->verbose) {
1318         printf("\nChecking for unreferenced entries\n");
1319         fflush(stdout);
1320     }
1321     code = GC(map, misc);
1322     if (code) {
1323         afs_com_err(whoami, code, "looking for unreferenced entries");
1324         goto abort;
1325     }
1326
1327     DumpRecreate(map, misc);    /* check for owner cycles */
1328     if (misc->recreate)
1329         fclose(misc->recreate);
1330
1331     if (misc->anon != 2)        /* once for each hash table */
1332         fprintf(stderr, "Problems with ANON=%d\n", misc->anon);
1333     if (misc->ncells || misc->ninsts)
1334         fprintf(stderr, "Unexpected entry type\n");
1335     if (misc->nusers != ntohl(cheader.usercount)) {
1336         fprintf(stderr,
1337                 "User count inconsistent: should be %d, header claims: %d\n",
1338                 misc->nusers, ntohl(cheader.usercount));
1339     }
1340     if (misc->ngroups != ntohl(cheader.groupcount)) {
1341         fprintf(stderr,
1342                 "Group count inconsistent: should be %d, header claims: %d\n",
1343                 misc->ngroups, ntohl(cheader.groupcount));
1344     }
1345     if (misc->maxId > ntohl(cheader.maxID))
1346         fprintf(stderr,
1347                 "Database's max user Id (%d) is smaller than largest user's Id (%d).\n",
1348                 ntohl(cheader.maxID), misc->maxId);
1349     if (misc->minId < ntohl(cheader.maxGroup))
1350         fprintf(stderr,
1351                 "Database's max group Id (%d) is smaller than largest group's Id (%d).\n",
1352                 ntohl(cheader.maxGroup), misc->minId);
1353
1354     if (misc->verbose) {
1355         printf("\nMaxId = %d, MinId = %d, MaxForeignId = %d\n", misc->maxId,
1356                misc->minId, misc->maxForId);
1357         printf
1358             ("Free list is %d entries in length, %d groups on orphan list\n",
1359              misc->freeLength, misc->orphanLength);
1360         printf
1361             ("The longest owner list is %d, the longest continuation block chain is %d\n",
1362              misc->maxOwnerLength, misc->maxContLength);
1363         printf("%d users ; %d foreign users ; and %d groups\n", misc->nusers,
1364                misc->nforeigns, misc->ngroups);
1365     }
1366
1367     free(map);
1368     return code;
1369 }
1370
1371 #include "AFS_component_version_number.c"
1372
1373 int
1374 WorkerBee(struct cmd_syndesc *as, void *arock)
1375 {
1376     afs_int32 code;
1377     char *recreateFile;
1378     struct misc_data misc;      /* info & statistics */
1379
1380     initialize_PT_error_table();
1381     initialize_U_error_table();
1382
1383     pr_dbaseName = AFSDIR_SERVER_PRDB_FILEPATH;
1384     memset(&misc, 0, sizeof(misc));
1385
1386     pr_dbaseName = as->parms[0].items->data;    /* -database */
1387     misc.listuheader = (as->parms[1].items ? 1 : 0);    /* -uheader  */
1388     misc.listpheader = (as->parms[2].items ? 1 : 0);    /* -pheader  */
1389     misc.listentries = (as->parms[3].items ? 1 : 0);    /* -entries  */
1390     misc.verbose = (as->parms[4].items ? 1 : 0);        /* -verbose  */
1391     recreateFile = (as->parms[5].items ? as->parms[5].items->data : NULL);      /* -rebuild  */
1392
1393     fd = open(pr_dbaseName, O_RDONLY, 0);
1394     if (fd == -1) {
1395         afs_com_err(whoami, errno, "Open failed on db %s", pr_dbaseName);
1396         exit(2);
1397     }
1398
1399     /* Read the ubik header */
1400     if (misc.listuheader) {
1401         readUbikHeader(&misc);
1402     }
1403
1404     code = ReadHeader();
1405     if (code)
1406         return code;
1407     if (misc.listpheader)
1408         printheader(&cheader);
1409
1410     if (recreateFile) {
1411         misc.recreate = fopen(recreateFile, "w");
1412         if (misc.recreate == 0) {
1413             afs_com_err(whoami, errno,
1414                     "can't create file for recreation instructions: %s",
1415                     recreateFile);
1416             exit(4);
1417         }
1418     }
1419     code = CheckPrDatabase(&misc);
1420     if (code) {
1421         afs_com_err(whoami, code, "Checking prserver database");
1422         exit(3);
1423     }
1424     exit(0);
1425 }
1426
1427 int
1428 main(int argc, char *argv[])
1429 {
1430     struct cmd_syndesc *ts;
1431
1432     setlinebuf(stdout);
1433
1434     ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "PRDB check");
1435     cmd_AddParm(ts, "-database", CMD_SINGLE, CMD_REQUIRED, "ptdb_file");
1436     cmd_AddParm(ts, "-uheader", CMD_FLAG, CMD_OPTIONAL,
1437                 "Display UBIK header");
1438     cmd_AddParm(ts, "-pheader", CMD_FLAG, CMD_OPTIONAL,
1439                 "Display KADB header");
1440     cmd_AddParm(ts, "-entries", CMD_FLAG, CMD_OPTIONAL, "Display entries");
1441     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
1442     cmd_AddParm(ts, "-rebuild", CMD_SINGLE, CMD_OPTIONAL | CMD_HIDE,
1443                 "out_file");
1444
1445     return cmd_Dispatch(argc, argv);
1446 }
1447
1448
1449 #if defined(SUPERGROUPS)
1450
1451 /* new routines to deal with very large ID numbers */
1452
1453 void
1454 zeromap(struct idused *idmap)
1455 {
1456     while (idmap) {
1457         memset(idmap->idcount, 0, sizeof idmap->idcount);
1458         idmap = idmap->idnext;
1459     }
1460 }
1461
1462 void
1463 inccount(struct idused **idmapp, int id)
1464 {
1465     struct idused *idmap;
1466
1467     if (IDCOUNT & (IDCOUNT - 1)) {
1468         fprintf(stderr, "IDCOUNT must be power of 2!\n");
1469         exit(1);
1470     }
1471     while ((idmap = *idmapp) != NULL) {
1472         if (idmap->idstart == (id & ~(IDCOUNT - 1)))
1473             break;
1474         idmapp = &idmap->idnext;
1475     }
1476     if (!idmap) {
1477         idmap = calloc(1, sizeof *idmap);
1478         if (!idmap) {
1479             perror("idmap");
1480             exit(1);
1481         }
1482         idmap->idstart = id & ~(IDCOUNT - 1);
1483         idmap->idnext = *idmapp;
1484         *idmapp = idmap;
1485     }
1486     ++idmap->idcount[id & (IDCOUNT - 1)];
1487 }
1488
1489 int
1490 idcount(struct idused **idmapp, int id)
1491 {
1492     struct idused *idmap;
1493
1494     if (IDCOUNT & (IDCOUNT - 1)) {
1495         fprintf(stderr, "IDCOUNT must be power of 2!\n");
1496         exit(1);
1497     }
1498     while ((idmap = *idmapp) != NULL) {
1499         if (idmap->idstart == (id & ~(IDCOUNT - 1))) {
1500             return idmap->idcount[id & (IDCOUNT - 1)];
1501         }
1502         idmapp = &idmap->idnext;
1503     }
1504     return 0;
1505 }
1506 #endif /* SUPERGROUPS */