]> the.earth.li Git - autodns.git/commitdiff
Allow adding of new users via email.
authorJonathan McDowell <noodles@earth.li>
Thu, 2 Nov 2006 12:21:33 +0000 (12:21 +0000)
committerJonathan McDowell <noodles@earth.li>
Thu, 2 Nov 2006 12:21:33 +0000 (12:21 +0000)
Introduce the ADDUSER command to allow adding new users via email.

git-archimport-id: noodles@earth.li--pie/autodns--mainline--1.0--patch-20

autodns.conf
autodns.pl

index 56e09f811cbc515628de71495675687200f0649b..f221cc8a3e74058a8a2f60fa0c089efd490660a1 100644 (file)
@@ -35,3 +35,7 @@ $reload_command="sudo ndc reconfig 2>&1";
 # signature can be +/- this many seconds from current time before being
 # rejected.
 $expiry = 7200;
+
+# The directory that secondary zone files live in; we'll create <username>
+# directorys underneath this.
+$zonefiledir = "/var/cache/bind/secondary";
index 04d0d514a660d9edf847268375d051b3a4c36261..edaf5fbd62feb5a5c5079fe9eeb302c84bb3e54d 100755 (executable)
@@ -11,6 +11,7 @@
 use strict;
 use Date::Parse;
 use Fcntl qw(:flock);
+use File::Path;
 use File::Temp qw(tempfile);
 use IPC::Open3;
 use MIME::Parser;
@@ -22,7 +23,7 @@ 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);
+       $lockfile $reload_command $expiry $zonefiledir);
 
 $VERSION="0.0.8";
 
@@ -108,7 +109,7 @@ 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.
 #
@@ -116,7 +117,7 @@ sub fatalerror($) {
 #
 sub getuserinfo($) {
        my $gpguser = shift;
-       my ($user, $priviledge, $server);
+       my ($user, $privilege, $server);
 
        open (CONFIGFILE, "< $usersfile") or
                fatalerror("Couldn't open user configuration file.");
@@ -124,10 +125,10 @@ sub getuserinfo($) {
        foreach (<CONFIGFILE>) {
                if (/^([^#.]+):$gpguser:(\d+):(.+)$/) {
                        $user=$1;
-                       $priviledge=$2;
+                       $privilege=$2;
                        $server=$3;
                        chomp $user;
-                       chomp $priviledge;
+                       chomp $privilege;
                        chomp $server;
        
                        if ($user !~ /^.+$/) {
@@ -149,7 +150,36 @@ sub getuserinfo($) {
                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;
@@ -230,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);
+                               }
+                       }
                }
        }
 
@@ -489,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";
@@ -504,7 +580,12 @@ zone \"$domain\" {
                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 commands.\n";
+                       " 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";