Home | History | Annotate | Line # | Download | only in veriexecgen
veriexecgen.c revision 1.8
      1  1.8  christos /* $NetBSD: veriexecgen.c,v 1.8 2006/10/30 20:22:54 christos 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.1      elad struct fentry {
     70  1.1      elad 	char filename[MAXPATHLEN];
     71  1.1      elad 	char *hash_val;
     72  1.1      elad 	int flags;
     73  1.1      elad 	TAILQ_ENTRY(fentry) f;
     74  1.1      elad };
     75  1.1      elad TAILQ_HEAD(, fentry) fehead;
     76  1.1      elad 
     77  1.1      elad struct hash {
     78  1.1      elad 	const char     *hashname;
     79  1.1      elad 	char           *(*filefunc) (const char *, char *);
     80  1.1      elad } hashes[] = {
     81  1.1      elad 	{ "MD5", MD5File },
     82  1.1      elad 	{ "SHA1", SHA1File },
     83  1.1      elad 	{ "SHA256", SHA256_File },
     84  1.1      elad 	{ "SHA384", SHA384_File },
     85  1.1      elad 	{ "SHA512", SHA512_File },
     86  1.1      elad 	{ "RMD160", RMD160File },
     87  1.1      elad 	{ NULL, NULL },
     88  1.1      elad };
     89  1.1      elad 
     90  1.3      elad int Aflag, aflag, Dflag, Fflag, rflag, Sflag, vflag;
     91  1.1      elad 
     92  1.1      elad static void
     93  1.1      elad usage(void)
     94  1.1      elad {
     95  1.1      elad 	(void)fprintf(stderr,
     96  1.3      elad 	    "usage: %s [-AaDrSv] [-d dir] [-o fingerprintdb]"
     97  1.1      elad 	    " [-t algorithm]\n", getprogname());
     98  1.1      elad }
     99  1.1      elad 
    100  1.1      elad static void
    101  1.1      elad banner(struct hash *hash_type, char **search_path)
    102  1.1      elad {
    103  1.1      elad 	int j;
    104  1.1      elad 
    105  1.1      elad 	(void)printf("Fingerprinting ");
    106  1.1      elad 
    107  1.1      elad 	for (j = 0; search_path[j] != NULL; j++)
    108  1.1      elad 		(void)printf("%s ", search_path[j]);
    109  1.1      elad 
    110  1.1      elad 	(void)printf("(%s) (%s) using %s\n",
    111  1.1      elad 	    aflag ? "all files" : "executables only",
    112  1.1      elad 	    rflag ? "recursive" : "non-recursive",
    113  1.1      elad 	    hash_type->hashname);
    114  1.1      elad }
    115  1.1      elad 
    116  1.1      elad static struct hash *
    117  1.1      elad find_hash(char *hash_type)
    118  1.1      elad {
    119  1.1      elad 	struct hash *hash;
    120  1.1      elad 
    121  1.1      elad 	for (hash = hashes; hash->hashname != NULL; hash++)
    122  1.1      elad 		if (strcasecmp(hash_type, hash->hashname) == 0)
    123  1.1      elad 			return hash;
    124  1.1      elad 	return NULL;
    125  1.1      elad }
    126  1.1      elad 
    127  1.1      elad static char *
    128  1.1      elad do_hash(char *filename, struct hash * h)
    129  1.1      elad {
    130  1.1      elad 	return h->filefunc(filename, NULL);
    131  1.1      elad }
    132  1.1      elad 
    133  1.1      elad static int
    134  1.1      elad figure_flags(char *path, mode_t mode)
    135  1.1      elad {
    136  1.1      elad #ifdef notyet
    137  1.1      elad 	if (Fflag) {
    138  1.1      elad 		/* Try to figure out right flag(s). */
    139  1.1      elad 		return VERIEXEC_DIRECT;
    140  1.1      elad }
    141  1.1      elad #endif /* notyet */
    142  1.1      elad 
    143  1.1      elad 	if (!IS_EXEC(mode))
    144  1.1      elad 		return VERIEXEC_FILE;
    145  1.1      elad 	else
    146  1.1      elad 		return 0;
    147  1.1      elad }
    148  1.1      elad 
    149  1.1      elad static int
    150  1.1      elad check_dup(char *filename)
    151  1.1      elad {
    152  1.1      elad 	struct fentry *lwalk;
    153  1.1      elad 
    154  1.1      elad 	TAILQ_FOREACH(lwalk, &fehead, f) {
    155  1.1      elad 		if (strncmp(lwalk->filename, filename,
    156  1.1      elad 			    (unsigned long) MAXPATHLEN) == 0)
    157  1.1      elad 			return 1;
    158  1.1      elad 	}
    159  1.1      elad 
    160  1.1      elad 	return 0;
    161  1.1      elad }
    162  1.1      elad 
    163  1.1      elad static void
    164  1.1      elad add_new_entry(FTSENT *file, struct hash *hash)
    165  1.1      elad {
    166  1.1      elad 	struct fentry *e;
    167  1.1      elad 	struct stat sb;
    168  1.1      elad 
    169  1.1      elad 	if (file->fts_info == FTS_SL) {
    170  1.1      elad 		if (stat(file->fts_path, &sb) == -1)
    171  1.1      elad 			err(1, "Cannot stat symlink");
    172  1.1      elad 	} else
    173  1.1      elad 		sb = *file->fts_statp;
    174  1.1      elad 
    175  1.5      elad 	if (!aflag && !Dflag && !IS_EXEC(sb.st_mode))
    176  1.1      elad 		return;
    177  1.1      elad 
    178  1.1      elad 	e = ecalloc(1UL, sizeof(*e));
    179  1.1      elad 
    180  1.1      elad 	if (realpath(file->fts_accpath, e->filename) == NULL)
    181  1.1      elad 		err(1, "Cannot find absolute path");
    182  1.1      elad 	if (check_dup(e->filename)) {
    183  1.1      elad 		free(e);
    184  1.1      elad 		return;
    185  1.1      elad 	}
    186  1.1      elad 	if ((e->hash_val = do_hash(e->filename, hash)) == NULL)
    187  1.1      elad 		errx(1, "Cannot calculate hash");
    188  1.1      elad 	e->flags = figure_flags(e->filename, sb.st_mode);
    189  1.1      elad 
    190  1.1      elad 	TAILQ_INSERT_TAIL(&fehead, e, f);
    191  1.1      elad }
    192  1.1      elad 
    193  1.1      elad static void
    194  1.1      elad walk_dir(char **search_path, struct hash *hash)
    195  1.1      elad {
    196  1.1      elad 	FTS *fh;
    197  1.1      elad 	FTSENT *file;
    198  1.1      elad 
    199  1.1      elad 	if ((fh = fts_open(search_path, FTS_PHYSICAL, NULL)) == NULL)
    200  1.1      elad 		err(1, "fts_open");
    201  1.1      elad 
    202  1.1      elad 	while ((file = fts_read(fh)) != NULL) {
    203  1.1      elad 		if (!rflag && file->fts_level > 1) {
    204  1.1      elad 			fts_set(fh, file, FTS_SKIP);
    205  1.1      elad 			continue;
    206  1.1      elad 		}
    207  1.1      elad 
    208  1.1      elad 		switch (file->fts_info) {
    209  1.1      elad 		case FTS_D:
    210  1.1      elad 		case FTS_DC:
    211  1.1      elad 		case FTS_DP:
    212  1.1      elad 			continue;
    213  1.1      elad 		default:
    214  1.1      elad 			break;
    215  1.1      elad 		}
    216  1.1      elad 
    217  1.1      elad 		if (file->fts_errno) {
    218  1.1      elad 			errx(1, "%s: %s", file->fts_path,
    219  1.1      elad 			    strerror(file->fts_errno));
    220  1.1      elad 		}
    221  1.1      elad 
    222  1.1      elad 		add_new_entry(file, hash);
    223  1.1      elad 	}
    224  1.1      elad 
    225  1.1      elad 	fts_close(fh);
    226  1.1      elad }
    227  1.1      elad 
    228  1.1      elad static char *
    229  1.1      elad flags2str(int flags)
    230  1.1      elad {
    231  1.1      elad 	if (flags != 0)
    232  1.7      elad 		return "FILE, INDIRECT";
    233  1.1      elad 	else
    234  1.1      elad 		return "";
    235  1.1      elad }
    236  1.1      elad 
    237  1.1      elad static void
    238  1.1      elad store_entries(char *dbfile, struct hash *hash)
    239  1.1      elad {
    240  1.1      elad 	FILE *fp;
    241  1.1      elad 	int move = 1;
    242  1.1      elad 	char old_dbfile[MAXPATHLEN];
    243  1.1      elad 	time_t ct;
    244  1.1      elad 	struct stat sb;
    245  1.1      elad 	struct fentry  *e;
    246  1.1      elad 
    247  1.1      elad 	if (stat(dbfile, &sb) != 0) {
    248  1.1      elad 		if (errno == ENOENT)
    249  1.1      elad 			move = 0;
    250  1.1      elad 		else
    251  1.1      elad 			err(1, "could not stat %s", dbfile);
    252  1.1      elad 	}
    253  1.1      elad 	if (move && !Aflag) {
    254  1.1      elad 		if (vflag)
    255  1.1      elad 			(void)printf("\nBacking up existing fingerprint file "
    256  1.1      elad 			    "to \"%s.old\"\n\n", dbfile);
    257  1.1      elad 
    258  1.1      elad 		if (snprintf(old_dbfile, MAXPATHLEN, "%s.old", dbfile) <
    259  1.1      elad 		    strlen(dbfile) + 4) {
    260  1.1      elad 			err(1, "%s", old_dbfile);
    261  1.1      elad 		}
    262  1.1      elad 		if (rename(dbfile, old_dbfile) == -1)
    263  1.1      elad 			err(1, "could not rename file");
    264  1.1      elad 	}
    265  1.1      elad 
    266  1.1      elad 	fp = efopen(dbfile, Aflag ? "a" : "w+");
    267  1.1      elad 
    268  1.1      elad 	time(&ct);
    269  1.1      elad 	(void)fprintf(fp, "# Generated by %s, %.24s\n",
    270  1.1      elad 		getlogin(), ctime(&ct));
    271  1.1      elad 
    272  1.1      elad 	TAILQ_FOREACH(e, &fehead, f) {
    273  1.1      elad 		if (vflag)
    274  1.1      elad 			(void)printf("Adding %s.\n", e->filename);
    275  1.1      elad 
    276  1.1      elad 		(void)fprintf(fp, "%s %s %s %s\n", e->filename,
    277  1.1      elad 			hash->hashname, e->hash_val, flags2str(e->flags));
    278  1.1      elad 	}
    279  1.1      elad 
    280  1.1      elad 	(void)fclose(fp);
    281  1.1      elad 
    282  1.1      elad 	if (!vflag)
    283  1.1      elad 		return;
    284  1.1      elad 
    285  1.1      elad 	(void)printf("\n\n"
    286  1.1      elad 	   "#############################################################\n"
    287  1.1      elad 	   "  PLEASE VERIFY CONTENTS OF %s AND FINE-TUNE THE\n"
    288  1.1      elad 	   "  FLAGS WHERE APPROPRIATE AFTER READING veriexecctl(8)\n"
    289  1.1      elad 	   "#############################################################\n",
    290  1.1      elad 	   dbfile);
    291  1.1      elad }
    292  1.1      elad 
    293  1.1      elad int
    294  1.1      elad main(int argc, char **argv)
    295  1.1      elad {
    296  1.1      elad 	int ch, total = 0;
    297  1.1      elad 	char *dbfile = NULL;
    298  1.1      elad 	char **search_path = NULL;
    299  1.1      elad 	struct hash *hash = NULL;
    300  1.1      elad 
    301  1.3      elad 	Aflag = aflag = Dflag = Fflag = rflag = Sflag = vflag = 0;
    302  1.1      elad 
    303  1.3      elad 	while ((ch = getopt(argc, argv, "AaDd:ho:rSt:v")) != -1) {
    304  1.1      elad 		switch (ch) {
    305  1.1      elad 		case 'A':
    306  1.1      elad 			Aflag = 1;
    307  1.1      elad 			break;
    308  1.1      elad 		case 'a':
    309  1.1      elad 			aflag = 1;
    310  1.1      elad 			break;
    311  1.1      elad 		case 'D':
    312  1.1      elad 			Dflag = 1;
    313  1.1      elad 			break;
    314  1.1      elad 		case 'd':
    315  1.1      elad 			search_path = erealloc(search_path, sizeof(char *) *
    316  1.1      elad 			    (total + 1));
    317  1.1      elad 			search_path[total] = optarg;
    318  1.1      elad 			search_path[++total] = NULL;
    319  1.1      elad 			break;
    320  1.1      elad #ifdef notyet
    321  1.1      elad 		case 'F':
    322  1.1      elad 			Fflag = 1;
    323  1.1      elad 			break;
    324  1.1      elad #endif /* notyet */
    325  1.1      elad 		case 'h':
    326  1.1      elad 			usage();
    327  1.1      elad 			return 0;
    328  1.1      elad 		case 'o':
    329  1.1      elad 			dbfile = optarg;
    330  1.1      elad 			break;
    331  1.1      elad 		case 'r':
    332  1.1      elad 			rflag = 1;
    333  1.1      elad 			break;
    334  1.3      elad 		case 'S':
    335  1.3      elad 			Sflag = 1;
    336  1.3      elad 			break;
    337  1.1      elad 		case 't':
    338  1.1      elad 			hash = find_hash(optarg);
    339  1.1      elad 			break;
    340  1.1      elad 		case 'v':
    341  1.1      elad 			vflag = 1;
    342  1.1      elad 			break;
    343  1.1      elad 		default:
    344  1.1      elad 			usage();
    345  1.1      elad 			return 1;
    346  1.1      elad 		}
    347  1.1      elad 	}
    348  1.1      elad 
    349  1.1      elad 	if (dbfile == NULL)
    350  1.1      elad 		dbfile = DEFAULT_DBFILE;
    351  1.1      elad 
    352  1.1      elad 	if (hash == NULL) {
    353  1.1      elad 		if ((hash = find_hash(DEFAULT_HASH)) == NULL)
    354  1.1      elad 			errx(1, "No hash algorithm");
    355  1.1      elad 	}
    356  1.1      elad 
    357  1.1      elad 	TAILQ_INIT(&fehead);
    358  1.1      elad 
    359  1.1      elad 	if (search_path == NULL)
    360  1.1      elad 		Dflag = 1;
    361  1.1      elad 
    362  1.1      elad 	if (Dflag) {
    363  1.1      elad 		char *sys_paths[] = DEFAULT_SYSPATHS;
    364  1.1      elad 
    365  1.1      elad 		if (vflag)
    366  1.1      elad 			banner(hash, sys_paths);
    367  1.1      elad 		walk_dir(sys_paths, hash);
    368  1.1      elad 	}
    369  1.1      elad 
    370  1.1      elad 	if (search_path != NULL) {
    371  1.1      elad 		if (vflag)
    372  1.1      elad 			banner(hash, search_path);
    373  1.1      elad 		walk_dir(search_path, hash);
    374  1.1      elad 	}
    375  1.1      elad 
    376  1.1      elad 	store_entries(dbfile, hash);
    377  1.1      elad 
    378  1.4      elad 	if (Sflag && chflags(dbfile, SF_IMMUTABLE) != 0)
    379  1.3      elad 		err(1, "Can't set immutable flag");
    380  1.3      elad 
    381  1.1      elad 	return 0;
    382  1.1      elad }
    383