]> andersk Git - moira.git/blob - afssync/pt_util.c
Re-incorporated partial cross-cell support
[moira.git] / afssync / pt_util.c
1 /*
2  *
3  * ptdump: Program to dump the AFS protection server database
4  *         into an ascii file.
5  *
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.
10  */
11
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <strings.h>
17 #include <sys/file.h>
18
19 #include <afs/param.h>
20 #include <lock.h>
21 #include <netinet/in.h>
22 #define UBIK_INTERNALS
23 #include <ubik.h>
24 #include <rx/xdr.h>
25 #include <rx/rx.h>
26 #include "ptint.h"
27 #include "ptserver.h"
28 #include "pterror.h"
29
30 #define IDHash(x) (abs(x) % HASHSIZE)
31 #define print_id(x) ( ((flags&DO_SYS)==0 && (abs(x)>32767)) || \
32                       ((flags&DO_OTR)==0 && (abs(x)<32768)))
33
34 extern char *optarg;
35 extern int optind;
36 extern int errno;
37 extern char *sys_errlist[];
38 #define strerror(a) sys_errlist[a]
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: ptdump [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 (Moira) 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, 0600)) < 0) {
148         fprintf(stderr, "ptdump: cannot open %s: %s\n",
149                 pfile, sys_errlist[errno]);
150         exit (1);
151     }
152     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
153         fprintf(stderr, "ptdump: error reading %s: %s\n",
154                 pfile, sys_errlist[errno]);
155         exit (1);
156     }
157
158     if (dfile) {
159         if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
160             fprintf(stderr, "ptdump: error opening %s: %s\n",
161                     dfile, sys_errlist[errno]);
162             exit(1);
163         }
164     } else
165         dfp = (wflag ? stdin : stdout);
166
167     uh = (struct ubik_hdr *)buffer;
168     if (ntohl(uh->magic) != UBIK_MAGIC)
169         fprintf(stderr, "ptdump: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
170                 pfile, ntohl(uh->magic), UBIK_MAGIC);
171     bcopy(&uh->version, &uv, sizeof(struct ubik_version));
172     fprintf(stderr, "Ubik Version is: %d.%d\n",
173             uv.epoch, uv.counter);
174     if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
175         fprintf(stderr, "ptdump: error reading %s: %s\n",
176                 pfile, sys_errlist[errno]);
177         exit (1);
178     }
179
180     Initdb();
181     initialize_pt_error_table();
182
183     if (wflag) {
184         struct usr_list *u;
185
186         while(fgets(buffer, sizeof(buffer), dfp)) {
187             int id, oid, cid, flags, quota, uid;
188             char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
189
190             if (isspace(*buffer)) {
191                 sscanf(buffer, "%s %d", mem, &uid);
192
193                 for (u=usr_head; u; u=u->next)
194                     if (u->uid && u->uid==uid) break;
195                 if (u) {
196                     /* Add user - deferred because it is probably foreign */
197                     u->uid = 0;
198                     if (FindByID(0, uid))
199                         code = PRIDEXIST;
200                     else {
201                         if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
202                             gentry.ngroups++;
203                             code = pr_WriteEntry(0,0,gpos,&gentry);
204                             if (code)
205                                 fprintf(stderr, "Error setting group count on %s: %s\n",
206                                         name, error_message(code));
207                         }
208                         code = CreateEntry
209                             (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
210                              SYSADMINID/*oid*/, SYSADMINID/*cid*/);
211                     }
212                     if (code)
213                         fprintf(stderr, "Error while creating %s: %s\n",
214                                 u->name, error_message(code));
215                     continue;
216                 }
217                 /* Add user to group */
218                 if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
219                     code = PRPERM;
220                 } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
221                     code = pr_ReadEntry(0,0,upos,&uentry);
222                     if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
223                     if (!code) code = AddToEntry (0, &gentry, gpos, uid);
224                     if (!code) code = AddToEntry (0, &uentry, upos, id);
225                 } else
226                     code = PRNOENT;
227
228                 if (code)
229                     fprintf(stderr, "Error while adding %s to %s: %s\n",
230                             mem, name, error_message(code));
231             } else {
232                 sscanf(buffer, "%s %d/%d %d %d %d",
233                        name, &flags, &quota, &id, &oid, &cid);
234
235                 if (FindByID(0, id))
236                     code = PRIDEXIST;
237                 else
238                     code = CreateEntry(0, name, &id, 1/*idflag*/,
239                                        flags&PRGRP, oid, cid);
240                 if (code == PRBADNAM) {
241                     u = (struct usr_list *)malloc(sizeof(struct usr_list));
242                     u->next = usr_head;
243                     u->uid = id;
244                     strcpy(u->name, name);
245                     usr_head = u;
246                 } else
247                 if (code) {
248                     fprintf(stderr, "Error while creating %s: %s\n",
249                             name, error_message(code));
250                 } else if ((flags&PRACCESS) ||
251                            (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
252                     gpos = FindByID(0, id);
253                     code = pr_ReadEntry(0,0,gpos,&gentry);
254                     if (!code) {
255                         gentry.flags = flags;
256                         code = pr_WriteEntry(0,0,gpos,&gentry);
257                     }
258                     if (code)
259                         fprintf(stderr,"Error while setting flags on %s: %s\n",
260                                 name, error_message(code));
261                 }
262             }
263         }
264         for (u=usr_head; u; u=u->next)
265             if (u->uid)
266                 fprintf(stderr, "Error while creating %s: %s\n",
267                         u->name, error_message(PRBADNAM));
268     } else {
269         for (i = 0; i < HASHSIZE; i++) {
270             upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
271             while (upos)
272                 upos = display_entry(upos);
273         }
274         if (flags & DO_GRP)
275             display_groups();
276     }
277
278     lseek (dbase_fd, 0, L_SET);         /* rewind to beginning of file */
279     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
280         fprintf(stderr, "ptdump: error reading %s: %s\n",
281                 pfile, sys_errlist[errno]);
282         exit (1);
283     }
284     uh = (struct ubik_hdr *)buffer;
285     if ((uh->version.epoch != uv.epoch) ||
286         (uh->version.counter != uv.counter)) {
287         fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
288         fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
289                 uv.epoch, uv.counter, uh->version.epoch,
290                 uh->version.counter);
291     }
292     close (dbase_fd);
293     exit (0);
294 }
295
296 int display_entry (offset)
297 int offset;
298 {
299     register int i;
300
301     lseek (dbase_fd, offset+HDRSIZE, L_SET);
302     read(dbase_fd, &pre, sizeof(struct prentry));
303
304     fix_pre(&pre);
305
306     if ((pre.flags & PRFREE) == 0) {
307         if (pre.flags & PRGRP) {
308             if (flags & DO_GRP)
309                 add_group(pre.id);
310         } else {
311             if (print_id(pre.id) && (flags&DO_USR))
312                 fprintf(dfp, FMT_BASE,
313                         pre.name, pre.flags, pre.ngroups,
314                         pre.id, pre.owner, pre.creator);
315             checkin(&pre);
316         }
317     }
318     return(nflag ? pre.nextName: pre.nextID);
319 }
320
321 void add_group(id)
322     long id;
323 {
324     struct grp_list *g;
325     register long i;
326
327     i = grp_count++ % 1024;
328     if (i == 0) {
329         g = (struct grp_list *)malloc(sizeof(struct grp_list));
330         g->next = grp_head;
331         grp_head = g;
332     }
333     g = grp_head;
334     g->groups[i] = id;
335 }
336
337 void display_groups()
338 {
339     register int i, id;
340     struct grp_list *g;
341
342     g = grp_head;
343     while (grp_count--) {
344         i = grp_count%1024;
345         id = g->groups[i];
346         display_group(id);
347         if (i==0) {
348             grp_head = g->next;
349             free(g);
350             g = grp_head;
351         }
352     }
353 }
354
355 void display_group(id)
356     int id;
357 {
358     register int i, offset;
359     int print_grp = 0;
360
361     offset = ntohl(prh.idHash[IDHash(id)]);
362     while (offset) {
363         lseek(dbase_fd, offset+HDRSIZE, L_SET);
364         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
365             fprintf(stderr, "ptdump: read i/o error: %s\n",
366                     strerror(errno));
367             exit (1);
368         }
369         fix_pre(&pre);
370         if (pre.id == id)
371             break;
372         offset = pre.nextID;
373     }
374
375     if (print_id(id)) {
376         fprintf(dfp, FMT_BASE,
377                 pre.name, pre.flags, pre.ngroups,
378                 pre.id, pre.owner, pre.creator);
379         print_grp = 1;
380     }
381
382     if ((flags&DO_MEM) == 0)
383         return;
384
385     for (i=0; i<PRSIZE; i++) {
386         if ((id=pre.entries[i]) == 0)
387             break;
388         if (id==PRBADID) continue;
389         if (print_id(id) || print_grp==1) {
390             if (print_grp==0) {
391                 fprintf(dfp, FMT_BASE,
392                         pre.name, pre.flags, pre.ngroups,
393                         pre.id, pre.owner, pre.creator);
394                 print_grp = 2;
395             }
396             fprintf(dfp, FMT_MEM, id_to_name(id), id);
397         }
398     }
399     if (i == PRSIZE) {
400         offset = pre.next;
401         while (offset) {
402             lseek(dbase_fd, offset+HDRSIZE, L_SET);
403             read(dbase_fd, &prco, sizeof(struct contentry));
404             prco.next = ntohl(prco.next);
405             for (i = 0; i < COSIZE; i++) {
406                 prco.entries[i] = ntohl(prco.entries[i]);
407                 if ((id=prco.entries[i]) == 0)
408                     break;
409                 if (id==PRBADID) continue;
410                 if (print_id(id) || print_grp==1) {
411                     if (print_grp==0) {
412                         fprintf(dfp, FMT_BASE,
413                                 pre.name, pre.flags, pre.ngroups,
414                                 pre.id, pre.owner, pre.creator);
415                         print_grp = 2;
416                     }
417                     fprintf(dfp, FMT_MEM, id_to_name(id), id);
418                 }
419             }
420             if ((i == COSIZE) && prco.next)
421                 offset = prco.next;
422             else offset = 0;
423         }
424     }
425 }
426
427 void fix_pre(pre)
428     struct prentry *pre;
429 {
430     register int i;
431     
432     pre->flags = ntohl(pre->flags);
433     pre->id = ntohl(pre->id);
434     pre->cellid = ntohl(pre->cellid);
435     pre->next = ntohl(pre->next);
436     pre->nextID = ntohl(pre->nextID);
437     pre->nextName = ntohl(pre->nextName);
438     pre->owner = ntohl(pre->owner);
439     pre->creator = ntohl(pre->creator);
440     pre->ngroups = ntohl(pre->ngroups);
441     pre->nusers = ntohl(pre->nusers);
442     pre->count = ntohl(pre->count);
443     pre->instance = ntohl(pre->instance);
444     pre->owned = ntohl(pre->owned);
445     pre->nextOwned = ntohl(pre->nextOwned);
446     pre->parent = ntohl(pre->parent);
447     pre->sibling = ntohl(pre->sibling);
448     pre->child = ntohl(pre->child);
449     for (i = 0; i < PRSIZE; i++) {
450         pre->entries[i] = ntohl(pre->entries[i]);
451     }
452 }
453
454 char *id_to_name(id)
455 int id;
456 {
457     register int offset;
458     static struct prentry pre;
459     char *name;
460
461     name = check_core(id);
462     if (name) return(name);
463     offset = ntohl(prh.idHash[IDHash(id)]);
464     while (offset) {
465         lseek(dbase_fd, offset+HDRSIZE, L_SET);
466         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
467             fprintf(stderr, "ptdump: read i/o error: %s\n",
468                     sys_errlist[errno]);
469             exit (1);
470         }
471         pre.id = ntohl(pre.id);
472         if (pre.id == id) {
473             name = checkin(&pre);
474             return(name);
475         }
476         offset = ntohl(pre.nextID);
477     }
478     return 0;
479 }
480
481 char *checkin(pre)
482 struct prentry *pre;
483 {
484     struct hash_entry *he, *last;
485     register int id;
486
487     id = pre->id;
488     last = (struct hash_entry *)0;
489     he = hat[IDHash(id)];
490     while (he) {
491         if (id == he->h_id) return(he->h_name);
492         last = he;
493         he = he->next;
494     }
495     he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
496     if (he == 0) {
497         fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
498         exit (1);
499     }
500     he->h_id = id;
501     he->next = (struct hash_entry *)0;
502     strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
503     if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
504     else last->next = he;
505     return(he->h_name);
506 }
507
508 char *check_core(id)
509 register int id;
510 {
511     struct hash_entry *he;
512     he = hat[IDHash(id)];
513     while (he) {
514         if (id == he->h_id) return(he->h_name);
515         he = he->next;
516     }
517     return 0;
518 }
This page took 0.240126 seconds and 5 git commands to generate.