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