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>
21 #include <afs/param.h>
23 #include <netinet/in.h>
24 #define UBIK_INTERNALS
33 #define IDHash(x) (abs(x) % HASHSIZE)
35 int display_entry(int offset);
37 void add_group(long id);
38 void display_groups(void);
39 void display_group(int id);
40 void fix_pre(struct prentry *pre);
41 char *checkin(struct prentry *pre);
42 char *check_core(register int id);
43 char *id_to_name(int id);
46 char h_name[PR_MAXNAMELEN];
48 struct hash_entry *next;
50 struct hash_entry *hat[HASHSIZE];
52 static struct contentry prco;
53 static struct prentry pre;
54 static struct prheader prh;
55 static struct ubik_version uv;
58 struct grp_list *next;
61 static struct grp_list *grp_head=0;
62 static long grp_count=0;
65 struct usr_list *next;
66 char name[PR_MAXNAMELEN];
69 static struct usr_list *usr_head=0;
75 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
76 #define FMT_MEM " %-8s %d\n"
88 int main(int argc, char **argv)
93 struct prentry uentry, gentry;
96 char *pfile = "/usr/afs/db/prdb.DB0";
99 while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
117 flags |= (DO_GRP|DO_MEM);
130 "Usage: pt_util [options] [-d data] [-p prdb]\n");
131 fputs(" Options:\n", stderr);
132 fputs(" -w Update prdb with contents of data file\n", stderr);
133 fputs(" -u Display users\n", stderr);
134 fputs(" -g Display groups\n", stderr);
135 fputs(" -m Display group members\n", stderr);
136 fputs(" -n Follow name hash chains (not id hashes)\n", stderr);
137 fputs(" -s Display only system (Moira) data\n", stderr);
138 fputs(" -x Display extra users/groups\n", stderr);
142 if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) {
143 fprintf(stderr, "pt_util: cannot open %s: %s\n",
144 pfile, strerror(errno));
147 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
148 fprintf(stderr, "pt_util: error reading %s: %s\n",
149 pfile, strerror(errno));
154 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
155 fprintf(stderr, "pt_util: error opening %s: %s\n",
156 dfile, strerror(errno));
160 dfp = (wflag ? stdin : stdout);
162 uh = (struct ubik_hdr *)buffer;
163 if (ntohl(uh->magic) != UBIK_MAGIC)
164 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
165 pfile, ntohl(uh->magic), UBIK_MAGIC);
166 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
167 if (wflag && uv.epoch==0 && uv.counter==0) {
168 uv.epoch=2; /* a ubik version of 0 or 1 has special meaning */
169 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
170 lseek(dbase_fd, 0, SEEK_SET);
171 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
172 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
173 pfile, strerror(errno));
177 fprintf(stderr, "Ubik Version is: %d.%d\n",
178 uv.epoch, uv.counter);
179 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
180 fprintf(stderr, "pt_util: error reading %s: %s\n",
181 pfile, strerror(errno));
186 initialize_pt_error_table();
191 while(fgets(buffer, sizeof(buffer), dfp)) {
192 int id, oid, cid, flags, quota, uid;
193 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
195 if (isspace(*buffer)) {
196 sscanf(buffer, "%s %d", mem, &uid);
198 for (u=usr_head; u; u=u->next)
199 if (u->uid && u->uid==uid) break;
201 /* Add user - deferred because it is probably foreign */
203 if (FindByID(0, uid))
206 if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
208 code = pr_WriteEntry(0,0,gpos,&gentry);
210 fprintf(stderr, "Error setting group count on %s: %s\n",
211 name, error_message(code));
214 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
215 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
218 fprintf(stderr, "Error while creating %s: %s\n",
219 u->name, error_message(code));
222 /* Add user to group */
223 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
225 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
226 code = pr_ReadEntry(0,0,upos,&uentry);
227 if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
228 if (!code) code = AddToEntry (0, &gentry, gpos, uid);
229 if (!code) code = AddToEntry (0, &uentry, upos, id);
234 fprintf(stderr, "Error while adding %s to %s: %s\n",
235 mem, name, error_message(code));
237 sscanf(buffer, "%s %d/%d %d %d %d",
238 name, &flags, "a, &id, &oid, &cid);
240 str = malloc(strlen(name) + 1);
243 fprintf(stderr, "malloc failed!");
247 strncpy(name, str, PR_MAXNAMELEN);
248 name[PR_MAXNAMELEN] = '\0';
252 code = CreateEntry(0, name, &id, 1/*idflag*/,
253 flags&PRGRP, oid, cid);
254 if (code == PRBADNAM) {
255 u = (struct usr_list *)malloc(sizeof(struct usr_list));
258 strcpy(u->name, name);
262 fprintf(stderr, "Error while creating %s: %s\n",
263 name, error_message(code));
264 } else if ((flags&PRACCESS) ||
265 (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
266 gpos = FindByID(0, id);
267 code = pr_ReadEntry(0,0,gpos,&gentry);
269 gentry.flags = flags;
270 gentry.ngroups = quota;
271 code = pr_WriteEntry(0,0,gpos,&gentry);
274 fprintf(stderr,"Error while setting flags on %s: %s\n",
275 name, error_message(code));
279 for (u=usr_head; u; u=u->next)
281 fprintf(stderr, "Error while creating %s: %s\n",
282 u->name, error_message(PRBADNAM));
284 for (i = 0; i < HASHSIZE; i++) {
285 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
287 upos = display_entry(upos);
293 lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */
294 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
295 fprintf(stderr, "pt_util: error reading %s: %s\n",
296 pfile, strerror(errno));
299 uh = (struct ubik_hdr *)buffer;
300 if ((uh->version.epoch != uv.epoch) ||
301 (uh->version.counter != uv.counter)) {
302 fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
303 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
304 uv.epoch, uv.counter, uh->version.epoch,
305 uh->version.counter);
311 int display_entry(int offset)
315 lseek (dbase_fd, offset+HDRSIZE, L_SET);
316 read(dbase_fd, &pre, sizeof(struct prentry));
320 if ((pre.flags & PRFREE) == 0) {
321 if (pre.flags & PRGRP) {
325 if (print_id(pre.id) && (flags&DO_USR))
326 fprintf(dfp, FMT_BASE,
327 pre.name, pre.flags, pre.ngroups,
328 pre.id, pre.owner, pre.creator);
332 return(nflag ? pre.nextName: pre.nextID);
335 void add_group(long id)
340 i = grp_count++ % 1024;
342 g = (struct grp_list *)malloc(sizeof(struct grp_list));
350 void display_groups(void)
356 while (grp_count--) {
368 void display_group(int 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)
439 void fix_pre(struct prentry *pre)
442 char *str = malloc(4 * strlen(pre->name) + 1);
446 fprintf(stderr, "malloc failed in fix_pre()!");
449 strvis(str, pre->name, VIS_WHITE);
450 if (strlen(str) > PR_MAXNAMELEN)
452 fprintf(stderr, "encoding greater than PR_MAXNAMELEN!\n");
453 fprintf(stderr, "groupname %s will not be encoded!\n", pre->name);
457 strncpy(pre->name, str, PR_MAXNAMELEN);
458 pre->name[PR_MAXNAMELEN] = '\0';
460 pre->flags = ntohl(pre->flags);
461 pre->id = ntohl(pre->id);
462 pre->cellid = ntohl(pre->cellid);
463 pre->next = ntohl(pre->next);
464 pre->nextID = ntohl(pre->nextID);
465 pre->nextName = ntohl(pre->nextName);
466 pre->owner = ntohl(pre->owner);
467 pre->creator = ntohl(pre->creator);
468 pre->ngroups = ntohl(pre->ngroups);
469 pre->nusers = ntohl(pre->nusers);
470 pre->count = ntohl(pre->count);
471 pre->instance = ntohl(pre->instance);
472 pre->owned = ntohl(pre->owned);
473 pre->nextOwned = ntohl(pre->nextOwned);
474 pre->parent = ntohl(pre->parent);
475 pre->sibling = ntohl(pre->sibling);
476 pre->child = ntohl(pre->child);
477 for (i = 0; i < PRSIZE; i++) {
478 pre->entries[i] = ntohl(pre->entries[i]);
482 char *id_to_name(int id)
485 static struct prentry pre;
488 name = check_core(id);
489 if (name) return(name);
490 offset = ntohl(prh.idHash[IDHash(id)]);
492 lseek(dbase_fd, offset+HDRSIZE, L_SET);
493 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
494 fprintf(stderr, "pt_util: read i/o error: %s\n",
498 pre.id = ntohl(pre.id);
500 name = checkin(&pre);
503 offset = ntohl(pre.nextID);
508 char *checkin(struct prentry *pre)
510 struct hash_entry *he, *last;
514 last = (struct hash_entry *)0;
515 he = hat[IDHash(id)];
517 if (id == he->h_id) return(he->h_name);
521 he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
523 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
527 he->next = (struct hash_entry *)0;
528 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
529 if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
530 else last->next = he;
534 char *check_core(register int id)
536 struct hash_entry *he;
537 he = hat[IDHash(id)];
539 if (id == he->h_id) return(he->h_name);
545 /* returns 1 if the id value should be printered, otherwise 0 */
548 /* process system (Moira) users */
551 /* hard-coded list of IDs in the system id space that aren't actually
552 * system users. Skip them here. Mmm, legacies.
867 if (id > -131073 && id < 131073)
872 /* process non-system (not in Moira) users */
873 else if (flags & DO_OTR)
875 /* hard-coded list of IDs in the system id space that aren't actually
876 * system users. Print them here. Mmm, legacies.
1191 if (id < -131072 || id > 131072)
1196 /* neither flag set, don't do anything */