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