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 (/^mail_dir (.*)/) {
41 $config{'mail_dir'} = $1;
42 } elsif (/^syncsite (.*)/) {
43 push @{$config{'syncsites'}}, $1;
55 # Takes an armored OpenPGP stream and submits it to the keyserver. Returns the
56 # difference between what we just added and what we had before (ie the least
57 # data need to get from what we had to what we have).
61 my (@errors, @mergedata);
63 my $pid = open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR,
64 $config{'pks_bin_dir'}."/onak", "-u", "add");
68 @mergedata = <MERGEOUT>;
78 sub processmail($$$$$) {
85 # HELP, ADD, INCREMENTAL, VERBOSE INDEX <keyid>, INDEX <keyid>,
86 # GET <keyid>, LAST <days>
88 if ($subject =~ /^(INCREMENTAL|ADD)$/i) {
92 my @newupdate = submitupdate($body);
96 foreach $i (@{$config{'syncsites'}}) {
97 if (! defined($seenby->{$i})) {
102 open (LOG, ">>$config{'logfile'}");
103 @time = localtime(time);
105 print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
106 $time[3], $time[4] + 1, $time[5] + 1900,
107 $time[2], $time[1], $time[0];
108 print LOG "] onak-mail[$$]: Syncing with $count sites.\n";
111 if ($subject =~ /ADD/i) {
112 open(MAIL, "|$config{mta}");
113 print MAIL "From: $config{adminemail}\n";
114 print MAIL "To: $replyto\n";
115 print MAIL "Subject: Reply to ADD\n";
116 print MAIL "Precedence: list\n";
117 print MAIL "MIME-Version: 1.0\n";
118 print MAIL "Content-Type: text/plain\n";
120 print MAIL "Thank you for your recent key submission.",
121 " It has been processed and synced\n",
122 "with ", $count, " other keyservers.\n";
126 if ((! defined($newupdate[0])) || $newupdate[0] eq '') {
127 open (LOG, ">>$config{'logfile'}");
129 print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
130 $time[3], $time[4] + 1, $time[5] + 1900,
131 $time[2], $time[1], $time[0];
132 print LOG "] onak-mail[$$]: Nothing to sync.\n";
138 open(MAIL, "|$config{mta}");
139 print MAIL "From: $config{adminemail}\n";
141 foreach $i (@{$config{'syncsites'}}) {
142 if (! defined($seenby->{$i})) {
151 print MAIL "Subject: incremental\n";
152 foreach $site (keys %$seenby) {
153 print MAIL "X-KeyServer-Sent: $site\n";
155 print MAIL "X-KeyServer-Sent: $config{thissite}\n";
156 print MAIL "Precedence: list\n";
157 print MAIL "MIME-Version: 1.0\n";
158 print MAIL "Content-Type: application/pgp-keys\n";
160 print MAIL @newupdate;
163 } elsif ($subject =~ /^(VERBOSE )?INDEX (.*)$/i) {
164 my (@indexdata, $command);
171 my $pid = open3(\*INDEXIN, \*INDEXOUT, \*INDEXERR,
172 $config{'pks_bin_dir'}."/onak", $command, "$2");
174 @indexdata = <INDEXOUT>;
179 open(MAIL, "|$config{mta}");
180 print MAIL "From: $config{adminemail}\n";
181 print MAIL "To: $replyto\n";
182 print MAIL "Subject: Reply to INDEX $2\n";
183 print MAIL "Precedence: list\n";
184 print MAIL "MIME-Version: 1.0\n";
185 print MAIL "Content-Type: text/plain\n";
187 print MAIL "Below follows the reply to your recent keyserver query:\n";
189 print MAIL @indexdata;
194 my ($inheader, %seenby, $subject, $from, $replyto, @body, @syncmail);
199 # First dump the incoming mail to a file; this means that if we're receiving
200 # loads of updates we don't spawn lots of processes but instead leave the
201 # mails on disk to be dealt with sequentially.
203 my @time = localtime;
204 my $tmpfile = sprintf "%s/%04d%02d%02d-%02d%02d%02d-%d.onak",
213 open(MAILFILE, '>'.$tmpfile.'.tmp');
218 rename $tmpfile.".tmp", $tmpfile;
221 # Lock here to ensure that only one copy of us is processing the incoming
222 # mail queue at any point in time.
224 sysopen(LOCKFILE, $config{'mail_dir'}.'/onak-mail.lck',
225 O_WRONLY|O_CREAT|O_EXCL) or exit;
230 opendir(MAILDIR, $config{'mail_dir'});
231 while ($file = readdir(MAILDIR)) {
232 next if $file !~ /\.onak$/;
235 $subject = $from = $replyto = "";
239 open(FILE, '<'.$config{'mail_dir'}.'/'.$file);
242 if (/^Subject:\s*(.*)\s*$/i) {
244 } elsif (/^X-KeyServer-Sent:\s*(.*)\s*$/i) {
246 } elsif (/^From:\s*(.*)\s*$/i) {
248 } elsif (/^Reply-To:\s*(.*)\s*$/i) {
258 if ($replyto eq '') {
262 unlink $config{'mail_dir'}.'/'.$file;
264 processmail($subject, $from, $replyto, \%seenby, \@body);
267 unlink $config{'mail_dir'}.'/onak-mail.lck';