Sync with manuals from netbsd-8 branch.
[minix3.git] / usr.bin / wall / wall.c
blob636a01c15eb6624ec0ec9818f64868ea9824e286
1 /* $NetBSD: wall.c,v 1.29 2011/09/06 18:45:21 joerg Exp $ */
3 /*
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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 */
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.29 2011/09/06 18:45:21 joerg Exp $");
43 #endif /* not lint */
46 * This program is not related to David Wall, whose Stanford Ph.D. thesis
47 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/uio.h>
55 #include <ctype.h>
56 #include <err.h>
57 #include <grp.h>
58 #include <errno.h>
59 #include <paths.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <util.h>
67 #include "utmpentry.h"
68 #include "term_chk.h"
70 static void addgroup(char *);
71 static void makemsg(const char *);
72 __dead static void usage(void);
74 static struct wallgroup {
75 gid_t gid;
76 char *name;
77 char **mem;
78 struct wallgroup *next;
79 } *grouplist;
81 static int nobanner;
82 static size_t mbufsize;
83 static char *mbuf;
85 /* ARGSUSED */
86 int
87 main(int argc, char **argv)
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;
97 setprogname(argv[0]);
98 egid = getegid();
99 if (setegid(getgid()) == -1)
100 err(1, "setegid");
101 pw = getpwnam("nobody");
103 (void)check_sender(NULL, getuid(), egid);
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();
119 argc -= optind;
120 argv += optind;
121 if (argc > 1)
122 usage();
124 makemsg(*argv);
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;
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;
145 if (ingroup == 0)
146 continue;
149 /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
150 if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1]))
151 continue;
153 if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
154 warnx("%s", p);
156 exit(0);
159 static void
160 addgroup(char *name)
162 int i;
163 struct group *grp;
164 struct wallgroup *g;
166 grp = getgrnam(name);
167 if ((grp = getgrnam(name)) == NULL)
168 errx(1, "unknown group `%s'", name);
169 for (i = 0; grp->gr_mem[i]; i++)
170 continue;
172 g = (struct wallgroup *)malloc(sizeof *g);
173 if (g == NULL)
174 err(1, "malloc");
175 g->gid = grp->gr_gid;
176 g->name = name;
177 g->mem = (char **)malloc(i + 1);
178 if (g->mem == NULL)
179 err(1, "malloc");
180 for (i = 0; grp->gr_mem[i] != NULL; i++) {
181 g->mem[i] = strdup(grp->gr_mem[i]);
182 if (g->mem[i] == NULL)
183 err(1, "malloc");
185 g->mem[i] = NULL;
186 g->next = grouplist;
187 grouplist = g;
190 static void
191 makemsg(const char *fname)
193 int ch, cnt;
194 struct tm *lt;
195 struct passwd *pw;
196 struct stat sbuf;
197 time_t now;
198 FILE *fp;
199 int fd;
200 const char *whom, *tty;
201 char *p, tmpname[MAXPATHLEN], lbuf[100],
202 hostname[MAXHOSTNAMELEN+1];
204 (void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
205 if ((fd = mkstemp(tmpname)) == -1)
206 err(1, "can't open temporary file");
207 (void)unlink(tmpname);
208 if (!(fp = fdopen(fd, "r+")))
209 err(1, "can't open temporary file");
211 if (!nobanner) {
212 if (!(whom = getlogin()))
213 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
214 (void)gethostname(hostname, sizeof(hostname));
215 hostname[sizeof(hostname) - 1] = '\0';
216 (void)time(&now);
217 lt = localtime(&now);
220 * all this stuff is to blank out a square for the message;
221 * we wrap message lines at column 79, not 80, because some
222 * terminals wrap after 79, some do not, and we can't tell.
223 * Which means that we may leave a non-blank character
224 * in column 80, but that can't be helped.
226 (void)fprintf(fp, "\r%79s\r\n", " ");
227 (void)snprintf(lbuf, sizeof lbuf,
228 "Broadcast Message from %s@%s", whom, hostname);
229 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
230 tty = ttyname(STDERR_FILENO);
231 if (tty == NULL)
232 tty = "??";
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);
238 (void)fprintf(fp, "%79s\r\n", " ");
240 if (fname && !(freopen(fname, "r", stdin)))
241 err(1, "can't read %s", fname);
242 while (fgets(lbuf, sizeof(lbuf), stdin))
243 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
244 if (cnt == 79 || ch == '\n') {
245 for (; cnt < 79; ++cnt)
246 putc(' ', fp);
247 putc('\r', fp);
248 putc('\n', fp);
249 cnt = -1;
251 if (ch != '\n')
252 putc(ch, fp);
254 (void)fprintf(fp, "%79s\r\n", " ");
255 rewind(fp);
257 if (fstat(fd, &sbuf))
258 err(1, "can't stat temporary file");
259 if ((uint64_t)sbuf.st_size > SIZE_T_MAX)
260 errx(1, "file too big");
261 mbufsize = sbuf.st_size;
262 if (!(mbuf = malloc(mbufsize)))
263 err(1, "malloc");
264 if (fread(mbuf, 1, mbufsize, fp) != mbufsize)
265 err(1, "can't read temporary file");
266 (void)fclose(fp);
269 static void
270 usage(void)
273 (void)fprintf(stderr, "usage: %s [-g group] [file]\n", getprogname());
274 exit(1);