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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)comsat.c 8.1 (Berkeley) 6/4/93";
40 static const char rcsid[] =
44 #include <sys/param.h>
45 #include <sys/socket.h>
50 #include <netinet/in.h>
68 #define dsyslog if (debug) syslog
72 static char hostname[MAXHOSTNAMELEN];
74 static void jkfprintf(FILE *, char[], char[], off_t);
75 static void mailfor(char *);
76 static void notify(struct utmpx *, char[], off_t, int);
77 static void reapchildren(int);
80 main(int argc __unused, char *argv[] __unused)
82 struct sockaddr_in from;
87 /* verify proper invocation */
88 fromlen = sizeof(from);
89 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
90 err(1, "getsockname");
91 openlog("comsat", LOG_PID, LOG_DAEMON);
92 if (chdir(_PATH_MAILDIR)) {
93 syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
94 (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
97 (void)gethostname(hostname, sizeof(hostname));
98 (void)signal(SIGTTOU, SIG_IGN);
99 (void)signal(SIGCHLD, reapchildren);
101 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
115 reapchildren(int signo __unused)
117 while (wait3(NULL, WNOHANG, NULL) > 0);
128 char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
129 char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
131 if (!(cp = strchr(name, '@')))
134 offset = strtoll(cp + 1, NULL, 10);
135 if (!(cp = strchr(cp + 1, ':')))
139 sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
142 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
143 (int)sizeof(utp->ut_user), file);
146 folder = strcmp(buf, file);
148 while ((utp = getutxent()) != NULL)
149 if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
150 notify(utp, file, offset, folder);
154 static const char *cr;
157 notify(struct utmpx *utp, char file[], off_t offset, int folder)
163 const char *s = utp->ut_line;
165 if (strncmp(s, "pts/", 4) == 0)
167 if (strchr(s, '/')) {
168 /* A slash is an attempt to break security... */
169 syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
173 (void)snprintf(tty, sizeof(tty), "%s%.*s",
174 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
175 if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
176 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
179 dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
182 syslog(LOG_NOTICE, "fork failed (%m)");
189 if ((tp = fopen(tty, "w")) == NULL) {
190 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
193 (void)tcgetattr(fileno(tp), &tio);
194 cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ? "\n" : "\n\r";
195 switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
197 case (S_IXUSR | S_IXGRP):
199 "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
200 cr, utp->ut_user, (int)sizeof(hostname), hostname,
201 folder ? cr : "", folder ? "to " : "", folder ? file : "",
203 jkfprintf(tp, utp->ut_user, file, offset);
206 (void)fprintf(tp, "\007");
209 (void)fprintf(tp, "\007");
219 jkfprintf(FILE *tp, char user[], char file[], off_t offset)
221 unsigned char *cp, ch;
223 int linecnt, charcnt, inheader;
225 unsigned char line[BUFSIZ];
227 /* Set effective uid to user in case mail drop is on nfs */
228 if ((p = getpwnam(user)) != NULL)
229 (void) setuid(p->pw_uid);
231 if ((fi = fopen(file, "r")) == NULL)
234 (void)fseeko(fi, offset, SEEK_CUR);
236 * Print the first 7 lines or 560 characters of the new mail
237 * (whichever comes first). Skip header crap other than
238 * From, Subject, To, and Date.
243 while (fgets(line, sizeof(line), fi) != NULL) {
245 if (line[0] == '\n') {
249 if (line[0] == ' ' || line[0] == '\t' ||
250 (strncmp(line, "From:", 5) &&
251 strncmp(line, "Subject:", 8)))
254 if (linecnt <= 0 || charcnt <= 0) {
255 (void)fprintf(tp, "...more...%s", cr);
259 /* strip weird stuff so can't trojan horse stupid terminals */
260 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
261 /* disable upper controls and enable all other
262 8bit codes due to lack of locale knowledge
264 if (((ch & 0x80) && ch < 0xA0) ||
265 (!(ch & 0x80) && !isprint(ch) &&
266 !isspace(ch) && ch != '\a' && ch != '\b')
270 (void)fputs("M-", tp);
274 (void)fputc('^', tp);
282 (void)fprintf(tp, "----%s\n", cr);