rwall.c revision 1.12 1 /* $NetBSD: rwall.c,v 1.12 2000/09/04 15:21:24 taca Exp $ */
2
3 /*
4 * Copyright (c) 1993 Christopher G. Demetriou
5 * Copyright (c) 1988, 1990 Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT("@(#) Copyright (c) 1988 Regents of the University of California.\n\
40 All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "from: @(#)wall.c 5.14 (Berkeley) 3/2/91";
46 #else
47 __RCSID("$NetBSD: rwall.c,v 1.12 2000/09/04 15:21:24 taca Exp $");
48 #endif
49 #endif /* not lint */
50
51 /*
52 * This program is not related to David Wall, whose Stanford Ph.D. thesis
53 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
54 */
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/stat.h>
58 #include <err.h>
59 #include <paths.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <time.h>
65 #include <unistd.h>
66
67 #include <rpc/rpc.h>
68 #include <rpcsvc/rwall.h>
69
70 struct timeval timeout = { 25, 0 };
71 int mbufsize;
72 char *mbuf;
73
74 int main __P((int, char **));
75 void makemsg __P((char *));
76
77 int
78 main(argc, argv)
79 int argc;
80 char **argv;
81 {
82 char *wallhost, res;
83 CLIENT *cl;
84
85 if ((argc < 2) || (argc > 3)) {
86 fprintf(stderr, "usage: %s hostname [file]\n", argv[0]);
87 exit(1);
88 }
89
90 wallhost = argv[1];
91
92 makemsg(argv[2]);
93
94 /*
95 * Create client "handle" used for calling MESSAGEPROG on the
96 * server designated on the command line. We tell the rpc package
97 * to use the "tcp" protocol when contacting the server.
98 */
99 cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
100 if (cl == NULL) {
101 /*
102 * Couldn't establish connection with server.
103 * Print error message and die.
104 */
105 clnt_pcreateerror(wallhost);
106 exit(1);
107 }
108
109 if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, (caddr_t)&mbuf, xdr_void, &res, timeout) != RPC_SUCCESS) {
110 /*
111 * An error occurred while calling the server.
112 * Print error message and die.
113 */
114 clnt_perror(cl, wallhost);
115 exit(1);
116 }
117
118 exit(0);
119 }
120
121 void
122 makemsg(fname)
123 char *fname;
124 {
125 struct tm *lt;
126 struct passwd *pw;
127 struct stat sbuf;
128 time_t now;
129 FILE *fp;
130 int fd;
131 const char *whom;
132 char *tty, hostname[MAXHOSTNAMELEN + 1], lbuf[100], tmpname[32];
133
134 (void)strcpy(tmpname, _PATH_TMP);
135 (void)strcat(tmpname, "/wall.XXXXXX");
136 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
137 err(1, "can't open temporary file.");
138 (void)unlink(tmpname);
139
140 if (!(whom = getlogin()))
141 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
142 (void)gethostname(hostname, sizeof(hostname));
143 hostname[sizeof(hostname) - 1] = '\0';
144 (void)time(&now);
145 lt = localtime(&now);
146
147 /*
148 * all this stuff is to blank out a square for the message;
149 * we wrap message lines at column 79, not 80, because some
150 * terminals wrap after 79, some do not, and we can't tell.
151 * Which means that we may leave a non-blank character
152 * in column 80, but that can't be helped.
153 */
154 (void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
155 whom, hostname);
156 tty = ttyname(STDERR_FILENO);
157 if (tty == NULL)
158 (void)fprintf(fp, " at %d:%02d ...\n",
159 lt->tm_hour, lt->tm_min);
160 else
161 (void)fprintf(fp, " (%s) at %d:%02d ...\n", tty,
162 lt->tm_hour, lt->tm_min);
163
164 putc('\n', fp);
165
166 if (fname && !(freopen(fname, "r", stdin)))
167 err(1, "can't read %s.", fname);
168 while (fgets(lbuf, sizeof(lbuf), stdin))
169 fputs(lbuf, fp);
170 rewind(fp);
171
172 if (fstat(fd, &sbuf))
173 err(1, "can't stat temporary file.");
174 mbufsize = sbuf.st_size;
175 if (!(mbuf = malloc((u_int)mbufsize)))
176 err(1, "malloc");
177 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
178 err(1, "can't read temporary file.");
179 (void)close(fd);
180 }
181