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 && (abs(x)>32767)) || \
32 ((flags&DO_OTR)==0 && (abs(x)<32768)))
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";
104 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
122 flags |= (DO_GRP|DO_MEM);
135 "Usage: ptdump [options] [-d data] [-p prdb]\n");
136 fputs(" Options:\n", stderr);
137 fputs(" -w Update prdb with contents of data file\n", stderr);
138 fputs(" -u Display users\n", stderr);
139 fputs(" -g Display groups\n", stderr);
140 fputs(" -m Display group members\n", stderr);
141 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
142 fputs(" -s Display only system (Moira) data\n", stderr);
143 fputs(" -x Display extra users/groups\n", stderr);
147 if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) {
148 fprintf(stderr, "ptdump: cannot open %s: %s\n",
149 pfile, sys_errlist[errno]);
152 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
153 fprintf(stderr, "ptdump: error reading %s: %s\n",
154 pfile, sys_errlist[errno]);
159 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
160 fprintf(stderr, "ptdump: error opening %s: %s\n",
161 dfile, sys_errlist[errno]);
165 dfp = (wflag ? stdin : stdout);
167 uh = (struct ubik_hdr *)buffer;
168 if (ntohl(uh->magic) != UBIK_MAGIC)
169 fprintf(stderr, "ptdump: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
170 pfile, ntohl(uh->magic), UBIK_MAGIC);
171 bcopy(&uh->version, &uv, sizeof(struct ubik_version));
172 fprintf(stderr, "Ubik Version is: %d.%d\n",
173 uv.epoch, uv.counter);
174 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
175 fprintf(stderr, "ptdump: error reading %s: %s\n",
176 pfile, sys_errlist[errno]);
181 initialize_pt_error_table();
186 while(fgets(buffer, sizeof(buffer), dfp)) {
187 int id, oid, cid, flags, quota, uid;
188 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
190 if (isspace(*buffer)) {
191 sscanf(buffer, "%s %d", mem, &uid);
193 for (u=usr_head; u; u=u->next)
194 if (u->uid && u->uid==uid) break;
196 /* Add user - deferred because it is probably foreign */
198 if (FindByID(0, uid))
201 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
203 code = pr_WriteEntry(0,0,gpos,&gentry);
205 fprintf(stderr, "Error setting group count on %s: %s\n",
206 name, error_message(code));
209 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
210 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
213 fprintf(stderr, "Error while creating %s: %s\n",
214 u->name, error_message(code));
217 /* Add user to group */
218 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
220 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
221 code = pr_ReadEntry(0,0,upos,&uentry);
222 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
223 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
224 if (!code) code = AddToEntry (0, &uentry, upos, id);
229 fprintf(stderr, "Error while adding %s to %s: %s\n",
230 mem, name, error_message(code));
232 sscanf(buffer, "%s %d/%d %d %d %d",
233 name, &flags, "a, &id, &oid, &cid);
238 code = CreateEntry(0, name, &id, 1/*idflag*/,
239 flags&PRGRP, oid, cid);
240 if (code == PRBADNAM) {
241 u = (struct usr_list *)malloc(sizeof(struct usr_list));
244 strcpy(u->name, name);
248 fprintf(stderr, "Error while creating %s: %s\n",
249 name, error_message(code));
250 } else if ((flags&PRACCESS) ||
251 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
252 gpos = FindByID(0, id);
253 code = pr_ReadEntry(0,0,gpos,&gentry);
255 gentry.flags = flags;
256 code = pr_WriteEntry(0,0,gpos,&gentry);
259 fprintf(stderr,"Error while setting flags on %s: %s\n",
260 name, error_message(code));
264 for (u=usr_head; u; u=u->next)
266 fprintf(stderr, "Error while creating %s: %s\n",
267 u->name, error_message(PRBADNAM));
269 for (i = 0; i < HASHSIZE; i++) {
270 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
272 upos = display_entry(upos);
278 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
279 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
280 fprintf(stderr, "ptdump: error reading %s: %s\n",
281 pfile, sys_errlist[errno]);
284 uh = (struct ubik_hdr *)buffer;
285 if ((uh->version.epoch != uv.epoch) ||
286 (uh->version.counter != uv.counter)) {
287 fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
288 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
289 uv.epoch, uv.counter, uh->version.epoch,
290 uh->version.counter);
296 int display_entry (offset)
301 lseek (dbase_fd, offset+HDRSIZE, L_SET);
302 read(dbase_fd, &pre, sizeof(struct prentry));
306 if ((pre.flags & PRFREE) == 0) {
307 if (pre.flags & PRGRP) {
311 if (print_id(pre.id) && (flags&DO_USR))
312 fprintf(dfp, FMT_BASE,
313 pre.name, pre.flags, pre.ngroups,
314 pre.id, pre.owner, pre.creator);
318 return(nflag ? pre.nextName: pre.nextID);
327 i = grp_count++ % 1024;
329 g = (struct grp_list *)malloc(sizeof(struct grp_list));
337 void display_groups()
343 while (grp_count--) {
355 void display_group(id)
358 register int i, offset;
361 offset = ntohl(prh.idHash[IDHash(id)]);
363 lseek(dbase_fd, offset+HDRSIZE, L_SET);
364 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
365 fprintf(stderr, "ptdump: read i/o error: %s\n",
376 fprintf(dfp, FMT_BASE,
377 pre.name, pre.flags, pre.ngroups,
378 pre.id, pre.owner, pre.creator);
382 if ((flags&DO_MEM) == 0)
385 for (i=0; i<PRSIZE; i++) {
386 if ((id=pre.entries[i]) == 0)
388 if (id==PRBADID) continue;
389 if (print_id(id) || print_grp==1) {
391 fprintf(dfp, FMT_BASE,
392 pre.name, pre.flags, pre.ngroups,
393 pre.id, pre.owner, pre.creator);
396 fprintf(dfp, FMT_MEM, id_to_name(id), id);
402 lseek(dbase_fd, offset+HDRSIZE, L_SET);
403 read(dbase_fd, &prco, sizeof(struct contentry));
404 prco.next = ntohl(prco.next);
405 for (i = 0; i < COSIZE; i++) {
406 prco.entries[i] = ntohl(prco.entries[i]);
407 if ((id=prco.entries[i]) == 0)
409 if (id==PRBADID) continue;
410 if (print_id(id) || print_grp==1) {
412 fprintf(dfp, FMT_BASE,
413 pre.name, pre.flags, pre.ngroups,
414 pre.id, pre.owner, pre.creator);
417 fprintf(dfp, FMT_MEM, id_to_name(id), id);
420 if ((i == COSIZE) && prco.next)
432 pre->flags = ntohl(pre->flags);
433 pre->id = ntohl(pre->id);
434 pre->cellid = ntohl(pre->cellid);
435 pre->next = ntohl(pre->next);
436 pre->nextID = ntohl(pre->nextID);
437 pre->nextName = ntohl(pre->nextName);
438 pre->owner = ntohl(pre->owner);
439 pre->creator = ntohl(pre->creator);
440 pre->ngroups = ntohl(pre->ngroups);
441 pre->nusers = ntohl(pre->nusers);
442 pre->count = ntohl(pre->count);
443 pre->instance = ntohl(pre->instance);
444 pre->owned = ntohl(pre->owned);
445 pre->nextOwned = ntohl(pre->nextOwned);
446 pre->parent = ntohl(pre->parent);
447 pre->sibling = ntohl(pre->sibling);
448 pre->child = ntohl(pre->child);
449 for (i = 0; i < PRSIZE; i++) {
450 pre->entries[i] = ntohl(pre->entries[i]);
458 static struct prentry pre;
461 name = check_core(id);
462 if (name) return(name);
463 offset = ntohl(prh.idHash[IDHash(id)]);
465 lseek(dbase_fd, offset+HDRSIZE, L_SET);
466 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
467 fprintf(stderr, "ptdump: read i/o error: %s\n",
471 pre.id = ntohl(pre.id);
473 name = checkin(&pre);
476 offset = ntohl(pre.nextID);
484 struct hash_entry *he, *last;
488 last = (struct hash_entry *)0;
489 he = hat[IDHash(id)];
491 if (id == he->h_id) return(he->h_name);
495 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
497 fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
501 he->next = (struct hash_entry *)0;
502 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
503 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
504 else last->next = he;
511 struct hash_entry *he;
512 he = hat[IDHash(id)];
514 if (id == he->h_id) return(he->h_name);