1 # dnsadmin common functions
3 BEGIN { push(@INC, ".."); };
6 &foreign_require("bind8", "bind8-lib.pl");
7 do "$bind8::module_root_directory/records-lib.pl";
9 # mapping between record types and names
10 %code_map = ("A", "Address", "NS", "Name Server", "CNAME", "Name Alias",
11 "MX", "Mail Server", "HINFO", "Host Information", "TXT", "Text",
12 "WKS", "Well Known Service", "RP", "Responsible Person",
13 "PTR", "Reverse Address");
16 # Returns the current bind4 configuration
19 if (!@get_config_cache) {
20 @get_config_cache = &read_config_file($config{'named_boot_file'});
22 return \@get_config_cache;
25 # read_config_file(filename, [dont expand includes])
26 # Read and parse a BIND4 format config file
29 # read primary config file
35 s/\r|\n//g; # strip newline
36 s/;.*$//g; # strip comments
37 s/\s+$//g; # strip trailing spaces
38 if (/^(\S+)\s*(.*)$/) {
42 $dir{'values'} = [ split(/\s+/, $2) ];
52 # expand include directives
53 for($i=0; $i<@rv; $i++) {
54 if ($rv[$i]->{'name'} eq "include" && !$_[1]) {
55 # replace this include directive
56 $inc = $rv[$i]->{'value'};
58 $inc = &base_directory(\@rv)."/".$inc;
60 @inc = &read_config_file($inc, 1);
62 # update index of included structures
63 for($j=0; $j<@inc; $j++) {
64 $inc[$j]->{'index'} += $rv[$i]->{'index'};
67 # update index of directives after include
68 for($j=$i+1; $j<@rv; $j++) {
69 $rv[$j]->{'index'} += scalar(@inc) - 1;
72 splice(@rv, $i--, 1, @inc);
78 # find_config(name, &array)
82 foreach $c (@{$_[1]}) {
83 if ($c->{'name'} eq $_[0]) {
87 return @rv ? wantarray ? @rv : $rv[0]
88 : wantarray ? () : undef;
91 # base_directory([&config])
92 # Returns the base directory for include and domain files
95 $conf = @_ ? $_[0] : &get_config();
96 $dir = &find_config("directory", $conf);
97 if ($dir) { return $dir->{'values'}->[0]; }
98 $config{'named_boot_file'} =~ /^(.*)\/[^\/]+$/;
102 # create_zone(&details)
105 local(@v) = @{$_[0]->{'values'}};
106 open(ZONE, ">> $config{'named_boot_file'}");
107 print ZONE $_[0]->{'name'}.(@v ? " ".join(" ", @v) : "")."\n";
111 # modify_zone(&old, &details)
114 local(@v) = @{$_[1]->{'values'}};
115 &replace_file_line($_[0]->{'file'}, $_[0]->{'line'},
116 $_[1]->{'name'}.(@v ? " ".join(" ", @v) : "")."\n");
122 &replace_file_line($_[0]->{'file'}, $_[0]->{'line'});
125 # find_reverse(address)
126 # Returns the zone and record structures for the PTR record for some address
129 local($conf, @zl, $rev, $z, $revconf, $revfile, $revrec, @revrecs, @octs, $rr);
131 # find reverse domain
132 $conf = &get_config();
133 @zl = &find_config("primary", $conf);
134 @octs = split(/\./, $_[0]);
135 for($i=2; $i>=0; $i--) {
136 $rev = &ip_to_arpa(join('.', @octs[0..$i]));
139 if (lc($z->{'values'}->[0]) eq $rev) {
140 # found the reverse master domain
147 # find reverse record
149 $revfile = $revconf->{'values'}->[1];
150 @revrecs = &read_zone_file($revfile, $revconf->{'values'}->[0]);
151 local $addr = &ip_to_arpa($_[0]);
152 foreach $rr (@revrecs) {
153 if ($rr->{'type'} eq "PTR" &&
154 lc($rr->{'name'}) eq lc($addr)) {
155 # found the reverse record
161 return ($revconf, $revfile, $revrec);
164 # find_forward(address)
165 # Returns the zone and record structures for the A record for some address
168 local($conf, @zl, $fwd, $z, $fwdconf, $fwdfile, $fwdrec, @fwdrecs, @octs, $rr);
170 # find reverse domain
171 local $host = $_[0]; $host =~ s/\.$//;
172 $conf = &get_config();
173 @zl = &find_config("primary", $conf);
174 local @parts = split(/\./, $host);
175 DOMAIN: for($i=1; $i<@parts; $i++) {
176 local $fwd = join(".", @parts[$i .. @parts-1]);
179 if (lc($z->{'values'}->[0]) eq $fwd) {
180 # Found the forward master!
187 # find forward record
189 $fwdfile = $fwdconf->{'values'}->[1];
190 local @fwdrecs = &read_zone_file($fwdfile, $fwdconf->{'values'}->[0]);
191 foreach $fr (@fwdrecs) {
192 if ($fr->{'type'} eq 'A' &&
193 $fr->{'name'} eq $_[0]) {
194 # found the forward record
200 return ($fwdconf, $fwdfile, $fwdrec);
203 # can_edit_zone(&access, zone)
207 return 1 if ($access{'zones'} eq '*');
208 foreach (split(/\s+/, $access{'zones'})) {
209 return 1 if ($_ eq $_[1]);