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