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