[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.3

1.3     ! deraadt     1: /*     $OpenBSD: kcmd.c,v 1.2 1995/03/21 07:58:32 cgd 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.3     ! deraadt    42: static char rcsid[] = "$OpenBSD: kcmd.c,v 1.2 1995/03/21 07:58:32 cgd 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    getport __P((int *));
                     75:
                     76: int
                     77: kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm,
                     78:     cred, schedule, msg_data, laddr, faddr, authopts)
                     79:        int *sock;
                     80:        char **ahost;
                     81:        u_short rport;
                     82:        char *locuser, *remuser, *cmd;
                     83:        int *fd2p;
                     84:        KTEXT ticket;
                     85:        char *service;
                     86:        char *realm;
                     87:        CREDENTIALS *cred;
                     88:        Key_schedule schedule;
                     89:        MSG_DAT *msg_data;
                     90:        struct sockaddr_in *laddr, *faddr;
                     91:        long authopts;
                     92: {
                     93:        int s, timo = 1, pid;
                     94:        long oldmask;
                     95:        struct sockaddr_in sin, from;
                     96:        char c;
                     97: #ifdef ATHENA_COMPAT
                     98:        int lport = IPPORT_RESERVED - 1;
                     99: #else
                    100:        int lport = START_PORT;
                    101: #endif
                    102:        struct hostent *hp;
                    103:        int rc;
                    104:        char *host_save;
                    105:        int status;
                    106:
                    107:        pid = getpid();
                    108:        hp = gethostbyname(*ahost);
                    109:        if (hp == NULL) {
                    110:                /* fprintf(stderr, "%s: unknown host\n", *ahost); */
                    111:                return (-1);
                    112:        }
                    113:
                    114:        host_save = malloc(strlen(hp->h_name) + 1);
                    115:        strcpy(host_save, hp->h_name);
                    116:        *ahost = host_save;
                    117:
                    118: #ifdef KERBEROS
                    119:        /* If realm is null, look up from table */
                    120:        if (realm == NULL || realm[0] == '\0')
                    121:                realm = krb_realmofhost(host_save);
                    122: #endif /* KERBEROS */
                    123:
                    124:        oldmask = sigblock(sigmask(SIGURG));
                    125:        for (;;) {
                    126:                s = getport(&lport);
                    127:                if (s < 0) {
                    128:                        if (errno == EAGAIN)
                    129:                                fprintf(stderr,
                    130:                                        "kcmd(socket): All ports in use\n");
                    131:                        else
                    132:                                perror("kcmd: socket");
                    133:                        sigsetmask(oldmask);
                    134:                        return (-1);
                    135:                }
                    136:                fcntl(s, F_SETOWN, pid);
                    137:                sin.sin_family = hp->h_addrtype;
                    138: #if defined(ultrix) || defined(sun)
                    139:                bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
                    140: #else
                    141:                bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length);
                    142: #endif
                    143:                sin.sin_port = rport;
                    144:                if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
                    145:                        break;
                    146:                (void) close(s);
                    147:                if (errno == EADDRINUSE) {
                    148:                        lport--;
                    149:                        continue;
                    150:                }
                    151:                /*
                    152:                 * don't wait very long for Kerberos rcmd.
                    153:                 */
                    154:                if (errno == ECONNREFUSED && timo <= 4) {
                    155:                        /* sleep(timo); don't wait at all here */
                    156:                        timo *= 2;
                    157:                        continue;
                    158:                }
                    159: #if !(defined(ultrix) || defined(sun))
                    160:                if (hp->h_addr_list[1] != NULL) {
                    161:                        int oerrno = errno;
                    162:
                    163:                        fprintf(stderr,
                    164:                            "kcmd: connect to address %s: ",
                    165:                            inet_ntoa(sin.sin_addr));
                    166:                        errno = oerrno;
                    167:                        perror(NULL);
                    168:                        hp->h_addr_list++;
                    169:                        bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr,
                    170:                            hp->h_length);
                    171:                        fprintf(stderr, "Trying %s...\n",
                    172:                                inet_ntoa(sin.sin_addr));
                    173:                        continue;
                    174:                }
                    175: #endif /* !(defined(ultrix) || defined(sun)) */
                    176:                if (errno != ECONNREFUSED)
                    177:                        perror(hp->h_name);
                    178:                sigsetmask(oldmask);
                    179:                return (-1);
                    180:        }
                    181:        lport--;
                    182:        if (fd2p == 0) {
                    183:                write(s, "", 1);
                    184:                lport = 0;
                    185:        } else {
                    186:                char num[8];
                    187:                int s2 = getport(&lport), s3;
                    188:                int len = sizeof(from);
                    189:
                    190:                if (s2 < 0) {
                    191:                        status = -1;
                    192:                        goto bad;
                    193:                }
                    194:                listen(s2, 1);
                    195:                (void) sprintf(num, "%d", lport);
                    196:                if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
                    197:                        perror("kcmd(write): setting up stderr");
                    198:                        (void) close(s2);
                    199:                        status = -1;
                    200:                        goto bad;
                    201:                }
                    202:                s3 = accept(s2, (struct sockaddr *)&from, &len);
                    203:                (void) close(s2);
                    204:                if (s3 < 0) {
                    205:                        perror("kcmd:accept");
                    206:                        lport = 0;
                    207:                        status = -1;
                    208:                        goto bad;
                    209:                }
                    210:                *fd2p = s3;
                    211:                from.sin_port = ntohs((u_short)from.sin_port);
                    212:                if (from.sin_family != AF_INET ||
                    213:                    from.sin_port >= IPPORT_RESERVED) {
                    214:                        fprintf(stderr,
                    215:                         "kcmd(socket): protocol failure in circuit setup.\n");
                    216:                        status = -1;
                    217:                        goto bad2;
                    218:                }
                    219:        }
                    220:        /*
                    221:         * Kerberos-authenticated service.  Don't have to send locuser,
                    222:         * since its already in the ticket, and we'll extract it on
                    223:         * the other side.
                    224:         */
                    225:        /* (void) write(s, locuser, strlen(locuser)+1); */
                    226:
                    227:        /* set up the needed stuff for mutual auth, but only if necessary */
                    228:        if (authopts & KOPT_DO_MUTUAL) {
                    229:                int sin_len;
                    230:                *faddr = sin;
                    231:
                    232:                sin_len = sizeof(struct sockaddr_in);
                    233:                if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
                    234:                        perror("kcmd(getsockname)");
                    235:                        status = -1;
                    236:                        goto bad2;
                    237:                }
                    238:        }
                    239: #ifdef KERBEROS
                    240:        if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
                    241:                               realm, (unsigned long) getpid(), msg_data,
                    242:                               cred, schedule,
                    243:                               laddr,
                    244:                               faddr,
                    245:                               "KCMDV0.1")) != KSUCCESS)
                    246:                goto bad2;
                    247: #endif /* KERBEROS */
                    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: }
                    280:
                    281: int
                    282: getport(alport)
                    283:        int *alport;
                    284: {
                    285:        struct sockaddr_in sin;
                    286:        int s;
                    287:
                    288:        sin.sin_family = AF_INET;
                    289:        sin.sin_addr.s_addr = INADDR_ANY;
                    290:        s = socket(AF_INET, SOCK_STREAM, 0);
                    291:        if (s < 0)
                    292:                return (-1);
                    293:        for (;;) {
                    294:                sin.sin_port = htons((u_short)*alport);
                    295:                if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
                    296:                        return (s);
                    297:                if (errno != EADDRINUSE) {
                    298:                        (void) close(s);
                    299:                        return (-1);
                    300:                }
                    301:                (*alport)--;
                    302: #ifdef ATHENA_COMPAT
                    303:                if (*alport == IPPORT_RESERVED/2) {
                    304: #else
                    305:                if (*alport == IPPORT_RESERVED) {
                    306: #endif
                    307:                        (void) close(s);
                    308:                        errno = EAGAIN;         /* close */
                    309:                        return (-1);
                    310:                }
                    311:        }
                    312: }