changeset 111:fa73423234bf

bsmtp2dma is feature-complete
author Franz Glasner <hg@dom66.de>
date Mon, 14 Oct 2019 14:38:04 +0200
parents af4eeb94144a
children 0838fdca3a2b
files bin/bsmtp2dma
diffstat 1 files changed, 63 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/bin/bsmtp2dma	Mon Oct 14 09:47:34 2019 +0200
+++ b/bin/bsmtp2dma	Mon Oct 14 14:38:04 2019 +0200
@@ -21,23 +21,23 @@
 
 Options:
 
+  -V           Show the program version and usage and exit.
+
   -8           Does nothing. Just a compatibility option for `bsmtp`.
 
+  -c ADDRESS   Set then "CC:" header.
+
   -d n         Does nothing. Just a compatibility option for `bsmtp`.
 
   -f ADDRESS   Set the "From:" header.
 
   -h MAILHOST:PORT  Does nothing. Just a compatibility option for `bsmtp`.
 
-  -n           Do a dry-run: just print out what would be done
-
-  -s SUBJECT   Set the "Subject:" header
+  -l NUMBER    Does nothing. Just a compatibility option for `bsmtp`.
 
   -r ADDRESS   Set the "Reply-To:" header
 
-  -l NUMBER    Does nothing. Just a compatibility option for `bsmtp`.
-
-  -V           Show the program version and usage and exit.
+  -s SUBJECT   Set the "Subject:" header
 
 
 Usage:
@@ -56,7 +56,12 @@
 test -r "${CONFIGDIR}/bsmtp2dma.conf" && . "${CONFIGDIR}/bsmtp2dma.conf"
 
 
+#
 # Default configuration values
+#
+# `sendmail` is also valid for `dma` because of the mapping within
+#  `/etc/mail/mailer.conf`
+#
 : ${MAILER:=/usr/sbin/sendmail}
 
 
@@ -84,6 +89,7 @@
     local _addr
 
     _addr="$1"
+    test -n "${_addr}" || return 1
 
     if printf "%s" "${_addr}" | grep -q -E -e '^[^<>]+<[^<>]+@[^<>]+>$'; then
         email_name=$(printf '%s' "${_addr}" | sed -E -e 's/[[:space:]]*<.+$//')
@@ -120,12 +126,36 @@
         0 on success, other values on errors or the error exit code from the
         underlying mailer
 
+    This procedure starts the configured mailer as coproc and sends
+    email headers and contents to the started mailer.
+
     '
-    local _recipient _rc _oifs _text _pid_mailer
+    local _recipient _rc _oifs _text _pid_mailer _recipient_addr
+    local _from_from _from_addr _sender_addr
 
     _recipient="$1"
     _rc=0
 
+    if parse_addr "${_recipient}"; then
+        _recipient_addr="${email_addr}"
+    else
+        echo "ERROR: unknown recipient address format in \`${_recipient}'" >&2
+        return 1
+    fi
+    _sender_addr="$(whoami)@$(hostname -f)"
+    if [ -z "${FROM}" ]; then
+        _from_addr="${_sender_addr}"
+        _from_from="${_from_addr}"
+    else
+       if parse_addr "${FROM}"; then
+           _from_from="${FROM}"
+           _from_addr="${email_addr}"
+       else
+           echo "ERROR: unknown sender name in \`${FROM}'" >&2
+           return 1
+       fi
+    fi
+
     mkfifo -m 0600 "${MAILFIFO}"
     _rc=$?
     if [ ${_rc} -ne 0 ]; then
@@ -134,10 +164,28 @@
 
     exec 3<>"${MAILFIFO}"
 
-    "$MAILER" -f bacula@fmgapp7-bacula9.intern.feldmann-mg.com hostmaster@feldmann-mg.com <&3 &
+    "$MAILER" -f "${_sender_addr}" "${_recipient_addr}" <&3 &
     _pid_mailer=$!
 
-    # preserve leading white space when reading
+    printf "To: %s\n" "${_recipient}" >&3
+    printf "From: %s\n" "${_from_from}" >&3
+    if [ "${_sender_addr}" != "${_from_addr}" ]; then
+        printf "Sender: %s\n" "${_sender_addr}" >&3
+    fi
+    if [ -n "${SUBJECT}" ]; then
+        printf "Subject: %s\n" "${SUBJECT}" >&3
+    fi
+    if [ -n "${REPLYTO}" ]; then
+        # XXX TBD proper Reply-To header value checks
+        printf "Reply-To: %s\n" "${REPLYTO}" >&3
+    fi
+    if [ -n "${CC}" ]; then
+        # XXX TBD proper CC header value checks
+        printf "Cc: %s\n" "${CC}" >&3
+    fi
+    printf "\n" >&3
+
+    # preserve leading white space when reading with `read`
     _oifs="$IFS"
     IFS="
 "
@@ -162,7 +210,7 @@
 }
 
 
-while getopts "V8c:f:h:l:nr:s:" _opt; do
+while getopts "V8c:d:f:h:l:nr:s:" _opt; do
     case ${_opt} in
         V)
             echo "bsmtp2dma v${VERSION} (rv:@@HGREVISION@@)"
@@ -175,6 +223,9 @@
         c)
             CC="$OPTARG"
             ;;
+        d)
+            : # VOID
+            ;;
         f)
             FROM="$OPTARG"
             ;;
@@ -184,9 +235,6 @@
         l)
             : # VOID
             ;;
-        n)
-            DRYRUN="YES"
-            ;;
         r)
             REPLYTO="$OPTARG"
             ;;
@@ -206,8 +254,6 @@
 # return code
 _rc=0
 
-DRYRUN="NO"
-
 MAILTMPDIR="$(mktemp -d)"
 MAILFIFO="${MAILTMPDIR}/mail-stdin"
 MAILCONTENT="${MAILTMPDIR}/mail-text"
@@ -216,7 +262,7 @@
 # Clean up existing temporary stuff on all sorts of exit
 # (including the "exit" call (signal 0))
 #
-trap 'if [ -d "${MAILTMPDIR}" ]; then rm -r "${MAILTMPDIR}"; fi; exit;' 0 1 2 15
+trap 'if [ -d "${MAILTMPDIR}" ]; then rm -rf "${MAILTMPDIR}"; fi; exit;' 0 1 2 15
 
 test -d "${MAILTMPDIR}" || { echo "ERROR: no existing private tmp dir" >&2; exit 1; }
 
@@ -237,7 +283,7 @@
 # Collect the mail text from stdin into a temporary file
 #
 exec 3>"${MAILCONTENT}"
-# preserve leading white space when reading
+# preserve leading white space when reading with `read`
 _oifs="$IFS"
 IFS="
 "