rwall.c revision 91643
1/*
2 * Copyright (c) 1993 Christopher G. Demetriou
3 * Copyright (c) 1988, 1990 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by the University of
17 *	California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36
37__FBSDID("$FreeBSD: head/usr.bin/rwall/rwall.c 91643 2002-03-04 20:27:38Z markm $");
38
39#ifndef lint
40static const char copyright[] =
41"@(#) Copyright (c) 1988 Regents of the University of California.\n\
42 All rights reserved.\n";
43#endif /* not lint */
44
45#ifndef lint
46static const char sccsid[] = "from: @(#)wall.c	5.14 (Berkeley) 3/2/91";
47#endif
48
49/*
50 * This program is not related to David Wall, whose Stanford Ph.D. thesis
51 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
52 */
53
54#include <sys/param.h>
55#include <sys/stat.h>
56#include <sys/uio.h>
57#include <rpc/rpc.h>
58#include <rpcsvc/rwall.h>
59#include <err.h>
60#include <paths.h>
61#include <pwd.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <time.h>
66#include <unistd.h>
67
68char *mbuf;
69
70static char notty[] = "no tty";
71
72void	makemsg(const char *);
73static void usage(void);
74
75/* ARGSUSED */
76int
77main(int argc, char *argv[])
78{
79	char *wallhost, res;
80	CLIENT *cl;
81	struct timeval tv;
82
83	if ((argc < 2) || (argc > 3))
84		usage();
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		errx(1, "%s", clnt_spcreateerror(wallhost));
102	}
103
104	tv.tv_sec = 15;		/* XXX ?? */
105	tv.tv_usec = 0;
106	if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void,
107	    &res, tv) != RPC_SUCCESS) {
108		/*
109		 * An error occurred while calling the server.
110		 * Print error message and die.
111		 */
112		errx(1, "%s", clnt_sperror(cl, wallhost));
113	}
114
115	return (0);
116}
117
118static void
119usage(void)
120{
121	fprintf(stderr, "usage: rwall hostname [file]\n");
122	exit(1);
123}
124
125void
126makemsg(const char *fname)
127{
128	struct tm *lt;
129	struct passwd *pw;
130	struct stat sbuf;
131	time_t now;
132	FILE *fp;
133	int fd;
134	size_t mbufsize;
135	char *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
136	const char *whom;
137
138	snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
139	fd = mkstemp(tmpname);
140	fp = fdopen(fd, "r+");
141	if (fd == -1 || !fp)
142		err(1, "can't open temporary file");
143	unlink(tmpname);
144
145	whom = getlogin();
146	if (!whom) {
147		pw = getpwuid(getuid());
148		whom = pw ? pw->pw_name : "???";
149	}
150	gethostname(hostname, sizeof(hostname));
151	time(&now);
152	lt = localtime(&now);
153
154	/*
155	 * all this stuff is to blank out a square for the message;
156	 * we wrap message lines at column 79, not 80, because some
157	 * terminals wrap after 79, some do not, and we can't tell.
158	 * Which means that we may leave a non-blank character
159	 * in column 80, but that can't be helped.
160	 */
161	fprintf(fp, "Remote Broadcast Message from %s@%s\n",
162	    whom, hostname);
163	tty = ttyname(STDERR_FILENO);
164	if (tty == NULL)
165		tty = notty;
166	fprintf(fp, "        (%s) at %d:%02d ...\n", tty,
167	    lt->tm_hour, lt->tm_min);
168
169	putc('\n', fp);
170
171	if (fname && !(freopen(fname, "r", stdin)))
172		err(1, "can't read %s", fname);
173	while (fgets(lbuf, sizeof(lbuf), stdin))
174		fputs(lbuf, fp);
175	rewind(fp);
176
177	if (fstat(fd, &sbuf))
178		err(1, "can't stat temporary file");
179	mbufsize = (size_t)sbuf.st_size;
180	mbuf = malloc(mbufsize);
181	if (mbuf == NULL)
182		err(1, "out of memory");
183	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
184		err(1, "can't read temporary file");
185	close(fd);
186}
187