2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
7 # <sales@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
31 # CONTRIBUTION SUBMISSION POLICY:
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
48 # END BPS TAGGED BLOCK }}}
51 rt-mailgate - Mail interface to RT.
61 GetOptions( $opts, "queue=s", "action=s", "url=s",
62 "jar=s", "help", "debug", "extension=s",
63 "timeout=i", "verify-ssl!", "ca-file=s",
66 my $gateway = RT::Client::MailGateway->new();
70 package RT::Client::MailGateway;
73 use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
74 use File::Temp qw(tempfile tempdir);
75 $DYNAMIC_FILE_UPLOAD = 1;
77 use constant EX_TEMPFAIL => 75;
78 use constant BUFFER_SIZE => 8192;
82 my $self = bless {}, $class;
90 if ( $opts->{running_in_test_harness} ) {
91 $self->{running_in_test_harness} = 1;
94 $self->validate_cli_flags($opts);
96 my $ua = $self->get_useragent($opts);
97 my $post_params = $self->setup_session($opts);
98 $self->upload_message( $ua => $post_params );
99 $self->exit_with_success();
102 sub exit_with_success {
104 if ( $self->{running_in_test_harness} ) {
113 if ( $self->{running_in_test_harness} ) {
123 if ( $self->{running_in_test_harness} ) {
131 sub validate_cli_flags {
134 if ( $opts->{'help'} ) {
136 Pod::Usage::pod2usage( { verbose => 2 } );
137 return $self->permfail()
138 ; # Don't want to succeed if this is really an email!
141 unless ( $opts->{'url'} ) {
143 "$0 invoked improperly\n\nNo 'url' provided to mail gateway!\n";
144 return $self->permfail();
147 $opts->{"verify-ssl"} = 1 unless defined $opts->{"verify-ssl"};
153 my $ua = LWP::UserAgent->new();
154 $ua->agent("rt-mailgate/@RT_VERSION_MAJOR@.@RT_VERSION_MINOR@.@RT_VERSION_PATCH@ ");
155 $ua->cookie_jar( { file => $opts->{'jar'} } ) if $opts->{'jar'};
157 $ua->ssl_opts( verify_hostname => $opts->{'verify-ssl'} );
158 $ua->ssl_opts( SSL_ca_file => $opts->{'ca-file'} )
159 if $opts->{'ca-file'};
168 foreach (qw(queue action)) {
169 $post_params{$_} = $opts->{$_} if defined $opts->{$_};
172 if ( ( $opts->{'extension'} || '' ) =~ /^(?:action|queue|ticket)$/i ) {
173 $post_params{ lc $opts->{'extension'} } = $ENV{'EXTENSION'}
174 || $opts->{ $opts->{'extension'} };
175 } elsif ( $opts->{'extension'} && $ENV{'EXTENSION'} ) {
177 "Value of the --extension argument is not action, queue or ticket"
178 . ", but environment variable EXTENSION is also defined. The former is ignored.\n";
181 # add ENV{'EXTENSION'} as X-RT-MailExtension to the message header
182 if ( my $value = ( $ENV{'EXTENSION'} || $opts->{'extension'} ) ) {
184 # prepare value to avoid MIME format breakage
185 # strip trailing newline symbols
186 $value =~ s/(\r*\n)+$//;
188 # make a correct multiline header field,
189 # with tabs in the beginning of each line
190 $value =~ s/(\r*\n)/$1\t/g;
191 $opts->{'headers'} .= "X-RT-Mail-Extension: $value\n";
194 # Read the message in from STDIN
195 # _raw_message is used for testing
196 my $message = $opts->{'_raw_message'} || $self->slurp_message();
197 unless ( $message->{'filename'} ) {
198 $post_params{'message'} = [
200 'Content-Type' => 'application/octet-stream',
201 Content => ${ $message->{'content'} },
204 $post_params{'message'} = [
205 $message->{'filename'}, '',
206 'Content-Type' => 'application/octet-stream',
210 return \%post_params;
216 my $post_params = shift;
217 my $full_url = $opts->{'url'} . "/REST/1.0/NoAuth/mail-gateway";
218 print STDERR "$0: connecting to $full_url\n" if $opts->{'debug'};
220 $ua->timeout( exists( $opts->{'timeout'} ) ? $opts->{'timeout'} : 180 );
221 my $r = $ua->post( $full_url, $post_params, Content_Type => 'form-data' );
225 while ($n++ < 3 and $r->is_redirect) {
226 $full_url = $r->header( "Location" );
227 $r = $ua->post( $full_url, $post_params, Content_Type => 'form-data' );
230 $self->check_failure($r);
232 my $content = $r->content;
233 print STDERR $content . "\n" if $opts->{'debug'};
235 return if ( $content =~ /^(ok|not ok)/ );
237 # It's not the server's fault if the mail is bogus. We just want to know that
238 # *something* came out of the server.
242 The RT server which handled your email did not behave as expected. It
248 return $self->tempfail();
254 return if $r->is_success;
256 print STDERR "HTTP request failed: @{[ $r->status_line ]}. "
257 ."Your webserver logs may have more information or there may be a network problem.\n";
258 print STDERR "\n$0: undefined server error\n" if $opts->{'debug'};
259 return $self->tempfail();
268 my ( $fh, $filename )
269 = eval { tempfile( DIR => tempdir( CLEANUP => 1 ) ) };
271 print STDERR "$0: Couldn't create temp file, using memory\n";
272 print STDERR "error: $@\n" if $@;
274 my $message = \do { local ( @ARGV, $/ ); <STDIN> };
275 unless ( $$message =~ /\S/ ) {
276 print STDERR "$0: no message passed on STDIN\n";
277 $self->exit_with_success;
279 $$message = $opts->{'headers'} . $$message if $opts->{'headers'};
280 return ( { content => $message } );
286 print $fh $opts->{'headers'} if $opts->{'headers'};
291 my $status = read \*STDIN, $buf, BUFFER_SIZE;
292 unless ( defined $status ) {
293 print STDERR "$0: couldn't read message: $!\n";
294 return $self->tempfail();
295 } elsif ( !$status ) {
298 $empty = 0 if $buf =~ /\S/;
304 print STDERR "$0: no message passed on STDIN\n";
305 $self->exit_with_success;
307 print STDERR "$0: temp file is '$filename'\n" if $opts->{'debug'};
308 return ( { filename => $filename } );
313 rt-mailgate --help : this text
315 Usual invocation (from MTA):
317 rt-mailgate --action (correspond|comment|...) --queue queuename
318 --url http://your.rt.server/
320 [ --extension (queue|action|ticket) ]
321 [ --timeout seconds ]
331 Specifies what happens to email sent to this alias. The avaliable
332 basic actions are: C<correspond>, C<comment>.
335 If you've set the RT configuration variable B<< C<UnsafeEmailCommands> >>,
336 C<take> and C<resolve> are also available. You can execute two or more
337 actions on a single message using a C<-> separated list. RT will execute
338 the actions in the listed order. For example you can use C<take-comment>,
339 C<correspond-resolve> or C<take-comment-resolve> as actions.
341 Note that C<take> and C<resolve> actions ignore message text if used
342 alone. Include a C<comment> or C<correspond> action if you want RT
343 to record the incoming message.
345 The default action is C<correspond>.
349 This flag determines which queue this alias should create a ticket in if no ticket identifier
354 This flag tells the mail gateway where it can find your RT server. You should
355 probably use the same URL that users use to log into RT.
357 If you have a self-signed SSL certificate, you may also need to pass
358 C<--ca-file> or C<--no-verify-ssl>, below.
360 =item C<--ca-file> I<path>
362 Specifies the path to the public SSL certificate for the certificate
363 authority that should be used to verify the website's SSL certificate.
364 If your webserver uses a self-signed certificate, you should
365 preferentially use this option over C<--no-verify-ssl>, as it will
366 ensure that the self-signed certificate that the mailgate is seeing the
367 I<right> self-signed certificate.
369 =item C<--no-verify-ssl>
371 This flag tells the mail gateway to trust all SSL certificates,
372 regardless of if their hostname matches the certificate, and regardless
373 of CA. This is required if you have a self-signed certificate, or some
374 other certificate which is not traceable back to an certificate your
375 system ultimitely trusts.
377 =item C<--extension> OPTIONAL
379 Some MTAs will route mail sent to user-foo@host or user+foo@host to user@host
380 and present "foo" in the environment variable $EXTENSION. By specifying
381 the value "queue" for this parameter, the queue this message should be
382 submitted to will be set to the value of $EXTENSION. By specifying
383 "ticket", $EXTENSION will be interpreted as the id of the ticket this message
384 is related to. "action" will allow the user to specify either "comment" or
385 "correspond" in the address extension.
387 =item C<--debug> OPTIONAL
389 Print debugging output to standard error
392 =item C<--timeout> OPTIONAL
394 Configure the timeout for posting the message to the web server. The
395 default timeout is 3 minutes (180 seconds).
402 The RT mail gateway is the primary mechanism for communicating with RT
403 via email. This program simply directs the email to the RT web server,
404 which handles filing correspondence and sending out any required mail.
405 It is designed to be run as part of the mail delivery process, either
406 called directly by the MTA or C<procmail>, or in a F<.forward> or
411 Much of the set up of the mail gateway depends on your MTA and mail
412 routing configuration.
414 You need to route mail to C<rt-mailgate> for the queues you're
415 monitoring. For instance, if you're using F</etc/aliases> and you have a
416 "bugs" queue, you will want something like this:
418 bugs: "|@RT_BIN_PATH_R@/rt-mailgate --queue bugs --action correspond
419 --url http://rt.mycorp.com/"
421 bugs-comment: "|@RT_BIN_PATH_R@/rt-mailgate --queue bugs --action comment
422 --url http://rt.mycorp.com/"
424 Note that you don't have to run your RT server on your mail server, as
425 the mail gateway will happily relay to a different machine.
429 By default, the mail gateway will accept mail from anyone. However,
430 there are situations in which you will want to authenticate users
431 before allowing them to communicate with the system. You can do this
432 via a plug-in mechanism in the RT configuration.
434 You can set the array C<@MailPlugins> to be a list of plugins. The
435 default plugin, if this is not given, is C<Auth::MailFrom> - that is,
436 authentication of the person is done based on the C<From> header of the
437 email. If you have additional filters or authentication mechanisms, you
438 can list them here and they will be called in order:
441 "Filter::SpamAssassin",
446 See the documentation for any additional plugins you have.
448 You may also put Perl subroutines into the C<@MailPlugins> array, if
449 they behave as described below.
451 =head1 WRITING PLUGINS
453 What's actually going on in the above is that C<@MailPlugins> is a
454 list of Perl modules; RT prepends C<RT::Interface::Email::> to the name,
455 to form a package name, and then C<use>'s this module. The module is
456 expected to provide a C<GetCurrentUser> subroutine, which takes a hash of
463 A C<MIME::Entity> object representing the email
467 An C<RT::CurrentUser> object
471 The authentication level returned from the previous plugin.
473 =item Ticket [OPTIONAL]
475 The ticket under discussion
477 =item Queue [OPTIONAL]
479 If we don't already have a ticket id, we need to know which queue we're talking about
483 The action being performed. At the moment, it's one of "comment" or "correspond"
487 It returns two values, the new C<RT::CurrentUser> object, and the new
488 authentication level. The authentication level can be zero, not allowed
489 to communicate with RT at all, (a "permission denied" error is mailed to
490 the correspondent) or one, which is the normal mode of operation.
491 Additionally, if C<-1> is returned, then the processing of the plug-ins
492 stops immediately and the message is ignored.
500 Some MTAs will route mail sent to user-foo@host or user+foo@host to user@host
501 and present "foo" in the environment variable C<EXTENSION>. Mailgate adds value
502 of this variable to message in the C<X-RT-Mail-Extension> field of the message
505 See also C<--extension> option. Note that value of the environment variable is
506 always added to the message header when it's not empty even if C<--extension>
507 option is not provided.