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