]> git.sommitrealweird.co.uk Git - onak.git/blob - onak-mail.pl
cscvs to tla changeset 86
[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.6 2003/06/04 20:57:11 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 (/^maintainer_email (.*)/) {
35                         $config{'adminemail'} = $1;
36                 } elsif (/^mail_delivery_client (.*)/) {
37                         $config{'mta'} = $1;
38                 } elsif (/^syncsite (.*)/) {
39                         push @{$config{'syncsites'}}, $1;
40                 }
41         }
42
43         close(CONFIG);
44
45         return;
46 }
47
48 #
49 # submitupdate
50 #
51 # Takes an armored OpenPGP stream and submits it to the keyserver. Returns the
52 # difference between what we just added and what we had before (ie the least
53 # data need to get from what we had to what we have).
54 #
55 sub submitupdate {
56         my @data = @_;
57         my (@errors, @mergedata);
58
59         open3(\*MERGEIN, \*MERGEOUT, \*MERGEERR,
60                 "/home/noodles/onak-0.0.3/onak", "-u", "add");
61
62         print MERGEIN @data;
63         close MERGEIN;
64         @mergedata = <MERGEOUT>;
65         @errors = <MERGEERR>;
66
67         open (LOG, ">>/home/noodles/onak-0.0.3/keyadd.log");
68         print LOG "[".localtime(time)."] ", @errors;
69         close LOG;
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
108         $count = 0;
109         foreach $i (@{$config{'syncsites'}}) {
110                 if (! defined($seenby{$i})) {
111                         $count++;
112                 }
113         }
114
115         open (LOG, ">>/home/noodles/logs/keyadd.log");
116         print LOG "[".localtime(time)."] Syncing with $count sites.\n";
117         close LOG;
118
119         if ($newupdate[0] eq '') {
120                 open (LOG, ">>/home/noodles/logs/keyadd.log");
121                 print LOG "[".localtime(time)."] Nothing to sync.\n";
122                 close LOG;
123                 $count = 0;
124         }
125
126         if ($count > 0) {
127                 open(MAIL, "|$config{mta}");
128                 print MAIL "From: $config{adminemail}\n";
129                 print MAIL "To: ";
130                 foreach $i (@{$config{'syncsites'}}) {
131                         if (! defined($seenby{$i})) {
132                                 print MAIL "$i";
133                                 $count--;
134                                 if ($count > 0) {
135                                         print MAIL ", ";
136                                 }
137                         }
138                 }
139                 print MAIL "\n";
140                 print MAIL "Subject: incremental\n";
141                 foreach $site (keys %seenby) {
142                         print MAIL "X-KeyServer-Sent: $site\n";
143                 }
144                 print MAIL "X-KeyServer-Sent: $config{thissite}\n";
145                 print MAIL "Precedence: list\n";
146                 print MAIL "MIME-Version: 1.0\n";
147                 print MAIL "Content-Type: application/pgp-keys\n";
148                 print MAIL "\n";
149                 print @newupdate;
150                 close MAIL;
151         }
152 }