Cleanups for mailwrapper(8):

- K&R -> ANSI prototype [O]
 - Do not bother to do free right before exit() or execve() [O]
 - Remove some dead code in addarg()
 - Make additional parameters specified in mailer.conf(5)
   actually work and document the fact. [N]
 - Avoid using __progname but instead use getprogname()
   and setprogname() to provide more sensible messages. [O, N]
 - Update $OpenBSD$ and $NetBSD$ to reflect the fact that we
   have sync'ed with their code.
 - WARNS=6

Obtained from/Inspired by:	OpenBSD [O], NetBSD [N] (partially)
This commit is contained in:
Xin LI 2006-06-06 05:01:12 +00:00
parent ed48a217f6
commit 8fe1b8c03e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=159326
3 changed files with 106 additions and 88 deletions

View File

@ -31,7 +31,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd December 16, 1998
.Dd May 31, 2006
.Dt MAILER.CONF 5
.Os
.Sh NAME
@ -41,29 +41,59 @@
.Sh DESCRIPTION
The file
.Pa /etc/mail/mailer.conf
contains a series of pairs.
The first member of each pair is the name
contains a series of lines of the form
.Pp
.Pa name
.Pa program
.Op Ar arguments ...
.Pp
The first word of each line is the
.Pa name
of a program invoking
.Xr mailwrapper 8
which is typically a symbolic link to
.Pa /usr/sbin/sendmail .
(On a typical system,
.Xr mailwrapper 8 .
(For example, on a typical system
.Pa /usr/sbin/sendmail
would be a symbolic link to
.Xr mailwrapper 8 ,
as would
.Xr newaliases 1
and
.Xr mailq 1
would be set up this way.)
The second member of each pair is the name of the program to
actually execute when the first name is invoked.
The file may also
contain comments, denoted by a # mark in the first column of any line.
.Xr mailq 1 .
Thus,
.Pa name
might be
.Dq sendmail
or
.Dq newaliases
etc.)
.Pp
The second word of each line is the name of the
.Pa program
to actually execute when the first name is invoked.
.Pp
The further
.Ar arguments ,
if any, are passed to the
.Pa program ,
followed by the arguments
.Xr mailwrapper 8
was called with.
.Pp
The file may also contain comment lines, denoted by a
.Sq #
mark in the first column of any line.
first column of any line.
.Sh FILES
/etc/mail/mailer.conf
.Sh EXAMPLES
The following is an example of how to set up an
This example shows how to set up
.Nm
for traditional sendmail invocation behavior.
.Bd -literal
# Execute the "real" sendmail program, named /usr/libexec/sendmail/sendmail
to invoke the traditional
.Xr sendmail 8
program:
.Bd -literal -offset indent
# Execute the "real" sendmail program located in
# /usr/libexec/sendmail/sendmail
sendmail /usr/libexec/sendmail/sendmail
send-mail /usr/libexec/sendmail/sendmail
mailq /usr/libexec/sendmail/sendmail
@ -71,20 +101,35 @@ newaliases /usr/libexec/sendmail/sendmail
.Ed
.Pp
This example shows how to invoke a sendmail-workalike like Postfix in
place of sendmail.
.Bd -literal
place of
.Xr sendmail 8 :
.Bd -literal -offset indent
# Emulate sendmail using postfix
sendmail /usr/local/sbin/sendmail
send-mail /usr/local/sbin/sendmail
mailq /usr/local/sbin/sendmail
newaliases /usr/local/sbin/sendmail
.Ed
.Pp
This example shows the use of the mini_sendmail package from ports
in place of
.Xr sendmail 8 :
Note the use of additional arguments.
.Bd -literal -offset indent
# Send outgoing mail to a smart relay using mini_sendmail
sendmail /usr/local/bin/mini_sendmail -srelayhost
send-mail /usr/local/bin/mini_sendmail -srelayhost
.Ed
.Sh SEE ALSO
.Xr mail 1 ,
.Xr mailq 1 ,
.Xr newaliases 1 ,
.Xr mailwrapper 8 ,
.Xr sendmail 8
.Pp
.Xr postfix 1
.Pq Pa ports/mail/postfix ,
.Xr mini_sendmail 8 Pq Pa ports/mail/mini_sendmail
.Sh HISTORY
.Nm
appeared in

View File

@ -8,6 +8,7 @@ MAN= mailwrapper.8
DPADD= ${LIBUTIL}
LDADD= -lutil
WARNS?= 6
.endif
.if ${MK_MAILWRAPPER} != "no" || ${MK_SENDMAIL} != "no"

View File

