3 # Copyright 1999-2001 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.
12 $ENV{'PATH'}="/usr/local/bin:/usr/bin:/bin:/usr/sbin";
14 my ($from, $subject, $gpguser, $gpggood, $usersfile, $lockfile, $priv);
15 my ($user, $server, $inprocess, $delcount, $addcount, $reload_command);
16 my ($domain, @MAIL, @GPGERROR, @COMMANDS, %zones);
17 my ($me, $ccreply, $conffile, $domainlistroot, @cfgfiles, $VERSION);
22 # Local configuration here (until it gets moved to a config file).
24 # These are sort of suitable for a Debian setup.
27 # Who I should reply as.
28 $me="autodns\@earth.li";
30 # Who replies should be CCed to.
31 $ccreply="noodles\@earth.li";
33 # Where to look for zones we're already hosting.
34 @cfgfiles=("/etc/bind/named.conf",
35 "/etc/bind/named.secondary.conf");
37 # The file we should add/delete domains from.
38 $conffile="/etc/bind/named.secondary.conf";
40 # The file that contains details of the authorized users.
41 $usersfile="/etc/bind/autodns.users";
43 # Base file name to for list of users domains.
44 $domainlistroot="/etc/bind/domains.";
46 # The lockfile we use to ensure we have exclusive access to the
47 # $domainlistroot$user files and $conffile.
48 $lockfile="/etc/bind/autodns.lck";
50 # The command to reload the nameserver domains list.
51 $reload_command="sudo ndc reconfig 2>&1";
54 ### There should be no need to edit anything below (unless you're not
55 ### using BIND). This statement might even be true now - let me know if not.
59 # Try to figure out what zones we currently know about by parsing config
60 # files. Sets the item in %zones to 1 for each zone it finds.
62 # Call with the name of a config file to read:
64 # &getzones("/etc/named.conf");
69 open (NAMEDCONF, "< $namedfile") or
70 &fatalerror("Can't open $namedfile");
73 if (/^\s*zone\s*"([^"]+)"/) {
82 # Check that a domain is only made up of valid characters.
84 # These are: a-z, 0-9, - or .
89 if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,4}$/) {
97 # Deal with a fatal error by printing an error message, closing the pipe to
98 # sendmail and exiting.
100 # fatalerror("I'm melting!");
105 print REPLY $message;
108 flock(LOCKFILE, LOCK_UN);
117 # Get user details from usersfile based on a PGP ID.
119 # A users entry looks like:
121 # <username>:<keyid>:<priviledge level>:<master server ip>
123 # Priviledge level is not currently used.
125 # ($user, $priv, $server) = &getuserinfo("5B430367");
129 my ($user, $priviledge, $server);
131 open (CONFIGFILE, "< $usersfile") or
132 &fatalerror("Couldn't open user configuration file.");
134 foreach (<CONFIGFILE>) {
135 if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
143 if ($user !~ /^.+$/) {
145 &fatalerror("Error in user configuration file: Can't get username.\n");
148 if ($server !~ /^(\d{1,3}\.){3}\d{1,3}$/) {
149 $server =~ s/\d\.]//g;
151 &fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
159 &fatalerror("User not found.\n");
162 return ($user, $priviledge, $server);
165 $delcount=$addcount=$inprocess=0;
167 # Read in the mail from stdin.
170 $subject = "Reply from AutoDNS";
171 # Now lets try to find out who it's from.
174 if (/^From: (.*)/i) { $from=$1; chomp $from;}
175 if (/^Subject:\s+(re:)?(.*)$/i) { $subject="Re: ".$2 if ($2);}
178 if ((! defined($from)) || $from =~ /^$/ ) {
179 die "Couldn't find a from address.";
180 } elsif ($from =~ /mailer-daemon@/i) {
181 die "From address is mailer-daemon, ignoring.";
184 if (! defined($subject)) { $subject="Reply from AutoDNS"; };
186 # We've got a from address. Start a reply.
188 open(REPLY, "|sendmail -t -oem -oi") or die "Couldn't spawn sendmail";
190 print REPLY "From: $me\n";
191 print REPLY "To: $from\n";
193 # Check to see if our CC address is the same as the from address and if so
196 if ($from ne $ccreply) {
197 print REPLY "Cc: $ccreply\n";
203 Copyright 1999-2001 Project Purple. Written by Jonathan McDowell.
204 Released under the GPL.
209 # Now run GPG against our incoming mail, first making sure that our locale is
210 # set to C so that we get the messages in English as we expect.
213 open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
219 # And grab what it has to say.
225 # Check who it's from and if the signature was a good one.
227 foreach (@GPGERROR) {
229 if (/Signature made.* (.*)$/) {
233 print REPLY "Some errors ocurred\n";
234 } elsif (/BAD signature/) {
236 print REPLY "BAD signature!\n";
237 } elsif (/public key not found/) {
239 print REPLY "Public Key not found\n";
243 # If we have an empty user it probably wasn't signed.
245 print REPLY "Message appears not to be GPG signed.\n";
250 # Check the signature we got was ok.
252 print REPLY "Good GPG signature found. ($gpguser)\n";
254 print REPLY "Bad GPG signature!\n";
259 # Now let's check if we know this person.
260 ($user, $priv, $server) = &getuserinfo($gpguser);
262 if (! defined($user) || ! $user) {
263 print REPLY "Unknown user.\n";
268 print REPLY "Got user '$user'\n";
270 # Right. We know this is a valid user. Get a lock to ensure we have exclusive
271 # access to the configs from here on in.
272 open (LOCKFILE,">$lockfile") ||
273 &fatalerror("Couldn't open lock file\n");
274 &fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
276 # Ok, now we should figure out what domains we already know about.
277 foreach my $cfgfile (@cfgfiles) {
281 foreach (@COMMANDS) {
282 # Remove trailing CRs and leading/trailing whitespace
289 print REPLY ">>>$_\n";
294 # Empty line, so ignore it.
298 } elsif (/^BEGIN$/) {
300 } elsif ($inprocess && /^ADD\s+(.*)$/) {
303 # Convert domain to lower case.
304 $domain =~ tr/[A-Z]/[a-z]/;
305 if (! valid_domain($domain)) {
306 $domain =~ s/[-a-z0-9.]//g;
307 print REPLY "Invalid character(s) in domain name: $domain\n";
308 } elsif (defined($zones{$domain}) && $zones{$domain}) {
309 print REPLY "We already secondary $domain\n";
311 print REPLY "Adding domain $domain\n";
314 open (DOMAINSFILE, ">>$conffile");
316 ### Domain added for '$user'
320 masters { $server; };
321 file \"secondary/$user/$domain\";
322 allow-transfer { none; };
323 allow-query { any; };
327 open (DOMAINLIST, ">>$domainlistroot$user") or
328 &fatalerror("Couldn't open file.\n");
329 print DOMAINLIST "$domain\n";
333 } elsif ($inprocess && /^DEL\s(.*)$/) {
336 # Convert domain to lower case.
337 $domain =~ tr/[A-Z]/[a-z]/;
338 if (!valid_domain($domain)) {
339 $domain =~ s/[-a-z0-9.]//g;
340 print REPLY "Invalid character(s) in domain name: $domain\n";
341 } elsif (!defined($zones{$domain}) || !$zones{$domain}) {
342 print REPLY "$domain does not exist!\n";
344 print REPLY "Deleting domain $domain\n";
347 open (DOMAINLIST, "<$domainlistroot$user") or
348 &fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
349 my @cfg = <DOMAINLIST>;
351 @newcfg = grep { ! /^$domain$/ } @cfg;
352 if (scalar @cfg == scalar @newcfg) {
353 print REPLY "Didn't find $domain in $domainlistroot$user!\n";
354 print REPLY "You are only allowed to delete your own domains that exist.\n";
358 open (DOMAINLIST, ">$domainlistroot$user") or
359 &fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
360 print DOMAINLIST @newcfg;
365 open (DOMAINSFILE, "<$conffile") or
366 &fatalerror("Couldn't open file $conffile for reading: $!\n");
368 local $/ = ''; # eat whole paragraphs
369 while (<DOMAINSFILE>) {
370 unless (/^\s*zone\s+"$domain"/) {
374 if ($newcfg[-1] =~ /^###/) {
375 # remove comment and \n
380 } # end of paragraph eating
383 print REPLY "Didn't find $domain in $conffile!\n";
387 open (DOMAINSFILE, ">$conffile") or
388 &fatalerror("Couldn't open $conffile for writing: $!\n");
389 print DOMAINSFILE @newcfg;
394 } elsif ($inprocess && /^LIST$/) {
395 print REPLY "Listing domains for user $user\n";
396 print REPLY "------\n";
397 if (open (DOMAINLIST, "<$domainlistroot$user")) {
399 while (<DOMAINLIST>) {
404 print REPLY "------\n";
405 print REPLY "Total of $count domains.\n";
407 print REPLY "Couldn't open $domainlistroot$user: $!\n";
409 } elsif ($inprocess && /^HELP$/) {
410 print REPLY "In order to use the service, you will need to send GPG signed\n";
411 print REPLY "messages.\n\n";
412 print REPLY "The format of the text in these messages is important, as they represent\n";
413 print REPLY "commands to autodns. Commands are formatted one per line, and enclosed\n";
414 print REPLY "by \"BEGIN\" and \"END\" commands (without the quotes).\n";
415 print REPLY "Current valid commands are:\n";
416 print REPLY "BEGIN - begin processing.\n";
417 print REPLY "END - end processing.\n";
418 print REPLY "HELP - display this message.\n";
419 print REPLY "LIST - show all the zones currently held by you.\n";
420 print REPLY "ADD <domain> - adds the domain <domain> for processing.\n";
421 print REPLY "DEL <domain> - removes the domain <domain> if you own it.\n";
422 } elsif ($inprocess) {
423 print REPLY "Unknown command!\n";
426 flock(LOCKFILE, LOCK_UN);
430 print REPLY "Added $addcount domains.\n" if $addcount;
431 print REPLY "Removed $delcount domains.\n" if $delcount;
432 if ($addcount || $delcount) {
433 print REPLY "Reloading nameserver config.\n";
434 print REPLY `$reload_command`;