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