added hostaccess support
[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 <linux/stddef.h>
26
27 #include <types.h>
28
29 #include "stroke.h"
30 #include "stroke_keywords.h"
31
32 struct stroke_token {
33 char *name;
34 stroke_keyword_t kw;
35 };
36
37 static char* push_string(stroke_msg_t *msg, char *string)
38 {
39 u_int string_start = msg->length;
40
41 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
42 {
43 return NULL;
44 }
45 else
46 {
47 msg->length += strlen(string) + 1;
48 strcpy((char*)msg + string_start, string);
49 return (char*)string_start;
50 }
51 }
52
53 static int send_stroke_msg (stroke_msg_t *msg)
54 {
55 struct sockaddr_un ctl_addr = { AF_UNIX, STROKE_SOCKET };
56 int sock;
57 char buffer[64];
58 int byte_count;
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
110 msg.add_conn.rekey.ipsec_lifetime = 0;
111 msg.add_conn.rekey.ike_lifetime = 0;
112 msg.add_conn.rekey.margin = 0;
113 msg.add_conn.rekey.tries = 0;
114 msg.add_conn.rekey.fuzz = 0;
115
116 msg.add_conn.algorithms.ike = NULL;
117 msg.add_conn.algorithms.esp = NULL;
118
119 msg.add_conn.dpd.delay = 0;
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.subnet = push_string(&msg, my_net);
125 msg.add_conn.me.subnet_mask = my_netmask;
126 msg.add_conn.me.cert = NULL;
127 msg.add_conn.me.ca = NULL;
128 msg.add_conn.me.sendcert = 1;
129 msg.add_conn.me.hostaccess = FALSE;
130 msg.add_conn.me.protocol = 0;
131 msg.add_conn.me.port = 0;
132
133 msg.add_conn.other.id = push_string(&msg, other_id);
134 msg.add_conn.other.address = push_string(&msg, other_addr);
135 msg.add_conn.other.subnet = push_string(&msg, other_net);
136 msg.add_conn.other.subnet_mask = other_netmask;
137 msg.add_conn.other.cert = NULL;
138 msg.add_conn.other.ca = NULL;
139 msg.add_conn.other.sendcert = 1;
140 msg.add_conn.other.hostaccess = FALSE;
141 msg.add_conn.other.protocol = 0;
142 msg.add_conn.other.port = 0;
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 route_connection(char *name)
178 {
179 stroke_msg_t msg;
180
181 msg.type = STR_ROUTE;
182 msg.length = offsetof(stroke_msg_t, buffer);
183 msg.route.name = push_string(&msg, name);
184 return send_stroke_msg(&msg);
185 }
186
187 static int unroute_connection(char *name)
188 {
189 stroke_msg_t msg;
190
191 msg.type = STR_UNROUTE;
192 msg.length = offsetof(stroke_msg_t, buffer);
193 msg.unroute.name = push_string(&msg, name);
194 return send_stroke_msg(&msg);
195 }
196
197 static int show_status(stroke_keyword_t kw, char *connection)
198 {
199 stroke_msg_t msg;
200
201 msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
202 msg.length = offsetof(stroke_msg_t, buffer);
203 msg.status.name = push_string(&msg, connection);
204 return send_stroke_msg(&msg);
205 }
206
207 static int list_flags[] = {
208 LIST_CERTS,
209 LIST_CACERTS,
210 LIST_CRLS,
211 LIST_ALL
212 };
213
214 static int list(stroke_keyword_t kw, bool utc)
215 {
216 stroke_msg_t msg;
217
218 msg.type = STR_LIST;
219 msg.length = offsetof(stroke_msg_t, buffer);
220 msg.list.utc = utc;
221 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
222 return send_stroke_msg(&msg);
223 }
224
225 static int reread_flags[] = {
226 REREAD_CACERTS,
227 REREAD_CRLS,
228 REREAD_ALL
229 };
230
231 static int reread(stroke_keyword_t kw)
232 {
233 stroke_msg_t msg;
234
235 msg.type = STR_REREAD;
236 msg.length = offsetof(stroke_msg_t, buffer);
237 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
238 return send_stroke_msg(&msg);
239 }
240
241 static int set_logtype(char *context, char *type, int enable)
242 {
243 stroke_msg_t msg;
244
245 msg.type = STR_LOGTYPE;
246 msg.length = offsetof(stroke_msg_t, buffer);
247 msg.logtype.context = push_string(&msg, context);
248 msg.logtype.type = push_string(&msg, type);
249 msg.logtype.enable = enable;
250 return send_stroke_msg(&msg);
251 }
252
253 static int set_loglevel(char *context, u_int level)
254 {
255 stroke_msg_t msg;
256
257 msg.type = STR_LOGLEVEL;
258 msg.length = offsetof(stroke_msg_t, buffer);
259 msg.loglevel.context = push_string(&msg, context);
260 msg.loglevel.level = level;
261 return send_stroke_msg(&msg);
262 }
263
264 static void exit_error(char *error)
265 {
266 if (error)
267 {
268 fprintf(stderr, "%s\n", error);
269 }
270 exit(-1);
271 }
272
273 static void exit_usage(char *error)
274 {
275 printf("Usage:\n");
276 printf(" Add a connection:\n");
277 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
278 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
279 printf(" where: ID is any IKEv2 ID \n");
280 printf(" ADDR is a IPv4 address\n");
281 printf(" NET is a IPv4 address of the subnet to tunnel\n");
282 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
283 printf(" Delete a connection:\n");
284 printf(" stroke delete NAME\n");
285 printf(" where: NAME is a connection name added with \"stroke add\"\n");
286 printf(" Initiate a connection:\n");
287 printf(" stroke up NAME\n");
288 printf(" where: NAME is a connection name added with \"stroke add\"\n");
289 printf(" Terminate a connection:\n");
290 printf(" stroke down NAME\n");
291 printf(" where: NAME is a connection name added with \"stroke add\"\n");
292 printf(" Set logtype for a logging context:\n");
293 printf(" stroke logtype CONTEXT TYPE ENABLE\n");
294 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
295 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
296 printf(" TYPE is CONTROL|ERROR|AUDIT|RAW|PRIVATE\n");
297 printf(" ENABLE is 0|1\n");
298 printf(" Set loglevel for a logging context:\n");
299 printf(" stroke loglevel CONTEXT LEVEL\n");
300 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
301 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
302 printf(" LEVEL is 0|1|2|3\n");
303 printf(" Show connection status:\n");
304 printf(" stroke status\n");
305 printf(" Show list of locally loaded certificates and crls:\n");
306 printf(" stroke listcerts|listcacerts|listcrls|listall\n");
307 printf(" Reload ca certificates and crls:\n");
308 printf(" stroke rereadcacerts|rereadcrls|rereadall\n");
309 exit_error(error);
310 }
311
312 int main(int argc, char *argv[])
313 {
314 const stroke_token_t *token;
315 int res = 0;
316
317 if (argc < 2)
318 {
319 exit_usage(NULL);
320 }
321
322 token = in_word_set(argv[1], strlen(argv[1]));
323
324 if (token == NULL)
325 {
326 exit_usage("unknown keyword");
327 }
328
329 switch (token->kw)
330 {
331 case STROKE_ADD:
332 if (argc < 11)
333 {
334 exit_usage("\"add\" needs more parameters...");
335 }
336 res = add_connection(argv[2],
337 argv[3], argv[4],
338 argv[5], argv[6],
339 argv[7], argv[8],
340 atoi(argv[9]), atoi(argv[10]));
341 break;
342 case STROKE_DELETE:
343 case STROKE_DEL:
344 if (argc < 3)
345 {
346 exit_usage("\"delete\" needs a connection name");
347 }
348 res = del_connection(argv[2]);
349 break;
350 case STROKE_UP:
351 if (argc < 3)
352 {
353 exit_usage("\"up\" needs a connection name");
354 }
355 res = initiate_connection(argv[2]);
356 break;
357 case STROKE_DOWN:
358 if (argc < 3)
359 {
360 exit_usage("\"down\" needs a connection name");
361 }
362 res = terminate_connection(argv[2]);
363 break;
364 case STROKE_ROUTE:
365 if (argc < 3)
366 {
367 exit_usage("\"route\" needs a connection name");
368 }
369 res = route_connection(argv[2]);
370 break;
371 case STROKE_UNROUTE:
372 if (argc < 3)
373 {
374 exit_usage("\"unroute\" needs a connection name");
375 }
376 res = unroute_connection(argv[2]);
377 break;
378 case STROKE_LOGTYPE:
379 if (argc < 5)
380 {
381 exit_usage("\"logtype\" needs more parameters...");
382 }
383 res = set_logtype(argv[2], argv[3], atoi(argv[4]));
384 break;
385 case STROKE_LOGLEVEL:
386 if (argc < 4)
387 {
388 exit_usage("\"logtype\" needs more parameters...");
389 }
390 res = set_loglevel(argv[2], atoi(argv[3]));
391 break;
392 case STROKE_STATUS:
393 case STROKE_STATUSALL:
394 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
395 break;
396 case STROKE_LIST_CERTS:
397 case STROKE_LIST_CACERTS:
398 case STROKE_LIST_CRLS:
399 case STROKE_LIST_ALL:
400 res = list(token->kw, argc > 2 && streq(argv[2], "--utc"));
401 break;
402 case STROKE_REREAD_CACERTS:
403 case STROKE_REREAD_CRLS:
404 case STROKE_REREAD_ALL:
405 res = reread(token->kw);
406 break;
407 default:
408 exit_usage(NULL);
409 }
410 return res;
411 }