implemented simple delete from database
[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 { "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 'F':
113 if (!attest->set_file(attest, optarg, op == OP_ADD))
114 {
115 exit(EXIT_FAILURE);
116 }
117 continue;
118 case 'P':
119 if (!attest->set_product(attest, optarg, op == OP_ADD))
120 {
121 exit(EXIT_FAILURE);
122 }
123 continue;
124 case '1':
125 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1);
126 continue;
127 case '2':
128 attest->set_algo(attest, PTS_MEAS_ALGO_SHA256);
129 continue;
130 case '3':
131 attest->set_algo(attest, PTS_MEAS_ALGO_SHA384);
132 continue;
133 case '4':
134 if (!attest->set_did(attest, atoi(optarg)))
135 {
136 exit(EXIT_FAILURE);
137 }
138 continue;
139 case '5':
140 if (!attest->set_fid(attest, atoi(optarg)))
141 {
142 exit(EXIT_FAILURE);
143 }
144 continue;
145 case '6':
146 if (!attest->set_pid(attest, atoi(optarg)))
147 {
148 exit(EXIT_FAILURE);
149 }
150 continue;
151 }
152 break;
153 }
154
155 switch (op)
156 {
157 case OP_USAGE:
158 usage();
159 break;
160 case OP_PRODUCTS:
161 attest->list_products(attest);
162 break;
163 case OP_FILES:
164 attest->list_files(attest);
165 break;
166 case OP_HASHES:
167 attest->list_hashes(attest);
168 break;
169 case OP_ADD:
170 attest->add(attest);
171 break;
172 case OP_DEL:
173 attest->delete(attest);
174 break;
175 default:
176 usage();
177 exit(EXIT_FAILURE);
178 }
179 }
180
181 int main(int argc, char *argv[])
182 {
183 char *uri;
184
185 atexit(library_deinit);
186
187 /* initialize library */
188 if (!library_init(NULL))
189 {
190 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
191 }
192 if (!lib->plugins->load(lib->plugins, NULL,
193 lib->settings->get_str(lib->settings, "attest.load", "sqlite")))
194 {
195 exit(SS_RC_INITIALIZATION_FAILED);
196 }
197
198 uri = lib->settings->get_str(lib->settings, "attest.database", NULL);
199 if (!uri)
200 {
201 fprintf(stderr, "database URI attest.database not set.\n");
202 exit(SS_RC_INITIALIZATION_FAILED);
203 }
204 attest = attest_db_create(uri);
205 if (!attest)
206 {
207 exit(SS_RC_INITIALIZATION_FAILED);
208 }
209 atexit(cleanup);
210
211 do_args(argc, argv);
212
213 exit(EXIT_SUCCESS);
214 }
215