Show contents of the CP payload in message_t stringification
[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;
60 char buffer[512];
61 int byte_count;
62
63 ctl_addr.sun_family = AF_UNIX;
64 strcpy(ctl_addr.sun_path, STROKE_SOCKET);
65
66 msg->output_verbosity = 1; /* CONTROL */
67
68 sock = socket(AF_UNIX, SOCK_STREAM, 0);
69 if (sock < 0)
70 {
71 fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
72 return -1;
73 }
74 if (connect(sock, (struct sockaddr *)&ctl_addr,
75 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
76 {
77 fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
78 close(sock);
79 return -1;
80 }
81
82 /* send message */
83 if (write(sock, msg, msg->length) != msg->length)
84 {
85 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
86 close(sock);
87 return -1;
88 }
89
90 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
91 {
92 buffer[byte_count] = '\0';
93 printf("%s", buffer);
94
95 /* we prompt if we receive the "Passphrase:" magic keyword */
96 if (byte_count >= 12 &&
97 strcmp(buffer + byte_count - 12, "Passphrase:\n") == 0)
98 {
99 if (fgets(buffer, sizeof(buffer), stdin))
100 {
101 ignore_result(write(sock, buffer, strlen(buffer)));
102 }
103 }
104 }
105 if (byte_count < 0)
106 {
107 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
108 }
109
110 close(sock);
111 return 0;
112 }
113
114 static int add_connection(char *name,
115 char *my_id, char *other_id,
116 char *my_addr, char *other_addr,
117 char *my_nets, char *other_nets)
118 {
119 stroke_msg_t msg;
120
121 memset(&msg, 0, sizeof(msg));
122 msg.length = offsetof(stroke_msg_t, buffer);
123 msg.type = STR_ADD_CONN;
124
125 msg.add_conn.name = push_string(&msg, name);
126 msg.add_conn.ikev2 = 1;
127 msg.add_conn.auth_method = 2;
128 msg.add_conn.mode = 1;
129 msg.add_conn.mobike = 1;
130 msg.add_conn.dpd.action = 1;
131
132 msg.add_conn.me.id = push_string(&msg, my_id);
133 msg.add_conn.me.address = push_string(&msg, my_addr);
134 msg.add_conn.me.ikeport = 500;
135 msg.add_conn.me.subnets = push_string(&msg, my_nets);
136 msg.add_conn.me.sendcert = 1;
137
138 msg.add_conn.other.id = push_string(&msg, other_id);
139 msg.add_conn.other.address = push_string(&msg, other_addr);
140 msg.add_conn.other.ikeport = 500;
141 msg.add_conn.other.subnets = push_string(&msg, other_nets);
142 msg.add_conn.other.sendcert = 1;
143
144 return send_stroke_msg(&msg);
145 }
146
147 static int del_connection(char *name)
148 {
149 stroke_msg_t msg;
150
151 msg.length = offsetof(stroke_msg_t, buffer);
152 msg.type = STR_DEL_CONN;
153 msg.initiate.name = push_string(&msg, name);
154 return send_stroke_msg(&msg);
155 }
156
157 static int initiate_connection(char *name)
158 {
159 stroke_msg_t msg;
160
161 msg.length = offsetof(stroke_msg_t, buffer);
162 msg.type = STR_INITIATE;
163 msg.initiate.name = push_string(&msg, name);
164 return send_stroke_msg(&msg);
165 }
166
167 static int terminate_connection(char *name)
168 {
169 stroke_msg_t msg;
170
171 msg.type = STR_TERMINATE;
172 msg.length = offsetof(stroke_msg_t, buffer);
173 msg.initiate.name = push_string(&msg, name);
174 return send_stroke_msg(&msg);
175 }
176
177 static int terminate_connection_srcip(char *start, char *end)
178 {
179 stroke_msg_t msg;
180
181 msg.type = STR_TERMINATE_SRCIP;
182 msg.length = offsetof(stroke_msg_t, buffer);
183 msg.terminate_srcip.start = push_string(&msg, start);
184 msg.terminate_srcip.end = push_string(&msg, end);
185 return send_stroke_msg(&msg);
186 }
187
188 static int route_connection(char *name)
189 {
190 stroke_msg_t msg;
191
192 msg.type = STR_ROUTE;
193 msg.length = offsetof(stroke_msg_t, buffer);
194 msg.route.name = push_string(&msg, name);
195 return send_stroke_msg(&msg);
196 }
197
198 static int unroute_connection(char *name)
199 {
200 stroke_msg_t msg;
201
202 msg.type = STR_UNROUTE;
203 msg.length = offsetof(stroke_msg_t, buffer);
204 msg.unroute.name = push_string(&msg, name);
205 return send_stroke_msg(&msg);
206 }
207
208 static int show_status(stroke_keyword_t kw, char *connection)
209 {
210 stroke_msg_t msg;
211
212 msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
213 msg.length = offsetof(stroke_msg_t, buffer);
214 msg.status.name = push_string(&msg, connection);
215 return send_stroke_msg(&msg);
216 }
217
218 static int list_flags[] = {
219 LIST_PUBKEYS,
220 LIST_CERTS,
221 LIST_CACERTS,
222 LIST_OCSPCERTS,
223 LIST_AACERTS,
224 LIST_ACERTS,
225 LIST_GROUPS,
226 LIST_CAINFOS,
227 LIST_CRLS,
228 LIST_OCSP,
229 LIST_ALGS,
230 LIST_ALL
231 };
232
233 static int list(stroke_keyword_t kw, int utc)
234 {
235 stroke_msg_t msg;
236
237 msg.type = STR_LIST;
238 msg.length = offsetof(stroke_msg_t, buffer);
239 msg.list.utc = utc;
240 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
241 return send_stroke_msg(&msg);
242 }
243
244 static int reread_flags[] = {
245 REREAD_SECRETS,
246 REREAD_CACERTS,
247 REREAD_OCSPCERTS,
248 REREAD_AACERTS,
249 REREAD_ACERTS,
250 REREAD_CRLS,
251 REREAD_ALL
252 };
253
254 static int reread(stroke_keyword_t kw)
255 {
256 stroke_msg_t msg;
257
258 msg.type = STR_REREAD;
259 msg.length = offsetof(stroke_msg_t, buffer);
260 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
261 return send_stroke_msg(&msg);
262 }
263
264 static int purge_flags[] = {
265 PURGE_OCSP,
266 PURGE_IKE,
267 };
268
269 static int purge(stroke_keyword_t kw)
270 {
271 stroke_msg_t msg;
272
273 msg.type = STR_PURGE;
274 msg.length = offsetof(stroke_msg_t, buffer);
275 msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
276 return send_stroke_msg(&msg);
277 }
278
279 static int leases(stroke_keyword_t kw, char *pool, char *address)
280 {
281
282 stroke_msg_t msg;
283
284 msg.type = STR_LEASES;
285 msg.length = offsetof(stroke_msg_t, buffer);
286 msg.leases.pool = push_string(&msg, pool);
287 msg.leases.address = push_string(&msg, address);
288 return send_stroke_msg(&msg);
289 }
290
291 static int set_loglevel(char *type, u_int level)
292 {
293 stroke_msg_t msg;
294
295 msg.type = STR_LOGLEVEL;
296 msg.length = offsetof(stroke_msg_t, buffer);
297 msg.loglevel.type = push_string(&msg, type);
298 msg.loglevel.level = level;
299 return send_stroke_msg(&msg);
300 }
301
302 static void exit_error(char *error)
303 {
304 if (error)
305 {
306 fprintf(stderr, "%s\n", error);
307 }
308 exit(-1);
309 }
310
311 static void exit_usage(char *error)
312 {
313 printf("Usage:\n");
314 printf(" Add a connection:\n");
315 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
316 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
317 printf(" where: ID is any IKEv2 ID \n");
318 printf(" ADDR is a IPv4 address\n");
319 printf(" NET is a IPv4 subnet in CIDR notation\n");
320 printf(" Delete a connection:\n");
321 printf(" stroke delete NAME\n");
322 printf(" where: NAME is a connection name added with \"stroke add\"\n");
323 printf(" Initiate a connection:\n");
324 printf(" stroke up NAME\n");
325 printf(" where: NAME is a connection name added with \"stroke add\"\n");
326 printf(" Terminate a connection:\n");
327 printf(" stroke down NAME\n");
328 printf(" where: NAME is a connection name added with \"stroke add\"\n");
329 printf(" Terminate a connection by remote srcip:\n");
330 printf(" stroke down-srcip START [END]\n");
331 printf(" where: START and optional END define the clients source IP\n");
332 printf(" Set loglevel for a logging type:\n");
333 printf(" stroke loglevel TYPE LEVEL\n");
334 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
335 printf(" LEVEL is -1|0|1|2|3|4\n");
336 printf(" Show connection status:\n");
337 printf(" stroke status\n");
338 printf(" Show list of authority and attribute certificates:\n");
339 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
340 printf(" Show list of end entity certificates, ca info records and crls:\n");
341 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
342 printf(" Show list of supported algorithms:\n");
343 printf(" stroke listalgs\n");
344 printf(" Reload authority and attribute certificates:\n");
345 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
346 printf(" Reload secrets and crls:\n");
347 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
348 printf(" Purge ocsp cache entries:\n");
349 printf(" stroke purgeocsp\n");
350 printf(" Purge IKE_SAs without a CHILD_SA:\n");
351 printf(" stroke purgeike\n");
352 printf(" Show leases of a pool:\n");
353 printf(" stroke leases [POOL [ADDRESS]]\n");
354 exit_error(error);
355 }
356
357 int main(int argc, char *argv[])
358 {
359 const stroke_token_t *token;
360 int res = 0;
361
362 library_init(NULL);
363 atexit(library_deinit);
364
365 if (argc < 2)
366 {
367 exit_usage(NULL);
368 }
369
370 token = in_word_set(argv[1], strlen(argv[1]));
371
372 if (token == NULL)
373 {
374 exit_usage("unknown keyword");
375 }
376
377 switch (token->kw)
378 {
379 case STROKE_ADD:
380 if (argc < 11)
381 {
382 exit_usage("\"add\" needs more parameters...");
383 }
384 res = add_connection(argv[2],
385 argv[3], argv[4],
386 argv[5], argv[6],
387 argv[7], argv[8]);
388 break;
389 case STROKE_DELETE:
390 case STROKE_DEL:
391 if (argc < 3)
392 {
393 exit_usage("\"delete\" needs a connection name");
394 }
395 res = del_connection(argv[2]);
396 break;
397 case STROKE_UP:
398 if (argc < 3)
399 {
400 exit_usage("\"up\" needs a connection name");
401 }
402 res = initiate_connection(argv[2]);
403 break;
404 case STROKE_DOWN:
405 if (argc < 3)
406 {
407 exit_usage("\"down\" needs a connection name");
408 }
409 res = terminate_connection(argv[2]);
410 break;
411 case STROKE_DOWN_SRCIP:
412 if (argc < 3)
413 {
414 exit_usage("\"down-srcip\" needs start and optional end address");
415 }
416 res = terminate_connection_srcip(argv[2], argc > 3 ? argv[3] : NULL);
417 break;
418 case STROKE_ROUTE:
419 if (argc < 3)
420 {
421 exit_usage("\"route\" needs a connection name");
422 }
423 res = route_connection(argv[2]);
424 break;
425 case STROKE_UNROUTE:
426 if (argc < 3)
427 {
428 exit_usage("\"unroute\" needs a connection name");
429 }
430 res = unroute_connection(argv[2]);
431 break;
432 case STROKE_LOGLEVEL:
433 if (argc < 4)
434 {
435 exit_usage("\"logtype\" needs more parameters...");
436 }
437 res = set_loglevel(argv[2], atoi(argv[3]));
438 break;
439 case STROKE_STATUS:
440 case STROKE_STATUSALL:
441 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
442 break;
443 case STROKE_LIST_PUBKEYS:
444 case STROKE_LIST_CERTS:
445 case STROKE_LIST_CACERTS:
446 case STROKE_LIST_OCSPCERTS:
447 case STROKE_LIST_AACERTS:
448 case STROKE_LIST_ACERTS:
449 case STROKE_LIST_CAINFOS:
450 case STROKE_LIST_CRLS:
451 case STROKE_LIST_OCSP:
452 case STROKE_LIST_ALGS:
453 case STROKE_LIST_ALL:
454 res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
455 break;
456 case STROKE_REREAD_SECRETS:
457 case STROKE_REREAD_CACERTS:
458 case STROKE_REREAD_OCSPCERTS:
459 case STROKE_REREAD_AACERTS:
460 case STROKE_REREAD_ACERTS:
461 case STROKE_REREAD_CRLS:
462 case STROKE_REREAD_ALL:
463 res = reread(token->kw);
464 break;
465 case STROKE_PURGE_OCSP:
466 case STROKE_PURGE_IKE:
467 res = purge(token->kw);
468 break;
469 case STROKE_LEASES:
470 res = leases(token->kw, argc > 2 ? argv[2] : NULL,
471 argc > 3 ? argv[3] : NULL);
472 break;
473 default:
474 exit_usage(NULL);
475 }
476 return res;
477 }