2 # usage: (cat RRs; echo .) | userv dyndns <zone> <subdomain>
3 # Not all zone file formats are accepted:
4 # - All RRs must have owners specified.
5 # - All RRs must have TTLs specified.
6 # - The owner must be specified as a sub-subdomain, relative
7 # to <subdomain>.<zone>, and so must not have a trailing `.';
8 # where the owner is to be <subdomain>.<zone>, `@' must be used.
10 # Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
11 # Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
13 # Chancellor Masters and Scholars of the University of Cambridge
14 # Copyright 2010 Tony Finch <fanf@dotat.at>
16 # This is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 3 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful, but
22 # WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 # General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with userv-utils; if not, see http://www.gnu.org/licenses/.
32 $vardir= "/var/lib/userv/dyndns";
33 $defconf= "/etc/userv/dyndns-domains";
34 $libdir= "/usr/share/userv/dyndns";
37 remove "$vardir/tmp/$$" or $! == ENOENT or
38 warn "cannot remove tempfile:$!\n";
46 @ARGV==2 or die "need <zone> and <domain> arguments\n";
47 ($zone,$subdomain) = @ARGV;
48 domainsyntax("command line",$zone);
49 domainsyntax("command line",$subdomain) unless $subdomain eq '@';
51 @userv_groups= split m/ /, $ENV{'USERV_GROUP'};
60 $fh->open("< $cf") or die "$cf: $!\n";
63 length or die "$cf:".($? ? "read:$?" : "eof")."\n";
64 s/^\s+//; chomp; s/\s+$//;
66 next if m/^\#/ or !m/\S/;
67 if (m/^zone\s+(\S+)$/) {
68 $thiszone= $1 eq $zone;
69 } elsif (m/^ratelimit\s+(\d+)\s+(\d+)\s+(\d+)$/) {
71 } elsif (m/^ttlrange\s+(\d+)\s+(\d+)$/) {
72 ($ttlmin,$ttlmax) = ($1,$2);
73 } elsif (m/^rrs\s+([A-Za-z0-9 \t]+)$/) {
76 grep { y/a-z/A-Z/; $rrt_allowed{$_}= 1; } split m/\s+/, $1;
77 } elsif (m/^include\s+(\S.*)$/) {
78 return if readconf($1);
79 } elsif (m/^subdomain\s+(\S+)\s+(\S+)$/) {
80 next unless $thiszone;
81 next unless $1 eq $subdomain;
82 next unless grep { $_ eq $2 } @userv_groups;
85 die "$cf:$.: config error\n";
88 close $fh or die "$cf: close: $!\n";
93 or die "permission denied\n";
95 chdir "$vardir" or die "chdir dyndns:$!\n";
97 open T,">tmp/$$" or die "create temp file: $!\n";
101 die "input:$.:".($? ? "$?" : "eof") unless length;
104 s/^(\S+)\s+(\d+)\s+([A-Za-z][0-9A-Za-z]*)\s+//
105 or die "input:$.:bogus line\n";
106 ($owner,$ttl,$type)= ($1,$2,$3);
108 $write_owner= $subdomain;
110 domainsyntax("input:$.",$owner) unless $owner eq '@';
111 $write_owner= $subdomain eq '@' ? $owner : "$owner.$subdomain";
113 length "$write_owner.$zone." < 255
114 or die "input:$.:$owner:resulting domain name too long\n";
117 if ($ttl < $ttlmin) {
118 warn "input:$.:$owner:capping ttl $ttl at lower bound $ttlmin\n";
121 if ($ttl > $ttlmax) {
122 warn "input:$.:$owner:capping ttl $ttl at upper bound $ttlmax\n";
126 die "input:$.:$owner:rr type not permitted:$type\n"
127 unless $rrt_allowed{$type};
128 if (exists $rrset_ttl{$owner,$type}) {
129 die "input:$.:$owner:$type:RRset has varying TTLs\n"
130 unless $rrset_ttl{$owner,$type} == $ttl;
132 $rrset_ttl{$owner,$type}= $ttl;
135 die "input:$.:$owner:CNAME and other records, or multiple CNAMEs\n"
137 ? exists $owner_types{$owner}
138 : exists $owner_types{$owner}->{'CNAME'};
141 defined($addr= inet_aton $_) or
142 die "input:$.:$owner:invalid IP address\n";
143 $data= inet_ntoa($addr);
144 } elsif ($type eq 'AAAA') {
145 defined($addr= inet_pton(AF_INET6, $_)) or
146 die "input:$.:$owner:invalid IPv6 address\n";
147 $data = inet_ntop(AF_INET6, $addr);
148 } elsif ($type eq 'CNAME') {
149 $data= domainsyntax_rel("input:$.:$owner:canonical name",$_).".";
150 } elsif ($type eq 'MX') {
151 m/^(\d+)\s+(\S+)$/ or die "input:$.:$owner:invalid MX syntax\n";
152 ($pref,$target) = ($1,$2);
154 die "input:$.:$owner:invalid MX preference\n"
155 if $pref<0 || $pref>65535;
156 $target= domainsyntax_rel("input:$.:$owner:mail exchanger",$target);
157 $data= "$pref $target.";
159 die "input:$.:$owner:unsupported RR type:$type\n";
161 $owner_types{$owner}->{$type}= 1;
163 print T "$write_owner $ttl $type $data\n"
164 or die "write data to temp file:$!\n";
167 close T or die "close RR data include:$!\n";
168 open STDIN, "< tmp/$$" or die "reopen RR data include:$!\n";
169 remove "tmp/$$" or die "close RR data include:$!\n";
171 chdir "zone,$zone" or die "chdir:$zone:$!\n";
173 exec "with-lock-ex","-w","Lock",
174 "$libdir/update", $zone, $subdomain, @rates;
175 die "execute update program:$!\n";
177 sub domainsyntax ($$) {
180 die "bad char:\`$&'\n" if $d =~ m/[^-.0-9a-z]/;
182 die "label starts with hyphen\n" if $d =~ m/\.\-/;
183 die "label ends with hyphen\n" if $d =~ m/\-\./;
184 die "empty label or dot at start or end\n" if $d =~ m/\.\./;
185 die "label too long\n" if $d =~ m/\..{64,}\./;
186 die "domain name too long\n" if length $d > 255;
189 die "$w:invalid domain name:\`$d':$@";
192 sub domainsyntax_rel ($$) {
194 unless ($d =~ s/\.$//) {
195 $d .= '.' unless $d =~ s/^\@$//;
196 $d .= ($subdomain eq '@' ? "$zone" : "$subdomain.$zone");