moved measurement and metadata flags to product_file table
[strongswan.git] / src / libimcv / plugins / imv_attestation / attest.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <getopt.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <syslog.h>
23
24 #include <library.h>
25 #include <debug.h>
26
27 #include <imcv.h>
28 #include <libpts.h>
29 #include <pts/pts_meas_algo.h>
30
31 #include "attest_db.h"
32 #include "attest_usage.h"
33
34 /**
35 * global debug output variables
36 */
37 static int debug_level = 0;
38 static bool stderr_quiet = TRUE;
39
40 /**
41 * attest dbg function
42 */
43 static void attest_dbg(debug_t group, level_t level, char *fmt, ...)
44 {
45 int priority = LOG_INFO;
46 char buffer[8192];
47 char *current = buffer, *next;
48 va_list args;
49
50 if (level <= debug_level)
51 {
52 if (!stderr_quiet)
53 {
54 va_start(args, fmt);
55 vfprintf(stderr, fmt, args);
56 fprintf(stderr, "\n");
57 va_end(args);
58 }
59
60 /* write in memory buffer first */
61 va_start(args, fmt);
62 vsnprintf(buffer, sizeof(buffer), fmt, args);
63 va_end(args);
64
65 /* do a syslog with every line */
66 while (current)
67 {
68 next = strchr(current, '\n');
69 if (next)
70 {
71 *(next++) = '\0';
72 }
73 syslog(priority, "%s\n", current);
74 current = next;
75 }
76 }
77 }
78
79 /**
80 * global attestation database object
81 */
82 attest_db_t *attest;
83
84 /**
85 * atexit handler to close db on shutdown
86 */
87 static void cleanup(void)
88 {
89 attest->destroy(attest);
90 libpts_deinit();
91 libimcv_deinit();
92 closelog();
93 }
94
95 static void do_args(int argc, char *argv[])
96 {
97 enum {
98 OP_UNDEF,
99 OP_USAGE,
100 OP_FILES,
101 OP_COMPONENTS,
102 OP_PRODUCTS,
103 OP_HASHES,
104 OP_ADD,
105 OP_DEL,
106 } op = OP_UNDEF;
107
108 /* reinit getopt state */
109 optind = 0;
110
111 while (TRUE)
112 {
113 int c;
114
115 struct option long_opts[] = {
116 { "help", no_argument, NULL, 'h' },
117 { "components", no_argument, NULL, 'c' },
118 { "files", no_argument, NULL, 'f' },
119 { "products", no_argument, NULL, 'p' },
120 { "hashes", no_argument, NULL, 'H' },
121 { "add", no_argument, NULL, 'a' },
122 { "delete", no_argument, NULL, 'd' },
123 { "del", no_argument, NULL, 'd' },
124 { "products", no_argument, NULL, 'p' },
125 { "hashes", no_argument, NULL, 'H' },
126 { "add", no_argument, NULL, 'a' },
127 { "delete", no_argument, NULL, 'd' },
128 { "del", no_argument, NULL, 'd' },
129 { "directory", required_argument, NULL, 'D' },
130 { "dir", required_argument, NULL, 'D' },
131 { "file", required_argument, NULL, 'F' },
132 { "product", required_argument, NULL, 'P' },
133 { "sha1", no_argument, NULL, '1' },
134 { "sha256", no_argument, NULL, '2' },
135 { "sha384", no_argument, NULL, '3' },
136 { "did", required_argument, NULL, '4' },
137 { "fid", required_argument, NULL, '5' },
138 { "pid", required_argument, NULL, '6' },
139 { 0,0,0,0 }
140 };
141
142 c = getopt_long(argc, argv, "", long_opts, NULL);
143 switch (c)
144 {
145 case EOF:
146 break;
147 case 'h':
148 op = OP_USAGE;
149 break;
150 case 'c':
151 op = OP_COMPONENTS;
152 continue;
153 case 'f':
154 op = OP_FILES;
155 continue;
156 case 'p':
157 op = OP_PRODUCTS;
158 continue;
159 case 'H':
160 op = OP_HASHES;
161 continue;
162 case 'a':
163 op = OP_ADD;
164 continue;
165 case 'd':
166 op = OP_DEL;
167 continue;
168 case 'D':
169 if (!attest->set_directory(attest, optarg, op == OP_ADD))
170 {
171 exit(EXIT_FAILURE);
172 }
173 continue;
174 case 'H':
175 op = OP_HASHES;
176 continue;
177 case 'a':
178 op = OP_ADD;
179 continue;
180 case 'd':
181 op = OP_DEL;
182 continue;
183 case 'D':
184 if (!attest->set_directory(attest, optarg, op == OP_ADD))
185 {
186 exit(EXIT_FAILURE);
187 }
188 continue;
189 case 'F':
190 if (!attest->set_file(attest, optarg, op == OP_ADD))
191 {
192 exit(EXIT_FAILURE);
193 }
194 continue;
195 case 'P':
196 if (!attest->set_product(attest, optarg, op == OP_ADD))
197 {
198 exit(EXIT_FAILURE);
199 }
200 continue;
201 case '1':
202 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1);
203 continue;
204 case '2':
205 attest->set_algo(attest, PTS_MEAS_ALGO_SHA256);
206 continue;
207 case '3':
208 attest->set_algo(attest, PTS_MEAS_ALGO_SHA384);
209 continue;
210 case '4':
211 if (!attest->set_did(attest, atoi(optarg)))
212 {
213 exit(EXIT_FAILURE);
214 }
215 continue;
216 case '5':
217 if (!attest->set_fid(attest, atoi(optarg)))
218 {
219 exit(EXIT_FAILURE);
220 }
221 continue;
222 case '6':
223 if (!attest->set_pid(attest, atoi(optarg)))
224 {
225 exit(EXIT_FAILURE);
226 }
227 continue;
228 }
229 break;
230 }
231
232 switch (op)
233 {
234 case OP_USAGE:
235 usage();
236 break;
237 case OP_PRODUCTS:
238 attest->list_products(attest);
239 break;
240 case OP_COMPONENTS:
241 attest->list_components(attest);
242 break;
243 case OP_FILES:
244 attest->list_files(attest);
245 break;
246 case OP_HASHES:
247 attest->list_hashes(attest);
248 break;
249 case OP_ADD:
250 attest->add(attest);
251 break;
252 case OP_DEL:
253 attest->delete(attest);
254 break;
255 case OP_HASHES:
256 attest->list_hashes(attest);
257 break;
258 default:
259 usage();
260 exit(EXIT_FAILURE);
261 }
262 }
263
264 int main(int argc, char *argv[])
265 {
266 char *uri;
267
268 /* enable attest debugging hook */
269 dbg = attest_dbg;
270 openlog("attest", 0, LOG_DEBUG);
271
272 atexit(library_deinit);
273
274 /* initialize library */
275 if (!library_init(NULL))
276 {
277 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
278 }
279 if (!lib->plugins->load(lib->plugins, NULL,
280 lib->settings->get_str(lib->settings, "attest.load", PLUGINS)))
281 {
282 exit(SS_RC_INITIALIZATION_FAILED);
283 }
284
285 uri = lib->settings->get_str(lib->settings, "attest.database", NULL);
286 if (!uri)
287 {
288 fprintf(stderr, "database URI attest.database not set.\n");
289 exit(SS_RC_INITIALIZATION_FAILED);
290 }
291 attest = attest_db_create(uri);
292 if (!attest)
293 {
294 exit(SS_RC_INITIALIZATION_FAILED);
295 }
296 atexit(cleanup);
297 libimcv_init();
298 libpts_init();
299
300 do_args(argc, argv);
301
302 exit(EXIT_SUCCESS);
303 }
304