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");
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)
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));
199 if (wflag && uv.epoch == 0 && uv.counter == 0) {
200 uv.epoch = 2; /* a ubik version of 0 or 1 has special meaning */
201 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
202 lseek(dbase_fd, 0, SEEK_SET);
203 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
204 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
205 pfile, strerror(errno));
209 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
210 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
211 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
217 initialize_PT_error_table();
222 while (fgets(buffer, sizeof(buffer), dfp)) {
223 int id, oid, cid, flags, quota, uid;
224 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
226 if (isspace(*buffer)) {
227 sscanf(buffer, "%s %d", mem, &uid);
229 for (u = usr_head; u; u = u->next)
230 if (u->uid && u->uid == uid)
233 /* Add user - deferred because it is probably foreign */
235 if (FindByID(0, uid))
239 && (flags & (PRGRP | PRQUOTA)) ==
242 code = pr_WriteEntry(0, 0, gpos, &gentry);
245 "Error setting group count on %s: %s\n",
246 name, error_message(code));
248 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
250 SYSADMINID /*oid */ ,
251 SYSADMINID /*cid */ );
254 fprintf(stderr, "Error while creating %s: %s\n",
255 u->name, error_message(code));
258 /* Add user to group */
259 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
261 } else if ((upos = FindByID(0, uid))
262 && (gpos = FindByID(0, id))) {
263 code = pr_ReadEntry(0, 0, upos, &uentry);
265 code = pr_ReadEntry(0, 0, gpos, &gentry);
267 code = AddToEntry(0, &gentry, gpos, uid);
269 code = AddToEntry(0, &uentry, upos, id);
274 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
275 name, error_message(code));
277 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
283 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
284 flags & PRGRP, oid, cid);
285 if (code == PRBADNAM) {
286 u = (struct usr_list *)malloc(sizeof(struct usr_list));
289 strcpy(u->name, name);
292 fprintf(stderr, "Error while creating %s: %s\n", name,
293 error_message(code));
294 } else if ((flags & PRACCESS)
295 || (flags & (PRGRP | PRQUOTA)) ==
297 gpos = FindByID(0, id);
298 code = pr_ReadEntry(0, 0, gpos, &gentry);
300 gentry.flags = flags;
301 gentry.ngroups = quota;
302 code = pr_WriteEntry(0, 0, gpos, &gentry);
306 "Error while setting flags on %s: %s\n", name,
307 error_message(code));
311 for (u = usr_head; u; u = u->next)
313 fprintf(stderr, "Error while creating %s: %s\n", u->name,
314 error_message(PRBADNAM));
316 for (i = 0; i < HASHSIZE; i++) {
317 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
319 upos = display_entry(upos);
325 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
326 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
327 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
331 uh = (struct ubik_hdr *)buffer;
332 if ((uh->version.epoch != uv.epoch)
333 || (uh->version.counter != uv.counter)) {
335 "pt_util: Ubik Version number changed during execution.\n");
336 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
337 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
344 display_entry(int offset)
348 lseek(dbase_fd, offset + HDRSIZE, L_SET);
349 read(dbase_fd, &pre, sizeof(struct prentry));
353 if ((pre.flags & PRFREE) == 0) {
354 if (pre.flags & PRGRP) {
358 if (print_id(pre.id) && (flags & DO_USR))
359 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
360 pre.id, pre.owner, pre.creator);
364 return (nflag ? pre.nextName : pre.nextID);
373 i = grp_count++ % 1024;
375 g = (struct grp_list *)malloc(sizeof(struct grp_list));
390 while (grp_count--) {
391 i = grp_count % 1024;
403 display_group(int id)
405 register int i, offset;
408 offset = ntohl(prh.idHash[IDHash(id)]);
410 lseek(dbase_fd, offset + HDRSIZE, L_SET);
411 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
412 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
422 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
423 pre.owner, pre.creator);
427 if ((flags & DO_MEM) == 0)
430 for (i = 0; i < PRSIZE; i++) {
431 if ((id = pre.entries[i]) == 0)
435 if (print_id(id) || print_grp == 1) {
436 if (print_grp == 0) {
437 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
438 pre.id, pre.owner, pre.creator);
441 fprintf(dfp, FMT_MEM, id_to_name(id), id);
447 lseek(dbase_fd, offset + HDRSIZE, L_SET);
448 read(dbase_fd, &prco, sizeof(struct contentry));
449 prco.next = ntohl(prco.next);
450 for (i = 0; i < COSIZE; i++) {
451 prco.entries[i] = ntohl(prco.entries[i]);
452 if ((id = prco.entries[i]) == 0)
456 if (print_id(id) || print_grp == 1) {
457 if (print_grp == 0) {
458 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
459 pre.ngroups, pre.id, pre.owner, pre.creator);
462 fprintf(dfp, FMT_MEM, id_to_name(id), id);
465 if ((i == COSIZE) && prco.next)
474 fix_pre(struct prentry *pre)
478 pre->flags = ntohl(pre->flags);
479 pre->id = ntohl(pre->id);
480 pre->cellid = ntohl(pre->cellid);
481 pre->next = ntohl(pre->next);
482 pre->nextID = ntohl(pre->nextID);
483 pre->nextName = ntohl(pre->nextName);
484 pre->owner = ntohl(pre->owner);
485 pre->creator = ntohl(pre->creator);
486 pre->ngroups = ntohl(pre->ngroups);
487 pre->nusers = ntohl(pre->nusers);
488 pre->count = ntohl(pre->count);
489 pre->instance = ntohl(pre->instance);
490 pre->owned = ntohl(pre->owned);
491 pre->nextOwned = ntohl(pre->nextOwned);
492 pre->parent = ntohl(pre->parent);
493 pre->sibling = ntohl(pre->sibling);
494 pre->child = ntohl(pre->child);
495 for (i = 0; i < PRSIZE; i++) {
496 pre->entries[i] = ntohl(pre->entries[i]);
504 static struct prentry pre;
507 name = check_core(id);
510 offset = ntohl(prh.idHash[IDHash(id)]);
512 lseek(dbase_fd, offset + HDRSIZE, L_SET);
513 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
514 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
517 pre.id = ntohl(pre.id);
519 name = checkin(&pre);
522 offset = ntohl(pre.nextID);
528 checkin(struct prentry *pre)
530 struct hash_entry *he, *last;
534 last = (struct hash_entry *)0;
535 he = hat[IDHash(id)];
542 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
544 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
548 he->next = (struct hash_entry *)0;
549 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
550 if (last == (struct hash_entry *)0)
551 hat[IDHash(id)] = he;
558 check_core(register int id)
560 struct hash_entry *he;
561 he = hat[IDHash(id)];