2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)comsat.c 8.1 (Berkeley) 6/4/93";
42 static const char rcsid[] =
46 #include <sys/param.h>
47 #include <sys/socket.h>
52 #include <netinet/in.h>
70 #define dsyslog if (debug) syslog
74 static char hostname[MAXHOSTNAMELEN];
76 static void jkfprintf(FILE *, char[], char[], off_t);
77 static void mailfor(char *);
78 static void notify(struct utmpx *, char[], off_t, int);
79 static void reapchildren(int);
82 main(int argc __unused, char *argv[] __unused)
84 struct sockaddr_in from;
89 /* verify proper invocation */
90 fromlen = sizeof(from);
91 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
92 err(1, "getsockname");
93 openlog("comsat", LOG_PID, LOG_DAEMON);
94 if (chdir(_PATH_MAILDIR)) {
95 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
96 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
99 (void)gethostname(hostname, sizeof(hostname));
100 (void)signal(SIGTTOU, SIG_IGN);
101 (void)signal(SIGCHLD, reapchildren);
103 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
117 reapchildren(int signo __unused)
119 while (wait3(NULL, WNOHANG, NULL) > 0);
130 char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
131 char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
133 if (!(cp = strchr(name, '@')))
136 offset = strtoll(cp + 1, NULL, 10);
137 if (!(cp = strchr(cp + 1, ':')))
141 sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
144 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
145 (int)sizeof(utp->ut_user), file);
148 folder = strcmp(buf, file);
150 while ((utp = getutxent()) != NULL)
151 if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
152 notify(utp, file, offset, folder);
156 static const char *cr;
159 notify(struct utmpx *utp, char file[], off_t offset, int folder)
165 const char *s = utp->ut_line;
167 if (strncmp(s, "pts/", 4) == 0)
169 if (strchr(s, '/')) {
170 /* A slash is an attempt to break security... */
171 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
175 (void)snprintf(tty, sizeof(tty), "%s%.*s",
176 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
177 if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
178 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
181 dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
184 syslog(LOG_NOTICE, "fork failed (%m)");
191 if ((tp = fopen(tty, "w")) == NULL) {
192 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
195 (void)tcgetattr(fileno(tp), &tio);
196 cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ? "\n" : "\n\r";
197 switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
199 case (S_IXUSR | S_IXGRP):
201 "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
202 cr, utp->ut_user, (int)sizeof(hostname), hostname,
203 folder ? cr : "", folder ? "to " : "", folder ? file : "",
205 jkfprintf(tp, utp->ut_user, file, offset);
208 (void)fprintf(tp, "\007");
211 (void)fprintf(tp, "\007");
221 jkfprintf(FILE *tp, char user[], char file[], off_t offset)
223 unsigned char *cp, ch;
225 int linecnt, charcnt, inheader;
227 unsigned char line[BUFSIZ];
229 /* Set effective uid to user in case mail drop is on nfs */
230 if ((p = getpwnam(user)) != NULL)
231 (void) setuid(p->pw_uid);
233 if ((fi = fopen(file, "r")) == NULL)
236 (void)fseeko(fi, offset, SEEK_CUR);
238 * Print the first 7 lines or 560 characters of the new mail
239 * (whichever comes first). Skip header crap other than
240 * From, Subject, To, and Date.
245 while (fgets(line, sizeof(line), fi) != NULL) {
247 if (line[0] == '\n') {
251 if (line[0] == ' ' || line[0] == '\t' ||
252 (strncmp(line, "From:", 5) &&
253 strncmp(line, "Subject:", 8)))
256 if (linecnt <= 0 || charcnt <= 0) {
257 (void)fprintf(tp, "...more...%s", cr);
261 /* strip weird stuff so can't trojan horse stupid terminals */
262 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
263 /* disable upper controls and enable all other
264 8bit codes due to lack of locale knowledge
266 if (((ch & 0x80) && ch < 0xA0) ||
267 (!(ch & 0x80) && !isprint(ch) &&
268 !isspace(ch) && ch != '\a' && ch != '\b')
272 (void)fputs("M-", tp);
276 (void)fputc('^', tp);
284 (void)fprintf(tp, "----%s\n", cr);