added missing equals() method assignment for ID_ANY identities
[strongswan.git] / src / starter / starterwhack.c
1 /* strongSwan whack functions to communicate with pluto (whack.c)
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
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 <sys/types.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #include <stddef.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #include <freeswan.h>
24
25 #include <constants.h>
26 #include <defs.h>
27 #include <log.h>
28 #include <whack.h>
29
30 #include "starterwhack.h"
31 #include "confread.h"
32 #include "files.h"
33
34 #define ip_version(string) (strchr(string, '.') ? AF_INET : AF_INET6)
35
36 static int
37 pack_str (char **p, char **next, char **roof)
38 {
39 const char *s = (*p==NULL) ? "" : *p; /* note: NULL becomes ""! */
40 size_t len = strlen(s) + 1;
41
42 if ((*roof - *next) < len)
43 {
44 return 0; /* not enough space */
45 }
46 else
47 {
48 strcpy(*next, s);
49 *next += len;
50 *p = NULL; /* don't send pointers on the wire! */
51 return 1;
52 }
53 }
54
55 static int
56 send_whack_msg (whack_message_t *msg)
57 {
58 struct sockaddr_un ctl_addr;
59 int sock;
60 ssize_t len;
61 char *str_next, *str_roof;
62
63 ctl_addr.sun_family = AF_UNIX;
64 strcpy(ctl_addr.sun_path, PLUTO_CTL_FILE);
65
66 /* pack strings */
67 str_next = (char *)msg->string;
68 str_roof = (char *)&msg->string[sizeof(msg->string)];
69
70 if (!pack_str(&msg->name, &str_next, &str_roof)
71 || !pack_str(&msg->left.id, &str_next, &str_roof)
72 || !pack_str(&msg->left.cert, &str_next, &str_roof)
73 || !pack_str(&msg->left.ca, &str_next, &str_roof)
74 || !pack_str(&msg->left.groups, &str_next, &str_roof)
75 || !pack_str(&msg->left.updown, &str_next, &str_roof)
76 || !pack_str(&msg->left.virt, &str_next, &str_roof)
77 || !pack_str(&msg->right.id, &str_next, &str_roof)
78 || !pack_str(&msg->right.cert, &str_next, &str_roof)
79 || !pack_str(&msg->right.ca, &str_next, &str_roof)
80 || !pack_str(&msg->right.groups, &str_next, &str_roof)
81 || !pack_str(&msg->right.updown, &str_next, &str_roof)
82 || !pack_str(&msg->right.virt, &str_next, &str_roof)
83 || !pack_str(&msg->keyid, &str_next, &str_roof)
84 || !pack_str(&msg->myid, &str_next, &str_roof)
85 || !pack_str(&msg->cacert, &str_next, &str_roof)
86 || !pack_str(&msg->ldaphost, &str_next, &str_roof)
87 || !pack_str(&msg->ldapbase, &str_next, &str_roof)
88 || !pack_str(&msg->crluri, &str_next, &str_roof)
89 || !pack_str(&msg->crluri2, &str_next, &str_roof)
90 || !pack_str(&msg->ocspuri, &str_next, &str_roof)
91 || !pack_str(&msg->ike, &str_next, &str_roof)
92 || !pack_str(&msg->esp, &str_next, &str_roof)
93 || !pack_str(&msg->sc_data, &str_next, &str_roof)
94 || (str_roof - str_next < msg->keyval.len))
95 {
96 plog("send_wack_msg(): can't pack strings");
97 return -1;
98 }
99 if (msg->keyval.ptr)
100 memcpy(str_next, msg->keyval.ptr, msg->keyval.len);
101 msg->keyval.ptr = NULL;
102 str_next += msg->keyval.len;
103 len = str_next - (char *)msg;
104
105 /* connect to pluto ctl */
106 sock = socket(AF_UNIX, SOCK_STREAM, 0);
107 if (sock < 0)
108 {
109 plog("socket() failed: %s", strerror(errno));
110 return -1;
111 }
112 if (connect(sock, (struct sockaddr *)&ctl_addr,
113 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
114 {
115 plog("connect(pluto_ctl) failed: %s", strerror(errno));
116 close(sock);
117 return -1;
118 }
119
120 /* send message */
121 if (write(sock, msg, len) != len)
122 {
123 plog("write(pluto_ctl) failed: %s", strerror(errno));
124 close(sock);
125 return -1;
126 }
127
128 /* TODO: read reply */
129 close(sock);
130 return 0;
131 }
132
133 static void
134 init_whack_msg(whack_message_t *msg)
135 {
136 memset(msg, 0, sizeof(whack_message_t));
137 msg->magic = WHACK_MAGIC;
138 }
139
140 static char *
141 connection_name(starter_conn_t *conn)
142 {
143 /* if connection name is '%auto', create a new name like conn_xxxxx */
144 static char buf[32];
145
146 if (streq(conn->name, "%auto"))
147 {
148 sprintf(buf, "conn_%ld", conn->id);
149 return buf;
150 }
151 return conn->name;
152 }
153
154 static void
155 set_whack_end(whack_end_t *w, starter_end_t *end, sa_family_t family)
156 {
157 if (end->srcip && end->srcip[0] != '%')
158 {
159 int len = 0;
160 char *pos;
161
162 pos = strchr(end->srcip, '/');
163 if (pos)
164 {
165 /* use first address only for pluto */
166 len = pos - end->srcip;
167 }
168 w->has_srcip = !end->has_natip;
169 ttoaddr(end->srcip, len, ip_version(end->srcip), &w->host_srcip);
170 }
171 else
172 {
173 anyaddr(AF_INET, &w->host_srcip);
174 }
175
176 w->id = end->id;
177 w->cert = end->cert;
178 w->ca = end->ca;
179 w->groups = end->groups;
180 w->host_addr = end->addr;
181 w->has_client = end->has_client;
182
183 if (family == AF_INET6 && isanyaddr(&end->nexthop))
184 {
185 anyaddr(AF_INET6, &end->nexthop);
186 }
187 w->host_nexthop = end->nexthop;
188
189 if (w->has_client)
190 {
191 char *pos;
192 int len = 0;
193
194 pos = strchr(end->subnet, ',');
195 if (pos)
196 {
197 len = pos - end->subnet;
198 }
199 ttosubnet(end->subnet, len, ip_version(end->subnet), &w->client);
200 }
201 else
202 {
203 if (end->has_virt)
204 {
205 w->virt = end->subnet;
206 }
207 w->client.addr.u.v4.sin_family = addrtypeof(&w->host_addr);
208 }
209
210 w->has_client_wildcard = end->has_client_wildcard;
211 w->has_port_wildcard = end->has_port_wildcard;
212 w->has_natip = end->has_natip;
213 w->allow_any = end->allow_any && !end->dns_failed;
214 w->modecfg = end->modecfg;
215 w->hostaccess = end->hostaccess;
216 w->sendcert = end->sendcert;
217 w->updown = end->updown;
218 w->host_port = IKE_UDP_PORT;
219 w->port = end->port;
220 w->protocol = end->protocol;
221
222 if (w->port != 0)
223 {
224 int port = htons(w->port);
225
226 setportof(port, &w->host_addr);
227 setportof(port, &w->client.addr);
228 }
229 }
230
231 static int
232 starter_whack_add_pubkey (starter_conn_t *conn, starter_end_t *end
233 , const char *lr)
234 {
235 const char *err;
236 static char keyspace[1024 + 4];
237 whack_message_t msg;
238
239 init_whack_msg(&msg);
240
241 msg.whack_key = TRUE;
242 msg.pubkey_alg = PUBKEY_ALG_RSA;
243 if (end->id && end->rsakey)
244 {
245 /* special values to ignore */
246 if (streq(end->rsakey, "")
247 || streq(end->rsakey, "%none")
248 || streq(end->rsakey, "%cert")
249 || streq(end->rsakey, "0x00"))
250 {
251 return 0;
252 }
253 msg.keyid = end->id;
254 err = atobytes(end->rsakey, 0, keyspace, sizeof(keyspace), &msg.keyval.len);
255 if (err)
256 {
257 plog("conn %s/%s: rsakey malformed [%s]", connection_name(conn), lr, err);
258 return 1;
259 }
260 else
261 {
262 msg.keyval.ptr = keyspace;
263 return send_whack_msg(&msg);
264 }
265 }
266 return 0;
267 }
268
269 int
270 starter_whack_add_conn(starter_conn_t *conn)
271 {
272 whack_message_t msg;
273 int r;
274
275 init_whack_msg(&msg);
276
277 msg.whack_connection = TRUE;
278 msg.name = connection_name(conn);
279
280 msg.ikev1 = conn->keyexchange != KEY_EXCHANGE_IKEV2;
281 msg.addr_family = conn->addr_family;
282 msg.tunnel_addr_family = conn->tunnel_addr_family;
283 msg.sa_ike_life_seconds = conn->sa_ike_life_seconds;
284 msg.sa_ipsec_life_seconds = conn->sa_ipsec_life_seconds;
285 msg.sa_rekey_margin = conn->sa_rekey_margin;
286 msg.sa_rekey_fuzz = conn->sa_rekey_fuzz;
287 msg.sa_keying_tries = conn->sa_keying_tries;
288 msg.policy = conn->policy;
289
290 /*
291 * Make sure the IKEv2-only policy bits are unset for IKEv1 connections
292 */
293 msg.policy &= ~POLICY_DONT_REAUTH;
294 msg.policy &= ~POLICY_BEET;
295 msg.policy &= ~POLICY_MOBIKE;
296 msg.policy &= ~POLICY_FORCE_ENCAP;
297
298 set_whack_end(&msg.left, &conn->left, conn->addr_family);
299 set_whack_end(&msg.right, &conn->right, conn->addr_family);
300
301 msg.esp = conn->esp;
302 msg.ike = conn->ike;
303 msg.pfsgroup = conn->pfsgroup;
304
305 /* taken from pluto/whack.c */
306 if (msg.pfsgroup)
307 {
308 char esp_buf[256];
309
310 snprintf(esp_buf, sizeof (esp_buf), "%s;%s"
311 , msg.esp ? msg.esp : ""
312 , msg.pfsgroup ? msg.pfsgroup : "");
313 msg.esp = esp_buf;
314
315 DBG(DBG_CONTROL,
316 DBG_log("Setting --esp=%s", msg.esp)
317 )
318 }
319 msg.dpd_delay = conn->dpd_delay;
320 msg.dpd_timeout = conn->dpd_timeout;
321 msg.dpd_action = conn->dpd_action;
322 /* msg.dpd_count = conn->dpd_count; not supported yet by strongSwan */
323
324 r = send_whack_msg(&msg);
325
326 if (r == 0 && (conn->policy & POLICY_PUBKEY))
327 {
328 r += starter_whack_add_pubkey (conn, &conn->left, "left");
329 r += starter_whack_add_pubkey (conn, &conn->right, "right");
330 }
331
332 return r;
333 }
334
335 int
336 starter_whack_del_conn(starter_conn_t *conn)
337 {
338 whack_message_t msg;
339
340 init_whack_msg(&msg);
341 msg.whack_delete = TRUE;
342 msg.name = connection_name(conn);
343 return send_whack_msg(&msg);
344 }
345
346 int
347 starter_whack_route_conn(starter_conn_t *conn)
348 {
349 whack_message_t msg;
350
351 init_whack_msg(&msg);
352 msg.whack_route = TRUE;
353 msg.name = connection_name(conn);
354 return send_whack_msg(&msg);
355 }
356
357 int
358 starter_whack_initiate_conn(starter_conn_t *conn)
359 {
360 whack_message_t msg;
361
362 init_whack_msg(&msg);
363 msg.whack_initiate = TRUE;
364 msg.whack_async = TRUE;
365 msg.name = connection_name(conn);
366 return send_whack_msg(&msg);
367 }
368
369 int
370 starter_whack_listen(void)
371 {
372 whack_message_t msg;
373 init_whack_msg(&msg);
374 msg.whack_listen = TRUE;
375 return send_whack_msg(&msg);
376 }
377
378 int starter_whack_shutdown(void)
379 {
380 whack_message_t msg;
381
382 init_whack_msg(&msg);
383 msg.whack_shutdown = TRUE;
384 return send_whack_msg(&msg);
385 }
386
387 int
388 starter_whack_add_ca(starter_ca_t *ca)
389 {
390 whack_message_t msg;
391
392 init_whack_msg(&msg);
393
394 msg.whack_ca = TRUE;
395 msg.name = ca->name;
396 msg.cacert = ca->cacert;
397 msg.ldaphost = ca->ldaphost;
398 msg.ldapbase = ca->ldapbase;
399 msg.crluri = ca->crluri;
400 msg.crluri2 = ca->crluri2;
401 msg.ocspuri = ca->ocspuri;
402 msg.whack_strict = ca->strict;
403
404 return send_whack_msg(&msg);
405 }
406
407 int
408 starter_whack_del_ca(starter_ca_t *ca)
409 {
410 whack_message_t msg;
411
412 init_whack_msg(&msg);
413
414 msg.whack_delete = TRUE;
415 msg.whack_ca = TRUE;
416 msg.name = ca->name;
417
418 return send_whack_msg(&msg);
419 }