veriexecctl.c revision 1.28 1 1.28 oster /* $NetBSD: veriexecctl.c,v 1.28 2007/05/15 22:01:19 oster Exp $ */
2 1.1 blymn
3 1.1 blymn /*-
4 1.24 elad * Copyright 2005 Elad Efrat <elad (at) NetBSD.org>
5 1.6 blymn * Copyright 2005 Brett Lymn <blymn (at) netbsd.org>
6 1.6 blymn *
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.6 blymn #include <sys/param.h>
34 1.27 elad #include <sys/statvfs.h>
35 1.6 blymn #include <sys/verified_exec.h>
36 1.1 blymn
37 1.1 blymn #include <stdio.h>
38 1.2 thorpej #include <stdlib.h>
39 1.6 blymn #include <string.h>
40 1.1 blymn #include <fcntl.h>
41 1.6 blymn #include <unistd.h>
42 1.5 blymn #include <err.h>
43 1.6 blymn #include <errno.h>
44 1.28 oster #include <sys/ioctl.h>
45 1.6 blymn
46 1.25 elad #include <prop/proplib.h>
47 1.25 elad
48 1.6 blymn #include "veriexecctl.h"
49 1.6 blymn
50 1.6 blymn #define VERIEXEC_DEVICE "/dev/veriexec"
51 1.6 blymn
52 1.27 elad #define STATUS_STRING(status) ((status) == FINGERPRINT_NOTEVAL ? \
53 1.27 elad "not evaluated" : \
54 1.27 elad (status) == FINGERPRINT_VALID ? \
55 1.27 elad "valid" : \
56 1.27 elad (status) == FINGERPRINT_NOMATCH ? \
57 1.27 elad "mismatch" : \
58 1.27 elad "<unknown>")
59 1.6 blymn
60 1.27 elad extern int yyparse(void);
61 1.7 he
62 1.27 elad int gfd, verbose = 0, error = EXIT_SUCCESS;
63 1.27 elad size_t line = 0;
64 1.6 blymn
65 1.8 christos static void
66 1.27 elad usage(void)
67 1.6 blymn {
68 1.27 elad const char *progname = getprogname();
69 1.25 elad
70 1.27 elad (void)fprintf(stderr, "Usage:\n"
71 1.27 elad "%s [-ekv] load <signature_file>\n"
72 1.27 elad "%s delete <file | mount_point>\n"
73 1.27 elad "%s query <file>\n"
74 1.27 elad "%s dump\n"
75 1.27 elad "%s flush\n", progname, progname, progname, progname, progname);
76 1.1 blymn
77 1.8 christos exit(1);
78 1.6 blymn }
79 1.6 blymn
80 1.19 elad static void
81 1.27 elad flags2str(uint8_t flags, char *buf, size_t len)
82 1.19 elad {
83 1.27 elad uint8_t all;
84 1.19 elad
85 1.27 elad all = (VERIEXEC_DIRECT | VERIEXEC_INDIRECT | VERIEXEC_FILE |
86 1.27 elad VERIEXEC_UNTRUSTED);
87 1.27 elad if (flags & ~all) {
88 1.27 elad if (verbose)
89 1.27 elad warnx("Contaminated flags `0x%x'", (flags & ~all));
90 1.19 elad return;
91 1.19 elad }
92 1.19 elad
93 1.19 elad while (flags) {
94 1.19 elad if (*buf)
95 1.27 elad strlcat(buf, ", ", len);
96 1.19 elad
97 1.19 elad if (flags & VERIEXEC_DIRECT) {
98 1.27 elad strlcat(buf, "direct", len);
99 1.19 elad flags &= ~VERIEXEC_DIRECT;
100 1.19 elad continue;
101 1.19 elad }
102 1.19 elad if (flags & VERIEXEC_INDIRECT) {
103 1.27 elad strlcat(buf, "indirect", len);
104 1.19 elad flags &= ~VERIEXEC_INDIRECT;
105 1.19 elad continue;
106 1.19 elad }
107 1.19 elad if (flags & VERIEXEC_FILE) {
108 1.27 elad strlcat(buf, "file", len);
109 1.19 elad flags &= ~VERIEXEC_FILE;
110 1.19 elad continue;
111 1.19 elad }
112 1.19 elad if (flags & VERIEXEC_UNTRUSTED) {
113 1.27 elad strlcat(buf, "untrusted", len);
114 1.19 elad flags &= ~VERIEXEC_UNTRUSTED;
115 1.19 elad continue;
116 1.19 elad }
117 1.19 elad }
118 1.19 elad }
119 1.19 elad
120 1.19 elad static void
121 1.25 elad print_query(prop_dictionary_t qp, char *file)
122 1.19 elad {
123 1.23 elad struct statvfs sv;
124 1.25 elad const char *v;
125 1.19 elad int i;
126 1.25 elad uint8_t u8;
127 1.27 elad char buf[64];
128 1.19 elad
129 1.23 elad if (statvfs(file, &sv) != 0)
130 1.23 elad err(1, "Can't statvfs() `%s'\n", file);
131 1.23 elad
132 1.19 elad printf("Filename: %s\n", file);
133 1.23 elad printf("Mount: %s\n", sv.f_mntonname);
134 1.25 elad prop_dictionary_get_uint8(qp, "entry-type", &u8);
135 1.27 elad memset(buf, 0, sizeof(buf));
136 1.27 elad flags2str(u8, buf, sizeof(buf));
137 1.27 elad printf("Entry flags: %s\n", buf);
138 1.25 elad prop_dictionary_get_uint8(qp, "status", &u8);
139 1.25 elad printf("Entry status: %s\n", STATUS_STRING(u8));
140 1.25 elad printf("Fingerprint algorithm: %s\n", dict_gets(qp, "fp-type"));
141 1.19 elad printf("Fingerprint: ");
142 1.27 elad v = dict_getd(qp, "fp");
143 1.25 elad for (i = 0; i < prop_data_size(prop_dictionary_get(qp, "fp")); i++)
144 1.25 elad printf("%02x", v[i] & 0xff);
145 1.19 elad printf("\n");
146 1.19 elad }
147 1.19 elad
148 1.27 elad static char *
149 1.27 elad escape(const char *s)
150 1.27 elad {
151 1.27 elad char *q, *p;
152 1.27 elad size_t len;
153 1.27 elad
154 1.27 elad len = strlen(s);
155 1.27 elad if (len >= MAXPATHLEN)
156 1.27 elad return (NULL);
157 1.27 elad
158 1.27 elad len *= 2;
159 1.27 elad q = p = calloc(1, len + 1);
160 1.27 elad
161 1.27 elad while (*s) {
162 1.27 elad if (*s == ' ' || *s == '\t')
163 1.27 elad *p++ = '\\';
164 1.27 elad
165 1.27 elad *p++ = *s++;
166 1.27 elad }
167 1.27 elad
168 1.27 elad return (q);
169 1.27 elad }
170 1.27 elad
171 1.27 elad static void
172 1.27 elad print_entry(prop_dictionary_t entry)
173 1.27 elad {
174 1.27 elad char *file, *fp;
175 1.27 elad const uint8_t *v;
176 1.27 elad size_t len, i;
177 1.27 elad uint8_t u8;
178 1.27 elad char flags[64];
179 1.27 elad
180 1.27 elad /* Get fingerprint in ASCII. */
181 1.27 elad len = prop_data_size(prop_dictionary_get(entry, "fp"));
182 1.27 elad len *= 2;
183 1.27 elad fp = calloc(1, len + 1);
184 1.27 elad v = dict_getd(entry, "fp");
185 1.27 elad for (i = 0; i < len; i++)
186 1.27 elad snprintf(fp, len + 1, "%s%02x", fp, v[i] & 0xff);
187 1.27 elad
188 1.27 elad /* Get flags. */
189 1.27 elad memset(flags, 0, sizeof(flags));
190 1.27 elad prop_dictionary_get_uint8(entry, "entry-type", &u8);
191 1.27 elad flags2str(u8, flags, sizeof(flags));
192 1.27 elad
193 1.27 elad file = escape(dict_gets(entry, "file"));
194 1.27 elad printf("%s %s %s %s\n", file, dict_gets(entry, "fp-type"), fp, flags);
195 1.27 elad free(file);
196 1.27 elad }
197 1.27 elad
198 1.6 blymn int
199 1.6 blymn main(int argc, char **argv)
200 1.6 blymn {
201 1.27 elad extern boolean_t keep_filename, eval_on_load;
202 1.6 blymn int c;
203 1.6 blymn
204 1.8 christos setprogname(argv[0]);
205 1.6 blymn
206 1.27 elad while ((c = getopt(argc, argv, "ekv")) != -1)
207 1.6 blymn switch (c) {
208 1.27 elad case 'e':
209 1.27 elad eval_on_load = TRUE;
210 1.27 elad break;
211 1.27 elad
212 1.27 elad case 'k':
213 1.27 elad keep_filename = TRUE;
214 1.27 elad break;
215 1.27 elad
216 1.6 blymn case 'v':
217 1.6 blymn verbose = 1;
218 1.6 blymn break;
219 1.6 blymn
220 1.6 blymn default:
221 1.8 christos usage();
222 1.6 blymn }
223 1.1 blymn
224 1.6 blymn argc -= optind;
225 1.6 blymn argv += optind;
226 1.6 blymn
227 1.8 christos if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
228 1.8 christos err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
229 1.1 blymn
230 1.9 elad /*
231 1.9 elad * Handle the different commands we can do.
232 1.9 elad */
233 1.8 christos if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
234 1.27 elad extern FILE *yyin;
235 1.27 elad int lfd;
236 1.27 elad
237 1.27 elad lfd = open(argv[1], O_RDONLY|O_EXLOCK, 0);
238 1.27 elad if (lfd == -1)
239 1.27 elad err(1, "Cannot open `%s'", argv[1]);
240 1.27 elad
241 1.27 elad yyin = fdopen(lfd, "r");
242 1.27 elad
243 1.27 elad yyparse();
244 1.27 elad
245 1.27 elad (void)fclose(yyin);
246 1.18 elad } else if (argc == 2 && strcasecmp(argv[0], "delete") == 0) {
247 1.25 elad prop_dictionary_t dp;
248 1.18 elad struct stat sb;
249 1.18 elad
250 1.18 elad if (stat(argv[1], &sb) == -1)
251 1.18 elad err(1, "Can't stat `%s'", argv[1]);
252 1.18 elad
253 1.25 elad dp = prop_dictionary_create();
254 1.25 elad dict_sets(dp, "file", argv[1]);
255 1.21 elad
256 1.18 elad /*
257 1.18 elad * If it's a regular file, remove it. If it's a directory,
258 1.18 elad * remove the entire table. If it's neither, abort.
259 1.18 elad */
260 1.21 elad if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
261 1.27 elad errx(1, "`%s' is not a regular file or directory.",
262 1.27 elad argv[1]);
263 1.18 elad
264 1.27 elad if (prop_dictionary_send_ioctl(dp, gfd, VERIEXEC_DELETE) != 0)
265 1.18 elad err(1, "Error deleting `%s'", argv[1]);
266 1.25 elad
267 1.25 elad prop_object_release(dp);
268 1.19 elad } else if (argc == 2 && strcasecmp(argv[0], "query") == 0) {
269 1.25 elad prop_dictionary_t qp, rqp;
270 1.27 elad int r;
271 1.19 elad
272 1.25 elad qp = prop_dictionary_create();
273 1.21 elad
274 1.25 elad dict_sets(qp, "file", argv[1]);
275 1.19 elad
276 1.27 elad r = prop_dictionary_sendrecv_ioctl(qp, gfd, VERIEXEC_QUERY,
277 1.27 elad &rqp);
278 1.27 elad if (r) {
279 1.27 elad if (r == ENOENT)
280 1.27 elad errx(1, "No Veriexec entry for `%s'", argv[1]);
281 1.27 elad
282 1.19 elad err(1, "Error querying `%s'", argv[1]);
283 1.27 elad }
284 1.19 elad
285 1.25 elad if (rqp != NULL) {
286 1.25 elad print_query(rqp, argv[1]);
287 1.25 elad prop_object_release(rqp);
288 1.25 elad }
289 1.25 elad
290 1.25 elad prop_object_release(qp);
291 1.27 elad } else if (argc == 1 && strcasecmp(argv[0], "dump") == 0) {
292 1.27 elad prop_array_t entries;
293 1.27 elad size_t nentries, i;
294 1.27 elad
295 1.27 elad if (prop_array_recv_ioctl(gfd, VERIEXEC_DUMP,
296 1.27 elad &entries) == -1)
297 1.27 elad err(1, "Error dumping tables");
298 1.27 elad
299 1.27 elad nentries = prop_array_count(entries);
300 1.27 elad for (i = 0; i < nentries; i++)
301 1.27 elad print_entry(prop_array_get(entries, i));
302 1.27 elad
303 1.27 elad prop_object_release(entries);
304 1.27 elad } else if (argc == 1 && strcasecmp(argv[0], "flush") == 0) {
305 1.27 elad if (ioctl(gfd, VERIEXEC_FLUSH) == -1)
306 1.27 elad err(1, "Cannot flush Veriexec database");
307 1.8 christos } else
308 1.8 christos usage();
309 1.1 blymn
310 1.8 christos (void)close(gfd);
311 1.27 elad return error;
312 1.1 blymn }
313