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