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