]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/wall.c
term-utils: verify writing to streams was successful
[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 <utmp.h>
60 #include <getopt.h>
61
62 #include "nls.h"
63 #include "xalloc.h"
64 #include "strutils.h"
65 #include "ttymsg.h"
66 #include "pathnames.h"
67 #include "carefulputc.h"
68 #include "c.h"
69 #include "fileutils.h"
70 #include "closestream.h"
71
72 #define IGNOREUSER "sleeper"
73 #define WRITE_TIME_OUT 300 /* in seconds */
74
75 #ifndef MAXHOSTNAMELEN
76 # ifdef HOST_NAME_MAX
77 # define MAXHOSTNAMELEN HOST_NAME_MAX
78 # else
79 # define MAXHOSTNAMELEN 64
80 # endif
81 #endif
82
83 /* Function prototypes */
84 char *makemsg(char *fname, size_t *mbufsize, int print_banner);
85 static void usage(FILE *out);
86
87 static void __attribute__((__noreturn__)) usage(FILE *out)
88 {
89 fputs(_("\nUsage:\n"), out);
90 fprintf(out,
91 _(" %s [options] [<file>]\n"),program_invocation_short_name);
92
93 fputs(_("\nOptions:\n"), out);
94 fputs(_(" -n, --nobanner do not print banner, works only for root\n"
95 " -t, --timeout <timeout> write timeout in seconds\n"
96 " -V, --version output version information and exit\n"
97 " -h, --help display this help and exit\n\n"), out);
98
99 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
100 }
101
102 int
103 main(int argc, char **argv) {
104 extern int optind;
105 int ch;
106 struct iovec iov;
107 struct utmp *utmpptr;
108 char *p;
109 char line[sizeof(utmpptr->ut_line) + 1];
110 int print_banner = TRUE;
111 char *mbuf;
112 size_t mbufsize;
113 long timeout = WRITE_TIME_OUT;
114
115 setlocale(LC_ALL, "");
116 bindtextdomain(PACKAGE, LOCALEDIR);
117 textdomain(PACKAGE);
118 atexit(close_stdout);
119
120 static const struct option longopts[] = {
121 { "nobanner", no_argument, 0, 'n' },
122 { "timeout", required_argument, 0, 't' },
123 { "version", no_argument, 0, 'V' },
124 { "help", no_argument, 0, 'h' },
125 { NULL, 0, 0, 0 }
126 };
127
128 while ((ch = getopt_long(argc, argv, "nt:Vh", longopts, NULL)) != -1) {
129 switch (ch) {
130 case 'n':
131 if (geteuid() == 0)
132 print_banner = FALSE;
133 else
134 warnx(_("--nobanner is available only for root"));
135 break;
136 case 't':
137 timeout = strtoll_or_err(optarg, _("invalid timeout argument"));
138 if (timeout < 1)
139 errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg);
140 break;
141 case 'V':
142 printf(_("%s from %s\n"), program_invocation_short_name,
143 PACKAGE_STRING);
144 exit(EXIT_SUCCESS);
145 case 'h':
146 usage(stdout);
147 default:
148 usage(stderr);
149 }
150 }
151 argc -= optind;
152 argv += optind;
153 if (argc > 1)
154 usage(stderr);
155
156 mbuf = makemsg(*argv, &mbufsize, print_banner);
157
158 iov.iov_base = mbuf;
159 iov.iov_len = mbufsize;
160 while((utmpptr = getutent())) {
161 if (!utmpptr->ut_name[0] ||
162 !strncmp(utmpptr->ut_name, IGNOREUSER,
163 sizeof(utmpptr->ut_name)))
164 continue;
165 #ifdef USER_PROCESS
166 if (utmpptr->ut_type != USER_PROCESS)
167 continue;
168 #endif
169
170 /* Joey Hess reports that use-sessreg in /etc/X11/wdm/
171 produces ut_line entries like :0, and a write
172 to /dev/:0 fails. */
173 if (utmpptr->ut_line[0] == ':')
174 continue;
175
176 xstrncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line));
177 if ((p = ttymsg(&iov, 1, line, timeout)) != NULL)
178 warnx("%s", p);
179 }
180 endutent();
181 free(mbuf);
182 exit(EXIT_SUCCESS);
183 }
184
185 char *
186 makemsg(char *fname, size_t *mbufsize, int print_banner)
187 {
188 register int ch, cnt;
189 struct tm *lt;
190 struct passwd *pw;
191 struct stat sbuf;
192 time_t now;
193 FILE *fp;
194 char *p, *whom, *where, *hostname, *lbuf, *tmpname, *mbuf;
195 long line_max;
196
197 hostname = xmalloc(sysconf(_SC_HOST_NAME_MAX) + 1);
198 line_max = sysconf(_SC_LINE_MAX);
199 lbuf = xmalloc(line_max);
200
201 if ((fp = xfmkstemp(&tmpname)) == NULL)
202 err(EXIT_FAILURE, _("can't open temporary file"));
203 unlink(tmpname);
204 free(tmpname);
205
206 if (print_banner == TRUE) {
207 if (!(whom = getlogin()) || !*whom)
208 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
209 if (!whom) {
210 whom = "someone";
211 warn(_("cannot get passwd uid"));
212 }
213 where = ttyname(STDOUT_FILENO);
214 if (!where) {
215 where = "somewhere";
216 warn(_("cannot get tty name"));
217 }
218 gethostname(hostname, sizeof(hostname));
219 time(&now);
220 lt = localtime(&now);
221
222 /*
223 * all this stuff is to blank out a square for the message;
224 * we wrap message lines at column 79, not 80, because some
225 * terminals wrap after 79, some do not, and we can't tell.
226 * Which means that we may leave a non-blank character
227 * in column 80, but that can't be helped.
228 */
229 /* snprintf is not always available, but the sprintf's here
230 will not overflow as long as %d takes at most 100 chars */
231 fprintf(fp, "\r%79s\r\n", " ");
232 sprintf(lbuf, _("Broadcast Message from %s@%s"),
233 whom, hostname);
234 fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
235 sprintf(lbuf, " (%s) at %d:%02d ...",
236 where, lt->tm_hour, lt->tm_min);
237 fprintf(fp, "%-79.79s\r\n", lbuf);
238 }
239 fprintf(fp, "%79s\r\n", " ");
240
241 free(hostname);
242
243 if (fname) {
244 /*
245 * When we are not root, but suid or sgid, refuse to read files
246 * (e.g. device files) that the user may not have access to.
247 * After all, our invoker can easily do "wall < file"
248 * instead of "wall file".
249 */
250 uid_t uid = getuid();
251 if (uid && (uid != geteuid() || getgid() != getegid()))
252 errx(EXIT_FAILURE, _("will not read %s - use stdin."),
253 fname);
254
255 if (!freopen(fname, "r", stdin))
256 err(EXIT_FAILURE, _("cannot open file %s"), fname);
257 }
258
259 while (fgets(lbuf, line_max, stdin)) {
260 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
261 if (cnt == 79 || ch == '\n') {
262 for (; cnt < 79; ++cnt)
263 putc(' ', fp);
264 putc('\r', fp);
265 putc('\n', fp);
266 cnt = 0;
267 }
268 if (ch != '\n')
269 carefulputc(ch, fp);
270 }
271 }
272 fprintf(fp, "%79s\r\n", " ");
273
274 free(lbuf);
275 rewind(fp);
276
277 if (fstat(fileno(fp), &sbuf))
278 err(EXIT_FAILURE, _("fstat failed"));
279
280 *mbufsize = (size_t) sbuf.st_size;
281 mbuf = xmalloc(*mbufsize);
282
283 if (fread(mbuf, 1, *mbufsize, fp) != *mbufsize)
284 err(EXIT_FAILURE, _("fread failed"));
285
286 if (close_stream(fp) != 0)
287 errx(EXIT_FAILURE, _("write error"));
288 return mbuf;
289 }