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