xref: /freebsd/usr.bin/wall/wall.c (revision bd76376f801ea53ac8456ff38bf34725a39494ac)
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  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
34075f2939SMark Murray #include <sys/cdefs.h>
35075f2939SMark Murray 
36075f2939SMark Murray __FBSDID("$FreeBSD$");
37075f2939SMark Murray 
389b50d902SRodney W. Grimes #ifndef lint
39752d887aSPhilippe Charnier static const char copyright[] =
409b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1990, 1993\n\
419b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
42075f2939SMark Murray #endif
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #ifndef lint
45075f2939SMark Murray static const char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
46752d887aSPhilippe Charnier #endif
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes /*
499b50d902SRodney W. Grimes  * This program is not related to David Wall, whose Stanford Ph.D. thesis
509b50d902SRodney W. Grimes  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
519b50d902SRodney W. Grimes  */
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include <sys/param.h>
549b50d902SRodney W. Grimes #include <sys/stat.h>
559b50d902SRodney W. Grimes #include <sys/uio.h>
569b50d902SRodney W. Grimes 
57752d887aSPhilippe Charnier #include <ctype.h>
58752d887aSPhilippe Charnier #include <err.h>
596a20d55aSWarner Losh #include <grp.h>
609566348dSAndrey A. Chernov #include <locale.h>
619b50d902SRodney W. Grimes #include <paths.h>
629b50d902SRodney W. Grimes #include <pwd.h>
639b50d902SRodney W. Grimes #include <stdio.h>
649b50d902SRodney W. Grimes #include <stdlib.h>
659b50d902SRodney W. Grimes #include <string.h>
6658011702SAndrey A. Chernov #include <time.h>
67bd76376fSEd Schouten #define	_ULOG_POSIX_NAMES
68bd76376fSEd Schouten #include <ulog.h>
699b50d902SRodney W. Grimes #include <unistd.h>
709b50d902SRodney W. Grimes 
71728d043eSDima Dorfman #include "ttymsg.h"
72728d043eSDima Dorfman 
736a20d55aSWarner Losh static void makemsg(char *);
746a20d55aSWarner Losh static void usage(void);
759b50d902SRodney W. Grimes 
766a20d55aSWarner Losh struct wallgroup {
776a20d55aSWarner Losh 	struct wallgroup *next;
786a20d55aSWarner Losh 	char		*name;
796a20d55aSWarner Losh 	gid_t		gid;
806a20d55aSWarner Losh } *grouplist;
819b50d902SRodney W. Grimes int nobanner;
829b50d902SRodney W. Grimes int mbufsize;
839b50d902SRodney W. Grimes char *mbuf;
849b50d902SRodney W. Grimes 
85b457a3e1SOlivier Houchard static int
86bd76376fSEd Schouten ttystat(char *line)
87b457a3e1SOlivier Houchard {
88b457a3e1SOlivier Houchard 	struct stat sb;
89b457a3e1SOlivier Houchard 	char ttybuf[MAXPATHLEN];
90b457a3e1SOlivier Houchard 
91bd76376fSEd Schouten 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
92b457a3e1SOlivier Houchard 	if (stat(ttybuf, &sb) == 0) {
93b457a3e1SOlivier Houchard 		return (0);
94b457a3e1SOlivier Houchard 	} else
95b457a3e1SOlivier Houchard 		return (-1);
96b457a3e1SOlivier Houchard }
97b457a3e1SOlivier Houchard 
989b50d902SRodney W. Grimes int
996a20d55aSWarner Losh main(int argc, char *argv[])
1009b50d902SRodney W. Grimes {
1019b50d902SRodney W. Grimes 	struct iovec iov;
102bd76376fSEd Schouten 	struct utmpx *utmp;
1036a20d55aSWarner Losh 	int ch;
1042cfec5adSRuslan Ermilov 	int ingroup;
1056a20d55aSWarner Losh 	struct wallgroup *g;
1066a20d55aSWarner Losh 	struct group *grp;
107728d043eSDima Dorfman 	char **np;
108728d043eSDima Dorfman 	const char *p;
1096a20d55aSWarner Losh 	struct passwd *pw;
1109b50d902SRodney W. Grimes 
1119566348dSAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
1129566348dSAndrey A. Chernov 
1136a20d55aSWarner Losh 	while ((ch = getopt(argc, argv, "g:n")) != -1)
1149b50d902SRodney W. Grimes 		switch (ch) {
1159b50d902SRodney W. Grimes 		case 'n':
1169b50d902SRodney W. Grimes 			/* undoc option for shutdown: suppress banner */
1179b50d902SRodney W. Grimes 			if (geteuid() == 0)
1189b50d902SRodney W. Grimes 				nobanner = 1;
1199b50d902SRodney W. Grimes 			break;
1206a20d55aSWarner Losh 		case 'g':
1216a20d55aSWarner Losh 			g = (struct wallgroup *)malloc(sizeof *g);
1226a20d55aSWarner Losh 			g->next = grouplist;
1236a20d55aSWarner Losh 			g->name = optarg;
1246a20d55aSWarner Losh 			g->gid = -1;
1256a20d55aSWarner Losh 			grouplist = g;
1266a20d55aSWarner Losh 			break;
1279b50d902SRodney W. Grimes 		case '?':
1289b50d902SRodney W. Grimes 		default:
129752d887aSPhilippe Charnier 			usage();
1309b50d902SRodney W. Grimes 		}
1319b50d902SRodney W. Grimes 	argc -= optind;
1329b50d902SRodney W. Grimes 	argv += optind;
1339b50d902SRodney W. Grimes 	if (argc > 1)
134752d887aSPhilippe Charnier 		usage();
1359b50d902SRodney W. Grimes 
1366a20d55aSWarner Losh 	for (g = grouplist; g; g = g->next) {
1376a20d55aSWarner Losh 		grp = getgrnam(g->name);
138003ff943SWarner Losh 		if (grp != NULL)
1396a20d55aSWarner Losh 			g->gid = grp->gr_gid;
140003ff943SWarner Losh 		else
141003ff943SWarner Losh 			warnx("%s: no such group", g->name);
1426a20d55aSWarner Losh 	}
1436a20d55aSWarner Losh 
1449b50d902SRodney W. Grimes 	makemsg(*argv);
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes 	iov.iov_base = mbuf;
1479b50d902SRodney W. Grimes 	iov.iov_len = mbufsize;
1489b50d902SRodney W. Grimes 	/* NOSTRICT */
149bd76376fSEd Schouten 	while ((utmp = getutxent()) != NULL) {
150bd76376fSEd Schouten 		if (utmp->ut_type != USER_PROCESS)
1519b50d902SRodney W. Grimes 			continue;
152bd76376fSEd Schouten 		if (ttystat(utmp->ut_line) != 0)
153b457a3e1SOlivier Houchard 			continue;
1546a20d55aSWarner Losh 		if (grouplist) {
1552cfec5adSRuslan Ermilov 			ingroup = 0;
156bd76376fSEd Schouten 			pw = getpwnam(utmp->ut_user);
1576a20d55aSWarner Losh 			if (!pw)
1586a20d55aSWarner Losh 				continue;
1596a20d55aSWarner Losh 			for (g = grouplist; g && ingroup == 0; g = g->next) {
160075f2939SMark Murray 				if (g->gid == (gid_t)-1)
1616a20d55aSWarner Losh 					continue;
1626a20d55aSWarner Losh 				if (g->gid == pw->pw_gid)
1636a20d55aSWarner Losh 					ingroup = 1;
1642cfec5adSRuslan Ermilov 				else if ((grp = getgrgid(g->gid)) != NULL) {
1652cfec5adSRuslan Ermilov 					for (np = grp->gr_mem; *np; np++) {
166bd76376fSEd Schouten 						if (strcmp(*np, utmp->ut_user) == 0) {
1676a20d55aSWarner Losh 							ingroup = 1;
1682cfec5adSRuslan Ermilov 							break;
1692cfec5adSRuslan Ermilov 						}
1702cfec5adSRuslan Ermilov 					}
1712cfec5adSRuslan Ermilov 				}
1726a20d55aSWarner Losh 			}
1736a20d55aSWarner Losh 			if (ingroup == 0)
1746a20d55aSWarner Losh 				continue;
1756a20d55aSWarner Losh 		}
176bd76376fSEd Schouten 		if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
177752d887aSPhilippe Charnier 			warnx("%s", p);
1789b50d902SRodney W. Grimes 	}
1799b50d902SRodney W. Grimes 	exit(0);
1809b50d902SRodney W. Grimes }
1819b50d902SRodney W. Grimes 
182752d887aSPhilippe Charnier static void
183752d887aSPhilippe Charnier usage()
184752d887aSPhilippe Charnier {
185003ff943SWarner Losh 	(void)fprintf(stderr, "usage: wall [-g group] [file]\n");
186752d887aSPhilippe Charnier 	exit(1);
187752d887aSPhilippe Charnier }
188752d887aSPhilippe Charnier 
1899b50d902SRodney W. Grimes void
1906a20d55aSWarner Losh makemsg(char *fname)
1919b50d902SRodney W. Grimes {
1926a20d55aSWarner Losh 	int cnt;
1936a20d55aSWarner Losh 	unsigned char ch;
1949b50d902SRodney W. Grimes 	struct tm *lt;
1959b50d902SRodney W. Grimes 	struct passwd *pw;
1969b50d902SRodney W. Grimes 	struct stat sbuf;
19758011702SAndrey A. Chernov 	time_t now;
1989b50d902SRodney W. Grimes 	FILE *fp;
1999b50d902SRodney W. Grimes 	int fd;
200075f2939SMark Murray 	char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
201075f2939SMark Murray 	const char *tty;
20256e7ae90SKris Kennaway 	const char *whom;
203a47f98edSKris Kennaway 	gid_t egid;
2049b50d902SRodney W. Grimes 
20556e7ae90SKris Kennaway 	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
20656e7ae90SKris Kennaway 	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
20756e7ae90SKris Kennaway 		err(1, "can't open temporary file");
2089b50d902SRodney W. Grimes 	(void)unlink(tmpname);
2099b50d902SRodney W. Grimes 
2109b50d902SRodney W. Grimes 	if (!nobanner) {
21156e7ae90SKris Kennaway 		tty = ttyname(STDERR_FILENO);
21256e7ae90SKris Kennaway 		if (tty == NULL)
21380af0816SNick Hibma 			tty = "no tty";
21480af0816SNick Hibma 
2159b50d902SRodney W. Grimes 		if (!(whom = getlogin()))
2169b50d902SRodney W. Grimes 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
2179b50d902SRodney W. Grimes 		(void)gethostname(hostname, sizeof(hostname));
2189b50d902SRodney W. Grimes 		(void)time(&now);
2199b50d902SRodney W. Grimes 		lt = localtime(&now);
2209b50d902SRodney W. Grimes 
2219b50d902SRodney W. Grimes 		/*
2229b50d902SRodney W. Grimes 		 * all this stuff is to blank out a square for the message;
2239b50d902SRodney W. Grimes 		 * we wrap message lines at column 79, not 80, because some
2249b50d902SRodney W. Grimes 		 * terminals wrap after 79, some do not, and we can't tell.
2259b50d902SRodney W. Grimes 		 * Which means that we may leave a non-blank character
2269b50d902SRodney W. Grimes 		 * in column 80, but that can't be helped.
2279b50d902SRodney W. Grimes 		 */
2289b50d902SRodney W. Grimes 		(void)fprintf(fp, "\r%79s\r\n", " ");
22998df703fSMatthew Dillon 		(void)snprintf(lbuf, sizeof(lbuf),
23098df703fSMatthew Dillon 		    "Broadcast Message from %s@%s",
2319b50d902SRodney W. Grimes 		    whom, hostname);
2329b50d902SRodney W. Grimes 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
23398df703fSMatthew Dillon 		(void)snprintf(lbuf, sizeof(lbuf),
23480af0816SNick Hibma 		    "        (%s) at %d:%02d %s...", tty,
2356e5d42b9SJeroen Ruigrok van der Werven 		    lt->tm_hour, lt->tm_min, lt->tm_zone);
2369b50d902SRodney W. Grimes 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
2379b50d902SRodney W. Grimes 	}
2389b50d902SRodney W. Grimes 	(void)fprintf(fp, "%79s\r\n", " ");
2399b50d902SRodney W. Grimes 
240a47f98edSKris Kennaway 	if (fname) {
241a47f98edSKris Kennaway 		egid = getegid();
242a47f98edSKris Kennaway 		setegid(getgid());
243a47f98edSKris Kennaway 	       	if (freopen(fname, "r", stdin) == NULL)
24456e7ae90SKris Kennaway 			err(1, "can't read %s", fname);
245a47f98edSKris Kennaway 		setegid(egid);
246a47f98edSKris Kennaway 	}
2473c58f6ddSDavid Schultz 	while (fgets(lbuf, sizeof(lbuf), stdin)) {
2489b50d902SRodney W. Grimes 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
249c87eca8bSDaniel Baker 			if (ch == '\r') {
2503c58f6ddSDavid Schultz 				putc('\r', fp);
251c87eca8bSDaniel Baker 				cnt = 0;
2523c58f6ddSDavid Schultz 				continue;
2533c58f6ddSDavid Schultz 			} else if (ch == '\n') {
2549b50d902SRodney W. Grimes 				for (; cnt < 79; ++cnt)
2559b50d902SRodney W. Grimes 					putc(' ', fp);
2569b50d902SRodney W. Grimes 				putc('\r', fp);
2579b50d902SRodney W. Grimes 				putc('\n', fp);
2583c58f6ddSDavid Schultz 				break;
2593c58f6ddSDavid Schultz 			}
2603c58f6ddSDavid Schultz 			if (cnt == 79) {
2613c58f6ddSDavid Schultz 				putc('\r', fp);
2623c58f6ddSDavid Schultz 				putc('\n', fp);
2639b50d902SRodney W. Grimes 				cnt = 0;
2643c58f6ddSDavid Schultz 			}
2653c58f6ddSDavid Schultz 			if (((ch & 0x80) && ch < 0xA0) ||
26658011702SAndrey A. Chernov 				   /* disable upper controls */
26758011702SAndrey A. Chernov 				   (!isprint(ch) && !isspace(ch) &&
26858011702SAndrey A. Chernov 				    ch != '\a' && ch != '\b')
269fc092135SAndrey A. Chernov 				  ) {
270fc092135SAndrey A. Chernov 				if (ch & 0x80) {
271fc092135SAndrey A. Chernov 					ch &= 0x7F;
27258011702SAndrey A. Chernov 					putc('M', fp);
27358011702SAndrey A. Chernov 					if (++cnt == 79) {
27458011702SAndrey A. Chernov 						putc('\r', fp);
27558011702SAndrey A. Chernov 						putc('\n', fp);
27658011702SAndrey A. Chernov 						cnt = 0;
277fc092135SAndrey A. Chernov 					}
27858011702SAndrey A. Chernov 					putc('-', fp);
27958011702SAndrey A. Chernov 					if (++cnt == 79) {
28058011702SAndrey A. Chernov 						putc('\r', fp);
28158011702SAndrey A. Chernov 						putc('\n', fp);
28258011702SAndrey A. Chernov 						cnt = 0;
28358011702SAndrey A. Chernov 					}
28458011702SAndrey A. Chernov 				}
28558011702SAndrey A. Chernov 				if (iscntrl(ch)) {
28658011702SAndrey A. Chernov 					ch ^= 040;
287acc879c1SGuido van Rooij 					putc('^', fp);
28858011702SAndrey A. Chernov 					if (++cnt == 79) {
28958011702SAndrey A. Chernov 						putc('\r', fp);
29058011702SAndrey A. Chernov 						putc('\n', fp);
29158011702SAndrey A. Chernov 						cnt = 0;
29258011702SAndrey A. Chernov 					}
29358011702SAndrey A. Chernov 				}
2943c58f6ddSDavid Schultz 			}
2959b50d902SRodney W. Grimes 			putc(ch, fp);
2969b50d902SRodney W. Grimes 		}
297b4d6ab01SDaniel Baker 	}
2989b50d902SRodney W. Grimes 	(void)fprintf(fp, "%79s\r\n", " ");
2999b50d902SRodney W. Grimes 	rewind(fp);
3009b50d902SRodney W. Grimes 
301752d887aSPhilippe Charnier 	if (fstat(fd, &sbuf))
30256e7ae90SKris Kennaway 		err(1, "can't stat temporary file");
3039b50d902SRodney W. Grimes 	mbufsize = sbuf.st_size;
304752d887aSPhilippe Charnier 	if (!(mbuf = malloc((u_int)mbufsize)))
30556e7ae90SKris Kennaway 		err(1, "out of memory");
306075f2939SMark Murray 	if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
30756e7ae90SKris Kennaway 		err(1, "can't read temporary file");
3089b50d902SRodney W. Grimes 	(void)close(fd);
3099b50d902SRodney W. Grimes }
310