From f7d8523942f5c98db7be0d2233d906dfdd78dd27 Mon Sep 17 00:00:00 2001 From: Julian Rother Date: Thu, 31 Mar 2022 02:28:26 +0200 Subject: [PATCH] Remove ssmtp support and add From overwrite option --- defaults/main.yml | 5 +++- tasks/main.yml | 43 +++++++++++++++++++++++++++++----- tasks/nullmailer.yml | 27 --------------------- tasks/ssmtp.yml | 9 ------- templates/nullmailer-inject.j2 | 23 ++++++++++++++++++ templates/smtp.conf.j2 | 21 ----------------- 6 files changed, 64 insertions(+), 64 deletions(-) delete mode 100644 tasks/nullmailer.yml delete mode 100644 tasks/ssmtp.yml create mode 100644 templates/nullmailer-inject.j2 delete mode 100644 templates/smtp.conf.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 3713d7b..1b98d3a 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,8 @@ mail: + from: "noreply@example.com" + overwrite_from_envelope: no + overwrite_from_header: no + overwrite_from_header_except_users: [] root: "root@example.com" server: "mail.example.com" domain: "example.com" - backend: nullmailer diff --git a/tasks/main.yml b/tasks/main.yml index 3f9d183..575a036 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,6 +1,37 @@ -- include_tasks: ssmtp.yml - when: - - ansible_distribution_release == "stretch" - - mail.backend == "ssmtp" -- include_tasks: nullmailer.yml - when: (ansible_distribution_release != "stretch" or mail.backend != "ssmtp") +- name: install nullmailer + apt: + pkg: nullmailer + +- name: divert nullmailer-inject + community.general.dpkg_divert: + path: /usr/bin/nullmailer-inject + divert: /usr/bin/nullmailer-inject-bin + rename: yes + +- name: create nullmailer-inject wrapper script + template: + src: nullmailer-inject.j2 + dest: /usr/bin/nullmailer-inject + mode: 0755 + +- name: setup nullmailer envelope from overwrite + copy: + dest: "/etc/nullmailer/allmailfrom" + mode: "0644" + owner: root + group: root + content: "{{ mail.from if mail.overwrite_from_envelope else '' }}" + +- name: setup nullmailer + notify: restart nullmailer + copy: + dest: "{{ item.dest }}" + content: "{{ item.content }}" + owner: "{{ item.owner }}" + group: "{{ item.group }}" + mode: "{{ item.mode }}" + with_items: + - {dest: "/etc/nullmailer/adminaddr", mode: "0644", owner: root, group: root, content: "{{ mail.root }}"} + - {dest: "/etc/nullmailer/remotes", mode: "0600", owner: mail, group: mail, content: "{{ mail.server }}"} + - {dest: "/etc/nullmailer/defaultdomain", mode: "0644", owner: root, group: root, content: "{{ mail.domain }}"} + - {dest: "/etc/mailname", mode: "0644", owner: root, group: root, content: "{{ inventory_hostname }}\n"} diff --git a/tasks/nullmailer.yml b/tasks/nullmailer.yml deleted file mode 100644 index 78f0e7e..0000000 --- a/tasks/nullmailer.yml +++ /dev/null @@ -1,27 +0,0 @@ -- name: install nullmailer - apt: - pkg: nullmailer - -- name: remove ssmtp - apt: - pkg: ssmtp - state: absent - -- name: remove ssmtp config - file: - path: /etc/ssmtp/ssmtp.conf - state: absent - -- name: setup nullmailer - notify: restart nullmailer - copy: - dest: "{{ item.dest }}" - content: "{{ item.content }}" - owner: "{{ item.owner }}" - group: "{{ item.group }}" - mode: "{{ item.mode }}" - with_items: - - {dest: "/etc/nullmailer/adminaddr", mode: "0644", owner: root, group: root, content: "{{ mail.root }}"} - - {dest: "/etc/nullmailer/remotes", mode: "0600", owner: mail, group: mail, content: "{{ mail.server }}"} - - {dest: "/etc/nullmailer/defaultdomain", mode: "0644", owner: root, group: root, content: "{{ mail.domain }}"} - - {dest: "/etc/mailname", mode: "0644", owner: root, group: root, content: "{{ inventory_hostname }}\n"} diff --git a/tasks/ssmtp.yml b/tasks/ssmtp.yml deleted file mode 100644 index 8b841ae..0000000 --- a/tasks/ssmtp.yml +++ /dev/null @@ -1,9 +0,0 @@ -- name: install ssmtp - apt: - pkg: ssmtp - -- name: setup ssmtp - template: - src: smtp.conf.j2 - dest: /etc/ssmtp/ssmtp.conf - mode: 0600 diff --git a/templates/nullmailer-inject.j2 b/templates/nullmailer-inject.j2 new file mode 100644 index 0000000..2866df0 --- /dev/null +++ b/templates/nullmailer-inject.j2 @@ -0,0 +1,23 @@ +#!/bin/sh + +{% if mail.overwrite_from_header %} +username="$(id -un 2> /dev/null || echo nobody)" + +excepted=0 + +for excepted_user in {% for item in mail.overwrite_from_header_except_users %} {{ item|quote }} {% endfor %}; do + if [ "$username" = "$excepted_user" ]; then + excepted=1 + break + fi +done + +if [ "$excepted" = "0" ]; then + export NULLMAILER_NAME="${username} on $(hostname -f)" + export NULLMAILER_USER={{ mail.from.split('@')[0]|quote }} + export NULLMAILER_HOST={{ mail.from.split('@', 1)[1]|quote }} + export NULLMAILER_FLAGS=f +fi +{% endif %} + +exec /usr/bin/nullmailer-inject-bin "$@" diff --git a/templates/smtp.conf.j2 b/templates/smtp.conf.j2 deleted file mode 100644 index 783aa6d..0000000 --- a/templates/smtp.conf.j2 +++ /dev/null @@ -1,21 +0,0 @@ -# -# Config file for sSMTP sendmail -# -# The person who gets all mail for userids < 1000 -# Make this empty to disable rewriting. -root={{ mail['root'] }} - -# The place where the mail goes. The actual machine name is required no -# MX records are consulted. Commonly mailhosts are named mail.domain.com -mailhub={{ mail['server'] }} - -# Where will the mail seem to come from? -rewriteDomain={{ mail['domain'] }} - -# The full hostname -hostname={{ inventory_hostname }} - -# Are users allowed to set their own From: address? -# YES - Allow the user to specify their own From: address -# NO - Use the system generated From: address -FromLineOverride=YES