1 /* $NetBSD: wall.c,v 1.29 2011/09/06 18:45:21 joerg 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.29 2011/09/06 18:45:21 joerg 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 static void addgroup(char *);
71 static void makemsg(const char *);
72 __dead
static void usage(void);
74 static struct wallgroup
{
78 struct wallgroup
*next
;
82 static size_t mbufsize
;
87 main(int argc
, char **argv
)
99 if (setegid(getgid()) == -1)
101 pw
= getpwnam("nobody");
103 (void)check_sender(NULL
, getuid(), egid
);
105 while ((ch
= getopt(argc
, argv
, "g:n")) != -1)
108 /* undoc option for shutdown: suppress banner */
109 if (geteuid() == 0 || (pw
&& getuid() == pw
->pw_uid
))
127 iov
.iov_len
= mbufsize
;
128 (void)getutentries(NULL
, &ep
);
130 for (; ep
; ep
= ep
->next
) {
135 pw
= getpwnam(ep
->name
);
138 for (wg
= grouplist
; wg
&& !ingroup
; wg
= wg
->next
) {
139 if (wg
->gid
== pw
->pw_gid
)
141 for (mem
= wg
->mem
; *mem
&& !ingroup
; mem
++)
142 if (strcmp(ep
->name
, *mem
) == 0)
149 /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
150 if (ep
->line
[0] == ':' && isdigit((unsigned char)ep
->line
[1]))
153 if ((p
= ttymsg(&iov
, 1, ep
->line
, 60*5)) != NULL
)
166 grp
= getgrnam(name
);
167 if ((grp
= getgrnam(name
)) == NULL
)
168 errx(1, "unknown group `%s'", name
);
169 for (i
= 0; grp
->gr_mem
[i
]; i
++)
172 g
= (struct wallgroup
*)malloc(sizeof *g
);
175 g
->gid
= grp
->gr_gid
;
177 g
->mem
= (char **)malloc(i
+ 1);
180 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
181 g
->mem
[i
] = strdup(grp
->gr_mem
[i
]);
182 if (g
->mem
[i
] == NULL
)
191 makemsg(const char *fname
)
200 const char *whom
, *tty
;
201 char *p
, tmpname
[MAXPATHLEN
], lbuf
[100],
202 hostname
[MAXHOSTNAMELEN
+1];
204 (void)snprintf(tmpname
, sizeof tmpname
, "%s/wall.XXXXXX", _PATH_TMP
);
205 if ((fd
= mkstemp(tmpname
)) == -1)
206 err(1, "can't open temporary file");
207 (void)unlink(tmpname
);
208 if (!(fp
= fdopen(fd
, "r+")))
209 err(1, "can't open temporary file");
212 if (!(whom
= getlogin()))
213 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
214 (void)gethostname(hostname
, sizeof(hostname
));
215 hostname
[sizeof(hostname
) - 1] = '\0';
217 lt
= localtime(&now
);
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.
226 (void)fprintf(fp
, "\r%79s\r\n", " ");
227 (void)snprintf(lbuf
, sizeof lbuf
,
228 "Broadcast Message from %s@%s", whom
, hostname
);
229 (void)fprintf(fp
, "%-79.79s\007\007\r\n", lbuf
);
230 tty
= ttyname(STDERR_FILENO
);
233 (void)snprintf(lbuf
, sizeof lbuf
,
234 " (%s) at %d:%02d %s...", tty
,
235 lt
->tm_hour
, lt
->tm_min
, lt
->tm_zone
);
236 (void)fprintf(fp
, "%-79.79s\r\n", lbuf
);
238 (void)fprintf(fp
, "%79s\r\n", " ");
240 if (fname
&& !(freopen(fname
, "r", stdin
)))
241 err(1, "can't read %s", fname
);
242 while (fgets(lbuf
, sizeof(lbuf
), stdin
))
243 for (cnt
= 0, p
= lbuf
; (ch
= *p
) != '\0'; ++p
, ++cnt
) {
244 if (cnt
== 79 || ch
== '\n') {
245 for (; cnt
< 79; ++cnt
)
254 (void)fprintf(fp
, "%79s\r\n", " ");
257 if (fstat(fd
, &sbuf
))
258 err(1, "can't stat temporary file");
259 if ((uint64_t)sbuf
.st_size
> SIZE_T_MAX
)
260 errx(1, "file too big");
261 mbufsize
= sbuf
.st_size
;
262 if (!(mbuf
= malloc(mbufsize
)))
264 if (fread(mbuf
, 1, mbufsize
, fp
) != mbufsize
)
265 err(1, "can't read temporary file");
273 (void)fprintf(stderr
, "usage: %s [-g group] [file]\n", getprogname());