]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/wall.c
wall: remove unnecessary warning
[thirdparty/util-linux.git] / term-utils / 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 * Modified Sun Mar 12 10:34:34 1995, faith@cs.unc.edu, for Linux
34 */
35
36 /*
37 * This program is not related to David Wall, whose Stanford Ph.D. thesis
38 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
39 *
40 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
41 * - added Native Language Support
42 *
43 */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/uio.h>
49
50 #include <errno.h>
51 #include <paths.h>
52 #include <ctype.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include <utmpx.h>
60 #include <getopt.h>
61 #include <sys/types.h>
62 #include <grp.h>
63
64 #include "nls.h"
65 #include "xalloc.h"
66 #include "strutils.h"
67 #include "ttymsg.h"
68 #include "pathnames.h"
69 #include "carefulputc.h"
70 #include "c.h"
71 #include "fileutils.h"
72 #include "closestream.h"
73
74 #define TERM_WIDTH 79
75 #define WRITE_TIME_OUT 300 /* in seconds */
76
77 /* Function prototypes */
78 static char *makemsg(char *fname, char **mvec, int mvecsz,
79 size_t *mbufsize, int print_banner);
80
81 static void __attribute__((__noreturn__)) usage(void)
82 {
83 FILE *out = stdout;
84 fputs(USAGE_HEADER, out);
85 fprintf(out,
86 _(" %s [options] [<file> | <message>]\n"), program_invocation_short_name);
87
88 fputs(USAGE_SEPARATOR, out);
89 fputs(_("Write a message to all users.\n"), out);
90
91 fputs(USAGE_OPTIONS, out);
92 fputs(_(" -g, --group <group> only send message to group\n"), out);
93 fputs(_(" -n, --nobanner do not print banner, works only for root\n"), out);
94 fputs(_(" -t, --timeout <timeout> write timeout in seconds\n"), out);
95 fputs(USAGE_SEPARATOR, out);
96 printf(USAGE_HELP_OPTIONS(25));
97 printf(USAGE_MAN_TAIL("wall(1)"));
98
99 exit(EXIT_SUCCESS);
100 }
101
102 struct group_workspace {
103 gid_t requested_group;
104 int ngroups;
105
106 /* getgrouplist() on OSX takes int* not gid_t* */
107 #ifdef __APPLE__
108 int *groups;
109 #else
110 gid_t *groups;
111 #endif
112 };
113
114 static gid_t get_group_gid(const char *optarg)
115 {
116 struct group *gr;
117 gid_t gid;
118
119 if ((gr = getgrnam(optarg)))
120 return gr->gr_gid;
121
122 gid = strtou32_or_err(optarg, _("invalid group argument"));
123 if (!getgrgid(gid))
124 errx(EXIT_FAILURE, _("%s: unknown gid"), optarg);
125
126 return gid;
127 }
128
129 static struct group_workspace *init_group_workspace(const char *optarg)
130 {
131 struct group_workspace *buf = xmalloc(sizeof(struct group_workspace));
132
133 buf->requested_group = get_group_gid(optarg);
134 buf->ngroups = sysconf(_SC_NGROUPS_MAX) + 1; /* room for the primary gid */
135 buf->groups = xcalloc(sizeof(*buf->groups), buf->ngroups);
136
137 return buf;
138 }
139
140 static void free_group_workspace(struct group_workspace *buf)
141 {
142 if (!buf)
143 return;
144
145 free(buf->groups);
146 free(buf);
147 }
148
149 static int is_gr_member(const char *login, const struct group_workspace *buf)
150 {
151 struct passwd *pw;
152 int ngroups = buf->ngroups;
153 int rc;
154
155 pw = getpwnam(login);
156 if (!pw)
157 return 0;
158
159 if (buf->requested_group == pw->pw_gid)
160 return 1;
161
162 rc = getgrouplist(login, pw->pw_gid, buf->groups, &ngroups);
163 if (rc < 0) {
164 /* buffer too small, not sure how this can happen, since
165 we used sysconf to get the size... */
166 errx(EXIT_FAILURE,
167 _("getgrouplist found more groups than sysconf allows"));
168 }
169
170 for (; ngroups >= 0; --ngroups) {
171 if (buf->requested_group == (gid_t) buf->groups[ngroups])
172 return 1;
173 }
174
175 return 0;
176 }
177
178 int main(int argc, char **argv)
179 {
180 int ch;
181 struct iovec iov;
182 struct utmpx *utmpptr;
183 char *p;
184 char line[sizeof(utmpptr->ut_line) + 1];
185 int print_banner = TRUE;
186 struct group_workspace *group_buf = NULL;
187 char *mbuf, *fname = NULL;
188 size_t mbufsize;
189 unsigned timeout = WRITE_TIME_OUT;
190 char **mvec = NULL;
191 int mvecsz = 0;
192
193 static const struct option longopts[] = {
194 { "nobanner", no_argument, NULL, 'n' },
195 { "timeout", required_argument, NULL, 't' },
196 { "group", required_argument, NULL, 'g' },
197 { "version", no_argument, NULL, 'V' },
198 { "help", no_argument, NULL, 'h' },
199 { NULL, 0, NULL, 0 }
200 };
201
202 setlocale(LC_ALL, "");
203 bindtextdomain(PACKAGE, LOCALEDIR);
204 textdomain(PACKAGE);
205 atexit(close_stdout);
206
207 while ((ch = getopt_long(argc, argv, "nt:g:Vh", longopts, NULL)) != -1) {
208 switch (ch) {
209 case 'n':
210 if (geteuid() == 0)
211 print_banner = FALSE;
212 else
213 warnx(_("--nobanner is available only for root"));
214 break;
215 case 't':
216 timeout = strtou32_or_err(optarg, _("invalid timeout argument"));
217 if (timeout < 1)
218 errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg);
219 break;
220 case 'g':
221 group_buf = init_group_workspace(optarg);
222 break;
223 case 'V':
224 printf(UTIL_LINUX_VERSION);
225 exit(EXIT_SUCCESS);
226 case 'h':
227 usage();
228 default:
229 errtryhelp(EXIT_FAILURE);
230 }
231 }
232 argc -= optind;
233 argv += optind;
234
235 if (argc == 1 && access(argv[0], F_OK) == 0)
236 fname = argv[0];
237 else if (argc >= 1) {
238 mvec = argv;
239 mvecsz = argc;
240 }
241
242 mbuf = makemsg(fname, mvec, mvecsz, &mbufsize, print_banner);
243
244 iov.iov_base = mbuf;
245 iov.iov_len = mbufsize;
246 while((utmpptr = getutxent())) {
247 if (!utmpptr->ut_user[0])
248 continue;
249 #ifdef USER_PROCESS
250 if (utmpptr->ut_type != USER_PROCESS)
251 continue;
252 #endif
253 /* Joey Hess reports that use-sessreg in /etc/X11/wdm/
254 produces ut_line entries like :0, and a write
255 to /dev/:0 fails. */
256 if (utmpptr->ut_line[0] == ':')
257 continue;
258
259 if (group_buf && !is_gr_member(utmpptr->ut_user, group_buf))
260 continue;
261
262 mem2strcpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line), sizeof(line));
263 if ((p = ttymsg(&iov, 1, line, timeout)) != NULL)
264 warnx("%s", p);
265 }
266 endutxent();
267 free(mbuf);
268 free_group_workspace(group_buf);
269 exit(EXIT_SUCCESS);
270 }
271
272 struct buffer {
273 size_t sz;
274 size_t used;
275 char *data;
276 };
277
278 static void buf_enlarge(struct buffer *bs, size_t len)
279 {
280 if (bs->sz == 0 || len > bs->sz - bs->used) {
281 bs->sz += len < 128 ? 128 : len;
282 bs->data = xrealloc(bs->data, bs->sz);
283 }
284 }
285
286 static void buf_puts(struct buffer *bs, const char *s)
287 {
288 size_t len = strlen(s);
289
290 buf_enlarge(bs, len + 1);
291 memcpy(bs->data + bs->used, s, len + 1);
292 bs->used += len;
293 }
294
295 static void buf_printf(struct buffer *bs, const char *fmt, ...)
296 {
297 int rc;
298 va_list ap;
299 size_t limit;
300
301 buf_enlarge(bs, 0); /* default size */
302 limit = bs->sz - bs->used;
303
304 va_start(ap, fmt);
305 rc = vsnprintf(bs->data + bs->used, limit, fmt, ap);
306 va_end(ap);
307
308 if (rc >= 0 && (size_t) rc >= limit) { /* not enough, enlarge */
309 buf_enlarge(bs, (size_t)rc + 1);
310 limit = bs->sz - bs->used;
311 va_start(ap, fmt);
312 rc = vsnprintf(bs->data + bs->used, limit, fmt, ap);
313 va_end(ap);
314 }
315
316 if (rc > 0)
317 bs->used += rc;
318 }
319
320 static void buf_putc_careful(struct buffer *bs, int c)
321 {
322 if (isprint(c) || c == '\a' || c == '\t' || c == '\r' || c == '\n') {
323 buf_enlarge(bs, 1);
324 bs->data[bs->used++] = c;
325 } else if (!isascii(c))
326 buf_printf(bs, "\\%3o", (unsigned char)c);
327 else {
328 char tmp[] = { '^', c ^ 0x40, '\0' };
329 buf_puts(bs, tmp);
330 }
331 }
332
333 static char *makemsg(char *fname, char **mvec, int mvecsz,
334 size_t *mbufsize, int print_banner)
335 {
336 struct buffer _bs = {.used = 0}, *bs = &_bs;
337 register int ch, cnt;
338 char *p, *lbuf;
339 long line_max;
340
341 line_max = sysconf(_SC_LINE_MAX);
342 if (line_max <= 0)
343 line_max = 512;
344
345 lbuf = xmalloc(line_max);
346
347 if (print_banner == TRUE) {
348 char *hostname = xgethostname();
349 char *whom, *where, *date;
350 struct passwd *pw;
351 time_t now;
352
353 if (!(whom = getlogin()) || !*whom)
354 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
355 if (!whom) {
356 whom = "someone";
357 warn(_("cannot get passwd uid"));
358 }
359 where = ttyname(STDOUT_FILENO);
360 if (!where) {
361 where = "somewhere";
362 } else if (strncmp(where, "/dev/", 5) == 0)
363 where += 5;
364
365 time(&now);
366 date = xstrdup(ctime(&now));
367 date[strlen(date) - 1] = '\0';
368
369 /*
370 * all this stuff is to blank out a square for the message;
371 * we wrap message lines at column 79, not 80, because some
372 * terminals wrap after 79, some do not, and we can't tell.
373 * Which means that we may leave a non-blank character
374 * in column 80, but that can't be helped.
375 */
376 /* snprintf is not always available, but the sprintf's here
377 will not overflow as long as %d takes at most 100 chars */
378 buf_printf(bs, "\r%*s\r\n", TERM_WIDTH, " ");
379
380 snprintf(lbuf, line_max,
381 _("Broadcast message from %s@%s (%s) (%s):"),
382 whom, hostname, where, date);
383 buf_printf(bs, "%-*.*s\007\007\r\n", TERM_WIDTH, TERM_WIDTH, lbuf);
384 free(hostname);
385 free(date);
386 }
387 buf_printf(bs, "%*s\r\n", TERM_WIDTH, " ");
388
389 if (mvec) {
390 /*
391 * Read message from argv[]
392 */
393 int i;
394
395 for (i = 0; i < mvecsz; i++) {
396 buf_puts(bs, mvec[i]);
397 if (i < mvecsz - 1)
398 buf_puts(bs, " ");
399 }
400 buf_puts(bs, "\r\n");
401 } else {
402 /*
403 * read message from <file>
404 */
405 if (fname) {
406 /*
407 * When we are not root, but suid or sgid, refuse to read files
408 * (e.g. device files) that the user may not have access to.
409 * After all, our invoker can easily do "wall < file"
410 * instead of "wall file".
411 */
412 uid_t uid = getuid();
413 if (uid && (uid != geteuid() || getgid() != getegid()))
414 errx(EXIT_FAILURE, _("will not read %s - use stdin."),
415 fname);
416
417 if (!freopen(fname, "r", stdin))
418 err(EXIT_FAILURE, _("cannot open %s"), fname);
419
420 }
421
422 /*
423 * Read message from stdin.
424 */
425 while (fgets(lbuf, line_max, stdin)) {
426 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
427 if (cnt == TERM_WIDTH || ch == '\n') {
428 for (; cnt < TERM_WIDTH; ++cnt)
429 buf_puts(bs, " ");
430 buf_puts(bs, "\r\n");
431 cnt = 0;
432 }
433 if (ch == '\t')
434 cnt += (7 - (cnt % 8));
435 if (ch != '\n')
436 buf_putc_careful(bs, ch);
437 }
438 }
439 }
440 buf_printf(bs, "%*s\r\n", TERM_WIDTH, " ");
441
442 free(lbuf);
443
444 bs->data[bs->used] = '\0'; /* be paranoid */
445 *mbufsize = bs->used;
446 return bs->data;
447 }