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