Home | History | Annotate | Line # | Download | only in veriexecctl
veriexecctl.c revision 1.5.6.11
      1  1.5.6.11     tron /*	$NetBSD: veriexecctl.c,v 1.5.6.11 2005/07/02 15:46:26 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.11     tron extern int yynerrs;
     53   1.5.6.5     tron int gfd, verbose = 0, phase;
     54   1.5.6.3     tron size_t line;
     55       1.1    blymn 
     56   1.5.6.1     tron /*
     57   1.5.6.1     tron  * Prototypes
     58   1.5.6.1     tron  */
     59   1.5.6.3     tron static FILE *openlock(const char *);
     60   1.5.6.3     tron static void phase1_preload(void);
     61   1.5.6.3     tron static int fingerprint_load(char*);
     62   1.5.6.3     tron static void usage(void) __attribute__((__noreturn__));
     63   1.5.6.2     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.9     tron 		if (ioctl(gfd, VERIEXEC_TABLESIZE, &(vup->vu_param)) == -1) {
    116   1.5.6.9     tron 			if (errno != EEXIST)
    117   1.5.6.9     tron 				err(1, "Error in phase 1: Can't "
    118   1.5.6.9     tron 				    "set hash table size for device %d",
    119   1.5.6.9     tron 				    vup->vu_param.dev);
    120   1.5.6.9     tron 		}
    121   1.5.6.1     tron 
    122   1.5.6.1     tron 		if (verbose) {
    123   1.5.6.1     tron 			printf(" => Hash table sizing successful for device "
    124   1.5.6.2     tron 			    "%d. (%zu entries)\n", vup->vu_param.dev,
    125   1.5.6.1     tron 			    vup->vu_param.hash_size);
    126   1.5.6.1     tron 		}
    127   1.5.6.1     tron 
    128   1.5.6.1     tron 		CIRCLEQ_REMOVE(&params_list, vup, vu_list);
    129   1.5.6.1     tron 		free(vup);
    130   1.5.6.1     tron 	}
    131   1.5.6.1     tron }
    132   1.5.6.1     tron 
    133   1.5.6.1     tron /*
    134   1.5.6.1     tron  * Load the fingerprint. Assumes that the fingerprint pseudo-device is
    135   1.5.6.2     tron  * opened and the file handle is in gfd.
    136   1.5.6.1     tron  */
    137   1.5.6.1     tron void
    138   1.5.6.1     tron phase2_load(void)
    139   1.5.6.1     tron {
    140   1.5.6.3     tron 	if (ioctl(gfd, VERIEXEC_LOAD, &params) == -1)
    141   1.5.6.7     tron 		warn("Cannot load params from `%s'", params.file);
    142   1.5.6.1     tron 	free(params.fingerprint);
    143   1.5.6.1     tron }
    144   1.5.6.1     tron 
    145   1.5.6.1     tron /*
    146   1.5.6.1     tron  * Fingerprint load handling.
    147   1.5.6.1     tron  */
    148   1.5.6.3     tron static int
    149   1.5.6.2     tron fingerprint_load(char *ifile)
    150   1.5.6.1     tron {
    151   1.5.6.1     tron 	CIRCLEQ_INIT(&params_list);
    152  1.5.6.11     tron 	memset(&params, 0, sizeof(params));
    153       1.1    blymn 
    154   1.5.6.3     tron 	if ((yyin = openlock(ifile)) == NULL)
    155   1.5.6.3     tron 		err(1, "Cannot open `%s'", ifile);
    156       1.1    blymn 
    157   1.5.6.1     tron 	/*
    158   1.5.6.1     tron 	 * Phase 1: Scan all config files, creating the list of devices
    159   1.5.6.1     tron 	 *	    we have fingerprinted files on, and the amount of
    160   1.5.6.1     tron 	 *	    files per device. Lock all files to maintain sync.
    161   1.5.6.1     tron 	 */
    162   1.5.6.1     tron 	phase = 1;
    163   1.5.6.1     tron 
    164   1.5.6.1     tron 	if (verbose) {
    165   1.5.6.3     tron 		(void)printf("Phase 1: Building hash table information:\n");
    166   1.5.6.3     tron 		(void)printf("=> Parsing \"%s\"\n", ifile);
    167   1.5.6.1     tron 	}
    168   1.5.6.1     tron 
    169  1.5.6.11     tron 	line = 1;
    170   1.5.6.1     tron 	yyparse();
    171  1.5.6.11     tron 	if (yynerrs)
    172  1.5.6.11     tron 		return -1;
    173   1.5.6.1     tron 
    174   1.5.6.3     tron 	phase1_preload();
    175   1.5.6.1     tron 
    176   1.5.6.1     tron 	/*
    177   1.5.6.1     tron 	 * Phase 2: After we have a circular queue containing all the
    178   1.5.6.1     tron 	 * 	    devices we care about and the sizes for the hash
    179   1.5.6.1     tron 	 *	    tables, do a rescan, this time actually loading the
    180   1.5.6.1     tron 	 *	    file data.
    181   1.5.6.1     tron 	 */
    182   1.5.6.1     tron 	rewind(yyin);
    183   1.5.6.1     tron 	phase = 2;
    184   1.5.6.1     tron 	if (verbose) {
    185   1.5.6.3     tron 		(void)printf("Phase 2: Loading per-file fingerprints.\n");
    186   1.5.6.3     tron 		(void)printf("=> Parsing \"%s\"\n", ifile);
    187       1.1    blymn 	}
    188       1.1    blymn 
    189  1.5.6.11     tron 	line = 1;
    190       1.1    blymn 	yyparse();
    191   1.5.6.1     tron 
    192   1.5.6.3     tron 	(void)fclose(yyin);
    193   1.5.6.1     tron 
    194   1.5.6.3     tron 	return 0;
    195   1.5.6.3     tron }
    196   1.5.6.3     tron 
    197   1.5.6.3     tron static void
    198   1.5.6.3     tron usage(void)
    199   1.5.6.3     tron {
    200   1.5.6.8     tron 	(void)fprintf(stderr, "Usage: %s [-v] [load <signature_file>]\n",
    201   1.5.6.4     tron 	    getprogname());
    202   1.5.6.3     tron 	exit(1);
    203   1.5.6.1     tron }
    204   1.5.6.1     tron 
    205   1.5.6.1     tron int
    206   1.5.6.1     tron main(int argc, char **argv)
    207   1.5.6.1     tron {
    208   1.5.6.1     tron 	int c;
    209   1.5.6.1     tron 
    210   1.5.6.3     tron 	setprogname(argv[0]);
    211   1.5.6.1     tron 
    212   1.5.6.3     tron 	while ((c = getopt(argc, argv, "v")) != -1)
    213   1.5.6.1     tron 		switch (c) {
    214   1.5.6.1     tron 		case 'v':
    215   1.5.6.1     tron 			verbose = 1;
    216   1.5.6.1     tron 			break;
    217   1.5.6.1     tron 
    218   1.5.6.1     tron 		default:
    219   1.5.6.3     tron 			usage();
    220   1.5.6.1     tron 		}
    221   1.5.6.1     tron 
    222   1.5.6.1     tron 	argc -= optind;
    223   1.5.6.1     tron 	argv += optind;
    224   1.5.6.1     tron 
    225   1.5.6.3     tron 	if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
    226   1.5.6.3     tron 		err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
    227   1.5.6.1     tron 
    228   1.5.6.4     tron 	/*
    229   1.5.6.4     tron 	 * Handle the different commands we can do.
    230   1.5.6.4     tron 	 */
    231   1.5.6.3     tron 	if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
    232   1.5.6.1     tron 		filename = argv[1];
    233   1.5.6.1     tron 		fingerprint_load(argv[1]);
    234   1.5.6.3     tron 	} else
    235   1.5.6.3     tron 		usage();
    236   1.5.6.1     tron 
    237   1.5.6.3     tron 	(void)close(gfd);
    238   1.5.6.3     tron 	return 0;
    239       1.1    blymn }
    240