]>
Commit | Line | Data |
---|---|---|
6dbe3af9 | 1 | /* |
726f69e2 KZ |
2 | * Copyright (c) 1988, 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
6dbe3af9 KZ |
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 | * | |
726f69e2 | 33 | * Modified Sun Mar 12 10:34:34 1995, faith@cs.unc.edu, for Linux |
6dbe3af9 KZ |
34 | */ |
35 | ||
6dbe3af9 KZ |
36 | /* |
37 | * This program is not related to David Wall, whose Stanford Ph.D. thesis | |
38 | * is entitled "Mechanisms for Broadcast and Selective Broadcast". | |
7eda085c | 39 | * |
b50945d4 | 40 | * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
41 | * - added Native Language Support |
42 | * | |
6dbe3af9 KZ |
43 | */ |
44 | ||
45 | #include <sys/param.h> | |
46 | #include <sys/stat.h> | |
47 | #include <sys/time.h> | |
48 | #include <sys/uio.h> | |
726f69e2 | 49 | |
aa00c136 | 50 | #include <errno.h> |
726f69e2 | 51 | #include <paths.h> |
7eda085c | 52 | #include <ctype.h> |
6dbe3af9 KZ |
53 | #include <pwd.h> |
54 | #include <stdio.h> | |
55 | #include <stdlib.h> | |
726f69e2 | 56 | #include <string.h> |
c07ebfa1 | 57 | #include <time.h> |
726f69e2 KZ |
58 | #include <unistd.h> |
59 | #include <utmp.h> | |
76cc41b7 | 60 | #include <getopt.h> |
66ee8158 | 61 | |
7eda085c | 62 | #include "nls.h" |
4b5156cb | 63 | #include "xalloc.h" |
8abcf290 | 64 | #include "strutils.h" |
66ee8158 | 65 | #include "ttymsg.h" |
726f69e2 | 66 | #include "pathnames.h" |
66ee8158 | 67 | #include "carefulputc.h" |
eb76ca98 | 68 | #include "c.h" |
a000bbb6 | 69 | #include "fileutils.h" |
cdd2a8c3 | 70 | #include "closestream.h" |
726f69e2 | 71 | |
42e296b5 SK |
72 | #define TERM_WIDTH 79 |
73 | #define WRITE_TIME_OUT 300 /* in seconds */ | |
6dbe3af9 | 74 | |
98e2d4de | 75 | /* Function prototypes */ |
cdd3cc7f KZ |
76 | static char *makemsg(char *fname, char **mvec, int mvecsz, |
77 | size_t *mbufsize, int print_banner); | |
6dbe3af9 | 78 | |
76cc41b7 | 79 | static void __attribute__((__noreturn__)) usage(FILE *out) |
dfdf2081 | 80 | { |
bbeae1ce | 81 | fputs(USAGE_HEADER, out); |
a290362b | 82 | fprintf(out, |
bbeae1ce | 83 | _(" %s [options] [<file> | <message>]\n"), program_invocation_short_name); |
451dbcfa BS |
84 | |
85 | fputs(USAGE_SEPARATOR, out); | |
86 | fputs(_("Write a message to all users.\n"), out); | |
87 | ||
bbeae1ce SK |
88 | fputs(USAGE_OPTIONS, out); |
89 | fputs(_(" -n, --nobanner do not print banner, works only for root\n"), out); | |
90 | fputs(_(" -t, --timeout <timeout> write timeout in seconds\n"), out); | |
91 | fputs(USAGE_SEPARATOR, out); | |
92 | fputs(USAGE_HELP, out); | |
93 | fputs(USAGE_VERSION, out); | |
94 | fprintf(out, USAGE_MAN_TAIL("wall(1)")); | |
76cc41b7 SK |
95 | |
96 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
dfdf2081 DB |
97 | } |
98 | ||
d81c3055 KZ |
99 | int main(int argc, char **argv) |
100 | { | |
6dbe3af9 KZ |
101 | int ch; |
102 | struct iovec iov; | |
7eda085c | 103 | struct utmp *utmpptr; |
66ee8158 | 104 | char *p; |
7eda085c | 105 | char line[sizeof(utmpptr->ut_line) + 1]; |
98e2d4de | 106 | int print_banner = TRUE; |
cdd3cc7f | 107 | char *mbuf, *fname = NULL; |
98e2d4de | 108 | size_t mbufsize; |
b43d3a3a | 109 | unsigned timeout = WRITE_TIME_OUT; |
cdd3cc7f KZ |
110 | char **mvec = NULL; |
111 | int mvecsz = 0; | |
7eda085c | 112 | |
76cc41b7 SK |
113 | static const struct option longopts[] = { |
114 | { "nobanner", no_argument, 0, 'n' }, | |
cae7485e | 115 | { "timeout", required_argument, 0, 't' }, |
76cc41b7 SK |
116 | { "version", no_argument, 0, 'V' }, |
117 | { "help", no_argument, 0, 'h' }, | |
118 | { NULL, 0, 0, 0 } | |
119 | }; | |
120 | ||
3acc206d SK |
121 | setlocale(LC_ALL, ""); |
122 | bindtextdomain(PACKAGE, LOCALEDIR); | |
123 | textdomain(PACKAGE); | |
124 | atexit(close_stdout); | |
125 | ||
cae7485e | 126 | while ((ch = getopt_long(argc, argv, "nt:Vh", longopts, NULL)) != -1) { |
6dbe3af9 KZ |
127 | switch (ch) { |
128 | case 'n': | |
6dbe3af9 | 129 | if (geteuid() == 0) |
98e2d4de | 130 | print_banner = FALSE; |
76cc41b7 SK |
131 | else |
132 | warnx(_("--nobanner is available only for root")); | |
6dbe3af9 | 133 | break; |
cae7485e | 134 | case 't': |
b43d3a3a | 135 | timeout = strtou32_or_err(optarg, _("invalid timeout argument")); |
cae7485e SK |
136 | if (timeout < 1) |
137 | errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg); | |
138 | break; | |
76cc41b7 | 139 | case 'V': |
bbeae1ce | 140 | printf(UTIL_LINUX_VERSION); |
76cc41b7 SK |
141 | exit(EXIT_SUCCESS); |
142 | case 'h': | |
143 | usage(stdout); | |
6dbe3af9 | 144 | default: |
76cc41b7 | 145 | usage(stderr); |
6dbe3af9 | 146 | } |
00f41c7d | 147 | } |
6dbe3af9 KZ |
148 | argc -= optind; |
149 | argv += optind; | |
6dbe3af9 | 150 | |
cdd3cc7f KZ |
151 | if (argc == 1 && access(argv[0], F_OK) == 0) |
152 | fname = argv[0]; | |
153 | else if (argc >= 1) { | |
154 | mvec = argv; | |
155 | mvecsz = argc; | |
156 | } | |
157 | ||
158 | mbuf = makemsg(fname, mvec, mvecsz, &mbufsize, print_banner); | |
7eda085c | 159 | |
6dbe3af9 KZ |
160 | iov.iov_base = mbuf; |
161 | iov.iov_len = mbufsize; | |
7eda085c | 162 | while((utmpptr = getutent())) { |
cfa7fe89 | 163 | if (!utmpptr->ut_user[0]) |
7eda085c KZ |
164 | continue; |
165 | #ifdef USER_PROCESS | |
166 | if (utmpptr->ut_type != USER_PROCESS) | |
6dbe3af9 | 167 | continue; |
6dbe3af9 | 168 | #endif |
e8f26419 KZ |
169 | /* Joey Hess reports that use-sessreg in /etc/X11/wdm/ |
170 | produces ut_line entries like :0, and a write | |
171 | to /dev/:0 fails. */ | |
172 | if (utmpptr->ut_line[0] == ':') | |
173 | continue; | |
174 | ||
c07ebfa1 | 175 | xstrncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line)); |
cae7485e | 176 | if ((p = ttymsg(&iov, 1, line, timeout)) != NULL) |
aa00c136 | 177 | warnx("%s", p); |
6dbe3af9 | 178 | } |
7eda085c | 179 | endutent(); |
98e2d4de | 180 | free(mbuf); |
d0acbd38 | 181 | exit(EXIT_SUCCESS); |
6dbe3af9 KZ |
182 | } |
183 | ||
890e1035 KZ |
184 | struct buffer { |
185 | size_t sz; | |
186 | size_t used; | |
187 | char *data; | |
188 | }; | |
189 | ||
190 | static void buf_enlarge(struct buffer *bs, size_t len) | |
191 | { | |
192 | if (bs->sz == 0 || len > bs->sz - bs->used) { | |
193 | bs->sz += len < 128 ? 128 : len; | |
194 | bs->data = xrealloc(bs->data, bs->sz); | |
195 | } | |
196 | } | |
197 | ||
198 | static void buf_puts(struct buffer *bs, const char *s) | |
199 | { | |
200 | size_t len = strlen(s); | |
201 | ||
202 | buf_enlarge(bs, len + 1); | |
203 | memcpy(bs->data + bs->used, s, len + 1); | |
204 | bs->used += len; | |
205 | } | |
206 | ||
207 | static void buf_printf(struct buffer *bs, const char *fmt, ...) | |
208 | { | |
209 | int rc; | |
210 | va_list ap; | |
211 | size_t limit; | |
212 | ||
213 | buf_enlarge(bs, 0); /* default size */ | |
214 | limit = bs->sz - bs->used; | |
215 | ||
216 | va_start(ap, fmt); | |
217 | rc = vsnprintf(bs->data + bs->used, limit, fmt, ap); | |
218 | va_end(ap); | |
219 | ||
06fa5817 YK |
220 | if (rc >= 0 && (size_t) rc >= limit) { /* not enoght, enlarge */ |
221 | buf_enlarge(bs, (size_t)rc + 1); | |
890e1035 KZ |
222 | limit = bs->sz - bs->used; |
223 | va_start(ap, fmt); | |
224 | rc = vsnprintf(bs->data + bs->used, limit, fmt, ap);; | |
225 | va_end(ap); | |
226 | } | |
227 | ||
228 | if (rc > 0) | |
229 | bs->used += rc; | |
230 | } | |
231 | ||
232 | static void buf_putc_careful(struct buffer *bs, int c) | |
233 | { | |
234 | if (isprint(c) || c == '\a' || c == '\t' || c == '\r' || c == '\n') { | |
235 | buf_enlarge(bs, 1); | |
236 | bs->data[bs->used++] = c; | |
237 | } else if (!isascii(c)) | |
238 | buf_printf(bs, "\\%3o", (unsigned char)c); | |
239 | else { | |
240 | char tmp[] = { '^', c ^ 0x40, '\0' }; | |
241 | buf_puts(bs, tmp); | |
242 | } | |
243 | } | |
244 | ||
d81c3055 KZ |
245 | static char *makemsg(char *fname, char **mvec, int mvecsz, |
246 | size_t *mbufsize, int print_banner) | |
6dbe3af9 | 247 | { |
890e1035 | 248 | struct buffer _bs = {.used = 0}, *bs = &_bs; |
6dbe3af9 | 249 | register int ch, cnt; |
890e1035 | 250 | char *p, *lbuf; |
98e2d4de | 251 | long line_max; |
6dbe3af9 | 252 | |
98e2d4de | 253 | line_max = sysconf(_SC_LINE_MAX); |
479912e7 KZ |
254 | if (line_max <= 0) |
255 | line_max = 512; | |
256 | ||
98e2d4de | 257 | lbuf = xmalloc(line_max); |
d0acbd38 | 258 | |
98e2d4de | 259 | if (print_banner == TRUE) { |
2281e1ed | 260 | char *hostname = xgethostname(); |
d81c3055 KZ |
261 | char *whom, *where, *date; |
262 | struct passwd *pw; | |
263 | time_t now; | |
264 | ||
7eda085c | 265 | if (!(whom = getlogin()) || !*whom) |
6dbe3af9 | 266 | whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; |
98e2d4de | 267 | if (!whom) { |
2b6fc908 | 268 | whom = "someone"; |
98e2d4de SK |
269 | warn(_("cannot get passwd uid")); |
270 | } | |
271 | where = ttyname(STDOUT_FILENO); | |
272 | if (!where) { | |
2b6fc908 | 273 | where = "somewhere"; |
98e2d4de | 274 | warn(_("cannot get tty name")); |
d81c3055 KZ |
275 | } else if (strncmp(where, "/dev/", 5) == 0) |
276 | where += 5; | |
277 | ||
00f41c7d | 278 | time(&now); |
d81c3055 KZ |
279 | date = xstrdup(ctime(&now)); |
280 | date[strlen(date) - 1] = '\0'; | |
6dbe3af9 KZ |
281 | |
282 | /* | |
283 | * all this stuff is to blank out a square for the message; | |
284 | * we wrap message lines at column 79, not 80, because some | |
285 | * terminals wrap after 79, some do not, and we can't tell. | |
286 | * Which means that we may leave a non-blank character | |
287 | * in column 80, but that can't be helped. | |
288 | */ | |
2b6fc908 KZ |
289 | /* snprintf is not always available, but the sprintf's here |
290 | will not overflow as long as %d takes at most 100 chars */ | |
890e1035 KZ |
291 | buf_printf(bs, "\r%*s\r\n", TERM_WIDTH, " "); |
292 | ||
293 | snprintf(lbuf, line_max, | |
294 | _("Broadcast message from %s@%s (%s) (%s):"), | |
295 | whom, hostname, where, date); | |
296 | buf_printf(bs, "%-*.*s\007\007\r\n", TERM_WIDTH, TERM_WIDTH, lbuf); | |
d81c3055 KZ |
297 | free(hostname); |
298 | free(date); | |
6dbe3af9 | 299 | } |
890e1035 | 300 | buf_printf(bs, "%*s\r\n", TERM_WIDTH, " "); |
6dbe3af9 | 301 | |
cdd3cc7f KZ |
302 | if (mvec) { |
303 | /* | |
304 | * Read message from argv[] | |
305 | */ | |
306 | int i; | |
307 | ||
308 | for (i = 0; i < mvecsz; i++) { | |
890e1035 | 309 | buf_puts(bs, mvec[i]); |
cdd3cc7f | 310 | if (i < mvecsz - 1) |
890e1035 | 311 | buf_puts(bs, " "); |
cdd3cc7f | 312 | } |
890e1035 | 313 | buf_puts(bs, "\r\n"); |
cdd3cc7f | 314 | } else { |
66ee8158 | 315 | /* |
cdd3cc7f | 316 | * read message from <file> |
66ee8158 | 317 | */ |
cdd3cc7f KZ |
318 | if (fname) { |
319 | /* | |
320 | * When we are not root, but suid or sgid, refuse to read files | |
321 | * (e.g. device files) that the user may not have access to. | |
322 | * After all, our invoker can easily do "wall < file" | |
323 | * instead of "wall file". | |
324 | */ | |
325 | uid_t uid = getuid(); | |
326 | if (uid && (uid != geteuid() || getgid() != getegid())) | |
327 | errx(EXIT_FAILURE, _("will not read %s - use stdin."), | |
328 | fname); | |
d0acbd38 | 329 | |
cdd3cc7f KZ |
330 | if (!freopen(fname, "r", stdin)) |
331 | err(EXIT_FAILURE, _("cannot open %s"), fname); | |
332 | ||
333 | } | |
66ee8158 | 334 | |
cdd3cc7f KZ |
335 | /* |
336 | * Read message from stdin. | |
337 | */ | |
338 | while (fgets(lbuf, line_max, stdin)) { | |
339 | for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { | |
42e296b5 SK |
340 | if (cnt == TERM_WIDTH || ch == '\n') { |
341 | for (; cnt < TERM_WIDTH; ++cnt) | |
890e1035 KZ |
342 | buf_puts(bs, " "); |
343 | buf_puts(bs, "\r\n"); | |
cdd3cc7f KZ |
344 | cnt = 0; |
345 | } | |
5721ea57 SK |
346 | if (ch == '\t') |
347 | cnt += (7 - (cnt % 8)); | |
cdd3cc7f | 348 | if (ch != '\n') |
890e1035 | 349 | buf_putc_careful(bs, ch); |
726f69e2 | 350 | } |
6dbe3af9 | 351 | } |
66ee8158 | 352 | } |
890e1035 | 353 | buf_printf(bs, "%*s\r\n", TERM_WIDTH, " "); |
98e2d4de SK |
354 | |
355 | free(lbuf); | |
d0acbd38 | 356 | |
890e1035 KZ |
357 | bs->data[bs->used] = '\0'; /* be paranoid */ |
358 | *mbufsize = bs->used; | |
359 | return bs->data; | |
6dbe3af9 | 360 | } |