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> 68ab90a4d1SEd Schouten #include <utmpx.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 85bd76376fSEd Schouten ttystat(char *line) 86b457a3e1SOlivier Houchard { 87b457a3e1SOlivier Houchard struct stat sb; 88b457a3e1SOlivier Houchard char ttybuf[MAXPATHLEN]; 89b457a3e1SOlivier Houchard 90bd76376fSEd Schouten (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, 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; 101bd76376fSEd Schouten struct utmpx *utmp; 1026a20d55aSWarner Losh int ch; 1032cfec5adSRuslan Ermilov int ingroup; 1046a20d55aSWarner Losh struct wallgroup *g; 1056a20d55aSWarner Losh struct group *grp; 106728d043eSDima Dorfman char **np; 107728d043eSDima Dorfman const char *p; 1086a20d55aSWarner Losh struct passwd *pw; 1099b50d902SRodney W. Grimes 1109566348dSAndrey A. Chernov (void)setlocale(LC_CTYPE, ""); 1119566348dSAndrey A. Chernov 1126a20d55aSWarner Losh while ((ch = getopt(argc, argv, "g:n")) != -1) 1139b50d902SRodney W. Grimes switch (ch) { 1149b50d902SRodney W. Grimes case 'n': 1159b50d902SRodney W. Grimes /* undoc option for shutdown: suppress banner */ 1169b50d902SRodney W. Grimes if (geteuid() == 0) 1179b50d902SRodney W. Grimes nobanner = 1; 1189b50d902SRodney W. Grimes break; 1196a20d55aSWarner Losh case 'g': 1206a20d55aSWarner Losh g = (struct wallgroup *)malloc(sizeof *g); 1216a20d55aSWarner Losh g->next = grouplist; 1226a20d55aSWarner Losh g->name = optarg; 1236a20d55aSWarner Losh g->gid = -1; 1246a20d55aSWarner Losh grouplist = g; 1256a20d55aSWarner Losh break; 1269b50d902SRodney W. Grimes case '?': 1279b50d902SRodney W. Grimes default: 128752d887aSPhilippe Charnier usage(); 1299b50d902SRodney W. Grimes } 1309b50d902SRodney W. Grimes argc -= optind; 1319b50d902SRodney W. Grimes argv += optind; 1329b50d902SRodney W. Grimes if (argc > 1) 133752d887aSPhilippe Charnier usage(); 1349b50d902SRodney W. Grimes 1356a20d55aSWarner Losh for (g = grouplist; g; g = g->next) { 1366a20d55aSWarner Losh grp = getgrnam(g->name); 137003ff943SWarner Losh if (grp != NULL) 1386a20d55aSWarner Losh g->gid = grp->gr_gid; 139003ff943SWarner Losh else 140003ff943SWarner Losh warnx("%s: no such group", g->name); 1416a20d55aSWarner Losh } 1426a20d55aSWarner Losh 1439b50d902SRodney W. Grimes makemsg(*argv); 1449b50d902SRodney W. Grimes 1459b50d902SRodney W. Grimes iov.iov_base = mbuf; 1469b50d902SRodney W. Grimes iov.iov_len = mbufsize; 1479b50d902SRodney W. Grimes /* NOSTRICT */ 148bd76376fSEd Schouten while ((utmp = getutxent()) != NULL) { 149bd76376fSEd Schouten if (utmp->ut_type != USER_PROCESS) 1509b50d902SRodney W. Grimes continue; 151bd76376fSEd Schouten if (ttystat(utmp->ut_line) != 0) 152b457a3e1SOlivier Houchard continue; 1536a20d55aSWarner Losh if (grouplist) { 1542cfec5adSRuslan Ermilov ingroup = 0; 155bd76376fSEd Schouten pw = getpwnam(utmp->ut_user); 1566a20d55aSWarner Losh if (!pw) 1576a20d55aSWarner Losh continue; 1586a20d55aSWarner Losh for (g = grouplist; g && ingroup == 0; g = g->next) { 159075f2939SMark Murray if (g->gid == (gid_t)-1) 1606a20d55aSWarner Losh continue; 1616a20d55aSWarner Losh if (g->gid == pw->pw_gid) 1626a20d55aSWarner Losh ingroup = 1; 1632cfec5adSRuslan Ermilov else if ((grp = getgrgid(g->gid)) != NULL) { 1642cfec5adSRuslan Ermilov for (np = grp->gr_mem; *np; np++) { 165bd76376fSEd Schouten if (strcmp(*np, utmp->ut_user) == 0) { 1666a20d55aSWarner Losh ingroup = 1; 1672cfec5adSRuslan Ermilov break; 1682cfec5adSRuslan Ermilov } 1692cfec5adSRuslan Ermilov } 1702cfec5adSRuslan Ermilov } 1716a20d55aSWarner Losh } 1726a20d55aSWarner Losh if (ingroup == 0) 1736a20d55aSWarner Losh continue; 1746a20d55aSWarner Losh } 175bd76376fSEd Schouten if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL) 176752d887aSPhilippe Charnier warnx("%s", p); 1779b50d902SRodney W. Grimes } 1789b50d902SRodney W. Grimes exit(0); 1799b50d902SRodney W. Grimes } 1809b50d902SRodney W. Grimes 181752d887aSPhilippe Charnier static void 182645e7f1bSEd Schouten usage(void) 183752d887aSPhilippe Charnier { 184003ff943SWarner Losh (void)fprintf(stderr, "usage: wall [-g group] [file]\n"); 185752d887aSPhilippe Charnier exit(1); 186752d887aSPhilippe Charnier } 187752d887aSPhilippe Charnier 1889b50d902SRodney W. Grimes void 1896a20d55aSWarner Losh makemsg(char *fname) 1909b50d902SRodney W. Grimes { 1916a20d55aSWarner Losh int cnt; 1926a20d55aSWarner Losh unsigned char ch; 1939b50d902SRodney W. Grimes struct tm *lt; 1949b50d902SRodney W. Grimes struct passwd *pw; 1959b50d902SRodney W. Grimes struct stat sbuf; 19658011702SAndrey A. Chernov time_t now; 1979b50d902SRodney W. Grimes FILE *fp; 1989b50d902SRodney W. Grimes int fd; 199075f2939SMark Murray char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 200075f2939SMark Murray const char *tty; 20156e7ae90SKris Kennaway const char *whom; 202a47f98edSKris Kennaway gid_t egid; 2039b50d902SRodney W. Grimes 20456e7ae90SKris Kennaway (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 20556e7ae90SKris Kennaway if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 20656e7ae90SKris Kennaway err(1, "can't open temporary file"); 2079b50d902SRodney W. Grimes (void)unlink(tmpname); 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes if (!nobanner) { 21056e7ae90SKris Kennaway tty = ttyname(STDERR_FILENO); 21156e7ae90SKris Kennaway if (tty == NULL) 21280af0816SNick Hibma tty = "no tty"; 21380af0816SNick Hibma 2149b50d902SRodney W. Grimes if (!(whom = getlogin())) 2159b50d902SRodney W. Grimes whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 2169b50d902SRodney W. Grimes (void)gethostname(hostname, sizeof(hostname)); 2179b50d902SRodney W. Grimes (void)time(&now); 2189b50d902SRodney W. Grimes lt = localtime(&now); 2199b50d902SRodney W. Grimes 2209b50d902SRodney W. Grimes /* 2219b50d902SRodney W. Grimes * all this stuff is to blank out a square for the message; 2229b50d902SRodney W. Grimes * we wrap message lines at column 79, not 80, because some 2239b50d902SRodney W. Grimes * terminals wrap after 79, some do not, and we can't tell. 2249b50d902SRodney W. Grimes * Which means that we may leave a non-blank character 2259b50d902SRodney W. Grimes * in column 80, but that can't be helped. 2269b50d902SRodney W. Grimes */ 2279b50d902SRodney W. Grimes (void)fprintf(fp, "\r%79s\r\n", " "); 22898df703fSMatthew Dillon (void)snprintf(lbuf, sizeof(lbuf), 22998df703fSMatthew Dillon "Broadcast Message from %s@%s", 2309b50d902SRodney W. Grimes whom, hostname); 2319b50d902SRodney W. Grimes (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 23298df703fSMatthew Dillon (void)snprintf(lbuf, sizeof(lbuf), 23380af0816SNick Hibma " (%s) at %d:%02d %s...", tty, 2346e5d42b9SJeroen Ruigrok van der Werven lt->tm_hour, lt->tm_min, lt->tm_zone); 2359b50d902SRodney W. Grimes (void)fprintf(fp, "%-79.79s\r\n", lbuf); 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes (void)fprintf(fp, "%79s\r\n", " "); 2389b50d902SRodney W. Grimes 239a47f98edSKris Kennaway if (fname) { 240a47f98edSKris Kennaway egid = getegid(); 241a47f98edSKris Kennaway setegid(getgid()); 242a47f98edSKris Kennaway if (freopen(fname, "r", stdin) == NULL) 24356e7ae90SKris Kennaway err(1, "can't read %s", fname); 244a47f98edSKris Kennaway setegid(egid); 245a47f98edSKris Kennaway } 2463c58f6ddSDavid Schultz while (fgets(lbuf, sizeof(lbuf), stdin)) { 2479b50d902SRodney W. Grimes for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 248c87eca8bSDaniel Baker if (ch == '\r') { 2493c58f6ddSDavid Schultz putc('\r', fp); 250c87eca8bSDaniel Baker cnt = 0; 2513c58f6ddSDavid Schultz continue; 2523c58f6ddSDavid Schultz } else if (ch == '\n') { 2539b50d902SRodney W. Grimes for (; cnt < 79; ++cnt) 2549b50d902SRodney W. Grimes putc(' ', fp); 2559b50d902SRodney W. Grimes putc('\r', fp); 2569b50d902SRodney W. Grimes putc('\n', fp); 2573c58f6ddSDavid Schultz break; 2583c58f6ddSDavid Schultz } 2593c58f6ddSDavid Schultz if (cnt == 79) { 2603c58f6ddSDavid Schultz putc('\r', fp); 2613c58f6ddSDavid Schultz putc('\n', fp); 2629b50d902SRodney W. Grimes cnt = 0; 2633c58f6ddSDavid Schultz } 2643c58f6ddSDavid Schultz 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 } 2933c58f6ddSDavid Schultz } 2949b50d902SRodney W. Grimes putc(ch, fp); 2959b50d902SRodney W. Grimes } 296b4d6ab01SDaniel Baker } 2979b50d902SRodney W. Grimes (void)fprintf(fp, "%79s\r\n", " "); 2989b50d902SRodney W. Grimes rewind(fp); 2999b50d902SRodney W. Grimes 300752d887aSPhilippe Charnier if (fstat(fd, &sbuf)) 30156e7ae90SKris Kennaway err(1, "can't stat temporary file"); 3029b50d902SRodney W. Grimes mbufsize = sbuf.st_size; 303752d887aSPhilippe Charnier if (!(mbuf = malloc((u_int)mbufsize))) 30456e7ae90SKris Kennaway err(1, "out of memory"); 305075f2939SMark Murray if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 30656e7ae90SKris Kennaway err(1, "can't read temporary file"); 3079b50d902SRodney W. Grimes (void)close(fd); 3089b50d902SRodney W. Grimes } 309