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