xref: /netbsd/usr.bin/wall/wall.c (revision 245f9c45)
1*245f9c45Schristos /*	$NetBSD: wall.c,v 1.30 2015/11/21 14:59:51 christos Exp $	*/
2a8bfd3d1Sjtc 
361f28255Scgd /*
4a8bfd3d1Sjtc  * Copyright (c) 1988, 1990, 1993
5a8bfd3d1Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32c897d730Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
3598e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
39a8bfd3d1Sjtc #if 0
40a8bfd3d1Sjtc static char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
41a8bfd3d1Sjtc #endif
42*245f9c45Schristos __RCSID("$NetBSD: wall.c,v 1.30 2015/11/21 14:59:51 christos Exp $");
4361f28255Scgd #endif /* not lint */
4461f28255Scgd 
4561f28255Scgd /*
4661f28255Scgd  * This program is not related to David Wall, whose Stanford Ph.D. thesis
4761f28255Scgd  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
4861f28255Scgd  */
4961f28255Scgd 
5061f28255Scgd #include <sys/param.h>
5161f28255Scgd #include <sys/stat.h>
5261f28255Scgd #include <sys/time.h>
5361f28255Scgd #include <sys/uio.h>
54a8bfd3d1Sjtc 
55606b73cdSdholland #include <ctype.h>
56a6f32a1dSlukem #include <err.h>
57b6a63b16Slukem #include <grp.h>
582abe3770Sitojun #include <errno.h>
59a8bfd3d1Sjtc #include <paths.h>
6061f28255Scgd #include <pwd.h>
6161f28255Scgd #include <stdio.h>
6261f28255Scgd #include <stdlib.h>
63f7c6bf57Sjtc #include <string.h>
64f7c6bf57Sjtc #include <unistd.h>
65c897d730Schristos #include <util.h>
66a8bfd3d1Sjtc 
67c020d65eSchristos #include "utmpentry.h"
6845aed1fbSchristos #include "term_chk.h"
69c020d65eSchristos 
708618a988Sjoerg static void	addgroup(char *);
71*245f9c45Schristos static void	makemsg(struct iovec *, const char *, int);
728618a988Sjoerg __dead static void	usage(void);
73b6a63b16Slukem 
748618a988Sjoerg static struct wallgroup {
75b6a63b16Slukem 	gid_t	gid;
76b6a63b16Slukem 	char	*name;
77b6a63b16Slukem 	char	**mem;
78b6a63b16Slukem 	struct wallgroup *next;
79b6a63b16Slukem } *grouplist;
8061f28255Scgd 
81f7c6bf57Sjtc int
main(int argc,char ** argv)82c5775359Smjl main(int argc, char **argv)
8361f28255Scgd {
8461f28255Scgd 	int ch;
8561f28255Scgd 	struct iovec iov;
86b6a63b16Slukem 	char *p, **mem;
87c020d65eSchristos 	struct utmpentry *ep;
882abe3770Sitojun 	gid_t egid;
89b6a63b16Slukem 	struct wallgroup *wg;
90b6a63b16Slukem 	struct passwd *pw;
91*245f9c45Schristos 	int nobanner;
922abe3770Sitojun 
93b6a63b16Slukem 	setprogname(argv[0]);
942abe3770Sitojun 	egid = getegid();
952abe3770Sitojun 	if (setegid(getgid()) == -1)
96*245f9c45Schristos 		err(EXIT_FAILURE, "setegid");
97b6a63b16Slukem 	pw = getpwnam("nobody");
98*245f9c45Schristos 	if (pw == NULL)
99*245f9c45Schristos 		errx(EXIT_FAILURE, "Can't find passwd entry for `nobody'");
10061f28255Scgd 
101fc23d32cSchristos 	(void)check_sender(NULL, getuid(), egid);
10245aed1fbSchristos 
103*245f9c45Schristos 	nobanner = 0;
104b6a63b16Slukem 	while ((ch = getopt(argc, argv, "g:n")) != -1)
10561f28255Scgd 		switch (ch) {
10661f28255Scgd 		case 'n':
10761f28255Scgd 			/* undoc option for shutdown: suppress banner */
108b6a63b16Slukem 			if (geteuid() == 0 || (pw && getuid() == pw->pw_uid))
10961f28255Scgd 				nobanner = 1;
11061f28255Scgd 			break;
111b6a63b16Slukem 		case 'g':
112b6a63b16Slukem 			addgroup(optarg);
113b6a63b16Slukem 			break;
11461f28255Scgd 		case '?':
11561f28255Scgd 		default:
116b6a63b16Slukem 			usage();
11761f28255Scgd 		}
11861f28255Scgd 	argc -= optind;
11961f28255Scgd 	argv += optind;
12061f28255Scgd 	if (argc > 1)
121b6a63b16Slukem 		usage();
12261f28255Scgd 
123*245f9c45Schristos 	makemsg(&iov, *argv, nobanner);
12461f28255Scgd 
125c020d65eSchristos 	(void)getutentries(NULL, &ep);
1262abe3770Sitojun 	(void)setegid(egid);
127b6a63b16Slukem 	for (; ep; ep = ep->next) {
128b6a63b16Slukem 		if (grouplist) {
129b6a63b16Slukem 			int ingroup;
130b6a63b16Slukem 
131b6a63b16Slukem 			ingroup = 0;
132b6a63b16Slukem 			pw = getpwnam(ep->name);
133b6a63b16Slukem 			if (!pw)
134b6a63b16Slukem 				continue;
135b6a63b16Slukem 			for (wg = grouplist; wg && !ingroup; wg = wg->next) {
136b6a63b16Slukem 				if (wg->gid == pw->pw_gid)
137b6a63b16Slukem 					ingroup = 1;
138b6a63b16Slukem 				for (mem = wg->mem; *mem && !ingroup; mem++)
139b6a63b16Slukem 					if (strcmp(ep->name, *mem) == 0)
140b6a63b16Slukem 						ingroup = 1;
141b6a63b16Slukem 			}
142b6a63b16Slukem 			if (ingroup == 0)
143b6a63b16Slukem 				continue;
144b6a63b16Slukem 		}
145606b73cdSdholland 
146606b73cdSdholland 		/* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
147606b73cdSdholland 		if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1]))
148606b73cdSdholland 			continue;
149606b73cdSdholland 
150c020d65eSchristos 		if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
151a6f32a1dSlukem 			warnx("%s", p);
152b6a63b16Slukem 	}
153*245f9c45Schristos 	return EXIT_SUCCESS;
15461f28255Scgd }
15561f28255Scgd 
1568618a988Sjoerg static void
addgroup(char * name)157b6a63b16Slukem addgroup(char *name)
158b6a63b16Slukem {
159*245f9c45Schristos 	size_t i;
160b6a63b16Slukem 	struct group *grp;
161b6a63b16Slukem 	struct wallgroup *g;
162b6a63b16Slukem 
163aff92a77Slukem 	grp = getgrnam(name);
164aff92a77Slukem 	if ((grp = getgrnam(name)) == NULL)
165*245f9c45Schristos 		errx(EXIT_FAILURE, "unknown group `%s'", name);
166b6a63b16Slukem 	for (i = 0; grp->gr_mem[i]; i++)
167b6a63b16Slukem 		continue;
168b6a63b16Slukem 
169*245f9c45Schristos 	g = malloc(sizeof(*g));
170b6a63b16Slukem 	if (g == NULL)
171*245f9c45Schristos 		err(EXIT_FAILURE, "malloc");
172b6a63b16Slukem 	g->gid = grp->gr_gid;
173b6a63b16Slukem 	g->name = name;
174*245f9c45Schristos 	g->mem = calloc(i + 1, sizeof(*g->mem));
175b6a63b16Slukem 	if (g->mem == NULL)
176*245f9c45Schristos 		err(EXIT_FAILURE, "calloc");
177b6a63b16Slukem 	for (i = 0; grp->gr_mem[i] != NULL; i++) {
178b6a63b16Slukem 		g->mem[i] = strdup(grp->gr_mem[i]);
179b6a63b16Slukem 		if (g->mem[i] == NULL)
180*245f9c45Schristos 			err(EXIT_FAILURE, "strdup");
181b6a63b16Slukem 	}
182b6a63b16Slukem 	g->mem[i] = NULL;
183b6a63b16Slukem 	g->next = grouplist;
184b6a63b16Slukem 	grouplist = g;
185b6a63b16Slukem }
186b6a63b16Slukem 
1878618a988Sjoerg static void
makebanner(FILE * fp)188*245f9c45Schristos makebanner(FILE *fp)
18961f28255Scgd {
190*245f9c45Schristos 	const char *whom, *tty;
191*245f9c45Schristos 	char hostname[MAXHOSTNAMELEN + 1], lbuf[100];
192*245f9c45Schristos 	time_t now;
19361f28255Scgd 	struct tm *lt;
19461f28255Scgd 	struct passwd *pw;
19561f28255Scgd 
19661f28255Scgd 	if (!(whom = getlogin()))
19761f28255Scgd 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
19861f28255Scgd 	(void)gethostname(hostname, sizeof(hostname));
199ed666575Smrg 	hostname[sizeof(hostname) - 1] = '\0';
20061f28255Scgd 	(void)time(&now);
20161f28255Scgd 	lt = localtime(&now);
20261f28255Scgd 
20361f28255Scgd 	/*
20461f28255Scgd 	 * all this stuff is to blank out a square for the message;
20561f28255Scgd 	 * we wrap message lines at column 79, not 80, because some
20661f28255Scgd 	 * terminals wrap after 79, some do not, and we can't tell.
20761f28255Scgd 	 * Which means that we may leave a non-blank character
20861f28255Scgd 	 * in column 80, but that can't be helped.
20961f28255Scgd 	 */
21061f28255Scgd 	(void)fprintf(fp, "\r%79s\r\n", " ");
21123768a9eSmrg 	(void)snprintf(lbuf, sizeof lbuf,
21223768a9eSmrg 	    "Broadcast Message from %s@%s", whom, hostname);
21361f28255Scgd 	(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
21499ebb803Staca 	tty = ttyname(STDERR_FILENO);
21599ebb803Staca 	if (tty == NULL)
21620117cd1Staca 		tty = "??";
217*245f9c45Schristos 	(void)snprintf(lbuf, sizeof lbuf, "        (%s) at %d:%02d %s...", tty,
218b6a63b16Slukem 	    lt->tm_hour, lt->tm_min, lt->tm_zone);
21961f28255Scgd 	(void)fprintf(fp, "%-79.79s\r\n", lbuf);
22061f28255Scgd }
221*245f9c45Schristos 
222*245f9c45Schristos static void
makemsg(struct iovec * iov,const char * fname,int nobanner)223*245f9c45Schristos makemsg(struct iovec *iov, const char *fname, int nobanner)
224*245f9c45Schristos {
225*245f9c45Schristos 	int ch, cnt;
226*245f9c45Schristos 	struct stat sbuf;
227*245f9c45Schristos 	FILE *fp;
228*245f9c45Schristos 	int fd;
229*245f9c45Schristos 	char *p, tmpname[MAXPATHLEN], lbuf[100];
230*245f9c45Schristos 	size_t mbufsize;
231*245f9c45Schristos 	char *mbuf;
232*245f9c45Schristos 
233*245f9c45Schristos 	(void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
234*245f9c45Schristos 	if ((fd = mkstemp(tmpname)) == -1)
235*245f9c45Schristos 		err(EXIT_FAILURE, "can't open temporary file");
236*245f9c45Schristos 	(void)unlink(tmpname);
237*245f9c45Schristos 	if (!(fp = fdopen(fd, "r+")))
238*245f9c45Schristos 		err(EXIT_FAILURE, "can't open temporary file");
239*245f9c45Schristos 
240*245f9c45Schristos 	if (!nobanner)
241*245f9c45Schristos 		makebanner(fp);
242*245f9c45Schristos 
24361f28255Scgd 	(void)fprintf(fp, "%79s\r\n", " ");
24461f28255Scgd 
245a6f32a1dSlukem 	if (fname && !(freopen(fname, "r", stdin)))
246*245f9c45Schristos 		err(EXIT_FAILURE, "can't read %s", fname);
247*245f9c45Schristos 
24861f28255Scgd 	while (fgets(lbuf, sizeof(lbuf), stdin))
249a8bfd3d1Sjtc 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
25061f28255Scgd 			if (cnt == 79 || ch == '\n') {
25161f28255Scgd 				for (; cnt < 79; ++cnt)
25261f28255Scgd 					putc(' ', fp);
25361f28255Scgd 				putc('\r', fp);
25461f28255Scgd 				putc('\n', fp);
255f90eda1cSmycroft 				cnt = -1;
256f90eda1cSmycroft 			}
257f90eda1cSmycroft 			if (ch != '\n')
25861f28255Scgd 				putc(ch, fp);
25961f28255Scgd 		}
26061f28255Scgd 	(void)fprintf(fp, "%79s\r\n", " ");
26161f28255Scgd 	rewind(fp);
26261f28255Scgd 
263*245f9c45Schristos 	if (fstat(fd, &sbuf) == -1)
264*245f9c45Schristos 		err(EXIT_FAILURE, "can't stat temporary file");
265a4b11de1Slukem 	if ((uint64_t)sbuf.st_size > SIZE_T_MAX)
266*245f9c45Schristos 		errx(EXIT_FAILURE, "file too big");
267*245f9c45Schristos 	mbufsize = (size_t)sbuf.st_size;
2682abe3770Sitojun 	if (!(mbuf = malloc(mbufsize)))
269*245f9c45Schristos 		err(EXIT_FAILURE, "malloc");
2702abe3770Sitojun 	if (fread(mbuf, 1, mbufsize, fp) != mbufsize)
271*245f9c45Schristos 		err(EXIT_FAILURE, "can't read temporary file");
2722abe3770Sitojun 	(void)fclose(fp);
273*245f9c45Schristos 	iov->iov_base = mbuf;
274*245f9c45Schristos 	iov->iov_len = mbufsize;
27561f28255Scgd }
276b6a63b16Slukem 
2778618a988Sjoerg static void
usage(void)278b6a63b16Slukem usage(void)
279b6a63b16Slukem {
280b6a63b16Slukem 
281*245f9c45Schristos 	(void)fprintf(stderr, "Usage: %s [-g group] [file]\n", getprogname());
282*245f9c45Schristos 	exit(EXIT_FAILURE);
283b6a63b16Slukem }
284