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>
21 #include <afsconfig.h>
22 #include <afs/param.h>
27 #include <netinet/in.h>
28 #define UBIK_INTERNALS
36 #define IDHash(x) (abs(x) % HASHSIZE)
37 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
38 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
46 void display_groups();
54 char h_name[PR_MAXNAMELEN];
56 struct hash_entry *next;
58 struct hash_entry *hat[HASHSIZE];
60 static struct contentry prco;
61 static struct prentry pre;
62 static struct prheader prh;
63 static struct ubik_version uv;
66 struct grp_list *next;
69 static struct grp_list *grp_head=0;
70 static long grp_count=0;
73 struct usr_list *next;
74 char name[PR_MAXNAMELEN];
77 static struct usr_list *usr_head=0;
83 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
84 #define FMT_MEM " %-8s %d\n"
103 struct prentry uentry, gentry;
106 char *pfile = "/usr/afs/db/prdb.DB0";
108 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
126 flags |= (DO_GRP|DO_MEM);
139 "Usage: pt_util [options] [-d data] [-p prdb]\n");
140 fputs(" Options:\n", stderr);
141 fputs(" -w Update prdb with contents of data file\n", stderr);
142 fputs(" -u Display users\n", stderr);
143 fputs(" -g Display groups\n", stderr);
144 fputs(" -m Display group members\n", stderr);
145 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
146 fputs(" -s Display only system data\n", stderr);
147 fputs(" -x Display extra users/groups\n", stderr);
151 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY)|O_CREAT, 0600))
153 fprintf(stderr, "pt_util: cannot open %s: %s\n",
154 pfile, strerror(errno));
157 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
158 fprintf(stderr, "pt_util: error reading %s: %s\n",
159 pfile, strerror(errno));
164 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
165 fprintf(stderr, "pt_util: error opening %s: %s\n",
166 dfile, strerror(errno));
170 dfp = (wflag ? stdin : stdout);
172 uh = (struct ubik_hdr *)buffer;
173 if (ntohl(uh->magic) != UBIK_MAGIC)
174 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
175 pfile, ntohl(uh->magic), UBIK_MAGIC);
176 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
177 if (wflag && uv.epoch==0 && uv.counter==0) {
178 uv.epoch=2; /* a ubik version of 0 or 1 has special meaning */
179 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
180 lseek(dbase_fd, 0, SEEK_SET);
181 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
182 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
183 pfile, strerror(errno));
187 fprintf(stderr, "Ubik Version is: %d.%d\n",
188 uv.epoch, uv.counter);
189 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
190 fprintf(stderr, "pt_util: error reading %s: %s\n",
191 pfile, strerror(errno));
196 initialize_pt_error_table();
201 while(fgets(buffer, sizeof(buffer), dfp)) {
202 int id, oid, cid, flags, quota, uid;
203 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
205 if (isspace(*buffer)) {
206 sscanf(buffer, "%s %d", mem, &uid);
208 for (u=usr_head; u; u=u->next)
209 if (u->uid && u->uid==uid) break;
211 /* Add user - deferred because it is probably foreign */
213 if (FindByID(0, uid))
216 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
218 code = pr_WriteEntry(0,0,gpos,&gentry);
220 fprintf(stderr, "Error setting group count on %s: %s\n",
221 name, error_message(code));
224 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
225 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
228 fprintf(stderr, "Error while creating %s: %s\n",
229 u->name, error_message(code));
232 /* Add user to group */
233 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
235 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
236 code = pr_ReadEntry(0,0,upos,&uentry);
237 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
238 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
239 if (!code) code = AddToEntry (0, &uentry, upos, id);
244 fprintf(stderr, "Error while adding %s to %s: %s\n",
245 mem, name, error_message(code));
247 sscanf(buffer, "%s %d/%d %d %d %d",
248 name, &flags, "a, &id, &oid, &cid);
253 code = CreateEntry(0, name, &id, 1/*idflag*/,
254 flags&PRGRP, oid, cid);
255 if (code == PRBADNAM) {
256 u = (struct usr_list *)malloc(sizeof(struct usr_list));
259 strcpy(u->name, name);
263 fprintf(stderr, "Error while creating %s: %s\n",
264 name, error_message(code));
265 } else if ((flags&PRACCESS) ||
266 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
267 gpos = FindByID(0, id);
268 code = pr_ReadEntry(0,0,gpos,&gentry);
270 gentry.flags = flags;
271 gentry.ngroups = quota;
272 code = pr_WriteEntry(0,0,gpos,&gentry);
275 fprintf(stderr,"Error while setting flags on %s: %s\n",
276 name, error_message(code));
280 for (u=usr_head; u; u=u->next)
282 fprintf(stderr, "Error while creating %s: %s\n",
283 u->name, error_message(PRBADNAM));
285 for (i = 0; i < HASHSIZE; i++) {
286 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
288 upos = display_entry(upos);
294 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
295 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
296 fprintf(stderr, "pt_util: error reading %s: %s\n",
297 pfile, strerror(errno));
300 uh = (struct ubik_hdr *)buffer;
301 if ((uh->version.epoch != uv.epoch) ||
302 (uh->version.counter != uv.counter)) {
303 fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
304 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
305 uv.epoch, uv.counter, uh->version.epoch,
306 uh->version.counter);
312 int display_entry (offset)
317 lseek (dbase_fd, offset+HDRSIZE, L_SET);
318 read(dbase_fd, &pre, sizeof(struct prentry));
322 if ((pre.flags & PRFREE) == 0) {
323 if (pre.flags & PRGRP) {
327 if (print_id(pre.id) && (flags&DO_USR))
328 fprintf(dfp, FMT_BASE,
329 pre.name, pre.flags, pre.ngroups,
330 pre.id, pre.owner, pre.creator);
334 return(nflag ? pre.nextName: pre.nextID);
343 i = grp_count++ % 1024;
345 g = (struct grp_list *)malloc(sizeof(struct grp_list));
353 void display_groups()
359 while (grp_count--) {
371 void display_group(id)
374 register int i, offset;
377 offset = ntohl(prh.idHash[IDHash(id)]);
379 lseek(dbase_fd, offset+HDRSIZE, L_SET);
380 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
381 fprintf(stderr, "pt_util: read i/o error: %s\n",
392 fprintf(dfp, FMT_BASE,
393 pre.name, pre.flags, pre.ngroups,
394 pre.id, pre.owner, pre.creator);
398 if ((flags&DO_MEM) == 0)
401 for (i=0; i<PRSIZE; i++) {
402 if ((id=pre.entries[i]) == 0)
404 if (id==PRBADID) continue;
405 if (print_id(id) || print_grp==1) {
407 fprintf(dfp, FMT_BASE,
408 pre.name, pre.flags, pre.ngroups,
409 pre.id, pre.owner, pre.creator);
412 fprintf(dfp, FMT_MEM, id_to_name(id), id);
418 lseek(dbase_fd, offset+HDRSIZE, L_SET);
419 read(dbase_fd, &prco, sizeof(struct contentry));
420 prco.next = ntohl(prco.next);
421 for (i = 0; i < COSIZE; i++) {
422 prco.entries[i] = ntohl(prco.entries[i]);
423 if ((id=prco.entries[i]) == 0)
425 if (id==PRBADID) continue;
426 if (print_id(id) || print_grp==1) {
428 fprintf(dfp, FMT_BASE,
429 pre.name, pre.flags, pre.ngroups,
430 pre.id, pre.owner, pre.creator);
433 fprintf(dfp, FMT_MEM, id_to_name(id), id);
436 if ((i == COSIZE) && prco.next)
448 pre->flags = ntohl(pre->flags);
449 pre->id = ntohl(pre->id);
450 pre->cellid = ntohl(pre->cellid);
451 pre->next = ntohl(pre->next);
452 pre->nextID = ntohl(pre->nextID);
453 pre->nextName = ntohl(pre->nextName);
454 pre->owner = ntohl(pre->owner);
455 pre->creator = ntohl(pre->creator);
456 pre->ngroups = ntohl(pre->ngroups);
457 pre->nusers = ntohl(pre->nusers);
458 pre->count = ntohl(pre->count);
459 pre->instance = ntohl(pre->instance);
460 pre->owned = ntohl(pre->owned);
461 pre->nextOwned = ntohl(pre->nextOwned);
462 pre->parent = ntohl(pre->parent);
463 pre->sibling = ntohl(pre->sibling);
464 pre->child = ntohl(pre->child);
465 for (i = 0; i < PRSIZE; i++) {
466 pre->entries[i] = ntohl(pre->entries[i]);
474 static struct prentry pre;
477 name = check_core(id);
478 if (name) return(name);
479 offset = ntohl(prh.idHash[IDHash(id)]);
481 lseek(dbase_fd, offset+HDRSIZE, L_SET);
482 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
483 fprintf(stderr, "pt_util: read i/o error: %s\n",
487 pre.id = ntohl(pre.id);
489 name = checkin(&pre);
492 offset = ntohl(pre.nextID);
500 struct hash_entry *he, *last;
504 last = (struct hash_entry *)0;
505 he = hat[IDHash(id)];
507 if (id == he->h_id) return(he->h_name);
511 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
513 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
517 he->next = (struct hash_entry *)0;
518 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
519 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
520 else last->next = he;
527 struct hash_entry *he;
528 he = hat[IDHash(id)];
530 if (id == he->h_id) return(he->h_name);