1 /* $OpenBSD: comsat.c,v 1.35 2006/08/17 23:52:06 ray 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.35 2006/08/17 23:52:06 ray 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 ssize_t 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) == -1) { 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)) == -1) { 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 wantreadutmp = 0; 133 doreadutmp(); 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 /* ARGSUSED */ 156 void 157 reapchildren(int signo) 158 { 159 int save_errno = errno; 160 161 while (wait3(NULL, WNOHANG, NULL) > 0) 162 ; 163 errno = save_errno; 164 } 165 166 /* ARGSUSED */ 167 void 168 readutmp(int signo) 169 { 170 wantreadutmp = 1; 171 } 172 173 void 174 doreadutmp(void) 175 { 176 static u_int utmpsize; /* last malloced size for utmp */ 177 static time_t utmpmtime; /* last modification time for utmp */ 178 struct stat statbf; 179 180 if (time(NULL) - lastmsgtime >= MAXIDLE) 181 exit(0); 182 (void)fstat(uf, &statbf); 183 if (statbf.st_mtime > utmpmtime) { 184 utmpmtime = statbf.st_mtime; 185 /* avoid int overflow */ 186 if (statbf.st_size > INT_MAX - 10 * sizeof(struct utmp)) { 187 syslog(LOG_ALERT, "utmp file excessively large"); 188 exit(1); 189 } 190 if (statbf.st_size > utmpsize) { 191 u_int nutmpsize = statbf.st_size + 10 * 192 sizeof(struct utmp); 193 struct utmp *u; 194 195 if ((u = realloc(utmp, nutmpsize)) == NULL) { 196 free(utmp); 197 syslog(LOG_ERR, "%s", strerror(errno)); 198 exit(1); 199 } 200 utmp = u; 201 utmpsize = nutmpsize; 202 } 203 (void)lseek(uf, 0, SEEK_SET); 204 nutmp = read(uf, utmp, statbf.st_size)/sizeof(struct utmp); 205 } 206 (void)alarm(15); 207 } 208 209 void 210 mailfor(char *name) 211 { 212 struct utmp *utp = &utmp[nutmp]; 213 char utname[UT_NAMESIZE+1]; 214 char *cp; 215 off_t offset; 216 217 if (!(cp = strchr(name, '@'))) 218 return; 219 *cp = '\0'; 220 offset = atoi(cp + 1); 221 while (--utp >= utmp) { 222 memcpy(utname, utp->ut_name, UT_NAMESIZE); 223 utname[UT_NAMESIZE] = '\0'; 224 if (!strncmp(utname, name, UT_NAMESIZE)) 225 notify(utp, offset); 226 } 227 } 228 229 static char *cr; 230 231 void 232 notify(struct utmp *utp, off_t offset) 233 { 234 FILE *tp; 235 struct stat stb; 236 struct termios ttybuf; 237 char tty[MAXPATHLEN], name[UT_NAMESIZE + 1]; 238 239 (void)snprintf(tty, sizeof(tty), "%s%.*s", 240 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line); 241 if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) { 242 /* A slash is an attempt to break security... */ 243 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty); 244 return; 245 } 246 if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) { 247 dsyslog(LOG_DEBUG, "%.*s: wrong mode on %s", 248 (int)sizeof(utp->ut_name), utp->ut_name, tty); 249 return; 250 } 251 dsyslog(LOG_DEBUG, "notify %.*s on %s", (int)sizeof(utp->ut_name), 252 utp->ut_name, tty); 253 if (fork()) 254 return; 255 (void)signal(SIGALRM, SIG_DFL); 256 (void)alarm(30); 257 if ((tp = fopen(tty, "w")) == NULL) { 258 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno)); 259 _exit(1); 260 } 261 (void)tcgetattr(fileno(tp), &ttybuf); 262 cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ? 263 "\n" : "\n\r"; 264 memcpy(name, utp->ut_name, UT_NAMESIZE); 265 name[UT_NAMESIZE] = '\0'; 266 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s", 267 cr, name, (int)sizeof(hostname), hostname, cr, cr); 268 jkfprintf(tp, name, offset); 269 (void)fclose(tp); 270 _exit(0); 271 } 272 273 void 274 jkfprintf(FILE *tp, char name[], off_t offset) 275 { 276 char *cp, ch; 277 char visout[5], *s2; 278 FILE *fi; 279 int linecnt, charcnt, inheader; 280 struct passwd *p; 281 char line[BUFSIZ]; 282 283 /* Set effective uid to user in case mail drop is on nfs */ 284 if ((p = getpwnam(name)) != NULL) { 285 (void) seteuid(p->pw_uid); 286 (void) setuid(p->pw_uid); 287 } 288 289 if ((fi = fopen(name, "r")) == NULL) 290 return; 291 292 (void)fseeko(fi, offset, SEEK_SET); 293 /* 294 * Print the first 7 lines or 560 characters of the new mail 295 * (whichever comes first). Skip header crap other than 296 * From, Subject, To, and Date. 297 */ 298 linecnt = 7; 299 charcnt = 560; 300 inheader = 1; 301 while (fgets(line, sizeof(line), fi) != NULL) { 302 if (inheader) { 303 if (line[0] == '\n') { 304 inheader = 0; 305 continue; 306 } 307 if (line[0] == ' ' || line[0] == '\t' || 308 (strncmp(line, "From:", 5) && 309 strncmp(line, "Subject:", 8))) 310 continue; 311 } 312 if (linecnt <= 0 || charcnt <= 0) { 313 (void)fprintf(tp, "...more...%s", cr); 314 (void)fclose(fi); 315 return; 316 } 317 /* strip weird stuff so can't trojan horse stupid terminals */ 318 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) { 319 ch = toascii(ch); 320 vis(visout, ch, VIS_SAFE|VIS_NOSLASH, cp[1]); 321 for (s2 = visout; *s2; s2++) 322 (void)fputc(*s2, tp); 323 } 324 (void)fputs(cr, tp); 325 --linecnt; 326 } 327 (void)fprintf(tp, "----%s\n", cr); 328 (void)fclose(fi); 329 } 330