Home | History | Annotate | Line # | Download | only in comsat
comsat.c revision 1.12
      1 /*	$NetBSD: comsat.c,v 1.12 1998/07/03 02:27:58 mrg 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.12 1998/07/03 02:27:58 mrg 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 <vis.h>
     68 #include <unistd.h>
     69 #include <utmp.h>
     70 
     71 int	debug = 0;
     72 #define	dsyslog	if (debug) syslog
     73 
     74 #define MAXIDLE	120
     75 
     76 char	hostname[MAXHOSTNAMELEN+1];
     77 struct	utmp *utmp = NULL;
     78 time_t	lastmsgtime;
     79 int	nutmp, uf;
     80 
     81 void jkfprintf __P((FILE *, char[], off_t));
     82 void mailfor __P((char *));
     83 void notify __P((struct utmp *, off_t));
     84 void onalrm __P((int));
     85 void reapchildren __P((int));
     86 int main __P((int, char *[]));
     87 
     88 int
     89 main(argc, argv)
     90 	int argc;
     91 	char *argv[];
     92 {
     93 	struct sockaddr_in from;
     94 	int cc;
     95 	int fromlen;
     96 	char msgbuf[100];
     97 	sigset_t sigset;
     98 
     99 	/* verify proper invocation */
    100 	fromlen = sizeof(from);
    101 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
    102 		(void)fprintf(stderr,
    103 		    "comsat: getsockname: %s.\n", strerror(errno));
    104 		exit(1);
    105 	}
    106 	openlog("comsat", LOG_PID, LOG_DAEMON);
    107 	if (chdir(_PATH_MAILDIR)) {
    108 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
    109 		(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
    110 		exit(1);
    111 	}
    112 	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
    113 		syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
    114 		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
    115 		exit(1);
    116 	}
    117 	(void)time(&lastmsgtime);
    118 	(void)gethostname(hostname, sizeof(hostname));
    119 	onalrm(0);
    120 	(void)signal(SIGALRM, onalrm);
    121 	(void)signal(SIGTTOU, SIG_IGN);
    122 	(void)signal(SIGCHLD, reapchildren);
    123 	for (;;) {
    124 		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
    125 		if (cc <= 0) {
    126 			if (errno != EINTR)
    127 				sleep(1);
    128 			errno = 0;
    129 			continue;
    130 		}
    131 		if (!nutmp)		/* no one has logged in yet */
    132 			continue;
    133 		sigemptyset(&sigset);
    134 		sigaddset(&sigset, SIGALRM);
    135 		sigprocmask(SIG_SETMASK, &sigset, NULL);
    136 		msgbuf[cc] = '\0';
    137 		(void)time(&lastmsgtime);
    138 		mailfor(msgbuf);
    139 		sigemptyset(&sigset);
    140 		sigprocmask(SIG_SETMASK, &sigset, NULL);
    141 	}
    142 }
    143 
    144 void
    145 reapchildren(signo)
    146 	int signo;
    147 {
    148 
    149 	while (wait3(NULL, WNOHANG, NULL) > 0);
    150 }
    151 
    152 void
    153 onalrm(signo)
    154 	int signo;
    155 {
    156 	static u_int utmpsize;		/* last malloced size for utmp */
    157 	static u_int utmpmtime;		/* last modification time for utmp */
    158 	struct stat statbf;
    159 
    160 	if (time(NULL) - lastmsgtime >= MAXIDLE)
    161 		exit(0);
    162 	(void)alarm((u_int)15);
    163 	(void)fstat(uf, &statbf);
    164 	if (statbf.st_mtime > utmpmtime) {
    165 		utmpmtime = statbf.st_mtime;
    166 		if (statbf.st_size > utmpsize) {
    167 			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
    168 			if ((utmp = realloc(utmp, utmpsize)) == NULL) {
    169 				syslog(LOG_ERR, "%s", strerror(errno));
    170 				exit(1);
    171 			}
    172 		}
    173 		(void)lseek(uf, (off_t)0, SEEK_SET);
    174 		nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
    175 	}
    176 }
    177 
    178 void
    179 mailfor(name)
    180 	char *name;
    181 {
    182 	struct utmp *utp = &utmp[nutmp];
    183 	char *cp;
    184 	off_t offset;
    185 
    186 	if (!(cp = strchr(name, '@')))
    187 		return;
    188 	*cp = '\0';
    189 	offset = atoi(cp + 1);
    190 	while (--utp >= utmp)
    191 		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
    192 			notify(utp, offset);
    193 }
    194 
    195 static char *cr;
    196 
    197 void
    198 notify(utp, offset)
    199 	struct utmp *utp;
    200 	off_t offset;
    201 {
    202 	FILE *tp;
    203 	struct passwd *p;
    204 	struct stat stb;
    205 	struct termios ttybuf;
    206 	char tty[20], name[sizeof(utmp[0].ut_name) + 1];
    207 
    208 	(void)snprintf(tty, sizeof(tty), "%s%.*s",
    209 	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
    210 	if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
    211 		/* A slash is an attempt to break security... */
    212 		/*
    213 		 * XXX but what about something like "/dev/pts/5"
    214 		 * that we may one day "support". ?
    215 		 */
    216 		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
    217 		return;
    218 	}
    219 	if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
    220 		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
    221 		return;
    222 	}
    223 	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
    224 	if (fork())
    225 		return;
    226 	(void)signal(SIGALRM, SIG_DFL);
    227 	(void)alarm((u_int)30);
    228 	if ((tp = fopen(tty, "w")) == NULL) {
    229 		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
    230 		_exit(-1);
    231 	}
    232 	(void)tcgetattr(fileno(tp), &ttybuf);
    233 	cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
    234 	    "\n" : "\n\r";
    235 	(void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
    236 	name[sizeof(name) - 1] = '\0';
    237 
    238 	/* Set uid/gid/groups to users in case mail drop is on nfs */
    239 	if ((p = getpwnam(name)) == NULL ||
    240 	    initgroups(p->pw_name, p->pw_gid) < 0 ||
    241 	    setgid(p->pw_gid) < 0 ||
    242 	    setuid(p->pw_uid) < 0)
    243 		_exit(-1);
    244 
    245 	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
    246 	    cr, name, (int)sizeof(hostname), hostname, cr, cr);
    247 	jkfprintf(tp, name, offset);
    248 	(void)fclose(tp);
    249 	_exit(0);
    250 }
    251 
    252 void
    253 jkfprintf(tp, name, offset)
    254 	FILE *tp;
    255 	char name[];
    256 	off_t offset;
    257 {
    258 	FILE *fi;
    259 	int linecnt, charcnt, inheader;
    260 	char line[BUFSIZ], visline[BUFSIZ*4];
    261 
    262 	if ((fi = fopen(name, "r")) == NULL)
    263 		return;
    264 
    265 	(void)fseek(fi, offset, SEEK_SET);
    266 	/*
    267 	 * Print the first 7 lines or 560 characters of the new mail
    268 	 * (whichever comes first).  Skip header crap other than
    269 	 * From, Subject, To, and Date.
    270 	 */
    271 	linecnt = 7;
    272 	charcnt = 560;
    273 	inheader = 1;
    274 	while (fgets(line, sizeof(line), fi) != NULL) {
    275 		if (inheader) {
    276 			if (line[0] == '\n') {
    277 				inheader = 0;
    278 				continue;
    279 			}
    280 			if (line[0] == ' ' || line[0] == '\t' ||
    281 			    (strncasecmp(line, "From:", 5) &&
    282 			    strncasecmp(line, "Subject:", 8)))
    283 				continue;
    284 		}
    285 		if (linecnt <= 0 || charcnt <= 0) {
    286 			(void)fprintf(tp, "...more...%s", cr);
    287 			(void)fclose(fi);
    288 			return;
    289 		}
    290 		/* strip weird stuff so can't trojan horse stupid terminals */
    291 		(void)strvis(visline, line, VIS_CSTYLE);
    292 		fputs(visline, tp);
    293 		--linecnt;
    294 	}
    295 	(void)fprintf(tp, "----%s\n", cr);
    296 	(void)fclose(fi);
    297 }
    298