1 /* $OpenBSD: comsat.c,v 1.45 2016/04/02 16:33:28 millert 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 #include <sys/limits.h> 33 #include <sys/socket.h> 34 #include <sys/stat.h> 35 #include <sys/wait.h> 36 37 #include <netinet/in.h> 38 39 #include <ctype.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <netdb.h> 43 #include <limits.h> 44 #include <paths.h> 45 #include <pwd.h> 46 #include <signal.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <syslog.h> 51 #include <termios.h> 52 #include <unistd.h> 53 #include <utmp.h> 54 #include <vis.h> 55 #include <err.h> 56 57 int debug = 0; 58 #define dsyslog if (debug) syslog 59 60 #define MAXIDLE 120 61 62 char hostname[HOST_NAME_MAX+1]; 63 struct utmp *utmp = NULL; 64 time_t lastmsgtime; 65 int nutmp, uf; 66 67 void jkfprintf(FILE *, char[], off_t); 68 void mailfor(char *); 69 void notify(struct utmp *, off_t); 70 void readutmp(int); 71 void doreadutmp(void); 72 void reapchildren(int); 73 74 volatile sig_atomic_t wantreadutmp; 75 76 int 77 main(int argc, char *argv[]) 78 { 79 struct sockaddr_storage from; 80 struct sigaction sa; 81 ssize_t cc; 82 socklen_t fromlen; 83 char msgbuf[100]; 84 sigset_t sigset; 85 86 /* verify proper invocation */ 87 fromlen = sizeof(from); 88 if (getsockname(0, (struct sockaddr *)&from, &fromlen) == -1) { 89 (void)fprintf(stderr, 90 "comsat: getsockname: %s.\n", strerror(errno)); 91 exit(1); 92 } 93 94 if (pledge("stdio rpath wpath proc tty", NULL) == -1) 95 err(1, "pledge"); 96 97 openlog("comsat", LOG_PID, LOG_DAEMON); 98 if (chdir(_PATH_MAILDIR)) { 99 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR); 100 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 101 exit(1); 102 } 103 if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) == -1) { 104 syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP); 105 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 106 exit(1); 107 } 108 (void)time(&lastmsgtime); 109 (void)gethostname(hostname, sizeof(hostname)); 110 doreadutmp(); 111 112 (void)signal(SIGTTOU, SIG_IGN); 113 114 bzero(&sa, sizeof sa); 115 sigemptyset(&sa.sa_mask); 116 sa.sa_handler = readutmp; 117 sa.sa_flags = 0; /* no SA_RESTART */ 118 (void)sigaction(SIGALRM, &sa, NULL); 119 120 sa.sa_handler = reapchildren; 121 sa.sa_flags = SA_RESTART; 122 (void)sigaction(SIGCHLD, &sa, NULL); 123 124 for (;;) { 125 if (wantreadutmp) { 126 wantreadutmp = 0; 127 doreadutmp(); 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 /* ARGSUSED */ 150 void 151 reapchildren(int signo) 152 { 153 int save_errno = errno; 154 155 while (wait3(NULL, WNOHANG, NULL) > 0) 156 ; 157 errno = save_errno; 158 } 159 160 /* ARGSUSED */ 161 void 162 readutmp(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 time_t utmpmtime; /* last modification time for utmp */ 172 struct stat statbf; 173 174 if (time(NULL) - lastmsgtime >= MAXIDLE) 175 exit(0); 176 (void)fstat(uf, &statbf); 177 if (statbf.st_mtime > utmpmtime) { 178 utmpmtime = statbf.st_mtime; 179 /* avoid int overflow */ 180 if (statbf.st_size > INT_MAX - 10 * sizeof(struct utmp)) { 181 syslog(LOG_ALERT, "utmp file excessively large"); 182 exit(1); 183 } 184 if (statbf.st_size > utmpsize) { 185 u_int nutmpsize = statbf.st_size + 10 * 186 sizeof(struct utmp); 187 struct utmp *u; 188 189 if ((u = realloc(utmp, nutmpsize)) == NULL) { 190 free(utmp); 191 syslog(LOG_ERR, "%s", strerror(errno)); 192 exit(1); 193 } 194 utmp = u; 195 utmpsize = nutmpsize; 196 } 197 (void)lseek(uf, 0, SEEK_SET); 198 nutmp = read(uf, utmp, statbf.st_size)/sizeof(struct utmp); 199 dsyslog(LOG_DEBUG, "read %d utmp entries", nutmp); 200 } 201 (void)alarm(15); 202 } 203 204 void 205 mailfor(char *name) 206 { 207 struct utmp *utp = &utmp[nutmp]; 208 char utname[UT_NAMESIZE+1]; 209 const char *errstr; 210 char *cp; 211 off_t offset; 212 213 dsyslog(LOG_DEBUG, "mail for '%s'", name); 214 if (!(cp = strchr(name, '@'))) 215 return; 216 *cp++ = '\0'; 217 cp[strcspn(cp, " \t\n")] = '\0'; 218 offset = strtonum(cp, 0, LLONG_MAX, &errstr); 219 if (errstr) { 220 syslog(LOG_ERR, "'%s' is %s", cp + 1, errstr); 221 return; 222 } 223 while (--utp >= utmp) { 224 memcpy(utname, utp->ut_name, UT_NAMESIZE); 225 utname[UT_NAMESIZE] = '\0'; 226 dsyslog(LOG_DEBUG, "check %s against %s", name, utname); 227 if (!strncmp(utname, name, UT_NAMESIZE)) 228 notify(utp, offset); 229 } 230 } 231 232 static char *cr; 233 234 void 235 notify(struct utmp *utp, off_t offset) 236 { 237 int fd; 238 FILE *tp; 239 struct stat stb; 240 struct termios ttybuf; 241 char tty[PATH_MAX], name[UT_NAMESIZE + 1]; 242 243 (void)snprintf(tty, sizeof(tty), "%s%.*s", 244 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line); 245 if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) { 246 /* A slash is an attempt to break security... */ 247 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty); 248 return; 249 } 250 if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) { 251 dsyslog(LOG_DEBUG, "%.*s: wrong mode on %s", 252 (int)sizeof(utp->ut_name), utp->ut_name, tty); 253 return; 254 } 255 dsyslog(LOG_DEBUG, "notify %.*s on %s", (int)sizeof(utp->ut_name), 256 utp->ut_name, tty); 257 if (fork()) 258 return; 259 (void)signal(SIGALRM, SIG_DFL); 260 (void)alarm(30); 261 fd = open(tty, O_WRONLY); 262 if (fd == -1 || (tp = fdopen(fd, "w")) == NULL) { 263 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno)); 264 _exit(1); 265 } 266 (void)tcgetattr(fileno(tp), &ttybuf); 267 cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ? 268 "\n" : "\n\r"; 269 memcpy(name, utp->ut_name, UT_NAMESIZE); 270 name[UT_NAMESIZE] = '\0'; 271 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s", 272 cr, name, (int)sizeof(hostname), hostname, cr, cr); 273 jkfprintf(tp, name, offset); 274 (void)fclose(tp); 275 _exit(0); 276 } 277 278 void 279 jkfprintf(FILE *tp, char name[], off_t offset) 280 { 281 char *cp, ch; 282 char visout[5], *s2; 283 FILE *fi; 284 int linecnt, charcnt, inheader; 285 char line[BUFSIZ]; 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