#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Criticality: High Check that emails can be sent. ''' import os import subprocess import sys import random import time import imp YELLOW = '\033[93m' GREEN = '\033[92m' RED = '\033[91m' DEF = '\033[0m' os.chdir(os.path.dirname(__file__)) if not os.path.isfile('../utils.py'): print('The envsetup configuration was not found.') sys.exit(1) else: es_utils = imp.load_source('es_utils', '../utils.py') conf = es_utils.load_conf() def print_color(txt, col): print('%s%s%s' % (col, txt, DEF)) def print_yellow(txt): print_color(txt, YELLOW) def print_red(txt): print_color(txt, RED) def print_green(txt): print_color(txt, GREEN) def check_listening_port(): # check that postfix listens the port 25 correctly status, out = subprocess.getstatusoutput('netstat -pant | grep master | grep 127.0.0.1:25') if status != 0: print_red('The port 25 is not listened by postfix "master" process.') return 1 else: print_green('Postfix is listening port 25 correctly.') return 0 def check_relay(): print('Checking if SMTP relay conforms to conf.') status, out = subprocess.getstatusoutput('grep relayhost /etc/postfix/main.cf') if status == 0: configured_relay = out[len('relayhost'):].strip(' \t=') else: configured_relay = None if not configured_relay: # no relay configured, check relayless situations ip = conf.get('NETWORK_IP_NAT') if not ip: ip = conf.get('NETWORK_IP') with open('/etc/mailname', 'r') as f: d = f.read().strip() if d not in ('ubicast.tv', 'ubicast.eu'): print_yellow('/etc/mailname does not contain ubicast.eu or ubicast.tv, mails will probably not be received on ubicast mailing lists') return 3 # check spf status, output = subprocess.getstatusoutput('host -t TXT ubicast.eu') if ip not in output: print_red('ip %s is not in ubicast.eu SPF and emails sent from support@ubicast.eu will be treated as spam' % ip) return 1 conf_relay = conf.get('EMAIL_SMTP_SERVER') if conf_relay != configured_relay: print_red('Configured STMP relay (%s) does not match the expected value (%s).' % (configured_relay, conf_relay)) return 1 else: print_green('STMP relay is properly set.') return 0 def send_test_email(): email = 'noreply+%s-%s@ubicast.eu' % (time.time(), random.randint(0, 1000)) print('Sending test email to "%s".' % email) cmd = 'echo "This is a test email" | mail -s "Test email from `cat /etc/hostname`" %s' % email subprocess.getstatusoutput(cmd) time.sleep(3) cmd = 'grep "%s" /var/log/mail.log' % email status, out = subprocess.getstatusoutput(cmd) if status != 0: time.sleep(7) status, out = subprocess.getstatusoutput(cmd) if status != 0: print_red('Failed to send email.') print('No log entry found using command: %s' % cmd) return 1 if 'bounced' not in out or 'The email account that you tried to reach does not exist.' in out: print_green('Email sent.') return 0 else: print_red('Failed to send email.') print('Sending log line:\n%s' % out) return 1 if not os.path.exists('/etc/postfix'): print_red('Postfix dir does not exists, please install postfix.') sys.exit(1) rc = check_listening_port() if rc == 0: rc = check_relay() if send_test_email() == 1: rc = 1 sys.exit(rc)