1N/A/*
1N/A * Copyright (c) 1998 Sendmail, Inc. All rights reserved.
1N/A * Copyright (c) 1990, 1993, 1994
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level
1N/A * of the sendmail distribution.
1N/A */
1N/A
1N/A/*
1N/A * Copyright 1994-2007 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A#ifndef lint
1N/Astatic char copyright[] =
1N/A"@(#) Copyright (c) 1990, 1993, 1994\n\
1N/A The Regents of the University of California. All rights reserved.\n";
1N/A#endif /* not lint */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#ifndef lint
1N/Astatic char sccsid[] = "@(#)mail.local.c 8.83 (Berkeley) 12/17/98";
1N/Astatic char sccsi2[] = "%W% (Sun) %G%";
1N/A#endif /* not lint */
1N/A
1N/A#include <sys/param.h>
1N/A#include <sys/stat.h>
1N/A#include <sys/socket.h>
1N/A#include <sys/file.h>
1N/A
1N/A#include <netinet/in.h>
1N/A
1N/A#include <errno.h>
1N/A#include <fcntl.h>
1N/A#include <netdb.h>
1N/A#include <pwd.h>
1N/A#include <stdio.h>
1N/A#include <stdlib.h>
1N/A#include <signal.h>
1N/A#include <ctype.h>
1N/A#include <string.h>
1N/A#include <sysexits.h>
1N/A#include <time.h>
1N/A#include <unistd.h>
1N/A#include <maillock.h>
1N/A#include <grp.h>
1N/A
1N/A#ifdef __STDC__
1N/A#include <stdarg.h>
1N/A#else
1N/A#include <varargs.h>
1N/A#endif
1N/A
1N/A#include <syslog.h>
1N/A
1N/A#include <sysexits.h>
1N/A#include <ctype.h>
1N/A
1N/A#include <sm/conf.h>
1N/A#include <sendmail/pathnames.h>
1N/A
1N/A/*
1N/A** If you don't have flock, you could try using lockf instead.
1N/A*/
1N/A
1N/A#ifdef LDA_USE_LOCKF
1N/A# define flock(a, b) lockf(a, b, 0)
1N/A# ifdef LOCK_EX
1N/A# undef LOCK_EX
1N/A# endif /* LOCK_EX */
1N/A# define LOCK_EX F_LOCK
1N/A#endif /* LDA_USE_LOCKF */
1N/A
1N/A#ifndef LOCK_EX
1N/A# include <sys/file.h>
1N/A#endif /* ! LOCK_EX */
1N/A
1N/A#ifndef MAILER_DAEMON
1N/A# define MAILER_DAEMON "MAILER-DAEMON"
1N/A#endif
1N/A
1N/Atypedef int bool;
1N/A
1N/A#define FALSE 0
1N/A#define TRUE 1
1N/A
1N/Abool EightBitMime = TRUE; /* advertise 8BITMIME in LMTP */
1N/Astatic int eval = EX_OK; /* sysexits.h error value. */
1N/Astatic int lmtpmode = 0;
1N/Abool bouncequota = FALSE; /* permanent error when over quota */
1N/A
1N/A#define _PATH_MAILDIR "/var/mail"
1N/A#define _PATH_LOCTMP "/tmp/local.XXXXXX"
1N/A#define _PATH_LOCHTMP "/tmp/lochd.XXXXXX"
1N/A#define FALSE 0
1N/A#define TRUE 1
1N/A#define MAXLINE 2048
1N/A
1N/Astatic void deliver(int, int, char *, bool);
1N/Astatic void e_to_sys(int);
1N/Astatic void err(const char *fmt, ...);
1N/Astatic void notifybiff(char *);
1N/Astatic void store(char *, int);
1N/Astatic void usage(void);
1N/Astatic void vwarn();
1N/Astatic void warn(const char *fmt, ...);
1N/Astatic void mailerr(const char *, const char *, ...);
1N/Astatic void sigterm_handler();
1N/A
1N/Astatic char unix_from_line[MAXLINE];
1N/Astatic int ulen;
1N/Astatic int content_length;
1N/Astatic int bfd, hfd; /* temp file */
1N/Astatic uid_t src_uid, targ_uid, saved_uid;
1N/Astatic int sigterm_caught;
1N/A
1N/Aint
1N/Amain(argc, argv)
1N/A int argc;
1N/A char *argv[];
1N/A{
1N/A struct passwd *pw;
1N/A int ch;
1N/A uid_t uid;
1N/A char *from;
1N/A struct group *grpptr;
1N/A void dolmtp();
1N/A
1N/A openlog("mail.local", 0, LOG_MAIL);
1N/A
1N/A from = NULL;
1N/A pw = NULL;
1N/A sigterm_caught = FALSE;
1N/A
1N/A (void) sigset(SIGTERM, sigterm_handler);
1N/A
1N/A while ((ch = getopt(argc, argv, "7bdf:r:l")) != EOF)
1N/A switch (ch) {
1N/A case '7': /* Do not advertise 8BITMIME */
1N/A EightBitMime = FALSE;
1N/A break;
1N/A
1N/A case 'b': /* bounce mail when over quota. */
1N/A bouncequota = TRUE;
1N/A break;
1N/A
1N/A case 'd': /* Backward compatible. */
1N/A break;
1N/A case 'f':
1N/A case 'r': /* Backward compatible. */
1N/A if (from != NULL) {
1N/A warn("multiple -f options");
1N/A usage();
1N/A }
1N/A from = optarg;
1N/A break;
1N/A case 'l':
1N/A lmtpmode++;
1N/A break;
1N/A case '?':
1N/A default:
1N/A usage();
1N/A }
1N/A argc -= optind;
1N/A argv += optind;
1N/A
1N/A notifybiff(NULL); /* initialize biff structures */
1N/A
1N/A /*
1N/A * We expect sendmail will invoke us with saved id 0
1N/A * We then do setgid and setuid defore delivery
1N/A * setgid to mail group
1N/A */
1N/A if ((grpptr = getgrnam("mail")) != NULL)
1N/A (void) setgid(grpptr->gr_gid);
1N/A saved_uid = geteuid();
1N/A
1N/A if (lmtpmode) {
1N/A if (saved_uid != 0) {
1N/A warn("only super-user can use -l option");
1N/A exit(EX_CANTCREAT);
1N/A }
1N/A dolmtp(bouncequota);
1N/A }
1N/A
1N/A if (!*argv)
1N/A usage();
1N/A
1N/A /*
1N/A * If from not specified, use the name from getlogin() if the
1N/A * uid matches, otherwise, use the name from the password file
1N/A * corresponding to the uid.
1N/A */
1N/A uid = getuid();
1N/A if (!from && (!(from = getlogin()) ||
1N/A !(pw = getpwnam(from)) || pw->pw_uid != uid))
1N/A from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
1N/A src_uid = pw ? pw->pw_uid : uid;
1N/A
1N/A /*
1N/A * There is no way to distinguish the error status of one delivery
1N/A * from the rest of the deliveries. So, if we failed hard on one
1N/A * or more deliveries, but had no failures on any of the others, we
1N/A * return a hard failure. If we failed temporarily on one or more
1N/A * deliveries, we return a temporary failure regardless of the other
1N/A * failures. This results in the delivery being reattempted later
1N/A * at the expense of repeated failures and multiple deliveries.
1N/A */
1N/A
1N/A for (store(from, 0); *argv; ++argv)
1N/A deliver(hfd, bfd, *argv, bouncequota);
1N/A return (eval);
1N/A}
1N/A
1N/Avoid
1N/Asigterm_handler()
1N/A{
1N/A sigterm_caught = TRUE;
1N/A (void) sigignore(SIGTERM);
1N/A}
1N/A
1N/Achar *
1N/Aparseaddr(s)
1N/A char *s;
1N/A{
1N/A char *p;
1N/A int len;
1N/A
1N/A if (*s++ != '<')
1N/A return NULL;
1N/A
1N/A p = s;
1N/A
1N/A /* at-domain-list */
1N/A while (*p == '@') {
1N/A p++;
1N/A if (*p == '[') {
1N/A p++;
1N/A while (isascii(*p) &&
1N/A (isalnum(*p) || *p == '.' ||
1N/A *p == '-' || *p == ':'))
1N/A p++;
1N/A if (*p++ != ']')
1N/A return NULL;
1N/A } else {
1N/A while ((isascii(*p) && isalnum(*p)) ||
1N/A strchr(".-_", *p))
1N/A p++;
1N/A }
1N/A if (*p == ',' && p[1] == '@')
1N/A p++;
1N/A else if (*p == ':' && p[1] != '@')
1N/A p++;
1N/A else
1N/A return NULL;
1N/A }
1N/A
1N/A s = p;
1N/A
1N/A /* local-part */
1N/A if (*p == '\"') {
1N/A p++;
1N/A while (*p && *p != '\"') {
1N/A if (*p == '\\') {
1N/A if (!*++p)
1N/A return NULL;
1N/A }
1N/A p++;
1N/A }
1N/A if (!*p++)
1N/A return NULL;
1N/A } else {
1N/A while (*p && *p != '@' && *p != '>') {
1N/A if (*p == '\\') {
1N/A if (!*++p)
1N/A return NULL;
1N/A } else {
1N/A if (*p <= ' ' || (*p & 128) ||
1N/A strchr("<>()[]\\,;:\"", *p))
1N/A return NULL;
1N/A }
1N/A p++;
1N/A }
1N/A }
1N/A
1N/A /* @domain */
1N/A if (*p == '@') {
1N/A p++;
1N/A if (*p == '[') {
1N/A p++;
1N/A while (isascii(*p) &&
1N/A (isalnum(*p) || *p == '.' ||
1N/A *p == '-' || *p == ':'))
1N/A p++;
1N/A if (*p++ != ']')
1N/A return NULL;
1N/A } else {
1N/A while ((isascii(*p) && isalnum(*p)) ||
1N/A strchr(".-_", *p))
1N/A p++;
1N/A }
1N/A }
1N/A
1N/A if (*p++ != '>')
1N/A return NULL;
1N/A if (*p && *p != ' ')
1N/A return NULL;
1N/A len = p - s - 1;
1N/A
1N/A if (*s == '\0' || len <= 0)
1N/A {
1N/A s = MAILER_DAEMON;
1N/A len = strlen(s);
1N/A }
1N/A
1N/A p = malloc(len + 1);
1N/A if (p == NULL) {
1N/A printf("421 4.3.0 memory exhausted\r\n");
1N/A exit(EX_TEMPFAIL);
1N/A }
1N/A
1N/A strncpy(p, s, len);
1N/A p[len] = '\0';
1N/A return p;
1N/A}
1N/A
1N/Achar *
1N/Aprocess_recipient(addr)
1N/A char *addr;
1N/A{
1N/A if (getpwnam(addr) == NULL) {
1N/A return "550 5.1.1 user unknown";
1N/A }
1N/A
1N/A return NULL;
1N/A}
1N/A
1N/A#define RCPT_GROW 30
1N/A
1N/Avoid
1N/Adolmtp(bouncequota)
1N/A bool bouncequota;
1N/A{
1N/A char *return_path = NULL;
1N/A char **rcpt_addr = NULL;
1N/A int rcpt_num = 0;
1N/A int rcpt_alloc = 0;
1N/A bool gotlhlo = FALSE;
1N/A char myhostname[MAXHOSTNAMELEN];
1N/A char buf[4096];
1N/A char *err;
1N/A char *p;
1N/A int i;
1N/A
1N/A gethostname(myhostname, sizeof myhostname - 1);
1N/A
1N/A printf("220 %s LMTP ready\r\n", myhostname);
1N/A for (;;) {
1N/A if (sigterm_caught) {
1N/A for (; rcpt_num > 0; rcpt_num--)
1N/A printf("451 4.3.0 shutting down\r\n");
1N/A exit(EX_OK);
1N/A }
1N/A fflush(stdout);
1N/A if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1N/A exit(EX_OK);
1N/A }
1N/A p = buf + strlen(buf) - 1;
1N/A if (p >= buf && *p == '\n')
1N/A *p-- = '\0';
1N/A if (p >= buf && *p == '\r')
1N/A *p-- = '\0';
1N/A
1N/A switch (buf[0]) {
1N/A
1N/A case 'd':
1N/A case 'D':
1N/A if (strcasecmp(buf, "data") == 0) {
1N/A if (rcpt_num == 0) {
1N/A printf("503 5.5.1 No recipients\r\n");
1N/A continue;
1N/A }
1N/A store(return_path, rcpt_num);
1N/A if (bfd == -1 || hfd == -1)
1N/A continue;
1N/A
1N/A for (i = 0; i < rcpt_num; i++) {
1N/A p = strchr(rcpt_addr[i], '+');
1N/A if (p != NULL)
1N/A *p++ = '\0';
1N/A deliver(hfd, bfd, rcpt_addr[i],
1N/A bouncequota);
1N/A }
1N/A close(bfd);
1N/A close(hfd);
1N/A goto rset;
1N/A }
1N/A goto syntaxerr;
1N/A /* NOTREACHED */
1N/A break;
1N/A
1N/A case 'l':
1N/A case 'L':
1N/A if (strncasecmp(buf, "lhlo ", 5) == 0)
1N/A {
1N/A /* check for duplicate per RFC 1651 4.2 */
1N/A if (gotlhlo)
1N/A {
1N/A printf("503 %s Duplicate LHLO\r\n",
1N/A myhostname);
1N/A continue;
1N/A }
1N/A gotlhlo = TRUE;
1N/A printf("250-%s\r\n", myhostname);
1N/A if (EightBitMime)
1N/A printf("250-8BITMIME\r\n");
1N/A printf("250-ENHANCEDSTATUSCODES\r\n");
1N/A printf("250 PIPELINING\r\n");
1N/A continue;
1N/A }
1N/A goto syntaxerr;
1N/A /* NOTREACHED */
1N/A break;
1N/A
1N/A case 'm':
1N/A case 'M':
1N/A if (strncasecmp(buf, "mail ", 5) == 0) {
1N/A if (return_path != NULL) {
1N/A printf("503 5.5.1 Nested MAIL command\r\n");
1N/A continue;
1N/A }
1N/A if (strncasecmp(buf+5, "from:", 5) != 0 ||
1N/A ((return_path = parseaddr(buf+10)) == NULL)) {
1N/A printf("501 5.5.4 Syntax error in parameters\r\n");
1N/A continue;
1N/A }
1N/A printf("250 2.5.0 ok\r\n");
1N/A continue;
1N/A }
1N/A goto syntaxerr;
1N/A
1N/A case 'n':
1N/A case 'N':
1N/A if (strcasecmp(buf, "noop") == 0) {
1N/A printf("250 2.0.0 ok\r\n");
1N/A continue;
1N/A }
1N/A goto syntaxerr;
1N/A
1N/A case 'q':
1N/A case 'Q':
1N/A if (strcasecmp(buf, "quit") == 0) {
1N/A printf("221 2.0.0 bye\r\n");
1N/A exit(EX_OK);
1N/A }
1N/A goto syntaxerr;
1N/A
1N/A case 'r':
1N/A case 'R':
1N/A if (strncasecmp(buf, "rcpt ", 5) == 0) {
1N/A if (return_path == NULL) {
1N/A printf("503 5.5.1 Need MAIL command\r\n");
1N/A continue;
1N/A }
1N/A if (rcpt_num >= rcpt_alloc) {
1N/A rcpt_alloc += RCPT_GROW;
1N/A rcpt_addr = (char **)
1N/A realloc((char *)rcpt_addr,
1N/A rcpt_alloc * sizeof(char **));
1N/A if (rcpt_addr == NULL) {
1N/A printf("421 4.3.0 memory exhausted\r\n");
1N/A exit(EX_TEMPFAIL);
1N/A }
1N/A }
1N/A if (strncasecmp(buf+5, "to:", 3) != 0 ||
1N/A ((rcpt_addr[rcpt_num] = parseaddr(buf+8)) == NULL)) {
1N/A printf("501 5.5.4 Syntax error in parameters\r\n");
1N/A continue;
1N/A }
1N/A if ((err = process_recipient(rcpt_addr[rcpt_num])) != NULL) {
1N/A printf("%s\r\n", err);
1N/A continue;
1N/A }
1N/A rcpt_num++;
1N/A printf("250 2.1.5 ok\r\n");
1N/A continue;
1N/A }
1N/A else if (strcasecmp(buf, "rset") == 0) {
1N/A printf("250 2.0.0 ok\r\n");
1N/A
1N/A rset:
1N/A while (rcpt_num > 0) {
1N/A free(rcpt_addr[--rcpt_num]);
1N/A }
1N/A if (return_path != NULL)
1N/A free(return_path);
1N/A return_path = NULL;
1N/A continue;
1N/A }
1N/A goto syntaxerr;
1N/A
1N/A case 'v':
1N/A case 'V':
1N/A if (strncasecmp(buf, "vrfy ", 5) == 0) {
1N/A printf("252 2.3.3 try RCPT to attempt delivery\r\n");
1N/A continue;
1N/A }
1N/A goto syntaxerr;
1N/A
1N/A default:
1N/A syntaxerr:
1N/A printf("500 5.5.2 Syntax error\r\n");
1N/A continue;
1N/A }
1N/A }
1N/A}
1N/A
1N/Astatic void
1N/Astore(from, lmtprcpts)
1N/A char *from;
1N/A int lmtprcpts;
1N/A{
1N/A FILE *fp = NULL;
1N/A time_t tval;
1N/A bool fullline = TRUE; /* current line is terminated */
1N/A bool prevfl; /* previous line was terminated */
1N/A char line[MAXLINE];
1N/A FILE *bfp, *hfp;
1N/A char *btn, *htn;
1N/A int in_header_section;
1N/A int newfd;
1N/A
1N/A bfd = -1;
1N/A hfd = -1;
1N/A btn = strdup(_PATH_LOCTMP);
1N/A if ((bfd = mkstemp(btn)) == -1 || (bfp = fdopen(bfd, "w+")) == NULL) {
1N/A if (bfd != -1)
1N/A (void) close(bfd);
1N/A if (lmtprcpts) {
1N/A printf("451 4.3.0 unable to open temporary file\r\n");
1N/A return;
1N/A } else {
1N/A mailerr("451 4.3.0", "unable to open temporary file");
1N/A exit(eval);
1N/A }
1N/A }
1N/A (void) unlink(btn);
1N/A free(btn);
1N/A
1N/A if (lmtpmode) {
1N/A printf("354 go ahead\r\n");
1N/A fflush(stdout);
1N/A }
1N/A
1N/A htn = strdup(_PATH_LOCHTMP);
1N/A if ((hfd = mkstemp(htn)) == -1 || (hfp = fdopen(hfd, "w+")) == NULL) {
1N/A if (hfd != -1)
1N/A (void) close(hfd);
1N/A e_to_sys(errno);
1N/A err("unable to open temporary file");
1N/A }
1N/A (void) unlink(htn);
1N/A free(htn);
1N/A
1N/A in_header_section = TRUE;
1N/A content_length = 0;
1N/A fp = hfp;
1N/A
1N/A line[0] = '\0';
1N/A while (fgets(line, sizeof(line), stdin) != (char *)NULL)
1N/A {
1N/A size_t line_len = 0;
1N/A int peek;
1N/A
1N/A prevfl = fullline; /* preserve state of previous line */
1N/A while (line[line_len] != '\n' && line_len < sizeof(line) - 2)
1N/A line_len++;
1N/A line_len++;
1N/A
1N/A /* Check for dot-stuffing */
1N/A if (prevfl && lmtprcpts && line[0] == '.')
1N/A {
1N/A if (line[1] == '\n' ||
1N/A (line[1] == '\r' && line[2] == '\n'))
1N/A goto lmtpdot;
1N/A memcpy(line, line + 1, line_len);
1N/A line_len--;
1N/A }
1N/A
1N/A /* Check to see if we have the full line from fgets() */
1N/A fullline = FALSE;
1N/A if (line_len > 0)
1N/A {
1N/A if (line[line_len - 1] == '\n')
1N/A {
1N/A if (line_len >= 2 &&
1N/A line[line_len - 2] == '\r')
1N/A {
1N/A line[line_len - 2] = '\n';
1N/A line[line_len - 1] = '\0';
1N/A line_len--;
1N/A }
1N/A fullline = TRUE;
1N/A }
1N/A else if (line[line_len - 1] == '\r')
1N/A {
1N/A /* Did we just miss the CRLF? */
1N/A peek = fgetc(stdin);
1N/A if (peek == '\n')
1N/A {
1N/A line[line_len - 1] = '\n';
1N/A fullline = TRUE;
1N/A }
1N/A else
1N/A (void) ungetc(peek, stdin);
1N/A }
1N/A }
1N/A else
1N/A fullline = TRUE;
1N/A
1N/A if (prevfl && line[0] == '\n' && in_header_section) {
1N/A in_header_section = FALSE;
1N/A if (fflush(fp) == EOF || ferror(fp)) {
1N/A if (lmtprcpts) {
1N/A while (lmtprcpts--)
1N/A printf("451 4.3.0 temporary file write error\r\n");
1N/A fclose(fp);
1N/A return;
1N/A } else {
1N/A mailerr("451 4.3.0",
1N/A "temporary file write error");
1N/A fclose(fp);
1N/A exit(eval);
1N/A }
1N/A }
1N/A fp = bfp;
1N/A continue;
1N/A }
1N/A
1N/A if (in_header_section) {
1N/A if (strncasecmp("Content-Length:", line, 15) == 0) {
1N/A continue; /* skip this header */
1N/A }
1N/A } else
1N/A content_length += strlen(line);
1N/A (void) fwrite(line, sizeof(char), line_len, fp);
1N/A if (ferror(fp)) {
1N/A if (lmtprcpts) {
1N/A while (lmtprcpts--)
1N/A printf("451 4.3.0 temporary file write error\r\n");
1N/A fclose(fp);
1N/A return;
1N/A } else {
1N/A mailerr("451 4.3.0",
1N/A "temporary file write error");
1N/A fclose(fp);
1N/A exit(eval);
1N/A }
1N/A }
1N/A }
1N/A if (sigterm_caught) {
1N/A if (lmtprcpts)
1N/A while (lmtprcpts--)
1N/A printf("451 4.3.0 shutting down\r\n");
1N/A else
1N/A mailerr("451 4.3.0", "shutting down");
1N/A fclose(fp);
1N/A exit(eval);
1N/A }
1N/A
1N/A if (lmtprcpts) {
1N/A /* Got a premature EOF -- toss message and exit */
1N/A exit(EX_OK);
1N/A }
1N/A
1N/A /* If message not newline terminated, need an extra. */
1N/A if (!strchr(line, '\n')) {
1N/A (void) putc('\n', fp);
1N/A content_length++;
1N/A }
1N/A
1N/A lmtpdot:
1N/A
1N/A /* Output a newline; note, empty messages are allowed. */
1N/A (void) putc('\n', fp);
1N/A
1N/A if (fflush(fp) == EOF || ferror(fp)) {
1N/A if (lmtprcpts) {
1N/A while (lmtprcpts--) {
1N/A printf("451 4.3.0 temporary file write error\r\n");
1N/A }
1N/A fclose(fp);
1N/A return;
1N/A } else {
1N/A mailerr("451 4.3.0", "temporary file write error");
1N/A fclose(fp);
1N/A exit(eval);
1N/A }
1N/A }
1N/A
1N/A if ((newfd = dup(bfd)) >= 0) {
1N/A fclose(bfp);
1N/A bfd = newfd;
1N/A }
1N/A if ((newfd = dup(hfd)) >= 0) {
1N/A fclose(hfp);
1N/A hfd = newfd;
1N/A }
1N/A (void) time(&tval);
1N/A (void) snprintf(unix_from_line, sizeof (unix_from_line), "From %s %s",
1N/A from, ctime(&tval));
1N/A ulen = strlen(unix_from_line);
1N/A}
1N/A
1N/Astatic void
1N/Ahandle_error(err_num, bouncequota, path)
1N/A int err_num;
1N/A bool bouncequota;
1N/A char *path;
1N/A{
1N/A#ifdef EDQUOT
1N/A if (err_num == EDQUOT && bouncequota) {
1N/A mailerr("552 5.2.2", "%s: %s", path, sm_errstring(err_num));
1N/A } else
1N/A#endif /* EDQUOT */
1N/A mailerr("450 4.2.0", "%s: %s", path, sm_errstring(err_num));
1N/A}
1N/A
1N/Astatic void
1N/Adeliver(hfd, bfd, name, bouncequota)
1N/A int hfd;
1N/A int bfd;
1N/A char *name;
1N/A bool bouncequota;
1N/A{
1N/A struct stat fsb, sb;
1N/A int mbfd = -1, nr, nw = 0, off;
1N/A char biffmsg[100], buf[8*1024], path[MAXPATHLEN];
1N/A off_t curoff, cursize;
1N/A int len;
1N/A struct passwd *pw = NULL;
1N/A
1N/A /*
1N/A * Disallow delivery to unknown names -- special mailboxes
1N/A * can be handled in the sendmail aliases file.
1N/A */
1N/A if ((pw = getpwnam(name)) == NULL) {
1N/A eval = EX_TEMPFAIL;
1N/A mailerr("451 4.3.0", "cannot lookup name: %s", name);
1N/A return;
1N/A }
1N/A endpwent();
1N/A
1N/A if (sigterm_caught) {
1N/A mailerr("451 4.3.0", "shutting down");
1N/A return;
1N/A }
1N/A
1N/A /* mailbox may be NFS mounted, seteuid to user */
1N/A targ_uid = pw->pw_uid;
1N/A (void) seteuid(targ_uid);
1N/A
1N/A if ((saved_uid != 0) && (src_uid != targ_uid)) {
1N/A /*
1N/A * If saved_uid == 0 (root), anything is OK; this is
1N/A * as it should be. But to prevent a random user from
1N/A * calling "mail.local foo" in an attempt to hijack
1N/A * foo's mail-box, make sure src_uid == targ_uid o/w.
1N/A */
1N/A warn("%s: wrong owner (is %d, should be %d)",
1N/A name, src_uid, targ_uid);
1N/A eval = EX_CANTCREAT;
1N/A return;
1N/A }
1N/A
1N/A path[0] = '\0';
1N/A (void) snprintf(path, sizeof (path), "%s/%s", _PATH_MAILDIR, name);
1N/A
1N/A /*
1N/A * If the mailbox is linked or a symlink, fail. There's an obvious
1N/A * race here, that the file was replaced with a symbolic link after
1N/A * the lstat returned, but before the open. We attempt to detect
1N/A * this by comparing the original stat information and information
1N/A * returned by an fstat of the file descriptor returned by the open.
1N/A *
1N/A * NB: this is a symptom of a larger problem, that the mail spooling
1N/A * directory is writeable by the wrong users. If that directory is
1N/A * writeable, system security is compromised for other reasons, and
1N/A * it cannot be fixed here.
1N/A *
1N/A * If we created the mailbox, set the owner/group. If that fails,
1N/A * just return. Another process may have already opened it, so we
1N/A * can't unlink it. Historically, binmail set the owner/group at
1N/A * each mail delivery. We no longer do this, assuming that if the
1N/A * ownership or permissions were changed there was a reason.
1N/A *
1N/A * XXX
1N/A * open(2) should support flock'ing the file.
1N/A */
1N/Atryagain:
1N/A /* should check lock status, but... maillock return no value */
1N/A maillock(name, 10);
1N/A
1N/A if (sigterm_caught) {
1N/A mailerr("451 4.3.0", "shutting down");
1N/A goto err0;
1N/A }
1N/A
1N/A if (lstat(path, &sb)) {
1N/A mbfd = open(path, O_APPEND|O_CREAT|O_EXCL|O_WRONLY,
1N/A S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
1N/A if (mbfd != -1)
1N/A (void) fchmod(mbfd, 0660);
1N/A
1N/A
1N/A if (mbfd == -1) {
1N/A if (errno == EEXIST) {
1N/A mailunlock();
1N/A goto tryagain;
1N/A }
1N/A }
1N/A } else if (sb.st_nlink != 1) {
1N/A mailerr("550 5.2.0", "%s: too many links", path);
1N/A goto err0;
1N/A } else if (!S_ISREG(sb.st_mode)) {
1N/A mailerr("550 5.2.0", "%s: irregular file", path);
1N/A goto err0;
1N/A } else {
1N/A mbfd = open(path, O_APPEND|O_WRONLY, 0);
1N/A if (mbfd != -1 &&
1N/A (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
1N/A S_ISLNK(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
1N/A sb.st_ino != fsb.st_ino)) {
1N/A eval = EX_TEMPFAIL;
1N/A mailerr("550 5.2.0",
1N/A "%s: fstat: file changed after open", path);
1N/A goto err1;
1N/A }
1N/A }
1N/A
1N/A if (mbfd == -1) {
1N/A mailerr("450 4.2.0", "%s: %s", path, strerror(errno));
1N/A goto err0;
1N/A }
1N/A
1N/A if (sigterm_caught) {
1N/A mailerr("451 4.3.0", "shutting down");
1N/A goto err0;
1N/A }
1N/A
1N/A /* Get the starting offset of the new message for biff. */
1N/A curoff = lseek(mbfd, (off_t)0, SEEK_END);
1N/A (void) snprintf(biffmsg, sizeof (biffmsg), "%s@%ld\n", name, curoff);
1N/A
1N/A /* Copy the message into the file. */
1N/A if (lseek(hfd, (off_t)0, SEEK_SET) == (off_t)-1) {
1N/A mailerr("450 4.2.0", "temporary file: %s", strerror(errno));
1N/A goto err1;
1N/A }
1N/A /* Copy the message into the file. */
1N/A if (lseek(bfd, (off_t)0, SEEK_SET) == (off_t)-1) {
1N/A mailerr("450 4.2.0", "temporary file: %s", strerror(errno));
1N/A goto err1;
1N/A }
1N/A if ((write(mbfd, unix_from_line, ulen)) != ulen) {
1N/A handle_error(errno, bouncequota, path);
1N/A goto err2;
1N/A }
1N/A
1N/A if (sigterm_caught) {
1N/A mailerr("451 4.3.0", "shutting down");
1N/A goto err2;
1N/A }
1N/A
1N/A while ((nr = read(hfd, buf, sizeof (buf))) > 0)
1N/A for (off = 0; off < nr; nr -= nw, off += nw)
1N/A if ((nw = write(mbfd, buf + off, nr)) < 0)
1N/A {
1N/A handle_error(errno, bouncequota, path);
1N/A goto err2;
1N/A }
1N/A if (nr < 0) {
1N/A handle_error(errno, bouncequota, path);
1N/A goto err2;
1N/A }
1N/A
1N/A if (sigterm_caught) {
1N/A mailerr("451 4.3.0", "shutting down");
1N/A goto err2;
1N/A }
1N/A
1N/A (void) snprintf(buf, sizeof (buf), "Content-Length: %d\n\n",
1N/A content_length);
1N/A len = strlen(buf);
1N/A if (write(mbfd, buf, len) != len) {
1N/A handle_error(errno, bouncequota, path);
1N/A goto err2;
1N/A }
1N/A
1N/A if (sigterm_caught) {
1N/A mailerr("451 4.3.0", "shutting down");
1N/A goto err2;
1N/A }
1N/A
1N/A while ((nr = read(bfd, buf, sizeof (buf))) > 0) {
1N/A for (off = 0; off < nr; nr -= nw, off += nw)
1N/A if ((nw = write(mbfd, buf + off, nr)) < 0) {
1N/A handle_error(errno, bouncequota, path);
1N/A goto err2;
1N/A }
1N/A if (sigterm_caught) {
1N/A mailerr("451 4.3.0", "shutting down");
1N/A goto err2;
1N/A }
1N/A }
1N/A if (nr < 0) {
1N/A handle_error(errno, bouncequota, path);
1N/A goto err2;
1N/A }
1N/A
1N/A /* Flush to disk, don't wait for update. */
1N/A if (fsync(mbfd)) {
1N/A handle_error(errno, bouncequota, path);
1N/Aerr2: if (mbfd >= 0)
1N/A (void)ftruncate(mbfd, curoff);
1N/Aerr1: (void)close(mbfd);
1N/Aerr0: mailunlock();
1N/A (void)seteuid(saved_uid);
1N/A return;
1N/A }
1N/A
1N/A /*
1N/A ** Save the current size so if the close() fails below
1N/A ** we can make sure no other process has changed the mailbox
1N/A ** between the failed close and the re-open()/re-lock().
1N/A ** If something else has changed the size, we shouldn't
1N/A ** try to truncate it as we may do more harm then good
1N/A ** (e.g., truncate a later message delivery).
1N/A */
1N/A
1N/A if (fstat(mbfd, &sb) < 0)
1N/A cursize = 0;
1N/A else
1N/A cursize = sb.st_size;
1N/A
1N/A /* Close and check -- NFS doesn't write until the close. */
1N/A if (close(mbfd))
1N/A {
1N/A handle_error(errno, bouncequota, path);
1N/A mbfd = open(path, O_WRONLY, 0);
1N/A if (mbfd < 0 ||
1N/A cursize == 0
1N/A || flock(mbfd, LOCK_EX) < 0 ||
1N/A fstat(mbfd, &sb) < 0 ||
1N/A sb.st_size != cursize ||
1N/A sb.st_nlink != 1 ||
1N/A !S_ISREG(sb.st_mode) ||
1N/A sb.st_dev != fsb.st_dev ||
1N/A sb.st_ino != fsb.st_ino ||
1N/A sb.st_uid != fsb.st_uid)
1N/A {
1N/A /* Don't use a bogus file */
1N/A if (mbfd >= 0)
1N/A {
1N/A (void) close(mbfd);
1N/A mbfd = -1;
1N/A }
1N/A }
1N/A
1N/A /* Attempt to truncate back to pre-write size */
1N/A goto err2;
1N/A } else
1N/A notifybiff(biffmsg);
1N/A
1N/A mailunlock();
1N/A
1N/A (void)seteuid(saved_uid);
1N/A
1N/A if (lmtpmode) {
1N/A printf("250 2.1.5 %s OK\r\n", name);
1N/A }
1N/A}
1N/A
1N/Astatic void
1N/Anotifybiff(msg)
1N/A char *msg;
1N/A{
1N/A static struct sockaddr_in addr;
1N/A static int f = -1;
1N/A struct hostent *hp;
1N/A struct servent *sp;
1N/A int len;
1N/A
1N/A if (msg == NULL) {
1N/A /* Be silent if biff service not available. */
1N/A if ((sp = getservbyname("biff", "udp")) == NULL)
1N/A return;
1N/A if ((hp = gethostbyname("localhost")) == NULL) {
1N/A warn("localhost: %s", strerror(errno));
1N/A return;
1N/A }
1N/A addr.sin_family = hp->h_addrtype;
1N/A (void) memmove(&addr.sin_addr, hp->h_addr, hp->h_length);
1N/A addr.sin_port = sp->s_port;
1N/A return;
1N/A }
1N/A
1N/A if (addr.sin_family == 0)
1N/A return; /* did not initialize */
1N/A
1N/A if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
1N/A warn("socket: %s", strerror(errno));
1N/A return;
1N/A }
1N/A len = strlen(msg) + 1;
1N/A if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof (addr))
1N/A != len)
1N/A warn("sendto biff: %s", strerror(errno));
1N/A}
1N/A
1N/Astatic void
1N/Ausage()
1N/A{
1N/A eval = EX_USAGE;
1N/A err("usage: mail.local [-l] [-f from] user ...");
1N/A}
1N/A
1N/Astatic void
1N/A/*VARARGS2*/
1N/A#ifdef __STDC__
1N/Amailerr(const char *hdr, const char *fmt, ...)
1N/A#else
1N/Amailerr(hdr, fmt, va_alist)
1N/A const char *hdr;
1N/A const char *fmt;
1N/A va_dcl
1N/A#endif
1N/A{
1N/A va_list ap;
1N/A
1N/A#ifdef __STDC__
1N/A va_start(ap, fmt);
1N/A#else
1N/A va_start(ap);
1N/A#endif
1N/A if (lmtpmode)
1N/A {
1N/A if (hdr != NULL)
1N/A printf("%s ", hdr);
1N/A vprintf(fmt, ap);
1N/A printf("\r\n");
1N/A }
1N/A else
1N/A {
1N/A e_to_sys(errno);
1N/A vwarn(fmt, ap);
1N/A }
1N/A}
1N/A
1N/Astatic void
1N/A/*VARARGS1*/
1N/A#ifdef __STDC__
1N/Aerr(const char *fmt, ...)
1N/A#else
1N/Aerr(fmt, va_alist)
1N/A const char *fmt;
1N/A va_dcl
1N/A#endif
1N/A{
1N/A va_list ap;
1N/A
1N/A#ifdef __STDC__
1N/A va_start(ap, fmt);
1N/A#else
1N/A va_start(ap);
1N/A#endif
1N/A vwarn(fmt, ap);
1N/A va_end(ap);
1N/A
1N/A exit(eval);
1N/A}
1N/A
1N/Astatic void
1N/A/*VARARGS1*/
1N/A#ifdef __STDC__
1N/Awarn(const char *fmt, ...)
1N/A#else
1N/Awarn(fmt, va_alist)
1N/A const char *fmt;
1N/A va_dcl
1N/A#endif
1N/A{
1N/A va_list ap;
1N/A
1N/A#ifdef __STDC__
1N/A va_start(ap, fmt);
1N/A#else
1N/A va_start(ap);
1N/A#endif
1N/A vwarn(fmt, ap);
1N/A va_end(ap);
1N/A}
1N/A
1N/Astatic void
1N/Avwarn(fmt, ap)
1N/A const char *fmt;
1N/A va_list ap;
1N/A{
1N/A /*
1N/A * Log the message to stderr.
1N/A *
1N/A * Don't use LOG_PERROR as an openlog() flag to do this,
1N/A * it's not portable enough.
1N/A */
1N/A if (eval != EX_USAGE)
1N/A (void) fprintf(stderr, "mail.local: ");
1N/A (void) vfprintf(stderr, fmt, ap);
1N/A (void) fprintf(stderr, "\n");
1N/A
1N/A /* Log the message to syslog. */
1N/A vsyslog(LOG_ERR, fmt, ap);
1N/A}
1N/A
1N/A/*
1N/A * e_to_sys --
1N/A * Guess which errno's are temporary. Gag me.
1N/A */
1N/Astatic void
1N/Ae_to_sys(num)
1N/A int num;
1N/A{
1N/A /* Temporary failures override hard errors. */
1N/A if (eval == EX_TEMPFAIL)
1N/A return;
1N/A
1N/A switch (num) /* Hopefully temporary errors. */
1N/A {
1N/A#ifdef EDQUOT
1N/A case EDQUOT: /* Disc quota exceeded */
1N/A if (bouncequota)
1N/A {
1N/A eval = EX_UNAVAILABLE;
1N/A break;
1N/A }
1N/A /* FALLTHROUGH */
1N/A#endif /* EDQUOT */
1N/A#ifdef EAGAIN
1N/A case EAGAIN: /* Resource temporarily unavailable */
1N/A#endif
1N/A#ifdef EBUSY
1N/A case EBUSY: /* Device busy */
1N/A#endif
1N/A#ifdef EPROCLIM
1N/A case EPROCLIM: /* Too many processes */
1N/A#endif
1N/A#ifdef EUSERS
1N/A case EUSERS: /* Too many users */
1N/A#endif
1N/A#ifdef ECONNABORTED
1N/A case ECONNABORTED: /* Software caused connection abort */
1N/A#endif
1N/A#ifdef ECONNREFUSED
1N/A case ECONNREFUSED: /* Connection refused */
1N/A#endif
1N/A#ifdef ECONNRESET
1N/A case ECONNRESET: /* Connection reset by peer */
1N/A#endif
1N/A#ifdef EDEADLK
1N/A case EDEADLK: /* Resource deadlock avoided */
1N/A#endif
1N/A#ifdef EFBIG
1N/A case EFBIG: /* File too large */
1N/A#endif
1N/A#ifdef EHOSTDOWN
1N/A case EHOSTDOWN: /* Host is down */
1N/A#endif
1N/A#ifdef EHOSTUNREACH
1N/A case EHOSTUNREACH: /* No route to host */
1N/A#endif
1N/A#ifdef EMFILE
1N/A case EMFILE: /* Too many open files */
1N/A#endif
1N/A#ifdef ENETDOWN
1N/A case ENETDOWN: /* Network is down */
1N/A#endif
1N/A#ifdef ENETRESET
1N/A case ENETRESET: /* Network dropped connection on reset */
1N/A#endif
1N/A#ifdef ENETUNREACH
1N/A case ENETUNREACH: /* Network is unreachable */
1N/A#endif
1N/A#ifdef ENFILE
1N/A case ENFILE: /* Too many open files in system */
1N/A#endif
1N/A#ifdef ENOBUFS
1N/A case ENOBUFS: /* No buffer space available */
1N/A#endif
1N/A#ifdef ENOMEM
1N/A case ENOMEM: /* Cannot allocate memory */
1N/A#endif
1N/A#ifdef ENOSPC
1N/A case ENOSPC: /* No space left on device */
1N/A#endif
1N/A#ifdef EROFS
1N/A case EROFS: /* Read-only file system */
1N/A#endif
1N/A#ifdef ESTALE
1N/A case ESTALE: /* Stale NFS file handle */
1N/A#endif
1N/A#ifdef ETIMEDOUT
1N/A case ETIMEDOUT: /* Connection timed out */
1N/A#endif
1N/A#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1N/A case EWOULDBLOCK: /* Operation would block. */
1N/A#endif
1N/A eval = EX_TEMPFAIL;
1N/A break;
1N/A default:
1N/A eval = EX_UNAVAILABLE;
1N/A break;
1N/A }
1N/A}