Saturday, 14 September 2013

How to set up RT part 6: Configuring rt-mailgate

rt-mailgate is the mail gateway which RT uses to convert emails to tickets. Before we get stuck into configuring it, first a few things need configuring in RT.

The RT configuration file can be found at
It's best not to modify this file directly, but to add config fragments under RT_SiteConfig.d and then recompile. To ready RT for email, we do the following:

/etc/request-tracker4/RT_SiteConfig.d/50-debconf :
  Set($rtname, '');
  Set($Organization, '');
  Set($CorrespondAddress , '');
  Set($CommentAddress , '');
  Set($WebPath , "/rt");
  Set($WebBaseURL , "");

/etc/request-tracker4/RT_SiteConfig.d/60-mailconf :
  Set($SendmailPath, "/usr/lib/sendmail");    
  Set($SendmailArguments, "-t");
  Set($OwnerEmail, ""); #who to email errors to
Then run

# update-rt-siteconfig-4
to update RT.

Now we can look at configuring rt-mailgate itself. First, let's make sure rt-mailgate works in its own right.
# echo "Test123" | rt-mailgate --queue General --action correspond --url

If this gave you a 403, then you'll need to configure
# /etc/apache2/sites-available/apache2-modperl2.conf
There should be a stanza about limiting access to mail gateway. Since it's NoAuth anyway, let's just allow access across the board for it:
<Location /rt/REST/1.0/NoAuth>
    Order Allow,Deny
    Allow from all
    Satisfy any
Add the line "Allow from" and restart Apache.

To configure exim to forward emails to rt-mailgate, edit /etc/aliases :
  rt: "|/usr/bin/rt-mailgate --queue general --action correspond --url"
  rt-comment: "|/usr/bin/rt-mailgate --queue general --action comment --url"

In my installation, I decided to make queue names which were modular, with a 'from' and a 'to' part, of the shape 'from@to' so that I could use email addresses that look like To set this up, I added two files to exim, one a list of requestors, and one a list of recipients:

/etc/exim/queuenames/list.of.requestors (the bit before the @) :

vim /etc/exim/queuenames/list.of.recipients (the bit after the @) :

NB make sure that the two lists combine to make the queue names that are configured in the RT gui.

Also to note - if you add to the list of requestors then you don’t need to reconfigure exim, but if you add to the list of recipients you’ll need to do dpkg-reconfigure exim4-config again and add in the extra subdomain as a destination to receive mail for.

Alternative - and probably easier to keep track of - these settings are all saved in /etc/exim/update-exim4.conf.conf. If you do add to the list of requestors you can add in the additional domains here, under dc_other_hostnames, making sure to separate your items with colons (no csv here, it will silently fail, madness lies beyond). 
Then run update-exim4.conf (yes, that's the command), and then restart exim.

Also to configure to make this work: /etc/exim/conf.d/main/01_exim4-config_listmacrosdefs
    RT4_URL =

and /etc/exim/conf.d/router/950_exim4-config_rt
 driver            = redirect
 domains           = /etc/exim4/queuenames/list.of.recipients
 local_parts       = /etc/exim4/queuenames/list.of.requestors

 local_part_suffix = -comment
 pipe_transport    = request_tracker4_pipe
 data              =   "|/usr/bin/rt-mailgate-4 \
                       --queue \"${local_part}@${extract{1}{.}{$domain}}\" \
                       --action ${substr_1:${if eq{$local_part_suffix}{} \
                       {-correspond}{$local_part_suffix}} } \
                       --url RT4_URL"
 user              = www-data

and /etc/exim/conf.d/transport/30_exim4-config_rt_pipe
 driver         = pipe
 allow_commands = /usr/bin/rt-mailgate-4

Recompile the config
dpkg-reconfigure -plow exim4-config

Test making a ticket via email from the command line of the exim server.
echo "This is a test ticket" | mail -s "test $(date)"

If these tests all went smoothly and the server is able to email out, then next we need to configure RT to interact with users by email.

NB here I am using swaks to test RT so that I can send emails internally without needing to open exim up to receive emails from gmail (and therefore spam). A basic swaks command looks like
swaks --to --from --server -d "Subject: This is the subject line \n\n Here is some text that should go into the body"
Telnet would also work, but is a bit more long-winded.

More testing:  

1. Test making a ticket via email from another internal server (this allows us to test before the smtp port is opened to the whole world:
swaks --to --from --server

2. A person not in the domain should not be able to make a ticket
swaks --to --from --server
Action: no ticket created
/var/log/request-tracker/rt4.log:  RT could not load a valid user, and RT's configuration does not allow
for the creation of a new user for your email. Could not record email: Could not load a valid user

3. An email sent to a non-existent queue should not create a ticket
swaks --to --from --server
Action: no ticket created
/var/log/exim4/mainlog: Connection refused
exim -Mvh <emailID>: spam score of -1
Mail gets returned to sender with the message “A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: retry timeout exceeded”

4. From a valid, privileged user in RT who is not a user in AD:
swaks --to --from --server
Action: ticket created, but user cannot access selfservice as no password set.

4. From a valid AD user who is present but unprivileged user in RT
Action: ticket created. User can access selfservice.

5a. From a valid AD user who is not in RT:
Action: ticket created, unprivileged user created. User can access selfservice but not see ticket

5b. As root, make this new user privileged.
Action: User can now access selfservice and see ticket, as well as the queue that ticket was sent to.