SELinux阻止Fail2Ban通过msmtp发送电子邮件通知

我有msmtp作为一个空客户端连接到我的AWS SES账户的SMTP,提供警报,如cron,monit,并希望Fail2Ban很快到我的电子邮件地址。 但是,Fail2Ban不会打球,或者更准确地说,selinux正在阻止事情的发生。

在许可模式下,action_mwl工作得很好。 我禁止了电子邮件。 在强制模式下,Fail2Banlogging一个错误,不发送邮件。 根据msmtp日志,试图发送它,但是它不会。

下面是Fail2Ban日志条目的一部分(部分):

2015-09-29 12:25:12,543 fail2ban.actions [31113]: ERROR Failed to execute ban jail 'wordpress' action 'sendmail-whois-lines' info 'CallingMap({'ipjailmatches': <function <lambda> at 0x2c5ac08>, 'matches': u' 

msmtp报告:

 Sep 29 12:25:12 host=email-smtp.eu-west-1.amazonaws.com tls=on auth=on user=12345 [email protected] [email protected] errormsg='cannot connect to email-smtp.eu-west-1.amazonaws.com, port 587: Permission denied' exitcode=EX_TEMPFAIL 

这不是一个msmtpconfiguration问题,也不是一个电子邮件正文内容问题,因为我可以从命令行pipe道发送确切的Fail2Ban消息到msmtp(直接,或通过sendmail符号链接)就好了,它发送精美。 证书等,因此是好的。 也可以通过cron工作。 这意味着它也不是防火墙问题。

 $ sudo ls -lZ /usr/bin/msmtp -rwxr-xr-x. root root system_u:object_r:bin_t:s0 /usr/bin/msmtp $ sudo ls -lZ /usr/bin/sendmail lrwxrwxrwx. root root unconfined_u:object_r:bin_t:s0 /usr/bin/sendmail -> /usr/bin/msmtp 

在jail.conf中:

 mta = sendmail 

sealert不给我任何提示,我可以承认或采取行动。

我已经确认fail2ban以root身份运行:

 $ ps aux | grep fail2ban 

我添加了一些额外的日志logging,现在得到这在/ var / log / messages

 Sep 29 16:11:15 ip-172-31-6-51 setroubleshoot: SELinux is preventing /usr/bin/msmtp from name_connect access on the tcp_socket port 587. For complete SELinux messages. run sealert -l 78f05dbd-a953-4196-9f14-afaabb5a4d88 Sep 29 16:11:15 ip-172-31-6-51 python: SELinux is preventing /usr/bin/msmtp from name_connect access on the tcp_socket port 587. 

在哪里看下一个? 我怎么能告诉SELinux Fail2Ban被允许与msmtp很好的玩?

在添加更详细的日志logging之后,我从系统(和迈克尔·汉普顿)得到了足够的提示来解决这个问题。

 yum install setroubleshoot setools 

这会在/ var / log / messages中产生更多信息,并提供如下工具:

 sealert -a /var/log/audit/audit.log 

也:

 ausearch -m avc 

这些会给你如下的指示:

 Sep 29 16:11:15 ip-172-31-6-51 setroubleshoot: SELinux is preventing /usr/bin/msmtp from name_connect access on the tcp_socket port 587. For complete SELinux messages. run sealert -l 78f05dbd-a953-4196-9f14-afaabb5a4d88 

运行build议的命令:

 sealert -l 78f05dbd-a953-4196-9f14-afaabb5a4d88 

给我:

 If you believe that msmtp should be allowed name_connect access on the port 587 tcp_socket by default. Then you should report this as a bug. You can generate a local policy module to allow this access. Do allow this access for now by executing: # grep msmtp /var/log/audit/audit.log | audit2allow -M mypol # semodule -i mypol.pp 

所以我做了:

 $ grep msmtp /var/log/audit/audit.log | audit2allow -M fail2ban_msmtp 

我看了一下看看是什么造成的:

 $ vim fail2ban_msmtp.te 

然后安装策略,使其在重新启动后保持不变:

 $ semodule -i fail2ban_msmtp.pp 

然后我禁止了一个随机的IP来触发电子邮件确认现在最后通过msmtp发送给我所需的电子邮件:

 $ fail2ban-client set sshd banip 162.229.158.134 

普雷斯托! 这么简单,这个SELinux的东西。

PS另一种方式似乎是(未testing):

 $ setsebool -P nis_enabled 1