2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)comsat.c 8.1 (Berkeley) 6/4/93";
44 static const char rcsid[] =
48 #include <sys/param.h>
49 #include <sys/socket.h>
54 #include <netinet/in.h>
72 #define dsyslog if (debug) syslog
76 char hostname[MAXHOSTNAMELEN];
78 void jkfprintf(FILE *, char[], char[], off_t);
80 void notify(struct utmpx *, char[], off_t, int);
81 void reapchildren(int);
84 main(int argc __unused, char *argv[] __unused)
86 struct sockaddr_in from;
91 /* verify proper invocation */
92 fromlen = sizeof(from);
93 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
94 err(1, "getsockname");
95 openlog("comsat", LOG_PID, LOG_DAEMON);
96 if (chdir(_PATH_MAILDIR)) {
97 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
98 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
101 (void)gethostname(hostname, sizeof(hostname));
102 (void)signal(SIGTTOU, SIG_IGN);
103 (void)signal(SIGCHLD, reapchildren);
105 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
119 reapchildren(int signo __unused)
121 while (wait3(NULL, WNOHANG, NULL) > 0);
132 char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
133 char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
135 if (!(cp = strchr(name, '@')))
138 offset = strtoll(cp + 1, NULL, 10);
139 if (!(cp = strchr(cp + 1, ':')))
143 sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
146 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
147 (int)sizeof(utp->ut_user), file);
150 folder = strcmp(buf, file);
152 while ((utp = getutxent()) != NULL)
153 if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
154 notify(utp, file, offset, folder);
158 static const char *cr;
161 notify(struct utmpx *utp, char file[], off_t offset, int folder)
167 const char *s = utp->ut_line;
169 if (strncmp(s, "pts/", 4) == 0)
171 if (strchr(s, '/')) {
172 /* A slash is an attempt to break security... */
173 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
177 (void)snprintf(tty, sizeof(tty), "%s%.*s",
178 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
179 if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
180 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
183 dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
186 syslog(LOG_NOTICE, "fork failed (%m)");
193 if ((tp = fopen(tty, "w")) == NULL) {
194 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
197 (void)tcgetattr(fileno(tp), &tio);
198 cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ? "\n" : "\n\r";
199 switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
201 case (S_IXUSR | S_IXGRP):
203 "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
204 cr, utp->ut_user, (int)sizeof(hostname), hostname,
205 folder ? cr : "", folder ? "to " : "", folder ? file : "",
207 jkfprintf(tp, utp->ut_user, file, offset);
210 (void)fprintf(tp, "\007");
213 (void)fprintf(tp, "\007");
223 jkfprintf(FILE *tp, char user[], char file[], off_t offset)
225 unsigned char *cp, ch;
227 int linecnt, charcnt, inheader;
229 unsigned char line[BUFSIZ];
231 /* Set effective uid to user in case mail drop is on nfs */
232 if ((p = getpwnam(user)) != NULL)
233 (void) setuid(p->pw_uid);
235 if ((fi = fopen(file, "r")) == NULL)
238 (void)fseeko(fi, offset, SEEK_CUR);
240 * Print the first 7 lines or 560 characters of the new mail
241 * (whichever comes first). Skip header crap other than
242 * From, Subject, To, and Date.
247 while (fgets(line, sizeof(line), fi) != NULL) {
249 if (line[0] == '\n') {
253 if (line[0] == ' ' || line[0] == '\t' ||
254 (strncmp(line, "From:", 5) &&
255 strncmp(line, "Subject:", 8)))
258 if (linecnt <= 0 || charcnt <= 0) {
259 (void)fprintf(tp, "...more...%s", cr);
263 /* strip weird stuff so can't trojan horse stupid terminals */
264 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
265 /* disable upper controls and enable all other
266 8bit codes due to lack of locale knowledge
268 if (((ch & 0x80) && ch < 0xA0) ||
269 (!(ch & 0x80) && !isprint(ch) &&
270 !isspace(ch) && ch != '\a' && ch != '\b')
274 (void)fputs("M-", tp);
278 (void)fputc('^', tp);
286 (void)fprintf(tp, "----%s\n", cr);