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
36 #include <afs/com_err.h>
41 #define IDHash(x) (abs(x) % HASHSIZE)
42 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
43 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
51 void display_groups();
57 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 register struct cmd_syndesc *cs; /*Command line syntax descriptor */
107 register 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(register struct cmd_syndesc *a_as, void *arock)
137 struct prentry uentry, gentry;
140 char *pfile = "/usr/afs/db/prdb.DB0";
141 struct cmd_parmdesc *tparm;
145 if (tparm[0].items) {
148 if (tparm[1].items) {
151 if (tparm[2].items) {
154 if (tparm[3].items) {
155 flags |= (DO_GRP | DO_MEM);
157 if (tparm[4].items) {
160 if (tparm[5].items) {
163 if (tparm[6].items) {
166 if (tparm[7].items) {
167 pfile = tparm[7].items->data;
169 if (tparm[8].items) {
170 dfile = tparm[8].items->data;
173 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
175 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
179 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
180 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
186 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
187 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
192 dfp = (wflag ? stdin : stdout);
194 uh = (struct ubik_hdr *)buffer;
195 if (ntohl(uh->magic) != UBIK_MAGIC)
196 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
197 pfile, ntohl(uh->magic), UBIK_MAGIC);
198 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
200 if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
201 uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
202 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
203 lseek(dbase_fd, 0, SEEK_SET);
204 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
205 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
206 pfile, strerror(errno));
211 /* Now that any writeback is done, swap these */
212 uv.epoch = ntohl(uv.epoch);
213 uv.counter = ntohl(uv.counter);
215 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
216 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
217 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
223 initialize_PT_error_table();
228 while (fgets(buffer, sizeof(buffer), dfp)) {
229 int id, oid, cid, flags, quota, uid;
230 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
232 if (isspace(*buffer)) {
233 sscanf(buffer, "%s %d", mem, &uid);
235 for (u = usr_head; u; u = u->next)
236 if (u->uid && u->uid == uid)
239 /* Add user - deferred because it is probably foreign */
241 if (FindByID(0, uid))
245 && (flags & (PRGRP | PRQUOTA)) ==
248 code = pr_WriteEntry(0, 0, gpos, &gentry);
251 "Error setting group count on %s: %s\n",
252 name, afs_error_message(code));
254 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
256 SYSADMINID /*oid */ ,
257 SYSADMINID /*cid */ );
260 fprintf(stderr, "Error while creating %s: %s\n",
261 u->name, afs_error_message(code));
264 /* Add user to group */
265 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
267 } else if ((upos = FindByID(0, uid))
268 && (gpos = FindByID(0, id))) {
269 code = pr_ReadEntry(0, 0, upos, &uentry);
271 code = pr_ReadEntry(0, 0, gpos, &gentry);
273 code = AddToEntry(0, &gentry, gpos, uid);
275 code = AddToEntry(0, &uentry, upos, id);
280 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
281 name, afs_error_message(code));
283 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
289 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
290 flags & PRGRP, oid, cid);
291 if (code == PRBADNAM) {
292 u = (struct usr_list *)malloc(sizeof(struct usr_list));
295 strcpy(u->name, name);
298 fprintf(stderr, "Error while creating %s: %s\n", name,
299 afs_error_message(code));
300 } else if ((flags & PRACCESS)
301 || (flags & (PRGRP | PRQUOTA)) ==
303 gpos = FindByID(0, id);
304 code = pr_ReadEntry(0, 0, gpos, &gentry);
306 gentry.flags = flags;
307 gentry.ngroups = quota;
308 code = pr_WriteEntry(0, 0, gpos, &gentry);
312 "Error while setting flags on %s: %s\n", name,
313 afs_error_message(code));
317 for (u = usr_head; u; u = u->next)
319 fprintf(stderr, "Error while creating %s: %s\n", u->name,
320 afs_error_message(PRBADNAM));
322 for (i = 0; i < HASHSIZE; i++) {
323 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
326 newpos = display_entry(upos);
327 if (newpos == upos) {
328 fprintf(stderr, "pt_util: hash error in %s chain %d\n",
329 nflag ? "name":"id", i);
339 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
340 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
341 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
345 uh = (struct ubik_hdr *)buffer;
347 uh->version.epoch = ntohl(uh->version.epoch);
348 uh->version.counter = ntohl(uh->version.counter);
350 if ((uh->version.epoch != uv.epoch)
351 || (uh->version.counter != uv.counter)) {
353 "pt_util: Ubik Version number changed during execution.\n");
354 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
355 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
362 display_entry(int offset)
366 lseek(dbase_fd, offset + HDRSIZE, L_SET);
367 read(dbase_fd, &pre, sizeof(struct prentry));
371 if ((pre.flags & PRFREE) == 0) {
372 if (pre.flags & PRGRP) {
376 if (print_id(pre.id) && (flags & DO_USR))
377 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
378 pre.id, pre.owner, pre.creator);
382 return (nflag ? pre.nextName : pre.nextID);
391 i = grp_count++ % 1024;
393 g = (struct grp_list *)malloc(sizeof(struct grp_list));
408 while (grp_count--) {
409 i = grp_count % 1024;
421 display_group(int id)
423 register int i, offset;
426 offset = ntohl(prh.idHash[IDHash(id)]);
428 lseek(dbase_fd, offset + HDRSIZE, L_SET);
429 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
430 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
440 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
441 pre.owner, pre.creator);
445 if ((flags & DO_MEM) == 0)
448 for (i = 0; i < PRSIZE; i++) {
449 if ((id = pre.entries[i]) == 0)
453 if (print_id(id) || print_grp == 1) {
454 if (print_grp == 0) {
455 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
456 pre.id, pre.owner, pre.creator);
459 fprintf(dfp, FMT_MEM, id_to_name(id), id);
465 lseek(dbase_fd, offset + HDRSIZE, L_SET);
466 read(dbase_fd, &prco, sizeof(struct contentry));
467 prco.next = ntohl(prco.next);
468 for (i = 0; i < COSIZE; i++) {
469 prco.entries[i] = ntohl(prco.entries[i]);
470 if ((id = prco.entries[i]) == 0)
474 if (print_id(id) || print_grp == 1) {
475 if (print_grp == 0) {
476 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
477 pre.ngroups, pre.id, pre.owner, pre.creator);
480 fprintf(dfp, FMT_MEM, id_to_name(id), id);
483 if ((i == COSIZE) && prco.next)
492 fix_pre(struct prentry *pre)
496 pre->flags = ntohl(pre->flags);
497 pre->id = ntohl(pre->id);
498 pre->cellid = ntohl(pre->cellid);
499 pre->next = ntohl(pre->next);
500 pre->nextID = ntohl(pre->nextID);
501 pre->nextName = ntohl(pre->nextName);
502 pre->owner = ntohl(pre->owner);
503 pre->creator = ntohl(pre->creator);
504 pre->ngroups = ntohl(pre->ngroups);
505 pre->nusers = ntohl(pre->nusers);
506 pre->count = ntohl(pre->count);
507 pre->instance = ntohl(pre->instance);
508 pre->owned = ntohl(pre->owned);
509 pre->nextOwned = ntohl(pre->nextOwned);
510 pre->parent = ntohl(pre->parent);
511 pre->sibling = ntohl(pre->sibling);
512 pre->child = ntohl(pre->child);
513 for (i = 0; i < PRSIZE; i++) {
514 pre->entries[i] = ntohl(pre->entries[i]);
522 static struct prentry pre;
525 name = check_core(id);
528 offset = ntohl(prh.idHash[IDHash(id)]);
530 lseek(dbase_fd, offset + HDRSIZE, L_SET);
531 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
532 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
535 pre.id = ntohl(pre.id);
537 name = checkin(&pre);
540 offset = ntohl(pre.nextID);
546 checkin(struct prentry *pre)
548 struct hash_entry *he, *last;
552 last = (struct hash_entry *)0;
553 he = hat[IDHash(id)];
560 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
562 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
566 he->next = (struct hash_entry *)0;
567 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
568 if (last == (struct hash_entry *)0)
569 hat[IDHash(id)] = he;
576 check_core(register int id)
578 struct hash_entry *he;
579 he = hat[IDHash(id)];