18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1988, 1990, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 32075f2939SMark Murray #include <sys/cdefs.h> 33075f2939SMark Murray 34075f2939SMark Murray __FBSDID("$FreeBSD$"); 35075f2939SMark Murray 369b50d902SRodney W. Grimes #ifndef lint 37752d887aSPhilippe Charnier static const char copyright[] = 389b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1990, 1993\n\ 399b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 40075f2939SMark Murray #endif 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #ifndef lint 43075f2939SMark Murray static const char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 44752d887aSPhilippe Charnier #endif 459b50d902SRodney W. Grimes 469b50d902SRodney W. Grimes /* 479b50d902SRodney W. Grimes * This program is not related to David Wall, whose Stanford Ph.D. thesis 489b50d902SRodney W. Grimes * is entitled "Mechanisms for Broadcast and Selective Broadcast". 499b50d902SRodney W. Grimes */ 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes #include <sys/param.h> 529b50d902SRodney W. Grimes #include <sys/stat.h> 539b50d902SRodney W. Grimes #include <sys/uio.h> 549b50d902SRodney W. Grimes 55752d887aSPhilippe Charnier #include <ctype.h> 56752d887aSPhilippe Charnier #include <err.h> 576a20d55aSWarner Losh #include <grp.h> 589566348dSAndrey A. Chernov #include <locale.h> 599b50d902SRodney W. Grimes #include <paths.h> 609b50d902SRodney W. Grimes #include <pwd.h> 619b50d902SRodney W. Grimes #include <stdio.h> 629b50d902SRodney W. Grimes #include <stdlib.h> 639b50d902SRodney W. Grimes #include <string.h> 6458011702SAndrey A. Chernov #include <time.h> 659b50d902SRodney W. Grimes #include <unistd.h> 66ab90a4d1SEd Schouten #include <utmpx.h> 67586fbee6SGleb Smirnoff #include <wchar.h> 68586fbee6SGleb Smirnoff #include <wctype.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 75d7698e07SEd Schouten static struct wallgroup { 766a20d55aSWarner Losh struct wallgroup *next; 776a20d55aSWarner Losh char *name; 786a20d55aSWarner Losh gid_t gid; 796a20d55aSWarner Losh } *grouplist; 80d7698e07SEd Schouten static int nobanner; 81d7698e07SEd Schouten static int mbufsize; 82d7698e07SEd Schouten static 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; 192586fbee6SGleb Smirnoff wchar_t 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; 199586fbee6SGleb Smirnoff char hostname[MAXHOSTNAMELEN], tmpname[64]; 200586fbee6SGleb Smirnoff wchar_t *p, *tmp, lbuf[256], codebuf[13]; 201075f2939SMark Murray const char *tty; 20256e7ae90SKris Kennaway const char *whom; 203a47f98edSKris Kennaway gid_t egid; 2049b50d902SRodney W. Grimes 20556e7ae90SKris Kennaway (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 20656e7ae90SKris Kennaway if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 20756e7ae90SKris Kennaway err(1, "can't open temporary file"); 2089b50d902SRodney W. Grimes (void)unlink(tmpname); 2099b50d902SRodney W. Grimes 2109b50d902SRodney W. Grimes if (!nobanner) { 21156e7ae90SKris Kennaway tty = ttyname(STDERR_FILENO); 21256e7ae90SKris Kennaway if (tty == NULL) 21380af0816SNick Hibma tty = "no tty"; 21480af0816SNick Hibma 2159b50d902SRodney W. Grimes if (!(whom = getlogin())) 2169b50d902SRodney W. Grimes whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 2179b50d902SRodney W. Grimes (void)gethostname(hostname, sizeof(hostname)); 2189b50d902SRodney W. Grimes (void)time(&now); 2199b50d902SRodney W. Grimes lt = localtime(&now); 2209b50d902SRodney W. Grimes 2219b50d902SRodney W. Grimes /* 2229b50d902SRodney W. Grimes * all this stuff is to blank out a square for the message; 2239b50d902SRodney W. Grimes * we wrap message lines at column 79, not 80, because some 2249b50d902SRodney W. Grimes * terminals wrap after 79, some do not, and we can't tell. 2259b50d902SRodney W. Grimes * Which means that we may leave a non-blank character 2269b50d902SRodney W. Grimes * in column 80, but that can't be helped. 2279b50d902SRodney W. Grimes */ 228586fbee6SGleb Smirnoff (void)fwprintf(fp, L"\r%79s\r\n", " "); 229586fbee6SGleb Smirnoff (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t), 230586fbee6SGleb Smirnoff L"Broadcast Message from %s@%s", 2319b50d902SRodney W. Grimes whom, hostname); 232586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%-79.79S\007\007\r\n", lbuf); 233586fbee6SGleb Smirnoff (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t), 234586fbee6SGleb Smirnoff L" (%s) at %d:%02d %s...", tty, 2356e5d42b9SJeroen Ruigrok van der Werven lt->tm_hour, lt->tm_min, lt->tm_zone); 236586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%-79.79S\r\n", lbuf); 2379b50d902SRodney W. Grimes } 238586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%79s\r\n", " "); 2399b50d902SRodney W. Grimes 240a47f98edSKris Kennaway if (fname) { 241a47f98edSKris Kennaway egid = getegid(); 242a47f98edSKris Kennaway setegid(getgid()); 243a47f98edSKris Kennaway if (freopen(fname, "r", stdin) == NULL) 24456e7ae90SKris Kennaway err(1, "can't read %s", fname); 24550e04779SEitan Adler if (setegid(egid) != 0) 24650e04779SEitan Adler err(1, "setegid failed"); 247a47f98edSKris Kennaway } 248b0023655SDavid E. O'Brien cnt = 0; 249586fbee6SGleb Smirnoff while (fgetws(lbuf, sizeof(lbuf)/sizeof(wchar_t), stdin)) { 250586fbee6SGleb Smirnoff for (p = lbuf; (ch = *p) != L'\0'; ++p, ++cnt) { 251586fbee6SGleb Smirnoff if (ch == L'\r') { 252586fbee6SGleb Smirnoff putwc(L'\r', fp); 253c87eca8bSDaniel Baker cnt = 0; 2543c58f6ddSDavid Schultz continue; 255586fbee6SGleb Smirnoff } else if (ch == L'\n') { 2569b50d902SRodney W. Grimes for (; cnt < 79; ++cnt) 257586fbee6SGleb Smirnoff putwc(L' ', fp); 258586fbee6SGleb Smirnoff putwc(L'\r', fp); 259586fbee6SGleb Smirnoff putwc(L'\n', fp); 2603c58f6ddSDavid Schultz break; 2613c58f6ddSDavid Schultz } 2623c58f6ddSDavid Schultz if (cnt == 79) { 263586fbee6SGleb Smirnoff putwc(L'\r', fp); 264586fbee6SGleb Smirnoff putwc(L'\n', fp); 2659b50d902SRodney W. Grimes cnt = 0; 2663c58f6ddSDavid Schultz } 267586fbee6SGleb Smirnoff if (iswprint(ch) || iswspace(ch) || ch == L'\a' || ch == L'\b') { 268586fbee6SGleb Smirnoff putwc(ch, fp); 269586fbee6SGleb Smirnoff } else { 270586fbee6SGleb Smirnoff (void)swprintf(codebuf, sizeof(codebuf)/sizeof(wchar_t), L"<0x%X>", ch); 271586fbee6SGleb Smirnoff for (tmp = codebuf; *tmp != L'\0'; ++tmp) { 272586fbee6SGleb Smirnoff putwc(*tmp, fp); 27358011702SAndrey A. Chernov if (++cnt == 79) { 274586fbee6SGleb Smirnoff putwc(L'\r', fp); 275586fbee6SGleb Smirnoff putwc(L'\n', fp); 27658011702SAndrey A. Chernov cnt = 0; 27758011702SAndrey A. Chernov } 27858011702SAndrey A. Chernov } 279586fbee6SGleb Smirnoff --cnt; 28058011702SAndrey A. Chernov } 28158011702SAndrey A. Chernov } 2823c58f6ddSDavid Schultz } 283586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%79s\r\n", " "); 2849b50d902SRodney W. Grimes rewind(fp); 2859b50d902SRodney W. Grimes 286752d887aSPhilippe Charnier if (fstat(fd, &sbuf)) 28756e7ae90SKris Kennaway err(1, "can't stat temporary file"); 2889b50d902SRodney W. Grimes mbufsize = sbuf.st_size; 289752d887aSPhilippe Charnier if (!(mbuf = malloc((u_int)mbufsize))) 29056e7ae90SKris Kennaway err(1, "out of memory"); 291075f2939SMark Murray if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 29256e7ae90SKris Kennaway err(1, "can't read temporary file"); 293*e8d2bea9SWarner Losh fclose(fp); 2949b50d902SRodney W. Grimes } 295