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 <afs/param.h>
23 #include <netinet/in.h>
24 #define UBIK_INTERNALS
32 #define IDHash(x) (abs(x) % HASHSIZE)
33 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
34 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
42 void display_groups();
50 char h_name[PR_MAXNAMELEN];
52 struct hash_entry *next;
54 struct hash_entry *hat[HASHSIZE];
56 static struct contentry prco;
57 static struct prentry pre;
58 static struct prheader prh;
59 static struct ubik_version uv;
62 struct grp_list *next;
65 static struct grp_list *grp_head=0;
66 static long grp_count=0;
69 struct usr_list *next;
70 char name[PR_MAXNAMELEN];
73 static struct usr_list *usr_head=0;
79 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
80 #define FMT_MEM " %-8s %d\n"
99 struct prentry uentry, gentry;
102 char *pfile = "/usr/afs/db/prdb.DB0";
104 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
122 flags |= (DO_GRP|DO_MEM);
135 "Usage: pt_util [options] [-d data] [-p prdb]\n");
136 fputs(" Options:\n", stderr);
137 fputs(" -w Update prdb with contents of data file\n", stderr);
138 fputs(" -u Display users\n", stderr);
139 fputs(" -g Display groups\n", stderr);
140 fputs(" -m Display group members\n", stderr);
141 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
142 fputs(" -s Display only system data\n", stderr);
143 fputs(" -x Display extra users/groups\n", stderr);
147 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY)|O_CREAT, 0600))
149 fprintf(stderr, "pt_util: cannot open %s: %s\n",
150 pfile, strerror(errno));
153 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
154 fprintf(stderr, "pt_util: error reading %s: %s\n",
155 pfile, strerror(errno));
160 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
161 fprintf(stderr, "pt_util: error opening %s: %s\n",
162 dfile, strerror(errno));
166 dfp = (wflag ? stdin : stdout);
168 uh = (struct ubik_hdr *)buffer;
169 if (ntohl(uh->magic) != UBIK_MAGIC)
170 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
171 pfile, ntohl(uh->magic), UBIK_MAGIC);
172 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
173 if (wflag && uv.epoch==0 && uv.counter==0) {
174 uv.epoch=2; /* a ubik version of 0 or 1 has special meaning */
175 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
176 lseek(dbase_fd, 0, SEEK_SET);
177 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
178 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
179 pfile, strerror(errno));
183 fprintf(stderr, "Ubik Version is: %d.%d\n",
184 uv.epoch, uv.counter);
185 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
186 fprintf(stderr, "pt_util: error reading %s: %s\n",
187 pfile, strerror(errno));
192 initialize_pt_error_table();
197 while(fgets(buffer, sizeof(buffer), dfp)) {
198 int id, oid, cid, flags, quota, uid;
199 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
201 if (isspace(*buffer)) {
202 sscanf(buffer, "%s %d", mem, &uid);
204 for (u=usr_head; u; u=u->next)
205 if (u->uid && u->uid==uid) break;
207 /* Add user - deferred because it is probably foreign */
209 if (FindByID(0, uid))
212 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
214 code = pr_WriteEntry(0,0,gpos,&gentry);
216 fprintf(stderr, "Error setting group count on %s: %s\n",
217 name, error_message(code));
220 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
221 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
224 fprintf(stderr, "Error while creating %s: %s\n",
225 u->name, error_message(code));
228 /* Add user to group */
229 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
231 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
232 code = pr_ReadEntry(0,0,upos,&uentry);
233 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
234 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
235 if (!code) code = AddToEntry (0, &uentry, upos, id);
240 fprintf(stderr, "Error while adding %s to %s: %s\n",
241 mem, name, error_message(code));
243 sscanf(buffer, "%s %d/%d %d %d %d",
244 name, &flags, "a, &id, &oid, &cid);
249 code = CreateEntry(0, name, &id, 1/*idflag*/,
250 flags&PRGRP, oid, cid);
251 if (code == PRBADNAM) {
252 u = (struct usr_list *)malloc(sizeof(struct usr_list));
255 strcpy(u->name, name);
259 fprintf(stderr, "Error while creating %s: %s\n",
260 name, error_message(code));
261 } else if ((flags&PRACCESS) ||
262 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
263 gpos = FindByID(0, id);
264 code = pr_ReadEntry(0,0,gpos,&gentry);
266 gentry.flags = flags;
267 gentry.ngroups = quota;
268 code = pr_WriteEntry(0,0,gpos,&gentry);
271 fprintf(stderr,"Error while setting flags on %s: %s\n",
272 name, error_message(code));
276 for (u=usr_head; u; u=u->next)
278 fprintf(stderr, "Error while creating %s: %s\n",
279 u->name, error_message(PRBADNAM));
281 for (i = 0; i < HASHSIZE; i++) {
282 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
284 upos = display_entry(upos);
290 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
291 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
292 fprintf(stderr, "pt_util: error reading %s: %s\n",
293 pfile, strerror(errno));
296 uh = (struct ubik_hdr *)buffer;
297 if ((uh->version.epoch != uv.epoch) ||
298 (uh->version.counter != uv.counter)) {
299 fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
300 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
301 uv.epoch, uv.counter, uh->version.epoch,
302 uh->version.counter);
308 int display_entry (offset)
313 lseek (dbase_fd, offset+HDRSIZE, L_SET);
314 read(dbase_fd, &pre, sizeof(struct prentry));
318 if ((pre.flags & PRFREE) == 0) {
319 if (pre.flags & PRGRP) {
323 if (print_id(pre.id) && (flags&DO_USR))
324 fprintf(dfp, FMT_BASE,
325 pre.name, pre.flags, pre.ngroups,
326 pre.id, pre.owner, pre.creator);
330 return(nflag ? pre.nextName: pre.nextID);
339 i = grp_count++ % 1024;
341 g = (struct grp_list *)malloc(sizeof(struct grp_list));
349 void display_groups()
355 while (grp_count--) {
367 void display_group(id)
370 register int i, offset;
373 offset = ntohl(prh.idHash[IDHash(id)]);
375 lseek(dbase_fd, offset+HDRSIZE, L_SET);
376 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
377 fprintf(stderr, "pt_util: read i/o error: %s\n",
388 fprintf(dfp, FMT_BASE,
389 pre.name, pre.flags, pre.ngroups,
390 pre.id, pre.owner, pre.creator);
394 if ((flags&DO_MEM) == 0)
397 for (i=0; i<PRSIZE; i++) {
398 if ((id=pre.entries[i]) == 0)
400 if (id==PRBADID) continue;
401 if (print_id(id) || print_grp==1) {
403 fprintf(dfp, FMT_BASE,
404 pre.name, pre.flags, pre.ngroups,
405 pre.id, pre.owner, pre.creator);
408 fprintf(dfp, FMT_MEM, id_to_name(id), id);
414 lseek(dbase_fd, offset+HDRSIZE, L_SET);
415 read(dbase_fd, &prco, sizeof(struct contentry));
416 prco.next = ntohl(prco.next);
417 for (i = 0; i < COSIZE; i++) {
418 prco.entries[i] = ntohl(prco.entries[i]);
419 if ((id=prco.entries[i]) == 0)
421 if (id==PRBADID) continue;
422 if (print_id(id) || print_grp==1) {
424 fprintf(dfp, FMT_BASE,
425 pre.name, pre.flags, pre.ngroups,
426 pre.id, pre.owner, pre.creator);
429 fprintf(dfp, FMT_MEM, id_to_name(id), id);
432 if ((i == COSIZE) && prco.next)
444 pre->flags = ntohl(pre->flags);
445 pre->id = ntohl(pre->id);
446 pre->cellid = ntohl(pre->cellid);
447 pre->next = ntohl(pre->next);
448 pre->nextID = ntohl(pre->nextID);
449 pre->nextName = ntohl(pre->nextName);
450 pre->owner = ntohl(pre->owner);
451 pre->creator = ntohl(pre->creator);
452 pre->ngroups = ntohl(pre->ngroups);
453 pre->nusers = ntohl(pre->nusers);
454 pre->count = ntohl(pre->count);
455 pre->instance = ntohl(pre->instance);
456 pre->owned = ntohl(pre->owned);
457 pre->nextOwned = ntohl(pre->nextOwned);
458 pre->parent = ntohl(pre->parent);
459 pre->sibling = ntohl(pre->sibling);
460 pre->child = ntohl(pre->child);
461 for (i = 0; i < PRSIZE; i++) {
462 pre->entries[i] = ntohl(pre->entries[i]);
470 static struct prentry pre;
473 name = check_core(id);
474 if (name) return(name);
475 offset = ntohl(prh.idHash[IDHash(id)]);
477 lseek(dbase_fd, offset+HDRSIZE, L_SET);
478 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
479 fprintf(stderr, "pt_util: read i/o error: %s\n",
483 pre.id = ntohl(pre.id);
485 name = checkin(&pre);
488 offset = ntohl(pre.nextID);
496 struct hash_entry *he, *last;
500 last = (struct hash_entry *)0;
501 he = hat[IDHash(id)];
503 if (id == he->h_id) return(he->h_name);
507 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
509 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
513 he->next = (struct hash_entry *)0;
514 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
515 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
516 else last->next = he;
523 struct hash_entry *he;
524 he = hat[IDHash(id)];
526 if (id == he->h_id) return(he->h_name);