veriexecctl.c revision 1.18 1 /* $NetBSD: veriexecctl.c,v 1.18 2005/12/10 02:10:00 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 /*
141 * If there's no access type specified, use the default.
142 */
143 if (!(params.type & (VERIEXEC_DIRECT|VERIEXEC_INDIRECT|VERIEXEC_FILE)))
144 params.type |= VERIEXEC_DIRECT;
145 if (ioctl(gfd, VERIEXEC_LOAD, ¶ms) == -1)
146 warn("Cannot load params from `%s'", params.file);
147 free(params.fingerprint);
148 }
149
150 /*
151 * Fingerprint load handling.
152 */
153 static int
154 fingerprint_load(char *ifile)
155 {
156 CIRCLEQ_INIT(¶ms_list);
157 memset(¶ms, 0, sizeof(params));
158
159 if ((yyin = openlock(ifile)) == NULL)
160 err(1, "Cannot open `%s'", ifile);
161
162 /*
163 * Phase 1: Scan all config files, creating the list of devices
164 * we have fingerprinted files on, and the amount of
165 * files per device. Lock all files to maintain sync.
166 */
167 phase = 1;
168
169 if (verbose) {
170 (void)printf("Phase 1: Building hash table information:\n");
171 (void)printf("=> Parsing \"%s\"\n", ifile);
172 }
173
174 line = 1;
175 yyparse();
176 if (yynerrs)
177 return -1;
178
179 phase1_preload();
180
181 /*
182 * Phase 2: After we have a circular queue containing all the
183 * devices we care about and the sizes for the hash
184 * tables, do a rescan, this time actually loading the
185 * file data.
186 */
187 rewind(yyin);
188 phase = 2;
189 if (verbose) {
190 (void)printf("Phase 2: Loading per-file fingerprints.\n");
191 (void)printf("=> Parsing \"%s\"\n", ifile);
192 }
193
194 line = 1;
195 yyparse();
196
197 (void)fclose(yyin);
198
199 return 0;
200 }
201
202 static void
203 usage(void)
204 {
205 (void)fprintf(stderr, "Usage: %s [-v] [load <signature_file>]\n",
206 getprogname());
207 exit(1);
208 }
209
210 int
211 main(int argc, char **argv)
212 {
213 int c;
214
215 setprogname(argv[0]);
216
217 while ((c = getopt(argc, argv, "v")) != -1)
218 switch (c) {
219 case 'v':
220 verbose = 1;
221 break;
222
223 default:
224 usage();
225 }
226
227 argc -= optind;
228 argv += optind;
229
230 if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
231 err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
232
233 /*
234 * Handle the different commands we can do.
235 */
236 if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
237 filename = argv[1];
238 fingerprint_load(argv[1]);
239 } else if (argc == 2 && strcasecmp(argv[0], "delete") == 0) {
240 struct veriexec_delete_params dp;
241 struct stat sb;
242
243 /* Get device and inode */
244 if (stat(argv[1], &sb) == -1)
245 err(1, "Can't stat `%s'", argv[1]);
246
247 /*
248 * If it's a regular file, remove it. If it's a directory,
249 * remove the entire table. If it's neither, abort.
250 */
251 if (S_ISDIR(sb.st_mode))
252 dp.ino = 0;
253 else if (S_ISREG(sb.st_mode))
254 dp.ino = sb.st_ino;
255 else
256 errx(1, "`%s' is not a regular file or directory.", argv[1]);
257
258 dp.dev = sb.st_dev;
259
260 if (ioctl(gfd, VERIEXEC_DELETE, &dp) == -1)
261 err(1, "Error deleting `%s'", argv[1]);
262 } else
263 usage();
264
265 (void)close(gfd);
266 return 0;
267 }
268