3 # Copyright 1999-2004 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.6 2005/04/08 11:37:16 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, $usersfile, $lockfile, $priv);
20 my ($user, $server, $inprocess, $delcount, $addcount, $reload_command);
21 my ($domain, @MAIL, @GPGERROR, @COMMANDS, %zones);
22 my ($me, $ccreply, $conffile, $domainlistroot, @cfgfiles, $VERSION);
27 # Local configuration here (until it gets moved to a config file).
29 # These are sort of suitable for a Debian setup.
32 # Who I should reply as.
33 $me="autodns\@earth.li";
35 # Who replies should be CCed to.
36 $ccreply="noodles\@earth.li";
38 # Where to look for zones we're already hosting.
39 @cfgfiles=("/etc/bind/named.conf",
40 "/etc/bind/named.secondary.conf");
42 # The file we should add/delete domains from.
43 $conffile="/etc/bind/named.secondary.conf";
45 # The file that contains details of the authorized users.
46 $usersfile="/etc/bind/autodns.users";
48 # Base file name to for list of users domains.
49 $domainlistroot="/etc/bind/domains.";
51 # The lockfile we use to ensure we have exclusive access to the
52 # $domainlistroot$user files and $conffile.
53 $lockfile="/etc/bind/autodns.lck";
55 # The command to reload the nameserver domains list.
56 $reload_command="sudo ndc reconfig 2>&1";
59 ### There should be no need to edit anything below (unless you're not
60 ### using BIND). This statement might even be true now - let me know if not.
64 # Try to figure out what zones we currently know about by parsing config
65 # files. Sets the item in %zones to 1 for each zone it finds.
67 # Call with the name of a config file to read:
69 # &getzones("/etc/named.conf");
74 open (NAMEDCONF, "< $namedfile") or
75 &fatalerror("Can't open $namedfile");
78 if (/^\s*zone\s*"([^"]+)"/) {
87 # Check that a domain is only made up of valid characters.
89 # These are: a-z, 0-9, - or .
94 if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,4}$/) {
96 } elsif ($domain =~ /^(?:[0-9\/-]+\.)+in-addr.arpa$/) {
104 # Deal with a fatal error by printing an error message, closing the pipe to
105 # sendmail and exiting.
107 # fatalerror("I'm melting!");
112 print REPLY $message;
115 flock(LOCKFILE, LOCK_UN);
124 # Get user details from usersfile based on a PGP ID.
126 # A users entry looks like:
128 # <username>:<keyid>:<priviledge level>:<master server ip>
130 # Priviledge level is not currently used.
132 # ($user, $priv, $server) = &getuserinfo("5B430367");
136 my ($user, $priviledge, $server);
138 open (CONFIGFILE, "< $usersfile") or
139 &fatalerror("Couldn't open user configuration file.");
141 foreach (<CONFIGFILE>) {
142 if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
150 if ($user !~ /^.+$/) {
152 &fatalerror("Error in user configuration file: Can't get username.\n");
155 if ($server !~ /^(\d{1,3}\.){3}\d{1,3}$/) {
156 $server =~ s/\d\.]//g;
158 &fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
166 &fatalerror("User not found.\n");
169 return ($user, $priviledge, $server);
172 $delcount=$addcount=$inprocess=0;
174 # Read in the mail from stdin.
177 $subject = "Reply from AutoDNS";
178 # Now lets try to find out who it's from.
181 if (/^From: (.*)/i) { $from=$1; chomp $from;}
182 if (/^Subject:\s+(re:)?(.*)$/i) { $subject="Re: ".$2 if ($2);}
185 if ((! defined($from)) || $from =~ /^$/ ) {
186 die "Couldn't find a from address.";
187 } elsif ($from =~ /mailer-daemon@/i) {
188 die "From address is mailer-daemon, ignoring.";
191 if (! defined($subject)) { $subject="Reply from AutoDNS"; };
193 # We've got a from address. Start a reply.
195 open(REPLY, "|sendmail -t -oem -oi") or die "Couldn't spawn sendmail";
197 print REPLY "From: $me\n";
198 print REPLY "To: $from\n";
200 # Check to see if our CC address is the same as the from address and if so
203 if ($from ne $ccreply) {
204 print REPLY "Cc: $ccreply\n";
210 Copyright 1999-2004 Project Purple. Written by Jonathan McDowell.
211 Released under the GPL.
216 # Throw the mail at MIME::Parser and see if it accepts it.
218 my $parser = new MIME::Parser;
219 $parser->output_to_core(1); # No temporary files
220 my $entity = $parser->parse_data(\@MAIL);
223 # Make sure locale is set to C so we get messages in English as we expect.
227 if ($entity->parts) {
230 my ($got_sig, $got_text) = (0, 0);
231 my ($sig_name,$sig_fh,$text_name,$text_fh);
232 ($sig_fh, $sig_name) = tempfile();
233 ($text_fh, $text_name) = tempfile();
235 foreach my $subent ($entity->parts) {
236 if ($subent->effective_type eq "text/plain") {
237 @COMMANDS = split /\n/,$subent->bodyhandle->as_string;
239 my $str = $subent->as_string;
245 } elsif ($subent->effective_type eq
246 "application/pgp-signature") {
247 print $sig_fh $subent->as_string;
253 if ($got_sig && $got_text) {
254 open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch --verify ".
255 $sig_name." ".$text_name);
260 my @GPGOUTPUT=<GPGOUT>;
270 open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
276 # And grab what it has to say.
283 # Check who it's from and if the signature was a good one.
285 foreach (@GPGERROR) {
287 if (/Signature made.* (.*)$/) {
291 print REPLY "Some errors ocurred\n";
292 } elsif (/BAD signature/) {
294 print REPLY "BAD signature!\n";
295 } elsif (/public key not found/) {
297 print REPLY "Public Key not found\n";
301 # If we have an empty user it probably wasn't signed.
303 print REPLY "Message appears not to be GPG signed.\n";
308 # Check the signature we got was ok.
310 print REPLY "Good GPG signature found. ($gpguser)\n";
312 print REPLY "Bad GPG signature!\n";
317 # Now let's check if we know this person.
318 ($user, $priv, $server) = &getuserinfo($gpguser);
320 if (! defined($user) || ! $user) {
321 print REPLY "Unknown user.\n";
326 print REPLY "Got user '$user'\n";
328 # Right. We know this is a valid user. Get a lock to ensure we have exclusive
329 # access to the configs from here on in.
330 open (LOCKFILE,">$lockfile") ||
331 &fatalerror("Couldn't open lock file\n");
332 &fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
334 # Ok, now we should figure out what domains we already know about.
335 foreach my $cfgfile (@cfgfiles) {
339 # Force existance of the $domainlistroot$user file
340 if (! -e $domainlistroot.$user) {
341 open (DOMAINLIST, ">>$domainlistroot$user") or
342 &fatalerror("Couldn't create domains file.\n");
346 foreach (@COMMANDS) {
347 # Remove trailing CRs and leading/trailing whitespace
354 print REPLY ">>>$_\n";
359 # Empty line, so ignore it.
363 } elsif (/^BEGIN$/) {
365 } elsif ($inprocess && /^ADD\s+(.*)$/) {
368 # Convert domain to lower case.
369 $domain =~ tr/[A-Z]/[a-z]/;
370 if (! valid_domain($domain)) {
371 $domain =~ s/[-a-z0-9.]//g;
372 print REPLY "Invalid character(s) in domain name: $domain\n";
373 } elsif (defined($zones{$domain}) && $zones{$domain}) {
374 print REPLY "We already secondary $domain\n";
376 print REPLY "Adding domain $domain\n";
382 open (DOMAINSFILE, ">>$conffile");
384 ### Domain added for '$user'
388 masters { $server; };
389 file \"secondary/$user/$df\";
390 allow-transfer { none; };
391 allow-query { any; };
395 open (DOMAINLIST, ">>$domainlistroot$user") or
396 &fatalerror("Couldn't open file.\n");
397 print DOMAINLIST "$domain\n";
401 } elsif ($inprocess && /^DEL\s(.*)$/) {
404 # Convert domain to lower case.
405 $domain =~ tr/[A-Z]/[a-z]/;
406 if (!valid_domain($domain)) {
407 $domain =~ s/[-a-z0-9.]//g;
408 print REPLY "Invalid character(s) in domain name: $domain\n";
409 } elsif (!defined($zones{$domain}) || !$zones{$domain}) {
410 print REPLY "$domain does not exist!\n";
412 print REPLY "Deleting domain $domain\n";
415 open (DOMAINLIST, "<$domainlistroot$user") or
416 &fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
417 my @cfg = <DOMAINLIST>;
419 @newcfg = grep { ! /^$domain$/ } @cfg;
420 if (scalar @cfg == scalar @newcfg) {
421 print REPLY "Didn't find $domain in $domainlistroot$user!\n";
422 print REPLY "You are only allowed to delete your own domains that exist.\n";
426 open (DOMAINLIST, ">$domainlistroot$user") or
427 &fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
428 print DOMAINLIST @newcfg;
433 open (DOMAINSFILE, "<$conffile") or
434 &fatalerror("Couldn't open file $conffile for reading: $!\n");
436 local $/ = ''; # eat whole paragraphs
437 while (<DOMAINSFILE>) {
438 unless (/^\s*zone\s+"$domain"/) {
442 if ($newcfg[-1] =~ /^###/) {
443 # remove comment and \n
448 } # end of paragraph eating
451 print REPLY "Didn't find $domain in $conffile!\n";
455 open (DOMAINSFILE, ">$conffile") or
456 &fatalerror("Couldn't open $conffile for writing: $!\n");
457 print DOMAINSFILE @newcfg;
462 } elsif ($inprocess && /^LIST$/) {
463 print REPLY "Listing domains for user $user\n";
464 print REPLY "------\n";
465 if (open (DOMAINLIST, "<$domainlistroot$user")) {
467 while (<DOMAINLIST>) {
472 print REPLY "------\n";
473 print REPLY "Total of $count domains.\n";
475 print REPLY "Couldn't open $domainlistroot$user: $!\n";
477 } elsif ($inprocess && /^HELP$/) {
478 print REPLY "In order to use the service, you will need to send GPG signed\n";
479 print REPLY "messages.\n\n";
480 print REPLY "The format of the text in these messages is important, as they represent\n";
481 print REPLY "commands to autodns. Commands are formatted one per line, and enclosed\n";
482 print REPLY "by \"BEGIN\" and \"END\" commands (without the quotes).\n";
483 print REPLY "Current valid commands are:\n";
484 print REPLY "BEGIN - begin processing.\n";
485 print REPLY "END - end processing.\n";
486 print REPLY "HELP - display this message.\n";
487 print REPLY "LIST - show all the zones currently held by you.\n";
488 print REPLY "ADD <domain> - adds the domain <domain> for processing.\n";
489 print REPLY "DEL <domain> - removes the domain <domain> if you own it.\n";
490 } elsif ($inprocess) {
491 print REPLY "Unknown command!\n";
494 flock(LOCKFILE, LOCK_UN);
498 print REPLY "Added $addcount domains.\n" if $addcount;
499 print REPLY "Removed $delcount domains.\n" if $delcount;
500 if ($addcount || $delcount) {
501 print REPLY "Reloading nameserver config.\n";
502 print REPLY `$reload_command`;