[BACK]Return to comsat.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / libexec / comsat

File: [cvs.NetBSD.org] / src / libexec / comsat / comsat.c (download)

Revision 1.42, Sat May 29 23:12:30 2010 UTC (10 years, 5 months ago) by dholland
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-mips64-premerge-20101231, localcount-20160914, is-mlppp-base, is-mlppp, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, HEAD
Changes since 1.41: +3 -3 lines

Remove useless cast, probably motivated by an older and stupider lint.

/*	$NetBSD: comsat.c,v 1.42 2010/05/29 23:12:30 dholland Exp $	*/

/*
 * Copyright (c) 1980, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\
 The Regents of the University of California.  All rights reserved.");
#if 0
static char sccsid[] = "from: @(#)comsat.c	8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: comsat.c,v 1.42 2010/05/29 23:12:30 dholland Exp $");
#endif
#endif /* not lint */

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/wait.h>

#include <netinet/in.h>

#include <errno.h>
#include <netdb.h>
#include <paths.h>
#include <pwd.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
#include <time.h>
#include <vis.h>
#include <unistd.h>
#ifdef SUPPORT_UTMP
#include <utmp.h>
#endif
#ifdef SUPPORT_UTMPX
#include <utmpx.h>
#endif

#include "utmpentry.h"

#if !defined(SUPPORT_UTMP) && !defined(SUPPORT_UTMPX)
	#error "SUPPORT_UTMP and/or SUPPORT_UTMPX must be defined"
#endif

#define	dsyslog	if (debug) syslog

#define MAXIDLE	120

static int	logging;
static int	debug;
static char	hostname[MAXHOSTNAMELEN + 1];
static int	nutmp;
static struct	utmpentry *utmp = NULL;
static time_t	lastmsgtime;
static volatile sig_atomic_t needupdate;

int main(int, char *[]);
static void jkfprintf(FILE *, const char *, off_t, const char *);
static void mailfor(char *);
static void notify(const struct utmpentry *, off_t);
static void onalrm(int);
static void checkutmp(void);

int
main(int argc, char *argv[])
{
	struct sockaddr_storage from;
	int cc, ch;
	socklen_t fromlen;
	char msgbuf[100];
	sigset_t nsigset, osigset;

	/* verify proper invocation */
	fromlen = sizeof(from);
	if (getsockname(0, (struct sockaddr *)(void *)&from, &fromlen) == -1)
		err(1, "getsockname");

	openlog("comsat", LOG_PID, LOG_DAEMON);
	while ((ch = getopt(argc, argv, "l")) != -1)
		switch (ch) {
		case 'l':
			logging = 1;
			break;
		default:
			syslog(LOG_ERR, "Usage: %s [-l]", getprogname());
			exit(1);
		}
	if (chdir(_PATH_MAILDIR) == -1) {
		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
		(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
		exit(1);
	}
	(void)time(&lastmsgtime);
	(void)gethostname(hostname, sizeof(hostname));
	hostname[sizeof(hostname) - 1] = '\0';
	(void)signal(SIGALRM, onalrm);
	(void)signal(SIGTTOU, SIG_IGN);
	(void)signal(SIGCHLD, SIG_IGN);
	(void)sigemptyset(&nsigset);
	(void)sigaddset(&nsigset, SIGALRM);
	if (sigprocmask(SIG_SETMASK, NULL, &osigset) == -1) {
		syslog(LOG_ERR, "sigprocmask get failed (%m)");
		exit(1);
	}
	needupdate = 1;
	for (;;) {
		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
		if (cc <= 0) {
			if (errno != EINTR)
				sleep(1);
			errno = 0;
			checkutmp();
			continue;
		} else
			checkutmp();
		if (!nutmp)		/* no one has logged in yet */
			continue;
		if (sigprocmask(SIG_SETMASK, &nsigset, NULL) == -1) {
			syslog(LOG_ERR, "sigprocmask set failed (%m)");
			exit(1);
		}
		msgbuf[cc] = '\0';
		(void)time(&lastmsgtime);
		mailfor(msgbuf);
		if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1) {
			syslog(LOG_ERR, "sigprocmask restore failed (%m)");
			exit(1);
		}
	}
}

static void
/*ARGSUSED*/
onalrm(int signo)
{
	needupdate = 1;
}

static void
checkutmp(void)
{
	if (!needupdate)
		return;
	needupdate = 0;

	if (time(NULL) - lastmsgtime >= MAXIDLE)
		exit(0);
	(void)alarm((u_int)15);
	nutmp = getutentries(NULL, &utmp);
}

