xref: /openbsd-src/libexec/comsat/comsat.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: comsat.c,v 1.37 2012/12/04 02:24:47 deraadt 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/limits.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 
37 #include <netinet/in.h>
38 
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <netdb.h>
43 #include <paths.h>
44 #include <pwd.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <syslog.h>
50 #include <termios.h>
51 #include <unistd.h>
52 #include <utmp.h>
53 #include <vis.h>
54 
55 int	debug = 0;
56 #define	dsyslog	if (debug) syslog
57 
58 #define MAXIDLE	120
59 
60 char	hostname[MAXHOSTNAMELEN];
61 struct	utmp *utmp = NULL;
62 time_t	lastmsgtime;
63 int	nutmp, uf;
64 
65 void jkfprintf(FILE *, char[], off_t);
66 void mailfor(char *);
67 void notify(struct utmp *, off_t);
68 void readutmp(int);
69 void doreadutmp(void);
70 void reapchildren(int);
71 
72 volatile sig_atomic_t wantreadutmp;
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	struct sockaddr_storage from;
78 	struct sigaction sa;
79 	ssize_t cc;
80 	socklen_t fromlen;
81 	char msgbuf[100];
82 	sigset_t sigset;
83 
84 	/* verify proper invocation */
85 	fromlen = sizeof(from);
86 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) == -1) {
87 		(void)fprintf(stderr,
88 		    "comsat: getsockname: %s.\n", strerror(errno));
89 		exit(1);
90 	}
91 	openlog("comsat", LOG_PID, LOG_DAEMON);
92 	if (chdir(_PATH_MAILDIR)) {
93 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
94 		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
95 		exit(1);
96 	}
97 	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) == -1) {
98 		syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
99 		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
100 		exit(1);
101 	}
102 	(void)time(&lastmsgtime);
103 	(void)gethostname(hostname, sizeof(hostname));
104 	doreadutmp();
105 
106 	(void)signal(SIGTTOU, SIG_IGN);
107 
108 	bzero(&sa, sizeof sa);
109 	sigemptyset(&sa.sa_mask);
110 	sa.sa_handler = readutmp;
111 	sa.sa_flags = 0;			/* no SA_RESTART */
112 	(void)sigaction(SIGALRM, &sa, NULL);
113 
114 	sa.sa_handler = reapchildren;
115 	sa.sa_flags = SA_RESTART;
116 	(void)sigaction(SIGCHLD, &sa, NULL);
117 
118 	for (;;) {
119 		if (wantreadutmp) {
120 			wantreadutmp = 0;
121 			doreadutmp();
122 		}
123 
124 		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
125 		if (cc <= 0) {
126 			if (errno != EINTR)
127 				sleep(1);
128 			continue;
129 		}
130 		if (!nutmp)		/* no one has logged in yet */
131 			continue;
132 		sigemptyset(&sigset);
133 		sigaddset(&sigset, SIGALRM);
134 		sigprocmask(SIG_SETMASK, &sigset, NULL);
135 		msgbuf[cc] = '\0';
136 		(void)time(&lastmsgtime);
137 		mailfor(msgbuf);
138 		sigemptyset(&sigset);
139 		sigprocmask(SIG_SETMASK, &sigset, NULL);
140 	}
141 }
142 
143 /* ARGSUSED */
144 void
145 reapchildren(int signo)
146 {
147 	int save_errno = errno;
148 
149 	while (wait3(NULL, WNOHANG, NULL) > 0)
150 		;
151 	errno = save_errno;
152 }
153 
154 /* ARGSUSED */
155 void
156 readutmp(int signo)
157 {
158 	wantreadutmp = 1;
159 }
160 
161 void
162 doreadutmp(void)
163 {
164 	static u_int utmpsize;		/* last malloced size for utmp */
165 	static time_t utmpmtime;	/* last modification time for utmp */
166 	struct stat statbf;
167 
168 	if (time(NULL) - lastmsgtime >= MAXIDLE)
169 		exit(0);
170 	(void)fstat(uf, &statbf);
171 	if (statbf.st_mtime > utmpmtime) {
172 		utmpmtime = statbf.st_mtime;
173 		/* avoid int overflow */
174 		if (statbf.st_size > INT_MAX - 10 * sizeof(struct utmp)) {
175 			syslog(LOG_ALERT, "utmp file excessively large");
176 			exit(1);
177 		}
178 		if (statbf.st_size > utmpsize) {
179 			u_int nutmpsize = statbf.st_size + 10 *
180 			    sizeof(struct utmp);
181 			struct utmp *u;
182 
183 			if ((u = realloc(utmp, nutmpsize)) == NULL) {
184 				free(utmp);
185 				syslog(LOG_ERR, "%s", strerror(errno));
186 				exit(1);
187 			}
188 			utmp = u;
189 			utmpsize = nutmpsize;
190 		}
191 		(void)lseek(uf, 0, SEEK_SET);
192 		nutmp = read(uf, utmp, statbf.st_size)/sizeof(struct utmp);
193 	}
194 	(void)alarm(15);
195 }
196 
197 void
198 mailfor(char *name)
199 {
200 	struct utmp *utp = &utmp[nutmp];
201 	char utname[UT_NAMESIZE+1];
202 	char *cp;
203 	off_t offset;
204 
205 	if (!(cp = strchr(name, '@')))
206 		return;
207 	*cp = '\0';
208 	offset = atoi(cp + 1);
209 	while (--utp >= utmp) {
210 		memcpy(utname, utp->ut_name, UT_NAMESIZE);
211 		utname[UT_NAMESIZE] = '\0';
212 		if (!strncmp(utname, name, UT_NAMESIZE))
213 			notify(utp, offset);
214 	}
215 }
216 
217 static char *cr;
218 
219 void
220 notify(struct utmp *utp, off_t offset)
221 {
222 	FILE *tp;
223 	struct stat stb;
224 	struct termios ttybuf;
225 	char tty[MAXPATHLEN], name[UT_NAMESIZE + 1];
226 
227 	(void)snprintf(tty, sizeof(tty), "%s%.*s",
228 	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
229 	if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
230 		/* A slash is an attempt to break security... */
231 		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
232 		return;
233 	}
234 	if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
235 		dsyslog(LOG_DEBUG, "%.*s: wrong mode on %s",
236 		    (int)sizeof(utp->ut_name), utp->ut_name, tty);
237 		return;
238 	}
239 	dsyslog(LOG_DEBUG, "notify %.*s on %s", (int)sizeof(utp->ut_name),
240 	    utp->ut_name, tty);
241 	if (fork())
242 		return;
243 	(void)signal(SIGALRM, SIG_DFL);
244 	(void)alarm(30);
245 	if ((tp = fopen(tty, "w")) == NULL) {
246 		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
247 		_exit(1);
248 	}
249 	(void)tcgetattr(fileno(tp), &ttybuf);
250 	cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
251 	    "\n" : "\n\r";
252 	memcpy(name, utp->ut_name, UT_NAMESIZE);
253 	name[UT_NAMESIZE] = '\0';
254 	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
255 	    cr, name, (int)sizeof(hostname), hostname, cr, cr);
256 	jkfprintf(tp, name, offset);
257 	(void)fclose(tp);
258 	_exit(0);
259 }
260 
261 void
262 jkfprintf(FILE *tp, char name[], off_t offset)
263 {
264 	char *cp, ch;
265 	char visout[5], *s2;
266 	FILE *fi;
267 	int linecnt, charcnt, inheader;
268 	struct passwd *p;
269 	char line[BUFSIZ];
270 
271 	/* Set effective uid to user in case mail drop is on nfs */
272 	if ((p = getpwnam(name)) != NULL) {
273 		(void) seteuid(p->pw_uid);
274 		(void) setuid(p->pw_uid);
275 	}
276 
277 	if ((fi = fopen(name, "r")) == NULL)
278 		return;
279 
280 	(void)fseeko(fi, offset, SEEK_SET);
281 	/*
282 	 * Print the first 7 lines or 560 characters of the new mail
283 	 * (whichever comes first).  Skip header crap other than
284 	 * From, Subject, To, and Date.
285 	 */
286 	linecnt = 7;
287 	charcnt = 560;
288 	inheader = 1;
289 	while (fgets(line, sizeof(line), fi) != NULL) {
290 		if (inheader) {
291 			if (line[0] == '\n') {
292 				inheader = 0;
293 				continue;
294 			}
295 			if (line[0] == ' ' || line[0] == '\t' ||
296 			    (strncmp(line, "From:", 5) &&
297 			    strncmp(line, "Subject:", 8)))
298 				continue;
299 		}
300 		if (linecnt <= 0 || charcnt <= 0) {
301 			(void)fprintf(tp, "...more...%s", cr);
302 			(void)fclose(fi);
303 			return;
304 		}
305 		/* strip weird stuff so can't trojan horse stupid terminals */
306 		for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
307 			ch = toascii(ch);
308 			vis(visout, ch, VIS_SAFE|VIS_NOSLASH, cp[1]);
309 			for (s2 = visout; *s2; s2++)
310 				(void)fputc(*s2, tp);
311 		}
312 		(void)fputs(cr, tp);
313 		--linecnt;
314 	}
315 	(void)fprintf(tp, "----%s\n", cr);
316 	(void)fclose(fi);
317 }
318