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