]> the.earth.li Git - autodns.git/blobdiff - autodns.pl
Allow adding of new users via email.
[autodns.git] / autodns.pl
index e221c09b407a12ef9b648889953b4e85bf93e59f..edaf5fbd62feb5a5c5079fe9eeb302c84bb3e54d 100755 (executable)
@@ -5,21 +5,25 @@
 # http://www.earth.li/projectpurple/progs/autodns.html
 # Released under the GPL.
 #
-# $Id: autodns.pl,v 1.10 2005/05/16 17:24:10 noodles Exp $
+# $Id: autodns.pl,v 1.15 2005/06/15 10:26:25 noodles Exp $
 #
 
 use strict;
+use Date::Parse;
 use Fcntl qw(:flock);
+use File::Path;
 use File::Temp qw(tempfile);
 use IPC::Open3;
 use MIME::Parser;
 
 $ENV{'PATH'}="/usr/local/bin:/usr/bin:/bin:/usr/sbin";
 
-my ($from, $subject, $gpguser, $gpggood, $usersfile, $lockfile, $priv);
-my ($user, $server, $inprocess, $delcount, $addcount, $reload_command);
-my ($domain, @MAIL, @GPGERROR, @COMMANDS, %zones);
-my ($me, $ccreply, $conffile, $domainlistroot, @cfgfiles, $VERSION);
+my ($from, $subject, $gpguser, $gpggood, $priv);
+my ($user, $server, $inprocess, $delcount, $addcount);
+my ($domain, @MAIL, @GPGERROR, @COMMANDS, %zones, $VERSION);
+
+use vars qw($me $ccreply $conffile $domainlistroot @cfgfiles $usersfile
+       $lockfile $reload_command $expiry $zonefiledir);
 
 $VERSION="0.0.8";
 
