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";
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 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
172 if (wflag && uv.epoch==0 && uv.counter==0) {
173 uv.epoch=2; /* a ubik version of 0 or 1 has special meaning */
174 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
175 lseek(dbase_fd, 0, SEEK_SET);
176 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
177 fprintf(stderr, "ptdump: error writing ubik version to %s: %s\n",
178 pfile, sys_errlist[errno]);
182 fprintf(stderr, "Ubik Version is: %d.%d\n",
183 uv.epoch, uv.counter);
184 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
185 fprintf(stderr, "ptdump: error reading %s: %s\n",
186 pfile, sys_errlist[errno]);
191 initialize_pt_error_table();
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 (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) {
230 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
231 code = pr_ReadEntry(0,0,upos,&uentry);
232 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
233 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
234 if (!code) code = AddToEntry (0, &uentry, upos, id);
239 fprintf(stderr, "Error while adding %s to %s: %s\n",
240 mem, name, error_message(code));
242 sscanf(buffer, "%s %d/%d %d %d %d",
243 name, &flags, "a, &id, &oid, &cid);
248 code = CreateEntry(0, name, &id, 1/*idflag*/,
249 flags&PRGRP, oid, cid);
250 if (code == PRBADNAM) {
251 u = (struct usr_list *)malloc(sizeof(struct usr_list));
254 strcpy(u->name, name);
258 fprintf(stderr, "Error while creating %s: %s\n",
259 name, error_message(code));
260 } else if ((flags&PRACCESS) ||
261 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
262 gpos = FindByID(0, id);
263 code = pr_ReadEntry(0,0,gpos,&gentry);
265 gentry.flags = flags;
266 gentry.ngroups = quota;
267 code = pr_WriteEntry(0,0,gpos,&gentry);
270 fprintf(stderr,"Error while setting flags on %s: %s\n",
271 name, error_message(code));
275 for (u=usr_head; u; u=u->next)
277 fprintf(stderr, "Error while creating %s: %s\n",
278 u->name, error_message(PRBADNAM));
280 for (i = 0; i < HASHSIZE; i++) {
281 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
283 upos = display_entry(upos);
289 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
290 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
291 fprintf(stderr, "ptdump: error reading %s: %s\n",
292 pfile, sys_errlist[errno]);
295 uh = (struct ubik_hdr *)buffer;
296 if ((uh->version.epoch != uv.epoch) ||
297 (uh->version.counter != uv.counter)) {
298 fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
299 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
300 uv.epoch, uv.counter, uh->version.epoch,
301 uh->version.counter);
307 int display_entry (offset)
312 lseek (dbase_fd, offset+HDRSIZE, L_SET);
313 read(dbase_fd, &pre, sizeof(struct prentry));
317 if ((pre.flags & PRFREE) == 0) {
318 if (pre.flags & PRGRP) {
322 if (print_id(pre.id) && (flags&DO_USR))
323 fprintf(dfp, FMT_BASE,
324 pre.name, pre.flags, pre.ngroups,
325 pre.id, pre.owner, pre.creator);
329 return(nflag ? pre.nextName: pre.nextID);
338 i = grp_count++ % 1024;
340 g = (struct grp_list *)malloc(sizeof(struct grp_list));
348 void display_groups()
354 while (grp_count--) {
366 void display_group(id)
369 register int i, offset;
372 offset = ntohl(prh.idHash[IDHash(id)]);
374 lseek(dbase_fd, offset+HDRSIZE, L_SET);
375 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
376 fprintf(stderr, "ptdump: read i/o error: %s\n",
387 fprintf(dfp, FMT_BASE,
388 pre.name, pre.flags, pre.ngroups,
389 pre.id, pre.owner, pre.creator);
393 if ((flags&DO_MEM) == 0)
396 for (i=0; i<PRSIZE; i++) {
397 if ((id=pre.entries[i]) == 0)
399 if (id==PRBADID) continue;
400 if (print_id(id) || print_grp==1) {
402 fprintf(dfp, FMT_BASE,
403 pre.name, pre.flags, pre.ngroups,
404 pre.id, pre.owner, pre.creator);
407 fprintf(dfp, FMT_MEM, id_to_name(id), id);
413 lseek(dbase_fd, offset+HDRSIZE, L_SET);
414 read(dbase_fd, &prco, sizeof(struct contentry));
415 prco.next = ntohl(prco.next);
416 for (i = 0; i < COSIZE; i++) {
417 prco.entries[i] = ntohl(prco.entries[i]);
418 if ((id=prco.entries[i]) == 0)
420 if (id==PRBADID) continue;
421 if (print_id(id) || print_grp==1) {
423 fprintf(dfp, FMT_BASE,
424 pre.name, pre.flags, pre.ngroups,
425 pre.id, pre.owner, pre.creator);
428 fprintf(dfp, FMT_MEM, id_to_name(id), id);
431 if ((i == COSIZE) && prco.next)
443 pre->flags = ntohl(pre->flags);
444 pre->id = ntohl(pre->id);
445 pre->cellid = ntohl(pre->cellid);
446 pre->next = ntohl(pre->next);
447 pre->nextID = ntohl(pre->nextID);
448 pre->nextName = ntohl(pre->nextName);
449 pre->owner = ntohl(pre->owner);
450 pre->creator = ntohl(pre->creator);
451 pre->ngroups = ntohl(pre->ngroups);
452 pre->nusers = ntohl(pre->nusers);
453 pre->count = ntohl(pre->count);
454 pre->instance = ntohl(pre->instance);
455 pre->owned = ntohl(pre->owned);
456 pre->nextOwned = ntohl(pre->nextOwned);
457 pre->parent = ntohl(pre->parent);
458 pre->sibling = ntohl(pre->sibling);
459 pre->child = ntohl(pre->child);
460 for (i = 0; i < PRSIZE; i++) {
461 pre->entries[i] = ntohl(pre->entries[i]);
469 static struct prentry pre;
472 name = check_core(id);
473 if (name) return(name);
474 offset = ntohl(prh.idHash[IDHash(id)]);
476 lseek(dbase_fd, offset+HDRSIZE, L_SET);
477 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
478 fprintf(stderr, "ptdump: read i/o error: %s\n",
482 pre.id = ntohl(pre.id);
484 name = checkin(&pre);
487 offset = ntohl(pre.nextID);
495 struct hash_entry *he, *last;
499 last = (struct hash_entry *)0;
500 he = hat[IDHash(id)];
502 if (id == he->h_id) return(he->h_name);
506 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
508 fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
512 he->next = (struct hash_entry *)0;
513 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
514 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
515 else last->next = he;
522 struct hash_entry *he;
523 he = hat[IDHash(id)];
525 if (id == he->h_id) return(he->h_name);