implementation of an CFG attribute framework, currently supporting virtual IPs
[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
31 #include "stroke_msg.h"
32 #include "stroke_keywords.h"
33
34 struct stroke_token {
35 char *name;
36 stroke_keyword_t kw;
37 };
38
39 static char* push_string(stroke_msg_t *msg, char *string)
40 {
41 unsigned long string_start = msg->length;
42
43 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
44 {
45 return NULL;
46 }
47 else
48 {
49 msg->length += strlen(string) + 1;
50 strcpy((char*)msg + string_start, string);
51 return (char*)string_start;
52 }
53 }
54
55 static int send_stroke_msg (stroke_msg_t *msg)
56 {
57 struct sockaddr_un ctl_addr = { AF_UNIX, STROKE_SOCKET };
58 int sock;
59 char buffer[64];
60 int byte_count;
61
62 msg->output_verbosity = 1; /* CONTROL */
63
64 sock = socket(AF_UNIX, SOCK_STREAM, 0);
65 if (sock < 0)
66 {
67 fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
68 return -1;
69 }
70 if (connect(sock, (struct sockaddr *)&ctl_addr,
71 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
72 {
73 fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
74 close(sock);
75 return -1;
76 }
77
78 /* send message */
79 if (write(sock, msg, msg->length) != msg->length)
80 {
81 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
82 close(sock);
83 return -1;
84 }
85
86 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
87 {
88 buffer[byte_count] = '\0';
89 printf("%s", buffer);
90 }
91 if (byte_count < 0)
92 {
93 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
94 }
95
96 close(sock);
97 return 0;
98 }
99
100 static int add_connection(char *name,
101 char *my_id, char *other_id,
102 char *my_addr, char *other_addr,
103 char *my_net, char *other_net,
104 u_int my_netmask, u_int other_netmask)
105 {
106 stroke_msg_t msg;
107
108 memset(&msg, 0, sizeof(msg));
109 msg.length = offsetof(stroke_msg_t, buffer);
110 msg.type = STR_ADD_CONN;
111
112 msg.add_conn.name = push_string(&msg, name);
113 msg.add_conn.ikev2 = 1;
114 msg.add_conn.auth_method = 2;
115 msg.add_conn.mode = 1;
116 msg.add_conn.mobike = 1;
117 msg.add_conn.dpd.action = 1;
118
119 msg.add_conn.me.id = push_string(&msg, my_id);
120 msg.add_conn.me.address = push_string(&msg, my_addr);
121 msg.add_conn.me.subnet = push_string(&msg, my_net);
122 msg.add_conn.me.subnet_mask = my_netmask;
123 msg.add_conn.me.sendcert = 1;
124
125 msg.add_conn.other.id = push_string(&msg, other_id);
126 msg.add_conn.other.address = push_string(&msg, other_addr);
127 msg.add_conn.other.subnet = push_string(&msg, other_net);
128 msg.add_conn.other.subnet_mask = other_netmask;
129 msg.add_conn.other.sendcert = 1;
130
131 return send_stroke_msg(&msg);
132 }
133
134 static int del_connection(char *name)
135 {
136 stroke_msg_t msg;
137
138 msg.length = offsetof(stroke_msg_t, buffer);
139 msg.type = STR_DEL_CONN;
140 msg.initiate.name = push_string(&msg, name);
141 return send_stroke_msg(&msg);
142 }
143
144 static int initiate_connection(char *name)
145 {
146 stroke_msg_t msg;
147
148 msg.length = offsetof(stroke_msg_t, buffer);
149 msg.type = STR_INITIATE;
150 msg.initiate.name = push_string(&msg, name);
151 return send_stroke_msg(&msg);
152 }
153
154 static int terminate_connection(char *name)
155 {
156 stroke_msg_t msg;
157
158 msg.type = STR_TERMINATE;
159 msg.length = offsetof(stroke_msg_t, buffer);
160 msg.initiate.name = push_string(&msg, name);
161 return send_stroke_msg(&msg);
162 }
163
164 static int route_connection(char *name)
165 {
166 stroke_msg_t msg;
167
168 msg.type = STR_ROUTE;
169 msg.length = offsetof(stroke_msg_t, buffer);
170 msg.route.name = push_string(&msg, name);
171 return send_stroke_msg(&msg);
172 }
173
174 static int unroute_connection(char *name)
175 {
176 stroke_msg_t msg;
177
178 msg.type = STR_UNROUTE;
179 msg.length = offsetof(stroke_msg_t, buffer);
180 msg.unroute.name = push_string(&msg, name);
181 return send_stroke_msg(&msg);
182 }
183
184 static int show_status(stroke_keyword_t kw, char *connection)
185 {
186 stroke_msg_t msg;
187
188 msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
189 msg.length = offsetof(stroke_msg_t, buffer);
190 msg.status.name = push_string(&msg, connection);
191 return send_stroke_msg(&msg);
192 }
193
194 static int list_flags[] = {
195 LIST_CERTS,
196 LIST_CACERTS,
197 LIST_OCSPCERTS,
198 LIST_AACERTS,
199 LIST_ACERTS,
200 LIST_GROUPS,
201 LIST_CAINFOS,
202 LIST_CRLS,
203 LIST_OCSP,
204 LIST_ALL
205 };
206
207 static int list(stroke_keyword_t kw, int utc)
208 {
209 stroke_msg_t msg;
210
211 msg.type = STR_LIST;
212 msg.length = offsetof(stroke_msg_t, buffer);
213 msg.list.utc = utc;
214 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
215 return send_stroke_msg(&msg);
216 }
217
218 static int reread_flags[] = {
219 REREAD_SECRETS,
220 REREAD_CACERTS,
221 REREAD_OCSPCERTS,
222 REREAD_AACERTS,
223 REREAD_ACERTS,
224 REREAD_CRLS,
225 REREAD_ALL
226 };
227
228 static int reread(stroke_keyword_t kw)
229 {
230 stroke_msg_t msg;
231
232 msg.type = STR_REREAD;
233 msg.length = offsetof(stroke_msg_t, buffer);
234 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
235 return send_stroke_msg(&msg);
236 }
237
238 static int purge_flags[] = {
239 PURGE_OCSP
240 };
241
242 static int purge(stroke_keyword_t kw)
243 {
244 stroke_msg_t msg;
245
246 msg.type = STR_PURGE;
247 msg.length = offsetof(stroke_msg_t, buffer);
248 msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
249 return send_stroke_msg(&msg);
250 }
251
252 static int set_loglevel(char *type, u_int level)
253 {
254 stroke_msg_t msg;
255
256 msg.type = STR_LOGLEVEL;
257 msg.length = offsetof(stroke_msg_t, buffer);
258 msg.loglevel.type = push_string(&msg, type);
259 msg.loglevel.level = level;
260 return send_stroke_msg(&msg);
261 }
262
263 static void exit_error(char *error)
264 {
265 if (error)
266 {
267 fprintf(stderr, "%s\n", error);
268 }
269 exit(-1);
270 }
271
272 static void exit_usage(char *error)
273 {
274 printf("Usage:\n");
275 printf(" Add a connection:\n");
276 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
277 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
278 printf(" where: ID is any IKEv2 ID \n");
279 printf(" ADDR is a IPv4 address\n");
280 printf(" NET is a IPv4 address of the subnet to tunnel\n");
281 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
282 printf(" Delete a connection:\n");
283 printf(" stroke delete NAME\n");
284 printf(" where: NAME is a connection name added with \"stroke add\"\n");
285 printf(" Initiate a connection:\n");
286 printf(" stroke up NAME\n");
287 printf(" where: NAME is a connection name added with \"stroke add\"\n");
288 printf(" Terminate a connection:\n");
289 printf(" stroke down NAME\n");
290 printf(" where: NAME is a connection name added with \"stroke add\"\n");
291 printf(" Set loglevel for a logging type:\n");
292 printf(" stroke loglevel TYPE LEVEL\n");
293 printf(" where: TYPE is any|dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib\n");
294 printf(" LEVEL is -1|0|1|2|3|4\n");
295 printf(" Show connection status:\n");
296 printf(" stroke status\n");
297 printf(" Show list of authority and attribute certificates:\n");
298 printf(" stroke listcacerts|listocspcerts|listaacerts|listacerts\n");
299 printf(" Show list of end entity certificates, ca info records and crls:\n");
300 printf(" stroke listcerts|listcainfos|listcrls|listall\n");
301 printf(" Reload authority and attribute certificates:\n");
302 printf(" stroke rereadcacerts|rereadocspcerts|rereadaacerts|rereadacerts\n");
303 printf(" Reload secrets and crls:\n");
304 printf(" stroke rereadsecrets|rereadcrls|rereadall\n");
305 printf(" Purge ocsp cache entries:\n");
306 printf(" stroke purgeocsp\n");
307 exit_error(error);
308 }
309
310 int main(int argc, char *argv[])
311 {
312 const stroke_token_t *token;
313 int res = 0;
314
315 if (argc < 2)
316 {
317 exit_usage(NULL);
318 }
319
320 token = in_word_set(argv[1], strlen(argv[1]));
321
322 if (token == NULL)
323 {
324 exit_usage("unknown keyword");
325 }
326
327 switch (token->kw)
328 {
329 case STROKE_ADD:
330 if (argc < 11)
331 {
332 exit_usage("\"add\" needs more parameters...");
333 }
334 res = add_connection(argv[2],
335 argv[3], argv[4],
336 argv[5], argv[6],
337 argv[7], argv[8],
338 atoi(argv[9]), atoi(argv[10]));
339 break;
340 case STROKE_DELETE:
341 case STROKE_DEL:
342 if (argc < 3)
343 {
344 exit_usage("\"delete\" needs a connection name");
345 }
346 res = del_connection(argv[2]);
347 break;
348 case STROKE_UP:
349 if (argc < 3)
350 {
351 exit_usage("\"up\" needs a connection name");
352 }
353 res = initiate_connection(argv[2]);
354 break;
355 case STROKE_DOWN:
356 if (argc < 3)
357 {
358 exit_usage("\"down\" needs a connection name");
359 }
360 res = terminate_connection(argv[2]);
361 break;
362 case STROKE_ROUTE:
363 if (argc < 3)
364 {
365 exit_usage("\"route\" needs a connection name");
366 }
367 res = route_connection(argv[2]);
368 break;
369 case STROKE_UNROUTE:
370 if (argc < 3)
371 {
372 exit_usage("\"unroute\" needs a connection name");
373 }
374 res = unroute_connection(argv[2]);
375 break;
376 case STROKE_LOGLEVEL:
377 if (argc < 4)
378 {
379 exit_usage("\"logtype\" needs more parameters...");
380 }
381 res = set_loglevel(argv[2], atoi(argv[3]));
382 break;
383 case STROKE_STATUS:
384 case STROKE_STATUSALL:
385 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
386 break;
387 case STROKE_LIST_CERTS:
388 case STROKE_LIST_CACERTS:
389 case STROKE_LIST_OCSPCERTS:
390 case STROKE_LIST_AACERTS:
391 case STROKE_LIST_ACERTS:
392 case STROKE_LIST_CAINFOS:
393 case STROKE_LIST_CRLS:
394 case STROKE_LIST_OCSP:
395 case STROKE_LIST_ALL:
396 res = list(token->kw, argc > 2 && strcmp(argv[2], "--utc") == 0);
397 break;
398 case STROKE_REREAD_SECRETS:
399 case STROKE_REREAD_CACERTS:
400 case STROKE_REREAD_OCSPCERTS:
401 case STROKE_REREAD_AACERTS:
402 case STROKE_REREAD_ACERTS:
403 case STROKE_REREAD_CRLS:
404 case STROKE_REREAD_ALL:
405 res = reread(token->kw);
406 break;
407 case STROKE_PURGE_OCSP:
408 res = purge(token->kw);
409 break;
410 default:
411 exit_usage(NULL);
412 }
413 return res;
414 }