]> the.earth.li Git - autodns.git/blobdiff - autodns.pl
cscvs to tla changeset 5
[autodns.git] / autodns.pl
index afc6c5bb8d732488430b63fba646e4a985fc57ea..67377a3025ee36bcb4cc875361b95a55e4ca2774 100755 (executable)
@@ -1,16 +1,18 @@
 #!/usr/bin/perl -Tw
-# autodns 0.0.6
-# Copyright 1999-2003 Project Purple. Written by Jonathan McDowell
+# autodns 0.0.7
+# Copyright 1999-2004 Project Purple. Written by Jonathan McDowell
 # See ACKNOWLEDGEMENTS file for full details of contributors.
 # http://www.earth.li/projectpurple/progs/autodns.html
 # Released under the GPL.
 #
-# $Id: autodns.pl,v 1.2 2003/06/04 17:27:00 noodles Exp $
+# $Id: autodns.pl,v 1.5 2005/03/21 15:11:01 noodles Exp $
 #
 
 use strict;
-use IPC::Open3;
 use Fcntl qw(:flock);
+use File::Temp qw(tempfile);
+use IPC::Open3;
+use MIME::Parser;
 
 $ENV{'PATH'}="/usr/local/bin:/usr/bin:/bin:/usr/sbin";
 
@@ -19,7 +21,7 @@ my ($user, $server, $inprocess, $delcount, $addcount, $reload_command);
 my ($domain, @MAIL, @GPGERROR, @COMMANDS, %zones);
 my ($me, $ccreply, $conffile, $domainlistroot, @cfgfiles, $VERSION);
 
-$VERSION="0.0.6";
+$VERSION="0.0.7";
 
 #
 # Local configuration here (until it gets moved to a config file).
@@ -205,27 +207,78 @@ print REPLY <<EOF;
 Subject: $subject
 
 AutoDNS $VERSION
-Copyright 1999-2001 Project Purple. Written by Jonathan McDowell.
+Copyright 1999-2004 Project Purple. Written by Jonathan McDowell.
 Released under the GPL.
 
 EOF
 
 #
-# Now run GPG against our incoming mail, first making sure that our locale is
-# set to C so that we get the messages in English as we expect.
+# Throw the mail at MIME::Parser and see if it accepts it.
+#
+my $parser = new MIME::Parser;
+$parser->output_to_core(1); # No temporary files
+my $entity = $parser->parse_data(\@MAIL);
+
+#
+# Make sure locale is set to C so we get messages in English as we expect.
 #
 $ENV{'LC_ALL'}="C";
-open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
 
-# Feed it the mail.
-print GPGIN @MAIL;
-close GPGIN;
+if ($entity->parts) {
+       # MIME
+
+       my ($got_sig, $got_text) = (0, 0);
+       my ($sig_name,$sig_fh,$text_name,$text_fh);
+       ($sig_fh, $sig_name) = tempfile();
+       ($text_fh, $text_name) = tempfile();
+
+       foreach my $subent ($entity->parts) {
+               if ($subent->effective_type eq "text/plain") {
+                       @COMMANDS = split /\n/,$subent->bodyhandle->as_string;
+
+                       my $str = $subent->as_string;
+                       $str =~ s/=\n$//;
+                       $str =~ s/\n/\r\n/g;
+                       print $text_fh $str;
+                       close($text_fh);
+                       $got_text++;
+               } elsif ($subent->effective_type eq
+                               "application/pgp-signature") {
+                       print $sig_fh $subent->as_string;
+                       close($sig_fh);
+                       $got_sig++;
+               }
+       }
+
+       if ($got_sig && $got_text) {
+               open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch --verify ".
+                       $sig_name." ".$text_name);
+
+               close GPGIN;
 
-# And grab what it has to say.
-@GPGERROR=<GPGERR>;
-@COMMANDS=<GPGOUT>;
-close GPGERR;
-close GPGOUT;
+               @GPGERROR=<GPGERR>;
+               my @GPGOUTPUT=<GPGOUT>;
+               close GPGERR;
+               close GPGOUT;
+
+               unlink($text_name);
+               unlink($sig_name);
+       }
+} else {
+       # Clear text.
+
+       open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch");
+
+       # Feed it the mail.
+       print GPGIN @MAIL;
+       close GPGIN;
+
+       # And grab what it has to say.
+       @GPGERROR=<GPGERR>;
+       @COMMANDS=<GPGOUT>;
+       close GPGERR;
+       close GPGOUT;
+}
 
 # Check who it's from and if the signature was a good one.
 $gpggood=1;
@@ -316,6 +369,9 @@ foreach (@COMMANDS) {
                        print REPLY "Adding domain $domain\n";
                        $zones{$domain}=1;
 
+                       my $df = $domain;
+                       $df =~ tr,/,:,;
+
                        open (DOMAINSFILE, ">>$conffile");
                        print DOMAINSFILE "
 ### Domain added for '$user'
@@ -323,7 +379,7 @@ foreach (@COMMANDS) {
 zone \"$domain\" {
        type slave;
        masters { $server; };
-       file \"secondary/$user/$domain\";
+       file \"secondary/$user/$df\";
        allow-transfer { none; };
        allow-query { any; };
 };\n";