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

Annotation of src/usr.bin/rsh/kcmd.c, Revision 1.1

1.1     ! deraadt     1: /*     $OpenBSD: kcmd.c,v 1.15 2002/02/17 19:42:31 millert Exp $       */
        !             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
        !            42: static char rcsid[] = "$OpenBSD: kcmd.c,v 1.15 2002/02/17 19:42:31 millert Exp $";
        !            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 <des.h>
        !            55: #include <kerberosIV/krb.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 <err.h>
        !            68:
        !            69: #ifndef MAXHOSTNAMELEN
        !            70: #define MAXHOSTNAMELEN 64
        !            71: #endif
        !            72:
        !            73: #define        START_PORT      5120     /* arbitrary */
        !            74:
        !            75: int    getport(int *);
        !            76: int    kcmd(int *, char **, u_short, char *, char *, char *,
        !            77:            int *, KTEXT, char *, char *, CREDENTIALS *,
        !            78:            Key_schedule, MSG_DAT *, struct sockaddr_in *,
        !            79:            struct sockaddr_in *, long);
        !            80:
        !            81: int
        !            82: kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm,
        !            83:     cred, schedule, msg_data, laddr, faddr, authopts)
        !            84:        int *sock;
        !            85:        char **ahost;
        !            86:        u_short rport;
        !            87:        char *locuser, *remuser, *cmd;
        !            88:        int *fd2p;
        !            89:        KTEXT ticket;
        !            90:        char *service;
        !            91:        char *realm;
        !            92:        CREDENTIALS *cred;
        !            93:        Key_schedule schedule;
        !            94:        MSG_DAT *msg_data;
        !            95:        struct sockaddr_in *laddr, *faddr;
        !            96:        long authopts;
        !            97: {
        !            98:        int s, timo = 1, pid;
        !            99:        sigset_t mask, oldmask;
        !           100:        struct sockaddr_in sin, from;
        !           101:        char c;
        !           102:        int lport = IPPORT_RESERVED - 1;
        !           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:                herror(*ahost);
        !           112:                return (-1);
        !           113:        }
        !           114:        if ((host_save = strdup(hp->h_name)) == NULL) {
        !           115:                warn("can't allocate memory");
        !           116:                return (-1);
        !           117:        }
        !           118:        *ahost = host_save;
        !           119:
        !           120:        /* If realm is null, look up from table */
        !           121:        if (realm == NULL || realm[0] == '\0')
        !           122:                realm = krb_realmofhost(host_save);
        !           123:
        !           124:        sigemptyset(&mask);
        !           125:        sigaddset(&mask, SIGURG);
        !           126:        sigprocmask(SIG_BLOCK, &mask, &oldmask);
        !           127:        for (;;) {
        !           128:                s = getport(&lport);
        !           129:                if (s < 0) {
        !           130:                        if (errno == EAGAIN)
        !           131:                                fprintf(stderr,
        !           132:                                        "kcmd(socket): All ports in use\n");
        !           133:                        else
        !           134:                                perror("kcmd: socket");
        !           135:                        sigprocmask(SIG_SETMASK, &oldmask, NULL);
        !           136:                        return (-1);
        !           137:                }
        !           138:                fcntl(s, F_SETOWN, pid);
        !           139:                bzero(&sin, sizeof sin);
        !           140:                sin.sin_len = sizeof(struct sockaddr_in);
        !           141:                sin.sin_family = hp->h_addrtype;
        !           142:                sin.sin_port = rport;
        !           143:                bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
        !           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 (hp->h_addr_list[1] != NULL) {
        !           160:                        int oerrno = errno;
        !           161:
        !           162:                        fprintf(stderr,
        !           163:                            "kcmd: connect to address %s: ",
        !           164:                            inet_ntoa(sin.sin_addr));
        !           165:                        errno = oerrno;
        !           166:                        perror(NULL);
        !           167:                        hp->h_addr_list++;
        !           168:                        bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
        !           169:                        fprintf(stderr, "Trying %s...\n",
        !           170:                                inet_ntoa(sin.sin_addr));
        !           171:                        continue;
        !           172:                }
        !           173:                if (errno != ECONNREFUSED)
        !           174:                        perror(hp->h_name);
        !           175:                sigprocmask(SIG_SETMASK, &oldmask, NULL);
        !           176:                return (-1);
        !           177:        }
        !           178:        if (fd2p == 0) {
        !           179:                write(s, "", 1);
        !           180:                lport = 0;
        !           181:        } else {
        !           182:                char num[8];
        !           183:                int s2 = getport(&lport), s3;
        !           184:                int len = sizeof(from);
        !           185:
        !           186:                if (s2 < 0) {
        !           187:                        status = -1;
        !           188:                        goto bad;
        !           189:                }
        !           190:                listen(s2, 1);
        !           191:                (void) snprintf(num, sizeof(num), "%d", lport);
        !           192:                if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
        !           193:                        perror("kcmd(write): setting up stderr");
        !           194:                        (void) close(s2);
        !           195:                        status = -1;
        !           196:                        goto bad;
        !           197:                }
        !           198: again:
        !           199:                 s3 = accept(s2, (struct sockaddr *)&from, &len);
        !           200:                /*
        !           201:                 * XXX careful for ftp bounce attacks. If discovered, shut them
        !           202:                 * down and check for the real auxiliary channel to connect.
        !           203:                 */
        !           204:                if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
        !           205:                        (void) close(s3);
        !           206:                        goto again;
        !           207:                }
        !           208:                (void) close(s2);
        !           209:                if (s3 < 0) {
        !           210:                        perror("kcmd:accept");
        !           211:                        lport = 0;
        !           212:                        status = -1;
        !           213:                        goto bad;
        !           214:                }
        !           215:                *fd2p = s3;
        !           216:                from.sin_port = ntohs(from.sin_port);
        !           217:                if (from.sin_family != AF_INET ||
        !           218:                    from.sin_port >= IPPORT_RESERVED ||
        !           219:                    from.sin_port < IPPORT_RESERVED / 2) {
        !           220:                        fprintf(stderr,
        !           221:                         "kcmd(socket): protocol failure in circuit setup.\n");
        !           222:                        status = -1;
        !           223:                        goto bad2;
        !           224:                }
        !           225:        }
        !           226:        /*
        !           227:         * Kerberos-authenticated service.  Don't have to send locuser,
        !           228:         * since its already in the ticket, and we'll extract it on
        !           229:         * the other side.
        !           230:         */
        !           231:        /* (void) write(s, locuser, strlen(locuser)+1); */
        !           232:
        !           233:        /* set up the needed stuff for mutual auth, but only if necessary */
        !           234:        if (authopts & KOPT_DO_MUTUAL) {
        !           235:                int sin_len;
        !           236:                *faddr = sin;
        !           237:
        !           238:                sin_len = sizeof(struct sockaddr_in);
        !           239:                if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
        !           240:                        perror("kcmd(getsockname)");
        !           241:                        status = -1;
        !           242:                        goto bad2;
        !           243:                }
        !           244:        }
        !           245:        if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
        !           246:                               realm, (unsigned long) getpid(), msg_data,
        !           247:                               cred, schedule,
        !           248:                               laddr,
        !           249:                               faddr,
        !           250:                               "KCMDV0.1")) != KSUCCESS)
        !           251:                goto bad2;
        !           252:
        !           253:        (void) write(s, remuser, strlen(remuser)+1);
        !           254:        (void) write(s, cmd, strlen(cmd)+1);
        !           255:
        !           256:        if ((rc = read(s, &c, 1)) != 1) {
        !           257:                if (rc == -1)
        !           258:                        perror(*ahost);
        !           259:                else
        !           260:                        fprintf(stderr,"kcmd: bad connection with remote host\n");
        !           261:                status = -1;
        !           262:                goto bad2;
        !           263:        }
        !           264:        if (c != '\0') {
        !           265:                while (read(s, &c, 1) == 1) {
        !           266:                        (void) write(2, &c, 1);
        !           267:                        if (c == '\n')
        !           268:                                break;
        !           269:                }
        !           270:                status = -1;
        !           271:                goto bad2;
        !           272:        }
        !           273:        sigprocmask(SIG_SETMASK, &oldmask, NULL);
        !           274:        *sock = s;
        !           275:        return (KSUCCESS);
        !           276: bad2:
        !           277:        if (lport)
        !           278:                (void) close(*fd2p);
        !           279: bad:
        !           280:        (void) close(s);
        !           281:        sigprocmask(SIG_SETMASK, &oldmask, NULL);
        !           282:        return (status);
        !           283: }
        !           284:
        !           285: int
        !           286: getport(alport)
        !           287:        int *alport;
        !           288: {
        !           289:        struct sockaddr_in sin;
        !           290:        int s;
        !           291:
        !           292:        /* First try to get a "reserved" [sic] port, for interoperability with
        !           293:           broken klogind (aix, e.g.) */
        !           294:
        !           295:        s = rresvport(alport);
        !           296:        if (s >= 0)
        !           297:                return s;
        !           298:
        !           299:        /* Failed; if EACCES, we're not root, so just get an unreserved port
        !           300:           and hope that's good enough */
        !           301:
        !           302:        if (errno != EACCES)
        !           303:                return -1;
        !           304:
        !           305:        if (*alport < IPPORT_RESERVED)
        !           306:                *alport = START_PORT;
        !           307:        sin.sin_family = AF_INET;
        !           308:        sin.sin_addr.s_addr = INADDR_ANY;
        !           309:        s = socket(AF_INET, SOCK_STREAM, 0);
        !           310:        if (s < 0)
        !           311:                return (-1);
        !           312:        for (;;) {
        !           313:                sin.sin_port = htons((u_short)*alport);
        !           314:                if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
        !           315:                        return (s);
        !           316:                if (errno != EADDRINUSE) {
        !           317:                        (void) close(s);
        !           318:                        return (-1);
        !           319:                }
        !           320:                (*alport)--;
        !           321:                if (*alport == IPPORT_RESERVED) {
        !           322:                        (void) close(s);
        !           323:                        errno = EAGAIN;         /* close */
        !           324:                        return (-1);
        !           325:                }
        !           326:        }
        !           327: }