wall.c revision 234275
1238438Sdteske/* 2238438Sdteske * Copyright (c) 1988, 1990, 1993 3238438Sdteske * The Regents of the University of California. All rights reserved. 4238438Sdteske * 5238438Sdteske * Redistribution and use in source and binary forms, with or without 6238438Sdteske * modification, are permitted provided that the following conditions 7238438Sdteske * are met: 8238438Sdteske * 1. Redistributions of source code must retain the above copyright 9238438Sdteske * notice, this list of conditions and the following disclaimer. 10238438Sdteske * 2. Redistributions in binary form must reproduce the above copyright 11238438Sdteske * notice, this list of conditions and the following disclaimer in the 12238438Sdteske * documentation and/or other materials provided with the distribution. 13238438Sdteske * 4. Neither the name of the University nor the names of its contributors 14238438Sdteske * may be used to endorse or promote products derived from this software 15238438Sdteske * without specific prior written permission. 16238438Sdteske * 17238438Sdteske * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18238438Sdteske * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19238438Sdteske * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20238438Sdteske * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21238438Sdteske * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22238438Sdteske * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23238438Sdteske * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24238438Sdteske * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25238438Sdteske * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26238438Sdteske * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27238438Sdteske * SUCH DAMAGE. 28238438Sdteske */ 29238438Sdteske 30238438Sdteske#include <sys/cdefs.h> 31238438Sdteske 32238438Sdteske__FBSDID("$FreeBSD: stable/9/usr.bin/wall/wall.c 234275 2012-04-14 09:48:52Z glebius $"); 33238438Sdteske 34238438Sdteske#ifndef lint 35238438Sdteskestatic const char copyright[] = 36238438Sdteske"@(#) Copyright (c) 1988, 1990, 1993\n\ 37238438Sdteske The Regents of the University of California. All rights reserved.\n"; 38238438Sdteske#endif 39238438Sdteske 40238438Sdteske#ifndef lint 41238438Sdteskestatic const char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 42238438Sdteske#endif 43238438Sdteske 44238438Sdteske/* 45238438Sdteske * This program is not related to David Wall, whose Stanford Ph.D. thesis 46238438Sdteske * is entitled "Mechanisms for Broadcast and Selective Broadcast". 47238438Sdteske */ 48238438Sdteske 49238438Sdteske#include <sys/param.h> 50238438Sdteske#include <sys/stat.h> 51238438Sdteske#include <sys/uio.h> 52238438Sdteske 53238438Sdteske#include <ctype.h> 54238438Sdteske#include <err.h> 55238438Sdteske#include <grp.h> 56238438Sdteske#include <locale.h> 57238438Sdteske#include <paths.h> 58238438Sdteske#include <pwd.h> 59238438Sdteske#include <stdio.h> 60238438Sdteske#include <stdlib.h> 61238438Sdteske#include <string.h> 62238438Sdteske#include <time.h> 63238438Sdteske#include <unistd.h> 64238438Sdteske#include <utmpx.h> 65238438Sdteske#include <wchar.h> 66238438Sdteske#include <wctype.h> 67238438Sdteske 68238438Sdteske#include "ttymsg.h" 69238438Sdteske 70238438Sdteskestatic void makemsg(char *); 71238438Sdteskestatic void usage(void); 72238438Sdteske 73238438Sdteskestruct wallgroup { 74238438Sdteske struct wallgroup *next; 75238438Sdteske char *name; 76238438Sdteske gid_t gid; 77238438Sdteske} *grouplist; 78238438Sdteskeint nobanner; 79238438Sdteskeint mbufsize; 80238438Sdteskechar *mbuf; 81238438Sdteske 82238438Sdteskestatic int 83238438Sdteskettystat(char *line) 84238438Sdteske{ 85238438Sdteske struct stat sb; 86238438Sdteske char ttybuf[MAXPATHLEN]; 87238438Sdteske 88238438Sdteske (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line); 89238438Sdteske if (stat(ttybuf, &sb) == 0) { 90238438Sdteske return (0); 91238438Sdteske } else 92238438Sdteske return (-1); 93238438Sdteske} 94238438Sdteske 95238438Sdteskeint 96238438Sdteskemain(int argc, char *argv[]) 97238438Sdteske{ 98238438Sdteske struct iovec iov; 99238438Sdteske struct utmpx *utmp; 100238438Sdteske int ch; 101238438Sdteske int ingroup; 102238438Sdteske struct wallgroup *g; 103238438Sdteske struct group *grp; 104238438Sdteske char **np; 105238438Sdteske const char *p; 106238438Sdteske struct passwd *pw; 107238438Sdteske 108238438Sdteske (void)setlocale(LC_CTYPE, ""); 109238438Sdteske 110238438Sdteske while ((ch = getopt(argc, argv, "g:n")) != -1) 111238438Sdteske switch (ch) { 112238438Sdteske case 'n': 113238438Sdteske /* undoc option for shutdown: suppress banner */ 114238438Sdteske if (geteuid() == 0) 115238438Sdteske nobanner = 1; 116238438Sdteske break; 117238438Sdteske case 'g': 118238438Sdteske g = (struct wallgroup *)malloc(sizeof *g); 119238438Sdteske g->next = grouplist; 120238438Sdteske g->name = optarg; 121238438Sdteske g->gid = -1; 122238438Sdteske grouplist = g; 123238438Sdteske break; 124238438Sdteske case '?': 125238438Sdteske default: 126238438Sdteske usage(); 127238438Sdteske } 128238438Sdteske argc -= optind; 129238438Sdteske argv += optind; 130238438Sdteske if (argc > 1) 131238438Sdteske usage(); 132238438Sdteske 133238438Sdteske for (g = grouplist; g; g = g->next) { 134238438Sdteske grp = getgrnam(g->name); 135238438Sdteske if (grp != NULL) 136238438Sdteske g->gid = grp->gr_gid; 137238438Sdteske else 138238438Sdteske warnx("%s: no such group", g->name); 139238438Sdteske } 140238438Sdteske 141238438Sdteske makemsg(*argv); 142238438Sdteske 143 iov.iov_base = mbuf; 144 iov.iov_len = mbufsize; 145 /* NOSTRICT */ 146 while ((utmp = getutxent()) != NULL) { 147 if (utmp->ut_type != USER_PROCESS) 148 continue; 149 if (ttystat(utmp->ut_line) != 0) 150 continue; 151 if (grouplist) { 152 ingroup = 0; 153 pw = getpwnam(utmp->ut_user); 154 if (!pw) 155 continue; 156 for (g = grouplist; g && ingroup == 0; g = g->next) { 157 if (g->gid == (gid_t)-1) 158 continue; 159 if (g->gid == pw->pw_gid) 160 ingroup = 1; 161 else if ((grp = getgrgid(g->gid)) != NULL) { 162 for (np = grp->gr_mem; *np; np++) { 163 if (strcmp(*np, utmp->ut_user) == 0) { 164 ingroup = 1; 165 break; 166 } 167 } 168 } 169 } 170 if (ingroup == 0) 171 continue; 172 } 173 if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL) 174 warnx("%s", p); 175 } 176 exit(0); 177} 178 179static void 180usage(void) 181{ 182 (void)fprintf(stderr, "usage: wall [-g group] [file]\n"); 183 exit(1); 184} 185 186void 187makemsg(char *fname) 188{ 189 int cnt; 190 wchar_t ch; 191 struct tm *lt; 192 struct passwd *pw; 193 struct stat sbuf; 194 time_t now; 195 FILE *fp; 196 int fd; 197 char hostname[MAXHOSTNAMELEN], tmpname[64]; 198 wchar_t *p, *tmp, lbuf[256], codebuf[13]; 199 const char *tty; 200 const char *whom; 201 gid_t egid; 202 203 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 204 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 205 err(1, "can't open temporary file"); 206 (void)unlink(tmpname); 207 208 if (!nobanner) { 209 tty = ttyname(STDERR_FILENO); 210 if (tty == NULL) 211 tty = "no tty"; 212 213 if (!(whom = getlogin())) 214 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 215 (void)gethostname(hostname, sizeof(hostname)); 216 (void)time(&now); 217 lt = localtime(&now); 218 219 /* 220 * all this stuff is to blank out a square for the message; 221 * we wrap message lines at column 79, not 80, because some 222 * terminals wrap after 79, some do not, and we can't tell. 223 * Which means that we may leave a non-blank character 224 * in column 80, but that can't be helped. 225 */ 226 (void)fwprintf(fp, L"\r%79s\r\n", " "); 227 (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t), 228 L"Broadcast Message from %s@%s", 229 whom, hostname); 230 (void)fwprintf(fp, L"%-79.79S\007\007\r\n", lbuf); 231 (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t), 232 L" (%s) at %d:%02d %s...", tty, 233 lt->tm_hour, lt->tm_min, lt->tm_zone); 234 (void)fwprintf(fp, L"%-79.79S\r\n", lbuf); 235 } 236 (void)fwprintf(fp, L"%79s\r\n", " "); 237 238 if (fname) { 239 egid = getegid(); 240 setegid(getgid()); 241 if (freopen(fname, "r", stdin) == NULL) 242 err(1, "can't read %s", fname); 243 setegid(egid); 244 } 245 cnt = 0; 246 while (fgetws(lbuf, sizeof(lbuf)/sizeof(wchar_t), stdin)) { 247 for (p = lbuf; (ch = *p) != L'\0'; ++p, ++cnt) { 248 if (ch == L'\r') { 249 putwc(L'\r', fp); 250 cnt = 0; 251 continue; 252 } else if (ch == L'\n') { 253 for (; cnt < 79; ++cnt) 254 putwc(L' ', fp); 255 putwc(L'\r', fp); 256 putwc(L'\n', fp); 257 break; 258 } 259 if (cnt == 79) { 260 putwc(L'\r', fp); 261 putwc(L'\n', fp); 262 cnt = 0; 263 } 264 if (iswprint(ch) || iswspace(ch) || ch == L'\a' || ch == L'\b') { 265 putwc(ch, fp); 266 } else { 267 (void)swprintf(codebuf, sizeof(codebuf)/sizeof(wchar_t), L"<0x%X>", ch); 268 for (tmp = codebuf; *tmp != L'\0'; ++tmp) { 269 putwc(*tmp, fp); 270 if (++cnt == 79) { 271 putwc(L'\r', fp); 272 putwc(L'\n', fp); 273 cnt = 0; 274 } 275 } 276 --cnt; 277 } 278 } 279 } 280 (void)fwprintf(fp, L"%79s\r\n", " "); 281 rewind(fp); 282 283 if (fstat(fd, &sbuf)) 284 err(1, "can't stat temporary file"); 285 mbufsize = sbuf.st_size; 286 if (!(mbuf = malloc((u_int)mbufsize))) 287 err(1, "out of memory"); 288 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 289 err(1, "can't read temporary file"); 290 (void)close(fd); 291} 292