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