3076a7e99a72f44feddc30baa8f482292020b51c
[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
11 use strict;
12 use IPC::Open3;
13
14 my %config;
15
16 #
17 # readconfig
18 #
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.
21 #
22 sub readconfig {
23
24         open(CONFIG, "/home/noodles/projects/onak/onak.conf") or
25                 die "Can't read config file: $!";
26         
27         while (<CONFIG>) {
28                 if (/^#/ or /^$/) {
29                         # Ignore; comment line.
30                 } elsif (/^this_site (.*)/) {
31                         $config{'thissite'} = $1;
32                 } elsif (/^maintainer_email (.*)/) {
33                         $config{'adminemail'} = $1;
34                 } elsif (/^mail_delivery_client (.*)/) {
35                         $config{'mta'} = $1;
36                 } elsif (/^syncsite (.*)/) {
37                         push @{$config{'syncsites'}}, $1;
38                 }
39         }
40
41         close(CONFIG);
42
43         return;
44 }
45
46 #
47 # submitupdate
48 #
49 # Takes an armored OpenPGP stream and submits it to the keyserver. Returns the
50 # difference between what we just added and what we had before (ie the least
51 # data need to get from what we had to what we have).
52 #
53 sub submitupdate {
54         my @data = @_;
55         my (@errors, @mergedata);
56
57         open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR,
58                 "/home/noodles/onak-0.0.3/onak", "-u", "add");
59
60         print MERGEIN @data;
61         close MERGEIN;
62         @mergedata = <MERGEOUT>;
63         @errors = <MERGEERR>;
64
65         open (LOG, ">>/home/noodles/onak-0.0.3/keyadd.log");
66         print LOG "[".localtime(time)."] ", @errors;
67         close LOG;
68
69         return @mergedata;
70 }
71
72 my ($inheader, %seenby, $subject, $from, $replyto, @body, @syncmail);
73
74 $inheader = 1;
75 $subject = "";
76 &readconfig;
77
78 while (<>) {
79         if ($inheader) {
80                 if (/^Subject:\s*(.*)\s*$/i) {
81                         $subject = $1;
82                 } elsif (/^X-KeyServer-Sent:\s*(.*)\s*$/i) {
83                         $seenby{$1} = 1;
84                 } elsif (/^From:\s*(.*)\s*$/i) {
85                         $from = $1;
86                 } elsif (/^Reply-To:\s*(.*)\s*$/i) {
87                         $replyto = $1;
88                 } elsif (/^$/) {
89                         $inheader = 0;
90                 }
91         }
92         if (!$inheader) {
93                 push @body, $_;
94         }
95 }
96
97 # HELP, ADD, INCREMENTAL, VERBOSE INDEX <keyid>, INDEX <keyid>, GET <keyid>,
98 # LAST <days>
99
100 if ($subject =~ /^INCREMENTAL$/i) {
101         my $site;
102         my $count;
103         my $i;
104         my @newupdate = submitupdate(@body);
105
106         $count = 0;
107         foreach $i (@{$config{'syncsites'}}) {
108                 if (! defined($seenby{$i})) {
109                         $count++;
110                 }
111         }
112
113         open (LOG, ">>/home/noodles/logs/keyadd.log");
114         print LOG "[".localtime(time)."] Syncing with $count sites.\n";
115         close LOG;
116
117         if ($newupdate[0] eq '') {
118                 open (LOG, ">>/home/noodles/logs/keyadd.log");
119                 print LOG "[".localtime(time)."] Nothing to sync.\n";
120                 close LOG;
121                 $count = 0;
122         }
123
124         if ($count > 0) {
125                 open(MAIL, "|$config{mta}");
126                 print MAIL "From: $config{adminemail}\n";
127                 print MAIL "To: ";
128                 foreach $i (@{$config{'syncsites'}}) {
129                         if (! defined($seenby{$i})) {
130                                 print MAIL "$i";
131                                 $count--;
132                                 if ($count > 0) {
133                                         print MAIL ", ";
134                                 }
135                         }
136                 }
137                 print MAIL "\n";
138                 print MAIL "Subject: incremental\n";
139                 foreach $site (keys %seenby) {
140                         print MAIL "X-KeyServer-Sent: $site\n";
141                 }
142                 print MAIL "X-KeyServer-Sent: $config{thissite}\n";
143                 print MAIL "Precedence: list\n";
144                 print MAIL "MIME-Version: 1.0\n";
145                 print MAIL "Content-Type: application/pgp-keys\n";
146                 print MAIL "\n";
147                 print @newupdate;
148                 close MAIL;
149         }
150 }