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