wall.c revision 73255
1/*
2 * Copyright (c) 1988, 1990, 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.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1988, 1990, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/usr.bin/wall/wall.c 73255 2001-03-01 05:43:12Z imp $";
46#endif /* not lint */
47
48/*
49 * This program is not related to David Wall, whose Stanford Ph.D. thesis
50 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
51 */
52
53#include <sys/param.h>
54#include <sys/stat.h>
55#include <sys/uio.h>
56
57#include <ctype.h>
58#include <err.h>
59#include <grp.h>
60#include <locale.h>
61#include <paths.h>
62#include <pwd.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
67#include <unistd.h>
68#include <utmp.h>
69
70static void makemsg(char *);
71static void usage(void);
72char   *ttymsg(struct iovec *, int, const char *, int);
73
74#define	IGNOREUSER	"sleeper"
75
76struct wallgroup {
77	struct wallgroup *next;
78	char		*name;
79	gid_t		gid;
80} *grouplist;
81int nobanner;
82int mbufsize;
83char *mbuf;
84
85int
86main(int argc, char *argv[])
87{
88	struct iovec iov;
89	struct utmp utmp;
90	gid_t grps[NGROUPS_MAX];
91	int ch;
92	int ingroup, ngrps, i;
93	FILE *fp;
94	struct wallgroup *g;
95	struct group *grp;
96	char *p;
97	struct passwd *pw;
98	char line[sizeof(utmp.ut_line) + 1];
99	char username[sizeof(utmp.ut_name) + 1];
100
101	ingroup = 0;
102	(void)setlocale(LC_CTYPE, "");
103
104	while ((ch = getopt(argc, argv, "g:n")) != -1)
105		switch (ch) {
106		case 'n':
107			/* undoc option for shutdown: suppress banner */
108			if (geteuid() == 0)
109				nobanner = 1;
110			break;
111		case 'g':
112			g = (struct wallgroup *)malloc(sizeof *g);
113			g->next = grouplist;
114			g->name = optarg;
115			g->gid = -1;
116			grouplist = g;
117			break;
118		case '?':
119		default:
120			usage();
121		}
122	argc -= optind;
123	argv += optind;
124	if (argc > 1)
125		usage();
126
127	for (g = grouplist; g; g = g->next) {
128		grp = getgrnam(g->name);
129		if (grp)
130			g->gid = grp->gr_gid;
131	}
132
133	makemsg(*argv);
134
135	if (!(fp = fopen(_PATH_UTMP, "r")))
136		err(1, "cannot read %s", _PATH_UTMP);
137	iov.iov_base = mbuf;
138	iov.iov_len = mbufsize;
139	/* NOSTRICT */
140	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
141		if (!utmp.ut_name[0] ||
142		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
143			continue;
144		if (grouplist) {
145			strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name));
146			pw = getpwnam(username);
147			if (!pw)
148				continue;
149			ngrps = getgroups(pw->pw_gid, grps);
150			for (g = grouplist; g && ingroup == 0; g = g->next) {
151				if (g->gid == -1)
152					continue;
153				if (g->gid == pw->pw_gid)
154					ingroup = 1;
155				for (i = 0; i < ngrps && ingroup == 0; i++)
156					if (g->gid == grps[i])
157						ingroup = 1;
158			}
159			if (ingroup == 0)
160				continue;
161		}
162		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
163		line[sizeof(utmp.ut_line)] = '\0';
164		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
165			warnx("%s", p);
166	}
167	exit(0);
168}
169
170static void
171usage()
172{
173	(void)fprintf(stderr, "usage: wall [file]\n");
174	exit(1);
175}
176
177void
178makemsg(char *fname)
179{
180	int cnt;
181	unsigned char ch;
182	struct tm *lt;
183	struct passwd *pw;
184	struct stat sbuf;
185	time_t now;
186	FILE *fp;
187	int fd;
188	char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
189	const char *whom;
190
191	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
192	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
193		err(1, "can't open temporary file");
194	(void)unlink(tmpname);
195
196	if (!nobanner) {
197		tty = ttyname(STDERR_FILENO);
198		if (tty == NULL)
199			tty = "no tty";
200
201		if (!(whom = getlogin()))
202			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
203		(void)gethostname(hostname, sizeof(hostname));
204		(void)time(&now);
205		lt = localtime(&now);
206
207		/*
208		 * all this stuff is to blank out a square for the message;
209		 * we wrap message lines at column 79, not 80, because some
210		 * terminals wrap after 79, some do not, and we can't tell.
211		 * Which means that we may leave a non-blank character
212		 * in column 80, but that can't be helped.
213		 */
214		(void)fprintf(fp, "\r%79s\r\n", " ");
215		(void)snprintf(lbuf, sizeof(lbuf),
216		    "Broadcast Message from %s@%s",
217		    whom, hostname);
218		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
219		(void)snprintf(lbuf, sizeof(lbuf),
220		    "        (%s) at %d:%02d %s...", tty,
221		    lt->tm_hour, lt->tm_min, lt->tm_zone);
222		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
223	}
224	(void)fprintf(fp, "%79s\r\n", " ");
225
226	if (fname && !(freopen(fname, "r", stdin)))
227		err(1, "can't read %s", fname);
228	while (fgets(lbuf, sizeof(lbuf), stdin))
229		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
230			if (ch == '\r') {
231				cnt = 0;
232			} else if (cnt == 79 || ch == '\n') {
233				for (; cnt < 79; ++cnt)
234					putc(' ', fp);
235				putc('\r', fp);
236				putc('\n', fp);
237				cnt = 0;
238			} else if (((ch & 0x80) && ch < 0xA0) ||
239				   /* disable upper controls */
240				   (!isprint(ch) && !isspace(ch) &&
241				    ch != '\a' && ch != '\b')
242				  ) {
243				if (ch & 0x80) {
244					ch &= 0x7F;
245					putc('M', fp);
246					if (++cnt == 79) {
247						putc('\r', fp);
248						putc('\n', fp);
249						cnt = 0;
250					}
251					putc('-', fp);
252					if (++cnt == 79) {
253						putc('\r', fp);
254						putc('\n', fp);
255						cnt = 0;
256					}
257				}
258				if (iscntrl(ch)) {
259					ch ^= 040;
260					putc('^', fp);
261					if (++cnt == 79) {
262						putc('\r', fp);
263						putc('\n', fp);
264						cnt = 0;
265					}
266				}
267				putc(ch, fp);
268			} else {
269				putc(ch, fp);
270			}
271		}
272	(void)fprintf(fp, "%79s\r\n", " ");
273	rewind(fp);
274
275	if (fstat(fd, &sbuf))
276		err(1, "can't stat temporary file");
277	mbufsize = sbuf.st_size;
278	if (!(mbuf = malloc((u_int)mbufsize)))
279		err(1, "out of memory");
280	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
281		err(1, "can't read temporary file");
282	(void)close(fd);
283}
284