wall.c revision 73320
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 73320 2001-03-02 07:30:37Z 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 != NULL)
130			g->gid = grp->gr_gid;
131		else
132			warnx("%s: no such group", g->name);
133	}
134
135	makemsg(*argv);
136
137	if (!(fp = fopen(_PATH_UTMP, "r")))
138		err(1, "cannot read %s", _PATH_UTMP);
139	iov.iov_base = mbuf;
140	iov.iov_len = mbufsize;
141	/* NOSTRICT */
142	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
143		if (!utmp.ut_name[0] ||
144		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
145			continue;
146		if (grouplist) {
147			strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name));
148			pw = getpwnam(username);
149			if (!pw)
150				continue;
151			ngrps = getgroups(pw->pw_gid, grps);
152			for (g = grouplist; g && ingroup == 0; g = g->next) {
153				if (g->gid == -1)
154					continue;
155				if (g->gid == pw->pw_gid)
156					ingroup = 1;
157				for (i = 0; i < ngrps && ingroup == 0; i++)
158					if (g->gid == grps[i])
159						ingroup = 1;
160			}
161			if (ingroup == 0)
162				continue;
163		}
164		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
165		line[sizeof(utmp.ut_line)] = '\0';
166		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
167			warnx("%s", p);
168	}
169	exit(0);
170}
171
172static void
173usage()
174{
175	(void)fprintf(stderr, "usage: wall [-g group] [file]\n");
176	exit(1);
177}
178
179void
180makemsg(char *fname)
181{
182	int cnt;
183	unsigned char ch;
184	struct tm *lt;
185	struct passwd *pw;
186	struct stat sbuf;
187	time_t now;
188	FILE *fp;
189	int fd;
190	char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
191	const char *whom;
192
193	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
194	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
195		err(1, "can't open temporary file");
196	(void)unlink(tmpname);
197
198	if (!nobanner) {
199		tty = ttyname(STDERR_FILENO);
200		if (tty == NULL)
201			tty = "no tty";
202
203		if (!(whom = getlogin()))
204			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
205		(void)gethostname(hostname, sizeof(hostname));
206		(void)time(&now);
207		lt = localtime(&now);
208
209		/*
210		 * all this stuff is to blank out a square for the message;
211		 * we wrap message lines at column 79, not 80, because some
212		 * terminals wrap after 79, some do not, and we can't tell.
213		 * Which means that we may leave a non-blank character
214		 * in column 80, but that can't be helped.
215		 */
216		(void)fprintf(fp, "\r%79s\r\n", " ");
217		(void)snprintf(lbuf, sizeof(lbuf),
218		    "Broadcast Message from %s@%s",
219		    whom, hostname);
220		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
221		(void)snprintf(lbuf, sizeof(lbuf),
222		    "        (%s) at %d:%02d %s...", tty,
223		    lt->tm_hour, lt->tm_min, lt->tm_zone);
224		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
225	}
226	(void)fprintf(fp, "%79s\r\n", " ");
227
228	if (fname && !(freopen(fname, "r", stdin)))
229		err(1, "can't read %s", fname);
230	while (fgets(lbuf, sizeof(lbuf), stdin))
231		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
232			if (ch == '\r') {
233				cnt = 0;
234			} else if (cnt == 79 || ch == '\n') {
235				for (; cnt < 79; ++cnt)
236					putc(' ', fp);
237				putc('\r', fp);
238				putc('\n', fp);
239				cnt = 0;
240			} else if (((ch & 0x80) && ch < 0xA0) ||
241				   /* disable upper controls */
242				   (!isprint(ch) && !isspace(ch) &&
243				    ch != '\a' && ch != '\b')
244				  ) {
245				if (ch & 0x80) {
246					ch &= 0x7F;
247					putc('M', fp);
248					if (++cnt == 79) {
249						putc('\r', fp);
250						putc('\n', fp);
251						cnt = 0;
252					}
253					putc('-', fp);
254					if (++cnt == 79) {
255						putc('\r', fp);
256						putc('\n', fp);
257						cnt = 0;
258					}
259				}
260				if (iscntrl(ch)) {
261					ch ^= 040;
262					putc('^', fp);
263					if (++cnt == 79) {
264						putc('\r', fp);
265						putc('\n', fp);
266						cnt = 0;
267					}
268				}
269				putc(ch, fp);
270			} else {
271				putc(ch, fp);
272			}
273		}
274	(void)fprintf(fp, "%79s\r\n", " ");
275	rewind(fp);
276
277	if (fstat(fd, &sbuf))
278		err(1, "can't stat temporary file");
279	mbufsize = sbuf.st_size;
280	if (!(mbuf = malloc((u_int)mbufsize)))
281		err(1, "out of memory");
282	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
283		err(1, "can't read temporary file");
284	(void)close(fd);
285}
286