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