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>
18 #include <sys/types.h>
25 #define L_SET SEEK_SET
32 #include <afsconfig.h>
33 #include <afs/param.h>
34 #include <afs/com_err.h>
37 #include <afs/cmd.h> /*Command line parsing */
38 #include <afs/afsutil.h>
42 #include <netinet/in.h>
44 #define UBIK_INTERNALS
48 #include <afs/com_err.h>
52 #include "ptprototypes.h"
54 #define IDHash(x) (abs(x) % HASHSIZE)
55 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
56 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
63 static int display_entry(int);
64 static void add_group(long);
65 static void display_groups(void);
66 static void display_group(int);
67 static void fix_pre(struct prentry *);
68 static char *id_to_name(int);
69 static char *checkin(struct prentry *);
70 static char *check_core(int);
71 static int CommandProc(struct cmd_syndesc *, void *);
74 char h_name[PR_MAXNAMELEN];
76 struct hash_entry *next;
78 struct hash_entry *hat[HASHSIZE];
80 static struct contentry prco;
81 static struct prentry pre;
82 static struct prheader prh;
83 static struct ubik_version uv;
86 struct grp_list *next;
89 static struct grp_list *grp_head = 0;
90 static long grp_count = 0;
93 struct usr_list *next;
94 char name[PR_MAXNAMELEN];
97 static struct usr_list *usr_head = 0;
103 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
104 #define FMT_MEM " %-8s %d\n"
117 main(int argc, char **argv)
120 struct cmd_syndesc *cs; /*Command line syntax descriptor */
121 afs_int32 code; /*Return code */
123 cs = cmd_CreateSyntax(NULL, CommandProc, NULL,
124 "access protection database");
125 cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
126 "update prdb with contents of data file");
127 cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
128 cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
129 cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
130 "display group members");
131 cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
132 "follow name hash chains (not id hashes)");
133 cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
134 "display only system data");
135 cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
136 "display extra users/groups");
137 cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
138 cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
139 code = cmd_Dispatch(argc, argv);
146 CommandProc(struct cmd_syndesc *a_as, void *arock)
152 struct prentry uentry, gentry;
155 const char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
158 struct cmd_parmdesc *tparm;
162 if (tparm[0].items) {
165 if (tparm[1].items) {
168 if (tparm[2].items) {
171 if (tparm[3].items) {
172 flags |= (DO_GRP | DO_MEM);
174 if (tparm[4].items) {
177 if (tparm[5].items) {
180 if (tparm[6].items) {
183 if (tparm[7].items) {
184 pfile = tparm[7].items->data;
186 if (tparm[8].items) {
187 dfile = tparm[8].items->data;
191 afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
194 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
196 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
200 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
201 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
207 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
208 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
213 dfp = (wflag ? stdin : stdout);
215 uh = (struct ubik_hdr *)buffer;
216 if (ntohl(uh->magic) != UBIK_MAGIC)
217 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
218 pfile, ntohl(uh->magic), UBIK_MAGIC);
219 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
221 if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
222 uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
223 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
224 lseek(dbase_fd, 0, SEEK_SET);
225 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
226 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
227 pfile, strerror(errno));
232 /* Now that any writeback is done, swap these */
233 uv.epoch = ntohl(uv.epoch);
234 uv.counter = ntohl(uv.counter);
236 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
237 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
238 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
244 initialize_PT_error_table();
249 while (fgets(buffer, sizeof(buffer), dfp)) {
250 int id, oid, cid, flags, quota, uid;
251 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
253 if (isspace(*buffer)) {
254 sscanf(buffer, "%s %d", mem, &uid);
256 for (u = usr_head; u; u = u->next)
257 if (u->uid && u->uid == uid)
260 /* Add user - deferred because it is probably foreign */
262 if (FindByID(0, uid))
266 && (flags & (PRGRP | PRQUOTA)) ==
269 code = pr_WriteEntry(0, 0, gpos, &gentry);
272 "Error setting group count on %s: %s\n",
273 name, afs_error_message(code));
275 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
277 SYSADMINID /*oid */ ,
278 SYSADMINID /*cid */ );
281 fprintf(stderr, "Error while creating %s: %s\n",
282 u->name, afs_error_message(code));
285 /* Add user to group */
286 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
288 } else if ((upos = FindByID(0, uid))
289 && (gpos = FindByID(0, id))) {
290 code = pr_ReadEntry(0, 0, upos, &uentry);
292 code = pr_ReadEntry(0, 0, gpos, &gentry);
294 code = AddToEntry(0, &gentry, gpos, uid);
296 code = AddToEntry(0, &uentry, upos, id);
301 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
302 name, afs_error_message(code));
304 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
310 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
311 flags & PRGRP, oid, cid);
312 if (code == PRBADNAM) {
313 u = (struct usr_list *)malloc(sizeof(struct usr_list));
316 strcpy(u->name, name);
319 fprintf(stderr, "Error while creating %s: %s\n", name,
320 afs_error_message(code));
321 } else if ((flags & PRACCESS)
322 || (flags & (PRGRP | PRQUOTA)) ==
324 gpos = FindByID(0, id);
325 code = pr_ReadEntry(0, 0, gpos, &gentry);
327 gentry.flags = flags;
328 gentry.ngroups = quota;
329 code = pr_WriteEntry(0, 0, gpos, &gentry);
333 "Error while setting flags on %s: %s\n", name,
334 afs_error_message(code));
338 for (u = usr_head; u; u = u->next)
340 fprintf(stderr, "Error while creating %s: %s\n", u->name,
341 afs_error_message(PRBADNAM));
343 for (i = 0; i < HASHSIZE; i++) {
344 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
347 newpos = display_entry(upos);
348 if (newpos == upos) {
349 fprintf(stderr, "pt_util: hash error in %s chain %d\n",
350 nflag ? "name":"id", i);
360 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
361 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
362 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
366 uh = (struct ubik_hdr *)buffer;
368 uh->version.epoch = ntohl(uh->version.epoch);
369 uh->version.counter = ntohl(uh->version.counter);
371 if ((uh->version.epoch != uv.epoch)
372 || (uh->version.counter != uv.counter)) {
374 "pt_util: Ubik Version number changed during execution.\n");
375 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
376 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
383 display_entry(int offset)
385 lseek(dbase_fd, offset + HDRSIZE, L_SET);
386 read(dbase_fd, &pre, sizeof(struct prentry));
390 if ((pre.flags & PRFREE) == 0) {
391 if (pre.flags & PRGRP) {
395 if (print_id(pre.id) && (flags & DO_USR))
396 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
397 pre.id, pre.owner, pre.creator);
401 return (nflag ? pre.nextName : pre.nextID);
410 i = grp_count++ % 1024;
412 g = (struct grp_list *)malloc(sizeof(struct grp_list));
427 while (grp_count--) {
428 i = grp_count % 1024;
440 display_group(int id)
445 offset = ntohl(prh.idHash[IDHash(id)]);
447 lseek(dbase_fd, offset + HDRSIZE, L_SET);
448 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
449 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
459 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
460 pre.owner, pre.creator);
464 if ((flags & DO_MEM) == 0)
467 for (i = 0; i < PRSIZE; i++) {
468 if ((id = pre.entries[i]) == 0)
472 if (print_id(id) || print_grp == 1) {
473 if (print_grp == 0) {
474 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
475 pre.id, pre.owner, pre.creator);
478 fprintf(dfp, FMT_MEM, id_to_name(id), id);
484 lseek(dbase_fd, offset + HDRSIZE, L_SET);
485 read(dbase_fd, &prco, sizeof(struct contentry));
486 prco.next = ntohl(prco.next);
487 for (i = 0; i < COSIZE; i++) {
488 prco.entries[i] = ntohl(prco.entries[i]);
489 if ((id = prco.entries[i]) == 0)
493 if (print_id(id) || print_grp == 1) {
494 if (print_grp == 0) {
495 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
496 pre.ngroups, pre.id, pre.owner, pre.creator);
499 fprintf(dfp, FMT_MEM, id_to_name(id), id);
502 if ((i == COSIZE) && prco.next)
511 fix_pre(struct prentry *pre)
515 pre->flags = ntohl(pre->flags);
516 pre->id = ntohl(pre->id);
517 pre->cellid = ntohl(pre->cellid);
518 pre->next = ntohl(pre->next);
519 pre->nextID = ntohl(pre->nextID);
520 pre->nextName = ntohl(pre->nextName);
521 pre->owner = ntohl(pre->owner);
522 pre->creator = ntohl(pre->creator);
523 pre->ngroups = ntohl(pre->ngroups);
524 pre->nusers = ntohl(pre->nusers);
525 pre->count = ntohl(pre->count);
526 pre->instance = ntohl(pre->instance);
527 pre->owned = ntohl(pre->owned);
528 pre->nextOwned = ntohl(pre->nextOwned);
529 pre->parent = ntohl(pre->parent);
530 pre->sibling = ntohl(pre->sibling);
531 pre->child = ntohl(pre->child);
532 for (i = 0; i < PRSIZE; i++) {
533 pre->entries[i] = ntohl(pre->entries[i]);
541 static struct prentry pre;
544 name = check_core(id);
547 offset = ntohl(prh.idHash[IDHash(id)]);
549 lseek(dbase_fd, offset + HDRSIZE, L_SET);
550 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
551 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
554 pre.id = ntohl(pre.id);
556 name = checkin(&pre);
559 offset = ntohl(pre.nextID);
565 checkin(struct prentry *pre)
567 struct hash_entry *he, *last;
571 last = (struct hash_entry *)0;
572 he = hat[IDHash(id)];
579 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
581 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
585 he->next = (struct hash_entry *)0;
586 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
587 if (last == (struct hash_entry *)0)
588 hat[IDHash(id)] = he;
597 struct hash_entry *he;
598 he = hat[IDHash(id)];