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