* Copyright (c) 1980 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)comsat.c 5.22 (Berkeley) 6/24/90";
#define dsyslog if (debug) syslog
char hostname
[MAXHOSTNAMELEN
];
struct utmp
*utmp
= NULL
;
time_t lastmsgtime
, time();
void onalrm(), reapchildren();
/* verify proper invocation */
if (getsockname(0, &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
);
if ((uf
= open(_PATH_UTMP
, O_RDONLY
, 0)) < 0) {
syslog(LOG_ERR
, ".main: %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((union wait
*)NULL
, WNOHANG
, (struct rusage
*)NULL
) > 0);
static u_int utmpsize
; /* last malloced size for utmp */
static u_int utmpmtime
; /* last modification time for utmp */
char *malloc(), *realloc();
if (time((time_t *)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
);
utmp
= (struct utmp
*)realloc((char *)utmp
, utmpsize
);
utmp
= (struct utmp
*)malloc(utmpsize
);
syslog(LOG_ERR
, "malloc failed");
(void)lseek(uf
, 0L, L_SET
);
nutmp
= read(uf
, utmp
, (int)statbf
.st_size
)/sizeof(struct utmp
);
register struct utmp
*utp
= &utmp
[nutmp
];
if (!(cp
= index(name
, '@')))
if (!strncmp(utp
->ut_name
, name
, sizeof(utmp
[0].ut_name
)))
register struct utmp
*utp
;
static char tty
[20] = _PATH_DEV
;
char name
[sizeof(utmp
[0].ut_name
) + 1];
(void)strncpy(tty
+ sizeof(_PATH_DEV
) - 1, utp
->ut_line
,
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
, "fopen of tty %s failed", tty
);
(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
, sizeof(hostname
), hostname
, cr
, cr
);
jkfprintf(tp
, name
, offset
);
jkfprintf(tp
, name
, offset
)
register int linecnt
, charcnt
, inheader
;
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
);