xref: /freebsd/usr.bin/rwall/rwall.c (revision c207889672ffe85eeb6dde64ab1a26cf3d62a6b8)
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";*/
43c2078896SGeoff Rehmet static char rcsid[] = "$Id: rwall.c,v 1.1 1993/09/16 01:11:04 jtc 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;
74c2078896SGeoff Rehmet 
75c2078896SGeoff Rehmet 	if ((argc < 2) || (argc > 3)) {
76c2078896SGeoff Rehmet 		fprintf(stderr, "usage: %s hostname [file]\n", argv[0]);
77c2078896SGeoff Rehmet 		exit(1);
78c2078896SGeoff Rehmet 	}
79c2078896SGeoff Rehmet 
80c2078896SGeoff Rehmet 	wallhost = argv[1];
81c2078896SGeoff Rehmet 
82c2078896SGeoff Rehmet 	makemsg(argv[2]);
83c2078896SGeoff Rehmet 
84c2078896SGeoff Rehmet 	/*
85c2078896SGeoff Rehmet 	 * Create client "handle" used for calling MESSAGEPROG on the
86c2078896SGeoff Rehmet 	 * server designated on the command line. We tell the rpc package
87c2078896SGeoff Rehmet 	 * to use the "tcp" protocol when contacting the server.
88c2078896SGeoff Rehmet 	*/
89c2078896SGeoff Rehmet 	cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
90c2078896SGeoff Rehmet 	if (cl == NULL) {
91c2078896SGeoff Rehmet 		/*
92c2078896SGeoff Rehmet 		 * Couldn't establish connection with server.
93c2078896SGeoff Rehmet 		 * Print error message and die.
94c2078896SGeoff Rehmet 		 */
95c2078896SGeoff Rehmet 		clnt_pcreateerror(wallhost);
96c2078896SGeoff Rehmet 		exit(1);
97c2078896SGeoff Rehmet 	}
98c2078896SGeoff Rehmet 
99c2078896SGeoff Rehmet 	if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void, &res, NULL) != RPC_SUCCESS) {
100c2078896SGeoff Rehmet 		/*
101c2078896SGeoff Rehmet 		 * An error occurred while calling the server.
102c2078896SGeoff Rehmet 		 * Print error message and die.
103c2078896SGeoff Rehmet 		 */
104c2078896SGeoff Rehmet 		clnt_perror(cl, wallhost);
105c2078896SGeoff Rehmet 		exit(1);
106c2078896SGeoff Rehmet 	}
107c2078896SGeoff Rehmet 
108c2078896SGeoff Rehmet 	exit(0);
109c2078896SGeoff Rehmet }
110c2078896SGeoff Rehmet 
111c2078896SGeoff Rehmet makemsg(fname)
112c2078896SGeoff Rehmet 	char *fname;
113c2078896SGeoff Rehmet {
114c2078896SGeoff Rehmet 	register int ch, cnt;
115c2078896SGeoff Rehmet 	struct tm *lt;
116c2078896SGeoff Rehmet 	struct passwd *pw;
117c2078896SGeoff Rehmet 	struct stat sbuf;
118c2078896SGeoff Rehmet 	time_t now, time();
119c2078896SGeoff Rehmet 	FILE *fp;
120c2078896SGeoff Rehmet 	int fd;
121c2078896SGeoff Rehmet 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
122c2078896SGeoff Rehmet 	char *getlogin(), *strcpy(), *ttyname();
123c2078896SGeoff Rehmet 
124c2078896SGeoff Rehmet 	(void)strcpy(tmpname, _PATH_TMP);
125c2078896SGeoff Rehmet 	(void)strcat(tmpname, "/wall.XXXXXX");
126c2078896SGeoff Rehmet 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
127c2078896SGeoff Rehmet 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
128c2078896SGeoff Rehmet 		exit(1);
129c2078896SGeoff Rehmet 	}
130c2078896SGeoff Rehmet 	(void)unlink(tmpname);
131c2078896SGeoff Rehmet 
132c2078896SGeoff Rehmet 	if (!(whom = getlogin()))
133c2078896SGeoff Rehmet 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
134c2078896SGeoff Rehmet 	(void)gethostname(hostname, sizeof(hostname));
135c2078896SGeoff Rehmet 	(void)time(&now);
136c2078896SGeoff Rehmet 	lt = localtime(&now);
137c2078896SGeoff Rehmet 
138c2078896SGeoff Rehmet 	/*
139c2078896SGeoff Rehmet 	 * all this stuff is to blank out a square for the message;
140c2078896SGeoff Rehmet 	 * we wrap message lines at column 79, not 80, because some
141c2078896SGeoff Rehmet 	 * terminals wrap after 79, some do not, and we can't tell.
142c2078896SGeoff Rehmet 	 * Which means that we may leave a non-blank character
143c2078896SGeoff Rehmet 	 * in column 80, but that can't be helped.
144c2078896SGeoff Rehmet 	 */
145c2078896SGeoff Rehmet 	(void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
146c2078896SGeoff Rehmet 	    whom, hostname);
147c2078896SGeoff Rehmet 	(void)fprintf(fp, "        (%s) at %d:%02d ...\n", ttyname(2),
148c2078896SGeoff Rehmet 	    lt->tm_hour, lt->tm_min);
149c2078896SGeoff Rehmet 
150c2078896SGeoff Rehmet 	putc('\n', fp);
151c2078896SGeoff Rehmet 
152c2078896SGeoff Rehmet 	if (fname && !(freopen(fname, "r", stdin))) {
153c2078896SGeoff Rehmet 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
154c2078896SGeoff Rehmet 		exit(1);
155c2078896SGeoff Rehmet 	}
156c2078896SGeoff Rehmet 	while (fgets(lbuf, sizeof(lbuf), stdin))
157c2078896SGeoff Rehmet 		fputs(lbuf, fp);
158c2078896SGeoff Rehmet 	rewind(fp);
159c2078896SGeoff Rehmet 
160c2078896SGeoff Rehmet 	if (fstat(fd, &sbuf)) {
161c2078896SGeoff Rehmet 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
162c2078896SGeoff Rehmet 		exit(1);
163c2078896SGeoff Rehmet 	}
164c2078896SGeoff Rehmet 	mbufsize = sbuf.st_size;
165c2078896SGeoff Rehmet 	if (!(mbuf = malloc((u_int)mbufsize))) {
166c2078896SGeoff Rehmet 		(void)fprintf(stderr, "wall: out of memory.\n");
167c2078896SGeoff Rehmet 		exit(1);
168c2078896SGeoff Rehmet 	}
169c2078896SGeoff Rehmet 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
170c2078896SGeoff Rehmet 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
171c2078896SGeoff Rehmet 		exit(1);
172c2078896SGeoff Rehmet 	}
173c2078896SGeoff Rehmet 	(void)close(fd);
174c2078896SGeoff Rehmet }
175