xref: /freebsd/libexec/comsat/comsat.c (revision 6469bdcdb6a5968dc7edfcfb495d427b4bfdb3dd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)comsat.c	8.1 (Berkeley) 6/4/93";
41 #endif
42 static const char rcsid[] =
43   "$FreeBSD$";
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <sys/stat.h>
49 #include <sys/file.h>
50 #include <sys/wait.h>
51 
52 #include <netinet/in.h>
53 
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <netdb.h>
58 #include <paths.h>
59 #include <pwd.h>
60 #include <termios.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <syslog.h>
66 #include <unistd.h>
67 #include <utmpx.h>
68 
69 static int	debug = 0;
70 #define	dsyslog	if (debug) syslog
71 
72 #define MAXIDLE	120
73 
74 static char	hostname[MAXHOSTNAMELEN];
75 
76 static void	jkfprintf(FILE *, char[], char[], off_t);
77 static void	mailfor(char *);
78 static void	notify(struct utmpx *, char[], off_t, int);
79 static void	reapchildren(int);
80 
81 int
82 main(int argc __unused, char *argv[] __unused)
83 {
84 	struct sockaddr_in from;
85 	socklen_t fromlen;
86 	int cc;
87 	char msgbuf[256];
88 
89 	/* verify proper invocation */
90 	fromlen = sizeof(from);
91 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
92 		err(1, "getsockname");
93 	openlog("comsat", LOG_PID, LOG_DAEMON);
94 	if (chdir(_PATH_MAILDIR)) {
95 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
96 		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
97 		exit(1);
98 	}
99 	(void)gethostname(hostname, sizeof(hostname));
100 	(void)signal(SIGTTOU, SIG_IGN);
101 	(void)signal(SIGCHLD, reapchildren);
102 	for (;;) {
103 		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
104 		if (cc <= 0) {
105 			if (errno != EINTR)
106 				sleep(1);
107 			errno = 0;
108 			continue;
109 		}
110 		msgbuf[cc] = '\0';
111 		mailfor(msgbuf);
112 		sigsetmask(0L);
113 	}
114 }
115 
116 static void
117 reapchildren(int signo __unused)
118 {
119 	while (wait3(NULL, WNOHANG, NULL) > 0);
120 }
121 
122 static void
123 mailfor(char *name)
124 {
125 	struct utmpx *utp;
126 	char *cp;
127 	char *file;
128 	off_t offset;
129 	int folder;
130 	char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
131 	char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
132 
133 	if (!(cp = strchr(name, '@')))
134 		return;
135 	*cp = '\0';
136 	offset = strtoll(cp + 1, NULL, 10);
137 	if (!(cp = strchr(cp + 1, ':')))
138 		file = name;
139 	else
140 		file = cp + 1;
141 	sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
142 	    name);
143 	if (*file != '/') {
144 		sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
145 		    (int)sizeof(utp->ut_user), file);
146 		file = buf2;
147 	}
148 	folder = strcmp(buf, file);
149 	setutxent();
150 	while ((utp = getutxent()) != NULL)
151 		if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
152 			notify(utp, file, offset, folder);
153 	endutxent();
154 }
155 
156 static const char *cr;
157 
158 static void
159 notify(struct utmpx *utp, char file[], off_t offset, int folder)
160 {
161 	FILE *tp;
162 	struct stat stb;
163 	struct termios tio;
164 	char tty[20];
165 	const char *s = utp->ut_line;
166 
167 	if (strncmp(s, "pts/", 4) == 0)
168 		s += 4;
169 	if (strchr(s, '/')) {
170 		/* A slash is an attempt to break security... */
171 		syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
172 		    utp->ut_line);
173 		return;
174 	}
175 	(void)snprintf(tty, sizeof(tty), "%s%.*s",
176 	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
177 	if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
178 		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
179 		return;
180 	}
181 	dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
182 	switch (fork()) {
183 	case -1:
184 		syslog(LOG_NOTICE, "fork failed (%m)");
185 		return;
186 	case 0:
187 		break;
188 	default:
189 		return;
190 	}
191 	if ((tp = fopen(tty, "w")) == NULL) {
192 		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
193 		_exit(1);
194 	}
195 	(void)tcgetattr(fileno(tp), &tio);
196 	cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ?  "\n" : "\n\r";
197 	switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
198 	case S_IXUSR:
199 	case (S_IXUSR | S_IXGRP):
200 		(void)fprintf(tp,
201 		    "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
202 		    cr, utp->ut_user, (int)sizeof(hostname), hostname,
203 		    folder ? cr : "", folder ? "to " : "", folder ? file : "",
204 		    cr, cr);
205 		jkfprintf(tp, utp->ut_user, file, offset);
206 		break;
207 	case S_IXGRP:
208 		(void)fprintf(tp, "\007");
209 		(void)fflush(tp);
210 		(void)sleep(1);
211 		(void)fprintf(tp, "\007");
212 		break;
213 	default:
214 		break;
215 	}
216 	(void)fclose(tp);
217 	_exit(0);
218 }
219 
220 static void
221 jkfprintf(FILE *tp, char user[], char file[], off_t offset)
222 {
223 	unsigned char *cp, ch;
224 	FILE *fi;
225 	int linecnt, charcnt, inheader;
226 	struct passwd *p;
227 	unsigned char line[BUFSIZ];
228 
229 	/* Set effective uid to user in case mail drop is on nfs */
230 	if ((p = getpwnam(user)) != NULL)
231 		(void) setuid(p->pw_uid);
232 
233 	if ((fi = fopen(file, "r")) == NULL)
234 		return;
235 
236 	(void)fseeko(fi, offset, SEEK_CUR);
237 	/*
238 	 * Print the first 7 lines or 560 characters of the new mail
239 	 * (whichever comes first).  Skip header crap other than
240 	 * From, Subject, To, and Date.
241 	 */
242 	linecnt = 7;
243 	charcnt = 560;
244 	inheader = 1;
245 	while (fgets(line, sizeof(line), fi) != NULL) {
246 		if (inheader) {
247 			if (line[0] == '\n') {
248 				inheader = 0;
249 				continue;
250 			}
251 			if (line[0] == ' ' || line[0] == '\t' ||
252 			    (strncmp(line, "From:", 5) &&
253 			    strncmp(line, "Subject:", 8)))
254 				continue;
255 		}
256 		if (linecnt <= 0 || charcnt <= 0) {
257 			(void)fprintf(tp, "...more...%s", cr);
258 			(void)fclose(fi);
259 			return;
260 		}
261 		/* strip weird stuff so can't trojan horse stupid terminals */
262 		for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
263 			/* disable upper controls and enable all other
264 			   8bit codes due to lack of locale knowledge
265 			 */
266 			if (((ch & 0x80) && ch < 0xA0) ||
267 			    (!(ch & 0x80) && !isprint(ch) &&
268 			     !isspace(ch) && ch != '\a' && ch != '\b')
269 			   ) {
270 				if (ch & 0x80) {
271 					ch &= ~0x80;
272 					(void)fputs("M-", tp);
273 				}
274 				if (iscntrl(ch)) {
275 					ch ^= 0x40;
276 					(void)fputc('^', tp);
277 				}
278 			}
279 			(void)fputc(ch, tp);
280 		}
281 		(void)fputs(cr, tp);
282 		--linecnt;
283 	}
284 	(void)fprintf(tp, "----%s\n", cr);
285 	(void)fclose(fi);
286 }
287