6dbaf646430d3d33843e256aab0903ecbe951bff
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>
22 #include <sys/fcntl.h>
32 #include "stroke_msg.h"
33 #include "stroke_keywords.h"
40 static char* push_string(stroke_msg_t
*msg
, char *string
)
42 unsigned long string_start
= msg
->length
;
44 if (string
== NULL
|| msg
->length
+ strlen(string
) >= sizeof(stroke_msg_t
))
50 msg
->length
+= strlen(string
) + 1;
51 strcpy((char*)msg
+ string_start
, string
);
52 return (char*)string_start
;
56 static int send_stroke_msg (stroke_msg_t
*msg
)
58 struct sockaddr_un ctl_addr
;
60 char buffer
[512], *pass
;
62 ctl_addr
.sun_family
= AF_UNIX
;
63 strcpy(ctl_addr
.sun_path
, STROKE_SOCKET
);
65 msg
->output_verbosity
= 1; /* CONTROL */
67 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
70 fprintf(stderr
, "Opening unix socket %s: %s\n", STROKE_SOCKET
, strerror(errno
));
73 if (connect(sock
, (struct sockaddr
*)&ctl_addr
,
74 offsetof(struct sockaddr_un
, sun_path
) + strlen(ctl_addr
.sun_path
)) < 0)
76 fprintf(stderr
, "Connect to socket failed: %s\n", strerror(errno
));
82 if (write(sock
, msg
, msg
->length
) != msg
->length
)
84 fprintf(stderr
, "writing to socket failed: %s\n", strerror(errno
));
89 while ((byte_count
= read(sock
, buffer
, sizeof(buffer
)-1)) > 0)
91 buffer
[byte_count
] = '\0';
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) ||
97 strcmp(buffer
+ byte_count
- 5, "PIN:\n") == 0))
99 /* remove trailing newline */
100 pass
= strrchr(buffer
, '\n');
105 pass
= getpass(buffer
);
108 ignore_result(write(sock
, pass
, strlen(pass
)));
109 ignore_result(write(sock
, "\n", 1));
114 printf("%s", buffer
);
119 fprintf(stderr
, "reading from socket failed: %s\n", strerror(errno
));
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
)
133 memset(&msg
, 0, sizeof(msg
));
134 msg
.length
= offsetof(stroke_msg_t
, buffer
);
135 msg
.type
= STR_ADD_CONN
;
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;
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;
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;
156 return send_stroke_msg(&msg
);
159 static int del_connection(char *name
)
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
);
169 static int initiate_connection(char *name
)
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
);
179 static int terminate_connection(char *name
)
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
);
189 static int terminate_connection_srcip(char *start
, char *end
)
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
);
200 static int rekey_connection(char *name
)
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
);
210 static int route_connection(char *name
)
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
);
220 static int unroute_connection(char *name
)
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
);
230 static int show_status(stroke_keyword_t kw
, char *connection
)
234 msg
.type
= (kw
== STROKE_STATUS
)? STR_STATUS
:STR_STATUS_ALL
;
235 msg
.length
= offsetof(stroke_msg_t
, buffer
);
236 msg
.status
.name
= push_string(&msg
, connection
);
237 return send_stroke_msg(&msg
);
240 static int list_flags
[] = {
255 static int list(stroke_keyword_t kw
, int utc
)
260 msg
.length
= offsetof(stroke_msg_t
, buffer
);
262 msg
.list
.flags
= list_flags
[kw
- STROKE_LIST_FIRST
];
263 return send_stroke_msg(&msg
);
266 static int reread_flags
[] = {
276 static int reread(stroke_keyword_t kw
)
280 msg
.type
= STR_REREAD
;
281 msg
.length
= offsetof(stroke_msg_t
, buffer
);
282 msg
.reread
.flags
= reread_flags
[kw
- STROKE_REREAD_FIRST
];
283 return send_stroke_msg(&msg
);
286 static int purge_flags
[] = {
293 static int purge(stroke_keyword_t kw
)
297 msg
.type
= STR_PURGE
;
298 msg
.length
= offsetof(stroke_msg_t
, buffer
);
299 msg
.purge
.flags
= purge_flags
[kw
- STROKE_PURGE_FIRST
];
300 return send_stroke_msg(&msg
);
303 static int export_flags
[] = {
307 static int export(stroke_keyword_t kw
, char *selector
)
311 msg
.type
= STR_EXPORT
;
312 msg
.length
= offsetof(stroke_msg_t
, buffer
);
313 msg
.export
.selector
= push_string(&msg
, selector
);
314 msg
.export
.flags
= export_flags
[kw
- STROKE_EXPORT_FIRST
];
315 return send_stroke_msg(&msg
);
318 static int leases(stroke_keyword_t kw
, char *pool
, char *address
)
322 msg
.type
= STR_LEASES
;
323 msg
.length
= offsetof(stroke_msg_t
, buffer
);
324 msg
.leases
.pool
= push_string(&msg
, pool
);
325 msg
.leases
.address
= push_string(&msg
, address
);
326 return send_stroke_msg(&msg
);
329 static int memusage()
333 msg
.type
= STR_MEMUSAGE
;
334 msg
.length
= offsetof(stroke_msg_t
, buffer
);
335 return send_stroke_msg(&msg
);
338 static int set_loglevel(char *type
, u_int level
)
342 msg
.type
= STR_LOGLEVEL
;
343 msg
.length
= offsetof(stroke_msg_t
, buffer
);
344 msg
.loglevel
.type
= push_string(&msg
, type
);
345 msg
.loglevel
.level
= level
;
346 return send_stroke_msg(&msg
);
349 static void exit_error(char *error
)
353 fprintf(stderr
, "%s\n", error
);
358 static void exit_usage(char *error
)
361 printf(" Add a connection:\n");
362 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
363 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
364 printf(" where: ID is any IKEv2 ID \n");
365 printf(" ADDR is a IPv4 address\n");
366 printf(" NET is a IPv4 subnet in CIDR notation\n");
367 printf(" Delete a connection:\n");
368 printf(" stroke delete NAME\n");
369 printf(" where: NAME is a connection name added with \"stroke add\"\n");
370 printf(" Initiate a connection:\n");
371 printf(" stroke up NAME\n");
372 printf(" where: NAME is a connection name added with \"stroke add\"\n");
373 printf(" Terminate a connection:\n");
374 printf(" stroke down NAME\n");
375 printf(" where: NAME is a connection name added with \"stroke add\"\n");
376 printf(" Terminate a connection by remote srcip:\n");
377 printf(" stroke down-srcip START [END]\n");
378 printf(" where: START and optional END define the clients source IP\n");
379 printf(" Set loglevel for a logging type:\n");
380 printf(" stroke loglevel TYPE LEVEL\n");
381 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
382 printf(" LEVEL is -1|0|1|2|3|4\n");
383 printf(" Show connection status:\n");
384 printf(" stroke status\n");
385 printf(" Show list of authority and attribute certificates:\n");
386 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
387 printf(" Show list of end entity certificates, ca info records and crls:\n");
388 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
389 printf(" Show list of supported algorithms:\n");
390 printf(" stroke listalgs\n");
391 printf(" Reload authority and attribute certificates:\n");
392 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
393 printf(" Reload secrets and crls:\n");
394 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
395 printf(" Purge ocsp cache entries:\n");
396 printf(" stroke purgeocsp\n");
397 printf(" Purge CRL cache entries:\n");
398 printf(" stroke purgecrls\n");
399 printf(" Purge X509 cache entries:\n");
400 printf(" stroke purgecerts\n");
401 printf(" Purge IKE_SAs without a CHILD_SA:\n");
402 printf(" stroke purgeike\n");
403 printf(" Export credentials to the console:\n");
404 printf(" stroke exportx509 DN\n");
405 printf(" Show leases of a pool:\n");
406 printf(" stroke leases [POOL [ADDRESS]]\n");
410 int main(int argc
, char *argv
[])
412 const stroke_token_t
*token
;
416 atexit(library_deinit
);
423 token
= in_word_set(argv
[1], strlen(argv
[1]));
427 exit_usage("unknown keyword");
435 exit_usage("\"add\" needs more parameters...");
437 res
= add_connection(argv
[2],
446 exit_usage("\"delete\" needs a connection name");
448 res
= del_connection(argv
[2]);
453 exit_usage("\"up\" needs a connection name");
455 res
= initiate_connection(argv
[2]);
460 exit_usage("\"down\" needs a connection name");
462 res
= terminate_connection(argv
[2]);
464 case STROKE_DOWN_SRCIP
:
467 exit_usage("\"down-srcip\" needs start and optional end address");
469 res
= terminate_connection_srcip(argv
[2], argc
> 3 ? argv
[3] : NULL
);
474 exit_usage("\"rekey\" needs a connection name");
476 res
= rekey_connection(argv
[2]);
481 exit_usage("\"route\" needs a connection name");
483 res
= route_connection(argv
[2]);
488 exit_usage("\"unroute\" needs a connection name");
490 res
= unroute_connection(argv
[2]);
492 case STROKE_LOGLEVEL
:
495 exit_usage("\"logtype\" needs more parameters...");
497 res
= set_loglevel(argv
[2], atoi(argv
[3]));
500 case STROKE_STATUSALL
:
501 res
= show_status(token
->kw
, argc
> 2 ? argv
[2] : NULL
);
503 case STROKE_LIST_PUBKEYS
:
504 case STROKE_LIST_CERTS
:
505 case STROKE_LIST_CACERTS
:
506 case STROKE_LIST_OCSPCERTS
:
507 case STROKE_LIST_AACERTS
:
508 case STROKE_LIST_ACERTS
:
509 case STROKE_LIST_CAINFOS
:
510 case STROKE_LIST_CRLS
:
511 case STROKE_LIST_OCSP
:
512 case STROKE_LIST_ALGS
:
513 case STROKE_LIST_ALL
:
514 res
= list(token
->kw
, argc
> 2 && strcmp(argv
[2], "--utc") == 0);
516 case STROKE_REREAD_SECRETS
:
517 case STROKE_REREAD_CACERTS
:
518 case STROKE_REREAD_OCSPCERTS
:
519 case STROKE_REREAD_AACERTS
:
520 case STROKE_REREAD_ACERTS
:
521 case STROKE_REREAD_CRLS
:
522 case STROKE_REREAD_ALL
:
523 res
= reread(token
->kw
);
525 case STROKE_PURGE_OCSP
:
526 case STROKE_PURGE_CRLS
:
527 case STROKE_PURGE_CERTS
:
528 case STROKE_PURGE_IKE
:
529 res
= purge(token
->kw
);
531 case STROKE_EXPORT_X509
:
534 exit_usage("\"exportx509\" needs a distinguished name");
536 res
= export(token
->kw
, argv
[2]);
539 res
= leases(token
->kw
, argc
> 2 ? argv
[2] : NULL
,
540 argc
> 3 ? argv
[3] : NULL
);
542 case STROKE_MEMUSAGE
: