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>
64 #if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1
65 # include <systemd/sd-login.h>
66 # include <systemd/sd-daemon.h>
73 #include "pathnames.h"
74 #include "carefulputc.h"
77 #include "fileutils.h"
78 #include "closestream.h"
79 #include "timeutils.h"
83 #define WRITE_TIME_OUT 300 /* in seconds */
85 /* Function prototypes */
86 static char *makemsg(char *fname
, char **mvec
, int mvecsz
,
87 size_t *mbufsize
, int print_banner
);
89 static void __attribute__((__noreturn__
)) usage(void)
92 fputs(USAGE_HEADER
, out
);
94 _(" %s [options] [<file> | <message>]\n"), program_invocation_short_name
);
96 fputs(USAGE_SEPARATOR
, out
);
97 fputs(_("Write a message to all users.\n"), out
);
99 fputs(USAGE_OPTIONS
, out
);
100 fputs(_(" -g, --group <group> only send message to group\n"), out
);
101 fputs(_(" -n, --nobanner do not print banner, works only for root\n"), out
);
102 fputs(_(" -t, --timeout <timeout> write timeout in seconds\n"), out
);
103 fputs(USAGE_SEPARATOR
, out
);
104 printf(USAGE_HELP_OPTIONS(25));
105 printf(USAGE_MAN_TAIL("wall(1)"));
110 struct group_workspace
{
111 gid_t requested_group
;
114 /* getgrouplist() on OSX takes int* not gid_t* */
122 static gid_t
get_group_gid(const char *group
)
127 if ((gr
= getgrnam(group
)))
130 gid
= strtou32_or_err(group
, _("invalid group argument"));
132 errx(EXIT_FAILURE
, _("%s: unknown gid"), group
);
137 static struct group_workspace
*init_group_workspace(const char *group
)
139 struct group_workspace
*buf
= xmalloc(sizeof(struct group_workspace
));
141 buf
->requested_group
= get_group_gid(group
);
142 buf
->ngroups
= sysconf(_SC_NGROUPS_MAX
) + 1; /* room for the primary gid */
143 buf
->groups
= xcalloc(sizeof(*buf
->groups
), buf
->ngroups
);
148 static void free_group_workspace(struct group_workspace
*buf
)
157 static int is_gr_member(const char *login
, const struct group_workspace
*buf
)
160 int ngroups
= buf
->ngroups
;
163 pw
= getpwnam(login
);
167 if (buf
->requested_group
== pw
->pw_gid
)
170 rc
= getgrouplist(login
, pw
->pw_gid
, buf
->groups
, &ngroups
);
172 /* buffer too small, not sure how this can happen, since
173 we used sysconf to get the size... */
175 _("getgrouplist found more groups than sysconf allows"));
178 for (; ngroups
>= 0; --ngroups
) {
179 if (buf
->requested_group
== (gid_t
) buf
->groups
[ngroups
])
186 int main(int argc
, char **argv
)
190 struct utmpx
*utmpptr
;
192 char line
[sizeof(utmpptr
->ut_line
) + 1];
193 int print_banner
= TRUE
;
194 struct group_workspace
*group_buf
= NULL
;
195 char *mbuf
, *fname
= NULL
;
197 unsigned timeout
= WRITE_TIME_OUT
;
201 static const struct option longopts
[] = {
202 { "nobanner", no_argument
, NULL
, 'n' },
203 { "timeout", required_argument
, NULL
, 't' },
204 { "group", required_argument
, NULL
, 'g' },
205 { "version", no_argument
, NULL
, 'V' },
206 { "help", no_argument
, NULL
, 'h' },
210 setlocale(LC_ALL
, "");
211 bindtextdomain(PACKAGE
, LOCALEDIR
);
213 close_stdout_atexit();
215 while ((ch
= getopt_long(argc
, argv
, "nt:g:Vh", longopts
, NULL
)) != -1) {
219 print_banner
= FALSE
;
221 warnx(_("--nobanner is available only for root"));
224 timeout
= strtou32_or_err(optarg
, _("invalid timeout argument"));
226 errx(EXIT_FAILURE
, _("invalid timeout argument: %s"), optarg
);
229 group_buf
= init_group_workspace(optarg
);
233 print_version(EXIT_SUCCESS
);
237 errtryhelp(EXIT_FAILURE
);
243 if (argc
== 1 && access(argv
[0], F_OK
) == 0)
245 else if (argc
>= 1) {
250 mbuf
= makemsg(fname
, mvec
, mvecsz
, &mbufsize
, print_banner
);
253 iov
.iov_len
= mbufsize
;
255 #if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1
256 if (sd_booted() > 0) {
257 char **sessions_list
;
260 sessions
= sd_get_sessions(&sessions_list
);
262 errx(EXIT_FAILURE
, _("error getting sessions: %s"),
263 strerror(-sessions
));
265 for (int i
= 0; i
< sessions
; i
++) {
269 if ((r
= sd_session_get_username(sessions_list
[i
], &name
)) < 0)
270 errx(EXIT_FAILURE
, _("get user name failed: %s"), strerror (-r
));
272 if (!(group_buf
&& !is_gr_member(name
, group_buf
))) {
273 if (sd_session_get_tty(sessions_list
[i
], &tty
) >= 0) {
274 if ((p
= ttymsg(&iov
, 1, tty
, timeout
)) != NULL
)
281 free(sessions_list
[i
]);
287 while ((utmpptr
= getutxent())) {
288 if (!utmpptr
->ut_user
[0])
291 if (utmpptr
->ut_type
!= USER_PROCESS
)
294 /* Joey Hess reports that use-sessreg in /etc/X11/wdm/ produces
295 * ut_line entries like :0, and a write to /dev/:0 fails.
297 * It also seems that some login manager may produce empty ut_line.
299 if (!*utmpptr
->ut_line
|| *utmpptr
->ut_line
== ':')
302 if (group_buf
&& !is_gr_member(utmpptr
->ut_user
, group_buf
))
305 mem2strcpy(line
, utmpptr
->ut_line
, sizeof(utmpptr
->ut_line
), sizeof(line
));
306 if ((p
= ttymsg(&iov
, 1, line
, timeout
)) != NULL
)
313 free_group_workspace(group_buf
);
317 static char *makemsg(char *fname
, char **mvec
, int mvecsz
,
318 size_t *mbufsize
, int print_banner
)
321 FILE * fs
= open_memstream(&retbuf
, mbufsize
);
322 size_t lbuflen
= 512;
323 lbuf
= xmalloc(lbuflen
);
325 if (print_banner
== TRUE
) {
326 char *hostname
= xgethostname();
327 char *whom
, *where
, date
[CTIME_BUFSIZ
];
333 warn(_("cannot get passwd uid"));
335 where
= ttyname(STDOUT_FILENO
);
338 } else if (strncmp(where
, "/dev/", 5) == 0)
343 date
[strlen(date
) - 1] = '\0';
346 * all this stuff is to blank out a square for the message;
347 * we wrap message lines at column 79, not 80, because some
348 * terminals wrap after 79, some do not, and we can't tell.
349 * Which means that we may leave a non-blank character
350 * in column 80, but that can't be helped.
352 /* snprintf is not always available, but the sprintf's here
353 will not overflow as long as %d takes at most 100 chars */
354 fprintf(fs
, "\r%*s\r\n", TERM_WIDTH
, " ");
356 snprintf(lbuf
, lbuflen
,
357 _("Broadcast message from %s@%s (%s) (%s):"),
358 whom
, hostname
, where
, date
);
359 fprintf(fs
, "%-*.*s\007\007\r\n", TERM_WIDTH
, TERM_WIDTH
, lbuf
);
362 fprintf(fs
, "%*s\r\n", TERM_WIDTH
, " ");
366 * Read message from argv[]
370 for (i
= 0; i
< mvecsz
; i
++) {
378 * read message from <file>
382 * When we are not root, but suid or sgid, refuse to read files
383 * (e.g. device files) that the user may not have access to.
384 * After all, our invoker can easily do "wall < file"
385 * instead of "wall file".
387 uid_t uid
= getuid();
388 if (uid
&& (uid
!= geteuid() || getgid() != getegid()))
389 errx(EXIT_FAILURE
, _("will not read %s - use stdin."),
392 if (!freopen(fname
, "r", stdin
))
393 err(EXIT_FAILURE
, _("cannot open %s"), fname
);
398 * Read message from stdin.
400 while (getline(&lbuf
, &lbuflen
, stdin
) >= 0)
401 fputs_careful(lbuf
, fs
, '^', true, TERM_WIDTH
);
403 fprintf(fs
, "%*s\r\n", TERM_WIDTH
, " ");