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