* Copyright (c) 1980 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not 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 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)comsat.c 5.12 (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((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] = "/dev/";
char name
[sizeof (utmp
[0].ut_name
) + 1];
(void)strncpy(tty
+ 5, utp
->ut_line
, sizeof(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
) ? "" : "\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
;
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 (cp
= index(line
, '\n'))
fprintf(tp
, "%s%s\n", line
, cr
);
if (--linecnt
<= 0 || charcnt
<= 0) {
fprintf(tp
, "...more...%s\n", cr
);
fprintf(tp
, "----%s\n", cr
);