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