17c478bd9Sstevel@tonic-gate /*
2740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
87c478bd9Sstevel@tonic-gate  * All Rights Reserved.
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate /*
127c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
137c478bd9Sstevel@tonic-gate  * All rights reserved.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted provided
167c478bd9Sstevel@tonic-gate  * that: (1) source distributions retain this entire copyright notice and
177c478bd9Sstevel@tonic-gate  * comment, and (2) distributions including binaries display the following
187c478bd9Sstevel@tonic-gate  * acknowledgement: ``This product includes software developed by the
197c478bd9Sstevel@tonic-gate  * University of California, Berkeley and its contributors'' in the
207c478bd9Sstevel@tonic-gate  * documentation or other materials provided with the distribution and in
217c478bd9Sstevel@tonic-gate  * all advertising materials mentioning features or use of this software.
227c478bd9Sstevel@tonic-gate  * Neither the name of the University nor the names of its contributors may
237c478bd9Sstevel@tonic-gate  * be used to endorse or promote products derived from this software without
247c478bd9Sstevel@tonic-gate  * specific prior written permission.
257c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
267c478bd9Sstevel@tonic-gate  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
277c478bd9Sstevel@tonic-gate  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/wait.h>
347c478bd9Sstevel@tonic-gate #include <sys/file.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <netinet/in.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <sys/ttold.h>
437c478bd9Sstevel@tonic-gate #include <utmpx.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>	/* for MAXHOSTNAMELEN */
477c478bd9Sstevel@tonic-gate #include <netdb.h>
487c478bd9Sstevel@tonic-gate #include <syslog.h>
497c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
507c478bd9Sstevel@tonic-gate #include <pwd.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * comsat
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifndef UTMPX_FILE
587c478bd9Sstevel@tonic-gate #define	UTMPX_FILE "/etc/utmpx"
597c478bd9Sstevel@tonic-gate #endif	/* UTMPX_FILE */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int	debug = 0;
627c478bd9Sstevel@tonic-gate #define	dsyslog	if (debug) syslog
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate struct	sockaddr_in sin = { AF_INET };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate char	hostname[MAXHOSTNAMELEN];
677c478bd9Sstevel@tonic-gate struct	utmpx *utmp = NULL;
687c478bd9Sstevel@tonic-gate int	nutmp;
697c478bd9Sstevel@tonic-gate int	uf;
707c478bd9Sstevel@tonic-gate unsigned utmpmtime = 0;			/* last modification time for utmp */
717c478bd9Sstevel@tonic-gate unsigned utmpsize = 0;			/* last malloced size for utmp */
727c478bd9Sstevel@tonic-gate time_t	lastmsgtime;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifndef SYSV
757c478bd9Sstevel@tonic-gate int	reapchildren();
767c478bd9Sstevel@tonic-gate #else
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	rindex strrchr
797c478bd9Sstevel@tonic-gate #define	index strchr
807c478bd9Sstevel@tonic-gate #define	signal(s, f)	sigset((s), (f))
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #ifndef sigmask
837c478bd9Sstevel@tonic-gate #define	sigmask(m)	(1 << ((m)-1))
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	set2mask(setp)	((setp)->__sigbits[0])
877c478bd9Sstevel@tonic-gate #define	mask2set(mask, setp) \
887c478bd9Sstevel@tonic-gate 	((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask)))
897c478bd9Sstevel@tonic-gate 
90740638c8Sbw static int
sigsetmask(int mask)91*bd9520e5SToomas Soome sigsetmask(int mask)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	sigset_t oset;
947c478bd9Sstevel@tonic-gate 	sigset_t nset;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
977c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
987c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
997c478bd9Sstevel@tonic-gate 	return (set2mask(&oset));
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
102740638c8Sbw static int
sigblock(int mask)103*bd9520e5SToomas Soome sigblock(int mask)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	sigset_t oset;
1067c478bd9Sstevel@tonic-gate 	sigset_t nset;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
1097c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
1107c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
1117c478bd9Sstevel@tonic-gate 	return (set2mask(&oset));
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #endif /* SYSV */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #define	MAXIDLE	120
1187c478bd9Sstevel@tonic-gate #define	NAMLEN (sizeof (uts[0].ut_name) + 1)
1197c478bd9Sstevel@tonic-gate 
120740638c8Sbw void jkfprintf(FILE *tp, char *name, int mbox, int offset);
121740638c8Sbw void mailfor(char *name);
122740638c8Sbw void notify(struct utmpx *utp, int offset);
123740638c8Sbw void onalrm(int sig);
124740638c8Sbw 
125740638c8Sbw int
main(int argc,char * argv[])126*bd9520e5SToomas Soome main(int argc, char *argv[])
1277c478bd9Sstevel@tonic-gate {
128*bd9520e5SToomas Soome 	int cc;
1297c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
1307c478bd9Sstevel@tonic-gate 	char msgbuf[100];
1317c478bd9Sstevel@tonic-gate 	struct sockaddr_in from;
1327c478bd9Sstevel@tonic-gate 	socklen_t fromlen;
1337c478bd9Sstevel@tonic-gate 	int c;
1347c478bd9Sstevel@tonic-gate 	extern int optind;
1357c478bd9Sstevel@tonic-gate 	extern int getopt();
1367c478bd9Sstevel@tonic-gate 	extern char *optarg;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	openlog("comsat", 0, LOG_DAEMON);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != -1) {
1417c478bd9Sstevel@tonic-gate 		switch ((char)c) {
1427c478bd9Sstevel@tonic-gate 		case'd':
1437c478bd9Sstevel@tonic-gate 			debug++;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		default:
1467c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "invalid argument %s", argv[optind]);
1477c478bd9Sstevel@tonic-gate 			exit(1);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/* verify proper invocation */
1527c478bd9Sstevel@tonic-gate 	fromlen = (socklen_t)sizeof (from);
1537c478bd9Sstevel@tonic-gate 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
1547c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: ", argv[0]);
1557c478bd9Sstevel@tonic-gate 		perror("getsockname");
1567c478bd9Sstevel@tonic-gate 		_exit(1);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #ifdef SYSV
1607c478bd9Sstevel@tonic-gate 	chdir("/var/mail");
1617c478bd9Sstevel@tonic-gate #else
1627c478bd9Sstevel@tonic-gate 	chdir("/var/spool/mail");
1637c478bd9Sstevel@tonic-gate #endif /* SYSV */
1647c478bd9Sstevel@tonic-gate 	if ((uf = open(UTMPX_FILE, 0)) < 0) {
1657c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %m", UTMPX_FILE);
1667c478bd9Sstevel@tonic-gate 		(void) recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
1677c478bd9Sstevel@tonic-gate 		exit(1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	(void) time(&lastmsgtime);
1707c478bd9Sstevel@tonic-gate 	(void) gethostname(hostname, sizeof (hostname));
171740638c8Sbw 	onalrm(0);
172740638c8Sbw 	(void) signal(SIGALRM, onalrm);
1737c478bd9Sstevel@tonic-gate 	(void) signal(SIGTTOU, SIG_IGN);
1747c478bd9Sstevel@tonic-gate #ifndef SYSV
1757c478bd9Sstevel@tonic-gate 	(void) signal(SIGCHLD, reapchildren);
1767c478bd9Sstevel@tonic-gate #else
1777c478bd9Sstevel@tonic-gate 	(void) signal(SIGCHLD, SIG_IGN); /* no zombies */
1787c478bd9Sstevel@tonic-gate #endif /* SYSV */
1797c478bd9Sstevel@tonic-gate 	for (;;) {
1807c478bd9Sstevel@tonic-gate 		cc = recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
1817c478bd9Sstevel@tonic-gate 		if (cc <= 0) {
1827c478bd9Sstevel@tonic-gate 			if (errno != EINTR)
1837c478bd9Sstevel@tonic-gate 				sleep(1);
1847c478bd9Sstevel@tonic-gate 			errno = 0;
1857c478bd9Sstevel@tonic-gate 			continue;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		if (nutmp == 0)			/* no users (yet) */
1887c478bd9Sstevel@tonic-gate 			continue;
1897c478bd9Sstevel@tonic-gate 		sigblock(sigmask(SIGALRM));
1907c478bd9Sstevel@tonic-gate 		msgbuf[cc] = 0;
1917c478bd9Sstevel@tonic-gate 		(void) time(&lastmsgtime);
1927c478bd9Sstevel@tonic-gate 		mailfor(msgbuf);
1937c478bd9Sstevel@tonic-gate 		sigsetmask(0);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate #ifndef SYSV
reapchildren()1987c478bd9Sstevel@tonic-gate reapchildren()
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	while (wait3((struct wait *)0, WNOHANG, (struct rusage *)0) > 0)
2027c478bd9Sstevel@tonic-gate 		;
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate #endif /* SYSV */
2057c478bd9Sstevel@tonic-gate 
206740638c8Sbw /* ARGSUSED */
207740638c8Sbw void
onalrm(int sig)208740638c8Sbw onalrm(int sig)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	struct stat statbf;
2117c478bd9Sstevel@tonic-gate 	time_t now;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	(void) time(&now);
2147c478bd9Sstevel@tonic-gate 	if ((ulong_t)now - (ulong_t)lastmsgtime >= MAXIDLE)
2157c478bd9Sstevel@tonic-gate 		exit(0);
2167c478bd9Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "alarm\n");
2177c478bd9Sstevel@tonic-gate 	alarm(15);
2187c478bd9Sstevel@tonic-gate 	fstat(uf, &statbf);
2197c478bd9Sstevel@tonic-gate 	if (statbf.st_mtime > utmpmtime) {
2207c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, " changed\n");
2217c478bd9Sstevel@tonic-gate 		utmpmtime = statbf.st_mtime;
2227c478bd9Sstevel@tonic-gate 		if (statbf.st_size > utmpsize) {
2237c478bd9Sstevel@tonic-gate 			utmpsize = statbf.st_size + 10 * sizeof (struct utmpx);
2247c478bd9Sstevel@tonic-gate 			if (utmp)
2257c478bd9Sstevel@tonic-gate 				utmp = (struct utmpx *)realloc(utmp, utmpsize);
2267c478bd9Sstevel@tonic-gate 			else
2277c478bd9Sstevel@tonic-gate 				utmp = (struct utmpx *)malloc(utmpsize);
2287c478bd9Sstevel@tonic-gate 			if (! utmp) {
2297c478bd9Sstevel@tonic-gate 				dsyslog(LOG_DEBUG, "malloc failed\n");
2307c478bd9Sstevel@tonic-gate 				exit(1);
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 		lseek(uf, 0, 0);
2347c478bd9Sstevel@tonic-gate 		nutmp = read(uf, utmp, statbf.st_size)/sizeof (struct utmpx);
2357c478bd9Sstevel@tonic-gate 	} else
2367c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, " ok\n");
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
239740638c8Sbw void
mailfor(char * name)240*bd9520e5SToomas Soome mailfor(char *name)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	struct utmpx *utp = &utmp[nutmp];
243*bd9520e5SToomas Soome 	char *cp;
2447c478bd9Sstevel@tonic-gate 	char *rindex();
2457c478bd9Sstevel@tonic-gate 	int offset;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * Don't bother doing anything if nobody is
2497c478bd9Sstevel@tonic-gate 	 * logged into the system.
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	if (utmp == NULL || nutmp == 0)
2527c478bd9Sstevel@tonic-gate 		return;
2537c478bd9Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "mailfor %s\n", name);
2547c478bd9Sstevel@tonic-gate 	cp = name;
2557c478bd9Sstevel@tonic-gate 	while (*cp && *cp != '@')
2567c478bd9Sstevel@tonic-gate 		cp++;
2577c478bd9Sstevel@tonic-gate 	if (*cp == 0) {
2587c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "bad format\n");
2597c478bd9Sstevel@tonic-gate 		return;
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	*cp = 0;
2627c478bd9Sstevel@tonic-gate 	offset = atoi(cp+1);
2637c478bd9Sstevel@tonic-gate 	while (--utp >= utmp)
264*bd9520e5SToomas Soome 		if (utp->ut_type == USER_PROCESS &&
265*bd9520e5SToomas Soome 		    strncmp(utp->ut_name, name, sizeof (utmp[0].ut_name)) == 0)
2667c478bd9Sstevel@tonic-gate 			notify(utp, offset);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate char	*cr;
2707c478bd9Sstevel@tonic-gate 
271740638c8Sbw void
notify(struct utmpx * utp,int offset)272*bd9520e5SToomas Soome notify(struct utmpx *utp, int offset)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	FILE *tp;
2757c478bd9Sstevel@tonic-gate 	struct sgttyb gttybuf;
2767c478bd9Sstevel@tonic-gate 	char tty[sizeof (utmp[0].ut_line) + 5];
2777c478bd9Sstevel@tonic-gate 	char name[sizeof (utmp[0].ut_name) + 1];
2787c478bd9Sstevel@tonic-gate 	struct stat stb, stl;
2797c478bd9Sstevel@tonic-gate 	time_t timep[2];
2807c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
2817c478bd9Sstevel@tonic-gate 	int fd, mbox;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	strcpy(tty, "/dev/");
2857c478bd9Sstevel@tonic-gate 	strncat(tty, utp->ut_line, sizeof (utp->ut_line));
2867c478bd9Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
2877c478bd9Sstevel@tonic-gate 	if (stat(tty, &stb) == -1) {
2887c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "can't stat tty\n");
2897c478bd9Sstevel@tonic-gate 		return;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	if ((stb.st_mode & 0100) == 0) {
2927c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "wrong mode\n");
2937c478bd9Sstevel@tonic-gate 		return;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 	if (fork())
2967c478bd9Sstevel@tonic-gate 		return;
2977c478bd9Sstevel@tonic-gate 	signal(SIGALRM, SIG_DFL);
2987c478bd9Sstevel@tonic-gate 	alarm(30);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	strncpy(name, utp->ut_name, sizeof (utp->ut_name));
3017c478bd9Sstevel@tonic-gate 	name[sizeof (name) - 1] = '\0';
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Do all operations that check protections as the user who
3057c478bd9Sstevel@tonic-gate 	 * will be getting the biff.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	if ((pwd = getpwnam(name)) == (struct passwd *)-1) {
3087c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "getpwnam failed\n");
3097c478bd9Sstevel@tonic-gate 		exit(1);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	if (setuid(pwd->pw_uid) == -1) {
3127c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "setuid failed\n");
3137c478bd9Sstevel@tonic-gate 		exit(1);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/*
3177c478bd9Sstevel@tonic-gate 	 * We need to make sure that the tty listed in the utmp
3187c478bd9Sstevel@tonic-gate 	 * file really is a tty device so that a corrupted utmp
3197c478bd9Sstevel@tonic-gate 	 * file doesn't cause us to over-write a real file.
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	if ((fd = open(tty, O_RDWR)) == -1) {
3227c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "can't open tty");
3237c478bd9Sstevel@tonic-gate 		exit(1);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	if (isatty(fd) == 0) {
3267c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "line listed in utmp file is not a tty\n");
3277c478bd9Sstevel@tonic-gate 		exit(1);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * For the case where the user getting the biff is root,
3327c478bd9Sstevel@tonic-gate 	 * we need to make sure that the tty we will be sending
3337c478bd9Sstevel@tonic-gate 	 * the biff to is also owned by root.
3347c478bd9Sstevel@tonic-gate 	 *
3357c478bd9Sstevel@tonic-gate 	 * Check after open, to prevent race on open.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stb) != 0 || stb.st_uid != pwd->pw_uid) {
3397c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG,
3407c478bd9Sstevel@tonic-gate 		    "tty is not owned by user getting the biff\n");
3417c478bd9Sstevel@tonic-gate 		exit(1);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * Prevent race by doing fdopen on fd, not fopen
3467c478bd9Sstevel@tonic-gate 	 * Fopen opens w/ O_CREAT, which is dangerous too
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	if ((tp = fdopen(fd, "w")) == 0) {
3497c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "fdopen failed\n");
3507c478bd9Sstevel@tonic-gate 		exit(-1);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TIOCGETP, &gttybuf) == -1) {
3547c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "ioctl TIOCGETP failed\n");
3557c478bd9Sstevel@tonic-gate 		exit(1);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r";
3587c478bd9Sstevel@tonic-gate 	fprintf(tp, "%s\n\007New mail for %s@%.*s\007 has arrived:%s\n",
3597c478bd9Sstevel@tonic-gate 	    cr, name, sizeof (hostname), hostname, cr);
3607c478bd9Sstevel@tonic-gate 	fprintf(tp, "----%s\n", cr);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if ((mbox = open(name, O_RDONLY)) == -1) {
3637c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "can't open mailbox for %s", name);
3647c478bd9Sstevel@tonic-gate 		exit(1);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * In case of a worldwritable mail spool directory, we must take
3687c478bd9Sstevel@tonic-gate 	 * care we don't open and read from the wrong file.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	if (fstat(mbox, &stb) == -1 || lstat(name, &stl) == -1) {
3717c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "stat() failed on mail file\n");
3727c478bd9Sstevel@tonic-gate 		exit(1);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * Here we make sure that the file wasn't a hardlink or softlink
3777c478bd9Sstevel@tonic-gate 	 * while we opened it and that it wasn't changed afterwards
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	if (!S_ISREG(stl.st_mode) ||
3807c478bd9Sstevel@tonic-gate 	    stl.st_dev != stb.st_dev ||
3817c478bd9Sstevel@tonic-gate 	    stl.st_ino != stb.st_ino ||
3827c478bd9Sstevel@tonic-gate 	    stl.st_uid != pwd->pw_uid ||
3837c478bd9Sstevel@tonic-gate 	    stb.st_nlink != 1) {
3847c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "mail spool file must be plain file\n");
3857c478bd9Sstevel@tonic-gate 		exit(1);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	timep[0] = stb.st_atime;
3897c478bd9Sstevel@tonic-gate 	timep[1] = stb.st_mtime;
3907c478bd9Sstevel@tonic-gate 	jkfprintf(tp, name, mbox, offset);
3917c478bd9Sstevel@tonic-gate 	utime(name, timep);
3927c478bd9Sstevel@tonic-gate 	exit(0);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
395740638c8Sbw void
jkfprintf(FILE * tp,char * name,int mbox,int offset)396*bd9520e5SToomas Soome jkfprintf(FILE *tp, char *name, int mbox, int offset)
3977c478bd9Sstevel@tonic-gate {
398*bd9520e5SToomas Soome 	FILE *fi;
399*bd9520e5SToomas Soome 	int linecnt, charcnt;
4007c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
4017c478bd9Sstevel@tonic-gate 	int inheader;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "HERE %s's mail starting at %d\n",
4047c478bd9Sstevel@tonic-gate 	    name, offset);
4057c478bd9Sstevel@tonic-gate 	if ((fi = fdopen(mbox, "r")) == NULL) {
4067c478bd9Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "Cant read the mail\n");
4077c478bd9Sstevel@tonic-gate 		return;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	fseek(fi, offset, L_SET);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * Print the first 7 lines or 560 characters of the new mail
4147c478bd9Sstevel@tonic-gate 	 * (whichever comes first).  Skip header crap other than
4157c478bd9Sstevel@tonic-gate 	 * From, Subject, To, and Date.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	linecnt = 7;
4187c478bd9Sstevel@tonic-gate 	charcnt = 560;
4197c478bd9Sstevel@tonic-gate 	inheader = 1;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fi) != NULL) {
423*bd9520e5SToomas Soome 		char *cp;
4247c478bd9Sstevel@tonic-gate 		char *index();
4257c478bd9Sstevel@tonic-gate 		int cnt;
4267c478bd9Sstevel@tonic-gate 		int i;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		if (linecnt <= 0 || charcnt <= 0) {
4297c478bd9Sstevel@tonic-gate 			fprintf(tp, "...more...%s\n", cr);
4307c478bd9Sstevel@tonic-gate 			return;
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		if (strncmp(line, "From ", 5) == 0)
4337c478bd9Sstevel@tonic-gate 			continue;
4347c478bd9Sstevel@tonic-gate 		if (inheader && (line[0] == ' ' || line[0] == '\t'))
4357c478bd9Sstevel@tonic-gate 			continue;
4367c478bd9Sstevel@tonic-gate 		cp = index(line, ':');
4377c478bd9Sstevel@tonic-gate 		if (cp == 0 || (index(line, ' ') && index(line, ' ') < cp))
4387c478bd9Sstevel@tonic-gate 			inheader = 0;
4397c478bd9Sstevel@tonic-gate 		else
4407c478bd9Sstevel@tonic-gate 			cnt = cp - line;
4417c478bd9Sstevel@tonic-gate 		if (inheader &&
4427c478bd9Sstevel@tonic-gate 		    strncmp(line, "Date", cnt) &&
4437c478bd9Sstevel@tonic-gate 		    strncmp(line, "From", cnt) &&
4447c478bd9Sstevel@tonic-gate 		    strncmp(line, "Subject", cnt) &&
4457c478bd9Sstevel@tonic-gate 		    strncmp(line, "To", cnt))
4467c478bd9Sstevel@tonic-gate 			continue;
4477c478bd9Sstevel@tonic-gate 		cp = index(line, '\n');
4487c478bd9Sstevel@tonic-gate 		if (cp)
4497c478bd9Sstevel@tonic-gate 			*cp = '\0';
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		for (i = strlen(line); i-- > 0; )
4527c478bd9Sstevel@tonic-gate 			if (!isprint(line[i]))
4537c478bd9Sstevel@tonic-gate 				line[i] = ' ';
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		fprintf(tp, "%s%s\n", line, cr);
4577c478bd9Sstevel@tonic-gate 		linecnt--, charcnt -= strlen(line);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	fprintf(tp, "----%s\n", cr);
4607c478bd9Sstevel@tonic-gate }
461