Deleted Added
full compact
wall.c (201224) wall.c (202200)
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35
36__FBSDID("$FreeBSD: head/usr.bin/wall/wall.c 201224 2009-12-29 22:32:43Z ed $");
36__FBSDID("$FreeBSD: head/usr.bin/wall/wall.c 202200 2010-01-13 18:09:54Z ed $");
37
38#ifndef lint
39static const char copyright[] =
40"@(#) Copyright (c) 1988, 1990, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
42#endif
43
44#ifndef lint
45static const char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93";
46#endif
47
48/*
49 * This program is not related to David Wall, whose Stanford Ph.D. thesis
50 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
51 */
52
53#include <sys/param.h>
54#include <sys/stat.h>
55#include <sys/uio.h>
56
57#include <ctype.h>
58#include <err.h>
59#include <grp.h>
60#include <locale.h>
61#include <paths.h>
62#include <pwd.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
37
38#ifndef lint
39static const char copyright[] =
40"@(#) Copyright (c) 1988, 1990, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
42#endif
43
44#ifndef lint
45static const char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93";
46#endif
47
48/*
49 * This program is not related to David Wall, whose Stanford Ph.D. thesis
50 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
51 */
52
53#include <sys/param.h>
54#include <sys/stat.h>
55#include <sys/uio.h>
56
57#include <ctype.h>
58#include <err.h>
59#include <grp.h>
60#include <locale.h>
61#include <paths.h>
62#include <pwd.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
67#define _ULOG_POSIX_NAMES
68#include <ulog.h>
69#include <unistd.h>
67#include <unistd.h>
68#include <utmpx.h>
70
71#include "ttymsg.h"
72
73static void makemsg(char *);
74static void usage(void);
75
76struct wallgroup {
77 struct wallgroup *next;
78 char *name;
79 gid_t gid;
80} *grouplist;
81int nobanner;
82int mbufsize;
83char *mbuf;
84
85static int
86ttystat(char *line)
87{
88 struct stat sb;
89 char ttybuf[MAXPATHLEN];
90
91 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
92 if (stat(ttybuf, &sb) == 0) {
93 return (0);
94 } else
95 return (-1);
96}
97
98int
99main(int argc, char *argv[])
100{
101 struct iovec iov;
102 struct utmpx *utmp;
103 int ch;
104 int ingroup;
105 struct wallgroup *g;
106 struct group *grp;
107 char **np;
108 const char *p;
109 struct passwd *pw;
110
111 (void)setlocale(LC_CTYPE, "");
112
113 while ((ch = getopt(argc, argv, "g:n")) != -1)
114 switch (ch) {
115 case 'n':
116 /* undoc option for shutdown: suppress banner */
117 if (geteuid() == 0)
118 nobanner = 1;
119 break;
120 case 'g':
121 g = (struct wallgroup *)malloc(sizeof *g);
122 g->next = grouplist;
123 g->name = optarg;
124 g->gid = -1;
125 grouplist = g;
126 break;
127 case '?':
128 default:
129 usage();
130 }
131 argc -= optind;
132 argv += optind;
133 if (argc > 1)
134 usage();
135
136 for (g = grouplist; g; g = g->next) {
137 grp = getgrnam(g->name);
138 if (grp != NULL)
139 g->gid = grp->gr_gid;
140 else
141 warnx("%s: no such group", g->name);
142 }
143
144 makemsg(*argv);
145
146 iov.iov_base = mbuf;
147 iov.iov_len = mbufsize;
148 /* NOSTRICT */
149 while ((utmp = getutxent()) != NULL) {
150 if (utmp->ut_type != USER_PROCESS)
151 continue;
152 if (ttystat(utmp->ut_line) != 0)
153 continue;
154 if (grouplist) {
155 ingroup = 0;
156 pw = getpwnam(utmp->ut_user);
157 if (!pw)
158 continue;
159 for (g = grouplist; g && ingroup == 0; g = g->next) {
160 if (g->gid == (gid_t)-1)
161 continue;
162 if (g->gid == pw->pw_gid)
163 ingroup = 1;
164 else if ((grp = getgrgid(g->gid)) != NULL) {
165 for (np = grp->gr_mem; *np; np++) {
166 if (strcmp(*np, utmp->ut_user) == 0) {
167 ingroup = 1;
168 break;
169 }
170 }
171 }
172 }
173 if (ingroup == 0)
174 continue;
175 }
176 if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
177 warnx("%s", p);
178 }
179 exit(0);
180}
181
182static void
183usage(void)
184{
185 (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
186 exit(1);
187}
188
189void
190makemsg(char *fname)
191{
192 int cnt;
193 unsigned char ch;
194 struct tm *lt;
195 struct passwd *pw;
196 struct stat sbuf;
197 time_t now;
198 FILE *fp;
199 int fd;
200 char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
201 const char *tty;
202 const char *whom;
203 gid_t egid;
204
205 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
206 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
207 err(1, "can't open temporary file");
208 (void)unlink(tmpname);
209
210 if (!nobanner) {
211 tty = ttyname(STDERR_FILENO);
212 if (tty == NULL)
213 tty = "no tty";
214
215 if (!(whom = getlogin()))
216 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
217 (void)gethostname(hostname, sizeof(hostname));
218 (void)time(&now);
219 lt = localtime(&now);
220
221 /*
222 * all this stuff is to blank out a square for the message;
223 * we wrap message lines at column 79, not 80, because some
224 * terminals wrap after 79, some do not, and we can't tell.
225 * Which means that we may leave a non-blank character
226 * in column 80, but that can't be helped.
227 */
228 (void)fprintf(fp, "\r%79s\r\n", " ");
229 (void)snprintf(lbuf, sizeof(lbuf),
230 "Broadcast Message from %s@%s",
231 whom, hostname);
232 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
233 (void)snprintf(lbuf, sizeof(lbuf),
234 " (%s) at %d:%02d %s...", tty,
235 lt->tm_hour, lt->tm_min, lt->tm_zone);
236 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
237 }
238 (void)fprintf(fp, "%79s\r\n", " ");
239
240 if (fname) {
241 egid = getegid();
242 setegid(getgid());
243 if (freopen(fname, "r", stdin) == NULL)
244 err(1, "can't read %s", fname);
245 setegid(egid);
246 }
247 while (fgets(lbuf, sizeof(lbuf), stdin)) {
248 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
249 if (ch == '\r') {
250 putc('\r', fp);
251 cnt = 0;
252 continue;
253 } else if (ch == '\n') {
254 for (; cnt < 79; ++cnt)
255 putc(' ', fp);
256 putc('\r', fp);
257 putc('\n', fp);
258 break;
259 }
260 if (cnt == 79) {
261 putc('\r', fp);
262 putc('\n', fp);
263 cnt = 0;
264 }
265 if (((ch & 0x80) && ch < 0xA0) ||
266 /* disable upper controls */
267 (!isprint(ch) && !isspace(ch) &&
268 ch != '\a' && ch != '\b')
269 ) {
270 if (ch & 0x80) {
271 ch &= 0x7F;
272 putc('M', fp);
273 if (++cnt == 79) {
274 putc('\r', fp);
275 putc('\n', fp);
276 cnt = 0;
277 }
278 putc('-', fp);
279 if (++cnt == 79) {
280 putc('\r', fp);
281 putc('\n', fp);
282 cnt = 0;
283 }
284 }
285 if (iscntrl(ch)) {
286 ch ^= 040;
287 putc('^', fp);
288 if (++cnt == 79) {
289 putc('\r', fp);
290 putc('\n', fp);
291 cnt = 0;
292 }
293 }
294 }
295 putc(ch, fp);
296 }
297 }
298 (void)fprintf(fp, "%79s\r\n", " ");
299 rewind(fp);
300
301 if (fstat(fd, &sbuf))
302 err(1, "can't stat temporary file");
303 mbufsize = sbuf.st_size;
304 if (!(mbuf = malloc((u_int)mbufsize)))
305 err(1, "out of memory");
306 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
307 err(1, "can't read temporary file");
308 (void)close(fd);
309}
69
70#include "ttymsg.h"
71
72static void makemsg(char *);
73static void usage(void);
74
75struct wallgroup {
76 struct wallgroup *next;
77 char *name;
78 gid_t gid;
79} *grouplist;
80int nobanner;
81int mbufsize;
82char *mbuf;
83
84static int
85ttystat(char *line)
86{
87 struct stat sb;
88 char ttybuf[MAXPATHLEN];
89
90 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
91 if (stat(ttybuf, &sb) == 0) {
92 return (0);
93 } else
94 return (-1);
95}
96
97int
98main(int argc, char *argv[])
99{
100 struct iovec iov;
101 struct utmpx *utmp;
102 int ch;
103 int ingroup;
104 struct wallgroup *g;
105 struct group *grp;
106 char **np;
107 const char *p;
108 struct passwd *pw;
109
110 (void)setlocale(LC_CTYPE, "");
111
112 while ((ch = getopt(argc, argv, "g:n")) != -1)
113 switch (ch) {
114 case 'n':
115 /* undoc option for shutdown: suppress banner */
116 if (geteuid() == 0)
117 nobanner = 1;
118 break;
119 case 'g':
120 g = (struct wallgroup *)malloc(sizeof *g);
121 g->next = grouplist;
122 g->name = optarg;
123 g->gid = -1;
124 grouplist = g;
125 break;
126 case '?':
127 default:
128 usage();
129 }
130 argc -= optind;
131 argv += optind;
132 if (argc > 1)
133 usage();
134
135 for (g = grouplist; g; g = g->next) {
136 grp = getgrnam(g->name);
137 if (grp != NULL)
138 g->gid = grp->gr_gid;
139 else
140 warnx("%s: no such group", g->name);
141 }
142
143 makemsg(*argv);
144
145 iov.iov_base = mbuf;
146 iov.iov_len = mbufsize;
147 /* NOSTRICT */
148 while ((utmp = getutxent()) != NULL) {
149 if (utmp->ut_type != USER_PROCESS)
150 continue;
151 if (ttystat(utmp->ut_line) != 0)
152 continue;
153 if (grouplist) {
154 ingroup = 0;
155 pw = getpwnam(utmp->ut_user);
156 if (!pw)
157 continue;
158 for (g = grouplist; g && ingroup == 0; g = g->next) {
159 if (g->gid == (gid_t)-1)
160 continue;
161 if (g->gid == pw->pw_gid)
162 ingroup = 1;
163 else if ((grp = getgrgid(g->gid)) != NULL) {
164 for (np = grp->gr_mem; *np; np++) {
165 if (strcmp(*np, utmp->ut_user) == 0) {
166 ingroup = 1;
167 break;
168 }
169 }
170 }
171 }
172 if (ingroup == 0)
173 continue;
174 }
175 if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
176 warnx("%s", p);
177 }
178 exit(0);
179}
180
181static void
182usage(void)
183{
184 (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
185 exit(1);
186}
187
188void
189makemsg(char *fname)
190{
191 int cnt;
192 unsigned char ch;
193 struct tm *lt;
194 struct passwd *pw;
195 struct stat sbuf;
196 time_t now;
197 FILE *fp;
198 int fd;
199 char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
200 const char *tty;
201 const char *whom;
202 gid_t egid;
203
204 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
205 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
206 err(1, "can't open temporary file");
207 (void)unlink(tmpname);
208
209 if (!nobanner) {
210 tty = ttyname(STDERR_FILENO);
211 if (tty == NULL)
212 tty = "no tty";
213
214 if (!(whom = getlogin()))
215 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
216 (void)gethostname(hostname, sizeof(hostname));
217 (void)time(&now);
218 lt = localtime(&now);
219
220 /*
221 * all this stuff is to blank out a square for the message;
222 * we wrap message lines at column 79, not 80, because some
223 * terminals wrap after 79, some do not, and we can't tell.
224 * Which means that we may leave a non-blank character
225 * in column 80, but that can't be helped.
226 */
227 (void)fprintf(fp, "\r%79s\r\n", " ");
228 (void)snprintf(lbuf, sizeof(lbuf),
229 "Broadcast Message from %s@%s",
230 whom, hostname);
231 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
232 (void)snprintf(lbuf, sizeof(lbuf),
233 " (%s) at %d:%02d %s...", tty,
234 lt->tm_hour, lt->tm_min, lt->tm_zone);
235 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
236 }
237 (void)fprintf(fp, "%79s\r\n", " ");
238
239 if (fname) {
240 egid = getegid();
241 setegid(getgid());
242 if (freopen(fname, "r", stdin) == NULL)
243 err(1, "can't read %s", fname);
244 setegid(egid);
245 }
246 while (fgets(lbuf, sizeof(lbuf), stdin)) {
247 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
248 if (ch == '\r') {
249 putc('\r', fp);
250 cnt = 0;
251 continue;
252 } else if (ch == '\n') {
253 for (; cnt < 79; ++cnt)
254 putc(' ', fp);
255 putc('\r', fp);
256 putc('\n', fp);
257 break;
258 }
259 if (cnt == 79) {
260 putc('\r', fp);
261 putc('\n', fp);
262 cnt = 0;
263 }
264 if (((ch & 0x80) && ch < 0xA0) ||
265 /* disable upper controls */
266 (!isprint(ch) && !isspace(ch) &&
267 ch != '\a' && ch != '\b')
268 ) {
269 if (ch & 0x80) {
270 ch &= 0x7F;
271 putc('M', fp);
272 if (++cnt == 79) {
273 putc('\r', fp);
274 putc('\n', fp);
275 cnt = 0;
276 }
277 putc('-', fp);
278 if (++cnt == 79) {
279 putc('\r', fp);
280 putc('\n', fp);
281 cnt = 0;
282 }
283 }
284 if (iscntrl(ch)) {
285 ch ^= 040;
286 putc('^', fp);
287 if (++cnt == 79) {
288 putc('\r', fp);
289 putc('\n', fp);
290 cnt = 0;
291 }
292 }
293 }
294 putc(ch, fp);
295 }
296 }
297 (void)fprintf(fp, "%79s\r\n", " ");
298 rewind(fp);
299
300 if (fstat(fd, &sbuf))
301 err(1, "can't stat temporary file");
302 mbufsize = sbuf.st_size;
303 if (!(mbuf = malloc((u_int)mbufsize)))
304 err(1, "out of memory");
305 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
306 err(1, "can't read temporary file");
307 (void)close(fd);
308}