comsat.c revision 1.30 1 1.30 enami /* $NetBSD: comsat.c,v 1.30 2004/07/10 07:10:43 enami Exp $ */
2 1.10 mrg
3 1.1 cgd /*
4 1.6 jtc * Copyright (c) 1980, 1993
5 1.6 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.25 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.10 mrg #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.10 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
35 1.10 mrg The Regents of the University of California. All rights reserved.\n");
36 1.10 mrg #if 0
37 1.10 mrg static char sccsid[] = "from: @(#)comsat.c 8.1 (Berkeley) 6/4/93";
38 1.10 mrg #else
39 1.30 enami __RCSID("$NetBSD: comsat.c,v 1.30 2004/07/10 07:10:43 enami Exp $");
40 1.10 mrg #endif
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #include <sys/param.h>
44 1.1 cgd #include <sys/socket.h>
45 1.1 cgd #include <sys/stat.h>
46 1.1 cgd #include <sys/file.h>
47 1.1 cgd #include <sys/wait.h>
48 1.1 cgd
49 1.1 cgd #include <netinet/in.h>
50 1.1 cgd
51 1.6 jtc #include <ctype.h>
52 1.6 jtc #include <errno.h>
53 1.6 jtc #include <netdb.h>
54 1.6 jtc #include <paths.h>
55 1.6 jtc #include <pwd.h>
56 1.27 christos #include <err.h>
57 1.6 jtc #include <signal.h>
58 1.1 cgd #include <stdio.h>
59 1.5 jtc #include <stdlib.h>
60 1.6 jtc #include <string.h>
61 1.1 cgd #include <syslog.h>
62 1.8 mycroft #include <termios.h>
63 1.11 kleink #include <time.h>
64 1.12 mrg #include <vis.h>
65 1.5 jtc #include <unistd.h>
66 1.27 christos #ifdef SUPPORT_UTMP
67 1.6 jtc #include <utmp.h>
68 1.27 christos #endif
69 1.27 christos #ifdef SUPPORT_UTMPX
70 1.27 christos #include <utmpx.h>
71 1.27 christos #endif
72 1.27 christos
73 1.27 christos #include "utmpentry.h"
74 1.27 christos
75 1.27 christos #if !defined(SUPPORT_UTMP) && !defined(SUPPORT_UTMPX)
76 1.27 christos #error "SUPPORT_UTMP and/or SUPPORT_UTMPX must be defined"
77 1.27 christos #endif
78 1.1 cgd
79 1.1 cgd #define dsyslog if (debug) syslog
80 1.1 cgd
81 1.1 cgd #define MAXIDLE 120
82 1.1 cgd
83 1.27 christos static int logging;
84 1.27 christos static int debug = 0;
85 1.27 christos static char hostname[MAXHOSTNAMELEN+1];
86 1.27 christos static time_t utmpmtime; /* last modification time for utmp/x */
87 1.27 christos static int nutmp;
88 1.27 christos static struct utmpentry *utmp = NULL;
89 1.27 christos static time_t lastmsgtime;
90 1.27 christos
91 1.27 christos int main(int, char *[]);
92 1.27 christos static void jkfprintf(FILE *, const char *, off_t, const char *);
93 1.27 christos static void mailfor(const char *);
94 1.27 christos static void notify(const struct utmpentry *, off_t);
95 1.27 christos static void onalrm(int);
96 1.27 christos static void reapchildren(int);
97 1.6 jtc
98 1.6 jtc int
99 1.17 mjl main(int argc, char *argv[])
100 1.1 cgd {
101 1.15 mjl struct sockaddr_storage from;
102 1.13 mrg int cc, ch;
103 1.27 christos socklen_t fromlen;
104 1.1 cgd char msgbuf[100];
105 1.27 christos sigset_t nsigset, osigset;
106 1.1 cgd
107 1.1 cgd /* verify proper invocation */
108 1.1 cgd fromlen = sizeof(from);
109 1.27 christos if (getsockname(0, (struct sockaddr *)(void *)&from, &fromlen) == -1)
110 1.28 enami err(1, "getsockname");
111 1.13 mrg
112 1.1 cgd openlog("comsat", LOG_PID, LOG_DAEMON);
113 1.13 mrg while ((ch = getopt(argc, argv, "l")) != -1)
114 1.13 mrg switch (ch) {
115 1.13 mrg case 'l':
116 1.13 mrg logging = 1;
117 1.13 mrg break;
118 1.13 mrg default:
119 1.19 cgd syslog(LOG_ERR, "Usage: %s [-l]", getprogname());
120 1.13 mrg exit(1);
121 1.13 mrg }
122 1.27 christos if (chdir(_PATH_MAILDIR) == -1) {
123 1.1 cgd syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
124 1.12 mrg (void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
125 1.1 cgd exit(1);
126 1.1 cgd }
127 1.1 cgd (void)time(&lastmsgtime);
128 1.1 cgd (void)gethostname(hostname, sizeof(hostname));
129 1.14 mrg hostname[sizeof(hostname) - 1] = '\0';
130 1.6 jtc onalrm(0);
131 1.1 cgd (void)signal(SIGALRM, onalrm);
132 1.1 cgd (void)signal(SIGTTOU, SIG_IGN);
133 1.1 cgd (void)signal(SIGCHLD, reapchildren);
134 1.27 christos (void)sigemptyset(&nsigset);
135 1.27 christos (void)sigaddset(&nsigset, SIGALRM);
136 1.27 christos if (sigprocmask(SIG_SETMASK, NULL, &osigset) == -1) {
137 1.27 christos syslog(LOG_ERR, "sigprocmask get failed (%m)");
138 1.27 christos exit(1);
139 1.27 christos }
140 1.1 cgd for (;;) {
141 1.1 cgd cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
142 1.1 cgd if (cc <= 0) {
143 1.1 cgd if (errno != EINTR)
144 1.1 cgd sleep(1);
145 1.1 cgd errno = 0;
146 1.1 cgd continue;
147 1.1 cgd }
148 1.1 cgd if (!nutmp) /* no one has logged in yet */
149 1.1 cgd continue;
150 1.27 christos if (sigprocmask(SIG_SETMASK, &nsigset, NULL) == -1) {
151 1.27 christos syslog(LOG_ERR, "sigprocmask set failed (%m)");
152 1.27 christos exit(1);
153 1.27 christos }
154 1.4 cgd msgbuf[cc] = '\0';
155 1.1 cgd (void)time(&lastmsgtime);
156 1.1 cgd mailfor(msgbuf);
157 1.27 christos if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1) {
158 1.27 christos syslog(LOG_ERR, "sigprocmask restore failed (%m)");
159 1.27 christos exit(1);
160 1.27 christos }
161 1.1 cgd }
162 1.1 cgd }
163 1.1 cgd
164 1.27 christos static void
165 1.27 christos /*ARGSUSED*/
166 1.17 mjl reapchildren(int signo)
167 1.1 cgd {
168 1.12 mrg
169 1.27 christos while (wait3(NULL, WNOHANG, NULL) != -1)
170 1.27 christos continue;
171 1.1 cgd }
172 1.1 cgd
173 1.27 christos static void
174 1.27 christos /*ARGSUSED*/
175 1.17 mjl onalrm(int signo)
176 1.1 cgd {
177 1.1 cgd struct stat statbf;
178 1.27 christos time_t newtime = 0;
179 1.1 cgd
180 1.6 jtc if (time(NULL) - lastmsgtime >= MAXIDLE)
181 1.1 cgd exit(0);
182 1.1 cgd (void)alarm((u_int)15);
183 1.27 christos #ifdef SUPPORT_UTMP
184 1.27 christos if (stat(_PATH_UTMP, &statbf) != -1)
185 1.27 christos if (statbf.st_mtime > newtime)
186 1.27 christos newtime = statbf.st_mtime;
187 1.27 christos #endif
188 1.27 christos #ifdef SUPPORT_UTMPX
189 1.29 christos if (stat(_PATH_UTMPX, &statbf) != -1)
190 1.27 christos if (statbf.st_mtime > newtime)
191 1.27 christos newtime = statbf.st_mtime;
192 1.27 christos #endif
193 1.27 christos if (newtime > utmpmtime) {
194 1.27 christos freeutentries(utmp);
195 1.27 christos nutmp = getutentries(NULL, &utmp);
196 1.27 christos utmpmtime = newtime;
197 1.1 cgd }
198 1.1 cgd }
199 1.1 cgd
200 1.27 christos static void
201 1.27 christos mailfor(const char *name)
202 1.1 cgd {
203 1.27 christos struct utmpentry *ep;
204 1.21 atatat char *cp, *fn;
205 1.1 cgd off_t offset;
206 1.30 enami intmax_t val;
207 1.1 cgd
208 1.6 jtc if (!(cp = strchr(name, '@')))
209 1.1 cgd return;
210 1.1 cgd *cp = '\0';
211 1.21 atatat errno = 0;
212 1.30 enami offset = val = strtoimax(cp + 1, &fn, 10);
213 1.30 enami if (errno == ERANGE || offset != val)
214 1.21 atatat return;
215 1.22 onoe if (fn && *fn && *fn != '\n') {
216 1.21 atatat /*
217 1.21 atatat * Procmail sends messages to comsat with a trailing colon
218 1.21 atatat * and a pathname to the folder where the new message was
219 1.21 atatat * deposited. Since we can't reliably open only regular
220 1.21 atatat * files, we need to ignore these. With one exception:
221 1.21 atatat * if it mentions the user's system mailbox.
222 1.21 atatat */
223 1.27 christos char maildir[MAXPATHLEN];
224 1.21 atatat int l = snprintf(maildir, sizeof(maildir), ":%s/%s",
225 1.21 atatat _PATH_MAILDIR, name);
226 1.21 atatat if (l > sizeof(maildir) || strcmp(maildir, fn) != 0)
227 1.21 atatat return;
228 1.21 atatat }
229 1.27 christos for (ep = utmp; ep != NULL; ep = ep->next)
230 1.27 christos if (strcmp(ep->name, name) == 0)
231 1.27 christos notify(ep, offset);
232 1.1 cgd }
233 1.1 cgd
234 1.27 christos static void
235 1.27 christos notify(const struct utmpentry *ep, off_t offset)
236 1.1 cgd {
237 1.4 cgd FILE *tp;
238 1.12 mrg struct passwd *p;
239 1.4 cgd struct stat stb;
240 1.8 mycroft struct termios ttybuf;
241 1.27 christos char tty[sizeof(_PATH_DEV) + sizeof(ep->line) + 1];
242 1.27 christos const char *cr;
243 1.1 cgd
244 1.27 christos (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ep->line);
245 1.6 jtc if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
246 1.4 cgd /* A slash is an attempt to break security... */
247 1.12 mrg /*
248 1.12 mrg * XXX but what about something like "/dev/pts/5"
249 1.12 mrg * that we may one day "support". ?
250 1.12 mrg */
251 1.4 cgd syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
252 1.4 cgd return;
253 1.4 cgd }
254 1.1 cgd if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
255 1.27 christos dsyslog(LOG_DEBUG, "%s: wrong mode on %s", ep->name, tty);
256 1.1 cgd return;
257 1.1 cgd }
258 1.27 christos dsyslog(LOG_DEBUG, "notify %s on %s", ep->name, tty);
259 1.1 cgd if (fork())
260 1.1 cgd return;
261 1.1 cgd (void)signal(SIGALRM, SIG_DFL);
262 1.1 cgd (void)alarm((u_int)30);
263 1.1 cgd if ((tp = fopen(tty, "w")) == NULL) {
264 1.27 christos dsyslog(LOG_ERR, "open `%s' (%s)", tty, strerror(errno));
265 1.27 christos _exit(1);
266 1.27 christos }
267 1.27 christos if (tcgetattr(fileno(tp), &ttybuf) == -1) {
268 1.27 christos dsyslog(LOG_ERR, "tcgetattr `%s' (%s)", tty, strerror(errno));
269 1.16 mjl _exit(1);
270 1.1 cgd }
271 1.8 mycroft cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
272 1.1 cgd "\n" : "\n\r";
273 1.12 mrg /* Set uid/gid/groups to users in case mail drop is on nfs */
274 1.27 christos if ((p = getpwnam(ep->name)) == NULL ||
275 1.27 christos initgroups(p->pw_name, p->pw_gid) == -1 ||
276 1.27 christos setgid(p->pw_gid) == -1 ||
277 1.27 christos setuid(p->pw_uid) == -1)
278 1.16 mjl _exit(1);
279 1.13 mrg
280 1.13 mrg if (logging)
281 1.27 christos syslog(LOG_INFO, "biff message for %s", ep->name);
282 1.12 mrg
283 1.1 cgd (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
284 1.27 christos cr, ep->name, (int)sizeof(hostname), hostname, cr, cr);
285 1.27 christos jkfprintf(tp, ep->name, offset, cr);
286 1.1 cgd (void)fclose(tp);
287 1.1 cgd _exit(0);
288 1.1 cgd }
289 1.1 cgd
290 1.27 christos static void
291 1.27 christos jkfprintf(FILE *tp, const char *name, off_t offset, const char *cr)
292 1.1 cgd {
293 1.12 mrg FILE *fi;
294 1.12 mrg int linecnt, charcnt, inheader;
295 1.27 christos char line[BUFSIZ], visline[BUFSIZ * 4 + 1], *nl;
296 1.4 cgd
297 1.1 cgd if ((fi = fopen(name, "r")) == NULL)
298 1.1 cgd return;
299 1.6 jtc
300 1.27 christos (void)fseeko(fi, offset, SEEK_SET);
301 1.1 cgd /*
302 1.1 cgd * Print the first 7 lines or 560 characters of the new mail
303 1.1 cgd * (whichever comes first). Skip header crap other than
304 1.1 cgd * From, Subject, To, and Date.
305 1.1 cgd */
306 1.1 cgd linecnt = 7;
307 1.1 cgd charcnt = 560;
308 1.1 cgd inheader = 1;
309 1.1 cgd while (fgets(line, sizeof(line), fi) != NULL) {
310 1.20 atatat line[sizeof(line) - 1] = '\0';
311 1.1 cgd if (inheader) {
312 1.1 cgd if (line[0] == '\n') {
313 1.1 cgd inheader = 0;
314 1.1 cgd continue;
315 1.1 cgd }
316 1.1 cgd if (line[0] == ' ' || line[0] == '\t' ||
317 1.12 mrg (strncasecmp(line, "From:", 5) &&
318 1.12 mrg strncasecmp(line, "Subject:", 8)))
319 1.1 cgd continue;
320 1.21 atatat }
321 1.21 atatat if (strncmp(line, "From ", 5) == 0) {
322 1.21 atatat (void)fprintf(tp, "----%s", cr);
323 1.21 atatat (void)fclose(fi);
324 1.21 atatat return;
325 1.1 cgd }
326 1.1 cgd if (linecnt <= 0 || charcnt <= 0) {
327 1.1 cgd (void)fprintf(tp, "...more...%s", cr);
328 1.6 jtc (void)fclose(fi);
329 1.1 cgd return;
330 1.1 cgd }
331 1.20 atatat if ((nl = strchr(line, '\n')) != NULL)
332 1.20 atatat *nl = '\0';
333 1.1 cgd /* strip weird stuff so can't trojan horse stupid terminals */
334 1.12 mrg (void)strvis(visline, line, VIS_CSTYLE);
335 1.20 atatat (void)fputs(visline, tp);
336 1.20 atatat (void)fputs(cr, tp);
337 1.1 cgd --linecnt;
338 1.1 cgd }
339 1.1 cgd (void)fprintf(tp, "----%s\n", cr);
340 1.6 jtc (void)fclose(fi);
341 1.1 cgd }
342