Merge branch 'vendor/WPA_SUPPLICANT'
[dragonfly.git] / libexec / comsat / comsat.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * @(#)comsat.c 8.1 (Berkeley) 6/4/93
32  * $FreeBSD: head/libexec/comsat/comsat.c 326025 2017-11-20 19:49:47Z pfg $
33  */
34
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/file.h>
39 #include <sys/wait.h>
40
41 #include <netinet/in.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <netdb.h>
47 #include <paths.h>
48 #include <pwd.h>
49 #include <termios.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sysexits.h>
55 #include <syslog.h>
56 #include <unistd.h>
57 #include <utmpx.h>
58
59 static int      debug = 0;
60 #define dsyslog if (debug) syslog
61
62 #define MAXIDLE 120
63
64 static char     hostname[MAXHOSTNAMELEN];
65
66 static void     jkfprintf(FILE *, char[], char[], off_t);
67 static void     mailfor(char *);
68 static void     notify(struct utmpx *, char[], off_t, int);
69 static void     reapchildren(int);
70
71 int
72 main(int argc __unused, char *argv[] __unused)
73 {
74         struct sockaddr_in from;
75         socklen_t fromlen;
76         int cc;
77         char msgbuf[256];
78
79         /* verify proper invocation */
80         fromlen = sizeof(from);
81         if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
82                 err(1, "getsockname");
83         openlog("comsat", LOG_PID, LOG_DAEMON);
84         if (chdir(_PATH_MAILDIR)) {
85                 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
86                 recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
87                 exit(1);
88         }
89         gethostname(hostname, sizeof(hostname));
90         signal(SIGTTOU, SIG_IGN);
91         signal(SIGCHLD, reapchildren);
92         for (;;) {
93                 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
94                 if (cc <= 0) {
95                         if (errno != EINTR)
96                                 sleep(1);
97                         errno = 0;
98                         continue;
99                 }
100                 msgbuf[cc] = '\0';
101                 mailfor(msgbuf);
102                 sigsetmask(0L);
103         }
104 }
105
106 static void
107 reapchildren(int signo __unused)
108 {
109         while (wait3(NULL, WNOHANG, NULL) > 0);
110 }
111
112 static void
113 mailfor(char *name)
114 {
115         struct utmpx *utp;
116         char *cp;
117         char *file;
118         off_t offset;
119         int folder;
120         char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
121         char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
122
123         if (!(cp = strchr(name, '@')))
124                 return;
125         *cp = '\0';
126         offset = strtoll(cp + 1, NULL, 10);
127         if (!(cp = strchr(cp + 1, ':')))
128                 file = name;
129         else
130                 file = cp + 1;
131         sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
132             name);
133         if (*file != '/') {
134                 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
135                     (int)sizeof(utp->ut_user), file);
136                 file = buf2;
137         }
138         folder = strcmp(buf, file);
139         setutxent();
140         while ((utp = getutxent()) != NULL)
141                 if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
142                         notify(utp, file, offset, folder);
143         endutxent();
144 }
145
146 static const char *cr;
147
148 static void
149 notify(struct utmpx *utp, char file[], off_t offset, int folder)
150 {
151         FILE *tp;
152         struct stat stb;
153         struct termios tio;
154         char tty[20];
155         const char *s = utp->ut_line;
156
157         if (strncmp(s, "pts/", 4) == 0)
158                 s += 4;
159         if (strchr(s, '/')) {
160                 /* A slash is an attempt to break security... */
161                 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
162                     utp->ut_line);
163                 return;
164         }
165         snprintf(tty, sizeof(tty), "%s%.*s",
166             _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
167         if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
168                 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
169                 return;
170         }
171         dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
172         switch (fork()) {
173         case -1:
174                 syslog(LOG_NOTICE, "fork failed (%m)");
175                 return;
176         case 0:
177                 break;
178         default:
179                 return;
180         }
181         if ((tp = fopen(tty, "w")) == NULL) {
182                 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
183                 _exit(1);
184         }
185         tcgetattr(fileno(tp), &tio);
186         cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ?  "\n" : "\n\r";
187         switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
188         case S_IXUSR:
189         case (S_IXUSR | S_IXGRP):
190                 fprintf(tp, 
191                     "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
192                     cr, utp->ut_user, (int)sizeof(hostname), hostname,
193                     folder ? cr : "", folder ? "to " : "", folder ? file : "",
194                     cr, cr);
195                 jkfprintf(tp, utp->ut_user, file, offset);
196                 break;
197         case S_IXGRP:
198                 fprintf(tp, "\007");
199                 fflush(tp);      
200                 sleep(1);
201                 fprintf(tp, "\007");
202                 break;
203         default:
204                 break;
205         }       
206         fclose(tp);
207         _exit(EX_OK);
208 }
209
210 static void
211 jkfprintf(FILE *tp, char user[], char file[], off_t offset)
212 {
213         unsigned char *cp, ch;
214         FILE *fi;
215         int linecnt, charcnt, inheader;
216         struct passwd *p;
217         unsigned char line[BUFSIZ];
218
219         /* Set effective uid to user in case mail drop is on nfs */
220         if ((p = getpwnam(user)) != NULL)
221                 setuid(p->pw_uid);
222
223         if ((fi = fopen(file, "r")) == NULL)
224                 return;
225
226         fseeko(fi, offset, SEEK_CUR);
227         /*
228          * Print the first 7 lines or 560 characters of the new mail
229          * (whichever comes first).  Skip header crap other than
230          * From, Subject, To, and Date.
231          */
232         linecnt = 7;
233         charcnt = 560;
234         inheader = 1;
235         while (fgets(line, sizeof(line), fi) != NULL) {
236                 if (inheader) {
237                         if (line[0] == '\n') {
238                                 inheader = 0;
239                                 continue;
240                         }
241                         if (line[0] == ' ' || line[0] == '\t' ||
242                             (strncmp(line, "From:", 5) &&
243                             strncmp(line, "Subject:", 8)))
244                                 continue;
245                 }
246                 if (linecnt <= 0 || charcnt <= 0) {
247                         fprintf(tp, "...more...%s", cr);
248                         fclose(fi);
249                         return;
250                 }
251                 /* strip weird stuff so can't trojan horse stupid terminals */
252                 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
253                         /* disable upper controls and enable all other
254                            8bit codes due to lack of locale knowledge
255                          */
256                         if (((ch & 0x80) && ch < 0xA0) ||
257                             (!(ch & 0x80) && !isprint(ch) &&
258                              !isspace(ch) && ch != '\a' && ch != '\b')
259                            ) {
260                                 if (ch & 0x80) {
261                                         ch &= ~0x80;
262                                         fputs("M-", tp);
263                                 }
264                                 if (iscntrl(ch)) {
265                                         ch ^= 0x40;
266                                         fputc('^', tp);
267                                 }
268                         }
269                         fputc(ch, tp);
270                 }
271                 fputs(cr, tp);
272                 --linecnt;
273         }
274         fprintf(tp, "----%s\n", cr);
275         fclose(fi);
276 }