]> andersk Git - moira.git/blame - afssync/pt_util.c
Assume we're using OpenAFS headers/libraries. Changes include:
[moira.git] / afssync / pt_util.c
CommitLineData
19afa59e 1/*
2 *
53b8dd38 3 * pt_util: Program to dump the AFS protection server database
19afa59e 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>
19afa59e 17#include <sys/file.h>
302d677f 18#include <stdlib.h>
19afa59e 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"
302d677f 30#include "vis.h"
19afa59e 31
32#define IDHash(x) (abs(x) % HASHSIZE)
c65cba46 33#define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
34 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
19afa59e 35
36extern char *optarg;
37extern int optind;
38extern int errno;
39extern char *sys_errlist[];
40#define strerror(a) sys_errlist[a]
41
42int display_entry();
43void add_group();
44void display_groups();
45void display_group();
46void fix_pre();
47char *checkin();
48char *check_core();
49char *id_to_name();
50
51struct hash_entry {
52 char h_name[PR_MAXNAMELEN];
53 int h_id;
54 struct hash_entry *next;
55};
56struct hash_entry *hat[HASHSIZE];
57
58static struct contentry prco;
59static struct prentry pre;
60static struct prheader prh;
61static struct ubik_version uv;
62
63struct grp_list {
64 struct grp_list *next;
65 long groups[1024];
66};
67static struct grp_list *grp_head=0;
68static long grp_count=0;
69
70struct usr_list {
71 struct usr_list *next;
72 char name[PR_MAXNAMELEN];
73 long uid;
19afa59e 74};
75static struct usr_list *usr_head=0;
76
77char buffer[1024];
78int dbase_fd;
79FILE *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
90int nflag = 0;
91int wflag = 0;
92int flags = 0;
93
94main(argc, argv)
95int argc;
96char **argv;
97{
19afa59e 98 register int i;
ebf8510d 99 register long code;
100 long cc, upos, gpos;
101 struct prentry uentry, gentry;
102 struct ubik_hdr *uh;
19afa59e 103 char *dfile = 0;
ebf8510d 104 char *pfile = "/usr/afs/db/prdb.DB0";
302d677f 105 char *str;
19afa59e 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,
eb8bd30b 138 "Usage: pt_util [options] [-d data] [-p prdb]\n");
19afa59e 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) {
53b8dd38 151 fprintf(stderr, "pt_util: cannot open %s: %s\n",
19afa59e 152 pfile, sys_errlist[errno]);
153 exit (1);
154 }
155 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
53b8dd38 156 fprintf(stderr, "pt_util: error reading %s: %s\n",
19afa59e 157 pfile, sys_errlist[errno]);
158 exit (1);
159 }
160
161 if (dfile) {
162 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
53b8dd38 163 fprintf(stderr, "pt_util: error opening %s: %s\n",
19afa59e 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)
53b8dd38 172 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
19afa59e 173 pfile, ntohl(uh->magic), UBIK_MAGIC);
c7e4633e 174 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
07c68477 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) {
53b8dd38 180 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
07c68477 181 pfile, sys_errlist[errno]);
182 exit (1);
183 }
184 }
19afa59e 185 fprintf(stderr, "Ubik Version is: %d.%d\n",
186 uv.epoch, uv.counter);
187 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
53b8dd38 188 fprintf(stderr, "pt_util: error reading %s: %s\n",
19afa59e 189 pfile, sys_errlist[errno]);
190 exit (1);
191 }
192
ebf8510d 193 Initdb();
19afa59e 194 initialize_pt_error_table();
19afa59e 195
196 if (wflag) {
6518f874 197 struct usr_list *u;
198
19afa59e 199 while(fgets(buffer, sizeof(buffer), dfp)) {
19afa59e 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);
d61de718 205
19afa59e 206 for (u=usr_head; u; u=u->next)
207 if (u->uid && u->uid==uid) break;
208 if (u) {
ebf8510d 209 /* Add user - deferred because it is probably foreign */
19afa59e 210 u->uid = 0;
ebf8510d 211 if (FindByID(0, uid))
212 code = PRIDEXIST;
d61de718 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 }
ebf8510d 221 code = CreateEntry
222 (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
223 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
d61de718 224 }
ebf8510d 225 if (code)
19afa59e 226 fprintf(stderr, "Error while creating %s: %s\n",
ebf8510d 227 u->name, error_message(code));
d61de718 228 continue;
19afa59e 229 }
ebf8510d 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));
19afa59e 244 } else {
245 sscanf(buffer, "%s %d/%d %d %d %d",
246 name, &flags, &quota, &id, &oid, &cid);
ebf8510d 247
302d677f 248 str = malloc(strlen(name) + 1);
249 if (!str)
250 {
251 fprintf(stderr, "malloc failed!");
252 exit(1);
253 }
254 strunvis(str, name);
d2b471ce 255 strncpy(name, str, PR_MAXNAMELEN);
256 name[PR_MAXNAMELEN] = '\0';
ebf8510d 257 if (FindByID(0, id))
258 code = PRIDEXIST;
259 else
260 code = CreateEntry(0, name, &id, 1/*idflag*/,
261 flags&PRGRP, oid, cid);
ebf8510d 262 if (code == PRBADNAM) {
19afa59e 263 u = (struct usr_list *)malloc(sizeof(struct usr_list));
264 u->next = usr_head;
265 u->uid = id;
19afa59e 266 strcpy(u->name, name);
267 usr_head = u;
ebf8510d 268 } else
ebf8510d 269 if (code) {
19afa59e 270 fprintf(stderr, "Error while creating %s: %s\n",
ebf8510d 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;
35f22231 278 gentry.ngroups = quota;
ebf8510d 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));
19afa59e 284 }
19afa59e 285 }
286 }
6518f874 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));
19afa59e 291 } else {
292 for (i = 0; i < HASHSIZE; i++) {
ebf8510d 293 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
294 while (upos)
295 upos = display_entry(upos);
19afa59e 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) {
53b8dd38 303 fprintf(stderr, "pt_util: error reading %s: %s\n",
19afa59e 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)) {
53b8dd38 310 fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
19afa59e 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
319int display_entry (offset)
320int 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
344void 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
360void 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
378void 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) {
53b8dd38 388 fprintf(stderr, "pt_util: read i/o error: %s\n",
19afa59e 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
450void fix_pre(pre)
451 struct prentry *pre;
452{
453 register int i;
302d677f 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);
d2b471ce 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 }
19afa59e 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
494char *id_to_name(id)
495int 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) {
53b8dd38 507 fprintf(stderr, "pt_util: read i/o error: %s\n",
19afa59e 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
521char *checkin(pre)
522struct 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) {
53b8dd38 537 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
19afa59e 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
548char *check_core(id)
549register 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.155219 seconds and 5 git commands to generate.