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