1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Interface::Email::Auth::Crypt;
56 RT::Interface::Email::Auth::Crypt - decrypting and verifying protected emails
60 This mail plugin decrypts and verifies incoming emails. Supported
61 encryption protocols are GnuPG and SMIME.
63 This code is independant from code that encrypts/sign outgoing emails, so
64 it's possible to decrypt data without bringing in encryption. To enable
65 it put the module in the mail plugins list:
67 Set(@MailPlugins, 'Auth::MailFrom', 'Auth::Crypt', ...other filters...);
69 C<Auth::Crypt> will not function without C<Auth::MailFrom> listed before
74 To use the gnupg-secured mail gateway, you need to do the following:
76 Set up a GnuPG key directory with a pubring containing only the keys
77 you care about and specify the following in your SiteConfig.pm
79 Set(%GnuPGOptions, homedir => '/opt/rt4/var/data/GnuPG');
81 Read also: L<RT::Crypt> and L<RT::Crypt::GnuPG>.
85 To use the SMIME-secured mail gateway, you need to do the following:
87 Set up a SMIME key directory with files containing keys for queues'
88 addresses and specify the following in your SiteConfig.pm
92 OpenSSL => '/usr/bin/openssl',
93 Keyring => '/opt/rt4/var/data/smime',
94 CAPath => '/opt/rt4/var/data/smime/signing-ca.pem',
96 'queue.address@example.com' => 'passphrase',
101 Read also: L<RT::Crypt> and L<RT::Crypt::SMIME>.
105 sub ApplyBeforeDecode { return 1 }
108 use RT::EmailParser ();
113 RawMessageRef => undef,
119 # we clean all possible headers
122 X-RT-Incoming-Encryption
123 X-RT-Incoming-Signature X-RT-Privacy
124 X-RT-Sign X-RT-Encrypt
126 map "X-RT-$_-Status", RT::Crypt->Protocols;
127 foreach my $p ( $args{'Message'}->parts_DFS ) {
128 $p->head->delete($_) for @headers;
131 my (@res) = RT::Crypt->VerifyDecrypt(
133 Entity => $args{'Message'},
136 if (RT->Config->Get('Crypt')->{'RejectOnUnencrypted'}) {
139 Template => 'Error: unencrypted message',
140 Arguments => { Message => $args{'Message'} },
142 return (-1, 'rejected because the message is unencrypted with RejectOnUnencrypted enabled');
145 $args{'Message'}->head->replace(
146 'X-RT-Incoming-Encryption' => 'Not encrypted'
152 if ( grep {$_->{'exit_code'}} @res ) {
153 my @fail = grep {$_->{status}{Status} ne "DONE"}
154 map { my %ret = %{$_}; map {+{%ret, status => $_}} RT::Crypt->ParseStatus( Protocol => $_->{Protocol}, Status => $_->{status})}
156 for my $fail ( @fail ) {
157 $RT::Logger->warning("Failure during ".$fail->{Protocol}." ". lc($fail->{status}{Operation}) . ": ". $fail->{status}{Message});
159 my $reject = HandleErrors( Message => $args{'Message'}, Result => \@res );
160 return (0, 'rejected because of problems during decrypting and verifying')
164 # attach the original encrypted message
165 $args{'Message'}->attach(
166 Type => 'application/x-rt-original-message',
167 Disposition => 'inline',
168 Data => ${ $args{'RawMessageRef'} },
172 my @check_protocols = RT::Crypt->EnabledOnIncoming;
173 foreach my $part ( $args{'Message'}->parts_DFS ) {
176 foreach my $protocol ( @check_protocols ) {
177 my @status = grep defined && length,
178 map Encode::decode( "UTF-8", $_), $part->head->get( "X-RT-$protocol-Status" );
181 push @found, $protocol;
183 for ( map RT::Crypt->ParseStatus( Protocol => $protocol, Status => "$_" ), @status ) {
184 if ( $_->{Operation} eq 'Decrypt' && $_->{Status} eq 'DONE' ) {
187 if ( $_->{Operation} eq 'Verify' && $_->{Status} eq 'DONE' ) {
188 $part->head->replace(
189 'X-RT-Incoming-Signature' => Encode::encode( "UTF-8", $_->{UserString} )
195 $part->head->replace(
196 'X-RT-Incoming-Encryption' =>
197 $decrypted ? 'Success' : 'Not encrypted'
202 $args{'Message'}->head->replace( 'X-RT-Privacy' => Encode::encode( "UTF-8", $_ ) )
203 foreach grep !$seen{$_}++, @found;
218 foreach my $run ( @{ $args{'Result'} } ) {
219 my @status = RT::Crypt->ParseStatus( Protocol => $run->{'Protocol'}, Status => $run->{'status'} );
220 unless ( $sent_once{'NoPrivateKey'} ) {
221 unless ( CheckNoPrivateKey( Message => $args{'Message'}, Status => \@status ) ) {
222 $sent_once{'NoPrivateKey'}++;
223 $reject = 1 if RT->Config->Get('Crypt')->{'RejectOnMissingPrivateKey'};
226 unless ( $sent_once{'BadData'} ) {
227 unless ( CheckBadData( Message => $args{'Message'}, Status => \@status ) ) {
228 $sent_once{'BadData'}++;
229 $reject = 1 if RT->Config->Get('Crypt')->{'RejectOnBadData'};
236 sub CheckNoPrivateKey {
237 my %args = (Message => undef, Status => [], @_ );
238 my @status = @{ $args{'Status'} };
240 my @decrypts = grep $_->{'Operation'} eq 'Decrypt', @status;
241 return 1 unless @decrypts;
242 foreach my $action ( @decrypts ) {
243 # if at least one secrete key exist then it's another error
245 grep !$_->{'User'}{'SecretKeyMissing'},
246 @{ $action->{'EncryptedTo'} };
249 $RT::Logger->error("Couldn't decrypt a message: have no private key");
251 return EmailErrorToSender(
253 Template => 'Error: no private key',
254 Arguments => { Message => $args{'Message'} },
259 my %args = (Message => undef, Status => [], @_ );
260 my @bad_data_messages =
262 grep $_->{'Status'} ne 'DONE' && $_->{'Operation'} eq 'Data',
263 @{ $args{'Status'} };
264 return 1 unless @bad_data_messages;
266 return EmailErrorToSender(
268 Template => 'Error: bad encrypted data',
269 Arguments => { Messages => [ @bad_data_messages ] },
273 sub EmailErrorToSender {
276 $args{'Arguments'} ||= {};
277 $args{'Arguments'}{'TicketObj'} ||= $args{'Ticket'};
279 my $address = (RT::Interface::Email::ParseSenderAddressFromHead( $args{'Message'}->head ))[0];
280 my ($status) = RT::Interface::Email::SendEmailUsingTemplate(
282 Template => $args{'Template'},
283 Arguments => $args{'Arguments'},
284 InReplyTo => $args{'Message'},
287 $RT::Logger->error("Couldn't send '$args{'Template'}''");
292 RT::Base->_ImportOverlays();