xref: /netbsd-src/libexec/comsat/comsat.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: comsat.c,v 1.17 2000/10/04 18:52:50 mjl 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.17 2000/10/04 18:52:50 mjl 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 (FILE *, char[], off_t);
83 void mailfor (char *);
84 void notify (struct utmp *, off_t);
85 void onalrm (int);
86 void reapchildren (int);
87 
88 int
89 main(int argc, char *argv[])
90 {
91 	struct sockaddr_storage from;
92 	int cc, ch;
93 	int fromlen;
94 	char msgbuf[100];
95 	sigset_t sigset;
96 	extern char *__progname;
97 
98 	/* verify proper invocation */
99 	fromlen = sizeof(from);
100 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
101 		(void)fprintf(stderr,
102 		    "comsat: getsockname: %s.\n", strerror(errno));
103 		exit(1);
104 	}
105 
106 	openlog("comsat", LOG_PID, LOG_DAEMON);
107 	while ((ch = getopt(argc, argv, "l")) != -1)
108 		switch (ch) {
109 		case 'l':
110 			logging = 1;
111 			break;
112 		default:
113 			syslog(LOG_ERR, "usage: %s [-l]", __progname);
114 			exit(1);
115 		}
116 	if (chdir(_PATH_MAILDIR)) {
117 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
118 		(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
119 		exit(1);
120 	}
121 	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
122 		syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
123 		(void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
124 		exit(1);
125 	}
126 	(void)time(&lastmsgtime);
127 	(void)gethostname(hostname, sizeof(hostname));
128 	hostname[sizeof(hostname) - 1] = '\0';
129 	onalrm(0);
130 	(void)signal(SIGALRM, onalrm);
131 	(void)signal(SIGTTOU, SIG_IGN);
132 	(void)signal(SIGCHLD, reapchildren);
133 	for (;;) {
134 		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
135 		if (cc <= 0) {
136 			if (errno != EINTR)
137 				sleep(1);
138 			errno = 0;
139 			continue;
140 		}
141 		if (!nutmp)		/* no one has logged in yet */
142 			continue;
143 		sigemptyset(&sigset);
144 		sigaddset(&sigset, SIGALRM);
145 		sigprocmask(SIG_SETMASK, &sigset, NULL);
146 		msgbuf[cc] = '\0';
147 		(void)time(&lastmsgtime);
148 		mailfor(msgbuf);
149 		sigemptyset(&sigset);
150 		sigprocmask(SIG_SETMASK, &sigset, NULL);
151 	}
152 }
153 
154 void
155 reapchildren(int signo)
156 {
157 
158 	while (wait3(NULL, WNOHANG, NULL) > 0);
159 }
160 
161 void
162 onalrm(int signo)
163 {
164 	static u_int utmpsize;		/* last malloced size for utmp */
165 	static u_int utmpmtime;		/* last modification time for utmp */
166 	struct stat statbf;
167 
168 	if (time(NULL) - lastmsgtime >= MAXIDLE)
169 		exit(0);
170 	(void)alarm((u_int)15);
171 	(void)fstat(uf, &statbf);
172 	if (statbf.st_mtime > utmpmtime) {
173 		utmpmtime = statbf.st_mtime;
174 		if (statbf.st_size > utmpsize) {
175 			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
176 			if ((utmp = realloc(utmp, utmpsize)) == NULL) {
177 				syslog(LOG_ERR, "%s", strerror(errno));
178 				exit(1);
179 			}
180 		}
181 		(void)lseek(uf, (off_t)0, SEEK_SET);
182 		nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
183 	}
184 }
185 
186 void
187 mailfor(char *name)
188 {
189 	struct utmp *utp = &utmp[nutmp];
190 	char *cp;
191 	off_t offset;
192 
193 	if (!(cp = strchr(name, '@')))
194 		return;
195 	*cp = '\0';
196 	offset = atoi(cp + 1);
197 	while (--utp >= utmp)
198 		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
199 			notify(utp, offset);
200 }
201 
202 static char *cr;
203 
204 void
205 notify(struct utmp *utp, off_t offset)
206 {
207 	FILE *tp;
208 	struct passwd *p;
209 	struct stat stb;
210 	struct termios ttybuf;
211 	char tty[20], name[sizeof(utmp[0].ut_name) + 1];
212 
213 	(void)snprintf(tty, sizeof(tty), "%s%.*s",
214 	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
215 	if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
216 		/* A slash is an attempt to break security... */
217 		/*
218 		 * XXX but what about something like "/dev/pts/5"
219 		 * that we may one day "support". ?
220 		 */
221 		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
222 		return;
223 	}
224 	if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
225 		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
226 		return;
227 	}
228 	dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_name, tty);
229 	if (fork())
230 		return;
231 	(void)signal(SIGALRM, SIG_DFL);
232 	(void)alarm((u_int)30);
233 	if ((tp = fopen(tty, "w")) == NULL) {
234 		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
235 		_exit(1);
236 	}
237 	(void)tcgetattr(fileno(tp), &ttybuf);
238 	cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ?
239 	    "\n" : "\n\r";
240 	(void)strncpy(name, utp->ut_name, sizeof(name));
241 	name[sizeof(name) - 1] = '\0';
242 
243 	/* Set uid/gid/groups to users in case mail drop is on nfs */
244 	if ((p = getpwnam(name)) == NULL ||
245 	    initgroups(p->pw_name, p->pw_gid) < 0 ||
246 	    setgid(p->pw_gid) < 0 ||
247 	    setuid(p->pw_uid) < 0)
248 		_exit(1);
249 
250 	if (logging)
251 		syslog(LOG_INFO, "biff message for %s", name);
252 
253 	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
254 	    cr, name, (int)sizeof(hostname), hostname, cr, cr);
255 	jkfprintf(tp, name, offset);
256 	(void)fclose(tp);
257 	_exit(0);
258 }
259 
260 void
261 jkfprintf(FILE *tp, char name[], off_t offset)
262 {
263 	FILE *fi;
264 	int linecnt, charcnt, inheader;
265 	char line[BUFSIZ], visline[BUFSIZ*4];
266 
267 	if ((fi = fopen(name, "r")) == NULL)
268 		return;
269 
270 	(void)fseek(fi, offset, SEEK_SET);
271 	/*
272 	 * Print the first 7 lines or 560 characters of the new mail
273 	 * (whichever comes first).  Skip header crap other than
274 	 * From, Subject, To, and Date.
275 	 */
276 	linecnt = 7;
277 	charcnt = 560;
278 	inheader = 1;
279 	while (fgets(line, sizeof(line), fi) != NULL) {
280 		if (inheader) {
281 			if (line[0] == '\n') {
282 				inheader = 0;
283 				continue;
284 			}
285 			if (line[0] == ' ' || line[0] == '\t' ||
286 			    (strncasecmp(line, "From:", 5) &&
287 			    strncasecmp(line, "Subject:", 8)))
288 				continue;
289 		}
290 		if (linecnt <= 0 || charcnt <= 0) {
291 			(void)fprintf(tp, "...more...%s", cr);
292 			(void)fclose(fi);
293 			return;
294 		}
295 		/* strip weird stuff so can't trojan horse stupid terminals */
296 		(void)strvis(visline, line, VIS_CSTYLE);
297 		fputs(visline, tp);
298 		--linecnt;
299 	}
300 	(void)fprintf(tp, "----%s\n", cr);
301 	(void)fclose(fi);
302 }
303