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