- added separate implementation for connection_store, credential_store, policy_store
[strongswan.git] / Source / 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()
150 {
151 stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
152 int res;
153
154 msg->length = sizeof(stroke_msg_t);
155 msg->type = STR_STATUS;
156 res = send_stroke_msg(msg);
157 free(msg);
158 return res;
159 }
160
161 static int set_logtype(char *context, char *type, int enable)
162 {
163 stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
164 int res;
165
166 msg->length = sizeof(stroke_msg_t);
167 msg->type = STR_LOGTYPE;
168 msg->logtype.context = push_string(&msg, context);
169 msg->logtype.type = push_string(&msg, type);
170 msg->logtype.enable = enable;
171 res = send_stroke_msg(msg);
172 free(msg);
173 return res;
174 }
175
176 static int set_loglevel(char *context, u_int level)
177 {
178 stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
179 int res;
180
181 msg->length = sizeof(stroke_msg_t);
182 msg->type = STR_LOGLEVEL;
183 msg->loglevel.context = push_string(&msg, context);
184 msg->loglevel.level = level;
185 res = send_stroke_msg(msg);
186 free(msg);
187 return res;
188 }
189
190 static void exit_error(char *error)
191 {
192 if (error)
193 {
194 fprintf(stderr, "%s\n", error);
195 }
196 exit(-1);
197 }
198
199 static void exit_usage(char *error)
200 {
201 printf("Usage:\n");
202 printf(" Add a connection:\n");
203 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
204 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
205 printf(" where: ID is any IKEv2 ID \n");
206 printf(" ADDR is a IPv4 address\n");
207 printf(" NET is a IPv4 address of the subnet to tunnel\n");
208 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
209 printf(" Initiate a connection:\n");
210 printf(" stroke up NAME\n");
211 printf(" where: NAME is a connection name added with \"stroke add\"\n");
212 printf(" Terminate a connection:\n");
213 printf(" stroke down NAME\n");
214 printf(" where: NAME is a connection name added with \"stroke add\"\n");
215 printf(" Set logtype for a logging context:\n");
216 printf(" stroke logtype CONTEXT TYPE ENABLE\n");
217 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
218 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
219 printf(" TYPE is CONTROL|ERROR|AUDIT|RAW|PRIVATE\n");
220 printf(" ENABLE is 0|1\n");
221 printf(" Set loglevel for a logging context:\n");
222 printf(" stroke loglevel CONTEXT LEVEL\n");
223 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
224 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
225 printf(" LEVEL is 0|1|2|3\n");
226 printf(" Show connection status:\n");
227 printf(" stroke status\n");
228 exit_error(error);
229 }
230
231 int main(int argc, char *argv[])
232 {
233 int res;
234
235 if (argc < 2)
236 {
237 exit_usage(NULL);
238 }
239
240 if (strcmp(argv[1], "status") == 0 ||
241 strcmp(argv[1], "statusall") == 0)
242 {
243 res = show_status();
244 }
245
246 else if (strcmp(argv[1], "up") == 0)
247 {
248 if (argc < 3)
249 {
250 exit_usage("\"up\" needs a connection name");
251 }
252 res = initiate_connection(argv[2]);
253 }
254 else if (strcmp(argv[1], "down") == 0)
255 {
256 if (argc < 3)
257 {
258 exit_usage("\"down\" needs a connection name");
259 }
260 res = terminate_connection(argv[2]);
261 }
262 else if (strcmp(argv[1], "add") == 0)
263 {
264 if (argc < 11)
265 {
266 exit_usage("\"add\" needs more parameters...");
267 }
268 res = add_connection(argv[2],
269 argv[3], argv[4],
270 argv[5], argv[6],
271 argv[7], argv[8],
272 atoi(argv[9]), atoi(argv[10]));
273 }
274 else if (strcmp(argv[1], "logtype") == 0)
275 {
276 if (argc < 5)
277 {
278 exit_usage("\"logtype\" needs more parameters...");
279 }
280 res = set_logtype(argv[2], argv[3], atoi(argv[4]));
281 }
282 else if (strcmp(argv[1], "loglevel") == 0)
283 {
284 if (argc < 4)
285 {
286 exit_usage("\"logtype\" needs more parameters...");
287 }
288 res = set_loglevel(argv[2], atoi(argv[3]));
289 }
290 else
291 {
292 exit_usage(NULL);
293 }
294
295 return res;
296 }