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
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/file.h>
36 #include <sys/wait.h>
37
38 #include <netinet/in.h>
39
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <netdb.h>
44 #include <paths.h>
45 #include <pwd.h>
46 #include <termios.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <utmpx.h>
54
55 static int debug = 0;
56 #define dsyslog if (debug) syslog
57
58 #define MAXIDLE 120
59
60 static char hostname[MAXHOSTNAMELEN];
61
62 static void jkfprintf(FILE *, char[], off_t);
63 static void mailfor(char *);
64 static void notify(struct utmpx *, char[], off_t, int);
65 static void reapchildren(int);
66
67 int
main(int argc __unused,char * argv[]__unused)68 main(int argc __unused, char *argv[] __unused)
69 {
70 struct sockaddr_in from;
71 socklen_t fromlen;
72 int cc;
73 char msgbuf[256];
74
75 /* verify proper invocation */
76 fromlen = sizeof(from);
77 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
78 err(1, "getsockname");
79 openlog("comsat", LOG_PID, LOG_DAEMON);
80 if (chdir(_PATH_MAILDIR)) {
81 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
82 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
83 exit(1);
84 }
85 (void)gethostname(hostname, sizeof(hostname));
86 (void)signal(SIGTTOU, SIG_IGN);
87 (void)signal(SIGCHLD, reapchildren);
88 for (;;) {
89 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
90 if (cc <= 0) {
91 if (errno != EINTR)
92 sleep(1);
93 errno = 0;
94 continue;
95 }
96 msgbuf[cc] = '\0';
97 mailfor(msgbuf);
98 sigsetmask(0L);
99 }
100 }
101
102 static void
reapchildren(int signo __unused)103 reapchildren(int signo __unused)
104 {
105 while (wait3(NULL, WNOHANG, NULL) > 0);
106 }
107
108 static void
mailfor(char * name)109 mailfor(char *name)
110 {
111 struct utmpx *utp;
112 char *cp;
113 char *file;
114 off_t offset;
115 int folder;
116 char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
117 char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
118
119 if (!(cp = strchr(name, '@')))
120 return;
121 *cp = '\0';
122 offset = strtoll(cp + 1, NULL, 10);
123 if (!(cp = strchr(cp + 1, ':')))
124 file = name;
125 else
126 file = cp + 1;
127 sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
128 name);
129 if (*file != '/') {
130 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
131 (int)sizeof(utp->ut_user), file);
132 file = buf2;
133 }
134 folder = strcmp(buf, file);
135 setutxent();
136 while ((utp = getutxent()) != NULL)
137 if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
138 notify(utp, file, offset, folder);
139 endutxent();
140 }
141
142 static const char *cr;
143
144 static void
notify(struct utmpx * utp,char file[],off_t offset,int folder)145 notify(struct utmpx *utp, char file[], off_t offset, int folder)
146 {
147 FILE *tp;
148 struct stat stb;
149 struct termios tio;
150 struct passwd *p;
151 char tty[20];
152 const char *s = utp->ut_line;
153
154 if (strncmp(s, "pts/", 4) == 0)
155 s += 4;
156 if (strchr(s, '/')) {
157 /* A slash is an attempt to break security... */
158 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
159 utp->ut_line);
160 return;
161 }
162 (void)snprintf(tty, sizeof(tty), "%s%.*s",
163 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
164 if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
165 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
166 return;
167 }
168 dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
169 switch (fork()) {
170 case -1:
171 syslog(LOG_NOTICE, "fork failed (%m)");
172 return;
173 case 0:
174 break;
175 default:
176 return;
177 }
178 if ((tp = fopen(tty, "w")) == NULL) {
179 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
180 _exit(1);
181 }
182 (void)tcgetattr(fileno(tp), &tio);
183 cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ? "\n" : "\n\r";
184
185 /* Set uid/gid/groups to user's in case mail drop is on nfs */
186 if ((p = getpwnam(utp->ut_user)) == NULL ||
187 initgroups(p->pw_name, p->pw_gid) == -1 ||
188 setgid(p->pw_gid) == -1 ||
189 setuid(p->pw_uid) == -1)
190 return;
191
192 switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
193 case S_IXUSR:
194 case (S_IXUSR | S_IXGRP):
195 (void)fprintf(tp,
196 "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
197 cr, utp->ut_user, (int)sizeof(hostname), hostname,
198 folder ? cr : "", folder ? "to " : "", folder ? file : "",
199 cr, cr);
200 jkfprintf(tp, file, offset);
201 break;
202 case S_IXGRP:
203 (void)fprintf(tp, "\007");
204 (void)fflush(tp);
205 (void)sleep(1);
206 (void)fprintf(tp, "\007");
207 break;
208 default:
209 break;
210 }
211 (void)fclose(tp);
212 _exit(0);
213 }
214
215 static void
jkfprintf(FILE * tp,char file[],off_t offset)216 jkfprintf(FILE *tp, char file[], off_t offset)
217 {
218 unsigned char *cp, ch;
219 FILE *fi;
220 int linecnt, charcnt, inheader;
221 unsigned char line[BUFSIZ];
222
223 if ((fi = fopen(file, "r")) == NULL)
224 return;
225
226 (void)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 (void)fprintf(tp, "...more...%s", cr);
248 (void)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 (void)fputs("M-", tp);
263 }
264 if (iscntrl(ch)) {
265 ch ^= 0x40;
266 (void)fputc('^', tp);
267 }
268 }
269 (void)fputc(ch, tp);
270 }
271 (void)fputs(cr, tp);
272 --linecnt;
273 }
274 (void)fprintf(tp, "----%s\n", cr);
275 (void)fclose(fi);
276 }
277