xref: /dragonfly/libexec/comsat/comsat.c (revision b40e316c)
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  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)comsat.c	8.1 (Berkeley) 6/4/93
35  * $FreeBSD: src/libexec/comsat/comsat.c,v 1.13.2.1 2002/08/09 02:56:30 johan Exp $
36  * $DragonFly: src/libexec/comsat/comsat.c,v 1.3 2003/11/14 03:54:29 dillon Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/file.h>
43 #include <sys/wait.h>
44 
45 #include <netinet/in.h>
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <netdb.h>
51 #include <paths.h>
52 #include <pwd.h>
53 #include <termios.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <syslog.h>
59 #include <unistd.h>
60 #include <utmp.h>
61 
62 int	debug = 0;
63 #define	dsyslog	if (debug) syslog
64 
65 #define MAXIDLE	120
66 
67 char	hostname[MAXHOSTNAMELEN];
68 struct	utmp *utmp = NULL;
69 time_t	lastmsgtime;
70 int	nutmp, uf;
71 
72 void jkfprintf (FILE *, char[], char[], off_t);
73 void mailfor (char *);
74 void notify (struct utmp *, char[], off_t, int);
75 void onalrm (int);
76 void reapchildren (int);
77 
78 int
79 main(argc, argv)
80 	int argc;
81 	char *argv[];
82 {
83 	struct sockaddr_in from;
84 	register int cc;
85 	int fromlen;
86 	char msgbuf[256];
87 
88 	/* verify proper invocation */
89 	fromlen = sizeof(from);
90 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
91 		err(1, "getsockname");
92 	openlog("comsat", LOG_PID, LOG_DAEMON);
93 	if (chdir(_PATH_MAILDIR)) {
94 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
95 		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
96 		exit(1);
97 	}
98 	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
99 		syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
100 		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
101 		exit(1);
102 	}
103 	(void)time(&lastmsgtime);
104 	(void)gethostname(hostname, sizeof(hostname));
105 	onalrm(0);
106 	(void)signal(SIGALRM, onalrm);
107 	(void)signal(SIGTTOU, SIG_IGN);
108 	(void)signal(SIGCHLD, reapchildren);
109 	for (;;) {
110 		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
111 		if (cc <= 0) {
112 			if (errno != EINTR)
113 				sleep(1);
114 			errno = 0;
115 			continue;
116 		}
117 		if (!nutmp)		/* no one has logged in yet */
118 			continue;
119 		sigblock(sigmask(SIGALRM));
120 		msgbuf[cc] = '\0';
121 		(void)time(&lastmsgtime);
122 		mailfor(msgbuf);
123 		sigsetmask(0L);
124 	}
125 }
126 
127 void
128 reapchildren(signo)
129 	int signo;
130 {
131 	while (wait3(NULL, WNOHANG, NULL) > 0);
132 }
133 
134 void
135 onalrm(signo)
136 	int signo;
137 {
138 	static u_int utmpsize;		/* last malloced size for utmp */
139 	static u_int utmpmtime;		/* last modification time for utmp */
140 	struct stat statbf;
141 
142 	if (time(NULL) - lastmsgtime >= MAXIDLE)
143 		exit(0);
144 	(void)alarm((u_int)15);
145 	(void)fstat(uf, &statbf);
146 	if (statbf.st_mtime > utmpmtime) {
147 		utmpmtime = statbf.st_mtime;
148 		if (statbf.st_size > utmpsize) {
149 			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
150 			if ((utmp = realloc(utmp, utmpsize)) == NULL) {
151 				syslog(LOG_ERR, "%s", strerror(errno));
152 				exit(1);
153 			}
154 		}
155 		(void)lseek(uf, (off_t)0, L_SET);
156 		nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
157 	}
158 }
159 
160 void
161 mailfor(name)
162 	char *name;
163 {
164 	register struct utmp *utp = &utmp[nutmp];
165 	register char *cp;
166 	char *file;
167 	off_t offset;
168 	int folder;
169 	char buf[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
170 	char buf2[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
171 
172 	if (!(cp = strchr(name, '@')))
173 		return;
174 	*cp = '\0';
175 	offset = atoi(cp + 1);
176 	if (!(cp = strchr(cp + 1, ':')))
177 		file = name;
178 	else
179 		file = cp + 1;
180 	sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utmp[0].ut_name),
181 	    name);
182 	if (*file != '/') {
183 		sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
184 		    (int)sizeof(utmp[0].ut_name), file);
185 		file = buf2;
186 	}
187 	folder = strcmp(buf, file);
188 	while (--utp >= utmp)
189 		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
190 			notify(utp, file, offset, folder);
191 }
192 
193 static char *cr;
194 
195 void
196 notify(utp, file, offset, folder)
197 	register struct utmp *utp;
198 	char file[];
199 	off_t offset;
200 	int folder;
201 {
202 	FILE *tp;
203 	struct stat stb;
204 	struct termios tio;
205 	char tty[20], name[sizeof(utmp[0].ut_name) + 1];
206 
207 	(void)snprintf(tty, sizeof(tty), "%s%.*s",
208 	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
209 	if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
210 		/* A slash is an attempt to break security... */
211 		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
212 		return;
213 	}
214 	if (stat(tty, &stb) || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
215 		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
216 		return;
217 	}
218 	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
219 	if (fork())
220 		return;
221 	(void)signal(SIGALRM, SIG_DFL);
222 	(void)alarm((u_int)30);
223 	if ((tp = fopen(tty, "w")) == NULL) {
224 		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
225 		_exit(1);
226 	}
227 	(void)tcgetattr(fileno(tp), &tio);
228 	cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ?  "\n" : "\n\r";
229 	(void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
230 	name[sizeof(name) - 1] = '\0';
231 	switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
232 	case S_IXUSR:
233 	case (S_IXUSR | S_IXGRP):
234 		(void)fprintf(tp,
235 		    "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
236 		    cr, name, (int)sizeof(hostname), hostname,
237 		    folder ? cr : "", folder ? "to " : "", folder ? file : "",
238 		    cr, cr);
239 		jkfprintf(tp, name, file, offset);
240 		break;
241 	case S_IXGRP:
242 		(void)fprintf(tp, "\007");
243 		(void)fflush(tp);
244 		(void)sleep(1);
245 		(void)fprintf(tp, "\007");
246 		break;
247 	default:
248 		break;
249 	}
250 	(void)fclose(tp);
251 	_exit(0);
252 }
253 
254 void
255 jkfprintf(tp, user, file, offset)
256 	register FILE *tp;
257 	char user[];
258 	char file[];
259 	off_t offset;
260 {
261 	register unsigned char *cp, ch;
262 	register FILE *fi;
263 	register int linecnt, charcnt, inheader;
264 	register struct passwd *p;
265 	unsigned char line[BUFSIZ];
266 
267 	/* Set effective uid to user in case mail drop is on nfs */
268 	if ((p = getpwnam(user)) != NULL)
269 		(void) setuid(p->pw_uid);
270 
271 	if ((fi = fopen(file, "r")) == NULL)
272 		return;
273 
274 	(void)fseek(fi, offset, L_SET);
275 	/*
276 	 * Print the first 7 lines or 560 characters of the new mail
277 	 * (whichever comes first).  Skip header crap other than
278 	 * From, Subject, To, and Date.
279 	 */
280 	linecnt = 7;
281 	charcnt = 560;
282 	inheader = 1;
283 	while (fgets(line, sizeof(line), fi) != NULL) {
284 		if (inheader) {
285 			if (line[0] == '\n') {
286 				inheader = 0;
287 				continue;
288 			}
289 			if (line[0] == ' ' || line[0] == '\t' ||
290 			    (strncmp(line, "From:", 5) &&
291 			    strncmp(line, "Subject:", 8)))
292 				continue;
293 		}
294 		if (linecnt <= 0 || charcnt <= 0) {
295 			(void)fprintf(tp, "...more...%s", cr);
296 			(void)fclose(fi);
297 			return;
298 		}
299 		/* strip weird stuff so can't trojan horse stupid terminals */
300 		for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
301 			/* disable upper controls and enable all other
302 			   8bit codes due to lack of locale knowledge
303 			 */
304 			if (((ch & 0x80) && ch < 0xA0) ||
305 			    (!(ch & 0x80) && !isprint(ch) &&
306 			     !isspace(ch) && ch != '\a' && ch != '\b')
307 			   ) {
308 				if (ch & 0x80) {
309 					ch &= ~0x80;
310 					(void)fputs("M-", tp);
311 				}
312 				if (iscntrl(ch)) {
313 					ch ^= 0x40;
314 					(void)fputc('^', tp);
315 				}
316 			}
317 			(void)fputc(ch, tp);
318 		}
319 		(void)fputs(cr, tp);
320 		--linecnt;
321 	}
322 	(void)fprintf(tp, "----%s\n", cr);
323 	(void)fclose(fi);
324 }
325