comsat.c revision 1.30 1 /* $NetBSD: comsat.c,v 1.30 2004/07/10 07:10:43 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.30 2004/07/10 07:10:43 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_UTMPX, &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 intmax_t val;
207
208 if (!(cp = strchr(name, '@')))
209 return;
210 *cp = '\0';
211 errno = 0;
212 offset = val = strtoimax(cp + 1, &fn, 10);
213 if (errno == ERANGE || offset != val)
214 return;
215 if (fn && *fn && *fn != '\n') {
216 /*
217 * Procmail sends messages to comsat with a trailing colon
218 * and a pathname to the folder where the new message was
219 * deposited. Since we can't reliably open only regular
220 * files, we need to ignore these. With one exception:
221 * if it mentions the user's system mailbox.
222 */
223 char maildir[MAXPATHLEN];
224 int l = snprintf(maildir, sizeof(maildir), ":%s/%s",
225 _PATH_MAILDIR, name);
226 if (l > sizeof(maildir) || strcmp(maildir, fn) != 0)
227 return;
228 }
229 for (ep = utmp; ep != NULL; ep = ep->next)
230 if (strcmp(ep->name, name) == 0)
231 notify(ep, offset);
232 }
233
234 static void
235 notify(const struct utmpentry *ep, off_t offset)
236 {
237 FILE *tp;
238 struct passwd *p;
239 struct stat stb;
240 struct termios ttybuf;
241 char tty[sizeof(_PATH_DEV) + sizeof(ep->line) + 1];
242 const char *cr;
243
244 (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ep->line);
245 if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
246 /* A slash is an attempt to break security... */
247 /*
248 * XXX but what about something like "/dev/pts/5"
249 * that we may one day "support". ?
250 */
251 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
252 return;
253 }
254 if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
255 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", ep->name, tty);
256 return;
257 }
258 dsyslog(LOG_DEBUG, "notify %s on %s", ep->name, tty);
259 if (fork())
260 return;
261 (void)signal(SIGALRM, SIG_DFL);
262 (void)alarm((u_int)30);
263 if ((tp = fopen(tty, "w")) == NULL) {
264 dsyslog(LOG_ERR, "open `%s' (%s)", tty, strerror(errno));
265 _exit(1);
266 }
267 if (tcgetattr(fileno(tp), &ttybuf) == -1) {
268 dsyslog(LOG_ERR, "tcgetattr `%s' (%s)", tty, strerror(errno));
269 _exit(1);
270 }
271 cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
272 "\n" : "\n\r";
273 /* Set uid/gid/groups to users in case mail drop is on nfs */
274 if ((p = getpwnam(ep->name)) == NULL ||
275 initgroups(p->pw_name, p->pw_gid) == -1 ||
276 setgid(p->pw_gid) == -1 ||
277 setuid(p->pw_uid) == -1)
278 _exit(1);
279
280 if (logging)
281 syslog(LOG_INFO, "biff message for %s", ep->name);
282
283 (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
284 cr, ep->name, (int)sizeof(hostname), hostname, cr, cr);
285 jkfprintf(tp, ep->name, offset, cr);
286 (void)fclose(tp);
287 _exit(0);
288 }
289
290 static void
291 jkfprintf(FILE *tp, const char *name, off_t offset, const char *cr)
292 {
293 FILE *fi;
294 int linecnt, charcnt, inheader;
295 char line[BUFSIZ], visline[BUFSIZ * 4 + 1], *nl;
296
297 if ((fi = fopen(name, "r")) == NULL)
298 return;
299
300 (void)fseeko(fi, offset, SEEK_SET);
301 /*
302 * Print the first 7 lines or 560 characters of the new mail
303 * (whichever comes first). Skip header crap other than
304 * From, Subject, To, and Date.
305 */
306 linecnt = 7;
307 charcnt = 560;
308 inheader = 1;
309 while (fgets(line, sizeof(line), fi) != NULL) {
310 line[sizeof(line) - 1] = '\0';
311 if (inheader) {
312 if (line[0] == '\n') {
313 inheader = 0;
314 continue;
315 }
316 if (line[0] == ' ' || line[0] == '\t' ||
317 (strncasecmp(line, "From:", 5) &&
318 strncasecmp(line, "Subject:", 8)))
319 continue;
320 }
321 if (strncmp(line, "From ", 5) == 0) {
322 (void)fprintf(tp, "----%s", cr);
323 (void)fclose(fi);
324 return;
325 }
326 if (linecnt <= 0 || charcnt <= 0) {
327 (void)fprintf(tp, "...more...%s", cr);
328 (void)fclose(fi);
329 return;
330 }
331 if ((nl = strchr(line, '\n')) != NULL)
332 *nl = '\0';
333 /* strip weird stuff so can't trojan horse stupid terminals */
334 (void)strvis(visline, line, VIS_CSTYLE);
335 (void)fputs(visline, tp);
336 (void)fputs(cr, tp);
337 --linecnt;
338 }
339 (void)fprintf(tp, "----%s\n", cr);
340 (void)fclose(fi);
341 }
342