- Use socklen_t
[dragonfly.git] / libexec / comsat / comsat.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)comsat.c 8.1 (Berkeley) 6/4/93
35 * $FreeBSD: src/libexec/comsat/comsat.c,v 1.13.2.1 2002/08/09 02:56:30 johan Exp $
667f3b18 36 * $DragonFly: src/libexec/comsat/comsat.c,v 1.6 2005/05/03 17:39:03 liamfoy Exp $
984263bc
MD
37 */
38
984263bc
MD
39#include <sys/param.h>
40#include <sys/socket.h>
41#include <sys/stat.h>
42#include <sys/file.h>
43#include <sys/wait.h>
44
45#include <netinet/in.h>
46
47#include <ctype.h>
48#include <err.h>
49#include <errno.h>
50#include <netdb.h>
51#include <paths.h>
52#include <pwd.h>
53#include <termios.h>
54#include <signal.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
7dedf5cb 58#include <sysexits.h>
984263bc
MD
59#include <syslog.h>
60#include <unistd.h>
61#include <utmp.h>
62
63int debug = 0;
64#define dsyslog if (debug) syslog
65
66#define MAXIDLE 120
67
68char hostname[MAXHOSTNAMELEN];
69struct utmp *utmp = NULL;
70time_t lastmsgtime;
71int nutmp, uf;
72
7dedf5cb 73void jkfprintf (FILE *, const char *, const char *, off_t);
3eefb696 74void mailfor (char *);
7dedf5cb 75void notify (struct utmp *, char *, off_t, int);
3eefb696
MD
76void onalrm (int);
77void reapchildren (int);
984263bc
MD
78
79int
7dedf5cb 80main(int argc __unused, char **argv __unused)
984263bc
MD
81{
82 struct sockaddr_in from;
667f3b18 83 socklen_t fromlen;
7dedf5cb 84 int cc;
984263bc
MD
85 char msgbuf[256];
86
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);
7dedf5cb 94 recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
984263bc
MD
95 exit(1);
96 }
97 if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
98 syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
7dedf5cb 99 recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
984263bc
MD
100 exit(1);
101 }
7dedf5cb
LF
102 time(&lastmsgtime);
103 gethostname(hostname, sizeof(hostname));
984263bc 104 onalrm(0);
7dedf5cb
LF
105 signal(SIGALRM, onalrm);
106 signal(SIGTTOU, SIG_IGN);
107 signal(SIGCHLD, reapchildren);
984263bc
MD
108 for (;;) {
109 cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
110 if (cc <= 0) {
111 if (errno != EINTR)
112 sleep(1);
113 errno = 0;
114 continue;
115 }
116 if (!nutmp) /* no one has logged in yet */
117 continue;
118 sigblock(sigmask(SIGALRM));
119 msgbuf[cc] = '\0';
7dedf5cb 120 time(&lastmsgtime);
984263bc
MD
121 mailfor(msgbuf);
122 sigsetmask(0L);
123 }
124}
125
126void
7dedf5cb 127reapchildren(int signo __unused)
984263bc
MD
128{
129 while (wait3(NULL, WNOHANG, NULL) > 0);
130}
131
132void
7dedf5cb 133onalrm(int signo __unused)
984263bc
MD
134{
135 static u_int utmpsize; /* last malloced size for utmp */
7dedf5cb 136 static time_t utmpmtime; /* last modification time for utmp */
984263bc
MD
137 struct stat statbf;
138
139 if (time(NULL) - lastmsgtime >= MAXIDLE)
7dedf5cb
LF
140 exit(EX_OK);
141 alarm((u_int)15);
142 fstat(uf, &statbf);
984263bc
MD
143 if (statbf.st_mtime > utmpmtime) {
144 utmpmtime = statbf.st_mtime;
145 if (statbf.st_size > utmpsize) {
146 utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
147 if ((utmp = realloc(utmp, utmpsize)) == NULL) {
5b3fa239 148 syslog(LOG_ERR, "realloc: %m");
984263bc
MD
149 exit(1);
150 }
151 }
7dedf5cb 152 lseek(uf, (off_t)0, L_SET);
984263bc
MD
153 nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
154 }
155}
156
157void
7dedf5cb 158mailfor(char *name)
984263bc 159{
7dedf5cb
LF
160 struct utmp *utp = &utmp[nutmp];
161 char *cp;
984263bc
MD
162 char *file;
163 off_t offset;
164 int folder;
165 char buf[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
166 char buf2[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
167
168 if (!(cp = strchr(name, '@')))
169 return;
170 *cp = '\0';
171 offset = atoi(cp + 1);
172 if (!(cp = strchr(cp + 1, ':')))
173 file = name;
174 else
175 file = cp + 1;
176 sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utmp[0].ut_name),
177 name);
178 if (*file != '/') {
179 sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
180 (int)sizeof(utmp[0].ut_name), file);
181 file = buf2;
182 }
183 folder = strcmp(buf, file);
184 while (--utp >= utmp)
185 if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
186 notify(utp, file, offset, folder);
187}
188
7dedf5cb 189static const char *cr;
984263bc
MD
190
191void
7dedf5cb 192notify(struct utmp *utp, char *file, off_t offset, int folder)
984263bc
MD
193{
194 FILE *tp;
195 struct stat stb;
196 struct termios tio;
197 char tty[20], name[sizeof(utmp[0].ut_name) + 1];
198
7dedf5cb 199 snprintf(tty, sizeof(tty), "%s%.*s",
984263bc
MD
200 _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
201 if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
202 /* A slash is an attempt to break security... */
203 syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
204 return;
205 }
206 if (stat(tty, &stb) || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
207 dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
208 return;
209 }
210 dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
211 if (fork())
212 return;
7dedf5cb
LF
213 signal(SIGALRM, SIG_DFL);
214 alarm((u_int)30);
984263bc
MD
215 if ((tp = fopen(tty, "w")) == NULL) {
216 dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
217 _exit(1);
218 }
7dedf5cb 219 tcgetattr(fileno(tp), &tio);
984263bc 220 cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ? "\n" : "\n\r";
7dedf5cb 221 strncpy(name, utp->ut_name, sizeof(utp->ut_name));
984263bc
MD
222 name[sizeof(name) - 1] = '\0';
223 switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
224 case S_IXUSR:
225 case (S_IXUSR | S_IXGRP):
7dedf5cb 226 fprintf(tp,
984263bc
MD
227 "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
228 cr, name, (int)sizeof(hostname), hostname,
229 folder ? cr : "", folder ? "to " : "", folder ? file : "",
230 cr, cr);
231 jkfprintf(tp, name, file, offset);
232 break;
233 case S_IXGRP:
7dedf5cb
LF
234 fprintf(tp, "\007");
235 fflush(tp);
236 sleep(1);
237 fprintf(tp, "\007");
984263bc
MD
238 break;
239 default:
240 break;
241 }
7dedf5cb
LF
242 fclose(tp);
243 _exit(EX_OK);
984263bc
MD
244}
245
246void
7dedf5cb 247jkfprintf(FILE *tp, const char *user, const char *file, off_t offset)
984263bc 248{
7dedf5cb
LF
249 unsigned char *cp, ch;
250 FILE *fi;
251 int linecnt, charcnt, inheader;
252 struct passwd *p;
984263bc
MD
253 unsigned char line[BUFSIZ];
254
255 /* Set effective uid to user in case mail drop is on nfs */
256 if ((p = getpwnam(user)) != NULL)
7dedf5cb 257 setuid(p->pw_uid);
984263bc
MD
258
259 if ((fi = fopen(file, "r")) == NULL)
260 return;
261
7dedf5cb 262 fseek(fi, offset, L_SET);
984263bc
MD
263 /*
264 * Print the first 7 lines or 560 characters of the new mail
265 * (whichever comes first). Skip header crap other than
266 * From, Subject, To, and Date.
267 */
268 linecnt = 7;
269 charcnt = 560;
270 inheader = 1;
271 while (fgets(line, sizeof(line), fi) != NULL) {
272 if (inheader) {
273 if (line[0] == '\n') {
274 inheader = 0;
275 continue;
276 }
277 if (line[0] == ' ' || line[0] == '\t' ||
278 (strncmp(line, "From:", 5) &&
279 strncmp(line, "Subject:", 8)))
280 continue;
281 }
282 if (linecnt <= 0 || charcnt <= 0) {
7dedf5cb
LF
283 fprintf(tp, "...more...%s", cr);
284 fclose(fi);
984263bc
MD
285 return;
286 }
287 /* strip weird stuff so can't trojan horse stupid terminals */
288 for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
289 /* disable upper controls and enable all other
290 8bit codes due to lack of locale knowledge
291 */
292 if (((ch & 0x80) && ch < 0xA0) ||
293 (!(ch & 0x80) && !isprint(ch) &&
294 !isspace(ch) && ch != '\a' && ch != '\b')
295 ) {
296 if (ch & 0x80) {
297 ch &= ~0x80;
7dedf5cb 298 fputs("M-", tp);
984263bc
MD
299 }
300 if (iscntrl(ch)) {
301 ch ^= 0x40;
7dedf5cb 302 fputc('^', tp);
984263bc
MD
303 }
304 }
7dedf5cb 305 fputc(ch, tp);
984263bc 306 }
7dedf5cb 307 fputs(cr, tp);
984263bc
MD
308 --linecnt;
309 }
7dedf5cb
LF
310 fprintf(tp, "----%s\n", cr);
311 fclose(fi);
984263bc 312}