5 * pt_util: Program to dump the AFS protection server database
8 * Assumptions: We *cheat* here and read the datafile directly, ie.
9 * not going through the ubik distributed data manager.
10 * therefore the database must be quiescent for the
11 * output of this program to be valid.
14 #include <sys/types.h>
21 #define L_SET SEEK_SET
28 #include <afsconfig.h>
29 #include <afs/param.h>
30 #include <afs/com_err.h>
33 #include <afs/cmd.h> /*Command line parsing */
34 #include <afs/afsutil.h>
38 #include <netinet/in.h>
40 #define UBIK_INTERNALS
44 #include <afs/com_err.h>
48 #include "ptprototypes.h"
50 #define IDHash(x) (abs(x) % HASHSIZE)
51 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
52 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
59 static int display_entry(int);
60 static void add_group(long);
61 static void display_groups(void);
62 static void display_group(int);
63 static void fix_pre(struct prentry *);
64 static char *id_to_name(int);
65 static char *checkin(struct prentry *);
66 static char *check_core(int);
67 static int CommandProc(struct cmd_syndesc *, void *);
70 char h_name[PR_MAXNAMELEN];
72 struct hash_entry *next;
74 struct hash_entry *hat[HASHSIZE];
76 static struct contentry prco;
77 static struct prentry pre;
78 static struct prheader prh;
79 static struct ubik_version uv;
82 struct grp_list *next;
85 static struct grp_list *grp_head = 0;
86 static long grp_count = 0;
89 struct usr_list *next;
90 char name[PR_MAXNAMELEN];
93 static struct usr_list *usr_head = 0;
99 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
100 #define FMT_MEM " %-8s %d\n"
113 main(int argc, char **argv)
116 register struct cmd_syndesc *cs; /*Command line syntax descriptor */
117 register afs_int32 code; /*Return code */
119 cs = cmd_CreateSyntax(NULL, CommandProc, NULL,
120 "access protection database");
121 cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
122 "update prdb with contents of data file");
123 cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
124 cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
125 cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
126 "display group members");
127 cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
128 "follow name hash chains (not id hashes)");
129 cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
130 "display only system data");
131 cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
132 "display extra users/groups");
133 cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
134 cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
135 code = cmd_Dispatch(argc, argv);
142 CommandProc(register struct cmd_syndesc *a_as, void *arock)
148 struct prentry uentry, gentry;
151 char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
154 struct cmd_parmdesc *tparm;
158 if (tparm[0].items) {
161 if (tparm[1].items) {
164 if (tparm[2].items) {
167 if (tparm[3].items) {
168 flags |= (DO_GRP | DO_MEM);
170 if (tparm[4].items) {
173 if (tparm[5].items) {
176 if (tparm[6].items) {
179 if (tparm[7].items) {
180 pfile = tparm[7].items->data;
182 if (tparm[8].items) {
183 dfile = tparm[8].items->data;
187 afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
190 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
192 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
196 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
197 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
203 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
204 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
209 dfp = (wflag ? stdin : stdout);
211 uh = (struct ubik_hdr *)buffer;
212 if (ntohl(uh->magic) != UBIK_MAGIC)
213 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
214 pfile, ntohl(uh->magic), UBIK_MAGIC);
215 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
217 if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
218 uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
219 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
220 lseek(dbase_fd, 0, SEEK_SET);
221 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
222 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
223 pfile, strerror(errno));
228 /* Now that any writeback is done, swap these */
229 uv.epoch = ntohl(uv.epoch);
230 uv.counter = ntohl(uv.counter);
232 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
233 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
234 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
240 initialize_PT_error_table();
245 while (fgets(buffer, sizeof(buffer), dfp)) {
246 int id, oid, cid, flags, quota, uid;
247 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
249 if (isspace(*buffer)) {
250 sscanf(buffer, "%s %d", mem, &uid);
252 for (u = usr_head; u; u = u->next)
253 if (u->uid && u->uid == uid)
256 /* Add user - deferred because it is probably foreign */
258 if (FindByID(0, uid))
262 && (flags & (PRGRP | PRQUOTA)) ==
265 code = pr_WriteEntry(0, 0, gpos, &gentry);
268 "Error setting group count on %s: %s\n",
269 name, afs_error_message(code));
271 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
273 SYSADMINID /*oid */ ,
274 SYSADMINID /*cid */ );
277 fprintf(stderr, "Error while creating %s: %s\n",
278 u->name, afs_error_message(code));
281 /* Add user to group */
282 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
284 } else if ((upos = FindByID(0, uid))
285 && (gpos = FindByID(0, id))) {
286 code = pr_ReadEntry(0, 0, upos, &uentry);
288 code = pr_ReadEntry(0, 0, gpos, &gentry);
290 code = AddToEntry(0, &gentry, gpos, uid);
292 code = AddToEntry(0, &uentry, upos, id);
297 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
298 name, afs_error_message(code));
300 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
306 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
307 flags & PRGRP, oid, cid);
308 if (code == PRBADNAM) {
309 u = (struct usr_list *)malloc(sizeof(struct usr_list));
312 strcpy(u->name, name);
315 fprintf(stderr, "Error while creating %s: %s\n", name,
316 afs_error_message(code));
317 } else if ((flags & PRACCESS)
318 || (flags & (PRGRP | PRQUOTA)) ==
320 gpos = FindByID(0, id);
321 code = pr_ReadEntry(0, 0, gpos, &gentry);
323 gentry.flags = flags;
324 gentry.ngroups = quota;
325 code = pr_WriteEntry(0, 0, gpos, &gentry);
329 "Error while setting flags on %s: %s\n", name,
330 afs_error_message(code));
334 for (u = usr_head; u; u = u->next)
336 fprintf(stderr, "Error while creating %s: %s\n", u->name,
337 afs_error_message(PRBADNAM));
339 for (i = 0; i < HASHSIZE; i++) {
340 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
343 newpos = display_entry(upos);
344 if (newpos == upos) {
345 fprintf(stderr, "pt_util: hash error in %s chain %d\n",
346 nflag ? "name":"id", i);
356 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
357 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
358 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
362 uh = (struct ubik_hdr *)buffer;
364 uh->version.epoch = ntohl(uh->version.epoch);
365 uh->version.counter = ntohl(uh->version.counter);
367 if ((uh->version.epoch != uv.epoch)
368 || (uh->version.counter != uv.counter)) {
370 "pt_util: Ubik Version number changed during execution.\n");
371 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
372 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
379 display_entry(int offset)
381 lseek(dbase_fd, offset + HDRSIZE, L_SET);
382 read(dbase_fd, &pre, sizeof(struct prentry));
386 if ((pre.flags & PRFREE) == 0) {
387 if (pre.flags & PRGRP) {
391 if (print_id(pre.id) && (flags & DO_USR))
392 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
393 pre.id, pre.owner, pre.creator);
397 return (nflag ? pre.nextName : pre.nextID);
406 i = grp_count++ % 1024;
408 g = (struct grp_list *)malloc(sizeof(struct grp_list));
423 while (grp_count--) {
424 i = grp_count % 1024;
436 display_group(int id)
438 register int i, offset;
441 offset = ntohl(prh.idHash[IDHash(id)]);
443 lseek(dbase_fd, offset + HDRSIZE, L_SET);
444 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
445 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
455 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
456 pre.owner, pre.creator);
460 if ((flags & DO_MEM) == 0)
463 for (i = 0; i < PRSIZE; i++) {
464 if ((id = pre.entries[i]) == 0)
468 if (print_id(id) || print_grp == 1) {
469 if (print_grp == 0) {
470 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
471 pre.id, pre.owner, pre.creator);
474 fprintf(dfp, FMT_MEM, id_to_name(id), id);
480 lseek(dbase_fd, offset + HDRSIZE, L_SET);
481 read(dbase_fd, &prco, sizeof(struct contentry));
482 prco.next = ntohl(prco.next);
483 for (i = 0; i < COSIZE; i++) {
484 prco.entries[i] = ntohl(prco.entries[i]);
485 if ((id = prco.entries[i]) == 0)
489 if (print_id(id) || print_grp == 1) {
490 if (print_grp == 0) {
491 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
492 pre.ngroups, pre.id, pre.owner, pre.creator);
495 fprintf(dfp, FMT_MEM, id_to_name(id), id);
498 if ((i == COSIZE) && prco.next)
507 fix_pre(struct prentry *pre)
511 pre->flags = ntohl(pre->flags);
512 pre->id = ntohl(pre->id);
513 pre->cellid = ntohl(pre->cellid);
514 pre->next = ntohl(pre->next);
515 pre->nextID = ntohl(pre->nextID);
516 pre->nextName = ntohl(pre->nextName);
517 pre->owner = ntohl(pre->owner);
518 pre->creator = ntohl(pre->creator);
519 pre->ngroups = ntohl(pre->ngroups);
520 pre->nusers = ntohl(pre->nusers);
521 pre->count = ntohl(pre->count);
522 pre->instance = ntohl(pre->instance);
523 pre->owned = ntohl(pre->owned);
524 pre->nextOwned = ntohl(pre->nextOwned);
525 pre->parent = ntohl(pre->parent);
526 pre->sibling = ntohl(pre->sibling);
527 pre->child = ntohl(pre->child);
528 for (i = 0; i < PRSIZE; i++) {
529 pre->entries[i] = ntohl(pre->entries[i]);
537 static struct prentry pre;
540 name = check_core(id);
543 offset = ntohl(prh.idHash[IDHash(id)]);
545 lseek(dbase_fd, offset + HDRSIZE, L_SET);
546 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
547 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
550 pre.id = ntohl(pre.id);
552 name = checkin(&pre);
555 offset = ntohl(pre.nextID);
561 checkin(struct prentry *pre)
563 struct hash_entry *he, *last;
567 last = (struct hash_entry *)0;
568 he = hat[IDHash(id)];
575 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
577 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
581 he->next = (struct hash_entry *)0;
582 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
583 if (last == (struct hash_entry *)0)
584 hat[IDHash(id)] = he;
591 check_core(register int id)
593 struct hash_entry *he;
594 he = hat[IDHash(id)];