Annotation of src/usr.bin/rwall/rwall.c, Revision 1.9
1.9 ! avsm 1: /* $OpenBSD: rwall.c,v 1.8 2003/06/10 22:20:50 deraadt 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: #ifndef lint
34: char copyright[] =
35: "@(#) Copyright (c) 1988 Regents of the University of California.\n\
36: All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: /*static char sccsid[] = "from: @(#)wall.c 5.14 (Berkeley) 3/2/91";*/
1.9 ! avsm 41: static char rcsid[] = "$OpenBSD: rwall.c,v 1.8 2003/06/10 22:20:50 deraadt Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: /*
45: * This program is not related to David Wall, whose Stanford Ph.D. thesis
46: * is entitled "Mechanisms for Broadcast and Selective Broadcast".
47: */
48:
49: #include <stdio.h>
50: #include <stdlib.h>
51: #include <string.h>
52: #include <time.h>
53: #include <sys/param.h>
54: #include <sys/types.h>
55: #include <sys/stat.h>
56: #include <pwd.h>
57: #include <unistd.h>
58: #include <paths.h>
59:
60: #include <rpc/rpc.h>
61: #include <rpcsvc/rwall.h>
62:
63: struct timeval timeout = { 25, 0 };
64: int mbufsize;
65: char *mbuf;
66:
67: void makemsg ();
68:
69: int
1.8 deraadt 70: main(int argc, char *argv[])
1.1 deraadt 71: {
72: char *wallhost, res;
73: CLIENT *cl;
74:
75: if ((argc < 2) || (argc > 3)) {
76: fprintf(stderr, "usage: %s hostname [file]\n", argv[0]);
77: exit(1);
78: }
79:
80: wallhost = argv[1];
81:
82: makemsg(argv[2]);
83:
84: /*
85: * Create client "handle" used for calling MESSAGEPROG on the
86: * server designated on the command line. We tell the rpc package
87: * to use the "tcp" protocol when contacting the server.
88: */
89: cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
90: if (cl == NULL) {
91: /*
92: * Couldn't establish connection with server.
93: * Print error message and die.
94: */
95: clnt_pcreateerror(wallhost);
96: exit(1);
97: }
98:
99: if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void, &res, timeout) != RPC_SUCCESS) {
100: /*
101: * An error occurred while calling the server.
102: * Print error message and die.
103: */
104: clnt_perror(cl, wallhost);
105: exit(1);
106: }
107:
108: exit(0);
109: }
110:
111: void
1.8 deraadt 112: makemsg(char *fname)
1.1 deraadt 113: {
114: struct tm *lt;
115: struct passwd *pw;
116: struct stat sbuf;
117: time_t now;
118: FILE *fp;
119: int fd;
1.6 deraadt 120: char *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[MAXPATHLEN];
1.1 deraadt 121:
1.9 ! avsm 122: snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXXXXXX", _PATH_TMP);
1.5 deraadt 123: if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) {
1.1 deraadt 124: (void)fprintf(stderr, "wall: can't open temporary file.\n");
125: exit(1);
126: }
127: (void)unlink(tmpname);
128:
129: if (!(whom = getlogin()))
130: whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
131: (void)gethostname(hostname, sizeof(hostname));
132: (void)time(&now);
133: lt = localtime(&now);
134:
135: /*
136: * all this stuff is to blank out a square for the message;
137: * we wrap message lines at column 79, not 80, because some
138: * terminals wrap after 79, some do not, and we can't tell.
139: * Which means that we may leave a non-blank character
140: * in column 80, but that can't be helped.
141: */
142: (void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
143: whom, hostname);
144: (void)fprintf(fp, " (%s) at %d:%02d ...\n", ttyname(2),
145: lt->tm_hour, lt->tm_min);
146:
147: putc('\n', fp);
148:
149: if (fname && !(freopen(fname, "r", stdin))) {
150: (void)fprintf(stderr, "wall: can't read %s.\n", fname);
151: exit(1);
152: }
153: while (fgets(lbuf, sizeof(lbuf), stdin))
154: fputs(lbuf, fp);
155: rewind(fp);
156:
157: if (fstat(fd, &sbuf)) {
158: (void)fprintf(stderr, "wall: can't stat temporary file.\n");
159: exit(1);
160: }
161: mbufsize = sbuf.st_size;
162: if (!(mbuf = malloc((u_int)mbufsize))) {
163: (void)fprintf(stderr, "wall: out of memory.\n");
164: exit(1);
165: }
166: if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
167: (void)fprintf(stderr, "wall: can't read temporary file.\n");
168: exit(1);
169: }
170: (void)close(fd);
171: }