3 # onak-mail.pl - Mail processing interface for onak, an OpenPGP Keyserver.
5 # Written by Jonathan McDowell <noodles@earth.li>
6 # Copyright 2002-2005 Project Purple
7 # Released under the GPL.
20 # Reads in our config file. Ignores any command it doesn't understand rather
21 # than having to list all the ones that are of no interest to us.
25 open(CONFIG, "@CONFIG@") or
26 die "Can't read config file: $!";
30 # Ignore; comment line.
31 } elsif (/^this_site (.*)/) {
32 $config{'thissite'} = $1;
33 } elsif (/^logfile (.*)/) {
34 $config{'logfile'} = $1;
35 } elsif (/^maintainer_email (.*)/) {
36 $config{'adminemail'} = $1;
37 } elsif (/^mail_delivery_client (.*)/) {
39 } elsif (/^pks_bin_dir (.*)/) {
40 $config{'pks_bin_dir'} = $1;
41 } elsif (/^db_dir (.*)/) {
42 $config{'db_dir'} = $1;
43 } elsif (/^mail_dir (.*)/) {
44 $config{'mail_dir'} = $1;
45 } elsif (/^syncsite (.*)/) {
46 push @{$config{'syncsites'}}, $1;
58 # Takes an armored OpenPGP stream and submits it to the keyserver. Returns the
59 # difference between what we just added and what we had before (ie the least
60 # data need to get from what we had to what we have).
64 my (@errors, @mergedata);
66 my $pid = open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR,
67 $config{'pks_bin_dir'}."/onak", "-u", "add");
71 @mergedata = <MERGEOUT>;
81 sub processmail($$$$$) {
88 # HELP, ADD, INCREMENTAL, VERBOSE INDEX <keyid>, INDEX <keyid>,
89 # GET <keyid>, LAST <days>
91 if ($subject =~ /^(INCREMENTAL|ADD)$/i) {
95 my @newupdate = submitupdate($body);
99 foreach $i (@{$config{'syncsites'}}) {
100 if (! defined($seenby->{$i})) {
105 open (LOG, ">>$config{'logfile'}");
106 @time = localtime(time);
108 print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
109 $time[3], $time[4] + 1, $time[5] + 1900,
110 $time[2], $time[1], $time[0];
111 print LOG "] onak-mail[$$]: Syncing with $count sites.\n";
114 if ($subject =~ /ADD/i) {
115 open(MAIL, "|$config{mta}");
116 print MAIL "From: $config{adminemail}\n";
117 print MAIL "To: $replyto\n";
118 print MAIL "Subject: Reply to ADD\n";
119 print MAIL "Precedence: list\n";
120 print MAIL "MIME-Version: 1.0\n";
121 print MAIL "Content-Type: text/plain\n";
123 print MAIL "Thank you for your recent key submission.",
124 " It has been processed and synced\n",
125 "with ", $count, " other keyservers.\n";
129 if ((! defined($newupdate[0])) || $newupdate[0] eq '') {
130 open (LOG, ">>$config{'logfile'}");
132 print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
133 $time[3], $time[4] + 1, $time[5] + 1900,
134 $time[2], $time[1], $time[0];
135 print LOG "] onak-mail[$$]: Nothing to sync.\n";
141 open(MAIL, "|$config{mta}");
142 print MAIL "From: $config{adminemail}\n";
144 foreach $i (@{$config{'syncsites'}}) {
145 if (! defined($seenby->{$i})) {
154 print MAIL "Subject: incremental\n";
155 foreach $site (keys %$seenby) {
156 print MAIL "X-KeyServer-Sent: $site\n";
158 print MAIL "X-KeyServer-Sent: $config{thissite}\n";
159 print MAIL "Precedence: list\n";
160 print MAIL "MIME-Version: 1.0\n";
161 print MAIL "Content-Type: application/pgp-keys\n";
163 print MAIL @newupdate;
166 } elsif ($subject =~ /^(VERBOSE )?INDEX (.*)$/i) {
167 my (@indexdata, $command);
174 my $pid = open3(\*INDEXIN, \*INDEXOUT, \*INDEXERR,
175 $config{'pks_bin_dir'}."/onak", $command, "$2");
177 @indexdata = <INDEXOUT>;
182 open(MAIL, "|$config{mta}");
183 print MAIL "From: $config{adminemail}\n";
184 print MAIL "To: $replyto\n";
185 print MAIL "Subject: Reply to INDEX $2\n";
186 print MAIL "Precedence: list\n";
187 print MAIL "MIME-Version: 1.0\n";
188 print MAIL "Content-Type: text/plain\n";
190 print MAIL "Below follows the reply to your recent keyserver query:\n";
192 print MAIL @indexdata;
197 my ($inheader, %seenby, $subject, $from, $replyto, @body, @syncmail);
202 # First dump the incoming mail to a file; this means that if we're receiving
203 # loads of updates we don't spawn lots of processes but instead leave the
204 # mails on disk to be dealt with sequentially.
206 my @time = localtime;
207 my $tmpfile = sprintf "%s/%04d%02d%02d-%02d%02d%02d-%d.onak",
216 open(MAILFILE, '>'.$tmpfile);
223 # Lock here to ensure that only one copy of us is processing the incoming
224 # mail queue at any point in time.
226 sysopen(LOCKFILE, $config{'db_dir'}.'/onak-mail.lck',
227 O_WRONLY|O_CREAT|O_EXCL) or exit;
232 opendir(MAILDIR, $config{'mail_dir'});
233 while ($file = readdir(MAILDIR)) {
234 next if $file !~ /\.onak$/;
237 $subject = $from = $replyto = "";
241 open(FILE, '<'.$config{'mail_dir'}.'/'.$file);
244 if (/^Subject:\s*(.*)\s*$/i) {
246 } elsif (/^X-KeyServer-Sent:\s*(.*)\s*$/i) {
248 } elsif (/^From:\s*(.*)\s*$/i) {
250 } elsif (/^Reply-To:\s*(.*)\s*$/i) {
260 if ($replyto eq '') {
264 unlink $config{'mail_dir'}.'/'.$file;
266 processmail($subject, $from, $replyto, \%seenby, \@body);
269 unlink $config{'db_dir'}.'/onak-mail.lck';