Home | History | Annotate | Line # | Download | only in veriexecgen
veriexecgen.c revision 1.10
      1  1.10       agc /* $NetBSD: veriexecgen.c,v 1.10 2006/12/04 21:22:40 agc Exp $ */
      2   1.1      elad 
      3   1.1      elad /*-
      4   1.1      elad  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5   1.1      elad  * All rights reserved.
      6   1.1      elad  *
      7   1.1      elad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      elad  * by Matt Fleming.
      9   1.1      elad  *
     10   1.1      elad  * Redistribution and use in source and binary forms, with or without
     11   1.1      elad  * modification, are permitted provided that the following conditions
     12   1.1      elad  * are met:
     13   1.1      elad  * 1. Redistributions of source code must retain the above copyright
     14   1.1      elad  *    notice, this list of conditions and the following disclaimer.
     15   1.1      elad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      elad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      elad  *    documentation and/or other materials provided with the distribution.
     18   1.1      elad  * 3. All advertising materials mentioning features or use of this software
     19   1.1      elad  *    must display the following acknowledgement:
     20   1.1      elad  *        This product includes software developed by the NetBSD
     21   1.1      elad  *        Foundation, Inc. and its contributors.
     22   1.1      elad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      elad  *    contributors may be used to endorse or promote products derived
     24   1.1      elad  *    from this software without specific prior written permission.
     25   1.1      elad  *
     26   1.1      elad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      elad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      elad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      elad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      elad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      elad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      elad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      elad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      elad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      elad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      elad  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      elad  */
     38   1.1      elad 
     39   1.1      elad #include <sys/param.h>
     40   1.1      elad #include <sys/types.h>
     41   1.1      elad #include <sys/queue.h>
     42   1.1      elad #include <sys/stat.h>
     43   1.1      elad #include <sys/dirent.h>
     44   1.1      elad #include <sys/verified_exec.h>
     45   1.1      elad 
     46   1.1      elad #include <err.h>
     47   1.1      elad #include <errno.h>
     48   1.1      elad #include <fts.h>
     49   1.1      elad #include <stdio.h>
     50   1.1      elad #include <stdlib.h>
     51   1.1      elad #include <string.h>
     52   1.1      elad #include <time.h>
     53   1.1      elad #include <unistd.h>
     54   1.1      elad #include <util.h>
     55   1.1      elad 
     56   1.1      elad #include <md5.h>
     57   1.2      elad #include <sha1.h>
     58   1.8  christos #include <sha2.h>
     59   1.8  christos #include <rmd160.h>
     60   1.1      elad 
     61   1.1      elad #define IS_EXEC(mode) ((mode) & (S_IXUSR | S_IXGRP | S_IXOTH))
     62   1.1      elad 
     63   1.1      elad #define DEFAULT_DBFILE  "/etc/signatures"
     64   1.1      elad #define DEFAULT_HASH    "sha256"
     65   1.1      elad #define DEFAULT_SYSPATHS { "/bin", "/sbin", "/usr/bin", "/usr/sbin", \
     66   1.6      elad 			   "/lib", "/usr/lib", "/libexec", "/usr/libexec", \
     67   1.6      elad 			   NULL }
     68   1.1      elad 
     69  1.10       agc /* this struct describes a directory entry to generate a hash for */
     70   1.1      elad struct fentry {
     71  1.10       agc 	char filename[MAXPATHLEN];	/* name of entry */
     72  1.10       agc 	char *hash_val;			/* its associated hash value */
     73  1.10       agc 	int flags;			/* any associated flags */
     74  1.10       agc 	TAILQ_ENTRY(fentry) f;		/* its place in the queue */
     75   1.1      elad };
     76   1.1      elad TAILQ_HEAD(, fentry) fehead;
     77   1.1      elad 
     78  1.10       agc /* this struct defines the possible hash algorithms */
     79   1.1      elad struct hash {
     80   1.1      elad 	const char     *hashname;
     81   1.1      elad 	char           *(*filefunc) (const char *, char *);
     82   1.1      elad } hashes[] = {
     83   1.1      elad 	{ "MD5", MD5File },
     84   1.1      elad 	{ "SHA1", SHA1File },
     85   1.1      elad 	{ "SHA256", SHA256_File },
     86   1.1      elad 	{ "SHA384", SHA384_File },
     87   1.1      elad 	{ "SHA512", SHA512_File },
     88   1.1      elad 	{ "RMD160", RMD160File },
     89   1.1      elad 	{ NULL, NULL },
     90   1.1      elad };
     91   1.1      elad 
     92  1.10       agc static int Fflag;
     93   1.9       agc 
     94  1.10       agc static int	exit_on_error;	/* exit if we can't create a hash */
     95  1.10       agc static int	append_output;	/* append output to signatures file */
     96  1.10       agc static int	all_files;	/* scan for non-executable files as well */
     97  1.10       agc static int	scan_system_dirs;	/* scan system directories */
     98  1.10       agc static int	recursive_scan;	/* perform the scan recursively */
     99  1.10       agc static int	make_immutable;	/* set immutable flag on signatures file */
    100  1.10       agc static int	verbose;	/* verbose execution */
    101   1.9       agc 
    102   1.9       agc /* warn about a problem - exit if exit_on_error is set */
    103   1.9       agc static void
    104   1.9       agc gripe(const char *fmt, const char *filename)
    105   1.9       agc {
    106   1.9       agc 	warn(fmt, filename);
    107   1.9       agc 	if (exit_on_error) {
    108   1.9       agc 		/* error out on problematic files */
    109   1.9       agc 		exit(EXIT_FAILURE);
    110   1.9       agc 	}
    111   1.9       agc }
    112   1.1      elad 
    113  1.10       agc /* print usage message */
    114   1.1      elad static void
    115   1.1      elad usage(void)
    116   1.1      elad {
    117   1.1      elad 	(void)fprintf(stderr,
    118  1.10       agc 	    "usage: %s [-AaDrSvW] [-d dir] [-o fingerprintdb]"
    119   1.1      elad 	    " [-t algorithm]\n", getprogname());
    120   1.1      elad }
    121   1.1      elad 
    122  1.10       agc /* tell people what we're doing - scan dirs, fingerprint etc */
    123   1.1      elad static void
    124   1.1      elad banner(struct hash *hash_type, char **search_path)
    125   1.1      elad {
    126   1.1      elad 	int j;
    127   1.1      elad 
    128   1.1      elad 	(void)printf("Fingerprinting ");
    129   1.1      elad 
    130   1.1      elad 	for (j = 0; search_path[j] != NULL; j++)
    131   1.1      elad 		(void)printf("%s ", search_path[j]);
    132   1.1      elad 
    133   1.1      elad 	(void)printf("(%s) (%s) using %s\n",
    134  1.10       agc 	    all_files ? "all files" : "executables only",
    135  1.10       agc 	    recursive_scan ? "recursive" : "non-recursive",
    136   1.1      elad 	    hash_type->hashname);
    137   1.1      elad }
    138   1.1      elad 
    139  1.10       agc /* find a hash algorithm, given its name */
    140   1.1      elad static struct hash *
    141   1.1      elad find_hash(char *hash_type)
    142   1.1      elad {
    143   1.1      elad 	struct hash *hash;
    144   1.1      elad 
    145   1.1      elad 	for (hash = hashes; hash->hashname != NULL; hash++)
    146   1.1      elad 		if (strcasecmp(hash_type, hash->hashname) == 0)
    147   1.1      elad 			return hash;
    148   1.1      elad 	return NULL;
    149   1.1      elad }
    150   1.1      elad 
    151  1.10       agc /* perform the hashing operation on `filename' */
    152   1.1      elad static char *
    153   1.1      elad do_hash(char *filename, struct hash * h)
    154   1.1      elad {
    155   1.1      elad 	return h->filefunc(filename, NULL);
    156   1.1      elad }
    157   1.1      elad 
    158  1.10       agc /* return flags for `path' */
    159   1.1      elad static int
    160   1.1      elad figure_flags(char *path, mode_t mode)
    161   1.1      elad {
    162   1.1      elad #ifdef notyet
    163   1.1      elad 	if (Fflag) {
    164   1.1      elad 		/* Try to figure out right flag(s). */
    165   1.1      elad 		return VERIEXEC_DIRECT;
    166  1.10       agc 	}
    167   1.1      elad #endif /* notyet */
    168   1.1      elad 
    169  1.10       agc 	return (IS_EXEC(mode)) ? 0 : VERIEXEC_FILE;
    170   1.1      elad }
    171   1.1      elad 
    172  1.10       agc /* check to see that we don't have a duplicate entry */
    173   1.1      elad static int
    174   1.1      elad check_dup(char *filename)
    175   1.1      elad {
    176   1.1      elad 	struct fentry *lwalk;
    177   1.1      elad 
    178   1.1      elad 	TAILQ_FOREACH(lwalk, &fehead, f) {
    179   1.1      elad 		if (strncmp(lwalk->filename, filename,
    180   1.1      elad 			    (unsigned long) MAXPATHLEN) == 0)
    181   1.1      elad 			return 1;
    182   1.1      elad 	}
    183   1.1      elad 
    184   1.1      elad 	return 0;
    185   1.1      elad }
    186   1.1      elad 
    187  1.10       agc /* add a new entry to the list for `file' */
    188   1.1      elad static void
    189   1.1      elad add_new_entry(FTSENT *file, struct hash *hash)
    190   1.1      elad {
    191   1.1      elad 	struct fentry *e;
    192   1.1      elad 	struct stat sb;
    193   1.1      elad 
    194   1.1      elad 	if (file->fts_info == FTS_SL) {
    195  1.10       agc 		/* we have a symbolic link */
    196   1.9       agc 		if (stat(file->fts_path, &sb) == -1) {
    197   1.9       agc 			gripe("Cannot stat symlink `%s'", file->fts_path);
    198   1.9       agc 			return;
    199   1.9       agc 		}
    200   1.1      elad 	} else
    201   1.1      elad 		sb = *file->fts_statp;
    202   1.1      elad 
    203  1.10       agc 	if (!all_files && !scan_system_dirs && !IS_EXEC(sb.st_mode))
    204   1.1      elad 		return;
    205   1.1      elad 
    206   1.1      elad 	e = ecalloc(1UL, sizeof(*e));
    207   1.1      elad 
    208   1.9       agc 	if (realpath(file->fts_accpath, e->filename) == NULL) {
    209   1.9       agc 		gripe("Cannot find absolute path `%s'", file->fts_accpath);
    210   1.9       agc 		return;
    211   1.9       agc 	}
    212   1.1      elad 	if (check_dup(e->filename)) {
    213   1.1      elad 		free(e);
    214   1.1      elad 		return;
    215   1.1      elad 	}
    216   1.9       agc 	if ((e->hash_val = do_hash(e->filename, hash)) == NULL) {
    217   1.9       agc 		gripe("Cannot calculate hash `%s'", e->filename);
    218   1.9       agc 		return;
    219   1.9       agc 	}
    220   1.1      elad 	e->flags = figure_flags(e->filename, sb.st_mode);
    221   1.1      elad 
    222   1.1      elad 	TAILQ_INSERT_TAIL(&fehead, e, f);
    223   1.1      elad }
    224   1.1      elad 
    225  1.10       agc /* walk through a directory */
    226   1.1      elad static void
    227   1.1      elad walk_dir(char **search_path, struct hash *hash)
    228   1.1      elad {
    229   1.1      elad 	FTS *fh;
    230   1.1      elad 	FTSENT *file;
    231   1.1      elad 
    232   1.9       agc 	if ((fh = fts_open(search_path, FTS_PHYSICAL, NULL)) == NULL) {
    233   1.9       agc 		gripe("fts_open `%s'", (const char *)search_path);
    234   1.9       agc 		return;
    235   1.9       agc 	}
    236   1.1      elad 
    237   1.1      elad 	while ((file = fts_read(fh)) != NULL) {
    238  1.10       agc 		if (!recursive_scan && file->fts_level > 1) {
    239   1.1      elad 			fts_set(fh, file, FTS_SKIP);
    240   1.1      elad 			continue;
    241   1.1      elad 		}
    242   1.1      elad 
    243   1.1      elad 		switch (file->fts_info) {
    244   1.1      elad 		case FTS_D:
    245   1.1      elad 		case FTS_DC:
    246   1.1      elad 		case FTS_DP:
    247   1.1      elad 			continue;
    248   1.1      elad 		default:
    249   1.1      elad 			break;
    250   1.1      elad 		}
    251   1.1      elad 
    252   1.1      elad 		if (file->fts_errno) {
    253  1.10       agc 			if (exit_on_error) {
    254  1.10       agc 				errx(EXIT_FAILURE, "%s: %s", file->fts_path,
    255  1.10       agc 				    strerror(file->fts_errno));
    256  1.10       agc 			}
    257  1.10       agc 		} else {
    258  1.10       agc 			add_new_entry(file, hash);
    259   1.1      elad 		}
    260   1.1      elad 	}
    261   1.1      elad 
    262   1.1      elad 	fts_close(fh);
    263   1.1      elad }
    264   1.1      elad 
    265  1.10       agc /* return a string representation of the flags */
    266   1.1      elad static char *
    267   1.1      elad flags2str(int flags)
    268   1.1      elad {
    269  1.10       agc 	return (flags == 0) ? "" : "FILE, INDIRECT";
    270   1.1      elad }
    271   1.1      elad 
    272  1.10       agc /* store the list in the signatures file */
    273   1.1      elad static void
    274   1.1      elad store_entries(char *dbfile, struct hash *hash)
    275   1.1      elad {
    276   1.1      elad 	FILE *fp;
    277   1.1      elad 	int move = 1;
    278   1.1      elad 	char old_dbfile[MAXPATHLEN];
    279   1.1      elad 	time_t ct;
    280   1.1      elad 	struct stat sb;
    281   1.1      elad 	struct fentry  *e;
    282   1.1      elad 
    283   1.1      elad 	if (stat(dbfile, &sb) != 0) {
    284   1.1      elad 		if (errno == ENOENT)
    285   1.1      elad 			move = 0;
    286   1.1      elad 		else
    287  1.10       agc 			err(EXIT_FAILURE, "could not stat %s", dbfile);
    288   1.1      elad 	}
    289  1.10       agc 	if (move && !append_output) {
    290  1.10       agc 		if (verbose)
    291   1.1      elad 			(void)printf("\nBacking up existing fingerprint file "
    292   1.1      elad 			    "to \"%s.old\"\n\n", dbfile);
    293   1.1      elad 
    294   1.1      elad 		if (snprintf(old_dbfile, MAXPATHLEN, "%s.old", dbfile) <
    295   1.1      elad 		    strlen(dbfile) + 4) {
    296  1.10       agc 			err(EXIT_FAILURE, "%s", old_dbfile);
    297   1.1      elad 		}
    298   1.1      elad 		if (rename(dbfile, old_dbfile) == -1)
    299  1.10       agc 			err(EXIT_FAILURE, "could not rename file");
    300   1.1      elad 	}
    301   1.1      elad 
    302  1.10       agc 	fp = efopen(dbfile, append_output ? "a" : "w+");
    303   1.1      elad 
    304   1.1      elad 	time(&ct);
    305   1.1      elad 	(void)fprintf(fp, "# Generated by %s, %.24s\n",
    306   1.1      elad 		getlogin(), ctime(&ct));
    307   1.1      elad 
    308   1.1      elad 	TAILQ_FOREACH(e, &fehead, f) {
    309  1.10       agc 		if (verbose)
    310   1.1      elad 			(void)printf("Adding %s.\n", e->filename);
    311   1.1      elad 
    312   1.1      elad 		(void)fprintf(fp, "%s %s %s %s\n", e->filename,
    313   1.1      elad 			hash->hashname, e->hash_val, flags2str(e->flags));
    314   1.1      elad 	}
    315   1.1      elad 
    316   1.1      elad 	(void)fclose(fp);
    317   1.1      elad 
    318  1.10       agc 	if (verbose) {
    319  1.10       agc 		(void)printf("\n\n"
    320  1.10       agc "#############################################################\n"
    321  1.10       agc "  PLEASE VERIFY CONTENTS OF %s AND FINE-TUNE THE\n"
    322  1.10       agc "  FLAGS WHERE APPROPRIATE AFTER READING veriexecctl(8)\n"
    323  1.10       agc "#############################################################\n",
    324  1.10       agc 			dbfile);
    325  1.10       agc 	}
    326   1.1      elad }
    327   1.1      elad 
    328   1.1      elad int
    329   1.1      elad main(int argc, char **argv)
    330   1.1      elad {
    331   1.1      elad 	int ch, total = 0;
    332   1.1      elad 	char *dbfile = NULL;
    333   1.1      elad 	char **search_path = NULL;
    334   1.1      elad 	struct hash *hash = NULL;
    335   1.1      elad 
    336  1.10       agc 	append_output = 0;
    337  1.10       agc 	all_files = 0;
    338  1.10       agc 	scan_system_dirs = 0;
    339  1.10       agc 	recursive_scan = 0;
    340  1.10       agc 	make_immutable = 0;
    341  1.10       agc 	verbose = 0;
    342  1.10       agc 	Fflag = 0;
    343   1.1      elad 
    344   1.9       agc 	/* error out if we have a dangling symlink or other fs problem */
    345   1.9       agc 	exit_on_error = 1;
    346   1.9       agc 
    347   1.9       agc 	while ((ch = getopt(argc, argv, "AaDd:ho:rSt:vW")) != -1) {
    348   1.1      elad 		switch (ch) {
    349   1.1      elad 		case 'A':
    350  1.10       agc 			append_output = 1;
    351   1.1      elad 			break;
    352   1.1      elad 		case 'a':
    353  1.10       agc 			all_files = 1;
    354   1.1      elad 			break;
    355   1.1      elad 		case 'D':
    356  1.10       agc 			scan_system_dirs = 1;
    357   1.1      elad 			break;
    358   1.1      elad 		case 'd':
    359   1.1      elad 			search_path = erealloc(search_path, sizeof(char *) *
    360   1.1      elad 			    (total + 1));
    361   1.1      elad 			search_path[total] = optarg;
    362   1.1      elad 			search_path[++total] = NULL;
    363   1.1      elad 			break;
    364   1.1      elad #ifdef notyet
    365   1.1      elad 		case 'F':
    366   1.1      elad 			Fflag = 1;
    367   1.1      elad 			break;
    368   1.1      elad #endif /* notyet */
    369   1.1      elad 		case 'h':
    370   1.1      elad 			usage();
    371  1.10       agc 			return EXIT_SUCCESS;
    372   1.1      elad 		case 'o':
    373   1.1      elad 			dbfile = optarg;
    374   1.1      elad 			break;
    375   1.1      elad 		case 'r':
    376  1.10       agc 			recursive_scan = 1;
    377   1.1      elad 			break;
    378   1.3      elad 		case 'S':
    379  1.10       agc 			make_immutable = 1;
    380   1.3      elad 			break;
    381   1.1      elad 		case 't':
    382  1.10       agc 			if ((hash = find_hash(optarg)) == NULL) {
    383  1.10       agc 				errx(EXIT_FAILURE,
    384  1.10       agc 					"No such hash algorithm (%s)",
    385  1.10       agc 					optarg);
    386  1.10       agc 			}
    387   1.1      elad 			break;
    388   1.1      elad 		case 'v':
    389  1.10       agc 			verbose = 1;
    390   1.1      elad 			break;
    391   1.9       agc 		case 'W':
    392   1.9       agc 			exit_on_error = 0;
    393   1.9       agc 			break;
    394   1.1      elad 		default:
    395   1.1      elad 			usage();
    396  1.10       agc 			return EXIT_FAILURE;
    397   1.1      elad 		}
    398   1.1      elad 	}
    399   1.1      elad 
    400   1.1      elad 	if (dbfile == NULL)
    401   1.1      elad 		dbfile = DEFAULT_DBFILE;
    402   1.1      elad 
    403   1.1      elad 	if (hash == NULL) {
    404   1.1      elad 		if ((hash = find_hash(DEFAULT_HASH)) == NULL)
    405  1.10       agc 			errx(EXIT_FAILURE, "No hash algorithm");
    406   1.1      elad 	}
    407   1.1      elad 
    408   1.1      elad 	TAILQ_INIT(&fehead);
    409   1.1      elad 
    410   1.1      elad 	if (search_path == NULL)
    411  1.10       agc 		scan_system_dirs = 1;
    412   1.1      elad 
    413  1.10       agc 	if (scan_system_dirs) {
    414   1.1      elad 		char *sys_paths[] = DEFAULT_SYSPATHS;
    415   1.1      elad 
    416  1.10       agc 		if (verbose)
    417   1.1      elad 			banner(hash, sys_paths);
    418   1.1      elad 		walk_dir(sys_paths, hash);
    419   1.1      elad 	}
    420   1.1      elad 
    421   1.1      elad 	if (search_path != NULL) {
    422  1.10       agc 		if (verbose)
    423   1.1      elad 			banner(hash, search_path);
    424   1.1      elad 		walk_dir(search_path, hash);
    425   1.1      elad 	}
    426   1.1      elad 
    427   1.1      elad 	store_entries(dbfile, hash);
    428   1.1      elad 
    429  1.10       agc 	if (make_immutable && chflags(dbfile, SF_IMMUTABLE) != 0)
    430  1.10       agc 		err(EXIT_FAILURE, "Can't set immutable flag");
    431   1.3      elad 
    432  1.10       agc 	return EXIT_SUCCESS;
    433   1.1      elad }
    434