xref: /netbsd-src/usr.bin/wall/wall.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: wall.c,v 1.26 2008/07/21 14:19:27 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
41 #endif
42 __RCSID("$NetBSD: wall.c,v 1.26 2008/07/21 14:19:27 lukem Exp $");
43 #endif /* not lint */
44 
45 /*
46  * This program is not related to David Wall, whose Stanford Ph.D. thesis
47  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
48  */
49 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/uio.h>
54 
55 #include <err.h>
56 #include <grp.h>
57 #include <errno.h>
58 #include <paths.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <util.h>
65 
66 #include "utmpentry.h"
67 #include "term_chk.h"
68 
69 void	addgroup(char *);
70 void	makemsg(const char *);
71 int	main(int, char **);
72 void	usage(void);
73 
74 struct wallgroup {
75 	gid_t	gid;
76 	char	*name;
77 	char	**mem;
78 	struct wallgroup *next;
79 } *grouplist;
80 
81 int nobanner;
82 size_t mbufsize;
83 char *mbuf;
84 
85 /* ARGSUSED */
86 int
87 main(int argc, char **argv)
88 {
89 	int ch;
90 	struct iovec iov;
91 	char *p, **mem;
92 	struct utmpentry *ep;
93 	gid_t egid;
94 	struct wallgroup *wg;
95 	struct passwd *pw;
96 
97 	setprogname(argv[0]);
98 	egid = getegid();
99 	if (setegid(getgid()) == -1)
100 		err(1, "setegid");
101 	pw = getpwnam("nobody");
102 
103 	(void)check_sender(NULL, getuid(), egid);
104 
105 	while ((ch = getopt(argc, argv, "g:n")) != -1)
106 		switch (ch) {
107 		case 'n':
108 			/* undoc option for shutdown: suppress banner */
109 			if (geteuid() == 0 || (pw && getuid() == pw->pw_uid))
110 				nobanner = 1;
111 			break;
112 		case 'g':
113 			addgroup(optarg);
114 			break;
115 		case '?':
116 		default:
117 			usage();
118 		}
119 	argc -= optind;
120 	argv += optind;
121 	if (argc > 1)
122 		usage();
123 
124 	makemsg(*argv);
125 
126 	iov.iov_base = mbuf;
127 	iov.iov_len = mbufsize;
128 	(void)getutentries(NULL, &ep);
129 	(void)setegid(egid);
130 	for (; ep; ep = ep->next) {
131 		if (grouplist) {
132 			int ingroup;
133 
134 			ingroup = 0;
135 			pw = getpwnam(ep->name);
136 			if (!pw)
137 				continue;
138 			for (wg = grouplist; wg && !ingroup; wg = wg->next) {
139 				if (wg->gid == pw->pw_gid)
140 					ingroup = 1;
141 				for (mem = wg->mem; *mem && !ingroup; mem++)
142 					if (strcmp(ep->name, *mem) == 0)
143 						ingroup = 1;
144 			}
145 			if (ingroup == 0)
146 				continue;
147 		}
148 		if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
149 			warnx("%s", p);
150 	}
151 	exit(0);
152 }
153 
154 void
155 addgroup(char *name)
156 {
157 	int i;
158 	struct group *grp;
159 	struct wallgroup *g;
160 
161 	grp = getgrnam(name);
162 	if ((grp = getgrnam(name)) == NULL)
163 		errx(1, "unknown group `%s'", name);
164 	for (i = 0; grp->gr_mem[i]; i++)
165 		continue;
166 
167 	g = (struct wallgroup *)malloc(sizeof *g);
168 	if (g == NULL)
169 		err(1, "malloc");
170 	g->gid = grp->gr_gid;
171 	g->name = name;
172 	g->mem = (char **)malloc(i + 1);
173 	if (g->mem == NULL)
174 		err(1, "malloc");
175 	for (i = 0; grp->gr_mem[i] != NULL; i++) {
176 		g->mem[i] = strdup(grp->gr_mem[i]);
177 		if (g->mem[i] == NULL)
178 			err(1, "malloc");
179 	}
180 	g->mem[i] = NULL;
181 	g->next = grouplist;
182 	grouplist = g;
183 }
184 
185 void
186 makemsg(const char *fname)
187 {
188 	int ch, cnt;
189 	struct tm *lt;
190 	struct passwd *pw;
191 	struct stat sbuf;
192 	time_t now;
193 	FILE *fp;
194 	int fd;
195 	const char *whom;
196 	char *p, *tty, tmpname[MAXPATHLEN], lbuf[100],
197 	    hostname[MAXHOSTNAMELEN+1];
198 
199 	(void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
200 	if ((fd = mkstemp(tmpname)) == -1)
201 		err(1, "can't open temporary file");
202 	(void)unlink(tmpname);
203 	if (!(fp = fdopen(fd, "r+")))
204 		err(1, "can't open temporary file");
205 
206 	if (!nobanner) {
207 		if (!(whom = getlogin()))
208 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
209 		(void)gethostname(hostname, sizeof(hostname));
210 		hostname[sizeof(hostname) - 1] = '\0';
211 		(void)time(&now);
212 		lt = localtime(&now);
213 
214 		/*
215 		 * all this stuff is to blank out a square for the message;
216 		 * we wrap message lines at column 79, not 80, because some
217 		 * terminals wrap after 79, some do not, and we can't tell.
218 		 * Which means that we may leave a non-blank character
219 		 * in column 80, but that can't be helped.
220 		 */
221 		(void)fprintf(fp, "\r%79s\r\n", " ");
222 		(void)snprintf(lbuf, sizeof lbuf,
223 		    "Broadcast Message from %s@%s", whom, hostname);
224 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
225 		tty = ttyname(STDERR_FILENO);
226 		if (tty == NULL)
227 			tty = "??";
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 && !(freopen(fname, "r", stdin)))
236 		err(1, "can't read %s", fname);
237 	while (fgets(lbuf, sizeof(lbuf), stdin))
238 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
239 			if (cnt == 79 || ch == '\n') {
240 				for (; cnt < 79; ++cnt)
241 					putc(' ', fp);
242 				putc('\r', fp);
243 				putc('\n', fp);
244 				cnt = -1;
245 			}
246 			if (ch != '\n')
247 				putc(ch, fp);
248 		}
249 	(void)fprintf(fp, "%79s\r\n", " ");
250 	rewind(fp);
251 
252 	if (fstat(fd, &sbuf))
253 		err(1, "can't stat temporary file");
254 	if (sbuf.st_size > SIZE_T_MAX)
255 		errx(1, "file too big");
256 	mbufsize = sbuf.st_size;
257 	if (!(mbuf = malloc(mbufsize)))
258 		err(1, "malloc");
259 	if (fread(mbuf, 1, mbufsize, fp) != mbufsize)
260 		err(1, "can't read temporary file");
261 	(void)fclose(fp);
262 }
263 
264 void
265 usage(void)
266 {
267 
268 	(void)fprintf(stderr, "usage: %s [-g group] [file]\n", getprogname());
269 	exit(1);
270 }
271