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>
30 #include <netinet/in.h>
31 #define UBIK_INTERNALS
39 #define IDHash(x) (abs(x) % HASHSIZE)
40 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
41 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
48 void display_groups();
56 char h_name[PR_MAXNAMELEN];
58 struct hash_entry *next;
60 struct hash_entry *hat[HASHSIZE];
62 static struct contentry prco;
63 static struct prentry pre;
64 static struct prheader prh;
65 static struct ubik_version uv;
68 struct grp_list *next;
71 static struct grp_list *grp_head = 0;
72 static long grp_count = 0;
75 struct usr_list *next;
76 char name[PR_MAXNAMELEN];
79 static struct usr_list *usr_head = 0;
85 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
86 #define FMT_MEM " %-8s %d\n"
105 struct prentry uentry, gentry;
108 char *pfile = "/usr/afs/db/prdb.DB0";
110 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
128 flags |= (DO_GRP | DO_MEM);
140 fprintf(stderr, "Usage: pt_util [options] [-d data] [-p prdb]\n");
141 fputs(" Options:\n", stderr);
142 fputs(" -w Update prdb with contents of data file\n", stderr);
143 fputs(" -u Display users\n", stderr);
144 fputs(" -g Display groups\n", stderr);
145 fputs(" -m Display group members\n", stderr);
146 fputs(" -n Follow name hash chains (not id hashes)\n",
148 fputs(" -s Display only system data\n", stderr);
149 fputs(" -x Display extra users/groups\n", stderr);
153 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
155 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
159 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
160 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
166 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
167 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
172 dfp = (wflag ? stdin : stdout);
174 uh = (struct ubik_hdr *)buffer;
175 if (ntohl(uh->magic) != UBIK_MAGIC)
176 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
177 pfile, ntohl(uh->magic), UBIK_MAGIC);
178 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
179 if (wflag && uv.epoch == 0 && uv.counter == 0) {
180 uv.epoch = 2; /* a ubik version of 0 or 1 has special meaning */
181 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
182 lseek(dbase_fd, 0, SEEK_SET);
183 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
184 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
185 pfile, strerror(errno));
189 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
190 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
191 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
197 initialize_PT_error_table();
202 while (fgets(buffer, sizeof(buffer), dfp)) {
203 int id, oid, cid, flags, quota, uid;
204 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
206 if (isspace(*buffer)) {
207 sscanf(buffer, "%s %d", mem, &uid);
209 for (u = usr_head; u; u = u->next)
210 if (u->uid && u->uid == uid)
213 /* Add user - deferred because it is probably foreign */
215 if (FindByID(0, uid))
219 && (flags & (PRGRP | PRQUOTA)) ==
222 code = pr_WriteEntry(0, 0, gpos, &gentry);
225 "Error setting group count on %s: %s\n",
226 name, error_message(code));
229 CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
231 SYSADMINID /*oid */ ,
232 SYSADMINID /*cid */ );
235 fprintf(stderr, "Error while creating %s: %s\n",
236 u->name, error_message(code));
239 /* Add user to group */
240 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
242 } else if ((upos = FindByID(0, uid))
243 && (gpos = FindByID(0, id))) {
244 code = pr_ReadEntry(0, 0, upos, &uentry);
246 code = pr_ReadEntry(0, 0, gpos, &gentry);
248 code = AddToEntry(0, &gentry, gpos, uid);
250 code = AddToEntry(0, &uentry, upos, id);
255 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
256 name, error_message(code));
258 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, "a, &id,
264 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
265 flags & PRGRP, oid, cid);
266 if (code == PRBADNAM) {
267 u = (struct usr_list *)malloc(sizeof(struct usr_list));
270 strcpy(u->name, name);
273 fprintf(stderr, "Error while creating %s: %s\n", name,
274 error_message(code));
275 } else if ((flags & PRACCESS)
276 || (flags & (PRGRP | PRQUOTA)) ==
278 gpos = FindByID(0, id);
279 code = pr_ReadEntry(0, 0, gpos, &gentry);
281 gentry.flags = flags;
282 gentry.ngroups = quota;
283 code = pr_WriteEntry(0, 0, gpos, &gentry);
287 "Error while setting flags on %s: %s\n", name,
288 error_message(code));
292 for (u = usr_head; u; u = u->next)
294 fprintf(stderr, "Error while creating %s: %s\n", u->name,
295 error_message(PRBADNAM));
297 for (i = 0; i < HASHSIZE; i++) {
298 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
300 upos = display_entry(upos);
306 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
307 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
308 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
312 uh = (struct ubik_hdr *)buffer;
313 if ((uh->version.epoch != uv.epoch)
314 || (uh->version.counter != uv.counter)) {
316 "pt_util: Ubik Version number changed during execution.\n");
317 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
318 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
325 display_entry(offset)
330 lseek(dbase_fd, offset + HDRSIZE, L_SET);
331 read(dbase_fd, &pre, sizeof(struct prentry));
335 if ((pre.flags & PRFREE) == 0) {
336 if (pre.flags & PRGRP) {
340 if (print_id(pre.id) && (flags & DO_USR))
341 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
342 pre.id, pre.owner, pre.creator);
346 return (nflag ? pre.nextName : pre.nextID);
356 i = grp_count++ % 1024;
358 g = (struct grp_list *)malloc(sizeof(struct grp_list));
373 while (grp_count--) {
374 i = grp_count % 1024;
389 register int i, offset;
392 offset = ntohl(prh.idHash[IDHash(id)]);
394 lseek(dbase_fd, offset + HDRSIZE, L_SET);
395 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
396 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
406 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
407 pre.owner, pre.creator);
411 if ((flags & DO_MEM) == 0)
414 for (i = 0; i < PRSIZE; i++) {
415 if ((id = pre.entries[i]) == 0)
419 if (print_id(id) || print_grp == 1) {
420 if (print_grp == 0) {
421 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
422 pre.id, pre.owner, pre.creator);
425 fprintf(dfp, FMT_MEM, id_to_name(id), id);
431 lseek(dbase_fd, offset + HDRSIZE, L_SET);
432 read(dbase_fd, &prco, sizeof(struct contentry));
433 prco.next = ntohl(prco.next);
434 for (i = 0; i < COSIZE; i++) {
435 prco.entries[i] = ntohl(prco.entries[i]);
436 if ((id = prco.entries[i]) == 0)
440 if (print_id(id) || print_grp == 1) {
441 if (print_grp == 0) {
442 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
443 pre.ngroups, pre.id, pre.owner, pre.creator);
446 fprintf(dfp, FMT_MEM, id_to_name(id), id);
449 if ((i == COSIZE) && prco.next)
463 pre->flags = ntohl(pre->flags);
464 pre->id = ntohl(pre->id);
465 pre->cellid = ntohl(pre->cellid);
466 pre->next = ntohl(pre->next);
467 pre->nextID = ntohl(pre->nextID);
468 pre->nextName = ntohl(pre->nextName);
469 pre->owner = ntohl(pre->owner);
470 pre->creator = ntohl(pre->creator);
471 pre->ngroups = ntohl(pre->ngroups);
472 pre->nusers = ntohl(pre->nusers);
473 pre->count = ntohl(pre->count);
474 pre->instance = ntohl(pre->instance);
475 pre->owned = ntohl(pre->owned);
476 pre->nextOwned = ntohl(pre->nextOwned);
477 pre->parent = ntohl(pre->parent);
478 pre->sibling = ntohl(pre->sibling);
479 pre->child = ntohl(pre->child);
480 for (i = 0; i < PRSIZE; i++) {
481 pre->entries[i] = ntohl(pre->entries[i]);
490 static struct prentry pre;
493 name = check_core(id);
496 offset = ntohl(prh.idHash[IDHash(id)]);
498 lseek(dbase_fd, offset + HDRSIZE, L_SET);
499 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
500 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
503 pre.id = ntohl(pre.id);
505 name = checkin(&pre);
508 offset = ntohl(pre.nextID);
517 struct hash_entry *he, *last;
521 last = (struct hash_entry *)0;
522 he = hat[IDHash(id)];
529 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
531 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
535 he->next = (struct hash_entry *)0;
536 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
537 if (last == (struct hash_entry *)0)
538 hat[IDHash(id)] = he;
548 struct hash_entry *he;
549 he = hat[IDHash(id)];