xref: /netbsd-src/libexec/comsat/comsat.c (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * 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 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)comsat.c	5.24 (Berkeley) 2/25/91";*/
42 static char rcsid[] = "$Id: comsat.c,v 1.2 1993/08/01 18:31:05 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/file.h>
49 #include <sys/wait.h>
50 
51 #include <netinet/in.h>
52 
53 #include <stdio.h>
54 #include <sgtty.h>
55 #include <utmp.h>
56 #include <signal.h>
57 #include <errno.h>
58 #include <netdb.h>
59 #include <syslog.h>
60 #include <ctype.h>
61 #include <string.h>
62 #include <paths.h>
63 
64 int	debug = 0;
65 #define	dsyslog	if (debug) syslog
66 
67 #define MAXIDLE	120
68 
69 char	hostname[MAXHOSTNAMELEN];
70 struct	utmp *utmp = NULL;
71 time_t	lastmsgtime, time();
72 int	nutmp, uf;
73 
74 /* ARGSUSED */
75 main(argc, argv)
76 	int argc;
77 	char **argv;
78 {
79 	extern int errno;
80 	register int cc;
81 	char msgbuf[100];
82 	struct sockaddr_in from;
83 	int fromlen;
84 	void onalrm(), reapchildren();
85 
86 	/* verify proper invocation */
87 	fromlen = sizeof(from);
88 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
89 		(void)fprintf(stderr,
90 		    "comsat: getsockname: %s.\n", strerror(errno));
91 		exit(1);
92 	}
93 	openlog("comsat", LOG_PID, LOG_DAEMON);
94 	if (chdir(_PATH_MAILDIR)) {
95 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
96 		exit(1);
97 	}
98 	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
99 		syslog(LOG_ERR, ".main: %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();
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()
129 {
130 	while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
131 }
132 
133 void
134 onalrm()
135 {
136 	static u_int utmpsize;		/* last malloced size for utmp */
137 	static u_int utmpmtime;		/* last modification time for utmp */
138 	struct stat statbf;
139 	off_t lseek();
140 	char *malloc(), *realloc();
141 
142 	if (time((time_t *)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)
151 				utmp = (struct utmp *)realloc((char *)utmp, utmpsize);
152 			else
153 				utmp = (struct utmp *)malloc(utmpsize);
154 			if (!utmp) {
155 				syslog(LOG_ERR, "malloc failed");
156 				exit(1);
157 			}
158 		}
159 		(void)lseek(uf, 0L, L_SET);
160 		nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
161 	}
162 }
163 
164 mailfor(name)
165 	char *name;
166 {
167 	register struct utmp *utp = &utmp[nutmp];
168 	register char *cp;
169 	off_t offset;
170 
171 	if (!(cp = index(name, '@')))
172 		return;
173 	*cp = '\0';
174 	offset = atoi(cp + 1);
175 	while (--utp >= utmp)
176 		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
177 			notify(utp, offset);
178 }
179 
180 static char *cr;
181 
182 notify(utp, offset)
183 	register struct utmp *utp;
184 	off_t offset;
185 {
186 	static char tty[20] = _PATH_DEV;
187 	struct sgttyb gttybuf;
188 	struct stat stb;
189 	FILE *tp;
190 	char name[sizeof(utmp[0].ut_name) + 1];
191 
192 	(void)strncpy(tty + sizeof(_PATH_DEV) - 1, utp->ut_line,
193 	    sizeof(utp->ut_line));
194 	if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
195 		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
196 		return;
197 	}
198 	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
199 	if (fork())
200 		return;
201 	(void)signal(SIGALRM, SIG_DFL);
202 	(void)alarm((u_int)30);
203 	if ((tp = fopen(tty, "w")) == NULL) {
204 		dsyslog(LOG_ERR, "fopen of tty %s failed", tty);
205 		_exit(-1);
206 	}
207 	(void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
208 	cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ?
209 	    "\n" : "\n\r";
210 	(void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
211 	name[sizeof(name) - 1] = '\0';
212 	(void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
213 	    cr, name, sizeof(hostname), hostname, cr, cr);
214 	jkfprintf(tp, name, offset);
215 	(void)fclose(tp);
216 	_exit(0);
217 }
218 
219 jkfprintf(tp, name, offset)
220 	register FILE *tp;
221 	char name[];
222 	off_t offset;
223 {
224 	register char *cp, ch;
225 	register FILE *fi;
226 	register int linecnt, charcnt, inheader;
227 	char line[BUFSIZ];
228 
229 	if ((fi = fopen(name, "r")) == NULL)
230 		return;
231 	(void)fseek(fi, offset, L_SET);
232 	/*
233 	 * Print the first 7 lines or 560 characters of the new mail
234 	 * (whichever comes first).  Skip header crap other than
235 	 * From, Subject, To, and Date.
236 	 */
237 	linecnt = 7;
238 	charcnt = 560;
239 	inheader = 1;
240 	while (fgets(line, sizeof(line), fi) != NULL) {
241 		if (inheader) {
242 			if (line[0] == '\n') {
243 				inheader = 0;
244 				continue;
245 			}
246 			if (line[0] == ' ' || line[0] == '\t' ||
247 			    strncmp(line, "From:", 5) &&
248 			    strncmp(line, "Subject:", 8))
249 				continue;
250 		}
251 		if (linecnt <= 0 || charcnt <= 0) {
252 			(void)fprintf(tp, "...more...%s", cr);
253 			return;
254 		}
255 		/* strip weird stuff so can't trojan horse stupid terminals */
256 		for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
257 			ch = toascii(ch);
258 			if (!isprint(ch) && !isspace(ch))
259 				ch |= 0x40;
260 			(void)fputc(ch, tp);
261 		}
262 		(void)fputs(cr, tp);
263 		--linecnt;
264 	}
265 	(void)fprintf(tp, "----%s\n", cr);
266 }
267