xref: /freebsd/usr.bin/rwall/rwall.c (revision 56e7ae90cb45f7803a1821b1fd3178a04a99bdf3)
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
423f9b28f9SPhilippe Charnier #if 0
433f9b28f9SPhilippe Charnier static char sccsid[] = "from: @(#)wall.c	5.14 (Berkeley) 3/2/91";
443f9b28f9SPhilippe Charnier #endif
453f9b28f9SPhilippe Charnier static const char rcsid[] =
46c3aac50fSPeter Wemm   "$FreeBSD$";
47c2078896SGeoff Rehmet #endif /* not lint */
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/time.h>
57c2078896SGeoff Rehmet #include <sys/uio.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>
663f9b28f9SPhilippe Charnier #include <utmp.h>
67c2078896SGeoff Rehmet 
68c2078896SGeoff Rehmet #include <rpc/rpc.h>
69c2078896SGeoff Rehmet #include <rpcsvc/rwall.h>
70c2078896SGeoff Rehmet 
71c2078896SGeoff Rehmet int mbufsize;
72c2078896SGeoff Rehmet char *mbuf;
73c2078896SGeoff Rehmet 
743f9b28f9SPhilippe Charnier void	makemsg __P((char *));
753f9b28f9SPhilippe Charnier static void usage __P((void));
7656e7ae90SKris Kennaway char   *ttymsg __P((struct iovec *, int, char *, int));
773f9b28f9SPhilippe Charnier 
78c2078896SGeoff Rehmet /* ARGSUSED */
793f9b28f9SPhilippe Charnier int
80c2078896SGeoff Rehmet main(argc, argv)
81c2078896SGeoff Rehmet 	int argc;
82c2078896SGeoff Rehmet 	char **argv;
83c2078896SGeoff Rehmet {
84c2078896SGeoff Rehmet 	char *wallhost, res;
85c2078896SGeoff Rehmet 	CLIENT *cl;
8642f84d83SPeter Wemm 	struct timeval tv;
87c2078896SGeoff Rehmet 
883f9b28f9SPhilippe Charnier 	if ((argc < 2) || (argc > 3))
893f9b28f9SPhilippe Charnier 		usage();
90c2078896SGeoff Rehmet 
91c2078896SGeoff Rehmet 	wallhost = argv[1];
92c2078896SGeoff Rehmet 
93c2078896SGeoff Rehmet 	makemsg(argv[2]);
94c2078896SGeoff Rehmet 
95c2078896SGeoff Rehmet 	/*
96c2078896SGeoff Rehmet 	 * Create client "handle" used for calling MESSAGEPROG on the
97c2078896SGeoff Rehmet 	 * server designated on the command line. We tell the rpc package
98c2078896SGeoff Rehmet 	 * to use the "tcp" protocol when contacting the server.
99c2078896SGeoff Rehmet 	*/
100c2078896SGeoff Rehmet 	cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
101c2078896SGeoff Rehmet 	if (cl == NULL) {
102c2078896SGeoff Rehmet 		/*
103c2078896SGeoff Rehmet 		 * Couldn't establish connection with server.
104c2078896SGeoff Rehmet 		 * Print error message and die.
105c2078896SGeoff Rehmet 		 */
1063f9b28f9SPhilippe Charnier 		errx(1, "%s", clnt_spcreateerror(wallhost));
107c2078896SGeoff Rehmet 	}
108c2078896SGeoff Rehmet 
10942f84d83SPeter Wemm 	tv.tv_sec = 15;		/* XXX ?? */
11042f84d83SPeter Wemm 	tv.tv_usec = 0;
11142f84d83SPeter Wemm 	if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void, &res, tv) != RPC_SUCCESS) {
112c2078896SGeoff Rehmet 		/*
113c2078896SGeoff Rehmet 		 * An error occurred while calling the server.
114c2078896SGeoff Rehmet 		 * Print error message and die.
115c2078896SGeoff Rehmet 		 */
1163f9b28f9SPhilippe Charnier 		errx(1, "%s", clnt_sperror(cl, wallhost));
117c2078896SGeoff Rehmet 	}
118c2078896SGeoff Rehmet 
119c2078896SGeoff Rehmet 	exit(0);
120c2078896SGeoff Rehmet }
121c2078896SGeoff Rehmet 
1223f9b28f9SPhilippe Charnier static void
1233f9b28f9SPhilippe Charnier usage()
1243f9b28f9SPhilippe Charnier {
12556e7ae90SKris Kennaway 	(void)fprintf(stderr, "usage: rwall hostname [file]\n");
1263f9b28f9SPhilippe Charnier 	exit(1);
1273f9b28f9SPhilippe Charnier }
1283f9b28f9SPhilippe Charnier 
1293f9b28f9SPhilippe Charnier void
130c2078896SGeoff Rehmet makemsg(fname)
131c2078896SGeoff Rehmet 	char *fname;
132c2078896SGeoff Rehmet {
133c2078896SGeoff Rehmet 	struct tm *lt;
134c2078896SGeoff Rehmet 	struct passwd *pw;
135c2078896SGeoff Rehmet 	struct stat sbuf;
13656e7ae90SKris Kennaway 	time_t now;
137c2078896SGeoff Rehmet 	FILE *fp;
138c2078896SGeoff Rehmet 	int fd;
13956e7ae90SKris Kennaway 	char *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
14056e7ae90SKris Kennaway 	const char *whom;
141c2078896SGeoff Rehmet 
14256e7ae90SKris Kennaway 	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
14356e7ae90SKris Kennaway 	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
14456e7ae90SKris Kennaway 		err(1, "can't open temporary file");
145c2078896SGeoff Rehmet 	(void)unlink(tmpname);
146c2078896SGeoff Rehmet 
147c2078896SGeoff Rehmet 	if (!(whom = getlogin()))
148c2078896SGeoff Rehmet 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
149c2078896SGeoff Rehmet 	(void)gethostname(hostname, sizeof(hostname));
150c2078896SGeoff Rehmet 	(void)time(&now);
151c2078896SGeoff Rehmet 	lt = localtime(&now);
152c2078896SGeoff Rehmet 
153c2078896SGeoff Rehmet 	/*
154c2078896SGeoff Rehmet 	 * all this stuff is to blank out a square for the message;
155c2078896SGeoff Rehmet 	 * we wrap message lines at column 79, not 80, because some
156c2078896SGeoff Rehmet 	 * terminals wrap after 79, some do not, and we can't tell.
157c2078896SGeoff Rehmet 	 * Which means that we may leave a non-blank character
158c2078896SGeoff Rehmet 	 * in column 80, but that can't be helped.
159c2078896SGeoff Rehmet 	 */
160c2078896SGeoff Rehmet 	(void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
161c2078896SGeoff Rehmet 	    whom, hostname);
16256e7ae90SKris Kennaway 	tty = ttyname(STDERR_FILENO);
16356e7ae90SKris Kennaway 	if (tty == NULL)
16456e7ae90SKris Kennaway 		tty = "no tty";
16556e7ae90SKris Kennaway 	(void)fprintf(fp, "        (%s) at %d:%02d ...\n", tty,
166c2078896SGeoff Rehmet 	    lt->tm_hour, lt->tm_min);
167c2078896SGeoff Rehmet 
168c2078896SGeoff Rehmet 	putc('\n', fp);
169c2078896SGeoff Rehmet 
1703f9b28f9SPhilippe Charnier 	if (fname && !(freopen(fname, "r", stdin)))
17156e7ae90SKris Kennaway 		err(1, "can't read %s", fname);
172c2078896SGeoff Rehmet 	while (fgets(lbuf, sizeof(lbuf), stdin))
173c2078896SGeoff Rehmet 		fputs(lbuf, fp);
174c2078896SGeoff Rehmet 	rewind(fp);
175c2078896SGeoff Rehmet 
1763f9b28f9SPhilippe Charnier 	if (fstat(fd, &sbuf))
17756e7ae90SKris Kennaway 		err(1, "can't stat temporary file");
178c2078896SGeoff Rehmet 	mbufsize = sbuf.st_size;
1793f9b28f9SPhilippe Charnier 	if (!(mbuf = malloc((u_int)mbufsize)))
18056e7ae90SKris Kennaway 		err(1, "out of memory");
1813f9b28f9SPhilippe Charnier 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
18256e7ae90SKris Kennaway 		err(1, "can't read temporary file");
183c2078896SGeoff Rehmet 	(void)close(fd);
184c2078896SGeoff Rehmet }
185