* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)comsat.c 5.8 (Berkeley) %G%";
#define dsyslog if (debug) syslog
char hostname
[MAXHOSTNAMELEN
];
struct utmp
*utmp
= NULL
;
time_t lastmsgtime
, time();
int fromlen
, reapchildren(), onalrm();
/* verify proper invocation */
if (getsockname(0, &from
, &fromlen
) < 0) {
fprintf(stderr
, "%s: ", argv
[0]);
openlog("comsat", LOG_PID
, LOG_DAEMON
);
if (chdir("/usr/spool/mail")) {
syslog(LOG_ERR
, "chdir: /usr/spool/mail");
if ((uf
= open("/etc/utmp", O_RDONLY
, 0)) < 0) {
syslog(LOG_ERR
, ".main: /etc/utmp: %m");
(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((struct 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
)
dsyslog(LOG_DEBUG
, ".onalrm: alarm");
(void)fstat(uf
, &statbf
);
if (statbf
.st_mtime
> utmpmtime
) {
dsyslog(LOG_DEBUG
, ".onalrm: changed\n");
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
);
dsyslog(LOG_DEBUG
, ".onalrm: malloc failed");
(void)lseek(uf
, 0L, L_SET
);
nutmp
= read(uf
, utmp
, statbf
.st_size
)/sizeof(struct utmp
);
dsyslog(LOG_DEBUG
, ".onalrm: ok\n");
register struct utmp
*utp
= &utmp
[nutmp
];
dsyslog(LOG_DEBUG
, ".mailfor: mailfor %s\n", name
);
if (!(cp
= index(name
, '@'))) {
dsyslog(LOG_DEBUG
, ".mailfor: bad format\n");
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)strcpy(tty
, "/dev/");
(void)strncpy(tty
+ 5, utp
->ut_line
, sizeof(utp
->ut_line
));
dsyslog(LOG_DEBUG
, ".notify: notify %s on %s\n", utp
->ut_name
, tty
);
if (stat(tty
, &stb
) || !(stb
.st_mode
& S_IEXEC
)) {
dsyslog(LOG_DEBUG
, ".notify: wrong mode on tty");
(void)signal(SIGALRM
, SIG_DFL
);
if ((tp
= fopen(tty
, "w")) == NULL
) {
dsyslog(LOG_DEBUG
, ".notify: fopen of tty failed");
(void)ioctl(fileno(tp
), TIOCGETP
, >tybuf
);
cr
= (gttybuf
.sg_flags
&CRMOD
) && !(gttybuf
.sg_flags
&RAW
) ? "" : "\r";
(void)strncpy(name
, utp
->ut_name
, sizeof (utp
->ut_name
));
name
[sizeof (name
) - 1] = '\0';
fprintf(tp
, "%s\n\007New mail for %s@%.*s\007 has arrived:%s\n----%s\n",
cr
, name
, sizeof (hostname
), hostname
, cr
, cr
);
jkfprintf(tp
, name
, offset
);
jkfprintf(tp
, name
, offset
)
register int linecnt
, charcnt
, inheader
;
dsyslog(LOG_DEBUG
, ".jkfprint: HERE %s's mail starting at %d\n",
if ((fi
= fopen(name
, "r")) == NULL
) {
dsyslog(LOG_DEBUG
, ".jkfprintf: Can't read the mail\n");
(void)fseek(fi
, (long)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 (strncmp(line
, "From ", 5) == 0)
if (line
[0] == ' ' || line
[0] == '\t')
if (!(cp
= strpbrk(line
, ": ")) || *cp
== ' ')
else if (strncmp(line
, "From:", 5) &&
strncmp(line
, "Subject:", 8))
if (cp
= index(line
, '\n'))
fprintf(tp
, "%s%s\n", line
, cr
);
if (--linecnt
<= 0 || (charcnt
-= strlen(line
)) <= 0) {
fprintf(tp
, "...more...%s\n", cr
);
fprintf(tp
, "----%s\n", cr
);