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