1994-05-27 12:39:25 +00:00
|
|
|
/*
|
|
|
|
* 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. 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-11-20 07:23:44 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:39:25 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-11-20 07:23:44 +00:00
|
|
|
#if 0
|
1994-05-27 12:39:25 +00:00
|
|
|
static char sccsid[] = "@(#)comsat.c 8.1 (Berkeley) 6/4/93";
|
1997-11-20 07:23:44 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-27 12:39:25 +00: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 <ctype.h>
|
1997-11-20 07:23:44 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
1996-09-07 02:05:53 +00:00
|
|
|
#include <termios.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
2010-01-13 18:25:43 +00:00
|
|
|
#include <utmpx.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
int debug = 0;
|
|
|
|
#define dsyslog if (debug) syslog
|
|
|
|
|
|
|
|
#define MAXIDLE 120
|
|
|
|
|
|
|
|
char hostname[MAXHOSTNAMELEN];
|
|
|
|
|
2002-02-03 15:53:02 +00:00
|
|
|
void jkfprintf(FILE *, char[], char[], off_t);
|
|
|
|
void mailfor(char *);
|
2010-01-13 18:25:43 +00:00
|
|
|
void notify(struct utmpx *, char[], off_t, int);
|
2002-02-03 15:53:02 +00:00
|
|
|
void reapchildren(int);
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
int
|
2010-01-02 09:45:59 +00:00
|
|
|
main(int argc __unused, char *argv[] __unused)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in from;
|
2005-02-14 17:42:58 +00:00
|
|
|
socklen_t fromlen;
|
2002-02-03 15:53:02 +00:00
|
|
|
int cc;
|
1994-10-15 17:39:23 +00:00
|
|
|
char msgbuf[256];
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
/* verify proper invocation */
|
|
|
|
fromlen = sizeof(from);
|
1997-11-20 07:23:44 +00:00
|
|
|
if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
|
|
|
|
err(1, "getsockname");
|
1994-05-27 12:39:25 +00:00
|
|
|
openlog("comsat", LOG_PID, LOG_DAEMON);
|
|
|
|
if (chdir(_PATH_MAILDIR)) {
|
|
|
|
syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
|
|
|
|
(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
(void)gethostname(hostname, sizeof(hostname));
|
|
|
|
(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;
|
|
|
|
}
|
|
|
|
msgbuf[cc] = '\0';
|
|
|
|
mailfor(msgbuf);
|
|
|
|
sigsetmask(0L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-02 09:45:59 +00:00
|
|
|
reapchildren(int signo __unused)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
while (wait3(NULL, WNOHANG, NULL) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-03 15:53:02 +00:00
|
|
|
mailfor(char *name)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
2010-01-13 18:25:43 +00:00
|
|
|
struct utmpx *utp;
|
2002-02-03 15:53:02 +00:00
|
|
|
char *cp;
|
1994-10-15 17:39:23 +00:00
|
|
|
char *file;
|
1994-05-27 12:39:25 +00:00
|
|
|
off_t offset;
|
1994-10-15 17:39:23 +00:00
|
|
|
int folder;
|
2010-01-13 18:25:43 +00:00
|
|
|
char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
|
|
|
|
char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
if (!(cp = strchr(name, '@')))
|
|
|
|
return;
|
|
|
|
*cp = '\0';
|
2001-09-03 05:06:48 +00:00
|
|
|
offset = strtoll(cp + 1, NULL, 10);
|
1994-10-15 17:39:23 +00:00
|
|
|
if (!(cp = strchr(cp + 1, ':')))
|
|
|
|
file = name;
|
|
|
|
else
|
|
|
|
file = cp + 1;
|
2010-01-13 18:25:43 +00:00
|
|
|
sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
|
1998-06-30 15:19:51 +00:00
|
|
|
name);
|
1994-10-15 17:39:23 +00:00
|
|
|
if (*file != '/') {
|
1998-06-30 15:19:51 +00:00
|
|
|
sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
|
2010-01-13 18:25:43 +00:00
|
|
|
(int)sizeof(utp->ut_user), file);
|
1994-10-15 17:39:23 +00:00
|
|
|
file = buf2;
|
|
|
|
}
|
|
|
|
folder = strcmp(buf, file);
|
2010-01-13 18:25:43 +00:00
|
|
|
setutxent();
|
|
|
|
while ((utp = getutxent()) != NULL)
|
|
|
|
if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
|
1994-10-15 17:39:23 +00:00
|
|
|
notify(utp, file, offset, folder);
|
2010-01-13 18:25:43 +00:00
|
|
|
endutxent();
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
|
2010-01-02 09:45:59 +00:00
|
|
|
static const char *cr;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
void
|
2010-01-13 18:25:43 +00:00
|
|
|
notify(struct utmpx *utp, char file[], off_t offset, int folder)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
FILE *tp;
|
|
|
|
struct stat stb;
|
1996-09-07 02:05:53 +00:00
|
|
|
struct termios tio;
|
2010-01-13 18:25:43 +00:00
|
|
|
char tty[20];
|
|
|
|
const char *s = utp->ut_line;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2010-01-13 18:25:43 +00:00
|
|
|
if (strncmp(s, "pts/", 4) == 0)
|
|
|
|
s += 4;
|
|
|
|
if (strchr(s, '/')) {
|
1994-05-27 12:39:25 +00:00
|
|
|
/* A slash is an attempt to break security... */
|
2009-01-17 15:56:38 +00:00
|
|
|
syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
|
|
|
|
utp->ut_line);
|
1994-05-27 12:39:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-01-17 15:56:38 +00:00
|
|
|
(void)snprintf(tty, sizeof(tty), "%s%.*s",
|
|
|
|
_PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
|
|
|
|
if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
|
2010-01-13 18:25:43 +00:00
|
|
|
dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
|
1994-05-27 12:39:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-13 18:25:43 +00:00
|
|
|
dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_user, tty);
|
2009-01-17 15:56:38 +00:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
syslog(LOG_NOTICE, "fork failed (%m)");
|
|
|
|
return;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
1994-05-27 12:39:25 +00:00
|
|
|
return;
|
2009-01-17 15:56:38 +00:00
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
if ((tp = fopen(tty, "w")) == NULL) {
|
|
|
|
dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
|
1997-11-20 07:23:44 +00:00
|
|
|
_exit(1);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
1996-09-07 02:05:53 +00:00
|
|
|
(void)tcgetattr(fileno(tp), &tio);
|
1996-09-07 02:17:29 +00:00
|
|
|
cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ? "\n" : "\n\r";
|
2002-07-09 02:16:49 +00:00
|
|
|
switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
|
|
|
|
case S_IXUSR:
|
|
|
|
case (S_IXUSR | S_IXGRP):
|
|
|
|
(void)fprintf(tp,
|
|
|
|
"%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
|
2010-01-13 18:25:43 +00:00
|
|
|
cr, utp->ut_user, (int)sizeof(hostname), hostname,
|
2002-07-09 02:16:49 +00:00
|
|
|
folder ? cr : "", folder ? "to " : "", folder ? file : "",
|
|
|
|
cr, cr);
|
2010-01-13 18:25:43 +00:00
|
|
|
jkfprintf(tp, utp->ut_user, file, offset);
|
2002-07-09 02:16:49 +00:00
|
|
|
break;
|
|
|
|
case S_IXGRP:
|
|
|
|
(void)fprintf(tp, "\007");
|
|
|
|
(void)fflush(tp);
|
|
|
|
(void)sleep(1);
|
|
|
|
(void)fprintf(tp, "\007");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
(void)fclose(tp);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-03 15:53:02 +00:00
|
|
|
jkfprintf(FILE *tp, char user[], char file[], off_t offset)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
2002-02-03 15:53:02 +00:00
|
|
|
unsigned char *cp, ch;
|
|
|
|
FILE *fi;
|
|
|
|
int linecnt, charcnt, inheader;
|
|
|
|
struct passwd *p;
|
1997-09-15 00:27:49 +00:00
|
|
|
unsigned char line[BUFSIZ];
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
/* Set effective uid to user in case mail drop is on nfs */
|
1996-06-04 15:42:09 +00:00
|
|
|
if ((p = getpwnam(user)) != NULL)
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) setuid(p->pw_uid);
|
|
|
|
|
1996-06-04 15:42:09 +00:00
|
|
|
if ((fi = fopen(file, "r")) == NULL)
|
1994-05-27 12:39:25 +00:00
|
|
|
return;
|
|
|
|
|
2001-09-03 05:06:48 +00:00
|
|
|
(void)fseeko(fi, offset, SEEK_CUR);
|
1994-05-27 12:39:25 +00: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) {
|
|
|
|
if (inheader) {
|
|
|
|
if (line[0] == '\n') {
|
|
|
|
inheader = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line[0] == ' ' || line[0] == '\t' ||
|
1997-11-20 07:23:44 +00:00
|
|
|
(strncmp(line, "From:", 5) &&
|
|
|
|
strncmp(line, "Subject:", 8)))
|
1994-05-27 12:39:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (linecnt <= 0 || charcnt <= 0) {
|
|
|
|
(void)fprintf(tp, "...more...%s", cr);
|
|
|
|
(void)fclose(fi);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* strip weird stuff so can't trojan horse stupid terminals */
|
|
|
|
for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
|
1997-09-15 00:27:49 +00:00
|
|
|
/* disable upper controls and enable all other
|
|
|
|
8bit codes due to lack of locale knowledge
|
|
|
|
*/
|
|
|
|
if (((ch & 0x80) && ch < 0xA0) ||
|
|
|
|
(!(ch & 0x80) && !isprint(ch) &&
|
1997-09-15 00:42:04 +00:00
|
|
|
!isspace(ch) && ch != '\a' && ch != '\b')
|
1997-09-15 00:27:49 +00:00
|
|
|
) {
|
|
|
|
if (ch & 0x80) {
|
|
|
|
ch &= ~0x80;
|
1994-10-15 17:39:23 +00:00
|
|
|
(void)fputs("M-", tp);
|
1997-09-15 00:27:49 +00:00
|
|
|
}
|
|
|
|
if (iscntrl(ch)) {
|
|
|
|
ch ^= 0x40;
|
1994-10-15 17:39:23 +00:00
|
|
|
(void)fputc('^', tp);
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
(void)fputc(ch, tp);
|
|
|
|
}
|
|
|
|
(void)fputs(cr, tp);
|
|
|
|
--linecnt;
|
|
|
|
}
|
|
|
|
(void)fprintf(tp, "----%s\n", cr);
|
|
|
|
(void)fclose(fi);
|
|
|
|
}
|