wall.c revision 83082
114408Sjkh/* 214408Sjkh * Copyright (c) 1988, 1990, 1993 314408Sjkh * The Regents of the University of California. All rights reserved. 414408Sjkh * 514408Sjkh * Redistribution and use in source and binary forms, with or without 614408Sjkh * modification, are permitted provided that the following conditions 714408Sjkh * are met: 814408Sjkh * 1. Redistributions of source code must retain the above copyright 914408Sjkh * notice, this list of conditions and the following disclaimer. 1014408Sjkh * 2. Redistributions in binary form must reproduce the above copyright 1114408Sjkh * notice, this list of conditions and the following disclaimer in the 1214408Sjkh * documentation and/or other materials provided with the distribution. 1314408Sjkh * 3. All advertising materials mentioning features or use of this software 1414408Sjkh * must display the following acknowledgement: 1514408Sjkh * This product includes software developed by the University of 1614408Sjkh * California, Berkeley and its contributors. 1714408Sjkh * 4. Neither the name of the University nor the names of its contributors 1814408Sjkh * may be used to endorse or promote products derived from this software 1914408Sjkh * without specific prior written permission. 2014408Sjkh * 2114408Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2214408Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2314408Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2414408Sjkh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2514408Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2614408Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2714408Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2814408Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2914408Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3014408Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3114408Sjkh * SUCH DAMAGE. 3214408Sjkh */ 3314408Sjkh 3414408Sjkh#ifndef lint 3514408Sjkhstatic const char copyright[] = 3614408Sjkh"@(#) Copyright (c) 1988, 1990, 1993\n\ 3714408Sjkh The Regents of the University of California. All rights reserved.\n"; 3814408Sjkh#endif /* not lint */ 3914408Sjkh 4014408Sjkh#ifndef lint 4114408Sjkh#if 0 4214408Sjkhstatic char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 4314408Sjkh#endif 4414408Sjkhstatic const char rcsid[] = 4514408Sjkh "$FreeBSD: head/usr.bin/wall/wall.c 83082 2001-09-05 15:31:07Z ru $"; 4614408Sjkh#endif /* not lint */ 4714408Sjkh 4814408Sjkh/* 4914408Sjkh * This program is not related to David Wall, whose Stanford Ph.D. thesis 5014408Sjkh * is entitled "Mechanisms for Broadcast and Selective Broadcast". 5114408Sjkh */ 5214408Sjkh 5314408Sjkh#include <sys/param.h> 5414408Sjkh#include <sys/stat.h> 5514408Sjkh#include <sys/uio.h> 5614408Sjkh 5714408Sjkh#include <ctype.h> 5814408Sjkh#include <err.h> 5914408Sjkh#include <grp.h> 6014408Sjkh#include <locale.h> 6114408Sjkh#include <paths.h> 6214408Sjkh#include <pwd.h> 6314408Sjkh#include <stdio.h> 6414408Sjkh#include <stdlib.h> 6514408Sjkh#include <string.h> 6614408Sjkh#include <time.h> 6714408Sjkh#include <unistd.h> 6814408Sjkh#include <utmp.h> 6914408Sjkh 7014408Sjkhstatic void makemsg(char *); 7114408Sjkhstatic void usage(void); 7214408Sjkhchar *ttymsg(struct iovec *, int, const char *, int); 7314408Sjkh 7414408Sjkh#define IGNOREUSER "sleeper" 7514408Sjkh 7614408Sjkhstruct wallgroup { 7714408Sjkh struct wallgroup *next; 7814408Sjkh char *name; 7914408Sjkh gid_t gid; 8014408Sjkh} *grouplist; 8114408Sjkhint nobanner; 8214408Sjkhint mbufsize; 8314408Sjkhchar *mbuf; 8414408Sjkh 8514408Sjkhint 8614408Sjkhmain(int argc, char *argv[]) 8714408Sjkh{ 8814408Sjkh struct iovec iov; 8914408Sjkh struct utmp utmp; 9014408Sjkh int ch; 9114408Sjkh int ingroup; 9214408Sjkh FILE *fp; 9314408Sjkh struct wallgroup *g; 9418194Ssos struct group *grp; 9514408Sjkh char *p, **np; 9614408Sjkh struct passwd *pw; 9714408Sjkh char line[sizeof(utmp.ut_line) + 1]; 9814408Sjkh char username[sizeof(utmp.ut_name) + 1]; 9914408Sjkh 10014408Sjkh (void)setlocale(LC_CTYPE, ""); 10114408Sjkh 10214408Sjkh while ((ch = getopt(argc, argv, "g:n")) != -1) 10314408Sjkh switch (ch) { 10414408Sjkh case 'n': 10514408Sjkh /* undoc option for shutdown: suppress banner */ 10614408Sjkh if (geteuid() == 0) 10714408Sjkh nobanner = 1; 10814408Sjkh break; 10914408Sjkh case 'g': 11014408Sjkh g = (struct wallgroup *)malloc(sizeof *g); 11114408Sjkh g->next = grouplist; 11214408Sjkh g->name = optarg; 113 g->gid = -1; 114 grouplist = g; 115 break; 116 case '?': 117 default: 118 usage(); 119 } 120 argc -= optind; 121 argv += optind; 122 if (argc > 1) 123 usage(); 124 125 for (g = grouplist; g; g = g->next) { 126 grp = getgrnam(g->name); 127 if (grp != NULL) 128 g->gid = grp->gr_gid; 129 else 130 warnx("%s: no such group", g->name); 131 } 132 133 makemsg(*argv); 134 135 if (!(fp = fopen(_PATH_UTMP, "r"))) 136 err(1, "cannot read %s", _PATH_UTMP); 137 iov.iov_base = mbuf; 138 iov.iov_len = mbufsize; 139 /* NOSTRICT */ 140 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 141 if (!utmp.ut_name[0] || 142 !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 143 continue; 144 if (grouplist) { 145 ingroup = 0; 146 strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name)); 147 pw = getpwnam(username); 148 if (!pw) 149 continue; 150 for (g = grouplist; g && ingroup == 0; g = g->next) { 151 if (g->gid == -1) 152 continue; 153 if (g->gid == pw->pw_gid) 154 ingroup = 1; 155 else if ((grp = getgrgid(g->gid)) != NULL) { 156 for (np = grp->gr_mem; *np; np++) { 157 if (strcmp(*np, username) == 0) { 158 ingroup = 1; 159 break; 160 } 161 } 162 } 163 } 164 if (ingroup == 0) 165 continue; 166 } 167 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line)); 168 line[sizeof(utmp.ut_line)] = '\0'; 169 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL) 170 warnx("%s", p); 171 } 172 exit(0); 173} 174 175static void 176usage() 177{ 178 (void)fprintf(stderr, "usage: wall [-g group] [file]\n"); 179 exit(1); 180} 181 182void 183makemsg(char *fname) 184{ 185 int cnt; 186 unsigned char ch; 187 struct tm *lt; 188 struct passwd *pw; 189 struct stat sbuf; 190 time_t now; 191 FILE *fp; 192 int fd; 193 char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 194 const char *whom; 195 gid_t egid; 196 197 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 198 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 199 err(1, "can't open temporary file"); 200 (void)unlink(tmpname); 201 202 if (!nobanner) { 203 tty = ttyname(STDERR_FILENO); 204 if (tty == NULL) 205 tty = "no tty"; 206 207 if (!(whom = getlogin())) 208 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 209 (void)gethostname(hostname, sizeof(hostname)); 210 (void)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 (void)fprintf(fp, "\r%79s\r\n", " "); 221 (void)snprintf(lbuf, sizeof(lbuf), 222 "Broadcast Message from %s@%s", 223 whom, hostname); 224 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 225 (void)snprintf(lbuf, sizeof(lbuf), 226 " (%s) at %d:%02d %s...", tty, 227 lt->tm_hour, lt->tm_min, lt->tm_zone); 228 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 229 } 230 (void)fprintf(fp, "%79s\r\n", " "); 231 232 if (fname) { 233 egid = getegid(); 234 setegid(getgid()); 235 if (freopen(fname, "r", stdin) == NULL) 236 err(1, "can't read %s", fname); 237 setegid(egid); 238 } 239 while (fgets(lbuf, sizeof(lbuf), stdin)) 240 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 241 if (ch == '\r') { 242 cnt = 0; 243 } else if (cnt == 79 || ch == '\n') { 244 for (; cnt < 79; ++cnt) 245 putc(' ', fp); 246 putc('\r', fp); 247 putc('\n', fp); 248 cnt = 0; 249 } else if (((ch & 0x80) && ch < 0xA0) || 250 /* disable upper controls */ 251 (!isprint(ch) && !isspace(ch) && 252 ch != '\a' && ch != '\b') 253 ) { 254 if (ch & 0x80) { 255 ch &= 0x7F; 256 putc('M', fp); 257 if (++cnt == 79) { 258 putc('\r', fp); 259 putc('\n', fp); 260 cnt = 0; 261 } 262 putc('-', fp); 263 if (++cnt == 79) { 264 putc('\r', fp); 265 putc('\n', fp); 266 cnt = 0; 267 } 268 } 269 if (iscntrl(ch)) { 270 ch ^= 040; 271 putc('^', fp); 272 if (++cnt == 79) { 273 putc('\r', fp); 274 putc('\n', fp); 275 cnt = 0; 276 } 277 } 278 putc(ch, fp); 279 } else { 280 putc(ch, fp); 281 } 282 } 283 (void)fprintf(fp, "%79s\r\n", " "); 284 rewind(fp); 285 286 if (fstat(fd, &sbuf)) 287 err(1, "can't stat temporary file"); 288 mbufsize = sbuf.st_size; 289 if (!(mbuf = malloc((u_int)mbufsize))) 290 err(1, "out of memory"); 291 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 292 err(1, "can't read temporary file"); 293 (void)close(fd); 294} 295