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