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
32 #define IDHash(x) (abs(x) % HASHSIZE)
33 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-65535 || x>97536)) || \
34 ((flags&DO_OTR)==0 && (x>-65536 && x<97537)))
39 extern char *sys_errlist[];
40 #define strerror(a) sys_errlist[a]
44 void display_groups();
52 char h_name[PR_MAXNAMELEN];
54 struct hash_entry *next;
56 struct hash_entry *hat[HASHSIZE];
58 static struct contentry prco;
59 static struct prentry pre;
60 static struct prheader prh;
61 static struct ubik_version uv;
64 struct grp_list *next;
67 static struct grp_list *grp_head=0;
68 static long grp_count=0;
71 struct usr_list *next;
72 char name[PR_MAXNAMELEN];
75 static struct usr_list *usr_head=0;
81 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
82 #define FMT_MEM " %-8s %d\n"
101 struct prentry uentry, gentry;
104 char *pfile = "/usr/afs/db/prdb.DB0";
107 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
125 flags |= (DO_GRP|DO_MEM);
138 "Usage: pt_util [options] [-d data] [-p prdb]\n");
139 fputs(" Options:\n", stderr);
140 fputs(" -w Update prdb with contents of data file\n", stderr);
141 fputs(" -u Display users\n", stderr);
142 fputs(" -g Display groups\n", stderr);
143 fputs(" -m Display group members\n", stderr);
144 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
145 fputs(" -s Display only system (Moira) data\n", stderr);
146 fputs(" -x Display extra users/groups\n", stderr);
150 if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) {
151 fprintf(stderr, "pt_util: cannot open %s: %s\n",
152 pfile, sys_errlist[errno]);
155 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
156 fprintf(stderr, "pt_util: error reading %s: %s\n",
157 pfile, sys_errlist[errno]);
162 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
163 fprintf(stderr, "pt_util: error opening %s: %s\n",
164 dfile, sys_errlist[errno]);
168 dfp = (wflag ? stdin : stdout);
170 uh = (struct ubik_hdr *)buffer;
171 if (ntohl(uh->magic) != UBIK_MAGIC)
172 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
173 pfile, ntohl(uh->magic), UBIK_MAGIC);
174 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
175 if (wflag && uv.epoch==0 && uv.counter==0) {
176 uv.epoch=2; /* a ubik version of 0 or 1 has special meaning */
177 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
178 lseek(dbase_fd, 0, SEEK_SET);
179 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
180 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
181 pfile, sys_errlist[errno]);
185 fprintf(stderr, "Ubik Version is: %d.%d\n",
186 uv.epoch, uv.counter);
187 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
188 fprintf(stderr, "pt_util: error reading %s: %s\n",
189 pfile, sys_errlist[errno]);
194 initialize_pt_error_table();
199 while(fgets(buffer, sizeof(buffer), dfp)) {
200 int id, oid, cid, flags, quota, uid;
201 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
203 if (isspace(*buffer)) {
204 sscanf(buffer, "%s %d", mem, &uid);
206 for (u=usr_head; u; u=u->next)
207 if (u->uid && u->uid==uid) break;
209 /* Add user - deferred because it is probably foreign */
211 if (FindByID(0, uid))
214 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
216 code = pr_WriteEntry(0,0,gpos,&gentry);
218 fprintf(stderr, "Error setting group count on %s: %s\n",
219 name, error_message(code));
222 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
223 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
226 fprintf(stderr, "Error while creating %s: %s\n",
227 u->name, error_message(code));
230 /* Add user to group */
231 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
233 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
234 code = pr_ReadEntry(0,0,upos,&uentry);
235 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
236 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
237 if (!code) code = AddToEntry (0, &uentry, upos, id);
242 fprintf(stderr, "Error while adding %s to %s: %s\n",
243 mem, name, error_message(code));
245 sscanf(buffer, "%s %d/%d %d %d %d",
246 name, &flags, "a, &id, &oid, &cid);
248 str = malloc(strlen(name) + 1);
251 fprintf(stderr, "malloc failed!");
255 strncpy(name, str, PR_MAXNAMELEN);
256 name[PR_MAXNAMELEN] = '\0';
260 code = CreateEntry(0, name, &id, 1/*idflag*/,
261 flags&PRGRP, oid, cid);
262 if (code == PRBADNAM) {
263 u = (struct usr_list *)malloc(sizeof(struct usr_list));
266 strcpy(u->name, name);
270 fprintf(stderr, "Error while creating %s: %s\n",
271 name, error_message(code));
272 } else if ((flags&PRACCESS) ||
273 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
274 gpos = FindByID(0, id);
275 code = pr_ReadEntry(0,0,gpos,&gentry);
277 gentry.flags = flags;
278 gentry.ngroups = quota;
279 code = pr_WriteEntry(0,0,gpos,&gentry);
282 fprintf(stderr,"Error while setting flags on %s: %s\n",
283 name, error_message(code));
287 for (u=usr_head; u; u=u->next)
289 fprintf(stderr, "Error while creating %s: %s\n",
290 u->name, error_message(PRBADNAM));
292 for (i = 0; i < HASHSIZE; i++) {
293 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
295 upos = display_entry(upos);
301 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
302 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
303 fprintf(stderr, "pt_util: error reading %s: %s\n",
304 pfile, sys_errlist[errno]);
307 uh = (struct ubik_hdr *)buffer;
308 if ((uh->version.epoch != uv.epoch) ||
309 (uh->version.counter != uv.counter)) {
310 fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
311 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
312 uv.epoch, uv.counter, uh->version.epoch,
313 uh->version.counter);
319 int display_entry (offset)
324 lseek (dbase_fd, offset+HDRSIZE, L_SET);
325 read(dbase_fd, &pre, sizeof(struct prentry));
329 if ((pre.flags & PRFREE) == 0) {
330 if (pre.flags & PRGRP) {
334 if (print_id(pre.id) && (flags&DO_USR))
335 fprintf(dfp, FMT_BASE,
336 pre.name, pre.flags, pre.ngroups,
337 pre.id, pre.owner, pre.creator);
341 return(nflag ? pre.nextName: pre.nextID);
350 i = grp_count++ % 1024;
352 g = (struct grp_list *)malloc(sizeof(struct grp_list));
360 void display_groups()
366 while (grp_count--) {
378 void display_group(id)
381 register int i, offset;
384 offset = ntohl(prh.idHash[IDHash(id)]);
386 lseek(dbase_fd, offset+HDRSIZE, L_SET);
387 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
388 fprintf(stderr, "pt_util: read i/o error: %s\n",
399 fprintf(dfp, FMT_BASE,
400 pre.name, pre.flags, pre.ngroups,
401 pre.id, pre.owner, pre.creator);
405 if ((flags&DO_MEM) == 0)
408 for (i=0; i<PRSIZE; i++) {
409 if ((id=pre.entries[i]) == 0)
411 if (id==PRBADID) continue;
412 if (print_id(id) || print_grp==1) {
414 fprintf(dfp, FMT_BASE,
415 pre.name, pre.flags, pre.ngroups,
416 pre.id, pre.owner, pre.creator);
419 fprintf(dfp, FMT_MEM, id_to_name(id), id);
425 lseek(dbase_fd, offset+HDRSIZE, L_SET);
426 read(dbase_fd, &prco, sizeof(struct contentry));
427 prco.next = ntohl(prco.next);
428 for (i = 0; i < COSIZE; i++) {
429 prco.entries[i] = ntohl(prco.entries[i]);
430 if ((id=prco.entries[i]) == 0)
432 if (id==PRBADID) continue;
433 if (print_id(id) || print_grp==1) {
435 fprintf(dfp, FMT_BASE,
436 pre.name, pre.flags, pre.ngroups,
437 pre.id, pre.owner, pre.creator);
440 fprintf(dfp, FMT_MEM, id_to_name(id), id);
443 if ((i == COSIZE) && prco.next)
454 char *str = malloc(4 * strlen(pre->name) + 1);
458 fprintf(stderr, "malloc failed in fix_pre()!");
461 strvis(str, pre->name, VIS_WHITE);
462 if (strlen(str) > PR_MAXNAMELEN)
464 fprintf(stderr, "encoding greater than PR_MAXNAMELEN!\n");
465 fprintf(stderr, "groupname %s will not be encoded!\n", pre->name);
469 strncpy(pre->name, str, PR_MAXNAMELEN);
470 pre->name[PR_MAXNAMELEN] = '\0';
472 pre->flags = ntohl(pre->flags);
473 pre->id = ntohl(pre->id);
474 pre->cellid = ntohl(pre->cellid);
475 pre->next = ntohl(pre->next);
476 pre->nextID = ntohl(pre->nextID);
477 pre->nextName = ntohl(pre->nextName);
478 pre->owner = ntohl(pre->owner);
479 pre->creator = ntohl(pre->creator);
480 pre->ngroups = ntohl(pre->ngroups);
481 pre->nusers = ntohl(pre->nusers);
482 pre->count = ntohl(pre->count);
483 pre->instance = ntohl(pre->instance);
484 pre->owned = ntohl(pre->owned);
485 pre->nextOwned = ntohl(pre->nextOwned);
486 pre->parent = ntohl(pre->parent);
487 pre->sibling = ntohl(pre->sibling);
488 pre->child = ntohl(pre->child);
489 for (i = 0; i < PRSIZE; i++) {
490 pre->entries[i] = ntohl(pre->entries[i]);
498 static struct prentry pre;
501 name = check_core(id);
502 if (name) return(name);
503 offset = ntohl(prh.idHash[IDHash(id)]);
505 lseek(dbase_fd, offset+HDRSIZE, L_SET);
506 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
507 fprintf(stderr, "pt_util: read i/o error: %s\n",
511 pre.id = ntohl(pre.id);
513 name = checkin(&pre);
516 offset = ntohl(pre.nextID);
524 struct hash_entry *he, *last;
528 last = (struct hash_entry *)0;
529 he = hat[IDHash(id)];
531 if (id == he->h_id) return(he->h_name);
535 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
537 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
541 he->next = (struct hash_entry *)0;
542 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
543 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
544 else last->next = he;
551 struct hash_entry *he;
552 he = hat[IDHash(id)];
554 if (id == he->h_id) return(he->h_name);