1 /* $NetBSD: comsat.c,v 1.10 1997/10/07 10:57:11 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #if 0 41 static char sccsid[] = "from: @(#)comsat.c 8.1 (Berkeley) 6/4/93"; 42 #else 43 __RCSID("$NetBSD: comsat.c,v 1.10 1997/10/07 10:57:11 mrg Exp $"); 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/socket.h> 49 #include <sys/stat.h> 50 #include <sys/file.h> 51 #include <sys/wait.h> 52 53 #include <netinet/in.h> 54 55 #include <ctype.h> 56 #include <errno.h> 57 #include <netdb.h> 58 #include <paths.h> 59 #include <pwd.h> 60 #include <signal.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <syslog.h> 65 #include <termios.h> 66 #include <unistd.h> 67 #include <utmp.h> 68 69 int debug = 0; 70 #define dsyslog if (debug) syslog 71 72 #define MAXIDLE 120 73 74 char hostname[MAXHOSTNAMELEN]; 75 struct utmp *utmp = NULL; 76 time_t lastmsgtime; 77 int nutmp, uf; 78 79 void jkfprintf __P((FILE *, char[], off_t)); 80 void mailfor __P((char *)); 81 void notify __P((struct utmp *, off_t)); 82 void onalrm __P((int)); 83 void reapchildren __P((int)); 84 int main __P((int, char *[])); 85 86 int 87 main(argc, argv) 88 int argc; 89 char *argv[]; 90 { 91 struct sockaddr_in from; 92 register int cc; 93 int fromlen; 94 char msgbuf[100]; 95 sigset_t sigset; 96 97 /* verify proper invocation */ 98 fromlen = sizeof(from); 99 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 100 (void)fprintf(stderr, 101 "comsat: getsockname: %s.\n", strerror(errno)); 102 exit(1); 103 } 104 openlog("comsat", LOG_PID, LOG_DAEMON); 105 if (chdir(_PATH_MAILDIR)) { 106 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR); 107 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 108 exit(1); 109 } 110 if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) { 111 syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP); 112 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 113 exit(1); 114 } 115 (void)time(&lastmsgtime); 116 (void)gethostname(hostname, sizeof(hostname)); 117 onalrm(0); 118 (void)signal(SIGALRM, onalrm); 119 (void)signal(SIGTTOU, SIG_IGN); 120 (void)signal(SIGCHLD, reapchildren); 121 for (;;) { 122 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 123 if (cc <= 0) { 124 if (errno != EINTR) 125 sleep(1); 126 errno = 0; 127 continue; 128 } 129 if (!nutmp) /* no one has logged in yet */ 130 continue; 131 sigemptyset(&sigset); 132 sigaddset(&sigset, SIGALRM); 133 sigprocmask(SIG_SETMASK, &sigset, NULL); 134 msgbuf[cc] = '\0'; 135 (void)time(&lastmsgtime); 136 mailfor(msgbuf); 137 sigemptyset(&sigset); 138 sigprocmask(SIG_SETMASK, &sigset, NULL); 139 } 140 } 141 142 void 143 reapchildren(signo) 144 int signo; 145 { 146 while (wait3(NULL, WNOHANG, NULL) > 0); 147 } 148 149 void 150 onalrm(signo) 151 int signo; 152 { 153 static u_int utmpsize; /* last malloced size for utmp */ 154 static u_int utmpmtime; /* last modification time for utmp */ 155 struct stat statbf; 156 157 if (time(NULL) - lastmsgtime >= MAXIDLE) 158 exit(0); 159 (void)alarm((u_int)15); 160 (void)fstat(uf, &statbf); 161 if (statbf.st_mtime > utmpmtime) { 162 utmpmtime = statbf.st_mtime; 163 if (statbf.st_size > utmpsize) { 164 utmpsize = statbf.st_size + 10 * sizeof(struct utmp); 165 if ((utmp = realloc(utmp, utmpsize)) == NULL) { 166 syslog(LOG_ERR, "%s", strerror(errno)); 167 exit(1); 168 } 169 } 170 (void)lseek(uf, (off_t)0, SEEK_SET); 171 nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp); 172 } 173 } 174 175 void 176 mailfor(name) 177 char *name; 178 { 179 register struct utmp *utp = &utmp[nutmp]; 180 register char *cp; 181 off_t offset; 182 183 if (!(cp = strchr(name, '@'))) 184 return; 185 *cp = '\0'; 186 offset = atoi(cp + 1); 187 while (--utp >= utmp) 188 if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name))) 189 notify(utp, offset); 190 } 191 192 static char *cr; 193 194 void 195 notify(utp, offset) 196 register struct utmp *utp; 197 off_t offset; 198 { 199 FILE *tp; 200 struct stat stb; 201 struct termios ttybuf; 202 char tty[20], name[sizeof(utmp[0].ut_name) + 1]; 203 204 (void)snprintf(tty, sizeof(tty), "%s%.*s", 205 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line); 206 if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) { 207 /* A slash is an attempt to break security... */ 208 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty); 209 return; 210 } 211 if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) { 212 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty); 213 return; 214 } 215 dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty); 216 if (fork()) 217 return; 218 (void)signal(SIGALRM, SIG_DFL); 219 (void)alarm((u_int)30); 220 if ((tp = fopen(tty, "w")) == NULL) { 221 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno)); 222 _exit(-1); 223 } 224 (void)tcgetattr(fileno(tp), &ttybuf); 225 cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ? 226 "\n" : "\n\r"; 227 (void)strncpy(name, utp->ut_name, sizeof(utp->ut_name)); 228 name[sizeof(name) - 1] = '\0'; 229 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s", 230 cr, name, (int)sizeof(hostname), hostname, cr, cr); 231 jkfprintf(tp, name, offset); 232 (void)fclose(tp); 233 _exit(0); 234 } 235 236 void 237 jkfprintf(tp, name, offset) 238 register FILE *tp; 239 char name[]; 240 off_t offset; 241 { 242 register char *cp, ch; 243 register FILE *fi; 244 register int linecnt, charcnt, inheader; 245 register struct passwd *p; 246 char line[BUFSIZ]; 247 248 /* Set effective uid to user in case mail drop is on nfs */ 249 if ((p = getpwnam(name)) != NULL) 250 (void) setuid(p->pw_uid); 251 252 if ((fi = fopen(name, "r")) == NULL) 253 return; 254 255 (void)fseek(fi, offset, SEEK_SET); 256 /* 257 * Print the first 7 lines or 560 characters of the new mail 258 * (whichever comes first). Skip header crap other than 259 * From, Subject, To, and Date. 260 */ 261 linecnt = 7; 262 charcnt = 560; 263 inheader = 1; 264 while (fgets(line, sizeof(line), fi) != NULL) { 265 if (inheader) { 266 if (line[0] == '\n') { 267 inheader = 0; 268 continue; 269 } 270 if (line[0] == ' ' || line[0] == '\t' || 271 (strncmp(line, "From:", 5) && 272 strncmp(line, "Subject:", 8))) 273 continue; 274 } 275 if (linecnt <= 0 || charcnt <= 0) { 276 (void)fprintf(tp, "...more...%s", cr); 277 (void)fclose(fi); 278 return; 279 } 280 /* strip weird stuff so can't trojan horse stupid terminals */ 281 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) { 282 ch = toascii(ch); 283 if (!isprint(ch) && !isspace(ch)) 284 ch |= 0x40; 285 (void)fputc(ch, tp); 286 } 287 (void)fputs(cr, tp); 288 --linecnt; 289 } 290 (void)fprintf(tp, "----%s\n", cr); 291 (void)fclose(fi); 292 } 293