2 * Copyright (c) 1998-2001 Sendmail, 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.
16 "@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
17 All rights reserved.\n\
18 Copyright (c) 1988, 1993\n\
19 The Regents of the University of California. All rights reserved.\n")
21 SM_IDSTR(id, "@(#)$Id: rmail.c,v 8.61 2001/09/18 21:45:29 gshapiro Exp $")
24 * RMAIL -- UUCP mail server.
26 * This program reads the >From ... remote from ... lines that UUCP is so
27 * fond of and turns them into something reasonable. It then execs sendmail
28 * with various options built from these lines.
30 * The expected syntax is:
32 * <user> := [-a-z0-9]+
33 * <date> := ctime format
34 * <site> := [-a-z0-9!]+
35 * <blank line> := "^\n$"
36 * <from> := "From" <space> <user> <space> <date>
37 * [<space> "remote from" <space> <site>]
38 * <forward> := ">" <from>
39 * msg := <from> <forward>* <blank-line> <body>
41 * The output of rmail(8) compresses the <forward> lines into a single
44 * The err(3) routine is included here deliberately to make this code
45 * a bit more portable.
48 #include <sys/types.h>
49 #include <sys/param.h>
57 #include <sm/string.h>
60 # undef EX_OK /* unistd.h may have another use for this */
65 #include <sm/errstring.h>
66 #include <sendmail/pathnames.h>
68 static void err __P((int, const char *, ...));
69 static void usage __P((void));
70 static char *xalloc __P((int));
72 #define newstr(s) strcpy(xalloc(strlen(s) + 1), s)
80 /* some systems can't handle size zero mallocs */
86 err(EX_TEMPFAIL, "out of memory");
95 int ch, debug, i, pdes[2], pid, status;
96 size_t fplen = 0, fptlen = 0, len;
99 char *addrp = NULL, *domain, *p, *t;
100 char *from_path, *from_sys, *from_user;
101 char **args, buf[2048], lbuf[2048];
107 domain = "UUCP"; /* Default "domain". */
108 while ((ch = getopt(argc, argv, "D:T")) != -1)
132 from_path = from_sys = from_user = NULL;
135 /* Get and nul-terminate the line. */
136 if (sm_io_fgets(smioin, SM_TIME_DEFAULT, lbuf,
137 sizeof(lbuf)) == NULL)
138 err(EX_DATAERR, "no data");
139 if ((p = strchr(lbuf, '\n')) == NULL)
140 err(EX_DATAERR, "line too long");
143 /* Parse lines until reach a non-"From" line. */
144 if (!strncmp(lbuf, "From ", 5))
146 else if (!strncmp(lbuf, ">From ", 6))
148 else if (offset == 0)
150 "missing or empty From line: %s", lbuf);
157 if (addrp == NULL || *addrp == '\0')
158 err(EX_DATAERR, "corrupted From line: %s", lbuf);
160 /* Use the "remote from" if it exists. */
161 for (p = addrp; (p = strchr(p + 1, 'r')) != NULL; )
163 if (!strncmp(p, "remote from ", 12))
165 for (t = p += 12; *t != '\0'; ++t)
167 if (isascii(*t) && isspace(*t))
172 (void) sm_io_fprintf(smioerr,
180 /* Else use the string up to the last bang. */
184 err(EX_DATAERR, "bang starts address: %s",
186 else if ((t = strrchr(addrp, '!')) != NULL)
193 "corrupted From line: %s", lbuf);
195 (void) sm_io_fprintf(smioerr,
201 /* 'p' now points to any system string from this line. */
204 /* Nul terminate it as necessary. */
205 for (t = p; *t != '\0'; ++t)
207 if (isascii(*t) && isspace(*t))
212 /* If the first system, copy to the from_sys string. */
213 if (from_sys == NULL)
215 from_sys = newstr(p);
217 (void) sm_io_fprintf(smioerr,
223 /* Concatenate to the path string. */
225 if (from_path == NULL)
228 if ((from_path = malloc(fptlen = 256)) == NULL)
229 err(EX_TEMPFAIL, "out of memory");
231 if (fplen + len + 2 > fptlen)
233 fptlen += SM_MAX(fplen + len + 2, 256);
234 if ((from_path = realloc(from_path,
236 err(EX_TEMPFAIL, "out of memory");
238 memmove(from_path + fplen, p, len);
240 from_path[fplen++] = '!';
241 from_path[fplen] = '\0';
244 /* Save off from user's address; the last one wins. */
245 for (p = addrp; *p != '\0'; ++p)
247 if (isascii(*p) && isspace(*p))
253 if (from_user != NULL)
255 from_user = newstr(addrp);
259 if (from_path != NULL)
260 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
263 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
264 "from_user: %s\n", from_user);
268 offset = (off_t)sm_io_tell(smioin, SM_TIME_DEFAULT);
272 /* Allocate args (with room for sendmail args as well as recipients */
273 args = (char **)xalloc(sizeof(*args) * (10 + argc));
276 args[i++] = _PATH_SENDMAIL; /* Build sendmail's argument list. */
277 args[i++] = "-G"; /* relay submission */
278 args[i++] = "-oee"; /* No errors, just status. */
279 args[i++] = "-odq"; /* Queue it, don't try to deliver. */
280 args[i++] = "-oi"; /* Ignore '.' on a line by itself. */
282 /* set from system and protocol used */
283 if (from_sys == NULL)
284 sm_snprintf(buf, sizeof(buf), "-p%s", domain);
285 else if (strchr(from_sys, '.') == NULL)
286 sm_snprintf(buf, sizeof(buf), "-p%s:%s.%s",
287 domain, from_sys, domain);
289 sm_snprintf(buf, sizeof(buf), "-p%s:%s", domain, from_sys);
290 args[i++] = newstr(buf);
292 /* Set name of ``from'' person. */
293 sm_snprintf(buf, sizeof(buf), "-f%s%s",
294 from_path ? from_path : "", from_user);
295 args[i++] = newstr(buf);
298 ** Don't copy arguments beginning with - as they will be
299 ** passed to sendmail and could be interpreted as flags.
300 ** To prevent confusion of sendmail wrap < and > around
301 ** the address (helps to pass addrs like @gw1,@gw2:aa@bb)
304 while (*argv != NULL)
307 err(EX_USAGE, "dash precedes argument: %s", *argv);
309 if (strchr(*argv, ',') == NULL || strchr(*argv, '<') != NULL)
313 len = strlen(*argv) + 3;
314 if ((args[i] = malloc(len)) == NULL)
315 err(EX_TEMPFAIL, "Cannot malloc");
316 sm_snprintf(args[i++], len, "<%s>", *argv);
321 /* Paranoia check, argc used for args[] bound */
323 err(EX_SOFTWARE, "Argument count mismatch");
329 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
330 "Sendmail arguments:\n");
331 for (i = 0; args[i] != NULL; i++)
332 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
337 ** If called with a regular file as standard input, seek to the right
338 ** position in the file and just exec sendmail. Could probably skip
339 ** skip the stat, but it's not unreasonable to believe that a failed
340 ** seek will cause future reads to fail.
343 if (!fstat(STDIN_FILENO, &sb) && S_ISREG(sb.st_mode))
345 if (lseek(STDIN_FILENO, offset, SEEK_SET) != offset)
346 err(EX_TEMPFAIL, "stdin seek");
347 (void) execv(_PATH_SENDMAIL, args);
348 err(EX_OSERR, "%s", _PATH_SENDMAIL);
352 err(EX_OSERR, "pipe failed");
354 switch (pid = fork())
357 err(EX_OSERR, "fork failed");
361 if (pdes[0] != STDIN_FILENO)
363 (void) dup2(pdes[0], STDIN_FILENO);
364 (void) close(pdes[0]);
366 (void) close(pdes[1]);
367 (void) execv(_PATH_SENDMAIL, args);
368 err(EX_UNAVAILABLE, "%s", _PATH_SENDMAIL);
372 if ((fp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &(pdes[1]),
373 SM_IO_WRONLY, NULL)) == NULL)
374 err(EX_OSERR, "sm_io_open failed");
375 (void) close(pdes[0]);
377 /* Copy the file down the pipe. */
380 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", lbuf);
381 } while (sm_io_fgets(smioin, SM_TIME_DEFAULT, lbuf,
382 sizeof(lbuf)) != NULL);
384 if (sm_io_error(smioin))
385 err(EX_TEMPFAIL, "stdin: %s", sm_errstring(errno));
387 if (sm_io_close(fp, SM_TIME_DEFAULT))
388 err(EX_OSERR, "sm_io_close failed");
390 if ((waitpid(pid, &status, 0)) == -1)
391 err(EX_OSERR, "%s", _PATH_SENDMAIL);
393 if (!WIFEXITED(status))
394 err(EX_OSERR, "%s: did not terminate normally", _PATH_SENDMAIL);
396 if (WEXITSTATUS(status))
397 err(status, "%s: terminated with %d (non-zero) status",
398 _PATH_SENDMAIL, WEXITSTATUS(status));
407 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
408 "usage: rmail [-T] [-D domain] user ...\n");
414 err(int eval, const char *fmt, ...)
416 err(eval, fmt, va_alist)
420 #endif /* __STDC__ */
426 SM_VA_START(ap, fmt);
427 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "rmail: ");
428 (void) sm_io_vfprintf(smioerr, SM_TIME_DEFAULT, fmt, ap);
430 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "\n");