1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 const 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 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/socket.h> 43 #include <sys/stat.h> 44 #include <sys/file.h> 45 #include <sys/wait.h> 46 47 #include <netinet/in.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <netdb.h> 53 #include <paths.h> 54 #include <pwd.h> 55 #include <termios.h> 56 #include <signal.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <syslog.h> 61 #include <unistd.h> 62 #include <utmpx.h> 63 64 static int debug = 0; 65 #define dsyslog if (debug) syslog 66 67 #define MAXIDLE 120 68 69 static char hostname[MAXHOSTNAMELEN]; 70 71 static void jkfprintf(FILE *, char[], char[], off_t); 72 static void mailfor(char *); 73 static void notify(struct utmpx *, char[], off_t, int); 74 static void reapchildren(int); 75 76 int 77 main(int argc __unused, char *argv[] __unused) 78 { 79 struct sockaddr_in from; 80 socklen_t fromlen; 81 int cc; 82 char msgbuf[256]; 83 84 /* verify proper invocation */ 85 fromlen = sizeof(from); 86 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) 87 err(1, "getsockname"); 88 openlog("comsat", LOG_PID, LOG_DAEMON); 89 if (chdir(_PATH_MAILDIR)) { 90 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR); 91 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 92 exit(1); 93 } 94 (void)gethostname(hostname, sizeof(hostname)); 95 (void)signal(SIGTTOU, SIG_IGN); 96 (void)signal(SIGCHLD, reapchildren); 97 for (;;) { 98 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 99 if (cc <= 0) { 100 if (errno != EINTR) 101 sleep(1); 102 errno = 0; 103 continue; 104 } 105 msgbuf[cc] = '\0'; 106 mailfor(msgbuf); 107 sigsetmask(0L); 108 } 109 } 110 111 static void 112 reapchildren(int signo __unused) 113 { 114 while (wait3(NULL, WNOHANG, NULL) > 0); 115 } 116 117 static void 118 mailfor(char *name) 119 { 120 struct utmpx *utp; 121 char *cp; 122 char *file; 123 off_t offset; 124 int folder; 125 char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1]; 126 char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1]; 127 128 if (!(cp = strchr(name, '@'))) 129 return; 130 *cp = '\0'; 131 offset = strtoll(cp + 1, NULL, 10); 132 if (!(cp = strchr(cp + 1, ':'))) 133 file = name; 134 else 135 file = cp + 1; 136 sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user), 137 name); 138 if (*file != '/') { 139 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR, 140 (int)sizeof(utp->ut_user), file); 141 file = buf2; 142 } 143 folder = strcmp(buf, file); 144 setutxent(); 145 while ((utp = getutxent()) != NULL) 146 if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name)) 147 notify(utp, file, offset, folder); 148 endutxent(); 149 } 150 151 static const char *cr; 152 153 static void 154 notify(struct utmpx *utp, char file[], off_t offset, int folder) 155 { 156 FILE *tp; 157 struct stat stb; 158 struct termios tio; 159 char tty[20]; 160 const char *s = utp->ut_line; 161 162 if (strncmp(s, "pts/", 4) == 0) 163 s += 4; 164 if (strchr(s, '/')) { 165 /* A slash is an attempt to break security... */ 166 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'", 167 utp->ut_line); 168 return; 169 } 170 (void)snprintf(tty, sizeof(tty), "%s%.*s", 171 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line); 172 if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) { 173 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty); 174 return; 175 } 176 dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty); 177 switch (fork()) { 178 case -1: 179 syslog(LOG_NOTICE, "fork failed (%m)"); 180 return; 181 case 0: 182 break; 183 default: 184 return; 185 } 186 if ((tp = fopen(tty, "w")) == NULL) { 187 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno)); 188 _exit(1); 189 } 190 (void)tcgetattr(fileno(tp), &tio); 191 cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ? "\n" : "\n\r"; 192 switch (stb.st_mode & (S_IXUSR | S_IXGRP)) { 193 case S_IXUSR: 194 case (S_IXUSR | S_IXGRP): 195 (void)fprintf(tp, 196 "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s", 197 cr, utp->ut_user, (int)sizeof(hostname), hostname, 198 folder ? cr : "", folder ? "to " : "", folder ? file : "", 199 cr, cr); 200 jkfprintf(tp, utp->ut_user, file, offset); 201 break; 202 case S_IXGRP: 203 (void)fprintf(tp, "\007"); 204 (void)fflush(tp); 205 (void)sleep(1); 206 (void)fprintf(tp, "\007"); 207 break; 208 default: 209 break; 210 } 211 (void)fclose(tp); 212 _exit(0); 213 } 214 215 static void 216 jkfprintf(FILE *tp, char user[], char file[], off_t offset) 217 { 218 unsigned char *cp, ch; 219 FILE *fi; 220 int linecnt, charcnt, inheader; 221 struct passwd *p; 222 unsigned char line[BUFSIZ]; 223 224 /* Set effective uid to user in case mail drop is on nfs */ 225 if ((p = getpwnam(user)) != NULL) 226 (void) setuid(p->pw_uid); 227 228 if ((fi = fopen(file, "r")) == NULL) 229 return; 230 231 (void)fseeko(fi, offset, SEEK_CUR); 232 /* 233 * Print the first 7 lines or 560 characters of the new mail 234 * (whichever comes first). Skip header crap other than 235 * From, Subject, To, and Date. 236 */ 237 linecnt = 7; 238 charcnt = 560; 239 inheader = 1; 240 while (fgets(line, sizeof(line), fi) != NULL) { 241 if (inheader) { 242 if (line[0] == '\n') { 243 inheader = 0; 244 continue; 245 } 246 if (line[0] == ' ' || line[0] == '\t' || 247 (strncmp(line, "From:", 5) && 248 strncmp(line, "Subject:", 8))) 249 continue; 250 } 251 if (linecnt <= 0 || charcnt <= 0) { 252 (void)fprintf(tp, "...more...%s", cr); 253 (void)fclose(fi); 254 return; 255 } 256 /* strip weird stuff so can't trojan horse stupid terminals */ 257 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) { 258 /* disable upper controls and enable all other 259 8bit codes due to lack of locale knowledge 260 */ 261 if (((ch & 0x80) && ch < 0xA0) || 262 (!(ch & 0x80) && !isprint(ch) && 263 !isspace(ch) && ch != '\a' && ch != '\b') 264 ) { 265 if (ch & 0x80) { 266 ch &= ~0x80; 267 (void)fputs("M-", tp); 268 } 269 if (iscntrl(ch)) { 270 ch ^= 0x40; 271 (void)fputc('^', tp); 272 } 273 } 274 (void)fputc(ch, tp); 275 } 276 (void)fputs(cr, tp); 277 --linecnt; 278 } 279 (void)fprintf(tp, "----%s\n", cr); 280 (void)fclose(fi); 281 } 282