]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - libexec/comsat/comsat.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / libexec / comsat / comsat.c
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 const 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 #if 0
42 static char sccsid[] = "@(#)comsat.c    8.1 (Berkeley) 6/4/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <sys/stat.h>
51 #include <sys/file.h>
52 #include <sys/wait.h>
53
54 #include <netinet/in.h>
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <netdb.h>
60 #include <paths.h>
61 #include <pwd.h>
62 #include <termios.h>
63 #include <signal.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <syslog.h>
68 #include <unistd.h>
69 #include <utmp.h>
70
71 int     debug = 0;
72 #define dsyslog if (debug) syslog
73
74 #define MAXIDLE 120
75
76 char    hostname[MAXHOSTNAMELEN];
77 struct  utmp *utmp = NULL;
78 time_t  lastmsgtime;
79 int     nutmp, uf;
80
81 void jkfprintf(FILE *, char[], char[], off_t);
82 void mailfor(char *);
83 void notify(struct utmp *, char[], off_t, int);
84 void onalrm(int);
85 void reapchildren(int);
86
87 int
88 main(int argc, char *argv[])
89 {
90         struct sockaddr_in from;
91         socklen_t fromlen;
92         int cc;
93         char msgbuf[256];
94
95         /* verify proper invocation */
96         fromlen = sizeof(from);
97         if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
98                 err(1, "getsockname");
99         openlog("comsat", LOG_PID, LOG_DAEMON);
100         if (chdir(_PATH_MAILDIR)) {
101                 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
102                 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
103                 exit(1);
104         }
105         if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
106                 syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
107                 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
108                 exit(1);
109         }
110         (void)time(&lastmsgtime);
111         (void)gethostname(hostname, sizeof(hostname));
112         onalrm(0);
113         (void)signal(SIGALRM, onalrm);
114         (void)signal(SIGTTOU, SIG_IGN);
115         (void)signal(SIGCHLD, reapchildren);
116         for (;;) {
117                 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
118                 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
134 void
135 reapchildren(int signo)
136 {
137         while (wait3(NULL, WNOHANG, NULL) > 0);
138 }
139
140 void
141 onalrm(int signo)
142 {
143         static u_int utmpsize;          /* last malloced size for utmp */
144         static u_int utmpmtime;         /* last modification time for utmp */
145         struct stat statbf;
146
147         if (time(NULL) - lastmsgtime >= MAXIDLE)
148                 exit(0);
149         (void)alarm((u_int)15);
150         (void)fstat(uf, &statbf);
151         if (statbf.st_mtime > utmpmtime) {
152                 utmpmtime = statbf.st_mtime;
153                 if (statbf.st_size > utmpsize) {
154                         utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
155                         if ((utmp = realloc(utmp, utmpsize)) == NULL) {
156                                 syslog(LOG_ERR, "%s", strerror(errno));
157                                 exit(1);
158                         }
159                 }
160                 (void)lseek(uf, (off_t)0, SEEK_SET);
161                 nutmp = read(uf, utmp, (size_t)statbf.st_size)/sizeof(struct utmp);
162         }
163 }
164
165 void
166 mailfor(char *name)
167 {
168         struct utmp *utp = &utmp[nutmp];
169         char *cp;
170         char *file;
171         off_t offset;
172         int folder;
173         char buf[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
174         char buf2[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
175
176         if (!(cp = strchr(name, '@')))
177                 return;
178         *cp = '\0';
179         offset = strtoll(cp + 1, NULL, 10);
180         if (!(cp = strchr(cp + 1, ':')))
181                 file = name;
182         else
183                 file = cp + 1;
184         sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utmp[0].ut_name),
185             name);
186         if (*file != '/') {
187                 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
188                     (int)sizeof(utmp[0].ut_name), file);
189                 file = buf2;
190         }
191         folder = strcmp(buf, file);
192         while (--utp >= utmp)
193                 if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
194                         notify(utp, file, offset, folder);
195 }
196
197 static char *cr;
198
199 void
200 notify(struct utmp *utp, char file[], off_t offset, int folder)
201 {
202         FILE *tp;
203         struct stat stb;
204         struct termios tio;
205         char tty[20], name[sizeof(utmp[0].ut_name) + 1];
206         const char *cr = utp->ut_line;
207
208         if (strncmp(cr, "pts/", 4) == 0)
209                 cr += 4;
210         if (strchr(cr, '/')) {
211                 /* A slash is an attempt to break security... */
212                 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
213                     utp->ut_line);
214                 return;
215         }
216         (void)snprintf(tty, sizeof(tty), "%s%.*s",
217             _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
218         if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
219                 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
220                 return;
221         }
222         dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
223         switch (fork()) {
224         case -1:
225                 syslog(LOG_NOTICE, "fork failed (%m)");
226                 return;
227         case 0:
228                 break;
229         default:
230                 return;
231         }
232         (void)signal(SIGALRM, SIG_DFL);
233         (void)alarm((u_int)30);
234         if ((tp = fopen(tty, "w")) == NULL) {
235                 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
236                 _exit(1);
237         }
238         (void)tcgetattr(fileno(tp), &tio);
239         cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ?  "\n" : "\n\r";
240         (void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
241         name[sizeof(name) - 1] = '\0';
242         switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
243         case S_IXUSR:
244         case (S_IXUSR | S_IXGRP):
245                 (void)fprintf(tp, 
246                     "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
247                     cr, name, (int)sizeof(hostname), hostname,
248                     folder ? cr : "", folder ? "to " : "", folder ? file : "",
249                     cr, cr);
250                 jkfprintf(tp, name, file, offset);
251                 break;
252         case S_IXGRP:
253                 (void)fprintf(tp, "\007");
254                 (void)fflush(tp);      
255                 (void)sleep(1);
256                 (void)fprintf(tp, "\007");
257                 break;
258         default:
259                 break;
260         }       
261         (void)fclose(tp);
262         _exit(0);
263 }
264
265 void
266 jkfprintf(FILE *tp, char user[], char file[], off_t offset)
267 {
268         unsigned char *cp, ch;
269         FILE *fi;
270         int linecnt, charcnt, inheader;
271         struct passwd *p;
272         unsigned char line[BUFSIZ];
273
274         /* Set effective uid to user in case mail drop is on nfs */
275         if ((p = getpwnam(user)) != NULL)
276                 (void) setuid(p->pw_uid);
277
278         if ((fi = fopen(file, "r")) == NULL)
279                 return;
280
281         (void)fseeko(fi, offset, SEEK_CUR);
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                             (strncmp(line, "From:", 5) &&
298                             strncmp(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                 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
308                         /* disable upper controls and enable all other
309                            8bit codes due to lack of locale knowledge
310                          */
311                         if (((ch & 0x80) && ch < 0xA0) ||
312                             (!(ch & 0x80) && !isprint(ch) &&
313                              !isspace(ch) && ch != '\a' && ch != '\b')
314                            ) {
315                                 if (ch & 0x80) {
316                                         ch &= ~0x80;
317                                         (void)fputs("M-", tp);
318                                 }
319                                 if (iscntrl(ch)) {
320                                         ch ^= 0x40;
321                                         (void)fputc('^', tp);
322                                 }
323                         }
324                         (void)fputc(ch, tp);
325                 }
326                 (void)fputs(cr, tp);
327                 --linecnt;
328         }
329         (void)fprintf(tp, "----%s\n", cr);
330         (void)fclose(fi);
331 }