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