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
31 * @(#)comsat.c 8.1 (Berkeley) 6/4/93
32 * $FreeBSD: head/libexec/comsat/comsat.c 326025 2017-11-20 19:49:47Z pfg $
35 #include <sys/param.h>
36 #include <sys/socket.h>
41 #include <netinet/in.h>
60 #define dsyslog if (debug) syslog
64 static char hostname
[MAXHOSTNAMELEN
];
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);
72 main(int argc __unused
, char *argv
[] __unused
)
74 struct sockaddr_in from
;
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);
89 gethostname(hostname
, sizeof(hostname
));
90 signal(SIGTTOU
, SIG_IGN
);
91 signal(SIGCHLD
, reapchildren
);
93 cc
= recv(0, msgbuf
, sizeof(msgbuf
) - 1, 0);
107 reapchildren(int signo __unused
)
109 while (wait3(NULL
, WNOHANG
, NULL
) > 0);
120 char buf
[sizeof(_PATH_MAILDIR
) + sizeof(utp
->ut_user
) + 1];
121 char buf2
[sizeof(_PATH_MAILDIR
) + sizeof(utp
->ut_user
) + 1];
123 if (!(cp
= strchr(name
, '@')))
126 offset
= strtoll(cp
+ 1, NULL
, 10);
127 if (!(cp
= strchr(cp
+ 1, ':')))
131 sprintf(buf
, "%s/%.*s", _PATH_MAILDIR
, (int)sizeof(utp
->ut_user
),
134 sprintf(buf2
, "%s/%.*s", _PATH_MAILDIR
,
135 (int)sizeof(utp
->ut_user
), file
);
138 folder
= strcmp(buf
, file
);
140 while ((utp
= getutxent()) != NULL
)
141 if (utp
->ut_type
== USER_PROCESS
&& !strcmp(utp
->ut_user
, name
))
142 notify(utp
, file
, offset
, folder
);
146 static const char *cr
;
149 notify(struct utmpx
*utp
, char file
[], off_t offset
, int folder
)
155 const char *s
= utp
->ut_line
;
157 if (strncmp(s
, "pts/", 4) == 0)
159 if (strchr(s
, '/')) {
160 /* A slash is an attempt to break security... */
161 syslog(LOG_AUTH
| LOG_NOTICE
, "Unexpected `/' in `%s'",
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
);
171 dsyslog(LOG_DEBUG
, "notify %s on %s", utp
->ut_user
, tty
);
174 syslog(LOG_NOTICE
, "fork failed (%m)");
181 if ((tp
= fopen(tty
, "w")) == NULL
) {
182 dsyslog(LOG_ERR
, "%s: %s", tty
, strerror(errno
));
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
)) {
189 case (S_IXUSR
| S_IXGRP
):
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
: "",
195 jkfprintf(tp
, utp
->ut_user
, file
, offset
);
211 jkfprintf(FILE *tp
, char user
[], char file
[], off_t offset
)
213 unsigned char *cp
, ch
;
215 int linecnt
, charcnt
, inheader
;
217 unsigned char line
[BUFSIZ
];
219 /* Set effective uid to user in case mail drop is on nfs */
220 if ((p
= getpwnam(user
)) != NULL
)
223 if ((fi
= fopen(file
, "r")) == NULL
)
226 fseeko(fi
, offset
, SEEK_CUR
);
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.
235 while (fgets(line
, sizeof(line
), fi
) != NULL
) {
237 if (line
[0] == '\n') {
241 if (line
[0] == ' ' || line
[0] == '\t' ||
242 (strncmp(line
, "From:", 5) &&
243 strncmp(line
, "Subject:", 8)))
246 if (linecnt
<= 0 || charcnt
<= 0) {
247 fprintf(tp
, "...more...%s", cr
);
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
256 if (((ch
& 0x80) && ch
< 0xA0) ||
257 (!(ch
& 0x80) && !isprint(ch
) &&
258 !isspace(ch
) && ch
!= '\a' && ch
!= '\b')
274 fprintf(tp
, "----%s\n", cr
);