Annotation of src/usr.bin/rwall/rwall.c, Revision 1.12
1.12 ! deraadt 1: /* $OpenBSD: rwall.c,v 1.11 2008/07/09 19:41:56 sobrado Exp $ */
1.2 deraadt 2:
1.1 deraadt 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.
1.7 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
34: * This program is not related to David Wall, whose Stanford Ph.D. thesis
35: * is entitled "Mechanisms for Broadcast and Selective Broadcast".
36: */
37:
38: #include <stdio.h>
39: #include <stdlib.h>
40: #include <string.h>
41: #include <time.h>
42: #include <sys/param.h>
43: #include <sys/types.h>
44: #include <sys/stat.h>
45: #include <pwd.h>
46: #include <unistd.h>
47: #include <paths.h>
1.10 mickey 48: #include <err.h>
1.1 deraadt 49:
50: #include <rpc/rpc.h>
51: #include <rpcsvc/rwall.h>
52:
53: struct timeval timeout = { 25, 0 };
54: int mbufsize;
55: char *mbuf;
56:
1.10 mickey 57: void makemsg(char *);
1.1 deraadt 58:
59: int
1.8 deraadt 60: main(int argc, char *argv[])
1.1 deraadt 61: {
1.10 mickey 62: extern char *__progname;
1.1 deraadt 63: char *wallhost, res;
64: CLIENT *cl;
65:
66: if ((argc < 2) || (argc > 3)) {
1.11 sobrado 67: fprintf(stderr, "usage: %s host [file]\n", __progname);
1.1 deraadt 68: exit(1);
69: }
70:
71: wallhost = argv[1];
72:
73: makemsg(argv[2]);
74:
75: /*
76: * Create client "handle" used for calling MESSAGEPROG on the
77: * server designated on the command line. We tell the rpc package
78: * to use the "tcp" protocol when contacting the server.
79: */
80: cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
81: if (cl == NULL) {
82: /*
83: * Couldn't establish connection with server.
84: * Print error message and die.
85: */
86: clnt_pcreateerror(wallhost);
87: exit(1);
88: }
89:
1.10 mickey 90: if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void,
91: &res, timeout) != RPC_SUCCESS) {
1.1 deraadt 92: /*
1.10 mickey 93: * An error occurred while calling the server.
1.1 deraadt 94: * Print error message and die.
95: */
96: clnt_perror(cl, wallhost);
97: exit(1);
98: }
99:
100: exit(0);
101: }
102:
103: void
1.8 deraadt 104: makemsg(char *fname)
1.1 deraadt 105: {
106: struct tm *lt;
107: struct passwd *pw;
108: struct stat sbuf;
109: time_t now;
110: FILE *fp;
111: int fd;
1.6 deraadt 112: char *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[MAXPATHLEN];
1.1 deraadt 113:
1.10 mickey 114: snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXXXXXX", _PATH_TMP);
115: if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
116: err(1, "can't open temporary file");
1.1 deraadt 117: (void)unlink(tmpname);
118:
119: if (!(whom = getlogin()))
120: whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
121: (void)gethostname(hostname, sizeof(hostname));
122: (void)time(&now);
123: lt = localtime(&now);
124:
125: /*
126: * all this stuff is to blank out a square for the message;
127: * we wrap message lines at column 79, not 80, because some
128: * terminals wrap after 79, some do not, and we can't tell.
129: * Which means that we may leave a non-blank character
130: * in column 80, but that can't be helped.
131: */
132: (void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
133: whom, hostname);
134: (void)fprintf(fp, " (%s) at %d:%02d ...\n", ttyname(2),
135: lt->tm_hour, lt->tm_min);
136:
137: putc('\n', fp);
138:
1.10 mickey 139: if (fname && !(freopen(fname, "r", stdin)))
140: err(1, "%s", fname);
1.1 deraadt 141: while (fgets(lbuf, sizeof(lbuf), stdin))
142: fputs(lbuf, fp);
143: rewind(fp);
144:
1.10 mickey 145: if (fstat(fd, &sbuf))
146: err(1, "can't stat temporary file");
1.1 deraadt 147: mbufsize = sbuf.st_size;
1.10 mickey 148: if (!(mbuf = malloc((u_int)mbufsize)))
149: err(1, "malloc");
150: if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
151: err(1, "can't read temporary file");
1.1 deraadt 152: (void)close(fd);
153: }