Home | History | Annotate | Line # | Download | only in veriexecctl
veriexecctl.c revision 1.5.6.5
      1  1.5.6.5     tron /*	$NetBSD: veriexecctl.c,v 1.5.6.5 2005/06/10 15:13:52 tron Exp $	*/
      2      1.1    blymn 
      3      1.1    blymn /*-
      4  1.5.6.1     tron  * Copyright 2005 Elad Efrat <elad (at) bsd.org.il>
      5  1.5.6.1     tron  * Copyright 2005 Brett Lymn <blymn (at) netbsd.org>
      6  1.5.6.1     tron  *
      7      1.1    blymn  * All rights reserved.
      8      1.1    blymn  *
      9      1.1    blymn  * This code has been donated to The NetBSD Foundation by the Author.
     10      1.1    blymn  *
     11      1.1    blymn  * Redistribution and use in source and binary forms, with or without
     12      1.1    blymn  * modification, are permitted provided that the following conditions
     13      1.1    blymn  * are met:
     14      1.1    blymn  * 1. Redistributions of source code must retain the above copyright
     15      1.1    blymn  *    notice, this list of conditions and the following disclaimer.
     16      1.1    blymn  * 2. The name of the author may not be used to endorse or promote products
     17      1.1    blymn  *    derived from this software withough specific prior written permission
     18      1.1    blymn  *
     19      1.1    blymn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20      1.1    blymn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21      1.1    blymn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22      1.1    blymn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23      1.1    blymn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24      1.1    blymn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25      1.1    blymn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26      1.1    blymn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27      1.1    blymn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28      1.1    blymn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29      1.1    blymn  *
     30      1.1    blymn  *
     31      1.1    blymn  */
     32      1.1    blymn 
     33  1.5.6.1     tron #include <sys/ioctl.h>
     34  1.5.6.1     tron #include <sys/param.h>
     35  1.5.6.1     tron #include <sys/queue.h>
     36  1.5.6.1     tron #include <sys/verified_exec.h>
     37      1.1    blymn 
     38      1.1    blymn #include <stdio.h>
     39      1.2  thorpej #include <stdlib.h>
     40  1.5.6.1     tron #include <string.h>
     41      1.1    blymn #include <fcntl.h>
     42  1.5.6.1     tron #include <unistd.h>
     43      1.5    blymn #include <err.h>
     44  1.5.6.1     tron #include <errno.h>
     45  1.5.6.1     tron 
     46  1.5.6.1     tron #include "veriexecctl.h"
     47  1.5.6.1     tron 
     48  1.5.6.1     tron #define	VERIEXEC_DEVICE	"/dev/veriexec"
     49      1.5    blymn 
     50  1.5.6.1     tron extern struct veriexec_params params; /* in veriexecctl_parse.y */
     51  1.5.6.1     tron extern char *filename; /* in veriexecctl_conf.l */
     52  1.5.6.5     tron int gfd, verbose = 0, phase;
     53  1.5.6.3     tron size_t line;
     54      1.1    blymn 
     55  1.5.6.1     tron /*
     56  1.5.6.1     tron  * Prototypes
     57  1.5.6.1     tron  */
     58  1.5.6.3     tron static FILE *openlock(const char *);
     59  1.5.6.3     tron static void phase1_preload(void);
     60  1.5.6.3     tron static int fingerprint_load(char*);
     61  1.5.6.3     tron static void usage(void) __attribute__((__noreturn__));
     62  1.5.6.2     tron 
     63  1.5.6.1     tron 
     64  1.5.6.3     tron static FILE *
     65  1.5.6.1     tron openlock(const char *path)
     66      1.1    blymn {
     67  1.5.6.2     tron 	int lfd;
     68  1.5.6.1     tron 
     69  1.5.6.3     tron 	if ((lfd = open(path, O_RDONLY|O_EXLOCK, 0)) == -1)
     70  1.5.6.3     tron 		return NULL;
     71  1.5.6.1     tron 
     72  1.5.6.3     tron 	return fdopen(lfd, "r");
     73  1.5.6.1     tron }
     74  1.5.6.1     tron 
     75  1.5.6.5     tron struct veriexec_up *
     76  1.5.6.1     tron dev_lookup(dev_t d)
     77  1.5.6.1     tron {
     78  1.5.6.5     tron 	struct veriexec_up *p;
     79  1.5.6.1     tron 
     80  1.5.6.3     tron 	CIRCLEQ_FOREACH(p, &params_list, vu_list)
     81  1.5.6.1     tron 		if (p->vu_param.dev == d)
     82  1.5.6.1     tron 			return (p);
     83  1.5.6.1     tron 
     84  1.5.6.3     tron 	return NULL;
     85  1.5.6.1     tron }
     86  1.5.6.1     tron 
     87  1.5.6.5     tron struct veriexec_up *
     88  1.5.6.1     tron dev_add(dev_t d)
     89  1.5.6.1     tron {
     90  1.5.6.5     tron 	struct veriexec_up *up;
     91  1.5.6.1     tron 
     92  1.5.6.3     tron 	if ((up = calloc((size_t)1, sizeof(*up))) == NULL)
     93  1.5.6.1     tron 		err(1, "No memory");
     94  1.5.6.1     tron 
     95  1.5.6.1     tron 	up->vu_param.dev = d;
     96  1.5.6.1     tron 	up->vu_param.hash_size = 1;
     97  1.5.6.1     tron 
     98  1.5.6.1     tron 	CIRCLEQ_INSERT_TAIL(&params_list, up, vu_list);
     99  1.5.6.1     tron 
    100  1.5.6.3     tron 	return up;
    101  1.5.6.1     tron }
    102  1.5.6.1     tron 
    103  1.5.6.1     tron /* Load all devices, get rid of the list. */
    104  1.5.6.3     tron static void
    105  1.5.6.1     tron phase1_preload(void)
    106  1.5.6.1     tron {
    107  1.5.6.1     tron 	if (verbose)
    108  1.5.6.1     tron 		printf("Phase 1: Calculating hash table sizes:\n");
    109  1.5.6.1     tron 
    110  1.5.6.1     tron 	while (!CIRCLEQ_EMPTY(&params_list)) {
    111  1.5.6.5     tron 		struct veriexec_up *vup;
    112  1.5.6.1     tron 
    113  1.5.6.1     tron 		vup = CIRCLEQ_FIRST(&params_list);
    114  1.5.6.1     tron 
    115  1.5.6.3     tron 		if (ioctl(gfd, VERIEXEC_TABLESIZE, &(vup->vu_param)) == -1)
    116  1.5.6.3     tron 			err(1, "Error in phase 1: Can't "
    117  1.5.6.3     tron 			    "set hash table size for device %d",
    118  1.5.6.3     tron 			    vup->vu_param.dev);
    119  1.5.6.1     tron 
    120  1.5.6.1     tron 		if (verbose) {
    121  1.5.6.1     tron 			printf(" => Hash table sizing successful for device "
    122  1.5.6.2     tron 			    "%d. (%zu entries)\n", vup->vu_param.dev,
    123  1.5.6.1     tron 			    vup->vu_param.hash_size);
    124  1.5.6.1     tron 		}
    125  1.5.6.1     tron 
    126  1.5.6.1     tron 		CIRCLEQ_REMOVE(&params_list, vup, vu_list);
    127  1.5.6.1     tron 		free(vup);
    128  1.5.6.1     tron 	}
    129  1.5.6.1     tron }
    130  1.5.6.1     tron 
    131  1.5.6.1     tron /*
    132  1.5.6.1     tron  * Load the fingerprint. Assumes that the fingerprint pseudo-device is
    133  1.5.6.2     tron  * opened and the file handle is in gfd.
    134  1.5.6.1     tron  */
    135  1.5.6.1     tron void
    136  1.5.6.1     tron phase2_load(void)
    137  1.5.6.1     tron {
    138  1.5.6.3     tron 	if (ioctl(gfd, VERIEXEC_LOAD, &params) == -1)
    139  1.5.6.3     tron 		err(1, "Cannot load params from `%s'", params.file);
    140  1.5.6.1     tron 	free(params.fingerprint);
    141  1.5.6.1     tron }
    142  1.5.6.1     tron 
    143  1.5.6.1     tron /*
    144  1.5.6.1     tron  * Fingerprint load handling.
    145  1.5.6.1     tron  */
    146  1.5.6.3     tron static int
    147  1.5.6.2     tron fingerprint_load(char *ifile)
    148  1.5.6.1     tron {
    149  1.5.6.1     tron 	CIRCLEQ_INIT(&params_list);
    150      1.1    blymn 
    151  1.5.6.3     tron 	if ((yyin = openlock(ifile)) == NULL)
    152  1.5.6.3     tron 		err(1, "Cannot open `%s'", ifile);
    153      1.1    blymn 
    154  1.5.6.1     tron 	/*
    155  1.5.6.1     tron 	 * Phase 1: Scan all config files, creating the list of devices
    156  1.5.6.1     tron 	 *	    we have fingerprinted files on, and the amount of
    157  1.5.6.1     tron 	 *	    files per device. Lock all files to maintain sync.
    158  1.5.6.1     tron 	 */
    159  1.5.6.1     tron 	phase = 1;
    160  1.5.6.1     tron 
    161  1.5.6.1     tron 	if (verbose) {
    162  1.5.6.3     tron 		(void)printf("Phase 1: Building hash table information:\n");
    163  1.5.6.3     tron 		(void)printf("=> Parsing \"%s\"\n", ifile);
    164  1.5.6.1     tron 	}
    165  1.5.6.1     tron 
    166  1.5.6.1     tron 	yyparse();
    167  1.5.6.1     tron 
    168  1.5.6.3     tron 	phase1_preload();
    169  1.5.6.1     tron 
    170  1.5.6.1     tron 	/*
    171  1.5.6.1     tron 	 * Phase 2: After we have a circular queue containing all the
    172  1.5.6.1     tron 	 * 	    devices we care about and the sizes for the hash
    173  1.5.6.1     tron 	 *	    tables, do a rescan, this time actually loading the
    174  1.5.6.1     tron 	 *	    file data.
    175  1.5.6.1     tron 	 */
    176  1.5.6.1     tron 	rewind(yyin);
    177  1.5.6.1     tron 	phase = 2;
    178  1.5.6.1     tron 	if (verbose) {
    179  1.5.6.3     tron 		(void)printf("Phase 2: Loading per-file fingerprints.\n");
    180  1.5.6.3     tron 		(void)printf("=> Parsing \"%s\"\n", ifile);
    181      1.1    blymn 	}
    182      1.1    blymn 
    183      1.1    blymn 	yyparse();
    184  1.5.6.1     tron 
    185  1.5.6.3     tron 	(void)fclose(yyin);
    186  1.5.6.1     tron 
    187  1.5.6.3     tron 	return 0;
    188  1.5.6.3     tron }
    189  1.5.6.3     tron 
    190  1.5.6.3     tron static void
    191  1.5.6.3     tron usage(void)
    192  1.5.6.3     tron {
    193  1.5.6.4     tron 	(void)fprintf(stderr, "Usage: %s [-v] [load <signature_file>] ",
    194  1.5.6.4     tron 	    getprogname());
    195  1.5.6.3     tron 	exit(1);
    196  1.5.6.1     tron }
    197  1.5.6.1     tron 
    198  1.5.6.1     tron int
    199  1.5.6.1     tron main(int argc, char **argv)
    200  1.5.6.1     tron {
    201  1.5.6.1     tron 	char *newp;
    202  1.5.6.1     tron 	int c;
    203  1.5.6.3     tron 	size_t size;
    204  1.5.6.1     tron 	struct veriexec_fp_report report;
    205  1.5.6.1     tron 
    206  1.5.6.3     tron 	setprogname(argv[0]);
    207  1.5.6.1     tron 
    208  1.5.6.3     tron 	while ((c = getopt(argc, argv, "v")) != -1)
    209  1.5.6.1     tron 		switch (c) {
    210  1.5.6.1     tron 		case 'v':
    211  1.5.6.1     tron 			verbose = 1;
    212  1.5.6.1     tron 			break;
    213  1.5.6.1     tron 
    214  1.5.6.1     tron 		default:
    215  1.5.6.3     tron 			usage();
    216  1.5.6.1     tron 		}
    217  1.5.6.1     tron 
    218  1.5.6.1     tron 	argc -= optind;
    219  1.5.6.1     tron 	argv += optind;
    220  1.5.6.1     tron 
    221  1.5.6.3     tron 	if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
    222  1.5.6.3     tron 		err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
    223  1.5.6.1     tron 
    224  1.5.6.4     tron 	/*
    225  1.5.6.4     tron 	 * Handle the different commands we can do.
    226  1.5.6.4     tron 	 */
    227  1.5.6.3     tron 	if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
    228  1.5.6.1     tron 		line = 0;
    229  1.5.6.1     tron 		filename = argv[1];
    230  1.5.6.1     tron 		fingerprint_load(argv[1]);
    231  1.5.6.3     tron 	} else
    232  1.5.6.3     tron 		usage();
    233  1.5.6.1     tron 
    234  1.5.6.3     tron 	(void)close(gfd);
    235  1.5.6.3     tron 	return 0;
    236      1.1    blymn }
    237