X-Git-Url: https://the.earth.li/gitweb/?a=blobdiff_plain;f=onak-mail.pl;h=e5c0d046c188e1eeb8dcb2a592dcd9ac1f3ba975;hb=7f1c2d9ec737a90d4d216f59ccb8b93f9054c472;hp=0f108f45e48f3fa1d7f980990724d843e399818b;hpb=e02c731dfbb288c736f2cd09a9b6df0507c59ddd;p=onak.git diff --git a/onak-mail.pl b/onak-mail.pl index 0f108f4..e5c0d04 100755 --- a/onak-mail.pl +++ b/onak-mail.pl @@ -1,5 +1,4 @@ #!/usr/bin/perl -w - # # onak-mail.pl - Mail processing interface for onak, an OpenPGP Keyserver. # @@ -7,10 +6,51 @@ # Copyright 2002 Project Purple # Released under the GPL. # +# $Id: onak-mail.pl,v 1.9 2004/01/04 18:48:37 noodles Exp $ +# use strict; +use Fcntl ':flock'; use IPC::Open3; +my %config; + +# +# readconfig +# +# Reads in our config file. Ignores any command it doesn't understand rather +# than having to list all the ones that are of no interest to us. +# +sub readconfig { + + open(CONFIG, "/home/noodles/projects/onak/onak.conf") or + die "Can't read config file: $!"; + + while () { + if (/^#/ or /^$/) { + # Ignore; comment line. + } elsif (/^this_site (.*)/) { + $config{'thissite'} = $1; + } elsif (/^logfile (.*)/) { + $config{'logfile'} = $1; + } elsif (/^maintainer_email (.*)/) { + $config{'adminemail'} = $1; + } elsif (/^mail_delivery_client (.*)/) { + $config{'mta'} = $1; + } elsif (/^pks_bin_dir (.*)/) { + $config{'pks_bin_dir'} = $1; + } elsif (/^db_dir (.*)/) { + $config{'db_dir'} = $1; + } elsif (/^syncsite (.*)/) { + push @{$config{'syncsites'}}, $1; + } + } + + close(CONFIG); + + return; +} + # # submitupdate # @@ -22,31 +62,38 @@ sub submitupdate { my @data = @_; my (@errors, @mergedata); - open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR, "/home/noodles/onak-0.0.3/onak", "add"); + open(LOCKFILE, '>'.$config{'db_dir'}.'/onak-mail.lck'); + flock(LOCKFILE, LOCK_EX); + print LOCKFILE "$$"; + + open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR, + $config{'pks_bin_dir'}."/onak", "-u", "add"); print MERGEIN @data; close MERGEIN; - @errors = ; @mergedata = ; + close MERGEOUT; + @errors = ; + close MERGEERR; - open (LOG, ">>/home/noodles/onak-0.0.3/keyadd.log"); - print LOG @errors; - close LOG; + flock(LOCKFILE, LOCK_UN); + close(LOCKFILE); return @mergedata; } -my ($inheader, %syncsites, $subject, $from, $replyto, @body, @syncmail); +my ($inheader, %seenby, $subject, $from, $replyto, @body, @syncmail); $inheader = 1; $subject = ""; +&readconfig; while (<>) { if ($inheader) { if (/^Subject:\s*(.*)\s*$/i) { $subject = $1; } elsif (/^X-KeyServer-Sent:\s*(.*)\s*$/i) { - $syncsites{$1} = 1; + $seenby{$1} = 1; } elsif (/^From:\s*(.*)\s*$/i) { $from = $1; } elsif (/^Reply-To:\s*(.*)\s*$/i) { @@ -59,10 +106,98 @@ while (<>) { push @body, $_; } } +if (! defined($replyto)) { + $replyto = $from; +} # HELP, ADD, INCREMENTAL, VERBOSE INDEX , INDEX , GET , # LAST if ($subject =~ /^INCREMENTAL$/i) { - submitupdate(@body); + my $site; + my $count; + my $i; + my @newupdate = submitupdate(@body); + my @time; + + $count = 0; + foreach $i (@{$config{'syncsites'}}) { + if (! defined($seenby{$i})) { + $count++; + } + } + + open (LOG, ">>$config{'logfile'}"); + @time = localtime(time); + print LOG "["; + print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d", + $time[3], $time[4] + 1, $time[5] + 1900, + $time[2], $time[1], $time[0]; + print LOG "] onak-mail[$$]: Syncing with $count sites.\n"; + close LOG; + + if ((! defined($newupdate[0])) || $newupdate[0] eq '') { + open (LOG, ">>$config{'logfile'}"); + print LOG "["; + print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d", + $time[3], $time[4] + 1, $time[5] + 1900, + $time[2], $time[1], $time[0]; + print LOG "] onak-mail[$$]: Nothing to sync.\n"; + close LOG; + $count = 0; + } + + if ($count > 0) { + open(MAIL, "|$config{mta}"); + print MAIL "From: $config{adminemail}\n"; + print MAIL "To: "; + foreach $i (@{$config{'syncsites'}}) { + if (! defined($seenby{$i})) { + print MAIL "$i"; + $count--; + if ($count > 0) { + print MAIL ", "; + } + } + } + print MAIL "\n"; + print MAIL "Subject: incremental\n"; + foreach $site (keys %seenby) { + print MAIL "X-KeyServer-Sent: $site\n"; + } + print MAIL "X-KeyServer-Sent: $config{thissite}\n"; + print MAIL "Precedence: list\n"; + print MAIL "MIME-Version: 1.0\n"; + print MAIL "Content-Type: application/pgp-keys\n"; + print MAIL "\n"; + print MAIL @newupdate; + close MAIL; + } +} elsif ($subject =~ /^(VERBOSE )?INDEX (.*)$/i) { + my (@indexdata, $command); + + $command = "index"; + if (defined($1)) { + $command = "vindex"; + } + + open3(\*INDEXIN, \*INDEXOUT, \*INDEXERR, + $config{'pks_bin_dir'}."/onak", $command, "$2"); + close INDEXIN; + @indexdata = ; + close INDEXOUT; + close INDEXERR; + + open(MAIL, "|$config{mta}"); + print MAIL "From: $config{adminemail}\n"; + print MAIL "To: $replyto\n"; + print MAIL "Subject: Reply to INDEX $2\n"; + print MAIL "Precedence: list\n"; + print MAIL "MIME-Version: 1.0\n"; + print MAIL "Content-Type: text/plain\n"; + print MAIL "\n"; + print MAIL "Below follows the reply to your recent keyserver query:\n"; + print MAIL "\n"; + print MAIL @indexdata; + close MAIL; }