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