fixed sleep command in ikev1/esp-ah-tunnel scenario
[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 #include <string.h>
29
30 #include <library.h>
31
32 #include "stroke_msg.h"
33 #include "stroke_keywords.h"
34
35 struct stroke_token {
36 char *name;
37 stroke_keyword_t kw;
38 };
39
40 static char* push_string(stroke_msg_t *msg, char *string)
41 {
42 unsigned long string_start = msg->length;
43
44 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
45 {
46 return NULL;
47 }
48 else
49 {
50 msg->length += strlen(string) + 1;
51 strcpy((char*)msg + string_start, string);
52 return (char*)string_start;
53 }
54 }
55
56 static int send_stroke_msg (stroke_msg_t *msg)
57 {
58 struct sockaddr_un ctl_addr;
59 int sock, byte_count;
60 char buffer[512], *pass;
61
62 ctl_addr.sun_family = AF_UNIX;
63 strcpy(ctl_addr.sun_path, STROKE_SOCKET);
64
65 msg->output_verbosity = 1; /* CONTROL */
66
67 sock = socket(AF_UNIX, SOCK_STREAM, 0);
68 if (sock < 0)
69 {
70 fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
71 return -1;
72 }
73 if (connect(sock, (struct sockaddr *)&ctl_addr,
74 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
75 {
76 fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
77 close(sock);
78 return -1;
79 }
80
81 /* send message */
82 if (write(sock, msg, msg->length) != msg->length)
83 {
84 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
85 close(sock);
86 return -1;
87 }
88
89 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
90 {
91 buffer[byte_count] = '\0';
92
93 /* we prompt if we receive the "Passphrase:"/"PIN:" magic keyword */
94 if ((byte_count >= 12 &&
95 strcmp(buffer + byte_count - 12, "Passphrase:\n") == 0) ||
96 (byte_count >= 5 &&
97 strcmp(buffer + byte_count - 5, "PIN:\n") == 0))
98 {
99 /* remove trailing newline */
100 pass = strrchr(buffer, '\n');
101 if (pass)
102 {
103 *pass = ' ';
104 }
105 pass = getpass(buffer);
106 if (pass)
107 {
108 ignore_result(write(sock, pass, strlen(pass)));
109 ignore_result(write(sock, "\n", 1));
110 }
111 }
112 else
113 {
114 printf("%s", buffer);
115 }
116 }
117 if (byte_count < 0)
118 {
119 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
120 }
121
122 close(sock);
123 return 0;
124 }
125
126 static int add_connection(char *name,
127 char *my_id, char *other_id,
128 char *my_addr, char *other_addr,
129 char *my_nets, char *other_nets)
130 {
131 stroke_msg_t msg;
132
133 memset(&msg, 0, sizeof(msg));
134 msg.length = offsetof(stroke_msg_t, buffer);
135 msg.type = STR_ADD_CONN;
136
137 msg.add_conn.name = push_string(&msg, name);
138 msg.add_conn.ikev2 = 1;
139 msg.add_conn.auth_method = 2;
140 msg.add_conn.mode = 1;
141 msg.add_conn.mobike = 1;
142 msg.add_conn.dpd.action = 1;
143
144 msg.add_conn.me.id = push_string(&msg, my_id);
145 msg.add_conn.me.address = push_string(&msg, my_addr);
146 msg.add_conn.me.ikeport = 500;
147 msg.add_conn.me.subnets = push_string(&msg, my_nets);
148 msg.add_conn.me.sendcert = 1;
149
150 msg.add_conn.other.id = push_string(&msg, other_id);
151 msg.add_conn.other.address = push_string(&msg, other_addr);
152 msg.add_conn.other.ikeport = 500;
153 msg.add_conn.other.subnets = push_string(&msg, other_nets);
154 msg.add_conn.other.sendcert = 1;
155
156 return send_stroke_msg(&msg);
157 }
158
159 static int del_connection(char *name)
160 {
161 stroke_msg_t msg;
162
163 msg.length = offsetof(stroke_msg_t, buffer);
164 msg.type = STR_DEL_CONN;
165 msg.initiate.name = push_string(&msg, name);
166 return send_stroke_msg(&msg);
167 }
168
169 static int initiate_connection(char *name)
170 {
171 stroke_msg_t msg;
172
173 msg.length = offsetof(stroke_msg_t, buffer);
174 msg.type = STR_INITIATE;
175 msg.initiate.name = push_string(&msg, name);
176 return send_stroke_msg(&msg);
177 }
178
179 static int terminate_connection(char *name)
180 {
181 stroke_msg_t msg;
182
183 msg.type = STR_TERMINATE;
184 msg.length = offsetof(stroke_msg_t, buffer);
185 msg.initiate.name = push_string(&msg, name);
186 return send_stroke_msg(&msg);
187 }
188
189 static int terminate_connection_srcip(char *start, char *end)
190 {
191 stroke_msg_t msg;
192
193 msg.type = STR_TERMINATE_SRCIP;
194 msg.length = offsetof(stroke_msg_t, buffer);
195 msg.terminate_srcip.start = push_string(&msg, start);
196 msg.terminate_srcip.end = push_string(&msg, end);
197 return send_stroke_msg(&msg);
198 }
199
200 static int rekey_connection(char *name)
201 {
202 stroke_msg_t msg;
203
204 msg.type = STR_REKEY;
205 msg.length = offsetof(stroke_msg_t, buffer);
206 msg.rekey.name = push_string(&msg, name);
207 return send_stroke_msg(&msg);
208 }
209
210 static int route_connection(char *name)
211 {
212 stroke_msg_t msg;
213
214 msg.type = STR_ROUTE;
215 msg.length = offsetof(stroke_msg_t, buffer);
216 msg.route.name = push_string(&msg, name);
217 return send_stroke_msg(&msg);
218 }
219
220 static int unroute_connection(char *name)
221 {
222 stroke_msg_t msg;
223
224 msg.type = STR_UNROUTE;
225 msg.length = offsetof(stroke_msg_t, buffer);
226 msg.unroute.name = push_string(&msg, name);
227 return send_stroke_msg(&msg);
228 }
229
230 static int show_status(stroke_keyword_t kw, char *connection)
231 {
232 stroke_msg_t msg;
233
234 switch (kw)
235 {
236 case STROKE_STATUSALL:
237 msg.type = STR_STATUS_ALL;
238 break;
239 case STROKE_STATUSALL_NOBLK:
240 msg.type = STR_STATUS_ALL_NOBLK;
241 break;
242 default:
243 msg.type = STR_STATUS;
244 break;
245 }
246 msg.length = offsetof(stroke_msg_t, buffer);
247 msg.status.name = push_string(&msg, connection);
248 return send_stroke_msg(&msg);
249 }
250
251 static int list_flags[] = {
252 LIST_PUBKEYS,
253 LIST_CERTS,
254 LIST_CACERTS,
255 LIST_OCSPCERTS,
256 LIST_AACERTS,
257 LIST_ACERTS,
258 LIST_GROUPS,
259 LIST_CAINFOS,
260 LIST_CRLS,
261 LIST_OCSP,
262 LIST_ALGS,
263 LIST_ALL
264 };
265
266 static int list(stroke_keyword_t kw, int utc)
267 {
268 stroke_msg_t msg;
269
270 msg.type = STR_LIST;
271 msg.length = offsetof(stroke_msg_t, buffer);
272 msg.list.utc = utc;
273 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
274 return send_stroke_msg(&msg);
275 }
276
277 static int reread_flags[] = {
278 REREAD_SECRETS,
279 REREAD_CACERTS,
280 REREAD_OCSPCERTS,
281 REREAD_AACERTS,
282 REREAD_ACERTS,
283 REREAD_CRLS,
284 REREAD_ALL
285 };
286
287 static int reread(stroke_keyword_t kw)
288 {
289 stroke_msg_t msg;
290
291 msg.type = STR_REREAD;
292 msg.length = offsetof(stroke_msg_t, buffer);
293 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
294 return send_stroke_msg(&msg);
295 }
296
297 static int purge_flags[] = {
298 PURGE_OCSP,
299 PURGE_CRLS,
300 PURGE_CERTS,
301 PURGE_IKE,
302 };
303
304 static int purge(stroke_keyword_t kw)
305 {
306 stroke_msg_t msg;
307
308 msg.type = STR_PURGE;
309 msg.length = offsetof(stroke_msg_t, buffer);
310 msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
311 return send_stroke_msg(&msg);
312 }
313
314 static int export_flags[] = {
315 EXPORT_X509,
316 };
317
318 static int export(stroke_keyword_t kw, char *selector)
319 {
320 stroke_msg_t msg;
321
322 msg.type = STR_EXPORT;
323 msg.length = offsetof(stroke_msg_t, buffer);
324 msg.export.selector = push_string(&msg, selector);
325 msg.export.flags = export_flags[kw - STROKE_EXPORT_FIRST];
326 return send_stroke_msg(&msg);
327 }
328
329 static int leases(stroke_keyword_t kw, char *pool, char *address)
330 {
331 stroke_msg_t msg;
332
333 msg.type = STR_LEASES;
334 msg.length = offsetof(stroke_msg_t, buffer);
335 msg.leases.pool = push_string(&msg, pool);
336 msg.leases.address = push_string(&msg, address);
337 return send_stroke_msg(&msg);
338 }
339
340 static int memusage()
341 {
342 stroke_msg_t msg;
343
344 msg.type = STR_MEMUSAGE;
345 msg.length = offsetof(stroke_msg_t, buffer);
346 return send_stroke_msg(&msg);
347 }
348
349 static int set_loglevel(char *type, u_int level)
350 {
351 stroke_msg_t msg;
352
353 msg.type = STR_LOGLEVEL;
354 msg.length = offsetof(stroke_msg_t, buffer);
355 msg.loglevel.type = push_string(&msg, type);
356 msg.loglevel.level = level;
357 return send_stroke_msg(&msg);
358 }
359
360 static void exit_error(char *error)
361 {
362 if (error)
363 {
364 fprintf(stderr, "%s\n", error);
365 }
366 exit(-1);
367 }
368
369 static void exit_usage(char *error)
370 {
371 printf("Usage:\n");
372 printf(" Add a connection:\n");
373 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
374 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
375 printf(" where: ID is any IKEv2 ID \n");
376 printf(" ADDR is a IPv4 address\n");
377 printf(" NET is a IPv4 subnet in CIDR notation\n");
378 printf(" Delete a connection:\n");
379 printf(" stroke delete NAME\n");
380 printf(" where: NAME is a connection name added with \"stroke add\"\n");
381 printf(" Initiate a connection:\n");
382 printf(" stroke up NAME\n");
383 printf(" where: NAME is a connection name added with \"stroke add\"\n");
384 printf(" Terminate a connection:\n");
385 printf(" stroke down NAME\n");
386 printf(" where: NAME is a connection name added with \"stroke add\"\n");
387 printf(" Terminate a connection by remote srcip:\n");
388 printf(" stroke down-srcip START [END]\n");
389 printf(" where: START and optional END define the clients source IP\n");
390 printf(" Set loglevel for a logging type:\n");
391 printf(" stroke loglevel TYPE LEVEL\n");
392 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
393 printf(" LEVEL is -1|0|1|2|3|4\n");
394 printf(" Show connection status:\n");
395 printf(" stroke status\n");
396 printf(" Show extended status information:\n");
397 printf(" stroke statusall\n");
398 printf(" Show extended status information without blocking:\n");
399 printf(" stroke statusallnb\n");
400 printf(" Show list of authority and attribute certificates:\n");
401 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
402 printf(" Show list of end entity certificates, ca info records and crls:\n");
403 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
404 printf(" Show list of supported algorithms:\n");
405 printf(" stroke listalgs\n");
406 printf(" Reload authority and attribute certificates:\n");
407 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
408 printf(" Reload secrets and crls:\n");
409 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
410 printf(" Purge ocsp cache entries:\n");
411 printf(" stroke purgeocsp\n");
412 printf(" Purge CRL cache entries:\n");
413 printf(" stroke purgecrls\n");
414 printf(" Purge X509 cache entries:\n");
415 printf(" stroke purgecerts\n");
416 printf(" Purge IKE_SAs without a CHILD_SA:\n");
417 printf(" stroke purgeike\n");
418 printf(" Export credentials to the console:\n");
419 printf(" stroke exportx509 DN\n");
420 printf(" Show current memory usage:\n");
421 printf(" stroke memusage\n");
422 printf(" Show leases of a pool:\n");
423 printf(" stroke leases [POOL [ADDRESS]]\n");
424 exit_error(error);
425 }
426
427 int main(int argc, char *argv[])
428 {
429 const stroke_token_t *token;
430 int res = 0;
431
432 library_init(NULL);
433 atexit(library_deinit);
434
435 if (argc < 2)
436 {
437 exit_usage(NULL);
438 }
439
440 token = in_word_set(argv[1], strlen(argv[1]));
441
442 if (token == NULL)
443 {
444 exit_usage("unknown keyword");
445 }
446
447 switch (token->kw)
448 {
449 case STROKE_ADD:
450 if (argc < 11)
451 {
452 exit_usage("\"add\" needs more parameters...");
453 }
454 res = add_connection(argv[2],
455 argv[3], argv[4],
456 argv[5], argv[6],
457 argv[7], argv[8]);
458 break;
459 case STROKE_DELETE:
460 case STROKE_DEL:
461 if (argc < 3)
462 {
463 exit_usage("\"delete\" needs a connection name");
464 }
465 res = del_connection(argv[2]);
466 break;
467 case STROKE_UP:
468 if (argc < 3)
469 {
470 exit_usage("\"up\" needs a connection name");
471 }
472 res = initiate_connection(argv[2]);
473 break;
474 case STROKE_DOWN:
475 if (argc < 3)
476 {
477 exit_usage("\"down\" needs a connection name");
478 }
479 res = terminate_connection(argv[2]);
480 break;
481 case STROKE_DOWN_SRCIP:
482 if (argc < 3)
483 {
484 exit_usage("\"down-srcip\" needs start and optional end address");
485 }
486 res = terminate_connection_srcip(argv[2], argc > 3 ? argv[3] : NULL);
487 break;
488 case STROKE_REKEY:
489 if (argc < 3)
490 {
491 exit_usage("\"rekey\" needs a connection name");
492 }
493 res = rekey_connection(argv[2]);
494 break;
495 case STROKE_ROUTE:
496 if (argc < 3)
497 {
498 exit_usage("\"route\" needs a connection name");
499 }
500 res = route_connection(argv[2]);
501 break;
502 case STROKE_UNROUTE:
503 if (argc < 3)
504 {
505 exit_usage("\"unroute\" needs a connection name");
506 }
507 res = unroute_connection(argv[2]);
508 break;
509 case STROKE_LOGLEVEL:
510 if (argc < 4)
511 {
512 exit_usage("\"logtype\" needs more parameters...");
513 }
514 res = set_loglevel(argv[2], atoi(argv[3]));
515 break;
516 case STROKE_STATUS:
517 case STROKE_STATUSALL:
518 case STROKE_STATUSALL_NOBLK:
519 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
520 break;
521 case STROKE_LIST_PUBKEYS:
522 case STROKE_LIST_CERTS:
523 case STROKE_LIST_CACERTS:
524 case STROKE_LIST_OCSPCERTS:
525 case STROKE_LIST_AACERTS:
526 case STROKE_LIST_ACERTS:
527 case STROKE_LIST_CAINFOS:
528 case STROKE_LIST_CRLS:
529 case STROKE_LIST_OCSP:
530 case STROKE_LIST_ALGS:
531 case STROKE_LIST_ALL:
532 res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
533 break;
534 case STROKE_REREAD_SECRETS:
535 case STROKE_REREAD_CACERTS:
536 case STROKE_REREAD_OCSPCERTS:
537 case STROKE_REREAD_AACERTS:
538 case STROKE_REREAD_ACERTS:
539 case STROKE_REREAD_CRLS:
540 case STROKE_REREAD_ALL:
541 res = reread(token->kw);
542 break;
543 case STROKE_PURGE_OCSP:
544 case STROKE_PURGE_CRLS:
545 case STROKE_PURGE_CERTS:
546 case STROKE_PURGE_IKE:
547 res = purge(token->kw);
548 break;
549 case STROKE_EXPORT_X509:
550 if (argc != 3)
551 {
552 exit_usage("\"exportx509\" needs a distinguished name");
553 }
554 res = export(token->kw, argv[2]);
555 break;
556 case STROKE_LEASES:
557 res = leases(token->kw, argc > 2 ? argv[2] : NULL,
558 argc > 3 ? argv[3] : NULL);
559 break;
560 case STROKE_MEMUSAGE:
561 res = memusage();
562 break;
563 default:
564 exit_usage(NULL);
565 }
566 return res;
567 }