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