145976Sbostic /*-
245976Sbostic  * Copyright (c) 1990 The Regents of the University of California.
345976Sbostic  * All rights reserved.
445976Sbostic  *
545976Sbostic  * %sccs.include.redist.c%
645976Sbostic  */
745976Sbostic 
814465Ssam #ifndef lint
945976Sbostic char copyright[] =
1045976Sbostic "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
1145976Sbostic  All rights reserved.\n";
1245976Sbostic #endif /* not lint */
1314465Ssam 
1445976Sbostic #ifndef lint
15*47075Sdonn static char sccsid[] = "@(#)mail.local.c	5.5 (Berkeley) 03/07/91";
1645976Sbostic #endif /* not lint */
1745976Sbostic 
1829763Skarels #include <sys/param.h>
1916731Sralph #include <sys/stat.h>
2045976Sbostic #include <sys/socket.h>
2145976Sbostic #include <sys/errno.h>
2245976Sbostic #include <netinet/in.h>
2346672Sbostic #include <syslog.h>
2446672Sbostic #include <fcntl.h>
2545976Sbostic #include <netdb.h>
2645976Sbostic #include <pwd.h>
2745976Sbostic #include <time.h>
2846672Sbostic #include <unistd.h>
29579Sroot #include <stdio.h>
3046672Sbostic #include <stdlib.h>
3145976Sbostic #include <string.h>
3237465Sbostic #include "pathnames.h"
33579Sroot 
3445976Sbostic #define	FATAL		1
3545976Sbostic #define	NOTFATAL	0
36579Sroot 
37579Sroot main(argc, argv)
3845976Sbostic 	int argc;
3916731Sralph 	char **argv;
40579Sroot {
4145976Sbostic 	extern int optind;
4245976Sbostic 	extern char *optarg;
4345976Sbostic 	struct passwd *pw;
4445976Sbostic 	int ch, fd, eval;
4545976Sbostic 	uid_t uid;
4645976Sbostic 	char *from;
47579Sroot 
4845976Sbostic 	openlog("mail.local", LOG_PERROR, LOG_MAIL);
4916731Sralph 
5045976Sbostic 	from = NULL;
5145976Sbostic 	while ((ch = getopt(argc, argv, "df:r:")) != EOF)
5245976Sbostic 		switch(ch) {
5345976Sbostic 		case 'd':		/* backward compatible */
5416731Sralph 			break;
5516731Sralph 		case 'f':
5645976Sbostic 		case 'r':		/* backward compatible */
5745976Sbostic 			if (from)
5845976Sbostic 			    error(FATAL, "multiple -f options.");
5945976Sbostic 			from = optarg;
60579Sroot 			break;
6145976Sbostic 		case '?':
6216731Sralph 		default:
6345976Sbostic 			usage();
6416731Sralph 		}
6545976Sbostic 	argc -= optind;
6645976Sbostic 	argv += optind;
67579Sroot 
6845976Sbostic 	if (!*argv)
6945976Sbostic 		usage();
70579Sroot 
7145976Sbostic 	/*
7245976Sbostic 	 * If from not specified, use the name from getlogin() if the
7345976Sbostic 	 * uid matches, otherwise, use the name from the password file
7445976Sbostic 	 * corresponding to the uid.
7545976Sbostic 	 */
7645976Sbostic 	uid = getuid();
7746034Sbostic 	if (!from && (!(from = getlogin()) ||
7846034Sbostic 	    !(pw = getpwnam(from)) || pw->pw_uid != uid))
7945976Sbostic 		from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
80579Sroot 
8145976Sbostic 	fd = store(from);
8245976Sbostic 	for (eval = 0; *argv; ++argv)
8345976Sbostic 		eval |= deliver(fd, *argv);
8445976Sbostic 	exit(eval);
85579Sroot }
86579Sroot 
8745976Sbostic store(from)
8845976Sbostic 	char *from;
89579Sroot {
9045976Sbostic 	FILE *fp;
9145976Sbostic 	time_t tval;
9245976Sbostic 	int fd, eline;
9345976Sbostic 	char *tn, line[2048];
94579Sroot 
95*47075Sdonn 	tn = strdup(_PATH_LOCTMP);
9645976Sbostic 	if ((fd = mkstemp(tn)) == -1 || !(fp = fdopen(fd, "w+")))
9745976Sbostic 		error(FATAL, "unable to open temporary file.");
9845976Sbostic 	(void)unlink(tn);
99*47075Sdonn 	free(tn);
100579Sroot 
10145976Sbostic 	(void)time(&tval);
10245976Sbostic 	(void)fprintf(fp, "From %s %s", from, ctime(&tval));
103579Sroot 
10445976Sbostic 	line[0] = '\0';
10545976Sbostic 	for (eline = 1; fgets(line, sizeof(line), stdin);) {
10645976Sbostic 		if (line[0] == '\n')
10745976Sbostic 			eline = 1;
10845976Sbostic 		else {
10945976Sbostic 			if (eline && line[0] == 'F' && !bcmp(line, "From ", 5))
11045976Sbostic 				(void)putc('>', fp);
11145976Sbostic 			eline = 0;
11245976Sbostic 		}
11345976Sbostic 		(void)fprintf(fp, "%s", line);
11445976Sbostic 		if (ferror(fp))
11545976Sbostic 			break;
116579Sroot 	}
117579Sroot 
11845976Sbostic 	/* If message not newline terminated, need an extra. */
11945976Sbostic 	if (!index(line, '\n'))
12045976Sbostic 		(void)putc('\n', fp);
12145976Sbostic 	/* Output a newline; note, empty messages are allowed. */
12245976Sbostic 	(void)putc('\n', fp);
12311893Seric 
12445976Sbostic 	(void)fflush(fp);
12545976Sbostic 	if (ferror(fp))
12645976Sbostic 		error(FATAL, "temporary file write error.");
12745976Sbostic 	return(fd);
128579Sroot }
129579Sroot 
13045976Sbostic deliver(fd, name)
13145976Sbostic 	int fd;
13245976Sbostic 	char *name;
133579Sroot {
13445976Sbostic 	struct stat sb;
13545976Sbostic 	struct passwd *pw;
13645976Sbostic 	int created, mbfd, nr, nw, off, rval;
13745976Sbostic 	char biffmsg[100], buf[8*1024], path[MAXPATHLEN];
13845976Sbostic 	off_t curoff, lseek();
139579Sroot 
14045976Sbostic 	/*
14145976Sbostic 	 * Disallow delivery to unknown names -- special mailboxes can be
14245976Sbostic 	 * handled in the sendmail aliases file.
14345976Sbostic 	 */
14445976Sbostic 	if (!(pw = getpwnam(name))) {
14545976Sbostic 		error(NOTFATAL, "unknown name: %s.", name);
14645976Sbostic 		return(1);
14745976Sbostic 	}
148579Sroot 
14945976Sbostic 	(void)sprintf(path, "%s/%s", _PATH_MAILDIR, name);
150579Sroot 
15145976Sbostic 	if (!(created = lstat(path, &sb)) &&
15245976Sbostic 	    (sb.st_nlink != 1 || S_ISLNK(sb.st_mode))) {
15345976Sbostic 		error(NOTFATAL, "%s: linked file.", path);
15445976Sbostic 		return(1);
15516731Sralph 	}
156579Sroot 
157579Sroot 	/*
15845976Sbostic 	 * There's a race here -- two processes think they both created
15945976Sbostic 	 * the file.  This means the file cannot be unlinked.
160579Sroot 	 */
16145976Sbostic 	if ((mbfd =
16245976Sbostic 	    open(path, O_APPEND|O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
16345976Sbostic 		error(NOTFATAL, "%s: %s.", path, strerror(errno));
16445976Sbostic 		return(1);
16545976Sbostic 	}
166579Sroot 
16745976Sbostic 	rval = 0;
16845976Sbostic 	/* XXX: Open should allow flock'ing the file; see 4.4BSD. */
16945976Sbostic 	if (flock(mbfd, LOCK_EX)) {
17045976Sbostic 		error(NOTFATAL, "%s: %s.", path, strerror(errno));
17145976Sbostic 		rval = 1;
17245976Sbostic 		goto bad;
17345976Sbostic 	}
174579Sroot 
17545976Sbostic 	curoff = lseek(mbfd, 0L, SEEK_END);
17645976Sbostic 	(void)sprintf(biffmsg, "%s@%ld\n", name, curoff);
17745976Sbostic 	if (lseek(fd, 0L, SEEK_SET) == (off_t)-1) {
17845976Sbostic 		error(FATAL, "temporary file: %s.", strerror(errno));
17945976Sbostic 		rval = 1;
18045976Sbostic 		goto bad;
18145976Sbostic 	}
182579Sroot 
18345976Sbostic 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
18445976Sbostic 		for (off = 0; off < nr; nr -= nw, off += nw)
18545976Sbostic 			if ((nw = write(mbfd, buf + off, nr)) < 0) {
18645976Sbostic 				error(NOTFATAL,
18745976Sbostic 				    "%s: %s.", path, strerror(errno));
18845976Sbostic 				goto trunc;
18945976Sbostic 			}
19045976Sbostic 	if (nr < 0) {
19145976Sbostic 		error(FATAL, "temporary file: %s.", strerror(errno));
19245976Sbostic trunc:		(void)ftruncate(mbfd, curoff);
19345976Sbostic 		rval = 1;
194579Sroot 	}
195579Sroot 
19645976Sbostic 	/*
19745976Sbostic 	 * Set the owner and group.  Historically, binmail repeated this at
19845976Sbostic 	 * each mail delivery.  We no longer do this, assuming that if the
19945976Sbostic 	 * ownership or permissions were changed there was a reason for doing
20045976Sbostic 	 * so.
20145976Sbostic 	 */
20245976Sbostic bad:	if (created)
20345976Sbostic 		(void)fchown(mbfd, pw->pw_uid, pw->pw_gid);
204579Sroot 
20546541Sbostic 	(void)fsync(mbfd);		/* Don't wait for update. */
20646541Sbostic 	(void)close(mbfd);		/* Implicit unlock. */
207579Sroot 
20845976Sbostic 	if (!rval)
20945976Sbostic 		notifybiff(biffmsg);
21045976Sbostic 	return(rval);
211579Sroot }
212579Sroot 
21316731Sralph notifybiff(msg)
21416731Sralph 	char *msg;
21516731Sralph {
21616731Sralph 	static struct sockaddr_in addr;
21716731Sralph 	static int f = -1;
21845976Sbostic 	struct hostent *hp;
21945976Sbostic 	struct servent *sp;
22045976Sbostic 	int len;
22116731Sralph 
22245976Sbostic 	if (!addr.sin_family) {
22345976Sbostic 		/* Be silent if biff service not available. */
22445976Sbostic 		if (!(sp = getservbyname("biff", "udp")))
22545976Sbostic 			return;
22645976Sbostic 		if (!(hp = gethostbyname("localhost"))) {
22745976Sbostic 			error(NOTFATAL, "localhost: %s.", strerror(errno));
22845976Sbostic 			return;
22916731Sralph 		}
23045976Sbostic 		addr.sin_family = hp->h_addrtype;
23145976Sbostic 		bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
23245976Sbostic 		addr.sin_port = sp->s_port;
23316731Sralph 	}
23445976Sbostic 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
23545976Sbostic 		error(NOTFATAL, "socket: %s.", strerror(errno));
23645976Sbostic 		return;
23716731Sralph 	}
23845976Sbostic 	len = strlen(msg) + 1;
23946672Sbostic 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
24046672Sbostic 	    != len)
24145976Sbostic 		error(NOTFATAL, "sendto biff: %s.", strerror(errno));
24216731Sralph }
24316731Sralph 
24445976Sbostic usage()
245579Sroot {
24645976Sbostic 	error(FATAL, "usage: mail.local [-f from] user ...");
247579Sroot }
248579Sroot 
24945976Sbostic /* VARARGS */
25046554Sbostic error(isfatal, fmt)
25146554Sbostic 	int isfatal;
25246554Sbostic 	char *fmt;
253579Sroot {
25445976Sbostic 	va_list ap;
255579Sroot 
25646554Sbostic 	va_start(ap, fmt);
25745976Sbostic 	vsyslog(LOG_ERR, fmt, ap);
25845976Sbostic 	va_end(ap);
25945976Sbostic 	if (isfatal)
26045976Sbostic 		exit(1);
261579Sroot }
262