]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/setfmac/setfmac.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / setfmac / setfmac.c
1 /*-
2  * Copyright (c) 2002, 2004 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by NAI Labs, the
6  * Security Research Division of Network Associates, Inc. under
7  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
8  * CHATS research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33
34 #include <sys/types.h>
35 #include <sys/mac.h>
36 #include <sys/queue.h>
37 #include <sys/stat.h>
38
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fts.h>
43 #include <libgen.h>
44 #include <regex.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 struct label_spec {
51         struct label_spec_entry {
52                 regex_t regex;  /* compiled regular expression to match */
53                 char *regexstr; /* uncompiled regular expression */
54                 mode_t mode;    /* mode to possibly match */
55                 const char *modestr;    /* print-worthy ",-?" mode string */
56                 char *mactext;  /* MAC label to apply */
57                 int flags;      /* miscellaneous flags */
58 #define         F_DONTLABEL     0x01
59 #define         F_ALWAYSMATCH   0x02
60         } *entries,             /* entries[0..nentries] */
61           *match;               /* cached decision for MAC label to apply */
62         size_t nentries;        /* size of entries list */
63         STAILQ_ENTRY(label_spec) link;
64 };
65
66 struct label_specs {
67         STAILQ_HEAD(label_specs_head, label_spec) head;
68 };
69
70 void usage(int) __dead2;
71 struct label_specs *new_specs(void);
72 void add_specs(struct label_specs *, const char *, int);
73 void add_setfmac_specs(struct label_specs *, char *);
74 void add_spec_line(const char *, int, struct label_spec_entry *, char *);
75 int apply_specs(struct label_specs *, FTSENT *, int, int);
76 int specs_empty(struct label_specs *);
77
78 static int qflag;
79
80 int
81 main(int argc, char **argv)
82 {
83         FTSENT *ftsent;
84         FTS *fts;
85         struct label_specs *specs;
86         int eflag = 0, xflag = 0, vflag = 0, Rflag = 0, hflag;
87         int ch, is_setfmac;
88         char *bn;
89
90         bn = basename(argv[0]);
91         if (bn == NULL)
92                 err(1, "basename");
93         is_setfmac = strcmp(bn, "setfmac") == 0;
94         hflag = is_setfmac ? FTS_LOGICAL : FTS_PHYSICAL;
95         specs = new_specs();
96         while ((ch = getopt(argc, argv, is_setfmac ? "Rhq" : "ef:qs:vx")) !=
97             -1) {
98                 switch (ch) {
99                 case 'R':
100                         Rflag = 1;
101                         break;
102                 case 'e':
103                         eflag = 1;
104                         break;
105                 case 'f':
106                         add_specs(specs, optarg, 0);
107                         break;
108                 case 'h':
109                         hflag = FTS_PHYSICAL;
110                         break;
111                 case 'q':
112                         qflag = 1;
113                         break;
114                 case 's':
115                         add_specs(specs, optarg, 1);
116                         break;
117                 case 'v':
118                         vflag++;
119                         break;
120                 case 'x':
121                         xflag = FTS_XDEV;
122                         break;
123                 default:
124                         usage(is_setfmac);
125                 }
126         }
127         argc -= optind;
128         argv += optind;
129
130         if (is_setfmac) {
131                 if (argc <= 1)  
132                         usage(is_setfmac);
133                 add_setfmac_specs(specs, *argv);
134                 argc--;
135                 argv++;
136         } else {
137                 if (argc == 0 || specs_empty(specs))
138                         usage(is_setfmac);
139         }
140         fts = fts_open(argv, hflag | xflag, NULL);
141         if (fts == NULL)
142                 err(1, "cannot traverse filesystem%s", argc ? "s" : "");
143         while ((ftsent = fts_read(fts)) != NULL) {
144                 switch (ftsent->fts_info) {
145                 case FTS_DP:            /* skip post-order */
146                         break;
147                 case FTS_D:             /* do pre-order */
148                 case FTS_DC:            /* do cyclic? */
149                         /* don't ever recurse directories as setfmac(8) */
150                         if (is_setfmac && !Rflag)
151                                 fts_set(fts, ftsent, FTS_SKIP);
152                 case FTS_DEFAULT:       /* do default */
153                 case FTS_F:             /* do regular */
154                 case FTS_SL:            /* do symlink */
155                 case FTS_SLNONE:        /* do symlink */
156                 case FTS_W:             /* do whiteout */
157                         if (apply_specs(specs, ftsent, hflag, vflag)) {
158                                 if (eflag) {
159                                         errx(1, "labeling not supported in "
160                                             "%.*s", ftsent->fts_pathlen,
161                                             ftsent->fts_path);
162                                 }
163                                 if (!qflag)
164                                         warnx("labeling not supported in %.*s",
165                                             ftsent->fts_pathlen,
166                                             ftsent->fts_path);
167                                 fts_set(fts, ftsent, FTS_SKIP);
168                         }
169                         break;
170                 case FTS_DNR:           /* die on all errors */
171                 case FTS_ERR:
172                 case FTS_NS:
173                         err(1, "traversing %.*s", ftsent->fts_pathlen,
174                             ftsent->fts_path);
175                 default:
176                         errx(1, "CANNOT HAPPEN (%d) traversing %.*s",
177                             ftsent->fts_info, ftsent->fts_pathlen,
178                             ftsent->fts_path);
179                 }
180         }
181         fts_close(fts);
182         exit(0);
183 }
184
185 void
186 usage(int is_setfmac)
187 {
188
189         if (is_setfmac)
190                 fprintf(stderr, "usage: setfmac [-Rhq] label file ...\n");
191         else
192                 fprintf(stderr, "usage: setfsmac [-ehqvx] [-f specfile [...]] [-s specfile [...]] file ...\n");
193         exit(1);
194 }
195
196 static int
197 chomp_line(char **line, size_t *linesize)
198 {
199         char *s;
200         int freeme = 0;
201         
202         for (s = *line; (unsigned)(s - *line) < *linesize; s++) {
203                 if (!isspace(*s))
204                         break;
205         }
206         if (*s == '#') {
207                 **line = '\0';
208                 *linesize = 0;
209                 return (freeme);
210         }
211         memmove(*line, s, *linesize - (s - *line));
212         *linesize -= s - *line;
213         for (s = &(*line)[*linesize - 1]; s >= *line; s--) {
214                 if (!isspace(*s))
215                         break;
216         }
217         if (s != &(*line)[*linesize - 1]) {
218                 *linesize = s - *line + 1;
219         } else {
220                 s = malloc(*linesize + 1);
221                 if (s == NULL)
222                         err(1, "malloc");
223                 strncpy(s, *line, *linesize);
224                 *line = s;
225                 freeme = 1;
226         }
227         (*line)[*linesize] = '\0';
228         return (freeme);
229 }
230
231 void
232 add_specs(struct label_specs *specs, const char *file, int is_sebsd)
233 {
234         struct label_spec *spec;
235         FILE *fp;
236         char *line;
237         size_t nlines = 0, linesize;
238         int freeline;
239
240         spec = malloc(sizeof(*spec));
241         if (spec == NULL)
242                 err(1, "malloc");
243         fp = fopen(file, "r");
244         if (fp == NULL)
245                 err(1, "opening %s", file);
246         while ((line = fgetln(fp, &linesize)) != NULL) {
247                 freeline = chomp_line(&line, &linesize);
248                 if (linesize > 0) /* only allocate space for non-comments */
249                         nlines++;
250                 if (freeline)
251                         free(line);
252         }
253         if (ferror(fp))
254                 err(1, "fgetln on %s", file);
255         rewind(fp);
256         spec->entries = calloc(nlines, sizeof(*spec->entries));
257         if (spec->entries == NULL)
258                 err(1, "malloc");
259         spec->nentries = nlines;
260         while (nlines > 0) {
261                 line = fgetln(fp, &linesize);
262                 if (line == NULL) {
263                         if (feof(fp))
264                                 errx(1, "%s ended prematurely", file);
265                         else
266                                 err(1, "failure reading %s", file);
267                 }
268                 freeline = chomp_line(&line, &linesize);
269                 if (linesize == 0) {
270                         if (freeline)
271                                 free(line);
272                         continue;
273                 }
274                 add_spec_line(file, is_sebsd, &spec->entries[--nlines], line);
275                 if (freeline)
276                         free(line);
277         }
278         fclose(fp);
279         if (!qflag)
280                 warnx("%s: read %lu specifications", file,
281                     (long)spec->nentries);
282         STAILQ_INSERT_TAIL(&specs->head, spec, link);
283 }
284
285 void
286 add_setfmac_specs(struct label_specs *specs, char *label)
287 {
288         struct label_spec *spec;
289
290         spec = malloc(sizeof(*spec));
291         if (spec == NULL)
292                 err(1, "malloc");
293         spec->nentries = 1;
294         spec->entries = calloc(spec->nentries, sizeof(*spec->entries));
295         if (spec->entries == NULL)
296                 err(1, "malloc");
297         /* The _only_ thing specified here is the mactext! */
298         spec->entries->mactext = label;
299         spec->entries->flags |= F_ALWAYSMATCH;
300         STAILQ_INSERT_TAIL(&specs->head, spec, link);
301 }
302
303 void
304 add_spec_line(const char *file, int is_sebsd, struct label_spec_entry *entry,
305     char *line)
306 {
307         char *regexstr, *modestr, *macstr, *regerrorstr;
308         size_t size;
309         int error;
310
311         regexstr = strtok(line, " \t");
312         if (regexstr == NULL)
313                 errx(1, "%s: need regular expression", file);
314         modestr = strtok(NULL, " \t");
315         if (modestr == NULL)
316                 errx(1, "%s: need a label", file);
317         macstr = strtok(NULL, " \t");
318         if (macstr == NULL) {   /* the mode is just optional */
319                 macstr = modestr;
320                 modestr = NULL;
321         }
322         if (strtok(NULL, " \t") != NULL)
323                 errx(1, "%s: extraneous fields at end of line", file);
324         /* assume we need to anchor this regex */
325         if (asprintf(&regexstr, "^%s$", regexstr) == -1)
326                 err(1, "%s: processing regular expression", file);
327         entry->regexstr = regexstr;
328         error = regcomp(&entry->regex, regexstr, REG_EXTENDED | REG_NOSUB);
329         if (error) {
330                 size = regerror(error, &entry->regex, NULL, 0);
331                 regerrorstr = malloc(size);
332                 if (regerrorstr == NULL)
333                         err(1, "malloc");
334                 (void)regerror(error, &entry->regex, regerrorstr, size);
335                 errx(1, "%s: %s: %s", file, entry->regexstr, regerrorstr);
336         }
337         if (!is_sebsd) {
338                 entry->mactext = strdup(macstr);
339                 if (entry->mactext == NULL)
340                         err(1, "strdup");
341         } else {
342                 if (asprintf(&entry->mactext, "sebsd/%s", macstr) == -1)
343                         err(1, "asprintf");
344                 if (strcmp(macstr, "<<none>>") == 0)
345                         entry->flags |= F_DONTLABEL;
346         }
347         if (modestr != NULL) {
348                 if (strlen(modestr) != 2 || modestr[0] != '-')
349                         errx(1, "%s: invalid mode string: %s", file, modestr);
350                 switch (modestr[1]) {
351                 case 'b':
352                         entry->mode = S_IFBLK;
353                         entry->modestr = ",-b";
354                         break;
355                 case 'c':
356                         entry->mode = S_IFCHR;
357                         entry->modestr = ",-c";
358                         break;
359                 case 'd':
360                         entry->mode = S_IFDIR;
361                         entry->modestr = ",-d";
362                         break;
363                 case 'p':
364                         entry->mode = S_IFIFO;
365                         entry->modestr = ",-p";
366                         break;
367                 case 'l':
368                         entry->mode = S_IFLNK;
369                         entry->modestr = ",-l";
370                         break;
371                 case 's':
372                         entry->mode = S_IFSOCK;
373                         entry->modestr = ",-s";
374                         break;
375                 case '-':
376                         entry->mode = S_IFREG;
377                         entry->modestr = ",--";
378                         break;
379                 default:
380                         errx(1, "%s: invalid mode string: %s", file, modestr);
381                 }
382         } else {
383                 entry->modestr = "";
384         }
385 }
386
387 int
388 specs_empty(struct label_specs *specs)
389 {
390
391         return (STAILQ_EMPTY(&specs->head));
392 }
393
394 int
395 apply_specs(struct label_specs *specs, FTSENT *ftsent, int hflag, int vflag)
396 {
397         regmatch_t pmatch;
398         struct label_spec *ls;
399         struct label_spec_entry *ent;
400         char *regerrorstr, *macstr;
401         size_t size;
402         mac_t mac;
403         int error, matchedby;
404
405         /*
406          * Work through file context sources in order of specification
407          * on the command line, and through their entries in reverse
408          * order to find the "last" (hopefully "best") match.
409          */
410         matchedby = 0;
411         STAILQ_FOREACH(ls, &specs->head, link) {
412                 for (ls->match = NULL, ent = ls->entries;
413                     ent < &ls->entries[ls->nentries]; ent++) {
414                         if (ent->flags & F_ALWAYSMATCH)
415                                 goto matched;
416                         if (ent->mode != 0 &&
417                             (ftsent->fts_statp->st_mode & S_IFMT) != ent->mode)
418                                 continue;
419                         pmatch.rm_so = 0;
420                         pmatch.rm_eo = ftsent->fts_pathlen;
421                         error = regexec(&ent->regex, ftsent->fts_path, 1,
422                             &pmatch, REG_STARTEND);
423                         switch (error) {
424                         case REG_NOMATCH:
425                                 continue;
426                         case 0:
427                                 break;
428                         default:
429                                 size = regerror(error, &ent->regex, NULL, 0);
430                                 regerrorstr = malloc(size);
431                                 if (regerrorstr == NULL)
432                                         err(1, "malloc");
433                                 (void)regerror(error, &ent->regex, regerrorstr,
434                                     size);
435                                 errx(1, "%s: %s", ent->regexstr, regerrorstr);
436                         }
437                 matched:
438                         ls->match = ent;
439                         if (vflag) {
440                                 if (matchedby == 0) {
441                                         printf("%.*s matched by ",
442                                             ftsent->fts_pathlen,
443                                             ftsent->fts_path);
444                                         matchedby = 1;
445                                 }
446                                 printf("%s(%s%s,%s)", matchedby == 2 ? "," : "",
447                                     ent->regexstr, ent->modestr, ent->mactext);
448                                 if (matchedby == 1)
449                                         matchedby = 2;
450                         }
451                         break;
452                 }
453         }
454         if (vflag && matchedby)
455                 printf("\n");
456         size = 0;
457         STAILQ_FOREACH(ls, &specs->head, link) {
458                 /* cached match decision */
459                 if (ls->match && (ls->match->flags & F_DONTLABEL) == 0)
460                          /* add length of "x\0"/"y," */
461                         size += strlen(ls->match->mactext) + 1;
462         }
463         if (size == 0)
464                 return (0);
465         macstr = malloc(size);
466         if (macstr == NULL)
467                 err(1, "malloc");
468         *macstr = '\0';
469         STAILQ_FOREACH(ls, &specs->head, link) {
470                 /* cached match decision */
471                 if (ls->match && (ls->match->flags & F_DONTLABEL) == 0) {
472                         if (*macstr != '\0')
473                                 strcat(macstr, ",");
474                         strcat(macstr, ls->match->mactext);
475                 }
476         }
477         if (mac_from_text(&mac, macstr))
478                 err(1, "mac_from_text(%s)", macstr);
479         if ((hflag == FTS_PHYSICAL ? mac_set_link(ftsent->fts_accpath, mac) :
480             mac_set_file(ftsent->fts_accpath, mac)) != 0) {
481                 if (errno == EOPNOTSUPP) {
482                         mac_free(mac);
483                         free(macstr);
484                         return (1);
485                 }
486                 err(1, "mac_set_link(%.*s, %s)", ftsent->fts_pathlen,
487                     ftsent->fts_path, macstr);
488         }
489         mac_free(mac);
490         free(macstr);
491         return (0);
492 }
493
494 struct label_specs *
495 new_specs(void)
496 {
497         struct label_specs *specs;
498
499         specs = malloc(sizeof(*specs));
500         if (specs == NULL)
501                 err(1, "malloc");
502         STAILQ_INIT(&specs->head);
503         return (specs);
504 }