1 /* 2 * Copyright (c) 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)wall.c 8.2 (Berkeley) 11/16/93 31 * $FreeBSD: src/usr.bin/wall/wall.c,v 1.13.2.6 2001/10/18 08:08:17 des Exp $ 32 */ 33 34 /* 35 * This program is not related to David Wall, whose Stanford Ph.D. thesis 36 * is entitled "Mechanisms for Broadcast and Selective Broadcast". 37 */ 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 #include <sys/uio.h> 42 43 #include <ctype.h> 44 #include <err.h> 45 #include <grp.h> 46 #include <locale.h> 47 #include <paths.h> 48 #include <pwd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <time.h> 53 #include <unistd.h> 54 #include "utmpentry.h" 55 56 #include "ttymsg.h" 57 58 static void makemsg(char *); 59 static void usage(void); 60 61 static struct wallgroup { 62 struct wallgroup *next; 63 char *name; 64 gid_t gid; 65 } *grouplist; 66 static int nobanner; 67 static int mbufsize; 68 static char *mbuf; 69 70 int 71 main(int argc, char *argv[]) 72 { 73 struct iovec iov; 74 struct utmpentry *ep = NULL; /* avoid gcc warnings */ 75 int ch; 76 int ingroup; 77 struct wallgroup *g; 78 struct group *grp; 79 char **np; 80 const char *p; 81 struct passwd *pw; 82 83 (void)setlocale(LC_CTYPE, ""); 84 85 while ((ch = getopt(argc, argv, "g:n")) != -1) 86 switch (ch) { 87 case 'n': 88 /* undoc option for shutdown: suppress banner */ 89 if (geteuid() == 0) 90 nobanner = 1; 91 break; 92 case 'g': 93 g = (struct wallgroup *)malloc(sizeof *g); 94 g->next = grouplist; 95 g->name = optarg; 96 g->gid = -1; 97 grouplist = g; 98 break; 99 case '?': 100 default: 101 usage(); 102 } 103 argc -= optind; 104 argv += optind; 105 if (argc > 1) 106 usage(); 107 108 for (g = grouplist; g; g = g->next) { 109 grp = getgrnam(g->name); 110 if (grp != NULL) 111 g->gid = grp->gr_gid; 112 else 113 warnx("%s: no such group", g->name); 114 } 115 116 makemsg(*argv); 117 118 iov.iov_base = mbuf; 119 iov.iov_len = mbufsize; 120 121 getutentries(NULL, &ep); 122 /* NOSTRICT */ 123 for (; ep; ep = ep->next) { 124 if (grouplist) { 125 ingroup = 0; 126 pw = getpwnam(ep->name); 127 if (!pw) 128 continue; 129 for (g = grouplist; g && ingroup == 0; g = g->next) { 130 if (g->gid == (gid_t)-1) 131 continue; 132 if (g->gid == pw->pw_gid) 133 ingroup = 1; 134 else if ((grp = getgrgid(g->gid)) != NULL) { 135 for (np = grp->gr_mem; *np; np++) { 136 if (strcmp(*np, ep->name) == 0) { 137 ingroup = 1; 138 break; 139 } 140 } 141 } 142 } 143 if (ingroup == 0) 144 continue; 145 } 146 /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */ 147 if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1])) 148 continue; 149 150 if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL) 151 warnx("%s", p); 152 } 153 exit(0); 154 } 155 156 static void 157 usage(void) 158 { 159 (void)fprintf(stderr, "usage: wall [-g group] [file]\n"); 160 exit(1); 161 } 162 163 void 164 makemsg(char *fname) 165 { 166 int cnt; 167 unsigned char ch; 168 struct tm *lt; 169 struct passwd *pw; 170 struct stat sbuf; 171 time_t now; 172 FILE *fp; 173 int fd; 174 char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[MAXPATHLEN]; 175 const char *tty; 176 const char *whom; 177 gid_t egid; 178 179 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 180 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 181 err(1, "can't open temporary file"); 182 (void)unlink(tmpname); 183 184 if (!nobanner) { 185 tty = ttyname(STDERR_FILENO); 186 if (tty == NULL) 187 tty = "no tty"; 188 189 if (!(whom = getlogin())) 190 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 191 (void)gethostname(hostname, sizeof(hostname)); 192 (void)time(&now); 193 lt = localtime(&now); 194 195 /* 196 * all this stuff is to blank out a square for the message; 197 * we wrap message lines at column 79, not 80, because some 198 * terminals wrap after 79, some do not, and we can't tell. 199 * Which means that we may leave a non-blank character 200 * in column 80, but that can't be helped. 201 */ 202 (void)fprintf(fp, "\r%79s\r\n", " "); 203 (void)snprintf(lbuf, sizeof(lbuf), 204 "Broadcast Message from %s@%s", 205 whom, hostname); 206 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 207 (void)snprintf(lbuf, sizeof(lbuf), 208 " (%s) at %d:%02d %s...", tty, 209 lt->tm_hour, lt->tm_min, lt->tm_zone); 210 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 211 } 212 (void)fprintf(fp, "%79s\r\n", " "); 213 214 if (fname) { 215 egid = getegid(); 216 setegid(getgid()); 217 if (freopen(fname, "r", stdin) == NULL) 218 err(1, "can't read %s", fname); 219 setegid(egid); 220 } 221 while (fgets(lbuf, sizeof(lbuf), stdin)) 222 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 223 if (ch == '\r') { 224 cnt = 0; 225 } else if (cnt == 79 || ch == '\n') { 226 for (; cnt < 79; ++cnt) 227 putc(' ', fp); 228 putc('\r', fp); 229 putc('\n', fp); 230 cnt = 0; 231 } else if (((ch & 0x80) && ch < 0xA0) || 232 /* disable upper controls */ 233 (!isprint(ch) && !isspace(ch) && 234 ch != '\a' && ch != '\b') 235 ) { 236 if (ch & 0x80) { 237 ch &= 0x7F; 238 putc('M', fp); 239 if (++cnt == 79) { 240 putc('\r', fp); 241 putc('\n', fp); 242 cnt = 0; 243 } 244 putc('-', fp); 245 if (++cnt == 79) { 246 putc('\r', fp); 247 putc('\n', fp); 248 cnt = 0; 249 } 250 } 251 if (iscntrl(ch)) { 252 ch ^= 040; 253 putc('^', fp); 254 if (++cnt == 79) { 255 putc('\r', fp); 256 putc('\n', fp); 257 cnt = 0; 258 } 259 } 260 putc(ch, fp); 261 } else { 262 putc(ch, fp); 263 } 264 } 265 (void)fprintf(fp, "%79s\r\n", " "); 266 rewind(fp); 267 268 if (fstat(fd, &sbuf)) 269 err(1, "can't stat temporary file"); 270 mbufsize = sbuf.st_size; 271 if (!(mbuf = malloc((u_int)mbufsize))) 272 err(1, "out of memory"); 273 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 274 err(1, "can't read temporary file"); 275 (void)close(fd); 276 } 277