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