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