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 <afsconfig.h>
15 #include <afs/param.h>
22 #define L_SET SEEK_SET
26 #include <afs/com_err.h>
27 #include <afs/cmd.h> /*Command line parsing */
28 #include <afs/afsutil.h>
30 #define UBIK_INTERNALS
38 #include "ptprototypes.h"
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)))
49 static int display_entry(int);
50 static void add_group(long);
51 static void display_groups(void);
52 static void display_group(int);
53 static void fix_pre(struct prentry *);
54 static char *id_to_name(int);
55 static char *checkin(struct prentry *);
56 static char *check_core(int);
57 static int CommandProc(struct cmd_syndesc *, void *);
60 char h_name[PR_MAXNAMELEN];
62 struct hash_entry *next;
64 struct hash_entry *hat[HASHSIZE];
66 static struct contentry prco;
67 static struct prentry pre;
68 static struct prheader prh;
69 static struct ubik_version uv;
72 struct grp_list *next;
75 static struct grp_list *grp_head = 0;
76 static long grp_count = 0;
79 struct usr_list *next;
80 char name[PR_MAXNAMELEN];
83 static struct usr_list *usr_head = 0;
89 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
90 #define FMT_MEM " %-8s %d\n"
103 main(int argc, char **argv)
106 struct cmd_syndesc *cs; /*Command line syntax descriptor */
107 afs_int32 code; /*Return code */
109 cs = cmd_CreateSyntax(NULL, CommandProc, NULL,
110 "access protection database");
111 cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
112 "update prdb with contents of data file");
113 cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
114 cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
115 cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
116 "display group members");
117 cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
118 "follow name hash chains (not id hashes)");
119 cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
120 "display only system data");
121 cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
122 "display extra users/groups");
123 cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
124 cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
125 code = cmd_Dispatch(argc, argv);
132 CommandProc(struct cmd_syndesc *a_as, void *arock)
138 struct prentry uentry, gentry;
141 const char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
144 struct cmd_parmdesc *tparm;
148 if (tparm[0].items) {
151 if (tparm[1].items) {
154 if (tparm[2].items) {
157 if (tparm[3].items) {
158 flags |= (DO_GRP | DO_MEM);
160 if (tparm[4].items) {
163 if (tparm[5].items) {
166 if (tparm[6].items) {
169 if (tparm[7].items) {
170 pfile = tparm[7].items->data;
172 if (tparm[8].items) {
173 dfile = tparm[8].items->data;
177 afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
180 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
182 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
186 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
187 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
193 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
194 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
199 dfp = (wflag ? stdin : stdout);
201 uh = (struct ubik_hdr *)buffer;
202 if (ntohl(uh->magic) != UBIK_MAGIC)
203 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
204 pfile, ntohl(uh->magic), UBIK_MAGIC);
205 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
207 if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
208 uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
209 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
210 lseek(dbase_fd, 0, SEEK_SET);
211 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
212 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
213 pfile, strerror(errno));
218 /* Now that any writeback is done, swap these */
219 uv.epoch = ntohl(uv.epoch);
220 uv.counter = ntohl(uv.counter);
222 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
223 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
224 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
230 initialize_PT_error_table();
235 while (fgets(buffer, sizeof(buffer), dfp)) {
236 int id, oid, cid, flags, quota, uid;
237 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
239 if (isspace(*buffer)) {
240 sscanf(buffer, "%s %d", mem, &uid);
242 for (u = usr_head; u; u = u->next)
243 if (u->uid && u->uid == uid)
246 /* Add user - deferred because it is probably foreign */
248 if (FindByID(0, uid))
252 && (flags & (PRGRP | PRQUOTA)) ==
255 code = pr_WriteEntry(0, 0, gpos, &gentry);
258 "Error setting group count on %s: %s\n",
259 name, afs_error_message(code));
261 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
263 SYSADMINID /*oid */ ,
264 SYSADMINID /*cid */ );
267 fprintf(stderr, "Error while creating %s: %s\n",
268 u->name, afs_error_message(code));
271 /* Add user to group */
272 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
274 } else if ((upos = FindByID(0, uid))
275 && (gpos = FindByID(0, id))) {
276 code = pr_ReadEntry(0, 0, upos, &uentry);
278 code = pr_ReadEntry(0, 0, gpos, &gentry);
280 code = AddToEntry(0, &gentry, gpos, uid);
282 code = AddToEntry(0, &uentry, upos, id);
287 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
288 name, afs_error_message(code));
290 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
296 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
297 flags & PRGRP, oid, cid);
298 if (code == PRBADNAM) {
299 u = (struct usr_list *)malloc(sizeof(struct usr_list));
302 strcpy(u->name, name);
305 fprintf(stderr, "Error while creating %s: %s\n", name,
306 afs_error_message(code));
307 } else if ((flags & PRACCESS)
308 || (flags & (PRGRP | PRQUOTA)) ==
310 gpos = FindByID(0, id);
311 code = pr_ReadEntry(0, 0, gpos, &gentry);
313 gentry.flags = flags;
314 gentry.ngroups = quota;
315 code = pr_WriteEntry(0, 0, gpos, &gentry);
319 "Error while setting flags on %s: %s\n", name,
320 afs_error_message(code));
324 for (u = usr_head; u; u = u->next)
326 fprintf(stderr, "Error while creating %s: %s\n", u->name,
327 afs_error_message(PRBADNAM));
329 for (i = 0; i < HASHSIZE; i++) {
330 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
333 newpos = display_entry(upos);
334 if (newpos == upos) {
335 fprintf(stderr, "pt_util: hash error in %s chain %d\n",
336 nflag ? "name":"id", i);
346 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
347 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
348 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
352 uh = (struct ubik_hdr *)buffer;
354 uh->version.epoch = ntohl(uh->version.epoch);
355 uh->version.counter = ntohl(uh->version.counter);
357 if ((uh->version.epoch != uv.epoch)
358 || (uh->version.counter != uv.counter)) {
360 "pt_util: Ubik Version number changed during execution.\n");
361 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
362 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
369 display_entry(int offset)
371 lseek(dbase_fd, offset + HDRSIZE, L_SET);
372 read(dbase_fd, &pre, sizeof(struct prentry));
376 if ((pre.flags & PRFREE) == 0) {
377 if (pre.flags & PRGRP) {
381 if (print_id(pre.id) && (flags & DO_USR))
382 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
383 pre.id, pre.owner, pre.creator);
387 return (nflag ? pre.nextName : pre.nextID);
396 i = grp_count++ % 1024;
398 g = (struct grp_list *)malloc(sizeof(struct grp_list));
413 while (grp_count--) {
414 i = grp_count % 1024;
426 display_group(int id)
431 offset = ntohl(prh.idHash[IDHash(id)]);
433 lseek(dbase_fd, offset + HDRSIZE, L_SET);
434 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
435 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
445 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
446 pre.owner, pre.creator);
450 if ((flags & DO_MEM) == 0)
453 for (i = 0; i < PRSIZE; i++) {
454 if ((id = pre.entries[i]) == 0)
458 if (print_id(id) || print_grp == 1) {
459 if (print_grp == 0) {
460 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
461 pre.id, pre.owner, pre.creator);
464 fprintf(dfp, FMT_MEM, id_to_name(id), id);
470 lseek(dbase_fd, offset + HDRSIZE, L_SET);
471 read(dbase_fd, &prco, sizeof(struct contentry));
472 prco.next = ntohl(prco.next);
473 for (i = 0; i < COSIZE; i++) {
474 prco.entries[i] = ntohl(prco.entries[i]);
475 if ((id = prco.entries[i]) == 0)
479 if (print_id(id) || print_grp == 1) {
480 if (print_grp == 0) {
481 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
482 pre.ngroups, pre.id, pre.owner, pre.creator);
485 fprintf(dfp, FMT_MEM, id_to_name(id), id);
488 if ((i == COSIZE) && prco.next)
497 fix_pre(struct prentry *pre)
501 pre->flags = ntohl(pre->flags);
502 pre->id = ntohl(pre->id);
503 pre->cellid = ntohl(pre->cellid);
504 pre->next = ntohl(pre->next);
505 pre->nextID = ntohl(pre->nextID);
506 pre->nextName = ntohl(pre->nextName);
507 pre->owner = ntohl(pre->owner);
508 pre->creator = ntohl(pre->creator);
509 pre->ngroups = ntohl(pre->ngroups);
510 pre->nusers = ntohl(pre->nusers);
511 pre->count = ntohl(pre->count);
512 pre->instance = ntohl(pre->instance);
513 pre->owned = ntohl(pre->owned);
514 pre->nextOwned = ntohl(pre->nextOwned);
515 pre->parent = ntohl(pre->parent);
516 pre->sibling = ntohl(pre->sibling);
517 pre->child = ntohl(pre->child);
518 for (i = 0; i < PRSIZE; i++) {
519 pre->entries[i] = ntohl(pre->entries[i]);
527 static struct prentry pre;
530 name = check_core(id);
533 offset = ntohl(prh.idHash[IDHash(id)]);
535 lseek(dbase_fd, offset + HDRSIZE, L_SET);
536 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
537 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
540 pre.id = ntohl(pre.id);
542 name = checkin(&pre);
545 offset = ntohl(pre.nextID);
551 checkin(struct prentry *pre)
553 struct hash_entry *he, *last;
557 last = (struct hash_entry *)0;
558 he = hat[IDHash(id)];
565 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
567 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
571 he->next = (struct hash_entry *)0;
572 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
573 if (last == (struct hash_entry *)0)
574 hat[IDHash(id)] = he;
583 struct hash_entry *he;
584 he = hat[IDHash(id)];