aa0bea34db9c296995e8ff639f583b898ea1e99d
[strongswan.git] / src / libcharon / plugins / resolve / resolve_handler.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "resolve_handler.h"
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 #include <utils/debug.h>
24 #include <threading/mutex.h>
25
26 /* path to resolvconf executable */
27 #define RESOLVCONF_EXEC "/sbin/resolvconf"
28
29 /* default prefix used for resolvconf interfaces (should have high prio) */
30 #define RESOLVCONF_PREFIX "lo.inet.ipsec."
31
32 typedef struct private_resolve_handler_t private_resolve_handler_t;
33
34 /**
35 * Private data of an resolve_handler_t object.
36 */
37 struct private_resolve_handler_t {
38
39 /**
40 * Public resolve_handler_t interface.
41 */
42 resolve_handler_t public;
43
44 /**
45 * resolv.conf file to use
46 */
47 char *file;
48
49 /**
50 * use resolvconf instead of writing directly to resolv.conf
51 */
52 bool use_resolvconf;
53
54 /**
55 * prefix to be used for interface names sent to resolvconf
56 */
57 char *iface_prefix;
58
59 /**
60 * Mutex to access file exclusively
61 */
62 mutex_t *mutex;
63 };
64
65 /**
66 * Writes the given nameserver to resolv.conf
67 */
68 static bool write_nameserver(private_resolve_handler_t *this,
69 identification_t *server, host_t *addr)
70 {
71 FILE *in, *out;
72 char buf[1024];
73 size_t len;
74 bool handled = FALSE;
75
76 in = fopen(this->file, "r");
77 /* allows us to stream from in to out */
78 unlink(this->file);
79 out = fopen(this->file, "w");
80 if (out)
81 {
82 fprintf(out, "nameserver %H # by strongSwan, from %Y\n", addr,
83 server);
84 DBG1(DBG_IKE, "installing DNS server %H to %s", addr, this->file);
85 handled = TRUE;
86
87 /* copy rest of the file */
88 if (in)
89 {
90 while ((len = fread(buf, 1, sizeof(buf), in)))
91 {
92 ignore_result(fwrite(buf, 1, len, out));
93 }
94 }
95 fclose(out);
96 }
97 if (in)
98 {
99 fclose(in);
100 }
101 return handled;
102 }
103
104 /**
105 * Removes the given nameserver from resolv.conf
106 */
107 static void remove_nameserver(private_resolve_handler_t *this,
108 identification_t *server, host_t *addr)
109 {
110 FILE *in, *out;
111 char line[1024], matcher[512];
112
113 in = fopen(this->file, "r");
114 if (in)
115 {
116 /* allows us to stream from in to out */
117 unlink(this->file);
118 out = fopen(this->file, "w");
119 if (out)
120 {
121 snprintf(matcher, sizeof(matcher),
122 "nameserver %H # by strongSwan, from %Y\n",
123 addr, server);
124
125 /* copy all, but matching line */
126 while (fgets(line, sizeof(line), in))
127 {
128 if (strpfx(line, matcher))
129 {
130 DBG1(DBG_IKE, "removing DNS server %H from %s",
131 addr, this->file);
132 }
133 else
134 {
135 fputs(line, out);
136 }
137 }
138 fclose(out);
139 }
140 fclose(in);
141 }
142 }
143
144 /**
145 * Add or remove the given nameserver by invoking resolvconf.
146 */
147 static bool invoke_resolvconf(private_resolve_handler_t *this,
148 identification_t *server, host_t *addr,
149 bool install)
150 {
151 char cmd[128];
152
153 /* we use the nameserver's IP address as part of the interface name to
154 * make them unique */
155 if (snprintf(cmd, sizeof(cmd), "%s %s %s%H", RESOLVCONF_EXEC,
156 install ? "-a" : "-d", this->iface_prefix, addr) >= sizeof(cmd))
157 {
158 return FALSE;
159 }
160
161 if (install)
162 {
163 FILE *out;
164 bool success;
165
166 out = popen(cmd, "w");
167 if (!out)
168 {
169 return FALSE;
170 }
171 DBG1(DBG_IKE, "installing DNS server %H via resolvconf", addr);
172 fprintf(out, "nameserver %H\n", addr);
173 success = !ferror(out);
174 if (pclose(out) || !success)
175 {
176 invoke_resolvconf(this, server, addr, FALSE);
177 return FALSE;
178 }
179 }
180 else
181 {
182 ignore_result(system(cmd));
183 }
184 return TRUE;
185 }
186
187 METHOD(attribute_handler_t, handle, bool,
188 private_resolve_handler_t *this, ike_sa_t *ike_sa,
189 configuration_attribute_type_t type, chunk_t data)
190 {
191 identification_t *server;
192 host_t *addr;
193 bool handled;
194
195 switch (type)
196 {
197 case INTERNAL_IP4_DNS:
198 addr = host_create_from_chunk(AF_INET, data, 0);
199 break;
200 case INTERNAL_IP6_DNS:
201 addr = host_create_from_chunk(AF_INET6, data, 0);
202 break;
203 default:
204 return FALSE;
205 }
206
207 if (!addr || addr->is_anyaddr(addr))
208 {
209 DESTROY_IF(addr);
210 return FALSE;
211 }
212 server = ike_sa->get_other_id(ike_sa);
213
214 this->mutex->lock(this->mutex);
215 if (this->use_resolvconf)
216 {
217 handled = invoke_resolvconf(this, server, addr, TRUE);
218 }
219 else
220 {
221 handled = write_nameserver(this, server, addr);
222 }
223 this->mutex->unlock(this->mutex);
224 addr->destroy(addr);
225
226 if (!handled)
227 {
228 DBG1(DBG_IKE, "adding DNS server failed");
229 }
230 return handled;
231 }
232
233 METHOD(attribute_handler_t, release, void,
234 private_resolve_handler_t *this, ike_sa_t *ike_sa,
235 configuration_attribute_type_t type, chunk_t data)
236 {
237 identification_t *server;
238 host_t *addr;
239 int family;
240
241 switch (type)
242 {
243 case INTERNAL_IP4_DNS:
244 family = AF_INET;
245 break;
246 case INTERNAL_IP6_DNS:
247 family = AF_INET6;
248 break;
249 default:
250 return;
251 }
252 addr = host_create_from_chunk(family, data, 0);
253 server = ike_sa->get_other_id(ike_sa);
254
255 this->mutex->lock(this->mutex);
256 if (this->use_resolvconf)
257 {
258 invoke_resolvconf(this, server, addr, FALSE);
259 }
260 else
261 {
262 remove_nameserver(this, server, addr);
263 }
264 this->mutex->unlock(this->mutex);
265
266 addr->destroy(addr);
267 }
268
269 /**
270 * Attribute enumerator implementation
271 */
272 typedef struct {
273 /** implements enumerator_t interface */
274 enumerator_t public;
275 /** request IPv4 DNS? */
276 bool v4;
277 /** request IPv6 DNS? */
278 bool v6;
279 } attribute_enumerator_t;
280
281 static bool attribute_enumerate(attribute_enumerator_t *this,
282 configuration_attribute_type_t *type,
283 chunk_t *data)
284 {
285 if (this->v4)
286 {
287 *type = INTERNAL_IP4_DNS;
288 *data = chunk_empty;
289 this->v4 = FALSE;
290 return TRUE;
291 }
292 if (this->v6)
293 {
294 *type = INTERNAL_IP6_DNS;
295 *data = chunk_empty;
296 this->v6 = FALSE;
297 return TRUE;
298 }
299 return FALSE;
300 }
301
302 /**
303 * Check if a list has a host of given family
304 */
305 static bool has_host_family(linked_list_t *list, int family)
306 {
307 enumerator_t *enumerator;
308 host_t *host;
309 bool found = FALSE;
310
311 enumerator = list->create_enumerator(list);
312 while (enumerator->enumerate(enumerator, &host))
313 {
314 if (host->get_family(host) == family)
315 {
316 found = TRUE;
317 break;
318 }
319 }
320 enumerator->destroy(enumerator);
321
322 return found;
323 }
324
325 METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t*,
326 private_resolve_handler_t *this, ike_sa_t *ike_sa,
327 linked_list_t *vips)
328 {
329 attribute_enumerator_t *enumerator;
330
331 INIT(enumerator,
332 .public = {
333 .enumerate = (void*)attribute_enumerate,
334 .destroy = (void*)free,
335 },
336 .v4 = has_host_family(vips, AF_INET),
337 .v6 = has_host_family(vips, AF_INET6),
338 );
339 return &enumerator->public;
340 }
341
342 METHOD(resolve_handler_t, destroy, void,
343 private_resolve_handler_t *this)
344 {
345 this->mutex->destroy(this->mutex);
346 free(this);
347 }
348
349 /**
350 * See header
351 */
352 resolve_handler_t *resolve_handler_create()
353 {
354 private_resolve_handler_t *this;
355 struct stat st;
356
357 INIT(this,
358 .public = {
359 .handler = {
360 .handle = _handle,
361 .release = _release,
362 .create_attribute_enumerator = _create_attribute_enumerator,
363 },
364 .destroy = _destroy,
365 },
366 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
367 .file = lib->settings->get_str(lib->settings, "%s.plugins.resolve.file",
368 RESOLV_CONF, lib->ns),
369 );
370
371 if (stat(RESOLVCONF_EXEC, &st) == 0)
372 {
373 this->use_resolvconf = TRUE;
374 this->iface_prefix = lib->settings->get_str(lib->settings,
375 "%s.plugins.resolve.resolvconf.iface_prefix",
376 RESOLVCONF_PREFIX, lib->ns);
377 }
378
379 return &this->public;
380 }