pt_util: Catch sscanf failures
[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 int restricted = 0;
48
49 static int display_entry(int);
50 static void add_group(long);
51 static void display_groups(void);
52 static void display_group(int);
53 static void fix_pre(struct prentry *);
54 static char *id_to_name(int);
55 static char *checkin(struct prentry *);
56 static char *check_core(int);
57 static int CommandProc(struct cmd_syndesc *, void *);
58
59 struct hash_entry {
60     char h_name[PR_MAXNAMELEN];
61     int h_id;
62     struct hash_entry *next;
63 };
64 struct hash_entry *hat[HASHSIZE];
65
66 static struct contentry prco;
67 static struct prentry pre;
68 static struct prheader prh;
69 static struct ubik_version uv;
70
71 struct grp_list {
72     struct grp_list *next;
73     long groups[1024];
74 };
75 static struct grp_list *grp_head = 0;
76 static long grp_count = 0;
77
78 struct usr_list {
79     struct usr_list *next;
80     char name[PR_MAXNAMELEN];
81     long uid;
82 };
83 static struct usr_list *usr_head = 0;
84
85 char buffer[1024];
86 int dbase_fd;
87 FILE *dfp;
88
89 #define FMT_BASE "%-10s %d/%d %d %d %d\n"
90 #define FMT_MEM  "   %-8s %d\n"
91
92 #define DO_USR 1
93 #define DO_GRP 2
94 #define DO_MEM 4
95 #define DO_SYS 8
96 #define DO_OTR 16
97
98 int nflag = 0;
99 int wflag = 0;
100 int flags = 0;
101
102 int
103 main(int argc, char **argv)
104 {
105
106     struct cmd_syndesc *cs;     /*Command line syntax descriptor */
107     afs_int32 code;     /*Return code */
108
109     cs = cmd_CreateSyntax(NULL, CommandProc, NULL,
110                           "access protection database");
111     cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
112                 "update prdb with contents of data file");
113     cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
114     cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
115     cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
116                 "display group members");
117     cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
118                 "follow name hash chains (not id hashes)");
119     cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
120                 "display only system data");
121     cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
122                 "display extra users/groups");
123     cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
124     cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
125     code = cmd_Dispatch(argc, argv);
126
127     exit(code);
128
129 }
130
131 static int
132 CommandProc(struct cmd_syndesc *a_as, void *arock)
133 {
134     int i;
135     long code = 0;
136     long upos;
137     long gpos = 0;
138     struct prentry uentry, gentry;
139     struct ubik_hdr *uh;
140     char *dfile = 0;
141     const char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
142     char *pfile = NULL;
143     char pbuffer[1028];
144     struct cmd_parmdesc *tparm;
145
146     tparm = a_as->parms;
147
148     if (tparm[0].items) {
149         wflag++;
150     }
151     if (tparm[1].items) {
152         flags |= DO_USR;
153     }
154     if (tparm[2].items) {
155         flags |= DO_GRP;
156     }
157     if (tparm[3].items) {
158         flags |= (DO_GRP | DO_MEM);
159     }
160     if (tparm[4].items) {
161         nflag++;
162     }
163     if (tparm[5].items) {
164         flags |= DO_SYS;
165     }
166     if (tparm[6].items) {
167         flags |= DO_OTR;
168     }
169     if (tparm[7].items) {
170         pfile = tparm[7].items->data;
171     }
172     if (tparm[8].items) {
173         dfile = tparm[8].items->data;
174     }
175
176     if (pfile == NULL) {
177         snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
178         pfile = pbuffer;
179     }
180     if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
181         < 0) {
182         fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
183                 strerror(errno));
184         exit(1);
185     }
186     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
187         fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
188                 strerror(errno));
189         exit(1);
190     }
191
192     if (dfile) {
193         if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
194             fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
195                     strerror(errno));
196             exit(1);
197         }
198     } else
199         dfp = (wflag ? stdin : stdout);
200
201     uh = (struct ubik_hdr *)buffer;
202     if (ntohl(uh->magic) != UBIK_MAGIC)
203         fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
204                 pfile, ntohl(uh->magic), UBIK_MAGIC);
205     memcpy(&uv, &uh->version, sizeof(struct ubik_version));
206
207     if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
208         uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
209         memcpy(&uh->version, &uv, sizeof(struct ubik_version));
210         lseek(dbase_fd, 0, SEEK_SET);
211         if (write(dbase_fd, buffer, HDRSIZE) < 0) {
212             fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
213                     pfile, strerror(errno));
214             exit(1);
215         }
216     }
217
218     /* Now that any writeback is done, swap these */
219     uv.epoch = ntohl(uv.epoch);
220     uv.counter = ntohl(uv.counter);
221
222     fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
223     if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
224         fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
225                 strerror(errno));
226         exit(1);
227     }
228
229     Initdb();
230     initialize_PT_error_table();
231
232     if (wflag) {
233         struct usr_list *u;
234
235         while (fgets(buffer, sizeof(buffer), dfp)) {
236             int id, oid, cid, flags, quota, uid;
237             char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
238
239             if (isspace(*buffer)) {
240                 code = sscanf(buffer, "%s %d", mem, &uid);
241                 if (code != 2) {
242                     fprintf(stderr,
243                             "Insuffient data provided for group membership\n");
244                     exit(1);
245                 }
246
247                 for (u = usr_head; u; u = u->next)
248                     if (u->uid && u->uid == uid)
249                         break;
250                 if (u) {
251                     /* Add user - deferred because it is probably foreign */
252                     u->uid = 0;
253                     if (FindByID(0, uid))
254                         code = PRIDEXIST;
255                     else {
256                         if (!code
257                             && (flags & (PRGRP | PRQUOTA)) ==
258                             (PRGRP | PRQUOTA)) {
259                             gentry.ngroups++;
260                             code = pr_WriteEntry(0, 0, gpos, &gentry);
261                             if (code)
262                                 fprintf(stderr,
263                                         "Error setting group count on %s: %s\n",
264                                         name, afs_error_message(code));
265                         }
266                         code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
267                                            1 /*gflag */ ,
268                                            SYSADMINID /*oid */ ,
269                                            SYSADMINID /*cid */ );
270                     }
271                     if (code)
272                         fprintf(stderr, "Error while creating %s: %s\n",
273                                 u->name, afs_error_message(code));
274                     continue;
275                 }
276                 /* Add user to group */
277                 if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
278                     code = PRPERM;
279                 } else if ((upos = FindByID(0, uid))
280                            && (gpos = FindByID(0, id))) {
281                     code = pr_ReadEntry(0, 0, upos, &uentry);
282                     if (!code)
283                         code = pr_ReadEntry(0, 0, gpos, &gentry);
284                     if (!code)
285                         code = AddToEntry(0, &gentry, gpos, uid);
286                     if (!code)
287                         code = AddToEntry(0, &uentry, upos, id);
288                 } else
289                     code = PRNOENT;
290
291                 if (code)
292                     fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
293                             name, afs_error_message(code));
294             } else {
295                 code = sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, &quota, &id,
296                               &oid, &cid);
297                 if (code != 6) {
298                     fprintf(stderr,
299                             "Insufficient data provided for user/group\n");
300                     exit(1);
301                 }
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 = 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 = 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 = 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 }