1
1
/*
2
- * Copyright (C) 2012-2016 Tobias Brunner
2
+ * Copyright (C) 2012-2022 Tobias Brunner
3
3
* Copyright (C) 2009 Martin Willi
4
4
*
5
5
* Copyright (C) secunet Security Networks AG
29
29
/* path to resolvconf executable */
30
30
#define RESOLVCONF_EXEC "/sbin/resolvconf"
31
31
32
- /* default prefix used for resolvconf interfaces (should have high prio) */
33
- #define RESOLVCONF_PREFIX "lo.ipsec"
32
+ /* default interface/protocol used for resolvconf (should have high prio) */
33
+ #define RESOLVCONF_IFACE "lo.ipsec"
34
34
35
35
typedef struct private_resolve_handler_t private_resolve_handler_t ;
36
36
@@ -55,9 +55,9 @@ struct private_resolve_handler_t {
55
55
char * resolvconf ;
56
56
57
57
/**
58
- * Prefix to be used for interface names sent to resolvconf
58
+ * Interface name sent to resolvconf
59
59
*/
60
- char * iface_prefix ;
60
+ char * iface ;
61
61
62
62
/**
63
63
* Mutex to access file exclusively
@@ -184,39 +184,33 @@ static void remove_nameserver(private_resolve_handler_t *this, host_t *addr)
184
184
}
185
185
186
186
/**
187
- * Add or remove the given nameserver by invoking resolvconf.
187
+ * Install the given nameservers by invoking resolvconf. If the array is empty,
188
+ * remove the config.
188
189
*/
189
- static bool invoke_resolvconf (private_resolve_handler_t * this , host_t * addr ,
190
- bool install )
190
+ static bool invoke_resolvconf (private_resolve_handler_t * this , array_t * servers )
191
191
{
192
192
process_t * process ;
193
+ dns_server_t * dns ;
193
194
FILE * shell ;
194
- char buf [BUF_LEN ];
195
- int in , out , retval ;
196
-
197
- if (snprintf (buf , sizeof (buf ), "%H" , addr ) >= sizeof (buf ))
198
- {
199
- return FALSE;
200
- }
201
- translate (buf , ".:" , "__" );
202
-
203
- /* we use the nameserver's IP address as part of the interface name to
204
- * make them unique */
205
- process = process_start_shell (NULL , install ? & in : NULL , & out , NULL ,
206
- "2>&1 %s %s %s%s" , this -> resolvconf ,
207
- install ? "-a" : "-d" , this -> iface_prefix , buf );
195
+ int in , out , retval , i ;
208
196
197
+ process = process_start_shell (NULL , array_count (servers ) ? & in : NULL , & out ,
198
+ NULL , "2>&1 %s %s %s" , this -> resolvconf ,
199
+ array_count (servers ) ? "-a" : "-d" , this -> iface );
209
200
if (!process )
210
201
{
211
202
return FALSE;
212
203
}
213
- if (install )
204
+ if (array_count ( servers ) )
214
205
{
215
206
shell = fdopen (in , "w" );
216
207
if (shell )
217
208
{
218
- DBG1 (DBG_IKE , "installing DNS server %H via resolvconf" , addr );
219
- fprintf (shell , "nameserver %H\n" , addr );
209
+ for (i = 0 ; i < array_count (servers ); i ++ )
210
+ {
211
+ array_get (servers , i , & dns );
212
+ fprintf (shell , "nameserver %H\n" , dns -> server );
213
+ }
220
214
fclose (shell );
221
215
}
222
216
else
@@ -229,7 +223,7 @@ static bool invoke_resolvconf(private_resolve_handler_t *this, host_t *addr,
229
223
}
230
224
else
231
225
{
232
- DBG1 (DBG_IKE , "removing DNS server %H via resolvconf" , addr );
226
+ DBG1 (DBG_IKE , "removing DNS servers via resolvconf" );
233
227
}
234
228
shell = fdopen (out , "r" );
235
229
if (shell )
@@ -262,15 +256,7 @@ static bool invoke_resolvconf(private_resolve_handler_t *this, host_t *addr,
262
256
{
263
257
close (out );
264
258
}
265
- if (!process -> wait (process , & retval ) || retval != EXIT_SUCCESS )
266
- {
267
- if (install )
268
- { /* revert changes when installing fails */
269
- invoke_resolvconf (this , addr , FALSE);
270
- return FALSE;
271
- }
272
- }
273
- return TRUE;
259
+ return process -> wait (process , & retval ) && retval == EXIT_SUCCESS ;
274
260
}
275
261
276
262
METHOD (attribute_handler_t , handle , bool ,
@@ -302,22 +288,27 @@ METHOD(attribute_handler_t, handle, bool,
302
288
this -> mutex -> lock (this -> mutex );
303
289
if (array_bsearch (this -> servers , addr , dns_server_find , & found ) == -1 )
304
290
{
291
+ INIT (found ,
292
+ .server = addr -> clone (addr ),
293
+ .refcount = 1 ,
294
+ );
295
+ array_insert_create (& this -> servers , ARRAY_TAIL , found );
296
+ array_sort (this -> servers , dns_server_sort , NULL );
297
+
305
298
if (this -> resolvconf )
306
299
{
307
- handled = invoke_resolvconf (this , addr , TRUE);
300
+ DBG1 (DBG_IKE , "installing DNS server %H via resolvconf" , addr );
301
+ handled = invoke_resolvconf (this , this -> servers );
308
302
}
309
303
else
310
304
{
311
305
handled = write_nameserver (this , addr );
312
306
}
313
- if (handled )
307
+ if (! handled )
314
308
{
315
- INIT (found ,
316
- .server = addr -> clone (addr ),
317
- .refcount = 1 ,
318
- );
319
- array_insert_create (& this -> servers , ARRAY_TAIL , found );
320
- array_sort (this -> servers , dns_server_sort , NULL );
309
+ array_remove (this -> servers , ARRAY_TAIL , NULL );
310
+ found -> server -> destroy (found -> server );
311
+ free (found );
321
312
}
322
313
}
323
314
else
@@ -369,17 +360,19 @@ METHOD(attribute_handler_t, release, void,
369
360
}
370
361
else
371
362
{
363
+ array_remove (this -> servers , idx , NULL );
364
+ found -> server -> destroy (found -> server );
365
+ free (found );
366
+
372
367
if (this -> resolvconf )
373
368
{
374
- invoke_resolvconf (this , addr , FALSE);
369
+ DBG1 (DBG_IKE , "removing DNS server %H via resolvconf" , addr );
370
+ invoke_resolvconf (this , this -> servers );
375
371
}
376
372
else
377
373
{
378
374
remove_nameserver (this , addr );
379
375
}
380
- array_remove (this -> servers , idx , NULL );
381
- found -> server -> destroy (found -> server );
382
- free (found );
383
376
}
384
377
}
385
378
this -> mutex -> unlock (this -> mutex );
@@ -495,9 +488,11 @@ resolve_handler_t *resolve_handler_create()
495
488
.resolvconf = lib -> settings -> get_str (lib -> settings ,
496
489
"%s.plugins.resolve.resolvconf.path" ,
497
490
NULL , lib -> ns ),
498
- .iface_prefix = lib -> settings -> get_str (lib -> settings ,
491
+ .iface = lib -> settings -> get_str (lib -> settings ,
492
+ "%s.plugins.resolve.resolvconf.iface" ,
493
+ lib -> settings -> get_str (lib -> settings ,
499
494
"%s.plugins.resolve.resolvconf.iface_prefix" ,
500
- RESOLVCONF_PREFIX , lib -> ns ),
495
+ RESOLVCONF_IFACE , lib -> ns ) , lib -> ns ),
501
496
);
502
497
503
498
if (!this -> resolvconf && stat (RESOLVCONF_EXEC , & st ) == 0 )
0 commit comments