]>
Commit | Line | Data |
---|---|---|
84fb5b46 MKG |
1 | # BEGIN BPS TAGGED BLOCK {{{ |
2 | # | |
3 | # COPYRIGHT: | |
4 | # | |
5 | # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC | |
6 | # <sales@bestpractical.com> | |
7 | # | |
8 | # (Except where explicitly superseded by other copyright notices) | |
9 | # | |
10 | # | |
11 | # LICENSE: | |
12 | # | |
13 | # This work is made available to you under the terms of Version 2 of | |
14 | # the GNU General Public License. A copy of that license should have | |
15 | # been provided with this software, but in any event can be snarfed | |
16 | # from www.gnu.org. | |
17 | # | |
18 | # This work is distributed in the hope that it will be useful, but | |
19 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 | # General Public License for more details. | |
22 | # | |
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
26 | # 02110-1301 or visit their web page on the internet at | |
27 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. | |
28 | # | |
29 | # | |
30 | # CONTRIBUTION SUBMISSION POLICY: | |
31 | # | |
32 | # (The following paragraph is not intended to limit the rights granted | |
33 | # to you to modify and distribute this software under the terms of | |
34 | # the GNU General Public License and is only of importance to you if | |
35 | # you choose to contribute your changes and enhancements to the | |
36 | # community by submitting them to Best Practical Solutions, LLC.) | |
37 | # | |
38 | # By intentionally submitting any modifications, corrections or | |
39 | # derivatives to this work, or any other work intended for use with | |
40 | # Request Tracker, to Best Practical Solutions, LLC, you confirm that | |
41 | # you are the copyright holder for those contributions and you grant | |
42 | # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable, | |
43 | # royalty-free, perpetual, license to use, copy, create derivative | |
44 | # works based on those contributions, and sublicense and distribute | |
45 | # those contributions and any derivatives thereof. | |
46 | # | |
47 | # END BPS TAGGED BLOCK }}} | |
48 | ||
49 | package RT::Interface::Email::Auth::GnuPG; | |
50 | ||
51 | use strict; | |
52 | use warnings; | |
53 | ||
54 | =head2 GetCurrentUser | |
55 | ||
56 | To use the gnupg-secured mail gateway, you need to do the following: | |
57 | ||
58 | Set up a GnuPG key directory with a pubring containing only the keys | |
59 | you care about and specify the following in your SiteConfig.pm | |
60 | ||
61 | Set(%GnuPGOptions, homedir => '/opt/rt4/var/data/GnuPG'); | |
62 | Set(@MailPlugins, 'Auth::MailFrom', 'Auth::GnuPG', ...other filter...); | |
63 | ||
64 | =cut | |
65 | ||
66 | sub ApplyBeforeDecode { return 1 } | |
67 | ||
68 | use RT::Crypt::GnuPG; | |
69 | use RT::EmailParser (); | |
70 | ||
71 | sub GetCurrentUser { | |
72 | my %args = ( | |
73 | Message => undef, | |
74 | RawMessageRef => undef, | |
75 | @_ | |
76 | ); | |
77 | ||
78 | foreach my $p ( $args{'Message'}->parts_DFS ) { | |
79 | $p->head->delete($_) for qw( | |
80 | X-RT-GnuPG-Status X-RT-Incoming-Encrypton | |
81 | X-RT-Incoming-Signature X-RT-Privacy | |
82 | ); | |
83 | } | |
84 | ||
85 | my $msg = $args{'Message'}->dup; | |
86 | ||
87 | my ($status, @res) = VerifyDecrypt( | |
88 | Entity => $args{'Message'}, AddStatus => 1, | |
89 | ); | |
90 | if ( $status && !@res ) { | |
91 | $args{'Message'}->head->replace( | |
92 | 'X-RT-Incoming-Encryption' => 'Not encrypted' | |
93 | ); | |
94 | ||
95 | return 1; | |
96 | } | |
97 | ||
98 | # FIXME: Check if the message is encrypted to the address of | |
99 | # _this_ queue. send rejecting mail otherwise. | |
100 | ||
101 | unless ( $status ) { | |
102 | $RT::Logger->error("Had a problem during decrypting and verifying"); | |
103 | my $reject = HandleErrors( Message => $args{'Message'}, Result => \@res ); | |
104 | return (0, 'rejected because of problems during decrypting and verifying') | |
105 | if $reject; | |
106 | } | |
107 | ||
108 | # attach the original encrypted message | |
109 | $args{'Message'}->attach( | |
110 | Type => 'application/x-rt-original-message', | |
111 | Disposition => 'inline', | |
112 | Data => ${ $args{'RawMessageRef'} }, | |
113 | ); | |
114 | ||
115 | $args{'Message'}->head->replace( 'X-RT-Privacy' => 'PGP' ); | |
116 | ||
117 | foreach my $part ( $args{'Message'}->parts_DFS ) { | |
118 | my $decrypted; | |
119 | ||
120 | my $status = $part->head->get( 'X-RT-GnuPG-Status' ); | |
121 | if ( $status ) { | |
122 | for ( RT::Crypt::GnuPG::ParseStatus( $status ) ) { | |
123 | if ( $_->{Operation} eq 'Decrypt' && $_->{Status} eq 'DONE' ) { | |
124 | $decrypted = 1; | |
125 | } | |
126 | if ( $_->{Operation} eq 'Verify' && $_->{Status} eq 'DONE' ) { | |
127 | $part->head->replace( | |
128 | 'X-RT-Incoming-Signature' => $_->{UserString} | |
129 | ); | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | $part->head->replace( | |
135 | 'X-RT-Incoming-Encryption' => | |
136 | $decrypted ? 'Success' : 'Not encrypted' | |
137 | ); | |
138 | } | |
139 | ||
140 | return 1; | |
141 | } | |
142 | ||
143 | sub HandleErrors { | |
144 | my %args = ( | |
145 | Message => undef, | |
146 | Result => [], | |
147 | @_ | |
148 | ); | |
149 | ||
150 | my $reject = 0; | |
151 | ||
152 | my %sent_once = (); | |
153 | foreach my $run ( @{ $args{'Result'} } ) { | |
154 | my @status = RT::Crypt::GnuPG::ParseStatus( $run->{'status'} ); | |
155 | unless ( $sent_once{'NoPrivateKey'} ) { | |
156 | unless ( CheckNoPrivateKey( Message => $args{'Message'}, Status => \@status ) ) { | |
157 | $sent_once{'NoPrivateKey'}++; | |
158 | $reject = 1 if RT->Config->Get('GnuPG')->{'RejectOnMissingPrivateKey'}; | |
159 | } | |
160 | } | |
161 | unless ( $sent_once{'BadData'} ) { | |
162 | unless ( CheckBadData( Message => $args{'Message'}, Status => \@status ) ) { | |
163 | $sent_once{'BadData'}++; | |
164 | $reject = 1 if RT->Config->Get('GnuPG')->{'RejectOnBadData'}; | |
165 | } | |
166 | } | |
167 | } | |
168 | return $reject; | |
169 | } | |
170 | ||
171 | sub CheckNoPrivateKey { | |
172 | my %args = (Message => undef, Status => [], @_ ); | |
173 | my @status = @{ $args{'Status'} }; | |
174 | ||
175 | my @decrypts = grep $_->{'Operation'} eq 'Decrypt', @status; | |
176 | return 1 unless @decrypts; | |
177 | foreach my $action ( @decrypts ) { | |
178 | # if at least one secrete key exist then it's another error | |
179 | return 1 if | |
180 | grep !$_->{'User'}{'SecretKeyMissing'}, | |
181 | @{ $action->{'EncryptedTo'} }; | |
182 | } | |
183 | ||
184 | $RT::Logger->error("Couldn't decrypt a message: have no private key"); | |
185 | ||
186 | my $address = (RT::Interface::Email::ParseSenderAddressFromHead( $args{'Message'}->head ))[0]; | |
187 | my ($status) = RT::Interface::Email::SendEmailUsingTemplate( | |
188 | To => $address, | |
189 | Template => 'Error: no private key', | |
190 | Arguments => { | |
191 | Message => $args{'Message'}, | |
192 | TicketObj => $args{'Ticket'}, | |
193 | }, | |
194 | InReplyTo => $args{'Message'}, | |
195 | ); | |
196 | unless ( $status ) { | |
197 | $RT::Logger->error("Couldn't send 'Error: no private key'"); | |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
202 | sub CheckBadData { | |
203 | my %args = (Message => undef, Status => [], @_ ); | |
204 | my @bad_data_messages = | |
205 | map $_->{'Message'}, | |
206 | grep $_->{'Status'} ne 'DONE' && $_->{'Operation'} eq 'Data', | |
207 | @{ $args{'Status'} }; | |
208 | return 1 unless @bad_data_messages; | |
209 | ||
210 | $RT::Logger->error("Couldn't process a message: ". join ', ', @bad_data_messages ); | |
211 | ||
212 | my $address = (RT::Interface::Email::ParseSenderAddressFromHead( $args{'Message'}->head ))[0]; | |
213 | my ($status) = RT::Interface::Email::SendEmailUsingTemplate( | |
214 | To => $address, | |
215 | Template => 'Error: bad GnuPG data', | |
216 | Arguments => { | |
217 | Messages => [ @bad_data_messages ], | |
218 | TicketObj => $args{'Ticket'}, | |
219 | }, | |
220 | InReplyTo => $args{'Message'}, | |
221 | ); | |
222 | unless ( $status ) { | |
223 | $RT::Logger->error("Couldn't send 'Error: bad GnuPG data'"); | |
224 | } | |
225 | return 0; | |
226 | } | |
227 | ||
228 | sub VerifyDecrypt { | |
229 | my %args = ( | |
230 | Entity => undef, | |
231 | @_ | |
232 | ); | |
233 | ||
234 | my @res = RT::Crypt::GnuPG::VerifyDecrypt( %args ); | |
235 | unless ( @res ) { | |
236 | $RT::Logger->debug("No more encrypted/signed parts"); | |
237 | return 1; | |
238 | } | |
239 | ||
240 | $RT::Logger->debug('Found GnuPG protected parts'); | |
241 | ||
242 | # return on any error | |
243 | if ( grep $_->{'exit_code'}, @res ) { | |
244 | $RT::Logger->debug("Error during verify/decrypt operation"); | |
245 | return (0, @res); | |
246 | } | |
247 | ||
248 | # nesting | |
249 | my ($status, @nested) = VerifyDecrypt( %args ); | |
250 | return $status, @res, @nested; | |
251 | } | |
252 | ||
253 | RT::Base->_ImportOverlays(); | |
254 | ||
255 | 1; | |
256 |