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