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.10 2005/05/16 17:24:10 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);
29 my $file = '/etc/bind/autodns.conf';
30 unless (my $ret = do $file) {
31 warn "Couldn't parse $file\n" if $@;
32 warn "Couldn't do $file\n" unless defined $ret;
33 warn "Couldn't run $file\n" unless $ret;
34 die "Problem reading config file!\n";
38 ### There should be no need to edit anything below (unless you're not
39 ### using BIND). This statement might even be true now - let me know if not.
43 # Try to figure out what zones we currently know about by parsing config
44 # files. Sets the item in %zones to 1 for each zone it finds.
46 # Call with the name of a config file to read:
48 # &getzones("/etc/named.conf");
53 open (NAMEDCONF, "< $namedfile") or
54 &fatalerror("Can't open $namedfile");
57 if (/^\s*zone\s*"([^"]+)"/) {
66 # Check that a domain is only made up of valid characters.
68 # These are: a-z, 0-9, - or .
73 if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,4}$/) {
75 } elsif ($domain =~ /^(?:[0-9\/-]+\.)+in-addr.arpa$/) {
83 # Deal with a fatal error by printing an error message, closing the pipe to
84 # sendmail and exiting.
86 # fatalerror("I'm melting!");
94 flock(LOCKFILE, LOCK_UN);
103 # Get user details from usersfile based on a PGP ID.
105 # A users entry looks like:
107 # <username>:<keyid>:<priviledge level>:<master server ip>
109 # Priviledge level is not currently used.
111 # ($user, $priv, $server) = &getuserinfo("5B430367");
115 my ($user, $priviledge, $server);
117 open (CONFIGFILE, "< $usersfile") or
118 &fatalerror("Couldn't open user configuration file.");
120 foreach (<CONFIGFILE>) {
121 if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
129 if ($user !~ /^.+$/) {
131 &fatalerror("Error in user configuration file: Can't get username.\n");
134 if ($server !~ /^(\d{1,3}\.){3}\d{1,3}$/) {
135 $server =~ s/\d\.]//g;
137 &fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
145 &fatalerror("User not found.\n");
148 return ($user, $priviledge, $server);
151 $delcount=$addcount=$inprocess=0;
153 # Read in the mail from stdin.
156 $subject = "Reply from AutoDNS";
157 # Now lets try to find out who it's from.
160 if (/^From: (.*)/i) { $from=$1; chomp $from;}
161 if (/^Subject:\s+(re:)?(.*)$/i) { $subject="Re: ".$2 if ($2);}
164 if ((! defined($from)) || $from =~ /^$/ ) {
165 die "Couldn't find a from address.";
166 } elsif ($from =~ /mailer-daemon@/i) {
167 die "From address is mailer-daemon, ignoring.";
170 if (! defined($subject)) { $subject="Reply from AutoDNS"; };
172 # We've got a from address. Start a reply.
174 open(REPLY, "|sendmail -t -oem -oi") or die "Couldn't spawn sendmail";
176 print REPLY "From: $me\n";
177 print REPLY "To: $from\n";
179 # Check to see if our CC address is the same as the from address and if so
182 if ($from ne $ccreply) {
183 print REPLY "Cc: $ccreply\n";
189 Copyright 1999-2004 Project Purple. Written by Jonathan McDowell.
190 Released under the GPL.
195 # Throw the mail at MIME::Parser and see if it accepts it.
197 my $parser = new MIME::Parser;
198 $parser->output_to_core(1); # No temporary files
199 my $entity = $parser->parse_data(\@MAIL);
202 # Make sure locale is set to C so we get messages in English as we expect.
206 if ($entity->parts) {
209 my ($got_sig, $got_text) = (0, 0);
210 my ($sig_name,$sig_fh,$text_name,$text_fh);
211 ($sig_fh, $sig_name) = tempfile();
212 ($text_fh, $text_name) = tempfile();
214 foreach my $subent ($entity->parts) {
215 if ($subent->effective_type eq "text/plain") {
216 @COMMANDS = split /\n/,$subent->bodyhandle->as_string;
218 my $str = $subent->as_string;
224 } elsif ($subent->effective_type eq
225 "application/pgp-signature") {
226 print $sig_fh $subent->as_string;
232 if ($got_sig && $got_text) {
233 my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR,
234 "gpg --batch --verify ".
235 $sig_name." ".$text_name);
240 my @GPGOUTPUT=<GPGOUT>;
251 my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
257 # And grab what it has to say.
265 # Check who it's from and if the signature was a good one.
267 foreach (@GPGERROR) {
269 if (/Signature made.* (.*)$/) {
273 print REPLY "Some errors ocurred\n";
274 } elsif (/BAD signature/) {
276 print REPLY "BAD signature!\n";
277 } elsif (/public key not found/) {
279 print REPLY "Public Key not found\n";
283 # If we have an empty user it probably wasn't signed.
285 print REPLY "Message appears not to be GPG signed.\n";
290 # Check the signature we got was ok.
292 print REPLY "Good GPG signature found. ($gpguser)\n";
294 print REPLY "Bad GPG signature!\n";
299 # Now let's check if we know this person.
300 ($user, $priv, $server) = &getuserinfo($gpguser);
302 if (! defined($user) || ! $user) {
303 print REPLY "Unknown user.\n";
308 print REPLY "Got user '$user'\n";
310 # Right. We know this is a valid user. Get a lock to ensure we have exclusive
311 # access to the configs from here on in.
312 open (LOCKFILE,">$lockfile") ||
313 &fatalerror("Couldn't open lock file\n");
314 &fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
316 # Ok, now we should figure out what domains we already know about.
317 foreach my $cfgfile (@cfgfiles) {
321 # Force existance of the $domainlistroot$user file
322 if (! -e $domainlistroot.$user) {
323 open (DOMAINLIST, ">>$domainlistroot$user") or
324 &fatalerror("Couldn't create domains file.\n");
328 foreach (@COMMANDS) {
329 # Remove trailing CRs and leading/trailing whitespace
336 print REPLY ">>>$_\n";
341 # Empty line, so ignore it.
345 } elsif (/^BEGIN$/) {
347 } elsif ($inprocess && /^ADD\s+(.*)$/) {
350 # Convert domain to lower case.
351 $domain =~ tr/[A-Z]/[a-z]/;
352 if (! valid_domain($domain)) {
353 $domain =~ s/[-a-z0-9.]//g;
354 print REPLY "Invalid character(s) in domain name: $domain\n";
355 } elsif (defined($zones{$domain}) && $zones{$domain}) {
356 print REPLY "We already secondary $domain\n";
358 print REPLY "Adding domain $domain\n";
364 open (DOMAINSFILE, ">>$conffile");
366 ### Domain added for '$user'
370 masters { $server; };
371 file \"secondary/$user/$df\";
372 allow-transfer { none; };
373 allow-query { any; };
377 open (DOMAINLIST, ">>$domainlistroot$user") or
378 &fatalerror("Couldn't open file.\n");
379 print DOMAINLIST "$domain\n";
383 } elsif ($inprocess && /^DEL\s(.*)$/) {
386 # Convert domain to lower case.
387 $domain =~ tr/[A-Z]/[a-z]/;
388 if (!valid_domain($domain)) {
389 $domain =~ s/[-a-z0-9.]//g;
390 print REPLY "Invalid character(s) in domain name: $domain\n";
391 } elsif (!defined($zones{$domain}) || !$zones{$domain}) {
392 print REPLY "$domain does not exist!\n";
394 print REPLY "Deleting domain $domain\n";
397 open (DOMAINLIST, "<$domainlistroot$user") or
398 &fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
399 my @cfg = <DOMAINLIST>;
401 @newcfg = grep { ! /^$domain$/ } @cfg;
402 if (scalar @cfg == scalar @newcfg) {
403 print REPLY "Didn't find $domain in $domainlistroot$user!\n";
404 print REPLY "You are only allowed to delete your own domains that exist.\n";
408 open (DOMAINLIST, ">$domainlistroot$user") or
409 &fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
410 print DOMAINLIST @newcfg;
415 open (DOMAINSFILE, "<$conffile") or
416 &fatalerror("Couldn't open file $conffile for reading: $!\n");
418 local $/ = ''; # eat whole paragraphs
419 while (<DOMAINSFILE>) {
420 unless (/^\s*zone\s+"$domain"/) {
424 if ($newcfg[-1] =~ /^###/) {
425 # remove comment and \n
430 } # end of paragraph eating
433 print REPLY "Didn't find $domain in $conffile!\n";
437 open (DOMAINSFILE, ">$conffile") or
438 &fatalerror("Couldn't open $conffile for writing: $!\n");
439 print DOMAINSFILE @newcfg;
444 } elsif ($inprocess && /^LIST$/) {
445 print REPLY "Listing domains for user $user\n";
446 print REPLY "------\n";
447 if (open (DOMAINLIST, "<$domainlistroot$user")) {
449 while (<DOMAINLIST>) {
454 print REPLY "------\n";
455 print REPLY "Total of $count domains.\n";
457 print REPLY "Couldn't open $domainlistroot$user: $!\n";
459 } elsif ($inprocess && /^MASTER\s(.*)$/) {
460 if (($priv & 1) != 1) {
461 print REPLY "You're not authorised to use the MASTER ",
463 } elsif ($1 =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
465 print REPLY "Set master IP address to $1\n";
467 print REPLY "$1 doesn't look like a valid IPv4 ",
470 } elsif ($inprocess && /^HELP$/) {
471 print REPLY "In order to use the service, you will need to send GPG signed\n";
472 print REPLY "messages.\n\n";
473 print REPLY "The format of the text in these messages is important, as they represent\n";
474 print REPLY "commands to autodns. Commands are formatted one per line, and enclosed\n";
475 print REPLY "by \"BEGIN\" and \"END\" commands (without the quotes).\n";
476 print REPLY "Current valid commands are:\n";
477 print REPLY "BEGIN - begin processing.\n";
478 print REPLY "END - end processing.\n";
479 print REPLY "HELP - display this message.\n";
480 print REPLY "LIST - show all the zones currently held by you.\n";
481 print REPLY "ADD <domain> - adds the domain <domain> for processing.\n";
482 print REPLY "DEL <domain> - removes the domain <domain> if you own it.\n";
483 } elsif ($inprocess) {
484 print REPLY "Unknown command!\n";
487 flock(LOCKFILE, LOCK_UN);
491 print REPLY "Added $addcount domains.\n" if $addcount;
492 print REPLY "Removed $delcount domains.\n" if $delcount;
493 if ($addcount || $delcount) {
494 print REPLY "Reloading nameserver config.\n";
495 print REPLY `$reload_command`;