1/*
2 * Copyright (c) 1980, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)comsat.c	8.1 (Berkeley) 6/4/93";
43#endif
44static const char rcsid[] =
45  "$FreeBSD$";
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/socket.h>
50#include <sys/stat.h>
51#include <sys/file.h>
52#include <sys/wait.h>
53
54#include <netinet/in.h>
55
56#include <ctype.h>
57#include <err.h>
58#include <errno.h>
59#include <netdb.h>
60#include <paths.h>
61#include <pwd.h>
62#include <termios.h>
63#include <signal.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <syslog.h>
68#include <unistd.h>
69#include <utmpx.h>
70
71int	debug = 0;
72#define	dsyslog	if (debug) syslog
73
74#define MAXIDLE	120
75
76char	hostname[MAXHOSTNAMELEN];
77
78void jkfprintf(FILE *, char[], char[], off_t);
79void mailfor(char *);
80void notify(struct utmpx *, char[], off_t, int);
81void reapchildren(int);
82
83int
84main(int argc __unused, char *argv[] __unused)
85{
86	struct sockaddr_in from;
87	socklen_t fromlen;
88	int cc;
89	char msgbuf[256];
90
91	/* verify proper invocation */
92	fromlen = sizeof(from);
93	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
94		err(1, "getsockname");
95	openlog("comsat", LOG_PID, LOG_DAEMON);
96	if (chdir(_PATH_MAILDIR)) {
97		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
98		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
99		exit(1);
100	}
101	(void)gethostname(hostname, sizeof(hostname));
102	(void)signal(SIGTTOU, SIG_IGN);
103	(void)signal(SIGCHLD, reapchildren);
104	for (;;) {
105		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
106		if (cc <= 0) {
107			if (errno != EINTR)
108				sleep(1);
109			errno = 0;
110			continue;
111		}
112		msgbuf[cc] = '\0';
113		mailfor(msgbuf);
114		sigsetmask(0L);
115	}
116}
117
118void
119reapchildren(int signo __unused)
120{
121	while (wait3(NULL, WNOHANG, NULL) > 0);
122}
123
124void
125mailfor(char *name)
126{
127	struct utmpx *utp;
128	char *cp;
129	char *file;
130	off_t offset;
131	int folder;
132	char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
133	char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
134
135	if (!(cp = strchr(name, '@')))
136		return;
137	*cp = '\0';
138	offset = strtoll(cp + 1, NULL, 10);
139	if (!(cp = strchr(cp + 1, ':')))
140		file = name;
141	else
142		file = cp + 1;
143	sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
144	    name);
145	if (*file != '/') {
146		sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
147		    (int)sizeof(utp->ut_user), file);
148		file = buf2;
149	}
150	folder = strcmp(buf, file);
151	setutxent();
152	while ((utp = getutxent()) != NULL)
153		if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
154			notify(utp, file, offset, folder);
155	endutxent();
156}
157
158static const char *cr;
159
160void
161notify(struct utmpx *utp, char file[], off_t offset, int folder)
162{
163	FILE *tp;
164	struct stat stb;
165	struct termios tio;
166	char tty[20];
167	const char *s = utp->ut_line;
168
169	if (strncmp(s, "pts/", 4) == 0)
170		s += 4;
171	if (strchr(s, '/')) {
172		/* A slash is an attempt to break security... */
173		syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
174		    utp->ut_line);
175		return;
176	}
177	(void)snprintf(tty, sizeof(tty), "%s%.*s",
178	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
179	if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
180		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
181		return;
182	}
183	dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
184	switch (fork()) {
185	case -1:
186		syslog(LOG_NOTICE, "fork failed (%m)");
187		return;
188	case 0:
189		break;
190	default:
191		return;
192	}
193	if ((tp = fopen(tty, "w")) == NULL) {
194		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
195		_exit(1);
196	}
197	(void)tcgetattr(fileno(tp), &tio);
198	cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ?  "\n" : "\n\r";
199	switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
200	case S_IXUSR:
201	case (S_IXUSR | S_IXGRP):
202		(void)fprintf(tp,
203		    "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
204		    cr, utp->ut_user, (int)sizeof(hostname), hostname,
205		    folder ? cr : "", folder ? "to " : "", folder ? file : "",
206		    cr, cr);
207		jkfprintf(tp, utp->ut_user, file, offset);
208		break;
209	case S_IXGRP:
210		(void)fprintf(tp, "\007");
211		(void)fflush(tp);
212		(void)sleep(1);
213		(void)fprintf(tp, "\007");
214		break;
215	default:
216		break;
217	}
218	(void)fclose(tp);
219	_exit(0);
220}
221
222void
223jkfprintf(FILE *tp, char user[], char file[], off_t offset)
224{
225	unsigned char *cp, ch;
226	FILE *fi;
227	int linecnt, charcnt, inheader;
228	struct passwd *p;
229	unsigned char line[BUFSIZ];
230
231	/* Set effective uid to user in case mail drop is on nfs */
232	if ((p = getpwnam(user)) != NULL)
233		(void) setuid(p->pw_uid);
234
235	if ((fi = fopen(file, "r")) == NULL)
236		return;
237
238	(void)fseeko(fi, offset, SEEK_CUR);
239	/*
240	 * Print the first 7 lines or 560 characters of the new mail
241	 * (whichever comes first).  Skip header crap other than
242	 * From, Subject, To, and Date.
243	 */
244	linecnt = 7;
245	charcnt = 560;
246	inheader = 1;
247	while (fgets(line, sizeof(line), fi) != NULL) {
248		if (inheader) {
249			if (line[0] == '\n') {
250				inheader = 0;
251				continue;
252			}
253			if (line[0] == ' ' || line[0] == '\t' ||
254			    (strncmp(line, "From:", 5) &&
255			    strncmp(line, "Subject:", 8)))
256				continue;
257		}
258		if (linecnt <= 0 || charcnt <= 0) {
259			(void)fprintf(tp, "...more...%s", cr);
260			(void)fclose(fi);
261			return;
262		}
263		/* strip weird stuff so can't trojan horse stupid terminals */
264		for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
265			/* disable upper controls and enable all other
266			   8bit codes due to lack of locale knowledge
267			 */
268			if (((ch & 0x80) && ch < 0xA0) ||
269			    (!(ch & 0x80) && !isprint(ch) &&
270			     !isspace(ch) && ch != '\a' && ch != '\b')
271			   ) {
272				if (ch & 0x80) {
273					ch &= ~0x80;
274					(void)fputs("M-", tp);
275				}
276				if (iscntrl(ch)) {
277					ch ^= 0x40;
278					(void)fputc('^', tp);
279				}
280			}
281			(void)fputc(ch, tp);
282		}
283		(void)fputs(cr, tp);
284		--linecnt;
285	}
286	(void)fprintf(tp, "----%s\n", cr);
287	(void)fclose(fi);
288}
289