1 /* $NetBSD: comsat.c,v 1.36 2007/05/03 15:09:41 christos 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 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36 #if 0 37 static char sccsid[] = "from: @(#)comsat.c 8.1 (Berkeley) 6/4/93"; 38 #else 39 __RCSID("$NetBSD: comsat.c,v 1.36 2007/05/03 15:09:41 christos Exp $"); 40 #endif 41 #endif /* not lint */ 42 43 #include <sys/param.h> 44 #include <sys/socket.h> 45 #include <sys/stat.h> 46 #include <sys/file.h> 47 #include <sys/wait.h> 48 49 #include <netinet/in.h> 50 51 #include <errno.h> 52 #include <netdb.h> 53 #include <paths.h> 54 #include <pwd.h> 55 #include <err.h> 56 #include <signal.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <syslog.h> 61 #include <termios.h> 62 #include <time.h> 63 #include <vis.h> 64 #include <unistd.h> 65 #ifdef SUPPORT_UTMP 66 #include <utmp.h> 67 #endif 68 #ifdef SUPPORT_UTMPX 69 #include <utmpx.h> 70 #endif 71 72 #include "utmpentry.h" 73 74 #if !defined(SUPPORT_UTMP) && !defined(SUPPORT_UTMPX) 75 #error "SUPPORT_UTMP and/or SUPPORT_UTMPX must be defined" 76 #endif 77 78 #define dsyslog if (debug) syslog 79 80 #define MAXIDLE 120 81 82 static int logging; 83 static int debug; 84 static char hostname[MAXHOSTNAMELEN + 1]; 85 static time_t utmpmtime; /* last modification time for utmp/x */ 86 static int nutmp; 87 static struct utmpentry *utmp = NULL; 88 static time_t lastmsgtime; 89 static volatile sig_atomic_t needupdate; 90 91 int main(int, char *[]); 92 static void jkfprintf(FILE *, const char *, off_t, const char *); 93 static void mailfor(const char *); 94 static void notify(const struct utmpentry *, off_t); 95 static void onalrm(int); 96 static void checkutmp(void); 97 98 int 99 main(int argc, char *argv[]) 100 { 101 struct sockaddr_storage from; 102 int cc, ch; 103 socklen_t fromlen; 104 char msgbuf[100]; 105 sigset_t nsigset, osigset; 106 107 /* verify proper invocation */ 108 fromlen = sizeof(from); 109 if (getsockname(0, (struct sockaddr *)(void *)&from, &fromlen) == -1) 110 err(1, "getsockname"); 111 112 openlog("comsat", LOG_PID, LOG_DAEMON); 113 while ((ch = getopt(argc, argv, "l")) != -1) 114 switch (ch) { 115 case 'l': 116 logging = 1; 117 break; 118 default: 119 syslog(LOG_ERR, "Usage: %s [-l]", getprogname()); 120 exit(1); 121 } 122 if (chdir(_PATH_MAILDIR) == -1) { 123 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR); 124 (void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 125 exit(1); 126 } 127 (void)time(&lastmsgtime); 128 (void)gethostname(hostname, sizeof(hostname)); 129 hostname[sizeof(hostname) - 1] = '\0'; 130 (void)signal(SIGALRM, onalrm); 131 (void)signal(SIGTTOU, SIG_IGN); 132 (void)signal(SIGCHLD, SIG_IGN); 133 (void)sigemptyset(&nsigset); 134 (void)sigaddset(&nsigset, SIGALRM); 135 if (sigprocmask(SIG_SETMASK, NULL, &osigset) == -1) { 136 syslog(LOG_ERR, "sigprocmask get failed (%m)"); 137 exit(1); 138 } 139 needupdate = 1; 140 for (;;) { 141 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0); 142 if (cc <= 0) { 143 if (errno != EINTR) 144 sleep(1); 145 errno = 0; 146 checkutmp(); 147 continue; 148 } else 149 checkutmp(); 150 if (!nutmp) /* no one has logged in yet */ 151 continue; 152 if (sigprocmask(SIG_SETMASK, &nsigset, NULL) == -1) { 153 syslog(LOG_ERR, "sigprocmask set failed (%m)"); 154 exit(1); 155 } 156 msgbuf[cc] = '\0'; 157 (void)time(&lastmsgtime); 158 mailfor(msgbuf); 159 if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1) { 160 syslog(LOG_ERR, "sigprocmask restore failed (%m)"); 161 exit(1); 162 } 163 } 164 } 165 166 static void 167 /*ARGSUSED*/ 168 onalrm(int signo) 169 { 170 needupdate = 1; 171 } 172 173 static void 174 checkutmp(void) 175 { 176 struct stat statbf; 177 time_t newtime = 0; 178 179 if (!needupdate) 180 return; 181 needupdate = 0; 182 183 if (time(NULL) - lastmsgtime >= MAXIDLE) 184 exit(0); 185 (void)alarm((u_int)15); 186 #ifdef SUPPORT_UTMP 187 if (stat(_PATH_UTMP, &statbf) != -1) 188 if (statbf.st_mtime > newtime) 189 newtime = statbf.st_mtime; 190 #endif 191 #ifdef SUPPORT_UTMPX 192 if (stat(_PATH_UTMPX, &statbf) != -1) 193 if (statbf.st_mtime > newtime) 194 newtime = statbf.st_mtime; 195 #endif 196 if (newtime > utmpmtime) { 197 freeutentries(utmp); 198 nutmp = getutentries(NULL, &utmp); 199 utmpmtime = newtime; 200 } 201 } 202 203 static void 204 mailfor(const char *name) 205 { 206 struct utmpentry *ep; 207 char *cp, *fn; 208 off_t offset; 209 intmax_t val; 210 211 if (!(cp = strchr(name, '@'))) 212 return; 213 *cp = '\0'; 214 errno = 0; 215 offset = val = strtoimax(cp + 1, &fn, 10); 216 if (errno == ERANGE || offset != val) 217 return; 218 if (fn && *fn && *fn != '\n') { 219 /* 220 * Procmail sends messages to comsat with a trailing colon 221 * and a pathname to the folder where the new message was 222 * deposited. Since we can't reliably open only regular 223 * files, we need to ignore these. With one exception: 224 * if it mentions the user's system mailbox. 225 */ 226 char maildir[MAXPATHLEN]; 227 int l = snprintf(maildir, sizeof(maildir), ":%s/%s", 228 _PATH_MAILDIR, name); 229 if (l >= sizeof(maildir) || strcmp(maildir, fn) != 0) 230 return; 231 } 232 for (ep = utmp; ep != NULL; ep = ep->next) 233 if (strcmp(ep->name, name) == 0) 234 notify(ep, offset); 235 } 236 237 static void 238 notify(const struct utmpentry *ep, off_t offset) 239 { 240 FILE *tp; 241 struct passwd *p; 242 struct stat stb; 243 struct termios ttybuf; 244 char tty[sizeof(_PATH_DEV) + sizeof(ep->line) + 1]; 245 const char *cr = ep->line; 246 247 if (strncmp(cr, "pts/", 4) == 0) 248 cr += 4; 249 if (strchr(cr, '/')) { 250 /* A slash is an attempt to break security... */ 251 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'", 252 ep->line); 253 return; 254 } 255 (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ep->line); 256 if (stat(tty, &stb) == -1 || !(stb.st_mode & S_IEXEC)) { 257 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", ep->name, tty); 258 return; 259 } 260 dsyslog(LOG_DEBUG, "notify %s on %s", ep->name, tty); 261 switch (fork()) { 262 case -1: 263 syslog(LOG_NOTICE, "fork failed (%m)"); 264 return; 265 case 0: 266 break; 267 default: 268 return; 269 } 270 (void)signal(SIGALRM, SIG_DFL); 271 (void)alarm((u_int)30); 272 if ((tp = fopen(tty, "w")) == NULL) { 273 dsyslog(LOG_ERR, "open `%s' (%s)", tty, strerror(errno)); 274 _exit(1); 275 } 276 if (tcgetattr(fileno(tp), &ttybuf) == -1) { 277 dsyslog(LOG_ERR, "tcgetattr `%s' (%s)", tty, strerror(errno)); 278 _exit(1); 279 } 280 cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ? 281 "\n" : "\n\r"; 282 /* Set uid/gid/groups to users in case mail drop is on nfs */ 283 if ((p = getpwnam(ep->name)) == NULL || 284 initgroups(p->pw_name, p->pw_gid) == -1 || 285 setgid(p->pw_gid) == -1 || 286 setuid(p->pw_uid) == -1) 287 _exit(1); 288 289 if (logging) 290 syslog(LOG_INFO, "biff message for %s", ep->name); 291 292 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s", 293 cr, ep->name, (int)sizeof(hostname), hostname, cr, cr); 294 jkfprintf(tp, ep->name, offset, cr); 295 (void)fclose(tp); 296 _exit(0); 297 } 298 299 static void 300 jkfprintf(FILE *tp, const char *name, off_t offset, const char *cr) 301 { 302 FILE *fi; 303 int linecnt, charcnt, inheader; 304 char line[BUFSIZ], visline[BUFSIZ * 4 + 1], *nl; 305 306 if ((fi = fopen(name, "r")) == NULL) 307 return; 308 309 (void)fseeko(fi, offset, SEEK_SET); 310 /* 311 * Print the first 7 lines or 560 characters of the new mail 312 * (whichever comes first). Skip header crap other than 313 * From, Subject, To, and Date. 314 */ 315 linecnt = 7; 316 charcnt = 560; 317 inheader = 1; 318 while (fgets(line, sizeof(line), fi) != NULL) { 319 line[sizeof(line) - 1] = '\0'; 320 if (inheader) { 321 if (line[0] == '\n') { 322 inheader = 0; 323 continue; 324 } 325 if (line[0] == ' ' || line[0] == '\t' || 326 (strncasecmp(line, "From:", 5) && 327 strncasecmp(line, "Subject:", 8))) 328 continue; 329 } 330 if (strncmp(line, "From ", 5) == 0) { 331 (void)fprintf(tp, "----%s", cr); 332 (void)fclose(fi); 333 return; 334 } 335 if (linecnt <= 0 || charcnt <= 0) { 336 (void)fprintf(tp, "...more...%s", cr); 337 (void)fclose(fi); 338 return; 339 } 340 if ((nl = strchr(line, '\n')) != NULL) 341 *nl = '\0'; 342 /* strip weird stuff so can't trojan horse stupid terminals */ 343 (void)strvis(visline, line, VIS_CSTYLE); 344 (void)fputs(visline, tp); 345 (void)fputs(cr, tp); 346 --linecnt; 347 } 348 (void)fprintf(tp, "----%s\n", cr); 349 (void)fclose(fi); 350 } 351