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