@@ -45,13 +49,13 @@ unless (my $ret = do $file) {
 #
 # Call with the name of a config file to read:
 #
-# &getzones("/etc/named.conf");
+# getzones("/etc/named.conf");
 #
-sub getzones {
-       my ($namedfile) = @_;
+sub getzones($) {
+       my $namedfile = shift;
 
        open (NAMEDCONF, "< $namedfile") or
-               &fatalerror("Can't open $namedfile");
+               fatalerror("Can't open $namedfile");
 
        while (<NAMEDCONF>) {
                if (/^\s*zone\s*"([^"]+)"/) {
@@ -67,10 +71,11 @@ sub getzones {
 #
 # These are: a-z, 0-9, - or .
 #
-sub valid_domain {
+sub valid_domain($) {
        my $domain = shift;
        $domain = lc $domain;
-       if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,4}$/) {
+
+       if ($domain =~ /^(?:[a-z0-9-]+\.)+[a-z]{2,6}$/) {
                return 1;
        } elsif ($domain =~ /^(?:[0-9\/-]+\.)+in-addr.arpa$/) {
                return 1;
@@ -85,7 +90,7 @@ sub valid_domain {
 #
 # fatalerror("I'm melting!");
 #
-sub fatalerror {
+sub fatalerror($) {
        my $message = shift;
 
        print REPLY $message;
@@ -104,37 +109,37 @@ sub fatalerror {
 #
 # A users entry looks like:
 #
-# <username>:<keyid>:<priviledge level>:<master server ip>
+# <username>:<keyid>:<privilege level>:<master server ip>
 #
 # Priviledge level is not currently used.
 #
-# ($user, $priv, $server) = &getuserinfo("5B430367");
+# ($user, $priv, $server) = getuserinfo("5B430367");
 #
-sub getuserinfo {
+sub getuserinfo($) {
        my $gpguser = shift;
-       my ($user, $priviledge, $server);
+       my ($user, $privilege, $server);
 
        open (CONFIGFILE, "< $usersfile") or
-               &fatalerror("Couldn't open user configuration file.");
+               fatalerror("Couldn't open user configuration file.");
 
        foreach (<CONFIGFILE>) {
                if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
                        $user=$1;
-                       $priviledge=$2;
+                       $privilege=$2;
                        $server=$3;
                        chomp $user;
-                       chomp $priviledge;
+                       chomp $privilege;
                        chomp $server;
        
                        if ($user !~ /^.+$/) {
                                close(CONFIGFILE);
-                               &fatalerror("Error in user configuration file: Can't get username.\n");
+                               fatalerror("Error in user configuration file: Can't get username.\n");
                        }
 
                        if ($server !~ /^(\d{1,3}\.){3}\d{1,3}$/) {
                                $server =~ s/\d\.]//g;
                                close(CONFIGFILE); 
-                               &fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
+                               fatalerror("Error in user configuration file: Invalid primary server IP address ($server)\n");
                                exit;
                        } 
                }
@@ -142,10 +147,39 @@ sub getuserinfo {
        close(CONFIGFILE);
 
        if ($user =~ /^$/) {
-               &fatalerror("User not found.\n");
+               fatalerror("User not found.\n");
        }
 
-       return ($user, $priviledge, $server);
+       return ($user, $privilege, $server);
+}
+
+#
+# Add a new AutoDNS user.
+#
+# addautodnsuser($username, $keyid, $priv, $masterip);
+# <username>:<keyid>:<privilege level>:<master server ip>
+#
+sub addautodnsuser($$$$) {
+       my $username = shift;
+       my $keyid = shift;
+       my $priv = shift;
+       my $masterip = shift;
+
+       # Create domains file for the user.
+       open (DOMAINLIST, ">>$domainlistroot$username") or
+                       fatalerror("Couldn't create domains file.\n");
+       close DOMAINLIST;
+
+       # Make the directory for the zone files.
+       my @dirs = mkpath("$zonefiledir/$username", 0, 0775);
+       fatalerror("Couldn't create zone file directory.\n")
+                       if scalar(@dirs) == 0;
+
+       # Actually add them to the users file.
+       open(USERFILE, ">> $usersfile") or
+               fatalerror("Couldn't open user configuration file.");
+       print USERFILE "$username:$keyid:$priv:$masterip\n";
+       close(USERFILE);
 }
 
 $delcount=$addcount=$inprocess=0;
@@ -226,6 +260,23 @@ if ($entity->parts) {
                        print $sig_fh $subent->as_string;
                        close($sig_fh);
                        $got_sig++;
+               } elsif ($subent->effective_type eq "multipart/mixed") {
+                       my $str = $subent->as_string;
+                       print $text_fh $str;
+                       close($text_fh);
+                       $got_text++;
+       
+                       foreach my $mixent ($subent->parts) {
+                               if ($mixent->effective_type eq "text/plain") {
+                                       push @COMMANDS, (split /\n/,
+                                               $mixent->bodyhandle->as_string);
+                               }
+                               if ($mixent->effective_type eq
+                                               "application/pgp-keys") {
+                                       push @COMMANDS, (split /\n/,
+                                               $mixent->bodyhandle->as_string);
+                               }
+                       }
                }
        }
 
@@ -248,15 +299,16 @@ if ($entity->parts) {
 } else {
        # Clear text.
 
-       my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
+       my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch --verify");
 
        # Feed it the mail.
-       print GPGIN @MAIL;
+       print GPGIN $entity->bodyhandle->as_string;
        close GPGIN;
 
        # And grab what it has to say.
        @GPGERROR=<GPGERR>;
        @COMMANDS=<GPGOUT>;
+       @COMMANDS = split /\n/,$entity->bodyhandle->as_string;
        close GPGERR;
        close GPGOUT;
        waitpid $pid, 0;
@@ -264,10 +316,12 @@ if ($entity->parts) {
 
 # Check who it's from and if the signature was a good one.
 $gpggood=1;
+my $sigtime = 0;
 foreach (@GPGERROR) {
        chomp;
-       if (/Signature made.* (.*)$/) {
-               $gpguser=$1; 
+       if (/Signature made (.*) using.*ID (.*)$/) {
+               $sigtime = str2time($1);
+               $gpguser=$2;
        } elsif (/error/) {
                $gpggood = 0;
                print REPLY "Some errors ocurred\n";
@@ -296,8 +350,23 @@ if ($gpggood) {
        exit;
 }
 
+# Check if the signature is outside our acceptable range.
+if (!defined($sigtime)) {
+       print REPLY "Couldn't parse signature time.\n";
+       close REPLY;
+       exit;
+} elsif ($sigtime > (time + $expiry)) {
+       print REPLY "Signature too far into the future.\n";
+       close REPLY;
+       exit;
+} elsif ($sigtime < (time - $expiry)) {
+       print REPLY "Signature too far into the past.\n";
+       close REPLY;
+       exit;
+}
+
 # Now let's check if we know this person.
-($user, $priv, $server) = &getuserinfo($gpguser);
+($user, $priv, $server) = getuserinfo($gpguser);
 
 if (! defined($user) || ! $user) {
        print REPLY "Unknown user.\n";
@@ -310,8 +379,8 @@ print REPLY "Got user '$user'\n";
 # Right. We know this is a valid user. Get a lock to ensure we have exclusive
 # access to the configs from here on in.
 open (LOCKFILE,">$lockfile") ||
-        &fatalerror("Couldn't open lock file\n");
-&fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
+        fatalerror("Couldn't open lock file\n");
+fatalerror("Couldn't get lock\n") unless(flock(LOCKFILE,LOCK_EX));
 
 # Ok, now we should figure out what domains we already know about.
 foreach my $cfgfile (@cfgfiles) {
@@ -321,7 +390,7 @@ foreach my $cfgfile (@cfgfiles) {
 # Force existance of the $domainlistroot$user file
 if (! -e $domainlistroot.$user) {
        open (DOMAINLIST, ">>$domainlistroot$user") or
-                       &fatalerror("Couldn't create domains file.\n");
+                       fatalerror("Couldn't create domains file.\n");
        close DOMAINLIST;
 }
 
@@ -375,7 +444,7 @@ zone \"$domain\" {
                        close DOMAINSFILE;
 
                        open (DOMAINLIST, ">>$domainlistroot$user") or
-                               &fatalerror("Couldn't open file.\n");
+                               fatalerror("Couldn't open file.\n");
                        print DOMAINLIST "$domain\n";
                        close DOMAINLIST;
                        $addcount++;
@@ -395,7 +464,7 @@ zone \"$domain\" {
                        my (@newcfg,$found);
 
                        open (DOMAINLIST, "<$domainlistroot$user") or
-                               &fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
+                               fatalerror("Couldn't open file $domainlistroot$user for reading: $!.\n");
                        my @cfg = <DOMAINLIST>;
                        close(DOMAINLIST);
                        @newcfg = grep { ! /^$domain$/ } @cfg;
@@ -406,14 +475,14 @@ zone \"$domain\" {
                        }
 
                        open (DOMAINLIST, ">$domainlistroot$user") or 
-                               &fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
+                               fatalerror("Couldn't open file $domainlistroot$user for writing: $!.\n");
                        print DOMAINLIST @newcfg;
                        close DOMAINLIST;
 
                        $found=0;
                        @newcfg=();
                        open (DOMAINSFILE, "<$conffile") or
-                               &fatalerror("Couldn't open file $conffile for reading: $!\n");
+                               fatalerror("Couldn't open file $conffile for reading: $!\n");
                        {
                        local $/ = ''; # eat whole paragraphs
                        while (<DOMAINSFILE>) {
@@ -435,7 +504,7 @@ zone \"$domain\" {
                        }
 
                        open (DOMAINSFILE, ">$conffile") or
-                               &fatalerror("Couldn't open $conffile for writing: $!\n");
+                               fatalerror("Couldn't open $conffile for writing: $!\n");
                        print DOMAINSFILE @newcfg;
                        close DOMAINSFILE;
                        $delcount++;
@@ -467,6 +536,35 @@ zone \"$domain\" {
                        print REPLY "$1 doesn't look like a valid IPv4 ",
                                "address to me.\n";
                }
+       } elsif ($inprocess && /^ADDUSER\s(.*)$/) {
+               if (($priv & 2) != 2) {
+                       print REPLY "You're not authorised to use the ",
+                               "ADDUSER command.\n";
+               } elsif ($1 =~ /^([a-z0-9]+) ([A-Fa-f0-9]{8}) (\d+) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
+                       addautodnsuser($1, $2, $3, $4);
+
+                       print REPLY "Attempting to import new key:\n";
+
+                       # Feed our command mail to GPG so we can pull the
+                       # (hopefully included) new GPG key out from it.
+                       my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR,
+                                       "gpg --batch --import");
+
+                       # Feed it the mail.
+                       print GPGIN join("\n", @COMMANDS);
+                       close GPGIN;
+
+                       # And grab what it has to say.
+                       @GPGERROR=<GPGERR>;
+                       my @GPGOUTPUT=<GPGOUT>;
+                       close GPGERR;
+                       close GPGOUT;
+                       waitpid $pid, 0;
+
+                       print REPLY @GPGERROR;
+               } else {
+                       print REPLY "ADDUSER parameter error.\n";
+               }
        } elsif ($inprocess && /^HELP$/) {
                print REPLY "In order to use the service, you will need to send GPG signed\n";
                print REPLY "messages.\n\n";
@@ -480,6 +578,15 @@ zone \"$domain\" {
                print REPLY "LIST - show all the zones currently held by you.\n";
                print REPLY "ADD <domain> - adds the domain <domain> for processing.\n";
                print REPLY "DEL <domain> - removes the domain <domain> if you own it.\n";
+               if (($priv & 1) == 1) {
+                       print REPLY "MASTER <ip address> - set the nameserver".
+                       " we should slave off for subsequent ADD\ncommands.\n";
+               }
+               if (($priv & 2) == 2) {
+                       print REPLY "ADDUSER <username> <keyid> <privilege> ",
+                               "<masterip> - add a new user. Imports any key",
+                               "\nattached to the message into the keyring.\n";
+               }
        } elsif ($inprocess) {
                print REPLY "Unknown command!\n";
        }