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