]> the.earth.li Git - onak.git/blob - onak-mail.pl
cscvs to tla changeset 111
[onak.git] / onak-mail.pl
1 #!/usr/bin/perl -w
2 #
3 # onak-mail.pl - Mail processing interface for onak, an OpenPGP Keyserver.
4 #
5 # Written by Jonathan McDowell <noodles@earth.li>
6 # Copyright 2002 Project Purple
7 # Released under the GPL.
8 #
9 # $Id: onak-mail.pl,v 1.8 2003/10/11 22:17:17 noodles Exp $
10 #
11
12 use strict;
13 use IPC::Open3;
14
15 my %config;
16
17 #
18 # readconfig
19 #
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.
22 #
23 sub readconfig {
24
25         open(CONFIG, "/home/noodles/projects/onak/onak.conf") or
26                 die "Can't read config file: $!";
27         
28         while (<CONFIG>) {
29                 if (/^#/ or /^$/) {
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 (.*)/) {
38                         $config{'mta'} = $1;
39                 } elsif (/^pks_bin_dir (.*)/) {
40                         $config{'pks_bin_dir'} = $1;
41                 } elsif (/^syncsite (.*)/) {
42                         push @{$config{'syncsites'}}, $1;
43                 }
44         }
45
46         close(CONFIG);
47
48         return;
49 }
50
51 #
52 # submitupdate
53 #
54 # Takes an armored OpenPGP stream and submits it to the keyserver. Returns the
55 # difference between what we just added and what we had before (ie the least
56 # data need to get from what we had to what we have).
57 #
58 sub submitupdate {
59         my @data = @_;
60         my (@errors, @mergedata);
61
62         open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR,
63                 $config{'pks_bin_dir'}."/onak", "-u", "add");
64
65         print MERGEIN @data;
66         close MERGEIN;
67         @mergedata = <MERGEOUT>;
68         close MERGEOUT;
69         @errors = <MERGEERR>;
70         close MERGEERR;
71
72         return @mergedata;
73 }
74
75 my ($inheader, %seenby, $subject, $from, $replyto, @body, @syncmail);
76
77 $inheader = 1;
78 $subject = "";
79 &readconfig;
80
81 while (<>) {
82         if ($inheader) {
83                 if (/^Subject:\s*(.*)\s*$/i) {
84                         $subject = $1;
85                 } elsif (/^X-KeyServer-Sent:\s*(.*)\s*$/i) {
86                         $seenby{$1} = 1;
87                 } elsif (/^From:\s*(.*)\s*$/i) {
88                         $from = $1;
89                 } elsif (/^Reply-To:\s*(.*)\s*$/i) {
90                         $replyto = $1;
91                 } elsif (/^$/) {
92                         $inheader = 0;
93                 }
94         }
95         if (!$inheader) {
96                 push @body, $_;
97         }
98 }
99 if (! defined($replyto)) {
100         $replyto = $from;
101 }
102
103 # HELP, ADD, INCREMENTAL, VERBOSE INDEX <keyid>, INDEX <keyid>, GET <keyid>,
104 # LAST <days>
105
106 if ($subject =~ /^INCREMENTAL$/i) {
107         my $site;
108         my $count;
109         my $i;
110         my @newupdate = submitupdate(@body);
111         my @time;
112
113         $count = 0;
114         foreach $i (@{$config{'syncsites'}}) {
115                 if (! defined($seenby{$i})) {
116                         $count++;
117                 }
118         }
119
120         open (LOG, ">>$config{'logfile'}");
121         @time = localtime(time);
122         print LOG "[";
123         print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
124                 $time[3], $time[4] + 1, $time[5] + 1900,
125                 $time[2], $time[1], $time[0];
126         print LOG "] onak-mail[$$]: Syncing with $count sites.\n";
127         close LOG;
128
129         if ((! defined($newupdate[0])) || $newupdate[0] eq '') {
130                 open (LOG, ">>$config{'logfile'}");
131                 print LOG "[";
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";
136                 close LOG;
137                 $count = 0;
138         }
139
140         if ($count > 0) {
141                 open(MAIL, "|$config{mta}");
142                 print MAIL "From: $config{adminemail}\n";
143                 print MAIL "To: ";
144                 foreach $i (@{$config{'syncsites'}}) {
145                         if (! defined($seenby{$i})) {
146                                 print MAIL "$i";
147                                 $count--;
148                                 if ($count > 0) {
149                                         print MAIL ", ";
150                                 }
151                         }
152                 }
153                 print MAIL "\n";
154                 print MAIL "Subject: incremental\n";
155                 foreach $site (keys %seenby) {
156                         print MAIL "X-KeyServer-Sent: $site\n";
157                 }
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";
162                 print MAIL "\n";
163                 print MAIL @newupdate;
164                 close MAIL;
165         }
166 } elsif ($subject =~ /^(VERBOSE )?INDEX (.*)$/i) {
167         my (@indexdata, $command);
168
169         $command = "index";
170         if (defined($1)) {
171                 $command = "vindex";
172         }
173
174         open3(\*INDEXIN, \*INDEXOUT, \*INDEXERR,
175                 $config{'pks_bin_dir'}."/onak", $command, "$2");
176         close INDEXIN;
177         @indexdata = <INDEXOUT>;
178         close INDEXOUT;
179         close INDEXERR;
180
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";
188         print MAIL "\n";
189         print MAIL "Below follows the reply to your recent keyserver query:\n";
190         print MAIL "\n";
191         print MAIL @indexdata;
192         close MAIL;
193 }