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.
13 $vardir= "/var/lib/userv/dyndns";
14 $defconf= "/etc/userv/dyndns-domains";
15 $libdir= "/usr/share/userv/dyndns";
18 remove "$vardir/tmp/$$" or $! == ENOENT or
19 warn "cannot remove tempfile:$!\n";
27 @ARGV==2 or die "need <zone> and <domain> arguments\n";
28 ($zone,$subdomain) = @ARGV;
29 domainsyntax("command line",$zone);
30 domainsyntax("command line",$subdomain) unless $subdomain eq '@';
32 @userv_groups= split m/ /, $ENV{'USERV_GROUP'};
41 $fh->open("< $cf") or die "$cf: $!\n";
44 length or die "$cf:".($? ? "read:$?" : "eof")."\n";
45 s/^\s+//; chomp; s/\s+$//;
47 next if m/^\#/ or !m/\S/;
48 if (m/^zone\s+(\S+)$/) {
49 $thiszone= $1 eq $zone;
50 } elsif (m/^ratelimit\s+(\d+)\s+(\d+)\s+(\d+)$/) {
52 } elsif (m/^ttlrange\s+(\d+)\s+(\d+)$/) {
53 ($ttlmin,$ttlmax) = ($1,$2);
54 } elsif (m/^rrs\s+([A-Za-z0-9 \t]+)$/) {
57 grep { y/a-z/A-Z/; $rrt_allowed{$_}= 1; } split m/\s+/, $1;
58 } elsif (m/^include\s+(\S.*)$/) {
59 return if readconf($1);
60 } elsif (m/^subdomain\s+(\S+)\s+(\S+)$/) {
61 next unless $thiszone;
62 next unless $1 eq $subdomain;
63 next unless grep { $_ eq $2 } @userv_groups;
66 die "$cf:$.: config error\n";
69 close $fh or die "$cf: close: $!\n";
74 or die "permission denied\n";
76 chdir "$vardir" or die "chdir dyndns:$!\n";
78 open T,">tmp/$$" or die "create temp file: $!\n";
82 die "input:$.:".($? ? "$?" : "eof") unless length;
85 s/^(\S+)\s+(\d+)\s+([A-Za-z][0-9A-Za-z]*)\s+//
86 or die "input:$.:bogus line\n";
87 ($owner,$ttl,$type)= ($1,$2,$3);
89 $write_owner= $subdomain;
91 domainsyntax("input:$.",$owner) unless $owner eq '@';
92 $write_owner= $subdomain eq '@' ? $owner : "$owner.$subdomain";
94 length "$write_owner.$zone." < 255
95 or die "input:$.:$owner:resulting domain name too long\n";
99 warn "input:$.:$owner:capping ttl $ttl at lower bound $ttlmin\n";
102 if ($ttl > $ttlmax) {
103 warn "input:$.:$owner:capping ttl $ttl at upper bound $ttlmax\n";
107 die "input:$.:$owner:rr type not permitted:$type\n"
108 unless $rrt_allowed{$type};
109 if (exists $rrset_ttl{$owner,$type}) {
110 die "input:$.:$owner:$type:RRset has varying TTLs\n"
111 unless $rrset_ttl{$owner,$type} == $ttl;
113 $rrset_ttl{$owner,$type}= $ttl;
116 die "input:$.:$owner:CNAME and other records, or multiple CNAMEs\n"
118 ? exists $owner_types{$owner}
119 : exists $owner_types{$owner}->{'CNAME'};
122 defined($addr= inet_aton $_) or
123 die "input:$.:$owner:invalid IP address\n";
124 $data= inet_ntoa($addr);
125 } elsif ($type eq 'AAAA') {
126 defined($addr= inet_pton(AF_INET6, $_)) or
127 die "input:$.:$owner:invalid IPv6 address\n";
128 $data = inet_ntop(AF_INET6, $addr);
129 } elsif ($type eq 'CNAME') {
130 $data= domainsyntax_rel("input:$.:$owner:canonical name",$_).".";
131 } elsif ($type eq 'MX') {
132 m/^(\d+)\s+(\S+)$/ or die "input:$.:$owner:invalid MX syntax\n";
133 ($pref,$target) = ($1,$2);
135 die "input:$.:$owner:invalid MX preference\n"
136 if $pref<0 || $pref>65535;
137 $target= domainsyntax_rel("input:$.:$owner:mail exchanger",$target);
138 $data= "$pref $target.";
140 die "input:$.:$owner:unsupported RR type:$type\n";
142 $owner_types{$owner}->{$type}= 1;
144 print T "$write_owner $ttl $type $data\n"
145 or die "write data to temp file:$!\n";
148 close T or die "close RR data include:$!\n";
149 open STDIN, "< tmp/$$" or die "reopen RR data include:$!\n";
150 remove "tmp/$$" or die "close RR data include:$!\n";
152 chdir "zone,$zone" or die "chdir:$zone:$!\n";
154 exec "with-lock-ex","-w","Lock",
155 "$libdir/update", $zone, $subdomain, @rates;
156 die "execute update program:$!\n";
158 sub domainsyntax ($$) {
161 die "bad char:\`$&'\n" if $d =~ m/[^-.0-9a-z]/;
163 die "label starts with hyphen\n" if $d =~ m/\.\-/;
164 die "label ends with hyphen\n" if $d =~ m/\-\./;
165 die "empty label or dot at start or end\n" if $d =~ m/\.\./;
166 die "label too long\n" if $d =~ m/\..{64,}\./;
167 die "domain name too long\n" if length $d > 255;
170 die "$w:invalid domain name:\`$d':$@";
173 sub domainsyntax_rel ($$) {
175 unless ($d =~ s/\.$//) {
176 $d .= '.' unless $d =~ s/^\@$//;
177 $d .= ($subdomain eq '@' ? "$zone" : "$subdomain.$zone");