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

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