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