19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1988, 1990, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 34075f2939SMark Murray #include <sys/cdefs.h> 35075f2939SMark Murray 36075f2939SMark Murray __FBSDID("$FreeBSD$"); 37075f2939SMark Murray 389b50d902SRodney W. Grimes #ifndef lint 39752d887aSPhilippe Charnier static const char copyright[] = 409b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1990, 1993\n\ 419b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 42075f2939SMark Murray #endif 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #ifndef lint 45075f2939SMark Murray static const char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 46752d887aSPhilippe Charnier #endif 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes /* 499b50d902SRodney W. Grimes * This program is not related to David Wall, whose Stanford Ph.D. thesis 509b50d902SRodney W. Grimes * is entitled "Mechanisms for Broadcast and Selective Broadcast". 519b50d902SRodney W. Grimes */ 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes #include <sys/param.h> 549b50d902SRodney W. Grimes #include <sys/stat.h> 559b50d902SRodney W. Grimes #include <sys/uio.h> 569b50d902SRodney W. Grimes 57752d887aSPhilippe Charnier #include <ctype.h> 58752d887aSPhilippe Charnier #include <err.h> 596a20d55aSWarner Losh #include <grp.h> 609566348dSAndrey A. Chernov #include <locale.h> 619b50d902SRodney W. Grimes #include <paths.h> 629b50d902SRodney W. Grimes #include <pwd.h> 639b50d902SRodney W. Grimes #include <stdio.h> 649b50d902SRodney W. Grimes #include <stdlib.h> 659b50d902SRodney W. Grimes #include <string.h> 6658011702SAndrey A. Chernov #include <time.h> 679b50d902SRodney W. Grimes #include <unistd.h> 689b50d902SRodney W. Grimes #include <utmp.h> 699b50d902SRodney W. Grimes 70728d043eSDima Dorfman #include "ttymsg.h" 71728d043eSDima Dorfman 726a20d55aSWarner Losh static void makemsg(char *); 736a20d55aSWarner Losh static void usage(void); 749b50d902SRodney W. Grimes 756a20d55aSWarner Losh struct wallgroup { 766a20d55aSWarner Losh struct wallgroup *next; 776a20d55aSWarner Losh char *name; 786a20d55aSWarner Losh gid_t gid; 796a20d55aSWarner Losh } *grouplist; 809b50d902SRodney W. Grimes int nobanner; 819b50d902SRodney W. Grimes int mbufsize; 829b50d902SRodney W. Grimes char *mbuf; 839b50d902SRodney W. Grimes 84b457a3e1SOlivier Houchard static int 85b457a3e1SOlivier Houchard ttystat(char *line, int sz) 86b457a3e1SOlivier Houchard { 87b457a3e1SOlivier Houchard struct stat sb; 88b457a3e1SOlivier Houchard char ttybuf[MAXPATHLEN]; 89b457a3e1SOlivier Houchard 90b457a3e1SOlivier Houchard (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line); 91b457a3e1SOlivier Houchard if (stat(ttybuf, &sb) == 0) { 92b457a3e1SOlivier Houchard return (0); 93b457a3e1SOlivier Houchard } else 94b457a3e1SOlivier Houchard return (-1); 95b457a3e1SOlivier Houchard } 96b457a3e1SOlivier Houchard 979b50d902SRodney W. Grimes int 986a20d55aSWarner Losh main(int argc, char *argv[]) 999b50d902SRodney W. Grimes { 1009b50d902SRodney W. Grimes struct iovec iov; 1019b50d902SRodney W. Grimes struct utmp utmp; 1026a20d55aSWarner Losh int ch; 1032cfec5adSRuslan Ermilov int ingroup; 1049b50d902SRodney W. Grimes FILE *fp; 1056a20d55aSWarner Losh struct wallgroup *g; 1066a20d55aSWarner Losh struct group *grp; 107728d043eSDima Dorfman char **np; 108728d043eSDima Dorfman const char *p; 1096a20d55aSWarner Losh struct passwd *pw; 1109b50d902SRodney W. Grimes char line[sizeof(utmp.ut_line) + 1]; 1116a20d55aSWarner Losh char username[sizeof(utmp.ut_name) + 1]; 1129b50d902SRodney W. Grimes 1139566348dSAndrey A. Chernov (void)setlocale(LC_CTYPE, ""); 1149566348dSAndrey A. Chernov 1156a20d55aSWarner Losh while ((ch = getopt(argc, argv, "g:n")) != -1) 1169b50d902SRodney W. Grimes switch (ch) { 1179b50d902SRodney W. Grimes case 'n': 1189b50d902SRodney W. Grimes /* undoc option for shutdown: suppress banner */ 1199b50d902SRodney W. Grimes if (geteuid() == 0) 1209b50d902SRodney W. Grimes nobanner = 1; 1219b50d902SRodney W. Grimes break; 1226a20d55aSWarner Losh case 'g': 1236a20d55aSWarner Losh g = (struct wallgroup *)malloc(sizeof *g); 1246a20d55aSWarner Losh g->next = grouplist; 1256a20d55aSWarner Losh g->name = optarg; 1266a20d55aSWarner Losh g->gid = -1; 1276a20d55aSWarner Losh grouplist = g; 1286a20d55aSWarner Losh break; 1299b50d902SRodney W. Grimes case '?': 1309b50d902SRodney W. Grimes default: 131752d887aSPhilippe Charnier usage(); 1329b50d902SRodney W. Grimes } 1339b50d902SRodney W. Grimes argc -= optind; 1349b50d902SRodney W. Grimes argv += optind; 1359b50d902SRodney W. Grimes if (argc > 1) 136752d887aSPhilippe Charnier usage(); 1379b50d902SRodney W. Grimes 1386a20d55aSWarner Losh for (g = grouplist; g; g = g->next) { 1396a20d55aSWarner Losh grp = getgrnam(g->name); 140003ff943SWarner Losh if (grp != NULL) 1416a20d55aSWarner Losh g->gid = grp->gr_gid; 142003ff943SWarner Losh else 143003ff943SWarner Losh warnx("%s: no such group", g->name); 1446a20d55aSWarner Losh } 1456a20d55aSWarner Losh 1469b50d902SRodney W. Grimes makemsg(*argv); 1479b50d902SRodney W. Grimes 148752d887aSPhilippe Charnier if (!(fp = fopen(_PATH_UTMP, "r"))) 14956e7ae90SKris Kennaway err(1, "cannot read %s", _PATH_UTMP); 1509b50d902SRodney W. Grimes iov.iov_base = mbuf; 1519b50d902SRodney W. Grimes iov.iov_len = mbufsize; 1529b50d902SRodney W. Grimes /* NOSTRICT */ 1539b50d902SRodney W. Grimes while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 154ca78e3fbSTim J. Robbins if (!utmp.ut_name[0]) 1559b50d902SRodney W. Grimes continue; 156b457a3e1SOlivier Houchard if (ttystat(utmp.ut_line, UT_LINESIZE) != 0) 157b457a3e1SOlivier Houchard continue; 1586a20d55aSWarner Losh if (grouplist) { 1592cfec5adSRuslan Ermilov ingroup = 0; 1606a20d55aSWarner Losh strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name)); 1616a20d55aSWarner Losh pw = getpwnam(username); 1626a20d55aSWarner Losh if (!pw) 1636a20d55aSWarner Losh continue; 1646a20d55aSWarner Losh for (g = grouplist; g && ingroup == 0; g = g->next) { 165075f2939SMark Murray if (g->gid == (gid_t)-1) 1666a20d55aSWarner Losh continue; 1676a20d55aSWarner Losh if (g->gid == pw->pw_gid) 1686a20d55aSWarner Losh ingroup = 1; 1692cfec5adSRuslan Ermilov else if ((grp = getgrgid(g->gid)) != NULL) { 1702cfec5adSRuslan Ermilov for (np = grp->gr_mem; *np; np++) { 1712cfec5adSRuslan Ermilov if (strcmp(*np, username) == 0) { 1726a20d55aSWarner Losh ingroup = 1; 1732cfec5adSRuslan Ermilov break; 1742cfec5adSRuslan Ermilov } 1752cfec5adSRuslan Ermilov } 1762cfec5adSRuslan Ermilov } 1776a20d55aSWarner Losh } 1786a20d55aSWarner Losh if (ingroup == 0) 1796a20d55aSWarner Losh continue; 1806a20d55aSWarner Losh } 1819b50d902SRodney W. Grimes strncpy(line, utmp.ut_line, sizeof(utmp.ut_line)); 1829b50d902SRodney W. Grimes line[sizeof(utmp.ut_line)] = '\0'; 1839b50d902SRodney W. Grimes if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL) 184752d887aSPhilippe Charnier warnx("%s", p); 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes exit(0); 1879b50d902SRodney W. Grimes } 1889b50d902SRodney W. Grimes 189752d887aSPhilippe Charnier static void 190752d887aSPhilippe Charnier usage() 191752d887aSPhilippe Charnier { 192003ff943SWarner Losh (void)fprintf(stderr, "usage: wall [-g group] [file]\n"); 193752d887aSPhilippe Charnier exit(1); 194752d887aSPhilippe Charnier } 195752d887aSPhilippe Charnier 1969b50d902SRodney W. Grimes void 1976a20d55aSWarner Losh makemsg(char *fname) 1989b50d902SRodney W. Grimes { 1996a20d55aSWarner Losh int cnt; 2006a20d55aSWarner Losh unsigned char ch; 2019b50d902SRodney W. Grimes struct tm *lt; 2029b50d902SRodney W. Grimes struct passwd *pw; 2039b50d902SRodney W. Grimes struct stat sbuf; 20458011702SAndrey A. Chernov time_t now; 2059b50d902SRodney W. Grimes FILE *fp; 2069b50d902SRodney W. Grimes int fd; 207075f2939SMark Murray char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 208075f2939SMark Murray const char *tty; 20956e7ae90SKris Kennaway const char *whom; 210a47f98edSKris Kennaway gid_t egid; 2119b50d902SRodney W. Grimes 21256e7ae90SKris Kennaway (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 21356e7ae90SKris Kennaway if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 21456e7ae90SKris Kennaway err(1, "can't open temporary file"); 2159b50d902SRodney W. Grimes (void)unlink(tmpname); 2169b50d902SRodney W. Grimes 2179b50d902SRodney W. Grimes if (!nobanner) { 21856e7ae90SKris Kennaway tty = ttyname(STDERR_FILENO); 21956e7ae90SKris Kennaway if (tty == NULL) 22080af0816SNick Hibma tty = "no tty"; 22180af0816SNick Hibma 2229b50d902SRodney W. Grimes if (!(whom = getlogin())) 2239b50d902SRodney W. Grimes whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 2249b50d902SRodney W. Grimes (void)gethostname(hostname, sizeof(hostname)); 2259b50d902SRodney W. Grimes (void)time(&now); 2269b50d902SRodney W. Grimes lt = localtime(&now); 2279b50d902SRodney W. Grimes 2289b50d902SRodney W. Grimes /* 2299b50d902SRodney W. Grimes * all this stuff is to blank out a square for the message; 2309b50d902SRodney W. Grimes * we wrap message lines at column 79, not 80, because some 2319b50d902SRodney W. Grimes * terminals wrap after 79, some do not, and we can't tell. 2329b50d902SRodney W. Grimes * Which means that we may leave a non-blank character 2339b50d902SRodney W. Grimes * in column 80, but that can't be helped. 2349b50d902SRodney W. Grimes */ 2359b50d902SRodney W. Grimes (void)fprintf(fp, "\r%79s\r\n", " "); 23698df703fSMatthew Dillon (void)snprintf(lbuf, sizeof(lbuf), 23798df703fSMatthew Dillon "Broadcast Message from %s@%s", 2389b50d902SRodney W. Grimes whom, hostname); 2399b50d902SRodney W. Grimes (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 24098df703fSMatthew Dillon (void)snprintf(lbuf, sizeof(lbuf), 24180af0816SNick Hibma " (%s) at %d:%02d %s...", tty, 2426e5d42b9SJeroen Ruigrok van der Werven lt->tm_hour, lt->tm_min, lt->tm_zone); 2439b50d902SRodney W. Grimes (void)fprintf(fp, "%-79.79s\r\n", lbuf); 2449b50d902SRodney W. Grimes } 2459b50d902SRodney W. Grimes (void)fprintf(fp, "%79s\r\n", " "); 2469b50d902SRodney W. Grimes 247a47f98edSKris Kennaway if (fname) { 248a47f98edSKris Kennaway egid = getegid(); 249a47f98edSKris Kennaway setegid(getgid()); 250a47f98edSKris Kennaway if (freopen(fname, "r", stdin) == NULL) 25156e7ae90SKris Kennaway err(1, "can't read %s", fname); 252a47f98edSKris Kennaway setegid(egid); 253a47f98edSKris Kennaway } 2549b50d902SRodney W. Grimes while (fgets(lbuf, sizeof(lbuf), stdin)) 2559b50d902SRodney W. Grimes for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 256c87eca8bSDaniel Baker if (ch == '\r') { 257c87eca8bSDaniel Baker cnt = 0; 258c87eca8bSDaniel Baker } else if (cnt == 79 || ch == '\n') { 2599b50d902SRodney W. Grimes for (; cnt < 79; ++cnt) 2609b50d902SRodney W. Grimes putc(' ', fp); 2619b50d902SRodney W. Grimes putc('\r', fp); 2629b50d902SRodney W. Grimes putc('\n', fp); 2639b50d902SRodney W. Grimes cnt = 0; 26458011702SAndrey A. Chernov } else if (((ch & 0x80) && ch < 0xA0) || 26558011702SAndrey A. Chernov /* disable upper controls */ 26658011702SAndrey A. Chernov (!isprint(ch) && !isspace(ch) && 26758011702SAndrey A. Chernov ch != '\a' && ch != '\b') 268fc092135SAndrey A. Chernov ) { 269fc092135SAndrey A. Chernov if (ch & 0x80) { 270fc092135SAndrey A. Chernov ch &= 0x7F; 27158011702SAndrey A. Chernov putc('M', fp); 27258011702SAndrey A. Chernov if (++cnt == 79) { 27358011702SAndrey A. Chernov putc('\r', fp); 27458011702SAndrey A. Chernov putc('\n', fp); 27558011702SAndrey A. Chernov cnt = 0; 276fc092135SAndrey A. Chernov } 27758011702SAndrey A. Chernov putc('-', fp); 27858011702SAndrey A. Chernov if (++cnt == 79) { 27958011702SAndrey A. Chernov putc('\r', fp); 28058011702SAndrey A. Chernov putc('\n', fp); 28158011702SAndrey A. Chernov cnt = 0; 28258011702SAndrey A. Chernov } 28358011702SAndrey A. Chernov } 28458011702SAndrey A. Chernov if (iscntrl(ch)) { 28558011702SAndrey A. Chernov ch ^= 040; 286acc879c1SGuido van Rooij putc('^', fp); 28758011702SAndrey A. Chernov if (++cnt == 79) { 28858011702SAndrey A. Chernov putc('\r', fp); 28958011702SAndrey A. Chernov putc('\n', fp); 29058011702SAndrey A. Chernov cnt = 0; 29158011702SAndrey A. Chernov } 29258011702SAndrey A. Chernov } 293c87eca8bSDaniel Baker putc(ch, fp); 294b4d6ab01SDaniel Baker } else { 2959b50d902SRodney W. Grimes putc(ch, fp); 2969b50d902SRodney W. Grimes } 297b4d6ab01SDaniel Baker } 2989b50d902SRodney W. Grimes (void)fprintf(fp, "%79s\r\n", " "); 2999b50d902SRodney W. Grimes rewind(fp); 3009b50d902SRodney W. Grimes 301752d887aSPhilippe Charnier if (fstat(fd, &sbuf)) 30256e7ae90SKris Kennaway err(1, "can't stat temporary file"); 3039b50d902SRodney W. Grimes mbufsize = sbuf.st_size; 304752d887aSPhilippe Charnier if (!(mbuf = malloc((u_int)mbufsize))) 30556e7ae90SKris Kennaway err(1, "out of memory"); 306075f2939SMark Murray if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 30756e7ae90SKris Kennaway err(1, "can't read temporary file"); 3089b50d902SRodney W. Grimes (void)close(fd); 3099b50d902SRodney W. Grimes } 310