1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2007-2012 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
18 #include <sys/types.h>
20 #include <sys/socket.h>
31 #include "stroke_msg.h"
32 #include "stroke_keywords.h"
39 static char* push_string(stroke_msg_t
*msg
, char *string
)
41 unsigned long string_start
= msg
->length
;
43 if (string
== NULL
|| msg
->length
+ strlen(string
) >= sizeof(stroke_msg_t
))
49 msg
->length
+= strlen(string
) + 1;
50 strcpy((char*)msg
+ string_start
, string
);
51 return (char*)string_start
;
55 static int send_stroke_msg (stroke_msg_t
*msg
)
57 struct sockaddr_un ctl_addr
;
59 char buffer
[512], *pass
;
61 ctl_addr
.sun_family
= AF_UNIX
;
62 strcpy(ctl_addr
.sun_path
, STROKE_SOCKET
);
64 msg
->output_verbosity
= 1; /* CONTROL */
66 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
69 fprintf(stderr
, "Opening unix socket %s: %s\n", STROKE_SOCKET
, strerror(errno
));
72 if (connect(sock
, (struct sockaddr
*)&ctl_addr
,
73 offsetof(struct sockaddr_un
, sun_path
) + strlen(ctl_addr
.sun_path
)) < 0)
75 fprintf(stderr
, "Connect to socket failed: %s\n", strerror(errno
));
81 if (write(sock
, msg
, msg
->length
) != msg
->length
)
83 fprintf(stderr
, "writing to socket failed: %s\n", strerror(errno
));
88 while ((byte_count
= read(sock
, buffer
, sizeof(buffer
)-1)) > 0)
90 buffer
[byte_count
] = '\0';
92 /* we prompt if we receive a magic keyword */
93 if ((byte_count
>= 12 &&
94 strcmp(buffer
+ byte_count
- 12, "Passphrase:\n") == 0) ||
96 strcmp(buffer
+ byte_count
- 10, "Password:\n") == 0) ||
98 strcmp(buffer
+ byte_count
- 5, "PIN:\n") == 0))
100 /* remove trailing newline */
101 pass
= strrchr(buffer
, '\n');
107 pass
= getpass(buffer
);
113 ignore_result(write(sock
, pass
, strlen(pass
)));
114 ignore_result(write(sock
, "\n", 1));
119 printf("%s", buffer
);
124 fprintf(stderr
, "reading from socket failed: %s\n", strerror(errno
));
131 static int add_connection(char *name
,
132 char *my_id
, char *other_id
,
133 char *my_addr
, char *other_addr
,
134 char *my_nets
, char *other_nets
)
138 memset(&msg
, 0, sizeof(msg
));
139 msg
.length
= offsetof(stroke_msg_t
, buffer
);
140 msg
.type
= STR_ADD_CONN
;
142 msg
.add_conn
.name
= push_string(&msg
, name
);
143 msg
.add_conn
.version
= 2;
144 msg
.add_conn
.mode
= 1;
145 msg
.add_conn
.mobike
= 1;
146 msg
.add_conn
.dpd
.action
= 1;
148 msg
.add_conn
.me
.id
= push_string(&msg
, my_id
);
149 msg
.add_conn
.me
.address
= push_string(&msg
, my_addr
);
150 msg
.add_conn
.me
.ikeport
= 500;
151 msg
.add_conn
.me
.subnets
= push_string(&msg
, my_nets
);
152 msg
.add_conn
.me
.sendcert
= 1;
154 msg
.add_conn
.other
.id
= push_string(&msg
, other_id
);
155 msg
.add_conn
.other
.address
= push_string(&msg
, other_addr
);
156 msg
.add_conn
.other
.ikeport
= 500;
157 msg
.add_conn
.other
.subnets
= push_string(&msg
, other_nets
);
158 msg
.add_conn
.other
.sendcert
= 1;
160 return send_stroke_msg(&msg
);
163 static int del_connection(char *name
)
167 msg
.length
= offsetof(stroke_msg_t
, buffer
);
168 msg
.type
= STR_DEL_CONN
;
169 msg
.initiate
.name
= push_string(&msg
, name
);
170 return send_stroke_msg(&msg
);
173 static int initiate_connection(char *name
)
177 msg
.length
= offsetof(stroke_msg_t
, buffer
);
178 msg
.type
= STR_INITIATE
;
179 msg
.initiate
.name
= push_string(&msg
, name
);
180 return send_stroke_msg(&msg
);
183 static int terminate_connection(char *name
)
187 msg
.type
= STR_TERMINATE
;
188 msg
.length
= offsetof(stroke_msg_t
, buffer
);
189 msg
.initiate
.name
= push_string(&msg
, name
);
190 return send_stroke_msg(&msg
);
193 static int terminate_connection_srcip(char *start
, char *end
)
197 msg
.type
= STR_TERMINATE_SRCIP
;
198 msg
.length
= offsetof(stroke_msg_t
, buffer
);
199 msg
.terminate_srcip
.start
= push_string(&msg
, start
);
200 msg
.terminate_srcip
.end
= push_string(&msg
, end
);
201 return send_stroke_msg(&msg
);
204 static int rekey_connection(char *name
)
208 msg
.type
= STR_REKEY
;
209 msg
.length
= offsetof(stroke_msg_t
, buffer
);
210 msg
.rekey
.name
= push_string(&msg
, name
);
211 return send_stroke_msg(&msg
);
214 static int route_connection(char *name
)
218 msg
.type
= STR_ROUTE
;
219 msg
.length
= offsetof(stroke_msg_t
, buffer
);
220 msg
.route
.name
= push_string(&msg
, name
);
221 return send_stroke_msg(&msg
);
224 static int unroute_connection(char *name
)
228 msg
.type
= STR_UNROUTE
;
229 msg
.length
= offsetof(stroke_msg_t
, buffer
);
230 msg
.unroute
.name
= push_string(&msg
, name
);
231 return send_stroke_msg(&msg
);
234 static int show_status(stroke_keyword_t kw
, char *connection
)
240 case STROKE_STATUSALL
:
241 msg
.type
= STR_STATUS_ALL
;
243 case STROKE_STATUSALL_NOBLK
:
244 msg
.type
= STR_STATUS_ALL_NOBLK
;
247 msg
.type
= STR_STATUS
;
250 msg
.length
= offsetof(stroke_msg_t
, buffer
);
251 msg
.status
.name
= push_string(&msg
, connection
);
252 return send_stroke_msg(&msg
);
255 static int list_flags
[] = {
271 static int list(stroke_keyword_t kw
, int utc
)
276 msg
.length
= offsetof(stroke_msg_t
, buffer
);
278 msg
.list
.flags
= list_flags
[kw
- STROKE_LIST_FIRST
];
279 return send_stroke_msg(&msg
);
282 static int reread_flags
[] = {
292 static int reread(stroke_keyword_t kw
)
296 msg
.type
= STR_REREAD
;
297 msg
.length
= offsetof(stroke_msg_t
, buffer
);
298 msg
.reread
.flags
= reread_flags
[kw
- STROKE_REREAD_FIRST
];
299 return send_stroke_msg(&msg
);
302 static int purge_flags
[] = {
309 static int purge(stroke_keyword_t kw
)
313 msg
.type
= STR_PURGE
;
314 msg
.length
= offsetof(stroke_msg_t
, buffer
);
315 msg
.purge
.flags
= purge_flags
[kw
- STROKE_PURGE_FIRST
];
316 return send_stroke_msg(&msg
);
319 static int export_flags
[] = {
323 static int export(stroke_keyword_t kw
, char *selector
)
327 msg
.type
= STR_EXPORT
;
328 msg
.length
= offsetof(stroke_msg_t
, buffer
);
329 msg
.export
.selector
= push_string(&msg
, selector
);
330 msg
.export
.flags
= export_flags
[kw
- STROKE_EXPORT_FIRST
];
331 return send_stroke_msg(&msg
);
334 static int leases(stroke_keyword_t kw
, char *pool
, char *address
)
338 msg
.type
= STR_LEASES
;
339 msg
.length
= offsetof(stroke_msg_t
, buffer
);
340 msg
.leases
.pool
= push_string(&msg
, pool
);
341 msg
.leases
.address
= push_string(&msg
, address
);
342 return send_stroke_msg(&msg
);
345 static int memusage()
349 msg
.type
= STR_MEMUSAGE
;
350 msg
.length
= offsetof(stroke_msg_t
, buffer
);
351 return send_stroke_msg(&msg
);
354 static int user_credentials(char *name
, char *user
, char *pass
)
358 msg
.type
= STR_USER_CREDS
;
359 msg
.length
= offsetof(stroke_msg_t
, buffer
);
360 msg
.user_creds
.name
= push_string(&msg
, name
);
361 msg
.user_creds
.username
= push_string(&msg
, user
);
362 msg
.user_creds
.password
= push_string(&msg
, pass
);
363 return send_stroke_msg(&msg
);
367 static int set_loglevel(char *type
, u_int level
)
371 msg
.type
= STR_LOGLEVEL
;
372 msg
.length
= offsetof(stroke_msg_t
, buffer
);
373 msg
.loglevel
.type
= push_string(&msg
, type
);
374 msg
.loglevel
.level
= level
;
375 return send_stroke_msg(&msg
);
378 static void exit_error(char *error
)
382 fprintf(stderr
, "%s\n", error
);
387 static void exit_usage(char *error
)
390 printf(" Add a connection:\n");
391 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
392 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
393 printf(" where: ID is any IKEv2 ID \n");
394 printf(" ADDR is a IPv4 address\n");
395 printf(" NET is a IPv4 subnet in CIDR notation\n");
396 printf(" Delete a connection:\n");
397 printf(" stroke delete NAME\n");
398 printf(" where: NAME is a connection name added with \"stroke add\"\n");
399 printf(" Initiate a connection:\n");
400 printf(" stroke up NAME\n");
401 printf(" where: NAME is a connection name added with \"stroke add\"\n");
402 printf(" Terminate a connection:\n");
403 printf(" stroke down NAME\n");
404 printf(" where: NAME is a connection name added with \"stroke add\"\n");
405 printf(" Terminate a connection by remote srcip:\n");
406 printf(" stroke down-srcip START [END]\n");
407 printf(" where: START and optional END define the clients source IP\n");
408 printf(" Set loglevel for a logging type:\n");
409 printf(" stroke loglevel TYPE LEVEL\n");
410 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|asn|enc|tnc|imc|imv|pts|tls|lib\n");
411 printf(" LEVEL is -1|0|1|2|3|4\n");
412 printf(" Show connection status:\n");
413 printf(" stroke status\n");
414 printf(" Show extended status information:\n");
415 printf(" stroke statusall\n");
416 printf(" Show extended status information without blocking:\n");
417 printf(" stroke statusallnb\n");
418 printf(" Show list of authority and attribute certificates:\n");
419 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
420 printf(" Show list of end entity certificates, ca info records and crls:\n");
421 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
422 printf(" Show list of supported algorithms:\n");
423 printf(" stroke listalgs\n");
424 printf(" Reload authority and attribute certificates:\n");
425 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
426 printf(" Reload secrets and crls:\n");
427 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
428 printf(" Purge ocsp cache entries:\n");
429 printf(" stroke purgeocsp\n");
430 printf(" Purge CRL cache entries:\n");
431 printf(" stroke purgecrls\n");
432 printf(" Purge X509 cache entries:\n");
433 printf(" stroke purgecerts\n");
434 printf(" Purge IKE_SAs without a CHILD_SA:\n");
435 printf(" stroke purgeike\n");
436 printf(" Export credentials to the console:\n");
437 printf(" stroke exportx509 DN\n");
438 printf(" Show current memory usage:\n");
439 printf(" stroke memusage\n");
440 printf(" Show leases of a pool:\n");
441 printf(" stroke leases [POOL [ADDRESS]]\n");
442 printf(" Set username and password for a connection:\n");
443 printf(" stroke user-creds NAME USERNAME [PASSWORD]\n");
444 printf(" where: NAME is a connection name added with \"stroke add\"\n");
445 printf(" USERNAME is the username\n");
446 printf(" PASSWORD is the optional password, you'll be asked to enter it if not given\n");
450 int main(int argc
, char *argv
[])
452 const stroke_token_t
*token
;
456 atexit(library_deinit
);
463 token
= in_word_set(argv
[1], strlen(argv
[1]));
467 exit_usage("unknown keyword");
475 exit_usage("\"add\" needs more parameters...");
477 res
= add_connection(argv
[2],
486 exit_usage("\"delete\" needs a connection name");
488 res
= del_connection(argv
[2]);
493 exit_usage("\"up\" needs a connection name");
495 res
= initiate_connection(argv
[2]);
500 exit_usage("\"down\" needs a connection name");
502 res
= terminate_connection(argv
[2]);
504 case STROKE_DOWN_SRCIP
:
507 exit_usage("\"down-srcip\" needs start and optional end address");
509 res
= terminate_connection_srcip(argv
[2], argc
> 3 ? argv
[3] : NULL
);
514 exit_usage("\"rekey\" needs a connection name");
516 res
= rekey_connection(argv
[2]);
521 exit_usage("\"route\" needs a connection name");
523 res
= route_connection(argv
[2]);
528 exit_usage("\"unroute\" needs a connection name");
530 res
= unroute_connection(argv
[2]);
532 case STROKE_LOGLEVEL
:
535 exit_usage("\"logtype\" needs more parameters...");
537 res
= set_loglevel(argv
[2], atoi(argv
[3]));
540 case STROKE_STATUSALL
:
541 case STROKE_STATUSALL_NOBLK
:
542 res
= show_status(token
->kw
, argc
> 2 ? argv
[2] : NULL
);
544 case STROKE_LIST_PUBKEYS
:
545 case STROKE_LIST_CERTS
:
546 case STROKE_LIST_CACERTS
:
547 case STROKE_LIST_OCSPCERTS
:
548 case STROKE_LIST_AACERTS
:
549 case STROKE_LIST_ACERTS
:
550 case STROKE_LIST_CAINFOS
:
551 case STROKE_LIST_CRLS
:
552 case STROKE_LIST_OCSP
:
553 case STROKE_LIST_ALGS
:
554 case STROKE_LIST_PLUGINS
:
555 case STROKE_LIST_ALL
:
556 res
= list(token
->kw
, argc
> 2 && strcmp(argv
[2], "--utc") == 0);
558 case STROKE_REREAD_SECRETS
:
559 case STROKE_REREAD_CACERTS
:
560 case STROKE_REREAD_OCSPCERTS
:
561 case STROKE_REREAD_AACERTS
:
562 case STROKE_REREAD_ACERTS
:
563 case STROKE_REREAD_CRLS
:
564 case STROKE_REREAD_ALL
:
565 res
= reread(token
->kw
);
567 case STROKE_PURGE_OCSP
:
568 case STROKE_PURGE_CRLS
:
569 case STROKE_PURGE_CERTS
:
570 case STROKE_PURGE_IKE
:
571 res
= purge(token
->kw
);
573 case STROKE_EXPORT_X509
:
576 exit_usage("\"exportx509\" needs a distinguished name");
578 res
= export(token
->kw
, argv
[2]);
581 res
= leases(token
->kw
, argc
> 2 ? argv
[2] : NULL
,
582 argc
> 3 ? argv
[3] : NULL
);
584 case STROKE_MEMUSAGE
:
587 case STROKE_USER_CREDS
:
590 exit_usage("\"user-creds\" needs a connection name, "
591 "username and optionally a password");
593 res
= user_credentials(argv
[2], argv
[3], argc
> 4 ? argv
[4] : NULL
);