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