Home | History | Annotate | Line # | Download | only in comsat
comsat.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1980 Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.2  mycroft /*static char sccsid[] = "from: @(#)comsat.c	5.24 (Berkeley) 2/25/91";*/
     42  1.3   briggs static char rcsid[] = "$Id: comsat.c,v 1.4 1994/02/01 00:32:22 cgd Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #include <sys/param.h>
     46  1.1      cgd #include <sys/socket.h>
     47  1.1      cgd #include <sys/stat.h>
     48  1.1      cgd #include <sys/file.h>
     49  1.1      cgd #include <sys/wait.h>
     50  1.1      cgd 
     51  1.1      cgd #include <netinet/in.h>
     52  1.1      cgd 
     53  1.1      cgd #include <stdio.h>
     54  1.1      cgd #include <sgtty.h>
     55  1.1      cgd #include <utmp.h>
     56  1.1      cgd #include <signal.h>
     57  1.1      cgd #include <errno.h>
     58  1.1      cgd #include <netdb.h>
     59  1.1      cgd #include <syslog.h>
     60  1.1      cgd #include <ctype.h>
     61  1.1      cgd #include <string.h>
     62  1.4      cgd #include <pwd.h>
     63  1.1      cgd #include <paths.h>
     64  1.1      cgd 
     65  1.1      cgd int	debug = 0;
     66  1.1      cgd #define	dsyslog	if (debug) syslog
     67  1.1      cgd 
     68  1.1      cgd #define MAXIDLE	120
     69  1.1      cgd 
     70  1.1      cgd char	hostname[MAXHOSTNAMELEN];
     71  1.1      cgd struct	utmp *utmp = NULL;
     72  1.1      cgd time_t	lastmsgtime, time();
     73  1.1      cgd int	nutmp, uf;
     74  1.1      cgd 
     75  1.1      cgd /* ARGSUSED */
     76  1.1      cgd main(argc, argv)
     77  1.1      cgd 	int argc;
     78  1.1      cgd 	char **argv;
     79  1.1      cgd {
     80  1.1      cgd 	extern int errno;
     81  1.1      cgd 	register int cc;
     82  1.1      cgd 	char msgbuf[100];
     83  1.1      cgd 	struct sockaddr_in from;
     84  1.1      cgd 	int fromlen;
     85  1.1      cgd 	void onalrm(), reapchildren();
     86  1.1      cgd 
     87  1.1      cgd 	/* verify proper invocation */
     88  1.1      cgd 	fromlen = sizeof(from);
     89  1.1      cgd 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
     90  1.1      cgd 		(void)fprintf(stderr,
     91  1.1      cgd 		    "comsat: getsockname: %s.\n", strerror(errno));
     92  1.1      cgd 		exit(1);
     93  1.1      cgd 	}
     94  1.1      cgd 	openlog("comsat", LOG_PID, LOG_DAEMON);
     95  1.1      cgd 	if (chdir(_PATH_MAILDIR)) {
     96  1.1      cgd 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
     97  1.1      cgd 		exit(1);
     98  1.1      cgd 	}
     99  1.1      cgd 	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
    100  1.4      cgd 		syslog(LOG_ERR, "main: %s: %m", _PATH_UTMP);
    101  1.1      cgd 		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
    102  1.1      cgd 		exit(1);
    103  1.1      cgd 	}
    104  1.1      cgd 	(void)time(&lastmsgtime);
    105  1.1      cgd 	(void)gethostname(hostname, sizeof(hostname));
    106  1.1      cgd 	onalrm();
    107  1.1      cgd 	(void)signal(SIGALRM, onalrm);
    108  1.1      cgd 	(void)signal(SIGTTOU, SIG_IGN);
    109  1.1      cgd 	(void)signal(SIGCHLD, reapchildren);
    110  1.1      cgd 	for (;;) {
    111  1.1      cgd 		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
    112  1.1      cgd 		if (cc <= 0) {
    113  1.1      cgd 			if (errno != EINTR)
    114  1.1      cgd 				sleep(1);
    115  1.1      cgd 			errno = 0;
    116  1.1      cgd 			continue;
    117  1.1      cgd 		}
    118  1.1      cgd 		if (!nutmp)		/* no one has logged in yet */
    119  1.1      cgd 			continue;
    120  1.1      cgd 		sigblock(sigmask(SIGALRM));
    121  1.4      cgd 		msgbuf[cc] = '\0';
    122  1.1      cgd 		(void)time(&lastmsgtime);
    123  1.1      cgd 		mailfor(msgbuf);
    124  1.1      cgd 		sigsetmask(0L);
    125  1.1      cgd 	}
    126  1.1      cgd }
    127  1.1      cgd 
    128  1.1      cgd void
    129  1.1      cgd reapchildren()
    130  1.1      cgd {
    131  1.1      cgd 	while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
    132  1.1      cgd }
    133  1.1      cgd 
    134  1.1      cgd void
    135  1.1      cgd onalrm()
    136  1.1      cgd {
    137  1.1      cgd 	static u_int utmpsize;		/* last malloced size for utmp */
    138  1.1      cgd 	static u_int utmpmtime;		/* last modification time for utmp */
    139  1.1      cgd 	struct stat statbf;
    140  1.1      cgd 	off_t lseek();
    141  1.1      cgd 	char *malloc(), *realloc();
    142  1.1      cgd 
    143  1.1      cgd 	if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE)
    144  1.1      cgd 		exit(0);
    145  1.1      cgd 	(void)alarm((u_int)15);
    146  1.1      cgd 	(void)fstat(uf, &statbf);
    147  1.1      cgd 	if (statbf.st_mtime > utmpmtime) {
    148  1.1      cgd 		utmpmtime = statbf.st_mtime;
    149  1.1      cgd 		if (statbf.st_size > utmpsize) {
    150  1.1      cgd 			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
    151  1.1      cgd 			if (utmp)
    152  1.1      cgd 				utmp = (struct utmp *)realloc((char *)utmp, utmpsize);
    153  1.1      cgd 			else
    154  1.1      cgd 				utmp = (struct utmp *)malloc(utmpsize);
    155  1.1      cgd 			if (!utmp) {
    156  1.1      cgd 				syslog(LOG_ERR, "malloc failed");
    157  1.1      cgd 				exit(1);
    158  1.1      cgd 			}
    159  1.1      cgd 		}
    160  1.1      cgd 		(void)lseek(uf, 0L, L_SET);
    161  1.1      cgd 		nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
    162  1.1      cgd 	}
    163  1.1      cgd }
    164  1.1      cgd 
    165  1.1      cgd mailfor(name)
    166  1.1      cgd 	char *name;
    167  1.1      cgd {
    168  1.1      cgd 	register struct utmp *utp = &utmp[nutmp];
    169  1.1      cgd 	register char *cp;
    170  1.1      cgd 	off_t offset;
    171  1.1      cgd 
    172  1.1      cgd 	if (!(cp = index(name, '@')))
    173  1.1      cgd 		return;
    174  1.1      cgd 	*cp = '\0';
    175  1.1      cgd 	offset = atoi(cp + 1);
    176  1.1      cgd 	while (--utp >= utmp)
    177  1.1      cgd 		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
    178  1.1      cgd 			notify(utp, offset);
    179  1.1      cgd }
    180  1.1      cgd 
    181  1.1      cgd static char *cr;
    182  1.1      cgd 
    183  1.1      cgd notify(utp, offset)
    184  1.1      cgd 	register struct utmp *utp;
    185  1.1      cgd 	off_t offset;
    186  1.1      cgd {
    187  1.4      cgd 	FILE *tp;
    188  1.4      cgd 	struct stat stb;
    189  1.1      cgd 	struct sgttyb gttybuf;
    190  1.4      cgd 	char tty[20], name[sizeof(utmp[0].ut_name) + 1];
    191  1.1      cgd 
    192  1.4      cgd 	(void)snprintf(tty, sizeof(tty), "%s%.*s",
    193  1.4      cgd 	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
    194  1.4      cgd 	if (index(tty + sizeof(_PATH_DEV) - 1, '/')) {
    195  1.4      cgd 		/* A slash is an attempt to break security... */
    196  1.4      cgd 		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
    197  1.4      cgd 		return;
    198  1.4      cgd 	}
    199  1.1      cgd 	if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
    200  1.1      cgd 		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
    201  1.1      cgd 		return;
    202  1.1      cgd 	}
    203  1.1      cgd 	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
    204  1.1      cgd 	if (fork())
    205  1.1      cgd 		return;
    206  1.1      cgd 	(void)signal(SIGALRM, SIG_DFL);
    207  1.1      cgd 	(void)alarm((u_int)30);
    208  1.1      cgd 	if ((tp = fopen(tty, "w")) == NULL) {
    209  1.1      cgd 		dsyslog(LOG_ERR, "fopen of tty %s failed", tty);
    210  1.1      cgd 		_exit(-1);
    211  1.1      cgd 	}
    212  1.1      cgd 	(void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
    213  1.1      cgd 	cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ?
    214  1.1      cgd 	    "\n" : "\n\r";
    215  1.1      cgd 	(void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
    216  1.1      cgd 	name[sizeof(name) - 1] = '\0';
    217  1.1      cgd 	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
    218  1.1      cgd 	    cr, name, sizeof(hostname), hostname, cr, cr);
    219  1.1      cgd 	jkfprintf(tp, name, offset);
    220  1.1      cgd 	(void)fclose(tp);
    221  1.1      cgd 	_exit(0);
    222  1.1      cgd }
    223  1.1      cgd 
    224  1.1      cgd jkfprintf(tp, name, offset)
    225  1.1      cgd 	register FILE *tp;
    226  1.1      cgd 	char name[];
    227  1.1      cgd 	off_t offset;
    228  1.1      cgd {
    229  1.1      cgd 	register char *cp, ch;
    230  1.1      cgd 	register FILE *fi;
    231  1.1      cgd 	register int linecnt, charcnt, inheader;
    232  1.4      cgd 	register struct passwd *p;
    233  1.1      cgd 	char line[BUFSIZ];
    234  1.1      cgd 
    235  1.4      cgd 	/* Set effective uid to user in case mail drop is on nfs */
    236  1.4      cgd 	if ((p = getpwnam(name)) == NULL) {
    237  1.4      cgd 		/*
    238  1.4      cgd 		 * If user is not in passwd file, assume that it's
    239  1.4      cgd 		 * an attempt to break security...
    240  1.4      cgd 		 */
    241  1.4      cgd 		syslog(LOG_AUTH | LOG_NOTICE, "%s not in passwd file", name);
    242  1.3   briggs 		return;
    243  1.4      cgd 	} else
    244  1.4      cgd 		(void) setuid(p->pw_uid);
    245  1.4      cgd 
    246  1.1      cgd 	if ((fi = fopen(name, "r")) == NULL)
    247  1.1      cgd 		return;
    248  1.1      cgd 	(void)fseek(fi, offset, L_SET);
    249  1.1      cgd 	/*
    250  1.1      cgd 	 * Print the first 7 lines or 560 characters of the new mail
    251  1.1      cgd 	 * (whichever comes first).  Skip header crap other than
    252  1.1      cgd 	 * From, Subject, To, and Date.
    253  1.1      cgd 	 */
    254  1.1      cgd 	linecnt = 7;
    255  1.1      cgd 	charcnt = 560;
    256  1.1      cgd 	inheader = 1;
    257  1.1      cgd 	while (fgets(line, sizeof(line), fi) != NULL) {
    258  1.1      cgd 		if (inheader) {
    259  1.1      cgd 			if (line[0] == '\n') {
    260  1.1      cgd 				inheader = 0;
    261  1.1      cgd 				continue;
    262  1.1      cgd 			}
    263  1.1      cgd 			if (line[0] == ' ' || line[0] == '\t' ||
    264  1.1      cgd 			    strncmp(line, "From:", 5) &&
    265  1.1      cgd 			    strncmp(line, "Subject:", 8))
    266  1.1      cgd 				continue;
    267  1.1      cgd 		}
    268  1.1      cgd 		if (linecnt <= 0 || charcnt <= 0) {
    269  1.1      cgd 			(void)fprintf(tp, "...more...%s", cr);
    270  1.4      cgd 			fclose(fi);
    271  1.1      cgd 			return;
    272  1.1      cgd 		}
    273  1.1      cgd 		/* strip weird stuff so can't trojan horse stupid terminals */
    274  1.1      cgd 		for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
    275  1.1      cgd 			ch = toascii(ch);
    276  1.1      cgd 			if (!isprint(ch) && !isspace(ch))
    277  1.1      cgd 				ch |= 0x40;
    278  1.1      cgd 			(void)fputc(ch, tp);
    279  1.1      cgd 		}
    280  1.1      cgd 		(void)fputs(cr, tp);
    281  1.1      cgd 		--linecnt;
    282  1.1      cgd 	}
    283  1.1      cgd 	(void)fprintf(tp, "----%s\n", cr);
    284  1.4      cgd 	fclose(fi);
    285  1.1      cgd }
    286