2002-03-19 02:34:21 +03:00
|
|
|
/* $NetBSD: comsat.c,v 1.23 2002/03/18 23:34:21 mjl 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. 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.
|
|
|
|
*/
|
|
|
|
|
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
|
2002-03-19 02:34:21 +03:00
|
|
|
__RCSID("$NetBSD: comsat.c,v 1.23 2002/03/18 23:34:21 mjl 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>
|
|
|
|
|
1994-08-07 03:26:31 +04:00
|
|
|
#include <ctype.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
1994-08-07 03:26:31 +04:00
|
|
|
#include <paths.h>
|
1994-02-01 03:32:22 +03:00
|
|
|
#include <pwd.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>
|
1998-07-03 06:27:58 +04:00
|
|
|
#include <vis.h>
|
1994-08-05 05:01:16 +04:00
|
|
|
#include <unistd.h>
|
1994-08-07 03:26:31 +04:00
|
|
|
#include <utmp.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-07-04 23:38:39 +04:00
|
|
|
int logging;
|
1993-03-21 12:45:37 +03:00
|
|
|
int debug = 0;
|
|
|
|
#define dsyslog if (debug) syslog
|
|
|
|
|
|
|
|
#define MAXIDLE 120
|
|
|
|
|
1998-07-03 06:27:58 +04:00
|
|
|
char hostname[MAXHOSTNAMELEN+1];
|
1993-03-21 12:45:37 +03:00
|
|
|
struct utmp *utmp = NULL;
|
1994-08-07 03:26:31 +04:00
|
|
|
time_t lastmsgtime;
|
1993-03-21 12:45:37 +03:00
|
|
|
int nutmp, uf;
|
|
|
|
|
2000-10-04 22:52:50 +04:00
|
|
|
void jkfprintf (FILE *, char[], off_t);
|
|
|
|
void mailfor (char *);
|
|
|
|
void notify (struct utmp *, off_t);
|
|
|
|
void onalrm (int);
|
|
|
|
void reapchildren (int);
|
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
|
|
|
{
|
2000-08-22 20:42:37 +04:00
|
|
|
struct sockaddr_storage from;
|
1998-07-04 23:38:39 +04:00
|
|
|
int cc, ch;
|
1993-03-21 12:45:37 +03:00
|
|
|
int fromlen;
|
1994-08-07 03:26:31 +04:00
|
|
|
char msgbuf[100];
|
1995-03-22 00:47:04 +03:00
|
|
|
sigset_t sigset;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* verify proper invocation */
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"comsat: getsockname: %s.\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
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);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (chdir(_PATH_MAILDIR)) {
|
|
|
|
syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
|
1998-07-03 06:27:58 +04:00
|
|
|
(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
|
1994-08-07 03:26:31 +04:00
|
|
|
syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
|
1998-07-04 23:38:39 +04:00
|
|
|
(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));
|
1998-07-06 10:45:41 +04:00
|
|
|
hostname[sizeof(hostname) - 1] = '\0';
|
1994-08-07 03:26:31 +04:00
|
|
|
onalrm(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)signal(SIGALRM, onalrm);
|
|
|
|
(void)signal(SIGTTOU, SIG_IGN);
|
|
|
|
(void)signal(SIGCHLD, reapchildren);
|
|
|
|
for (;;) {
|
|
|
|
cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
|
|
|
|
if (cc <= 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
sleep(1);
|
|
|
|
errno = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!nutmp) /* no one has logged in yet */
|
|
|
|
continue;
|
1995-03-22 00:47:04 +03:00
|
|
|
sigemptyset(&sigset);
|
|
|
|
sigaddset(&sigset, SIGALRM);
|
|
|
|
sigprocmask(SIG_SETMASK, &sigset, NULL);
|
1994-02-01 03:32:22 +03:00
|
|
|
msgbuf[cc] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)time(&lastmsgtime);
|
|
|
|
mailfor(msgbuf);
|
1995-03-22 00:47:04 +03:00
|
|
|
sigemptyset(&sigset);
|
|
|
|
sigprocmask(SIG_SETMASK, &sigset, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-10-04 22:52:50 +04:00
|
|
|
reapchildren(int signo)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-03 06:27:58 +04:00
|
|
|
|
1994-08-07 03:26:31 +04:00
|
|
|
while (wait3(NULL, WNOHANG, NULL) > 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-10-04 22:52:50 +04:00
|
|
|
onalrm(int signo)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static u_int utmpsize; /* last malloced size for utmp */
|
|
|
|
static u_int utmpmtime; /* last modification time for utmp */
|
|
|
|
struct stat statbf;
|
|
|
|
|
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);
|
|
|
|
(void)fstat(uf, &statbf);
|
|
|
|
if (statbf.st_mtime > utmpmtime) {
|
|
|
|
utmpmtime = statbf.st_mtime;
|
|
|
|
if (statbf.st_size > utmpsize) {
|
|
|
|
utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
|
1994-08-07 03:26:31 +04:00
|
|
|
if ((utmp = realloc(utmp, utmpsize)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s", strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
1997-08-25 23:31:43 +04:00
|
|
|
(void)lseek(uf, (off_t)0, SEEK_SET);
|
1993-03-21 12:45:37 +03:00
|
|
|
nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-08-07 03:26:31 +04:00
|
|
|
void
|
2000-10-04 22:52:50 +04:00
|
|
|
mailfor(char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-03 06:27:58 +04:00
|
|
|
struct utmp *utp = &utmp[nutmp];
|
2001-03-17 00:39:08 +03:00
|
|
|
char *cp, *fn;
|
1993-03-21 12:45:37 +03:00
|
|
|
off_t offset;
|
|
|
|
|
1994-08-07 03:26:31 +04:00
|
|
|
if (!(cp = strchr(name, '@')))
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
*cp = '\0';
|
2001-03-17 00:39:08 +03:00
|
|
|
errno = 0;
|
|
|
|
offset = strtol(cp + 1, &fn, 10);
|
|
|
|
if (errno == ERANGE)
|
|
|
|
return;
|
2001-04-02 18:57:37 +04:00
|
|
|
if (fn && *fn && *fn != '\n') {
|
2001-03-17 00:39:08 +03:00
|
|
|
/*
|
|
|
|
* 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[128];
|
|
|
|
int l = snprintf(maildir, sizeof(maildir), ":%s/%s",
|
|
|
|
_PATH_MAILDIR, name);
|
|
|
|
if (l > sizeof(maildir) || strcmp(maildir, fn) != 0)
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
while (--utp >= utmp)
|
|
|
|
if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
|
|
|
|
notify(utp, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *cr;
|
|
|
|
|
1994-08-07 03:26:31 +04:00
|
|
|
void
|
2000-10-04 22:52:50 +04:00
|
|
|
notify(struct utmp *utp, off_t offset)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
FILE *tp;
|
1998-07-03 06:27:58 +04:00
|
|
|
struct passwd *p;
|
1994-02-01 03:32:22 +03:00
|
|
|
struct stat stb;
|
1995-05-02 06:05:47 +04:00
|
|
|
struct termios ttybuf;
|
1994-02-01 03:32:22 +03:00
|
|
|
char tty[20], name[sizeof(utmp[0].ut_name) + 1];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-02-01 03:32:22 +03:00
|
|
|
(void)snprintf(tty, sizeof(tty), "%s%.*s",
|
|
|
|
_PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
|
1994-08-07 03:26:31 +04:00
|
|
|
if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
|
1994-02-01 03:32:22 +03:00
|
|
|
/* A slash is an attempt to break security... */
|
1998-07-03 06:27:58 +04:00
|
|
|
/*
|
|
|
|
* XXX but what about something like "/dev/pts/5"
|
|
|
|
* that we may one day "support". ?
|
|
|
|
*/
|
1994-02-01 03:32:22 +03:00
|
|
|
syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
|
|
|
|
dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
|
|
|
|
return;
|
|
|
|
}
|
2000-10-04 22:24:03 +04:00
|
|
|
dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_name, tty);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (fork())
|
|
|
|
return;
|
|
|
|
(void)signal(SIGALRM, SIG_DFL);
|
|
|
|
(void)alarm((u_int)30);
|
|
|
|
if ((tp = fopen(tty, "w")) == NULL) {
|
1994-08-07 03:26:31 +04:00
|
|
|
dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
|
2000-10-04 22:24:03 +04:00
|
|
|
_exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-05-02 06:05:47 +04:00
|
|
|
(void)tcgetattr(fileno(tp), &ttybuf);
|
|
|
|
cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
|
1993-03-21 12:45:37 +03:00
|
|
|
"\n" : "\n\r";
|
2002-03-19 02:34:21 +03:00
|
|
|
(void)strlcpy(name, utp->ut_name, sizeof(name));
|
1998-07-03 06:27:58 +04:00
|
|
|
|
|
|
|
/* Set uid/gid/groups to users in case mail drop is on nfs */
|
|
|
|
if ((p = getpwnam(name)) == NULL ||
|
|
|
|
initgroups(p->pw_name, p->pw_gid) < 0 ||
|
|
|
|
setgid(p->pw_gid) < 0 ||
|
|
|
|
setuid(p->pw_uid) < 0)
|
2000-10-04 22:24:03 +04:00
|
|
|
_exit(1);
|
1998-07-03 06:27:58 +04:00
|
|
|
|
1998-07-04 23:38:39 +04:00
|
|
|
if (logging)
|
|
|
|
syslog(LOG_INFO, "biff message for %s", name);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
|
1994-08-07 03:26:31 +04:00
|
|
|
cr, name, (int)sizeof(hostname), hostname, cr, cr);
|
1993-03-21 12:45:37 +03:00
|
|
|
jkfprintf(tp, name, offset);
|
|
|
|
(void)fclose(tp);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
1994-08-07 03:26:31 +04:00
|
|
|
void
|
2000-10-04 22:52:50 +04:00
|
|
|
jkfprintf(FILE *tp, char name[], off_t offset)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-03 06:27:58 +04:00
|
|
|
FILE *fi;
|
|
|
|
int linecnt, charcnt, inheader;
|
2001-03-14 07:39:42 +03:00
|
|
|
char line[BUFSIZ], visline[BUFSIZ*4], *nl;
|
1994-02-01 03:32:22 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((fi = fopen(name, "r")) == NULL)
|
|
|
|
return;
|
1994-08-07 03:26:31 +04:00
|
|
|
|
1997-08-25 23:31:43 +04:00
|
|
|
(void)fseek(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) {
|
2001-03-14 07:39:42 +03:00
|
|
|
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' ||
|
1998-07-03 06:27:58 +04:00
|
|
|
(strncasecmp(line, "From:", 5) &&
|
|
|
|
strncasecmp(line, "Subject:", 8)))
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
2001-03-17 00:39:08 +03:00
|
|
|
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;
|
|
|
|
}
|
2001-03-14 07:39:42 +03:00
|
|
|
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 */
|
1998-07-03 06:27:58 +04:00
|
|
|
(void)strvis(visline, line, VIS_CSTYLE);
|
2001-03-14 07:39:42 +03:00
|
|
|
(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
|
|
|
}
|