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.
8 # $Id: autodns.pl,v 1.12 2005/05/31 12:20:23 noodles Exp $
13 use File::Temp qw(tempfile);
17 $ENV{'PATH'}="/usr/local/bin:/usr/bin:/bin:/usr/sbin";
19 my ($from, $subject, $gpguser, $gpggood, $priv);
20 my ($user, $server, $inprocess, $delcount, $addcount);
21 my ($domain, @MAIL, @GPGERROR, @COMMANDS, %zones, $VERSION);
23 use vars qw($me $ccreply $conffile $domainlistroot @cfgfiles $usersfile
24 $lockfile $reload_command);
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";
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.
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.
48 # Call with the name of a config file to read:
50 # getzones("/etc/named.conf");
53 my $namedfile = shift;
55 open (NAMEDCONF, "< $namedfile") or
56 fatalerror("Can't open $namedfile");
59 if (/^\s*zone\s*"([^"]+)"/) {
68 # Check that a domain is only made up of valid characters.
70 # These are: a-z, 0-9, - or .
76 if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,4}$/) {
78 } elsif ($domain =~ /^(?:[0-9\/-]+\.)+in-addr.arpa$/) {
86 # Deal with a fatal error by printing an error message, closing the pipe to
87 # sendmail and exiting.
89 # fatalerror("I'm melting!");
97 flock(LOCKFILE, LOCK_UN);
106 # Get user details from usersfile based on a PGP ID.
108 # A users entry looks like:
110 # <username>:<keyid>:<priviledge level>:<master server ip>
112 # Priviledge level is not currently used.
114 # ($user, $priv, $server) = getuserinfo("5B430367");
118 my ($user, $priviledge, $server);
120 open (CONFIGFILE, "< $usersfile") or
121 fatalerror("Couldn't open user configuration file.");
123 foreach (<CONFIGFILE>) {
124 if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
132 if ($user !~ /^.+$/) {
134 fatalerror("Error in user configuration file: Can't get username.\n");
137 if ($server !~ /^(\d{1,3}\.){3}\d{1,3}$/) {
138 $server =~ s/\d\.]//g;
140 fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
148 fatalerror("User not found.\n");
151 return ($user, $priviledge, $server);
154 $delcount=$addcount=$inprocess=0;
156 # Read in the mail from stdin.
159 $subject = "Reply from AutoDNS";
160 # Now lets try to find out who it's from.
163 if (/^From: (.*)/i) { $from=$1; chomp $from;}
164 if (/^Subject:\s+(re:)?(.*)$/i) { $subject="Re: ".$2 if ($2);}
167 if ((! defined($from)) || $from =~ /^$/ ) {
168 die "Couldn't find a from address.";
169 } elsif ($from =~ /mailer-daemon@/i) {
170 die "From address is mailer-daemon, ignoring.";
173 if (! defined($subject)) { $subject="Reply from AutoDNS"; };
175 # We've got a from address. Start a reply.
177 open(REPLY, "|sendmail -t -oem -oi") or die "Couldn't spawn sendmail";
179 print REPLY "From: $me\n";
180 print REPLY "To: $from\n";
182 # Check to see if our CC address is the same as the from address and if so
185 if ($from ne $ccreply) {
186 print REPLY "Cc: $ccreply\n";
192 Copyright 1999-2004 Project Purple. Written by Jonathan McDowell.
193 Released under the GPL.
198 # Throw the mail at MIME::Parser and see if it accepts it.
200 my $parser = new MIME::Parser;
201 $parser->output_to_core(1); # No temporary files
202 my $entity = $parser->parse_data(\@MAIL);
205 # Make sure locale is set to C so we get messages in English as we expect.
209 if ($entity->parts) {
212 my ($got_sig, $got_text) = (0, 0);
213 my ($sig_name,$sig_fh,$text_name,$text_fh);
214 ($sig_fh, $sig_name) = tempfile();
215 ($text_fh, $text_name) = tempfile();
217 foreach my $subent ($entity->parts) {
218 if ($subent->effective_type eq "text/plain") {
219 @COMMANDS = split /\n/,$subent->bodyhandle->as_string;
221 my $str = $subent->as_string;
227 } elsif ($subent->effective_type eq
228 "application/pgp-signature") {
229 print $sig_fh $subent->as_string;
235 if ($got_sig && $got_text) {
236 my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR,
237 "gpg --batch --verify ".
238 $sig_name." ".$text_name);
243 my @GPGOUTPUT=<GPGOUT>;
254 my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
260 # And grab what it has to say.
268 # Check who it's from and if the signature was a good one.
270 foreach (@GPGERROR) {
272 if (/Signature made.* (.*)$/) {
276 print REPLY "Some errors ocurred\n";
277 } elsif (/BAD signature/) {
279 print REPLY "BAD signature!\n";
280 } elsif (/public key not found/) {
282 print REPLY "Public Key not found\n";
286 # If we have an empty user it probably wasn't signed.
288 print REPLY "Message appears not to be GPG signed.\n";
293 # Check the signature we got was ok.
295 print REPLY "Good GPG signature found. ($gpguser)\n";
297 print REPLY "Bad GPG signature!\n";
302 # Now let's check if we know this person.
303 ($user, $priv, $server) = getuserinfo($gpguser);
305 if (! defined($user) || ! $user) {
306 print REPLY "Unknown user.\n";
311 print REPLY "Got user '$user'\n";
313 # Right. We know this is a valid user. Get a lock to ensure we have exclusive
314 # access to the configs from here on in.
315 open (LOCKFILE,">$lockfile") ||
316 fatalerror("Couldn't open lock file\n");
317 fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
319 # Ok, now we should figure out what domains we already know about.
320 foreach my $cfgfile (@cfgfiles) {
324 # Force existance of the $domainlistroot$user file
325 if (! -e $domainlistroot.$user) {
326 open (DOMAINLIST, ">>$domainlistroot$user") or
327 fatalerror("Couldn't create domains file.\n");
331 foreach (@COMMANDS) {
332 # Remove trailing CRs and leading/trailing whitespace
339 print REPLY ">>>$_\n";
344 # Empty line, so ignore it.
348 } elsif (/^BEGIN$/) {
350 } elsif ($inprocess && /^ADD\s+(.*)$/) {
353 # Convert domain to lower case.
354 $domain =~ tr/[A-Z]/[a-z]/;
355 if (! valid_domain($domain)) {
356 $domain =~ s/[-a-z0-9.]//g;
357 print REPLY "Invalid character(s) in domain name: $domain\n";
358 } elsif (defined($zones{$domain}) && $zones{$domain}) {
359 print REPLY "We already secondary $domain\n";
361 print REPLY "Adding domain $domain\n";
367 open (DOMAINSFILE, ">>$conffile");
369 ### Domain added for '$user'
373 masters { $server; };
374 file \"secondary/$user/$df\";
375 allow-transfer { none; };
376 allow-query { any; };
380 open (DOMAINLIST, ">>$domainlistroot$user") or
381 fatalerror("Couldn't open file.\n");
382 print DOMAINLIST "$domain\n";
386 } elsif ($inprocess && /^DEL\s(.*)$/) {
389 # Convert domain to lower case.
390 $domain =~ tr/[A-Z]/[a-z]/;
391 if (!valid_domain($domain)) {
392 $domain =~ s/[-a-z0-9.]//g;
393 print REPLY "Invalid character(s) in domain name: $domain\n";
394 } elsif (!defined($zones{$domain}) || !$zones{$domain}) {
395 print REPLY "$domain does not exist!\n";
397 print REPLY "Deleting domain $domain\n";
400 open (DOMAINLIST, "<$domainlistroot$user") or
401 fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
402 my @cfg = <DOMAINLIST>;
404 @newcfg = grep { ! /^$domain$/ } @cfg;
405 if (scalar @cfg == scalar @newcfg) {
406 print REPLY "Didn't find $domain in $domainlistroot$user!\n";
407 print REPLY "You are only allowed to delete your own domains that exist.\n";
411 open (DOMAINLIST, ">$domainlistroot$user") or
412 fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
413 print DOMAINLIST @newcfg;
418 open (DOMAINSFILE, "<$conffile") or
419 fatalerror("Couldn't open file $conffile for reading: $!\n");
421 local $/ = ''; # eat whole paragraphs
422 while (<DOMAINSFILE>) {
423 unless (/^\s*zone\s+"$domain"/) {
427 if ($newcfg[-1] =~ /^###/) {
428 # remove comment and \n
433 } # end of paragraph eating
436 print REPLY "Didn't find $domain in $conffile!\n";
440 open (DOMAINSFILE, ">$conffile") or
441 fatalerror("Couldn't open $conffile for writing: $!\n");
442 print DOMAINSFILE @newcfg;
447 } elsif ($inprocess && /^LIST$/) {
448 print REPLY "Listing domains for user $user\n";
449 print REPLY "------\n";
450 if (open (DOMAINLIST, "<$domainlistroot$user")) {
452 while (<DOMAINLIST>) {
457 print REPLY "------\n";
458 print REPLY "Total of $count domains.\n";
460 print REPLY "Couldn't open $domainlistroot$user: $!\n";
462 } elsif ($inprocess && /^MASTER\s(.*)$/) {
463 if (($priv & 1) != 1) {
464 print REPLY "You're not authorised to use the MASTER ",
466 } elsif ($1 =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
468 print REPLY "Set master IP address to $1\n";
470 print REPLY "$1 doesn't look like a valid IPv4 ",
473 } elsif ($inprocess && /^HELP$/) {
474 print REPLY "In order to use the service, you will need to send GPG signed\n";
475 print REPLY "messages.\n\n";
476 print REPLY "The format of the text in these messages is important, as they represent\n";
477 print REPLY "commands to autodns. Commands are formatted one per line, and enclosed\n";
478 print REPLY "by \"BEGIN\" and \"END\" commands (without the quotes).\n";
479 print REPLY "Current valid commands are:\n";
480 print REPLY "BEGIN - begin processing.\n";
481 print REPLY "END - end processing.\n";
482 print REPLY "HELP - display this message.\n";
483 print REPLY "LIST - show all the zones currently held by you.\n";
484 print REPLY "ADD <domain> - adds the domain <domain> for processing.\n";
485 print REPLY "DEL <domain> - removes the domain <domain> if you own it.\n";
486 } elsif ($inprocess) {
487 print REPLY "Unknown command!\n";
490 flock(LOCKFILE, LOCK_UN);
494 print REPLY "Added $addcount domains.\n" if $addcount;
495 print REPLY "Removed $delcount domains.\n" if $delcount;
496 if ($addcount || $delcount) {
497 print REPLY "Reloading nameserver config.\n";
498 print REPLY `$reload_command`;