reordered options
[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 { "products", no_argument, NULL, 'p' },
71 { "hashes", no_argument, NULL, 'H' },
72 { "add", no_argument, NULL, 'a' },
73 { "delete", no_argument, NULL, 'd' },
74 { "del", no_argument, NULL, 'd' },
75 { "directory", required_argument, NULL, 'D' },
76 { "dir", required_argument, NULL, 'D' },
77 { "file", required_argument, NULL, 'F' },
78 { "product", required_argument, NULL, 'P' },
79 { "sha1", no_argument, NULL, '1' },
80 { "sha256", no_argument, NULL, '2' },
81 { "sha384", no_argument, NULL, '3' },
82 { "did", required_argument, NULL, '4' },
83 { "fid", required_argument, NULL, '5' },
84 { "pid", required_argument, NULL, '6' },
85 { 0,0,0,0 }
86 };
87
88 c = getopt_long(argc, argv, "", long_opts, NULL);
89 switch (c)
90 {
91 case EOF:
92 break;
93 case 'h':
94 op = OP_USAGE;
95 break;
96 case 'f':
97 op = OP_FILES;
98 continue;
99 case 'p':
100 op = OP_PRODUCTS;
101 continue;
102 case 'H':
103 op = OP_HASHES;
104 continue;
105 case 'a':
106 op = OP_ADD;
107 continue;
108 case 'd':
109 op = OP_DEL;
110 continue;
111 case 'D':
112 if (!attest->set_directory(attest, optarg, op == OP_ADD))
113 {
114 exit(EXIT_FAILURE);
115 }
116 continue;
117 case 'H':
118 op = OP_HASHES;
119 continue;
120 case 'a':
121 op = OP_ADD;
122 continue;
123 case 'd':
124 op = OP_DEL;
125 continue;
126 case 'D':
127 if (!attest->set_directory(attest, optarg, op == OP_ADD))
128 {
129 exit(EXIT_FAILURE);
130 }
131 continue;
132 case 'F':
133 if (!attest->set_file(attest, optarg, op == OP_ADD))
134 {
135 exit(EXIT_FAILURE);
136 }
137 continue;
138 case 'P':
139 if (!attest->set_product(attest, optarg, op == OP_ADD))
140 {
141 exit(EXIT_FAILURE);
142 }
143 continue;
144 case '1':
145 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1);
146 continue;
147 case '2':
148 attest->set_algo(attest, PTS_MEAS_ALGO_SHA256);
149 continue;
150 case '3':
151 attest->set_algo(attest, PTS_MEAS_ALGO_SHA384);
152 continue;
153 case '4':
154 if (!attest->set_did(attest, atoi(optarg)))
155 {
156 exit(EXIT_FAILURE);
157 }
158 continue;
159 case '5':
160 if (!attest->set_fid(attest, atoi(optarg)))
161 {
162 exit(EXIT_FAILURE);
163 }
164 continue;
165 case '6':
166 if (!attest->set_pid(attest, atoi(optarg)))
167 {
168 exit(EXIT_FAILURE);
169 }
170 continue;
171 }
172 break;
173 }
174
175 switch (op)
176 {
177 case OP_USAGE:
178 usage();
179 break;
180 case OP_PRODUCTS:
181 attest->list_products(attest);
182 break;
183 case OP_FILES:
184 attest->list_files(attest);
185 break;
186 case OP_HASHES:
187 attest->list_hashes(attest);
188 break;
189 case OP_ADD:
190 attest->add(attest);
191 break;
192 case OP_DEL:
193 attest->delete(attest);
194 break;
195 case OP_HASHES:
196 attest->list_hashes(attest);
197 break;
198 default:
199 usage();
200 exit(EXIT_FAILURE);
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