* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char copyright
[] =
"@(#) Copyright (c) 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)rmail.c 8.1 (Berkeley) 5/31/93";
* RMAIL -- UUCP mail server.
* This program reads the >From ... remote from ... lines that UUCP is so
* fond of and turns them into something reasonable. It then execs sendmail
* with various options built from these lines.
* The expected syntax is:
* <from> := "From" <space> <user> <space> <date>
* [<space> "remote from" <space> <site>]
* <forward> := ">" <from>
* msg := <from> <forward>* <blank-line> <body>
* The output of rmail(8) compresses the <forward> lines into a single
* The err(3) routine is included here deliberately to make this code
void err
__P((int, const char *, ...));
extern int errno
, optind
;
size_t fplen
, fptlen
, len
;
int ch
, debug
, i
, pdes
[2], pid
, status
;
char *addrp
, *domain
, *p
, *t
;
char *from_path
, *from_sys
, *from_user
;
char *args
[100], buf
[2048], lbuf
[2048];
domain
= "UUCP"; /* Default "domain". */
while ((ch
= getopt(argc
, argv
, "D:T")) != EOF
)
from_path
= from_sys
= from_user
= NULL
;
/* Get and nul-terminate the line. */
if (fgets(lbuf
, sizeof(lbuf
), stdin
) == NULL
)
if ((p
= strchr(lbuf
, '\n')) == NULL
)
err(EX_DATAERR
, "line too long");
/* Parse lines until reach a non-"From" line. */
if (!strncmp(lbuf
, "From ", 5))
else if (!strncmp(lbuf
, ">From ", 6))
"missing or empty From line: %s", lbuf
);
err(EX_DATAERR
, "corrupted From line: %s", lbuf
);
/* Use the "remote from" if it exists. */
for (p
= addrp
; (p
= strchr(p
+ 1, 'r')) != NULL
;)
if (!strncmp(p
, "remote from ", 12)) {
for (t
= p
+= 12; *t
&& !isspace(*t
); ++t
);
/* Else use the string up to the last bang. */
"bang starts address: %s", addrp
);
else if ((t
= strrchr(addrp
, '!')) != NULL
) {
"corrupted From line: %s", lbuf
);
(void)fprintf(stderr
, "bang: %s\n", p
);
/* 'p' now points to any system string from this line. */
/* Nul terminate it as necessary. */
for (t
= p
; *t
&& !isspace(*t
); ++t
);
/* If the first system, copy to the from_sys string. */
if ((from_sys
= strdup(p
)) == NULL
)
"from_sys: %s\n", from_sys
);
/* Concatenate to the path string. */
if ((from_path
= malloc(fptlen
= 256)) == NULL
)
if (fplen
+ len
+ 2 > fptlen
) {
fptlen
+= MAX(fplen
+ len
+ 2, 256);
realloc(from_path
, fptlen
)) == NULL
)
memmove(from_path
+ fplen
, p
, len
);
from_path
[fplen
++] = '!';
/* Save off from user's address; the last one wins. */
for (p
= addrp
; *p
&& !isspace(*p
); ++p
);
if ((from_user
= strdup(addrp
)) == NULL
)
"from_path: %s\n", from_path
);
(void)fprintf(stderr
, "from_user: %s\n", from_user
);
offset
= (off_t
)ftell(stdin
);
args
[i
++] = _PATH_SENDMAIL
; /* Build sendmail's argument list. */
args
[i
++] = "-oee"; /* No errors, just status. */
args
[i
++] = "-odq"; /* Queue it, don't try to deliver. */
args
[i
++] = "-oi"; /* Ignore '.' on a line by itself. */
if (from_sys
!= NULL
) { /* Set sender's host name. */
if (strchr(from_sys
, '.') == NULL
)
(void)snprintf(buf
, sizeof(buf
),
"-oMs%s.%s", from_sys
, domain
);
(void)snprintf(buf
, sizeof(buf
), "-oMs%s", from_sys
);
if ((args
[i
++] = strdup(buf
)) == NULL
)
(void)snprintf(buf
, sizeof(buf
), "-oMr%s", domain
);
if ((args
[i
++] = strdup(buf
)) == NULL
)
/* Set name of ``from'' person. */
(void)snprintf(buf
, sizeof(buf
), "-f%s%s",
from_path
? from_path
: "", from_user
);
if ((args
[i
++] = strdup(buf
)) == NULL
)
* Don't copy arguments beginning with - as they will be
* passed to sendmail and could be interpreted as flags.
if (*argv
&& **argv
== '-')
err(EX_USAGE
, "dash precedes argument: %s", *argv
);
} while ((args
[i
++] = *argv
++) != NULL
);
(void)fprintf(stderr
, "Sendmail arguments:\n");
for (i
= 0; args
[i
]; i
++)
(void)fprintf(stderr
, "\t%s\n", args
[i
]);
* If called with a regular file as standard input, seek to the right
* position in the file and just exec sendmail. Could probably skip
* skip the stat, but it's not unreasonable to believe that a failed
* seek will cause future reads to fail.
if (!fstat(STDIN_FILENO
, &sb
) && S_ISREG(sb
.st_mode
)) {
if (lseek(STDIN_FILENO
, offset
, SEEK_SET
) != offset
)
err(EX_TEMPFAIL
, "stdin seek");
execv(_PATH_SENDMAIL
, args
);
err(EX_OSERR
, "%s", _PATH_SENDMAIL
);
if (pdes
[0] != STDIN_FILENO
) {
(void)dup2(pdes
[0], STDIN_FILENO
);
execv(_PATH_SENDMAIL
, args
);
if ((fp
= fdopen(pdes
[1], "w")) == NULL
)
/* Copy the file down the pipe. */
(void)fprintf(fp
, "%s", lbuf
);
} while (fgets(lbuf
, sizeof(lbuf
), stdin
) != NULL
);
err(EX_TEMPFAIL
, "stdin: %s", strerror(errno
));
if ((waitpid(pid
, &status
, 0)) == -1)
err(EX_OSERR
, "%s", _PATH_SENDMAIL
);
"%s: did not terminate normally", _PATH_SENDMAIL
);
err(status
, "%s: terminated with %d (non-zero) status",
_PATH_SENDMAIL
, WEXITSTATUS(status
));
(void)fprintf(stderr
, "usage: rmail [-T] [-D domain] user ...\n");
err(int eval
, const char *fmt
, ...)
(void)fprintf(stderr
, "rmail: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");