comsat.c revision 1.28 1 /* $NetBSD: comsat.c,v 1.28 2004/07/10 00:00:58 enami 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.28 2004/07/10 00:00:58 enami 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 <ctype.h>
52 #include <errno.h>
53 #include <netdb.h>
54 #include <paths.h>
55 #include <pwd.h>
56 #include <err.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <syslog.h>
62 #include <termios.h>
63 #include <time.h>
64 #include <vis.h>
65 #include <unistd.h>
66 #ifdef SUPPORT_UTMP
67 #include <utmp.h>
68 #endif
69 #ifdef SUPPORT_UTMPX
70 #include <utmpx.h>
71 #endif
72
73 #include "utmpentry.h"
74
75 #if !defined(SUPPORT_UTMP) && !defined(SUPPORT_UTMPX)
76 #error "SUPPORT_UTMP and/or SUPPORT_UTMPX must be defined"
77 #endif
78
79 #define dsyslog if (debug) syslog
80
81 #define MAXIDLE 120
82
83 static int logging;
84 static int debug = 0;
85 static char hostname[MAXHOSTNAMELEN+1];
86 static time_t utmpmtime; /* last modification time for utmp/x */
87 static int nutmp;
88 static struct utmpentry *utmp = NULL;
89 static time_t lastmsgtime;
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 reapchildren(int);
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 onalrm(0);
131 (void)signal(SIGALRM, onalrm);
132 (void)signal(SIGTTOU, SIG_IGN);
133 (void)signal(SIGCHLD, reapchildren);
134 (void)sigemptyset(&nsigset);
135 (void)sigaddset(&nsigset, SIGALRM);
136 if (sigprocmask(SIG_SETMASK, NULL, &osigset) == -1) {
137 syslog(LOG_ERR, "sigprocmask get failed (%m)");
138 exit(1);
139 }
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 continue;
147 }
148 if (!nutmp) /* no one has logged in yet */
149 continue;
150 if (sigprocmask(SIG_SETMASK, &nsigset, NULL) == -1) {
151 syslog(LOG_ERR, "sigprocmask set failed (%m)");
152 exit(1);
153 }
154 msgbuf[cc] = '\0';
155 (void)time(&lastmsgtime);
156 mailfor(msgbuf);
157 if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1) {
158 syslog(LOG_ERR, "sigprocmask restore failed (%m)");
159 exit(1);
160 }
161 }
162 }
163
164 static void
165 /*ARGSUSED*/
166 reapchildren(int signo)
167 {
168
169 while (wait3(NULL, WNOHANG, NULL) != -1)
170 continue;
171 }
172
173 static void
174 /*ARGSUSED*/
175 onalrm(int signo)
176 {
177 struct stat statbf;
178 time_t newtime = 0;
179
180 if (time(NULL) - lastmsgtime >= MAXIDLE)
181 exit(0);
182 (void)alarm((u_int)15);
183 #ifdef SUPPORT_UTMP
184 if (stat(_PATH_UTMP, &statbf) != -1)
185 if (statbf.st_mtime > newtime)
186 newtime = statbf.st_mtime;
187 #endif
188 #ifdef SUPPORT_UTMPX
189 if (stat(_PATH_UTMP, &statbf) != -1)
190 if (statbf.st_mtime > newtime)
191 newtime = statbf.st_mtime;
192 #endif
193 if (newtime > utmpmtime) {
194 freeutentries(utmp);
195 nutmp = getutentries(NULL, &utmp);
196 utmpmtime = newtime;
197 }
198 }
199
200 static void
201 mailfor(const char *name)
202 {
203 struct utmpentry *ep;
204 char *cp, *fn;
205 off_t offset;
206
207 if (!(cp = strchr(name, '@')))
208 return;
209 *cp = '\0';
210 errno = 0;
211 offset = strtol(cp + 1, &fn, 10);
212 if (errno == ERANGE && (offset == LONG_MAX || offset == LONG_MIN))
213 return;
214 if (fn && *fn && *fn != '\n') {
215 /*
216 * Procmail sends messages to comsat with a trailing colon
217 * and a pathname to the folder where the new message was
218 * deposited. Since we can't reliably open only regular
219 * files, we need to ignore these. With one exception:
220 * if it mentions the user's system mailbox.
221 */
222 char maildir[MAXPATHLEN];
223 int l = snprintf(maildir, sizeof(maildir), ":%s/%s",
224 _PATH_MAILDIR, name);
225 if (l > sizeof(maildir) || strcmp(maildir, fn) != 0)
226 return;
227 }
228 for (ep = utmp; ep != NULL; ep = ep->next)
229 if (strcmp(ep->name, name) == 0)
230 notify(ep, offset);
231 }
232
233 static void
234 notify(const struct utmpentry *ep, off_t offset)
235 {
236 FILE *tp;
237 struct passwd *p;
238 struct stat stb;
239 struct termios ttybuf;
240 char tty[sizeof(_PATH_DEV) + sizeof(ep->line) + 1];
241 const char *cr;
242
243 (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ep->line);
244 if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
245 /* A slash is an attempt to break security... */
246 /*
247 * XXX but what about something like "/dev/pts/5"
248 * that we may one day "support". ?
249 */
250 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
251 return;
252 }
253 if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
254 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", ep->name, tty);
255 return;
256 }
257 dsyslog(LOG_DEBUG, "notify %s on %s", ep->name, tty);
258 if (fork())
259 return;
260 (void)signal(SIGALRM, SIG_DFL);
261 (void)alarm((u_int)30);
262 if ((tp = fopen(tty, "w")) == NULL) {
263 dsyslog(LOG_ERR, "open `%s' (%s)", tty, strerror(errno));
264 _exit(1);
265 }
266 if (tcgetattr(fileno(tp), &ttybuf) == -1) {
267 dsyslog(LOG_ERR, "tcgetattr `%s' (%s)", tty, strerror(errno));
268 _exit(1);
269 }
270 cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
271 "\n" : "\n\r";
272 /* Set uid/gid/groups to users in case mail drop is on nfs */
273 if ((p = getpwnam(ep->name)) == NULL ||
274 initgroups(p->pw_name, p->pw_gid) == -1 ||
275 setgid(p->pw_gid) == -1 ||
276 setuid(p->pw_uid) == -1)
277 _exit(1);
278
279 if (logging)
280 syslog(LOG_INFO, "biff message for %s", ep->name);
281
282 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
283 cr, ep->name, (int)sizeof(hostname), hostname, cr, cr);
284 jkfprintf(tp, ep->name, offset, cr);
285 (void)fclose(tp);
286 _exit(0);
287 }
288
289 static void
290 jkfprintf(FILE *tp, const char *name, off_t offset, const char *cr)
291 {
292 FILE *fi;
293 int linecnt, charcnt, inheader;
294 char line[BUFSIZ], visline[BUFSIZ * 4 + 1], *nl;
295
296 if ((fi = fopen(name, "r")) == NULL)
297 return;
298
299 (void)fseeko(fi, offset, SEEK_SET);
300 /*
301 * Print the first 7 lines or 560 characters of the new mail
302 * (whichever comes first). Skip header crap other than
303 * From, Subject, To, and Date.
304 */
305 linecnt = 7;
306 charcnt = 560;
307 inheader = 1;
308 while (fgets(line, sizeof(line), fi) != NULL) {
309 line[sizeof(line) - 1] = '\0';
310 if (inheader) {
311 if (line[0] == '\n') {
312 inheader = 0;
313 continue;
314 }
315 if (line[0] == ' ' || line[0] == '\t' ||
316 (strncasecmp(line, "From:", 5) &&
317 strncasecmp(line, "Subject:", 8)))
318 continue;
319 }
320 if (strncmp(line, "From ", 5) == 0) {
321 (void)fprintf(tp, "----%s", cr);
322 (void)fclose(fi);
323 return;
324 }
325 if (linecnt <= 0 || charcnt <= 0) {
326 (void)fprintf(tp, "...more...%s", cr);
327 (void)fclose(fi);
328 return;
329 }
330 if ((nl = strchr(line, '\n')) != NULL)
331 *nl = '\0';
332 /* strip weird stuff so can't trojan horse stupid terminals */
333 (void)strvis(visline, line, VIS_CSTYLE);
334 (void)fputs(visline, tp);
335 (void)fputs(cr, tp);
336 --linecnt;
337 }
338 (void)fprintf(tp, "----%s\n", cr);
339 (void)fclose(fi);
340 }
341