1 /* $OpenBSD: comsat.c,v 1.17 2001/07/08 21:18:06 deraadt 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 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1980, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 /*static char sccsid[] = "from: @(#)comsat.c 8.1 (Berkeley) 6/4/93";*/ 44 static char rcsid[] = "$OpenBSD: comsat.c,v 1.17 2001/07/08 21:18:06 deraadt Exp $"; 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/socket.h> 49 #include <sys/stat.h> 50 #include <sys/wait.h> 51 52 #include <netinet/in.h> 53 54 #include <ctype.h> 55 #include <errno.h> 56 #include <fcntl.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 #include <vis.h> 69 70 int debug = 0; 71 #define dsyslog if (debug) syslog 72 73 #define MAXIDLE 120 74 75 char hostname[MAXHOSTNAMELEN]; 76 struct utmp *utmp = NULL; 77 time_t lastmsgtime; 78 int nutmp, uf; 79 80 void jkfprintf __P((FILE *, char[], off_t)); 81 void mailfor __P((char *)); 82 void notify __P((struct utmp *, off_t)); 83 void readutmp __P((int)); 84 void doreadutmp __P((void)); 85 void reapchildren __P((int)); 86 87 sig_atomic_t wantreadutmp; 88 89 int 90 main(argc, argv) 91 int argc; 92 char *argv[]; 93 { 94 struct sockaddr_storage from; 95 register int cc; 96 int fromlen; 97 char msgbuf[100]; 98 sigset_t sigset; 99 100 /* verify proper invocation */ 101 fromlen = sizeof(from); 102 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 103 (void)fprintf(stderr, 104 "comsat: getsockname: %s.\n", strerror(errno)); 105 exit(1); 106 } 107 openlog("comsat", LOG_PID, LOG_DAEMON); 108 if (chdir(_PATH_MAILDIR)) { 109 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR); 110 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 111 exit(1); 112 } 113 if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) { 114 syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP); 115 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 116 exit(1); 117 } 118 (void)time(&lastmsgtime); 119 (void)gethostname(hostname, sizeof(hostname)); 120 doreadutmp(); 121 (void)signal(SIGALRM, readutmp); 122 (void)signal(SIGTTOU, SIG_IGN); 123 (void)signal(SIGCHLD, reapchildren); 124 for (;;) { 125 if (wantreadutmp) { 126 doreadutmp(); 127 wantreadutmp = 0; 128 } 129 130 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 131 if (cc <= 0) { 132 if (errno != EINTR) 133 sleep(1); 134 continue; 135 } 136 if (!nutmp) /* no one has logged in yet */ 137 continue; 138 sigemptyset(&sigset); 139 sigaddset(&sigset, SIGALRM); 140 sigprocmask(SIG_SETMASK, &sigset, NULL); 141 msgbuf[cc] = '\0'; 142 (void)time(&lastmsgtime); 143 mailfor(msgbuf); 144 sigemptyset(&sigset); 145 sigprocmask(SIG_SETMASK, &sigset, NULL); 146 } 147 } 148 149 void 150 reapchildren(signo) 151 int signo; 152 { 153 int save_errno = errno; 154 155 while (wait3(NULL, WNOHANG, NULL) > 0) 156 ; 157 errno = save_errno; 158 } 159 160 void 161 readutmp(signo) 162 int signo; 163 { 164 wantreadutmp = 1; 165 } 166 167 void 168 doreadutmp(void) 169 { 170 static u_int utmpsize; /* last malloced size for utmp */ 171 static u_int utmpmtime; /* last modification time for utmp */ 172 struct stat statbf; 173 int save_errno = errno; 174 175 if (time(NULL) - lastmsgtime >= MAXIDLE) 176 exit(0); 177 (void)alarm((u_int)15); 178 (void)fstat(uf, &statbf); 179 if (statbf.st_mtime > utmpmtime) { 180 utmpmtime = statbf.st_mtime; 181 if (statbf.st_size > utmpsize) { 182 utmpsize = statbf.st_size + 10 * sizeof(struct utmp); 183 if ((utmp = realloc(utmp, utmpsize)) == NULL) { 184 syslog(LOG_ERR, "%s", strerror(errno)); 185 exit(1); 186 } 187 } 188 (void)lseek(uf, (off_t)0, SEEK_SET); 189 nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp); 190 } 191 errno = save_errno; 192 } 193 194 void 195 mailfor(name) 196 char *name; 197 { 198 register struct utmp *utp = &utmp[nutmp]; 199 register char *cp; 200 off_t offset; 201 202 if (!(cp = strchr(name, '@'))) 203 return; 204 *cp = '\0'; 205 offset = atoi(cp + 1); 206 while (--utp >= utmp) 207 if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name))) 208 notify(utp, offset); 209 } 210 211 static char *cr; 212 213 void 214 notify(utp, offset) 215 register struct utmp *utp; 216 off_t offset; 217 { 218 FILE *tp; 219 struct stat stb; 220 struct termios ttybuf; 221 char tty[20], name[sizeof(utmp[0].ut_name) + 1]; 222 223 (void)snprintf(tty, sizeof(tty), "%s%.*s", 224 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line); 225 if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) { 226 /* A slash is an attempt to break security... */ 227 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty); 228 return; 229 } 230 if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) { 231 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty); 232 return; 233 } 234 dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_name, tty); 235 if (fork()) 236 return; 237 (void)signal(SIGALRM, SIG_DFL); 238 (void)alarm((u_int)30); 239 if ((tp = fopen(tty, "w")) == NULL) { 240 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno)); 241 _exit(1); 242 } 243 (void)tcgetattr(fileno(tp), &ttybuf); 244 cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ? 245 "\n" : "\n\r"; 246 (void)strlcpy(name, utp->ut_name, sizeof(name)); 247 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s", 248 cr, name, (int)sizeof(hostname), hostname, cr, cr); 249 jkfprintf(tp, name, offset); 250 (void)fclose(tp); 251 _exit(0); 252 } 253 254 void 255 jkfprintf(tp, name, offset) 256 register FILE *tp; 257 char name[]; 258 off_t offset; 259 { 260 register char *cp, ch; 261 char visout[5], *s2; 262 register FILE *fi; 263 register int linecnt, charcnt, inheader; 264 register struct passwd *p; 265 char line[BUFSIZ]; 266 267 /* Set effective uid to user in case mail drop is on nfs */ 268 if ((p = getpwnam(name)) != NULL) { 269 (void) seteuid(p->pw_uid); 270 (void) setuid(p->pw_uid); 271 } 272 273 if ((fi = fopen(name, "r")) == NULL) 274 return; 275 276 (void)fseek(fi, offset, SEEK_SET); 277 /* 278 * Print the first 7 lines or 560 characters of the new mail 279 * (whichever comes first). Skip header crap other than 280 * From, Subject, To, and Date. 281 */ 282 linecnt = 7; 283 charcnt = 560; 284 inheader = 1; 285 while (fgets(line, sizeof(line), fi) != NULL) { 286 if (inheader) { 287 if (line[0] == '\n') { 288 inheader = 0; 289 continue; 290 } 291 if (line[0] == ' ' || line[0] == '\t' || 292 (strncmp(line, "From:", 5) && 293 strncmp(line, "Subject:", 8))) 294 continue; 295 } 296 if (linecnt <= 0 || charcnt <= 0) { 297 (void)fprintf(tp, "...more...%s", cr); 298 (void)fclose(fi); 299 return; 300 } 301 /* strip weird stuff so can't trojan horse stupid terminals */ 302 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) { 303 ch = toascii(ch); 304 vis(visout, ch, VIS_SAFE|VIS_NOSLASH, cp[1]); 305 for (s2 = visout; *s2; s2++) 306 (void)fputc(*s2, tp); 307 } 308 (void)fputs(cr, tp); 309 --linecnt; 310 } 311 (void)fprintf(tp, "----%s\n", cr); 312 (void)fclose(fi); 313 } 314