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

Annotation of src/usr.bin/rlogin/kcmd.c, Revision 1.5

1.5     ! millert     1: /*     $OpenBSD: kcmd.c,v 1.4 1996/08/18 18:21:24 tholo Exp $  */
1.1       deraadt     2: /*     $NetBSD: kcmd.c,v 1.2 1995/03/21 07:58:32 cgd Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1993
                      6:  *     The Regents of the University of California.  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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char Xsccsid[] = "derived from @(#)rcmd.c 5.17 (Berkeley) 6/27/88";
                     40: static char sccsid[] = "@(#)kcmd.c     8.2 (Berkeley) 8/19/93";
                     41: #else
1.5     ! millert    42: static char rcsid[] = "$OpenBSD: kcmd.c,v 1.4 1996/08/18 18:21:24 tholo Exp $";
1.1       deraadt    43: #endif
                     44: #endif /* not lint */
                     45:
                     46: #include <sys/param.h>
                     47: #include <sys/file.h>
                     48: #include <sys/socket.h>
                     49: #include <sys/stat.h>
                     50:
                     51: #include <netinet/in.h>
                     52: #include <arpa/inet.h>
                     53:
                     54: #include <kerberosIV/des.h>
                     55: #include <kerberosIV/krb.h>
                     56: #include <kerberosIV/kparse.h>
                     57:
                     58: #include <ctype.h>
                     59: #include <errno.h>
                     60: #include <netdb.h>
                     61: #include <pwd.h>
                     62: #include <signal.h>
                     63: #include <stdio.h>
                     64: #include <stdlib.h>
                     65: #include <string.h>
                     66: #include <unistd.h>
                     67:
                     68: #ifndef MAXHOSTNAMELEN
                     69: #define MAXHOSTNAMELEN 64
                     70: #endif
                     71:
                     72: #define        START_PORT      5120     /* arbitrary */
                     73:
                     74: int
                     75: kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm,
                     76:     cred, schedule, msg_data, laddr, faddr, authopts)
                     77:        int *sock;
                     78:        char **ahost;
                     79:        u_short rport;
                     80:        char *locuser, *remuser, *cmd;
                     81:        int *fd2p;
                     82:        KTEXT ticket;
                     83:        char *service;
                     84:        char *realm;
                     85:        CREDENTIALS *cred;
                     86:        Key_schedule schedule;
                     87:        MSG_DAT *msg_data;
                     88:        struct sockaddr_in *laddr, *faddr;
                     89:        long authopts;
                     90: {
                     91:        int s, timo = 1, pid;
                     92:        long oldmask;
                     93:        struct sockaddr_in sin, from;
                     94:        char c;
                     95: #ifdef ATHENA_COMPAT
                     96:        int lport = IPPORT_RESERVED - 1;
                     97: #else
                     98:        int lport = START_PORT;
                     99: #endif
                    100:        struct hostent *hp;
                    101:        int rc;
                    102:        char *host_save;
                    103:        int status;
                    104:
                    105:        pid = getpid();
                    106:        hp = gethostbyname(*ahost);
                    107:        if (hp == NULL) {
1.4       tholo     108:                herror(*ahost);
1.1       deraadt   109:                return (-1);
                    110:        }
1.5     ! millert   111:        if ((host_save = strdup(hp->h_name)) == NULL) {
        !           112:                warn("can't allocate memory");
        !           113:                return (-1);
        !           114:        }
1.1       deraadt   115:        *ahost = host_save;
                    116:
                    117:        /* If realm is null, look up from table */
                    118:        if (realm == NULL || realm[0] == '\0')
                    119:                realm = krb_realmofhost(host_save);
                    120:
                    121:        oldmask = sigblock(sigmask(SIGURG));
                    122:        for (;;) {
1.4       tholo     123:                s = rresvport(&lport);
1.1       deraadt   124:                if (s < 0) {
                    125:                        if (errno == EAGAIN)
                    126:                                fprintf(stderr,
                    127:                                        "kcmd(socket): All ports in use\n");
                    128:                        else
                    129:                                perror("kcmd: socket");
                    130:                        sigsetmask(oldmask);
                    131:                        return (-1);
                    132:                }
                    133:                fcntl(s, F_SETOWN, pid);
1.4       tholo     134:                bzero(&sin, sizeof sin);
                    135:                sin.sin_len = sizeof(struct sockaddr_in);
1.1       deraadt   136:                sin.sin_family = hp->h_addrtype;
                    137:                sin.sin_port = rport;
1.4       tholo     138:                bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
1.1       deraadt   139:                if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
                    140:                        break;
                    141:                (void) close(s);
                    142:                if (errno == EADDRINUSE) {
                    143:                        lport--;
                    144:                        continue;
                    145:                }
                    146:                /*
                    147:                 * don't wait very long for Kerberos rcmd.
                    148:                 */
                    149:                if (errno == ECONNREFUSED && timo <= 4) {
                    150:                        /* sleep(timo); don't wait at all here */
                    151:                        timo *= 2;
                    152:                        continue;
                    153:                }
                    154:                if (hp->h_addr_list[1] != NULL) {
                    155:                        int oerrno = errno;
                    156:
                    157:                        fprintf(stderr,
                    158:                            "kcmd: connect to address %s: ",
                    159:                            inet_ntoa(sin.sin_addr));
                    160:                        errno = oerrno;
                    161:                        perror(NULL);
                    162:                        hp->h_addr_list++;
1.4       tholo     163:                        bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
1.1       deraadt   164:                        fprintf(stderr, "Trying %s...\n",
                    165:                                inet_ntoa(sin.sin_addr));
                    166:                        continue;
                    167:                }
                    168:                if (errno != ECONNREFUSED)
                    169:                        perror(hp->h_name);
                    170:                sigsetmask(oldmask);
                    171:                return (-1);
                    172:        }
                    173:        if (fd2p == 0) {
                    174:                write(s, "", 1);
                    175:                lport = 0;
                    176:        } else {
                    177:                char num[8];
1.4       tholo     178:                int s2 = rresvport(&lport), s3;
1.1       deraadt   179:                int len = sizeof(from);
                    180:
                    181:                if (s2 < 0) {
                    182:                        status = -1;
                    183:                        goto bad;
                    184:                }
                    185:                listen(s2, 1);
1.4       tholo     186:                (void) snprintf(num, sizeof(num), "%d", lport);
1.1       deraadt   187:                if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
                    188:                        perror("kcmd(write): setting up stderr");
                    189:                        (void) close(s2);
                    190:                        status = -1;
                    191:                        goto bad;
                    192:                }
1.4       tholo     193: again:
1.1       deraadt   194:                s3 = accept(s2, (struct sockaddr *)&from, &len);
1.4       tholo     195:                 s3 = accept(s2, (struct sockaddr *)&from, &len);
                    196:                /*
                    197:                 * XXX careful for ftp bounce attacks. If discovered, shut them
                    198:                 * down and check for the real auxiliary channel to connect.
                    199:                 */
                    200:                if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
                    201:                        (void) close(s3);
                    202:                        goto again;
                    203:                }
1.1       deraadt   204:                (void) close(s2);
                    205:                if (s3 < 0) {
                    206:                        perror("kcmd:accept");
                    207:                        lport = 0;
                    208:                        status = -1;
                    209:                        goto bad;
                    210:                }
                    211:                *fd2p = s3;
1.4       tholo     212:                from.sin_port = ntohs(from.sin_port);
1.1       deraadt   213:                if (from.sin_family != AF_INET ||
1.4       tholo     214:                    from.sin_port >= IPPORT_RESERVED ||
                    215:                    from.sin_port < IPPORT_RESERVED / 2) {
1.1       deraadt   216:                        fprintf(stderr,
                    217:                         "kcmd(socket): protocol failure in circuit setup.\n");
                    218:                        status = -1;
                    219:                        goto bad2;
                    220:                }
                    221:        }
                    222:        /*
                    223:         * Kerberos-authenticated service.  Don't have to send locuser,
                    224:         * since its already in the ticket, and we'll extract it on
                    225:         * the other side.
                    226:         */
                    227:        /* (void) write(s, locuser, strlen(locuser)+1); */
                    228:
                    229:        /* set up the needed stuff for mutual auth, but only if necessary */
                    230:        if (authopts & KOPT_DO_MUTUAL) {
                    231:                int sin_len;
                    232:                *faddr = sin;
                    233:
                    234:                sin_len = sizeof(struct sockaddr_in);
                    235:                if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
                    236:                        perror("kcmd(getsockname)");
                    237:                        status = -1;
                    238:                        goto bad2;
                    239:                }
                    240:        }
                    241:        if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
                    242:                               realm, (unsigned long) getpid(), msg_data,
                    243:                               cred, schedule,
                    244:                               laddr,
                    245:                               faddr,
                    246:                               "KCMDV0.1")) != KSUCCESS)
                    247:                goto bad2;
                    248:
                    249:        (void) write(s, remuser, strlen(remuser)+1);
                    250:        (void) write(s, cmd, strlen(cmd)+1);
                    251:
                    252:        if ((rc = read(s, &c, 1)) != 1) {
                    253:                if (rc == -1)
                    254:                        perror(*ahost);
                    255:                else
                    256:                        fprintf(stderr,"kcmd: bad connection with remote host\n");
                    257:                status = -1;
                    258:                goto bad2;
                    259:        }
                    260:        if (c != '\0') {
                    261:                while (read(s, &c, 1) == 1) {
                    262:                        (void) write(2, &c, 1);
                    263:                        if (c == '\n')
                    264:                                break;
                    265:                }
                    266:                status = -1;
                    267:                goto bad2;
                    268:        }
                    269:        sigsetmask(oldmask);
                    270:        *sock = s;
                    271:        return (KSUCCESS);
                    272: bad2:
                    273:        if (lport)
                    274:                (void) close(*fd2p);
                    275: bad:
                    276:        (void) close(s);
                    277:        sigsetmask(oldmask);
                    278:        return (status);
                    279: }