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.11 2005/05/16 17:26:47 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");
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 .
75 if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,4}$/) {
77 } elsif ($domain =~ /^(?:[0-9\/-]+\.)+in-addr.arpa$/) {
85 # Deal with a fatal error by printing an error message, closing the pipe to
86 # sendmail and exiting.
88 # fatalerror("I'm melting!");
96 flock(LOCKFILE, LOCK_UN);
105 # Get user details from usersfile based on a PGP ID.
107 # A users entry looks like:
109 # <username>:<keyid>:<priviledge level>:<master server ip>
111 # Priviledge level is not currently used.
113 # ($user, $priv, $server) = &getuserinfo("5B430367");
117 my ($user, $priviledge, $server);
119 open (CONFIGFILE, "< $usersfile") or
120 &fatalerror("Couldn't open user configuration file.");
122 foreach (<CONFIGFILE>) {
123 if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
131 if ($user !~ /^.+$/) {
133 &fatalerror("Error in user configuration file: Can't get username.\n");
136 if ($server !~ /^(\d{1,3}\.){3}\d{1,3}$/) {
137 $server =~ s/\d\.]//g;
139 &fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
147 &fatalerror("User not found.\n");
150 return ($user, $priviledge, $server);
153 $delcount=$addcount=$inprocess=0;
155 # Read in the mail from stdin.
158 $subject = "Reply from AutoDNS";
159 # Now lets try to find out who it's from.
162 if (/^From: (.*)/i) { $from=$1; chomp $from;}
163 if (/^Subject:\s+(re:)?(.*)$/i) { $subject="Re: ".$2 if ($2);}
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.";
172 if (! defined($subject)) { $subject="Reply from AutoDNS"; };
174 # We've got a from address. Start a reply.
176 open(REPLY, "|sendmail -t -oem -oi") or die "Couldn't spawn sendmail";
178 print REPLY "From: $me\n";
179 print REPLY "To: $from\n";
181 # Check to see if our CC address is the same as the from address and if so
184 if ($from ne $ccreply) {
185 print REPLY "Cc: $ccreply\n";
191 Copyright 1999-2004 Project Purple. Written by Jonathan McDowell.
192 Released under the GPL.
197 # Throw the mail at MIME::Parser and see if it accepts it.
199 my $parser = new MIME::Parser;
200 $parser->output_to_core(1); # No temporary files
201 my $entity = $parser->parse_data(\@MAIL);
204 # Make sure locale is set to C so we get messages in English as we expect.
208 if ($entity->parts) {
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();
216 foreach my $subent ($entity->parts) {
217 if ($subent->effective_type eq "text/plain") {
218 @COMMANDS = split /\n/,$subent->bodyhandle->as_string;
220 my $str = $subent->as_string;
226 } elsif ($subent->effective_type eq
227 "application/pgp-signature") {
228 print $sig_fh $subent->as_string;
234 if ($got_sig && $got_text) {
235 my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR,
236 "gpg --batch --verify ".
237 $sig_name." ".$text_name);
242 my @GPGOUTPUT=<GPGOUT>;
253 my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
259 # And grab what it has to say.
267 # Check who it's from and if the signature was a good one.
269 foreach (@GPGERROR) {
271 if (/Signature made.* (.*)$/) {
275 print REPLY "Some errors ocurred\n";
276 } elsif (/BAD signature/) {
278 print REPLY "BAD signature!\n";
279 } elsif (/public key not found/) {
281 print REPLY "Public Key not found\n";
285 # If we have an empty user it probably wasn't signed.
287 print REPLY "Message appears not to be GPG signed.\n";
292 # Check the signature we got was ok.
294 print REPLY "Good GPG signature found. ($gpguser)\n";
296 print REPLY "Bad GPG signature!\n";
301 # Now let's check if we know this person.
302 ($user, $priv, $server) = &getuserinfo($gpguser);
304 if (! defined($user) || ! $user) {
305 print REPLY "Unknown user.\n";
310 print REPLY "Got user '$user'\n";
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));
318 # Ok, now we should figure out what domains we already know about.
319 foreach my $cfgfile (@cfgfiles) {
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");
330 foreach (@COMMANDS) {
331 # Remove trailing CRs and leading/trailing whitespace
338 print REPLY ">>>$_\n";
343 # Empty line, so ignore it.
347 } elsif (/^BEGIN$/) {
349 } elsif ($inprocess && /^ADD\s+(.*)$/) {
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";
360 print REPLY "Adding domain $domain\n";
366 open (DOMAINSFILE, ">>$conffile");
368 ### Domain added for '$user'
372 masters { $server; };
373 file \"secondary/$user/$df\";
374 allow-transfer { none; };
375 allow-query { any; };
379 open (DOMAINLIST, ">>$domainlistroot$user") or
380 &fatalerror("Couldn't open file.\n");
381 print DOMAINLIST "$domain\n";
385 } elsif ($inprocess && /^DEL\s(.*)$/) {
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";
396 print REPLY "Deleting domain $domain\n";
399 open (DOMAINLIST, "<$domainlistroot$user") or
400 &fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
401 my @cfg = <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";
410 open (DOMAINLIST, ">$domainlistroot$user") or
411 &fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
412 print DOMAINLIST @newcfg;
417 open (DOMAINSFILE, "<$conffile") or
418 &fatalerror("Couldn't open file $conffile for reading: $!\n");
420 local $/ = ''; # eat whole paragraphs
421 while (<DOMAINSFILE>) {
422 unless (/^\s*zone\s+"$domain"/) {
426 if ($newcfg[-1] =~ /^###/) {
427 # remove comment and \n
432 } # end of paragraph eating
435 print REPLY "Didn't find $domain in $conffile!\n";
439 open (DOMAINSFILE, ">$conffile") or
440 &fatalerror("Couldn't open $conffile for writing: $!\n");
441 print DOMAINSFILE @newcfg;
446 } elsif ($inprocess && /^LIST$/) {
447 print REPLY "Listing domains for user $user\n";
448 print REPLY "------\n";
449 if (open (DOMAINLIST, "<$domainlistroot$user")) {
451 while (<DOMAINLIST>) {
456 print REPLY "------\n";
457 print REPLY "Total of $count domains.\n";
459 print REPLY "Couldn't open $domainlistroot$user: $!\n";
461 } elsif ($inprocess && /^MASTER\s(.*)$/) {
462 if (($priv & 1) != 1) {
463 print REPLY "You're not authorised to use the MASTER ",
465 } elsif ($1 =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
467 print REPLY "Set master IP address to $1\n";
469 print REPLY "$1 doesn't look like a valid IPv4 ",
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";
489 flock(LOCKFILE, LOCK_UN);
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`;