3 * pt_util: Program to dump the AFS protection server database
6 * Assumptions: We *cheat* here and read the datafile directly, ie.
7 * not going through the ubik distributed data manager.
8 * therefore the database must be quiescent for the
9 * output of this program to be valid.
12 #include <sys/types.h>
20 #include <afs/param.h>
22 #include <netinet/in.h>
23 #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)))
43 extern char *sys_errlist[];
44 #define strerror(a) sys_errlist[a]
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";
111 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
129 flags |= (DO_GRP|DO_MEM);
142 "Usage: pt_util [options] [-d data] [-p prdb]\n");
143 fputs(" Options:\n", stderr);
144 fputs(" -w Update prdb with contents of data file\n", stderr);
145 fputs(" -u Display users\n", stderr);
146 fputs(" -g Display groups\n", stderr);
147 fputs(" -m Display group members\n", stderr);
148 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
149 fputs(" -s Display only system (Moira) data\n", stderr);
150 fputs(" -x Display extra users/groups\n", stderr);
154 if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) {
155 fprintf(stderr, "pt_util: cannot open %s: %s\n",
156 pfile, sys_errlist[errno]);
159 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
160 fprintf(stderr, "pt_util: error reading %s: %s\n",
161 pfile, sys_errlist[errno]);
166 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
167 fprintf(stderr, "pt_util: error opening %s: %s\n",
168 dfile, sys_errlist[errno]);
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, sys_errlist[errno]);
189 fprintf(stderr, "Ubik Version is: %d.%d\n",
190 uv.epoch, uv.counter);
191 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
192 fprintf(stderr, "pt_util: error reading %s: %s\n",
193 pfile, sys_errlist[errno]);
198 initialize_pt_error_table();
203 while(fgets(buffer, sizeof(buffer), dfp)) {
204 int id, oid, cid, flags, quota, uid;
205 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
207 if (isspace(*buffer)) {
208 sscanf(buffer, "%s %d", mem, &uid);
210 for (u=usr_head; u; u=u->next)
211 if (u->uid && u->uid==uid) break;
213 /* Add user - deferred because it is probably foreign */
215 if (FindByID(0, uid))
218 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
220 code = pr_WriteEntry(0,0,gpos,&gentry);
222 fprintf(stderr, "Error setting group count on %s: %s\n",
223 name, error_message(code));
226 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
227 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
230 fprintf(stderr, "Error while creating %s: %s\n",
231 u->name, error_message(code));
234 /* Add user to group */
235 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
237 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
238 code = pr_ReadEntry(0,0,upos,&uentry);
239 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
240 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
241 if (!code) code = AddToEntry (0, &uentry, upos, id);
246 fprintf(stderr, "Error while adding %s to %s: %s\n",
247 mem, name, error_message(code));
249 sscanf(buffer, "%s %d/%d %d %d %d",
250 name, &flags, "a, &id, &oid, &cid);
252 str = malloc(strlen(name) + 1);
255 fprintf(stderr, "malloc failed!");
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);
274 fprintf(stderr, "Error while creating %s: %s\n",
275 name, error_message(code));
276 } else if ((flags&PRACCESS) ||
277 (flags&(PRGRP|PRQUOTA))==(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);
286 fprintf(stderr,"Error while setting flags on %s: %s\n",
287 name, error_message(code));
291 for (u=usr_head; u; u=u->next)
293 fprintf(stderr, "Error while creating %s: %s\n",
294 u->name, error_message(PRBADNAM));
296 for (i = 0; i < HASHSIZE; i++) {
297 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
299 upos = display_entry(upos);
305 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
306 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
307 fprintf(stderr, "pt_util: error reading %s: %s\n",
308 pfile, sys_errlist[errno]);
311 uh = (struct ubik_hdr *)buffer;
312 if ((uh->version.epoch != uv.epoch) ||
313 (uh->version.counter != uv.counter)) {
314 fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
315 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
316 uv.epoch, uv.counter, uh->version.epoch,
317 uh->version.counter);
323 int display_entry (offset)
328 lseek (dbase_fd, offset+HDRSIZE, L_SET);
329 read(dbase_fd, &pre, sizeof(struct prentry));
333 if ((pre.flags & PRFREE) == 0) {
334 if (pre.flags & PRGRP) {
338 if (print_id(pre.id) && (flags&DO_USR))
339 fprintf(dfp, FMT_BASE,
340 pre.name, pre.flags, pre.ngroups,
341 pre.id, pre.owner, pre.creator);
345 return(nflag ? pre.nextName: pre.nextID);
354 i = grp_count++ % 1024;
356 g = (struct grp_list *)malloc(sizeof(struct grp_list));
364 void display_groups()
370 while (grp_count--) {
382 void display_group(id)
385 register int i, offset;
388 offset = ntohl(prh.idHash[IDHash(id)]);
390 lseek(dbase_fd, offset+HDRSIZE, L_SET);
391 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
392 fprintf(stderr, "pt_util: read i/o error: %s\n",
403 fprintf(dfp, FMT_BASE,
404 pre.name, pre.flags, pre.ngroups,
405 pre.id, pre.owner, pre.creator);
409 if ((flags&DO_MEM) == 0)
412 for (i=0; i<PRSIZE; i++) {
413 if ((id=pre.entries[i]) == 0)
415 if (id==PRBADID) continue;
416 if (print_id(id) || print_grp==1) {
418 fprintf(dfp, FMT_BASE,
419 pre.name, pre.flags, pre.ngroups,
420 pre.id, pre.owner, pre.creator);
423 fprintf(dfp, FMT_MEM, id_to_name(id), id);
429 lseek(dbase_fd, offset+HDRSIZE, L_SET);
430 read(dbase_fd, &prco, sizeof(struct contentry));
431 prco.next = ntohl(prco.next);
432 for (i = 0; i < COSIZE; i++) {
433 prco.entries[i] = ntohl(prco.entries[i]);
434 if ((id=prco.entries[i]) == 0)
436 if (id==PRBADID) continue;
437 if (print_id(id) || print_grp==1) {
439 fprintf(dfp, FMT_BASE,
440 pre.name, pre.flags, pre.ngroups,
441 pre.id, pre.owner, pre.creator);
444 fprintf(dfp, FMT_MEM, id_to_name(id), id);
447 if ((i == COSIZE) && prco.next)
458 char *str = malloc(4 * strlen(pre->name) + 1);
462 fprintf(stderr, "malloc failed in fix_pre()!");
465 strvis(str, pre->name, VIS_WHITE);
466 strcpy(pre->name, str);
467 pre->flags = ntohl(pre->flags);
468 pre->id = ntohl(pre->id);
469 pre->cellid = ntohl(pre->cellid);
470 pre->next = ntohl(pre->next);
471 pre->nextID = ntohl(pre->nextID);
472 pre->nextName = ntohl(pre->nextName);
473 pre->owner = ntohl(pre->owner);
474 pre->creator = ntohl(pre->creator);
475 pre->ngroups = ntohl(pre->ngroups);
476 pre->nusers = ntohl(pre->nusers);
477 pre->count = ntohl(pre->count);
478 pre->instance = ntohl(pre->instance);
479 pre->owned = ntohl(pre->owned);
480 pre->nextOwned = ntohl(pre->nextOwned);
481 pre->parent = ntohl(pre->parent);
482 pre->sibling = ntohl(pre->sibling);
483 pre->child = ntohl(pre->child);
484 for (i = 0; i < PRSIZE; i++) {
485 pre->entries[i] = ntohl(pre->entries[i]);
493 static struct prentry pre;
496 name = check_core(id);
497 if (name) return(name);
498 offset = ntohl(prh.idHash[IDHash(id)]);
500 lseek(dbase_fd, offset+HDRSIZE, L_SET);
501 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
502 fprintf(stderr, "pt_util: read i/o error: %s\n",
506 pre.id = ntohl(pre.id);
508 name = checkin(&pre);
511 offset = ntohl(pre.nextID);
519 struct hash_entry *he, *last;
523 last = (struct hash_entry *)0;
524 he = hat[IDHash(id)];
526 if (id == he->h_id) return(he->h_name);
530 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
532 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
536 he->next = (struct hash_entry *)0;
537 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
538 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
539 else last->next = he;
546 struct hash_entry *he;
547 he = hat[IDHash(id)];
549 if (id == he->h_id) return(he->h_name);