veriexecgen.c revision 1.9 1 1.9 agc /* $NetBSD: veriexecgen.c,v 1.9 2006/12/04 07:06:56 agc 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.1 elad * 3. All advertising materials mentioning features or use of this software
19 1.1 elad * must display the following acknowledgement:
20 1.1 elad * This product includes software developed by the NetBSD
21 1.1 elad * Foundation, Inc. and its contributors.
22 1.1 elad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 elad * contributors may be used to endorse or promote products derived
24 1.1 elad * from this software without specific prior written permission.
25 1.1 elad *
26 1.1 elad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 elad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 elad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 elad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 elad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 elad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 elad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 elad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 elad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 elad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 elad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 elad */
38 1.1 elad
39 1.1 elad #include <sys/param.h>
40 1.1 elad #include <sys/types.h>
41 1.1 elad #include <sys/queue.h>
42 1.1 elad #include <sys/stat.h>
43 1.1 elad #include <sys/dirent.h>
44 1.1 elad #include <sys/verified_exec.h>
45 1.1 elad
46 1.1 elad #include <err.h>
47 1.1 elad #include <errno.h>
48 1.1 elad #include <fts.h>
49 1.1 elad #include <stdio.h>
50 1.1 elad #include <stdlib.h>
51 1.1 elad #include <string.h>
52 1.1 elad #include <time.h>
53 1.1 elad #include <unistd.h>
54 1.1 elad #include <util.h>
55 1.1 elad
56 1.1 elad #include <md5.h>
57 1.2 elad #include <sha1.h>
58 1.8 christos #include <sha2.h>
59 1.8 christos #include <rmd160.h>
60 1.1 elad
61 1.1 elad #define IS_EXEC(mode) ((mode) & (S_IXUSR | S_IXGRP | S_IXOTH))
62 1.1 elad
63 1.1 elad #define DEFAULT_DBFILE "/etc/signatures"
64 1.1 elad #define DEFAULT_HASH "sha256"
65 1.1 elad #define DEFAULT_SYSPATHS { "/bin", "/sbin", "/usr/bin", "/usr/sbin", \
66 1.6 elad "/lib", "/usr/lib", "/libexec", "/usr/libexec", \
67 1.6 elad NULL }
68 1.1 elad
69 1.1 elad struct fentry {
70 1.1 elad char filename[MAXPATHLEN];
71 1.1 elad char *hash_val;
72 1.1 elad int flags;
73 1.1 elad TAILQ_ENTRY(fentry) f;
74 1.1 elad };
75 1.1 elad TAILQ_HEAD(, fentry) fehead;
76 1.1 elad
77 1.1 elad struct hash {
78 1.1 elad const char *hashname;
79 1.1 elad char *(*filefunc) (const char *, char *);
80 1.1 elad } hashes[] = {
81 1.1 elad { "MD5", MD5File },
82 1.1 elad { "SHA1", SHA1File },
83 1.1 elad { "SHA256", SHA256_File },
84 1.1 elad { "SHA384", SHA384_File },
85 1.1 elad { "SHA512", SHA512_File },
86 1.1 elad { "RMD160", RMD160File },
87 1.1 elad { NULL, NULL },
88 1.1 elad };
89 1.1 elad
90 1.9 agc static int Aflag, aflag, Dflag, Fflag, rflag, Sflag, vflag;
91 1.9 agc
92 1.9 agc static int exit_on_error;
93 1.9 agc
94 1.9 agc /* warn about a problem - exit if exit_on_error is set */
95 1.9 agc static void
96 1.9 agc gripe(const char *fmt, const char *filename)
97 1.9 agc {
98 1.9 agc warn(fmt, filename);
99 1.9 agc if (exit_on_error) {
100 1.9 agc /* error out on problematic files */
101 1.9 agc exit(EXIT_FAILURE);
102 1.9 agc }
103 1.9 agc }
104 1.1 elad
105 1.1 elad static void
106 1.1 elad usage(void)
107 1.1 elad {
108 1.1 elad (void)fprintf(stderr,
109 1.3 elad "usage: %s [-AaDrSv] [-d dir] [-o fingerprintdb]"
110 1.1 elad " [-t algorithm]\n", getprogname());
111 1.1 elad }
112 1.1 elad
113 1.1 elad static void
114 1.1 elad banner(struct hash *hash_type, char **search_path)
115 1.1 elad {
116 1.1 elad int j;
117 1.1 elad
118 1.1 elad (void)printf("Fingerprinting ");
119 1.1 elad
120 1.1 elad for (j = 0; search_path[j] != NULL; j++)
121 1.1 elad (void)printf("%s ", search_path[j]);
122 1.1 elad
123 1.1 elad (void)printf("(%s) (%s) using %s\n",
124 1.1 elad aflag ? "all files" : "executables only",
125 1.1 elad rflag ? "recursive" : "non-recursive",
126 1.1 elad hash_type->hashname);
127 1.1 elad }
128 1.1 elad
129 1.1 elad static struct hash *
130 1.1 elad find_hash(char *hash_type)
131 1.1 elad {
132 1.1 elad struct hash *hash;
133 1.1 elad
134 1.1 elad for (hash = hashes; hash->hashname != NULL; hash++)
135 1.1 elad if (strcasecmp(hash_type, hash->hashname) == 0)
136 1.1 elad return hash;
137 1.1 elad return NULL;
138 1.1 elad }
139 1.1 elad
140 1.1 elad static char *
141 1.1 elad do_hash(char *filename, struct hash * h)
142 1.1 elad {
143 1.1 elad return h->filefunc(filename, NULL);
144 1.1 elad }
145 1.1 elad
146 1.1 elad static int
147 1.1 elad figure_flags(char *path, mode_t mode)
148 1.1 elad {
149 1.1 elad #ifdef notyet
150 1.1 elad if (Fflag) {
151 1.1 elad /* Try to figure out right flag(s). */
152 1.1 elad return VERIEXEC_DIRECT;
153 1.1 elad }
154 1.1 elad #endif /* notyet */
155 1.1 elad
156 1.1 elad if (!IS_EXEC(mode))
157 1.1 elad return VERIEXEC_FILE;
158 1.1 elad else
159 1.1 elad return 0;
160 1.1 elad }
161 1.1 elad
162 1.1 elad static int
163 1.1 elad check_dup(char *filename)
164 1.1 elad {
165 1.1 elad struct fentry *lwalk;
166 1.1 elad
167 1.1 elad TAILQ_FOREACH(lwalk, &fehead, f) {
168 1.1 elad if (strncmp(lwalk->filename, filename,
169 1.1 elad (unsigned long) MAXPATHLEN) == 0)
170 1.1 elad return 1;
171 1.1 elad }
172 1.1 elad
173 1.1 elad return 0;
174 1.1 elad }
175 1.1 elad
176 1.1 elad static void
177 1.1 elad add_new_entry(FTSENT *file, struct hash *hash)
178 1.1 elad {
179 1.1 elad struct fentry *e;
180 1.1 elad struct stat sb;
181 1.1 elad
182 1.1 elad if (file->fts_info == FTS_SL) {
183 1.9 agc if (stat(file->fts_path, &sb) == -1) {
184 1.9 agc gripe("Cannot stat symlink `%s'", file->fts_path);
185 1.9 agc return;
186 1.9 agc }
187 1.1 elad } else
188 1.1 elad sb = *file->fts_statp;
189 1.1 elad
190 1.5 elad if (!aflag && !Dflag && !IS_EXEC(sb.st_mode))
191 1.1 elad return;
192 1.1 elad
193 1.1 elad e = ecalloc(1UL, sizeof(*e));
194 1.1 elad
195 1.9 agc if (realpath(file->fts_accpath, e->filename) == NULL) {
196 1.9 agc gripe("Cannot find absolute path `%s'", file->fts_accpath);
197 1.9 agc return;
198 1.9 agc }
199 1.1 elad if (check_dup(e->filename)) {
200 1.1 elad free(e);
201 1.1 elad return;
202 1.1 elad }
203 1.9 agc if ((e->hash_val = do_hash(e->filename, hash)) == NULL) {
204 1.9 agc gripe("Cannot calculate hash `%s'", e->filename);
205 1.9 agc return;
206 1.9 agc }
207 1.1 elad e->flags = figure_flags(e->filename, sb.st_mode);
208 1.1 elad
209 1.1 elad TAILQ_INSERT_TAIL(&fehead, e, f);
210 1.1 elad }
211 1.1 elad
212 1.1 elad static void
213 1.1 elad walk_dir(char **search_path, struct hash *hash)
214 1.1 elad {
215 1.1 elad FTS *fh;
216 1.1 elad FTSENT *file;
217 1.1 elad
218 1.9 agc if ((fh = fts_open(search_path, FTS_PHYSICAL, NULL)) == NULL) {
219 1.9 agc gripe("fts_open `%s'", (const char *)search_path);
220 1.9 agc return;
221 1.9 agc }
222 1.1 elad
223 1.1 elad while ((file = fts_read(fh)) != NULL) {
224 1.1 elad if (!rflag && file->fts_level > 1) {
225 1.1 elad fts_set(fh, file, FTS_SKIP);
226 1.1 elad continue;
227 1.1 elad }
228 1.1 elad
229 1.1 elad switch (file->fts_info) {
230 1.1 elad case FTS_D:
231 1.1 elad case FTS_DC:
232 1.1 elad case FTS_DP:
233 1.1 elad continue;
234 1.1 elad default:
235 1.1 elad break;
236 1.1 elad }
237 1.1 elad
238 1.1 elad if (file->fts_errno) {
239 1.1 elad errx(1, "%s: %s", file->fts_path,
240 1.1 elad strerror(file->fts_errno));
241 1.1 elad }
242 1.1 elad
243 1.1 elad add_new_entry(file, hash);
244 1.1 elad }
245 1.1 elad
246 1.1 elad fts_close(fh);
247 1.1 elad }
248 1.1 elad
249 1.1 elad static char *
250 1.1 elad flags2str(int flags)
251 1.1 elad {
252 1.1 elad if (flags != 0)
253 1.7 elad return "FILE, INDIRECT";
254 1.1 elad else
255 1.1 elad return "";
256 1.1 elad }
257 1.1 elad
258 1.1 elad static void
259 1.1 elad store_entries(char *dbfile, struct hash *hash)
260 1.1 elad {
261 1.1 elad FILE *fp;
262 1.1 elad int move = 1;
263 1.1 elad char old_dbfile[MAXPATHLEN];
264 1.1 elad time_t ct;
265 1.1 elad struct stat sb;
266 1.1 elad struct fentry *e;
267 1.1 elad
268 1.1 elad if (stat(dbfile, &sb) != 0) {
269 1.1 elad if (errno == ENOENT)
270 1.1 elad move = 0;
271 1.1 elad else
272 1.1 elad err(1, "could not stat %s", dbfile);
273 1.1 elad }
274 1.1 elad if (move && !Aflag) {
275 1.1 elad if (vflag)
276 1.1 elad (void)printf("\nBacking up existing fingerprint file "
277 1.1 elad "to \"%s.old\"\n\n", dbfile);
278 1.1 elad
279 1.1 elad if (snprintf(old_dbfile, MAXPATHLEN, "%s.old", dbfile) <
280 1.1 elad strlen(dbfile) + 4) {
281 1.1 elad err(1, "%s", old_dbfile);
282 1.1 elad }
283 1.1 elad if (rename(dbfile, old_dbfile) == -1)
284 1.1 elad err(1, "could not rename file");
285 1.1 elad }
286 1.1 elad
287 1.1 elad fp = efopen(dbfile, Aflag ? "a" : "w+");
288 1.1 elad
289 1.1 elad time(&ct);
290 1.1 elad (void)fprintf(fp, "# Generated by %s, %.24s\n",
291 1.1 elad getlogin(), ctime(&ct));
292 1.1 elad
293 1.1 elad TAILQ_FOREACH(e, &fehead, f) {
294 1.1 elad if (vflag)
295 1.1 elad (void)printf("Adding %s.\n", e->filename);
296 1.1 elad
297 1.1 elad (void)fprintf(fp, "%s %s %s %s\n", e->filename,
298 1.1 elad hash->hashname, e->hash_val, flags2str(e->flags));
299 1.1 elad }
300 1.1 elad
301 1.1 elad (void)fclose(fp);
302 1.1 elad
303 1.1 elad if (!vflag)
304 1.1 elad return;
305 1.1 elad
306 1.1 elad (void)printf("\n\n"
307 1.1 elad "#############################################################\n"
308 1.1 elad " PLEASE VERIFY CONTENTS OF %s AND FINE-TUNE THE\n"
309 1.1 elad " FLAGS WHERE APPROPRIATE AFTER READING veriexecctl(8)\n"
310 1.1 elad "#############################################################\n",
311 1.1 elad dbfile);
312 1.1 elad }
313 1.1 elad
314 1.1 elad int
315 1.1 elad main(int argc, char **argv)
316 1.1 elad {
317 1.1 elad int ch, total = 0;
318 1.1 elad char *dbfile = NULL;
319 1.1 elad char **search_path = NULL;
320 1.1 elad struct hash *hash = NULL;
321 1.1 elad
322 1.3 elad Aflag = aflag = Dflag = Fflag = rflag = Sflag = vflag = 0;
323 1.1 elad
324 1.9 agc /* error out if we have a dangling symlink or other fs problem */
325 1.9 agc exit_on_error = 1;
326 1.9 agc
327 1.9 agc while ((ch = getopt(argc, argv, "AaDd:ho:rSt:vW")) != -1) {
328 1.1 elad switch (ch) {
329 1.1 elad case 'A':
330 1.1 elad Aflag = 1;
331 1.1 elad break;
332 1.1 elad case 'a':
333 1.1 elad aflag = 1;
334 1.1 elad break;
335 1.1 elad case 'D':
336 1.1 elad Dflag = 1;
337 1.1 elad break;
338 1.1 elad case 'd':
339 1.1 elad search_path = erealloc(search_path, sizeof(char *) *
340 1.1 elad (total + 1));
341 1.1 elad search_path[total] = optarg;
342 1.1 elad search_path[++total] = NULL;
343 1.1 elad break;
344 1.1 elad #ifdef notyet
345 1.1 elad case 'F':
346 1.1 elad Fflag = 1;
347 1.1 elad break;
348 1.1 elad #endif /* notyet */
349 1.1 elad case 'h':
350 1.1 elad usage();
351 1.1 elad return 0;
352 1.1 elad case 'o':
353 1.1 elad dbfile = optarg;
354 1.1 elad break;
355 1.1 elad case 'r':
356 1.1 elad rflag = 1;
357 1.1 elad break;
358 1.3 elad case 'S':
359 1.3 elad Sflag = 1;
360 1.3 elad break;
361 1.1 elad case 't':
362 1.1 elad hash = find_hash(optarg);
363 1.1 elad break;
364 1.1 elad case 'v':
365 1.1 elad vflag = 1;
366 1.1 elad break;
367 1.9 agc case 'W':
368 1.9 agc exit_on_error = 0;
369 1.9 agc break;
370 1.1 elad default:
371 1.1 elad usage();
372 1.1 elad return 1;
373 1.1 elad }
374 1.1 elad }
375 1.1 elad
376 1.1 elad if (dbfile == NULL)
377 1.1 elad dbfile = DEFAULT_DBFILE;
378 1.1 elad
379 1.1 elad if (hash == NULL) {
380 1.1 elad if ((hash = find_hash(DEFAULT_HASH)) == NULL)
381 1.1 elad errx(1, "No hash algorithm");
382 1.1 elad }
383 1.1 elad
384 1.1 elad TAILQ_INIT(&fehead);
385 1.1 elad
386 1.1 elad if (search_path == NULL)
387 1.1 elad Dflag = 1;
388 1.1 elad
389 1.1 elad if (Dflag) {
390 1.1 elad char *sys_paths[] = DEFAULT_SYSPATHS;
391 1.1 elad
392 1.1 elad if (vflag)
393 1.1 elad banner(hash, sys_paths);
394 1.1 elad walk_dir(sys_paths, hash);
395 1.1 elad }
396 1.1 elad
397 1.1 elad if (search_path != NULL) {
398 1.1 elad if (vflag)
399 1.1 elad banner(hash, search_path);
400 1.1 elad walk_dir(search_path, hash);
401 1.1 elad }
402 1.1 elad
403 1.1 elad store_entries(dbfile, hash);
404 1.1 elad
405 1.4 elad if (Sflag && chflags(dbfile, SF_IMMUTABLE) != 0)
406 1.3 elad err(1, "Can't set immutable flag");
407 1.3 elad
408 1.1 elad return 0;
409 1.1 elad }
410