Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / wall / wall.c
blob70ada5536d34812a8f3b3ba56d01d73b3892342a
1 /* $NetBSD: wall.c,v 1.27 2009/02/15 06:06:55 dholland 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.27 2009/02/15 06:06:55 dholland 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 void addgroup(char *);
71 void makemsg(const char *);
72 int main(int, char **);
73 void usage(void);
75 struct wallgroup {
76 gid_t gid;
77 char *name;
78 char **mem;
79 struct wallgroup *next;
80 } *grouplist;
82 int nobanner;
83 size_t mbufsize;
84 char *mbuf;
86 /* ARGSUSED */
87 int
88 main(int argc, char **argv)
90 int ch;
91 struct iovec iov;
92 char *p, **mem;
93 struct utmpentry *ep;
94 gid_t egid;
95 struct wallgroup *wg;
96 struct passwd *pw;
98 setprogname(argv[0]);
99 egid = getegid();
100 if (setegid(getgid()) == -1)
101 err(1, "setegid");
102 pw = getpwnam("nobody");
104 (void)check_sender(NULL, getuid(), egid);
106 while ((ch = getopt(argc, argv, "g:n")) != -1)
107 switch (ch) {
108 case 'n':
109 /* undoc option for shutdown: suppress banner */
110 if (geteuid() == 0 || (pw && getuid() == pw->pw_uid))
111 nobanner = 1;
112 break;
113 case 'g':
114 addgroup(optarg);
115 break;
116 case '?':
117 default:
118 usage();
120 argc -= optind;
121 argv += optind;
122 if (argc > 1)
123 usage();
125 makemsg(*argv);
127 iov.iov_base = mbuf;
128 iov.iov_len = mbufsize;
129 (void)getutentries(NULL, &ep);
130 (void)setegid(egid);
131 for (; ep; ep = ep->next) {
132 if (grouplist) {
133 int ingroup;
135 ingroup = 0;
136 pw = getpwnam(ep->name);
137 if (!pw)
138 continue;
139 for (wg = grouplist; wg && !ingroup; wg = wg->next) {
140 if (wg->gid == pw->pw_gid)
141 ingroup = 1;
142 for (mem = wg->mem; *mem && !ingroup; mem++)
143 if (strcmp(ep->name, *mem) == 0)
144 ingroup = 1;
146 if (ingroup == 0)
147 continue;
150 /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
151 if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1]))
152 continue;
154 if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
155 warnx("%s", p);
157 exit(0);
160 void
161 addgroup(char *name)
163 int i;
164 struct group *grp;
165 struct wallgroup *g;
167 grp = getgrnam(name);
168 if ((grp = getgrnam(name)) == NULL)
169 errx(1, "unknown group `%s'", name);
170 for (i = 0; grp->gr_mem[i]; i++)
171 continue;
173 g = (struct wallgroup *)malloc(sizeof *g);
174 if (g == NULL)
175 err(1, "malloc");
176 g->gid = grp->gr_gid;
177 g->name = name;
178 g->mem = (char **)malloc(i + 1);
179 if (g->mem == NULL)
180 err(1, "malloc");
181 for (i = 0; grp->gr_mem[i] != NULL; i++) {
182 g->mem[i] = strdup(grp->gr_mem[i]);
183 if (g->mem[i] == NULL)
184 err(1, "malloc");
186 g->mem[i] = NULL;
187 g->next = grouplist;
188 grouplist = g;
191 void
192 makemsg(const char *fname)
194 int ch, cnt;
195 struct tm *lt;
196 struct passwd *pw;
197 struct stat sbuf;
198 time_t now;
199 FILE *fp;
200 int fd;
201 const char *whom, *tty;
202 char *p, tmpname[MAXPATHLEN], lbuf[100],
203 hostname[MAXHOSTNAMELEN+1];
205 (void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
206 if ((fd = mkstemp(tmpname)) == -1)
207 err(1, "can't open temporary file");
208 (void)unlink(tmpname);
209 if (!(fp = fdopen(fd, "r+")))
210 err(1, "can't open temporary file");
212 if (!nobanner) {
213 if (!(whom = getlogin()))
214 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
215 (void)gethostname(hostname, sizeof(hostname));
216 hostname[sizeof(hostname) - 1] = '\0';
217 (void)time(&now);
218 lt = localtime(&now);
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.
227 (void)fprintf(fp, "\r%79s\r\n", " ");
228 (void)snprintf(lbuf, sizeof lbuf,
229 "Broadcast Message from %s@%s", whom, hostname);
230 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
231 tty = ttyname(STDERR_FILENO);
232 if (tty == NULL)
233 tty = "??";
234 (void)snprintf(lbuf, sizeof lbuf,
235 " (%s) at %d:%02d %s...", tty,
236 lt->tm_hour, lt->tm_min, lt->tm_zone);
237 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
239 (void)fprintf(fp, "%79s\r\n", " ");
241 if (fname && !(freopen(fname, "r", stdin)))
242 err(1, "can't read %s", fname);
243 while (fgets(lbuf, sizeof(lbuf), stdin))
244 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
245 if (cnt == 79 || ch == '\n') {
246 for (; cnt < 79; ++cnt)
247 putc(' ', fp);
248 putc('\r', fp);
249 putc('\n', fp);
250 cnt = -1;
252 if (ch != '\n')
253 putc(ch, fp);
255 (void)fprintf(fp, "%79s\r\n", " ");
256 rewind(fp);
258 if (fstat(fd, &sbuf))
259 err(1, "can't stat temporary file");
260 if ((uint64_t)sbuf.st_size > SIZE_T_MAX)
261 errx(1, "file too big");
262 mbufsize = sbuf.st_size;
263 if (!(mbuf = malloc(mbufsize)))
264 err(1, "malloc");
265 if (fread(mbuf, 1, mbufsize, fp) != mbufsize)
266 err(1, "can't read temporary file");
267 (void)fclose(fp);
270 void
271 usage(void)
274 (void)fprintf(stderr, "usage: %s [-g group] [file]\n", getprogname());
275 exit(1);