pt_util: be admin
[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,
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;
240
241         while (fgets(buffer, sizeof(buffer), dfp)) {
242             int id, oid, cid, flags, 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     read(dbase_fd, &pre, sizeof(struct prentry));
398
399     fix_pre(&pre);
400
401     if ((pre.flags & PRFREE) == 0) {
402         if (pre.flags & PRGRP) {
403             if (flags & DO_GRP)
404                 add_group(pre.id);
405         } else {
406             if (print_id(pre.id) && (flags & DO_USR))
407                 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
408                         pre.id, pre.owner, pre.creator);
409             checkin(&pre);
410         }
411     }
412     return (nflag ? pre.nextName : pre.nextID);
413 }
414
415 static void
416 add_group(long id)
417 {
418     struct grp_list *g;
419     long i;
420
421     i = grp_count++ % 1024;
422     if (i == 0) {
423         g = malloc(sizeof(struct grp_list));
424         g->next = grp_head;
425         grp_head = g;
426     }
427     g = grp_head;
428     g->groups[i] = id;
429 }
430
431 static void
432 display_groups(void)
433 {
434     int i, id;
435     struct grp_list *g;
436
437     g = grp_head;
438     while (grp_count--) {
439         i = grp_count % 1024;
440         id = g->groups[i];
441         display_group(id);
442         if (i == 0) {
443             grp_head = g->next;
444             free(g);
445             g = grp_head;
446         }
447     }
448 }
449
450 static void
451 display_group(int id)
452 {
453     int i, offset;
454     int print_grp = 0;
455
456     offset = ntohl(prh.idHash[IDHash(id)]);
457     while (offset) {
458         lseek(dbase_fd, offset + HDRSIZE, L_SET);
459         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
460             fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
461             exit(1);
462         }
463         fix_pre(&pre);
464         if (pre.id == id)
465             break;
466         offset = pre.nextID;
467     }
468
469     if (print_id(id)) {
470         fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
471                 pre.owner, pre.creator);
472         print_grp = 1;
473     }
474
475     if ((flags & DO_MEM) == 0)
476         return;
477
478     for (i = 0; i < PRSIZE; i++) {
479         if ((id = pre.entries[i]) == 0)
480             break;
481         if (id == PRBADID)
482             continue;
483         if (print_id(id) || print_grp == 1) {
484             if (print_grp == 0) {
485                 fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
486                         pre.id, pre.owner, pre.creator);
487                 print_grp = 2;
488             }
489             fprintf(dfp, FMT_MEM, id_to_name(id), id);
490         }
491     }
492     if (i == PRSIZE) {
493         offset = pre.next;
494         while (offset) {
495             lseek(dbase_fd, offset + HDRSIZE, L_SET);
496             read(dbase_fd, &prco, sizeof(struct contentry));
497             prco.next = ntohl(prco.next);
498             for (i = 0; i < COSIZE; i++) {
499                 prco.entries[i] = ntohl(prco.entries[i]);
500                 if ((id = prco.entries[i]) == 0)
501                     break;
502                 if (id == PRBADID)
503                     continue;
504                 if (print_id(id) || print_grp == 1) {
505                     if (print_grp == 0) {
506                         fprintf(dfp, FMT_BASE, pre.name, pre.flags,
507                                 pre.ngroups, pre.id, pre.owner, pre.creator);
508                         print_grp = 2;
509                     }
510                     fprintf(dfp, FMT_MEM, id_to_name(id), id);
511                 }
512             }
513             if ((i == COSIZE) && prco.next)
514                 offset = prco.next;
515             else
516                 offset = 0;
517         }
518     }
519 }
520
521 static void
522 fix_pre(struct prentry *pre)
523 {
524     int i;
525
526     pre->flags = ntohl(pre->flags);
527     pre->id = ntohl(pre->id);
528     pre->cellid = ntohl(pre->cellid);
529     pre->next = ntohl(pre->next);
530     pre->nextID = ntohl(pre->nextID);
531     pre->nextName = ntohl(pre->nextName);
532     pre->owner = ntohl(pre->owner);
533     pre->creator = ntohl(pre->creator);
534     pre->ngroups = ntohl(pre->ngroups);
535     pre->nusers = ntohl(pre->nusers);
536     pre->count = ntohl(pre->count);
537     pre->instance = ntohl(pre->instance);
538     pre->owned = ntohl(pre->owned);
539     pre->nextOwned = ntohl(pre->nextOwned);
540     pre->parent = ntohl(pre->parent);
541     pre->sibling = ntohl(pre->sibling);
542     pre->child = ntohl(pre->child);
543     for (i = 0; i < PRSIZE; i++) {
544         pre->entries[i] = ntohl(pre->entries[i]);
545     }
546 }
547
548 static char *
549 id_to_name(int id)
550 {
551     int offset;
552     static struct prentry pre;
553     char *name;
554
555     name = check_core(id);
556     if (name)
557         return (name);
558     offset = ntohl(prh.idHash[IDHash(id)]);
559     while (offset) {
560         lseek(dbase_fd, offset + HDRSIZE, L_SET);
561         if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
562             fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
563             exit(1);
564         }
565         pre.id = ntohl(pre.id);
566         if (pre.id == id) {
567             name = checkin(&pre);
568             return (name);
569         }
570         offset = ntohl(pre.nextID);
571     }
572     return 0;
573 }
574
575 static char *
576 checkin(struct prentry *pre)
577 {
578     struct hash_entry *he, *last;
579     int id;
580
581     id = pre->id;
582     last = (struct hash_entry *)0;
583     he = hat[IDHash(id)];
584     while (he) {
585         if (id == he->h_id)
586             return (he->h_name);
587         last = he;
588         he = he->next;
589     }
590     he = malloc(sizeof(struct hash_entry));
591     if (he == 0) {
592         fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
593         exit(1);
594     }
595     he->h_id = id;
596     he->next = (struct hash_entry *)0;
597     strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
598     if (last == (struct hash_entry *)0)
599         hat[IDHash(id)] = he;
600     else
601         last->next = he;
602     return (he->h_name);
603 }
604
605 static char *
606 check_core(int id)
607 {
608     struct hash_entry *he;
609     he = hat[IDHash(id)];
610     while (he) {
611         if (id == he->h_id)
612             return (he->h_name);
613         he = he->next;
614     }
615     return 0;
616 }