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