* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char copyright
[] =
"@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)comsat.c 8.1 (Berkeley) %G%";
#define dsyslog if (debug) syslog
char hostname
[MAXHOSTNAMELEN
];
struct utmp
*utmp
= NULL
;
void jkfprintf
__P((FILE *, char[], off_t
));
void mailfor
__P((char *));
void notify
__P((struct utmp
*, off_t
));
void reapchildren
__P((int));
/* verify proper invocation */
if (getsockname(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
"comsat: getsockname: %s.\n", strerror(errno
));
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);
if ((uf
= open(_PATH_UTMP
, O_RDONLY
, 0)) < 0) {
syslog(LOG_ERR
, "open: %s: %m", _PATH_UTMP
);
(void) recv(0, msgbuf
, sizeof(msgbuf
) - 1, 0);
(void)time(&lastmsgtime
);
(void)gethostname(hostname
, sizeof(hostname
));
(void)signal(SIGALRM
, onalrm
);
(void)signal(SIGTTOU
, SIG_IGN
);
(void)signal(SIGCHLD
, reapchildren
);
cc
= recv(0, msgbuf
, sizeof(msgbuf
) - 1, 0);
if (!nutmp
) /* no one has logged in yet */
sigblock(sigmask(SIGALRM
));
(void)time(&lastmsgtime
);
while (wait3(NULL
, WNOHANG
, NULL
) > 0);
static u_int utmpsize
; /* last malloced size for utmp */
static u_int utmpmtime
; /* last modification time for utmp */
if (time(NULL
) - lastmsgtime
>= MAXIDLE
)
(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
);
if ((utmp
= realloc(utmp
, utmpsize
)) == NULL
) {
syslog(LOG_ERR
, "%s", strerror(errno
));
(void)lseek(uf
, (off_t
)0, L_SET
);
nutmp
= read(uf
, utmp
, (int)statbf
.st_size
)/sizeof(struct utmp
);
register struct utmp
*utp
= &utmp
[nutmp
];
if (!(cp
= strchr(name
, '@')))
if (!strncmp(utp
->ut_name
, name
, sizeof(utmp
[0].ut_name
)))
register struct utmp
*utp
;
char tty
[20], name
[sizeof(utmp
[0].ut_name
) + 1];
(void)snprintf(tty
, sizeof(tty
), "%s%.*s",
_PATH_DEV
, (int)sizeof(utp
->ut_line
), utp
->ut_line
);
if (strchr(tty
+ sizeof(_PATH_DEV
) - 1, '/')) {
/* A slash is an attempt to break security... */
syslog(LOG_AUTH
| LOG_NOTICE
, "'/' in \"%s\"", tty
);
if (stat(tty
, &stb
) || !(stb
.st_mode
& S_IEXEC
)) {
dsyslog(LOG_DEBUG
, "%s: wrong mode on %s", utp
->ut_name
, tty
);
dsyslog(LOG_DEBUG
, "notify %s on %s\n", utp
->ut_name
, tty
);
(void)signal(SIGALRM
, SIG_DFL
);
if ((tp
= fopen(tty
, "w")) == NULL
) {
dsyslog(LOG_ERR
, "%s: %s", tty
, strerror(errno
));
(void)ioctl(fileno(tp
), TIOCGETP
, >tybuf
);
cr
= (gttybuf
.sg_flags
&CRMOD
) && !(gttybuf
.sg_flags
&RAW
) ?
(void)strncpy(name
, utp
->ut_name
, sizeof(utp
->ut_name
));
name
[sizeof(name
) - 1] = '\0';
(void)fprintf(tp
, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
cr
, name
, (int)sizeof(hostname
), hostname
, cr
, cr
);
jkfprintf(tp
, name
, offset
);
jkfprintf(tp
, name
, offset
)
register int linecnt
, charcnt
, inheader
;
register struct passwd
*p
;
/* Set effective uid to user in case mail drop is on nfs */
if ((p
= getpwnam(name
)) != NULL
)
(void) setuid(p
->pw_uid
);
if ((fi
= fopen(name
, "r")) == NULL
)
(void)fseek(fi
, offset
, L_SET
);
* 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.
while (fgets(line
, sizeof(line
), fi
) != NULL
) {
if (line
[0] == ' ' || line
[0] == '\t' ||
strncmp(line
, "From:", 5) &&
strncmp(line
, "Subject:", 8))
if (linecnt
<= 0 || charcnt
<= 0) {
(void)fprintf(tp
, "...more...%s", cr
);
/* strip weird stuff so can't trojan horse stupid terminals */
for (cp
= line
; (ch
= *cp
) && ch
!= '\n'; ++cp
, --charcnt
) {
if (!isprint(ch
) && !isspace(ch
))
(void)fprintf(tp
, "----%s\n", cr
);