changeset 119:5c92aeaec114

Try to make the mailer's input independent of a trailing single "." -- as opensmtpd's sendmail does not grok them. Rely on closing fds in the proper order alone.
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 15 Oct 2019 23:28:39 +0200
parents cf9dde7a3a0d
children 5366fb3b222c
files bin/bsmtp2dma
diffstat 1 files changed, 25 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/bin/bsmtp2dma	Tue Oct 15 21:54:19 2019 +0200
+++ b/bin/bsmtp2dma	Tue Oct 15 23:28:39 2019 +0200
@@ -116,7 +116,8 @@
     Input (Globals):
         MAILER
         MAILCONTENT
-        MAILFIFO
+        MAILFIFO_STDIN
+        MAILFIFO_STDOUT
         CC
         FROM
         REPLYTO
@@ -131,7 +132,7 @@
 
     '
     local _recipient _rc _oifs _text _pid_mailer _recipient_addr
-    local _from_from _from_addr _sender_addr
+    local _from_from _from_addr _sender_addr _dummy
 
     _recipient="$1"
     _rc=0
@@ -156,17 +157,25 @@
        fi
     fi
 
-    mkfifo -m 0600 "${MAILFIFO}"
+    mkfifo -m 0600 "${MAILFIFO_STDIN}"
     _rc=$?
     if [ ${_rc} -ne 0 ]; then
         return ${_rc}
     fi
-
-    exec 3<>"${MAILFIFO}"
+    mkfifo -m 0600 "${MAILFIFO_STDOUT}"
+    _rc=$?
+    if [ ${_rc} -ne 0 ]; then
+        rm -f "${MAILFIFO_STDIN}"
+        return ${_rc}
+    fi
 
-    "$MAILER" -f "${_sender_addr}" "${_recipient_addr}" <&3 &
+
+    "$MAILER" -f "${_sender_addr}" "${_recipient_addr}" <${MAILFIFO_STDIN} >${MAILFIFO_STDOUT} &
     _pid_mailer=$!
 
+    exec 3>"${MAILFIFO_STDIN}"
+    exec 4<"${MAILFIFO_STDOUT}"
+
     printf "To: %s\n" "${_recipient}" >&3
     printf "From: %s\n" "${_from_from}" >&3
     if [ "${_sender_addr}" != "${_from_addr}" ]; then
@@ -199,18 +208,22 @@
         while read _text; do
             printf "%s\n" "$_text" >&3
         done
-    printf ".\n" >&3
-
+    # not all mailer recognize this
+    # printf ".\n" >&3
     IFS="$_oifs"
 
     # close the fd to the pipe: coproc should get EOF
     exec 3>&-
+    # read eventually remaining stuff from the mailer until EOF
+    IFS='' read _dummy <&4
+    exec 4<&-
 
     wait $_pid_mailer
     _rc=$?
 
-    # we are done with the named pipe
-    rm -f "${MAILFIFO}"
+    # we are done with the named pipes
+    rm -f "${MAILFIFO_STDIN}"
+    rm -f "${MAILFIFO_STDOUT}"
 
     return ${_rc}
 }
@@ -261,7 +274,8 @@
 _rc=0
 
 MAILTMPDIR="$(mktemp -d)"
-MAILFIFO="${MAILTMPDIR}/mail-stdin"
+MAILFIFO_STDIN="${MAILTMPDIR}/mail-stdin"
+MAILFIFO_STDOUT="${MAILTMPDIR}/mail-stdout"
 MAILCONTENT="${MAILTMPDIR}/mail-text"
 
 #