2 * Copyright (C) 2011 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
29 #include <pts/pts_meas_algo.h>
31 #include "attest_db.h"
32 #include "attest_usage.h"
35 * global debug output variables
37 static int debug_level
= 0;
38 static bool stderr_quiet
= TRUE
;
43 static void attest_dbg(debug_t group
, level_t level
, char *fmt
, ...)
45 int priority
= LOG_INFO
;
47 char *current
= buffer
, *next
;
50 if (level
<= debug_level
)
55 vfprintf(stderr
, fmt
, args
);
56 fprintf(stderr
, "\n");
60 /* write in memory buffer first */
62 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
65 /* do a syslog with every line */
68 next
= strchr(current
, '\n');
73 syslog(priority
, "%s\n", current
);
80 * global attestation database object
85 * atexit handler to close db on shutdown
87 static void cleanup(void)
89 attest
->destroy(attest
);
95 static void do_args(int argc
, char *argv
[])
110 /* reinit getopt state */
117 struct option long_opts
[] = {
118 { "help", no_argument
, NULL
, 'h' },
119 { "components", no_argument
, NULL
, 'c' },
120 { "files", no_argument
, NULL
, 'f' },
121 { "keys", no_argument
, NULL
, 'k' },
122 { "products", no_argument
, NULL
, 'p' },
123 { "hashes", no_argument
, NULL
, 'H' },
124 { "measurements", no_argument
, NULL
, 'M' },
125 { "add", no_argument
, NULL
, 'a' },
126 { "delete", no_argument
, NULL
, 'd' },
127 { "del", no_argument
, NULL
, 'd' },
128 { "products", no_argument
, NULL
, 'p' },
129 { "hashes", no_argument
, NULL
, 'H' },
130 { "add", no_argument
, NULL
, 'a' },
131 { "delete", no_argument
, NULL
, 'd' },
132 { "del", no_argument
, NULL
, 'd' },
133 { "directory", required_argument
, NULL
, 'D' },
134 { "dir", required_argument
, NULL
, 'D' },
135 { "file", required_argument
, NULL
, 'F' },
136 { "key", required_argument
, NULL
, 'K' },
137 { "owner", required_argument
, NULL
, 'O' },
138 { "product", required_argument
, NULL
, 'P' },
139 { "sha1", no_argument
, NULL
, '1' },
140 { "sha256", no_argument
, NULL
, '2' },
141 { "sha384", no_argument
, NULL
, '3' },
142 { "did", required_argument
, NULL
, '4' },
143 { "fid", required_argument
, NULL
, '5' },
144 { "pid", required_argument
, NULL
, '6' },
145 { "cid", required_argument
, NULL
, '7' },
146 { "kid", required_argument
, NULL
, '8' },
150 c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
174 op
= OP_MEASUREMENTS
;
183 if (!attest
->set_component(attest
, optarg
, op
== OP_ADD
))
189 if (!attest
->set_directory(attest
, optarg
, op
== OP_ADD
))
204 if (!attest
->set_directory(attest
, optarg
, op
== OP_ADD
))
210 if (!attest
->set_file(attest
, optarg
, op
== OP_ADD
))
216 if (!attest
->set_key(attest
, optarg
, op
== OP_ADD
))
222 attest
->set_owner(attest
, optarg
);
225 if (!attest
->set_product(attest
, optarg
, op
== OP_ADD
))
231 attest
->set_algo(attest
, PTS_MEAS_ALGO_SHA1
);
234 attest
->set_algo(attest
, PTS_MEAS_ALGO_SHA256
);
237 attest
->set_algo(attest
, PTS_MEAS_ALGO_SHA384
);
240 if (!attest
->set_did(attest
, atoi(optarg
)))
246 if (!attest
->set_fid(attest
, atoi(optarg
)))
252 if (!attest
->set_pid(attest
, atoi(optarg
)))
258 if (!attest
->set_cid(attest
, atoi(optarg
)))
264 if (!attest
->set_kid(attest
, atoi(optarg
)))
279 attest
->list_products(attest
);
282 attest
->list_keys(attest
);
285 attest
->list_components(attest
);
288 attest
->list_files(attest
);
291 attest
->list_hashes(attest
);
293 case OP_MEASUREMENTS
:
294 attest
->list_measurements(attest
);
300 attest
->delete(attest
);
303 attest
->list_hashes(attest
);
311 int main(int argc
, char *argv
[])
315 /* enable attest debugging hook */
317 openlog("attest", 0, LOG_DEBUG
);
319 atexit(library_deinit
);
321 /* initialize library */
322 if (!library_init(NULL
))
324 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
326 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
327 lib
->settings
->get_str(lib
->settings
, "attest.load", PLUGINS
)))
329 exit(SS_RC_INITIALIZATION_FAILED
);
332 uri
= lib
->settings
->get_str(lib
->settings
, "attest.database", NULL
);
335 fprintf(stderr
, "database URI attest.database not set.\n");
336 exit(SS_RC_INITIALIZATION_FAILED
);
338 attest
= attest_db_create(uri
);
341 exit(SS_RC_INITIALIZATION_FAILED
);