d5357222f9a2edc306b8f23ebc55086a324fe0ef
[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
31 static char* push_string(stroke_msg_t *msg, char *string)
32 {
33 u_int string_start = msg->length;
34
35 if (string == NULL || msg->length + strlen(string) >= sizeof(stroke_msg_t))
36 {
37 return NULL;
38 }
39 else
40 {
41 msg->length += strlen(string) + 1;
42 strcpy((char*)msg + string_start, string);
43 return (char*)string_start;
44 }
45 }
46
47 static int send_stroke_msg (stroke_msg_t *msg)
48 {
49 struct sockaddr_un ctl_addr = { AF_UNIX, STROKE_SOCKET };
50 int sock;
51 char buffer[64];
52 int byte_count;
53
54 sock = socket(AF_UNIX, SOCK_STREAM, 0);
55 if (sock < 0)
56 {
57 fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno));
58 return -1;
59 }
60 if (connect(sock, (struct sockaddr *)&ctl_addr,
61 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
62 {
63 fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
64 close(sock);
65 return -1;
66 }
67
68 /* send message */
69 if (write(sock, msg, msg->length) != msg->length)
70 {
71 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
72 close(sock);
73 return -1;
74 }
75
76 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
77 {
78 buffer[byte_count] = '\0';
79 printf("%s", buffer);
80 }
81 if (byte_count < 0)
82 {
83 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
84 }
85
86 close(sock);
87 return 0;
88 }
89
90 static int add_connection(char *name,
91 char *my_id, char *other_id,
92 char *my_addr, char *other_addr,
93 char *my_net, char *other_net,
94 u_int my_netmask, u_int other_netmask)
95 {
96 stroke_msg_t msg;
97
98 msg.length = offsetof(stroke_msg_t, buffer);
99 msg.type = STR_ADD_CONN;
100
101 msg.add_conn.name = push_string(&msg, name);
102 msg.add_conn.ikev2 = 1;
103
104 msg.add_conn.rekey.ipsec_lifetime = 0;
105 msg.add_conn.rekey.ike_lifetime = 0;
106 msg.add_conn.rekey.margin = 0;
107 msg.add_conn.rekey.tries = 0;
108 msg.add_conn.rekey.fuzz = 0;
109
110 msg.add_conn.me.id = push_string(&msg, my_id);
111 msg.add_conn.me.address = push_string(&msg, my_addr);
112 msg.add_conn.me.subnet = push_string(&msg, my_net);
113 msg.add_conn.me.subnet_mask = my_netmask;
114 msg.add_conn.me.cert = NULL;
115 msg.add_conn.me.ca = NULL;
116 msg.add_conn.me.sendcert = CERT_SEND_IF_ASKED;
117
118 msg.add_conn.other.id = push_string(&msg, other_id);
119 msg.add_conn.other.address = push_string(&msg, other_addr);
120 msg.add_conn.other.subnet = push_string(&msg, other_net);
121 msg.add_conn.other.subnet_mask = other_netmask;
122 msg.add_conn.other.cert = NULL;
123 msg.add_conn.other.ca = NULL;
124 msg.add_conn.other.sendcert = CERT_SEND_IF_ASKED;
125
126 return send_stroke_msg(&msg);
127 }
128
129 static int del_connection(char *name)
130 {
131 stroke_msg_t msg;
132
133 msg.length = offsetof(stroke_msg_t, buffer);
134 msg.type = STR_DEL_CONN;
135 msg.initiate.name = push_string(&msg, name);
136 return send_stroke_msg(&msg);
137 }
138
139 static int initiate_connection(char *name)
140 {
141 stroke_msg_t msg;
142
143 msg.length = offsetof(stroke_msg_t, buffer);
144 msg.type = STR_INITIATE;
145 msg.initiate.name = push_string(&msg, name);
146 return send_stroke_msg(&msg);
147 }
148
149 static int terminate_connection(char *name)
150 {
151 stroke_msg_t msg;
152
153 msg.type = STR_TERMINATE;
154 msg.length = offsetof(stroke_msg_t, buffer);
155 msg.initiate.name = push_string(&msg, name);
156 return send_stroke_msg(&msg);
157 }
158
159 static int show_status(char *mode, char *connection)
160 {
161 stroke_msg_t msg;
162
163 if (strcmp(mode, "statusall") == 0)
164 msg.type = STR_STATUS_ALL;
165 else
166 msg.type = STR_STATUS;
167
168 msg.length = offsetof(stroke_msg_t, buffer);
169 msg.status.name = push_string(&msg, connection);
170 return send_stroke_msg(&msg);
171 }
172
173 static int list_certs(void)
174 {
175 stroke_msg_t msg;
176
177 msg.type = STR_LIST_CERTS;
178 msg.length = offsetof(stroke_msg_t, buffer);
179 return send_stroke_msg(&msg);
180 }
181
182 static int set_logtype(char *context, char *type, int enable)
183 {
184 stroke_msg_t msg;
185
186 msg.type = STR_LOGTYPE;
187 msg.length = offsetof(stroke_msg_t, buffer);
188 msg.logtype.context = push_string(&msg, context);
189 msg.logtype.type = push_string(&msg, type);
190 msg.logtype.enable = enable;
191 return send_stroke_msg(&msg);
192 }
193
194 static int set_loglevel(char *context, u_int level)
195 {
196 stroke_msg_t msg;
197
198 msg.type = STR_LOGLEVEL;
199 msg.length = offsetof(stroke_msg_t, buffer);
200 msg.loglevel.context = push_string(&msg, context);
201 msg.loglevel.level = level;
202 return send_stroke_msg(&msg);
203 }
204
205 static void exit_error(char *error)
206 {
207 if (error)
208 {
209 fprintf(stderr, "%s\n", error);
210 }
211 exit(-1);
212 }
213
214 static void exit_usage(char *error)
215 {
216 printf("Usage:\n");
217 printf(" Add a connection:\n");
218 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
219 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
220 printf(" where: ID is any IKEv2 ID \n");
221 printf(" ADDR is a IPv4 address\n");
222 printf(" NET is a IPv4 address of the subnet to tunnel\n");
223 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
224 printf(" Delete a connection:\n");
225 printf(" stroke delete NAME\n");
226 printf(" where: NAME is a connection name added with \"stroke add\"\n");
227 printf(" Initiate a connection:\n");
228 printf(" stroke up NAME\n");
229 printf(" where: NAME is a connection name added with \"stroke add\"\n");
230 printf(" Terminate a connection:\n");
231 printf(" stroke down NAME\n");
232 printf(" where: NAME is a connection name added with \"stroke add\"\n");
233 printf(" Set logtype for a logging context:\n");
234 printf(" stroke logtype CONTEXT TYPE ENABLE\n");
235 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
236 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
237 printf(" TYPE is CONTROL|ERROR|AUDIT|RAW|PRIVATE\n");
238 printf(" ENABLE is 0|1\n");
239 printf(" Set loglevel for a logging context:\n");
240 printf(" stroke loglevel CONTEXT LEVEL\n");
241 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
242 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
243 printf(" LEVEL is 0|1|2|3\n");
244 printf(" Show connection status:\n");
245 printf(" stroke status\n");
246 printf(" Show list of locally loaded certificates:\n");
247 printf(" stroke listcerts\n");
248 exit_error(error);
249 }
250
251 int main(int argc, char *argv[])
252 {
253 int res = 0;
254 char *op;
255
256 if (argc < 2)
257 {
258 exit_usage(NULL);
259 }
260
261 op = argv[1];
262
263 if (streq(op, "status") || streq(op, "statusall"))
264 {
265 res = show_status(op, argc > 2 ? argv[2] : NULL);
266 }
267 else if (streq(op, "listcerts") || streq(op, "listall"))
268 {
269 res = list_certs();
270 }
271 else if (streq(op, "up"))
272 {
273 if (argc < 3)
274 {
275 exit_usage("\"up\" needs a connection name");
276 }
277 res = initiate_connection(argv[2]);
278 }
279 else if (streq(op, "down"))
280 {
281 if (argc < 3)
282 {
283 exit_usage("\"down\" needs a connection name");
284 }
285 res = terminate_connection(argv[2]);
286 }
287 else if (streq(op, "add"))
288 {
289 if (argc < 11)
290 {
291 exit_usage("\"add\" needs more parameters...");
292 }
293 res = add_connection(argv[2],
294 argv[3], argv[4],
295 argv[5], argv[6],
296 argv[7], argv[8],
297 atoi(argv[9]), atoi(argv[10]));
298 }
299 else if (streq(op, "delete"))
300 {
301 if (argc < 3)
302 {
303 exit_usage("\"delete\" needs a connection name");
304 }
305 res = del_connection(argv[2]);
306 }
307 else if (streq(op, "logtype"))
308 {
309 if (argc < 5)
310 {
311 exit_usage("\"logtype\" needs more parameters...");
312 }
313 res = set_logtype(argv[2], argv[3], atoi(argv[4]));
314 }
315 else if (streq(op, "loglevel"))
316 {
317 if (argc < 4)
318 {
319 exit_usage("\"logtype\" needs more parameters...");
320 }
321 res = set_loglevel(argv[2], atoi(argv[3]));
322 }
323 else
324 {
325 exit_usage(NULL);
326 }
327
328 return res;
329 }