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