[BACK]Return to rwall.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rwall

Annotation of src/usr.bin/rwall/rwall.c, Revision 1.10

1.10    ! mickey      1: /*     $OpenBSD: rwall.c,v 1.9 2003/07/02 00:21:16 avsm 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.10    ! mickey     41: static char rcsid[] = "$OpenBSD: rwall.c,v 1.9 2003/07/02 00:21:16 avsm 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>
1.10    ! mickey     59: #include <err.h>
1.1       deraadt    60:
                     61: #include <rpc/rpc.h>
                     62: #include <rpcsvc/rwall.h>
                     63:
                     64: struct timeval timeout = { 25, 0 };
                     65: int mbufsize;
                     66: char *mbuf;
                     67:
1.10    ! mickey     68: void makemsg(char *);
1.1       deraadt    69:
                     70: int
1.8       deraadt    71: main(int argc, char *argv[])
1.1       deraadt    72: {
1.10    ! mickey     73:        extern char *__progname;
1.1       deraadt    74:        char *wallhost, res;
                     75:        CLIENT *cl;
                     76:
                     77:        if ((argc < 2) || (argc > 3)) {
1.10    ! mickey     78:                fprintf(stderr, "usage: %s hostname [file]\n", __progname);
1.1       deraadt    79:                exit(1);
                     80:        }
                     81:
                     82:        wallhost = argv[1];
                     83:
                     84:        makemsg(argv[2]);
                     85:
                     86:        /*
                     87:         * Create client "handle" used for calling MESSAGEPROG on the
                     88:         * server designated on the command line. We tell the rpc package
                     89:         * to use the "tcp" protocol when contacting the server.
                     90:        */
                     91:        cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
                     92:        if (cl == NULL) {
                     93:                /*
                     94:                 * Couldn't establish connection with server.
                     95:                 * Print error message and die.
                     96:                 */
                     97:                clnt_pcreateerror(wallhost);
                     98:                exit(1);
                     99:        }
                    100:
1.10    ! mickey    101:        if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void,
        !           102:            &res, timeout) != RPC_SUCCESS) {
1.1       deraadt   103:                /*
1.10    ! mickey    104:                 * An error occurred while calling the server.
1.1       deraadt   105:                 * Print error message and die.
                    106:                 */
                    107:                clnt_perror(cl, wallhost);
                    108:                exit(1);
                    109:        }
                    110:
                    111:        exit(0);
                    112: }
                    113:
                    114: void
1.8       deraadt   115: makemsg(char *fname)
1.1       deraadt   116: {
                    117:        struct tm *lt;
                    118:        struct passwd *pw;
                    119:        struct stat sbuf;
                    120:        time_t now;
                    121:        FILE *fp;
                    122:        int fd;
1.6       deraadt   123:        char *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[MAXPATHLEN];
1.1       deraadt   124:
1.10    ! mickey    125:        snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXXXXXX", _PATH_TMP);
        !           126:        if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
        !           127:                err(1, "can't open temporary file");
1.1       deraadt   128:        (void)unlink(tmpname);
                    129:
                    130:        if (!(whom = getlogin()))
                    131:                whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
                    132:        (void)gethostname(hostname, sizeof(hostname));
                    133:        (void)time(&now);
                    134:        lt = localtime(&now);
                    135:
                    136:        /*
                    137:         * all this stuff is to blank out a square for the message;
                    138:         * we wrap message lines at column 79, not 80, because some
                    139:         * terminals wrap after 79, some do not, and we can't tell.
                    140:         * Which means that we may leave a non-blank character
                    141:         * in column 80, but that can't be helped.
                    142:         */
                    143:        (void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
                    144:            whom, hostname);
                    145:        (void)fprintf(fp, "        (%s) at %d:%02d ...\n", ttyname(2),
                    146:            lt->tm_hour, lt->tm_min);
                    147:
                    148:        putc('\n', fp);
                    149:
1.10    ! mickey    150:        if (fname && !(freopen(fname, "r", stdin)))
        !           151:                err(1, "%s", fname);
1.1       deraadt   152:        while (fgets(lbuf, sizeof(lbuf), stdin))
                    153:                fputs(lbuf, fp);
                    154:        rewind(fp);
                    155:
1.10    ! mickey    156:        if (fstat(fd, &sbuf))
        !           157:                err(1, "can't stat temporary file");
1.1       deraadt   158:        mbufsize = sbuf.st_size;
1.10    ! mickey    159:        if (!(mbuf = malloc((u_int)mbufsize)))
        !           160:                err(1, "malloc");
        !           161:        if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
        !           162:                err(1, "can't read temporary file");
1.1       deraadt   163:        (void)close(fd);
                    164: }