]> the.earth.li Git - autodns.git/blob - autodns.pl
6f14bd35efe3ba46266b45135e4996ac164ae9af
[autodns.git] / autodns.pl
1 #!/usr/bin/perl -Tw
2 # autodns 0.0.8
3 # Copyright 1999-2005 Project Purple. Written by Jonathan McDowell
4 # See ACKNOWLEDGEMENTS file for full details of contributors.
5 # http://www.earth.li/projectpurple/progs/autodns.html
6 # Released under the GPL.
7 #
8 # $Id: autodns.pl,v 1.11 2005/05/16 17:26:47 noodles Exp $
9 #
10
11 use strict;
12 use Fcntl qw(:flock);
13 use File::Temp qw(tempfile);
14 use IPC::Open3;
15 use MIME::Parser;
16
17 $ENV{'PATH'}="/usr/local/bin:/usr/bin:/bin:/usr/sbin";
18
19 my ($from, $subject, $gpguser, $gpggood, $priv);
20 my ($user, $server, $inprocess, $delcount, $addcount);
21 my ($domain, @MAIL, @GPGERROR, @COMMANDS, %zones, $VERSION);
22
23 use vars qw($me $ccreply $conffile $domainlistroot @cfgfiles $usersfile
24         $lockfile $reload_command);
25
26 $VERSION="0.0.8";
27
28 #
29 # Load our config
30 #
31 my $file = '/etc/bind/autodns.conf';
32 unless (my $ret = do $file) {
33         warn "Couldn't parse $file\n" if $@;
34         warn "Couldn't do $file\n" unless defined $ret;
35         warn "Couldn't run $file\n" unless $ret;
36         die "Problem reading config file!\n";
37 }
38
39 ###
40 ### There should be no need to edit anything below (unless you're not
41 ### using BIND). This statement might even be true now - let me know if not.
42 ###
43
44 #
45 # Try to figure out what zones we currently know about by parsing config
46 # files. Sets the item in %zones to 1 for each zone it finds.
47 #
48 # Call with the name of a config file to read:
49 #
50 # &getzones("/etc/named.conf");
51 #
52 sub getzones {
53         my ($namedfile) = @_;
54
55         open (NAMEDCONF, "< $namedfile") or
56                 &fatalerror("Can't open $namedfile");
57
58         while (<NAMEDCONF>) {
59                 if (/^\s*zone\s*"([^"]+)"/) {
60                         $zones{$1}=1;
61                 }
62         }
63
64         close NAMEDCONF;
65 }
66
67 #
68 # Check that a domain is only made up of valid characters.
69 #
70 # These are: a-z, 0-9, - or .
71 #
72 sub valid_domain {
73         my $domain = shift;
74         $domain = lc $domain;
75         if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,4}$/) {
76                 return 1;
77         } elsif ($domain =~ /^(?:[0-9\/-]+\.)+in-addr.arpa$/) {
78                 return 1;
79         } else {
80                 return 0;
81         }
82 }
83
84 #
85 # Deal with a fatal error by printing an error message, closing the pipe to
86 # sendmail and exiting.
87 #
88 # fatalerror("I'm melting!");
89 #
90 sub fatalerror {
91         my $message = shift;
92
93         print REPLY $message;
94         close(REPLY);
95
96         flock(LOCKFILE, LOCK_UN);
97         close(LOCKFILE);
98         unlink($lockfile);
99
100 #       die $message;
101         exit;
102 }
103
104 #
105 # Get user details from usersfile based on a PGP ID.
106 #
107 # A users entry looks like:
108 #
109 # <username>:<keyid>:<priviledge level>:<master server ip>
110 #
111 # Priviledge level is not currently used.
112 #
113 # ($user, $priv, $server) = &getuserinfo("5B430367");
114 #
115 sub getuserinfo {
116         my $gpguser = shift;
117         my ($user, $priviledge, $server);
118
119         open (CONFIGFILE, "< $usersfile") or
120                 &fatalerror("Couldn't open user configuration file.");
121
122         foreach (<CONFIGFILE>) {
123                 if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
124                         $user=$1;
125                         $priviledge=$2;
126                         $server=$3;
127                         chomp $user;
128                         chomp $priviledge;
129                         chomp $server;
130         
131                         if ($user !~ /^.+$/) {
132                                 close(CONFIGFILE);
133                                 &fatalerror("Error in user configuration file: Can't get username.\n");
134                         }
135
136                         if ($server !~ /^(\d{1,3}\.){3}\d{1,3}$/) {
137                                 $server =~ s/\d\.]//g;
138                                 close(CONFIGFILE); 
139                                 &fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
140                                 exit;
141                         } 
142                 }
143         } 
144         close(CONFIGFILE);
145
146         if ($user =~ /^$/) {
147                 &fatalerror("User not found.\n");
148         }
149
150         return ($user, $priviledge, $server);
151 }
152
153 $delcount=$addcount=$inprocess=0;
154
155 # Read in the mail from stdin.
156 @MAIL=<>;
157
158 $subject = "Reply from AutoDNS";
159 # Now lets try to find out who it's from.
160 foreach (@MAIL) {
161         if (/^$/) { last; }
162         if (/^From: (.*)/i) { $from=$1; chomp $from;}
163         if (/^Subject:\s+(re:)?(.*)$/i) { $subject="Re: ".$2 if ($2);}
164 }
165
166 if ((! defined($from)) || $from =~ /^$/ ) {
167         die "Couldn't find a from address.";
168 } elsif ($from =~ /mailer-daemon@/i) {
169         die "From address is mailer-daemon, ignoring.";
170 }
171
172 if (! defined($subject)) { $subject="Reply from AutoDNS"; };
173
174 # We've got a from address. Start a reply.
175
176 open(REPLY, "|sendmail -t -oem -oi") or die "Couldn't spawn sendmail";
177
178 print REPLY "From: $me\n";
179 print REPLY "To: $from\n";
180 #
181 # Check to see if our CC address is the same as the from address and if so
182 # don't CC.
183 #
184 if ($from ne $ccreply) {
185         print REPLY "Cc: $ccreply\n";
186 }
187 print REPLY <<EOF;
188 Subject: $subject
189
190 AutoDNS $VERSION
191 Copyright 1999-2004 Project Purple. Written by Jonathan McDowell.
192 Released under the GPL.
193
194 EOF
195
196 #
197 # Throw the mail at MIME::Parser and see if it accepts it.
198 #
199 my $parser = new MIME::Parser;
200 $parser->output_to_core(1); # No temporary files
201 my $entity = $parser->parse_data(\@MAIL);
202
203 #
204 # Make sure locale is set to C so we get messages in English as we expect.
205 #
206 $ENV{'LC_ALL'}="C";
207
208 if ($entity->parts) {
209         # MIME
210
211         my ($got_sig, $got_text) = (0, 0);
212         my ($sig_name,$sig_fh,$text_name,$text_fh);
213         ($sig_fh, $sig_name) = tempfile();
214         ($text_fh, $text_name) = tempfile();
215
216         foreach my $subent ($entity->parts) {
217                 if ($subent->effective_type eq "text/plain") {
218                         @COMMANDS = split /\n/,$subent->bodyhandle->as_string;
219
220                         my $str = $subent->as_string;
221                         $str =~ s/=\n$//;
222                         $str =~ s/\n/\r\n/g;
223                         print $text_fh $str;
224                         close($text_fh);
225                         $got_text++;
226                 } elsif ($subent->effective_type eq
227                                 "application/pgp-signature") {
228                         print $sig_fh $subent->as_string;
229                         close($sig_fh);
230                         $got_sig++;
231                 }
232         }
233
234         if ($got_sig && $got_text) {
235                 my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR,
236                         "gpg --batch --verify ".
237                         $sig_name." ".$text_name);
238
239                 close GPGIN;
240
241                 @GPGERROR=<GPGERR>;
242                 my @GPGOUTPUT=<GPGOUT>;
243                 close GPGERR;
244                 close GPGOUT;
245                 waitpid $pid, 0;
246
247                 unlink($text_name);
248                 unlink($sig_name);
249         }
250 } else {
251         # Clear text.
252
253         my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
254
255         # Feed it the mail.
256         print GPGIN @MAIL;
257         close GPGIN;
258
259         # And grab what it has to say.
260         @GPGERROR=<GPGERR>;
261         @COMMANDS=<GPGOUT>;
262         close GPGERR;
263         close GPGOUT;
264         waitpid $pid, 0;
265 }
266
267 # Check who it's from and if the signature was a good one.
268 $gpggood=1;
269 foreach (@GPGERROR) {
270         chomp;
271         if (/Signature made.* (.*)$/) {
272                 $gpguser=$1; 
273         } elsif (/error/) {
274                 $gpggood = 0;
275                 print REPLY "Some errors ocurred\n";
276         } elsif (/BAD signature/) {
277                 $gpggood = 0;
278                 print REPLY "BAD signature!\n";
279         } elsif (/public key not found/) {
280                 $gpggood = 0;
281                 print REPLY "Public Key not found\n";
282         }
283 }
284
285 # If we have an empty user it probably wasn't signed.
286 if (! $gpguser) {
287         print REPLY "Message appears not to be GPG signed.\n";
288         close REPLY;
289         exit;
290 }
291
292 # Check the signature we got was ok.
293 if ($gpggood) {
294         print REPLY "Good GPG signature found. ($gpguser)\n";
295 } else {
296         print REPLY "Bad GPG signature!\n";
297         close REPLY;
298         exit;
299 }
300
301 # Now let's check if we know this person.
302 ($user, $priv, $server) = &getuserinfo($gpguser);
303
304 if (! defined($user) || ! $user) {
305         print REPLY "Unknown user.\n";
306         close REPLY;
307         exit;
308 }
309
310 print REPLY "Got user '$user'\n";
311
312 # Right. We know this is a valid user. Get a lock to ensure we have exclusive
313 # access to the configs from here on in.
314 open (LOCKFILE,">$lockfile") ||
315          &fatalerror("Couldn't open lock file\n");
316 &fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
317
318 # Ok, now we should figure out what domains we already know about.
319 foreach my $cfgfile (@cfgfiles) {
320         getzones($cfgfile);
321 }
322
323 # Force existance of the $domainlistroot$user file
324 if (! -e $domainlistroot.$user) {
325         open (DOMAINLIST, ">>$domainlistroot$user") or
326                         &fatalerror("Couldn't create domains file.\n");
327         close DOMAINLIST;
328 }
329
330 foreach (@COMMANDS) {
331         # Remove trailing CRs and leading/trailing whitespace
332         chomp;
333         s/\r//;
334         s/^\s*//;
335         s/\s*$//;
336
337         if ($inprocess) {
338                 print REPLY ">>>$_\n";
339         }
340
341         if (/^$/) {
342                 #
343                 # Empty line, so ignore it.
344                 # 
345         } elsif (/^END$/) {
346                 $inprocess=0;
347         } elsif (/^BEGIN$/) {
348                 $inprocess=1;
349         } elsif ($inprocess && /^ADD\s+(.*)$/) {
350                 $domain = $1;
351
352                 # Convert domain to lower case.
353                 $domain =~ tr/[A-Z]/[a-z]/;
354                 if (! valid_domain($domain)) {
355                         $domain =~ s/[-a-z0-9.]//g;
356                         print REPLY "Invalid character(s) in domain name: $domain\n";
357                 } elsif (defined($zones{$domain}) && $zones{$domain}) {
358                         print REPLY "We already secondary $domain\n";
359                 } else {
360                         print REPLY "Adding domain $domain\n";
361                         $zones{$domain}=1;
362
363                         my $df = $domain;
364                         $df =~ tr,/,:,;
365
366                         open (DOMAINSFILE, ">>$conffile");
367                         print DOMAINSFILE "
368 ### Domain added for '$user'
369
370 zone \"$domain\" {
371         type slave;
372         masters { $server; };
373         file \"secondary/$user/$df\";
374         allow-transfer { none; };
375         allow-query { any; };
376 };\n";
377                         close DOMAINSFILE;
378
379                         open (DOMAINLIST, ">>$domainlistroot$user") or
380                                 &fatalerror("Couldn't open file.\n");
381                         print DOMAINLIST "$domain\n";
382                         close DOMAINLIST;
383                         $addcount++;
384                 }
385         } elsif ($inprocess && /^DEL\s(.*)$/) {
386                 $domain = $1;
387
388                 # Convert domain to lower case.
389                 $domain =~ tr/[A-Z]/[a-z]/;
390                 if (!valid_domain($domain)) {
391                         $domain =~ s/[-a-z0-9.]//g;
392                         print REPLY "Invalid character(s) in domain name: $domain\n";
393                 } elsif (!defined($zones{$domain}) || !$zones{$domain}) {
394                                 print REPLY "$domain does not exist!\n";
395                 } else {
396                         print REPLY "Deleting domain $domain\n";
397                         my (@newcfg,$found);
398
399                         open (DOMAINLIST, "<$domainlistroot$user") or
400                                 &fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
401                         my @cfg = <DOMAINLIST>;
402                         close(DOMAINLIST);
403                         @newcfg = grep { ! /^$domain$/ } @cfg;
404                         if (scalar @cfg == scalar @newcfg) {
405                                 print REPLY "Didn't find $domain in $domainlistroot$user!\n";
406                                 print REPLY "You are only allowed to delete your own domains that exist.\n";
407                                 next;
408                         }
409
410                         open (DOMAINLIST, ">$domainlistroot$user") or 
411                                 &fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
412                         print DOMAINLIST @newcfg;
413                         close DOMAINLIST;
414
415                         $found=0;
416                         @newcfg=();
417                         open (DOMAINSFILE, "<$conffile") or
418                                 &fatalerror("Couldn't open file $conffile for reading: $!\n");
419                         {
420                         local $/ = ''; # eat whole paragraphs
421                         while (<DOMAINSFILE>) {
422                                 unless (/^\s*zone\s+"$domain"/) {
423                                         push @newcfg, $_;
424                                 } else {
425                                         $found=1;
426                                         if ($newcfg[-1] =~ /^###/) {
427                                                 # remove comment and \n
428                                                 pop @newcfg;
429                                         }
430                                 }
431                         }
432                         } # end of paragraph eating
433
434                         if (!$found) {
435                                 print REPLY "Didn't find $domain in $conffile!\n";
436                                 next;
437                         }
438
439                         open (DOMAINSFILE, ">$conffile") or
440                                 &fatalerror("Couldn't open $conffile for writing: $!\n");
441                         print DOMAINSFILE @newcfg;
442                         close DOMAINSFILE;
443                         $delcount++;
444                         $zones{$domain} = 0;
445                 }
446         } elsif ($inprocess && /^LIST$/) {
447                 print REPLY "Listing domains for user $user\n";
448                 print REPLY "------\n";
449                 if (open (DOMAINLIST, "<$domainlistroot$user")) {
450                         my $count = 0;
451                         while (<DOMAINLIST>) {
452                                 $count++;
453                                 print REPLY;
454                         }
455                         close (DOMAINLIST);
456                         print REPLY "------\n";
457                         print REPLY "Total of $count domains.\n";
458                 } else {
459                         print REPLY "Couldn't open $domainlistroot$user: $!\n";
460                 }
461         } elsif ($inprocess && /^MASTER\s(.*)$/) {
462                 if (($priv & 1) != 1) {
463                         print REPLY "You're not authorised to use the MASTER ",
464                                 "command.\n";
465                 } elsif ($1 =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
466                         $server = $1;
467                         print REPLY "Set master IP address to $1\n";
468                 } else {
469                         print REPLY "$1 doesn't look like a valid IPv4 ",
470                                 "address to me.\n";
471                 }
472         } elsif ($inprocess && /^HELP$/) {
473                 print REPLY "In order to use the service, you will need to send GPG signed\n";
474                 print REPLY "messages.\n\n";
475                 print REPLY "The format of the text in these messages is important, as they represent\n";
476                 print REPLY "commands to autodns. Commands are formatted one per line, and enclosed\n";
477                 print REPLY "by \"BEGIN\" and \"END\" commands (without the quotes).\n";
478                 print REPLY "Current valid commands are:\n";
479                 print REPLY "BEGIN - begin processing.\n";
480                 print REPLY "END - end processing.\n";
481                 print REPLY "HELP - display this message.\n";
482                 print REPLY "LIST - show all the zones currently held by you.\n";
483                 print REPLY "ADD <domain> - adds the domain <domain> for processing.\n";
484                 print REPLY "DEL <domain> - removes the domain <domain> if you own it.\n";
485         } elsif ($inprocess) {
486                 print REPLY "Unknown command!\n";
487         }
488 }
489 flock(LOCKFILE, LOCK_UN);
490 close(LOCKFILE);
491 unlink($lockfile);
492
493 print REPLY "Added $addcount domains.\n" if $addcount;
494 print REPLY "Removed $delcount domains.\n" if $delcount;
495 if ($addcount || $delcount) {
496         print REPLY "Reloading nameserver config.\n";
497         print REPLY `$reload_command`;
498 }
499 close REPLY;
500
501 exit 0;