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