xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/in.comsat.c (revision 473:9c6ea6da29de)
10Sstevel@tonic-gate /*
2*473Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
80Sstevel@tonic-gate  * All Rights Reserved.
90Sstevel@tonic-gate  */
100Sstevel@tonic-gate 
110Sstevel@tonic-gate /*
120Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
130Sstevel@tonic-gate  * All rights reserved.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted provided
160Sstevel@tonic-gate  * that: (1) source distributions retain this entire copyright notice and
170Sstevel@tonic-gate  * comment, and (2) distributions including binaries display the following
180Sstevel@tonic-gate  * acknowledgement: ``This product includes software developed by the
190Sstevel@tonic-gate  * University of California, Berkeley and its contributors'' in the
200Sstevel@tonic-gate  * documentation or other materials provided with the distribution and in
210Sstevel@tonic-gate  * all advertising materials mentioning features or use of this software.
220Sstevel@tonic-gate  * Neither the name of the University nor the names of its contributors may
230Sstevel@tonic-gate  * be used to endorse or promote products derived from this software without
240Sstevel@tonic-gate  * specific prior written permission.
250Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
260Sstevel@tonic-gate  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
270Sstevel@tonic-gate  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/wait.h>
370Sstevel@tonic-gate #include <sys/file.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <ctype.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <netinet/in.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <sys/ttold.h>
460Sstevel@tonic-gate #include <utmpx.h>
470Sstevel@tonic-gate #include <signal.h>
480Sstevel@tonic-gate #include <errno.h>
490Sstevel@tonic-gate #include <sys/param.h>	/* for MAXHOSTNAMELEN */
500Sstevel@tonic-gate #include <netdb.h>
510Sstevel@tonic-gate #include <syslog.h>
520Sstevel@tonic-gate #include <sys/ioctl.h>
530Sstevel@tonic-gate #include <pwd.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * comsat
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #ifndef UTMPX_FILE
610Sstevel@tonic-gate #define	UTMPX_FILE "/etc/utmpx"
620Sstevel@tonic-gate #endif	/* UTMPX_FILE */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate int	debug = 0;
650Sstevel@tonic-gate #define	dsyslog	if (debug) syslog
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct	sockaddr_in sin = { AF_INET };
680Sstevel@tonic-gate 
690Sstevel@tonic-gate char	hostname[MAXHOSTNAMELEN];
700Sstevel@tonic-gate struct	utmpx *utmp = NULL;
710Sstevel@tonic-gate int	nutmp;
720Sstevel@tonic-gate int	uf;
730Sstevel@tonic-gate unsigned utmpmtime = 0;			/* last modification time for utmp */
740Sstevel@tonic-gate unsigned utmpsize = 0;			/* last malloced size for utmp */
750Sstevel@tonic-gate time_t	lastmsgtime;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #ifndef SYSV
780Sstevel@tonic-gate int	reapchildren();
790Sstevel@tonic-gate #else
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	rindex strrchr
820Sstevel@tonic-gate #define	index strchr
830Sstevel@tonic-gate #define	signal(s, f)	sigset((s), (f))
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #ifndef sigmask
860Sstevel@tonic-gate #define	sigmask(m)	(1 << ((m)-1))
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	set2mask(setp)	((setp)->__sigbits[0])
900Sstevel@tonic-gate #define	mask2set(mask, setp) \
910Sstevel@tonic-gate 	((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask)))
920Sstevel@tonic-gate 
93*473Sbw static int
sigsetmask(mask)94*473Sbw sigsetmask(mask)
950Sstevel@tonic-gate int mask;
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	sigset_t oset;
980Sstevel@tonic-gate 	sigset_t nset;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
1010Sstevel@tonic-gate 	mask2set(mask, &nset);
1020Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
1030Sstevel@tonic-gate 	return (set2mask(&oset));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
106*473Sbw static int
sigblock(mask)107*473Sbw sigblock(mask)
1080Sstevel@tonic-gate int mask;
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	sigset_t oset;
1110Sstevel@tonic-gate 	sigset_t nset;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
1140Sstevel@tonic-gate 	mask2set(mask, &nset);
1150Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
1160Sstevel@tonic-gate 	return (set2mask(&oset));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #endif /* SYSV */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate #define	MAXIDLE	120
1230Sstevel@tonic-gate #define	NAMLEN (sizeof (uts[0].ut_name) + 1)
1240Sstevel@tonic-gate 
125*473Sbw void jkfprintf(FILE *tp, char *name, int mbox, int offset);
126*473Sbw void mailfor(char *name);
127*473Sbw void notify(struct utmpx *utp, int offset);
128*473Sbw void onalrm(int sig);
129*473Sbw 
130*473Sbw int
main(argc,argv)1310Sstevel@tonic-gate main(argc, argv)
1320Sstevel@tonic-gate int argc;
1330Sstevel@tonic-gate char *argv[];
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	register int cc;
1360Sstevel@tonic-gate 	char buf[BUFSIZ];
1370Sstevel@tonic-gate 	char msgbuf[100];
1380Sstevel@tonic-gate 	struct sockaddr_in from;
1390Sstevel@tonic-gate 	socklen_t fromlen;
1400Sstevel@tonic-gate 	int c;
1410Sstevel@tonic-gate 	extern int optind;
1420Sstevel@tonic-gate 	extern int getopt();
1430Sstevel@tonic-gate 	extern char *optarg;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	openlog("comsat", 0, LOG_DAEMON);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != -1) {
1480Sstevel@tonic-gate 		switch ((char)c) {
1490Sstevel@tonic-gate 		case'd':
1500Sstevel@tonic-gate 			debug++;
1510Sstevel@tonic-gate 			break;
1520Sstevel@tonic-gate 		default:
1530Sstevel@tonic-gate 			syslog(LOG_ERR, "invalid argument %s", argv[optind]);
1540Sstevel@tonic-gate 			exit(1);
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/* verify proper invocation */
1590Sstevel@tonic-gate 	fromlen = (socklen_t)sizeof (from);
1600Sstevel@tonic-gate 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
1610Sstevel@tonic-gate 		fprintf(stderr, "%s: ", argv[0]);
1620Sstevel@tonic-gate 		perror("getsockname");
1630Sstevel@tonic-gate 		_exit(1);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #ifdef SYSV
1670Sstevel@tonic-gate 	chdir("/var/mail");
1680Sstevel@tonic-gate #else
1690Sstevel@tonic-gate 	chdir("/var/spool/mail");
1700Sstevel@tonic-gate #endif /* SYSV */
1710Sstevel@tonic-gate 	if ((uf = open(UTMPX_FILE, 0)) < 0) {
1720Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %m", UTMPX_FILE);
1730Sstevel@tonic-gate 		(void) recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
1740Sstevel@tonic-gate 		exit(1);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 	(void) time(&lastmsgtime);
1770Sstevel@tonic-gate 	(void) gethostname(hostname, sizeof (hostname));
178*473Sbw 	onalrm(0);
179*473Sbw 	(void) signal(SIGALRM, onalrm);
1800Sstevel@tonic-gate 	(void) signal(SIGTTOU, SIG_IGN);
1810Sstevel@tonic-gate #ifndef SYSV
1820Sstevel@tonic-gate 	(void) signal(SIGCHLD, reapchildren);
1830Sstevel@tonic-gate #else
1840Sstevel@tonic-gate 	(void) signal(SIGCHLD, SIG_IGN); /* no zombies */
1850Sstevel@tonic-gate #endif /* SYSV */
1860Sstevel@tonic-gate 	for (;;) {
1870Sstevel@tonic-gate 		cc = recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
1880Sstevel@tonic-gate 		if (cc <= 0) {
1890Sstevel@tonic-gate 			if (errno != EINTR)
1900Sstevel@tonic-gate 				sleep(1);
1910Sstevel@tonic-gate 			errno = 0;
1920Sstevel@tonic-gate 			continue;
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		if (nutmp == 0)			/* no users (yet) */
1950Sstevel@tonic-gate 			continue;
1960Sstevel@tonic-gate 		sigblock(sigmask(SIGALRM));
1970Sstevel@tonic-gate 		msgbuf[cc] = 0;
1980Sstevel@tonic-gate 		(void) time(&lastmsgtime);
1990Sstevel@tonic-gate 		mailfor(msgbuf);
2000Sstevel@tonic-gate 		sigsetmask(0);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate #ifndef SYSV
reapchildren()2050Sstevel@tonic-gate reapchildren()
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	while (wait3((struct wait *)0, WNOHANG, (struct rusage *)0) > 0)
2090Sstevel@tonic-gate 		;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate #endif /* SYSV */
2120Sstevel@tonic-gate 
213*473Sbw /* ARGSUSED */
214*473Sbw void
onalrm(int sig)215*473Sbw onalrm(int sig)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	struct stat statbf;
2180Sstevel@tonic-gate 	time_t now;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	(void) time(&now);
2210Sstevel@tonic-gate 	if ((ulong_t)now - (ulong_t)lastmsgtime >= MAXIDLE)
2220Sstevel@tonic-gate 		exit(0);
2230Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "alarm\n");
2240Sstevel@tonic-gate 	alarm(15);
2250Sstevel@tonic-gate 	fstat(uf, &statbf);
2260Sstevel@tonic-gate 	if (statbf.st_mtime > utmpmtime) {
2270Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, " changed\n");
2280Sstevel@tonic-gate 		utmpmtime = statbf.st_mtime;
2290Sstevel@tonic-gate 		if (statbf.st_size > utmpsize) {
2300Sstevel@tonic-gate 			utmpsize = statbf.st_size + 10 * sizeof (struct utmpx);
2310Sstevel@tonic-gate 			if (utmp)
2320Sstevel@tonic-gate 				utmp = (struct utmpx *)realloc(utmp, utmpsize);
2330Sstevel@tonic-gate 			else
2340Sstevel@tonic-gate 				utmp = (struct utmpx *)malloc(utmpsize);
2350Sstevel@tonic-gate 			if (! utmp) {
2360Sstevel@tonic-gate 				dsyslog(LOG_DEBUG, "malloc failed\n");
2370Sstevel@tonic-gate 				exit(1);
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 		lseek(uf, 0, 0);
2410Sstevel@tonic-gate 		nutmp = read(uf, utmp, statbf.st_size)/sizeof (struct utmpx);
2420Sstevel@tonic-gate 	} else
2430Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, " ok\n");
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
246*473Sbw void
mailfor(name)2470Sstevel@tonic-gate mailfor(name)
2480Sstevel@tonic-gate char *name;
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	struct utmpx *utp = &utmp[nutmp];
2510Sstevel@tonic-gate 	register char *cp;
2520Sstevel@tonic-gate 	char *rindex();
2530Sstevel@tonic-gate 	int offset;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * Don't bother doing anything if nobody is
2570Sstevel@tonic-gate 	 * logged into the system.
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	if (utmp == NULL || nutmp == 0)
2600Sstevel@tonic-gate 		return;
2610Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "mailfor %s\n", name);
2620Sstevel@tonic-gate 	cp = name;
2630Sstevel@tonic-gate 	while (*cp && *cp != '@')
2640Sstevel@tonic-gate 		cp++;
2650Sstevel@tonic-gate 	if (*cp == 0) {
2660Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "bad format\n");
2670Sstevel@tonic-gate 		return;
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 	*cp = 0;
2700Sstevel@tonic-gate 	offset = atoi(cp+1);
2710Sstevel@tonic-gate 	while (--utp >= utmp)
2720Sstevel@tonic-gate 		if ((utp->ut_type == USER_PROCESS) &&
2730Sstevel@tonic-gate 		    (!strncmp(utp->ut_name, name, sizeof (utmp[0].ut_name))))
2740Sstevel@tonic-gate 			notify(utp, offset);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate char	*cr;
2780Sstevel@tonic-gate 
279*473Sbw void
notify(utp,offset)2800Sstevel@tonic-gate notify(utp, offset)
2810Sstevel@tonic-gate struct utmpx *utp;
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	FILE *tp;
2840Sstevel@tonic-gate 	struct sgttyb gttybuf;
2850Sstevel@tonic-gate 	char tty[sizeof (utmp[0].ut_line) + 5];
2860Sstevel@tonic-gate 	char name[sizeof (utmp[0].ut_name) + 1];
2870Sstevel@tonic-gate 	struct stat stb, stl;
2880Sstevel@tonic-gate 	time_t timep[2];
2890Sstevel@tonic-gate 	struct passwd *pwd;
2900Sstevel@tonic-gate 	int fd, mbox;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	strcpy(tty, "/dev/");
2940Sstevel@tonic-gate 	strncat(tty, utp->ut_line, sizeof (utp->ut_line));
2950Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
2960Sstevel@tonic-gate 	if (stat(tty, &stb) == -1) {
2970Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "can't stat tty\n");
2980Sstevel@tonic-gate 		return;
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 	if ((stb.st_mode & 0100) == 0) {
3010Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "wrong mode\n");
3020Sstevel@tonic-gate 		return;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 	if (fork())
3050Sstevel@tonic-gate 		return;
3060Sstevel@tonic-gate 	signal(SIGALRM, SIG_DFL);
3070Sstevel@tonic-gate 	alarm(30);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	strncpy(name, utp->ut_name, sizeof (utp->ut_name));
3100Sstevel@tonic-gate 	name[sizeof (name) - 1] = '\0';
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/*
3130Sstevel@tonic-gate 	 * Do all operations that check protections as the user who
3140Sstevel@tonic-gate 	 * will be getting the biff.
3150Sstevel@tonic-gate 	 */
3160Sstevel@tonic-gate 	if ((pwd = getpwnam(name)) == (struct passwd *)-1) {
3170Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "getpwnam failed\n");
3180Sstevel@tonic-gate 		exit(1);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	if (setuid(pwd->pw_uid) == -1) {
3210Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "setuid failed\n");
3220Sstevel@tonic-gate 		exit(1);
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/*
3260Sstevel@tonic-gate 	 * We need to make sure that the tty listed in the utmp
3270Sstevel@tonic-gate 	 * file really is a tty device so that a corrupted utmp
3280Sstevel@tonic-gate 	 * file doesn't cause us to over-write a real file.
3290Sstevel@tonic-gate 	 */
3300Sstevel@tonic-gate 	if ((fd = open(tty, O_RDWR)) == -1) {
3310Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "can't open tty");
3320Sstevel@tonic-gate 		exit(1);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 	if (isatty(fd) == 0) {
3350Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "line listed in utmp file is not a tty\n");
3360Sstevel@tonic-gate 		exit(1);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * For the case where the user getting the biff is root,
3410Sstevel@tonic-gate 	 * we need to make sure that the tty we will be sending
3420Sstevel@tonic-gate 	 * the biff to is also owned by root.
3430Sstevel@tonic-gate 	 *
3440Sstevel@tonic-gate 	 * Check after open, to prevent race on open.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (fstat(fd, &stb) != 0 || stb.st_uid != pwd->pw_uid) {
3480Sstevel@tonic-gate 		dsyslog(LOG_DEBUG,
3490Sstevel@tonic-gate 		    "tty is not owned by user getting the biff\n");
3500Sstevel@tonic-gate 		exit(1);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	/*
3540Sstevel@tonic-gate 	 * Prevent race by doing fdopen on fd, not fopen
3550Sstevel@tonic-gate 	 * Fopen opens w/ O_CREAT, which is dangerous too
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	if ((tp = fdopen(fd, "w")) == 0) {
3580Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "fdopen failed\n");
3590Sstevel@tonic-gate 		exit(-1);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (ioctl(fd, TIOCGETP, &gttybuf) == -1) {
3630Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "ioctl TIOCGETP failed\n");
3640Sstevel@tonic-gate 		exit(1);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 	cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r";
3670Sstevel@tonic-gate 	fprintf(tp, "%s\n\007New mail for %s@%.*s\007 has arrived:%s\n",
3680Sstevel@tonic-gate 	    cr, name, sizeof (hostname), hostname, cr);
3690Sstevel@tonic-gate 	fprintf(tp, "----%s\n", cr);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if ((mbox = open(name, O_RDONLY)) == -1) {
3720Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "can't open mailbox for %s", name);
3730Sstevel@tonic-gate 		exit(1);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * In case of a worldwritable mail spool directory, we must take
3770Sstevel@tonic-gate 	 * care we don't open and read from the wrong file.
3780Sstevel@tonic-gate 	 */
3790Sstevel@tonic-gate 	if (fstat(mbox, &stb) == -1 || lstat(name, &stl) == -1) {
3800Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "stat() failed on mail file\n");
3810Sstevel@tonic-gate 		exit(1);
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/*
3850Sstevel@tonic-gate 	 * Here we make sure that the file wasn't a hardlink or softlink
3860Sstevel@tonic-gate 	 * while we opened it and that it wasn't changed afterwards
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	if (!S_ISREG(stl.st_mode) ||
3890Sstevel@tonic-gate 	    stl.st_dev != stb.st_dev ||
3900Sstevel@tonic-gate 	    stl.st_ino != stb.st_ino ||
3910Sstevel@tonic-gate 	    stl.st_uid != pwd->pw_uid ||
3920Sstevel@tonic-gate 	    stb.st_nlink != 1) {
3930Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "mail spool file must be plain file\n");
3940Sstevel@tonic-gate 		exit(1);
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	timep[0] = stb.st_atime;
3980Sstevel@tonic-gate 	timep[1] = stb.st_mtime;
3990Sstevel@tonic-gate 	jkfprintf(tp, name, mbox, offset);
4000Sstevel@tonic-gate 	utime(name, timep);
4010Sstevel@tonic-gate 	exit(0);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
404*473Sbw void
jkfprintf(tp,name,mbox,offset)4050Sstevel@tonic-gate jkfprintf(tp, name, mbox, offset)
4060Sstevel@tonic-gate register FILE *tp;
4070Sstevel@tonic-gate char *name;
4080Sstevel@tonic-gate int mbox;
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	register FILE *fi;
4110Sstevel@tonic-gate 	register int linecnt, charcnt;
4120Sstevel@tonic-gate 	char line[BUFSIZ];
4130Sstevel@tonic-gate 	int inheader;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	dsyslog(LOG_DEBUG, "HERE %s's mail starting at %d\n",
4160Sstevel@tonic-gate 	    name, offset);
4170Sstevel@tonic-gate 	if ((fi = fdopen(mbox, "r")) == NULL) {
4180Sstevel@tonic-gate 		dsyslog(LOG_DEBUG, "Cant read the mail\n");
4190Sstevel@tonic-gate 		return;
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	fseek(fi, offset, L_SET);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * Print the first 7 lines or 560 characters of the new mail
4260Sstevel@tonic-gate 	 * (whichever comes first).  Skip header crap other than
4270Sstevel@tonic-gate 	 * From, Subject, To, and Date.
4280Sstevel@tonic-gate 	 */
4290Sstevel@tonic-gate 	linecnt = 7;
4300Sstevel@tonic-gate 	charcnt = 560;
4310Sstevel@tonic-gate 	inheader = 1;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fi) != NULL) {
4350Sstevel@tonic-gate 		register char *cp;
4360Sstevel@tonic-gate 		char *index();
4370Sstevel@tonic-gate 		int cnt;
4380Sstevel@tonic-gate 		int i;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		if (linecnt <= 0 || charcnt <= 0) {
4410Sstevel@tonic-gate 			fprintf(tp, "...more...%s\n", cr);
4420Sstevel@tonic-gate 			return;
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 		if (strncmp(line, "From ", 5) == 0)
4450Sstevel@tonic-gate 			continue;
4460Sstevel@tonic-gate 		if (inheader && (line[0] == ' ' || line[0] == '\t'))
4470Sstevel@tonic-gate 			continue;
4480Sstevel@tonic-gate 		cp = index(line, ':');
4490Sstevel@tonic-gate 		if (cp == 0 || (index(line, ' ') && index(line, ' ') < cp))
4500Sstevel@tonic-gate 			inheader = 0;
4510Sstevel@tonic-gate 		else
4520Sstevel@tonic-gate 			cnt = cp - line;
4530Sstevel@tonic-gate 		if (inheader &&
4540Sstevel@tonic-gate 		    strncmp(line, "Date", cnt) &&
4550Sstevel@tonic-gate 		    strncmp(line, "From", cnt) &&
4560Sstevel@tonic-gate 		    strncmp(line, "Subject", cnt) &&
4570Sstevel@tonic-gate 		    strncmp(line, "To", cnt))
4580Sstevel@tonic-gate 			continue;
4590Sstevel@tonic-gate 		cp = index(line, '\n');
4600Sstevel@tonic-gate 		if (cp)
4610Sstevel@tonic-gate 			*cp = '\0';
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		for (i = strlen(line); i-- > 0; )
4640Sstevel@tonic-gate 			if (!isprint(line[i]))
4650Sstevel@tonic-gate 				line[i] = ' ';
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		fprintf(tp, "%s%s\n", line, cr);
4690Sstevel@tonic-gate 		linecnt--, charcnt -= strlen(line);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 	fprintf(tp, "----%s\n", cr);
4720Sstevel@tonic-gate }
473