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