3 * ptdump: Program to dump the AFS protection server database
6 * Assumptions: We *cheat* here and read the datafile directly, ie.
7 * not going through the ubik distributed data manager.
8 * therefore the database must be quiescent for the
9 * output of this program to be valid.
12 #include <sys/types.h>
19 #include <afs/param.h>
21 #include <netinet/in.h>
22 #define UBIK_INTERNALS
30 #define IDHash(x) (abs(x) % HASHSIZE)
31 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
32 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
37 extern char *sys_errlist[];
38 #define strerror(a) sys_errlist[a]
42 void display_groups();
50 char h_name[PR_MAXNAMELEN];
52 struct hash_entry *next;
54 struct hash_entry *hat[HASHSIZE];
56 static struct contentry prco;
57 static struct prentry pre;
58 static struct prheader prh;
59 static struct ubik_version uv;
62 struct grp_list *next;
65 static struct grp_list *grp_head=0;
66 static long grp_count=0;
69 struct usr_list *next;
70 char name[PR_MAXNAMELEN];
73 static struct usr_list *usr_head=0;
79 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
80 #define FMT_MEM " %-8s %d\n"
99 struct prentry uentry, gentry;
102 char *pfile = "/usr/afs/db/prdb.DB0";
105 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
123 flags |= (DO_GRP|DO_MEM);
136 "Usage: ptdump [options] [-d data] [-p prdb]\n");
137 fputs(" Options:\n", stderr);
138 fputs(" -w Update prdb with contents of data file\n", stderr);
139 fputs(" -u Display users\n", stderr);
140 fputs(" -g Display groups\n", stderr);
141 fputs(" -m Display group members\n", stderr);
142 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
143 fputs(" -s Display only system (Moira) data\n", stderr);
144 fputs(" -x Display extra users/groups\n", stderr);
148 if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) {
149 fprintf(stderr, "ptdump: cannot open %s: %s\n",
150 pfile, sys_errlist[errno]);
153 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
154 fprintf(stderr, "ptdump: error reading %s: %s\n",
155 pfile, sys_errlist[errno]);
160 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
161 fprintf(stderr, "ptdump: error opening %s: %s\n",
162 dfile, sys_errlist[errno]);
166 dfp = (wflag ? stdin : stdout);
168 uh = (struct ubik_hdr *)buffer;
169 if (ntohl(uh->magic) != UBIK_MAGIC)
170 fprintf(stderr, "ptdump: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
171 pfile, ntohl(uh->magic), UBIK_MAGIC);
172 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
173 fprintf(stderr, "Ubik Version is: %d.%d\n",
174 ntohl(uv.epoch), ntohl(uv.counter));
175 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
176 fprintf(stderr, "ptdump: error reading %s: %s\n",
177 pfile, sys_errlist[errno]);
182 initialize_pt_error_table();
184 #define FLUSH_GROUP \
186 code = pr_WriteEntry(0,0,gpos,&gentry); \
188 fprintf(stderr, "Error while writing group %s: %s\n", \
189 gentry.name, error_message(code)); \
190 else need_write = 0; \
196 while(fgets(buffer, sizeof(buffer), dfp)) {
197 int id, oid, cid, flags, quota, uid;
198 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
200 if (isspace(*buffer)) {
201 sscanf(buffer, "%s %d", mem, &uid);
203 for (u=usr_head; u; u=u->next)
204 if (u->uid && u->uid==uid) break;
206 /* Add user - deferred because it is probably foreign */
208 if (upos = FindByID(0, uid))
211 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
213 code = pr_WriteEntry(0,0,gpos,&gentry);
215 fprintf(stderr, "Error setting group count on %s: %s\n",
216 name, error_message(code));
219 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
220 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
223 fprintf(stderr, "Error while creating %s: %s\n",
224 u->name, error_message(code));
227 /* Add user to group */
228 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
231 if (!code && !upos) {
232 upos = FindByID(0,uid);
233 if (upos) code = pr_ReadEntry(0,0,upos,&uentry);
238 /* Fast group update */
239 if (!code && (gentry.count < PRSIZE)) {
241 for (i=0; i<PRSIZE; i++) {
242 if (gentry.entries[i] == PRBADID) {
243 gentry.entries[i] = uid;
249 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
252 if (!code) code = AddToEntry (0, &uentry, upos, id);
255 fprintf(stderr, "Error while adding %s to %s: %s\n",
256 mem, name, error_message(code));
259 sscanf(buffer, "%s %d/%d %d %d %d",
260 name, &flags, "a, &id, &oid, &cid);
262 if (gpos = FindByID(0, id))
265 code = CreateEntry(0, name, &id, 1/*idflag*/,
266 flags&PRGRP, oid, cid);
267 if (!code) gpos = FindByID(0, id);
269 if (code == PRBADNAM) {
270 u = (struct usr_list *)malloc(sizeof(struct usr_list));
273 strcpy(u->name, name);
277 fprintf(stderr, "Error while creating %s: %s\n",
278 name, error_message(code));
280 code = pr_ReadEntry(0,0,gpos,&gentry);
282 fprintf(stderr,"Error while reading group %s: %s\n",
283 name, error_message(code));
284 else if ((flags&PRACCESS) ||
285 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
286 gentry.flags = flags;
287 gentry.ngroups = quota;
291 "Error while setting flags on %s: %s\n",
292 name, error_message(code));
297 for (u=usr_head; u; u=u->next)
299 fprintf(stderr, "Error while creating %s: %s\n",
300 u->name, error_message(PRBADNAM));
302 for (i = 0; i < HASHSIZE; i++) {
303 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
305 upos = display_entry(upos);
311 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
312 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
313 fprintf(stderr, "ptdump: error reading %s: %s\n",
314 pfile, sys_errlist[errno]);
317 uh = (struct ubik_hdr *)buffer;
318 if ((uh->version.epoch != uv.epoch) ||
319 (uh->version.counter != uv.counter)) {
320 fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
321 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
322 ntohl(uv.epoch), ntohl(uv.counter),
323 ntohl(uh->version.epoch), ntohl(uh->version.counter));
329 int display_entry (offset)
334 lseek (dbase_fd, offset+HDRSIZE, L_SET);
335 read(dbase_fd, &pre, sizeof(struct prentry));
339 if ((pre.flags & PRFREE) == 0) {
340 if (pre.flags & PRGRP) {
344 if (print_id(pre.id) && (flags&DO_USR))
345 fprintf(dfp, FMT_BASE,
346 pre.name, pre.flags, pre.ngroups,
347 pre.id, pre.owner, pre.creator);
351 return(nflag ? pre.nextName: pre.nextID);
360 i = grp_count++ % 1024;
362 g = (struct grp_list *)malloc(sizeof(struct grp_list));
370 void display_groups()
376 while (grp_count--) {
388 void display_group(id)
391 register int i, offset;
394 offset = ntohl(prh.idHash[IDHash(id)]);
396 lseek(dbase_fd, offset+HDRSIZE, L_SET);
397 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
398 fprintf(stderr, "ptdump: read i/o error: %s\n",
409 fprintf(dfp, FMT_BASE,
410 pre.name, pre.flags, pre.ngroups,
411 pre.id, pre.owner, pre.creator);
415 if ((flags&DO_MEM) == 0)
418 for (i=0; i<PRSIZE; i++) {
419 if ((id=pre.entries[i]) == 0)
421 if (id==PRBADID) continue;
422 if (print_id(id) || print_grp==1) {
424 fprintf(dfp, FMT_BASE,
425 pre.name, pre.flags, pre.ngroups,
426 pre.id, pre.owner, pre.creator);
429 fprintf(dfp, FMT_MEM, id_to_name(id), id);
435 lseek(dbase_fd, offset+HDRSIZE, L_SET);
436 read(dbase_fd, &prco, sizeof(struct contentry));
437 prco.next = ntohl(prco.next);
438 for (i = 0; i < COSIZE; i++) {
439 prco.entries[i] = ntohl(prco.entries[i]);
440 if ((id=prco.entries[i]) == 0)
442 if (id==PRBADID) continue;
443 if (print_id(id) || print_grp==1) {
445 fprintf(dfp, FMT_BASE,
446 pre.name, pre.flags, pre.ngroups,
447 pre.id, pre.owner, pre.creator);
450 fprintf(dfp, FMT_MEM, id_to_name(id), id);
453 if ((i == COSIZE) && prco.next)
465 pre->flags = ntohl(pre->flags);
466 pre->id = ntohl(pre->id);
467 pre->cellid = ntohl(pre->cellid);
468 pre->next = ntohl(pre->next);
469 pre->nextID = ntohl(pre->nextID);
470 pre->nextName = ntohl(pre->nextName);
471 pre->owner = ntohl(pre->owner);
472 pre->creator = ntohl(pre->creator);
473 pre->ngroups = ntohl(pre->ngroups);
474 pre->nusers = ntohl(pre->nusers);
475 pre->count = ntohl(pre->count);
476 pre->instance = ntohl(pre->instance);
477 pre->owned = ntohl(pre->owned);
478 pre->nextOwned = ntohl(pre->nextOwned);
479 pre->parent = ntohl(pre->parent);
480 pre->sibling = ntohl(pre->sibling);
481 pre->child = ntohl(pre->child);
482 for (i = 0; i < PRSIZE; i++) {
483 pre->entries[i] = ntohl(pre->entries[i]);
491 static struct prentry pre;
494 name = check_core(id);
495 if (name) return(name);
496 offset = ntohl(prh.idHash[IDHash(id)]);
498 lseek(dbase_fd, offset+HDRSIZE, L_SET);
499 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
500 fprintf(stderr, "ptdump: read i/o error: %s\n",
504 pre.id = ntohl(pre.id);
506 name = checkin(&pre);
509 offset = ntohl(pre.nextID);
517 struct hash_entry *he, *last;
521 last = (struct hash_entry *)0;
522 he = hat[IDHash(id)];
524 if (id == he->h_id) return(he->h_name);
528 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
530 fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
534 he->next = (struct hash_entry *)0;
535 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
536 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
537 else last->next = he;
544 struct hash_entry *he;
545 he = hat[IDHash(id)];
547 if (id == he->h_id) return(he->h_name);