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