xref: /freebsd/usr.bin/wall/wall.c (revision 50e04779c499d5c329d1863b71ecd7c2b5824f76)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1988, 1990, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
30075f2939SMark Murray #include <sys/cdefs.h>
31075f2939SMark Murray 
32075f2939SMark Murray __FBSDID("$FreeBSD$");
33075f2939SMark Murray 
349b50d902SRodney W. Grimes #ifndef lint
35752d887aSPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1990, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
38075f2939SMark Murray #endif
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
41075f2939SMark Murray static const char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
42752d887aSPhilippe Charnier #endif
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes  * This program is not related to David Wall, whose Stanford Ph.D. thesis
469b50d902SRodney W. Grimes  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
479b50d902SRodney W. Grimes  */
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <sys/param.h>
509b50d902SRodney W. Grimes #include <sys/stat.h>
519b50d902SRodney W. Grimes #include <sys/uio.h>
529b50d902SRodney W. Grimes 
53752d887aSPhilippe Charnier #include <ctype.h>
54752d887aSPhilippe Charnier #include <err.h>
556a20d55aSWarner Losh #include <grp.h>
569566348dSAndrey A. Chernov #include <locale.h>
579b50d902SRodney W. Grimes #include <paths.h>
589b50d902SRodney W. Grimes #include <pwd.h>
599b50d902SRodney W. Grimes #include <stdio.h>
609b50d902SRodney W. Grimes #include <stdlib.h>
619b50d902SRodney W. Grimes #include <string.h>
6258011702SAndrey A. Chernov #include <time.h>
639b50d902SRodney W. Grimes #include <unistd.h>
64ab90a4d1SEd Schouten #include <utmpx.h>
65586fbee6SGleb Smirnoff #include <wchar.h>
66586fbee6SGleb Smirnoff #include <wctype.h>
679b50d902SRodney W. Grimes 
68728d043eSDima Dorfman #include "ttymsg.h"
69728d043eSDima Dorfman 
706a20d55aSWarner Losh static void makemsg(char *);
716a20d55aSWarner Losh static void usage(void);
729b50d902SRodney W. Grimes 
73d7698e07SEd Schouten static struct wallgroup {
746a20d55aSWarner Losh 	struct wallgroup *next;
756a20d55aSWarner Losh 	char		*name;
766a20d55aSWarner Losh 	gid_t		gid;
776a20d55aSWarner Losh } *grouplist;
78d7698e07SEd Schouten static int nobanner;
79d7698e07SEd Schouten static int mbufsize;
80d7698e07SEd Schouten static char *mbuf;
819b50d902SRodney W. Grimes 
82b457a3e1SOlivier Houchard static int
83bd76376fSEd Schouten ttystat(char *line)
84b457a3e1SOlivier Houchard {
85b457a3e1SOlivier Houchard 	struct stat sb;
86b457a3e1SOlivier Houchard 	char ttybuf[MAXPATHLEN];
87b457a3e1SOlivier Houchard 
88bd76376fSEd Schouten 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
89b457a3e1SOlivier Houchard 	if (stat(ttybuf, &sb) == 0) {
90b457a3e1SOlivier Houchard 		return (0);
91b457a3e1SOlivier Houchard 	} else
92b457a3e1SOlivier Houchard 		return (-1);
93b457a3e1SOlivier Houchard }
94b457a3e1SOlivier Houchard 
959b50d902SRodney W. Grimes int
966a20d55aSWarner Losh main(int argc, char *argv[])
979b50d902SRodney W. Grimes {
989b50d902SRodney W. Grimes 	struct iovec iov;
99bd76376fSEd Schouten 	struct utmpx *utmp;
1006a20d55aSWarner Losh 	int ch;
1012cfec5adSRuslan Ermilov 	int ingroup;
1026a20d55aSWarner Losh 	struct wallgroup *g;
1036a20d55aSWarner Losh 	struct group *grp;
104728d043eSDima Dorfman 	char **np;
105728d043eSDima Dorfman 	const char *p;
1066a20d55aSWarner Losh 	struct passwd *pw;
1079b50d902SRodney W. Grimes 
1089566348dSAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
1099566348dSAndrey A. Chernov 
1106a20d55aSWarner Losh 	while ((ch = getopt(argc, argv, "g:n")) != -1)
1119b50d902SRodney W. Grimes 		switch (ch) {
1129b50d902SRodney W. Grimes 		case 'n':
1139b50d902SRodney W. Grimes 			/* undoc option for shutdown: suppress banner */
1149b50d902SRodney W. Grimes 			if (geteuid() == 0)
1159b50d902SRodney W. Grimes 				nobanner = 1;
1169b50d902SRodney W. Grimes 			break;
1176a20d55aSWarner Losh 		case 'g':
1186a20d55aSWarner Losh 			g = (struct wallgroup *)malloc(sizeof *g);
1196a20d55aSWarner Losh 			g->next = grouplist;
1206a20d55aSWarner Losh 			g->name = optarg;
1216a20d55aSWarner Losh 			g->gid = -1;
1226a20d55aSWarner Losh 			grouplist = g;
1236a20d55aSWarner Losh 			break;
1249b50d902SRodney W. Grimes 		case '?':
1259b50d902SRodney W. Grimes 		default:
126752d887aSPhilippe Charnier 			usage();
1279b50d902SRodney W. Grimes 		}
1289b50d902SRodney W. Grimes 	argc -= optind;
1299b50d902SRodney W. Grimes 	argv += optind;
1309b50d902SRodney W. Grimes 	if (argc > 1)
131752d887aSPhilippe Charnier 		usage();
1329b50d902SRodney W. Grimes 
1336a20d55aSWarner Losh 	for (g = grouplist; g; g = g->next) {
1346a20d55aSWarner Losh 		grp = getgrnam(g->name);
135003ff943SWarner Losh 		if (grp != NULL)
1366a20d55aSWarner Losh 			g->gid = grp->gr_gid;
137003ff943SWarner Losh 		else
138003ff943SWarner Losh 			warnx("%s: no such group", g->name);
1396a20d55aSWarner Losh 	}
1406a20d55aSWarner Losh 
1419b50d902SRodney W. Grimes 	makemsg(*argv);
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes 	iov.iov_base = mbuf;
1449b50d902SRodney W. Grimes 	iov.iov_len = mbufsize;
1459b50d902SRodney W. Grimes 	/* NOSTRICT */
146bd76376fSEd Schouten 	while ((utmp = getutxent()) != NULL) {
147bd76376fSEd Schouten 		if (utmp->ut_type != USER_PROCESS)
1489b50d902SRodney W. Grimes 			continue;
149bd76376fSEd Schouten 		if (ttystat(utmp->ut_line) != 0)
150b457a3e1SOlivier Houchard 			continue;
1516a20d55aSWarner Losh 		if (grouplist) {
1522cfec5adSRuslan Ermilov 			ingroup = 0;
153bd76376fSEd Schouten 			pw = getpwnam(utmp->ut_user);
1546a20d55aSWarner Losh 			if (!pw)
1556a20d55aSWarner Losh 				continue;
1566a20d55aSWarner Losh 			for (g = grouplist; g && ingroup == 0; g = g->next) {
157075f2939SMark Murray 				if (g->gid == (gid_t)-1)
1586a20d55aSWarner Losh 					continue;
1596a20d55aSWarner Losh 				if (g->gid == pw->pw_gid)
1606a20d55aSWarner Losh 					ingroup = 1;
1612cfec5adSRuslan Ermilov 				else if ((grp = getgrgid(g->gid)) != NULL) {
1622cfec5adSRuslan Ermilov 					for (np = grp->gr_mem; *np; np++) {
163bd76376fSEd Schouten 						if (strcmp(*np, utmp->ut_user) == 0) {
1646a20d55aSWarner Losh 							ingroup = 1;
1652cfec5adSRuslan Ermilov 							break;
1662cfec5adSRuslan Ermilov 						}
1672cfec5adSRuslan Ermilov 					}
1682cfec5adSRuslan Ermilov 				}
1696a20d55aSWarner Losh 			}
1706a20d55aSWarner Losh 			if (ingroup == 0)
1716a20d55aSWarner Losh 				continue;
1726a20d55aSWarner Losh 		}
173bd76376fSEd Schouten 		if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
174752d887aSPhilippe Charnier 			warnx("%s", p);
1759b50d902SRodney W. Grimes 	}
1769b50d902SRodney W. Grimes 	exit(0);
1779b50d902SRodney W. Grimes }
1789b50d902SRodney W. Grimes 
179752d887aSPhilippe Charnier static void
180645e7f1bSEd Schouten usage(void)
181752d887aSPhilippe Charnier {
182003ff943SWarner Losh 	(void)fprintf(stderr, "usage: wall [-g group] [file]\n");
183752d887aSPhilippe Charnier 	exit(1);
184752d887aSPhilippe Charnier }
185752d887aSPhilippe Charnier 
1869b50d902SRodney W. Grimes void
1876a20d55aSWarner Losh makemsg(char *fname)
1889b50d902SRodney W. Grimes {
1896a20d55aSWarner Losh 	int cnt;
190586fbee6SGleb Smirnoff 	wchar_t ch;
1919b50d902SRodney W. Grimes 	struct tm *lt;
1929b50d902SRodney W. Grimes 	struct passwd *pw;
1939b50d902SRodney W. Grimes 	struct stat sbuf;
19458011702SAndrey A. Chernov 	time_t now;
1959b50d902SRodney W. Grimes 	FILE *fp;
1969b50d902SRodney W. Grimes 	int fd;
197586fbee6SGleb Smirnoff 	char hostname[MAXHOSTNAMELEN], tmpname[64];
198586fbee6SGleb Smirnoff 	wchar_t *p, *tmp, lbuf[256], codebuf[13];
199075f2939SMark Murray 	const char *tty;
20056e7ae90SKris Kennaway 	const char *whom;
201a47f98edSKris Kennaway 	gid_t egid;
2029b50d902SRodney W. Grimes 
20356e7ae90SKris Kennaway 	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
20456e7ae90SKris Kennaway 	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
20556e7ae90SKris Kennaway 		err(1, "can't open temporary file");
2069b50d902SRodney W. Grimes 	(void)unlink(tmpname);
2079b50d902SRodney W. Grimes 
2089b50d902SRodney W. Grimes 	if (!nobanner) {
20956e7ae90SKris Kennaway 		tty = ttyname(STDERR_FILENO);
21056e7ae90SKris Kennaway 		if (tty == NULL)
21180af0816SNick Hibma 			tty = "no tty";
21280af0816SNick Hibma 
2139b50d902SRodney W. Grimes 		if (!(whom = getlogin()))
2149b50d902SRodney W. Grimes 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
2159b50d902SRodney W. Grimes 		(void)gethostname(hostname, sizeof(hostname));
2169b50d902SRodney W. Grimes 		(void)time(&now);
2179b50d902SRodney W. Grimes 		lt = localtime(&now);
2189b50d902SRodney W. Grimes 
2199b50d902SRodney W. Grimes 		/*
2209b50d902SRodney W. Grimes 		 * all this stuff is to blank out a square for the message;
2219b50d902SRodney W. Grimes 		 * we wrap message lines at column 79, not 80, because some
2229b50d902SRodney W. Grimes 		 * terminals wrap after 79, some do not, and we can't tell.
2239b50d902SRodney W. Grimes 		 * Which means that we may leave a non-blank character
2249b50d902SRodney W. Grimes 		 * in column 80, but that can't be helped.
2259b50d902SRodney W. Grimes 		 */
226586fbee6SGleb Smirnoff 		(void)fwprintf(fp, L"\r%79s\r\n", " ");
227586fbee6SGleb Smirnoff 		(void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t),
228586fbee6SGleb Smirnoff 		    L"Broadcast Message from %s@%s",
2299b50d902SRodney W. Grimes 		    whom, hostname);
230586fbee6SGleb Smirnoff 		(void)fwprintf(fp, L"%-79.79S\007\007\r\n", lbuf);
231586fbee6SGleb Smirnoff 		(void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t),
232586fbee6SGleb Smirnoff 		    L"        (%s) at %d:%02d %s...", tty,
2336e5d42b9SJeroen Ruigrok van der Werven 		    lt->tm_hour, lt->tm_min, lt->tm_zone);
234586fbee6SGleb Smirnoff 		(void)fwprintf(fp, L"%-79.79S\r\n", lbuf);
2359b50d902SRodney W. Grimes 	}
236586fbee6SGleb Smirnoff 	(void)fwprintf(fp, L"%79s\r\n", " ");
2379b50d902SRodney W. Grimes 
238a47f98edSKris Kennaway 	if (fname) {
239a47f98edSKris Kennaway 		egid = getegid();
240a47f98edSKris Kennaway 		setegid(getgid());
241a47f98edSKris Kennaway 		if (freopen(fname, "r", stdin) == NULL)
24256e7ae90SKris Kennaway 			err(1, "can't read %s", fname);
243*50e04779SEitan Adler 		if (setegid(egid) != 0)
244*50e04779SEitan Adler 			err(1, "setegid failed");
245a47f98edSKris Kennaway 	}
246b0023655SDavid E. O'Brien 	cnt = 0;
247586fbee6SGleb Smirnoff 	while (fgetws(lbuf, sizeof(lbuf)/sizeof(wchar_t), stdin)) {
248586fbee6SGleb Smirnoff 		for (p = lbuf; (ch = *p) != L'\0'; ++p, ++cnt) {
249586fbee6SGleb Smirnoff 			if (ch == L'\r') {
250586fbee6SGleb Smirnoff 				putwc(L'\r', fp);
251c87eca8bSDaniel Baker 				cnt = 0;
2523c58f6ddSDavid Schultz 				continue;
253586fbee6SGleb Smirnoff 			} else if (ch == L'\n') {
2549b50d902SRodney W. Grimes 				for (; cnt < 79; ++cnt)
255586fbee6SGleb Smirnoff 					putwc(L' ', fp);
256586fbee6SGleb Smirnoff 				putwc(L'\r', fp);
257586fbee6SGleb Smirnoff 				putwc(L'\n', fp);
2583c58f6ddSDavid Schultz 				break;
2593c58f6ddSDavid Schultz 			}
2603c58f6ddSDavid Schultz 			if (cnt == 79) {
261586fbee6SGleb Smirnoff 				putwc(L'\r', fp);
262586fbee6SGleb Smirnoff 				putwc(L'\n', fp);
2639b50d902SRodney W. Grimes 				cnt = 0;
2643c58f6ddSDavid Schultz 			}
265586fbee6SGleb Smirnoff 			if (iswprint(ch) || iswspace(ch) || ch == L'\a' || ch == L'\b') {
266586fbee6SGleb Smirnoff 				putwc(ch, fp);
267586fbee6SGleb Smirnoff 			} else {
268586fbee6SGleb Smirnoff 				(void)swprintf(codebuf, sizeof(codebuf)/sizeof(wchar_t), L"<0x%X>", ch);
269586fbee6SGleb Smirnoff 				for (tmp = codebuf; *tmp != L'\0'; ++tmp) {
270586fbee6SGleb Smirnoff 					putwc(*tmp, fp);
27158011702SAndrey A. Chernov 					if (++cnt == 79) {
272586fbee6SGleb Smirnoff 						putwc(L'\r', fp);
273586fbee6SGleb Smirnoff 						putwc(L'\n', fp);
27458011702SAndrey A. Chernov 						cnt = 0;
27558011702SAndrey A. Chernov 					}
27658011702SAndrey A. Chernov 				}
277586fbee6SGleb Smirnoff 				--cnt;
27858011702SAndrey A. Chernov 			}
27958011702SAndrey A. Chernov 		}
2803c58f6ddSDavid Schultz 	}
281586fbee6SGleb Smirnoff 	(void)fwprintf(fp, L"%79s\r\n", " ");
2829b50d902SRodney W. Grimes 	rewind(fp);
2839b50d902SRodney W. Grimes 
284752d887aSPhilippe Charnier 	if (fstat(fd, &sbuf))
28556e7ae90SKris Kennaway 		err(1, "can't stat temporary file");
2869b50d902SRodney W. Grimes 	mbufsize = sbuf.st_size;
287752d887aSPhilippe Charnier 	if (!(mbuf = malloc((u_int)mbufsize)))
28856e7ae90SKris Kennaway 		err(1, "out of memory");
289075f2939SMark Murray 	if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
29056e7ae90SKris Kennaway 		err(1, "can't read temporary file");
2919b50d902SRodney W. Grimes 	(void)close(fd);
2929b50d902SRodney W. Grimes }
293