1c2078896SGeoff Rehmet /* 2c2078896SGeoff Rehmet * Copyright (c) 1993 Christopher G. Demetriou 3c2078896SGeoff Rehmet * Copyright (c) 1988, 1990 Regents of the University of California. 4c2078896SGeoff Rehmet * All rights reserved. 5c2078896SGeoff Rehmet * 6c2078896SGeoff Rehmet * Redistribution and use in source and binary forms, with or without 7c2078896SGeoff Rehmet * modification, are permitted provided that the following conditions 8c2078896SGeoff Rehmet * are met: 9c2078896SGeoff Rehmet * 1. Redistributions of source code must retain the above copyright 10c2078896SGeoff Rehmet * notice, this list of conditions and the following disclaimer. 11c2078896SGeoff Rehmet * 2. Redistributions in binary form must reproduce the above copyright 12c2078896SGeoff Rehmet * notice, this list of conditions and the following disclaimer in the 13c2078896SGeoff Rehmet * documentation and/or other materials provided with the distribution. 14c2078896SGeoff Rehmet * 3. All advertising materials mentioning features or use of this software 15c2078896SGeoff Rehmet * must display the following acknowledgement: 16c2078896SGeoff Rehmet * This product includes software developed by the University of 17c2078896SGeoff Rehmet * California, Berkeley and its contributors. 18c2078896SGeoff Rehmet * 4. Neither the name of the University nor the names of its contributors 19c2078896SGeoff Rehmet * may be used to endorse or promote products derived from this software 20c2078896SGeoff Rehmet * without specific prior written permission. 21c2078896SGeoff Rehmet * 22c2078896SGeoff Rehmet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23c2078896SGeoff Rehmet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24c2078896SGeoff Rehmet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25c2078896SGeoff Rehmet * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26c2078896SGeoff Rehmet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27c2078896SGeoff Rehmet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28c2078896SGeoff Rehmet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29c2078896SGeoff Rehmet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30c2078896SGeoff Rehmet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31c2078896SGeoff Rehmet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32c2078896SGeoff Rehmet * SUCH DAMAGE. 33c2078896SGeoff Rehmet */ 34c2078896SGeoff Rehmet 3520bee7c8SMark Murray #include <sys/cdefs.h> 3620bee7c8SMark Murray 3720bee7c8SMark Murray __FBSDID("$FreeBSD$"); 3820bee7c8SMark Murray 39c2078896SGeoff Rehmet #ifndef lint 403f9b28f9SPhilippe Charnier static const char copyright[] = 41c2078896SGeoff Rehmet "@(#) Copyright (c) 1988 Regents of the University of California.\n\ 42c2078896SGeoff Rehmet All rights reserved.\n"; 43c2078896SGeoff Rehmet #endif /* not lint */ 44c2078896SGeoff Rehmet 45c2078896SGeoff Rehmet #ifndef lint 4620bee7c8SMark Murray static const char sccsid[] = "from: @(#)wall.c 5.14 (Berkeley) 3/2/91"; 473f9b28f9SPhilippe Charnier #endif 48c2078896SGeoff Rehmet 49c2078896SGeoff Rehmet /* 50c2078896SGeoff Rehmet * This program is not related to David Wall, whose Stanford Ph.D. thesis 51c2078896SGeoff Rehmet * is entitled "Mechanisms for Broadcast and Selective Broadcast". 52c2078896SGeoff Rehmet */ 53c2078896SGeoff Rehmet 54c2078896SGeoff Rehmet #include <sys/param.h> 55c2078896SGeoff Rehmet #include <sys/stat.h> 56c2078896SGeoff Rehmet #include <sys/uio.h> 5720bee7c8SMark Murray #include <rpc/rpc.h> 5820bee7c8SMark Murray #include <rpcsvc/rwall.h> 593f9b28f9SPhilippe Charnier #include <err.h> 603f9b28f9SPhilippe Charnier #include <paths.h> 61c2078896SGeoff Rehmet #include <pwd.h> 62c2078896SGeoff Rehmet #include <stdio.h> 63c2078896SGeoff Rehmet #include <stdlib.h> 643f9b28f9SPhilippe Charnier #include <string.h> 6556e7ae90SKris Kennaway #include <time.h> 663f9b28f9SPhilippe Charnier #include <unistd.h> 67c2078896SGeoff Rehmet 68c2078896SGeoff Rehmet char *mbuf; 69c2078896SGeoff Rehmet 7020bee7c8SMark Murray static char notty[] = "no tty"; 7120bee7c8SMark Murray 7220bee7c8SMark Murray void makemsg(const char *); 7320bee7c8SMark Murray static void usage(void); 743f9b28f9SPhilippe Charnier 75c2078896SGeoff Rehmet /* ARGSUSED */ 763f9b28f9SPhilippe Charnier int 7720bee7c8SMark Murray main(int argc, char *argv[]) 78c2078896SGeoff Rehmet { 79c2078896SGeoff Rehmet char *wallhost, res; 80c2078896SGeoff Rehmet CLIENT *cl; 8142f84d83SPeter Wemm struct timeval tv; 82c2078896SGeoff Rehmet 833f9b28f9SPhilippe Charnier if ((argc < 2) || (argc > 3)) 843f9b28f9SPhilippe Charnier usage(); 85c2078896SGeoff Rehmet 86c2078896SGeoff Rehmet wallhost = argv[1]; 87c2078896SGeoff Rehmet 88c2078896SGeoff Rehmet makemsg(argv[2]); 89c2078896SGeoff Rehmet 90c2078896SGeoff Rehmet /* 91c2078896SGeoff Rehmet * Create client "handle" used for calling MESSAGEPROG on the 92c2078896SGeoff Rehmet * server designated on the command line. We tell the rpc package 93c2078896SGeoff Rehmet * to use the "tcp" protocol when contacting the server. 94c2078896SGeoff Rehmet */ 95c2078896SGeoff Rehmet cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp"); 96c2078896SGeoff Rehmet if (cl == NULL) { 97c2078896SGeoff Rehmet /* 98c2078896SGeoff Rehmet * Couldn't establish connection with server. 99c2078896SGeoff Rehmet * Print error message and die. 100c2078896SGeoff Rehmet */ 1013f9b28f9SPhilippe Charnier errx(1, "%s", clnt_spcreateerror(wallhost)); 102c2078896SGeoff Rehmet } 103c2078896SGeoff Rehmet 10442f84d83SPeter Wemm tv.tv_sec = 15; /* XXX ?? */ 10542f84d83SPeter Wemm tv.tv_usec = 0; 10620bee7c8SMark Murray if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void, 10720bee7c8SMark Murray &res, tv) != RPC_SUCCESS) { 108c2078896SGeoff Rehmet /* 109c2078896SGeoff Rehmet * An error occurred while calling the server. 110c2078896SGeoff Rehmet * Print error message and die. 111c2078896SGeoff Rehmet */ 1123f9b28f9SPhilippe Charnier errx(1, "%s", clnt_sperror(cl, wallhost)); 113c2078896SGeoff Rehmet } 114c2078896SGeoff Rehmet 11520bee7c8SMark Murray return (0); 116c2078896SGeoff Rehmet } 117c2078896SGeoff Rehmet 1183f9b28f9SPhilippe Charnier static void 11920bee7c8SMark Murray usage(void) 1203f9b28f9SPhilippe Charnier { 12120bee7c8SMark Murray fprintf(stderr, "usage: rwall hostname [file]\n"); 1223f9b28f9SPhilippe Charnier exit(1); 1233f9b28f9SPhilippe Charnier } 1243f9b28f9SPhilippe Charnier 1253f9b28f9SPhilippe Charnier void 12620bee7c8SMark Murray makemsg(const char *fname) 127c2078896SGeoff Rehmet { 128c2078896SGeoff Rehmet struct tm *lt; 129c2078896SGeoff Rehmet struct passwd *pw; 130c2078896SGeoff Rehmet struct stat sbuf; 13156e7ae90SKris Kennaway time_t now; 132c2078896SGeoff Rehmet FILE *fp; 133c2078896SGeoff Rehmet int fd; 13420bee7c8SMark Murray size_t mbufsize; 13556e7ae90SKris Kennaway char *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 13656e7ae90SKris Kennaway const char *whom; 137c2078896SGeoff Rehmet 13820bee7c8SMark Murray snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 13920bee7c8SMark Murray fd = mkstemp(tmpname); 14020bee7c8SMark Murray fp = fdopen(fd, "r+"); 14120bee7c8SMark Murray if (fd == -1 || !fp) 14256e7ae90SKris Kennaway err(1, "can't open temporary file"); 14320bee7c8SMark Murray unlink(tmpname); 144c2078896SGeoff Rehmet 14520bee7c8SMark Murray whom = getlogin(); 14620bee7c8SMark Murray if (!whom) { 14720bee7c8SMark Murray pw = getpwuid(getuid()); 14820bee7c8SMark Murray whom = pw ? pw->pw_name : "???"; 14920bee7c8SMark Murray } 15020bee7c8SMark Murray gethostname(hostname, sizeof(hostname)); 15120bee7c8SMark Murray time(&now); 152c2078896SGeoff Rehmet lt = localtime(&now); 153c2078896SGeoff Rehmet 154c2078896SGeoff Rehmet /* 155c2078896SGeoff Rehmet * all this stuff is to blank out a square for the message; 156c2078896SGeoff Rehmet * we wrap message lines at column 79, not 80, because some 157c2078896SGeoff Rehmet * terminals wrap after 79, some do not, and we can't tell. 158c2078896SGeoff Rehmet * Which means that we may leave a non-blank character 159c2078896SGeoff Rehmet * in column 80, but that can't be helped. 160c2078896SGeoff Rehmet */ 16120bee7c8SMark Murray fprintf(fp, "Remote Broadcast Message from %s@%s\n", 162c2078896SGeoff Rehmet whom, hostname); 16356e7ae90SKris Kennaway tty = ttyname(STDERR_FILENO); 16456e7ae90SKris Kennaway if (tty == NULL) 16520bee7c8SMark Murray tty = notty; 16620bee7c8SMark Murray fprintf(fp, " (%s) at %d:%02d ...\n", tty, 167c2078896SGeoff Rehmet lt->tm_hour, lt->tm_min); 168c2078896SGeoff Rehmet 169c2078896SGeoff Rehmet putc('\n', fp); 170c2078896SGeoff Rehmet 1713f9b28f9SPhilippe Charnier if (fname && !(freopen(fname, "r", stdin))) 17256e7ae90SKris Kennaway err(1, "can't read %s", fname); 173c2078896SGeoff Rehmet while (fgets(lbuf, sizeof(lbuf), stdin)) 174c2078896SGeoff Rehmet fputs(lbuf, fp); 175c2078896SGeoff Rehmet rewind(fp); 176c2078896SGeoff Rehmet 1773f9b28f9SPhilippe Charnier if (fstat(fd, &sbuf)) 17856e7ae90SKris Kennaway err(1, "can't stat temporary file"); 17920bee7c8SMark Murray mbufsize = (size_t)sbuf.st_size; 18020bee7c8SMark Murray mbuf = malloc(mbufsize); 18120bee7c8SMark Murray if (mbuf == NULL) 18256e7ae90SKris Kennaway err(1, "out of memory"); 1833f9b28f9SPhilippe Charnier if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 18456e7ae90SKris Kennaway err(1, "can't read temporary file"); 18520bee7c8SMark Murray close(fd); 186c2078896SGeoff Rehmet } 187