NetBSD/libexec/comsat/comsat.c

351 lines
9.1 KiB
C
Raw Normal View History

/* $NetBSD: comsat.c,v 1.36 2007/05/03 15:09:41 christos Exp $ */
1997-10-07 14:57:08 +04:00
1993-03-21 12:45:37 +03:00
/*
1994-08-07 03:26:31 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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
1993-03-21 12:45:37 +03:00
* 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.
*/
1997-10-07 14:57:08 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-10-07 14:57:08 +04:00
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#if 0
static char sccsid[] = "from: @(#)comsat.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: comsat.c,v 1.36 2007/05/03 15:09:41 christos Exp $");
1997-10-07 14:57:08 +04:00
#endif
1993-03-21 12:45:37 +03:00
#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>
1994-08-07 03:26:31 +04:00
#include <paths.h>
#include <pwd.h>
#include <err.h>
1994-08-07 03:26:31 +04:00
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
1995-05-02 06:05:47 +04:00
#include <termios.h>
1998-04-01 18:29:08 +04:00
#include <time.h>
#include <vis.h>
#include <unistd.h>
#ifdef SUPPORT_UTMP
1994-08-07 03:26:31 +04:00
#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
1993-03-21 12:45:37 +03:00
#define dsyslog if (debug) syslog
#define MAXIDLE 120
static int logging;
static int debug;
2004-07-10 11:11:38 +04:00
static char hostname[MAXHOSTNAMELEN + 1];
static time_t utmpmtime; /* last modification time for utmp/x */
static int nutmp;
static struct utmpentry *utmp = NULL;
static time_t lastmsgtime;
static volatile sig_atomic_t needupdate;
1993-03-21 12:45:37 +03:00
int main(int, char *[]);
static void jkfprintf(FILE *, const char *, off_t, const char *);
static void mailfor(const char *);
static void notify(const struct utmpentry *, off_t);
static void onalrm(int);
static void checkutmp(void);
1994-08-07 03:26:31 +04:00
int
2000-10-04 22:52:50 +04:00
main(int argc, char *argv[])
1993-03-21 12:45:37 +03:00
{
struct sockaddr_storage from;
1998-07-04 23:38:39 +04:00
int cc, ch;
socklen_t fromlen;
1994-08-07 03:26:31 +04:00
char msgbuf[100];
sigset_t nsigset, osigset;
1993-03-21 12:45:37 +03:00
/* verify proper invocation */
fromlen = sizeof(from);
if (getsockname(0, (struct sockaddr *)(void *)&from, &fromlen) == -1)
err(1, "getsockname");
1998-07-04 23:38:39 +04:00
1993-03-21 12:45:37 +03:00
openlog("comsat", LOG_PID, LOG_DAEMON);
1998-07-04 23:38:39 +04:00
while ((ch = getopt(argc, argv, "l")) != -1)
switch (ch) {
case 'l':
logging = 1;
break;
default:
2001-02-20 01:43:42 +03:00
syslog(LOG_ERR, "Usage: %s [-l]", getprogname());
1998-07-04 23:38:39 +04:00
exit(1);
}
if (chdir(_PATH_MAILDIR) == -1) {
1993-03-21 12:45:37 +03:00
syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
1993-03-21 12:45:37 +03:00
exit(1);
}
(void)time(&lastmsgtime);
(void)gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
1993-03-21 12:45:37 +03:00
(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;
1993-03-21 12:45:37 +03:00
for (;;) {
cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
if (cc <= 0) {
if (errno != EINTR)
sleep(1);
errno = 0;
checkutmp();
1993-03-21 12:45:37 +03:00
continue;
} else
checkutmp();
1993-03-21 12:45:37 +03:00
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';
1993-03-21 12:45:37 +03:00
(void)time(&lastmsgtime);
mailfor(msgbuf);
if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1) {
syslog(LOG_ERR, "sigprocmask restore failed (%m)");
exit(1);
}
1993-03-21 12:45:37 +03:00
}
}
static void
/*ARGSUSED*/
onalrm(int signo)
1993-03-21 12:45:37 +03:00
{
needupdate = 1;
1993-03-21 12:45:37 +03:00
}
static void
checkutmp(void)
1993-03-21 12:45:37 +03:00
{
struct stat statbf;
time_t newtime = 0;
1993-03-21 12:45:37 +03:00
if (!needupdate)
return;
needupdate = 0;
1994-08-07 03:26:31 +04:00
if (time(NULL) - lastmsgtime >= MAXIDLE)
1993-03-21 12:45:37 +03:00
exit(0);
(void)alarm((u_int)15);
#ifdef SUPPORT_UTMP
if (stat(_PATH_UTMP, &statbf) != -1)
if (statbf.st_mtime > newtime)
newtime = statbf.st_mtime;
#endif
#ifdef SUPPORT_UTMPX
2004-07-10 08:00:09 +04:00
if (stat(_PATH_UTMPX, &statbf) != -1)
if (statbf.st_mtime > newtime)
newtime = statbf.st_mtime;
#endif
if (newtime > utmpmtime) {
freeutentries(utmp);
nutmp = getutentries(NULL, &utmp);
utmpmtime = newtime;
1993-03-21 12:45:37 +03:00
}
}
static void
mailfor(const char *name)
1993-03-21 12:45:37 +03:00
{
struct utmpentry *ep;
char *cp, *fn;
1993-03-21 12:45:37 +03:00
off_t offset;
intmax_t val;
1993-03-21 12:45:37 +03:00
1994-08-07 03:26:31 +04:00
if (!(cp = strchr(name, '@')))
1993-03-21 12:45:37 +03:00
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",
2004-07-10 11:11:38 +04:00
_PATH_MAILDIR, name);
if (l >= sizeof(maildir) || strcmp(maildir, fn) != 0)
return;
}
for (ep = utmp; ep != NULL; ep = ep->next)
if (strcmp(ep->name, name) == 0)
notify(ep, offset);
1993-03-21 12:45:37 +03:00
}
static void
notify(const struct utmpentry *ep, off_t offset)
1993-03-21 12:45:37 +03:00
{
FILE *tp;
struct passwd *p;
struct stat stb;
1995-05-02 06:05:47 +04:00
struct termios ttybuf;
char tty[sizeof(_PATH_DEV) + sizeof(ep->line) + 1];
const char *cr = ep->line;
1993-03-21 12:45:37 +03:00
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);
1993-03-21 12:45:37 +03:00
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:
1993-03-21 12:45:37 +03:00
return;
}
1993-03-21 12:45:37 +03:00
(void)signal(SIGALRM, SIG_DFL);
(void)alarm((u_int)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);
1993-03-21 12:45:37 +03:00
}
1995-05-02 06:05:47 +04:00
cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
1993-03-21 12:45:37 +03:00
"\n" : "\n\r";
/* Set uid/gid/groups to users 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);
1998-07-04 23:38:39 +04:00
if (logging)
syslog(LOG_INFO, "biff message for %s", ep->name);
1998-07-04 23:38:39 +04:00
1993-03-21 12:45:37 +03:00
(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);
1993-03-21 12:45:37 +03:00
(void)fclose(tp);
_exit(0);
}
static void
jkfprintf(FILE *tp, const char *name, off_t offset, const char *cr)
1993-03-21 12:45:37 +03:00
{
FILE *fi;
int linecnt, charcnt, inheader;
char line[BUFSIZ], visline[BUFSIZ * 4 + 1], *nl;
1993-03-21 12:45:37 +03:00
if ((fi = fopen(name, "r")) == NULL)
return;
1994-08-07 03:26:31 +04:00
(void)fseeko(fi, offset, SEEK_SET);
1993-03-21 12:45:37 +03:00
/*
* Print the first 7 lines or 560 characters of the new mail
* (whichever comes first). Skip header crap other than
* From, Subject, To, and Date.
*/
linecnt = 7;
charcnt = 560;
inheader = 1;
while (fgets(line, sizeof(line), fi) != NULL) {
line[sizeof(line) - 1] = '\0';
1993-03-21 12:45:37 +03:00
if (inheader) {
if (line[0] == '\n') {
inheader = 0;
continue;
}
if (line[0] == ' ' || line[0] == '\t' ||
(strncasecmp(line, "From:", 5) &&
strncasecmp(line, "Subject:", 8)))
1993-03-21 12:45:37 +03:00
continue;
}
if (strncmp(line, "From ", 5) == 0) {
(void)fprintf(tp, "----%s", cr);
(void)fclose(fi);
return;
}
1993-03-21 12:45:37 +03:00
if (linecnt <= 0 || charcnt <= 0) {
(void)fprintf(tp, "...more...%s", cr);
1994-08-07 03:26:31 +04:00
(void)fclose(fi);
1993-03-21 12:45:37 +03:00
return;
}
if ((nl = strchr(line, '\n')) != NULL)
*nl = '\0';
1993-03-21 12:45:37 +03:00
/* strip weird stuff so can't trojan horse stupid terminals */
(void)strvis(visline, line, VIS_CSTYLE);
(void)fputs(visline, tp);
(void)fputs(cr, tp);
1993-03-21 12:45:37 +03:00
--linecnt;
}
(void)fprintf(tp, "----%s\n", cr);
1994-08-07 03:26:31 +04:00
(void)fclose(fi);
1993-03-21 12:45:37 +03:00
}