1 /* $NetBSD: veriexecctl.c,v 1.33 2008/08/31 23:37:45 dholland Exp $ */
4 * Copyright 2005 Elad Efrat <elad@NetBSD.org>
5 * Copyright 2005 Brett Lymn <blymn@netbsd.org>
9 * This code has been donated to The NetBSD Foundation by the Author.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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.
33 #include <sys/param.h>
34 #include <sys/statvfs.h>
35 #include <sys/verified_exec.h>
45 #include <sys/ioctl.h>
47 #include <prop/proplib.h>
49 #include "veriexecctl.h"
51 #define VERIEXEC_DEVICE "/dev/veriexec"
52 #define VERIEXEC_DEFAULT_CONFIG "/etc/signatures"
54 #define STATUS_STRING(status) ((status) == FINGERPRINT_NOTEVAL ? \
56 (status) == FINGERPRINT_VALID ? \
58 (status) == FINGERPRINT_NOMATCH ? \
62 extern int yyparse(void);
64 int gfd
, verbose
= 0, error
= EXIT_SUCCESS
;
70 const char *progname
= getprogname();
72 (void)fprintf(stderr
, "Usage:\n"
73 "%s [-ekv] load [signature_file]\n"
74 "%s delete <file | mount_point>\n"
77 "%s flush\n", progname
, progname
, progname
, progname
, progname
);
83 flags2str(uint8_t flags
, char *buf
, size_t len
)
87 all
= (VERIEXEC_DIRECT
| VERIEXEC_INDIRECT
| VERIEXEC_FILE
|
91 warnx("Contaminated flags `0x%x'", (flags
& ~all
));
97 strlcat(buf
, ", ", len
);
99 if (flags
& VERIEXEC_DIRECT
) {
100 strlcat(buf
, "direct", len
);
101 flags
&= ~VERIEXEC_DIRECT
;
104 if (flags
& VERIEXEC_INDIRECT
) {
105 strlcat(buf
, "indirect", len
);
106 flags
&= ~VERIEXEC_INDIRECT
;
109 if (flags
& VERIEXEC_FILE
) {
110 strlcat(buf
, "file", len
);
111 flags
&= ~VERIEXEC_FILE
;
114 if (flags
& VERIEXEC_UNTRUSTED
) {
115 strlcat(buf
, "untrusted", len
);
116 flags
&= ~VERIEXEC_UNTRUSTED
;
123 print_query(prop_dictionary_t qp
, char *file
)
131 if (statvfs(file
, &sv
) != 0)
132 err(1, "Can't statvfs() `%s'\n", file
);
134 printf("Filename: %s\n", file
);
135 printf("Mount: %s\n", sv
.f_mntonname
);
136 prop_dictionary_get_uint8(qp
, "entry-type", &u8
);
137 memset(buf
, 0, sizeof(buf
));
138 flags2str(u8
, buf
, sizeof(buf
));
139 printf("Entry flags: %s\n", buf
);
140 prop_dictionary_get_uint8(qp
, "status", &u8
);
141 printf("Entry status: %s\n", STATUS_STRING(u8
));
142 printf("Fingerprint algorithm: %s\n", dict_gets(qp
, "fp-type"));
143 printf("Fingerprint: ");
144 v
= dict_getd(qp
, "fp");
145 for (i
= 0; i
< prop_data_size(prop_dictionary_get(qp
, "fp")); i
++)
146 printf("%02x", v
[i
] & 0xff);
151 escape(const char *s
)
157 if (len
>= MAXPATHLEN
)
161 q
= p
= calloc(1, len
+ 1);
164 if (*s
== ' ' || *s
== '\t')
174 print_entry(prop_dictionary_t entry
)
182 /* Get fingerprint in ASCII. */
183 len
= prop_data_size(prop_dictionary_get(entry
, "fp"));
185 fp
= calloc(1, len
+ 1);
186 v
= dict_getd(entry
, "fp");
187 for (i
= 0; i
< len
; i
++)
188 snprintf(fp
, len
+ 1, "%s%02x", fp
, v
[i
] & 0xff);
191 memset(flags
, 0, sizeof(flags
));
192 prop_dictionary_get_uint8(entry
, "entry-type", &u8
);
193 flags2str(u8
, flags
, sizeof(flags
));
195 file
= escape(dict_gets(entry
, "file"));
196 printf("%s %s %s %s\n", file
, dict_gets(entry
, "fp-type"), fp
, flags
);
202 main(int argc
, char **argv
)
204 extern bool keep_filename
, eval_on_load
;
207 setprogname(argv
[0]);
209 while ((c
= getopt(argc
, argv
, "ekv")) != -1)
216 keep_filename
= true;
230 if ((gfd
= open(VERIEXEC_DEVICE
, O_RDWR
, 0)) == -1)
231 err(1, "Cannot open `%s'", VERIEXEC_DEVICE
);
234 * Handle the different commands we can do.
236 if ((argc
== 1 || argc
== 2) && strcasecmp(argv
[0], "load") == 0) {
242 file
= VERIEXEC_DEFAULT_CONFIG
;
246 lfd
= open(file
, O_RDONLY
|O_EXLOCK
, 0);
248 err(1, "Cannot open `%s'", argv
[1]);
250 yyin
= fdopen(lfd
, "r");
255 } else if (argc
== 2 && strcasecmp(argv
[0], "delete") == 0) {
256 prop_dictionary_t dp
;
259 if (stat(argv
[1], &sb
) == -1)
260 err(1, "Can't stat `%s'", argv
[1]);
263 * If it's a regular file, remove it. If it's a directory,
264 * remove the entire table. If it's neither, abort.
266 if (!S_ISDIR(sb
.st_mode
) && !S_ISREG(sb
.st_mode
))
267 errx(1, "`%s' is not a regular file or directory.",
270 dp
= prop_dictionary_create();
271 dict_sets(dp
, "file", argv
[1]);
273 if (prop_dictionary_send_ioctl(dp
, gfd
, VERIEXEC_DELETE
) != 0)
274 err(1, "Error deleting `%s'", argv
[1]);
276 prop_object_release(dp
);
277 } else if (argc
== 2 && strcasecmp(argv
[0], "query") == 0) {
278 prop_dictionary_t qp
, rqp
;
281 qp
= prop_dictionary_create();
283 dict_sets(qp
, "file", argv
[1]);
285 r
= prop_dictionary_sendrecv_ioctl(qp
, gfd
, VERIEXEC_QUERY
,
289 errx(1, "No Veriexec entry for `%s'", argv
[1]);
291 err(1, "Error querying `%s'", argv
[1]);
295 print_query(rqp
, argv
[1]);
296 prop_object_release(rqp
);
299 prop_object_release(qp
);
300 } else if (argc
== 1 && strcasecmp(argv
[0], "dump") == 0) {
301 prop_array_t entries
;
304 if (prop_array_recv_ioctl(gfd
, VERIEXEC_DUMP
,
306 err(1, "Error dumping tables");
308 nentries
= prop_array_count(entries
);
309 for (i
= 0; i
< nentries
; i
++)
310 print_entry(prop_array_get(entries
, i
));
312 prop_object_release(entries
);
313 } else if (argc
== 1 && strcasecmp(argv
[0], "flush") == 0) {
314 if (ioctl(gfd
, VERIEXEC_FLUSH
) == -1)
315 err(1, "Cannot flush Veriexec database");