experimental P2P-NAT-T for IKEv2 merged back from branch
[strongswan.git] / src / stroke / stroke.c
1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <sys/fcntl.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stddef.h>
28
29 #include "stroke.h"
30 #include "stroke_keywords.h"
31
32 struct stroke_token {
33 char *name;
34 stroke_keyword_t kw;
35 };
36
37 static char* push_string(stroke_msg_t *msg, char *string)
38 {
39 unsigned long string_start = msg->length;
40
41 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
42 {
43 return NULL;
44 }
45 else
46 {
47 msg->length += strlen(string) + 1;
48 strcpy((char*)msg + string_start, string);
49 return (char*)string_start;
50 }
51 }
52
53 static int send_stroke_msg (stroke_msg_t *msg)
54 {
55 struct sockaddr_un ctl_addr = { AF_UNIX, STROKE_SOCKET };
56 int sock;
57 char buffer[64];
58 int byte_count;
59
60 msg->output_verbosity = 1; /* CONTROL */
61
62 sock = socket(AF_UNIX, SOCK_STREAM, 0);
63 if (sock < 0)
64 {
65 fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
66 return -1;
67 }
68 if (connect(sock, (struct sockaddr *)&ctl_addr,
69 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
70 {
71 fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
72 close(sock);
73 return -1;
74 }
75
76 /* send message */
77 if (write(sock, msg, msg->length) != msg->length)
78 {
79 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
80 close(sock);
81 return -1;
82 }
83
84 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
85 {
86 buffer[byte_count] = '\0';
87 printf("%s", buffer);
88 }
89 if (byte_count < 0)
90 {
91 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
92 }
93
94 close(sock);
95 return 0;
96 }
97
98 static int add_connection(char *name,
99 char *my_id, char *other_id,
100 char *my_addr, char *other_addr,
101 char *my_net, char *other_net,
102 u_int my_netmask, u_int other_netmask)
103 {
104 stroke_msg_t msg;
105
106 msg.length = offsetof(stroke_msg_t, buffer);
107 msg.type = STR_ADD_CONN;
108
109 msg.add_conn.name = push_string(&msg, name);
110 msg.add_conn.ikev2 = 1;
111 msg.add_conn.auth_method = 2;
112 msg.add_conn.eap_type = 0;
113 msg.add_conn.mode = 1;
114 msg.add_conn.mobike = 1;
115 msg.add_conn.force_encap = 0;
116
117 msg.add_conn.rekey.reauth = 0;
118 msg.add_conn.rekey.ipsec_lifetime = 0;
119 msg.add_conn.rekey.ike_lifetime = 0;
120 msg.add_conn.rekey.margin = 0;
121 msg.add_conn.rekey.tries = 0;
122 msg.add_conn.rekey.fuzz = 0;
123
124 msg.add_conn.algorithms.ike = NULL;
125 msg.add_conn.algorithms.esp = NULL;
126
127 msg.add_conn.dpd.delay = 0;
128 msg.add_conn.dpd.action = 1;
129
130 msg.add_conn.p2p.mediation = 0;
131 msg.add_conn.p2p.mediated_by = NULL;
132 msg.add_conn.p2p.peerid = NULL;
133
134 msg.add_conn.me.id = push_string(&msg, my_id);
135 msg.add_conn.me.address = push_string(&msg, my_addr);
136 msg.add_conn.me.subnet = push_string(&msg, my_net);
137 msg.add_conn.me.subnet_mask = my_netmask;
138 msg.add_conn.me.sourceip = NULL;
139 msg.add_conn.me.virtual_ip = 0;
140 msg.add_conn.me.cert = NULL;
141 msg.add_conn.me.ca = NULL;
142 msg.add_conn.me.sendcert = 1;
143 msg.add_conn.me.hostaccess = 0;
144 msg.add_conn.me.tohost = 0;
145 msg.add_conn.me.protocol = 0;
146 msg.add_conn.me.port = 0;
147
148 msg.add_conn.other.id = push_string(&msg, other_id);
149 msg.add_conn.other.address = push_string(&msg, other_addr);
150 msg.add_conn.other.subnet = push_string(&msg, other_net);
151 msg.add_conn.other.subnet_mask = other_netmask;
152 msg.add_conn.other.sourceip = NULL;
153 msg.add_conn.other.virtual_ip = 0;
154 msg.add_conn.other.cert = NULL;
155 msg.add_conn.other.ca = NULL;
156 msg.add_conn.other.sendcert = 1;
157 msg.add_conn.other.hostaccess = 0;
158 msg.add_conn.other.tohost = 0;
159 msg.add_conn.other.protocol = 0;
160 msg.add_conn.other.port = 0;
161
162 return send_stroke_msg(&msg);
163 }
164
165 static int del_connection(char *name)
166 {
167 stroke_msg_t msg;
168
169 msg.length = offsetof(stroke_msg_t, buffer);
170 msg.type = STR_DEL_CONN;
171 msg.initiate.name = push_string(&msg, name);
172 return send_stroke_msg(&msg);
173 }
174
175 static int initiate_connection(char *name)
176 {
177 stroke_msg_t msg;
178
179 msg.length = offsetof(stroke_msg_t, buffer);
180 msg.type = STR_INITIATE;
181 msg.initiate.name = push_string(&msg, name);
182 return send_stroke_msg(&msg);
183 }
184
185 static int terminate_connection(char *name)
186 {
187 stroke_msg_t msg;
188
189 msg.type = STR_TERMINATE;
190 msg.length = offsetof(stroke_msg_t, buffer);
191 msg.initiate.name = push_string(&msg, name);
192 return send_stroke_msg(&msg);
193 }
194
195 static int route_connection(char *name)
196 {
197 stroke_msg_t msg;
198
199 msg.type = STR_ROUTE;
200 msg.length = offsetof(stroke_msg_t, buffer);
201 msg.route.name = push_string(&msg, name);
202 return send_stroke_msg(&msg);
203 }
204
205 static int unroute_connection(char *name)
206 {
207 stroke_msg_t msg;
208
209 msg.type = STR_UNROUTE;
210 msg.length = offsetof(stroke_msg_t, buffer);
211 msg.unroute.name = push_string(&msg, name);
212 return send_stroke_msg(&msg);
213 }
214
215 static int show_status(stroke_keyword_t kw, char *connection)
216 {
217 stroke_msg_t msg;
218
219 msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
220 msg.length = offsetof(stroke_msg_t, buffer);
221 msg.status.name = push_string(&msg, connection);
222 return send_stroke_msg(&msg);
223 }
224
225 static int list_flags[] = {
226 LIST_CERTS,
227 LIST_CACERTS,
228 LIST_OCSPCERTS,
229 LIST_AACERTS,
230 LIST_ACERTS,
231 LIST_GROUPS,
232 LIST_CAINFOS,
233 LIST_CRLS,
234 LIST_OCSP,
235 LIST_ALL
236 };
237
238 static int list(stroke_keyword_t kw, int utc)
239 {
240 stroke_msg_t msg;
241
242 msg.type = STR_LIST;
243 msg.length = offsetof(stroke_msg_t, buffer);
244 msg.list.utc = utc;
245 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
246 return send_stroke_msg(&msg);
247 }
248
249 static int reread_flags[] = {
250 REREAD_SECRETS,
251 REREAD_CACERTS,
252 REREAD_OCSPCERTS,
253 REREAD_AACERTS,
254 REREAD_ACERTS,
255 REREAD_CRLS,
256 REREAD_ALL
257 };
258
259 static int reread(stroke_keyword_t kw)
260 {
261 stroke_msg_t msg;
262
263 msg.type = STR_REREAD;
264 msg.length = offsetof(stroke_msg_t, buffer);
265 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
266 return send_stroke_msg(&msg);
267 }
268
269 static int purge_flags[] = {
270 PURGE_OCSP
271 };
272
273 static int purge(stroke_keyword_t kw)
274 {
275 stroke_msg_t msg;
276
277 msg.type = STR_PURGE;
278 msg.length = offsetof(stroke_msg_t, buffer);
279 msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
280 return send_stroke_msg(&msg);
281 }
282
283 static int set_loglevel(char *type, u_int level)
284 {
285 stroke_msg_t msg;
286
287 msg.type = STR_LOGLEVEL;
288 msg.length = offsetof(stroke_msg_t, buffer);
289 msg.loglevel.type = push_string(&msg, type);
290 msg.loglevel.level = level;
291 return send_stroke_msg(&msg);
292 }
293
294 static void exit_error(char *error)
295 {
296 if (error)
297 {
298 fprintf(stderr, "%s\n", error);
299 }
300 exit(-1);
301 }
302
303 static void exit_usage(char *error)
304 {
305 printf("Usage:\n");
306 printf(" Add a connection:\n");
307 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
308 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
309 printf(" where: ID is any IKEv2 ID \n");
310 printf(" ADDR is a IPv4 address\n");
311 printf(" NET is a IPv4 address of the subnet to tunnel\n");
312 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
313 printf(" Delete a connection:\n");
314 printf(" stroke delete NAME\n");
315 printf(" where: NAME is a connection name added with \"stroke add\"\n");
316 printf(" Initiate a connection:\n");
317 printf(" stroke up NAME\n");
318 printf(" where: NAME is a connection name added with \"stroke add\"\n");
319 printf(" Terminate a connection:\n");
320 printf(" stroke down NAME\n");
321 printf(" where: NAME is a connection name added with \"stroke add\"\n");
322 printf(" Set loglevel for a logging type:\n");
323 printf(" stroke loglevel TYPE LEVEL\n");
324 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
325 printf(" LEVEL is -1|0|1|2|3|4\n");
326 printf(" Show connection status:\n");
327 printf(" stroke status\n");
328 printf(" Show list of authority and attribute certificates:\n");
329 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
330 printf(" Show list of end entity certificates, ca info records and crls:\n");
331 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
332 printf(" Reload authority and attribute certificates:\n");
333 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
334 printf(" Reload secrets and crls:\n");
335 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
336 printf(" Purge ocsp cache entries:\n");
337 printf(" stroke purgeocsp\n");
338 exit_error(error);
339 }
340
341 int main(int argc, char *argv[])
342 {
343 const stroke_token_t *token;
344 int res = 0;
345
346 if (argc < 2)
347 {
348 exit_usage(NULL);
349 }
350
351 token = in_word_set(argv[1], strlen(argv[1]));
352
353 if (token == NULL)
354 {
355 exit_usage("unknown keyword");
356 }
357
358 switch (token->kw)
359 {
360 case STROKE_ADD:
361 if (argc < 11)
362 {
363 exit_usage("\"add\" needs more parameters...");
364 }
365 res = add_connection(argv[2],
366 argv[3], argv[4],
367 argv[5], argv[6],
368 argv[7], argv[8],
369 atoi(argv[9]), atoi(argv[10]));
370 break;
371 case STROKE_DELETE:
372 case STROKE_DEL:
373 if (argc < 3)
374 {
375 exit_usage("\"delete\" needs a connection name");
376 }
377 res = del_connection(argv[2]);
378 break;
379 case STROKE_UP:
380 if (argc < 3)
381 {
382 exit_usage("\"up\" needs a connection name");
383 }
384 res = initiate_connection(argv[2]);
385 break;
386 case STROKE_DOWN:
387 if (argc < 3)
388 {
389 exit_usage("\"down\" needs a connection name");
390 }
391 res = terminate_connection(argv[2]);
392 break;
393 case STROKE_ROUTE:
394 if (argc < 3)
395 {
396 exit_usage("\"route\" needs a connection name");
397 }
398 res = route_connection(argv[2]);
399 break;
400 case STROKE_UNROUTE:
401 if (argc < 3)
402 {
403 exit_usage("\"unroute\" needs a connection name");
404 }
405 res = unroute_connection(argv[2]);
406 break;
407 case STROKE_LOGLEVEL:
408 if (argc < 4)
409 {
410 exit_usage("\"logtype\" needs more parameters...");
411 }
412 res = set_loglevel(argv[2], atoi(argv[3]));
413 break;
414 case STROKE_STATUS:
415 case STROKE_STATUSALL:
416 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
417 break;
418 case STROKE_LIST_CERTS:
419 case STROKE_LIST_CACERTS:
420 case STROKE_LIST_OCSPCERTS:
421 case STROKE_LIST_AACERTS:
422 case STROKE_LIST_ACERTS:
423 case STROKE_LIST_CAINFOS:
424 case STROKE_LIST_CRLS:
425 case STROKE_LIST_OCSP:
426 case STROKE_LIST_ALL:
427 res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
428 break;
429 case STROKE_REREAD_SECRETS:
430 case STROKE_REREAD_CACERTS:
431 case STROKE_REREAD_OCSPCERTS:
432 case STROKE_REREAD_AACERTS:
433 case STROKE_REREAD_ACERTS:
434 case STROKE_REREAD_CRLS:
435 case STROKE_REREAD_ALL:
436 res = reread(token->kw);
437 break;
438 case STROKE_PURGE_OCSP:
439 res = purge(token->kw);
440 break;
441 default:
442 exit_usage(NULL);
443 }
444 return res;
445 }