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.7 2005/04/08 11:45:12 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 open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch --verify ".
234 $sig_name." ".$text_name);
239 my @GPGOUTPUT=<GPGOUT>;
249 open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
255 # And grab what it has to say.
262 # Check who it's from and if the signature was a good one.
264 foreach (@GPGERROR) {
266 if (/Signature made.* (.*)$/) {
270 print REPLY "Some errors ocurred\n";
271 } elsif (/BAD signature/) {
273 print REPLY "BAD signature!\n";
274 } elsif (/public key not found/) {
276 print REPLY "Public Key not found\n";
280 # If we have an empty user it probably wasn't signed.
282 print REPLY "Message appears not to be GPG signed.\n";
287 # Check the signature we got was ok.
289 print REPLY "Good GPG signature found. ($gpguser)\n";
291 print REPLY "Bad GPG signature!\n";
296 # Now let's check if we know this person.
297 ($user, $priv, $server) = &getuserinfo($gpguser);
299 if (! defined($user) || ! $user) {
300 print REPLY "Unknown user.\n";
305 print REPLY "Got user '$user'\n";
307 # Right. We know this is a valid user. Get a lock to ensure we have exclusive
308 # access to the configs from here on in.
309 open (LOCKFILE,">$lockfile") ||
310 &fatalerror("Couldn't open lock file\n");
311 &fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
313 # Ok, now we should figure out what domains we already know about.
314 foreach my $cfgfile (@cfgfiles) {
318 # Force existance of the $domainlistroot$user file
319 if (! -e $domainlistroot.$user) {
320 open (DOMAINLIST, ">>$domainlistroot$user") or
321 &fatalerror("Couldn't create domains file.\n");
325 foreach (@COMMANDS) {
326 # Remove trailing CRs and leading/trailing whitespace
333 print REPLY ">>>$_\n";
338 # Empty line, so ignore it.
342 } elsif (/^BEGIN$/) {
344 } elsif ($inprocess && /^ADD\s+(.*)$/) {
347 # Convert domain to lower case.
348 $domain =~ tr/[A-Z]/[a-z]/;
349 if (! valid_domain($domain)) {
350 $domain =~ s/[-a-z0-9.]//g;
351 print REPLY "Invalid character(s) in domain name: $domain\n";
352 } elsif (defined($zones{$domain}) && $zones{$domain}) {
353 print REPLY "We already secondary $domain\n";
355 print REPLY "Adding domain $domain\n";
361 open (DOMAINSFILE, ">>$conffile");
363 ### Domain added for '$user'
367 masters { $server; };
368 file \"secondary/$user/$df\";
369 allow-transfer { none; };
370 allow-query { any; };
374 open (DOMAINLIST, ">>$domainlistroot$user") or
375 &fatalerror("Couldn't open file.\n");
376 print DOMAINLIST "$domain\n";
380 } elsif ($inprocess && /^DEL\s(.*)$/) {
383 # Convert domain to lower case.
384 $domain =~ tr/[A-Z]/[a-z]/;
385 if (!valid_domain($domain)) {
386 $domain =~ s/[-a-z0-9.]//g;
387 print REPLY "Invalid character(s) in domain name: $domain\n";
388 } elsif (!defined($zones{$domain}) || !$zones{$domain}) {
389 print REPLY "$domain does not exist!\n";
391 print REPLY "Deleting domain $domain\n";
394 open (DOMAINLIST, "<$domainlistroot$user") or
395 &fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
396 my @cfg = <DOMAINLIST>;
398 @newcfg = grep { ! /^$domain$/ } @cfg;
399 if (scalar @cfg == scalar @newcfg) {
400 print REPLY "Didn't find $domain in $domainlistroot$user!\n";
401 print REPLY "You are only allowed to delete your own domains that exist.\n";
405 open (DOMAINLIST, ">$domainlistroot$user") or
406 &fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
407 print DOMAINLIST @newcfg;
412 open (DOMAINSFILE, "<$conffile") or
413 &fatalerror("Couldn't open file $conffile for reading: $!\n");
415 local $/ = ''; # eat whole paragraphs
416 while (<DOMAINSFILE>) {
417 unless (/^\s*zone\s+"$domain"/) {
421 if ($newcfg[-1] =~ /^###/) {
422 # remove comment and \n
427 } # end of paragraph eating
430 print REPLY "Didn't find $domain in $conffile!\n";
434 open (DOMAINSFILE, ">$conffile") or
435 &fatalerror("Couldn't open $conffile for writing: $!\n");
436 print DOMAINSFILE @newcfg;
441 } elsif ($inprocess && /^LIST$/) {
442 print REPLY "Listing domains for user $user\n";
443 print REPLY "------\n";
444 if (open (DOMAINLIST, "<$domainlistroot$user")) {
446 while (<DOMAINLIST>) {
451 print REPLY "------\n";
452 print REPLY "Total of $count domains.\n";
454 print REPLY "Couldn't open $domainlistroot$user: $!\n";
456 } elsif ($inprocess && /^HELP$/) {
457 print REPLY "In order to use the service, you will need to send GPG signed\n";
458 print REPLY "messages.\n\n";
459 print REPLY "The format of the text in these messages is important, as they represent\n";
460 print REPLY "commands to autodns. Commands are formatted one per line, and enclosed\n";
461 print REPLY "by \"BEGIN\" and \"END\" commands (without the quotes).\n";
462 print REPLY "Current valid commands are:\n";
463 print REPLY "BEGIN - begin processing.\n";
464 print REPLY "END - end processing.\n";
465 print REPLY "HELP - display this message.\n";
466 print REPLY "LIST - show all the zones currently held by you.\n";
467 print REPLY "ADD <domain> - adds the domain <domain> for processing.\n";
468 print REPLY "DEL <domain> - removes the domain <domain> if you own it.\n";
469 } elsif ($inprocess) {
470 print REPLY "Unknown command!\n";
473 flock(LOCKFILE, LOCK_UN);
477 print REPLY "Added $addcount domains.\n" if $addcount;
478 print REPLY "Removed $delcount domains.\n" if $delcount;
479 if ($addcount || $delcount) {
480 print REPLY "Reloading nameserver config.\n";
481 print REPLY `$reload_command`;