readded local_credential_store
[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.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.cert = NULL;
124 msg.add_conn.me.ca = NULL;
125 msg.add_conn.me.sendcert = 1;
126
127 msg.add_conn.other.id = push_string(&msg, other_id);
128 msg.add_conn.other.address = push_string(&msg, other_addr);
129 msg.add_conn.other.subnet = push_string(&msg, other_net);
130 msg.add_conn.other.subnet_mask = other_netmask;
131 msg.add_conn.other.cert = NULL;
132 msg.add_conn.other.ca = NULL;
133 msg.add_conn.other.sendcert = 1;
134
135 return send_stroke_msg(&msg);
136 }
137
138 static int del_connection(char *name)
139 {
140 stroke_msg_t msg;
141
142 msg.length = offsetof(stroke_msg_t, buffer);
143 msg.type = STR_DEL_CONN;
144 msg.initiate.name = push_string(&msg, name);
145 return send_stroke_msg(&msg);
146 }
147
148 static int initiate_connection(char *name)
149 {
150 stroke_msg_t msg;
151
152 msg.length = offsetof(stroke_msg_t, buffer);
153 msg.type = STR_INITIATE;
154 msg.initiate.name = push_string(&msg, name);
155 return send_stroke_msg(&msg);
156 }
157
158 static int terminate_connection(char *name)
159 {
160 stroke_msg_t msg;
161
162 msg.type = STR_TERMINATE;
163 msg.length = offsetof(stroke_msg_t, buffer);
164 msg.initiate.name = push_string(&msg, name);
165 return send_stroke_msg(&msg);
166 }
167
168 static int show_status(stroke_keyword_t kw, char *connection)
169 {
170 stroke_msg_t msg;
171
172 msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
173 msg.length = offsetof(stroke_msg_t, buffer);
174 msg.status.name = push_string(&msg, connection);
175 return send_stroke_msg(&msg);
176 }
177
178 static int list_flags[] = {
179 LIST_CERTS,
180 LIST_CACERTS,
181 LIST_CRLS,
182 LIST_ALL
183 };
184
185 static int list(stroke_keyword_t kw, bool utc)
186 {
187 stroke_msg_t msg;
188
189 msg.type = STR_LIST;
190 msg.length = offsetof(stroke_msg_t, buffer);
191 msg.list.utc = utc;
192 msg.list.flags = list_flags[kw - STROKE_LIST_FIRST];
193 return send_stroke_msg(&msg);
194 }
195
196 static int reread_flags[] = {
197 REREAD_CACERTS,
198 REREAD_CRLS,
199 REREAD_ALL
200 };
201
202 static int reread(stroke_keyword_t kw)
203 {
204 stroke_msg_t msg;
205
206 msg.type = STR_REREAD;
207 msg.length = offsetof(stroke_msg_t, buffer);
208 msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
209 return send_stroke_msg(&msg);
210 }
211
212 static int set_logtype(char *context, char *type, int enable)
213 {
214 stroke_msg_t msg;
215
216 msg.type = STR_LOGTYPE;
217 msg.length = offsetof(stroke_msg_t, buffer);
218 msg.logtype.context = push_string(&msg, context);
219 msg.logtype.type = push_string(&msg, type);
220 msg.logtype.enable = enable;
221 return send_stroke_msg(&msg);
222 }
223
224 static int set_loglevel(char *context, u_int level)
225 {
226 stroke_msg_t msg;
227
228 msg.type = STR_LOGLEVEL;
229 msg.length = offsetof(stroke_msg_t, buffer);
230 msg.loglevel.context = push_string(&msg, context);
231 msg.loglevel.level = level;
232 return send_stroke_msg(&msg);
233 }
234
235 static void exit_error(char *error)
236 {
237 if (error)
238 {
239 fprintf(stderr, "%s\n", error);
240 }
241 exit(-1);
242 }
243
244 static void exit_usage(char *error)
245 {
246 printf("Usage:\n");
247 printf(" Add a connection:\n");
248 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
249 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
250 printf(" where: ID is any IKEv2 ID \n");
251 printf(" ADDR is a IPv4 address\n");
252 printf(" NET is a IPv4 address of the subnet to tunnel\n");
253 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
254 printf(" Delete a connection:\n");
255 printf(" stroke delete NAME\n");
256 printf(" where: NAME is a connection name added with \"stroke add\"\n");
257 printf(" Initiate a connection:\n");
258 printf(" stroke up NAME\n");
259 printf(" where: NAME is a connection name added with \"stroke add\"\n");
260 printf(" Terminate a connection:\n");
261 printf(" stroke down NAME\n");
262 printf(" where: NAME is a connection name added with \"stroke add\"\n");
263 printf(" Set logtype for a logging context:\n");
264 printf(" stroke logtype CONTEXT TYPE ENABLE\n");
265 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
266 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
267 printf(" TYPE is CONTROL|ERROR|AUDIT|RAW|PRIVATE\n");
268 printf(" ENABLE is 0|1\n");
269 printf(" Set loglevel for a logging context:\n");
270 printf(" stroke loglevel CONTEXT LEVEL\n");
271 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
272 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
273 printf(" LEVEL is 0|1|2|3\n");
274 printf(" Show connection status:\n");
275 printf(" stroke status\n");
276 printf(" Show list of locally loaded certificates and crls:\n");
277 printf(" stroke listcerts|listcacerts|listcrls|listall\n");
278 printf(" Reload ca certificates and crls:\n");
279 printf(" stroke rereadcacerts|rereadcrls|rereadall\n");
280 exit_error(error);
281 }
282
283 int main(int argc, char *argv[])
284 {
285 const stroke_token_t *token;
286 int res = 0;
287
288 if (argc < 2)
289 {
290 exit_usage(NULL);
291 }
292
293 token = in_word_set(argv[1], strlen(argv[1]));
294
295 if (token == NULL)
296 {
297 exit_usage("unknown keyword");
298 }
299
300 switch (token->kw)
301 {
302 case STROKE_ADD:
303 if (argc < 11)
304 {
305 exit_usage("\"add\" needs more parameters...");
306 }
307 res = add_connection(argv[2],
308 argv[3], argv[4],
309 argv[5], argv[6],
310 argv[7], argv[8],
311 atoi(argv[9]), atoi(argv[10]));
312 break;
313 case STROKE_DELETE:
314 case STROKE_DEL:
315 if (argc < 3)
316 {
317 exit_usage("\"delete\" needs a connection name");
318 }
319 res = del_connection(argv[2]);
320 break;
321 case STROKE_UP:
322 if (argc < 3)
323 {
324 exit_usage("\"up\" needs a connection name");
325 }
326 res = initiate_connection(argv[2]);
327 break;
328 case STROKE_DOWN:
329 if (argc < 3)
330 {
331 exit_usage("\"down\" needs a connection name");
332 }
333 res = terminate_connection(argv[2]);
334 break;
335 case STROKE_LOGTYPE:
336 if (argc < 5)
337 {
338 exit_usage("\"logtype\" needs more parameters...");
339 }
340 res = set_logtype(argv[2], argv[3], atoi(argv[4]));
341 break;
342 case STROKE_LOGLEVEL:
343 if (argc < 4)
344 {
345 exit_usage("\"logtype\" needs more parameters...");
346 }
347 res = set_loglevel(argv[2], atoi(argv[3]));
348 break;
349 case STROKE_STATUS:
350 case STROKE_STATUSALL:
351 res = show_status(token->kw, argc > 2 ? argv[2] : NULL);
352 break;
353 case STROKE_LIST_CERTS:
354 case STROKE_LIST_CACERTS:
355 case STROKE_LIST_CRLS:
356 case STROKE_LIST_ALL:
357 res = list(token->kw, argc > 2 && streq(argv[2], "--utc"));
358 break;
359 case STROKE_REREAD_CACERTS:
360 case STROKE_REREAD_CRLS:
361 case STROKE_REREAD_ALL:
362 res = reread(token->kw);
363 break;
364 default:
365 exit_usage(NULL);
366 }
367 return res;
368 }