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