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