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