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