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