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();
188 while(fgets(buffer, sizeof(buffer), dfp)) {
190 int id, oid, cid, flags, quota, uid;
191 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
193 if (isspace(*buffer)) {
194 sscanf(buffer, "%s %d", mem, &uid);
195 for (u=usr_head; u; u=u->next)
196 if (u->uid && u->uid==uid) break;
198 i = PR_INewEntry(u->cid, u->name, u->uid, u->oid);
200 if (i && i!=PRIDEXIST) {
201 fprintf(stderr, "Error while creating %s: %s\n",
202 name, error_message(i));
205 i = PR_SetFieldsEntry(NULL, id,
206 ((flags&PRQUOTA)?PR_SF_NGROUPS:0)|
207 ((flags&PRACCESS)?PR_SF_ALLBITS:0),
208 flags >> PRIVATE_SHIFT,
211 fprintf(stderr, "Error while setting flags on %s\n",
214 i = PR_AddToGroup(NULL,uid,id);
215 if (i==0 || i==PRIDEXIST) continue;
216 fprintf(stderr, "Error while adding %s to %s\n", mem, name);
218 sscanf(buffer, "%s %d/%d %d %d %d",
219 name, &flags, "a, &id, &oid, &cid);
220 i = PR_INewEntry(cid, name, id, oid);
222 u = (struct usr_list *)malloc(sizeof(struct usr_list));
229 strcpy(u->name, name);
233 if (i && i != PRIDEXIST) {
234 fprintf(stderr, "Error while creating %s: %s\n",
235 name, error_message(i));
238 i = PR_SetFieldsEntry(NULL, id,
239 ((flags&PRQUOTA) ? PR_SF_NGROUPS : 0)|
240 ((flags&PRACCESS) ? PR_SF_ALLBITS : 0),
241 flags >> PRIVATE_SHIFT,
244 fprintf(stderr, "Error while setting flags on %s\n", name);
248 for (i = 0; i < HASHSIZE; i++) {
249 offset = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
251 offset = display_entry(offset);
257 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
258 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
259 fprintf(stderr, "ptdump: error reading %s: %s\n",
260 pfile, sys_errlist[errno]);
263 uh = (struct ubik_hdr *)buffer;
264 if ((uh->version.epoch != uv.epoch) ||
265 (uh->version.counter != uv.counter)) {
266 fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
267 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
268 uv.epoch, uv.counter, uh->version.epoch,
269 uh->version.counter);
275 int display_entry (offset)
280 lseek (dbase_fd, offset+HDRSIZE, L_SET);
281 read(dbase_fd, &pre, sizeof(struct prentry));
285 if ((pre.flags & PRFREE) == 0) {
286 if (pre.flags & PRGRP) {
290 if (print_id(pre.id) && (flags&DO_USR))
291 fprintf(dfp, FMT_BASE,
292 pre.name, pre.flags, pre.ngroups,
293 pre.id, pre.owner, pre.creator);
297 return(nflag ? pre.nextName: pre.nextID);
306 i = grp_count++ % 1024;
308 g = (struct grp_list *)malloc(sizeof(struct grp_list));
316 void display_groups()
322 while (grp_count--) {
334 void display_group(id)
337 register int i, offset;
340 offset = ntohl(prh.idHash[IDHash(id)]);
342 lseek(dbase_fd, offset+HDRSIZE, L_SET);
343 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
344 fprintf(stderr, "ptdump: read i/o error: %s\n",
355 fprintf(dfp, FMT_BASE,
356 pre.name, pre.flags, pre.ngroups,
357 pre.id, pre.owner, pre.creator);
361 if ((flags&DO_MEM) == 0)
364 for (i=0; i<PRSIZE; i++) {
365 if ((id=pre.entries[i]) == 0)
367 if (id==PRBADID) continue;
368 if (print_id(id) || print_grp==1) {
370 fprintf(dfp, FMT_BASE,
371 pre.name, pre.flags, pre.ngroups,
372 pre.id, pre.owner, pre.creator);
375 fprintf(dfp, FMT_MEM, id_to_name(id), id);
381 lseek(dbase_fd, offset+HDRSIZE, L_SET);
382 read(dbase_fd, &prco, sizeof(struct contentry));
383 prco.next = ntohl(prco.next);
384 for (i = 0; i < COSIZE; i++) {
385 prco.entries[i] = ntohl(prco.entries[i]);
386 if ((id=prco.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);
399 if ((i == COSIZE) && prco.next)
411 pre->flags = ntohl(pre->flags);
412 pre->id = ntohl(pre->id);
413 pre->cellid = ntohl(pre->cellid);
414 pre->next = ntohl(pre->next);
415 pre->nextID = ntohl(pre->nextID);
416 pre->nextName = ntohl(pre->nextName);
417 pre->owner = ntohl(pre->owner);
418 pre->creator = ntohl(pre->creator);
419 pre->ngroups = ntohl(pre->ngroups);
420 pre->nusers = ntohl(pre->nusers);
421 pre->count = ntohl(pre->count);
422 pre->instance = ntohl(pre->instance);
423 pre->owned = ntohl(pre->owned);
424 pre->nextOwned = ntohl(pre->nextOwned);
425 pre->parent = ntohl(pre->parent);
426 pre->sibling = ntohl(pre->sibling);
427 pre->child = ntohl(pre->child);
428 for (i = 0; i < PRSIZE; i++) {
429 pre->entries[i] = ntohl(pre->entries[i]);
437 static struct prentry pre;
440 name = check_core(id);
441 if (name) return(name);
442 offset = ntohl(prh.idHash[IDHash(id)]);
444 lseek(dbase_fd, offset+HDRSIZE, L_SET);
445 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
446 fprintf(stderr, "ptdump: read i/o error: %s\n",
450 pre.id = ntohl(pre.id);
452 name = checkin(&pre);
455 offset = ntohl(pre.nextID);
463 struct hash_entry *he, *last;
467 last = (struct hash_entry *)0;
468 he = hat[IDHash(id)];
470 if (id == he->h_id) return(he->h_name);
474 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
476 fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
480 he->next = (struct hash_entry *)0;
481 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
482 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
483 else last->next = he;
490 struct hash_entry *he;
491 he = hat[IDHash(id)];
493 if (id == he->h_id) return(he->h_name);