Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / ptserver / pt_util.c
1 /* $Id$ */
2
3 /*
4 *
5 * pt_util: Program to dump the AFS protection server database
6 * into an ascii file.
7 *
8 * Assumptions: We *cheat* here and read the datafile directly, ie.
9 * not going through the ubik distributed data manager.
10 * therefore the database must be quiescent for the
11 * output of this program to be valid.
12 */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 #include <roken.h>
18
19 #ifndef _WIN32
20 #include <sys/file.h>
21 #else
22 #define L_SET SEEK_SET
23 #endif
24 #include <ctype.h>
25
26 #include <afs/com_err.h>
27 #include <afs/cmd.h> /*Command line parsing */
28 #include <afs/afsutil.h>
29 #include <lock.h>
30 #define UBIK_INTERNALS
31 #include <ubik.h>
32 #include <rx/xdr.h>
33 #include <rx/rx.h>
34
35 #include "ptint.h"
36 #include "ptserver.h"
37 #include "pterror.h"
38 #include "ptprototypes.h"
39
40 #define IDHash(x) (abs(x) % HASHSIZE)
41 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
42 ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
43
44 extern char *optarg;
45 extern int optind;
46
47 extern int pr_noAuth;
48
49 int restricted = 0;
50
51 static int display_entry(int);
52 static void add_group(long);
53 static void display_groups(void);
54 static void display_group(int);
55 static void fix_pre(struct prentry *);
56 static char *id_to_name(int);
57 static char *checkin(struct prentry *);
58 static char *check_core(int);
59 static int CommandProc(struct cmd_syndesc *, void *);
60
61 struct hash_entry {
62 char h_name[PR_MAXNAMELEN];
63 int h_id;
64 struct hash_entry *next;
65 };
66 struct hash_entry *hat[HASHSIZE];
67
68 static struct contentry prco;
69 static struct prentry pre;
70 static struct prheader prh;
71 static struct ubik_version uv;
72
73 struct grp_list {
74 struct grp_list *next;
75 long groups[1024];
76 };
77 static struct grp_list *grp_head = 0;
78 static long grp_count = 0;
79
80 struct usr_list {
81 struct usr_list *next;
82 char name[PR_MAXNAMELEN];
83 long uid;
84 };
85 static struct usr_list *usr_head = 0;
86
87 char buffer[1024];
88 int dbase_fd;
89 FILE *dfp;
90
91 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
92 #define FMT_MEM " %-8s %d\n"
93
94 #define DO_USR 1
95 #define DO_GRP 2
96 #define DO_MEM 4
97 #define DO_SYS 8
98 #define DO_OTR 16
99
100 int nflag = 0;
101 int wflag = 0;
102 int flags = 0;
103
104 int
105 main(int argc, char **argv)
106 {
107
108 struct cmd_syndesc *cs; /*Command line syntax descriptor */
109 afs_int32 code; /*Return code */
110
111 cs = cmd_CreateSyntax(NULL, CommandProc, NULL, 0,
112 "access protection database");
113 cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
114 "update prdb with contents of data file");
115 cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
116 cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
117 cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
118 "display group members");
119 cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
120 "follow name hash chains (not id hashes)");
121 cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
122 "display only system data");
123 cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
124 "display extra users/groups");
125 cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
126 cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
127 code = cmd_Dispatch(argc, argv);
128
129 exit(code);
130
131 }
132
133 static int
134 CommandProc(struct cmd_syndesc *a_as, void *arock)
135 {
136 int i;
137 long code = 0;
138 long upos;
139 long gpos = 0;
140 struct prentry uentry, gentry;
141 struct ubik_hdr *uh;
142 char *dfile = 0;
143 const char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
144 char *pfile = NULL;
145 char pbuffer[1028];
146 struct cmd_parmdesc *tparm;
147
148 tparm = a_as->parms;
149
150 if (tparm[0].items) {
151 wflag++;
152 /* so we are treated as admin and can create "mis"owned
153 groups */
154 pr_noAuth = 1;
155 }
156 if (tparm[1].items) {
157 flags |= DO_USR;
158 }
159 if (tparm[2].items) {
160 flags |= DO_GRP;
161 }
162 if (tparm[3].items) {
163 flags |= (DO_GRP | DO_MEM);
164 }
165 if (tparm[4].items) {
166 nflag++;
167 }
168 if (tparm[5].items) {
169 flags |= DO_SYS;
170 }
171 if (tparm[6].items) {
172 flags |= DO_OTR;
173 }
174 if (tparm[7].items) {
175 pfile = tparm[7].items->data;
176 }
177 if (tparm[8].items) {
178 dfile = tparm[8].items->data;
179 }
180
181 if (pfile == NULL) {
182 snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
183 pfile = pbuffer;
184 }
185 if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
186 < 0) {
187 fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
188 strerror(errno));
189 exit(1);
190 }
191 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
192 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
193 strerror(errno));
194 exit(1);
195 }
196
197 if (dfile) {
198 if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
199 fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
200 strerror(errno));
201 exit(1);
202 }
203 } else
204 dfp = (wflag ? stdin : stdout);
205
206 uh = (struct ubik_hdr *)buffer;
207 if (ntohl(uh->magic) != UBIK_MAGIC)
208 fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
209 pfile, ntohl(uh->magic), UBIK_MAGIC);
210 memcpy(&uv, &uh->version, sizeof(struct ubik_version));
211
212 if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
213 uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
214 memcpy(&uh->version, &uv, sizeof(struct ubik_version));
215 lseek(dbase_fd, 0, SEEK_SET);
216 if (write(dbase_fd, buffer, HDRSIZE) < 0) {
217 fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
218 pfile, strerror(errno));
219 exit(1);
220 }
221 }
222
223 /* Now that any writeback is done, swap these */
224 uv.epoch = ntohl(uv.epoch);
225 uv.counter = ntohl(uv.counter);
226
227 fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
228 if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
229 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
230 strerror(errno));
231 exit(1);
232 }
233
234 Initdb();
235 initialize_PT_error_table();
236
237 if (wflag) {
238 struct usr_list *u;
239 int seenGroup = 0, id = 0, flags = 0;
240
241 while (fgets(buffer, sizeof(buffer), dfp)) {
242 int oid, cid, quota, uid;
243 char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
244
245 if (isspace(*buffer)) {
246 code = sscanf(buffer, "%s %d", mem, &uid);
247 if (code != 2) {
248 fprintf(stderr,
249 "Insuffient data provided for group membership\n");
250 exit(1);
251 }
252
253 if (!seenGroup) {
254 fprintf(stderr,
255 "Group member %s listed outside of group\n",
256 mem);
257 exit(1);
258 }
259
260 for (u = usr_head; u; u = u->next)
261 if (u->uid && u->uid == uid)
262 break;
263 if (u) {
264 /* Add user - deferred because it is probably foreign */
265 u->uid = 0;
266 if (FindByID(0, uid))
267 code = PRIDEXIST;
268 else {
269 if (!code
270 && (flags & (PRGRP | PRQUOTA)) ==
271 (PRGRP | PRQUOTA)) {
272 gentry.ngroups++;
273 code = pr_WriteEntry(0, 0, gpos, &gentry);
274 if (code)
275 fprintf(stderr,
276 "Error setting group count on %s: %s\n",
277 name, afs_error_message(code));
278 }
279 code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
280 1 /*gflag */ ,
281 SYSADMINID /*oid */ ,
282 SYSADMINID /*cid */ );
283 }
284 if (code)
285 fprintf(stderr, "Error while creating %s: %s\n",
286 u->name, afs_error_message(code));
287 continue;
288 }
289 /* Add user to group */
290 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
291 code = PRPERM;
292 } else if ((upos = FindByID(0, uid))
293 && (gpos = FindByID(0, id))) {
294 code = pr_ReadEntry(0, 0, upos, &uentry);
295 if (!code)
296 code = pr_ReadEntry(0, 0, gpos, &gentry);
297 if (!code)
298 code = AddToEntry(0, &gentry, gpos, uid);
299 if (!code)
300 code = AddToEntry(0, &uentry, upos, id);
301 } else
302 code = PRNOENT;
303
304 if (code)
305 fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
306 name, afs_error_message(code));
307 } else {
308 code = sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, &quota, &id,
309 &oid, &cid);
310 if (code != 6) {
311 fprintf(stderr,
312 "Insufficient data provided for user/group\n");
313 exit(1);
314 }
315
316 seenGroup = 1;
317
318 if (FindByID(0, id))
319 code = PRIDEXIST;
320 else
321 code = CreateEntry(0, name, &id, 1 /*idflag */ ,
322 flags & PRGRP, oid, cid);
323 if (code == PRBADNAM) {
324 u = malloc(sizeof(struct usr_list));
325 u->next = usr_head;
326 u->uid = id;
327 strcpy(u->name, name);
328 usr_head = u;
329 } else if (code) {
330 fprintf(stderr, "Error while creating %s: %s\n", name,
331 afs_error_message(code));
332 } else if ((flags & PRACCESS)
333 || (flags & (PRGRP | PRQUOTA)) ==
334 (PRGRP | PRQUOTA)) {
335 gpos = FindByID(0, id);
336 code = pr_ReadEntry(0, 0, gpos, &gentry);
337 if (!code) {
338 gentry.flags = flags;
339 gentry.ngroups = quota;
340 code = pr_WriteEntry(0, 0, gpos, &gentry);
341 }
342 if (code)
343 fprintf(stderr,
344 "Error while setting flags on %s: %s\n", name,
345 afs_error_message(code));
346 }
347 }
348 }
349 for (u = usr_head; u; u = u->next)
350 if (u->uid)
351 fprintf(stderr, "Error while creating %s: %s\n", u->name,
352 afs_error_message(PRBADNAM));
353 } else {
354 for (i = 0; i < HASHSIZE; i++) {
355 upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
356 while (upos) {
357 long newpos;
358 newpos = display_entry(upos);
359 if (newpos == upos) {
360 fprintf(stderr, "pt_util: hash error in %s chain %d\n",
361 nflag ? "name":"id", i);
362 exit(1);
363 } else
364 upos = newpos;
365 }
366 }
367 if (flags & DO_GRP)
368 display_groups();
369 }
370
371 lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
372 if (read(dbase_fd, buffer, HDRSIZE) < 0) {
373 fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
374 strerror(errno));
375 exit(1);
376 }
377 uh = (struct ubik_hdr *)buffer;
378
379 uh->version.epoch = ntohl(uh->version.epoch);
380 uh->version.counter = ntohl(uh->version.counter);
381
382 if ((uh->version.epoch != uv.epoch)
383 || (uh->version.counter != uv.counter)) {
384 fprintf(stderr,
385 "pt_util: Ubik Version number changed during execution.\n");
386 fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
387 uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
388 }
389 close(dbase_fd);
390 exit(0);
391 }
392
393 static int
394 display_entry(int offset)
395 {
396 lseek(dbase_fd, offset + HDRSIZE, L_SET);
397 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
398 fprintf(stderr, "pt_util: error reading entry %d: %s\n",
399 offset, strerror(errno));
400 exit(1);
401 }
402
403 fix_pre(&pre);
404
405 if ((pre.flags & PRFREE) == 0) {
406 if (pre.flags & PRGRP) {
407 if (flags & DO_GRP)
408 add_group(pre.id);
409 } else {
410 if (print_id(pre.id) && (flags & DO_USR))
411 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
412 pre.id, pre.owner, pre.creator);
413 checkin(&pre);
414 }
415 }
416 return (nflag ? pre.nextName : pre.nextID);
417 }
418
419 static void
420 add_group(long id)
421 {
422 struct grp_list *g;
423 long i;
424
425 i = grp_count++ % 1024;
426 if (i == 0) {
427 g = malloc(sizeof(struct grp_list));
428 g->next = grp_head;
429 grp_head = g;
430 }
431 g = grp_head;
432 g->groups[i] = id;
433 }
434
435 static void
436 display_groups(void)
437 {
438 int i, id;
439 struct grp_list *g;
440
441 g = grp_head;
442 while (grp_count--) {
443 i = grp_count % 1024;
444 id = g->groups[i];
445 display_group(id);
446 if (i == 0) {
447 grp_head = g->next;
448 free(g);
449 g = grp_head;
450 }
451 }
452 }
453
454 static void
455 display_group(int id)
456 {
457 int i, offset;
458 int print_grp = 0;
459
460 offset = ntohl(prh.idHash[IDHash(id)]);
461 while (offset) {
462 lseek(dbase_fd, offset + HDRSIZE, L_SET);
463 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
464 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
465 exit(1);
466 }
467 fix_pre(&pre);
468 if (pre.id == id)
469 break;
470 offset = pre.nextID;
471 }
472
473 if (print_id(id)) {
474 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
475 pre.owner, pre.creator);
476 print_grp = 1;
477 }
478
479 if ((flags & DO_MEM) == 0)
480 return;
481
482 for (i = 0; i < PRSIZE; i++) {
483 if ((id = pre.entries[i]) == 0)
484 break;
485 if (id == PRBADID)
486 continue;
487 if (print_id(id) || print_grp == 1) {
488 if (print_grp == 0) {
489 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
490 pre.id, pre.owner, pre.creator);
491 print_grp = 2;
492 }
493 fprintf(dfp, FMT_MEM, id_to_name(id), id);
494 }
495 }
496 if (i == PRSIZE) {
497 offset = pre.next;
498 while (offset) {
499 lseek(dbase_fd, offset + HDRSIZE, L_SET);
500 if (read(dbase_fd, &prco, sizeof(struct contentry)) < 0) {
501 fprintf(stderr, "pt_util: read i/o error: %s\n",
502 strerror(errno));
503 exit(1);
504 }
505 prco.next = ntohl(prco.next);
506 for (i = 0; i < COSIZE; i++) {
507 prco.entries[i] = ntohl(prco.entries[i]);
508 if ((id = prco.entries[i]) == 0)
509 break;
510 if (id == PRBADID)
511 continue;
512 if (print_id(id) || print_grp == 1) {
513 if (print_grp == 0) {
514 fprintf(dfp, FMT_BASE, pre.name, pre.flags,
515 pre.ngroups, pre.id, pre.owner, pre.creator);
516 print_grp = 2;
517 }
518 fprintf(dfp, FMT_MEM, id_to_name(id), id);
519 }
520 }
521 if ((i == COSIZE) && prco.next)
522 offset = prco.next;
523 else
524 offset = 0;
525 }
526 }
527 }
528
529 static void
530 fix_pre(struct prentry *pre)
531 {
532 int i;
533
534 pre->flags = ntohl(pre->flags);
535 pre->id = ntohl(pre->id);
536 pre->cellid = ntohl(pre->cellid);
537 pre->next = ntohl(pre->next);
538 pre->nextID = ntohl(pre->nextID);
539 pre->nextName = ntohl(pre->nextName);
540 pre->owner = ntohl(pre->owner);
541 pre->creator = ntohl(pre->creator);
542 pre->ngroups = ntohl(pre->ngroups);
543 pre->nusers = ntohl(pre->nusers);
544 pre->count = ntohl(pre->count);
545 pre->instance = ntohl(pre->instance);
546 pre->owned = ntohl(pre->owned);
547 pre->nextOwned = ntohl(pre->nextOwned);
548 pre->parent = ntohl(pre->parent);
549 pre->sibling = ntohl(pre->sibling);
550 pre->child = ntohl(pre->child);
551 for (i = 0; i < PRSIZE; i++) {
552 pre->entries[i] = ntohl(pre->entries[i]);
553 }
554 }
555
556 static char *
557 id_to_name(int id)
558 {
559 int offset;
560 static struct prentry pre;
561 char *name;
562
563 name = check_core(id);
564 if (name)
565 return (name);
566 offset = ntohl(prh.idHash[IDHash(id)]);
567 while (offset) {
568 lseek(dbase_fd, offset + HDRSIZE, L_SET);
569 if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
570 fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
571 exit(1);
572 }
573 pre.id = ntohl(pre.id);
574 if (pre.id == id) {
575 name = checkin(&pre);
576 return (name);
577 }
578 offset = ntohl(pre.nextID);
579 }
580 return 0;
581 }
582
583 static char *
584 checkin(struct prentry *pre)
585 {
586 struct hash_entry *he, *last;
587 int id;
588
589 id = pre->id;
590 last = (struct hash_entry *)0;
591 he = hat[IDHash(id)];
592 while (he) {
593 if (id == he->h_id)
594 return (he->h_name);
595 last = he;
596 he = he->next;
597 }
598 he = malloc(sizeof(struct hash_entry));
599 if (he == 0) {
600 fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
601 exit(1);
602 }
603 he->h_id = id;
604 he->next = (struct hash_entry *)0;
605 strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
606 if (last == (struct hash_entry *)0)
607 hat[IDHash(id)] = he;
608 else
609 last->next = he;
610 return (he->h_name);
611 }
612
613 static char *
614 check_core(int id)
615 {
616 struct hash_entry *he;
617 he = hat[IDHash(id)];
618 while (he) {
619 if (id == he->h_id)
620 return (he->h_name);
621 he = he->next;
622 }
623 return 0;
624 }