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