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