1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2014 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::GnuPG;
56 To use the gnupg-secured mail gateway, you need to do the following:
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
61 Set(%GnuPGOptions, homedir => '/opt/rt4/var/data/GnuPG');
62 Set(@MailPlugins, 'Auth::MailFrom', 'Auth::GnuPG', ...other filter...);
66 sub ApplyBeforeDecode { return 1 }
69 use RT::EmailParser ();
74 RawMessageRef => undef,
78 foreach my $p ( $args{'Message'}->parts_DFS ) {
79 $p->head->delete($_) for qw(
80 X-RT-GnuPG-Status X-RT-Incoming-Encryption
81 X-RT-Incoming-Signature X-RT-Privacy
82 X-RT-Sign X-RT-Encrypt
86 my $msg = $args{'Message'}->dup;
88 my ($status, @res) = VerifyDecrypt(
89 Entity => $args{'Message'}, AddStatus => 1,
91 if ( $status && !@res ) {
92 $args{'Message'}->head->replace(
93 'X-RT-Incoming-Encryption' => 'Not encrypted'
99 # FIXME: Check if the message is encrypted to the address of
100 # _this_ queue. send rejecting mail otherwise.
103 $RT::Logger->error("Had a problem during decrypting and verifying");
104 my $reject = HandleErrors( Message => $args{'Message'}, Result => \@res );
105 return (0, 'rejected because of problems during decrypting and verifying')
109 # attach the original encrypted message
110 $args{'Message'}->attach(
111 Type => 'application/x-rt-original-message',
112 Disposition => 'inline',
113 Data => ${ $args{'RawMessageRef'} },
116 $args{'Message'}->head->replace( 'X-RT-Privacy' => 'PGP' );
118 foreach my $part ( $args{'Message'}->parts_DFS ) {
121 my $status = Encode::decode( "UTF-8", $part->head->get( 'X-RT-GnuPG-Status' ) );
123 for ( RT::Crypt::GnuPG::ParseStatus( $status ) ) {
124 if ( $_->{Operation} eq 'Decrypt' && $_->{Status} eq 'DONE' ) {
127 if ( $_->{Operation} eq 'Verify' && $_->{Status} eq 'DONE' ) {
128 $part->head->replace(
129 'X-RT-Incoming-Signature' => Encode::encode( "UTF-8", $_->{UserString} )
135 $part->head->replace(
136 'X-RT-Incoming-Encryption' =>
137 $decrypted ? 'Success' : 'Not encrypted'
154 foreach my $run ( @{ $args{'Result'} } ) {
155 my @status = RT::Crypt::GnuPG::ParseStatus( $run->{'status'} );
156 unless ( $sent_once{'NoPrivateKey'} ) {
157 unless ( CheckNoPrivateKey( Message => $args{'Message'}, Status => \@status ) ) {
158 $sent_once{'NoPrivateKey'}++;
159 $reject = 1 if RT->Config->Get('GnuPG')->{'RejectOnMissingPrivateKey'};
162 unless ( $sent_once{'BadData'} ) {
163 unless ( CheckBadData( Message => $args{'Message'}, Status => \@status ) ) {
164 $sent_once{'BadData'}++;
165 $reject = 1 if RT->Config->Get('GnuPG')->{'RejectOnBadData'};
172 sub CheckNoPrivateKey {
173 my %args = (Message => undef, Status => [], @_ );
174 my @status = @{ $args{'Status'} };
176 my @decrypts = grep $_->{'Operation'} eq 'Decrypt', @status;
177 return 1 unless @decrypts;
178 foreach my $action ( @decrypts ) {
179 # if at least one secrete key exist then it's another error
181 grep !$_->{'User'}{'SecretKeyMissing'},
182 @{ $action->{'EncryptedTo'} };
185 $RT::Logger->error("Couldn't decrypt a message: have no private key");
187 my $address = (RT::Interface::Email::ParseSenderAddressFromHead( $args{'Message'}->head ))[0];
188 my ($status) = RT::Interface::Email::SendEmailUsingTemplate(
190 Template => 'Error: no private key',
192 Message => $args{'Message'},
193 TicketObj => $args{'Ticket'},
195 InReplyTo => $args{'Message'},
198 $RT::Logger->error("Couldn't send 'Error: no private key'");
204 my %args = (Message => undef, Status => [], @_ );
205 my @bad_data_messages =
207 grep $_->{'Status'} ne 'DONE' && $_->{'Operation'} eq 'Data',
208 @{ $args{'Status'} };
209 return 1 unless @bad_data_messages;
211 $RT::Logger->error("Couldn't process a message: ". join ', ', @bad_data_messages );
213 my $address = (RT::Interface::Email::ParseSenderAddressFromHead( $args{'Message'}->head ))[0];
214 my ($status) = RT::Interface::Email::SendEmailUsingTemplate(
216 Template => 'Error: bad GnuPG data',
218 Messages => [ @bad_data_messages ],
219 TicketObj => $args{'Ticket'},
221 InReplyTo => $args{'Message'},
224 $RT::Logger->error("Couldn't send 'Error: bad GnuPG data'");
235 my @res = RT::Crypt::GnuPG::VerifyDecrypt( %args );
237 $RT::Logger->debug("No more encrypted/signed parts");
241 $RT::Logger->debug('Found GnuPG protected parts');
243 # return on any error
244 if ( grep $_->{'exit_code'}, @res ) {
245 $RT::Logger->debug("Error during verify/decrypt operation");
250 my ($status, @nested) = VerifyDecrypt( %args );
251 return $status, @res, @nested;
254 RT::Base->_ImportOverlays();