xref: /netbsd-src/libexec/comsat/comsat.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: comsat.c,v 1.14 1998/07/06 06:47:38 mrg 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #if 0
41 static char sccsid[] = "from: @(#)comsat.c	8.1 (Berkeley) 6/4/93";
42 #else
43 __RCSID("$NetBSD: comsat.c,v 1.14 1998/07/06 06:47:38 mrg Exp $");
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <sys/stat.h>
50 #include <sys/file.h>
51 #include <sys/wait.h>
52 
53 #include <netinet/in.h>
54 
55 #include <ctype.h>
56 #include <errno.h>
57 #include <netdb.h>
58 #include <paths.h>
59 #include <pwd.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <syslog.h>
65 #include <termios.h>
66 #include <time.h>
67 #include <vis.h>
68 #include <unistd.h>
69 #include <utmp.h>
70 
71 int	logging;
72 int	debug = 0;
73 #define	dsyslog	if (debug) syslog
74 
75 #define MAXIDLE	120
76 
77 char	hostname[MAXHOSTNAMELEN+1];
78 struct	utmp *utmp = NULL;
79 time_t	lastmsgtime;
80 int	nutmp, uf;
81 
82 void jkfprintf __P((FILE *, char[], off_t));
83 void mailfor __P((char *));
84 void notify __P((struct utmp *, off_t));
85 void onalrm __P((int));
86 void reapchildren __P((int));
87 int main __P((int, char *[]));
88 
89 int
90 main(argc, argv)
91 	int argc;
92 	char *argv[];
93 {
94 	struct sockaddr_in from;
95 	int cc, ch;
96 	int fromlen;
97 	char msgbuf[100];
98 	sigset_t sigset;
99 	extern char *__progname;
100 
101 	/* verify proper invocation */
102 	fromlen = sizeof(from);
103 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
104 		(void)fprintf(stderr,
105 		    "comsat: getsockname: %s.\n", strerror(errno));
106 		exit(1);
107 	}
108 
109 	openlog("comsat", LOG_PID, LOG_DAEMON);
110 	while ((ch = getopt(argc, argv, "l")) != -1)
111 		switch (ch) {
112 		case 'l':
113 			logging = 1;
114 			break;
115 		default:
116 			syslog(LOG_ERR, "usage: %s [-l]", __progname);
117 			exit(1);
118 		}
119 	if (chdir(_PATH_MAILDIR)) {
120 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
121 		(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
122 		exit(1);
123 	}
124 	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
125 		syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
126 		(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
127 		exit(1);
128 	}
129 	(void)time(&lastmsgtime);
130 	(void)gethostname(hostname, sizeof(hostname));
131 	hostname[sizeof(hostname) - 1] = '\0';
132 	onalrm(0);
133 	(void)signal(SIGALRM, onalrm);
134 	(void)signal(SIGTTOU, SIG_IGN);
135 	(void)signal(SIGCHLD, reapchildren);
136 	for (;;) {
137 		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
138 		if (cc <= 0) {
139 			if (errno != EINTR)
140 				sleep(1);
141 			errno = 0;
142 			continue;
143 		}
144 		if (!nutmp)		/* no one has logged in yet */
145 			continue;
146 		sigemptyset(&sigset);
147 		sigaddset(&sigset, SIGALRM);
148 		sigprocmask(SIG_SETMASK, &sigset, NULL);
149 		msgbuf[cc] = '\0';
150 		(void)time(&lastmsgtime);
151 		mailfor(msgbuf);
152 		sigemptyset(&sigset);
153 		sigprocmask(SIG_SETMASK, &sigset, NULL);
154 	}
155 }
156 
157 void
158 reapchildren(signo)
159 	int signo;
160 {
161 
162 	while (wait3(NULL, WNOHANG, NULL) > 0);
163 }
164 
165 void
166 onalrm(signo)
167 	int signo;
168 {
169 	static u_int utmpsize;		/* last malloced size for utmp */
170 	static u_int utmpmtime;		/* last modification time for utmp */
171 	struct stat statbf;
172 
173 	if (time(NULL) - lastmsgtime >= MAXIDLE)
174 		exit(0);
175 	(void)alarm((u_int)15);
176 	(void)fstat(uf, &statbf);
177 	if (statbf.st_mtime > utmpmtime) {
178 		utmpmtime = statbf.st_mtime;
179 		if (statbf.st_size > utmpsize) {
180 			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
181 			if ((utmp = realloc(utmp, utmpsize)) == NULL) {
182 				syslog(LOG_ERR, "%s", strerror(errno));
183 				exit(1);
184 			}
185 		}
186 		(void)lseek(uf, (off_t)0, SEEK_SET);
187 		nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
188 	}
189 }
190 
191 void
192 mailfor(name)
193 	char *name;
194 {
195 	struct utmp *utp = &utmp[nutmp];
196 	char *cp;
197 	off_t offset;
198 
199 	if (!(cp = strchr(name, '@')))
200 		return;
201 	*cp = '\0';
202 	offset = atoi(cp + 1);
203 	while (--utp >= utmp)
204 		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
205 			notify(utp, offset);
206 }
207 
208 static char *cr;
209 
210 void
211 notify(utp, offset)
212 	struct utmp *utp;
213 	off_t offset;
214 {
215 	FILE *tp;
216 	struct passwd *p;
217 	struct stat stb;
218 	struct termios ttybuf;
219 	char tty[20], name[sizeof(utmp[0].ut_name) + 1];
220 
221 	(void)snprintf(tty, sizeof(tty), "%s%.*s",
222 	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
223 	if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
224 		/* A slash is an attempt to break security... */
225 		/*
226 		 * XXX but what about something like "/dev/pts/5"
227 		 * that we may one day "support". ?
228 		 */
229 		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
230 		return;
231 	}
232 	if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
233 		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
234 		return;
235 	}
236 	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
237 	if (fork())
238 		return;
239 	(void)signal(SIGALRM, SIG_DFL);
240 	(void)alarm((u_int)30);
241 	if ((tp = fopen(tty, "w")) == NULL) {
242 		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
243 		_exit(-1);
244 	}
245 	(void)tcgetattr(fileno(tp), &ttybuf);
246 	cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
247 	    "\n" : "\n\r";
248 	(void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
249 	name[sizeof(name) - 1] = '\0';
250 
251 	/* Set uid/gid/groups to users in case mail drop is on nfs */
252 	if ((p = getpwnam(name)) == NULL ||
253 	    initgroups(p->pw_name, p->pw_gid) < 0 ||
254 	    setgid(p->pw_gid) < 0 ||
255 	    setuid(p->pw_uid) < 0)
256 		_exit(-1);
257 
258 	if (logging)
259 		syslog(LOG_INFO, "biff message for %s", name);
260 
261 	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
262 	    cr, name, (int)sizeof(hostname), hostname, cr, cr);
263 	jkfprintf(tp, name, offset);
264 	(void)fclose(tp);
265 	_exit(0);
266 }
267 
268 void
269 jkfprintf(tp, name, offset)
270 	FILE *tp;
271 	char name[];
272 	off_t offset;
273 {
274 	FILE *fi;
275 	int linecnt, charcnt, inheader;
276 	char line[BUFSIZ], visline[BUFSIZ*4];
277 
278 	if ((fi = fopen(name, "r")) == NULL)
279 		return;
280 
281 	(void)fseek(fi, offset, SEEK_SET);
282 	/*
283 	 * Print the first 7 lines or 560 characters of the new mail
284 	 * (whichever comes first).  Skip header crap other than
285 	 * From, Subject, To, and Date.
286 	 */
287 	linecnt = 7;
288 	charcnt = 560;
289 	inheader = 1;
290 	while (fgets(line, sizeof(line), fi) != NULL) {
291 		if (inheader) {
292 			if (line[0] == '\n') {
293 				inheader = 0;
294 				continue;
295 			}
296 			if (line[0] == ' ' || line[0] == '\t' ||
297 			    (strncasecmp(line, "From:", 5) &&
298 			    strncasecmp(line, "Subject:", 8)))
299 				continue;
300 		}
301 		if (linecnt <= 0 || charcnt <= 0) {
302 			(void)fprintf(tp, "...more...%s", cr);
303 			(void)fclose(fi);
304 			return;
305 		}
306 		/* strip weird stuff so can't trojan horse stupid terminals */
307 		(void)strvis(visline, line, VIS_CSTYLE);
308 		fputs(visline, tp);
309 		--linecnt;
310 	}
311 	(void)fprintf(tp, "----%s\n", cr);
312 	(void)fclose(fi);
313 }
314