summaryrefslogtreecommitdiff
path: root/scripts/system/vyatta_update_resolv.pl
blob: a725a16ec045b5c6ba7223dc2acd64d4e1eea414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/perl -w
#
# Module: vyatta_update_resolv.pl
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Marat Nepomnyashy
# Date: December 2007
# Description: Script to update '/etc/resolv.conf' on commit of 'system domain-search domain' config.
#
# **** End License ****
#

use strict;
use lib "/opt/vyatta/share/perl5/";


use Getopt::Long;
my $change_dir = '';
my $modify_dir = '';
GetOptions("change_dir=s" => \$change_dir, "modify_dir=s" => \$modify_dir);


use VyattaConfig;
my $vc = new VyattaConfig();

if ($change_dir ne '') {
	$vc->{_changes_only_dir_base} = $change_dir;
}
if ($modify_dir ne '') {
	$vc->{_new_config_dir_base} = $modify_dir;
}


$vc->setLevel('system');

my @domains = $vc->returnValues('domain-search domain');
my $domain_name = $vc->returnValue('domain-name');

if (@domains > 0 && $domain_name && length($domain_name) > 0) {
	print STDERR "System configuration error.  Both \'domain-name\' and \'domain-search\' are specified, but only one of these mutually exclusive parameters is allowed.\n";
	print STDERR "System configuration commit aborted due to error(s).\n";
	exit(1);
}

my $doms = '';
foreach my $domain (@domains) {
	if (length($doms) > 0) {
		$doms .= ' ';
	}
	$doms .= $domain;
}

my $search = '';
if (length($doms) > 0) {
	$search = "search\t\t$doms\t\t#line generated by $0\n";
}

my $domain = '';
if ($domain_name && length($domain_name) > 0) {
	$domain = "domain\t\t$domain_name\t\t#line generated by $0\n";
}


# The following will re-write '/etc/resolv.conf' line by line,
# replacing the 'search' specifier with the latest values,
# or replacing the 'domain' specifier with the latest value.

my @resolv;
if (-e '/etc/resolv.conf') {
	open (RESOLV, '</etc/resolv.conf') or die("$0:  Error!  Unable to open '/etc/resolv.conf' for input: $!\n");
	@resolv = <RESOLV>;
	close (RESOLV);
}


my $foundSearch = 0;
my $foundDomain = 0;

open (RESOLV, '>/etc/resolv.conf') or die("$0:  Error!  Unable to open '/etc/resolv.conf' for output: $!\n");
foreach my $line (@resolv) {
	if ($line =~ /^search\s/) {
		$foundSearch = 1;
		if (length($search) > 0) {
			print RESOLV $search;
		}
	} elsif ($line =~ /^domain\s/) {
		$foundDomain = 1;
		if (length($domain) > 0) {
			print RESOLV $domain;
		}
	} else {
		print RESOLV $line;
	}
}
if ($foundSearch == 0 && length($search) > 0) {
	print RESOLV $search;
}
if ($foundDomain == 0 && length($domain) > 0) {
	print RESOLV $domain;
}

close (RESOLV);