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