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 */
29 #include <afs/afsutil.h>
32 #include <netinet/in.h>
33 #define UBIK_INTERNALS
37 #include <afs/com_err.h>
42 #define IDHash(x) (abs(x) % HASHSIZE)
43 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
44 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
52 void display_groups();
58 int CommandProc(struct cmd_syndesc *, void *);
61 char h_name[PR_MAXNAMELEN];
63 struct hash_entry *next;
65 struct hash_entry *hat[HASHSIZE];
67 static struct contentry prco;
68 static struct prentry pre;
69 static struct prheader prh;
70 static struct ubik_version uv;
73 struct grp_list *next;
76 static struct grp_list *grp_head = 0;
77 static long grp_count = 0;
80 struct usr_list *next;
81 char name[PR_MAXNAMELEN];
84 static struct usr_list *usr_head = 0;
90 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
91 #define FMT_MEM " %-8s %d\n"
104 main(int argc, char **argv)
107 register struct cmd_syndesc *cs; /*Command line syntax descriptor */
108 register afs_int32 code; /*Return code */
110 cs = cmd_CreateSyntax(NULL, CommandProc, NULL,
111 "access protection database");
112 cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
113 "update prdb with contents of data file");
114 cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
115 cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
116 cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
117 "display group members");
118 cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
119 "follow name hash chains (not id hashes)");
120 cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
121 "display only system data");
122 cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
123 "display extra users/groups");
124 cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
125 cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
126 code = cmd_Dispatch(argc, argv);
133 CommandProc(register struct cmd_syndesc *a_as, void *arock)
139 struct prentry uentry, gentry;
142 char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
145 struct cmd_parmdesc *tparm;
149 if (tparm[0].items) {
152 if (tparm[1].items) {
155 if (tparm[2].items) {
158 if (tparm[3].items) {
159 flags |= (DO_GRP | DO_MEM);
161 if (tparm[4].items) {
164 if (tparm[5].items) {
167 if (tparm[6].items) {
170 if (tparm[7].items) {
171 pfile = tparm[7].items->data;
173 if (tparm[8].items) {
174 dfile = tparm[8].items->data;
178 afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
181 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
183 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
187 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
188 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
194 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
195 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
200 dfp = (wflag ? stdin : stdout);
202 uh = (struct ubik_hdr *)buffer;
203 if (ntohl(uh->magic) != UBIK_MAGIC)
204 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
205 pfile, ntohl(uh->magic), UBIK_MAGIC);
206 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
208 if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
209 uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
210 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
211 lseek(dbase_fd, 0, SEEK_SET);
212 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
213 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
214 pfile, strerror(errno));
219 /* Now that any writeback is done, swap these */
220 uv.epoch = ntohl(uv.epoch);
221 uv.counter = ntohl(uv.counter);
223 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
224 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
225 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
231 initialize_PT_error_table();
236 while (fgets(buffer, sizeof(buffer), dfp)) {
237 int id, oid, cid, flags, quota, uid;
238 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
240 if (isspace(*buffer)) {
241 sscanf(buffer, "%s %d", mem, &uid);
243 for (u = usr_head; u; u = u->next)
244 if (u->uid && u->uid == uid)
247 /* Add user - deferred because it is probably foreign */
249 if (FindByID(0, uid))
253 && (flags & (PRGRP | PRQUOTA)) ==
256 code = pr_WriteEntry(0, 0, gpos, &gentry);
259 "Error setting group count on %s: %s\n",
260 name, afs_error_message(code));
262 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
264 SYSADMINID /*oid */ ,
265 SYSADMINID /*cid */ );
268 fprintf(stderr, "Error while creating %s: %s\n",
269 u->name, afs_error_message(code));
272 /* Add user to group */
273 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
275 } else if ((upos = FindByID(0, uid))
276 && (gpos = FindByID(0, id))) {
277 code = pr_ReadEntry(0, 0, upos, &uentry);
279 code = pr_ReadEntry(0, 0, gpos, &gentry);
281 code = AddToEntry(0, &gentry, gpos, uid);
283 code = AddToEntry(0, &uentry, upos, id);
288 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
289 name, afs_error_message(code));
291 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
297 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
298 flags & PRGRP, oid, cid);
299 if (code == PRBADNAM) {
300 u = (struct usr_list *)malloc(sizeof(struct usr_list));
303 strcpy(u->name, name);
306 fprintf(stderr, "Error while creating %s: %s\n", name,
307 afs_error_message(code));
308 } else if ((flags & PRACCESS)
309 || (flags & (PRGRP | PRQUOTA)) ==
311 gpos = FindByID(0, id);
312 code = pr_ReadEntry(0, 0, gpos, &gentry);
314 gentry.flags = flags;
315 gentry.ngroups = quota;
316 code = pr_WriteEntry(0, 0, gpos, &gentry);
320 "Error while setting flags on %s: %s\n", name,
321 afs_error_message(code));
325 for (u = usr_head; u; u = u->next)
327 fprintf(stderr, "Error while creating %s: %s\n", u->name,
328 afs_error_message(PRBADNAM));
330 for (i = 0; i < HASHSIZE; i++) {
331 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
334 newpos = display_entry(upos);
335 if (newpos == upos) {
336 fprintf(stderr, "pt_util: hash error in %s chain %d\n",
337 nflag ? "name":"id", i);
347 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
348 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
349 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
353 uh = (struct ubik_hdr *)buffer;
355 uh->version.epoch = ntohl(uh->version.epoch);
356 uh->version.counter = ntohl(uh->version.counter);
358 if ((uh->version.epoch != uv.epoch)
359 || (uh->version.counter != uv.counter)) {
361 "pt_util: Ubik Version number changed during execution.\n");
362 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
363 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
370 display_entry(int offset)
374 lseek(dbase_fd, offset + HDRSIZE, L_SET);
375 read(dbase_fd, &pre, sizeof(struct prentry));
379 if ((pre.flags & PRFREE) == 0) {
380 if (pre.flags & PRGRP) {
384 if (print_id(pre.id) && (flags & DO_USR))
385 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
386 pre.id, pre.owner, pre.creator);
390 return (nflag ? pre.nextName : pre.nextID);
399 i = grp_count++ % 1024;
401 g = (struct grp_list *)malloc(sizeof(struct grp_list));
416 while (grp_count--) {
417 i = grp_count % 1024;
429 display_group(int id)
431 register int i, offset;
434 offset = ntohl(prh.idHash[IDHash(id)]);
436 lseek(dbase_fd, offset + HDRSIZE, L_SET);
437 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
438 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
448 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
449 pre.owner, pre.creator);
453 if ((flags & DO_MEM) == 0)
456 for (i = 0; i < PRSIZE; i++) {
457 if ((id = pre.entries[i]) == 0)
461 if (print_id(id) || print_grp == 1) {
462 if (print_grp == 0) {
463 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
464 pre.id, pre.owner, pre.creator);
467 fprintf(dfp, FMT_MEM, id_to_name(id), id);
473 lseek(dbase_fd, offset + HDRSIZE, L_SET);
474 read(dbase_fd, &prco, sizeof(struct contentry));
475 prco.next = ntohl(prco.next);
476 for (i = 0; i < COSIZE; i++) {
477 prco.entries[i] = ntohl(prco.entries[i]);
478 if ((id = prco.entries[i]) == 0)
482 if (print_id(id) || print_grp == 1) {
483 if (print_grp == 0) {
484 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
485 pre.ngroups, pre.id, pre.owner, pre.creator);
488 fprintf(dfp, FMT_MEM, id_to_name(id), id);
491 if ((i == COSIZE) && prco.next)
500 fix_pre(struct prentry *pre)
504 pre->flags = ntohl(pre->flags);
505 pre->id = ntohl(pre->id);
506 pre->cellid = ntohl(pre->cellid);
507 pre->next = ntohl(pre->next);
508 pre->nextID = ntohl(pre->nextID);
509 pre->nextName = ntohl(pre->nextName);
510 pre->owner = ntohl(pre->owner);
511 pre->creator = ntohl(pre->creator);
512 pre->ngroups = ntohl(pre->ngroups);
513 pre->nusers = ntohl(pre->nusers);
514 pre->count = ntohl(pre->count);
515 pre->instance = ntohl(pre->instance);
516 pre->owned = ntohl(pre->owned);
517 pre->nextOwned = ntohl(pre->nextOwned);
518 pre->parent = ntohl(pre->parent);
519 pre->sibling = ntohl(pre->sibling);
520 pre->child = ntohl(pre->child);
521 for (i = 0; i < PRSIZE; i++) {
522 pre->entries[i] = ntohl(pre->entries[i]);
530 static struct prentry pre;
533 name = check_core(id);
536 offset = ntohl(prh.idHash[IDHash(id)]);
538 lseek(dbase_fd, offset + HDRSIZE, L_SET);
539 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
540 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
543 pre.id = ntohl(pre.id);
545 name = checkin(&pre);
548 offset = ntohl(pre.nextID);
554 checkin(struct prentry *pre)
556 struct hash_entry *he, *last;
560 last = (struct hash_entry *)0;
561 he = hat[IDHash(id)];
568 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
570 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
574 he->next = (struct hash_entry *)0;
575 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
576 if (last == (struct hash_entry *)0)
577 hat[IDHash(id)] = he;
584 check_core(register int id)
586 struct hash_entry *he;
587 he = hat[IDHash(id)];