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