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
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 the "Passphrase:"/"PIN:" magic keyword */
93 if ((byte_count
>= 12 &&
94 strcmp(buffer
+ byte_count
- 12, "Passphrase:\n") == 0) ||
96 strcmp(buffer
+ byte_count
- 5, "PIN:\n") == 0))
98 /* remove trailing newline */
99 pass
= strrchr(buffer
, '\n');
105 pass
= getpass(buffer
);
111 ignore_result(write(sock
, pass
, strlen(pass
)));
112 ignore_result(write(sock
, "\n", 1));
117 printf("%s", buffer
);
122 fprintf(stderr
, "reading from socket failed: %s\n", strerror(errno
));
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
)
136 memset(&msg
, 0, sizeof(msg
));
137 msg
.length
= offsetof(stroke_msg_t
, buffer
);
138 msg
.type
= STR_ADD_CONN
;
140 msg
.add_conn
.name
= push_string(&msg
, name
);
141 msg
.add_conn
.version
= 2;
142 msg
.add_conn
.mode
= 1;
143 msg
.add_conn
.mobike
= 1;
144 msg
.add_conn
.dpd
.action
= 1;
146 msg
.add_conn
.me
.id
= push_string(&msg
, my_id
);
147 msg
.add_conn
.me
.address
= push_string(&msg
, my_addr
);
148 msg
.add_conn
.me
.ikeport
= 500;
149 msg
.add_conn
.me
.subnets
= push_string(&msg
, my_nets
);
150 msg
.add_conn
.me
.sendcert
= 1;
152 msg
.add_conn
.other
.id
= push_string(&msg
, other_id
);
153 msg
.add_conn
.other
.address
= push_string(&msg
, other_addr
);
154 msg
.add_conn
.other
.ikeport
= 500;
155 msg
.add_conn
.other
.subnets
= push_string(&msg
, other_nets
);
156 msg
.add_conn
.other
.sendcert
= 1;
158 return send_stroke_msg(&msg
);
161 static int del_connection(char *name
)
165 msg
.length
= offsetof(stroke_msg_t
, buffer
);
166 msg
.type
= STR_DEL_CONN
;
167 msg
.initiate
.name
= push_string(&msg
, name
);
168 return send_stroke_msg(&msg
);
171 static int initiate_connection(char *name
)
175 msg
.length
= offsetof(stroke_msg_t
, buffer
);
176 msg
.type
= STR_INITIATE
;
177 msg
.initiate
.name
= push_string(&msg
, name
);
178 return send_stroke_msg(&msg
);
181 static int terminate_connection(char *name
)
185 msg
.type
= STR_TERMINATE
;
186 msg
.length
= offsetof(stroke_msg_t
, buffer
);
187 msg
.initiate
.name
= push_string(&msg
, name
);
188 return send_stroke_msg(&msg
);
191 static int terminate_connection_srcip(char *start
, char *end
)
195 msg
.type
= STR_TERMINATE_SRCIP
;
196 msg
.length
= offsetof(stroke_msg_t
, buffer
);
197 msg
.terminate_srcip
.start
= push_string(&msg
, start
);
198 msg
.terminate_srcip
.end
= push_string(&msg
, end
);
199 return send_stroke_msg(&msg
);
202 static int rekey_connection(char *name
)
206 msg
.type
= STR_REKEY
;
207 msg
.length
= offsetof(stroke_msg_t
, buffer
);
208 msg
.rekey
.name
= push_string(&msg
, name
);
209 return send_stroke_msg(&msg
);
212 static int route_connection(char *name
)
216 msg
.type
= STR_ROUTE
;
217 msg
.length
= offsetof(stroke_msg_t
, buffer
);
218 msg
.route
.name
= push_string(&msg
, name
);
219 return send_stroke_msg(&msg
);
222 static int unroute_connection(char *name
)
226 msg
.type
= STR_UNROUTE
;
227 msg
.length
= offsetof(stroke_msg_t
, buffer
);
228 msg
.unroute
.name
= push_string(&msg
, name
);
229 return send_stroke_msg(&msg
);
232 static int show_status(stroke_keyword_t kw
, char *connection
)
238 case STROKE_STATUSALL
:
239 msg
.type
= STR_STATUS_ALL
;
241 case STROKE_STATUSALL_NOBLK
:
242 msg
.type
= STR_STATUS_ALL_NOBLK
;
245 msg
.type
= STR_STATUS
;
248 msg
.length
= offsetof(stroke_msg_t
, buffer
);
249 msg
.status
.name
= push_string(&msg
, connection
);
250 return send_stroke_msg(&msg
);
253 static int list_flags
[] = {
269 static int list(stroke_keyword_t kw
, int utc
)
274 msg
.length
= offsetof(stroke_msg_t
, buffer
);
276 msg
.list
.flags
= list_flags
[kw
- STROKE_LIST_FIRST
];
277 return send_stroke_msg(&msg
);
280 static int reread_flags
[] = {
290 static int reread(stroke_keyword_t kw
)
294 msg
.type
= STR_REREAD
;
295 msg
.length
= offsetof(stroke_msg_t
, buffer
);
296 msg
.reread
.flags
= reread_flags
[kw
- STROKE_REREAD_FIRST
];
297 return send_stroke_msg(&msg
);
300 static int purge_flags
[] = {
307 static int purge(stroke_keyword_t kw
)
311 msg
.type
= STR_PURGE
;
312 msg
.length
= offsetof(stroke_msg_t
, buffer
);
313 msg
.purge
.flags
= purge_flags
[kw
- STROKE_PURGE_FIRST
];
314 return send_stroke_msg(&msg
);
317 static int export_flags
[] = {
321 static int export(stroke_keyword_t kw
, char *selector
)
325 msg
.type
= STR_EXPORT
;
326 msg
.length
= offsetof(stroke_msg_t
, buffer
);
327 msg
.export
.selector
= push_string(&msg
, selector
);
328 msg
.export
.flags
= export_flags
[kw
- STROKE_EXPORT_FIRST
];
329 return send_stroke_msg(&msg
);
332 static int leases(stroke_keyword_t kw
, char *pool
, char *address
)
336 msg
.type
= STR_LEASES
;
337 msg
.length
= offsetof(stroke_msg_t
, buffer
);
338 msg
.leases
.pool
= push_string(&msg
, pool
);
339 msg
.leases
.address
= push_string(&msg
, address
);
340 return send_stroke_msg(&msg
);
343 static int memusage()
347 msg
.type
= STR_MEMUSAGE
;
348 msg
.length
= offsetof(stroke_msg_t
, buffer
);
349 return send_stroke_msg(&msg
);
352 static int set_loglevel(char *type
, u_int level
)
356 msg
.type
= STR_LOGLEVEL
;
357 msg
.length
= offsetof(stroke_msg_t
, buffer
);
358 msg
.loglevel
.type
= push_string(&msg
, type
);
359 msg
.loglevel
.level
= level
;
360 return send_stroke_msg(&msg
);
363 static void exit_error(char *error
)
367 fprintf(stderr
, "%s\n", error
);
372 static void exit_usage(char *error
)
375 printf(" Add a connection:\n");
376 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
377 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
378 printf(" where: ID is any IKEv2 ID \n");
379 printf(" ADDR is a IPv4 address\n");
380 printf(" NET is a IPv4 subnet in CIDR notation\n");
381 printf(" Delete a connection:\n");
382 printf(" stroke delete NAME\n");
383 printf(" where: NAME is a connection name added with \"stroke add\"\n");
384 printf(" Initiate a connection:\n");
385 printf(" stroke up NAME\n");
386 printf(" where: NAME is a connection name added with \"stroke add\"\n");
387 printf(" Terminate a connection:\n");
388 printf(" stroke down NAME\n");
389 printf(" where: NAME is a connection name added with \"stroke add\"\n");
390 printf(" Terminate a connection by remote srcip:\n");
391 printf(" stroke down-srcip START [END]\n");
392 printf(" where: START and optional END define the clients source IP\n");
393 printf(" Set loglevel for a logging type:\n");
394 printf(" stroke loglevel TYPE LEVEL\n");
395 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|tnc|imc|imv|pts|tls|lib\n");
396 printf(" LEVEL is -1|0|1|2|3|4\n");
397 printf(" Show connection status:\n");
398 printf(" stroke status\n");
399 printf(" Show extended status information:\n");
400 printf(" stroke statusall\n");
401 printf(" Show extended status information without blocking:\n");
402 printf(" stroke statusallnb\n");
403 printf(" Show list of authority and attribute certificates:\n");
404 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
405 printf(" Show list of end entity certificates, ca info records and crls:\n");
406 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
407 printf(" Show list of supported algorithms:\n");
408 printf(" stroke listalgs\n");
409 printf(" Reload authority and attribute certificates:\n");
410 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
411 printf(" Reload secrets and crls:\n");
412 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
413 printf(" Purge ocsp cache entries:\n");
414 printf(" stroke purgeocsp\n");
415 printf(" Purge CRL cache entries:\n");
416 printf(" stroke purgecrls\n");
417 printf(" Purge X509 cache entries:\n");
418 printf(" stroke purgecerts\n");
419 printf(" Purge IKE_SAs without a CHILD_SA:\n");
420 printf(" stroke purgeike\n");
421 printf(" Export credentials to the console:\n");
422 printf(" stroke exportx509 DN\n");
423 printf(" Show current memory usage:\n");
424 printf(" stroke memusage\n");
425 printf(" Show leases of a pool:\n");
426 printf(" stroke leases [POOL [ADDRESS]]\n");
430 int main(int argc
, char *argv
[])
432 const stroke_token_t
*token
;
436 atexit(library_deinit
);
443 token
= in_word_set(argv
[1], strlen(argv
[1]));
447 exit_usage("unknown keyword");
455 exit_usage("\"add\" needs more parameters...");
457 res
= add_connection(argv
[2],
466 exit_usage("\"delete\" needs a connection name");
468 res
= del_connection(argv
[2]);
473 exit_usage("\"up\" needs a connection name");
475 res
= initiate_connection(argv
[2]);
480 exit_usage("\"down\" needs a connection name");
482 res
= terminate_connection(argv
[2]);
484 case STROKE_DOWN_SRCIP
:
487 exit_usage("\"down-srcip\" needs start and optional end address");
489 res
= terminate_connection_srcip(argv
[2], argc
> 3 ? argv
[3] : NULL
);
494 exit_usage("\"rekey\" needs a connection name");
496 res
= rekey_connection(argv
[2]);
501 exit_usage("\"route\" needs a connection name");
503 res
= route_connection(argv
[2]);
508 exit_usage("\"unroute\" needs a connection name");
510 res
= unroute_connection(argv
[2]);
512 case STROKE_LOGLEVEL
:
515 exit_usage("\"logtype\" needs more parameters...");
517 res
= set_loglevel(argv
[2], atoi(argv
[3]));
520 case STROKE_STATUSALL
:
521 case STROKE_STATUSALL_NOBLK
:
522 res
= show_status(token
->kw
, argc
> 2 ? argv
[2] : NULL
);
524 case STROKE_LIST_PUBKEYS
:
525 case STROKE_LIST_CERTS
:
526 case STROKE_LIST_CACERTS
:
527 case STROKE_LIST_OCSPCERTS
:
528 case STROKE_LIST_AACERTS
:
529 case STROKE_LIST_ACERTS
:
530 case STROKE_LIST_CAINFOS
:
531 case STROKE_LIST_CRLS
:
532 case STROKE_LIST_OCSP
:
533 case STROKE_LIST_ALGS
:
534 case STROKE_LIST_PLUGINS
:
535 case STROKE_LIST_ALL
:
536 res
= list(token
->kw
, argc
> 2 && strcmp(argv
[2], "--utc") == 0);
538 case STROKE_REREAD_SECRETS
:
539 case STROKE_REREAD_CACERTS
:
540 case STROKE_REREAD_OCSPCERTS
:
541 case STROKE_REREAD_AACERTS
:
542 case STROKE_REREAD_ACERTS
:
543 case STROKE_REREAD_CRLS
:
544 case STROKE_REREAD_ALL
:
545 res
= reread(token
->kw
);
547 case STROKE_PURGE_OCSP
:
548 case STROKE_PURGE_CRLS
:
549 case STROKE_PURGE_CERTS
:
550 case STROKE_PURGE_IKE
:
551 res
= purge(token
->kw
);
553 case STROKE_EXPORT_X509
:
556 exit_usage("\"exportx509\" needs a distinguished name");
558 res
= export(token
->kw
, argv
[2]);
561 res
= leases(token
->kw
, argc
> 2 ? argv
[2] : NULL
,
562 argc
> 3 ? argv
[3] : NULL
);
564 case STROKE_MEMUSAGE
: