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