comsat.c revision 3618
156893Sfenner/*
2127668Sbms * Copyright (c) 1980, 1993
3127668Sbms *	The Regents of the University of California.  All rights reserved.
456893Sfenner *
556893Sfenner * Redistribution and use in source and binary forms, with or without
656893Sfenner * modification, are permitted provided that the following conditions
756893Sfenner * are met:
856893Sfenner * 1. Redistributions of source code must retain the above copyright
956893Sfenner *    notice, this list of conditions and the following disclaimer.
1056893Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1156893Sfenner *    notice, this list of conditions and the following disclaimer in the
1256893Sfenner *    documentation and/or other materials provided with the distribution.
1356893Sfenner * 3. All advertising materials mentioning features or use of this software
1456893Sfenner *    must display the following acknowledgement:
15127668Sbms *	This product includes software developed by the University of
1656893Sfenner *	California, Berkeley and its contributors.
1756893Sfenner * 4. Neither the name of the University nor the names of its contributors
1856893Sfenner *    may be used to endorse or promote products derived from this software
1956893Sfenner *    without specific prior written permission.
20127668Sbms *
2156893Sfenner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2256893Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2356893Sfenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24127668Sbms * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25190207Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2656893Sfenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2756893Sfenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2856893Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2956893Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3056893Sfenner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3156893Sfenner * SUCH DAMAGE.
32127668Sbms */
3356893Sfenner
3456893Sfenner#ifndef lint
3556893Sfennerstatic char copyright[] =
3656893Sfenner"@(#) Copyright (c) 1980, 1993\n\
3756893Sfenner	The Regents of the University of California.  All rights reserved.\n";
3856893Sfenner#endif /* not lint */
39127668Sbms
4075115Sfenner#ifndef lint
4156893Sfennerstatic char sccsid[] = "@(#)comsat.c	8.1 (Berkeley) 6/4/93";
4256893Sfenner#endif /* not lint */
43127668Sbms
44127668Sbms#include <sys/param.h>
45127668Sbms#include <sys/socket.h>
46127668Sbms#include <sys/stat.h>
47127668Sbms#include <sys/file.h>
48127668Sbms#include <sys/wait.h>
49127668Sbms
50127668Sbms#include <netinet/in.h>
51127668Sbms
52127668Sbms#include <ctype.h>
53127668Sbms#include <errno.h>
54127668Sbms#include <netdb.h>
55127668Sbms#include <paths.h>
56127668Sbms#include <pwd.h>
57127668Sbms#include <sgtty.h>
58127668Sbms#include <signal.h>
59127668Sbms#include <stdio.h>
60127668Sbms#include <stdlib.h>
61127668Sbms#include <string.h>
62127668Sbms#include <syslog.h>
63214478Srpaulo#include <unistd.h>
64236192Sdelphij#include <utmp.h>
6556893Sfenner
66236192Sdelphijint	debug = 0;
6756893Sfenner#define	dsyslog	if (debug) syslog
6856893Sfenner
6956893Sfenner#define MAXIDLE	120
70127668Sbms
71127668Sbmschar	hostname[MAXHOSTNAMELEN];
72146773Ssamstruct	utmp *utmp = NULL;
7356893Sfennertime_t	lastmsgtime;
74127668Sbmsint	nutmp, uf;
75127668Sbms
7656893Sfennervoid jkfprintf __P((FILE *, char[], off_t));
7756893Sfennervoid mailfor __P((char *));
78127668Sbmsvoid notify __P((struct utmp *, char[], off_t, int));
7956893Sfennervoid onalrm __P((int));
80127668Sbmsvoid reapchildren __P((int));
8156893Sfenner
82127668Sbmsint
83127668Sbmsmain(argc, argv)
84127668Sbms	int argc;
85127668Sbms	char *argv[];
8656893Sfenner{
87127668Sbms	struct sockaddr_in from;
88127668Sbms	register int cc;
89127668Sbms	int fromlen;
90127668Sbms	char msgbuf[256];
91127668Sbms
92127668Sbms	/* verify proper invocation */
93127668Sbms	fromlen = sizeof(from);
94127668Sbms	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
95127668Sbms		(void)fprintf(stderr,
96127668Sbms		    "comsat: getsockname: %s.\n", strerror(errno));
97127668Sbms		exit(1);
9856893Sfenner	}
99214478Srpaulo	openlog("comsat", LOG_PID, LOG_DAEMON);
10056893Sfenner	if (chdir(_PATH_MAILDIR)) {
101214478Srpaulo		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
102214478Srpaulo		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
103214478Srpaulo		exit(1);
10456893Sfenner	}
10556893Sfenner	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
106214478Srpaulo		syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
107214478Srpaulo		(void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
10856893Sfenner		exit(1);
109236192Sdelphij	}
11056893Sfenner	(void)time(&lastmsgtime);
111127668Sbms	(void)gethostname(hostname, sizeof(hostname));
112127668Sbms	onalrm(0);
113127668Sbms	(void)signal(SIGALRM, onalrm);
114127668Sbms	(void)signal(SIGTTOU, SIG_IGN);
115127668Sbms	(void)signal(SIGCHLD, reapchildren);
116127668Sbms	for (;;) {
117127668Sbms		cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
118127668Sbms		if (cc <= 0) {
119			if (errno != EINTR)
120				sleep(1);
121			errno = 0;
122			continue;
123		}
124		if (!nutmp)		/* no one has logged in yet */
125			continue;
126		sigblock(sigmask(SIGALRM));
127		msgbuf[cc] = '\0';
128		(void)time(&lastmsgtime);
129		mailfor(msgbuf);
130		sigsetmask(0L);
131	}
132}
133
134void
135reapchildren(signo)
136	int signo;
137{
138	while (wait3(NULL, WNOHANG, NULL) > 0);
139}
140
141void
142onalrm(signo)
143	int signo;
144{
145	static u_int utmpsize;		/* last malloced size for utmp */
146	static u_int utmpmtime;		/* last modification time for utmp */
147	struct stat statbf;
148
149	if (time(NULL) - lastmsgtime >= MAXIDLE)
150		exit(0);
151	(void)alarm((u_int)15);
152	(void)fstat(uf, &statbf);
153	if (statbf.st_mtime > utmpmtime) {
154		utmpmtime = statbf.st_mtime;
155		if (statbf.st_size > utmpsize) {
156			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
157			if ((utmp = realloc(utmp, utmpsize)) == NULL) {
158				syslog(LOG_ERR, "%s", strerror(errno));
159				exit(1);
160			}
161		}
162		(void)lseek(uf, (off_t)0, L_SET);
163		nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
164	}
165}
166
167void
168mailfor(name)
169	char *name;
170{
171	register struct utmp *utp = &utmp[nutmp];
172	register char *cp;
173	char *file;
174	off_t offset;
175	int folder;
176	char buf[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
177	char buf2[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
178
179	if (!(cp = strchr(name, '@')))
180		return;
181	*cp = '\0';
182	offset = atoi(cp + 1);
183	if (!(cp = strchr(cp + 1, ':')))
184		file = name;
185	else
186		file = cp + 1;
187	sprintf(buf, "%s/%.*s", _PATH_MAILDIR, sizeof(utmp[0].ut_name), name);
188	if (*file != '/') {
189		sprintf(buf2, "%s/%.*s", _PATH_MAILDIR, sizeof(utmp[0].ut_name), file);
190		file = buf2;
191	}
192	folder = strcmp(buf, file);
193	while (--utp >= utmp)
194		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
195			notify(utp, file, offset, folder);
196}
197
198static char *cr;
199
200void
201notify(utp, file, offset, folder)
202	register struct utmp *utp;
203	char file[];
204	off_t offset;
205	int folder;
206{
207	FILE *tp;
208	struct stat stb;
209	struct sgttyb gttybuf;
210	char tty[20], name[sizeof(utmp[0].ut_name) + 1];
211
212	(void)snprintf(tty, sizeof(tty), "%s%.*s",
213	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
214	if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
215		/* A slash is an attempt to break security... */
216		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
217		return;
218	}
219	if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
220		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
221		return;
222	}
223	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
224	if (fork())
225		return;
226	(void)signal(SIGALRM, SIG_DFL);
227	(void)alarm((u_int)30);
228	if ((tp = fopen(tty, "w")) == NULL) {
229		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
230		_exit(-1);
231	}
232	(void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
233	cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ?
234	    "\n" : "\n\r";
235	(void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
236	name[sizeof(name) - 1] = '\0';
237	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
238	    cr, name, (int)sizeof(hostname), hostname,
239	    folder ? cr : "", folder ? "to " : "", folder ? file : "",
240	    cr, cr);
241	jkfprintf(tp, file, offset);
242	(void)fclose(tp);
243	_exit(0);
244}
245
246void
247jkfprintf(tp, name, offset)
248	register FILE *tp;
249	char name[];
250	off_t offset;
251{
252	register char *cp, ch;
253	register FILE *fi;
254	register int linecnt, charcnt, inheader;
255	register struct passwd *p;
256	char line[BUFSIZ];
257
258	/* Set effective uid to user in case mail drop is on nfs */
259	if ((p = getpwnam(name)) != NULL)
260		(void) setuid(p->pw_uid);
261
262	if ((fi = fopen(name, "r")) == NULL)
263		return;
264
265	(void)fseek(fi, offset, L_SET);
266	/*
267	 * Print the first 7 lines or 560 characters of the new mail
268	 * (whichever comes first).  Skip header crap other than
269	 * From, Subject, To, and Date.
270	 */
271	linecnt = 7;
272	charcnt = 560;
273	inheader = 1;
274	while (fgets(line, sizeof(line), fi) != NULL) {
275		if (inheader) {
276			if (line[0] == '\n') {
277				inheader = 0;
278				continue;
279			}
280			if (line[0] == ' ' || line[0] == '\t' ||
281			    strncmp(line, "From:", 5) &&
282			    strncmp(line, "Subject:", 8))
283				continue;
284		}
285		if (linecnt <= 0 || charcnt <= 0) {
286			(void)fprintf(tp, "...more...%s", cr);
287			(void)fclose(fi);
288			return;
289		}
290		/* strip weird stuff so can't trojan horse stupid terminals */
291		for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
292			if (!isprint(ch)) {
293				if (ch & 0x80)
294					(void)fputs("M-", tp);
295				ch &= 0177;
296				if (!isprint(ch)) {
297					if (ch == 0177)
298						ch = '?';
299					else
300				ch |= 0x40;
301					(void)fputc('^', tp);
302				}
303			}
304			(void)fputc(ch, tp);
305		}
306		(void)fputs(cr, tp);
307		--linecnt;
308	}
309	(void)fprintf(tp, "----%s\n", cr);
310	(void)fclose(fi);
311}
312