]> andersk Git - moira.git/blob - afssync/pt_util.c
bf75b5b493a9f37e93cbc56a40a3cd1f095075ca
[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 && (x<-32767 || x>97536)) || \
32                       ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
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                         gentry.ngroups = quota;
257                         code = pr_WriteEntry(0,0,gpos,&gentry);
258                     }
259                     if (code)
260                         fprintf(stderr,"Error while setting flags on %s: %s\n",
261                                 name, error_message(code));
262                 }
263             }
264         }
265         for (u=usr_head; u; u=u->next)
266             if (u->uid)
267                 fprintf(stderr, "Error while creating %s: %s\n",
268                         u->name, error_message(PRBADNAM));
269     } else {
270         for (i = 0; i < HASHSIZE; i++) {
271             upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
272             while (upos)
273                 upos = display_entry(upos);
274         }
275         if (flags & DO_GRP)
276             display_groups();
277     }
278
279     lseek (dbase_fd, 0, L_SET);         /* rewind to beginning of file */
280     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
281         fprintf(stderr, "ptdump: error reading %s: %s\n",
282                 pfile, sys_errlist[errno]);
283         exit (1);
284     }
285     uh = (struct ubik_hdr *)buffer;
286     if ((uh->version.epoch != uv.epoch) ||
287         (uh->version.counter != uv.counter)) {
288         fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
289         fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
290                 uv.epoch, uv.counter, uh->version.epoch,
291                 uh->version.counter);
292     }
293     close (dbase_fd);
294     exit (0);
295 }
296
297 int display_entry (offset)
298 int offset;
299 {
300     register int i;
301
302     lseek (dbase_fd, offset+HDRSIZE, L_SET);
303     read(dbase_fd, &pre, sizeof(struct prentry));
304
305     fix_pre(&pre);
306
307     if ((pre.flags & PRFREE) == 0) {
308         if (pre.flags & PRGRP) {
309             if (flags & DO_GRP)
310                 add_group(pre.id);
311         } else {
312             if (print_id(pre.id) && (flags&DO_USR))
313                 fprintf(dfp, FMT_BASE,
314                         pre.name, pre.flags, pre.ngroups,
315                         pre.id, pre.owner, pre.creator);
316             checkin(&pre);
317         }
318     }
319     return(nflag ? pre.nextName: pre.nextID);
320 }
321
322 void add_group(id)
323     long id;
324 {
325     struct grp_list *g;
326     register long i;
327
328     i = grp_count++ % 1024;
329     if (i == 0) {
330         g = (struct grp_list *)malloc(sizeof(struct grp_list));
331         g->next = grp_head;
332         grp_head = g;
333     }
334     g = grp_head;
335     g->groups[i] = id;
336 }
337
338 void display_groups()
339 {
340     register int i, id;
341     struct grp_list *g;
342
343     g = grp_head;
344     while (grp_count--) {
345         i = grp_count%1024;
346         id = g->groups[i];
347         display_group(id);
348         if (i==0) {
349             grp_head = g->next;
350             free(g);
351             g = grp_head;
352         }
353     }
354 }
355
356 void display_group(id)
357     int id;
358 {
359     register int i, offset;
360     int print_grp = 0;
361
362     offset = ntohl(prh.idHash[IDHash(id)]);
363     while (offset) {
364         lseek(dbase_fd, offset+HDRSIZE, L_SET);
365         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
366             fprintf(stderr, "ptdump: read i/o error: %s\n",
367                     strerror(errno));
368             exit (1);
369         }
370         fix_pre(&pre);
371         if (pre.id == id)
372             break;
373         offset = pre.nextID;
374     }
375
376     if (print_id(id)) {
377         fprintf(dfp, FMT_BASE,
378                 pre.name, pre.flags, pre.ngroups,
379                 pre.id, pre.owner, pre.creator);
380         print_grp = 1;
381     }
382
383     if ((flags&DO_MEM) == 0)
384         return;
385
386     for (i=0; i<PRSIZE; i++) {
387         if ((id=pre.entries[i]) == 0)
388             break;
389         if (id==PRBADID) continue;
390         if (print_id(id) || print_grp==1) {
391             if (print_grp==0) {
392                 fprintf(dfp, FMT_BASE,
393                         pre.name, pre.flags, pre.ngroups,
394                         pre.id, pre.owner, pre.creator);
395                 print_grp = 2;
396             }
397             fprintf(dfp, FMT_MEM, id_to_name(id), id);
398         }
399     }
400     if (i == PRSIZE) {
401         offset = pre.next;
402         while (offset) {
403             lseek(dbase_fd, offset+HDRSIZE, L_SET);
404             read(dbase_fd, &prco, sizeof(struct contentry));
405             prco.next = ntohl(prco.next);
406             for (i = 0; i < COSIZE; i++) {
407                 prco.entries[i] = ntohl(prco.entries[i]);
408                 if ((id=prco.entries[i]) == 0)
409                     break;
410                 if (id==PRBADID) continue;
411                 if (print_id(id) || print_grp==1) {
412                     if (print_grp==0) {
413                         fprintf(dfp, FMT_BASE,
414                                 pre.name, pre.flags, pre.ngroups,
415                                 pre.id, pre.owner, pre.creator);
416                         print_grp = 2;
417                     }
418                     fprintf(dfp, FMT_MEM, id_to_name(id), id);
419                 }
420             }
421             if ((i == COSIZE) && prco.next)
422                 offset = prco.next;
423             else offset = 0;
424         }
425     }
426 }
427
428 void fix_pre(pre)
429     struct prentry *pre;
430 {
431     register int i;
432     
433     pre->flags = ntohl(pre->flags);
434     pre->id = ntohl(pre->id);
435     pre->cellid = ntohl(pre->cellid);
436     pre->next = ntohl(pre->next);
437     pre->nextID = ntohl(pre->nextID);
438     pre->nextName = ntohl(pre->nextName);
439     pre->owner = ntohl(pre->owner);
440     pre->creator = ntohl(pre->creator);
441     pre->ngroups = ntohl(pre->ngroups);
442     pre->nusers = ntohl(pre->nusers);
443     pre->count = ntohl(pre->count);
444     pre->instance = ntohl(pre->instance);
445     pre->owned = ntohl(pre->owned);
446     pre->nextOwned = ntohl(pre->nextOwned);
447     pre->parent = ntohl(pre->parent);
448     pre->sibling = ntohl(pre->sibling);
449     pre->child = ntohl(pre->child);
450     for (i = 0; i < PRSIZE; i++) {
451         pre->entries[i] = ntohl(pre->entries[i]);
452     }
453 }
454
455 char *id_to_name(id)
456 int id;
457 {
458     register int offset;
459     static struct prentry pre;
460     char *name;
461
462     name = check_core(id);
463     if (name) return(name);
464     offset = ntohl(prh.idHash[IDHash(id)]);
465     while (offset) {
466         lseek(dbase_fd, offset+HDRSIZE, L_SET);
467         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
468             fprintf(stderr, "ptdump: read i/o error: %s\n",
469                     sys_errlist[errno]);
470             exit (1);
471         }
472         pre.id = ntohl(pre.id);
473         if (pre.id == id) {
474             name = checkin(&pre);
475             return(name);
476         }
477         offset = ntohl(pre.nextID);
478     }
479     return 0;
480 }
481
482 char *checkin(pre)
483 struct prentry *pre;
484 {
485     struct hash_entry *he, *last;
486     register int id;
487
488     id = pre->id;
489     last = (struct hash_entry *)0;
490     he = hat[IDHash(id)];
491     while (he) {
492         if (id == he->h_id) return(he->h_name);
493         last = he;
494         he = he->next;
495     }
496     he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
497     if (he == 0) {
498         fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
499         exit (1);
500     }
501     he->h_id = id;
502     he->next = (struct hash_entry *)0;
503     strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
504     if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
505     else last->next = he;
506     return(he->h_name);
507 }
508
509 char *check_core(id)
510 register int id;
511 {
512     struct hash_entry *he;
513     he = hat[IDHash(id)];
514     while (he) {
515         if (id == he->h_id) return(he->h_name);
516         he = he->next;
517     }
518     return 0;
519 }
This page took 0.082824 seconds and 3 git commands to generate.