1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1993 Christopher G. Demetriou 5 * Copyright (c) 1988, 1990 Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static const char copyright[] = 39 "@(#) Copyright (c) 1988 Regents of the University of California.\n\ 40 All rights reserved.\n"; 41 #endif /* not lint */ 42 43 44 #include <sys/cdefs.h> 45 /* 46 * This program is not related to David Wall, whose Stanford Ph.D. thesis 47 * is entitled "Mechanisms for Broadcast and Selective Broadcast". 48 */ 49 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 #include <sys/uio.h> 53 #include <rpc/rpc.h> 54 #include <rpcsvc/rwall.h> 55 #include <err.h> 56 #include <paths.h> 57 #include <pwd.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <time.h> 62 #include <unistd.h> 63 64 static char *mbuf; 65 66 static char notty[] = "no tty"; 67 68 static void makemsg(const char *); 69 static void usage(void) __dead2; 70 71 /* ARGSUSED */ 72 int 73 main(int argc, char *argv[]) 74 { 75 char *wallhost, res; 76 CLIENT *cl; 77 struct timeval tv; 78 79 if ((argc < 2) || (argc > 3)) 80 usage(); 81 82 wallhost = argv[1]; 83 84 makemsg(argv[2]); 85 86 /* 87 * Create client "handle" used for calling MESSAGEPROG on the 88 * server designated on the command line. We tell the rpc package 89 * to use the "tcp" protocol when contacting the server. 90 */ 91 cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp"); 92 if (cl == NULL) { 93 /* 94 * Couldn't establish connection with server. 95 * Print error message and die. 96 */ 97 errx(1, "%s", clnt_spcreateerror(wallhost)); 98 } 99 100 tv.tv_sec = 15; /* XXX ?? */ 101 tv.tv_usec = 0; 102 if (clnt_call(cl, WALLPROC_WALL, (xdrproc_t)xdr_wrapstring, &mbuf, 103 (xdrproc_t)xdr_void, &res, tv) != RPC_SUCCESS) { 104 /* 105 * An error occurred while calling the server. 106 * Print error message and die. 107 */ 108 errx(1, "%s", clnt_sperror(cl, wallhost)); 109 } 110 111 return (0); 112 } 113 114 static void 115 usage(void) 116 { 117 fprintf(stderr, "usage: rwall host [file]\n"); 118 exit(1); 119 } 120 121 static void 122 makemsg(const char *fname) 123 { 124 struct tm *lt; 125 struct passwd *pw; 126 struct stat sbuf; 127 time_t now; 128 FILE *fp; 129 int fd; 130 size_t mbufsize; 131 char *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 132 const char *whom; 133 134 snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 135 if ((fd = mkstemp(tmpname)) == -1 || (fp = fdopen(fd, "r+")) == NULL) 136 err(1, "can't open temporary file"); 137 unlink(tmpname); 138 139 whom = getlogin(); 140 if (!whom) { 141 pw = getpwuid(getuid()); 142 whom = pw ? pw->pw_name : "???"; 143 } 144 gethostname(hostname, sizeof(hostname)); 145 time(&now); 146 lt = localtime(&now); 147 148 /* 149 * all this stuff is to blank out a square for the message; 150 * we wrap message lines at column 79, not 80, because some 151 * terminals wrap after 79, some do not, and we can't tell. 152 * Which means that we may leave a non-blank character 153 * in column 80, but that can't be helped. 154 */ 155 fprintf(fp, "Remote Broadcast Message from %s@%s\n", 156 whom, hostname); 157 tty = ttyname(STDERR_FILENO); 158 if (tty == NULL) 159 tty = notty; 160 fprintf(fp, " (%s) at %d:%02d ...\n", tty, 161 lt->tm_hour, lt->tm_min); 162 163 putc('\n', fp); 164 165 if (fname && !(freopen(fname, "r", stdin))) 166 err(1, "can't read %s", fname); 167 while (fgets(lbuf, sizeof(lbuf), stdin)) 168 fputs(lbuf, fp); 169 rewind(fp); 170 171 if (fstat(fd, &sbuf)) 172 err(1, "can't stat temporary file"); 173 mbufsize = (size_t)sbuf.st_size; 174 mbuf = malloc(mbufsize); 175 if (mbuf == NULL) 176 err(1, "out of memory"); 177 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != (u_int)mbufsize) 178 err(1, "can't read temporary file"); 179 close(fd); 180 } 181