git: 961bcbeef251 - main - mailwrapper(8): change behaviour if mailer.conf cannot be opened

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 19 Apr 2024 22:34:05 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=961bcbeef251b87463446860fca9910a461a3909

commit 961bcbeef251b87463446860fca9910a461a3909
Author:     Lexi Winter <lexi@le-Fay.ORG>
AuthorDate: 2023-12-30 15:09:15 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-19 22:20:44 +0000

    mailwrapper(8): change behaviour if mailer.conf cannot be opened
    
    Previously, mailwrapper(8) would default to invoking _PATH_DEFAULTMTA
    (i.e., dma) if mailer.conf couldn't be opened for any reason, including
    transient errors like ENFILE.  This behaviour is undesirable, because if
    the administrator has configured a different MTA in mailer.conf, they
    almost certainly don't want mailwrapper to unpredictably fall back to
    the compiled-in default; and in any case, the default MTA is probably
    not running, meaning the mail may be queued and then never delivered,
    which is worse than not accepting it to begin with.
    
    Change this behaviour depending on why mailer.conf can't be opened:
    
    - If it doesn't exist, keep the existing behaviour of falling back to
      the default MTA, on the assumption that this is a reasonable default
      if mailer.conf hasn't been configured at all.
    
    - If it cannot be opened for any other reason, do not invoke an MTA and
      instead return an error to the caller.
    
    PR: 25218
    Reviewed by: imp, emaste, markj
    Pull Request: https://github.com/freebsd/freebsd-src/pull/969
---
 usr.sbin/mailwrapper/mailwrapper.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/usr.sbin/mailwrapper/mailwrapper.c b/usr.sbin/mailwrapper/mailwrapper.c
index f11361f10daa..b060ff970045 100644
--- a/usr.sbin/mailwrapper/mailwrapper.c
+++ b/usr.sbin/mailwrapper/mailwrapper.c
@@ -42,6 +42,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <errno.h>
 #include <libutil.h>
 #include <sysexits.h>
 #include <syslog.h>
@@ -110,13 +111,23 @@ main(int argc, char *argv[], char *envp[])
 		mailerconf = _PATH_MAILERCONF;
 
 	if (config == NULL && ((config = fopen(mailerconf, "r")) == NULL)) {
-		addarg(&al, NULL);
+		int serrno = errno;
 		openlog(getprogname(), LOG_PID, LOG_MAIL);
-		syslog(LOG_INFO, "cannot open %s, using %s as default MTA",
-		    mailerconf, _PATH_DEFAULTMTA);
-		closelog();
-		execve(_PATH_DEFAULTMTA, al.argv, envp);
-		err(EX_OSERR, "cannot exec %s", _PATH_DEFAULTMTA);
+
+		if (serrno == ENOENT) {
+			addarg(&al, NULL);
+			syslog(LOG_INFO, "%s does not exist, using %s as default MTA",
+			    mailerconf, _PATH_DEFAULTMTA);
+			closelog();
+			execve(_PATH_DEFAULTMTA, al.argv, envp);
+			err(EX_OSERR, "cannot exec %s", _PATH_DEFAULTMTA);
+		} else {
+			syslog(LOG_INFO, "cannot open %s: %s",
+			    mailerconf, strerror(serrno));
+			closelog();
+			errno = serrno;
+			err(EX_OSERR, "cannot open %s", mailerconf);
+		}
 		/*NOTREACHED*/
 	}