xref: /freebsd/usr.bin/wall/wall.c (revision cf4c5a533126ca1ddb1f070af73f8f53b9e77fd4)
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 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1988, 1990, 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
43 
44 #ifndef lint
45 static const char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
46 #endif
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 
70 #include "ttymsg.h"
71 
72 static void makemsg(char *);
73 static void usage(void);
74 
75 struct wallgroup {
76 	struct wallgroup *next;
77 	char		*name;
78 	gid_t		gid;
79 } *grouplist;
80 int nobanner;
81 int mbufsize;
82 char *mbuf;
83 
84 static int
85 ttystat(char *line, int sz)
86 {
87 	struct stat sb;
88 	char ttybuf[MAXPATHLEN];
89 
90 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, 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 utmp utmp;
102 	int ch;
103 	int ingroup;
104 	FILE *fp;
105 	struct wallgroup *g;
106 	struct group *grp;
107 	char **np;
108 	const char *p;
109 	struct passwd *pw;
110 	char line[sizeof(utmp.ut_line) + 1];
111 	char username[sizeof(utmp.ut_name) + 1];
112 
113 	(void)setlocale(LC_CTYPE, "");
114 
115 	while ((ch = getopt(argc, argv, "g:n")) != -1)
116 		switch (ch) {
117 		case 'n':
118 			/* undoc option for shutdown: suppress banner */
119 			if (geteuid() == 0)
120 				nobanner = 1;
121 			break;
122 		case 'g':
123 			g = (struct wallgroup *)malloc(sizeof *g);
124 			g->next = grouplist;
125 			g->name = optarg;
126 			g->gid = -1;
127 			grouplist = g;
128 			break;
129 		case '?':
130 		default:
131 			usage();
132 		}
133 	argc -= optind;
134 	argv += optind;
135 	if (argc > 1)
136 		usage();
137 
138 	for (g = grouplist; g; g = g->next) {
139 		grp = getgrnam(g->name);
140 		if (grp != NULL)
141 			g->gid = grp->gr_gid;
142 		else
143 			warnx("%s: no such group", g->name);
144 	}
145 
146 	makemsg(*argv);
147 
148 	if (!(fp = fopen(_PATH_UTMP, "r")))
149 		err(1, "cannot read %s", _PATH_UTMP);
150 	iov.iov_base = mbuf;
151 	iov.iov_len = mbufsize;
152 	/* NOSTRICT */
153 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
154 		if (!utmp.ut_name[0])
155 			continue;
156 		if (ttystat(utmp.ut_line, UT_LINESIZE) != 0)
157 			continue;
158 		if (grouplist) {
159 			ingroup = 0;
160 			strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name));
161 			pw = getpwnam(username);
162 			if (!pw)
163 				continue;
164 			for (g = grouplist; g && ingroup == 0; g = g->next) {
165 				if (g->gid == (gid_t)-1)
166 					continue;
167 				if (g->gid == pw->pw_gid)
168 					ingroup = 1;
169 				else if ((grp = getgrgid(g->gid)) != NULL) {
170 					for (np = grp->gr_mem; *np; np++) {
171 						if (strcmp(*np, username) == 0) {
172 							ingroup = 1;
173 							break;
174 						}
175 					}
176 				}
177 			}
178 			if (ingroup == 0)
179 				continue;
180 		}
181 		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
182 		line[sizeof(utmp.ut_line)] = '\0';
183 		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
184 			warnx("%s", p);
185 	}
186 	exit(0);
187 }
188 
189 static void
190 usage()
191 {
192 	(void)fprintf(stderr, "usage: wall [-g group] [file]\n");
193 	exit(1);
194 }
195 
196 void
197 makemsg(char *fname)
198 {
199 	int cnt;
200 	unsigned char ch;
201 	struct tm *lt;
202 	struct passwd *pw;
203 	struct stat sbuf;
204 	time_t now;
205 	FILE *fp;
206 	int fd;
207 	char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
208 	const char *tty;
209 	const char *whom;
210 	gid_t egid;
211 
212 	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
213 	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
214 		err(1, "can't open temporary file");
215 	(void)unlink(tmpname);
216 
217 	if (!nobanner) {
218 		tty = ttyname(STDERR_FILENO);
219 		if (tty == NULL)
220 			tty = "no tty";
221 
222 		if (!(whom = getlogin()))
223 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
224 		(void)gethostname(hostname, sizeof(hostname));
225 		(void)time(&now);
226 		lt = localtime(&now);
227 
228 		/*
229 		 * all this stuff is to blank out a square for the message;
230 		 * we wrap message lines at column 79, not 80, because some
231 		 * terminals wrap after 79, some do not, and we can't tell.
232 		 * Which means that we may leave a non-blank character
233 		 * in column 80, but that can't be helped.
234 		 */
235 		(void)fprintf(fp, "\r%79s\r\n", " ");
236 		(void)snprintf(lbuf, sizeof(lbuf),
237 		    "Broadcast Message from %s@%s",
238 		    whom, hostname);
239 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
240 		(void)snprintf(lbuf, sizeof(lbuf),
241 		    "        (%s) at %d:%02d %s...", tty,
242 		    lt->tm_hour, lt->tm_min, lt->tm_zone);
243 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
244 	}
245 	(void)fprintf(fp, "%79s\r\n", " ");
246 
247 	if (fname) {
248 		egid = getegid();
249 		setegid(getgid());
250 	       	if (freopen(fname, "r", stdin) == NULL)
251 			err(1, "can't read %s", fname);
252 		setegid(egid);
253 	}
254 	while (fgets(lbuf, sizeof(lbuf), stdin)) {
255 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
256 			if (ch == '\r') {
257 				putc('\r', fp);
258 				cnt = 0;
259 				continue;
260 			} else if (ch == '\n') {
261 				for (; cnt < 79; ++cnt)
262 					putc(' ', fp);
263 				putc('\r', fp);
264 				putc('\n', fp);
265 				break;
266 			}
267 			if (cnt == 79) {
268 				putc('\r', fp);
269 				putc('\n', fp);
270 				cnt = 0;
271 			}
272 			if (((ch & 0x80) && ch < 0xA0) ||
273 				   /* disable upper controls */
274 				   (!isprint(ch) && !isspace(ch) &&
275 				    ch != '\a' && ch != '\b')
276 				  ) {
277 				if (ch & 0x80) {
278 					ch &= 0x7F;
279 					putc('M', fp);
280 					if (++cnt == 79) {
281 						putc('\r', fp);
282 						putc('\n', fp);
283 						cnt = 0;
284 					}
285 					putc('-', fp);
286 					if (++cnt == 79) {
287 						putc('\r', fp);
288 						putc('\n', fp);
289 						cnt = 0;
290 					}
291 				}
292 				if (iscntrl(ch)) {
293 					ch ^= 040;
294 					putc('^', fp);
295 					if (++cnt == 79) {
296 						putc('\r', fp);
297 						putc('\n', fp);
298 						cnt = 0;
299 					}
300 				}
301 			}
302 			putc(ch, fp);
303 		}
304 	}
305 	(void)fprintf(fp, "%79s\r\n", " ");
306 	rewind(fp);
307 
308 	if (fstat(fd, &sbuf))
309 		err(1, "can't stat temporary file");
310 	mbufsize = sbuf.st_size;
311 	if (!(mbuf = malloc((u_int)mbufsize)))
312 		err(1, "out of memory");
313 	if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
314 		err(1, "can't read temporary file");
315 	(void)close(fd);
316 }
317