ptserver-simple-corrupt-protection-20080612
[openafs.git] / src / ptserver / pt_util.c
1 /* $Id$ */
2
3 /*
4  *
5  * pt_util: Program to dump the AFS protection server database
6  *         into an ascii file.
7  *
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.
12  */
13
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <sys/file.h>
21
22 #include <afsconfig.h>
23 #include <afs/param.h>
24
25 RCSID
26     ("$Header$");
27
28 #include <afs/cmd.h>            /*Command line parsing */
29 #include <errno.h>
30 #include <lock.h>
31 #include <netinet/in.h>
32 #define UBIK_INTERNALS
33 #include <ubik.h>
34 #include <rx/xdr.h>
35 #include <rx/rx.h>
36 #include <afs/com_err.h>
37 #include "ptint.h"
38 #include "ptserver.h"
39 #include "pterror.h"
40
41 #define IDHash(x) (abs(x) % HASHSIZE)
42 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
43                       ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
44
45 extern char *optarg;
46 extern int optind;
47
48 int restricted = 0;
49 int display_entry();
50 void add_group();
51 void display_groups();
52 void display_group();
53 void fix_pre();
54 char *checkin();
55 char *check_core();
56 char *id_to_name();
57 int CommandProc(struct cmd_syndesc *, void *);
58
59 struct hash_entry {
60     char h_name[PR_MAXNAMELEN];
61     int h_id;
62     struct hash_entry *next;
63 };
64 struct hash_entry *hat[HASHSIZE];
65
66 static struct contentry prco;
67 static struct prentry pre;
68 static struct prheader prh;
69 static struct ubik_version uv;
70
71 struct grp_list {
72     struct grp_list *next;
73     long groups[1024];
74 };
75 static struct grp_list *grp_head = 0;
76 static long grp_count = 0;
77
78 struct usr_list {
79     struct usr_list *next;
80     char name[PR_MAXNAMELEN];
81     long uid;
82 };
83 static struct usr_list *usr_head = 0;
84
85 char buffer[1024];
86 int dbase_fd;
87 FILE *dfp;
88
89 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
90 #define FMT_MEM  "   %-8s %d\n"
91
92 #define DO_USR 1
93 #define DO_GRP 2
94 #define DO_MEM 4
95 #define DO_SYS 8
96 #define DO_OTR 16
97
98 int nflag = 0;
99 int wflag = 0;
100 int flags = 0;
101
102 int
103 main(int argc, char **argv)
104 {
105
106     register struct cmd_syndesc *cs;    /*Command line syntax descriptor */
107     register afs_int32 code;    /*Return code */
108
109     cs = cmd_CreateSyntax(NULL, CommandProc, NULL,
110                           "access protection database");
111     cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
112                 "update prdb with contents of data file");
113     cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
114     cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
115     cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
116                 "display group members");
117     cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
118                 "follow name hash chains (not id hashes)");
119     cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
120                 "display only system data");
121     cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
122                 "display extra users/groups");
123     cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
124     cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
125     code = cmd_Dispatch(argc, argv);
126
127     exit(code);
128
129 }
130
131 int
132 CommandProc(register struct cmd_syndesc *a_as, void *arock)
133 {
134     register int i;
135     register long code;
136     long cc, upos, gpos;
137     struct prentry uentry, gentry;
138     struct ubik_hdr *uh;
139     char *dfile = 0;
140     char *pfile = "/usr/afs/db/prdb.DB0";
141     struct cmd_parmdesc *tparm;
142
143     tparm = a_as->parms;
144
145     if (tparm[0].items) {
146         wflag++;
147     }
148     if (tparm[1].items) {
149         flags |= DO_USR;
150     }
151     if (tparm[2].items) {
152         flags |= DO_GRP;
153     }
154     if (tparm[3].items) {
155         flags |= (DO_GRP | DO_MEM);
156     }
157     if (tparm[4].items) {
158         nflag++;
159     }
160     if (tparm[5].items) {
161         flags |= DO_SYS;
162     }
163     if (tparm[6].items) {
164         flags |= DO_OTR;
165     }
166     if (tparm[7].items) {
167         pfile = tparm[7].items->data;
168     }
169     if (tparm[8].items) {
170         dfile = tparm[8].items->data;
171     }
172
173     if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
174         < 0) {
175         fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
176                 strerror(errno));
177         exit(1);
178     }
179     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
180         fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
181                 strerror(errno));
182         exit(1);
183     }
184
185     if (dfile) {
186         if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
187             fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
188                     strerror(errno));
189             exit(1);
190         }
191     } else
192         dfp = (wflag ? stdin : stdout);
193
194     uh = (struct ubik_hdr *)buffer;
195     if (ntohl(uh->magic) != UBIK_MAGIC)
196         fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
197                 pfile, ntohl(uh->magic), UBIK_MAGIC);
198     memcpy(&uv, &uh->version, sizeof(struct ubik_version));
199
200     if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
201         uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
202         memcpy(&uh->version, &uv, sizeof(struct ubik_version));
203         lseek(dbase_fd, 0, SEEK_SET);
204         if (write(dbase_fd, buffer, HDRSIZE) < 0) {
205             fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
206                     pfile, strerror(errno));
207             exit(1);
208         }
209     }
210
211     /* Now that any writeback is done, swap these */
212     uv.epoch = ntohl(uv.epoch);
213     uv.counter = ntohl(uv.counter);
214
215     fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
216     if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
217         fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
218                 strerror(errno));
219         exit(1);
220     }
221
222     Initdb();
223     initialize_PT_error_table();
224
225     if (wflag) {
226         struct usr_list *u;
227
228         while (fgets(buffer, sizeof(buffer), dfp)) {
229             int id, oid, cid, flags, quota, uid;
230             char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
231
232             if (isspace(*buffer)) {
233                 sscanf(buffer, "%s %d", mem, &uid);
234
235                 for (u = usr_head; u; u = u->next)
236                     if (u->uid && u->uid == uid)
237                         break;
238                 if (u) {
239                     /* Add user - deferred because it is probably foreign */
240                     u->uid = 0;
241                     if (FindByID(0, uid))
242                         code = PRIDEXIST;
243                     else {
244                         if (!code
245                             && (flags & (PRGRP | PRQUOTA)) ==
246                             (PRGRP | PRQUOTA)) {
247                             gentry.ngroups++;
248                             code = pr_WriteEntry(0, 0, gpos, &gentry);
249                             if (code)
250                                 fprintf(stderr,
251                                         "Error setting group count on %s: %s\n",
252                                         name, afs_error_message(code));
253                         }
254                         code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
255                                            1 /*gflag */ ,
256                                            SYSADMINID /*oid */ ,
257                                            SYSADMINID /*cid */ );
258                     }
259                     if (code)
260                         fprintf(stderr, "Error while creating %s: %s\n",
261                                 u->name, afs_error_message(code));
262                     continue;
263                 }
264                 /* Add user to group */
265                 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
266                     code = PRPERM;
267                 } else if ((upos = FindByID(0, uid))
268                            && (gpos = FindByID(0, id))) {
269                     code = pr_ReadEntry(0, 0, upos, &uentry);
270                     if (!code)
271                         code = pr_ReadEntry(0, 0, gpos, &gentry);
272                     if (!code)
273                         code = AddToEntry(0, &gentry, gpos, uid);
274                     if (!code)
275                         code = AddToEntry(0, &uentry, upos, id);
276                 } else
277                     code = PRNOENT;
278
279                 if (code)
280                     fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
281                             name, afs_error_message(code));
282             } else {
283                 sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, &quota, &id,
284                        &oid, &cid);
285
286                 if (FindByID(0, id))
287                     code = PRIDEXIST;
288                 else
289                     code = CreateEntry(0, name, &id, 1 /*idflag */ ,
290                                        flags & PRGRP, oid, cid);
291                 if (code == PRBADNAM) {
292                     u = (struct usr_list *)malloc(sizeof(struct usr_list));
293                     u->next = usr_head;
294                     u->uid = id;
295                     strcpy(u->name, name);
296                     usr_head = u;
297                 } else if (code) {
298                     fprintf(stderr, "Error while creating %s: %s\n", name,
299                             afs_error_message(code));
300                 } else if ((flags & PRACCESS)
301                            || (flags & (PRGRP | PRQUOTA)) ==
302                            (PRGRP | PRQUOTA)) {
303                     gpos = FindByID(0, id);
304                     code = pr_ReadEntry(0, 0, gpos, &gentry);
305                     if (!code) {
306                         gentry.flags = flags;
307                         gentry.ngroups = quota;
308                         code = pr_WriteEntry(0, 0, gpos, &gentry);
309                     }
310                     if (code)
311                         fprintf(stderr,
312                                 "Error while setting flags on %s: %s\n", name,
313                                 afs_error_message(code));
314                 }
315             }
316         }
317         for (u = usr_head; u; u = u->next)
318             if (u->uid)
319                 fprintf(stderr, "Error while creating %s: %s\n", u->name,
320                         afs_error_message(PRBADNAM));
321     } else {
322         for (i = 0; i < HASHSIZE; i++) {
323             upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
324             while (upos) {
325                 long newpos;
326                 newpos = display_entry(upos);
327                 if (newpos == upos) {
328                     fprintf(stderr, "pt_util: hash error in %s chain %d\n", 
329                             nflag ? "name":"id", i);
330                     exit(1);
331                 } else
332                     upos = newpos;
333             }
334         }
335         if (flags & DO_GRP)
336             display_groups();
337     }
338
339     lseek(dbase_fd, 0, L_SET);  /* rewind to beginning of file */
340     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
341         fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
342                 strerror(errno));
343         exit(1);
344     }
345     uh = (struct ubik_hdr *)buffer;
346
347     uh->version.epoch = ntohl(uh->version.epoch);
348     uh->version.counter = ntohl(uh->version.counter);
349
350     if ((uh->version.epoch != uv.epoch)
351         || (uh->version.counter != uv.counter)) {
352         fprintf(stderr,
353                 "pt_util: Ubik Version number changed during execution.\n");
354         fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
355                 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
356     }
357     close(dbase_fd);
358     exit(0);
359 }
360
361 int
362 display_entry(int offset)
363 {
364     register int i;
365
366     lseek(dbase_fd, offset + HDRSIZE, L_SET);
367     read(dbase_fd, &pre, sizeof(struct prentry));
368
369     fix_pre(&pre);
370
371     if ((pre.flags & PRFREE) == 0) {
372         if (pre.flags & PRGRP) {
373             if (flags & DO_GRP)
374                 add_group(pre.id);
375         } else {
376             if (print_id(pre.id) && (flags & DO_USR))
377                 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
378                         pre.id, pre.owner, pre.creator);
379             checkin(&pre);
380         }
381     }
382     return (nflag ? pre.nextName : pre.nextID);
383 }
384
385 void
386 add_group(long id)
387 {
388     struct grp_list *g;
389     register long i;
390
391     i = grp_count++ % 1024;
392     if (i == 0) {
393         g = (struct grp_list *)malloc(sizeof(struct grp_list));
394         g->next = grp_head;
395         grp_head = g;
396     }
397     g = grp_head;
398     g->groups[i] = id;
399 }
400
401 void
402 display_groups()
403 {
404     register int i, id;
405     struct grp_list *g;
406
407     g = grp_head;
408     while (grp_count--) {
409         i = grp_count % 1024;
410         id = g->groups[i];
411         display_group(id);
412         if (i == 0) {
413             grp_head = g->next;
414             free(g);
415             g = grp_head;
416         }
417     }
418 }
419
420 void
421 display_group(int id)
422 {
423     register int i, offset;
424     int print_grp = 0;
425
426     offset = ntohl(prh.idHash[IDHash(id)]);
427     while (offset) {
428         lseek(dbase_fd, offset + HDRSIZE, L_SET);
429         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
430             fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
431             exit(1);
432         }
433         fix_pre(&pre);
434         if (pre.id == id)
435             break;
436         offset = pre.nextID;
437     }
438
439     if (print_id(id)) {
440         fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
441                 pre.owner, pre.creator);
442         print_grp = 1;
443     }
444
445     if ((flags & DO_MEM) == 0)
446         return;
447
448     for (i = 0; i < PRSIZE; i++) {
449         if ((id = pre.entries[i]) == 0)
450             break;
451         if (id == PRBADID)
452             continue;
453         if (print_id(id) || print_grp == 1) {
454             if (print_grp == 0) {
455                 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
456                         pre.id, pre.owner, pre.creator);
457                 print_grp = 2;
458             }
459             fprintf(dfp, FMT_MEM, id_to_name(id), id);
460         }
461     }
462     if (i == PRSIZE) {
463         offset = pre.next;
464         while (offset) {
465             lseek(dbase_fd, offset + HDRSIZE, L_SET);
466             read(dbase_fd, &prco, sizeof(struct contentry));
467             prco.next = ntohl(prco.next);
468             for (i = 0; i < COSIZE; i++) {
469                 prco.entries[i] = ntohl(prco.entries[i]);
470                 if ((id = prco.entries[i]) == 0)
471                     break;
472                 if (id == PRBADID)
473                     continue;
474                 if (print_id(id) || print_grp == 1) {
475                     if (print_grp == 0) {
476                         fprintf(dfp, FMT_BASE, pre.name, pre.flags,
477                                 pre.ngroups, pre.id, pre.owner, pre.creator);
478                         print_grp = 2;
479                     }
480                     fprintf(dfp, FMT_MEM, id_to_name(id), id);
481                 }
482             }
483             if ((i == COSIZE) && prco.next)
484                 offset = prco.next;
485             else
486                 offset = 0;
487         }
488     }
489 }
490
491 void
492 fix_pre(struct prentry *pre)
493 {
494     register int i;
495
496     pre->flags = ntohl(pre->flags);
497     pre->id = ntohl(pre->id);
498     pre->cellid = ntohl(pre->cellid);
499     pre->next = ntohl(pre->next);
500     pre->nextID = ntohl(pre->nextID);
501     pre->nextName = ntohl(pre->nextName);
502     pre->owner = ntohl(pre->owner);
503     pre->creator = ntohl(pre->creator);
504     pre->ngroups = ntohl(pre->ngroups);
505     pre->nusers = ntohl(pre->nusers);
506     pre->count = ntohl(pre->count);
507     pre->instance = ntohl(pre->instance);
508     pre->owned = ntohl(pre->owned);
509     pre->nextOwned = ntohl(pre->nextOwned);
510     pre->parent = ntohl(pre->parent);
511     pre->sibling = ntohl(pre->sibling);
512     pre->child = ntohl(pre->child);
513     for (i = 0; i < PRSIZE; i++) {
514         pre->entries[i] = ntohl(pre->entries[i]);
515     }
516 }
517
518 char *
519 id_to_name(int id)
520 {
521     register int offset;
522     static struct prentry pre;
523     char *name;
524
525     name = check_core(id);
526     if (name)
527         return (name);
528     offset = ntohl(prh.idHash[IDHash(id)]);
529     while (offset) {
530         lseek(dbase_fd, offset + HDRSIZE, L_SET);
531         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
532             fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
533             exit(1);
534         }
535         pre.id = ntohl(pre.id);
536         if (pre.id == id) {
537             name = checkin(&pre);
538             return (name);
539         }
540         offset = ntohl(pre.nextID);
541     }
542     return 0;
543 }
544
545 char *
546 checkin(struct prentry *pre)
547 {
548     struct hash_entry *he, *last;
549     register int id;
550
551     id = pre->id;
552     last = (struct hash_entry *)0;
553     he = hat[IDHash(id)];
554     while (he) {
555         if (id == he->h_id)
556             return (he->h_name);
557         last = he;
558         he = he->next;
559     }
560     he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
561     if (he == 0) {
562         fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
563         exit(1);
564     }
565     he->h_id = id;
566     he->next = (struct hash_entry *)0;
567     strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
568     if (last == (struct hash_entry *)0)
569         hat[IDHash(id)] = he;
570     else
571         last->next = he;
572     return (he->h_name);
573 }
574
575 char *
576 check_core(register int id)
577 {
578     struct hash_entry *he;
579     he = hat[IDHash(id)];
580     while (he) {
581         if (id == he->h_id)
582             return (he->h_name);
583         he = he->next;
584     }
585     return 0;
586 }