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