pt-tls-client: Report loaded plugins
[strongswan.git] / src / pt-tls-client / pt-tls-client.c
1 /*
2 * Copyright (C) 2010-2013 Martin Willi, revosec AG
3 * Copyright (C) 2013 Andreas Steffen, HSR Hochschule für 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 #include <unistd.h>
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <getopt.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <pt_tls.h>
26 #include <pt_tls_client.h>
27 #include <tnc/tnc.h>
28 #include <tls.h>
29
30 #include <library.h>
31 #include <utils/debug.h>
32 #include <credentials/sets/mem_cred.h>
33 #include <utils/optionsfrom.h>
34
35 /**
36 * Print usage information
37 */
38 static void usage(FILE *out, char *cmd)
39 {
40 fprintf(out, "usage:\n");
41 fprintf(out, " %s --connect <address> [--port <port>] [--cert <file>]+\n", cmd);
42 fprintf(out, " [--client <client-id>] [--secret <password>]\n");
43 fprintf(out, " [--optionsfrom <filename>]\n");
44 }
45
46 /**
47 * Client routine
48 */
49 static int client(char *address, u_int16_t port, char *identity)
50 {
51 pt_tls_client_t *assessment;
52 tls_t *tnccs;
53 identification_t *server, *client;
54 host_t *host;
55 status_t status;
56
57 host = host_create_from_dns(address, AF_UNSPEC, port);
58 if (!host)
59 {
60 return 1;
61 }
62 server = identification_create_from_string(address);
63 client = identification_create_from_string(identity);
64 tnccs = (tls_t*)tnc->tnccs->create_instance(tnc->tnccs, TNCCS_2_0, FALSE,
65 server, client, TNC_IFT_TLS_2_0, NULL);
66 if (!tnccs)
67 {
68 fprintf(stderr, "loading TNCCS failed: %s\n", PLUGINS);
69 host->destroy(host);
70 server->destroy(server);
71 client->destroy(client);
72 return 1;
73 }
74 assessment = pt_tls_client_create(host, server, client);
75 status = assessment->run_assessment(assessment, (tnccs_t*)tnccs);
76 assessment->destroy(assessment);
77 tnccs->destroy(tnccs);
78 return status;
79 }
80
81
82 /**
83 * In-Memory credential set
84 */
85 static mem_cred_t *creds;
86
87 /**
88 * Load certificate from file
89 */
90 static bool load_certificate(char *filename)
91 {
92 certificate_t *cert;
93
94 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
95 BUILD_FROM_FILE, filename, BUILD_END);
96 if (!cert)
97 {
98 DBG1(DBG_TLS, "loading certificate from '%s' failed", filename);
99 return FALSE;
100 }
101 creds->add_cert(creds, TRUE, cert);
102 return TRUE;
103 }
104
105 /**
106 * Load private key from file
107 */
108 static bool load_key(char *filename)
109 {
110 private_key_t *key;
111
112 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
113 BUILD_FROM_FILE, filename, BUILD_END);
114 if (!key)
115 {
116 DBG1(DBG_TLS, "loading key from '%s' failed", filename);
117 return FALSE;
118 }
119 creds->add_key(creds, key);
120 return TRUE;
121 }
122
123 /**
124 * Debug level
125 */
126 static level_t pt_tls_level = 1;
127
128 static void dbg_pt_tls(debug_t group, level_t level, char *fmt, ...)
129 {
130 if (level <= pt_tls_level)
131 {
132 va_list args;
133
134 va_start(args, fmt);
135 vfprintf(stderr, fmt, args);
136 fprintf(stderr, "\n");
137 va_end(args);
138 }
139 }
140
141 /**
142 * Handles --optionsfrom arguments
143 */
144 options_t *options;
145
146 /**
147 * Cleanup
148 */
149 static void cleanup()
150 {
151 lib->processor->cancel(lib->processor);
152 lib->credmgr->remove_set(lib->credmgr, &creds->set);
153 creds->destroy(creds);
154 options->destroy(options);
155 libtnccs_deinit();
156 library_deinit();
157 }
158
159 /**
160 * Initialize library
161 */
162 static void init()
163 {
164 plugin_feature_t features[] = {
165 PLUGIN_NOOP,
166 PLUGIN_PROVIDE(CUSTOM, "pt-tls-client"),
167 PLUGIN_DEPENDS(CUSTOM, "tnccs-manager"),
168 };
169 library_init(NULL);
170 libtnccs_init();
171
172 dbg = dbg_pt_tls;
173 options = options_create();
174
175 lib->plugins->add_static_features(lib->plugins, "pt-tls-client", features,
176 countof(features), TRUE);
177 if (!lib->plugins->load(lib->plugins,
178 lib->settings->get_str(lib->settings, "pt-tls-client.load", PLUGINS)))
179 {
180 exit(SS_RC_INITIALIZATION_FAILED);
181 }
182 lib->plugins->status(lib->plugins, LEVEL_CTRL);
183
184 creds = mem_cred_create();
185 lib->credmgr->add_set(lib->credmgr, &creds->set);
186
187 atexit(cleanup);
188 }
189
190 int main(int argc, char *argv[])
191 {
192 char *address = NULL, *identity = "%any", *secret = NULL;
193 int port = PT_TLS_PORT;
194
195 init();
196
197 while (TRUE)
198 {
199 struct option long_opts[] = {
200 {"help", no_argument, NULL, 'h' },
201 {"connect", required_argument, NULL, 'c' },
202 {"client", required_argument, NULL, 'i' },
203 {"secret", required_argument, NULL, 's' },
204 {"port", required_argument, NULL, 'p' },
205 {"cert", required_argument, NULL, 'x' },
206 {"key", required_argument, NULL, 'k' },
207 {"debug", required_argument, NULL, 'd' },
208 {"optionsfrom", required_argument, NULL, '+' },
209 {0,0,0,0 }
210 };
211 switch (getopt_long(argc, argv, "", long_opts, NULL))
212 {
213 case EOF:
214 break;
215 case 'h':
216 usage(stdout, argv[0]);
217 return 0;
218 case 'x':
219 if (!load_certificate(optarg))
220 {
221 return 1;
222 }
223 continue;
224 case 'k':
225 if (!load_key(optarg))
226 {
227 return 1;
228 }
229 continue;
230 case 'c':
231 if (address)
232 {
233 usage(stderr, argv[0]);
234 return 1;
235 }
236 address = optarg;
237 continue;
238 case 'i':
239 identity = optarg;
240 continue;
241 case 's':
242 secret = optarg;
243 continue;
244 case 'p':
245 port = atoi(optarg);
246 continue;
247 case 'd':
248 pt_tls_level = atoi(optarg);
249 continue;
250 case '+': /* --optionsfrom <filename> */
251 if (!options->from(options, optarg, &argc, &argv, optind))
252 {
253 return 1;
254 }
255 continue;
256 default:
257 usage(stderr, argv[0]);
258 return 1;
259 }
260 break;
261 }
262 if (!address)
263 {
264 usage(stderr, argv[0]);
265 return 1;
266 }
267 if (secret)
268 {
269 creds->add_shared(creds, shared_key_create(SHARED_EAP,
270 chunk_clone(chunk_from_str(secret))),
271 identification_create_from_string(identity), NULL);
272 }
273
274 return client(address, port, identity);
275 }