xref: /titanic_52/usr/src/cmd/sendmail/aux/mail.local.c (revision 7cdcb8abb9b4700219e82028dac3d78699314b6d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1998 Sendmail, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994
47c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
77c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level
87c478bd9Sstevel@tonic-gate  * of the sendmail distribution.
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate /*
12*7cdcb8abSjbeck  * Copyright 1994-2007 Sun Microsystems, Inc.  All rights reserved.
137c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #ifndef lint
177c478bd9Sstevel@tonic-gate static char copyright[] =
187c478bd9Sstevel@tonic-gate "@(#) Copyright (c) 1990, 1993, 1994\n\
197c478bd9Sstevel@tonic-gate 	The Regents of the University of California.  All rights reserved.\n";
207c478bd9Sstevel@tonic-gate #endif /* not lint */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #ifndef lint
257c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#)mail.local.c	8.83 (Berkeley) 12/17/98";
267c478bd9Sstevel@tonic-gate static char sccsi2[] = "%W% (Sun) %G%";
277c478bd9Sstevel@tonic-gate #endif /* not lint */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <sys/file.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <netinet/in.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <netdb.h>
397c478bd9Sstevel@tonic-gate #include <pwd.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <ctype.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <sysexits.h>
467c478bd9Sstevel@tonic-gate #include <time.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <maillock.h>
497c478bd9Sstevel@tonic-gate #include <grp.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #ifdef __STDC__
527c478bd9Sstevel@tonic-gate #include <stdarg.h>
537c478bd9Sstevel@tonic-gate #else
547c478bd9Sstevel@tonic-gate #include <varargs.h>
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <syslog.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sysexits.h>
607c478bd9Sstevel@tonic-gate #include <ctype.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <sm/conf.h>
637c478bd9Sstevel@tonic-gate #include <sendmail/pathnames.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate **  If you don't have flock, you could try using lockf instead.
677c478bd9Sstevel@tonic-gate */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #ifdef LDA_USE_LOCKF
707c478bd9Sstevel@tonic-gate # define flock(a, b)	lockf(a, b, 0)
717c478bd9Sstevel@tonic-gate # ifdef LOCK_EX
727c478bd9Sstevel@tonic-gate #  undef LOCK_EX
737c478bd9Sstevel@tonic-gate # endif /* LOCK_EX */
747c478bd9Sstevel@tonic-gate # define LOCK_EX        F_LOCK
757c478bd9Sstevel@tonic-gate #endif /* LDA_USE_LOCKF */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifndef LOCK_EX
787c478bd9Sstevel@tonic-gate # include <sys/file.h>
797c478bd9Sstevel@tonic-gate #endif /* ! LOCK_EX */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #ifndef MAILER_DAEMON
827c478bd9Sstevel@tonic-gate # define MAILER_DAEMON	"MAILER-DAEMON"
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate typedef int bool;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	FALSE	0
887c478bd9Sstevel@tonic-gate #define	TRUE	1
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate bool	EightBitMime = TRUE;		/* advertise 8BITMIME in LMTP */
917c478bd9Sstevel@tonic-gate static int eval = EX_OK;			/* sysexits.h error value. */
927c478bd9Sstevel@tonic-gate static int lmtpmode = 0;
937c478bd9Sstevel@tonic-gate bool	bouncequota = FALSE;		/* permanent error when over quota */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	_PATH_MAILDIR	"/var/mail"
967c478bd9Sstevel@tonic-gate #define	_PATH_LOCTMP	"/tmp/local.XXXXXX"
977c478bd9Sstevel@tonic-gate #define	_PATH_LOCHTMP	"/tmp/lochd.XXXXXX"
987c478bd9Sstevel@tonic-gate #define	FALSE 0
997c478bd9Sstevel@tonic-gate #define	TRUE  1
1007c478bd9Sstevel@tonic-gate #define	MAXLINE 2048
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static void	deliver(int, int, char *, bool);
1037c478bd9Sstevel@tonic-gate static void	e_to_sys(int);
1047c478bd9Sstevel@tonic-gate static void	err(const char *fmt, ...);
1057c478bd9Sstevel@tonic-gate static void	notifybiff(char *);
1067c478bd9Sstevel@tonic-gate static void	store(char *, int);
1077c478bd9Sstevel@tonic-gate static void	usage(void);
1087c478bd9Sstevel@tonic-gate static void	vwarn();
1097c478bd9Sstevel@tonic-gate static void	warn(const char *fmt, ...);
1107c478bd9Sstevel@tonic-gate static void	mailerr(const char *, const char *, ...);
1117c478bd9Sstevel@tonic-gate static void	sigterm_handler();
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static char	unix_from_line[MAXLINE];
1147c478bd9Sstevel@tonic-gate static int	ulen;
1157c478bd9Sstevel@tonic-gate static int	content_length;
1167c478bd9Sstevel@tonic-gate static int	bfd, hfd; /* temp file */
1177c478bd9Sstevel@tonic-gate static uid_t	src_uid, targ_uid, saved_uid;
1187c478bd9Sstevel@tonic-gate static int	sigterm_caught;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate int
1217c478bd9Sstevel@tonic-gate main(argc, argv)
1227c478bd9Sstevel@tonic-gate 	int argc;
1237c478bd9Sstevel@tonic-gate 	char *argv[];
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	struct passwd *pw;
1267c478bd9Sstevel@tonic-gate 	int ch;
1277c478bd9Sstevel@tonic-gate 	uid_t uid;
1287c478bd9Sstevel@tonic-gate 	char *from;
1297c478bd9Sstevel@tonic-gate 	struct  group *grpptr;
1307c478bd9Sstevel@tonic-gate 	void dolmtp();
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	openlog("mail.local", 0, LOG_MAIL);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	from = NULL;
1357c478bd9Sstevel@tonic-gate 	pw = NULL;
1367c478bd9Sstevel@tonic-gate 	sigterm_caught = FALSE;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm_handler);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "7bdf:r:l")) != EOF)
1417c478bd9Sstevel@tonic-gate 		switch (ch) {
1427c478bd9Sstevel@tonic-gate 		case '7':		/* Do not advertise 8BITMIME */
1437c478bd9Sstevel@tonic-gate 			EightBitMime = FALSE;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		case 'b':		/* bounce mail when over quota. */
1477c478bd9Sstevel@tonic-gate 			bouncequota = TRUE;
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		case 'd':		/* Backward compatible. */
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case 'f':
1537c478bd9Sstevel@tonic-gate 		case 'r':		/* Backward compatible. */
1547c478bd9Sstevel@tonic-gate 			if (from != NULL) {
1557c478bd9Sstevel@tonic-gate 				warn("multiple -f options");
1567c478bd9Sstevel@tonic-gate 				usage();
1577c478bd9Sstevel@tonic-gate 			}
1587c478bd9Sstevel@tonic-gate 			from = optarg;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		case 'l':
1617c478bd9Sstevel@tonic-gate 			lmtpmode++;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 		case '?':
1647c478bd9Sstevel@tonic-gate 		default:
1657c478bd9Sstevel@tonic-gate 			usage();
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	argc -= optind;
1687c478bd9Sstevel@tonic-gate 	argv += optind;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	notifybiff(NULL); /* initialize biff structures */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * We expect sendmail will invoke us with saved id 0
1747c478bd9Sstevel@tonic-gate 	 * We then do setgid and setuid defore delivery
1757c478bd9Sstevel@tonic-gate 	 * setgid to mail group
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	if ((grpptr = getgrnam("mail")) != NULL)
1787c478bd9Sstevel@tonic-gate 		(void) setgid(grpptr->gr_gid);
1797c478bd9Sstevel@tonic-gate 	saved_uid = geteuid();
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (lmtpmode) {
1827c478bd9Sstevel@tonic-gate 		if (saved_uid != 0) {
1837c478bd9Sstevel@tonic-gate 			warn("only super-user can use -l option");
1847c478bd9Sstevel@tonic-gate 			exit(EX_CANTCREAT);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		dolmtp(bouncequota);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (!*argv)
1907c478bd9Sstevel@tonic-gate 		usage();
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * If from not specified, use the name from getlogin() if the
1947c478bd9Sstevel@tonic-gate 	 * uid matches, otherwise, use the name from the password file
1957c478bd9Sstevel@tonic-gate 	 * corresponding to the uid.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	uid = getuid();
1987c478bd9Sstevel@tonic-gate 	if (!from && (!(from = getlogin()) ||
1997c478bd9Sstevel@tonic-gate 	    !(pw = getpwnam(from)) || pw->pw_uid != uid))
2007c478bd9Sstevel@tonic-gate 		from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
2017c478bd9Sstevel@tonic-gate 	src_uid = pw ? pw->pw_uid : uid;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * There is no way to distinguish the error status of one delivery
2057c478bd9Sstevel@tonic-gate 	 * from the rest of the deliveries.  So, if we failed hard on one
2067c478bd9Sstevel@tonic-gate 	 * or more deliveries, but had no failures on any of the others, we
2077c478bd9Sstevel@tonic-gate 	 * return a hard failure.  If we failed temporarily on one or more
2087c478bd9Sstevel@tonic-gate 	 * deliveries, we return a temporary failure regardless of the other
2097c478bd9Sstevel@tonic-gate 	 * failures.  This results in the delivery being reattempted later
2107c478bd9Sstevel@tonic-gate 	 * at the expense of repeated failures and multiple deliveries.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	for (store(from, 0); *argv; ++argv)
2147c478bd9Sstevel@tonic-gate 		deliver(hfd, bfd, *argv, bouncequota);
2157c478bd9Sstevel@tonic-gate 	return (eval);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate void
2197c478bd9Sstevel@tonic-gate sigterm_handler()
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	sigterm_caught = TRUE;
2227c478bd9Sstevel@tonic-gate 	(void) sigignore(SIGTERM);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate char *
2267c478bd9Sstevel@tonic-gate parseaddr(s)
2277c478bd9Sstevel@tonic-gate 	char *s;
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	char *p;
2307c478bd9Sstevel@tonic-gate 	int len;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (*s++ != '<')
2337c478bd9Sstevel@tonic-gate 		return NULL;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	p = s;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* at-domain-list */
2387c478bd9Sstevel@tonic-gate 	while (*p == '@') {
2397c478bd9Sstevel@tonic-gate 		p++;
2407c478bd9Sstevel@tonic-gate 		if (*p == '[') {
2417c478bd9Sstevel@tonic-gate 			p++;
2427c478bd9Sstevel@tonic-gate 			while (isascii(*p) &&
2437c478bd9Sstevel@tonic-gate 			       (isalnum(*p) || *p == '.' ||
2447c478bd9Sstevel@tonic-gate 				*p == '-' || *p == ':'))
2457c478bd9Sstevel@tonic-gate 				p++;
2467c478bd9Sstevel@tonic-gate 			if (*p++ != ']')
2477c478bd9Sstevel@tonic-gate 				return NULL;
2487c478bd9Sstevel@tonic-gate 		} else {
2497c478bd9Sstevel@tonic-gate 			while ((isascii(*p) && isalnum(*p)) ||
2507c478bd9Sstevel@tonic-gate 			       strchr(".-_", *p))
2517c478bd9Sstevel@tonic-gate 				p++;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		if (*p == ',' && p[1] == '@')
2547c478bd9Sstevel@tonic-gate 			p++;
2557c478bd9Sstevel@tonic-gate 		else if (*p == ':' && p[1] != '@')
2567c478bd9Sstevel@tonic-gate 			p++;
2577c478bd9Sstevel@tonic-gate 		else
2587c478bd9Sstevel@tonic-gate 			return NULL;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	s = p;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* local-part */
2647c478bd9Sstevel@tonic-gate 	if (*p == '\"') {
2657c478bd9Sstevel@tonic-gate 		p++;
2667c478bd9Sstevel@tonic-gate 		while (*p && *p != '\"') {
2677c478bd9Sstevel@tonic-gate 			if (*p == '\\') {
2687c478bd9Sstevel@tonic-gate 				if (!*++p)
2697c478bd9Sstevel@tonic-gate 					return NULL;
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			p++;
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 		if (!*p++)
2747c478bd9Sstevel@tonic-gate 			return NULL;
2757c478bd9Sstevel@tonic-gate 	} else {
2767c478bd9Sstevel@tonic-gate 		while (*p && *p != '@' && *p != '>') {
2777c478bd9Sstevel@tonic-gate 			if (*p == '\\') {
2787c478bd9Sstevel@tonic-gate 				if (!*++p)
2797c478bd9Sstevel@tonic-gate 					return NULL;
2807c478bd9Sstevel@tonic-gate 			} else {
2817c478bd9Sstevel@tonic-gate 			if (*p <= ' ' || (*p & 128) ||
2827c478bd9Sstevel@tonic-gate 			    strchr("<>()[]\\,;:\"", *p))
2837c478bd9Sstevel@tonic-gate 				return NULL;
2847c478bd9Sstevel@tonic-gate 			}
2857c478bd9Sstevel@tonic-gate 			p++;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* @domain */
2907c478bd9Sstevel@tonic-gate 	if (*p == '@') {
2917c478bd9Sstevel@tonic-gate 		p++;
2927c478bd9Sstevel@tonic-gate 		if (*p == '[') {
2937c478bd9Sstevel@tonic-gate 			p++;
2947c478bd9Sstevel@tonic-gate 			while (isascii(*p) &&
2957c478bd9Sstevel@tonic-gate 			       (isalnum(*p) || *p == '.' ||
2967c478bd9Sstevel@tonic-gate 				*p == '-' || *p == ':'))
2977c478bd9Sstevel@tonic-gate 				p++;
2987c478bd9Sstevel@tonic-gate 			if (*p++ != ']')
2997c478bd9Sstevel@tonic-gate 				return NULL;
3007c478bd9Sstevel@tonic-gate 		} else {
3017c478bd9Sstevel@tonic-gate 			while ((isascii(*p) && isalnum(*p)) ||
3027c478bd9Sstevel@tonic-gate 			       strchr(".-_", *p))
3037c478bd9Sstevel@tonic-gate 				p++;
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (*p++ != '>')
3087c478bd9Sstevel@tonic-gate 		return NULL;
3097c478bd9Sstevel@tonic-gate 	if (*p && *p != ' ')
3107c478bd9Sstevel@tonic-gate 		return NULL;
3117c478bd9Sstevel@tonic-gate 	len = p - s - 1;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (*s == '\0' || len <= 0)
3147c478bd9Sstevel@tonic-gate 	{
3157c478bd9Sstevel@tonic-gate 		s = MAILER_DAEMON;
3167c478bd9Sstevel@tonic-gate 		len = strlen(s);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	p = malloc(len + 1);
3207c478bd9Sstevel@tonic-gate 	if (p == NULL) {
3217c478bd9Sstevel@tonic-gate 		printf("421 4.3.0 memory exhausted\r\n");
3227c478bd9Sstevel@tonic-gate 		exit(EX_TEMPFAIL);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	strncpy(p, s, len);
3267c478bd9Sstevel@tonic-gate 	p[len] = '\0';
3277c478bd9Sstevel@tonic-gate 	return p;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate char *
3317c478bd9Sstevel@tonic-gate process_recipient(addr)
3327c478bd9Sstevel@tonic-gate 	char *addr;
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	if (getpwnam(addr) == NULL) {
3357c478bd9Sstevel@tonic-gate 		return "550 5.1.1 user unknown";
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	return NULL;
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate #define RCPT_GROW	30
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate void
3447c478bd9Sstevel@tonic-gate dolmtp(bouncequota)
3457c478bd9Sstevel@tonic-gate 	bool bouncequota;
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	char *return_path = NULL;
3487c478bd9Sstevel@tonic-gate 	char **rcpt_addr = NULL;
3497c478bd9Sstevel@tonic-gate 	int rcpt_num = 0;
3507c478bd9Sstevel@tonic-gate 	int rcpt_alloc = 0;
3517c478bd9Sstevel@tonic-gate 	bool gotlhlo = FALSE;
3527c478bd9Sstevel@tonic-gate 	char myhostname[MAXHOSTNAMELEN];
3537c478bd9Sstevel@tonic-gate 	char buf[4096];
3547c478bd9Sstevel@tonic-gate 	char *err;
3557c478bd9Sstevel@tonic-gate 	char *p;
3567c478bd9Sstevel@tonic-gate 	int i;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	gethostname(myhostname, sizeof myhostname - 1);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	printf("220 %s LMTP ready\r\n", myhostname);
3617c478bd9Sstevel@tonic-gate 	for (;;) {
3627c478bd9Sstevel@tonic-gate 		if (sigterm_caught) {
3637c478bd9Sstevel@tonic-gate 			for (; rcpt_num > 0; rcpt_num--)
3647c478bd9Sstevel@tonic-gate 				printf("451 4.3.0 shutting down\r\n");
3657c478bd9Sstevel@tonic-gate 			exit(EX_OK);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		fflush(stdout);
3687c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
3697c478bd9Sstevel@tonic-gate 			exit(EX_OK);
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 		p = buf + strlen(buf) - 1;
3727c478bd9Sstevel@tonic-gate 		if (p >= buf && *p == '\n')
3737c478bd9Sstevel@tonic-gate 			*p-- = '\0';
3747c478bd9Sstevel@tonic-gate 		if (p >= buf && *p == '\r')
3757c478bd9Sstevel@tonic-gate 			*p-- = '\0';
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		switch (buf[0]) {
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		case 'd':
3807c478bd9Sstevel@tonic-gate 		case 'D':
3817c478bd9Sstevel@tonic-gate 			if (strcasecmp(buf, "data") == 0) {
3827c478bd9Sstevel@tonic-gate 				if (rcpt_num == 0) {
3837c478bd9Sstevel@tonic-gate 					printf("503 5.5.1 No recipients\r\n");
3847c478bd9Sstevel@tonic-gate 					continue;
3857c478bd9Sstevel@tonic-gate 				}
3867c478bd9Sstevel@tonic-gate 				store(return_path, rcpt_num);
3877c478bd9Sstevel@tonic-gate 				if (bfd == -1 || hfd == -1)
3887c478bd9Sstevel@tonic-gate 					continue;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 				for (i = 0; i < rcpt_num; i++) {
3917c478bd9Sstevel@tonic-gate 					p = strchr(rcpt_addr[i], '+');
3927c478bd9Sstevel@tonic-gate 					if (p != NULL)
3937c478bd9Sstevel@tonic-gate 						*p++ = '\0';
3947c478bd9Sstevel@tonic-gate 					deliver(hfd, bfd, rcpt_addr[i],
3957c478bd9Sstevel@tonic-gate 						bouncequota);
3967c478bd9Sstevel@tonic-gate 				}
3977c478bd9Sstevel@tonic-gate 				close(bfd);
3987c478bd9Sstevel@tonic-gate 				close(hfd);
3997c478bd9Sstevel@tonic-gate 				goto rset;
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 			goto syntaxerr;
4027c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
4037c478bd9Sstevel@tonic-gate 			break;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		case 'l':
4067c478bd9Sstevel@tonic-gate 		case 'L':
4077c478bd9Sstevel@tonic-gate 			if (strncasecmp(buf, "lhlo ", 5) == 0)
4087c478bd9Sstevel@tonic-gate 			{
4097c478bd9Sstevel@tonic-gate 				/* check for duplicate per RFC 1651 4.2 */
4107c478bd9Sstevel@tonic-gate 				if (gotlhlo)
4117c478bd9Sstevel@tonic-gate 				{
4127c478bd9Sstevel@tonic-gate 					printf("503 %s Duplicate LHLO\r\n",
4137c478bd9Sstevel@tonic-gate 					       myhostname);
4147c478bd9Sstevel@tonic-gate 					continue;
4157c478bd9Sstevel@tonic-gate 				}
4167c478bd9Sstevel@tonic-gate 				gotlhlo = TRUE;
4177c478bd9Sstevel@tonic-gate 				printf("250-%s\r\n", myhostname);
4187c478bd9Sstevel@tonic-gate 				if (EightBitMime)
4197c478bd9Sstevel@tonic-gate 					printf("250-8BITMIME\r\n");
4207c478bd9Sstevel@tonic-gate 				printf("250-ENHANCEDSTATUSCODES\r\n");
4217c478bd9Sstevel@tonic-gate 				printf("250 PIPELINING\r\n");
4227c478bd9Sstevel@tonic-gate 				continue;
4237c478bd9Sstevel@tonic-gate 			}
4247c478bd9Sstevel@tonic-gate 			goto syntaxerr;
4257c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
4267c478bd9Sstevel@tonic-gate 			break;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		case 'm':
4297c478bd9Sstevel@tonic-gate 		case 'M':
4307c478bd9Sstevel@tonic-gate 			if (strncasecmp(buf, "mail ", 5) == 0) {
4317c478bd9Sstevel@tonic-gate 				if (return_path != NULL) {
4327c478bd9Sstevel@tonic-gate 					printf("503 5.5.1 Nested MAIL command\r\n");
4337c478bd9Sstevel@tonic-gate 					continue;
4347c478bd9Sstevel@tonic-gate 				}
4357c478bd9Sstevel@tonic-gate 				if (strncasecmp(buf+5, "from:", 5) != 0 ||
4367c478bd9Sstevel@tonic-gate 				    ((return_path = parseaddr(buf+10)) == NULL)) {
4377c478bd9Sstevel@tonic-gate 					printf("501 5.5.4 Syntax error in parameters\r\n");
4387c478bd9Sstevel@tonic-gate 					continue;
4397c478bd9Sstevel@tonic-gate 				}
4407c478bd9Sstevel@tonic-gate 				printf("250 2.5.0 ok\r\n");
4417c478bd9Sstevel@tonic-gate 				continue;
4427c478bd9Sstevel@tonic-gate 			}
4437c478bd9Sstevel@tonic-gate 			goto syntaxerr;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		case 'n':
4467c478bd9Sstevel@tonic-gate 		case 'N':
4477c478bd9Sstevel@tonic-gate 			if (strcasecmp(buf, "noop") == 0) {
4487c478bd9Sstevel@tonic-gate 				printf("250 2.0.0 ok\r\n");
4497c478bd9Sstevel@tonic-gate 				continue;
4507c478bd9Sstevel@tonic-gate 			}
4517c478bd9Sstevel@tonic-gate 			goto syntaxerr;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		case 'q':
4547c478bd9Sstevel@tonic-gate 		case 'Q':
4557c478bd9Sstevel@tonic-gate 			if (strcasecmp(buf, "quit") == 0) {
4567c478bd9Sstevel@tonic-gate 				printf("221 2.0.0 bye\r\n");
4577c478bd9Sstevel@tonic-gate 				exit(EX_OK);
4587c478bd9Sstevel@tonic-gate 			}
4597c478bd9Sstevel@tonic-gate 			goto syntaxerr;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		case 'r':
4627c478bd9Sstevel@tonic-gate 		case 'R':
4637c478bd9Sstevel@tonic-gate 			if (strncasecmp(buf, "rcpt ", 5) == 0) {
4647c478bd9Sstevel@tonic-gate 				if (return_path == NULL) {
4657c478bd9Sstevel@tonic-gate 					printf("503 5.5.1 Need MAIL command\r\n");
4667c478bd9Sstevel@tonic-gate 					continue;
4677c478bd9Sstevel@tonic-gate 				}
4687c478bd9Sstevel@tonic-gate 				if (rcpt_num >= rcpt_alloc) {
4697c478bd9Sstevel@tonic-gate 					rcpt_alloc += RCPT_GROW;
4707c478bd9Sstevel@tonic-gate 					rcpt_addr = (char **)
4717c478bd9Sstevel@tonic-gate 						realloc((char *)rcpt_addr,
4727c478bd9Sstevel@tonic-gate 							rcpt_alloc * sizeof(char **));
4737c478bd9Sstevel@tonic-gate 					if (rcpt_addr == NULL) {
4747c478bd9Sstevel@tonic-gate 						printf("421 4.3.0 memory exhausted\r\n");
4757c478bd9Sstevel@tonic-gate 						exit(EX_TEMPFAIL);
4767c478bd9Sstevel@tonic-gate 					}
4777c478bd9Sstevel@tonic-gate 				}
4787c478bd9Sstevel@tonic-gate 				if (strncasecmp(buf+5, "to:", 3) != 0 ||
4797c478bd9Sstevel@tonic-gate 				    ((rcpt_addr[rcpt_num] = parseaddr(buf+8)) == NULL)) {
4807c478bd9Sstevel@tonic-gate 					printf("501 5.5.4 Syntax error in parameters\r\n");
4817c478bd9Sstevel@tonic-gate 					continue;
4827c478bd9Sstevel@tonic-gate 				}
4837c478bd9Sstevel@tonic-gate 				if ((err = process_recipient(rcpt_addr[rcpt_num])) != NULL) {
4847c478bd9Sstevel@tonic-gate 					printf("%s\r\n", err);
4857c478bd9Sstevel@tonic-gate 					continue;
4867c478bd9Sstevel@tonic-gate 				}
4877c478bd9Sstevel@tonic-gate 				rcpt_num++;
4887c478bd9Sstevel@tonic-gate 				printf("250 2.1.5 ok\r\n");
4897c478bd9Sstevel@tonic-gate 				continue;
4907c478bd9Sstevel@tonic-gate 			}
4917c478bd9Sstevel@tonic-gate 			else if (strcasecmp(buf, "rset") == 0) {
4927c478bd9Sstevel@tonic-gate 				printf("250 2.0.0 ok\r\n");
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate   rset:
4957c478bd9Sstevel@tonic-gate 				while (rcpt_num > 0) {
4967c478bd9Sstevel@tonic-gate 					free(rcpt_addr[--rcpt_num]);
4977c478bd9Sstevel@tonic-gate 				}
4987c478bd9Sstevel@tonic-gate 				if (return_path != NULL)
4997c478bd9Sstevel@tonic-gate 					free(return_path);
5007c478bd9Sstevel@tonic-gate 				return_path = NULL;
5017c478bd9Sstevel@tonic-gate 				continue;
5027c478bd9Sstevel@tonic-gate 			}
5037c478bd9Sstevel@tonic-gate 			goto syntaxerr;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		case 'v':
5067c478bd9Sstevel@tonic-gate 		case 'V':
5077c478bd9Sstevel@tonic-gate 			if (strncasecmp(buf, "vrfy ", 5) == 0) {
5087c478bd9Sstevel@tonic-gate 				printf("252 2.3.3 try RCPT to attempt delivery\r\n");
5097c478bd9Sstevel@tonic-gate 				continue;
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 			goto syntaxerr;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		default:
5147c478bd9Sstevel@tonic-gate   syntaxerr:
5157c478bd9Sstevel@tonic-gate 			printf("500 5.5.2 Syntax error\r\n");
5167c478bd9Sstevel@tonic-gate 			continue;
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static void
5227c478bd9Sstevel@tonic-gate store(from, lmtprcpts)
5237c478bd9Sstevel@tonic-gate 	char *from;
5247c478bd9Sstevel@tonic-gate 	int lmtprcpts;
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	FILE *fp = NULL;
5277c478bd9Sstevel@tonic-gate 	time_t tval;
5287c478bd9Sstevel@tonic-gate 	bool fullline = TRUE;	/* current line is terminated */
5297c478bd9Sstevel@tonic-gate 	bool prevfl;		/* previous line was terminated */
5307c478bd9Sstevel@tonic-gate 	char line[MAXLINE];
5317c478bd9Sstevel@tonic-gate 	FILE *bfp, *hfp;
5327c478bd9Sstevel@tonic-gate 	char *btn, *htn;
5337c478bd9Sstevel@tonic-gate 	int in_header_section;
534*7cdcb8abSjbeck 	int newfd;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	bfd = -1;
5377c478bd9Sstevel@tonic-gate 	hfd = -1;
5387c478bd9Sstevel@tonic-gate 	btn = strdup(_PATH_LOCTMP);
5397c478bd9Sstevel@tonic-gate 	if ((bfd = mkstemp(btn)) == -1 || (bfp = fdopen(bfd, "w+")) == NULL) {
5407c478bd9Sstevel@tonic-gate 		if (bfd != -1)
5417c478bd9Sstevel@tonic-gate 			(void) close(bfd);
5427c478bd9Sstevel@tonic-gate 		if (lmtprcpts) {
5437c478bd9Sstevel@tonic-gate 			printf("451 4.3.0 unable to open temporary file\r\n");
5447c478bd9Sstevel@tonic-gate 			return;
5457c478bd9Sstevel@tonic-gate 		} else {
5467c478bd9Sstevel@tonic-gate 			mailerr("451 4.3.0", "unable to open temporary file");
5477c478bd9Sstevel@tonic-gate 			exit(eval);
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	(void) unlink(btn);
5517c478bd9Sstevel@tonic-gate 	free(btn);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (lmtpmode) {
5547c478bd9Sstevel@tonic-gate 		printf("354 go ahead\r\n");
5557c478bd9Sstevel@tonic-gate 		fflush(stdout);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	htn = strdup(_PATH_LOCHTMP);
5597c478bd9Sstevel@tonic-gate 	if ((hfd = mkstemp(htn)) == -1 || (hfp = fdopen(hfd, "w+")) == NULL) {
5607c478bd9Sstevel@tonic-gate 		if (hfd != -1)
5617c478bd9Sstevel@tonic-gate 			(void) close(hfd);
5627c478bd9Sstevel@tonic-gate 		e_to_sys(errno);
5637c478bd9Sstevel@tonic-gate 		err("unable to open temporary file");
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	(void) unlink(htn);
5667c478bd9Sstevel@tonic-gate 	free(htn);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	in_header_section = TRUE;
5697c478bd9Sstevel@tonic-gate 	content_length = 0;
5707c478bd9Sstevel@tonic-gate 	fp = hfp;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	line[0] = '\0';
5737c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof(line), stdin) != (char *)NULL)
5747c478bd9Sstevel@tonic-gate 	{
5757c478bd9Sstevel@tonic-gate 		size_t line_len = 0;
5767c478bd9Sstevel@tonic-gate 		int peek;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		prevfl = fullline;	/* preserve state of previous line */
5797c478bd9Sstevel@tonic-gate 		while (line[line_len] != '\n' && line_len < sizeof(line) - 2)
5807c478bd9Sstevel@tonic-gate 			line_len++;
5817c478bd9Sstevel@tonic-gate 		line_len++;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		/* Check for dot-stuffing */
5847c478bd9Sstevel@tonic-gate 		if (prevfl && lmtprcpts && line[0] == '.')
5857c478bd9Sstevel@tonic-gate 		{
5867c478bd9Sstevel@tonic-gate 			if (line[1] == '\n' ||
5877c478bd9Sstevel@tonic-gate 			    (line[1] == '\r' && line[2] == '\n'))
5887c478bd9Sstevel@tonic-gate 				goto lmtpdot;
5897c478bd9Sstevel@tonic-gate 			memcpy(line, line + 1, line_len);
5907c478bd9Sstevel@tonic-gate 			line_len--;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		/* Check to see if we have the full line from fgets() */
5947c478bd9Sstevel@tonic-gate 		fullline = FALSE;
5957c478bd9Sstevel@tonic-gate 		if (line_len > 0)
5967c478bd9Sstevel@tonic-gate 		{
5977c478bd9Sstevel@tonic-gate 			if (line[line_len - 1] == '\n')
5987c478bd9Sstevel@tonic-gate 			{
5997c478bd9Sstevel@tonic-gate 				if (line_len >= 2 &&
6007c478bd9Sstevel@tonic-gate 				    line[line_len - 2] == '\r')
6017c478bd9Sstevel@tonic-gate 				{
6027c478bd9Sstevel@tonic-gate 					line[line_len - 2] = '\n';
6037c478bd9Sstevel@tonic-gate 					line[line_len - 1] = '\0';
6047c478bd9Sstevel@tonic-gate 					line_len--;
6057c478bd9Sstevel@tonic-gate 				}
6067c478bd9Sstevel@tonic-gate 				fullline = TRUE;
6077c478bd9Sstevel@tonic-gate 			}
6087c478bd9Sstevel@tonic-gate 			else if (line[line_len - 1] == '\r')
6097c478bd9Sstevel@tonic-gate 			{
6107c478bd9Sstevel@tonic-gate 				/* Did we just miss the CRLF? */
6117c478bd9Sstevel@tonic-gate 				peek = fgetc(stdin);
6127c478bd9Sstevel@tonic-gate 				if (peek == '\n')
6137c478bd9Sstevel@tonic-gate 				{
6147c478bd9Sstevel@tonic-gate 					line[line_len - 1] = '\n';
6157c478bd9Sstevel@tonic-gate 					fullline = TRUE;
6167c478bd9Sstevel@tonic-gate 				}
6177c478bd9Sstevel@tonic-gate 				else
6187c478bd9Sstevel@tonic-gate 					(void) ungetc(peek, stdin);
6197c478bd9Sstevel@tonic-gate 			}
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		else
6227c478bd9Sstevel@tonic-gate 			fullline = TRUE;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		if (prevfl && line[0] == '\n' && in_header_section) {
6257c478bd9Sstevel@tonic-gate 			in_header_section = FALSE;
6267c478bd9Sstevel@tonic-gate 			if (fflush(fp) == EOF || ferror(fp)) {
6277c478bd9Sstevel@tonic-gate 				if (lmtprcpts) {
6287c478bd9Sstevel@tonic-gate 					while (lmtprcpts--)
6297c478bd9Sstevel@tonic-gate 						printf("451 4.3.0 temporary file write error\r\n");
6307c478bd9Sstevel@tonic-gate 					fclose(fp);
6317c478bd9Sstevel@tonic-gate 					return;
6327c478bd9Sstevel@tonic-gate 				} else {
6337c478bd9Sstevel@tonic-gate 					mailerr("451 4.3.0",
6347c478bd9Sstevel@tonic-gate 						"temporary file write error");
6357c478bd9Sstevel@tonic-gate 					fclose(fp);
6367c478bd9Sstevel@tonic-gate 					exit(eval);
6377c478bd9Sstevel@tonic-gate 				}
6387c478bd9Sstevel@tonic-gate 			}
6397c478bd9Sstevel@tonic-gate 			fp = bfp;
6407c478bd9Sstevel@tonic-gate 			continue;
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		if (in_header_section) {
6447c478bd9Sstevel@tonic-gate 			if (strncasecmp("Content-Length:", line, 15) == 0) {
6457c478bd9Sstevel@tonic-gate 				continue; /* skip this header */
6467c478bd9Sstevel@tonic-gate 			}
6477c478bd9Sstevel@tonic-gate 		} else
6487c478bd9Sstevel@tonic-gate 			content_length += strlen(line);
6497c478bd9Sstevel@tonic-gate 		(void) fwrite(line, sizeof(char), line_len, fp);
6507c478bd9Sstevel@tonic-gate 		if (ferror(fp)) {
6517c478bd9Sstevel@tonic-gate 			if (lmtprcpts) {
6527c478bd9Sstevel@tonic-gate 				while (lmtprcpts--)
6537c478bd9Sstevel@tonic-gate 					printf("451 4.3.0 temporary file write error\r\n");
6547c478bd9Sstevel@tonic-gate 				fclose(fp);
6557c478bd9Sstevel@tonic-gate 				return;
6567c478bd9Sstevel@tonic-gate 			} else {
6577c478bd9Sstevel@tonic-gate 				mailerr("451 4.3.0",
6587c478bd9Sstevel@tonic-gate 					"temporary file write error");
6597c478bd9Sstevel@tonic-gate 				fclose(fp);
6607c478bd9Sstevel@tonic-gate 				exit(eval);
6617c478bd9Sstevel@tonic-gate 			}
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	if (sigterm_caught) {
6657c478bd9Sstevel@tonic-gate 		if (lmtprcpts)
6667c478bd9Sstevel@tonic-gate 			while (lmtprcpts--)
6677c478bd9Sstevel@tonic-gate 				printf("451 4.3.0 shutting down\r\n");
6687c478bd9Sstevel@tonic-gate 		else
6697c478bd9Sstevel@tonic-gate 			mailerr("451 4.3.0", "shutting down");
6707c478bd9Sstevel@tonic-gate 		fclose(fp);
6717c478bd9Sstevel@tonic-gate 		exit(eval);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	if (lmtprcpts) {
6757c478bd9Sstevel@tonic-gate 		/* Got a premature EOF -- toss message and exit */
6767c478bd9Sstevel@tonic-gate 		exit(EX_OK);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/* If message not newline terminated, need an extra. */
6807c478bd9Sstevel@tonic-gate 	if (!strchr(line, '\n')) {
6817c478bd9Sstevel@tonic-gate 		(void) putc('\n', fp);
6827c478bd9Sstevel@tonic-gate 		content_length++;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate   lmtpdot:
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* Output a newline; note, empty messages are allowed. */
6887c478bd9Sstevel@tonic-gate 	(void) putc('\n', fp);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if (fflush(fp) == EOF || ferror(fp)) {
6917c478bd9Sstevel@tonic-gate 		if (lmtprcpts) {
6927c478bd9Sstevel@tonic-gate 			while (lmtprcpts--) {
6937c478bd9Sstevel@tonic-gate 				printf("451 4.3.0 temporary file write error\r\n");
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 			fclose(fp);
6967c478bd9Sstevel@tonic-gate 			return;
6977c478bd9Sstevel@tonic-gate 		} else {
6987c478bd9Sstevel@tonic-gate 			mailerr("451 4.3.0", "temporary file write error");
6997c478bd9Sstevel@tonic-gate 			fclose(fp);
7007c478bd9Sstevel@tonic-gate 			exit(eval);
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
704*7cdcb8abSjbeck 	if ((newfd = dup(bfd)) >= 0) {
705*7cdcb8abSjbeck 		fclose(bfp);
706*7cdcb8abSjbeck 		bfd = newfd;
707*7cdcb8abSjbeck 	}
708*7cdcb8abSjbeck 	if ((newfd = dup(hfd)) >= 0) {
709*7cdcb8abSjbeck 		fclose(hfp);
710*7cdcb8abSjbeck 		hfd = newfd;
711*7cdcb8abSjbeck 	}
7127c478bd9Sstevel@tonic-gate 	(void) time(&tval);
7137c478bd9Sstevel@tonic-gate 	(void) snprintf(unix_from_line, sizeof (unix_from_line), "From %s %s",
7147c478bd9Sstevel@tonic-gate 	    from, ctime(&tval));
7157c478bd9Sstevel@tonic-gate 	ulen = strlen(unix_from_line);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate static void
7191eca9c5fSjbeck handle_error(err_num, bouncequota, path)
7201eca9c5fSjbeck 	int err_num;
7211eca9c5fSjbeck 	bool bouncequota;
7221eca9c5fSjbeck 	char *path;
7231eca9c5fSjbeck {
7241eca9c5fSjbeck #ifdef EDQUOT
7251eca9c5fSjbeck 	if (err_num == EDQUOT && bouncequota) {
7261eca9c5fSjbeck 		mailerr("552 5.2.2", "%s: %s", path, sm_errstring(err_num));
7271eca9c5fSjbeck 	} else
7281eca9c5fSjbeck #endif /* EDQUOT */
7291eca9c5fSjbeck 		mailerr("450 4.2.0", "%s: %s", path, sm_errstring(err_num));
7301eca9c5fSjbeck }
7311eca9c5fSjbeck 
7321eca9c5fSjbeck static void
7337c478bd9Sstevel@tonic-gate deliver(hfd, bfd, name, bouncequota)
7347c478bd9Sstevel@tonic-gate 	int hfd;
7357c478bd9Sstevel@tonic-gate 	int bfd;
7367c478bd9Sstevel@tonic-gate 	char *name;
7377c478bd9Sstevel@tonic-gate 	bool bouncequota;
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	struct stat fsb, sb;
7407c478bd9Sstevel@tonic-gate 	int mbfd = -1, nr, nw = 0, off;
7417c478bd9Sstevel@tonic-gate 	char biffmsg[100], buf[8*1024], path[MAXPATHLEN];
7427c478bd9Sstevel@tonic-gate 	off_t curoff, cursize;
7437c478bd9Sstevel@tonic-gate 	int len;
7447c478bd9Sstevel@tonic-gate 	struct passwd *pw = NULL;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/*
7477c478bd9Sstevel@tonic-gate  	* Disallow delivery to unknown names -- special mailboxes
7487c478bd9Sstevel@tonic-gate  	* can be handled in the sendmail aliases file.
7497c478bd9Sstevel@tonic-gate  	*/
7507c478bd9Sstevel@tonic-gate 	if ((pw = getpwnam(name)) == NULL) {
7517c478bd9Sstevel@tonic-gate 		eval = EX_TEMPFAIL;
7527c478bd9Sstevel@tonic-gate 		mailerr("451 4.3.0", "cannot lookup name: %s", name);
7537c478bd9Sstevel@tonic-gate 		return;
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 	endpwent();
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (sigterm_caught) {
7587c478bd9Sstevel@tonic-gate 		mailerr("451 4.3.0", "shutting down");
7597c478bd9Sstevel@tonic-gate 		return;
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/* mailbox may be NFS mounted, seteuid to user */
7637c478bd9Sstevel@tonic-gate 	targ_uid = pw->pw_uid;
7647c478bd9Sstevel@tonic-gate 	(void) seteuid(targ_uid);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if ((saved_uid != 0) && (src_uid != targ_uid)) {
7677c478bd9Sstevel@tonic-gate 		/*
7687c478bd9Sstevel@tonic-gate 		 * If saved_uid == 0 (root), anything is OK; this is
7697c478bd9Sstevel@tonic-gate 		 * as it should be.  But to prevent a random user from
7707c478bd9Sstevel@tonic-gate 		 * calling "mail.local foo" in an attempt to hijack
7717c478bd9Sstevel@tonic-gate 		 * foo's mail-box, make sure src_uid == targ_uid o/w.
7727c478bd9Sstevel@tonic-gate 		 */
7737c478bd9Sstevel@tonic-gate 		warn("%s: wrong owner (is %d, should be %d)",
7747c478bd9Sstevel@tonic-gate 			name, src_uid, targ_uid);
7757c478bd9Sstevel@tonic-gate 		eval = EX_CANTCREAT;
7767c478bd9Sstevel@tonic-gate 		return;
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	path[0] = '\0';
7807c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s/%s", _PATH_MAILDIR, name);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/*
7837c478bd9Sstevel@tonic-gate 	 * If the mailbox is linked or a symlink, fail.  There's an obvious
7847c478bd9Sstevel@tonic-gate 	 * race here, that the file was replaced with a symbolic link after
7857c478bd9Sstevel@tonic-gate 	 * the lstat returned, but before the open.  We attempt to detect
7867c478bd9Sstevel@tonic-gate 	 * this by comparing the original stat information and information
7877c478bd9Sstevel@tonic-gate 	 * returned by an fstat of the file descriptor returned by the open.
7887c478bd9Sstevel@tonic-gate 	 *
7897c478bd9Sstevel@tonic-gate 	 * NB: this is a symptom of a larger problem, that the mail spooling
7907c478bd9Sstevel@tonic-gate 	 * directory is writeable by the wrong users.  If that directory is
7917c478bd9Sstevel@tonic-gate 	 * writeable, system security is compromised for other reasons, and
7927c478bd9Sstevel@tonic-gate 	 * it cannot be fixed here.
7937c478bd9Sstevel@tonic-gate 	 *
7947c478bd9Sstevel@tonic-gate 	 * If we created the mailbox, set the owner/group.  If that fails,
7957c478bd9Sstevel@tonic-gate 	 * just return.  Another process may have already opened it, so we
7967c478bd9Sstevel@tonic-gate 	 * can't unlink it.  Historically, binmail set the owner/group at
7977c478bd9Sstevel@tonic-gate 	 * each mail delivery.  We no longer do this, assuming that if the
7987c478bd9Sstevel@tonic-gate 	 * ownership or permissions were changed there was a reason.
7997c478bd9Sstevel@tonic-gate 	 *
8007c478bd9Sstevel@tonic-gate 	 * XXX
8017c478bd9Sstevel@tonic-gate 	 * open(2) should support flock'ing the file.
8027c478bd9Sstevel@tonic-gate 	 */
8037c478bd9Sstevel@tonic-gate tryagain:
8047c478bd9Sstevel@tonic-gate 	/* should check lock status, but... maillock return no value */
8057c478bd9Sstevel@tonic-gate 	maillock(name, 10);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if (sigterm_caught) {
8087c478bd9Sstevel@tonic-gate 		mailerr("451 4.3.0", "shutting down");
8097c478bd9Sstevel@tonic-gate 		goto err0;
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (lstat(path, &sb)) {
8137c478bd9Sstevel@tonic-gate 		mbfd = open(path, O_APPEND|O_CREAT|O_EXCL|O_WRONLY,
8147c478bd9Sstevel@tonic-gate 				S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
8157c478bd9Sstevel@tonic-gate 		if (mbfd != -1)
8167c478bd9Sstevel@tonic-gate 			(void) fchmod(mbfd, 0660);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		if (mbfd == -1) {
8207c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
8217c478bd9Sstevel@tonic-gate 				mailunlock();
8227c478bd9Sstevel@tonic-gate 				goto tryagain;
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 	} else if (sb.st_nlink != 1) {
8267c478bd9Sstevel@tonic-gate 		mailerr("550 5.2.0", "%s: too many links", path);
8277c478bd9Sstevel@tonic-gate 		goto err0;
8287c478bd9Sstevel@tonic-gate 	} else if (!S_ISREG(sb.st_mode)) {
8297c478bd9Sstevel@tonic-gate 		mailerr("550 5.2.0", "%s: irregular file", path);
8307c478bd9Sstevel@tonic-gate 		goto err0;
8317c478bd9Sstevel@tonic-gate 	} else {
8327c478bd9Sstevel@tonic-gate 		mbfd = open(path, O_APPEND|O_WRONLY, 0);
8337c478bd9Sstevel@tonic-gate 		if (mbfd != -1 &&
8347c478bd9Sstevel@tonic-gate 		    (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
8357c478bd9Sstevel@tonic-gate 		    S_ISLNK(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
8367c478bd9Sstevel@tonic-gate 		    sb.st_ino != fsb.st_ino)) {
8377c478bd9Sstevel@tonic-gate 			eval = EX_TEMPFAIL;
8387c478bd9Sstevel@tonic-gate 			mailerr("550 5.2.0",
8397c478bd9Sstevel@tonic-gate 				"%s: fstat: file changed after open", path);
8407c478bd9Sstevel@tonic-gate 			goto err1;
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	if (mbfd == -1) {
8457c478bd9Sstevel@tonic-gate 		mailerr("450 4.2.0", "%s: %s", path, strerror(errno));
8467c478bd9Sstevel@tonic-gate 		goto err0;
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (sigterm_caught) {
8507c478bd9Sstevel@tonic-gate 		mailerr("451 4.3.0", "shutting down");
8517c478bd9Sstevel@tonic-gate 		goto err0;
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	/* Get the starting offset of the new message for biff. */
8557c478bd9Sstevel@tonic-gate 	curoff = lseek(mbfd, (off_t)0, SEEK_END);
8567c478bd9Sstevel@tonic-gate 	(void) snprintf(biffmsg, sizeof (biffmsg), "%s@%ld\n", name, curoff);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/* Copy the message into the file. */
8597c478bd9Sstevel@tonic-gate 	if (lseek(hfd, (off_t)0, SEEK_SET) == (off_t)-1) {
8607c478bd9Sstevel@tonic-gate 		mailerr("450 4.2.0", "temporary file: %s", strerror(errno));
8617c478bd9Sstevel@tonic-gate 		goto err1;
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 	/* Copy the message into the file. */
8647c478bd9Sstevel@tonic-gate 	if (lseek(bfd, (off_t)0, SEEK_SET) == (off_t)-1) {
8657c478bd9Sstevel@tonic-gate 		mailerr("450 4.2.0", "temporary file: %s", strerror(errno));
8667c478bd9Sstevel@tonic-gate 		goto err1;
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 	if ((write(mbfd, unix_from_line, ulen)) != ulen) {
8691eca9c5fSjbeck 		handle_error(errno, bouncequota, path);
8707c478bd9Sstevel@tonic-gate 		goto err2;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (sigterm_caught) {
8747c478bd9Sstevel@tonic-gate 		mailerr("451 4.3.0", "shutting down");
8757c478bd9Sstevel@tonic-gate 		goto err2;
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	while ((nr = read(hfd, buf, sizeof (buf))) > 0)
8797c478bd9Sstevel@tonic-gate 		for (off = 0; off < nr; nr -= nw, off += nw)
8807c478bd9Sstevel@tonic-gate 			if ((nw = write(mbfd, buf + off, nr)) < 0)
8817c478bd9Sstevel@tonic-gate 			{
8821eca9c5fSjbeck 				handle_error(errno, bouncequota, path);
8837c478bd9Sstevel@tonic-gate 				goto err2;
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 	if (nr < 0) {
8861eca9c5fSjbeck 		handle_error(errno, bouncequota, path);
8877c478bd9Sstevel@tonic-gate 		goto err2;
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (sigterm_caught) {
8917c478bd9Sstevel@tonic-gate 		mailerr("451 4.3.0", "shutting down");
8927c478bd9Sstevel@tonic-gate 		goto err2;
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "Content-Length: %d\n\n",
8967c478bd9Sstevel@tonic-gate 	    content_length);
8977c478bd9Sstevel@tonic-gate 	len = strlen(buf);
8987c478bd9Sstevel@tonic-gate 	if (write(mbfd, buf, len) != len) {
8991eca9c5fSjbeck 		handle_error(errno, bouncequota, path);
9007c478bd9Sstevel@tonic-gate 		goto err2;
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if (sigterm_caught) {
9047c478bd9Sstevel@tonic-gate 		mailerr("451 4.3.0", "shutting down");
9057c478bd9Sstevel@tonic-gate 		goto err2;
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	while ((nr = read(bfd, buf, sizeof (buf))) > 0) {
9097c478bd9Sstevel@tonic-gate 		for (off = 0; off < nr; nr -= nw, off += nw)
9107c478bd9Sstevel@tonic-gate 			if ((nw = write(mbfd, buf + off, nr)) < 0) {
9111eca9c5fSjbeck 				handle_error(errno, bouncequota, path);
9127c478bd9Sstevel@tonic-gate 				goto err2;
9137c478bd9Sstevel@tonic-gate 			}
9147c478bd9Sstevel@tonic-gate 		if (sigterm_caught) {
9157c478bd9Sstevel@tonic-gate 			mailerr("451 4.3.0", "shutting down");
9167c478bd9Sstevel@tonic-gate 			goto err2;
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	if (nr < 0) {
9201eca9c5fSjbeck 		handle_error(errno, bouncequota, path);
9217c478bd9Sstevel@tonic-gate 		goto err2;
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/* Flush to disk, don't wait for update. */
9257c478bd9Sstevel@tonic-gate 	if (fsync(mbfd)) {
9261eca9c5fSjbeck 		handle_error(errno, bouncequota, path);
9277c478bd9Sstevel@tonic-gate err2:		if (mbfd >= 0)
9287c478bd9Sstevel@tonic-gate 			(void)ftruncate(mbfd, curoff);
9297c478bd9Sstevel@tonic-gate err1:		(void)close(mbfd);
9307c478bd9Sstevel@tonic-gate err0:		mailunlock();
9317c478bd9Sstevel@tonic-gate 		(void)seteuid(saved_uid);
9327c478bd9Sstevel@tonic-gate 		return;
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	**  Save the current size so if the close() fails below
9377c478bd9Sstevel@tonic-gate 	**  we can make sure no other process has changed the mailbox
9387c478bd9Sstevel@tonic-gate 	**  between the failed close and the re-open()/re-lock().
9397c478bd9Sstevel@tonic-gate 	**  If something else has changed the size, we shouldn't
9407c478bd9Sstevel@tonic-gate 	**  try to truncate it as we may do more harm then good
9417c478bd9Sstevel@tonic-gate 	**  (e.g., truncate a later message delivery).
9427c478bd9Sstevel@tonic-gate 	*/
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if (fstat(mbfd, &sb) < 0)
9457c478bd9Sstevel@tonic-gate 		cursize = 0;
9467c478bd9Sstevel@tonic-gate 	else
9477c478bd9Sstevel@tonic-gate 		cursize = sb.st_size;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	/* Close and check -- NFS doesn't write until the close. */
9507c478bd9Sstevel@tonic-gate 	if (close(mbfd))
9517c478bd9Sstevel@tonic-gate 	{
9521eca9c5fSjbeck 		handle_error(errno, bouncequota, path);
9537c478bd9Sstevel@tonic-gate 		mbfd = open(path, O_WRONLY, 0);
9547c478bd9Sstevel@tonic-gate 		if (mbfd < 0 ||
9557c478bd9Sstevel@tonic-gate 		    cursize == 0
9567c478bd9Sstevel@tonic-gate 		    || flock(mbfd, LOCK_EX) < 0 ||
9577c478bd9Sstevel@tonic-gate 		    fstat(mbfd, &sb) < 0 ||
9587c478bd9Sstevel@tonic-gate 		    sb.st_size != cursize ||
9597c478bd9Sstevel@tonic-gate 		    sb.st_nlink != 1 ||
9607c478bd9Sstevel@tonic-gate 		    !S_ISREG(sb.st_mode) ||
9617c478bd9Sstevel@tonic-gate 		    sb.st_dev != fsb.st_dev ||
9627c478bd9Sstevel@tonic-gate 		    sb.st_ino != fsb.st_ino ||
9637c478bd9Sstevel@tonic-gate 		    sb.st_uid != fsb.st_uid)
9647c478bd9Sstevel@tonic-gate 		{
9657c478bd9Sstevel@tonic-gate 			/* Don't use a bogus file */
9667c478bd9Sstevel@tonic-gate 			if (mbfd >= 0)
9677c478bd9Sstevel@tonic-gate 			{
9687c478bd9Sstevel@tonic-gate 				(void) close(mbfd);
9697c478bd9Sstevel@tonic-gate 				mbfd = -1;
9707c478bd9Sstevel@tonic-gate 			}
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 		/* Attempt to truncate back to pre-write size */
9747c478bd9Sstevel@tonic-gate 		goto err2;
9757c478bd9Sstevel@tonic-gate 	} else
9767c478bd9Sstevel@tonic-gate 		notifybiff(biffmsg);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	mailunlock();
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	(void)seteuid(saved_uid);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if (lmtpmode) {
9837c478bd9Sstevel@tonic-gate 		printf("250 2.1.5 %s OK\r\n", name);
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate static void
9887c478bd9Sstevel@tonic-gate notifybiff(msg)
9897c478bd9Sstevel@tonic-gate 	char *msg;
9907c478bd9Sstevel@tonic-gate {
9917c478bd9Sstevel@tonic-gate 	static struct sockaddr_in addr;
9927c478bd9Sstevel@tonic-gate 	static int f = -1;
9937c478bd9Sstevel@tonic-gate 	struct hostent *hp;
9947c478bd9Sstevel@tonic-gate 	struct servent *sp;
9957c478bd9Sstevel@tonic-gate 	int len;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (msg == NULL) {
9987c478bd9Sstevel@tonic-gate 		/* Be silent if biff service not available. */
9997c478bd9Sstevel@tonic-gate 		if ((sp = getservbyname("biff", "udp")) == NULL)
10007c478bd9Sstevel@tonic-gate 			return;
10017c478bd9Sstevel@tonic-gate 		if ((hp = gethostbyname("localhost")) == NULL) {
10027c478bd9Sstevel@tonic-gate 			warn("localhost: %s", strerror(errno));
10037c478bd9Sstevel@tonic-gate 			return;
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 		addr.sin_family = hp->h_addrtype;
10067c478bd9Sstevel@tonic-gate 		(void) memmove(&addr.sin_addr, hp->h_addr, hp->h_length);
10077c478bd9Sstevel@tonic-gate 		addr.sin_port = sp->s_port;
10087c478bd9Sstevel@tonic-gate 		return;
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	if (addr.sin_family == 0)
10127c478bd9Sstevel@tonic-gate 		return; /* did not initialize */
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
10157c478bd9Sstevel@tonic-gate 		warn("socket: %s", strerror(errno));
10167c478bd9Sstevel@tonic-gate 		return;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	len = strlen(msg) + 1;
10197c478bd9Sstevel@tonic-gate 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof (addr))
10207c478bd9Sstevel@tonic-gate 	    != len)
10217c478bd9Sstevel@tonic-gate 		warn("sendto biff: %s", strerror(errno));
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate static void
10257c478bd9Sstevel@tonic-gate usage()
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	eval = EX_USAGE;
10287c478bd9Sstevel@tonic-gate 	err("usage: mail.local [-l] [-f from] user ...");
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate static void
10327c478bd9Sstevel@tonic-gate /*VARARGS2*/
10337c478bd9Sstevel@tonic-gate #ifdef __STDC__
10347c478bd9Sstevel@tonic-gate mailerr(const char *hdr, const char *fmt, ...)
10357c478bd9Sstevel@tonic-gate #else
10367c478bd9Sstevel@tonic-gate mailerr(hdr, fmt, va_alist)
10377c478bd9Sstevel@tonic-gate 	const char *hdr;
10387c478bd9Sstevel@tonic-gate 	const char *fmt;
10397c478bd9Sstevel@tonic-gate 	va_dcl
10407c478bd9Sstevel@tonic-gate #endif
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	va_list ap;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate #ifdef __STDC__
10457c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
10467c478bd9Sstevel@tonic-gate #else
10477c478bd9Sstevel@tonic-gate 	va_start(ap);
10487c478bd9Sstevel@tonic-gate #endif
10497c478bd9Sstevel@tonic-gate 	if (lmtpmode)
10507c478bd9Sstevel@tonic-gate 	{
10517c478bd9Sstevel@tonic-gate 		if (hdr != NULL)
10527c478bd9Sstevel@tonic-gate 			printf("%s ", hdr);
10537c478bd9Sstevel@tonic-gate 		vprintf(fmt, ap);
10547c478bd9Sstevel@tonic-gate 		printf("\r\n");
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 	else
10577c478bd9Sstevel@tonic-gate 	{
10587c478bd9Sstevel@tonic-gate 		e_to_sys(errno);
10597c478bd9Sstevel@tonic-gate 		vwarn(fmt, ap);
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate static void
10647c478bd9Sstevel@tonic-gate /*VARARGS1*/
10657c478bd9Sstevel@tonic-gate #ifdef __STDC__
10667c478bd9Sstevel@tonic-gate err(const char *fmt, ...)
10677c478bd9Sstevel@tonic-gate #else
10687c478bd9Sstevel@tonic-gate err(fmt, va_alist)
10697c478bd9Sstevel@tonic-gate 	const char *fmt;
10707c478bd9Sstevel@tonic-gate 	va_dcl
10717c478bd9Sstevel@tonic-gate #endif
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	va_list ap;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate #ifdef __STDC__
10767c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
10777c478bd9Sstevel@tonic-gate #else
10787c478bd9Sstevel@tonic-gate 	va_start(ap);
10797c478bd9Sstevel@tonic-gate #endif
10807c478bd9Sstevel@tonic-gate 	vwarn(fmt, ap);
10817c478bd9Sstevel@tonic-gate 	va_end(ap);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	exit(eval);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate static void
10877c478bd9Sstevel@tonic-gate /*VARARGS1*/
10887c478bd9Sstevel@tonic-gate #ifdef __STDC__
10897c478bd9Sstevel@tonic-gate warn(const char *fmt, ...)
10907c478bd9Sstevel@tonic-gate #else
10917c478bd9Sstevel@tonic-gate warn(fmt, va_alist)
10927c478bd9Sstevel@tonic-gate 	const char *fmt;
10937c478bd9Sstevel@tonic-gate 	va_dcl
10947c478bd9Sstevel@tonic-gate #endif
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate 	va_list ap;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate #ifdef __STDC__
10997c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
11007c478bd9Sstevel@tonic-gate #else
11017c478bd9Sstevel@tonic-gate 	va_start(ap);
11027c478bd9Sstevel@tonic-gate #endif
11037c478bd9Sstevel@tonic-gate 	vwarn(fmt, ap);
11047c478bd9Sstevel@tonic-gate 	va_end(ap);
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate static void
11087c478bd9Sstevel@tonic-gate vwarn(fmt, ap)
11097c478bd9Sstevel@tonic-gate 	const char *fmt;
11107c478bd9Sstevel@tonic-gate 	va_list ap;
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	/*
11137c478bd9Sstevel@tonic-gate 	 * Log the message to stderr.
11147c478bd9Sstevel@tonic-gate 	 *
11157c478bd9Sstevel@tonic-gate 	 * Don't use LOG_PERROR as an openlog() flag to do this,
11167c478bd9Sstevel@tonic-gate 	 * it's not portable enough.
11177c478bd9Sstevel@tonic-gate 	 */
11187c478bd9Sstevel@tonic-gate 	if (eval != EX_USAGE)
11197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mail.local: ");
11207c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
11217c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	/* Log the message to syslog. */
11247c478bd9Sstevel@tonic-gate 	vsyslog(LOG_ERR, fmt, ap);
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * e_to_sys --
11297c478bd9Sstevel@tonic-gate  *	Guess which errno's are temporary.  Gag me.
11307c478bd9Sstevel@tonic-gate  */
11317c478bd9Sstevel@tonic-gate static void
11327c478bd9Sstevel@tonic-gate e_to_sys(num)
11337c478bd9Sstevel@tonic-gate 	int num;
11347c478bd9Sstevel@tonic-gate {
11357c478bd9Sstevel@tonic-gate 	/* Temporary failures override hard errors. */
11367c478bd9Sstevel@tonic-gate 	if (eval == EX_TEMPFAIL)
11377c478bd9Sstevel@tonic-gate 		return;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	switch (num)		/* Hopefully temporary errors. */
11407c478bd9Sstevel@tonic-gate 	{
11417c478bd9Sstevel@tonic-gate #ifdef EDQUOT
11427c478bd9Sstevel@tonic-gate 	case EDQUOT:		/* Disc quota exceeded */
11437c478bd9Sstevel@tonic-gate 		if (bouncequota)
11447c478bd9Sstevel@tonic-gate 		{
11457c478bd9Sstevel@tonic-gate 			eval = EX_UNAVAILABLE;
11467c478bd9Sstevel@tonic-gate 			break;
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
11497c478bd9Sstevel@tonic-gate #endif /* EDQUOT */
11507c478bd9Sstevel@tonic-gate #ifdef EAGAIN
11517c478bd9Sstevel@tonic-gate 	case EAGAIN:		/* Resource temporarily unavailable */
11527c478bd9Sstevel@tonic-gate #endif
11537c478bd9Sstevel@tonic-gate #ifdef EBUSY
11547c478bd9Sstevel@tonic-gate 	case EBUSY:		/* Device busy */
11557c478bd9Sstevel@tonic-gate #endif
11567c478bd9Sstevel@tonic-gate #ifdef EPROCLIM
11577c478bd9Sstevel@tonic-gate 	case EPROCLIM:		/* Too many processes */
11587c478bd9Sstevel@tonic-gate #endif
11597c478bd9Sstevel@tonic-gate #ifdef EUSERS
11607c478bd9Sstevel@tonic-gate 	case EUSERS:		/* Too many users */
11617c478bd9Sstevel@tonic-gate #endif
11627c478bd9Sstevel@tonic-gate #ifdef ECONNABORTED
11637c478bd9Sstevel@tonic-gate 	case ECONNABORTED:	/* Software caused connection abort */
11647c478bd9Sstevel@tonic-gate #endif
11657c478bd9Sstevel@tonic-gate #ifdef ECONNREFUSED
11667c478bd9Sstevel@tonic-gate 	case ECONNREFUSED:	/* Connection refused */
11677c478bd9Sstevel@tonic-gate #endif
11687c478bd9Sstevel@tonic-gate #ifdef ECONNRESET
11697c478bd9Sstevel@tonic-gate 	case ECONNRESET:	/* Connection reset by peer */
11707c478bd9Sstevel@tonic-gate #endif
11717c478bd9Sstevel@tonic-gate #ifdef EDEADLK
11727c478bd9Sstevel@tonic-gate 	case EDEADLK:		/* Resource deadlock avoided */
11737c478bd9Sstevel@tonic-gate #endif
11747c478bd9Sstevel@tonic-gate #ifdef EFBIG
11757c478bd9Sstevel@tonic-gate 	case EFBIG:		/* File too large */
11767c478bd9Sstevel@tonic-gate #endif
11777c478bd9Sstevel@tonic-gate #ifdef EHOSTDOWN
11787c478bd9Sstevel@tonic-gate 	case EHOSTDOWN:		/* Host is down */
11797c478bd9Sstevel@tonic-gate #endif
11807c478bd9Sstevel@tonic-gate #ifdef EHOSTUNREACH
11817c478bd9Sstevel@tonic-gate 	case EHOSTUNREACH:	/* No route to host */
11827c478bd9Sstevel@tonic-gate #endif
11837c478bd9Sstevel@tonic-gate #ifdef EMFILE
11847c478bd9Sstevel@tonic-gate 	case EMFILE:		/* Too many open files */
11857c478bd9Sstevel@tonic-gate #endif
11867c478bd9Sstevel@tonic-gate #ifdef ENETDOWN
11877c478bd9Sstevel@tonic-gate 	case ENETDOWN:		/* Network is down */
11887c478bd9Sstevel@tonic-gate #endif
11897c478bd9Sstevel@tonic-gate #ifdef ENETRESET
11907c478bd9Sstevel@tonic-gate 	case ENETRESET:		/* Network dropped connection on reset */
11917c478bd9Sstevel@tonic-gate #endif
11927c478bd9Sstevel@tonic-gate #ifdef ENETUNREACH
11937c478bd9Sstevel@tonic-gate 	case ENETUNREACH:	/* Network is unreachable */
11947c478bd9Sstevel@tonic-gate #endif
11957c478bd9Sstevel@tonic-gate #ifdef ENFILE
11967c478bd9Sstevel@tonic-gate 	case ENFILE:		/* Too many open files in system */
11977c478bd9Sstevel@tonic-gate #endif
11987c478bd9Sstevel@tonic-gate #ifdef ENOBUFS
11997c478bd9Sstevel@tonic-gate 	case ENOBUFS:		/* No buffer space available */
12007c478bd9Sstevel@tonic-gate #endif
12017c478bd9Sstevel@tonic-gate #ifdef ENOMEM
12027c478bd9Sstevel@tonic-gate 	case ENOMEM:		/* Cannot allocate memory */
12037c478bd9Sstevel@tonic-gate #endif
12047c478bd9Sstevel@tonic-gate #ifdef ENOSPC
12057c478bd9Sstevel@tonic-gate 	case ENOSPC:		/* No space left on device */
12067c478bd9Sstevel@tonic-gate #endif
12077c478bd9Sstevel@tonic-gate #ifdef EROFS
12087c478bd9Sstevel@tonic-gate 	case EROFS:		/* Read-only file system */
12097c478bd9Sstevel@tonic-gate #endif
12107c478bd9Sstevel@tonic-gate #ifdef ESTALE
12117c478bd9Sstevel@tonic-gate 	case ESTALE:		/* Stale NFS file handle */
12127c478bd9Sstevel@tonic-gate #endif
12137c478bd9Sstevel@tonic-gate #ifdef ETIMEDOUT
12147c478bd9Sstevel@tonic-gate 	case ETIMEDOUT:		/* Connection timed out */
12157c478bd9Sstevel@tonic-gate #endif
12167c478bd9Sstevel@tonic-gate #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
12177c478bd9Sstevel@tonic-gate 	case EWOULDBLOCK:	/* Operation would block. */
12187c478bd9Sstevel@tonic-gate #endif
12197c478bd9Sstevel@tonic-gate 		eval = EX_TEMPFAIL;
12207c478bd9Sstevel@tonic-gate 		break;
12217c478bd9Sstevel@tonic-gate 	default:
12227c478bd9Sstevel@tonic-gate 		eval = EX_UNAVAILABLE;
12237c478bd9Sstevel@tonic-gate 		break;
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate }
1226