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