81807b24fd38b233cb67a27b43429076da83e7a6
[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 { "products", no_argument, NULL, 'p' },
66 { "hashes", no_argument, NULL, 'H' },
67 { "add", no_argument, NULL, 'a' },
68 { "delete", no_argument, NULL, 'd' },
69 { "del", no_argument, NULL, 'd' },
70 { "directory", required_argument, NULL, 'D' },
71 { "dir", required_argument, NULL, 'D' },
72 { "file", required_argument, NULL, 'F' },
73 { "product", required_argument, NULL, 'P' },
74 { "sha1", no_argument, NULL, '1' },
75 { "sha256", no_argument, NULL, '2' },
76 { "sha384", no_argument, NULL, '3' },
77 { "did", required_argument, NULL, '4' },
78 { "fid", required_argument, NULL, '5' },
79 { "pid", required_argument, NULL, '6' },
80 { 0,0,0,0 }
81 };
82
83 c = getopt_long(argc, argv, "", long_opts, NULL);
84 switch (c)
85 {
86 case EOF:
87 break;
88 case 'h':
89 op = OP_USAGE;
90 break;
91 case 'f':
92 op = OP_FILES;
93 continue;
94 case 'p':
95 op = OP_PRODUCTS;
96 continue;
97 case 'H':
98 op = OP_HASHES;
99 continue;
100 case 'a':
101 op = OP_ADD;
102 continue;
103 case 'd':
104 op = OP_DEL;
105 continue;
106 case 'D':
107 if (!attest->set_directory(attest, optarg, op == OP_ADD))
108 {
109 exit(EXIT_FAILURE);
110 }
111 continue;
112 case 'H':
113 operation = OP_HASHES;
114 continue;
115 case 'F':
116 if (!attest->set_file(attest, optarg, op == OP_ADD))
117 {
118 exit(EXIT_FAILURE);
119 }
120 continue;
121 case 'P':
122 if (!attest->set_product(attest, optarg, op == OP_ADD))
123 {
124 exit(EXIT_FAILURE);
125 }
126 continue;
127 case '1':
128 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1);
129 continue;
130 case '2':
131 attest->set_algo(attest, PTS_MEAS_ALGO_SHA256);
132 continue;
133 case '3':
134 attest->set_algo(attest, PTS_MEAS_ALGO_SHA384);
135 continue;
136 case '4':
137 if (!attest->set_did(attest, atoi(optarg)))
138 {
139 exit(EXIT_FAILURE);
140 }
141 continue;
142 case '5':
143 if (!attest->set_fid(attest, atoi(optarg)))
144 {
145 exit(EXIT_FAILURE);
146 }
147 continue;
148 case '6':
149 if (!attest->set_pid(attest, atoi(optarg)))
150 {
151 exit(EXIT_FAILURE);
152 }
153 continue;
154 }
155 break;
156 }
157
158 switch (op)
159 {
160 case OP_USAGE:
161 usage();
162 break;
163 case OP_PRODUCTS:
164 attest->list_products(attest);
165 break;
166 case OP_FILES:
167 attest->list_files(attest);
168 break;
169 case OP_HASHES:
170 attest->list_hashes(attest);
171 break;
172 case OP_ADD:
173 attest->add(attest);
174 break;
175 case OP_DEL:
176 attest->delete(attest);
177 break;
178 case OP_HASHES:
179 if ((!product || *product == '\0') && (!file || *file == '\0'))
180 {
181 list_hashes(algo);
182 }
183 else
184 {
185 list_hashes_for_product(algo, product, pid);
186 }
187 break;
188 default:
189 usage();
190 exit(EXIT_FAILURE);
191 }
192
193 if (fid)
194 {
195 free(file);
196 }
197 if (pid)
198 {
199 free(product);
200 }
201
202 }
203
204 int main(int argc, char *argv[])
205 {
206 char *uri;
207
208 atexit(library_deinit);
209
210 /* initialize library */
211 if (!library_init(NULL))
212 {
213 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
214 }
215 if (!lib->plugins->load(lib->plugins, NULL,
216 lib->settings->get_str(lib->settings, "attest.load", "sqlite")))
217 {
218 exit(SS_RC_INITIALIZATION_FAILED);
219 }
220
221 uri = lib->settings->get_str(lib->settings, "attest.database", NULL);
222 if (!uri)
223 {
224 fprintf(stderr, "database URI attest.database not set.\n");
225 exit(SS_RC_INITIALIZATION_FAILED);
226 }
227 attest = attest_db_create(uri);
228 if (!attest)
229 {
230 exit(SS_RC_INITIALIZATION_FAILED);
231 }
232 atexit(cleanup);
233
234 do_args(argc, argv);
235
236 exit(EXIT_SUCCESS);
237 }
238