xref: /freebsd/usr.bin/rwall/rwall.c (revision ed838bb1cd248c9ee0a737e97fa49d0f9d5e46bb)
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 
35c2078896SGeoff Rehmet #ifndef lint
363f9b28f9SPhilippe Charnier static const char copyright[] =
37c2078896SGeoff Rehmet "@(#) Copyright (c) 1988 Regents of the University of California.\n\
38c2078896SGeoff Rehmet  All rights reserved.\n";
39c2078896SGeoff Rehmet #endif /* not lint */
40c2078896SGeoff Rehmet 
41c2078896SGeoff Rehmet #ifndef lint
4220bee7c8SMark Murray static const char sccsid[] = "from: @(#)wall.c	5.14 (Berkeley) 3/2/91";
433f9b28f9SPhilippe Charnier #endif
44c2078896SGeoff Rehmet 
45c8ad87deSMark Murray #include <sys/cdefs.h>
46c8ad87deSMark Murray __FBSDID("$FreeBSD$");
47c8ad87deSMark Murray 
48c2078896SGeoff Rehmet /*
49c2078896SGeoff Rehmet  * This program is not related to David Wall, whose Stanford Ph.D. thesis
50c2078896SGeoff Rehmet  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
51c2078896SGeoff Rehmet  */
52c2078896SGeoff Rehmet 
53c2078896SGeoff Rehmet #include <sys/param.h>
54c2078896SGeoff Rehmet #include <sys/stat.h>
55c2078896SGeoff Rehmet #include <sys/uio.h>
5620bee7c8SMark Murray #include <rpc/rpc.h>
5720bee7c8SMark Murray #include <rpcsvc/rwall.h>
583f9b28f9SPhilippe Charnier #include <err.h>
593f9b28f9SPhilippe Charnier #include <paths.h>
60c2078896SGeoff Rehmet #include <pwd.h>
61c2078896SGeoff Rehmet #include <stdio.h>
62c2078896SGeoff Rehmet #include <stdlib.h>
633f9b28f9SPhilippe Charnier #include <string.h>
6456e7ae90SKris Kennaway #include <time.h>
653f9b28f9SPhilippe Charnier #include <unistd.h>
66c2078896SGeoff Rehmet 
67c2078896SGeoff Rehmet char *mbuf;
68c2078896SGeoff Rehmet 
6920bee7c8SMark Murray static char notty[] = "no tty";
7020bee7c8SMark Murray 
7120bee7c8SMark Murray void	makemsg(const char *);
7220bee7c8SMark Murray static void usage(void);
733f9b28f9SPhilippe Charnier 
74c2078896SGeoff Rehmet /* ARGSUSED */
753f9b28f9SPhilippe Charnier int
7620bee7c8SMark Murray main(int argc, char *argv[])
77c2078896SGeoff Rehmet {
78c2078896SGeoff Rehmet 	char *wallhost, res;
79c2078896SGeoff Rehmet 	CLIENT *cl;
8042f84d83SPeter Wemm 	struct timeval tv;
81c2078896SGeoff Rehmet 
823f9b28f9SPhilippe Charnier 	if ((argc < 2) || (argc > 3))
833f9b28f9SPhilippe Charnier 		usage();
84c2078896SGeoff Rehmet 
85c2078896SGeoff Rehmet 	wallhost = argv[1];
86c2078896SGeoff Rehmet 
87c2078896SGeoff Rehmet 	makemsg(argv[2]);
88c2078896SGeoff Rehmet 
89c2078896SGeoff Rehmet 	/*
90c2078896SGeoff Rehmet 	 * Create client "handle" used for calling MESSAGEPROG on the
91c2078896SGeoff Rehmet 	 * server designated on the command line. We tell the rpc package
92c2078896SGeoff Rehmet 	 * to use the "tcp" protocol when contacting the server.
93c2078896SGeoff Rehmet 	*/
94c2078896SGeoff Rehmet 	cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
95c2078896SGeoff Rehmet 	if (cl == NULL) {
96c2078896SGeoff Rehmet 		/*
97c2078896SGeoff Rehmet 		 * Couldn't establish connection with server.
98c2078896SGeoff Rehmet 		 * Print error message and die.
99c2078896SGeoff Rehmet 		 */
1003f9b28f9SPhilippe Charnier 		errx(1, "%s", clnt_spcreateerror(wallhost));
101c2078896SGeoff Rehmet 	}
102c2078896SGeoff Rehmet 
10342f84d83SPeter Wemm 	tv.tv_sec = 15;		/* XXX ?? */
10442f84d83SPeter Wemm 	tv.tv_usec = 0;
105ed838bb1SPeter Wemm 	if (clnt_call(cl, WALLPROC_WALL, (xdrproc_t)xdr_wrapstring, &mbuf,
106ed838bb1SPeter Wemm 	    (xdrproc_t)xdr_void, &res, tv) != RPC_SUCCESS) {
107c2078896SGeoff Rehmet 		/*
108c2078896SGeoff Rehmet 		 * An error occurred while calling the server.
109c2078896SGeoff Rehmet 		 * Print error message and die.
110c2078896SGeoff Rehmet 		 */
1113f9b28f9SPhilippe Charnier 		errx(1, "%s", clnt_sperror(cl, wallhost));
112c2078896SGeoff Rehmet 	}
113c2078896SGeoff Rehmet 
11420bee7c8SMark Murray 	return (0);
115c2078896SGeoff Rehmet }
116c2078896SGeoff Rehmet 
1173f9b28f9SPhilippe Charnier static void
11820bee7c8SMark Murray usage(void)
1193f9b28f9SPhilippe Charnier {
12020bee7c8SMark Murray 	fprintf(stderr, "usage: rwall hostname [file]\n");
1213f9b28f9SPhilippe Charnier 	exit(1);
1223f9b28f9SPhilippe Charnier }
1233f9b28f9SPhilippe Charnier 
1243f9b28f9SPhilippe Charnier void
12520bee7c8SMark Murray makemsg(const char *fname)
126c2078896SGeoff Rehmet {
127c2078896SGeoff Rehmet 	struct tm *lt;
128c2078896SGeoff Rehmet 	struct passwd *pw;
129c2078896SGeoff Rehmet 	struct stat sbuf;
13056e7ae90SKris Kennaway 	time_t now;
131c2078896SGeoff Rehmet 	FILE *fp;
132c2078896SGeoff Rehmet 	int fd;
13320bee7c8SMark Murray 	size_t mbufsize;
13456e7ae90SKris Kennaway 	char *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
13556e7ae90SKris Kennaway 	const char *whom;
136c2078896SGeoff Rehmet 
13720bee7c8SMark Murray 	snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
138c8ad87deSMark Murray 	if ((fd = mkstemp(tmpname)) == -1 || (fp = fdopen(fd, "r+")) == NULL)
13956e7ae90SKris Kennaway 		err(1, "can't open temporary file");
14020bee7c8SMark Murray 	unlink(tmpname);
141c2078896SGeoff Rehmet 
14220bee7c8SMark Murray 	whom = getlogin();
14320bee7c8SMark Murray 	if (!whom) {
14420bee7c8SMark Murray 		pw = getpwuid(getuid());
14520bee7c8SMark Murray 		whom = pw ? pw->pw_name : "???";
14620bee7c8SMark Murray 	}
14720bee7c8SMark Murray 	gethostname(hostname, sizeof(hostname));
14820bee7c8SMark Murray 	time(&now);
149c2078896SGeoff Rehmet 	lt = localtime(&now);
150c2078896SGeoff Rehmet 
151c2078896SGeoff Rehmet 	/*
152c2078896SGeoff Rehmet 	 * all this stuff is to blank out a square for the message;
153c2078896SGeoff Rehmet 	 * we wrap message lines at column 79, not 80, because some
154c2078896SGeoff Rehmet 	 * terminals wrap after 79, some do not, and we can't tell.
155c2078896SGeoff Rehmet 	 * Which means that we may leave a non-blank character
156c2078896SGeoff Rehmet 	 * in column 80, but that can't be helped.
157c2078896SGeoff Rehmet 	 */
15820bee7c8SMark Murray 	fprintf(fp, "Remote Broadcast Message from %s@%s\n",
159c2078896SGeoff Rehmet 	    whom, hostname);
16056e7ae90SKris Kennaway 	tty = ttyname(STDERR_FILENO);
16156e7ae90SKris Kennaway 	if (tty == NULL)
16220bee7c8SMark Murray 		tty = notty;
16320bee7c8SMark Murray 	fprintf(fp, "        (%s) at %d:%02d ...\n", tty,
164c2078896SGeoff Rehmet 	    lt->tm_hour, lt->tm_min);
165c2078896SGeoff Rehmet 
166c2078896SGeoff Rehmet 	putc('\n', fp);
167c2078896SGeoff Rehmet 
1683f9b28f9SPhilippe Charnier 	if (fname && !(freopen(fname, "r", stdin)))
16956e7ae90SKris Kennaway 		err(1, "can't read %s", fname);
170c2078896SGeoff Rehmet 	while (fgets(lbuf, sizeof(lbuf), stdin))
171c2078896SGeoff Rehmet 		fputs(lbuf, fp);
172c2078896SGeoff Rehmet 	rewind(fp);
173c2078896SGeoff Rehmet 
1743f9b28f9SPhilippe Charnier 	if (fstat(fd, &sbuf))
17556e7ae90SKris Kennaway 		err(1, "can't stat temporary file");
17620bee7c8SMark Murray 	mbufsize = (size_t)sbuf.st_size;
17720bee7c8SMark Murray 	mbuf = malloc(mbufsize);
17820bee7c8SMark Murray 	if (mbuf == NULL)
17956e7ae90SKris Kennaway 		err(1, "out of memory");
180f4ac32deSDavid Malone 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != (u_int)mbufsize)
18156e7ae90SKris Kennaway 		err(1, "can't read temporary file");
18220bee7c8SMark Murray 	close(fd);
183c2078896SGeoff Rehmet }
184