veriexecgen.c revision 1.13 1 1.13 mjf /* $NetBSD: veriexecgen.c,v 1.13 2007/01/09 13:53:31 mjf Exp $ */
2 1.1 elad
3 1.1 elad /*-
4 1.1 elad * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 elad * All rights reserved.
6 1.1 elad *
7 1.1 elad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 elad * by Matt Fleming.
9 1.1 elad *
10 1.1 elad * Redistribution and use in source and binary forms, with or without
11 1.1 elad * modification, are permitted provided that the following conditions
12 1.1 elad * are met:
13 1.1 elad * 1. Redistributions of source code must retain the above copyright
14 1.1 elad * notice, this list of conditions and the following disclaimer.
15 1.1 elad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elad * notice, this list of conditions and the following disclaimer in the
17 1.1 elad * documentation and/or other materials provided with the distribution.
18 1.13 mjf * 3. Neither the name of The NetBSD Foundation nor the names of its
19 1.1 elad * contributors may be used to endorse or promote products derived
20 1.1 elad * from this software without specific prior written permission.
21 1.1 elad *
22 1.1 elad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.1 elad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.1 elad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1 elad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.1 elad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 elad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 elad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 elad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 elad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 elad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1 elad * POSSIBILITY OF SUCH DAMAGE.
33 1.1 elad */
34 1.11 agc #if HAVE_NBTOOL_CONFIG_H
35 1.11 agc #include "nbtool_config.h"
36 1.11 agc #endif
37 1.11 agc
38 1.11 agc #include <sys/cdefs.h>
39 1.11 agc
40 1.11 agc #ifndef lint
41 1.11 agc #ifdef __RCSID
42 1.13 mjf __RCSID("$NetBSD: veriexecgen.c,v 1.13 2007/01/09 13:53:31 mjf Exp $");
43 1.11 agc #endif
44 1.11 agc #endif /* not lint */
45 1.11 agc
46 1.11 agc #include <sys/types.h>
47 1.1 elad
48 1.1 elad #include <sys/param.h>
49 1.1 elad #include <sys/types.h>
50 1.1 elad #include <sys/queue.h>
51 1.1 elad #include <sys/stat.h>
52 1.1 elad #include <sys/dirent.h>
53 1.1 elad #include <sys/verified_exec.h>
54 1.1 elad
55 1.1 elad #include <err.h>
56 1.1 elad #include <errno.h>
57 1.1 elad #include <fts.h>
58 1.1 elad #include <stdio.h>
59 1.1 elad #include <stdlib.h>
60 1.1 elad #include <string.h>
61 1.1 elad #include <time.h>
62 1.1 elad #include <unistd.h>
63 1.1 elad #include <util.h>
64 1.1 elad
65 1.1 elad #include <md5.h>
66 1.2 elad #include <sha1.h>
67 1.8 christos #include <sha2.h>
68 1.8 christos #include <rmd160.h>
69 1.1 elad
70 1.1 elad #define IS_EXEC(mode) ((mode) & (S_IXUSR | S_IXGRP | S_IXOTH))
71 1.1 elad
72 1.1 elad #define DEFAULT_DBFILE "/etc/signatures"
73 1.1 elad #define DEFAULT_HASH "sha256"
74 1.1 elad #define DEFAULT_SYSPATHS { "/bin", "/sbin", "/usr/bin", "/usr/sbin", \
75 1.6 elad "/lib", "/usr/lib", "/libexec", "/usr/libexec", \
76 1.6 elad NULL }
77 1.1 elad
78 1.11 agc /* this struct defines a hash algorithm */
79 1.11 agc typedef struct hash_t {
80 1.11 agc const char *hashname; /* algorithm name */
81 1.11 agc char *(*filefunc)(const char *, char *); /* function */
82 1.11 agc } hash_t;
83 1.11 agc
84 1.11 agc /* this struct encapsulates various diverse options and arguments */
85 1.11 agc typedef struct veriexecgen_t {
86 1.11 agc int all_files; /* scan also for non-executable files */
87 1.11 agc int append_output; /* append output to existing sigs file */
88 1.11 agc char *dbfile; /* name of signatures database file */
89 1.11 agc int exit_on_error; /* exit if we can't create a hash */
90 1.11 agc char *prefix; /* any prefix to be discarded on output */
91 1.11 agc int recursive_scan;/* perform scan for files recursively */
92 1.11 agc int scan_system_dirs; /* just scan system directories */
93 1.11 agc int verbose; /* verbosity level */
94 1.11 agc } veriexecgen_t;
95 1.11 agc
96 1.10 agc /* this struct describes a directory entry to generate a hash for */
97 1.1 elad struct fentry {
98 1.10 agc char filename[MAXPATHLEN]; /* name of entry */
99 1.10 agc char *hash_val; /* its associated hash value */
100 1.10 agc int flags; /* any associated flags */
101 1.10 agc TAILQ_ENTRY(fentry) f; /* its place in the queue */
102 1.1 elad };
103 1.1 elad TAILQ_HEAD(, fentry) fehead;
104 1.1 elad
105 1.11 agc /* define the possible hash algorithms */
106 1.11 agc static hash_t hashes[] = {
107 1.1 elad { "MD5", MD5File },
108 1.1 elad { "SHA1", SHA1File },
109 1.1 elad { "SHA256", SHA256_File },
110 1.1 elad { "SHA384", SHA384_File },
111 1.1 elad { "SHA512", SHA512_File },
112 1.1 elad { "RMD160", RMD160File },
113 1.1 elad { NULL, NULL },
114 1.1 elad };
115 1.1 elad
116 1.10 agc static int Fflag;
117 1.9 agc
118 1.10 agc static int make_immutable; /* set immutable flag on signatures file */
119 1.9 agc
120 1.9 agc /* warn about a problem - exit if exit_on_error is set */
121 1.9 agc static void
122 1.11 agc gripe(veriexecgen_t *vp, const char *fmt, const char *filename)
123 1.9 agc {
124 1.9 agc warn(fmt, filename);
125 1.11 agc if (vp->exit_on_error) {
126 1.9 agc /* error out on problematic files */
127 1.9 agc exit(EXIT_FAILURE);
128 1.9 agc }
129 1.9 agc }
130 1.1 elad
131 1.10 agc /* print usage message */
132 1.1 elad static void
133 1.1 elad usage(void)
134 1.1 elad {
135 1.1 elad (void)fprintf(stderr,
136 1.12 wiz "usage: %s [-AaDrSvW] [-d dir] [-o fingerprintdb] [-p prefix]\n"
137 1.12 wiz "\t\t [-t algorithm]\n"
138 1.12 wiz "\t%s [-h]\n", getprogname(), getprogname());
139 1.1 elad }
140 1.1 elad
141 1.10 agc /* tell people what we're doing - scan dirs, fingerprint etc */
142 1.1 elad static void
143 1.11 agc banner(veriexecgen_t *vp, hash_t *hash_type, char **search_path)
144 1.1 elad {
145 1.1 elad int j;
146 1.1 elad
147 1.1 elad (void)printf("Fingerprinting ");
148 1.1 elad
149 1.1 elad for (j = 0; search_path[j] != NULL; j++)
150 1.1 elad (void)printf("%s ", search_path[j]);
151 1.1 elad
152 1.1 elad (void)printf("(%s) (%s) using %s\n",
153 1.11 agc vp->all_files ? "all files" : "executables only",
154 1.11 agc vp->recursive_scan ? "recursive" : "non-recursive",
155 1.1 elad hash_type->hashname);
156 1.1 elad }
157 1.1 elad
158 1.10 agc /* find a hash algorithm, given its name */
159 1.11 agc static hash_t *
160 1.1 elad find_hash(char *hash_type)
161 1.1 elad {
162 1.11 agc hash_t *hash;
163 1.1 elad
164 1.1 elad for (hash = hashes; hash->hashname != NULL; hash++)
165 1.1 elad if (strcasecmp(hash_type, hash->hashname) == 0)
166 1.1 elad return hash;
167 1.1 elad return NULL;
168 1.1 elad }
169 1.1 elad
170 1.10 agc /* perform the hashing operation on `filename' */
171 1.1 elad static char *
172 1.11 agc do_hash(char *filename, hash_t * h)
173 1.1 elad {
174 1.1 elad return h->filefunc(filename, NULL);
175 1.1 elad }
176 1.1 elad
177 1.10 agc /* return flags for `path' */
178 1.1 elad static int
179 1.1 elad figure_flags(char *path, mode_t mode)
180 1.1 elad {
181 1.1 elad #ifdef notyet
182 1.1 elad if (Fflag) {
183 1.1 elad /* Try to figure out right flag(s). */
184 1.1 elad return VERIEXEC_DIRECT;
185 1.10 agc }
186 1.1 elad #endif /* notyet */
187 1.1 elad
188 1.10 agc return (IS_EXEC(mode)) ? 0 : VERIEXEC_FILE;
189 1.1 elad }
190 1.1 elad
191 1.10 agc /* check to see that we don't have a duplicate entry */
192 1.1 elad static int
193 1.1 elad check_dup(char *filename)
194 1.1 elad {
195 1.1 elad struct fentry *lwalk;
196 1.1 elad
197 1.1 elad TAILQ_FOREACH(lwalk, &fehead, f) {
198 1.1 elad if (strncmp(lwalk->filename, filename,
199 1.1 elad (unsigned long) MAXPATHLEN) == 0)
200 1.1 elad return 1;
201 1.1 elad }
202 1.1 elad
203 1.1 elad return 0;
204 1.1 elad }
205 1.1 elad
206 1.10 agc /* add a new entry to the list for `file' */
207 1.1 elad static void
208 1.11 agc add_new_entry(veriexecgen_t *vp, FTSENT *file, hash_t *hash)
209 1.1 elad {
210 1.1 elad struct fentry *e;
211 1.1 elad struct stat sb;
212 1.1 elad
213 1.1 elad if (file->fts_info == FTS_SL) {
214 1.10 agc /* we have a symbolic link */
215 1.9 agc if (stat(file->fts_path, &sb) == -1) {
216 1.11 agc gripe(vp, "Cannot stat symlink `%s'", file->fts_path);
217 1.9 agc return;
218 1.9 agc }
219 1.1 elad } else
220 1.1 elad sb = *file->fts_statp;
221 1.1 elad
222 1.11 agc if (!vp->all_files && !vp->scan_system_dirs && !IS_EXEC(sb.st_mode))
223 1.1 elad return;
224 1.1 elad
225 1.1 elad e = ecalloc(1UL, sizeof(*e));
226 1.1 elad
227 1.9 agc if (realpath(file->fts_accpath, e->filename) == NULL) {
228 1.11 agc gripe(vp, "Cannot find absolute path `%s'", file->fts_accpath);
229 1.9 agc return;
230 1.9 agc }
231 1.1 elad if (check_dup(e->filename)) {
232 1.1 elad free(e);
233 1.1 elad return;
234 1.1 elad }
235 1.9 agc if ((e->hash_val = do_hash(e->filename, hash)) == NULL) {
236 1.11 agc gripe(vp, "Cannot calculate hash `%s'", e->filename);
237 1.9 agc return;
238 1.9 agc }
239 1.1 elad e->flags = figure_flags(e->filename, sb.st_mode);
240 1.1 elad
241 1.1 elad TAILQ_INSERT_TAIL(&fehead, e, f);
242 1.1 elad }
243 1.1 elad
244 1.10 agc /* walk through a directory */
245 1.1 elad static void
246 1.11 agc walk_dir(veriexecgen_t *vp, char **search_path, hash_t *hash)
247 1.1 elad {
248 1.1 elad FTS *fh;
249 1.1 elad FTSENT *file;
250 1.1 elad
251 1.9 agc if ((fh = fts_open(search_path, FTS_PHYSICAL, NULL)) == NULL) {
252 1.11 agc gripe(vp, "fts_open `%s'", (const char *)search_path);
253 1.9 agc return;
254 1.9 agc }
255 1.1 elad
256 1.1 elad while ((file = fts_read(fh)) != NULL) {
257 1.11 agc if (!vp->recursive_scan && file->fts_level > 1) {
258 1.1 elad fts_set(fh, file, FTS_SKIP);
259 1.1 elad continue;
260 1.1 elad }
261 1.1 elad
262 1.1 elad switch (file->fts_info) {
263 1.1 elad case FTS_D:
264 1.1 elad case FTS_DC:
265 1.1 elad case FTS_DP:
266 1.1 elad continue;
267 1.1 elad default:
268 1.1 elad break;
269 1.1 elad }
270 1.1 elad
271 1.1 elad if (file->fts_errno) {
272 1.11 agc if (vp->exit_on_error) {
273 1.10 agc errx(EXIT_FAILURE, "%s: %s", file->fts_path,
274 1.10 agc strerror(file->fts_errno));
275 1.10 agc }
276 1.10 agc } else {
277 1.11 agc add_new_entry(vp, file, hash);
278 1.1 elad }
279 1.1 elad }
280 1.1 elad
281 1.1 elad fts_close(fh);
282 1.1 elad }
283 1.1 elad
284 1.10 agc /* return a string representation of the flags */
285 1.1 elad static char *
286 1.1 elad flags2str(int flags)
287 1.1 elad {
288 1.10 agc return (flags == 0) ? "" : "FILE, INDIRECT";
289 1.1 elad }
290 1.1 elad
291 1.10 agc /* store the list in the signatures file */
292 1.1 elad static void
293 1.11 agc store_entries(veriexecgen_t *vp, hash_t *hash)
294 1.1 elad {
295 1.1 elad FILE *fp;
296 1.1 elad int move = 1;
297 1.1 elad char old_dbfile[MAXPATHLEN];
298 1.1 elad time_t ct;
299 1.1 elad struct stat sb;
300 1.1 elad struct fentry *e;
301 1.11 agc int prefixc;
302 1.1 elad
303 1.11 agc if (stat(vp->dbfile, &sb) != 0) {
304 1.1 elad if (errno == ENOENT)
305 1.1 elad move = 0;
306 1.1 elad else
307 1.11 agc err(EXIT_FAILURE, "could not stat %s", vp->dbfile);
308 1.1 elad }
309 1.11 agc if (move && !vp->append_output) {
310 1.11 agc if (vp->verbose)
311 1.1 elad (void)printf("\nBacking up existing fingerprint file "
312 1.11 agc "to \"%s.old\"\n\n", vp->dbfile);
313 1.1 elad
314 1.11 agc if (snprintf(old_dbfile, MAXPATHLEN, "%s.old", vp->dbfile) <
315 1.11 agc strlen(vp->dbfile) + 4) {
316 1.10 agc err(EXIT_FAILURE, "%s", old_dbfile);
317 1.1 elad }
318 1.11 agc if (rename(vp->dbfile, old_dbfile) == -1)
319 1.10 agc err(EXIT_FAILURE, "could not rename file");
320 1.1 elad }
321 1.1 elad
322 1.11 agc prefixc = (vp->prefix == NULL) ? -1 : strlen(vp->prefix);
323 1.11 agc
324 1.11 agc fp = efopen(vp->dbfile, vp->append_output ? "a" : "w+");
325 1.1 elad
326 1.1 elad time(&ct);
327 1.1 elad (void)fprintf(fp, "# Generated by %s, %.24s\n",
328 1.1 elad getlogin(), ctime(&ct));
329 1.1 elad
330 1.1 elad TAILQ_FOREACH(e, &fehead, f) {
331 1.11 agc if (vp->verbose)
332 1.1 elad (void)printf("Adding %s.\n", e->filename);
333 1.1 elad
334 1.11 agc
335 1.11 agc (void)fprintf(fp, "%s %s %s %s\n",
336 1.11 agc (prefixc < 0) ? e->filename : &e->filename[prefixc],
337 1.1 elad hash->hashname, e->hash_val, flags2str(e->flags));
338 1.1 elad }
339 1.1 elad
340 1.1 elad (void)fclose(fp);
341 1.1 elad
342 1.11 agc if (vp->verbose) {
343 1.10 agc (void)printf("\n\n"
344 1.10 agc "#############################################################\n"
345 1.10 agc " PLEASE VERIFY CONTENTS OF %s AND FINE-TUNE THE\n"
346 1.10 agc " FLAGS WHERE APPROPRIATE AFTER READING veriexecctl(8)\n"
347 1.10 agc "#############################################################\n",
348 1.11 agc vp->dbfile);
349 1.10 agc }
350 1.1 elad }
351 1.1 elad
352 1.1 elad int
353 1.1 elad main(int argc, char **argv)
354 1.1 elad {
355 1.1 elad int ch, total = 0;
356 1.1 elad char **search_path = NULL;
357 1.11 agc hash_t *hash = NULL;
358 1.11 agc veriexecgen_t v;
359 1.1 elad
360 1.11 agc (void) memset(&v, 0x0, sizeof(v));
361 1.10 agc make_immutable = 0;
362 1.10 agc Fflag = 0;
363 1.1 elad
364 1.9 agc /* error out if we have a dangling symlink or other fs problem */
365 1.11 agc v.exit_on_error = 1;
366 1.9 agc
367 1.11 agc while ((ch = getopt(argc, argv, "AaDd:ho:p:rSt:vW")) != -1) {
368 1.1 elad switch (ch) {
369 1.1 elad case 'A':
370 1.11 agc v.append_output = 1;
371 1.1 elad break;
372 1.1 elad case 'a':
373 1.11 agc v.all_files = 1;
374 1.1 elad break;
375 1.1 elad case 'D':
376 1.11 agc v.scan_system_dirs = 1;
377 1.1 elad break;
378 1.1 elad case 'd':
379 1.1 elad search_path = erealloc(search_path, sizeof(char *) *
380 1.1 elad (total + 1));
381 1.1 elad search_path[total] = optarg;
382 1.1 elad search_path[++total] = NULL;
383 1.1 elad break;
384 1.1 elad #ifdef notyet
385 1.1 elad case 'F':
386 1.1 elad Fflag = 1;
387 1.1 elad break;
388 1.1 elad #endif /* notyet */
389 1.1 elad case 'h':
390 1.1 elad usage();
391 1.10 agc return EXIT_SUCCESS;
392 1.1 elad case 'o':
393 1.11 agc v.dbfile = optarg;
394 1.11 agc break;
395 1.11 agc case 'p':
396 1.11 agc v.prefix = optarg;
397 1.1 elad break;
398 1.1 elad case 'r':
399 1.11 agc v.recursive_scan = 1;
400 1.1 elad break;
401 1.3 elad case 'S':
402 1.10 agc make_immutable = 1;
403 1.3 elad break;
404 1.1 elad case 't':
405 1.10 agc if ((hash = find_hash(optarg)) == NULL) {
406 1.10 agc errx(EXIT_FAILURE,
407 1.10 agc "No such hash algorithm (%s)",
408 1.10 agc optarg);
409 1.10 agc }
410 1.1 elad break;
411 1.1 elad case 'v':
412 1.11 agc v.verbose = 1;
413 1.1 elad break;
414 1.9 agc case 'W':
415 1.11 agc v.exit_on_error = 0;
416 1.9 agc break;
417 1.1 elad default:
418 1.1 elad usage();
419 1.10 agc return EXIT_FAILURE;
420 1.1 elad }
421 1.1 elad }
422 1.1 elad
423 1.11 agc if (v.dbfile == NULL)
424 1.11 agc v.dbfile = DEFAULT_DBFILE;
425 1.1 elad
426 1.1 elad if (hash == NULL) {
427 1.1 elad if ((hash = find_hash(DEFAULT_HASH)) == NULL)
428 1.10 agc errx(EXIT_FAILURE, "No hash algorithm");
429 1.1 elad }
430 1.1 elad
431 1.1 elad TAILQ_INIT(&fehead);
432 1.1 elad
433 1.1 elad if (search_path == NULL)
434 1.11 agc v.scan_system_dirs = 1;
435 1.1 elad
436 1.11 agc if (v.scan_system_dirs) {
437 1.1 elad char *sys_paths[] = DEFAULT_SYSPATHS;
438 1.1 elad
439 1.11 agc if (v.verbose)
440 1.11 agc banner(&v, hash, sys_paths);
441 1.11 agc walk_dir(&v, sys_paths, hash);
442 1.1 elad }
443 1.1 elad
444 1.1 elad if (search_path != NULL) {
445 1.11 agc if (v.verbose)
446 1.11 agc banner(&v, hash, search_path);
447 1.11 agc walk_dir(&v, search_path, hash);
448 1.1 elad }
449 1.1 elad
450 1.11 agc store_entries(&v, hash);
451 1.1 elad
452 1.11 agc if (make_immutable && chflags(v.dbfile, SF_IMMUTABLE) != 0)
453 1.10 agc err(EXIT_FAILURE, "Can't set immutable flag");
454 1.3 elad
455 1.10 agc return EXIT_SUCCESS;
456 1.1 elad }
457