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>
22 #include <afsconfig.h>
23 #include <afs/param.h>
28 #include <afs/cmd.h> /*Command line parsing */
31 #include <netinet/in.h>
32 #define UBIK_INTERNALS
40 #define IDHash(x) (abs(x) % HASHSIZE)
41 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
42 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
50 void display_groups();
56 int CommandProc(struct cmd_syndesc *);
59 char h_name[PR_MAXNAMELEN];
61 struct hash_entry *next;
63 struct hash_entry *hat[HASHSIZE];
65 static struct contentry prco;
66 static struct prentry pre;
67 static struct prheader prh;
68 static struct ubik_version uv;
71 struct grp_list *next;
74 static struct grp_list *grp_head = 0;
75 static long grp_count = 0;
78 struct usr_list *next;
79 char name[PR_MAXNAMELEN];
82 static struct usr_list *usr_head = 0;
88 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
89 #define FMT_MEM " %-8s %d\n"
102 main(int argc, char **argv)
105 register struct cmd_syndesc *cs; /*Command line syntax descriptor */
106 register afs_int32 code; /*Return code */
108 cs = cmd_CreateSyntax((char *)0, CommandProc, 0,
109 "access protection database");
110 cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
111 "update prdb with contents of data file");
112 cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
113 cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
114 cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
115 "display group members");
116 cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
117 "follow name hash chains (not id hashes)");
118 cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
119 "display only system data");
120 cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
121 "display extra users/groups");
122 cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
123 cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
124 code = cmd_Dispatch(argc, argv);
131 CommandProc(register struct cmd_syndesc *a_as)
136 struct prentry uentry, gentry;
139 char *pfile = "/usr/afs/db/prdb.DB0";
140 struct cmd_parmdesc *tparm;
144 if (tparm[0].items) {
147 if (tparm[1].items) {
150 if (tparm[2].items) {
153 if (tparm[3].items) {
154 flags |= (DO_GRP | DO_MEM);
156 if (tparm[4].items) {
159 if (tparm[5].items) {
162 if (tparm[6].items) {
165 if (tparm[7].items) {
166 pfile = tparm[7].items->data;
168 if (tparm[8].items) {
169 dfile = tparm[8].items->data;
172 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
174 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
178 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
179 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
185 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
186 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
191 dfp = (wflag ? stdin : stdout);
193 uh = (struct ubik_hdr *)buffer;
194 if (ntohl(uh->magic) != UBIK_MAGIC)
195 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
196 pfile, ntohl(uh->magic), UBIK_MAGIC);
197 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
198 if (wflag && uv.epoch == 0 && uv.counter == 0) {
199 uv.epoch = 2; /* a ubik version of 0 or 1 has special meaning */
200 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
201 lseek(dbase_fd, 0, SEEK_SET);
202 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
203 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
204 pfile, strerror(errno));
208 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
209 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
210 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
216 initialize_PT_error_table();
221 while (fgets(buffer, sizeof(buffer), dfp)) {
222 int id, oid, cid, flags, quota, uid;
223 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
225 if (isspace(*buffer)) {
226 sscanf(buffer, "%s %d", mem, &uid);
228 for (u = usr_head; u; u = u->next)
229 if (u->uid && u->uid == uid)
232 /* Add user - deferred because it is probably foreign */
234 if (FindByID(0, uid))
238 && (flags & (PRGRP | PRQUOTA)) ==
241 code = pr_WriteEntry(0, 0, gpos, &gentry);
244 "Error setting group count on %s: %s\n",
245 name, afs_error_message(code));
247 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
249 SYSADMINID /*oid */ ,
250 SYSADMINID /*cid */ );
253 fprintf(stderr, "Error while creating %s: %s\n",
254 u->name, afs_error_message(code));
257 /* Add user to group */
258 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
260 } else if ((upos = FindByID(0, uid))
261 && (gpos = FindByID(0, id))) {
262 code = pr_ReadEntry(0, 0, upos, &uentry);
264 code = pr_ReadEntry(0, 0, gpos, &gentry);
266 code = AddToEntry(0, &gentry, gpos, uid);
268 code = AddToEntry(0, &uentry, upos, id);
273 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
274 name, afs_error_message(code));
276 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
282 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
283 flags & PRGRP, oid, cid);
284 if (code == PRBADNAM) {
285 u = (struct usr_list *)malloc(sizeof(struct usr_list));
288 strcpy(u->name, name);
291 fprintf(stderr, "Error while creating %s: %s\n", name,
292 afs_error_message(code));
293 } else if ((flags & PRACCESS)
294 || (flags & (PRGRP | PRQUOTA)) ==
296 gpos = FindByID(0, id);
297 code = pr_ReadEntry(0, 0, gpos, &gentry);
299 gentry.flags = flags;
300 gentry.ngroups = quota;
301 code = pr_WriteEntry(0, 0, gpos, &gentry);
305 "Error while setting flags on %s: %s\n", name,
306 afs_error_message(code));
310 for (u = usr_head; u; u = u->next)
312 fprintf(stderr, "Error while creating %s: %s\n", u->name,
313 afs_error_message(PRBADNAM));
315 for (i = 0; i < HASHSIZE; i++) {
316 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
318 upos = display_entry(upos);
324 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
325 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
326 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
330 uh = (struct ubik_hdr *)buffer;
331 if ((uh->version.epoch != uv.epoch)
332 || (uh->version.counter != uv.counter)) {
334 "pt_util: Ubik Version number changed during execution.\n");
335 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
336 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
343 display_entry(int offset)
347 lseek(dbase_fd, offset + HDRSIZE, L_SET);
348 read(dbase_fd, &pre, sizeof(struct prentry));
352 if ((pre.flags & PRFREE) == 0) {
353 if (pre.flags & PRGRP) {
357 if (print_id(pre.id) && (flags & DO_USR))
358 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
359 pre.id, pre.owner, pre.creator);
363 return (nflag ? pre.nextName : pre.nextID);
372 i = grp_count++ % 1024;
374 g = (struct grp_list *)malloc(sizeof(struct grp_list));
389 while (grp_count--) {
390 i = grp_count % 1024;
402 display_group(int id)
404 register int i, offset;
407 offset = ntohl(prh.idHash[IDHash(id)]);
409 lseek(dbase_fd, offset + HDRSIZE, L_SET);
410 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
411 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
421 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
422 pre.owner, pre.creator);
426 if ((flags & DO_MEM) == 0)
429 for (i = 0; i < PRSIZE; i++) {
430 if ((id = pre.entries[i]) == 0)
434 if (print_id(id) || print_grp == 1) {
435 if (print_grp == 0) {
436 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
437 pre.id, pre.owner, pre.creator);
440 fprintf(dfp, FMT_MEM, id_to_name(id), id);
446 lseek(dbase_fd, offset + HDRSIZE, L_SET);
447 read(dbase_fd, &prco, sizeof(struct contentry));
448 prco.next = ntohl(prco.next);
449 for (i = 0; i < COSIZE; i++) {
450 prco.entries[i] = ntohl(prco.entries[i]);
451 if ((id = prco.entries[i]) == 0)
455 if (print_id(id) || print_grp == 1) {
456 if (print_grp == 0) {
457 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
458 pre.ngroups, pre.id, pre.owner, pre.creator);
461 fprintf(dfp, FMT_MEM, id_to_name(id), id);
464 if ((i == COSIZE) && prco.next)
473 fix_pre(struct prentry *pre)
477 pre->flags = ntohl(pre->flags);
478 pre->id = ntohl(pre->id);
479 pre->cellid = ntohl(pre->cellid);
480 pre->next = ntohl(pre->next);
481 pre->nextID = ntohl(pre->nextID);
482 pre->nextName = ntohl(pre->nextName);
483 pre->owner = ntohl(pre->owner);
484 pre->creator = ntohl(pre->creator);
485 pre->ngroups = ntohl(pre->ngroups);
486 pre->nusers = ntohl(pre->nusers);
487 pre->count = ntohl(pre->count);
488 pre->instance = ntohl(pre->instance);
489 pre->owned = ntohl(pre->owned);
490 pre->nextOwned = ntohl(pre->nextOwned);
491 pre->parent = ntohl(pre->parent);
492 pre->sibling = ntohl(pre->sibling);
493 pre->child = ntohl(pre->child);
494 for (i = 0; i < PRSIZE; i++) {
495 pre->entries[i] = ntohl(pre->entries[i]);
503 static struct prentry pre;
506 name = check_core(id);
509 offset = ntohl(prh.idHash[IDHash(id)]);
511 lseek(dbase_fd, offset + HDRSIZE, L_SET);
512 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
513 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
516 pre.id = ntohl(pre.id);
518 name = checkin(&pre);
521 offset = ntohl(pre.nextID);
527 checkin(struct prentry *pre)
529 struct hash_entry *he, *last;
533 last = (struct hash_entry *)0;
534 he = hat[IDHash(id)];
541 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
543 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
547 he->next = (struct hash_entry *)0;
548 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
549 if (last == (struct hash_entry *)0)
550 hat[IDHash(id)] = he;
557 check_core(register int id)
559 struct hash_entry *he;
560 he = hat[IDHash(id)];