]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/wall.c
Merge branch 'pg-manual-page-long-options' of https://github.com/jaalto/util-linux
[thirdparty/util-linux.git] / term-utils / wall.c
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 <utmp.h>
60 #include <getopt.h>
61
62 #include "nls.h"
63 #include "xalloc.h"
64 #include "strutils.h"
65 #include "ttymsg.h"
66 #include "pathnames.h"
67 #include "carefulputc.h"
68 #include "c.h"
69 #include "fileutils.h"
70 #include "closestream.h"
71
72 #define WRITE_TIME_OUT 300 /* in seconds */
73
74 /* Function prototypes */
75 static char *makemsg(char *fname, char **mvec, int mvecsz,
76 size_t *mbufsize, int print_banner);
77
78 static void __attribute__((__noreturn__)) usage(FILE *out)
79 {
80 fputs(USAGE_HEADER, out);
81 fprintf(out,
82 _(" %s [options] [<file> | <message>]\n"), program_invocation_short_name);
83 fputs(USAGE_OPTIONS, out);
84 fputs(_(" -n, --nobanner do not print banner, works only for root\n"), out);
85 fputs(_(" -t, --timeout <timeout> write timeout in seconds\n"), out);
86 fputs(USAGE_SEPARATOR, out);
87 fputs(USAGE_HELP, out);
88 fputs(USAGE_VERSION, out);
89 fprintf(out, USAGE_MAN_TAIL("wall(1)"));
90
91 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
92 }
93
94 int main(int argc, char **argv)
95 {
96 int ch;
97 struct iovec iov;
98 struct utmp *utmpptr;
99 char *p;
100 char line[sizeof(utmpptr->ut_line) + 1];
101 int print_banner = TRUE;
102 char *mbuf, *fname = NULL;
103 size_t mbufsize;
104 unsigned timeout = WRITE_TIME_OUT;
105 char **mvec = NULL;
106 int mvecsz = 0;
107
108 static const struct option longopts[] = {
109 { "nobanner", no_argument, 0, 'n' },
110 { "timeout", required_argument, 0, 't' },
111 { "version", no_argument, 0, 'V' },
112 { "help", no_argument, 0, 'h' },
113 { NULL, 0, 0, 0 }
114 };
115
116 setlocale(LC_ALL, "");
117 bindtextdomain(PACKAGE, LOCALEDIR);
118 textdomain(PACKAGE);
119 atexit(close_stdout);
120
121 while ((ch = getopt_long(argc, argv, "nt:Vh", longopts, NULL)) != -1) {
122 switch (ch) {
123 case 'n':
124 if (geteuid() == 0)
125 print_banner = FALSE;
126 else
127 warnx(_("--nobanner is available only for root"));
128 break;
129 case 't':
130 timeout = strtou32_or_err(optarg, _("invalid timeout argument"));
131 if (timeout < 1)
132 errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg);
133 break;
134 case 'V':
135 printf(UTIL_LINUX_VERSION);
136 exit(EXIT_SUCCESS);
137 case 'h':
138 usage(stdout);
139 default:
140 usage(stderr);
141 }
142 }
143 argc -= optind;
144 argv += optind;
145
146 if (argc == 1 && access(argv[0], F_OK) == 0)
147 fname = argv[0];
148 else if (argc >= 1) {
149 mvec = argv;
150 mvecsz = argc;
151 }
152
153 mbuf = makemsg(fname, mvec, mvecsz, &mbufsize, print_banner);
154
155 iov.iov_base = mbuf;
156 iov.iov_len = mbufsize;
157 while((utmpptr = getutent())) {
158 if (!utmpptr->ut_user[0])
159 continue;
160 #ifdef USER_PROCESS
161 if (utmpptr->ut_type != USER_PROCESS)
162 continue;
163 #endif
164 /* Joey Hess reports that use-sessreg in /etc/X11/wdm/
165 produces ut_line entries like :0, and a write
166 to /dev/:0 fails. */
167 if (utmpptr->ut_line[0] == ':')
168 continue;
169
170 xstrncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line));
171 if ((p = ttymsg(&iov, 1, line, timeout)) != NULL)
172 warnx("%s", p);
173 }
174 endutent();
175 free(mbuf);
176 exit(EXIT_SUCCESS);
177 }
178
179 static char *makemsg(char *fname, char **mvec, int mvecsz,
180 size_t *mbufsize, int print_banner)
181 {
182 register int ch, cnt;
183 struct stat sbuf;
184 FILE *fp;
185 char *p, *lbuf, *tmpname, *mbuf;
186 long line_max;
187
188 line_max = sysconf(_SC_LINE_MAX);
189 lbuf = xmalloc(line_max);
190
191 if ((fp = xfmkstemp(&tmpname, NULL)) == NULL)
192 err(EXIT_FAILURE, _("can't open temporary file"));
193 unlink(tmpname);
194 free(tmpname);
195
196 if (print_banner == TRUE) {
197 char *hostname = xgethostname();
198 char *whom, *where, *date;
199 struct passwd *pw;
200 time_t now;
201
202 if (!(whom = getlogin()) || !*whom)
203 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
204 if (!whom) {
205 whom = "someone";
206 warn(_("cannot get passwd uid"));
207 }
208 where = ttyname(STDOUT_FILENO);
209 if (!where) {
210 where = "somewhere";
211 warn(_("cannot get tty name"));
212 } else if (strncmp(where, "/dev/", 5) == 0)
213 where += 5;
214
215 time(&now);
216 date = xstrdup(ctime(&now));
217 date[strlen(date) - 1] = '\0';
218
219 /*
220 * all this stuff is to blank out a square for the message;
221 * we wrap message lines at column 79, not 80, because some
222 * terminals wrap after 79, some do not, and we can't tell.
223 * Which means that we may leave a non-blank character
224 * in column 80, but that can't be helped.
225 */
226 /* snprintf is not always available, but the sprintf's here
227 will not overflow as long as %d takes at most 100 chars */
228 fprintf(fp, "\r%79s\r\n", " ");
229 sprintf(lbuf, _("Broadcast message from %s@%s (%s) (%s):"),
230 whom, hostname, where, date);
231 fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
232 free(hostname);
233 free(date);
234 }
235 fprintf(fp, "%79s\r\n", " ");
236
237 if (mvec) {
238 /*
239 * Read message from argv[]
240 */
241 int i;
242
243 for (i = 0; i < mvecsz; i++) {
244 fputs(mvec[i], fp);
245 if (i < mvecsz - 1)
246 fputc(' ', fp);
247 }
248 fputc('\r', fp);
249 fputc('\n', fp);
250
251 } else {
252 /*
253 * read message from <file>
254 */
255 if (fname) {
256 /*
257 * When we are not root, but suid or sgid, refuse to read files
258 * (e.g. device files) that the user may not have access to.
259 * After all, our invoker can easily do "wall < file"
260 * instead of "wall file".
261 */
262 uid_t uid = getuid();
263 if (uid && (uid != geteuid() || getgid() != getegid()))
264 errx(EXIT_FAILURE, _("will not read %s - use stdin."),
265 fname);
266
267 if (!freopen(fname, "r", stdin))
268 err(EXIT_FAILURE, _("cannot open %s"), fname);
269
270 }
271
272 /*
273 * Read message from stdin.
274 */
275 while (fgets(lbuf, line_max, stdin)) {
276 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
277 if (cnt == 79 || ch == '\n') {
278 for (; cnt < 79; ++cnt)
279 putc(' ', fp);
280 putc('\r', fp);
281 putc('\n', fp);
282 cnt = 0;
283 }
284 if (ch == '\t')
285 cnt += (7 - (cnt % 8));
286 if (ch != '\n')
287 carefulputc(ch, fp, '^');
288 }
289 }
290 }
291 fprintf(fp, "%79s\r\n", " ");
292
293 free(lbuf);
294 rewind(fp);
295
296 if (fstat(fileno(fp), &sbuf))
297 err(EXIT_FAILURE, _("stat failed"));
298
299 *mbufsize = (size_t) sbuf.st_size;
300 mbuf = xmalloc(*mbufsize);
301
302 if (fread(mbuf, 1, *mbufsize, fp) != *mbufsize)
303 err(EXIT_FAILURE, _("fread failed"));
304
305 if (close_stream(fp) != 0)
306 errx(EXIT_FAILURE, _("write error"));
307 return mbuf;
308 }