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 <netinet/in.h>
29 #define UBIK_INTERNALS
37 #define IDHash(x) (abs(x) % HASHSIZE)
38 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
39 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
47 void display_groups();
55 char h_name[PR_MAXNAMELEN];
57 struct hash_entry *next;
59 struct hash_entry *hat[HASHSIZE];
61 static struct contentry prco;
62 static struct prentry pre;
63 static struct prheader prh;
64 static struct ubik_version uv;
67 struct grp_list *next;
70 static struct grp_list *grp_head=0;
71 static long grp_count=0;
74 struct usr_list *next;
75 char name[PR_MAXNAMELEN];
78 static struct usr_list *usr_head=0;
84 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
85 #define FMT_MEM " %-8s %d\n"
104 struct prentry uentry, gentry;
107 char *pfile = "/usr/afs/db/prdb.DB0";
109 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
127 flags |= (DO_GRP|DO_MEM);
140 "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", stderr);
147 fputs(" -s Display only system data\n", stderr);
148 fputs(" -x Display extra users/groups\n", stderr);
152 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY)|O_CREAT, 0600))
154 fprintf(stderr, "pt_util: cannot open %s: %s\n",
155 pfile, strerror(errno));
158 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
159 fprintf(stderr, "pt_util: error reading %s: %s\n",
160 pfile, strerror(errno));
165 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
166 fprintf(stderr, "pt_util: error opening %s: %s\n",
167 dfile, strerror(errno));
171 dfp = (wflag ? stdin : stdout);
173 uh = (struct ubik_hdr *)buffer;
174 if (ntohl(uh->magic) != UBIK_MAGIC)
175 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
176 pfile, ntohl(uh->magic), UBIK_MAGIC);
177 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
178 if (wflag && uv.epoch==0 && uv.counter==0) {
179 uv.epoch=2; /* a ubik version of 0 or 1 has special meaning */
180 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
181 lseek(dbase_fd, 0, SEEK_SET);
182 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
183 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
184 pfile, strerror(errno));
188 fprintf(stderr, "Ubik Version is: %d.%d\n",
189 uv.epoch, uv.counter);
190 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
191 fprintf(stderr, "pt_util: error reading %s: %s\n",
192 pfile, strerror(errno));
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) break;
212 /* Add user - deferred because it is probably foreign */
214 if (FindByID(0, uid))
217 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
219 code = pr_WriteEntry(0,0,gpos,&gentry);
221 fprintf(stderr, "Error setting group count on %s: %s\n",
222 name, error_message(code));
225 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
226 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
229 fprintf(stderr, "Error while creating %s: %s\n",
230 u->name, error_message(code));
233 /* Add user to group */
234 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
236 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
237 code = pr_ReadEntry(0,0,upos,&uentry);
238 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
239 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
240 if (!code) code = AddToEntry (0, &uentry, upos, id);
245 fprintf(stderr, "Error while adding %s to %s: %s\n",
246 mem, name, error_message(code));
248 sscanf(buffer, "%s %d/%d %d %d %d",
249 name, &flags, "a, &id, &oid, &cid);
254 code = CreateEntry(0, name, &id, 1/*idflag*/,
255 flags&PRGRP, oid, cid);
256 if (code == PRBADNAM) {
257 u = (struct usr_list *)malloc(sizeof(struct usr_list));
260 strcpy(u->name, name);
264 fprintf(stderr, "Error while creating %s: %s\n",
265 name, error_message(code));
266 } else if ((flags&PRACCESS) ||
267 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
268 gpos = FindByID(0, id);
269 code = pr_ReadEntry(0,0,gpos,&gentry);
271 gentry.flags = flags;
272 gentry.ngroups = quota;
273 code = pr_WriteEntry(0,0,gpos,&gentry);
276 fprintf(stderr,"Error while setting flags on %s: %s\n",
277 name, error_message(code));
281 for (u=usr_head; u; u=u->next)
283 fprintf(stderr, "Error while creating %s: %s\n",
284 u->name, error_message(PRBADNAM));
286 for (i = 0; i < HASHSIZE; i++) {
287 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
289 upos = display_entry(upos);
295 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
296 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
297 fprintf(stderr, "pt_util: error reading %s: %s\n",
298 pfile, strerror(errno));
301 uh = (struct ubik_hdr *)buffer;
302 if ((uh->version.epoch != uv.epoch) ||
303 (uh->version.counter != uv.counter)) {
304 fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
305 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
306 uv.epoch, uv.counter, uh->version.epoch,
307 uh->version.counter);
313 int display_entry (offset)
318 lseek (dbase_fd, offset+HDRSIZE, L_SET);
319 read(dbase_fd, &pre, sizeof(struct prentry));
323 if ((pre.flags & PRFREE) == 0) {
324 if (pre.flags & PRGRP) {
328 if (print_id(pre.id) && (flags&DO_USR))
329 fprintf(dfp, FMT_BASE,
330 pre.name, pre.flags, pre.ngroups,
331 pre.id, pre.owner, pre.creator);
335 return(nflag ? pre.nextName: pre.nextID);
344 i = grp_count++ % 1024;
346 g = (struct grp_list *)malloc(sizeof(struct grp_list));
354 void display_groups()
360 while (grp_count--) {
372 void display_group(id)
375 register int i, offset;
378 offset = ntohl(prh.idHash[IDHash(id)]);
380 lseek(dbase_fd, offset+HDRSIZE, L_SET);
381 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
382 fprintf(stderr, "pt_util: read i/o error: %s\n",
393 fprintf(dfp, FMT_BASE,
394 pre.name, pre.flags, pre.ngroups,
395 pre.id, pre.owner, pre.creator);
399 if ((flags&DO_MEM) == 0)
402 for (i=0; i<PRSIZE; i++) {
403 if ((id=pre.entries[i]) == 0)
405 if (id==PRBADID) continue;
406 if (print_id(id) || print_grp==1) {
408 fprintf(dfp, FMT_BASE,
409 pre.name, pre.flags, pre.ngroups,
410 pre.id, pre.owner, pre.creator);
413 fprintf(dfp, FMT_MEM, id_to_name(id), id);
419 lseek(dbase_fd, offset+HDRSIZE, L_SET);
420 read(dbase_fd, &prco, sizeof(struct contentry));
421 prco.next = ntohl(prco.next);
422 for (i = 0; i < COSIZE; i++) {
423 prco.entries[i] = ntohl(prco.entries[i]);
424 if ((id=prco.entries[i]) == 0)
426 if (id==PRBADID) continue;
427 if (print_id(id) || print_grp==1) {
429 fprintf(dfp, FMT_BASE,
430 pre.name, pre.flags, pre.ngroups,
431 pre.id, pre.owner, pre.creator);
434 fprintf(dfp, FMT_MEM, id_to_name(id), id);
437 if ((i == COSIZE) && prco.next)
449 pre->flags = ntohl(pre->flags);
450 pre->id = ntohl(pre->id);
451 pre->cellid = ntohl(pre->cellid);
452 pre->next = ntohl(pre->next);
453 pre->nextID = ntohl(pre->nextID);
454 pre->nextName = ntohl(pre->nextName);
455 pre->owner = ntohl(pre->owner);
456 pre->creator = ntohl(pre->creator);
457 pre->ngroups = ntohl(pre->ngroups);
458 pre->nusers = ntohl(pre->nusers);
459 pre->count = ntohl(pre->count);
460 pre->instance = ntohl(pre->instance);
461 pre->owned = ntohl(pre->owned);
462 pre->nextOwned = ntohl(pre->nextOwned);
463 pre->parent = ntohl(pre->parent);
464 pre->sibling = ntohl(pre->sibling);
465 pre->child = ntohl(pre->child);
466 for (i = 0; i < PRSIZE; i++) {
467 pre->entries[i] = ntohl(pre->entries[i]);
475 static struct prentry pre;
478 name = check_core(id);
479 if (name) return(name);
480 offset = ntohl(prh.idHash[IDHash(id)]);
482 lseek(dbase_fd, offset+HDRSIZE, L_SET);
483 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
484 fprintf(stderr, "pt_util: read i/o error: %s\n",
488 pre.id = ntohl(pre.id);
490 name = checkin(&pre);
493 offset = ntohl(pre.nextID);
501 struct hash_entry *he, *last;
505 last = (struct hash_entry *)0;
506 he = hat[IDHash(id)];
508 if (id == he->h_id) return(he->h_name);
512 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
514 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
518 he->next = (struct hash_entry *)0;
519 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
520 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
521 else last->next = he;
528 struct hash_entry *he;
529 he = hat[IDHash(id)];
531 if (id == he->h_id) return(he->h_name);