]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/wall.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / login-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@misiek.eu.org>
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 <paths.h>
51 #include <ctype.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <utmp.h>
58
59 #include "nls.h"
60 #include "ttymsg.h"
61 #include "pathnames.h"
62 #include "carefulputc.h"
63
64 void makemsg __P((char *));
65
66 #define IGNOREUSER "sleeper"
67
68 int nobanner;
69 int mbufsize;
70 char *mbuf;
71
72 char *progname = "wall";
73
74 int
75 main(int argc, char **argv) {
76 extern int optind;
77 int ch;
78 struct iovec iov;
79 struct utmp *utmpptr;
80 char *p;
81 char line[sizeof(utmpptr->ut_line) + 1];
82
83 setlocale(LC_ALL, "");
84 bindtextdomain(PACKAGE, LOCALEDIR);
85 textdomain(PACKAGE);
86
87 progname = argv[0];
88 p = rindex(progname, '/');
89 if (p)
90 progname = p+1;
91
92 while ((ch = getopt(argc, argv, "n")) != EOF)
93 switch (ch) {
94 case 'n':
95 /* undoc option for shutdown: suppress banner */
96 if (geteuid() == 0)
97 nobanner = 1;
98 break;
99 case '?':
100 default:
101 usage:
102 (void)fprintf(stderr, _("usage: %s [file]\n"), progname);
103 exit(1);
104 }
105 argc -= optind;
106 argv += optind;
107 if (argc > 1)
108 goto usage;
109
110 makemsg(*argv);
111
112 setutent();
113
114 iov.iov_base = mbuf;
115 iov.iov_len = mbufsize;
116 while((utmpptr = getutent())) {
117 if (!utmpptr->ut_name[0] ||
118 !strncmp(utmpptr->ut_name, IGNOREUSER, sizeof(utmpptr->ut_name)))
119 continue;
120 #ifdef USER_PROCESS
121 if (utmpptr->ut_type != USER_PROCESS)
122 continue;
123 #endif
124 strncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line));
125 line[sizeof(utmpptr->ut_line)-1] = '\0';
126 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
127 (void)fprintf(stderr, "%s: %s\n", progname, p);
128 }
129 endutent();
130 exit(0);
131 }
132
133 void
134 makemsg(fname)
135 char *fname;
136 {
137 register int ch, cnt;
138 struct tm *lt;
139 struct passwd *pw;
140 struct stat sbuf;
141 time_t now;
142 FILE *fp;
143 int fd;
144 char *p, *whom, *where, hostname[MAXHOSTNAMELEN],
145 lbuf[MAXHOSTNAMELEN + 320],
146 tmpname[sizeof(_PATH_TMP) + 20];
147
148 (void)sprintf(tmpname, "%s/wall.XXXXXX", _PATH_TMP);
149 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
150 (void)fprintf(stderr, _("%s: can't open temporary file.\n"), progname);
151 exit(1);
152 }
153 (void)unlink(tmpname);
154
155 if (!nobanner) {
156 if (!(whom = getlogin()) || !*whom)
157 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
158 if (!whom || strlen(whom) > 100)
159 whom = "someone";
160 where = ttyname(2);
161 if (!where || strlen(where) > 100)
162 where = "somewhere";
163 (void)gethostname(hostname, sizeof(hostname));
164 (void)time(&now);
165 lt = localtime(&now);
166
167 /*
168 * all this stuff is to blank out a square for the message;
169 * we wrap message lines at column 79, not 80, because some
170 * terminals wrap after 79, some do not, and we can't tell.
171 * Which means that we may leave a non-blank character
172 * in column 80, but that can't be helped.
173 */
174 /* snprintf is not always available, but the sprintf's here
175 will not overflow as long as %d takes at most 100 chars */
176 (void)fprintf(fp, "\r%79s\r\n", " ");
177 (void)sprintf(lbuf, _("Broadcast Message from %s@%s"),
178 whom, hostname);
179 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
180 (void)sprintf(lbuf, " (%s) at %d:%02d ...",
181 where, lt->tm_hour, lt->tm_min);
182 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
183 }
184 (void)fprintf(fp, "%79s\r\n", " ");
185
186 if (fname) {
187 /*
188 * When we are not root, but suid or sgid, refuse to read files
189 * (e.g. device files) that the user may not have access to.
190 * After all, our invoker can easily do "wall < file"
191 * instead of "wall file".
192 */
193 int uid = getuid();
194 if (uid && (uid != geteuid() || getgid() != getegid())) {
195 fprintf(stderr, _("%s: will not read %s - use stdin.\n"),
196 progname, fname);
197 exit(1);
198 }
199 if (!freopen(fname, "r", stdin)) {
200 fprintf(stderr, _("%s: can't read %s.\n"), progname, fname);
201 exit(1);
202 }
203 }
204
205 while (fgets(lbuf, sizeof(lbuf), stdin)) {
206 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
207 if (cnt == 79 || ch == '\n') {
208 for (; cnt < 79; ++cnt)
209 putc(' ', fp);
210 putc('\r', fp);
211 putc('\n', fp);
212 cnt = 0;
213 } else {
214 carefulputc(ch, fp);
215 }
216 }
217 }
218 (void)fprintf(fp, "%79s\r\n", " ");
219 rewind(fp);
220
221 if (fstat(fd, &sbuf)) {
222 (void)fprintf(stderr, _("%s: can't stat temporary file.\n"), progname);
223 exit(1);
224 }
225 mbufsize = sbuf.st_size;
226 if (!(mbuf = malloc((u_int)mbufsize))) {
227 (void)fprintf(stderr, _("%s: Out of memory!\n"), progname);
228 exit(1);
229 }
230 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
231 (void)fprintf(stderr, _("%s: can't read temporary file.\n"), progname);
232 exit(1);
233 }
234 (void)close(fd);
235 }