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