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