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