veriexecgen.c revision 1.3 1 1.3 elad /* $NetBSD: veriexecgen.c,v 1.3 2006/09/18 17:47:25 elad 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.1 elad #include <crypto/sha2.h>
59 1.1 elad #include <crypto/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.1 elad "/usr/lib", NULL }
67 1.1 elad
68 1.1 elad struct fentry {
69 1.1 elad char filename[MAXPATHLEN];
70 1.1 elad char *hash_val;
71 1.1 elad int flags;
72 1.1 elad TAILQ_ENTRY(fentry) f;
73 1.1 elad };
74 1.1 elad TAILQ_HEAD(, fentry) fehead;
75 1.1 elad
76 1.1 elad struct hash {
77 1.1 elad const char *hashname;
78 1.1 elad char *(*filefunc) (const char *, char *);
79 1.1 elad } hashes[] = {
80 1.1 elad { "MD5", MD5File },
81 1.1 elad { "SHA1", SHA1File },
82 1.1 elad { "SHA256", SHA256_File },
83 1.1 elad { "SHA384", SHA384_File },
84 1.1 elad { "SHA512", SHA512_File },
85 1.1 elad { "RMD160", RMD160File },
86 1.1 elad { NULL, NULL },
87 1.1 elad };
88 1.1 elad
89 1.3 elad int Aflag, aflag, Dflag, Fflag, rflag, Sflag, vflag;
90 1.1 elad
91 1.1 elad static void
92 1.1 elad usage(void)
93 1.1 elad {
94 1.1 elad (void)fprintf(stderr,
95 1.3 elad "usage: %s [-AaDrSv] [-d dir] [-o fingerprintdb]"
96 1.1 elad " [-t algorithm]\n", getprogname());
97 1.1 elad }
98 1.1 elad
99 1.1 elad static void
100 1.1 elad banner(struct hash *hash_type, char **search_path)
101 1.1 elad {
102 1.1 elad int j;
103 1.1 elad
104 1.1 elad (void)printf("Fingerprinting ");
105 1.1 elad
106 1.1 elad for (j = 0; search_path[j] != NULL; j++)
107 1.1 elad (void)printf("%s ", search_path[j]);
108 1.1 elad
109 1.1 elad (void)printf("(%s) (%s) using %s\n",
110 1.1 elad aflag ? "all files" : "executables only",
111 1.1 elad rflag ? "recursive" : "non-recursive",
112 1.1 elad hash_type->hashname);
113 1.1 elad }
114 1.1 elad
115 1.1 elad static struct hash *
116 1.1 elad find_hash(char *hash_type)
117 1.1 elad {
118 1.1 elad struct hash *hash;
119 1.1 elad
120 1.1 elad for (hash = hashes; hash->hashname != NULL; hash++)
121 1.1 elad if (strcasecmp(hash_type, hash->hashname) == 0)
122 1.1 elad return hash;
123 1.1 elad return NULL;
124 1.1 elad }
125 1.1 elad
126 1.1 elad static char *
127 1.1 elad do_hash(char *filename, struct hash * h)
128 1.1 elad {
129 1.1 elad return h->filefunc(filename, NULL);
130 1.1 elad }
131 1.1 elad
132 1.1 elad static int
133 1.1 elad figure_flags(char *path, mode_t mode)
134 1.1 elad {
135 1.1 elad #ifdef notyet
136 1.1 elad if (Fflag) {
137 1.1 elad /* Try to figure out right flag(s). */
138 1.1 elad return VERIEXEC_DIRECT;
139 1.1 elad }
140 1.1 elad #endif /* notyet */
141 1.1 elad
142 1.1 elad if (!IS_EXEC(mode))
143 1.1 elad return VERIEXEC_FILE;
144 1.1 elad else
145 1.1 elad return 0;
146 1.1 elad }
147 1.1 elad
148 1.1 elad static int
149 1.1 elad check_dup(char *filename)
150 1.1 elad {
151 1.1 elad struct fentry *lwalk;
152 1.1 elad
153 1.1 elad TAILQ_FOREACH(lwalk, &fehead, f) {
154 1.1 elad if (strncmp(lwalk->filename, filename,
155 1.1 elad (unsigned long) MAXPATHLEN) == 0)
156 1.1 elad return 1;
157 1.1 elad }
158 1.1 elad
159 1.1 elad return 0;
160 1.1 elad }
161 1.1 elad
162 1.1 elad static void
163 1.1 elad add_new_entry(FTSENT *file, struct hash *hash)
164 1.1 elad {
165 1.1 elad struct fentry *e;
166 1.1 elad struct stat sb;
167 1.1 elad
168 1.1 elad if (file->fts_info == FTS_SL) {
169 1.1 elad if (stat(file->fts_path, &sb) == -1)
170 1.1 elad err(1, "Cannot stat symlink");
171 1.1 elad } else
172 1.1 elad sb = *file->fts_statp;
173 1.1 elad
174 1.1 elad if (!aflag && !Dflag && IS_EXEC(sb.st_mode))
175 1.1 elad return;
176 1.1 elad
177 1.1 elad e = ecalloc(1UL, sizeof(*e));
178 1.1 elad
179 1.1 elad if (realpath(file->fts_accpath, e->filename) == NULL)
180 1.1 elad err(1, "Cannot find absolute path");
181 1.1 elad if (check_dup(e->filename)) {
182 1.1 elad free(e);
183 1.1 elad return;
184 1.1 elad }
185 1.1 elad if ((e->hash_val = do_hash(e->filename, hash)) == NULL)
186 1.1 elad errx(1, "Cannot calculate hash");
187 1.1 elad e->flags = figure_flags(e->filename, sb.st_mode);
188 1.1 elad
189 1.1 elad TAILQ_INSERT_TAIL(&fehead, e, f);
190 1.1 elad }
191 1.1 elad
192 1.1 elad static void
193 1.1 elad walk_dir(char **search_path, struct hash *hash)
194 1.1 elad {
195 1.1 elad FTS *fh;
196 1.1 elad FTSENT *file;
197 1.1 elad
198 1.1 elad if ((fh = fts_open(search_path, FTS_PHYSICAL, NULL)) == NULL)
199 1.1 elad err(1, "fts_open");
200 1.1 elad
201 1.1 elad while ((file = fts_read(fh)) != NULL) {
202 1.1 elad if (!rflag && file->fts_level > 1) {
203 1.1 elad fts_set(fh, file, FTS_SKIP);
204 1.1 elad continue;
205 1.1 elad }
206 1.1 elad
207 1.1 elad switch (file->fts_info) {
208 1.1 elad case FTS_D:
209 1.1 elad case FTS_DC:
210 1.1 elad case FTS_DP:
211 1.1 elad continue;
212 1.1 elad default:
213 1.1 elad break;
214 1.1 elad }
215 1.1 elad
216 1.1 elad if (file->fts_errno) {
217 1.1 elad errx(1, "%s: %s", file->fts_path,
218 1.1 elad strerror(file->fts_errno));
219 1.1 elad }
220 1.1 elad
221 1.1 elad add_new_entry(file, hash);
222 1.1 elad }
223 1.1 elad
224 1.1 elad fts_close(fh);
225 1.1 elad }
226 1.1 elad
227 1.1 elad static char *
228 1.1 elad flags2str(int flags)
229 1.1 elad {
230 1.1 elad if (flags != 0)
231 1.1 elad return "FILE";
232 1.1 elad else
233 1.1 elad return "";
234 1.1 elad }
235 1.1 elad
236 1.1 elad static void
237 1.1 elad store_entries(char *dbfile, struct hash *hash)
238 1.1 elad {
239 1.1 elad FILE *fp;
240 1.1 elad int move = 1;
241 1.1 elad char old_dbfile[MAXPATHLEN];
242 1.1 elad time_t ct;
243 1.1 elad struct stat sb;
244 1.1 elad struct fentry *e;
245 1.1 elad
246 1.1 elad if (stat(dbfile, &sb) != 0) {
247 1.1 elad if (errno == ENOENT)
248 1.1 elad move = 0;
249 1.1 elad else
250 1.1 elad err(1, "could not stat %s", dbfile);
251 1.1 elad }
252 1.1 elad if (move && !Aflag) {
253 1.1 elad if (vflag)
254 1.1 elad (void)printf("\nBacking up existing fingerprint file "
255 1.1 elad "to \"%s.old\"\n\n", dbfile);
256 1.1 elad
257 1.1 elad if (snprintf(old_dbfile, MAXPATHLEN, "%s.old", dbfile) <
258 1.1 elad strlen(dbfile) + 4) {
259 1.1 elad err(1, "%s", old_dbfile);
260 1.1 elad }
261 1.1 elad if (rename(dbfile, old_dbfile) == -1)
262 1.1 elad err(1, "could not rename file");
263 1.1 elad }
264 1.1 elad
265 1.1 elad fp = efopen(dbfile, Aflag ? "a" : "w+");
266 1.1 elad
267 1.1 elad time(&ct);
268 1.1 elad (void)fprintf(fp, "# Generated by %s, %.24s\n",
269 1.1 elad getlogin(), ctime(&ct));
270 1.1 elad
271 1.1 elad TAILQ_FOREACH(e, &fehead, f) {
272 1.1 elad if (vflag)
273 1.1 elad (void)printf("Adding %s.\n", e->filename);
274 1.1 elad
275 1.1 elad (void)fprintf(fp, "%s %s %s %s\n", e->filename,
276 1.1 elad hash->hashname, e->hash_val, flags2str(e->flags));
277 1.1 elad }
278 1.1 elad
279 1.1 elad (void)fclose(fp);
280 1.1 elad
281 1.1 elad if (!vflag)
282 1.1 elad return;
283 1.1 elad
284 1.1 elad (void)printf("\n\n"
285 1.1 elad "#############################################################\n"
286 1.1 elad " PLEASE VERIFY CONTENTS OF %s AND FINE-TUNE THE\n"
287 1.1 elad " FLAGS WHERE APPROPRIATE AFTER READING veriexecctl(8)\n"
288 1.1 elad "#############################################################\n",
289 1.1 elad dbfile);
290 1.1 elad }
291 1.1 elad
292 1.1 elad int
293 1.1 elad main(int argc, char **argv)
294 1.1 elad {
295 1.1 elad int ch, total = 0;
296 1.1 elad char *dbfile = NULL;
297 1.1 elad char **search_path = NULL;
298 1.1 elad struct hash *hash = NULL;
299 1.1 elad
300 1.3 elad Aflag = aflag = Dflag = Fflag = rflag = Sflag = vflag = 0;
301 1.1 elad
302 1.3 elad while ((ch = getopt(argc, argv, "AaDd:ho:rSt:v")) != -1) {
303 1.1 elad switch (ch) {
304 1.1 elad case 'A':
305 1.1 elad Aflag = 1;
306 1.1 elad break;
307 1.1 elad case 'a':
308 1.1 elad aflag = 1;
309 1.1 elad break;
310 1.1 elad case 'D':
311 1.1 elad Dflag = 1;
312 1.1 elad break;
313 1.1 elad case 'd':
314 1.1 elad search_path = erealloc(search_path, sizeof(char *) *
315 1.1 elad (total + 1));
316 1.1 elad search_path[total] = optarg;
317 1.1 elad search_path[++total] = NULL;
318 1.1 elad break;
319 1.1 elad #ifdef notyet
320 1.1 elad case 'F':
321 1.1 elad Fflag = 1;
322 1.1 elad break;
323 1.1 elad #endif /* notyet */
324 1.1 elad case 'h':
325 1.1 elad usage();
326 1.1 elad return 0;
327 1.1 elad case 'o':
328 1.1 elad dbfile = optarg;
329 1.1 elad break;
330 1.1 elad case 'r':
331 1.1 elad rflag = 1;
332 1.1 elad break;
333 1.3 elad case 'S':
334 1.3 elad Sflag = 1;
335 1.3 elad break;
336 1.1 elad case 't':
337 1.1 elad hash = find_hash(optarg);
338 1.1 elad break;
339 1.1 elad case 'v':
340 1.1 elad vflag = 1;
341 1.1 elad break;
342 1.1 elad default:
343 1.1 elad usage();
344 1.1 elad return 1;
345 1.1 elad }
346 1.1 elad }
347 1.1 elad
348 1.1 elad if (dbfile == NULL)
349 1.1 elad dbfile = DEFAULT_DBFILE;
350 1.1 elad
351 1.1 elad if (hash == NULL) {
352 1.1 elad if ((hash = find_hash(DEFAULT_HASH)) == NULL)
353 1.1 elad errx(1, "No hash algorithm");
354 1.1 elad }
355 1.1 elad
356 1.1 elad TAILQ_INIT(&fehead);
357 1.1 elad
358 1.1 elad if (search_path == NULL)
359 1.1 elad Dflag = 1;
360 1.1 elad
361 1.1 elad if (Dflag) {
362 1.1 elad char *sys_paths[] = DEFAULT_SYSPATHS;
363 1.1 elad
364 1.1 elad if (vflag)
365 1.1 elad banner(hash, sys_paths);
366 1.1 elad walk_dir(sys_paths, hash);
367 1.1 elad }
368 1.1 elad
369 1.1 elad if (search_path != NULL) {
370 1.1 elad if (vflag)
371 1.1 elad banner(hash, search_path);
372 1.1 elad walk_dir(search_path, hash);
373 1.1 elad }
374 1.1 elad
375 1.1 elad store_entries(dbfile, hash);
376 1.1 elad
377 1.3 elad if (chflags(dbfile, SF_IMMUTABLE) != 0)
378 1.3 elad err(1, "Can't set immutable flag");
379 1.3 elad
380 1.1 elad return 0;
381 1.1 elad }
382