static void
mailfor(char *name)
{
	struct utmpentry *ep;
	char *cp, *fn;
	off_t offset;
	intmax_t val;

	if (!(cp = strchr(name, '@')))
		return;
	*cp = '\0';
	errno = 0;
	offset = val = strtoimax(cp + 1, &fn, 10);
	if (errno == ERANGE || offset != val)
		return;
	if (fn && *fn && *fn != '\n') {
		/*
		 * Procmail sends messages to comsat with a trailing colon
		 * and a pathname to the folder where the new message was
		 * deposited.  Since we can't reliably open only regular
		 * files, we need to ignore these.  With one exception:
		 * if it mentions the user's system mailbox.
		 */
		char maildir[MAXPATHLEN];
		int l = snprintf(maildir, sizeof(maildir), ":%s/%s",
		    _PATH_MAILDIR, name);
		if (l >= (int)sizeof(maildir) || strcmp(maildir, fn) != 0)
			return;
	}
	for (ep = utmp; ep != NULL; ep = ep->next)
		if (strcmp(ep->name, name) == 0)
			notify(ep, offset);
}

static void
notify(const struct utmpentry *ep, off_t offset)
{
	FILE *tp;
	struct passwd *p;
	struct stat stb;
	struct termios ttybuf;
	char tty[sizeof(_PATH_DEV) + sizeof(ep->line) + 1];
	const char *cr = ep->line;

	if (strncmp(cr, "pts/", 4) == 0)
		cr += 4;
	if (strchr(cr, '/')) {
		/* A slash is an attempt to break security... */
		syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
		    ep->line);
		return;
	}
	(void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ep->line);
	if (stat(tty, &stb) == -1 || !(stb.st_mode & S_IEXEC)) {
		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", ep->name, tty);
		return;
	}
	dsyslog(LOG_DEBUG, "notify %s on %s", ep->name, tty);
	switch (fork()) {
	case -1:
		syslog(LOG_NOTICE, "fork failed (%m)");
		return;
	case 0:
		break;
	default:
		return;
	}
	(void)signal(SIGALRM, SIG_DFL);
	(void)alarm(30);
	if ((tp = fopen(tty, "w")) == NULL) {
		dsyslog(LOG_ERR, "open `%s' (%s)", tty, strerror(errno));
		_exit(1);
	}
	if (tcgetattr(fileno(tp), &ttybuf) == -1) {
		dsyslog(LOG_ERR, "tcgetattr `%s' (%s)", tty, strerror(errno));
		_exit(1);
	}
	cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
	    "\n" : "\n\r";
	/* Set uid/gid/groups to user's in case mail drop is on nfs */
	if ((p = getpwnam(ep->name)) == NULL ||
	    initgroups(p->pw_name, p->pw_gid) == -1 ||
	    setgid(p->pw_gid) == -1 ||
	    setuid(p->pw_uid) == -1)
		_exit(1);

	if (logging)
		syslog(LOG_INFO, "biff message for %s", ep->name);

	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
	    cr, ep->name, (int)sizeof(hostname), hostname, cr, cr);
	jkfprintf(tp, ep->name, offset, cr);
	(void)fclose(tp);
	_exit(0);
}

static void
jkfprintf(FILE *tp, const char *name, off_t offset, const char *cr)
{
	FILE *fi;
	int linecnt, charcnt, inheader;
	char line[BUFSIZ], visline[BUFSIZ * 4 + 1], *nl;

	if ((fi = fopen(name, "r")) == NULL)
		return;

	(void)fseeko(fi, offset, SEEK_SET);
	/*
	 * Print the first 7 lines or 560 characters of the new mail
	 * (whichever comes first).  Skip header crap other than
	 * From and Subject.
	 */
	linecnt = 7;
	charcnt = 560;
	inheader = 1;
	while (fgets(line, sizeof(line), fi) != NULL) {
		line[sizeof(line) - 1] = '\0';
		if (inheader) {
			if (line[0] == '\n') {
				inheader = 0;
				continue;
			}
			if (line[0] == ' ' || line[0] == '\t' ||
			    (strncasecmp(line, "From:", 5) &&
			    strncasecmp(line, "Subject:", 8)))
				continue;
		}
		if (strncmp(line, "From ", 5) == 0) {
			(void)fprintf(tp, "----%s", cr);
			(void)fclose(fi);
			return;
		}
		if (linecnt <= 0 || charcnt <= 0) {
			(void)fprintf(tp, "...more...%s", cr);
			(void)fclose(fi);
			return;
		}
		if ((nl = strchr(line, '\n')) != NULL)
			*nl = '\0';
		/* strip weird stuff so can't trojan horse stupid terminals */
		(void)strvis(visline, line, VIS_CSTYLE);
		(void)fputs(visline, tp);
		(void)fputs(cr, tp);
		--linecnt;
	}
	(void)fprintf(tp, "----%s\n", cr);
	(void)fclose(fi);
}