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