1 /* $NetBSD: wall.c,v 1.9 1997/10/20 03:13:34 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 45 #endif 46 __RCSID("$NetBSD: wall.c,v 1.9 1997/10/20 03:13:34 lukem Exp $"); 47 #endif /* not lint */ 48 49 /* 50 * This program is not related to David Wall, whose Stanford Ph.D. thesis 51 * is entitled "Mechanisms for Broadcast and Selective Broadcast". 52 */ 53 54 #include <sys/param.h> 55 #include <sys/stat.h> 56 #include <sys/time.h> 57 #include <sys/uio.h> 58 59 #include <err.h> 60 #include <paths.h> 61 #include <pwd.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 #include <utmp.h> 67 #include <util.h> 68 69 void makemsg __P((char *)); 70 int main __P((int, char *[])); 71 72 #define IGNOREUSER "sleeper" 73 74 int nobanner; 75 int mbufsize; 76 char *mbuf; 77 78 /* ARGSUSED */ 79 int 80 main(argc, argv) 81 int argc; 82 char **argv; 83 { 84 extern int optind; 85 int ch; 86 struct iovec iov; 87 struct utmp utmp; 88 FILE *fp; 89 char *p; 90 struct passwd *pep = getpwnam("nobody"); 91 char line[sizeof(utmp.ut_line) + 1]; 92 93 while ((ch = getopt(argc, argv, "n")) != -1) 94 switch (ch) { 95 case 'n': 96 /* undoc option for shutdown: suppress banner */ 97 if (geteuid() == 0 || (pep && getuid() == pep->pw_uid)) 98 nobanner = 1; 99 break; 100 case '?': 101 default: 102 usage: 103 (void)fprintf(stderr, "usage: wall [file]\n"); 104 exit(1); 105 } 106 argc -= optind; 107 argv += optind; 108 if (argc > 1) 109 goto usage; 110 111 makemsg(*argv); 112 113 if (!(fp = fopen(_PATH_UTMP, "r"))) 114 err(1, "cannot read %s", _PATH_UTMP); 115 iov.iov_base = mbuf; 116 iov.iov_len = mbufsize; 117 /* NOSTRICT */ 118 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 119 if (!utmp.ut_name[0] || 120 !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 121 continue; 122 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line)); 123 line[sizeof(utmp.ut_line)] = '\0'; 124 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL) 125 warnx("%s", p); 126 } 127 exit(0); 128 } 129 130 void 131 makemsg(fname) 132 char *fname; 133 { 134 register int ch, cnt; 135 struct tm *lt; 136 struct passwd *pw; 137 struct stat sbuf; 138 time_t now; 139 FILE *fp; 140 int fd; 141 char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 142 143 (void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP); 144 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) 145 err(1, "can't open temporary file"); 146 (void)unlink(tmpname); 147 148 if (!nobanner) { 149 if (!(whom = getlogin())) 150 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 151 (void)gethostname(hostname, sizeof(hostname)); 152 (void)time(&now); 153 lt = localtime(&now); 154 155 /* 156 * all this stuff is to blank out a square for the message; 157 * we wrap message lines at column 79, not 80, because some 158 * terminals wrap after 79, some do not, and we can't tell. 159 * Which means that we may leave a non-blank character 160 * in column 80, but that can't be helped. 161 */ 162 (void)fprintf(fp, "\r%79s\r\n", " "); 163 (void)snprintf(lbuf, sizeof lbuf, 164 "Broadcast Message from %s@%s", whom, hostname); 165 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 166 (void)snprintf(lbuf, sizeof lbuf, " (%s) at %d:%02d ...", 167 ttyname(2), lt->tm_hour, lt->tm_min); 168 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 169 } 170 (void)fprintf(fp, "%79s\r\n", " "); 171 172 if (fname && !(freopen(fname, "r", stdin))) 173 err(1, "can't read %s", fname); 174 while (fgets(lbuf, sizeof(lbuf), stdin)) 175 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 176 if (cnt == 79 || ch == '\n') { 177 for (; cnt < 79; ++cnt) 178 putc(' ', fp); 179 putc('\r', fp); 180 putc('\n', fp); 181 cnt = -1; 182 } 183 if (ch != '\n') 184 putc(ch, fp); 185 } 186 (void)fprintf(fp, "%79s\r\n", " "); 187 rewind(fp); 188 189 if (fstat(fd, &sbuf)) 190 err(1, "can't stat temporary file"); 191 mbufsize = sbuf.st_size; 192 if (!(mbuf = malloc((u_int)mbufsize))) 193 err(1, "malloc"); 194 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 195 err(1, "can't read temporary file"); 196 (void)close(fd); 197 } 198