Annotation of src/usr.bin/wall/wall.c, Revision 1.30
1.30 ! martijn 1: /* $OpenBSD: wall.c,v 1.29 2015/11/05 22:20:11 benno Exp $ */
1.1 deraadt 2: /* $NetBSD: wall.c,v 1.6 1994/11/17 07:17:58 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1988, 1990, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.18 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
34: * This program is not related to David Wall, whose Stanford Ph.D. thesis
35: * is entitled "Mechanisms for Broadcast and Selective Broadcast".
36: */
37:
1.29 benno 38: #include <sys/queue.h>
1.1 deraadt 39: #include <sys/stat.h>
40: #include <sys/time.h>
41: #include <sys/uio.h>
42:
1.30 ! martijn 43: #include <ctype.h>
! 44: #include <err.h>
! 45: #include <grp.h>
! 46: #include <limits.h>
1.1 deraadt 47: #include <paths.h>
48: #include <pwd.h>
49: #include <stdio.h>
50: #include <stdlib.h>
51: #include <string.h>
52: #include <unistd.h>
53: #include <utmp.h>
1.4 deraadt 54: #include <vis.h>
1.1 deraadt 55:
1.6 deraadt 56: struct wallgroup {
1.15 millert 57: gid_t gid;
58: char *name;
59: char **mem;
1.6 deraadt 60: struct wallgroup *next;
61: } *grouplist;
62:
1.29 benno 63: struct utmptty {
64: char *tty;
65: SLIST_ENTRY(utmptty) next;
66: };
67:
1.15 millert 68: void makemsg(char *);
69: void addgroup(struct group *, char *);
70: char *ttymsg(struct iovec *, int, char *, int);
71: __dead void usage(void);
1.30 ! martijn 72: static int isu8cont(unsigned char);
1.1 deraadt 73:
74: int nobanner;
75: int mbufsize;
76: char *mbuf;
77:
78: /* ARGSUSED */
79: int
1.15 millert 80: main(int argc, char **argv)
1.1 deraadt 81: {
1.15 millert 82: FILE *fp;
83: int ch, ingroup;
1.1 deraadt 84: struct iovec iov;
85: struct utmp utmp;
1.15 millert 86: char *p, **mem;
1.1 deraadt 87: char line[sizeof(utmp.ut_line) + 1];
1.15 millert 88: char username[sizeof(utmp.ut_name) + 1];
89: struct passwd *pw;
90: struct group *grp;
1.6 deraadt 91: struct wallgroup *g;
1.29 benno 92: struct utmptty *un;
93: SLIST_HEAD(,utmptty) utmphead;
94: SLIST_INIT(&utmphead);
1.1 deraadt 95:
1.8 millert 96: while ((ch = getopt(argc, argv, "ng:")) != -1)
1.1 deraadt 97: switch (ch) {
98: case 'n':
99: /* undoc option for shutdown: suppress banner */
1.15 millert 100: pw = getpwnam("nobody");
101: if (geteuid() == 0 || (pw && getuid() == pw->pw_uid))
1.1 deraadt 102: nobanner = 1;
103: break;
1.6 deraadt 104: case 'g':
1.15 millert 105: if ((grp = getgrnam(optarg)) == NULL)
106: errx(1, "unknown group `%s'", optarg);
107: addgroup(grp, optarg);
1.6 deraadt 108: break;
1.1 deraadt 109: default:
1.15 millert 110: usage();
1.1 deraadt 111: }
112: argc -= optind;
113: argv += optind;
114: if (argc > 1)
1.15 millert 115: usage();
1.6 deraadt 116:
1.1 deraadt 117: makemsg(*argv);
118:
1.29 benno 119: if (pledge("stdio rpath wpath getpw proc", NULL) == -1)
120: err(1, "pledge");
121:
1.9 form 122: if (!(fp = fopen(_PATH_UTMP, "r")))
1.21 henning 123: err(1, "cannot read %s", _PATH_UTMP);
1.1 deraadt 124: iov.iov_base = mbuf;
125: iov.iov_len = mbufsize;
1.29 benno 126:
1.15 millert 127: while (fread(&utmp, sizeof(utmp), 1, fp) == 1) {
1.16 deraadt 128: if (!utmp.ut_name[0])
1.1 deraadt 129: continue;
1.6 deraadt 130: if (grouplist) {
1.15 millert 131: ingroup = 0;
132: strncpy(username, utmp.ut_name, sizeof(utmp.ut_name));
133: username[sizeof(utmp.ut_name)] = '\0';
1.6 deraadt 134: pw = getpwnam(username);
135: if (!pw)
136: continue;
137: for (g = grouplist; g && ingroup == 0; g = g->next) {
138: if (g->gid == pw->pw_gid)
139: ingroup = 1;
1.15 millert 140: for (mem = g->mem; *mem && ingroup == 0; mem++)
141: if (strcmp(username, *mem) == 0)
1.6 deraadt 142: ingroup = 1;
143: }
144: if (ingroup == 0)
145: continue;
146: }
1.1 deraadt 147: strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
148: line[sizeof(utmp.ut_line)] = '\0';
1.29 benno 149: un = malloc(sizeof(struct utmptty));
150: if (un == NULL)
151: err(1, "malloc");
152: un->tty = strndup(line, sizeof(utmp.ut_line));
153: if (un->tty == NULL)
154: err(1, "strndup");
155: SLIST_INSERT_HEAD(&utmphead, un, next);
156: }
157: fclose(fp);
158:
159: if (pledge("stdio rpath wpath proc", NULL) == -1)
160: err(1, "pledge");
161:
162: SLIST_FOREACH(un, &utmphead, next) {
163: if ((p = ttymsg(&iov, 1, un->tty, 60*5)) != NULL)
1.13 deraadt 164: warnx("%s", p);
1.1 deraadt 165: }
1.29 benno 166:
1.1 deraadt 167: exit(0);
168: }
169:
170: void
1.15 millert 171: makemsg(char *fname)
1.1 deraadt 172: {
1.30 ! martijn 173: int cnt;
1.1 deraadt 174: struct tm *lt;
175: struct passwd *pw;
176: struct stat sbuf;
1.19 deraadt 177: time_t now;
1.1 deraadt 178: FILE *fp;
179: int fd;
1.26 deraadt 180: char *p, *whom, hostname[HOST_NAME_MAX+1], lbuf[100], tmpname[PATH_MAX];
1.11 d 181: char *ttynam;
1.30 ! martijn 182: unsigned char ch;
1.1 deraadt 183:
1.20 avsm 184: snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXXXXXX", _PATH_TMP);
1.15 millert 185: if ((fd = mkstemp(tmpname)) >= 0) {
186: (void)unlink(tmpname);
187: fp = fdopen(fd, "r+");
188: }
189: if (fd == -1 || fp == NULL)
1.21 henning 190: err(1, "can't open temporary file");
1.1 deraadt 191:
192: if (!nobanner) {
193: if (!(whom = getlogin()))
194: whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
195: (void)gethostname(hostname, sizeof(hostname));
196: (void)time(&now);
197: lt = localtime(&now);
1.11 d 198: if ((ttynam = ttyname(STDERR_FILENO)) == NULL)
199: ttynam = "(not a tty)";
1.1 deraadt 200:
201: /*
202: * all this stuff is to blank out a square for the message;
203: * we wrap message lines at column 79, not 80, because some
204: * terminals wrap after 79, some do not, and we can't tell.
205: * Which means that we may leave a non-blank character
206: * in column 80, but that can't be helped.
207: */
208: (void)fprintf(fp, "\r%79s\r\n", " ");
1.3 deraadt 209: (void)snprintf(lbuf, sizeof lbuf,
210: "Broadcast Message from %s@%s", whom, hostname);
1.1 deraadt 211: (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
1.3 deraadt 212: (void)snprintf(lbuf, sizeof lbuf,
1.11 d 213: " (%s) at %d:%02d ...", ttynam,
1.1 deraadt 214: lt->tm_hour, lt->tm_min);
215: (void)fprintf(fp, "%-79.79s\r\n", lbuf);
216: }
217: (void)fprintf(fp, "%79s\r\n", " ");
218:
1.13 deraadt 219: if (fname) {
220: gid_t egid = getegid();
221:
222: setegid(getgid());
223: if (freopen(fname, "r", stdin) == NULL)
1.21 henning 224: err(1, "can't read %s", fname);
1.13 deraadt 225: setegid(egid);
226: }
1.1 deraadt 227: while (fgets(lbuf, sizeof(lbuf), stdin))
228: for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
1.30 ! martijn 229: if (cnt == 79 || ch == '\n') {
! 230: for (; cnt < 79; ++cnt)
1.1 deraadt 231: putc(' ', fp);
232: putc('\r', fp);
233: putc('\n', fp);
234: cnt = -1;
1.30 ! martijn 235: } else if (!isu8cont(ch))
! 236: putc(isprint(ch) || isspace(ch) ?
! 237: ch : '?', fp);
1.1 deraadt 238: }
239: (void)fprintf(fp, "%79s\r\n", " ");
240: rewind(fp);
241:
1.9 form 242: if (fstat(fd, &sbuf))
1.21 henning 243: err(1, "can't stat temporary file");
1.1 deraadt 244: mbufsize = sbuf.st_size;
1.22 tom 245: mbuf = malloc((u_int)mbufsize);
246: if (mbuf == NULL)
247: err(1, NULL);
1.9 form 248: if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
1.21 henning 249: err(1, "can't read temporary file");
1.1 deraadt 250: (void)close(fd);
1.15 millert 251: }
252:
253: void
254: addgroup(struct group *grp, char *name)
255: {
256: int i;
257: struct wallgroup *g;
258:
259: for (i = 0; grp->gr_mem[i]; i++)
260: ;
261:
1.27 deraadt 262: g = malloc(sizeof *g);
1.15 millert 263: if (g == NULL)
1.22 tom 264: err(1, NULL);
1.15 millert 265: g->gid = grp->gr_gid;
266: g->name = name;
1.27 deraadt 267: g->mem = calloc(i + 1, sizeof(char *));
1.15 millert 268: if (g->mem == NULL)
1.22 tom 269: err(1, NULL);
1.15 millert 270: for (i = 0; grp->gr_mem[i] != NULL; i++) {
271: g->mem[i] = strdup(grp->gr_mem[i]);
272: if (g->mem[i] == NULL)
1.22 tom 273: err(1, NULL);
1.15 millert 274: }
275: g->mem[i] = NULL;
276: g->next = grouplist;
277: grouplist = g;
278: }
279:
280: void
281: usage(void)
282: {
283: extern char *__progname;
284:
285: (void)fprintf(stderr, "usage: %s [-g group] [file]\n", __progname);
286: exit(1);
1.30 ! martijn 287: }
! 288:
! 289: static int
! 290: isu8cont(unsigned char c)
! 291: {
! 292: return (c & (0x80 | 0x40)) == 0x80;
1.1 deraadt 293: }