2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2004 Networks Associates Technology, Inc.
7 * This software was developed for the FreeBSD Project by NAI Labs, the
8 * Security Research Division of Network Associates, Inc. under
9 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
10 * CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
38 #include <sys/queue.h>
53 struct label_spec_entry {
54 regex_t regex; /* compiled regular expression to match */
55 char *regexstr; /* uncompiled regular expression */
56 mode_t mode; /* mode to possibly match */
57 const char *modestr; /* print-worthy ",-?" mode string */
58 char *mactext; /* MAC label to apply */
59 int flags; /* miscellaneous flags */
60 #define F_DONTLABEL 0x01
61 #define F_ALWAYSMATCH 0x02
62 } *entries, /* entries[0..nentries] */
63 *match; /* cached decision for MAC label to apply */
64 size_t nentries; /* size of entries list */
65 STAILQ_ENTRY(label_spec) link;
69 STAILQ_HEAD(label_specs_head, label_spec) head;
72 void usage(int) __dead2;
73 struct label_specs *new_specs(void);
74 void add_specs(struct label_specs *, const char *, int);
75 void add_setfmac_specs(struct label_specs *, char *);
76 void add_spec_line(const char *, int, struct label_spec_entry *, char *);
77 int apply_specs(struct label_specs *, FTSENT *, int, int);
78 int specs_empty(struct label_specs *);
83 main(int argc, char **argv)
87 struct label_specs *specs;
88 int eflag = 0, xflag = 0, vflag = 0, Rflag = 0, hflag;
92 bn = basename(argv[0]);
95 is_setfmac = strcmp(bn, "setfmac") == 0;
96 hflag = is_setfmac ? FTS_LOGICAL : FTS_PHYSICAL;
98 while ((ch = getopt(argc, argv, is_setfmac ? "Rhq" : "ef:qs:vx")) !=
108 add_specs(specs, optarg, 0);
111 hflag = FTS_PHYSICAL;
117 add_specs(specs, optarg, 1);
135 add_setfmac_specs(specs, *argv);
139 if (argc == 0 || specs_empty(specs))
142 fts = fts_open(argv, hflag | xflag, NULL);
144 err(1, "cannot traverse filesystem%s", argc ? "s" : "");
145 while (errno = 0, (ftsent = fts_read(fts)) != NULL) {
146 switch (ftsent->fts_info) {
147 case FTS_DP: /* skip post-order */
149 case FTS_D: /* do pre-order */
150 case FTS_DC: /* do cyclic? */
151 /* don't ever recurse directories as setfmac(8) */
152 if (is_setfmac && !Rflag)
153 fts_set(fts, ftsent, FTS_SKIP);
154 case FTS_DEFAULT: /* do default */
155 case FTS_F: /* do regular */
156 case FTS_SL: /* do symlink */
157 case FTS_SLNONE: /* do symlink */
158 case FTS_W: /* do whiteout */
159 if (apply_specs(specs, ftsent, hflag, vflag)) {
161 errx(1, "labeling not supported in %s",
165 warnx("labeling not supported in %s",
167 fts_set(fts, ftsent, FTS_SKIP);
170 case FTS_DNR: /* die on all errors */
173 err(1, "traversing %s", ftsent->fts_path);
175 errx(1, "CANNOT HAPPEN (%d) traversing %s",
176 ftsent->fts_info, ftsent->fts_path);
186 usage(int is_setfmac)
190 fprintf(stderr, "usage: setfmac [-Rhq] label file ...\n");
192 fprintf(stderr, "usage: setfsmac [-ehqvx] [-f specfile [...]] [-s specfile [...]] file ...\n");
197 chomp_line(char **line, size_t *linesize)
202 for (s = *line; (unsigned)(s - *line) < *linesize; s++) {
211 memmove(*line, s, *linesize - (s - *line));
212 *linesize -= s - *line;
213 for (s = &(*line)[*linesize - 1]; s >= *line; s--) {
217 if (s != &(*line)[*linesize - 1]) {
218 *linesize = s - *line + 1;
220 s = malloc(*linesize + 1);
223 strncpy(s, *line, *linesize);
227 (*line)[*linesize] = '\0';
232 add_specs(struct label_specs *specs, const char *file, int is_sebsd)
234 struct label_spec *spec;
237 size_t nlines = 0, linesize;
240 spec = malloc(sizeof(*spec));
243 fp = fopen(file, "r");
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 */
254 err(1, "fgetln on %s", file);
256 spec->entries = calloc(nlines, sizeof(*spec->entries));
257 if (spec->entries == NULL)
259 spec->nentries = nlines;
261 line = fgetln(fp, &linesize);
264 errx(1, "%s ended prematurely", file);
266 err(1, "failure reading %s", file);
268 freeline = chomp_line(&line, &linesize);
274 add_spec_line(file, is_sebsd, &spec->entries[--nlines], line);
280 warnx("%s: read %lu specifications", file,
281 (long)spec->nentries);
282 STAILQ_INSERT_TAIL(&specs->head, spec, link);
286 add_setfmac_specs(struct label_specs *specs, char *label)
288 struct label_spec *spec;
290 spec = malloc(sizeof(*spec));
294 spec->entries = calloc(spec->nentries, sizeof(*spec->entries));
295 if (spec->entries == NULL)
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);
304 add_spec_line(const char *file, int is_sebsd, struct label_spec_entry *entry,
307 char *regexstr, *modestr, *macstr, *regerrorstr;
311 regexstr = strtok(line, " \t");
312 if (regexstr == NULL)
313 errx(1, "%s: need regular expression", file);
314 modestr = strtok(NULL, " \t");
316 errx(1, "%s: need a label", file);
317 macstr = strtok(NULL, " \t");
318 if (macstr == NULL) { /* the mode is just optional */
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(®exstr, "^%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);
330 size = regerror(error, &entry->regex, NULL, 0);
331 regerrorstr = malloc(size);
332 if (regerrorstr == NULL)
334 (void)regerror(error, &entry->regex, regerrorstr, size);
335 errx(1, "%s: %s: %s", file, entry->regexstr, regerrorstr);
338 entry->mactext = strdup(macstr);
339 if (entry->mactext == NULL)
342 if (asprintf(&entry->mactext, "sebsd/%s", macstr) == -1)
344 if (strcmp(macstr, "<<none>>") == 0)
345 entry->flags |= F_DONTLABEL;
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]) {
352 entry->mode = S_IFBLK;
353 entry->modestr = ",-b";
356 entry->mode = S_IFCHR;
357 entry->modestr = ",-c";
360 entry->mode = S_IFDIR;
361 entry->modestr = ",-d";
364 entry->mode = S_IFIFO;
365 entry->modestr = ",-p";
368 entry->mode = S_IFLNK;
369 entry->modestr = ",-l";
372 entry->mode = S_IFSOCK;
373 entry->modestr = ",-s";
376 entry->mode = S_IFREG;
377 entry->modestr = ",--";
380 errx(1, "%s: invalid mode string: %s", file, modestr);
388 specs_empty(struct label_specs *specs)
391 return (STAILQ_EMPTY(&specs->head));
395 apply_specs(struct label_specs *specs, FTSENT *ftsent, int hflag, int vflag)
398 struct label_spec *ls;
399 struct label_spec_entry *ent;
400 char *regerrorstr, *macstr;
403 int error, matchedby;
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.
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)
416 if (ent->mode != 0 &&
417 (ftsent->fts_statp->st_mode & S_IFMT) != ent->mode)
420 pmatch.rm_eo = ftsent->fts_pathlen;
421 error = regexec(&ent->regex, ftsent->fts_path, 1,
422 &pmatch, REG_STARTEND);
429 size = regerror(error, &ent->regex, NULL, 0);
430 regerrorstr = malloc(size);
431 if (regerrorstr == NULL)
433 (void)regerror(error, &ent->regex, regerrorstr,
435 errx(1, "%s: %s", ent->regexstr, regerrorstr);
440 if (matchedby == 0) {
441 printf("%s matched by ",
445 printf("%s(%s%s,%s)", matchedby == 2 ? "," : "",
446 ent->regexstr, ent->modestr, ent->mactext);
453 if (vflag && matchedby)
456 STAILQ_FOREACH(ls, &specs->head, link) {
457 /* cached match decision */
458 if (ls->match && (ls->match->flags & F_DONTLABEL) == 0)
459 /* add length of "x\0"/"y," */
460 size += strlen(ls->match->mactext) + 1;
464 macstr = malloc(size);
468 STAILQ_FOREACH(ls, &specs->head, link) {
469 /* cached match decision */
470 if (ls->match && (ls->match->flags & F_DONTLABEL) == 0) {
473 strcat(macstr, ls->match->mactext);
476 if (mac_from_text(&mac, macstr))
477 err(1, "mac_from_text(%s)", macstr);
478 if ((hflag == FTS_PHYSICAL ? mac_set_link(ftsent->fts_accpath, mac) :
479 mac_set_file(ftsent->fts_accpath, mac)) != 0) {
480 if (errno == EOPNOTSUPP) {
485 err(1, "mac_set_link(%s, %s)", ftsent->fts_path, macstr);
495 struct label_specs *specs;
497 specs = malloc(sizeof(*specs));
500 STAILQ_INIT(&specs->head);