2 * Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * By using this file, you agree to the terms and conditions set
8 * forth in the LICENSE file which can be found at the top level of
9 * the sendmail distribution.
18 "@(#) Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.\n\
19 All rights reserved.\n\
20 Copyright (c) 1988, 1993\n\
21 The Regents of the University of California. All rights reserved.\n")
23 SM_IDSTR(id, "@(#)$Id: rmail.c,v 8.63 2013-11-22 20:51:53 ca Exp $")
26 * RMAIL -- UUCP mail server.
28 * This program reads the >From ... remote from ... lines that UUCP is so
29 * fond of and turns them into something reasonable. It then execs sendmail
30 * with various options built from these lines.
32 * The expected syntax is:
34 * <user> := [-a-z0-9]+
35 * <date> := ctime format
36 * <site> := [-a-z0-9!]+
37 * <blank line> := "^\n$"
38 * <from> := "From" <space> <user> <space> <date>
39 * [<space> "remote from" <space> <site>]
40 * <forward> := ">" <from>
41 * msg := <from> <forward>* <blank-line> <body>
43 * The output of rmail(8) compresses the <forward> lines into a single
46 * The err(3) routine is included here deliberately to make this code
47 * a bit more portable.
50 #include <sys/types.h>
51 #include <sys/param.h>
59 #include <sm/string.h>
62 # undef EX_OK /* unistd.h may have another use for this */
67 #include <sm/errstring.h>
68 #include <sendmail/pathnames.h>
70 static void err __P((int, const char *, ...));
71 static void usage __P((void));
72 static char *xalloc __P((int));
74 #define newstr(s) strcpy(xalloc(strlen(s) + 1), s)
82 /* some systems can't handle size zero mallocs */
88 err(EX_TEMPFAIL, "out of memory");
97 int ch, debug, i, pdes[2], pid, status;
98 size_t fplen = 0, fptlen = 0, len;
101 char *addrp = NULL, *domain, *p, *t;
102 char *from_path, *from_sys, *from_user;
103 char **args, buf[2048], lbuf[2048];
109 domain = "UUCP"; /* Default "domain". */
110 while ((ch = getopt(argc, argv, "D:T")) != -1)
134 from_path = from_sys = from_user = NULL;
137 /* Get and nul-terminate the line. */
138 if (sm_io_fgets(smioin, SM_TIME_DEFAULT, lbuf,
140 err(EX_DATAERR, "no data");
141 if ((p = strchr(lbuf, '\n')) == NULL)
142 err(EX_DATAERR, "line too long");
145 /* Parse lines until reach a non-"From" line. */
146 if (!strncmp(lbuf, "From ", 5))
148 else if (!strncmp(lbuf, ">From ", 6))
150 else if (offset == 0)
152 "missing or empty From line: %s", lbuf);
159 if (addrp == NULL || *addrp == '\0')
160 err(EX_DATAERR, "corrupted From line: %s", lbuf);
162 /* Use the "remote from" if it exists. */
163 for (p = addrp; (p = strchr(p + 1, 'r')) != NULL; )
165 if (!strncmp(p, "remote from ", 12))
167 for (t = p += 12; *t != '\0'; ++t)
169 if (isascii(*t) && isspace(*t))
174 (void) sm_io_fprintf(smioerr,
182 /* Else use the string up to the last bang. */
186 err(EX_DATAERR, "bang starts address: %s",
188 else if ((t = strrchr(addrp, '!')) != NULL)
195 "corrupted From line: %s", lbuf);
197 (void) sm_io_fprintf(smioerr,
203 /* 'p' now points to any system string from this line. */
206 /* Nul terminate it as necessary. */
207 for (t = p; *t != '\0'; ++t)
209 if (isascii(*t) && isspace(*t))
214 /* If the first system, copy to the from_sys string. */
215 if (from_sys == NULL)
217 from_sys = newstr(p);
219 (void) sm_io_fprintf(smioerr,
225 /* Concatenate to the path string. */
227 if (from_path == NULL)
230 if ((from_path = malloc(fptlen = 256)) == NULL)
231 err(EX_TEMPFAIL, "out of memory");
233 if (fplen + len + 2 > fptlen)
235 fptlen += SM_MAX(fplen + len + 2, 256);
236 if ((from_path = realloc(from_path,
238 err(EX_TEMPFAIL, "out of memory");
240 memmove(from_path + fplen, p, len);
242 from_path[fplen++] = '!';
243 from_path[fplen] = '\0';
246 /* Save off from user's address; the last one wins. */
247 for (p = addrp; *p != '\0'; ++p)
249 if (isascii(*p) && isspace(*p))
255 if (from_user != NULL)
257 from_user = newstr(addrp);
261 if (from_path != NULL)
262 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
265 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
266 "from_user: %s\n", from_user);
270 offset = (off_t)sm_io_tell(smioin, SM_TIME_DEFAULT);
274 /* Allocate args (with room for sendmail args as well as recipients */
275 args = (char **)xalloc(sizeof(*args) * (10 + argc));
278 args[i++] = _PATH_SENDMAIL; /* Build sendmail's argument list. */
279 args[i++] = "-G"; /* relay submission */
280 args[i++] = "-oee"; /* No errors, just status. */
282 args[i++] = "-odq"; /* Queue it, don't try to deliver. */
284 args[i++] = "-odi"; /* Deliver in foreground. */
286 args[i++] = "-oi"; /* Ignore '.' on a line by itself. */
288 /* set from system and protocol used */
289 if (from_sys == NULL)
290 sm_snprintf(buf, sizeof(buf), "-p%s", domain);
291 else if (strchr(from_sys, '.') == NULL)
292 sm_snprintf(buf, sizeof(buf), "-p%s:%s.%s",
293 domain, from_sys, domain);
295 sm_snprintf(buf, sizeof(buf), "-p%s:%s", domain, from_sys);
296 args[i++] = newstr(buf);
298 /* Set name of ``from'' person. */
299 sm_snprintf(buf, sizeof(buf), "-f%s%s",
300 from_path ? from_path : "", from_user);
301 args[i++] = newstr(buf);
304 ** Don't copy arguments beginning with - as they will be
305 ** passed to sendmail and could be interpreted as flags.
306 ** To prevent confusion of sendmail wrap < and > around
307 ** the address (helps to pass addrs like @gw1,@gw2:aa@bb)
310 while (*argv != NULL)
313 err(EX_USAGE, "dash precedes argument: %s", *argv);
315 if (strchr(*argv, ',') == NULL || strchr(*argv, '<') != NULL)
319 len = strlen(*argv) + 3;
320 if ((args[i] = malloc(len)) == NULL)
321 err(EX_TEMPFAIL, "Cannot malloc");
322 sm_snprintf(args[i++], len, "<%s>", *argv);
327 /* Paranoia check, argc used for args[] bound */
329 err(EX_SOFTWARE, "Argument count mismatch");
335 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
336 "Sendmail arguments:\n");
337 for (i = 0; args[i] != NULL; i++)
338 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
343 ** If called with a regular file as standard input, seek to the right
344 ** position in the file and just exec sendmail. Could probably skip
345 ** skip the stat, but it's not unreasonable to believe that a failed
346 ** seek will cause future reads to fail.
349 if (!fstat(STDIN_FILENO, &sb) && S_ISREG(sb.st_mode))
351 if (lseek(STDIN_FILENO, offset, SEEK_SET) != offset)
352 err(EX_TEMPFAIL, "stdin seek");
353 (void) execv(_PATH_SENDMAIL, args);
354 err(EX_OSERR, "%s", _PATH_SENDMAIL);
358 err(EX_OSERR, "pipe failed");
360 switch (pid = fork())
363 err(EX_OSERR, "fork failed");
367 if (pdes[0] != STDIN_FILENO)
369 (void) dup2(pdes[0], STDIN_FILENO);
370 (void) close(pdes[0]);
372 (void) close(pdes[1]);
373 (void) execv(_PATH_SENDMAIL, args);
374 err(EX_UNAVAILABLE, "%s", _PATH_SENDMAIL);
378 if ((fp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &(pdes[1]),
379 SM_IO_WRONLY, NULL)) == NULL)
380 err(EX_OSERR, "sm_io_open failed");
381 (void) close(pdes[0]);
383 /* Copy the file down the pipe. */
386 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", lbuf);
387 } while (sm_io_fgets(smioin, SM_TIME_DEFAULT, lbuf,
390 if (sm_io_error(smioin))
391 err(EX_TEMPFAIL, "stdin: %s", sm_errstring(errno));
393 if (sm_io_close(fp, SM_TIME_DEFAULT))
394 err(EX_OSERR, "sm_io_close failed");
396 if ((waitpid(pid, &status, 0)) == -1)
397 err(EX_OSERR, "%s", _PATH_SENDMAIL);
399 if (!WIFEXITED(status))
400 err(EX_OSERR, "%s: did not terminate normally", _PATH_SENDMAIL);
402 if (WEXITSTATUS(status))
403 err(status, "%s: terminated with %d (non-zero) status",
404 _PATH_SENDMAIL, WEXITSTATUS(status));
413 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
414 "usage: rmail [-T] [-D domain] user ...\n");
420 err(int eval, const char *fmt, ...)
422 err(eval, fmt, va_alist)
426 #endif /* __STDC__ */
432 SM_VA_START(ap, fmt);
433 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "rmail: ");
434 (void) sm_io_vfprintf(smioerr, SM_TIME_DEFAULT, fmt, ap);
436 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "\n");