1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni *
4c2078896SGeoff Rehmet * Copyright (c) 1993 Christopher G. Demetriou
5c2078896SGeoff Rehmet * Copyright (c) 1988, 1990 Regents of the University of California.
6c2078896SGeoff Rehmet * All rights reserved.
7c2078896SGeoff Rehmet *
8c2078896SGeoff Rehmet * Redistribution and use in source and binary forms, with or without
9c2078896SGeoff Rehmet * modification, are permitted provided that the following conditions
10c2078896SGeoff Rehmet * are met:
11c2078896SGeoff Rehmet * 1. Redistributions of source code must retain the above copyright
12c2078896SGeoff Rehmet * notice, this list of conditions and the following disclaimer.
13c2078896SGeoff Rehmet * 2. Redistributions in binary form must reproduce the above copyright
14c2078896SGeoff Rehmet * notice, this list of conditions and the following disclaimer in the
15c2078896SGeoff Rehmet * documentation and/or other materials provided with the distribution.
16c2078896SGeoff Rehmet * 3. All advertising materials mentioning features or use of this software
17c2078896SGeoff Rehmet * must display the following acknowledgement:
18c2078896SGeoff Rehmet * This product includes software developed by the University of
19c2078896SGeoff Rehmet * California, Berkeley and its contributors.
20c2078896SGeoff Rehmet * 4. Neither the name of the University nor the names of its contributors
21c2078896SGeoff Rehmet * may be used to endorse or promote products derived from this software
22c2078896SGeoff Rehmet * without specific prior written permission.
23c2078896SGeoff Rehmet *
24c2078896SGeoff Rehmet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25c2078896SGeoff Rehmet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26c2078896SGeoff Rehmet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27c2078896SGeoff Rehmet * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28c2078896SGeoff Rehmet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29c2078896SGeoff Rehmet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30c2078896SGeoff Rehmet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31c2078896SGeoff Rehmet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32c2078896SGeoff Rehmet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33c2078896SGeoff Rehmet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34c2078896SGeoff Rehmet * SUCH DAMAGE.
35c2078896SGeoff Rehmet */
36c2078896SGeoff Rehmet
37c2078896SGeoff Rehmet /*
38c2078896SGeoff Rehmet * This program is not related to David Wall, whose Stanford Ph.D. thesis
39c2078896SGeoff Rehmet * is entitled "Mechanisms for Broadcast and Selective Broadcast".
40c2078896SGeoff Rehmet */
41c2078896SGeoff Rehmet
42c2078896SGeoff Rehmet #include <sys/param.h>
43c2078896SGeoff Rehmet #include <sys/stat.h>
44c2078896SGeoff Rehmet #include <sys/uio.h>
4520bee7c8SMark Murray #include <rpc/rpc.h>
4620bee7c8SMark Murray #include <rpcsvc/rwall.h>
473f9b28f9SPhilippe Charnier #include <err.h>
483f9b28f9SPhilippe Charnier #include <paths.h>
49c2078896SGeoff Rehmet #include <pwd.h>
50c2078896SGeoff Rehmet #include <stdio.h>
51c2078896SGeoff Rehmet #include <stdlib.h>
52821df508SXin LI #include <string.h>
5356e7ae90SKris Kennaway #include <time.h>
543f9b28f9SPhilippe Charnier #include <unistd.h>
55c2078896SGeoff Rehmet
5631dd0180SEd Schouten static char *mbuf;
57c2078896SGeoff Rehmet
5820bee7c8SMark Murray static char notty[] = "no tty";
5920bee7c8SMark Murray
6031dd0180SEd Schouten static void makemsg(const char *);
61*1a7ac2bdSAlfonso Gregory static void usage(void) __dead2;
623f9b28f9SPhilippe Charnier
63c2078896SGeoff Rehmet /* ARGSUSED */
643f9b28f9SPhilippe Charnier int
main(int argc,char * argv[])6520bee7c8SMark Murray main(int argc, char *argv[])
66c2078896SGeoff Rehmet {
67c2078896SGeoff Rehmet char *wallhost, res;
68c2078896SGeoff Rehmet CLIENT *cl;
6942f84d83SPeter Wemm struct timeval tv;
70c2078896SGeoff Rehmet
713f9b28f9SPhilippe Charnier if ((argc < 2) || (argc > 3))
723f9b28f9SPhilippe Charnier usage();
73c2078896SGeoff Rehmet
74c2078896SGeoff Rehmet wallhost = argv[1];
75c2078896SGeoff Rehmet
76c2078896SGeoff Rehmet makemsg(argv[2]);
77c2078896SGeoff Rehmet
78c2078896SGeoff Rehmet /*
79c2078896SGeoff Rehmet * Create client "handle" used for calling MESSAGEPROG on the
80c2078896SGeoff Rehmet * server designated on the command line. We tell the rpc package
81c2078896SGeoff Rehmet * to use the "tcp" protocol when contacting the server.
82c2078896SGeoff Rehmet */
83c2078896SGeoff Rehmet cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
84c2078896SGeoff Rehmet if (cl == NULL) {
85c2078896SGeoff Rehmet /*
86c2078896SGeoff Rehmet * Couldn't establish connection with server.
87c2078896SGeoff Rehmet * Print error message and die.
88c2078896SGeoff Rehmet */
893f9b28f9SPhilippe Charnier errx(1, "%s", clnt_spcreateerror(wallhost));
90c2078896SGeoff Rehmet }
91c2078896SGeoff Rehmet
9242f84d83SPeter Wemm tv.tv_sec = 15; /* XXX ?? */
9342f84d83SPeter Wemm tv.tv_usec = 0;
94ed838bb1SPeter Wemm if (clnt_call(cl, WALLPROC_WALL, (xdrproc_t)xdr_wrapstring, &mbuf,
95ed838bb1SPeter Wemm (xdrproc_t)xdr_void, &res, tv) != RPC_SUCCESS) {
96c2078896SGeoff Rehmet /*
97c2078896SGeoff Rehmet * An error occurred while calling the server.
98c2078896SGeoff Rehmet * Print error message and die.
99c2078896SGeoff Rehmet */
1003f9b28f9SPhilippe Charnier errx(1, "%s", clnt_sperror(cl, wallhost));
101c2078896SGeoff Rehmet }
102c2078896SGeoff Rehmet
10320bee7c8SMark Murray return (0);
104c2078896SGeoff Rehmet }
105c2078896SGeoff Rehmet
1063f9b28f9SPhilippe Charnier static void
usage(void)10720bee7c8SMark Murray usage(void)
1083f9b28f9SPhilippe Charnier {
109f682f10cSRuslan Ermilov fprintf(stderr, "usage: rwall host [file]\n");
1103f9b28f9SPhilippe Charnier exit(1);
1113f9b28f9SPhilippe Charnier }
1123f9b28f9SPhilippe Charnier
11331dd0180SEd Schouten static void
makemsg(const char * fname)11420bee7c8SMark Murray makemsg(const char *fname)
115c2078896SGeoff Rehmet {
116c2078896SGeoff Rehmet struct tm *lt;
117c2078896SGeoff Rehmet struct passwd *pw;
118c2078896SGeoff Rehmet struct stat sbuf;
11956e7ae90SKris Kennaway time_t now;
120c2078896SGeoff Rehmet FILE *fp;
121c2078896SGeoff Rehmet int fd;
12220bee7c8SMark Murray size_t mbufsize;
12356e7ae90SKris Kennaway char *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
12456e7ae90SKris Kennaway const char *whom;
125c2078896SGeoff Rehmet
12620bee7c8SMark Murray snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
127c8ad87deSMark Murray if ((fd = mkstemp(tmpname)) == -1 || (fp = fdopen(fd, "r+")) == NULL)
12856e7ae90SKris Kennaway err(1, "can't open temporary file");
12920bee7c8SMark Murray unlink(tmpname);
130c2078896SGeoff Rehmet
13120bee7c8SMark Murray whom = getlogin();
13220bee7c8SMark Murray if (!whom) {
13320bee7c8SMark Murray pw = getpwuid(getuid());
13420bee7c8SMark Murray whom = pw ? pw->pw_name : "???";
13520bee7c8SMark Murray }
13620bee7c8SMark Murray gethostname(hostname, sizeof(hostname));
13720bee7c8SMark Murray time(&now);
138c2078896SGeoff Rehmet lt = localtime(&now);
139c2078896SGeoff Rehmet
140c2078896SGeoff Rehmet /*
141c2078896SGeoff Rehmet * all this stuff is to blank out a square for the message;
142c2078896SGeoff Rehmet * we wrap message lines at column 79, not 80, because some
143c2078896SGeoff Rehmet * terminals wrap after 79, some do not, and we can't tell.
144c2078896SGeoff Rehmet * Which means that we may leave a non-blank character
145c2078896SGeoff Rehmet * in column 80, but that can't be helped.
146c2078896SGeoff Rehmet */
14720bee7c8SMark Murray fprintf(fp, "Remote Broadcast Message from %s@%s\n",
148c2078896SGeoff Rehmet whom, hostname);
14956e7ae90SKris Kennaway tty = ttyname(STDERR_FILENO);
15056e7ae90SKris Kennaway if (tty == NULL)
15120bee7c8SMark Murray tty = notty;
15220bee7c8SMark Murray fprintf(fp, " (%s) at %d:%02d ...\n", tty,
153c2078896SGeoff Rehmet lt->tm_hour, lt->tm_min);
154c2078896SGeoff Rehmet
155c2078896SGeoff Rehmet putc('\n', fp);
156c2078896SGeoff Rehmet
1573f9b28f9SPhilippe Charnier if (fname && !(freopen(fname, "r", stdin)))
15856e7ae90SKris Kennaway err(1, "can't read %s", fname);
159c2078896SGeoff Rehmet while (fgets(lbuf, sizeof(lbuf), stdin))
160c2078896SGeoff Rehmet fputs(lbuf, fp);
161c2078896SGeoff Rehmet rewind(fp);
162c2078896SGeoff Rehmet
1633f9b28f9SPhilippe Charnier if (fstat(fd, &sbuf))
16456e7ae90SKris Kennaway err(1, "can't stat temporary file");
16520bee7c8SMark Murray mbufsize = (size_t)sbuf.st_size;
16620bee7c8SMark Murray mbuf = malloc(mbufsize);
16720bee7c8SMark Murray if (mbuf == NULL)
16856e7ae90SKris Kennaway err(1, "out of memory");
169f4ac32deSDavid Malone if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != (u_int)mbufsize)
17056e7ae90SKris Kennaway err(1, "can't read temporary file");
17120bee7c8SMark Murray close(fd);
172c2078896SGeoff Rehmet }
173