]>
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 | 58 | #include <unistd.h> |
b4b919fe | 59 | #include <utmpx.h> |
76cc41b7 | 60 | #include <getopt.h> |
01544c52 JP |
61 | #include <sys/types.h> |
62 | #include <grp.h> | |
66ee8158 | 63 | |
7eda085c | 64 | #include "nls.h" |
4b5156cb | 65 | #include "xalloc.h" |
8abcf290 | 66 | #include "strutils.h" |
66ee8158 | 67 | #include "ttymsg.h" |
726f69e2 | 68 | #include "pathnames.h" |
66ee8158 | 69 | #include "carefulputc.h" |
eb76ca98 | 70 | #include "c.h" |
a000bbb6 | 71 | #include "fileutils.h" |
cdd2a8c3 | 72 | #include "closestream.h" |
726f69e2 | 73 | |
42e296b5 SK |
74 | #define TERM_WIDTH 79 |
75 | #define WRITE_TIME_OUT 300 /* in seconds */ | |
6dbe3af9 | 76 | |
98e2d4de | 77 | /* Function prototypes */ |
cdd3cc7f KZ |
78 | static char *makemsg(char *fname, char **mvec, int mvecsz, |
79 | size_t *mbufsize, int print_banner); | |
6dbe3af9 | 80 | |
76cc41b7 | 81 | static void __attribute__((__noreturn__)) usage(FILE *out) |
dfdf2081 | 82 | { |
bbeae1ce | 83 | fputs(USAGE_HEADER, out); |
a290362b | 84 | fprintf(out, |
bbeae1ce | 85 | _(" %s [options] [<file> | <message>]\n"), program_invocation_short_name); |
451dbcfa BS |
86 | |
87 | fputs(USAGE_SEPARATOR, out); | |
88 | fputs(_("Write a message to all users.\n"), out); | |
89 | ||
bbeae1ce | 90 | fputs(USAGE_OPTIONS, out); |
01544c52 | 91 | fputs(_(" -g, --group <group> only send message to group\n"), out); |
bbeae1ce SK |
92 | fputs(_(" -n, --nobanner do not print banner, works only for root\n"), out); |
93 | fputs(_(" -t, --timeout <timeout> write timeout in seconds\n"), out); | |
94 | fputs(USAGE_SEPARATOR, out); | |
95 | fputs(USAGE_HELP, out); | |
96 | fputs(USAGE_VERSION, out); | |
97 | fprintf(out, USAGE_MAN_TAIL("wall(1)")); | |
76cc41b7 SK |
98 | |
99 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
dfdf2081 DB |
100 | } |
101 | ||
01544c52 JP |
102 | struct group_workspace { |
103 | gid_t requested_group; | |
104 | int ngroups; | |
105 | gid_t *groups; | |
106 | }; | |
107 | ||
108 | static gid_t get_group_gid(const char *optarg) | |
109 | { | |
110 | struct group *gr; | |
e0383e3e | 111 | gid_t gid; |
01544c52 JP |
112 | |
113 | if ((gr = getgrnam(optarg))) | |
114 | return gr->gr_gid; | |
e0383e3e KZ |
115 | |
116 | gid = strtou32_or_err(optarg, _("invalid group argument")); | |
117 | if (!getgrgid(gid)) | |
118 | errx(EXIT_FAILURE, _("%s: unknown gid"), optarg); | |
119 | ||
120 | return gid; | |
01544c52 JP |
121 | } |
122 | ||
123 | static struct group_workspace *init_group_workspace(const char *optarg) | |
124 | { | |
125 | struct group_workspace *buf = xmalloc(sizeof(struct group_workspace)); | |
126 | ||
127 | buf->requested_group = get_group_gid(optarg); | |
128 | buf->ngroups = sysconf(_SC_NGROUPS_MAX) + 1; /* room for the primary gid */ | |
129 | buf->groups = xcalloc(sizeof(gid_t), buf->ngroups); | |
130 | ||
131 | return buf; | |
132 | } | |
133 | ||
134 | static void free_group_workspace(struct group_workspace *buf) | |
135 | { | |
136 | if (!buf) | |
137 | return; | |
138 | ||
139 | free(buf->groups); | |
140 | free(buf); | |
141 | } | |
142 | ||
143 | static int is_gr_member(const char *login, const struct group_workspace *buf) | |
144 | { | |
145 | struct passwd *pw; | |
146 | int ngroups = buf->ngroups; | |
147 | int rc; | |
148 | ||
149 | pw = getpwnam(login); | |
150 | if (buf->requested_group == pw->pw_gid) | |
151 | return 1; | |
152 | ||
153 | rc = getgrouplist(login, pw->pw_gid, buf->groups, &ngroups); | |
154 | if (rc < 0) { | |
155 | /* buffer too small, not sure how this can happen, since | |
156 | we used sysconf to get the size... */ | |
157 | errx(EXIT_FAILURE, | |
158 | _("getgrouplist found more groups than sysconf allows")); | |
159 | } | |
160 | ||
161 | for (; ngroups >= 0; --ngroups) { | |
162 | if (buf->requested_group == buf->groups[ngroups]) | |
163 | return 1; | |
164 | } | |
165 | ||
166 | return 0; | |
167 | } | |
168 | ||
d81c3055 KZ |
169 | int main(int argc, char **argv) |
170 | { | |
6dbe3af9 KZ |
171 | int ch; |
172 | struct iovec iov; | |
b4b919fe | 173 | struct utmpx *utmpptr; |
66ee8158 | 174 | char *p; |
7eda085c | 175 | char line[sizeof(utmpptr->ut_line) + 1]; |
98e2d4de | 176 | int print_banner = TRUE; |
01544c52 | 177 | struct group_workspace *group_buf = NULL; |
cdd3cc7f | 178 | char *mbuf, *fname = NULL; |
98e2d4de | 179 | size_t mbufsize; |
b43d3a3a | 180 | unsigned timeout = WRITE_TIME_OUT; |
cdd3cc7f KZ |
181 | char **mvec = NULL; |
182 | int mvecsz = 0; | |
7eda085c | 183 | |
76cc41b7 SK |
184 | static const struct option longopts[] = { |
185 | { "nobanner", no_argument, 0, 'n' }, | |
cae7485e | 186 | { "timeout", required_argument, 0, 't' }, |
01544c52 | 187 | { "group", required_argument, 0, 'g' }, |
76cc41b7 SK |
188 | { "version", no_argument, 0, 'V' }, |
189 | { "help", no_argument, 0, 'h' }, | |
190 | { NULL, 0, 0, 0 } | |
191 | }; | |
192 | ||
3acc206d SK |
193 | setlocale(LC_ALL, ""); |
194 | bindtextdomain(PACKAGE, LOCALEDIR); | |
195 | textdomain(PACKAGE); | |
196 | atexit(close_stdout); | |
197 | ||
01544c52 | 198 | while ((ch = getopt_long(argc, argv, "nt:g:Vh", longopts, NULL)) != -1) { |
6dbe3af9 KZ |
199 | switch (ch) { |
200 | case 'n': | |
6dbe3af9 | 201 | if (geteuid() == 0) |
98e2d4de | 202 | print_banner = FALSE; |
76cc41b7 SK |
203 | else |
204 | warnx(_("--nobanner is available only for root")); | |
6dbe3af9 | 205 | break; |
cae7485e | 206 | case 't': |
b43d3a3a | 207 | timeout = strtou32_or_err(optarg, _("invalid timeout argument")); |
cae7485e SK |
208 | if (timeout < 1) |
209 | errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg); | |
210 | break; | |
01544c52 JP |
211 | case 'g': |
212 | group_buf = init_group_workspace(optarg); | |
213 | break; | |
76cc41b7 | 214 | case 'V': |
bbeae1ce | 215 | printf(UTIL_LINUX_VERSION); |
76cc41b7 SK |
216 | exit(EXIT_SUCCESS); |
217 | case 'h': | |
218 | usage(stdout); | |
6dbe3af9 | 219 | default: |
76cc41b7 | 220 | usage(stderr); |
6dbe3af9 | 221 | } |
00f41c7d | 222 | } |
6dbe3af9 KZ |
223 | argc -= optind; |
224 | argv += optind; | |
6dbe3af9 | 225 | |
cdd3cc7f KZ |
226 | if (argc == 1 && access(argv[0], F_OK) == 0) |
227 | fname = argv[0]; | |
228 | else if (argc >= 1) { | |
229 | mvec = argv; | |
230 | mvecsz = argc; | |
231 | } | |
232 | ||
233 | mbuf = makemsg(fname, mvec, mvecsz, &mbufsize, print_banner); | |
7eda085c | 234 | |
6dbe3af9 KZ |
235 | iov.iov_base = mbuf; |
236 | iov.iov_len = mbufsize; | |
b4b919fe | 237 | while((utmpptr = getutxent())) { |
cfa7fe89 | 238 | if (!utmpptr->ut_user[0]) |
7eda085c KZ |
239 | continue; |
240 | #ifdef USER_PROCESS | |
241 | if (utmpptr->ut_type != USER_PROCESS) | |
6dbe3af9 | 242 | continue; |
6dbe3af9 | 243 | #endif |
e8f26419 KZ |
244 | /* Joey Hess reports that use-sessreg in /etc/X11/wdm/ |
245 | produces ut_line entries like :0, and a write | |
246 | to /dev/:0 fails. */ | |
247 | if (utmpptr->ut_line[0] == ':') | |
248 | continue; | |
249 | ||
01544c52 JP |
250 | if (group_buf && !is_gr_member(utmpptr->ut_user, group_buf)) |
251 | continue; | |
252 | ||
c07ebfa1 | 253 | xstrncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line)); |
cae7485e | 254 | if ((p = ttymsg(&iov, 1, line, timeout)) != NULL) |
aa00c136 | 255 | warnx("%s", p); |
6dbe3af9 | 256 | } |
b4b919fe | 257 | endutxent(); |
98e2d4de | 258 | free(mbuf); |
01544c52 | 259 | free_group_workspace(group_buf); |
d0acbd38 | 260 | exit(EXIT_SUCCESS); |
6dbe3af9 KZ |
261 | } |
262 | ||
890e1035 KZ |
263 | struct buffer { |
264 | size_t sz; | |
265 | size_t used; | |
266 | char *data; | |
267 | }; | |
268 | ||
269 | static void buf_enlarge(struct buffer *bs, size_t len) | |
270 | { | |
271 | if (bs->sz == 0 || len > bs->sz - bs->used) { | |
272 | bs->sz += len < 128 ? 128 : len; | |
273 | bs->data = xrealloc(bs->data, bs->sz); | |
274 | } | |
275 | } | |
276 | ||
277 | static void buf_puts(struct buffer *bs, const char *s) | |
278 | { | |
279 | size_t len = strlen(s); | |
280 | ||
281 | buf_enlarge(bs, len + 1); | |
282 | memcpy(bs->data + bs->used, s, len + 1); | |
283 | bs->used += len; | |
284 | } | |
285 | ||
286 | static void buf_printf(struct buffer *bs, const char *fmt, ...) | |
287 | { | |
288 | int rc; | |
289 | va_list ap; | |
290 | size_t limit; | |
291 | ||
292 | buf_enlarge(bs, 0); /* default size */ | |
293 | limit = bs->sz - bs->used; | |
294 | ||
295 | va_start(ap, fmt); | |
296 | rc = vsnprintf(bs->data + bs->used, limit, fmt, ap); | |
297 | va_end(ap); | |
298 | ||
9e930041 | 299 | if (rc >= 0 && (size_t) rc >= limit) { /* not enough, enlarge */ |
06fa5817 | 300 | buf_enlarge(bs, (size_t)rc + 1); |
890e1035 KZ |
301 | limit = bs->sz - bs->used; |
302 | va_start(ap, fmt); | |
303 | rc = vsnprintf(bs->data + bs->used, limit, fmt, ap);; | |
304 | va_end(ap); | |
305 | } | |
306 | ||
307 | if (rc > 0) | |
308 | bs->used += rc; | |
309 | } | |
310 | ||
311 | static void buf_putc_careful(struct buffer *bs, int c) | |
312 | { | |
313 | if (isprint(c) || c == '\a' || c == '\t' || c == '\r' || c == '\n') { | |
314 | buf_enlarge(bs, 1); | |
315 | bs->data[bs->used++] = c; | |
316 | } else if (!isascii(c)) | |
317 | buf_printf(bs, "\\%3o", (unsigned char)c); | |
318 | else { | |
319 | char tmp[] = { '^', c ^ 0x40, '\0' }; | |
320 | buf_puts(bs, tmp); | |
321 | } | |
322 | } | |
323 | ||
d81c3055 KZ |
324 | static char *makemsg(char *fname, char **mvec, int mvecsz, |
325 | size_t *mbufsize, int print_banner) | |
6dbe3af9 | 326 | { |
890e1035 | 327 | struct buffer _bs = {.used = 0}, *bs = &_bs; |
6dbe3af9 | 328 | register int ch, cnt; |
890e1035 | 329 | char *p, *lbuf; |
98e2d4de | 330 | long line_max; |
6dbe3af9 | 331 | |
98e2d4de | 332 | line_max = sysconf(_SC_LINE_MAX); |
479912e7 KZ |
333 | if (line_max <= 0) |
334 | line_max = 512; | |
335 | ||
98e2d4de | 336 | lbuf = xmalloc(line_max); |
d0acbd38 | 337 | |
98e2d4de | 338 | if (print_banner == TRUE) { |
2281e1ed | 339 | char *hostname = xgethostname(); |
d81c3055 KZ |
340 | char *whom, *where, *date; |
341 | struct passwd *pw; | |
342 | time_t now; | |
343 | ||
7eda085c | 344 | if (!(whom = getlogin()) || !*whom) |
6dbe3af9 | 345 | whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; |
98e2d4de | 346 | if (!whom) { |
2b6fc908 | 347 | whom = "someone"; |
98e2d4de SK |
348 | warn(_("cannot get passwd uid")); |
349 | } | |
350 | where = ttyname(STDOUT_FILENO); | |
351 | if (!where) { | |
2b6fc908 | 352 | where = "somewhere"; |
98e2d4de | 353 | warn(_("cannot get tty name")); |
d81c3055 KZ |
354 | } else if (strncmp(where, "/dev/", 5) == 0) |
355 | where += 5; | |
356 | ||
00f41c7d | 357 | time(&now); |
d81c3055 KZ |
358 | date = xstrdup(ctime(&now)); |
359 | date[strlen(date) - 1] = '\0'; | |
6dbe3af9 KZ |
360 | |
361 | /* | |
362 | * all this stuff is to blank out a square for the message; | |
363 | * we wrap message lines at column 79, not 80, because some | |
364 | * terminals wrap after 79, some do not, and we can't tell. | |
365 | * Which means that we may leave a non-blank character | |
366 | * in column 80, but that can't be helped. | |
367 | */ | |
2b6fc908 KZ |
368 | /* snprintf is not always available, but the sprintf's here |
369 | will not overflow as long as %d takes at most 100 chars */ | |
890e1035 KZ |
370 | buf_printf(bs, "\r%*s\r\n", TERM_WIDTH, " "); |
371 | ||
372 | snprintf(lbuf, line_max, | |
373 | _("Broadcast message from %s@%s (%s) (%s):"), | |
374 | whom, hostname, where, date); | |
375 | buf_printf(bs, "%-*.*s\007\007\r\n", TERM_WIDTH, TERM_WIDTH, lbuf); | |
d81c3055 KZ |
376 | free(hostname); |
377 | free(date); | |
6dbe3af9 | 378 | } |
890e1035 | 379 | buf_printf(bs, "%*s\r\n", TERM_WIDTH, " "); |
6dbe3af9 | 380 | |
cdd3cc7f KZ |
381 | if (mvec) { |
382 | /* | |
383 | * Read message from argv[] | |
384 | */ | |
385 | int i; | |
386 | ||
387 | for (i = 0; i < mvecsz; i++) { | |
890e1035 | 388 | buf_puts(bs, mvec[i]); |
cdd3cc7f | 389 | if (i < mvecsz - 1) |
890e1035 | 390 | buf_puts(bs, " "); |
cdd3cc7f | 391 | } |
890e1035 | 392 | buf_puts(bs, "\r\n"); |
cdd3cc7f | 393 | } else { |
66ee8158 | 394 | /* |
cdd3cc7f | 395 | * read message from <file> |
66ee8158 | 396 | */ |
cdd3cc7f KZ |
397 | if (fname) { |
398 | /* | |
399 | * When we are not root, but suid or sgid, refuse to read files | |
400 | * (e.g. device files) that the user may not have access to. | |
401 | * After all, our invoker can easily do "wall < file" | |
402 | * instead of "wall file". | |
403 | */ | |
404 | uid_t uid = getuid(); | |
405 | if (uid && (uid != geteuid() || getgid() != getegid())) | |
406 | errx(EXIT_FAILURE, _("will not read %s - use stdin."), | |
407 | fname); | |
d0acbd38 | 408 | |
cdd3cc7f KZ |
409 | if (!freopen(fname, "r", stdin)) |
410 | err(EXIT_FAILURE, _("cannot open %s"), fname); | |
411 | ||
412 | } | |
66ee8158 | 413 | |
cdd3cc7f KZ |
414 | /* |
415 | * Read message from stdin. | |
416 | */ | |
417 | while (fgets(lbuf, line_max, stdin)) { | |
418 | for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { | |
42e296b5 SK |
419 | if (cnt == TERM_WIDTH || ch == '\n') { |
420 | for (; cnt < TERM_WIDTH; ++cnt) | |
890e1035 KZ |
421 | buf_puts(bs, " "); |
422 | buf_puts(bs, "\r\n"); | |
cdd3cc7f KZ |
423 | cnt = 0; |
424 | } | |
5721ea57 SK |
425 | if (ch == '\t') |
426 | cnt += (7 - (cnt % 8)); | |
cdd3cc7f | 427 | if (ch != '\n') |
890e1035 | 428 | buf_putc_careful(bs, ch); |
726f69e2 | 429 | } |
6dbe3af9 | 430 | } |
66ee8158 | 431 | } |
890e1035 | 432 | buf_printf(bs, "%*s\r\n", TERM_WIDTH, " "); |
98e2d4de SK |
433 | |
434 | free(lbuf); | |
d0acbd38 | 435 | |
890e1035 KZ |
436 | bs->data[bs->used] = '\0'; /* be paranoid */ |
437 | *mbufsize = bs->used; | |
438 | return bs->data; | |
6dbe3af9 | 439 | } |