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