Mercurial > hgrepos > FreeBSD > ports > sysutils > local-bsdtools
comparison bin/bsmtp2dma @ 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 | dad9f2d80c10 |
| children | 5366fb3b222c |
comparison
equal
deleted
inserted
replaced
| 118:cf9dde7a3a0d | 119:5c92aeaec114 |
|---|---|
| 114 Will be written into the "To:" header also. | 114 Will be written into the "To:" header also. |
| 115 | 115 |
| 116 Input (Globals): | 116 Input (Globals): |
| 117 MAILER | 117 MAILER |
| 118 MAILCONTENT | 118 MAILCONTENT |
| 119 MAILFIFO | 119 MAILFIFO_STDIN |
| 120 MAILFIFO_STDOUT | |
| 120 CC | 121 CC |
| 121 FROM | 122 FROM |
| 122 REPLYTO | 123 REPLYTO |
| 123 SUBJECT | 124 SUBJECT |
| 124 | 125 |
| 129 This procedure starts the configured mailer as coproc and sends | 130 This procedure starts the configured mailer as coproc and sends |
| 130 email headers and contents to the started mailer. | 131 email headers and contents to the started mailer. |
| 131 | 132 |
| 132 ' | 133 ' |
| 133 local _recipient _rc _oifs _text _pid_mailer _recipient_addr | 134 local _recipient _rc _oifs _text _pid_mailer _recipient_addr |
| 134 local _from_from _from_addr _sender_addr | 135 local _from_from _from_addr _sender_addr _dummy |
| 135 | 136 |
| 136 _recipient="$1" | 137 _recipient="$1" |
| 137 _rc=0 | 138 _rc=0 |
| 138 | 139 |
| 139 if parse_addr "${_recipient}"; then | 140 if parse_addr "${_recipient}"; then |
| 154 echo "ERROR: unknown sender name in \`${FROM}'" >&2 | 155 echo "ERROR: unknown sender name in \`${FROM}'" >&2 |
| 155 return 1 | 156 return 1 |
| 156 fi | 157 fi |
| 157 fi | 158 fi |
| 158 | 159 |
| 159 mkfifo -m 0600 "${MAILFIFO}" | 160 mkfifo -m 0600 "${MAILFIFO_STDIN}" |
| 160 _rc=$? | 161 _rc=$? |
| 161 if [ ${_rc} -ne 0 ]; then | 162 if [ ${_rc} -ne 0 ]; then |
| 162 return ${_rc} | 163 return ${_rc} |
| 163 fi | 164 fi |
| 164 | 165 mkfifo -m 0600 "${MAILFIFO_STDOUT}" |
| 165 exec 3<>"${MAILFIFO}" | 166 _rc=$? |
| 166 | 167 if [ ${_rc} -ne 0 ]; then |
| 167 "$MAILER" -f "${_sender_addr}" "${_recipient_addr}" <&3 & | 168 rm -f "${MAILFIFO_STDIN}" |
| 169 return ${_rc} | |
| 170 fi | |
| 171 | |
| 172 | |
| 173 "$MAILER" -f "${_sender_addr}" "${_recipient_addr}" <${MAILFIFO_STDIN} >${MAILFIFO_STDOUT} & | |
| 168 _pid_mailer=$! | 174 _pid_mailer=$! |
| 175 | |
| 176 exec 3>"${MAILFIFO_STDIN}" | |
| 177 exec 4<"${MAILFIFO_STDOUT}" | |
| 169 | 178 |
| 170 printf "To: %s\n" "${_recipient}" >&3 | 179 printf "To: %s\n" "${_recipient}" >&3 |
| 171 printf "From: %s\n" "${_from_from}" >&3 | 180 printf "From: %s\n" "${_from_from}" >&3 |
| 172 if [ "${_sender_addr}" != "${_from_addr}" ]; then | 181 if [ "${_sender_addr}" != "${_from_addr}" ]; then |
| 173 printf "Sender: %s\n" "${_sender_addr}" >&3 | 182 printf "Sender: %s\n" "${_sender_addr}" >&3 |
| 197 " | 206 " |
| 198 cat "${MAILCONTENT}" | | 207 cat "${MAILCONTENT}" | |
| 199 while read _text; do | 208 while read _text; do |
| 200 printf "%s\n" "$_text" >&3 | 209 printf "%s\n" "$_text" >&3 |
| 201 done | 210 done |
| 202 printf ".\n" >&3 | 211 # not all mailer recognize this |
| 203 | 212 # printf ".\n" >&3 |
| 204 IFS="$_oifs" | 213 IFS="$_oifs" |
| 205 | 214 |
| 206 # close the fd to the pipe: coproc should get EOF | 215 # close the fd to the pipe: coproc should get EOF |
| 207 exec 3>&- | 216 exec 3>&- |
| 217 # read eventually remaining stuff from the mailer until EOF | |
| 218 IFS='' read _dummy <&4 | |
| 219 exec 4<&- | |
| 208 | 220 |
| 209 wait $_pid_mailer | 221 wait $_pid_mailer |
| 210 _rc=$? | 222 _rc=$? |
| 211 | 223 |
| 212 # we are done with the named pipe | 224 # we are done with the named pipes |
| 213 rm -f "${MAILFIFO}" | 225 rm -f "${MAILFIFO_STDIN}" |
| 226 rm -f "${MAILFIFO_STDOUT}" | |
| 214 | 227 |
| 215 return ${_rc} | 228 return ${_rc} |
| 216 } | 229 } |
| 217 | 230 |
| 218 | 231 |
| 259 | 272 |
| 260 # return code | 273 # return code |
| 261 _rc=0 | 274 _rc=0 |
| 262 | 275 |
| 263 MAILTMPDIR="$(mktemp -d)" | 276 MAILTMPDIR="$(mktemp -d)" |
| 264 MAILFIFO="${MAILTMPDIR}/mail-stdin" | 277 MAILFIFO_STDIN="${MAILTMPDIR}/mail-stdin" |
| 278 MAILFIFO_STDOUT="${MAILTMPDIR}/mail-stdout" | |
| 265 MAILCONTENT="${MAILTMPDIR}/mail-text" | 279 MAILCONTENT="${MAILTMPDIR}/mail-text" |
| 266 | 280 |
| 267 # | 281 # |
| 268 # Clean up existing temporary stuff on all sorts of exit | 282 # Clean up existing temporary stuff on all sorts of exit |
| 269 # (including the "exit" call (signal 0)) | 283 # (including the "exit" call (signal 0)) |
