cscvs to tla changeset 100
[onak.git] / onak-mail.pl
1 #!/usr/bin/perl -w
2
3 #
4 # onak-mail.pl - Mail processing interface for onak, an OpenPGP Keyserver.
5 #
6 # Written by Jonathan McDowell <noodles@earth.li>
7 # Copyright 2002 Project Purple
8 # Released under the GPL.
9 #
10 # $Id: onak-mail.pl,v 1.7 2003/09/28 17:25:40 noodles Exp $
11 #
12
13 use strict;
14 use IPC::Open3;
15
16 my %config;
17
18 #
19 # readconfig
20 #
21 # Reads in our config file. Ignores any command it doesn't understand rather
22 # than having to list all the ones that are of no interest to us.
23 #
24 sub readconfig {
25
26         open(CONFIG, "/home/noodles/projects/onak/onak.conf") or
27                 die "Can't read config file: $!";
28         
29         while (<CONFIG>) {
30                 if (/^#/ or /^$/) {
31                         # Ignore; comment line.
32                 } elsif (/^this_site (.*)/) {
33                         $config{'thissite'} = $1;
34                 } elsif (/^logfile (.*)/) {
35                         $config{'logfile'} = $1;
36                 } elsif (/^maintainer_email (.*)/) {
37                         $config{'adminemail'} = $1;
38                 } elsif (/^mail_delivery_client (.*)/) {
39                         $config{'mta'} = $1;
40                 } elsif (/^pks_bin_dir (.*)/) {
41                         $config{'pks_bin_dir'} = $1;
42                 } elsif (/^syncsite (.*)/) {
43                         push @{$config{'syncsites'}}, $1;
44                 }
45         }
46
47         close(CONFIG);
48
49         return;
50 }
51
52 #
53 # submitupdate
54 #
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).
58 #
59 sub submitupdate {
60         my @data = @_;
61         my (@errors, @mergedata);
62
63         open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR,
64                 $config{'pks_bin_dir'}."/onak", "-u", "add");
65
66         print MERGEIN @data;
67         close MERGEIN;
68         @mergedata = <MERGEOUT>;
69         @errors = <MERGEERR>;
70
71         return @mergedata;
72 }
73
74 my ($inheader, %seenby, $subject, $from, $replyto, @body, @syncmail);
75
76 $inheader = 1;
77 $subject = "";
78 &readconfig;
79
80 while (<>) {
81         if ($inheader) {
82                 if (/^Subject:\s*(.*)\s*$/i) {
83                         $subject = $1;
84                 } elsif (/^X-KeyServer-Sent:\s*(.*)\s*$/i) {
85                         $seenby{$1} = 1;
86                 } elsif (/^From:\s*(.*)\s*$/i) {
87                         $from = $1;
88                 } elsif (/^Reply-To:\s*(.*)\s*$/i) {
89                         $replyto = $1;
90                 } elsif (/^$/) {
91                         $inheader = 0;
92                 }
93         }
94         if (!$inheader) {
95                 push @body, $_;
96         }
97 }
98
99 # HELP, ADD, INCREMENTAL, VERBOSE INDEX <keyid>, INDEX <keyid>, GET <keyid>,
100 # LAST <days>
101
102 if ($subject =~ /^INCREMENTAL$/i) {
103         my $site;
104         my $count;
105         my $i;
106         my @newupdate = submitupdate(@body);
107         my @time;
108
109         $count = 0;
110         foreach $i (@{$config{'syncsites'}}) {
111                 if (! defined($seenby{$i})) {
112                         $count++;
113                 }
114         }
115
116         open (LOG, ">>$config{'logfile'}");
117         @time = localtime(time);
118         print LOG "[";
119         print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
120                 $time[3], $time[4] + 1, $time[5] + 1900,
121                 $time[2], $time[1], $time[0];
122         print LOG "] onak-mail[$$]: Syncing with $count sites.\n";
123         close LOG;
124
125         if ((! defined($newupdate[0])) || $newupdate[0] eq '') {
126                 open (LOG, ">>$config{'logfile'}");
127                 print LOG "[";
128                 print LOG sprintf "%02d/%02d/%04d %02d:%02d:%02d",
129                         $time[3], $time[4] + 1, $time[5] + 1900,
130                         $time[2], $time[1], $time[0];
131                 print LOG "] onak-mail[$$]: Nothing to sync.\n";
132                 close LOG;
133                 $count = 0;
134         }
135
136         if ($count > 0) {
137                 open(MAIL, "|$config{mta}");
138                 print MAIL "From: $config{adminemail}\n";
139                 print MAIL "To: ";
140                 foreach $i (@{$config{'syncsites'}}) {
141                         if (! defined($seenby{$i})) {
142                                 print MAIL "$i";
143                                 $count--;
144                                 if ($count > 0) {
145                                         print MAIL ", ";
146                                 }
147                         }
148                 }
149                 print MAIL "\n";
150                 print MAIL "Subject: incremental\n";
151                 foreach $site (keys %seenby) {
152                         print MAIL "X-KeyServer-Sent: $site\n";
153                 }
154                 print MAIL "X-KeyServer-Sent: $config{thissite}\n";
155                 print MAIL "Precedence: list\n";
156                 print MAIL "MIME-Version: 1.0\n";
157                 print MAIL "Content-Type: application/pgp-keys\n";
158                 print MAIL "\n";
159                 print @newupdate;
160                 close MAIL;
161         }
162 }