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