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
329b50d902SRodney W. Grimes /*
339b50d902SRodney W. Grimes * This program is not related to David Wall, whose Stanford Ph.D. thesis
349b50d902SRodney W. Grimes * is entitled "Mechanisms for Broadcast and Selective Broadcast".
359b50d902SRodney W. Grimes */
369b50d902SRodney W. Grimes
379b50d902SRodney W. Grimes #include <sys/param.h>
389b50d902SRodney W. Grimes #include <sys/stat.h>
399b50d902SRodney W. Grimes #include <sys/uio.h>
409b50d902SRodney W. Grimes
41752d887aSPhilippe Charnier #include <ctype.h>
42752d887aSPhilippe Charnier #include <err.h>
436a20d55aSWarner Losh #include <grp.h>
449566348dSAndrey A. Chernov #include <locale.h>
459b50d902SRodney W. Grimes #include <paths.h>
469b50d902SRodney W. Grimes #include <pwd.h>
479b50d902SRodney W. Grimes #include <stdio.h>
489b50d902SRodney W. Grimes #include <stdlib.h>
499b50d902SRodney W. Grimes #include <string.h>
5058011702SAndrey A. Chernov #include <time.h>
519b50d902SRodney W. Grimes #include <unistd.h>
52ab90a4d1SEd Schouten #include <utmpx.h>
53586fbee6SGleb Smirnoff #include <wchar.h>
54586fbee6SGleb Smirnoff #include <wctype.h>
559b50d902SRodney W. Grimes
56728d043eSDima Dorfman #include "ttymsg.h"
57728d043eSDima Dorfman
586a20d55aSWarner Losh static void makemsg(char *);
59*cccdaf50SAlfonso Gregory static void usage(void) __dead2;
609b50d902SRodney W. Grimes
61d7698e07SEd Schouten static struct wallgroup {
626a20d55aSWarner Losh struct wallgroup *next;
636a20d55aSWarner Losh char *name;
646a20d55aSWarner Losh gid_t gid;
656a20d55aSWarner Losh } *grouplist;
66d7698e07SEd Schouten static int nobanner;
67d7698e07SEd Schouten static int mbufsize;
68d7698e07SEd Schouten static char *mbuf;
699b50d902SRodney W. Grimes
70b457a3e1SOlivier Houchard static int
ttystat(char * line)71bd76376fSEd Schouten ttystat(char *line)
72b457a3e1SOlivier Houchard {
73b457a3e1SOlivier Houchard struct stat sb;
74b457a3e1SOlivier Houchard char ttybuf[MAXPATHLEN];
75b457a3e1SOlivier Houchard
76bd76376fSEd Schouten (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
77b457a3e1SOlivier Houchard if (stat(ttybuf, &sb) == 0) {
78b457a3e1SOlivier Houchard return (0);
79b457a3e1SOlivier Houchard } else
80b457a3e1SOlivier Houchard return (-1);
81b457a3e1SOlivier Houchard }
82b457a3e1SOlivier Houchard
839b50d902SRodney W. Grimes int
main(int argc,char * argv[])846a20d55aSWarner Losh main(int argc, char *argv[])
859b50d902SRodney W. Grimes {
869b50d902SRodney W. Grimes struct iovec iov;
87bd76376fSEd Schouten struct utmpx *utmp;
886a20d55aSWarner Losh int ch;
892cfec5adSRuslan Ermilov int ingroup;
906a20d55aSWarner Losh struct wallgroup *g;
916a20d55aSWarner Losh struct group *grp;
92728d043eSDima Dorfman char **np;
93728d043eSDima Dorfman const char *p;
946a20d55aSWarner Losh struct passwd *pw;
959b50d902SRodney W. Grimes
969566348dSAndrey A. Chernov (void)setlocale(LC_CTYPE, "");
979566348dSAndrey A. Chernov
986a20d55aSWarner Losh while ((ch = getopt(argc, argv, "g:n")) != -1)
999b50d902SRodney W. Grimes switch (ch) {
1009b50d902SRodney W. Grimes case 'n':
1019b50d902SRodney W. Grimes /* undoc option for shutdown: suppress banner */
1029b50d902SRodney W. Grimes if (geteuid() == 0)
1039b50d902SRodney W. Grimes nobanner = 1;
1049b50d902SRodney W. Grimes break;
1056a20d55aSWarner Losh case 'g':
1066a20d55aSWarner Losh g = (struct wallgroup *)malloc(sizeof *g);
1076a20d55aSWarner Losh g->next = grouplist;
1086a20d55aSWarner Losh g->name = optarg;
1096a20d55aSWarner Losh g->gid = -1;
1106a20d55aSWarner Losh grouplist = g;
1116a20d55aSWarner Losh break;
1129b50d902SRodney W. Grimes case '?':
1139b50d902SRodney W. Grimes default:
114752d887aSPhilippe Charnier usage();
1159b50d902SRodney W. Grimes }
1169b50d902SRodney W. Grimes argc -= optind;
1179b50d902SRodney W. Grimes argv += optind;
1189b50d902SRodney W. Grimes if (argc > 1)
119752d887aSPhilippe Charnier usage();
1209b50d902SRodney W. Grimes
1216a20d55aSWarner Losh for (g = grouplist; g; g = g->next) {
1226a20d55aSWarner Losh grp = getgrnam(g->name);
123003ff943SWarner Losh if (grp != NULL)
1246a20d55aSWarner Losh g->gid = grp->gr_gid;
125003ff943SWarner Losh else
126003ff943SWarner Losh warnx("%s: no such group", g->name);
1276a20d55aSWarner Losh }
1286a20d55aSWarner Losh
1299b50d902SRodney W. Grimes makemsg(*argv);
1309b50d902SRodney W. Grimes
1319b50d902SRodney W. Grimes iov.iov_base = mbuf;
1329b50d902SRodney W. Grimes iov.iov_len = mbufsize;
1339b50d902SRodney W. Grimes /* NOSTRICT */
134bd76376fSEd Schouten while ((utmp = getutxent()) != NULL) {
135bd76376fSEd Schouten if (utmp->ut_type != USER_PROCESS)
1369b50d902SRodney W. Grimes continue;
137bd76376fSEd Schouten if (ttystat(utmp->ut_line) != 0)
138b457a3e1SOlivier Houchard continue;
1396a20d55aSWarner Losh if (grouplist) {
1402cfec5adSRuslan Ermilov ingroup = 0;
141bd76376fSEd Schouten pw = getpwnam(utmp->ut_user);
1426a20d55aSWarner Losh if (!pw)
1436a20d55aSWarner Losh continue;
1446a20d55aSWarner Losh for (g = grouplist; g && ingroup == 0; g = g->next) {
145075f2939SMark Murray if (g->gid == (gid_t)-1)
1466a20d55aSWarner Losh continue;
1476a20d55aSWarner Losh if (g->gid == pw->pw_gid)
1486a20d55aSWarner Losh ingroup = 1;
1492cfec5adSRuslan Ermilov else if ((grp = getgrgid(g->gid)) != NULL) {
1502cfec5adSRuslan Ermilov for (np = grp->gr_mem; *np; np++) {
151bd76376fSEd Schouten if (strcmp(*np, utmp->ut_user) == 0) {
1526a20d55aSWarner Losh ingroup = 1;
1532cfec5adSRuslan Ermilov break;
1542cfec5adSRuslan Ermilov }
1552cfec5adSRuslan Ermilov }
1562cfec5adSRuslan Ermilov }
1576a20d55aSWarner Losh }
1586a20d55aSWarner Losh if (ingroup == 0)
1596a20d55aSWarner Losh continue;
1606a20d55aSWarner Losh }
161bd76376fSEd Schouten if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
162752d887aSPhilippe Charnier warnx("%s", p);
1639b50d902SRodney W. Grimes }
1649b50d902SRodney W. Grimes exit(0);
1659b50d902SRodney W. Grimes }
1669b50d902SRodney W. Grimes
167752d887aSPhilippe Charnier static void
usage(void)168645e7f1bSEd Schouten usage(void)
169752d887aSPhilippe Charnier {
170003ff943SWarner Losh (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
171752d887aSPhilippe Charnier exit(1);
172752d887aSPhilippe Charnier }
173752d887aSPhilippe Charnier
1749b50d902SRodney W. Grimes void
makemsg(char * fname)1756a20d55aSWarner Losh makemsg(char *fname)
1769b50d902SRodney W. Grimes {
1776a20d55aSWarner Losh int cnt;
178586fbee6SGleb Smirnoff wchar_t ch;
1799b50d902SRodney W. Grimes struct tm *lt;
1809b50d902SRodney W. Grimes struct passwd *pw;
1819b50d902SRodney W. Grimes struct stat sbuf;
18258011702SAndrey A. Chernov time_t now;
1839b50d902SRodney W. Grimes FILE *fp;
1849b50d902SRodney W. Grimes int fd;
185586fbee6SGleb Smirnoff char hostname[MAXHOSTNAMELEN], tmpname[64];
186586fbee6SGleb Smirnoff wchar_t *p, *tmp, lbuf[256], codebuf[13];
187075f2939SMark Murray const char *tty;
18856e7ae90SKris Kennaway const char *whom;
189a47f98edSKris Kennaway gid_t egid;
1909b50d902SRodney W. Grimes
19156e7ae90SKris Kennaway (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
19256e7ae90SKris Kennaway if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
19356e7ae90SKris Kennaway err(1, "can't open temporary file");
1949b50d902SRodney W. Grimes (void)unlink(tmpname);
1959b50d902SRodney W. Grimes
1969b50d902SRodney W. Grimes if (!nobanner) {
19756e7ae90SKris Kennaway tty = ttyname(STDERR_FILENO);
19856e7ae90SKris Kennaway if (tty == NULL)
19980af0816SNick Hibma tty = "no tty";
20080af0816SNick Hibma
2019b50d902SRodney W. Grimes if (!(whom = getlogin()))
2029b50d902SRodney W. Grimes whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
2039b50d902SRodney W. Grimes (void)gethostname(hostname, sizeof(hostname));
2049b50d902SRodney W. Grimes (void)time(&now);
2059b50d902SRodney W. Grimes lt = localtime(&now);
2069b50d902SRodney W. Grimes
2079b50d902SRodney W. Grimes /*
2089b50d902SRodney W. Grimes * all this stuff is to blank out a square for the message;
2099b50d902SRodney W. Grimes * we wrap message lines at column 79, not 80, because some
2109b50d902SRodney W. Grimes * terminals wrap after 79, some do not, and we can't tell.
2119b50d902SRodney W. Grimes * Which means that we may leave a non-blank character
2129b50d902SRodney W. Grimes * in column 80, but that can't be helped.
2139b50d902SRodney W. Grimes */
214586fbee6SGleb Smirnoff (void)fwprintf(fp, L"\r%79s\r\n", " ");
215586fbee6SGleb Smirnoff (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t),
216586fbee6SGleb Smirnoff L"Broadcast Message from %s@%s",
2179b50d902SRodney W. Grimes whom, hostname);
218586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%-79.79S\007\007\r\n", lbuf);
219586fbee6SGleb Smirnoff (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t),
220586fbee6SGleb Smirnoff L" (%s) at %d:%02d %s...", tty,
2216e5d42b9SJeroen Ruigrok van der Werven lt->tm_hour, lt->tm_min, lt->tm_zone);
222586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%-79.79S\r\n", lbuf);
2239b50d902SRodney W. Grimes }
224586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%79s\r\n", " ");
2259b50d902SRodney W. Grimes
226a47f98edSKris Kennaway if (fname) {
227a47f98edSKris Kennaway egid = getegid();
228a47f98edSKris Kennaway setegid(getgid());
229a47f98edSKris Kennaway if (freopen(fname, "r", stdin) == NULL)
23056e7ae90SKris Kennaway err(1, "can't read %s", fname);
23150e04779SEitan Adler if (setegid(egid) != 0)
23250e04779SEitan Adler err(1, "setegid failed");
233a47f98edSKris Kennaway }
234b0023655SDavid E. O'Brien cnt = 0;
235586fbee6SGleb Smirnoff while (fgetws(lbuf, sizeof(lbuf)/sizeof(wchar_t), stdin)) {
236586fbee6SGleb Smirnoff for (p = lbuf; (ch = *p) != L'\0'; ++p, ++cnt) {
237586fbee6SGleb Smirnoff if (ch == L'\r') {
238586fbee6SGleb Smirnoff putwc(L'\r', fp);
239c87eca8bSDaniel Baker cnt = 0;
2403c58f6ddSDavid Schultz continue;
241586fbee6SGleb Smirnoff } else if (ch == L'\n') {
2429b50d902SRodney W. Grimes for (; cnt < 79; ++cnt)
243586fbee6SGleb Smirnoff putwc(L' ', fp);
244586fbee6SGleb Smirnoff putwc(L'\r', fp);
245586fbee6SGleb Smirnoff putwc(L'\n', fp);
2463c58f6ddSDavid Schultz break;
2473c58f6ddSDavid Schultz }
2483c58f6ddSDavid Schultz if (cnt == 79) {
249586fbee6SGleb Smirnoff putwc(L'\r', fp);
250586fbee6SGleb Smirnoff putwc(L'\n', fp);
2519b50d902SRodney W. Grimes cnt = 0;
2523c58f6ddSDavid Schultz }
253586fbee6SGleb Smirnoff if (iswprint(ch) || iswspace(ch) || ch == L'\a' || ch == L'\b') {
254586fbee6SGleb Smirnoff putwc(ch, fp);
255586fbee6SGleb Smirnoff } else {
256586fbee6SGleb Smirnoff (void)swprintf(codebuf, sizeof(codebuf)/sizeof(wchar_t), L"<0x%X>", ch);
257586fbee6SGleb Smirnoff for (tmp = codebuf; *tmp != L'\0'; ++tmp) {
258586fbee6SGleb Smirnoff putwc(*tmp, fp);
25958011702SAndrey A. Chernov if (++cnt == 79) {
260586fbee6SGleb Smirnoff putwc(L'\r', fp);
261586fbee6SGleb Smirnoff putwc(L'\n', fp);
26258011702SAndrey A. Chernov cnt = 0;
26358011702SAndrey A. Chernov }
26458011702SAndrey A. Chernov }
265586fbee6SGleb Smirnoff --cnt;
26658011702SAndrey A. Chernov }
26758011702SAndrey A. Chernov }
2683c58f6ddSDavid Schultz }
269586fbee6SGleb Smirnoff (void)fwprintf(fp, L"%79s\r\n", " ");
2709b50d902SRodney W. Grimes rewind(fp);
2719b50d902SRodney W. Grimes
272752d887aSPhilippe Charnier if (fstat(fd, &sbuf))
27356e7ae90SKris Kennaway err(1, "can't stat temporary file");
2749b50d902SRodney W. Grimes mbufsize = sbuf.st_size;
275752d887aSPhilippe Charnier if (!(mbuf = malloc((u_int)mbufsize)))
27656e7ae90SKris Kennaway err(1, "out of memory");
277075f2939SMark Murray if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
27856e7ae90SKris Kennaway err(1, "can't read temporary file");
279e8d2bea9SWarner Losh fclose(fp);
2809b50d902SRodney W. Grimes }
281