rwall.c revision 2347
155714Skris/*
255714Skris * Copyright (c) 1993 Christopher G. Demetriou
355714Skris * Copyright (c) 1988, 1990 Regents of the University of California.
455714Skris * All rights reserved.
555714Skris *
655714Skris * Redistribution and use in source and binary forms, with or without
755714Skris * modification, are permitted provided that the following conditions
8280297Sjkim * are met:
955714Skris * 1. Redistributions of source code must retain the above copyright
1055714Skris *    notice, this list of conditions and the following disclaimer.
1155714Skris * 2. Redistributions in binary form must reproduce the above copyright
1255714Skris *    notice, this list of conditions and the following disclaimer in the
1355714Skris *    documentation and/or other materials provided with the distribution.
1455714Skris * 3. All advertising materials mentioning features or use of this software
15280297Sjkim *    must display the following acknowledgement:
1655714Skris *	This product includes software developed by the University of
1755714Skris *	California, Berkeley and its contributors.
1855714Skris * 4. Neither the name of the University nor the names of its contributors
1955714Skris *    may be used to endorse or promote products derived from this software
2055714Skris *    without specific prior written permission.
2155714Skris *
22280297Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2355714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2455714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2555714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2655714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2755714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2855714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2955714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3055714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3155714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3255714Skris * SUCH DAMAGE.
3355714Skris */
3455714Skris
3555714Skris#ifndef lint
3655714Skrischar copyright[] =
37280297Sjkim"@(#) Copyright (c) 1988 Regents of the University of California.\n\
3855714Skris All rights reserved.\n";
3955714Skris#endif /* not lint */
40280297Sjkim
4155714Skris#ifndef lint
4255714Skris/*static char sccsid[] = "from: @(#)wall.c	5.14 (Berkeley) 3/2/91";*/
4355714Skrisstatic char rcsid[] = "$Id: rwall.c,v 1.1 1993/09/16 01:11:04 jtc Exp $";
4455714Skris#endif /* not lint */
4555714Skris
4655714Skris/*
4755714Skris * This program is not related to David Wall, whose Stanford Ph.D. thesis
4855714Skris * is entitled "Mechanisms for Broadcast and Selective Broadcast".
4955714Skris */
5055714Skris
5155714Skris#include <sys/param.h>
52280297Sjkim#include <sys/stat.h>
5355714Skris#include <sys/time.h>
5455714Skris#include <sys/uio.h>
5555714Skris#include <utmp.h>
5655714Skris#include <pwd.h>
5755714Skris#include <stdio.h>
5855714Skris#include <stdlib.h>
5955714Skris#include <paths.h>
6055714Skris
6155714Skris#include <rpc/rpc.h>
62160814Ssimon#include <rpcsvc/rwall.h>
63280297Sjkim
64280297Sjkimint mbufsize;
65280297Sjkimchar *mbuf;
6655714Skris
67280297Sjkim/* ARGSUSED */
68280297Sjkimmain(argc, argv)
69280297Sjkim	int argc;
70194206Ssimon	char **argv;
71160814Ssimon{
72160814Ssimon	char *wallhost, res;
7355714Skris	CLIENT *cl;
74280297Sjkim
75280297Sjkim	if ((argc < 2) || (argc > 3)) {
76280297Sjkim		fprintf(stderr, "usage: %s hostname [file]\n", argv[0]);
77280297Sjkim		exit(1);
78280297Sjkim	}
79280297Sjkim
80280297Sjkim	wallhost = argv[1];
81280297Sjkim
82280297Sjkim	makemsg(argv[2]);
83280297Sjkim
84280297Sjkim	/*
85280297Sjkim	 * Create client "handle" used for calling MESSAGEPROG on the
86280297Sjkim	 * server designated on the command line. We tell the rpc package
87280297Sjkim	 * to use the "tcp" protocol when contacting the server.
88280297Sjkim	*/
89280297Sjkim	cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
90160814Ssimon	if (cl == NULL) {
91160814Ssimon		/*
92280297Sjkim		 * Couldn't establish connection with server.
93280297Sjkim		 * Print error message and die.
94280297Sjkim		 */
95280297Sjkim		clnt_pcreateerror(wallhost);
9655714Skris		exit(1);
97280297Sjkim	}
98280297Sjkim
9955714Skris	if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void, &res, NULL) != RPC_SUCCESS) {
100280297Sjkim		/*
101280297Sjkim		 * An error occurred while calling the server.
102280297Sjkim		 * Print error message and die.
103280297Sjkim		 */
104280297Sjkim		clnt_perror(cl, wallhost);
10555714Skris		exit(1);
106280297Sjkim	}
107280297Sjkim
108280297Sjkim	exit(0);
109280297Sjkim}
11055714Skris
111280297Sjkimmakemsg(fname)
112280297Sjkim	char *fname;
113280297Sjkim{
114280297Sjkim	register int ch, cnt;
115280297Sjkim	struct tm *lt;
11655714Skris	struct passwd *pw;
117280297Sjkim	struct stat sbuf;
118280297Sjkim	time_t now, time();
119280297Sjkim	FILE *fp;
12055714Skris	int fd;
121280297Sjkim	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
122280297Sjkim	char *getlogin(), *strcpy(), *ttyname();
123280297Sjkim
124280297Sjkim	(void)strcpy(tmpname, _PATH_TMP);
125280297Sjkim	(void)strcat(tmpname, "/wall.XXXXXX");
126280297Sjkim	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
127160814Ssimon		(void)fprintf(stderr, "wall: can't open temporary file.\n");
128280297Sjkim		exit(1);
129280297Sjkim	}
130280297Sjkim	(void)unlink(tmpname);
13155714Skris
132280297Sjkim	if (!(whom = getlogin()))
133280297Sjkim		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
134280297Sjkim	(void)gethostname(hostname, sizeof(hostname));
135280297Sjkim	(void)time(&now);
136280297Sjkim	lt = localtime(&now);
137280297Sjkim
138280297Sjkim	/*
139280297Sjkim	 * all this stuff is to blank out a square for the message;
140280297Sjkim	 * we wrap message lines at column 79, not 80, because some
141280297Sjkim	 * terminals wrap after 79, some do not, and we can't tell.
142280297Sjkim	 * Which means that we may leave a non-blank character
143280297Sjkim	 * in column 80, but that can't be helped.
144280297Sjkim	 */
14555714Skris	(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