c4632511d4a98effae534fad24144ee08d652a5a
[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
23 #include <library.h>
24
25 #include <pts/pts_meas_algo.h>
26
27 #include "attest_db.h"
28 #include "attest_usage.h"
29
30 /**
31 * global attestation database object
32 */
33 attest_db_t *attest;
34
35 /**
36 * atexit handler to close db on shutdown
37 */
38 static void cleanup(void)
39 {
40 attest->destroy(attest);
41 }
42
43 static void do_args(int argc, char *argv[])
44 {
45 enum {
46 OP_UNDEF,
47 OP_USAGE,
48 OP_FILES,
49 OP_PRODUCTS,
50 OP_HASHES,
51 OP_ADD,
52 OP_DEL,
53 } op = OP_UNDEF;
54
55 /* reinit getopt state */
56 optind = 0;
57
58 while (TRUE)
59 {
60 int c;
61
62 struct option long_opts[] = {
63 { "help", no_argument, NULL, 'h' },
64 { "files", no_argument, NULL, 'f' },
65 { "add", no_argument, NULL, 'a' },
66 { "delete", no_argument, NULL, 'd' },
67 { "del", no_argument, NULL, 'd' },
68 { "products", no_argument, NULL, 'p' },
69 { "hashes", no_argument, NULL, 'H' },
70 { "add", no_argument, NULL, 'a' },
71 { "delete", no_argument, NULL, 'd' },
72 { "del", no_argument, NULL, 'd' },
73 { "directory", required_argument, NULL, 'D' },
74 { "dir", required_argument, NULL, 'D' },
75 { "file", required_argument, NULL, 'F' },
76 { "product", required_argument, NULL, 'P' },
77 { "sha1", no_argument, NULL, '1' },
78 { "sha256", no_argument, NULL, '2' },
79 { "sha384", no_argument, NULL, '3' },
80 { "did", required_argument, NULL, '4' },
81 { "fid", required_argument, NULL, '5' },
82 { "pid", required_argument, NULL, '6' },
83 { 0,0,0,0 }
84 };
85
86 c = getopt_long(argc, argv, "", long_opts, NULL);
87 switch (c)
88 {
89 case EOF:
90 break;
91 case 'h':
92 op = OP_USAGE;
93 break;
94 case 'f':
95 op = OP_FILES;
96 continue;
97 case 'p':
98 op = OP_PRODUCTS;
99 continue;
100 case 'H':
101 op = OP_HASHES;
102 continue;
103 case 'a':
104 op = OP_ADD;
105 continue;
106 case 'd':
107 op = OP_DEL;
108 continue;
109 case 'D':
110 if (!attest->set_directory(attest, optarg, op == OP_ADD))
111 {
112 exit(EXIT_FAILURE);
113 }
114 continue;
115 case 'H':
116 op = OP_HASHES;
117 continue;
118 case 'a':
119 op = OP_ADD;
120 continue;
121 case 'd':
122 op = OP_DEL;
123 continue;
124 case 'D':
125 if (!attest->set_directory(attest, optarg, op == OP_ADD))
126 {
127 exit(EXIT_FAILURE);
128 }
129 continue;
130 case 'F':
131 if (!attest->set_file(attest, optarg, op == OP_ADD))
132 {
133 exit(EXIT_FAILURE);
134 }
135 continue;
136 case 'P':
137 if (!attest->set_product(attest, optarg, op == OP_ADD))
138 {
139 exit(EXIT_FAILURE);
140 }
141 continue;
142 case '1':
143 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1);
144 continue;
145 case '2':
146 attest->set_algo(attest, PTS_MEAS_ALGO_SHA256);
147 continue;
148 case '3':
149 attest->set_algo(attest, PTS_MEAS_ALGO_SHA384);
150 continue;
151 case '4':
152 if (!attest->set_did(attest, atoi(optarg)))
153 {
154 exit(EXIT_FAILURE);
155 }
156 continue;
157 case '5':
158 if (!attest->set_fid(attest, atoi(optarg)))
159 {
160 exit(EXIT_FAILURE);
161 }
162 continue;
163 case '6':
164 if (!attest->set_pid(attest, atoi(optarg)))
165 {
166 exit(EXIT_FAILURE);
167 }
168 continue;
169 }
170 break;
171 }
172
173 switch (op)
174 {
175 case OP_USAGE:
176 usage();
177 break;
178 case OP_PRODUCTS:
179 attest->list_products(attest);
180 break;
181 case OP_FILES:
182 attest->list_files(attest);
183 break;
184 case OP_HASHES:
185 attest->list_hashes(attest);
186 break;
187 case OP_ADD:
188 attest->add(attest);
189 break;
190 case OP_DEL:
191 attest->delete(attest);
192 break;
193 case OP_HASHES:
194 attest->list_hashes(attest);
195 break;
196 default:
197 usage();
198 exit(EXIT_FAILURE);
199 }
200 }
201
202 int main(int argc, char *argv[])
203 {
204 char *uri;
205
206 atexit(library_deinit);
207
208 /* initialize library */
209 if (!library_init(NULL))
210 {
211 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
212 }
213 if (!lib->plugins->load(lib->plugins, NULL,
214 lib->settings->get_str(lib->settings, "attest.load", "sqlite")))
215 {
216 exit(SS_RC_INITIALIZATION_FAILED);
217 }
218
219 uri = lib->settings->get_str(lib->settings, "attest.database", NULL);
220 if (!uri)
221 {
222 fprintf(stderr, "database URI attest.database not set.\n");
223 exit(SS_RC_INITIALIZATION_FAILED);
224 }
225 attest = attest_db_create(uri);
226 if (!attest)
227 {
228 exit(SS_RC_INITIALIZATION_FAILED);
229 }
230 atexit(cleanup);
231
232 do_args(argc, argv);
233
234 exit(EXIT_SUCCESS);
235 }
236