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