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>
66 #include "pathnames.h"
67 #include "carefulputc.h"
69 #include "fileutils.h"
70 #include "closestream.h"
72 #define IGNOREUSER "sleeper"
73 #define WRITE_TIME_OUT 300 /* in seconds */
75 /* Function prototypes */
76 static char *makemsg(char *fname
, char **mvec
, int mvecsz
,
77 size_t *mbufsize
, int print_banner
);
79 static void __attribute__((__noreturn__
)) usage(FILE *out
)
81 fputs(_("\nUsage:\n"), out
);
83 _(" %s [options] [<file> | <message>]\n"),
84 program_invocation_short_name
);
86 fputs(_("\nOptions:\n"), out
);
87 fputs(_(" -n, --nobanner do not print banner, works only for root\n"
88 " -t, --timeout <timeout> write timeout in seconds\n"
89 " -V, --version output version information and exit\n"
90 " -h, --help display this help and exit\n\n"), out
);
92 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
96 main(int argc
, char **argv
) {
101 char line
[sizeof(utmpptr
->ut_line
) + 1];
102 int print_banner
= TRUE
;
103 char *mbuf
, *fname
= NULL
;
105 unsigned timeout
= WRITE_TIME_OUT
;
109 static const struct option longopts
[] = {
110 { "nobanner", no_argument
, 0, 'n' },
111 { "timeout", required_argument
, 0, 't' },
112 { "version", no_argument
, 0, 'V' },
113 { "help", no_argument
, 0, 'h' },
117 setlocale(LC_ALL
, "");
118 bindtextdomain(PACKAGE
, LOCALEDIR
);
120 atexit(close_stdout
);
122 while ((ch
= getopt_long(argc
, argv
, "nt:Vh", longopts
, NULL
)) != -1) {
126 print_banner
= FALSE
;
128 warnx(_("--nobanner is available only for root"));
131 timeout
= strtou32_or_err(optarg
, _("invalid timeout argument"));
133 errx(EXIT_FAILURE
, _("invalid timeout argument: %s"), optarg
);
136 printf(_("%s from %s\n"), program_invocation_short_name
,
148 if (argc
== 1 && access(argv
[0], F_OK
) == 0)
150 else if (argc
>= 1) {
155 mbuf
= makemsg(fname
, mvec
, mvecsz
, &mbufsize
, print_banner
);
158 iov
.iov_len
= mbufsize
;
159 while((utmpptr
= getutent())) {
160 if (!utmpptr
->ut_name
[0] ||
161 !strncmp(utmpptr
->ut_name
, IGNOREUSER
,
162 sizeof(utmpptr
->ut_name
)))
165 if (utmpptr
->ut_type
!= USER_PROCESS
)
169 /* Joey Hess reports that use-sessreg in /etc/X11/wdm/
170 produces ut_line entries like :0, and a write
172 if (utmpptr
->ut_line
[0] == ':')
175 xstrncpy(line
, utmpptr
->ut_line
, sizeof(utmpptr
->ut_line
));
176 if ((p
= ttymsg(&iov
, 1, line
, timeout
)) != NULL
)
184 static char *makemsg(char *fname
, char **mvec
, int mvecsz
, size_t *mbufsize
, int print_banner
)
186 register int ch
, cnt
;
192 char *p
, *whom
, *where
, *lbuf
, *tmpname
, *mbuf
;
195 line_max
= sysconf(_SC_LINE_MAX
);
196 lbuf
= xmalloc(line_max
);
198 if ((fp
= xfmkstemp(&tmpname
, NULL
)) == NULL
)
199 err(EXIT_FAILURE
, _("can't open temporary file"));
203 if (print_banner
== TRUE
) {
204 char *hostname
= xgethostname();
205 if (!(whom
= getlogin()) || !*whom
)
206 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
209 warn(_("cannot get passwd uid"));
211 where
= ttyname(STDOUT_FILENO
);
214 warn(_("cannot get tty name"));
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 /* snprintf is not always available, but the sprintf's here
227 will not overflow as long as %d takes at most 100 chars */
228 fprintf(fp
, "\r%79s\r\n", " ");
229 sprintf(lbuf
, _("Broadcast Message from %s@%s"),
232 fprintf(fp
, "%-79.79s\007\007\r\n", lbuf
);
233 sprintf(lbuf
, " (%s) at %d:%02d ...",
234 where
, lt
->tm_hour
, lt
->tm_min
);
235 fprintf(fp
, "%-79.79s\r\n", lbuf
);
237 fprintf(fp
, "%79s\r\n", " ");
241 * Read message from argv[]
245 for (i
= 0; i
< mvecsz
; i
++) {
255 * read message from <file>
259 * When we are not root, but suid or sgid, refuse to read files
260 * (e.g. device files) that the user may not have access to.
261 * After all, our invoker can easily do "wall < file"
262 * instead of "wall file".
264 uid_t uid
= getuid();
265 if (uid
&& (uid
!= geteuid() || getgid() != getegid()))
266 errx(EXIT_FAILURE
, _("will not read %s - use stdin."),
269 if (!freopen(fname
, "r", stdin
))
270 err(EXIT_FAILURE
, _("cannot open %s"), fname
);
275 * Read message from stdin.
277 while (fgets(lbuf
, line_max
, stdin
)) {
278 for (cnt
= 0, p
= lbuf
; (ch
= *p
) != '\0'; ++p
, ++cnt
) {
279 if (cnt
== 79 || ch
== '\n') {
280 for (; cnt
< 79; ++cnt
)
291 fprintf(fp
, "%79s\r\n", " ");
296 if (fstat(fileno(fp
), &sbuf
))
297 err(EXIT_FAILURE
, _("stat failed"));
299 *mbufsize
= (size_t) sbuf
.st_size
;
300 mbuf
= xmalloc(*mbufsize
);
302 if (fread(mbuf
, 1, *mbufsize
, fp
) != *mbufsize
)
303 err(EXIT_FAILURE
, _("fread failed"));
305 if (close_stream(fp
) != 0)
306 errx(EXIT_FAILURE
, _("write error"));