32e628df1c8171b8c60cd82107fdf64c1c418e83
[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 route_connection(char *name)
201 {
202 stroke_msg_t msg;
203
204 msg.type = STR_ROUTE;
205 msg.length = offsetof(stroke_msg_t, buffer);
206 msg.route.name = push_string(&msg, name);
207 return send_stroke_msg(&msg);
208 }
209
210 static int unroute_connection(char *name)
211 {
212 stroke_msg_t msg;
213
214 msg.type = STR_UNROUTE;
215 msg.length = offsetof(stroke_msg_t, buffer);
216 msg.unroute.name = push_string(&msg, name);
217 return send_stroke_msg(&msg);
218 }
219
220 static int show_status(stroke_keyword_t kw, char *connection)
221 {
222 stroke_msg_t msg;
223
224 msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
225 msg.length = offsetof(stroke_msg_t, buffer);
226 msg.status.name = push_string(&msg, connection);
227 return send_stroke_msg(&msg);
228 }
229
230 static int list_flags[] = {
231 LIST_PUBKEYS,
232 LIST_CERTS,
233 LIST_CACERTS,
234 LIST_OCSPCERTS,
235 LIST_AACERTS,
236 LIST_ACERTS,
237 LIST_GROUPS,
238 LIST_CAINFOS,
239 LIST_CRLS,
240 LIST_OCSP,
241 LIST_ALGS,
242 LIST_ALL
243 };
244
245 static int list(stroke_keyword_t kw, int utc)
246 {
247 stroke_msg_t msg;
248
249 msg.type = STR_LIST;
250 msg.length = offsetof(stroke_msg_t, buffer);
251 msg.list.utc = utc;
252 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
253 return send_stroke_msg(&msg);
254 }
255
256 static int reread_flags[] = {
257 REREAD_SECRETS,
258 REREAD_CACERTS,
259 REREAD_OCSPCERTS,
260 REREAD_AACERTS,
261 REREAD_ACERTS,
262 REREAD_CRLS,
263 REREAD_ALL
264 };
265
266 static int reread(stroke_keyword_t kw)
267 {
268 stroke_msg_t msg;
269
270 msg.type = STR_REREAD;
271 msg.length = offsetof(stroke_msg_t, buffer);
272 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
273 return send_stroke_msg(&msg);
274 }
275
276 static int purge_flags[] = {
277 PURGE_OCSP,
278 PURGE_IKE,
279 };
280
281 static int purge(stroke_keyword_t kw)
282 {
283 stroke_msg_t msg;
284
285 msg.type = STR_PURGE;
286 msg.length = offsetof(stroke_msg_t, buffer);
287 msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
288 return send_stroke_msg(&msg);
289 }
290
291 static int leases(stroke_keyword_t kw, char *pool, char *address)
292 {
293
294 stroke_msg_t msg;
295
296 msg.type = STR_LEASES;
297 msg.length = offsetof(stroke_msg_t, buffer);
298 msg.leases.pool = push_string(&msg, pool);
299 msg.leases.address = push_string(&msg, address);
300 return send_stroke_msg(&msg);
301 }
302
303 static int set_loglevel(char *type, u_int level)
304 {
305 stroke_msg_t msg;
306
307 msg.type = STR_LOGLEVEL;
308 msg.length = offsetof(stroke_msg_t, buffer);
309 msg.loglevel.type = push_string(&msg, type);
310 msg.loglevel.level = level;
311 return send_stroke_msg(&msg);
312 }
313
314 static void exit_error(char *error)
315 {
316 if (error)
317 {
318 fprintf(stderr, "%s\n", error);
319 }
320 exit(-1);
321 }
322
323 static void exit_usage(char *error)
324 {
325 printf("Usage:\n");
326 printf(" Add a connection:\n");
327 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
328 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
329 printf(" where: ID is any IKEv2 ID \n");
330 printf(" ADDR is a IPv4 address\n");
331 printf(" NET is a IPv4 subnet in CIDR notation\n");
332 printf(" Delete a connection:\n");
333 printf(" stroke delete NAME\n");
334 printf(" where: NAME is a connection name added with \"stroke add\"\n");
335 printf(" Initiate a connection:\n");
336 printf(" stroke up NAME\n");
337 printf(" where: NAME is a connection name added with \"stroke add\"\n");
338 printf(" Terminate a connection:\n");
339 printf(" stroke down NAME\n");
340 printf(" where: NAME is a connection name added with \"stroke add\"\n");
341 printf(" Terminate a connection by remote srcip:\n");
342 printf(" stroke down-srcip START [END]\n");
343 printf(" where: START and optional END define the clients source IP\n");
344 printf(" Set loglevel for a logging type:\n");
345 printf(" stroke loglevel TYPE LEVEL\n");
346 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
347 printf(" LEVEL is -1|0|1|2|3|4\n");
348 printf(" Show connection status:\n");
349 printf(" stroke status\n");
350 printf(" Show list of authority and attribute certificates:\n");
351 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
352 printf(" Show list of end entity certificates, ca info records and crls:\n");
353 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
354 printf(" Show list of supported algorithms:\n");
355 printf(" stroke listalgs\n");
356 printf(" Reload authority and attribute certificates:\n");
357 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
358 printf(" Reload secrets and crls:\n");
359 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
360 printf(" Purge ocsp cache entries:\n");
361 printf(" stroke purgeocsp\n");
362 printf(" Purge IKE_SAs without a CHILD_SA:\n");
363 printf(" stroke purgeike\n");
364 printf(" Show leases of a pool:\n");
365 printf(" stroke leases [POOL [ADDRESS]]\n");
366 exit_error(error);
367 }
368
369 int main(int argc, char *argv[])
370 {
371 const stroke_token_t *token;
372 int res = 0;
373
374 library_init(NULL);
375 atexit(library_deinit);
376
377 if (argc < 2)
378 {
379 exit_usage(NULL);
380 }
381
382 token = in_word_set(argv[1], strlen(argv[1]));
383
384 if (token == NULL)
385 {
386 exit_usage("unknown keyword");
387 }
388
389 switch (token->kw)
390 {
391 case STROKE_ADD:
392 if (argc < 11)
393 {
394 exit_usage("\"add\" needs more parameters...");
395 }
396 res = add_connection(argv[2],
397 argv[3], argv[4],
398 argv[5], argv[6],
399 argv[7], argv[8]);
400 break;
401 case STROKE_DELETE:
402 case STROKE_DEL:
403 if (argc < 3)
404 {
405 exit_usage("\"delete\" needs a connection name");
406 }
407 res = del_connection(argv[2]);
408 break;
409 case STROKE_UP:
410 if (argc < 3)
411 {
412 exit_usage("\"up\" needs a connection name");
413 }
414 res = initiate_connection(argv[2]);
415 break;
416 case STROKE_DOWN:
417 if (argc < 3)
418 {
419 exit_usage("\"down\" needs a connection name");
420 }
421 res = terminate_connection(argv[2]);
422 break;
423 case STROKE_DOWN_SRCIP:
424 if (argc < 3)
425 {
426 exit_usage("\"down-srcip\" needs start and optional end address");
427 }
428 res = terminate_connection_srcip(argv[2], argc > 3 ? argv[3] : NULL);
429 break;
430 case STROKE_ROUTE:
431 if (argc < 3)
432 {
433 exit_usage("\"route\" needs a connection name");
434 }
435 res = route_connection(argv[2]);
436 break;
437 case STROKE_UNROUTE:
438 if (argc < 3)
439 {
440 exit_usage("\"unroute\" needs a connection name");
441 }
442 res = unroute_connection(argv[2]);
443 break;
444 case STROKE_LOGLEVEL:
445 if (argc < 4)
446 {
447 exit_usage("\"logtype\" needs more parameters...");
448 }
449 res = set_loglevel(argv[2], atoi(argv[3]));
450 break;
451 case STROKE_STATUS:
452 case STROKE_STATUSALL:
453 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
454 break;
455 case STROKE_LIST_PUBKEYS:
456 case STROKE_LIST_CERTS:
457 case STROKE_LIST_CACERTS:
458 case STROKE_LIST_OCSPCERTS:
459 case STROKE_LIST_AACERTS:
460 case STROKE_LIST_ACERTS:
461 case STROKE_LIST_CAINFOS:
462 case STROKE_LIST_CRLS:
463 case STROKE_LIST_OCSP:
464 case STROKE_LIST_ALGS:
465 case STROKE_LIST_ALL:
466 res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
467 break;
468 case STROKE_REREAD_SECRETS:
469 case STROKE_REREAD_CACERTS:
470 case STROKE_REREAD_OCSPCERTS:
471 case STROKE_REREAD_AACERTS:
472 case STROKE_REREAD_ACERTS:
473 case STROKE_REREAD_CRLS:
474 case STROKE_REREAD_ALL:
475 res = reread(token->kw);
476 break;
477 case STROKE_PURGE_OCSP:
478 case STROKE_PURGE_IKE:
479 res = purge(token->kw);
480 break;
481 case STROKE_LEASES:
482 res = leases(token->kw, argc > 2 ? argv[2] : NULL,
483 argc > 3 ? argv[3] : NULL);
484 break;
485 default:
486 exit_usage(NULL);
487 }
488 return res;
489 }