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];
77 static struct usr_list *usr_head=0;
83 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
84 #define FMT_MEM " %-8s %d\n"
103 register struct ubik_hdr *uh;
104 char *pfile = "/usr/afs/db/prdb.DB0";
107 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
125 flags |= (DO_GRP|DO_MEM);
138 "Usage: ptdump [options] [-d data] [-p prdb]\n");
139 fputs(" Options:\n", stderr);
140 fputs(" -w Update prdb with contents of data file\n", stderr);
141 fputs(" -u Display users\n", stderr);
142 fputs(" -g Display groups\n", stderr);
143 fputs(" -m Display group members\n", stderr);
144 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
145 fputs(" -s Display only system (Moira) data\n", stderr);
146 fputs(" -x Display extra users/groups\n", stderr);
150 if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) {
151 fprintf(stderr, "ptdump: cannot open %s: %s\n",
152 pfile, sys_errlist[errno]);
155 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
156 fprintf(stderr, "ptdump: error reading %s: %s\n",
157 pfile, sys_errlist[errno]);
162 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
163 fprintf(stderr, "ptdump: error opening %s: %s\n",
164 dfile, sys_errlist[errno]);
168 dfp = (wflag ? stdin : stdout);
170 uh = (struct ubik_hdr *)buffer;
171 if (ntohl(uh->magic) != UBIK_MAGIC)
172 fprintf(stderr, "ptdump: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
173 pfile, ntohl(uh->magic), UBIK_MAGIC);
174 bcopy(&uh->version, &uv, sizeof(struct ubik_version));
175 fprintf(stderr, "Ubik Version is: %d.%d\n",
176 uv.epoch, uv.counter);
177 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
178 fprintf(stderr, "ptdump: error reading %s: %s\n",
179 pfile, sys_errlist[errno]);
183 initialize_pt_error_table();
184 initialize_rxk_error_table();
185 initialize_acfg_error_table();
190 while(fgets(buffer, sizeof(buffer), dfp)) {
191 int id, oid, cid, flags, quota, uid;
192 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
194 if (isspace(*buffer)) {
195 sscanf(buffer, "%s %d", mem, &uid);
196 for (u=usr_head; u; u=u->next)
197 if (u->uid && u->uid==uid) break;
199 i = PR_INewEntry(u->cid, u->name, u->uid, u->oid);
202 fprintf(stderr, "Error while creating %s: %s\n",
203 name, error_message(i));
206 i = PR_SetFieldsEntry(NULL, id,
207 ((flags&PRQUOTA)?PR_SF_NGROUPS:0)|
208 ((flags&PRACCESS)?PR_SF_ALLBITS:0),
209 flags >> PRIVATE_SHIFT,
212 fprintf(stderr, "Error while setting flags on %s\n",
215 i = PR_AddToGroup(NULL,uid,id);
216 if (i==0 || i==PRIDEXIST) continue;
217 fprintf(stderr, "Error while adding %s to %s: %s\n",
218 mem, name, error_message(i));
220 sscanf(buffer, "%s %d/%d %d %d %d",
221 name, &flags, "a, &id, &oid, &cid);
222 i = PR_INewEntry(cid, name, id, oid);
224 u = (struct usr_list *)malloc(sizeof(struct usr_list));
231 strcpy(u->name, name);
235 if (i && i != PRIDEXIST) {
236 fprintf(stderr, "Error while creating %s: %s\n",
237 name, error_message(i));
240 i = PR_SetFieldsEntry(NULL, id,
241 ((flags&PRQUOTA) ? PR_SF_NGROUPS : 0)|
242 ((flags&PRACCESS) ? PR_SF_ALLBITS : 0),
243 flags >> PRIVATE_SHIFT,
246 fprintf(stderr, "Error while setting flags on %s: %s\n",
247 name, error_message(i));
250 for (u=usr_head; u; u=u->next)
252 fprintf(stderr, "Error while creating %s: %s\n",
253 u->name, error_message(PRBADNAM));
255 for (i = 0; i < HASHSIZE; i++) {
256 offset = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
258 offset = display_entry(offset);
264 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
265 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
266 fprintf(stderr, "ptdump: error reading %s: %s\n",
267 pfile, sys_errlist[errno]);
270 uh = (struct ubik_hdr *)buffer;
271 if ((uh->version.epoch != uv.epoch) ||
272 (uh->version.counter != uv.counter)) {
273 fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
274 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
275 uv.epoch, uv.counter, uh->version.epoch,
276 uh->version.counter);
282 int display_entry (offset)
287 lseek (dbase_fd, offset+HDRSIZE, L_SET);
288 read(dbase_fd, &pre, sizeof(struct prentry));
292 if ((pre.flags & PRFREE) == 0) {
293 if (pre.flags & PRGRP) {
297 if (print_id(pre.id) && (flags&DO_USR))
298 fprintf(dfp, FMT_BASE,
299 pre.name, pre.flags, pre.ngroups,
300 pre.id, pre.owner, pre.creator);
304 return(nflag ? pre.nextName: pre.nextID);
313 i = grp_count++ % 1024;
315 g = (struct grp_list *)malloc(sizeof(struct grp_list));
323 void display_groups()
329 while (grp_count--) {
341 void display_group(id)
344 register int i, offset;
347 offset = ntohl(prh.idHash[IDHash(id)]);
349 lseek(dbase_fd, offset+HDRSIZE, L_SET);
350 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
351 fprintf(stderr, "ptdump: read i/o error: %s\n",
362 fprintf(dfp, FMT_BASE,
363 pre.name, pre.flags, pre.ngroups,
364 pre.id, pre.owner, pre.creator);
368 if ((flags&DO_MEM) == 0)
371 for (i=0; i<PRSIZE; i++) {
372 if ((id=pre.entries[i]) == 0)
374 if (id==PRBADID) continue;
375 if (print_id(id) || print_grp==1) {
377 fprintf(dfp, FMT_BASE,
378 pre.name, pre.flags, pre.ngroups,
379 pre.id, pre.owner, pre.creator);
382 fprintf(dfp, FMT_MEM, id_to_name(id), id);
388 lseek(dbase_fd, offset+HDRSIZE, L_SET);
389 read(dbase_fd, &prco, sizeof(struct contentry));
390 prco.next = ntohl(prco.next);
391 for (i = 0; i < COSIZE; i++) {
392 prco.entries[i] = ntohl(prco.entries[i]);
393 if ((id=prco.entries[i]) == 0)
395 if (id==PRBADID) continue;
396 if (print_id(id) || print_grp==1) {
398 fprintf(dfp, FMT_BASE,
399 pre.name, pre.flags, pre.ngroups,
400 pre.id, pre.owner, pre.creator);
403 fprintf(dfp, FMT_MEM, id_to_name(id), id);
406 if ((i == COSIZE) && prco.next)
418 pre->flags = ntohl(pre->flags);
419 pre->id = ntohl(pre->id);
420 pre->cellid = ntohl(pre->cellid);
421 pre->next = ntohl(pre->next);
422 pre->nextID = ntohl(pre->nextID);
423 pre->nextName = ntohl(pre->nextName);
424 pre->owner = ntohl(pre->owner);
425 pre->creator = ntohl(pre->creator);
426 pre->ngroups = ntohl(pre->ngroups);
427 pre->nusers = ntohl(pre->nusers);
428 pre->count = ntohl(pre->count);
429 pre->instance = ntohl(pre->instance);
430 pre->owned = ntohl(pre->owned);
431 pre->nextOwned = ntohl(pre->nextOwned);
432 pre->parent = ntohl(pre->parent);
433 pre->sibling = ntohl(pre->sibling);
434 pre->child = ntohl(pre->child);
435 for (i = 0; i < PRSIZE; i++) {
436 pre->entries[i] = ntohl(pre->entries[i]);
444 static struct prentry pre;
447 name = check_core(id);
448 if (name) return(name);
449 offset = ntohl(prh.idHash[IDHash(id)]);
451 lseek(dbase_fd, offset+HDRSIZE, L_SET);
452 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
453 fprintf(stderr, "ptdump: read i/o error: %s\n",
457 pre.id = ntohl(pre.id);
459 name = checkin(&pre);
462 offset = ntohl(pre.nextID);
470 struct hash_entry *he, *last;
474 last = (struct hash_entry *)0;
475 he = hat[IDHash(id)];
477 if (id == he->h_id) return(he->h_name);
481 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
483 fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
487 he->next = (struct hash_entry *)0;
488 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
489 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
490 else last->next = he;
497 struct hash_entry *he;
498 he = hat[IDHash(id)];
500 if (id == he->h_id) return(he->h_name);