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