17c478bd9Sstevel@tonic-gate /* 2*740638c8Sbw * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* 77c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T 87c478bd9Sstevel@tonic-gate * All Rights Reserved. 97c478bd9Sstevel@tonic-gate */ 107c478bd9Sstevel@tonic-gate 117c478bd9Sstevel@tonic-gate /* 127c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 137c478bd9Sstevel@tonic-gate * All rights reserved. 147c478bd9Sstevel@tonic-gate * 157c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted provided 167c478bd9Sstevel@tonic-gate * that: (1) source distributions retain this entire copyright notice and 177c478bd9Sstevel@tonic-gate * comment, and (2) distributions including binaries display the following 187c478bd9Sstevel@tonic-gate * acknowledgement: ``This product includes software developed by the 197c478bd9Sstevel@tonic-gate * University of California, Berkeley and its contributors'' in the 207c478bd9Sstevel@tonic-gate * documentation or other materials provided with the distribution and in 217c478bd9Sstevel@tonic-gate * all advertising materials mentioning features or use of this software. 227c478bd9Sstevel@tonic-gate * Neither the name of the University nor the names of its contributors may 237c478bd9Sstevel@tonic-gate * be used to endorse or promote products derived from this software without 247c478bd9Sstevel@tonic-gate * specific prior written permission. 257c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 267c478bd9Sstevel@tonic-gate * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 277c478bd9Sstevel@tonic-gate * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/socket.h> 357c478bd9Sstevel@tonic-gate #include <sys/stat.h> 367c478bd9Sstevel@tonic-gate #include <sys/wait.h> 377c478bd9Sstevel@tonic-gate #include <sys/file.h> 387c478bd9Sstevel@tonic-gate #include <fcntl.h> 397c478bd9Sstevel@tonic-gate #include <ctype.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <netinet/in.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <sys/ttold.h> 467c478bd9Sstevel@tonic-gate #include <utmpx.h> 477c478bd9Sstevel@tonic-gate #include <signal.h> 487c478bd9Sstevel@tonic-gate #include <errno.h> 497c478bd9Sstevel@tonic-gate #include <sys/param.h> /* for MAXHOSTNAMELEN */ 507c478bd9Sstevel@tonic-gate #include <netdb.h> 517c478bd9Sstevel@tonic-gate #include <syslog.h> 527c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 537c478bd9Sstevel@tonic-gate #include <pwd.h> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* 567c478bd9Sstevel@tonic-gate * comsat 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #ifndef UTMPX_FILE 617c478bd9Sstevel@tonic-gate #define UTMPX_FILE "/etc/utmpx" 627c478bd9Sstevel@tonic-gate #endif /* UTMPX_FILE */ 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate int debug = 0; 657c478bd9Sstevel@tonic-gate #define dsyslog if (debug) syslog 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate struct sockaddr_in sin = { AF_INET }; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN]; 707c478bd9Sstevel@tonic-gate struct utmpx *utmp = NULL; 717c478bd9Sstevel@tonic-gate int nutmp; 727c478bd9Sstevel@tonic-gate int uf; 737c478bd9Sstevel@tonic-gate unsigned utmpmtime = 0; /* last modification time for utmp */ 747c478bd9Sstevel@tonic-gate unsigned utmpsize = 0; /* last malloced size for utmp */ 757c478bd9Sstevel@tonic-gate time_t lastmsgtime; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate #ifndef SYSV 787c478bd9Sstevel@tonic-gate int reapchildren(); 797c478bd9Sstevel@tonic-gate #else 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate #define rindex strrchr 827c478bd9Sstevel@tonic-gate #define index strchr 837c478bd9Sstevel@tonic-gate #define signal(s, f) sigset((s), (f)) 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #ifndef sigmask 867c478bd9Sstevel@tonic-gate #define sigmask(m) (1 << ((m)-1)) 877c478bd9Sstevel@tonic-gate #endif 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate #define set2mask(setp) ((setp)->__sigbits[0]) 907c478bd9Sstevel@tonic-gate #define mask2set(mask, setp) \ 917c478bd9Sstevel@tonic-gate ((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask))) 927c478bd9Sstevel@tonic-gate 93*740638c8Sbw static int 94*740638c8Sbw sigsetmask(mask) 957c478bd9Sstevel@tonic-gate int mask; 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate sigset_t oset; 987c478bd9Sstevel@tonic-gate sigset_t nset; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate (void) sigprocmask(0, (sigset_t *)0, &nset); 1017c478bd9Sstevel@tonic-gate mask2set(mask, &nset); 1027c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &nset, &oset); 1037c478bd9Sstevel@tonic-gate return (set2mask(&oset)); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 106*740638c8Sbw static int 107*740638c8Sbw sigblock(mask) 1087c478bd9Sstevel@tonic-gate int mask; 1097c478bd9Sstevel@tonic-gate { 1107c478bd9Sstevel@tonic-gate sigset_t oset; 1117c478bd9Sstevel@tonic-gate sigset_t nset; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate (void) sigprocmask(0, (sigset_t *)0, &nset); 1147c478bd9Sstevel@tonic-gate mask2set(mask, &nset); 1157c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &nset, &oset); 1167c478bd9Sstevel@tonic-gate return (set2mask(&oset)); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate #endif /* SYSV */ 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate #define MAXIDLE 120 1237c478bd9Sstevel@tonic-gate #define NAMLEN (sizeof (uts[0].ut_name) + 1) 1247c478bd9Sstevel@tonic-gate 125*740638c8Sbw void jkfprintf(FILE *tp, char *name, int mbox, int offset); 126*740638c8Sbw void mailfor(char *name); 127*740638c8Sbw void notify(struct utmpx *utp, int offset); 128*740638c8Sbw void onalrm(int sig); 129*740638c8Sbw 130*740638c8Sbw int 1317c478bd9Sstevel@tonic-gate main(argc, argv) 1327c478bd9Sstevel@tonic-gate int argc; 1337c478bd9Sstevel@tonic-gate char *argv[]; 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate register int cc; 1367c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 1377c478bd9Sstevel@tonic-gate char msgbuf[100]; 1387c478bd9Sstevel@tonic-gate struct sockaddr_in from; 1397c478bd9Sstevel@tonic-gate socklen_t fromlen; 1407c478bd9Sstevel@tonic-gate int c; 1417c478bd9Sstevel@tonic-gate extern int optind; 1427c478bd9Sstevel@tonic-gate extern int getopt(); 1437c478bd9Sstevel@tonic-gate extern char *optarg; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate openlog("comsat", 0, LOG_DAEMON); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "d")) != -1) { 1487c478bd9Sstevel@tonic-gate switch ((char)c) { 1497c478bd9Sstevel@tonic-gate case'd': 1507c478bd9Sstevel@tonic-gate debug++; 1517c478bd9Sstevel@tonic-gate break; 1527c478bd9Sstevel@tonic-gate default: 1537c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "invalid argument %s", argv[optind]); 1547c478bd9Sstevel@tonic-gate exit(1); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* verify proper invocation */ 1597c478bd9Sstevel@tonic-gate fromlen = (socklen_t)sizeof (from); 1607c478bd9Sstevel@tonic-gate if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 1617c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: ", argv[0]); 1627c478bd9Sstevel@tonic-gate perror("getsockname"); 1637c478bd9Sstevel@tonic-gate _exit(1); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate #ifdef SYSV 1677c478bd9Sstevel@tonic-gate chdir("/var/mail"); 1687c478bd9Sstevel@tonic-gate #else 1697c478bd9Sstevel@tonic-gate chdir("/var/spool/mail"); 1707c478bd9Sstevel@tonic-gate #endif /* SYSV */ 1717c478bd9Sstevel@tonic-gate if ((uf = open(UTMPX_FILE, 0)) < 0) { 1727c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s: %m", UTMPX_FILE); 1737c478bd9Sstevel@tonic-gate (void) recv(0, msgbuf, sizeof (msgbuf) - 1, 0); 1747c478bd9Sstevel@tonic-gate exit(1); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate (void) time(&lastmsgtime); 1777c478bd9Sstevel@tonic-gate (void) gethostname(hostname, sizeof (hostname)); 178*740638c8Sbw onalrm(0); 179*740638c8Sbw (void) signal(SIGALRM, onalrm); 1807c478bd9Sstevel@tonic-gate (void) signal(SIGTTOU, SIG_IGN); 1817c478bd9Sstevel@tonic-gate #ifndef SYSV 1827c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, reapchildren); 1837c478bd9Sstevel@tonic-gate #else 1847c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, SIG_IGN); /* no zombies */ 1857c478bd9Sstevel@tonic-gate #endif /* SYSV */ 1867c478bd9Sstevel@tonic-gate for (;;) { 1877c478bd9Sstevel@tonic-gate cc = recv(0, msgbuf, sizeof (msgbuf) - 1, 0); 1887c478bd9Sstevel@tonic-gate if (cc <= 0) { 1897c478bd9Sstevel@tonic-gate if (errno != EINTR) 1907c478bd9Sstevel@tonic-gate sleep(1); 1917c478bd9Sstevel@tonic-gate errno = 0; 1927c478bd9Sstevel@tonic-gate continue; 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate if (nutmp == 0) /* no users (yet) */ 1957c478bd9Sstevel@tonic-gate continue; 1967c478bd9Sstevel@tonic-gate sigblock(sigmask(SIGALRM)); 1977c478bd9Sstevel@tonic-gate msgbuf[cc] = 0; 1987c478bd9Sstevel@tonic-gate (void) time(&lastmsgtime); 1997c478bd9Sstevel@tonic-gate mailfor(msgbuf); 2007c478bd9Sstevel@tonic-gate sigsetmask(0); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate #ifndef SYSV 2057c478bd9Sstevel@tonic-gate reapchildren() 2067c478bd9Sstevel@tonic-gate { 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate while (wait3((struct wait *)0, WNOHANG, (struct rusage *)0) > 0) 2097c478bd9Sstevel@tonic-gate ; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate #endif /* SYSV */ 2127c478bd9Sstevel@tonic-gate 213*740638c8Sbw /* ARGSUSED */ 214*740638c8Sbw void 215*740638c8Sbw onalrm(int sig) 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate struct stat statbf; 2187c478bd9Sstevel@tonic-gate time_t now; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate (void) time(&now); 2217c478bd9Sstevel@tonic-gate if ((ulong_t)now - (ulong_t)lastmsgtime >= MAXIDLE) 2227c478bd9Sstevel@tonic-gate exit(0); 2237c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "alarm\n"); 2247c478bd9Sstevel@tonic-gate alarm(15); 2257c478bd9Sstevel@tonic-gate fstat(uf, &statbf); 2267c478bd9Sstevel@tonic-gate if (statbf.st_mtime > utmpmtime) { 2277c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, " changed\n"); 2287c478bd9Sstevel@tonic-gate utmpmtime = statbf.st_mtime; 2297c478bd9Sstevel@tonic-gate if (statbf.st_size > utmpsize) { 2307c478bd9Sstevel@tonic-gate utmpsize = statbf.st_size + 10 * sizeof (struct utmpx); 2317c478bd9Sstevel@tonic-gate if (utmp) 2327c478bd9Sstevel@tonic-gate utmp = (struct utmpx *)realloc(utmp, utmpsize); 2337c478bd9Sstevel@tonic-gate else 2347c478bd9Sstevel@tonic-gate utmp = (struct utmpx *)malloc(utmpsize); 2357c478bd9Sstevel@tonic-gate if (! utmp) { 2367c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "malloc failed\n"); 2377c478bd9Sstevel@tonic-gate exit(1); 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate lseek(uf, 0, 0); 2417c478bd9Sstevel@tonic-gate nutmp = read(uf, utmp, statbf.st_size)/sizeof (struct utmpx); 2427c478bd9Sstevel@tonic-gate } else 2437c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, " ok\n"); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 246*740638c8Sbw void 2477c478bd9Sstevel@tonic-gate mailfor(name) 2487c478bd9Sstevel@tonic-gate char *name; 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate struct utmpx *utp = &utmp[nutmp]; 2517c478bd9Sstevel@tonic-gate register char *cp; 2527c478bd9Sstevel@tonic-gate char *rindex(); 2537c478bd9Sstevel@tonic-gate int offset; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * Don't bother doing anything if nobody is 2577c478bd9Sstevel@tonic-gate * logged into the system. 2587c478bd9Sstevel@tonic-gate */ 2597c478bd9Sstevel@tonic-gate if (utmp == NULL || nutmp == 0) 2607c478bd9Sstevel@tonic-gate return; 2617c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "mailfor %s\n", name); 2627c478bd9Sstevel@tonic-gate cp = name; 2637c478bd9Sstevel@tonic-gate while (*cp && *cp != '@') 2647c478bd9Sstevel@tonic-gate cp++; 2657c478bd9Sstevel@tonic-gate if (*cp == 0) { 2667c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "bad format\n"); 2677c478bd9Sstevel@tonic-gate return; 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate *cp = 0; 2707c478bd9Sstevel@tonic-gate offset = atoi(cp+1); 2717c478bd9Sstevel@tonic-gate while (--utp >= utmp) 2727c478bd9Sstevel@tonic-gate if ((utp->ut_type == USER_PROCESS) && 2737c478bd9Sstevel@tonic-gate (!strncmp(utp->ut_name, name, sizeof (utmp[0].ut_name)))) 2747c478bd9Sstevel@tonic-gate notify(utp, offset); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate char *cr; 2787c478bd9Sstevel@tonic-gate 279*740638c8Sbw void 2807c478bd9Sstevel@tonic-gate notify(utp, offset) 2817c478bd9Sstevel@tonic-gate struct utmpx *utp; 2827c478bd9Sstevel@tonic-gate { 2837c478bd9Sstevel@tonic-gate FILE *tp; 2847c478bd9Sstevel@tonic-gate struct sgttyb gttybuf; 2857c478bd9Sstevel@tonic-gate char tty[sizeof (utmp[0].ut_line) + 5]; 2867c478bd9Sstevel@tonic-gate char name[sizeof (utmp[0].ut_name) + 1]; 2877c478bd9Sstevel@tonic-gate struct stat stb, stl; 2887c478bd9Sstevel@tonic-gate time_t timep[2]; 2897c478bd9Sstevel@tonic-gate struct passwd *pwd; 2907c478bd9Sstevel@tonic-gate int fd, mbox; 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate strcpy(tty, "/dev/"); 2947c478bd9Sstevel@tonic-gate strncat(tty, utp->ut_line, sizeof (utp->ut_line)); 2957c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty); 2967c478bd9Sstevel@tonic-gate if (stat(tty, &stb) == -1) { 2977c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "can't stat tty\n"); 2987c478bd9Sstevel@tonic-gate return; 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate if ((stb.st_mode & 0100) == 0) { 3017c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "wrong mode\n"); 3027c478bd9Sstevel@tonic-gate return; 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate if (fork()) 3057c478bd9Sstevel@tonic-gate return; 3067c478bd9Sstevel@tonic-gate signal(SIGALRM, SIG_DFL); 3077c478bd9Sstevel@tonic-gate alarm(30); 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate strncpy(name, utp->ut_name, sizeof (utp->ut_name)); 3107c478bd9Sstevel@tonic-gate name[sizeof (name) - 1] = '\0'; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate /* 3137c478bd9Sstevel@tonic-gate * Do all operations that check protections as the user who 3147c478bd9Sstevel@tonic-gate * will be getting the biff. 3157c478bd9Sstevel@tonic-gate */ 3167c478bd9Sstevel@tonic-gate if ((pwd = getpwnam(name)) == (struct passwd *)-1) { 3177c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "getpwnam failed\n"); 3187c478bd9Sstevel@tonic-gate exit(1); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate if (setuid(pwd->pw_uid) == -1) { 3217c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "setuid failed\n"); 3227c478bd9Sstevel@tonic-gate exit(1); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * We need to make sure that the tty listed in the utmp 3277c478bd9Sstevel@tonic-gate * file really is a tty device so that a corrupted utmp 3287c478bd9Sstevel@tonic-gate * file doesn't cause us to over-write a real file. 3297c478bd9Sstevel@tonic-gate */ 3307c478bd9Sstevel@tonic-gate if ((fd = open(tty, O_RDWR)) == -1) { 3317c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "can't open tty"); 3327c478bd9Sstevel@tonic-gate exit(1); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate if (isatty(fd) == 0) { 3357c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "line listed in utmp file is not a tty\n"); 3367c478bd9Sstevel@tonic-gate exit(1); 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate /* 3407c478bd9Sstevel@tonic-gate * For the case where the user getting the biff is root, 3417c478bd9Sstevel@tonic-gate * we need to make sure that the tty we will be sending 3427c478bd9Sstevel@tonic-gate * the biff to is also owned by root. 3437c478bd9Sstevel@tonic-gate * 3447c478bd9Sstevel@tonic-gate * Check after open, to prevent race on open. 3457c478bd9Sstevel@tonic-gate */ 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate if (fstat(fd, &stb) != 0 || stb.st_uid != pwd->pw_uid) { 3487c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, 3497c478bd9Sstevel@tonic-gate "tty is not owned by user getting the biff\n"); 3507c478bd9Sstevel@tonic-gate exit(1); 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* 3547c478bd9Sstevel@tonic-gate * Prevent race by doing fdopen on fd, not fopen 3557c478bd9Sstevel@tonic-gate * Fopen opens w/ O_CREAT, which is dangerous too 3567c478bd9Sstevel@tonic-gate */ 3577c478bd9Sstevel@tonic-gate if ((tp = fdopen(fd, "w")) == 0) { 3587c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "fdopen failed\n"); 3597c478bd9Sstevel@tonic-gate exit(-1); 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate if (ioctl(fd, TIOCGETP, >tybuf) == -1) { 3637c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "ioctl TIOCGETP failed\n"); 3647c478bd9Sstevel@tonic-gate exit(1); 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r"; 3677c478bd9Sstevel@tonic-gate fprintf(tp, "%s\n\007New mail for %s@%.*s\007 has arrived:%s\n", 3687c478bd9Sstevel@tonic-gate cr, name, sizeof (hostname), hostname, cr); 3697c478bd9Sstevel@tonic-gate fprintf(tp, "----%s\n", cr); 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate if ((mbox = open(name, O_RDONLY)) == -1) { 3727c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "can't open mailbox for %s", name); 3737c478bd9Sstevel@tonic-gate exit(1); 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate /* 3767c478bd9Sstevel@tonic-gate * In case of a worldwritable mail spool directory, we must take 3777c478bd9Sstevel@tonic-gate * care we don't open and read from the wrong file. 3787c478bd9Sstevel@tonic-gate */ 3797c478bd9Sstevel@tonic-gate if (fstat(mbox, &stb) == -1 || lstat(name, &stl) == -1) { 3807c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "stat() failed on mail file\n"); 3817c478bd9Sstevel@tonic-gate exit(1); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate /* 3857c478bd9Sstevel@tonic-gate * Here we make sure that the file wasn't a hardlink or softlink 3867c478bd9Sstevel@tonic-gate * while we opened it and that it wasn't changed afterwards 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate if (!S_ISREG(stl.st_mode) || 3897c478bd9Sstevel@tonic-gate stl.st_dev != stb.st_dev || 3907c478bd9Sstevel@tonic-gate stl.st_ino != stb.st_ino || 3917c478bd9Sstevel@tonic-gate stl.st_uid != pwd->pw_uid || 3927c478bd9Sstevel@tonic-gate stb.st_nlink != 1) { 3937c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "mail spool file must be plain file\n"); 3947c478bd9Sstevel@tonic-gate exit(1); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate timep[0] = stb.st_atime; 3987c478bd9Sstevel@tonic-gate timep[1] = stb.st_mtime; 3997c478bd9Sstevel@tonic-gate jkfprintf(tp, name, mbox, offset); 4007c478bd9Sstevel@tonic-gate utime(name, timep); 4017c478bd9Sstevel@tonic-gate exit(0); 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate 404*740638c8Sbw void 4057c478bd9Sstevel@tonic-gate jkfprintf(tp, name, mbox, offset) 4067c478bd9Sstevel@tonic-gate register FILE *tp; 4077c478bd9Sstevel@tonic-gate char *name; 4087c478bd9Sstevel@tonic-gate int mbox; 4097c478bd9Sstevel@tonic-gate { 4107c478bd9Sstevel@tonic-gate register FILE *fi; 4117c478bd9Sstevel@tonic-gate register int linecnt, charcnt; 4127c478bd9Sstevel@tonic-gate char line[BUFSIZ]; 4137c478bd9Sstevel@tonic-gate int inheader; 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "HERE %s's mail starting at %d\n", 4167c478bd9Sstevel@tonic-gate name, offset); 4177c478bd9Sstevel@tonic-gate if ((fi = fdopen(mbox, "r")) == NULL) { 4187c478bd9Sstevel@tonic-gate dsyslog(LOG_DEBUG, "Cant read the mail\n"); 4197c478bd9Sstevel@tonic-gate return; 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate fseek(fi, offset, L_SET); 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate /* 4257c478bd9Sstevel@tonic-gate * Print the first 7 lines or 560 characters of the new mail 4267c478bd9Sstevel@tonic-gate * (whichever comes first). Skip header crap other than 4277c478bd9Sstevel@tonic-gate * From, Subject, To, and Date. 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate linecnt = 7; 4307c478bd9Sstevel@tonic-gate charcnt = 560; 4317c478bd9Sstevel@tonic-gate inheader = 1; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fi) != NULL) { 4357c478bd9Sstevel@tonic-gate register char *cp; 4367c478bd9Sstevel@tonic-gate char *index(); 4377c478bd9Sstevel@tonic-gate int cnt; 4387c478bd9Sstevel@tonic-gate int i; 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate if (linecnt <= 0 || charcnt <= 0) { 4417c478bd9Sstevel@tonic-gate fprintf(tp, "...more...%s\n", cr); 4427c478bd9Sstevel@tonic-gate return; 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate if (strncmp(line, "From ", 5) == 0) 4457c478bd9Sstevel@tonic-gate continue; 4467c478bd9Sstevel@tonic-gate if (inheader && (line[0] == ' ' || line[0] == '\t')) 4477c478bd9Sstevel@tonic-gate continue; 4487c478bd9Sstevel@tonic-gate cp = index(line, ':'); 4497c478bd9Sstevel@tonic-gate if (cp == 0 || (index(line, ' ') && index(line, ' ') < cp)) 4507c478bd9Sstevel@tonic-gate inheader = 0; 4517c478bd9Sstevel@tonic-gate else 4527c478bd9Sstevel@tonic-gate cnt = cp - line; 4537c478bd9Sstevel@tonic-gate if (inheader && 4547c478bd9Sstevel@tonic-gate strncmp(line, "Date", cnt) && 4557c478bd9Sstevel@tonic-gate strncmp(line, "From", cnt) && 4567c478bd9Sstevel@tonic-gate strncmp(line, "Subject", cnt) && 4577c478bd9Sstevel@tonic-gate strncmp(line, "To", cnt)) 4587c478bd9Sstevel@tonic-gate continue; 4597c478bd9Sstevel@tonic-gate cp = index(line, '\n'); 4607c478bd9Sstevel@tonic-gate if (cp) 4617c478bd9Sstevel@tonic-gate *cp = '\0'; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate for (i = strlen(line); i-- > 0; ) 4647c478bd9Sstevel@tonic-gate if (!isprint(line[i])) 4657c478bd9Sstevel@tonic-gate line[i] = ' '; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate fprintf(tp, "%s%s\n", line, cr); 4697c478bd9Sstevel@tonic-gate linecnt--, charcnt -= strlen(line); 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate fprintf(tp, "----%s\n", cr); 4727c478bd9Sstevel@tonic-gate } 473