@ -1,5 +1,5 @@
/* $OpenBSD: mailwrapper.c,v 1.6 1999/12/17 05:06:28 mickey Exp $ */
/* $NetBSD: mailwrapper.c,v 1.3 1999/05/29 18:18:15 christos Exp $ */
/* $OpenBSD: mailwrapper.c,v 1.15 2003/03/09 01:24:26 millert Exp $ */
/* $NetBSD: mailwrapper.c,v 1.7 2003/02/17 21:18:46 is Exp $ */
/*
* Copyright (c) 1998
@ -38,11 +38,11 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <libutil.h>
#include <sysexits.h>
#include <syslog.h>
#include <stdarg.h>
#include "pathnames.h"
@ -54,93 +54,66 @@ struct arglist {
int main(int, char *[], char *[]);
static void initarg(struct arglist *);
static void addarg(struct arglist *, const char *, int);
static void freearg(struct arglist *, int);
extern const char *__progname; /* from crt0.o */
static void addarg(struct arglist *, const char *);
static void
initarg(al)
struct arglist *al;
initarg(struct arglist *al)
{
al->argc = 0;
al->maxc = 10;
if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
err(1, NULL);
err(EX_TEMPFAIL, "malloc");
}
static void
addarg(al, arg, copy)
struct arglist *al;
const char *arg;
int copy;
addarg(struct arglist *al, const char *arg)
{
char **argv2;
if (al->argc == al->maxc) {
al->maxc <<= 1;
if ((argv2 = realloc(al->argv,
al->maxc * sizeof(char *))) == NULL) {
if (al->argv)
free(al->argv);
al->argv = NULL;
err(1, NULL);
} else {
al->argv = argv2;
}
al->argv = realloc(al->argv, al->maxc * sizeof(char *));
if (al->argv == NULL)
err(EX_TEMPFAIL, "realloc");
}
if (copy) {
if ((al->argv[al->argc++] = strdup(arg)) == NULL)
err(1, NULL);
} else
al->argv[al->argc++] = (char *)arg;
}
static void
freearg(al, copy)
struct arglist *al;
int copy;
{
size_t i;
if (copy)
for (i = 0; i < al->argc; i++)
free(al->argv[i]);
free(al->argv);
if (arg == NULL)
al->argv[al->argc++] = NULL;
else if ((al->argv[al->argc++] = strdup(arg)) == NULL)
err(EX_TEMPFAIL, "strdup");
}
int
main(argc, argv, envp)
int argc;
char *argv[];
char *envp[];
main(int argc, char *argv[], char *envp[])
{
FILE *config;
char *line, *cp, *from, *to, *ap;
const char *progname;
size_t len, lineno = 0;
int i;
struct arglist al;
/* change __progname to mailwrapper so we get sensible error messages */
progname = getprogname();
setprogname("mailwrapper");
initarg(&al);
for (len = 0; len < argc; len++)
addarg(&al, argv[len], 0);
addarg(&al, argv[0]);
if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
addarg(&al, NULL, 0);
openlog("mailwrapper", LOG_PID, LOG_MAIL);
syslog(LOG_INFO, "can't open %s, using %s as default MTA",
addarg(&al, NULL);
openlog(getprogname(), LOG_PID, LOG_MAIL);
syslog(LOG_INFO, "cannot open %s, using %s as default MTA",
_PATH_MAILERCONF, _PATH_DEFAULTMTA);
closelog();
execve(_PATH_DEFAULTMTA, al.argv, envp);
freearg(&al, 0);
err(1, "execing %s", _PATH_DEFAULTMTA);
err(EX_OSERR, "cannot exec %s", _PATH_DEFAULTMTA);
/*NOTREACHED*/
}
for (;;) {
if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
if (feof(config))
errx(1, "no mapping in %s", _PATH_MAILERCONF);
err(1, "can't parse line %lu", (u_long)lineno);
errx(EX_CONFIG, "no mapping in %s", _PATH_MAILERCONF);
err(EX_CONFIG, "cannot parse line %lu", (u_long)lineno);
}
#define WS " \t\n"
@ -161,11 +134,12 @@ main(argc, argv, envp)
if ((to = strsep(&cp, WS)) == NULL)
goto parse_error;
if (strcmp(from, __progname) == 0) {
for (ap = strsep(&cp, WS); ap != NULL;
ap = strsep(&cp, WS))
if (*ap)
addarg(&al, ap, 0);
if (strcmp(from, progname) == 0) {
for (ap = strsep(&cp, WS); ap != NULL;
ap = strsep(&cp, WS)) {
if (*ap)
addarg(&al, ap);
}
break;
}
@ -174,17 +148,15 @@ main(argc, argv, envp)
(void)fclose(config);
addarg(&al, NULL, 0);
for (i = 1; i < argc; i++)
addarg(&al, argv[i]);
addarg(&al, NULL);
execve(to, al.argv, envp);
freearg(&al, 0);
warn("execing %s", to);
free(line);
exit(1);
err(EX_OSERR, "cannot exec %s", to);
/*NOTREACHED*/
parse_error:
freearg(&al, 0);
free(line);
errx(1, "parse error in %s at line %lu",
errx(EX_CONFIG, "parse error in %s at line %lu",
_PATH_MAILERCONF, (u_long)lineno);
/*NOTREACHED*/
}