3 # onak-mail.pl - Mail processing interface for onak, an OpenPGP Keyserver.
5 # Copyright 2002-2005 Jonathan McDowell <noodles@earth.li>
6 # Released under the GPL.
19 # Reads in our config file. Ignores any command it doesn't understand rather
20 # than having to list all the ones that are of no interest to us.
24 open(CONFIG, "@CONFIG@") or
25 die "Can't read config file: $!";
29 # Ignore; comment line.
30 } elsif (/^this_site (.*)/) {
31 $config{'thissite'} = $1;
32 } elsif (/^logfile (.*)/) {
33 $config{'logfile'} = $1;
34 } elsif (/^maintainer_email (.*)/) {
35 $config{'adminemail'} = $1;
36 } elsif (/^mail_delivery_client (.*)/) {
38 } elsif (/^pks_bin_dir (.*)/) {
39 $config{'pks_bin_dir'} = $1;
40 } elsif (/^db_dir (.*)/) {
41 $config{'db_dir'} = $1;
42 } elsif (/^mail_dir (.*)/) {
43 $config{'mail_dir'} = $1;
44 } elsif (/^syncsite (.*)/) {
45 push @{$config{'syncsites'}}, $1;
57 # Takes an armored OpenPGP stream and submits it to the keyserver. Returns the
58 # difference between what we just added and what we had before (ie the least
59 # data need to get from what we had to what we have).
63 my (@errors, @mergedata);
65 my $pid = open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR,
66 $config{'pks_bin_dir'}."/onak", "-u", "add");
70 @mergedata = <MERGEOUT>;
80 sub processmail($$$$$) {
87 # HELP, ADD, INCREMENTAL, VERBOSE INDEX <keyid>, INDEX <keyid>,
88 # GET <keyid>, LAST <days>
90 if ($subject =~ /^(INCREMENTAL|ADD)$/i) {
94 my @newupdate = submitupdate($body);
98 foreach $i (@{$config{'syncsites'}}) {
99 if (! defined($seenby->{$i})) {
104 open (LOG, ">>$config{'logfile'}");
105 @time = localtime(time);
107 print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
108 $time[3], $time[4] + 1, $time[5] + 1900,
109 $time[2], $time[1], $time[0];
110 print LOG "] onak-mail[$$]: Syncing with $count sites.\n";
113 if ($subject =~ /ADD/i) {
114 open(MAIL, "|$config{mta}");
115 print MAIL "From: $config{adminemail}\n";
116 print MAIL "To: $replyto\n";
117 print MAIL "Subject: Reply to ADD\n";
118 print MAIL "Precedence: list\n";
119 print MAIL "MIME-Version: 1.0\n";
120 print MAIL "Content-Type: text/plain\n";
122 print MAIL "Thank you for your recent key submission.",
123 " It has been processed and synced\n",
124 "with ", $count, " other keyservers.\n";
128 if ((! defined($newupdate[0])) || $newupdate[0] eq '') {
129 open (LOG, ">>$config{'logfile'}");
131 print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
132 $time[3], $time[4] + 1, $time[5] + 1900,
133 $time[2], $time[1], $time[0];
134 print LOG "] onak-mail[$$]: Nothing to sync.\n";
140 open(MAIL, "|$config{mta}");
141 print MAIL "From: $config{adminemail}\n";
143 foreach $i (@{$config{'syncsites'}}) {
144 if (! defined($seenby->{$i})) {
153 print MAIL "Subject: incremental\n";
154 foreach $site (keys %$seenby) {
155 print MAIL "X-KeyServer-Sent: $site\n";
157 print MAIL "X-KeyServer-Sent: $config{thissite}\n";
158 print MAIL "Precedence: list\n";
159 print MAIL "MIME-Version: 1.0\n";
160 print MAIL "Content-Type: application/pgp-keys\n";
162 print MAIL @newupdate;
165 } elsif ($subject =~ /^(VERBOSE )?INDEX (.*)$/i) {
166 my (@indexdata, $command);
173 my $pid = open3(\*INDEXIN, \*INDEXOUT, \*INDEXERR,
174 $config{'pks_bin_dir'}."/onak", $command, "$2");
176 @indexdata = <INDEXOUT>;
181 open(MAIL, "|$config{mta}");
182 print MAIL "From: $config{adminemail}\n";
183 print MAIL "To: $replyto\n";
184 print MAIL "Subject: Reply to INDEX $2\n";
185 print MAIL "Precedence: list\n";
186 print MAIL "MIME-Version: 1.0\n";
187 print MAIL "Content-Type: text/plain\n";
189 print MAIL "Below follows the reply to your recent keyserver query:\n";
191 print MAIL @indexdata;
196 my ($inheader, %seenby, $subject, $from, $replyto, @body, @syncmail);
201 # First dump the incoming mail to a file; this means that if we're receiving
202 # loads of updates we don't spawn lots of processes but instead leave the
203 # mails on disk to be dealt with sequentially.
205 my @time = localtime;
206 my $tmpfile = sprintf "%s/%04d%02d%02d-%02d%02d%02d-%d.onak",
215 open(MAILFILE, '>'.$tmpfile.'.tmp');
220 rename $tmpfile.".tmp", $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';