1 /* $NetBSD: wall.c,v 1.20 2002/08/16 20:21:49 itojun 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.20 2002/08/16 20:21:49 itojun 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 <errno.h> 61 #include <paths.h> 62 #include <pwd.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <unistd.h> 67 #include <util.h> 68 69 #include "utmpentry.h" 70 71 void makemsg(const char *); 72 int main(int, char **); 73 74 int nobanner; 75 size_t mbufsize; 76 char *mbuf; 77 78 /* ARGSUSED */ 79 int 80 main(int argc, char **argv) 81 { 82 int ch; 83 struct iovec iov; 84 char *p; 85 struct passwd *pep; 86 struct utmpentry *ep; 87 gid_t egid; 88 89 egid = getegid(); 90 if (setegid(getgid()) == -1) 91 err(1, "setegid"); 92 pep = getpwnam("nobody"); 93 94 while ((ch = getopt(argc, argv, "n")) != -1) 95 switch (ch) { 96 case 'n': 97 /* undoc option for shutdown: suppress banner */ 98 if (geteuid() == 0 || (pep && getuid() == pep->pw_uid)) 99 nobanner = 1; 100 break; 101 case '?': 102 default: 103 usage: 104 (void)fprintf(stderr, "usage: wall [file]\n"); 105 exit(1); 106 } 107 argc -= optind; 108 argv += optind; 109 if (argc > 1) 110 goto usage; 111 112 makemsg(*argv); 113 114 iov.iov_base = mbuf; 115 iov.iov_len = mbufsize; 116 (void)getutentries(NULL, &ep); 117 (void)setegid(egid); 118 for (; ep; ep = ep->next) 119 if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL) 120 warnx("%s", p); 121 exit(0); 122 } 123 124 void 125 makemsg(const char *fname) 126 { 127 int ch, cnt; 128 struct tm *lt; 129 struct passwd *pw; 130 struct stat sbuf; 131 time_t now; 132 FILE *fp; 133 int fd; 134 const char *whom; 135 char *p, *tty, tmpname[32], lbuf[100], hostname[MAXHOSTNAMELEN+1]; 136 137 (void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP); 138 if ((fd = mkstemp(tmpname)) == -1) 139 err(1, "can't open temporary file"); 140 (void)unlink(tmpname); 141 if (!(fp = fdopen(fd, "r+"))) 142 err(1, "can't open temporary file"); 143 144 if (!nobanner) { 145 if (!(whom = getlogin())) 146 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 147 (void)gethostname(hostname, sizeof(hostname)); 148 hostname[sizeof(hostname) - 1] = '\0'; 149 (void)time(&now); 150 lt = localtime(&now); 151 152 /* 153 * all this stuff is to blank out a square for the message; 154 * we wrap message lines at column 79, not 80, because some 155 * terminals wrap after 79, some do not, and we can't tell. 156 * Which means that we may leave a non-blank character 157 * in column 80, but that can't be helped. 158 */ 159 (void)fprintf(fp, "\r%79s\r\n", " "); 160 (void)snprintf(lbuf, sizeof lbuf, 161 "Broadcast Message from %s@%s", whom, hostname); 162 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 163 tty = ttyname(STDERR_FILENO); 164 if (tty == NULL) 165 tty = "??"; 166 (void)snprintf(lbuf, sizeof lbuf, 167 " (%s) at %d:%02d ...", tty, lt->tm_hour, 168 lt->tm_min); 169 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 170 } 171 (void)fprintf(fp, "%79s\r\n", " "); 172 173 if (fname && !(freopen(fname, "r", stdin))) 174 err(1, "can't read %s", fname); 175 while (fgets(lbuf, sizeof(lbuf), stdin)) 176 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 177 if (cnt == 79 || ch == '\n') { 178 for (; cnt < 79; ++cnt) 179 putc(' ', fp); 180 putc('\r', fp); 181 putc('\n', fp); 182 cnt = -1; 183 } 184 if (ch != '\n') 185 putc(ch, fp); 186 } 187 (void)fprintf(fp, "%79s\r\n", " "); 188 rewind(fp); 189 190 if (fstat(fd, &sbuf)) 191 err(1, "can't stat temporary file"); 192 if (sbuf.st_size > SIZE_T_MAX) 193 errx(1, "file too big"); 194 mbufsize = sbuf.st_size; 195 if (!(mbuf = malloc(mbufsize))) 196 err(1, "malloc"); 197 if (fread(mbuf, 1, mbufsize, fp) != mbufsize) 198 err(1, "can't read temporary file"); 199 (void)fclose(fp); 200 } 201