1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * 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 char copyright[] = 36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 37 All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 /*static char sccsid[] = "from: @(#)comsat.c 5.24 (Berkeley) 2/25/91";*/ 42 static char rcsid[] = "$Id: comsat.c,v 1.4 1994/02/01 00:32:22 cgd Exp $"; 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/socket.h> 47 #include <sys/stat.h> 48 #include <sys/file.h> 49 #include <sys/wait.h> 50 51 #include <netinet/in.h> 52 53 #include <stdio.h> 54 #include <sgtty.h> 55 #include <utmp.h> 56 #include <signal.h> 57 #include <errno.h> 58 #include <netdb.h> 59 #include <syslog.h> 60 #include <ctype.h> 61 #include <string.h> 62 #include <pwd.h> 63 #include <paths.h> 64 65 int debug = 0; 66 #define dsyslog if (debug) syslog 67 68 #define MAXIDLE 120 69 70 char hostname[MAXHOSTNAMELEN]; 71 struct utmp *utmp = NULL; 72 time_t lastmsgtime, time(); 73 int nutmp, uf; 74 75 /* ARGSUSED */ 76 main(argc, argv) 77 int argc; 78 char **argv; 79 { 80 extern int errno; 81 register int cc; 82 char msgbuf[100]; 83 struct sockaddr_in from; 84 int fromlen; 85 void onalrm(), reapchildren(); 86 87 /* verify proper invocation */ 88 fromlen = sizeof(from); 89 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 90 (void)fprintf(stderr, 91 "comsat: getsockname: %s.\n", strerror(errno)); 92 exit(1); 93 } 94 openlog("comsat", LOG_PID, LOG_DAEMON); 95 if (chdir(_PATH_MAILDIR)) { 96 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR); 97 exit(1); 98 } 99 if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) { 100 syslog(LOG_ERR, "main: %s: %m", _PATH_UTMP); 101 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 102 exit(1); 103 } 104 (void)time(&lastmsgtime); 105 (void)gethostname(hostname, sizeof(hostname)); 106 onalrm(); 107 (void)signal(SIGALRM, onalrm); 108 (void)signal(SIGTTOU, SIG_IGN); 109 (void)signal(SIGCHLD, reapchildren); 110 for (;;) { 111 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 112 if (cc <= 0) { 113 if (errno != EINTR) 114 sleep(1); 115 errno = 0; 116 continue; 117 } 118 if (!nutmp) /* no one has logged in yet */ 119 continue; 120 sigblock(sigmask(SIGALRM)); 121 msgbuf[cc] = '\0'; 122 (void)time(&lastmsgtime); 123 mailfor(msgbuf); 124 sigsetmask(0L); 125 } 126 } 127 128 void 129 reapchildren() 130 { 131 while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0); 132 } 133 134 void 135 onalrm() 136 { 137 static u_int utmpsize; /* last malloced size for utmp */ 138 static u_int utmpmtime; /* last modification time for utmp */ 139 struct stat statbf; 140 off_t lseek(); 141 char *malloc(), *realloc(); 142 143 if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE) 144 exit(0); 145 (void)alarm((u_int)15); 146 (void)fstat(uf, &statbf); 147 if (statbf.st_mtime > utmpmtime) { 148 utmpmtime = statbf.st_mtime; 149 if (statbf.st_size > utmpsize) { 150 utmpsize = statbf.st_size + 10 * sizeof(struct utmp); 151 if (utmp) 152 utmp = (struct utmp *)realloc((char *)utmp, utmpsize); 153 else 154 utmp = (struct utmp *)malloc(utmpsize); 155 if (!utmp) { 156 syslog(LOG_ERR, "malloc failed"); 157 exit(1); 158 } 159 } 160 (void)lseek(uf, 0L, L_SET); 161 nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp); 162 } 163 } 164 165 mailfor(name) 166 char *name; 167 { 168 register struct utmp *utp = &utmp[nutmp]; 169 register char *cp; 170 off_t offset; 171 172 if (!(cp = index(name, '@'))) 173 return; 174 *cp = '\0'; 175 offset = atoi(cp + 1); 176 while (--utp >= utmp) 177 if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name))) 178 notify(utp, offset); 179 } 180 181 static char *cr; 182 183 notify(utp, offset) 184 register struct utmp *utp; 185 off_t offset; 186 { 187 FILE *tp; 188 struct stat stb; 189 struct sgttyb gttybuf; 190 char tty[20], name[sizeof(utmp[0].ut_name) + 1]; 191 192 (void)snprintf(tty, sizeof(tty), "%s%.*s", 193 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line); 194 if (index(tty + sizeof(_PATH_DEV) - 1, '/')) { 195 /* A slash is an attempt to break security... */ 196 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty); 197 return; 198 } 199 if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) { 200 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty); 201 return; 202 } 203 dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty); 204 if (fork()) 205 return; 206 (void)signal(SIGALRM, SIG_DFL); 207 (void)alarm((u_int)30); 208 if ((tp = fopen(tty, "w")) == NULL) { 209 dsyslog(LOG_ERR, "fopen of tty %s failed", tty); 210 _exit(-1); 211 } 212 (void)ioctl(fileno(tp), TIOCGETP, >tybuf); 213 cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? 214 "\n" : "\n\r"; 215 (void)strncpy(name, utp->ut_name, sizeof(utp->ut_name)); 216 name[sizeof(name) - 1] = '\0'; 217 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s", 218 cr, name, sizeof(hostname), hostname, cr, cr); 219 jkfprintf(tp, name, offset); 220 (void)fclose(tp); 221 _exit(0); 222 } 223 224 jkfprintf(tp, name, offset) 225 register FILE *tp; 226 char name[]; 227 off_t offset; 228 { 229 register char *cp, ch; 230 register FILE *fi; 231 register int linecnt, charcnt, inheader; 232 register struct passwd *p; 233 char line[BUFSIZ]; 234 235 /* Set effective uid to user in case mail drop is on nfs */ 236 if ((p = getpwnam(name)) == NULL) { 237 /* 238 * If user is not in passwd file, assume that it's 239 * an attempt to break security... 240 */ 241 syslog(LOG_AUTH | LOG_NOTICE, "%s not in passwd file", name); 242 return; 243 } else 244 (void) setuid(p->pw_uid); 245 246 if ((fi = fopen(name, "r")) == NULL) 247 return; 248 (void)fseek(fi, offset, L_SET); 249 /* 250 * Print the first 7 lines or 560 characters of the new mail 251 * (whichever comes first). Skip header crap other than 252 * From, Subject, To, and Date. 253 */ 254 linecnt = 7; 255 charcnt = 560; 256 inheader = 1; 257 while (fgets(line, sizeof(line), fi) != NULL) { 258 if (inheader) { 259 if (line[0] == '\n') { 260 inheader = 0; 261 continue; 262 } 263 if (line[0] == ' ' || line[0] == '\t' || 264 strncmp(line, "From:", 5) && 265 strncmp(line, "Subject:", 8)) 266 continue; 267 } 268 if (linecnt <= 0 || charcnt <= 0) { 269 (void)fprintf(tp, "...more...%s", cr); 270 fclose(fi); 271 return; 272 } 273 /* strip weird stuff so can't trojan horse stupid terminals */ 274 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) { 275 ch = toascii(ch); 276 if (!isprint(ch) && !isspace(ch)) 277 ch |= 0x40; 278 (void)fputc(ch, tp); 279 } 280 (void)fputs(cr, tp); 281 --linecnt; 282 } 283 (void)fprintf(tp, "----%s\n", cr); 284 fclose(fi); 285 } 286