]>
Commit | Line | Data |
---|---|---|
d1b2a10e | 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> | |
b4cd3401 | 16 | #include <string.h> |
d1b2a10e | 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 | int need_gwrite = 0; | |
104 | ||
105 | while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) { | |
106 | switch (cc) { | |
107 | case 'p': | |
108 | pfile = optarg; | |
109 | break; | |
110 | case 'd': | |
111 | dfile = optarg; | |
112 | break; | |
113 | case 'n': | |
114 | nflag++; | |
115 | break; | |
116 | case 'w': | |
117 | wflag++; | |
118 | break; | |
119 | case 'u': | |
120 | flags |= DO_USR; | |
121 | break; | |
122 | case 'm': | |
123 | flags |= (DO_GRP|DO_MEM); | |
124 | break; | |
125 | case 'g': | |
126 | flags |= DO_GRP; | |
127 | break; | |
128 | case 's': | |
129 | flags |= DO_SYS; | |
130 | break; | |
131 | case 'x': | |
132 | flags |= DO_OTR; | |
133 | break; | |
134 | default: | |
135 | fprintf(stderr, | |
136 | "Usage: ptdump [options] [-d data] [-p prdb]\n"); | |
137 | fputs(" Options:\n", stderr); | |
138 | fputs(" -w Update prdb with contents of data file\n", stderr); | |
139 | fputs(" -u Display users\n", stderr); | |
140 | fputs(" -g Display groups\n", stderr); | |
141 | fputs(" -m Display group members\n", stderr); | |
142 | fputs(" -n Follow name hash chains (not id hashes)\n", stderr); | |
143 | fputs(" -s Display only system (Moira) data\n", stderr); | |
144 | fputs(" -x Display extra users/groups\n", stderr); | |
145 | exit(1); | |
146 | } | |
147 | } | |
148 | if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) { | |
149 | fprintf(stderr, "ptdump: cannot open %s: %s\n", | |
150 | pfile, sys_errlist[errno]); | |
151 | exit (1); | |
152 | } | |
153 | if (read(dbase_fd, buffer, HDRSIZE) < 0) { | |
154 | fprintf(stderr, "ptdump: error reading %s: %s\n", | |
155 | pfile, sys_errlist[errno]); | |
156 | exit (1); | |
157 | } | |
158 | ||
159 | if (dfile) { | |
160 | if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) { | |
161 | fprintf(stderr, "ptdump: error opening %s: %s\n", | |
162 | dfile, sys_errlist[errno]); | |
163 | exit(1); | |
164 | } | |
165 | } else | |
166 | dfp = (wflag ? stdin : stdout); | |
167 | ||
168 | uh = (struct ubik_hdr *)buffer; | |
169 | if (ntohl(uh->magic) != UBIK_MAGIC) | |
170 | fprintf(stderr, "ptdump: %s: Bad UBIK_MAGIC. Is %x should be %x\n", | |
171 | pfile, ntohl(uh->magic), UBIK_MAGIC); | |
b4cd3401 | 172 | memcpy(&uv, &uh->version, sizeof(struct ubik_version)); |
d1b2a10e | 173 | fprintf(stderr, "Ubik Version is: %d.%d\n", |
174 | ntohl(uv.epoch), ntohl(uv.counter)); | |
175 | if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) { | |
176 | fprintf(stderr, "ptdump: error reading %s: %s\n", | |
177 | pfile, sys_errlist[errno]); | |
178 | exit (1); | |
179 | } | |
180 | ||
181 | Initdb(); | |
182 | initialize_pt_error_table(); | |
183 | ||
184 | #define FLUSH_GROUP \ | |
185 | if (need_gwrite) { \ | |
186 | code = pr_WriteEntry(0,0,gpos,&gentry); \ | |
187 | if (code) \ | |
188 | fprintf(stderr, "Error while writing group %s: %s\n", \ | |
189 | gentry.name, error_message(code)); \ | |
190 | else need_write = 0; \ | |
191 | } | |
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 (upos = 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 | } | |
231 | if (!code && !upos) { | |
232 | upos = FindByID(0,uid); | |
233 | if (upos) code = pr_ReadEntry(0,0,upos,&uentry); | |
234 | } | |
235 | if (!upos || !gpos) | |
236 | code = PRNOENT; | |
237 | ||
238 | /* Fast group update */ | |
239 | if (!code && (gentry.count < PRSIZE)) { | |
240 | gentry.count++; | |
241 | for (i=0; i<PRSIZE; i++) { | |
242 | if (gentry.entries[i] == PRBADID) { | |
243 | gentry.entries[i] = uid; | |
244 | break; | |
245 | } | |
246 | } | |
247 | } else { | |
248 | FLUSH_GROUP; | |
249 | if (!code) code = AddToEntry (0, &gentry, gpos, uid); | |
250 | } | |
251 | ||
252 | if (!code) code = AddToEntry (0, &uentry, upos, id); | |
253 | ||
254 | if (code) | |
255 | fprintf(stderr, "Error while adding %s to %s: %s\n", | |
256 | mem, name, error_message(code)); | |
257 | } else { | |
258 | FLUSH_GROUP; | |
259 | sscanf(buffer, "%s %d/%d %d %d %d", | |
260 | name, &flags, "a, &id, &oid, &cid); | |
261 | ||
262 | if (gpos = FindByID(0, id)) | |
263 | code = PRIDEXIST; | |
264 | else { | |
265 | code = CreateEntry(0, name, &id, 1/*idflag*/, | |
266 | flags&PRGRP, oid, cid); | |
267 | if (!code) gpos = FindByID(0, id); | |
268 | } | |
269 | if (code == PRBADNAM) { | |
270 | u = (struct usr_list *)malloc(sizeof(struct usr_list)); | |
271 | u->next = usr_head; | |
272 | u->uid = id; | |
273 | strcpy(u->name, name); | |
274 | usr_head = u; | |
275 | } else | |
276 | if (code) { | |
277 | fprintf(stderr, "Error while creating %s: %s\n", | |
278 | name, error_message(code)); | |
279 | } | |
280 | code = pr_ReadEntry(0,0,gpos,&gentry); | |
281 | if (code) | |
282 | fprintf(stderr,"Error while reading group %s: %s\n", | |
283 | name, error_message(code)); | |
284 | else if ((flags&PRACCESS) || | |
285 | (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) { | |
286 | gentry.flags = flags; | |
287 | gentry.ngroups = quota; | |
288 | need_gwrite = 1; | |
289 | if (code) | |
290 | fprintf(stderr, | |
291 | "Error while setting flags on %s: %s\n", | |
292 | name, error_message(code)); | |
293 | } | |
294 | } | |
295 | } | |
296 | FLUSH_GROUP; | |
297 | for (u=usr_head; u; u=u->next) | |
298 | if (u->uid) | |
299 | fprintf(stderr, "Error while creating %s: %s\n", | |
300 | u->name, error_message(PRBADNAM)); | |
301 | } else { | |
302 | for (i = 0; i < HASHSIZE; i++) { | |
303 | upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]); | |
304 | while (upos) | |
305 | upos = display_entry(upos); | |
306 | } | |
307 | if (flags & DO_GRP) | |
308 | display_groups(); | |
309 | } | |
310 | ||
311 | lseek (dbase_fd, 0, L_SET); /* rewind to beginning of file */ | |
312 | if (read(dbase_fd, buffer, HDRSIZE) < 0) { | |
313 | fprintf(stderr, "ptdump: error reading %s: %s\n", | |
314 | pfile, sys_errlist[errno]); | |
315 | exit (1); | |
316 | } | |
317 | uh = (struct ubik_hdr *)buffer; | |
318 | if ((uh->version.epoch != uv.epoch) || | |
319 | (uh->version.counter != uv.counter)) { | |
320 | fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n"); | |
321 | fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n", | |
322 | ntohl(uv.epoch), ntohl(uv.counter), | |
323 | ntohl(uh->version.epoch), ntohl(uh->version.counter)); | |
324 | } | |
325 | close (dbase_fd); | |
326 | exit (0); | |
327 | } | |
328 | ||
329 | int display_entry (offset) | |
330 | int offset; | |
331 | { | |
332 | register int i; | |
333 | ||
334 | lseek (dbase_fd, offset+HDRSIZE, L_SET); | |
335 | read(dbase_fd, &pre, sizeof(struct prentry)); | |
336 | ||
337 | fix_pre(&pre); | |
338 | ||
339 | if ((pre.flags & PRFREE) == 0) { | |
340 | if (pre.flags & PRGRP) { | |
341 | if (flags & DO_GRP) | |
342 | add_group(pre.id); | |
343 | } else { | |
344 | if (print_id(pre.id) && (flags&DO_USR)) | |
345 | fprintf(dfp, FMT_BASE, | |
346 | pre.name, pre.flags, pre.ngroups, | |
347 | pre.id, pre.owner, pre.creator); | |
348 | checkin(&pre); | |
349 | } | |
350 | } | |
351 | return(nflag ? pre.nextName: pre.nextID); | |
352 | } | |
353 | ||
354 | void add_group(id) | |
355 | long id; | |
356 | { | |
357 | struct grp_list *g; | |
358 | register long i; | |
359 | ||
360 | i = grp_count++ % 1024; | |
361 | if (i == 0) { | |
362 | g = (struct grp_list *)malloc(sizeof(struct grp_list)); | |
363 | g->next = grp_head; | |
364 | grp_head = g; | |
365 | } | |
366 | g = grp_head; | |
367 | g->groups[i] = id; | |
368 | } | |
369 | ||
370 | void display_groups() | |
371 | { | |
372 | register int i, id; | |
373 | struct grp_list *g; | |
374 | ||
375 | g = grp_head; | |
376 | while (grp_count--) { | |
377 | i = grp_count%1024; | |
378 | id = g->groups[i]; | |
379 | display_group(id); | |
380 | if (i==0) { | |
381 | grp_head = g->next; | |
382 | free(g); | |
383 | g = grp_head; | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
388 | void display_group(id) | |
389 | int id; | |
390 | { | |
391 | register int i, offset; | |
392 | int print_grp = 0; | |
393 | ||
394 | offset = ntohl(prh.idHash[IDHash(id)]); | |
395 | while (offset) { | |
396 | lseek(dbase_fd, offset+HDRSIZE, L_SET); | |
397 | if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) { | |
398 | fprintf(stderr, "ptdump: read i/o error: %s\n", | |
399 | strerror(errno)); | |
400 | exit (1); | |
401 | } | |
402 | fix_pre(&pre); | |
403 | if (pre.id == id) | |
404 | break; | |
405 | offset = pre.nextID; | |
406 | } | |
407 | ||
408 | if (print_id(id)) { | |
409 | fprintf(dfp, FMT_BASE, | |
410 | pre.name, pre.flags, pre.ngroups, | |
411 | pre.id, pre.owner, pre.creator); | |
412 | print_grp = 1; | |
413 | } | |
414 | ||
415 | if ((flags&DO_MEM) == 0) | |
416 | return; | |
417 | ||
418 | for (i=0; i<PRSIZE; i++) { | |
419 | if ((id=pre.entries[i]) == 0) | |
420 | break; | |
421 | if (id==PRBADID) continue; | |
422 | if (print_id(id) || print_grp==1) { | |
423 | if (print_grp==0) { | |
424 | fprintf(dfp, FMT_BASE, | |
425 | pre.name, pre.flags, pre.ngroups, | |
426 | pre.id, pre.owner, pre.creator); | |
427 | print_grp = 2; | |
428 | } | |
429 | fprintf(dfp, FMT_MEM, id_to_name(id), id); | |
430 | } | |
431 | } | |
432 | if (i == PRSIZE) { | |
433 | offset = pre.next; | |
434 | while (offset) { | |
435 | lseek(dbase_fd, offset+HDRSIZE, L_SET); | |
436 | read(dbase_fd, &prco, sizeof(struct contentry)); | |
437 | prco.next = ntohl(prco.next); | |
438 | for (i = 0; i < COSIZE; i++) { | |
439 | prco.entries[i] = ntohl(prco.entries[i]); | |
440 | if ((id=prco.entries[i]) == 0) | |
441 | break; | |
442 | if (id==PRBADID) continue; | |
443 | if (print_id(id) || print_grp==1) { | |
444 | if (print_grp==0) { | |
445 | fprintf(dfp, FMT_BASE, | |
446 | pre.name, pre.flags, pre.ngroups, | |
447 | pre.id, pre.owner, pre.creator); | |
448 | print_grp = 2; | |
449 | } | |
450 | fprintf(dfp, FMT_MEM, id_to_name(id), id); | |
451 | } | |
452 | } | |
453 | if ((i == COSIZE) && prco.next) | |
454 | offset = prco.next; | |
455 | else offset = 0; | |
456 | } | |
457 | } | |
458 | } | |
459 | ||
460 | void fix_pre(pre) | |
461 | struct prentry *pre; | |
462 | { | |
463 | register int i; | |
464 | ||
465 | pre->flags = ntohl(pre->flags); | |
466 | pre->id = ntohl(pre->id); | |
467 | pre->cellid = ntohl(pre->cellid); | |
468 | pre->next = ntohl(pre->next); | |
469 | pre->nextID = ntohl(pre->nextID); | |
470 | pre->nextName = ntohl(pre->nextName); | |
471 | pre->owner = ntohl(pre->owner); | |
472 | pre->creator = ntohl(pre->creator); | |
473 | pre->ngroups = ntohl(pre->ngroups); | |
474 | pre->nusers = ntohl(pre->nusers); | |
475 | pre->count = ntohl(pre->count); | |
476 | pre->instance = ntohl(pre->instance); | |
477 | pre->owned = ntohl(pre->owned); | |
478 | pre->nextOwned = ntohl(pre->nextOwned); | |
479 | pre->parent = ntohl(pre->parent); | |
480 | pre->sibling = ntohl(pre->sibling); | |
481 | pre->child = ntohl(pre->child); | |
482 | for (i = 0; i < PRSIZE; i++) { | |
483 | pre->entries[i] = ntohl(pre->entries[i]); | |
484 | } | |
485 | } | |
486 | ||
487 | char *id_to_name(id) | |
488 | int id; | |
489 | { | |
490 | register int offset; | |
491 | static struct prentry pre; | |
492 | char *name; | |
493 | ||
494 | name = check_core(id); | |
495 | if (name) return(name); | |
496 | offset = ntohl(prh.idHash[IDHash(id)]); | |
497 | while (offset) { | |
498 | lseek(dbase_fd, offset+HDRSIZE, L_SET); | |
499 | if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) { | |
500 | fprintf(stderr, "ptdump: read i/o error: %s\n", | |
501 | sys_errlist[errno]); | |
502 | exit (1); | |
503 | } | |
504 | pre.id = ntohl(pre.id); | |
505 | if (pre.id == id) { | |
506 | name = checkin(&pre); | |
507 | return(name); | |
508 | } | |
509 | offset = ntohl(pre.nextID); | |
510 | } | |
511 | return 0; | |
512 | } | |
513 | ||
514 | char *checkin(pre) | |
515 | struct prentry *pre; | |
516 | { | |
517 | struct hash_entry *he, *last; | |
518 | register int id; | |
519 | ||
520 | id = pre->id; | |
521 | last = (struct hash_entry *)0; | |
522 | he = hat[IDHash(id)]; | |
523 | while (he) { | |
524 | if (id == he->h_id) return(he->h_name); | |
525 | last = he; | |
526 | he = he->next; | |
527 | } | |
528 | he = (struct hash_entry *)malloc(sizeof(struct hash_entry)); | |
529 | if (he == 0) { | |
530 | fprintf(stderr, "ptdump: No Memory for internal hash table.\n"); | |
531 | exit (1); | |
532 | } | |
533 | he->h_id = id; | |
534 | he->next = (struct hash_entry *)0; | |
535 | strncpy(he->h_name, pre->name, PR_MAXNAMELEN); | |
536 | if (last == (struct hash_entry *)0) hat[IDHash(id)] = he; | |
537 | else last->next = he; | |
538 | return(he->h_name); | |
539 | } | |
540 | ||
541 | char *check_core(id) | |
542 | register int id; | |
543 | { | |
544 | struct hash_entry *he; | |
545 | he = hat[IDHash(id)]; | |
546 | while (he) { | |
547 | if (id == he->h_id) return(he->h_name); | |
548 | he = he->next; | |
549 | } | |
550 | return 0; | |
551 | } |