]> andersk Git - moira.git/blob - afssync/ptdump.c
Initial revision
[moira.git] / afssync / ptdump.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 #include <lock.h>
19 #include <netinet/in.h>
20 #define UBIK_INTERNALS
21 #include <ubik.h>
22 #include <rx/xdr.h>
23 #include <rx/rx.h>
24 #include <afs/ptint.h>
25 #include <afs/ptserver.h>
26 extern char *optarg;
27 extern int optind;
28 extern int errno;
29 extern char *sys_errlist[];
30 struct prheader prh;
31 struct prentry pre;
32 struct ubik_version uv;
33 char buffer[1024];
34 int grpflag, nmflg, cflag;
35
36 main(argc, argv)
37 int argc;
38 char **argv;
39 {
40     int cc;
41     int fd, offset;
42     register int i;
43     char file[512];
44     register struct ubik_hdr *uh;
45     int didit;
46     strcpy(file, "/usr/afs/db/prdb.DB0");
47     while ((cc = getopt(argc, argv, "f:gnc")) != EOF) {
48         switch (cc) {
49         case 'f':
50             strncpy (file, optarg, sizeof(file));
51             break;
52         case 'g':
53             grpflag++;
54             break;
55         case 'c':
56             cflag++;
57             break;
58         case 'n':
59             nmflg++;            /* Use the Name hash chain */
60             break;
61         default:
62             fprintf(stderr, "ptdump: -%c: unknown option\n", cc);
63             break;
64         }
65     }
66     if (cflag && !grpflag) {
67         fprintf(stderr, "ptdump: -c requires -g flag.\n");
68         exit (1);
69     }
70     if ((fd = open(file, O_RDONLY, 0600)) < 0) {
71         fprintf(stderr, "ptdump: cannot open %s: %s\n",
72                 file, sys_errlist[errno]);
73         exit (1);
74     }
75     if (read(fd, buffer, HDRSIZE) < 0) {
76         fprintf(stderr, "ptdump: error reading %s: %s\n",
77                 file, sys_errlist[errno]);
78         exit (1);
79     }
80     uh = (struct ubik_hdr *)buffer;
81     if (ntohl(uh->magic) != UBIK_MAGIC)
82         fprintf(stderr, "ptdump: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
83                 file, ntohl(uh->magic), UBIK_MAGIC);
84     bcopy(&uh->version, &uv, sizeof(struct ubik_version));
85     fprintf(stderr, "Ubik Version is: %D.%d\n",
86             uv.epoch, uv.counter);
87     if (read(fd, &prh, sizeof(struct prheader)) < 0) {
88         fprintf(stderr, "ptdump: error reading %s: %s\n",
89                 file, sys_errlist[errno]);
90         exit (1);
91     }
92     for (i = 0; i < HASHSIZE; i++) {
93         offset = nmflg ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
94         didit = 0;
95         while (offset)
96             offset = display_entry(offset, fd, &didit);
97     }
98     lseek (fd, 0, L_SET);               /* rewind to beginning of file */
99     if (read(fd, buffer, HDRSIZE) < 0) {
100         fprintf(stderr, "ptdump: error reading %s: %s\n",
101                 file, sys_errlist[errno]);
102         exit (1);
103     }
104     uh = (struct ubik_hdr *)buffer;
105     if ((uh->version.epoch != uv.epoch) ||
106         (uh->version.counter != uv.counter)) {
107         fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
108         fprintf(stderr, "Old Version = %D.%d, new version = %D.%d\n",
109                 uv.epoch, uv.counter, uh->version.epoch,
110                 uh->version.counter);
111     }
112     close (fd);
113     exit (0);
114 }
115 int display_entry (offset, fd, didit)
116 int offset, fd;
117 int *didit;
118 {
119     void display_useful_groups();
120     char *checkin();
121     register int i;
122     lseek (fd, offset+HDRSIZE, L_SET);
123     read(fd, &pre, sizeof(struct prentry));
124     pre.flags = ntohl(pre.flags);
125     pre.id = ntohl(pre.id);
126     pre.cellid = ntohl(pre.cellid);
127     pre.next = ntohl(pre.next);
128     pre.nextID = ntohl(pre.nextID);
129     pre.nextName = ntohl(pre.nextName);
130     pre.owner = ntohl(pre.owner);
131     pre.creator = ntohl(pre.creator);
132     pre.ngroups = ntohl(pre.ngroups);
133     pre.nusers = ntohl(pre.nusers);
134     pre.count = ntohl(pre.count);
135     pre.instance = ntohl(pre.instance);
136     pre.owned = ntohl(pre.owned);
137     pre.nextOwned = ntohl(pre.nextOwned);
138     pre.parent = ntohl(pre.parent);
139     pre.sibling = ntohl(pre.sibling);
140     pre.child = ntohl(pre.child);
141     for (i = 0; i < PRSIZE; i++) {
142         pre.entries[i] = ntohl(pre.entries[i]);
143     }
144     if ((pre.flags & PRFREE) == 0) { /* A normal user */
145         if (cflag) (void) checkin(&pre);
146         if (((pre.flags & PRGRP) && grpflag) ||
147             (((pre.flags & PRGRP) == 0) && !grpflag)) {
148             if (!*didit && !cflag) {
149                 *didit = 1;
150                 printf("==========\n");
151             }
152             if (!cflag)
153                 printf("Name: %s ID: %D\n", pre.name, pre.id);
154             else display_useful_groups(&pre, fd);
155         }
156     }
157     return(nmflg ? pre.nextName : pre.nextID);
158 }
159 static struct contentry prco;
160 void display_useful_groups(pre, fd)
161 register struct prentry *pre;
162 int fd;
163 {
164     register int i;
165     register int offset;
166     char *id_to_name();
167     if (pre->entries[0] == 0) return;
168     printf("Group: %s\n", pre->name);
169     for (i = 0; i < PRSIZE; i++) {
170         if (pre->entries[i] == 0) break;
171         printf("   Member:  %s\n", id_to_name(pre->entries[i], fd));
172     }
173     if (i == PRSIZE) {
174         offset = pre->next;
175         while (offset) {
176             lseek(fd, offset+HDRSIZE, L_SET);
177             read(fd, &prco, sizeof(struct contentry));
178             prco.next = ntohl(prco.next);
179             for (i = 0; i < COSIZE; i++) {
180                 prco.entries[i] = ntohl(prco.entries[i]);
181                 if (prco.entries[i] == 0) break;
182                 printf("   Member(co):  %s\n", id_to_name(prco.entries[i], fd));
183             }
184             if ((i == COSIZE) && prco.next)
185                 offset = prco.next;
186             else offset = 0;
187         }
188     }
189 }
190 char *id_to_name(id, fd)
191 int id;
192 int fd;
193 {
194     register int offset;
195     struct prentry pre;
196     char *name;
197     char *check_core();
198     char *checkin();
199     long IDHash();
200     name = check_core(id);
201     if (name != NULL) return(name);
202     offset = ntohl(prh.idHash[IDHash(id)]);
203     if (offset == NULL) return("NOT FOUND");
204     while (offset) {
205         lseek(fd, offset+HDRSIZE, L_SET);
206         if (read(fd, &pre, sizeof(struct prentry)) < 0) {
207             fprintf(stderr, "ptdump: read i/o error: %s\n",
208                     sys_errlist[errno]);
209             exit (1);
210         }
211         pre.id = ntohl(pre.id);
212         if (pre.id == id) {
213             name = checkin(&pre);
214             return(name);
215         }
216         offset = ntohl(pre.nextID);
217     }
218     return("NOT FOUND");
219 }
220 struct hash_entry {
221     char h_name[PR_MAXNAMELEN];
222     int h_id;
223     struct hash_entry *next;
224 };
225 struct hash_entry *hat[HASHSIZE];
226 char *checkin(pre)
227 struct prentry *pre;
228 {
229     struct hash_entry *he, *last;
230     register int id;
231     long IDHash();
232     id = pre->id;
233     last = (struct hash_entry *)0;
234     he = hat[IDHash(id)];
235     while (he) {
236         if (id == he->h_id) return(he->h_name);
237         last = he;
238         he = he->next;
239     }
240     he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
241     if (he == NULL) {
242         fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
243         exit (1);
244     }
245     he->h_id = id;
246     he->next = (struct hash_entry *)0;
247     strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
248     if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
249     else last->next = he;
250     return(he->h_name);
251 }
252 char *check_core(id)
253 register int id;
254 {
255     struct hash_entry *he;
256     long IDHash();
257     he = hat[IDHash(id)];
258     while (he) {
259         if (id == he->h_id) return(he->h_name);
260         he = he->next;
261     }
262     return(NULL);
263 }
264
265 long IDHash(x)
266 long x;
267 {
268     /* returns hash bucket for x */
269     return ((abs(x)) % HASHSIZE);
270 }
This page took 0.125518 seconds and 5 git commands to generate.