1 /* $NetBSD: wall.c,v 1.27 2009/02/15 06:06:55 dholland Exp $ */
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93";
42 __RCSID("$NetBSD: wall.c,v 1.27 2009/02/15 06:06:55 dholland Exp $");
46 * This program is not related to David Wall, whose Stanford Ph.D. thesis
47 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
50 #include <sys/param.h>
67 #include "utmpentry.h"
70 void addgroup(char *);
71 void makemsg(const char *);
72 int main(int, char **);
79 struct wallgroup
*next
;
88 main(int argc
, char **argv
)
100 if (setegid(getgid()) == -1)
102 pw
= getpwnam("nobody");
104 (void)check_sender(NULL
, getuid(), egid
);
106 while ((ch
= getopt(argc
, argv
, "g:n")) != -1)
109 /* undoc option for shutdown: suppress banner */
110 if (geteuid() == 0 || (pw
&& getuid() == pw
->pw_uid
))
128 iov
.iov_len
= mbufsize
;
129 (void)getutentries(NULL
, &ep
);
131 for (; ep
; ep
= ep
->next
) {
136 pw
= getpwnam(ep
->name
);
139 for (wg
= grouplist
; wg
&& !ingroup
; wg
= wg
->next
) {
140 if (wg
->gid
== pw
->pw_gid
)
142 for (mem
= wg
->mem
; *mem
&& !ingroup
; mem
++)
143 if (strcmp(ep
->name
, *mem
) == 0)
150 /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
151 if (ep
->line
[0] == ':' && isdigit((unsigned char)ep
->line
[1]))
154 if ((p
= ttymsg(&iov
, 1, ep
->line
, 60*5)) != NULL
)
167 grp
= getgrnam(name
);
168 if ((grp
= getgrnam(name
)) == NULL
)
169 errx(1, "unknown group `%s'", name
);
170 for (i
= 0; grp
->gr_mem
[i
]; i
++)
173 g
= (struct wallgroup
*)malloc(sizeof *g
);
176 g
->gid
= grp
->gr_gid
;
178 g
->mem
= (char **)malloc(i
+ 1);
181 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
182 g
->mem
[i
] = strdup(grp
->gr_mem
[i
]);
183 if (g
->mem
[i
] == NULL
)
192 makemsg(const char *fname
)
201 const char *whom
, *tty
;
202 char *p
, tmpname
[MAXPATHLEN
], lbuf
[100],
203 hostname
[MAXHOSTNAMELEN
+1];
205 (void)snprintf(tmpname
, sizeof tmpname
, "%s/wall.XXXXXX", _PATH_TMP
);
206 if ((fd
= mkstemp(tmpname
)) == -1)
207 err(1, "can't open temporary file");
208 (void)unlink(tmpname
);
209 if (!(fp
= fdopen(fd
, "r+")))
210 err(1, "can't open temporary file");
213 if (!(whom
= getlogin()))
214 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
215 (void)gethostname(hostname
, sizeof(hostname
));
216 hostname
[sizeof(hostname
) - 1] = '\0';
218 lt
= localtime(&now
);
221 * all this stuff is to blank out a square for the message;
222 * we wrap message lines at column 79, not 80, because some
223 * terminals wrap after 79, some do not, and we can't tell.
224 * Which means that we may leave a non-blank character
225 * in column 80, but that can't be helped.
227 (void)fprintf(fp
, "\r%79s\r\n", " ");
228 (void)snprintf(lbuf
, sizeof lbuf
,
229 "Broadcast Message from %s@%s", whom
, hostname
);
230 (void)fprintf(fp
, "%-79.79s\007\007\r\n", lbuf
);
231 tty
= ttyname(STDERR_FILENO
);
234 (void)snprintf(lbuf
, sizeof lbuf
,
235 " (%s) at %d:%02d %s...", tty
,
236 lt
->tm_hour
, lt
->tm_min
, lt
->tm_zone
);
237 (void)fprintf(fp
, "%-79.79s\r\n", lbuf
);
239 (void)fprintf(fp
, "%79s\r\n", " ");
241 if (fname
&& !(freopen(fname
, "r", stdin
)))
242 err(1, "can't read %s", fname
);
243 while (fgets(lbuf
, sizeof(lbuf
), stdin
))
244 for (cnt
= 0, p
= lbuf
; (ch
= *p
) != '\0'; ++p
, ++cnt
) {
245 if (cnt
== 79 || ch
== '\n') {
246 for (; cnt
< 79; ++cnt
)
255 (void)fprintf(fp
, "%79s\r\n", " ");
258 if (fstat(fd
, &sbuf
))
259 err(1, "can't stat temporary file");
260 if ((uint64_t)sbuf
.st_size
> SIZE_T_MAX
)
261 errx(1, "file too big");
262 mbufsize
= sbuf
.st_size
;
263 if (!(mbuf
= malloc(mbufsize
)))
265 if (fread(mbuf
, 1, mbufsize
, fp
) != mbufsize
)
266 err(1, "can't read temporary file");
274 (void)fprintf(stderr
, "usage: %s [-g group] [file]\n", getprogname());