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