xref: /dragonfly/usr.bin/wall/wall.c (revision 6e5c5008)
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  * 3. 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  * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)wall.c	8.2 (Berkeley) 11/16/93
31  * $FreeBSD: src/usr.bin/wall/wall.c,v 1.13.2.6 2001/10/18 08:08:17 des Exp $
32  * $DragonFly: src/usr.bin/wall/wall.c,v 1.4 2005/08/31 16:27:53 liamfoy Exp $
33  */
34 
35 /*
36  * This program is not related to David Wall, whose Stanford Ph.D. thesis
37  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
38  */
39 
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/uio.h>
43 
44 #include <ctype.h>
45 #include <err.h>
46 #include <grp.h>
47 #include <locale.h>
48 #include <paths.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55 #include "utmpentry.h"
56 
57 #include "ttymsg.h"
58 
59 static void makemsg(char *);
60 static void usage(void);
61 
62 struct wallgroup {
63 	struct wallgroup *next;
64 	char		*name;
65 	gid_t		gid;
66 } *grouplist;
67 int nobanner;
68 int mbufsize;
69 char *mbuf;
70 
71 int
72 main(int argc, char *argv[])
73 {
74 	struct iovec iov;
75 	struct utmpentry *ep = NULL;	/* avoid gcc warnings */
76 	int ch;
77 	int ingroup;
78 	struct wallgroup *g;
79 	struct group *grp;
80 	char **np;
81 	const char *p;
82 	struct passwd *pw;
83 
84 	(void)setlocale(LC_CTYPE, "");
85 
86 	while ((ch = getopt(argc, argv, "g:n")) != -1)
87 		switch (ch) {
88 		case 'n':
89 			/* undoc option for shutdown: suppress banner */
90 			if (geteuid() == 0)
91 				nobanner = 1;
92 			break;
93 		case 'g':
94 			g = (struct wallgroup *)malloc(sizeof *g);
95 			g->next = grouplist;
96 			g->name = optarg;
97 			g->gid = -1;
98 			grouplist = g;
99 			break;
100 		case '?':
101 		default:
102 			usage();
103 		}
104 	argc -= optind;
105 	argv += optind;
106 	if (argc > 1)
107 		usage();
108 
109 	for (g = grouplist; g; g = g->next) {
110 		grp = getgrnam(g->name);
111 		if (grp != NULL)
112 			g->gid = grp->gr_gid;
113 		else
114 			warnx("%s: no such group", g->name);
115 	}
116 
117 	makemsg(*argv);
118 
119 	iov.iov_base = mbuf;
120 	iov.iov_len = mbufsize;
121 
122 	getutentries(NULL, &ep);
123 	/* NOSTRICT */
124 	for (; ep; ep = ep->next) {
125 		if (grouplist) {
126 			ingroup = 0;
127 			pw = getpwnam(ep->name);
128 			if (!pw)
129 				continue;
130 			for (g = grouplist; g && ingroup == 0; g = g->next) {
131 				if (g->gid == (gid_t)-1)
132 					continue;
133 				if (g->gid == pw->pw_gid)
134 					ingroup = 1;
135 				else if ((grp = getgrgid(g->gid)) != NULL) {
136 					for (np = grp->gr_mem; *np; np++) {
137 						if (strcmp(*np, ep->name) == 0) {
138 							ingroup = 1;
139 							break;
140 						}
141 					}
142 				}
143 			}
144 			if (ingroup == 0)
145 				continue;
146 		}
147 		/* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
148 		if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1]))
149 			continue;
150 
151 		if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
152 			warnx("%s", p);
153 	}
154 	exit(0);
155 }
156 
157 static void
158 usage(void)
159 {
160 	(void)fprintf(stderr, "usage: wall [-g group] [file]\n");
161 	exit(1);
162 }
163 
164 void
165 makemsg(char *fname)
166 {
167 	int cnt;
168 	unsigned char ch;
169 	struct tm *lt;
170 	struct passwd *pw;
171 	struct stat sbuf;
172 	time_t now;
173 	FILE *fp;
174 	int fd;
175 	char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[MAXPATHLEN];
176 	const char *tty;
177 	const char *whom;
178 	gid_t egid;
179 
180 	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
181 	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
182 		err(1, "can't open temporary file");
183 	(void)unlink(tmpname);
184 
185 	if (!nobanner) {
186 		tty = ttyname(STDERR_FILENO);
187 		if (tty == NULL)
188 			tty = "no tty";
189 
190 		if (!(whom = getlogin()))
191 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
192 		(void)gethostname(hostname, sizeof(hostname));
193 		(void)time(&now);
194 		lt = localtime(&now);
195 
196 		/*
197 		 * all this stuff is to blank out a square for the message;
198 		 * we wrap message lines at column 79, not 80, because some
199 		 * terminals wrap after 79, some do not, and we can't tell.
200 		 * Which means that we may leave a non-blank character
201 		 * in column 80, but that can't be helped.
202 		 */
203 		(void)fprintf(fp, "\r%79s\r\n", " ");
204 		(void)snprintf(lbuf, sizeof(lbuf),
205 		    "Broadcast Message from %s@%s",
206 		    whom, hostname);
207 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
208 		(void)snprintf(lbuf, sizeof(lbuf),
209 		    "        (%s) at %d:%02d %s...", tty,
210 		    lt->tm_hour, lt->tm_min, lt->tm_zone);
211 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
212 	}
213 	(void)fprintf(fp, "%79s\r\n", " ");
214 
215 	if (fname) {
216 		egid = getegid();
217 		setegid(getgid());
218 	       	if (freopen(fname, "r", stdin) == NULL)
219 			err(1, "can't read %s", fname);
220 		setegid(egid);
221 	}
222 	while (fgets(lbuf, sizeof(lbuf), stdin))
223 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
224 			if (ch == '\r') {
225 				cnt = 0;
226 			} else if (cnt == 79 || ch == '\n') {
227 				for (; cnt < 79; ++cnt)
228 					putc(' ', fp);
229 				putc('\r', fp);
230 				putc('\n', fp);
231 				cnt = 0;
232 			} else if (((ch & 0x80) && ch < 0xA0) ||
233 				   /* disable upper controls */
234 				   (!isprint(ch) && !isspace(ch) &&
235 				    ch != '\a' && ch != '\b')
236 				  ) {
237 				if (ch & 0x80) {
238 					ch &= 0x7F;
239 					putc('M', fp);
240 					if (++cnt == 79) {
241 						putc('\r', fp);
242 						putc('\n', fp);
243 						cnt = 0;
244 					}
245 					putc('-', fp);
246 					if (++cnt == 79) {
247 						putc('\r', fp);
248 						putc('\n', fp);
249 						cnt = 0;
250 					}
251 				}
252 				if (iscntrl(ch)) {
253 					ch ^= 040;
254 					putc('^', fp);
255 					if (++cnt == 79) {
256 						putc('\r', fp);
257 						putc('\n', fp);
258 						cnt = 0;
259 					}
260 				}
261 				putc(ch, fp);
262 			} else {
263 				putc(ch, fp);
264 			}
265 		}
266 	(void)fprintf(fp, "%79s\r\n", " ");
267 	rewind(fp);
268 
269 	if (fstat(fd, &sbuf))
270 		err(1, "can't stat temporary file");
271 	mbufsize = sbuf.st_size;
272 	if (!(mbuf = malloc((u_int)mbufsize)))
273 		err(1, "out of memory");
274 	if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
275 		err(1, "can't read temporary file");
276 	(void)close(fd);
277 }
278