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