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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* strongSwan whack functions to communicate with pluto (whack.c)
* Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
*
* 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.
*
* RCSID $Id: starterwhack.c,v 1.17 2006/04/17 10:32:36 as Exp $
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/stddef.h>
#include <unistd.h>
#include <errno.h>
#include <freeswan.h>
#include "../pluto/constants.h"
#include "../pluto/defs.h"
#include "../pluto/log.h"
#include "../pluto/whack.h"
#include "starterwhack.h"
#include "confread.h"
#include "files.h"
static int
pack_str (char **p, char **next, char **roof)
{
const char *s = (*p==NULL) ? "" : *p; /* note: NULL becomes ""! */
size_t len = strlen(s) + 1;
if ((*roof - *next) < len)
{
return 0; /* not enough space */
}
else
{
strcpy(*next, s);
*next += len;
*p = NULL; /* don't send pointers on the wire! */
return 1;
}
}
static int
send_whack_msg (whack_message_t *msg)
{
struct sockaddr_un ctl_addr = { AF_UNIX, PLUTO_CTL_FILE };
int sock;
ssize_t len;
char *str_next, *str_roof;
/* pack strings */
str_next = (char *)msg->string;
str_roof = (char *)&msg->string[sizeof(msg->string)];
if (!pack_str(&msg->name, &str_next, &str_roof)
|| !pack_str(&msg->left.id, &str_next, &str_roof)
|| !pack_str(&msg->left.cert, &str_next, &str_roof)
|| !pack_str(&msg->left.ca, &str_next, &str_roof)
|| !pack_str(&msg->left.groups, &str_next, &str_roof)
|| !pack_str(&msg->left.updown, &str_next, &str_roof)
#ifdef VIRTUAL_IP
|| !pack_str(&msg->left.virt, &str_next, &str_roof)
#endif
|| !pack_str(&msg->right.id, &str_next, &str_roof)
|| !pack_str(&msg->right.cert, &str_next, &str_roof)
|| !pack_str(&msg->right.ca, &str_next, &str_roof)
|| !pack_str(&msg->right.groups, &str_next, &str_roof)
|| !pack_str(&msg->right.updown, &str_next, &str_roof)
#ifdef VIRTUAL_IP
|| !pack_str(&msg->right.virt, &str_next, &str_roof)
#endif
|| !pack_str(&msg->keyid, &str_next, &str_roof)
|| !pack_str(&msg->myid, &str_next, &str_roof)
|| !pack_str(&msg->cacert, &str_next, &str_roof)
|| !pack_str(&msg->ldaphost, &str_next, &str_roof)
|| !pack_str(&msg->ldapbase, &str_next, &str_roof)
|| !pack_str(&msg->crluri, &str_next, &str_roof)
|| !pack_str(&msg->crluri2, &str_next, &str_roof)
|| !pack_str(&msg->ocspuri, &str_next, &str_roof)
|| !pack_str(&msg->ike, &str_next, &str_roof)
|| !pack_str(&msg->esp, &str_next, &str_roof)
|| !pack_str(&msg->sc_data, &str_next, &str_roof)
|| (str_roof - str_next < msg->keyval.len))
{
plog("send_wack_msg(): can't pack strings");
return -1;
}
if (msg->keyval.ptr)
memcpy(str_next, msg->keyval.ptr, msg->keyval.len);
msg->keyval.ptr = NULL;
str_next += msg->keyval.len;
len = str_next - (char *)msg;
/* connect to pluto ctl */
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
plog("socket() failed: %s", strerror(errno));
return -1;
}
if (connect(sock, (struct sockaddr *)&ctl_addr,
offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
{
plog("connect(pluto_ctl) failed: %s", strerror(errno));
close(sock);
return -1;
}
/* send message */
if (write(sock, msg, len) != len)
{
plog("write(pluto_ctl) failed: %s", strerror(errno));
close(sock);
return -1;
}
/* TODO: read reply */
close(sock);
return 0;
}
static void
init_whack_msg(whack_message_t *msg)
{
memset(msg, 0, sizeof(whack_message_t));
msg->magic = WHACK_MAGIC;
}
static char *
connection_name(starter_conn_t *conn)
{
/* if connection name is '%auto', create a new name like conn_xxxxx */
static char buf[32];
if (streq(conn->name, "%auto"))
{
sprintf(buf, "conn_%ld", conn->id);
return buf;
}
return conn->name;
}
static void
set_whack_end(whack_end_t *w, starter_end_t *end)
{
w->id = end->id;
w->cert = end->cert;
w->ca = end->ca;
w->groups = end->groups;
w->host_addr = end->addr;
w->host_nexthop = end->nexthop;
w->host_srcip = end->srcip;
if (end->has_client)
w->client = end->subnet;
else
w->client.addr.u.v4.sin_family = AF_INET;
w->has_client = end->has_client;
w->has_client_wildcard = end->has_client_wildcard;
w->has_port_wildcard = end->has_port_wildcard;
w->has_srcip = end->has_srcip;
w->modecfg = end->modecfg;
w->hostaccess = end->hostaccess;
w->sendcert = end->sendcert;
w->updown = end->updown;
w->host_port = IKE_UDP_PORT;
w->port = end->port;
w->protocol = end->protocol;
w->virt = end->virt;
if (w->port != 0)
{
int port = htons(w->port);
setportof(port, &w->host_addr);
setportof(port, &w->client.addr);
}
}
static int
starter_whack_add_pubkey (starter_conn_t *conn, starter_end_t *end
, const char *lr)
{
const char *err;
static char keyspace[1024 + 4];
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_key = TRUE;
msg.pubkey_alg = PUBKEY_ALG_RSA;
if (end->id && end->rsakey)
{
/* special values to ignore */
if (streq(end->rsakey, "")
|| streq(end->rsakey, "%none")
|| streq(end->rsakey, "%cert")
|| streq(end->rsakey, "0x00"))
{
return 0;
}
msg.keyid = end->id;
err = atobytes(end->rsakey, 0, keyspace, sizeof(keyspace), &msg.keyval.len);
if (err)
{
plog("conn %s/%s: rsakey malformed [%s]", connection_name(conn), lr, err);
return 1;
}
else
{
msg.keyval.ptr = keyspace;
return send_whack_msg(&msg);
}
}
return 0;
}
int
starter_whack_add_conn(starter_conn_t *conn)
{
whack_message_t msg;
int r;
init_whack_msg(&msg);
msg.whack_connection = TRUE;
msg.name = connection_name(conn);
msg.addr_family = conn->addr_family;
msg.tunnel_addr_family = conn->tunnel_addr_family;
msg.sa_ike_life_seconds = conn->sa_ike_life_seconds;
msg.sa_ipsec_life_seconds = conn->sa_ipsec_life_seconds;
msg.sa_rekey_margin = conn->sa_rekey_margin;
msg.sa_rekey_fuzz = conn->sa_rekey_fuzz;
msg.sa_keying_tries = conn->sa_keying_tries;
msg.policy = conn->policy;
set_whack_end(&msg.left, &conn->left);
set_whack_end(&msg.right, &conn->right);
msg.esp = conn->esp;
msg.ike = conn->ike;
msg.pfsgroup = conn->pfsgroup;
/* taken from pluto/whack.c */
if (msg.pfsgroup)
{
char esp_buf[256];
snprintf(esp_buf, sizeof (esp_buf), "%s;%s"
, msg.esp ? msg.esp : ""
, msg.pfsgroup ? msg.pfsgroup : "");
msg.esp = esp_buf;
DBG(DBG_CONTROL,
DBG_log("Setting --esp=%s", msg.esp)
)
}
msg.dpd_delay = conn->dpd_delay;
msg.dpd_timeout = conn->dpd_timeout;
msg.dpd_action = conn->dpd_action;
/* msg.dpd_count = conn->dpd_count; not supported yet by strongSwan */
r = send_whack_msg(&msg);
if (r == 0 && (conn->policy & POLICY_RSASIG))
{
r += starter_whack_add_pubkey (conn, &conn->left, "left");
r += starter_whack_add_pubkey (conn, &conn->right, "right");
}
return r;
}
int
starter_whack_del_conn(starter_conn_t *conn)
{
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_delete = TRUE;
msg.name = connection_name(conn);
return send_whack_msg(&msg);
}
int
starter_whack_route_conn(starter_conn_t *conn)
{
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_route = TRUE;
msg.name = connection_name(conn);
return send_whack_msg(&msg);
}
int
starter_whack_initiate_conn(starter_conn_t *conn)
{
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_initiate = TRUE;
msg.whack_async = TRUE;
msg.name = connection_name(conn);
return send_whack_msg(&msg);
}
int
starter_whack_listen(void)
{
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_listen = TRUE;
return send_whack_msg(&msg);
}
int starter_whack_shutdown(void)
{
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_shutdown = TRUE;
return send_whack_msg(&msg);
}
int
starter_whack_add_ca(starter_ca_t *ca)
{
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_ca = TRUE;
msg.name = ca->name;
msg.cacert = ca->cacert;
msg.ldaphost = ca->ldaphost;
msg.ldapbase = ca->ldapbase;
msg.crluri = ca->crluri;
msg.crluri2 = ca->crluri2;
msg.ocspuri = ca->ocspuri;
msg.whack_strict = ca->strict;
return send_whack_msg(&msg);
}
int
starter_whack_del_ca(starter_ca_t *ca)
{
whack_message_t msg;
init_whack_msg(&msg);
msg.whack_delete = TRUE;
msg.whack_ca = TRUE;
msg.name = ca->name;
return send_whack_msg(&msg);
}
|