1 /* $NetBSD: rwall.c,v 1.7 1997/10/19 14:30:08 lukem Exp $ */ 2 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 #include <sys/cdefs.h> 38 #ifndef lint 39 __COPYRIGHT("@(#) Copyright (c) 1988 Regents of the University of California.\n\ 40 All rights reserved.\n"); 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "from: @(#)wall.c 5.14 (Berkeley) 3/2/91"; 46 #else 47 __RCSID("$NetBSD: rwall.c,v 1.7 1997/10/19 14:30:08 lukem Exp $"); 48 #endif 49 #endif /* not lint */ 50 51 /* 52 * This program is not related to David Wall, whose Stanford Ph.D. thesis 53 * is entitled "Mechanisms for Broadcast and Selective Broadcast". 54 */ 55 #include <sys/types.h> 56 #include <sys/param.h> 57 #include <sys/stat.h> 58 #include <err.h> 59 #include <paths.h> 60 #include <pwd.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <time.h> 65 #include <unistd.h> 66 67 #include <rpc/rpc.h> 68 #include <rpcsvc/rwall.h> 69 70 struct timeval timeout = { 25, 0 }; 71 int mbufsize; 72 char *mbuf; 73 74 int main __P((int, char **)); 75 void makemsg __P((char *)); 76 77 int 78 main(argc, argv) 79 int argc; 80 char **argv; 81 { 82 char *wallhost, res; 83 CLIENT *cl; 84 85 if ((argc < 2) || (argc > 3)) { 86 fprintf(stderr, "usage: %s hostname [file]\n", argv[0]); 87 exit(1); 88 } 89 90 wallhost = argv[1]; 91 92 makemsg(argv[2]); 93 94 /* 95 * Create client "handle" used for calling MESSAGEPROG on the 96 * server designated on the command line. We tell the rpc package 97 * to use the "tcp" protocol when contacting the server. 98 */ 99 cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp"); 100 if (cl == NULL) { 101 /* 102 * Couldn't establish connection with server. 103 * Print error message and die. 104 */ 105 clnt_pcreateerror(wallhost); 106 exit(1); 107 } 108 109 if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void, &res, timeout) != RPC_SUCCESS) { 110 /* 111 * An error occurred while calling the server. 112 * Print error message and die. 113 */ 114 clnt_perror(cl, wallhost); 115 exit(1); 116 } 117 118 exit(0); 119 } 120 121 void 122 makemsg(fname) 123 char *fname; 124 { 125 struct tm *lt; 126 struct passwd *pw; 127 struct stat sbuf; 128 time_t now; 129 FILE *fp; 130 int fd; 131 char *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[32]; 132 133 (void)strcpy(tmpname, _PATH_TMP); 134 (void)strcat(tmpname, "/wall.XXXXXX"); 135 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) 136 err(1, "can't open temporary file."); 137 (void)unlink(tmpname); 138 139 if (!(whom = getlogin())) 140 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 141 (void)gethostname(hostname, sizeof(hostname)); 142 (void)time(&now); 143 lt = localtime(&now); 144 145 /* 146 * all this stuff is to blank out a square for the message; 147 * we wrap message lines at column 79, not 80, because some 148 * terminals wrap after 79, some do not, and we can't tell. 149 * Which means that we may leave a non-blank character 150 * in column 80, but that can't be helped. 151 */ 152 (void)fprintf(fp, "Remote Broadcast Message from %s@%s\n", 153 whom, hostname); 154 (void)fprintf(fp, " (%s) at %d:%02d ...\n", ttyname(2), 155 lt->tm_hour, lt->tm_min); 156 157 putc('\n', fp); 158 159 if (fname && !(freopen(fname, "r", stdin))) 160 err(1, "can't read %s.", fname); 161 while (fgets(lbuf, sizeof(lbuf), stdin)) 162 fputs(lbuf, fp); 163 rewind(fp); 164 165 if (fstat(fd, &sbuf)) 166 err(1, "can't stat temporary file."); 167 mbufsize = sbuf.st_size; 168 if (!(mbuf = malloc((u_int)mbufsize))) 169 err(1, "malloc"); 170 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 171 err(1, "can't read temporary file."); 172 (void)close(fd); 173 } 174