added mobike=yes|no connection option
[strongswan.git] / src / stroke / stroke.c
1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include <stdlib.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <sys/fcntl.h>
21 #include <unistd.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stddef.h>
26
27 #include "stroke.h"
28 #include "stroke_keywords.h"
29
30 struct stroke_token {
31 char *name;
32 stroke_keyword_t kw;
33 };
34
35 static char* push_string(stroke_msg_t *msg, char *string)
36 {
37 unsigned long string_start = msg->length;
38
39 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
40 {
41 return NULL;
42 }
43 else
44 {
45 msg->length += strlen(string) + 1;
46 strcpy((char*)msg + string_start, string);
47 return (char*)string_start;
48 }
49 }
50
51 static int send_stroke_msg (stroke_msg_t *msg)
52 {
53 struct sockaddr_un ctl_addr = { AF_UNIX, STROKE_SOCKET };
54 int sock;
55 char buffer[64];
56 int byte_count;
57
58 msg->output_verbosity = 1; /* CONTROL */
59
60 sock = socket(AF_UNIX, SOCK_STREAM, 0);
61 if (sock < 0)
62 {
63 fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
64 return -1;
65 }
66 if (connect(sock, (struct sockaddr *)&ctl_addr,
67 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
68 {
69 fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
70 close(sock);
71 return -1;
72 }
73
74 /* send message */
75 if (write(sock, msg, msg->length) != msg->length)
76 {
77 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
78 close(sock);
79 return -1;
80 }
81
82 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
83 {
84 buffer[byte_count] = '\0';
85 printf("%s", buffer);
86 }
87 if (byte_count < 0)
88 {
89 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
90 }
91
92 close(sock);
93 return 0;
94 }
95
96 static int add_connection(char *name,
97 char *my_id, char *other_id,
98 char *my_addr, char *other_addr,
99 char *my_net, char *other_net,
100 u_int my_netmask, u_int other_netmask)
101 {
102 stroke_msg_t msg;
103
104 msg.length = offsetof(stroke_msg_t, buffer);
105 msg.type = STR_ADD_CONN;
106
107 msg.add_conn.name = push_string(&msg, name);
108 msg.add_conn.ikev2 = 1;
109 msg.add_conn.auth_method = 2;
110 msg.add_conn.eap_type = 0;
111 msg.add_conn.mode = 1;
112 msg.add_conn.mobike = 1;
113
114 msg.add_conn.rekey.reauth = 0;
115 msg.add_conn.rekey.ipsec_lifetime = 0;
116 msg.add_conn.rekey.ike_lifetime = 0;
117 msg.add_conn.rekey.margin = 0;
118 msg.add_conn.rekey.tries = 0;
119 msg.add_conn.rekey.fuzz = 0;
120
121 msg.add_conn.algorithms.ike = NULL;
122 msg.add_conn.algorithms.esp = NULL;
123
124 msg.add_conn.dpd.delay = 0;
125 msg.add_conn.dpd.action = 1;
126
127 msg.add_conn.me.id = push_string(&msg, my_id);
128 msg.add_conn.me.address = push_string(&msg, my_addr);
129 msg.add_conn.me.subnet = push_string(&msg, my_net);
130 msg.add_conn.me.subnet_mask = my_netmask;
131 msg.add_conn.me.sourceip = NULL;
132 msg.add_conn.me.virtual_ip = 0;
133 msg.add_conn.me.cert = NULL;
134 msg.add_conn.me.ca = NULL;
135 msg.add_conn.me.sendcert = 1;
136 msg.add_conn.me.hostaccess = 0;
137 msg.add_conn.me.tohost = 0;
138 msg.add_conn.me.protocol = 0;
139 msg.add_conn.me.port = 0;
140
141 msg.add_conn.other.id = push_string(&msg, other_id);
142 msg.add_conn.other.address = push_string(&msg, other_addr);
143 msg.add_conn.other.subnet = push_string(&msg, other_net);
144 msg.add_conn.other.subnet_mask = other_netmask;
145 msg.add_conn.other.sourceip = NULL;
146 msg.add_conn.other.virtual_ip = 0;
147 msg.add_conn.other.cert = NULL;
148 msg.add_conn.other.ca = NULL;
149 msg.add_conn.other.sendcert = 1;
150 msg.add_conn.other.hostaccess = 0;
151 msg.add_conn.other.tohost = 0;
152 msg.add_conn.other.protocol = 0;
153 msg.add_conn.other.port = 0;
154
155 return send_stroke_msg(&msg);
156 }
157
158 static int del_connection(char *name)
159 {
160 stroke_msg_t msg;
161
162 msg.length = offsetof(stroke_msg_t, buffer);
163 msg.type = STR_DEL_CONN;
164 msg.initiate.name = push_string(&msg, name);
165 return send_stroke_msg(&msg);
166 }
167
168 static int initiate_connection(char *name)
169 {
170 stroke_msg_t msg;
171
172 msg.length = offsetof(stroke_msg_t, buffer);
173 msg.type = STR_INITIATE;
174 msg.initiate.name = push_string(&msg, name);
175 return send_stroke_msg(&msg);
176 }
177
178 static int terminate_connection(char *name)
179 {
180 stroke_msg_t msg;
181
182 msg.type = STR_TERMINATE;
183 msg.length = offsetof(stroke_msg_t, buffer);
184 msg.initiate.name = push_string(&msg, name);
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_CERTS,
220 LIST_CACERTS,
221 LIST_OCSPCERTS,
222 LIST_AACERTS,
223 LIST_ACERTS,
224 LIST_GROUPS,
225 LIST_CAINFOS,
226 LIST_CRLS,
227 LIST_OCSP,
228 LIST_ALL
229 };
230
231 static int list(stroke_keyword_t kw, int utc)
232 {
233 stroke_msg_t msg;
234
235 msg.type = STR_LIST;
236 msg.length = offsetof(stroke_msg_t, buffer);
237 msg.list.utc = utc;
238 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
239 return send_stroke_msg(&msg);
240 }
241
242 static int reread_flags[] = {
243 REREAD_SECRETS,
244 REREAD_CACERTS,
245 REREAD_OCSPCERTS,
246 REREAD_AACERTS,
247 REREAD_ACERTS,
248 REREAD_CRLS,
249 REREAD_ALL
250 };
251
252 static int reread(stroke_keyword_t kw)
253 {
254 stroke_msg_t msg;
255
256 msg.type = STR_REREAD;
257 msg.length = offsetof(stroke_msg_t, buffer);
258 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
259 return send_stroke_msg(&msg);
260 }
261
262 static int purge_flags[] = {
263 PURGE_OCSP
264 };
265
266 static int purge(stroke_keyword_t kw)
267 {
268 stroke_msg_t msg;
269
270 msg.type = STR_PURGE;
271 msg.length = offsetof(stroke_msg_t, buffer);
272 msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
273 return send_stroke_msg(&msg);
274 }
275
276 static int set_loglevel(char *type, u_int level)
277 {
278 stroke_msg_t msg;
279
280 msg.type = STR_LOGLEVEL;
281 msg.length = offsetof(stroke_msg_t, buffer);
282 msg.loglevel.type = push_string(&msg, type);
283 msg.loglevel.level = level;
284 return send_stroke_msg(&msg);
285 }
286
287 static void exit_error(char *error)
288 {
289 if (error)
290 {
291 fprintf(stderr, "%s\n", error);
292 }
293 exit(-1);
294 }
295
296 static void exit_usage(char *error)
297 {
298 printf("Usage:\n");
299 printf(" Add a connection:\n");
300 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
301 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
302 printf(" where: ID is any IKEv2 ID \n");
303 printf(" ADDR is a IPv4 address\n");
304 printf(" NET is a IPv4 address of the subnet to tunnel\n");
305 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
306 printf(" Delete a connection:\n");
307 printf(" stroke delete NAME\n");
308 printf(" where: NAME is a connection name added with \"stroke add\"\n");
309 printf(" Initiate a connection:\n");
310 printf(" stroke up NAME\n");
311 printf(" where: NAME is a connection name added with \"stroke add\"\n");
312 printf(" Terminate a connection:\n");
313 printf(" stroke down NAME\n");
314 printf(" where: NAME is a connection name added with \"stroke add\"\n");
315 printf(" Set loglevel for a logging type:\n");
316 printf(" stroke loglevel TYPE LEVEL\n");
317 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
318 printf(" LEVEL is -1|0|1|2|3|4\n");
319 printf(" Show connection status:\n");
320 printf(" stroke status\n");
321 printf(" Show list of authority and attribute certificates:\n");
322 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
323 printf(" Show list of end entity certificates, ca info records and crls:\n");
324 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
325 printf(" Reload authority and attribute certificates:\n");
326 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
327 printf(" Reload secrets and crls:\n");
328 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
329 printf(" Purge ocsp cache entries:\n");
330 printf(" stroke purgeocsp\n");
331 exit_error(error);
332 }
333
334 int main(int argc, char *argv[])
335 {
336 const stroke_token_t *token;
337 int res = 0;
338
339 if (argc < 2)
340 {
341 exit_usage(NULL);
342 }
343
344 token = in_word_set(argv[1], strlen(argv[1]));
345
346 if (token == NULL)
347 {
348 exit_usage("unknown keyword");
349 }
350
351 switch (token->kw)
352 {
353 case STROKE_ADD:
354 if (argc < 11)
355 {
356 exit_usage("\"add\" needs more parameters...");
357 }
358 res = add_connection(argv[2],
359 argv[3], argv[4],
360 argv[5], argv[6],
361 argv[7], argv[8],
362 atoi(argv[9]), atoi(argv[10]));
363 break;
364 case STROKE_DELETE:
365 case STROKE_DEL:
366 if (argc < 3)
367 {
368 exit_usage("\"delete\" needs a connection name");
369 }
370 res = del_connection(argv[2]);
371 break;
372 case STROKE_UP:
373 if (argc < 3)
374 {
375 exit_usage("\"up\" needs a connection name");
376 }
377 res = initiate_connection(argv[2]);
378 break;
379 case STROKE_DOWN:
380 if (argc < 3)
381 {
382 exit_usage("\"down\" needs a connection name");
383 }
384 res = terminate_connection(argv[2]);
385 break;
386 case STROKE_ROUTE:
387 if (argc < 3)
388 {
389 exit_usage("\"route\" needs a connection name");
390 }
391 res = route_connection(argv[2]);
392 break;
393 case STROKE_UNROUTE:
394 if (argc < 3)
395 {
396 exit_usage("\"unroute\" needs a connection name");
397 }
398 res = unroute_connection(argv[2]);
399 break;
400 case STROKE_LOGLEVEL:
401 if (argc < 4)
402 {
403 exit_usage("\"logtype\" needs more parameters...");
404 }
405 res = set_loglevel(argv[2], atoi(argv[3]));
406 break;
407 case STROKE_STATUS:
408 case STROKE_STATUSALL:
409 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
410 break;
411 case STROKE_LIST_CERTS:
412 case STROKE_LIST_CACERTS:
413 case STROKE_LIST_OCSPCERTS:
414 case STROKE_LIST_AACERTS:
415 case STROKE_LIST_ACERTS:
416 case STROKE_LIST_CAINFOS:
417 case STROKE_LIST_CRLS:
418 case STROKE_LIST_OCSP:
419 case STROKE_LIST_ALL:
420 res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
421 break;
422 case STROKE_REREAD_SECRETS:
423 case STROKE_REREAD_CACERTS:
424 case STROKE_REREAD_OCSPCERTS:
425 case STROKE_REREAD_AACERTS:
426 case STROKE_REREAD_ACERTS:
427 case STROKE_REREAD_CRLS:
428 case STROKE_REREAD_ALL:
429 res = reread(token->kw);
430 break;
431 case STROKE_PURGE_OCSP:
432 res = purge(token->kw);
433 break;
434 default:
435 exit_usage(NULL);
436 }
437 return res;
438 }