2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * Modified Sun Mar 12 10:34:34 1995, faith@cs.unc.edu, for Linux
37 * This program is not related to David Wall, whose Stanford Ph.D. thesis
38 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
40 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
41 * - added Native Language Support
45 #include <sys/param.h>
61 #include <sys/types.h>
68 #include "pathnames.h"
69 #include "carefulputc.h"
71 #include "fileutils.h"
72 #include "closestream.h"
75 #define WRITE_TIME_OUT 300 /* in seconds */
77 /* Function prototypes */
78 static char *makemsg(char *fname
, char **mvec
, int mvecsz
,
79 size_t *mbufsize
, int print_banner
);
81 static void __attribute__((__noreturn__
)) usage(void)
84 fputs(USAGE_HEADER
, out
);
86 _(" %s [options] [<file> | <message>]\n"), program_invocation_short_name
);
88 fputs(USAGE_SEPARATOR
, out
);
89 fputs(_("Write a message to all users.\n"), out
);
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)"));
102 struct group_workspace
{
103 gid_t requested_group
;
106 /* getgrouplist() on OSX takes int* not gid_t* */
114 static gid_t
get_group_gid(const char *group
)
119 if ((gr
= getgrnam(group
)))
122 gid
= strtou32_or_err(group
, _("invalid group argument"));
124 errx(EXIT_FAILURE
, _("%s: unknown gid"), group
);
129 static struct group_workspace
*init_group_workspace(const char *group
)
131 struct group_workspace
*buf
= xmalloc(sizeof(struct group_workspace
));
133 buf
->requested_group
= get_group_gid(group
);
134 buf
->ngroups
= sysconf(_SC_NGROUPS_MAX
) + 1; /* room for the primary gid */
135 buf
->groups
= xcalloc(sizeof(*buf
->groups
), buf
->ngroups
);
140 static void free_group_workspace(struct group_workspace
*buf
)
149 static int is_gr_member(const char *login
, const struct group_workspace
*buf
)
152 int ngroups
= buf
->ngroups
;
155 pw
= getpwnam(login
);
159 if (buf
->requested_group
== pw
->pw_gid
)
162 rc
= getgrouplist(login
, pw
->pw_gid
, buf
->groups
, &ngroups
);
164 /* buffer too small, not sure how this can happen, since
165 we used sysconf to get the size... */
167 _("getgrouplist found more groups than sysconf allows"));
170 for (; ngroups
>= 0; --ngroups
) {
171 if (buf
->requested_group
== (gid_t
) buf
->groups
[ngroups
])
178 int main(int argc
, char **argv
)
182 struct utmpx
*utmpptr
;
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
;
189 unsigned timeout
= WRITE_TIME_OUT
;
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' },
202 setlocale(LC_ALL
, "");
203 bindtextdomain(PACKAGE
, LOCALEDIR
);
205 close_stdout_atexit();
207 while ((ch
= getopt_long(argc
, argv
, "nt:g:Vh", longopts
, NULL
)) != -1) {
211 print_banner
= FALSE
;
213 warnx(_("--nobanner is available only for root"));
216 timeout
= strtou32_or_err(optarg
, _("invalid timeout argument"));
218 errx(EXIT_FAILURE
, _("invalid timeout argument: %s"), optarg
);
221 group_buf
= init_group_workspace(optarg
);
225 print_version(EXIT_SUCCESS
);
229 errtryhelp(EXIT_FAILURE
);
235 if (argc
== 1 && access(argv
[0], F_OK
) == 0)
237 else if (argc
>= 1) {
242 mbuf
= makemsg(fname
, mvec
, mvecsz
, &mbufsize
, print_banner
);
245 iov
.iov_len
= mbufsize
;
246 while((utmpptr
= getutxent())) {
247 if (!utmpptr
->ut_user
[0])
250 if (utmpptr
->ut_type
!= USER_PROCESS
)
253 /* Joey Hess reports that use-sessreg in /etc/X11/wdm/ produces
254 * ut_line entries like :0, and a write to /dev/:0 fails.
256 * It also seems that some login manager may produce empty ut_line.
258 if (!*utmpptr
->ut_line
|| *utmpptr
->ut_line
== ':')
261 if (group_buf
&& !is_gr_member(utmpptr
->ut_user
, group_buf
))
264 mem2strcpy(line
, utmpptr
->ut_line
, sizeof(utmpptr
->ut_line
), sizeof(line
));
265 if ((p
= ttymsg(&iov
, 1, line
, timeout
)) != NULL
)
270 free_group_workspace(group_buf
);
280 static void buf_enlarge(struct buffer
*bs
, size_t len
)
282 if (bs
->sz
== 0 || len
> bs
->sz
- bs
->used
) {
283 bs
->sz
+= len
< 128 ? 128 : len
;
284 bs
->data
= xrealloc(bs
->data
, bs
->sz
);
288 static void buf_puts(struct buffer
*bs
, const char *s
)
290 size_t len
= strlen(s
);
292 buf_enlarge(bs
, len
+ 1);
293 memcpy(bs
->data
+ bs
->used
, s
, len
+ 1);
297 static void buf_printf(struct buffer
*bs
, const char *fmt
, ...)
303 buf_enlarge(bs
, 0); /* default size */
304 limit
= bs
->sz
- bs
->used
;
307 rc
= vsnprintf(bs
->data
+ bs
->used
, limit
, fmt
, ap
);
310 if (rc
>= 0 && (size_t) rc
>= limit
) { /* not enough, enlarge */
311 buf_enlarge(bs
, (size_t)rc
+ 1);
312 limit
= bs
->sz
- bs
->used
;
314 rc
= vsnprintf(bs
->data
+ bs
->used
, limit
, fmt
, ap
);
322 static void buf_putc_careful(struct buffer
*bs
, int c
)
324 if (isprint(c
) || c
== '\a' || c
== '\t' || c
== '\r' || c
== '\n') {
326 bs
->data
[bs
->used
++] = c
;
327 } else if (!isascii(c
))
328 buf_printf(bs
, "\\%3o", (unsigned char)c
);
330 char tmp
[] = { '^', c
^ 0x40, '\0' };
335 static char *makemsg(char *fname
, char **mvec
, int mvecsz
,
336 size_t *mbufsize
, int print_banner
)
338 struct buffer _bs
= {.used
= 0}, *bs
= &_bs
;
339 register int ch
, cnt
;
343 line_max
= sysconf(_SC_LINE_MAX
);
347 lbuf
= xmalloc(line_max
);
349 if (print_banner
== TRUE
) {
350 char *hostname
= xgethostname();
351 char *whom
, *where
, *date
;
355 if (!(whom
= getlogin()) || !*whom
)
356 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
359 warn(_("cannot get passwd uid"));
361 where
= ttyname(STDOUT_FILENO
);
364 } else if (strncmp(where
, "/dev/", 5) == 0)
368 date
= xstrdup(ctime(&now
));
369 date
[strlen(date
) - 1] = '\0';
372 * all this stuff is to blank out a square for the message;
373 * we wrap message lines at column 79, not 80, because some
374 * terminals wrap after 79, some do not, and we can't tell.
375 * Which means that we may leave a non-blank character
376 * in column 80, but that can't be helped.
378 /* snprintf is not always available, but the sprintf's here
379 will not overflow as long as %d takes at most 100 chars */
380 buf_printf(bs
, "\r%*s\r\n", TERM_WIDTH
, " ");
382 snprintf(lbuf
, line_max
,
383 _("Broadcast message from %s@%s (%s) (%s):"),
384 whom
, hostname
, where
, date
);
385 buf_printf(bs
, "%-*.*s\007\007\r\n", TERM_WIDTH
, TERM_WIDTH
, lbuf
);
389 buf_printf(bs
, "%*s\r\n", TERM_WIDTH
, " ");
393 * Read message from argv[]
397 for (i
= 0; i
< mvecsz
; i
++) {
398 buf_puts(bs
, mvec
[i
]);
402 buf_puts(bs
, "\r\n");
405 * read message from <file>
409 * When we are not root, but suid or sgid, refuse to read files
410 * (e.g. device files) that the user may not have access to.
411 * After all, our invoker can easily do "wall < file"
412 * instead of "wall file".
414 uid_t uid
= getuid();
415 if (uid
&& (uid
!= geteuid() || getgid() != getegid()))
416 errx(EXIT_FAILURE
, _("will not read %s - use stdin."),
419 if (!freopen(fname
, "r", stdin
))
420 err(EXIT_FAILURE
, _("cannot open %s"), fname
);
425 * Read message from stdin.
427 while (fgets(lbuf
, line_max
, stdin
)) {
428 for (cnt
= 0, p
= lbuf
; (ch
= *p
) != '\0'; ++p
, ++cnt
) {
429 if (cnt
== TERM_WIDTH
|| ch
== '\n') {
430 for (; cnt
< TERM_WIDTH
; ++cnt
)
432 buf_puts(bs
, "\r\n");
436 cnt
+= (7 - (cnt
% 8));
438 buf_putc_careful(bs
, ch
);
442 buf_printf(bs
, "%*s\r\n", TERM_WIDTH
, " ");
446 bs
->data
[bs
->used
] = '\0'; /* be paranoid */
447 *mbufsize
= bs
->used
;