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

Annotation of src/usr.bin/ssh/ssh.c, Revision 1.208

1.1       deraadt     1: /*
1.32      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * Ssh client program.  This program can be used to log into a remote machine.
                      6:  * The software supports strong authentication, encryption, and forwarding
                      7:  * of X11, TCP/IP, and authentication connections.
                      8:  *
1.64      deraadt     9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
                     14:  *
                     15:  * Copyright (c) 1999 Niels Provos.  All rights reserved.
1.202     markus     16:  * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl.  All rights reserved.
1.64      deraadt    17:  *
                     18:  * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
                     19:  * in Canada (German citizen).
                     20:  *
                     21:  * Redistribution and use in source and binary forms, with or without
                     22:  * modification, are permitted provided that the following conditions
                     23:  * are met:
                     24:  * 1. Redistributions of source code must retain the above copyright
                     25:  *    notice, this list of conditions and the following disclaimer.
                     26:  * 2. Redistributions in binary form must reproduce the above copyright
                     27:  *    notice, this list of conditions and the following disclaimer in the
                     28:  *    documentation and/or other materials provided with the distribution.
                     29:  *
                     30:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     31:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     32:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     33:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     34:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     35:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     36:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     37:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     38:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     39:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32      deraadt    40:  */
1.1       deraadt    41:
                     42: #include "includes.h"
1.208   ! markus     43: RCSID("$OpenBSD: ssh.c,v 1.207 2004/03/09 22:11:05 markus Exp $");
1.49      markus     44:
                     45: #include <openssl/evp.h>
1.72      markus     46: #include <openssl/err.h>
1.1       deraadt    47:
1.84      markus     48: #include "ssh.h"
                     49: #include "ssh1.h"
                     50: #include "ssh2.h"
                     51: #include "compat.h"
                     52: #include "cipher.h"
1.1       deraadt    53: #include "xmalloc.h"
                     54: #include "packet.h"
                     55: #include "buffer.h"
1.123     markus     56: #include "channels.h"
1.49      markus     57: #include "key.h"
1.58      markus     58: #include "authfd.h"
1.49      markus     59: #include "authfile.h"
1.83      markus     60: #include "pathnames.h"
1.81      markus     61: #include "clientloop.h"
1.84      markus     62: #include "log.h"
                     63: #include "readconf.h"
                     64: #include "sshconnect.h"
                     65: #include "tildexpand.h"
1.89      markus     66: #include "dispatch.h"
1.84      markus     67: #include "misc.h"
1.95      markus     68: #include "kex.h"
                     69: #include "mac.h"
1.115     stevesk    70: #include "sshtty.h"
1.49      markus     71:
1.127     markus     72: #ifdef SMARTCARD
                     73: #include "scard.h"
1.137     jakob      74: #endif
1.127     markus     75:
1.49      markus     76: extern char *__progname;
1.1       deraadt    77:
1.31      markus     78: /* Flag indicating whether debug mode is on.  This can be set on the command line. */
1.1       deraadt    79: int debug_flag = 0;
                     80:
1.46      markus     81: /* Flag indicating whether a tty should be allocated */
1.1       deraadt    82: int tty_flag = 0;
1.79      markus     83: int no_tty_flag = 0;
                     84: int force_tty_flag = 0;
1.1       deraadt    85:
1.45      markus     86: /* don't exec a shell */
                     87: int no_shell_flag = 0;
                     88:
1.33      markus     89: /*
                     90:  * Flag indicating that nothing should be read from stdin.  This can be set
                     91:  * on the command line.
                     92:  */
1.1       deraadt    93: int stdin_null_flag = 0;
                     94:
1.33      markus     95: /*
                     96:  * Flag indicating that ssh should fork after authentication.  This is useful
1.172     deraadt    97:  * so that the passphrase can be entered manually, and then ssh goes to the
1.33      markus     98:  * background.
                     99:  */
1.1       deraadt   100: int fork_after_authentication_flag = 0;
                    101:
1.33      markus    102: /*
                    103:  * General data structure for command line options and options configurable
                    104:  * in configuration files.  See readconf.h.
                    105:  */
1.1       deraadt   106: Options options;
                    107:
1.139     markus    108: /* optional user configfile */
                    109: char *config = NULL;
                    110:
1.33      markus    111: /*
                    112:  * Name of the host we are connecting to.  This is the name given on the
                    113:  * command line, or the HostName specified for the user-supplied name in a
                    114:  * configuration file.
                    115:  */
1.1       deraadt   116: char *host;
                    117:
1.22      provos    118: /* socket address the host resolves to */
1.37      markus    119: struct sockaddr_storage hostaddr;
1.1       deraadt   120:
1.112     markus    121: /* Private host keys. */
1.173     markus    122: Sensitive sensitive_data;
1.1       deraadt   123:
1.10      dugsong   124: /* Original real UID. */
                    125: uid_t original_real_uid;
1.177     markus    126: uid_t original_effective_uid;
1.1       deraadt   127:
1.45      markus    128: /* command to be executed */
                    129: Buffer command;
                    130:
1.85      djm       131: /* Should we execute a command or invoke a subsystem? */
                    132: int subsystem_flag = 0;
                    133:
1.170     markus    134: /* # of replies received for global requests */
                    135: static int client_global_request_id = 0;
                    136:
1.186     djm       137: /* pid of proxycommand child process */
                    138: pid_t proxy_command_pid = 0;
                    139:
1.1       deraadt   140: /* Prints a help message to the user.  This function never returns. */
                    141:
1.126     itojun    142: static void
1.93      itojun    143: usage(void)
1.1       deraadt   144: {
1.208   ! markus    145:        fprintf(stderr,
        !           146: "usage: ssh [-1246AaCfghkNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
        !           147: "           [-D port] [-e escape_char] [-F configfile] [-i identity_file]\n"
        !           148: "           [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]\n"
        !           149: "           [-p port] [-R port:host:hostport] [user@]hostname [command]\n"
        !           150:        );
1.31      markus    151:        exit(1);
1.1       deraadt   152: }
                    153:
1.126     itojun    154: static int ssh_session(void);
                    155: static int ssh_session2(void);
                    156: static void load_public_identity_files(void);
1.45      markus    157:
1.32      deraadt   158: /*
                    159:  * Main program for the ssh client.
                    160:  */
1.2       provos    161: int
                    162: main(int ac, char **av)
1.1       deraadt   163: {
1.178     markus    164:        int i, opt, exit_status;
1.35      markus    165:        u_short fwd_port, fwd_host_port;
1.141     stevesk   166:        char sfwd_port[6], sfwd_host_port[6];
1.205     markus    167:        char *p, *cp, *line, buf[256];
1.31      markus    168:        struct stat st;
1.98      markus    169:        struct passwd *pw;
1.45      markus    170:        int dummy;
1.144     stevesk   171:        extern int optind, optreset;
                    172:        extern char *optarg;
1.31      markus    173:
1.33      markus    174:        /*
                    175:         * Save the original real uid.  It will be needed later (uid-swapping
                    176:         * may clobber the real uid).
                    177:         */
1.31      markus    178:        original_real_uid = getuid();
                    179:        original_effective_uid = geteuid();
                    180:
1.184     stevesk   181:        /*
                    182:         * Use uid-swapping to give up root privileges for the duration of
                    183:         * option processing.  We will re-instantiate the rights when we are
                    184:         * ready to create the privileged port, and will permanently drop
                    185:         * them when the port has been created (actually, when the connection
                    186:         * has been made, as we may need to create the port several times).
                    187:         */
                    188:        PRIV_END;
                    189:
1.31      markus    190:        /* If we are installed setuid root be careful to not drop core. */
                    191:        if (original_real_uid != original_effective_uid) {
                    192:                struct rlimit rlim;
                    193:                rlim.rlim_cur = rlim.rlim_max = 0;
                    194:                if (setrlimit(RLIMIT_CORE, &rlim) < 0)
                    195:                        fatal("setrlimit failed: %.100s", strerror(errno));
1.1       deraadt   196:        }
1.107     markus    197:        /* Get user data. */
                    198:        pw = getpwuid(original_real_uid);
                    199:        if (!pw) {
1.191     itojun    200:                logit("You don't exist, go away!");
1.107     markus    201:                exit(1);
                    202:        }
                    203:        /* Take a copy of the returned structure. */
                    204:        pw = pwcopy(pw);
1.31      markus    205:
1.33      markus    206:        /*
                    207:         * Set our umask to something reasonable, as some files are created
                    208:         * with the default umask.  This will make them world-readable but
                    209:         * writable only by the owner, which is ok for all files for which we
                    210:         * don't set the modes explicitly.
                    211:         */
1.31      markus    212:        umask(022);
                    213:
                    214:        /* Initialize option structure to indicate that no values have been set. */
                    215:        initialize_options(&options);
                    216:
                    217:        /* Parse command-line arguments. */
                    218:        host = NULL;
                    219:
1.128     fgsch     220: again:
                    221:        while ((opt = getopt(ac, av,
1.202     markus    222:            "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVXY")) != -1) {
1.31      markus    223:                switch (opt) {
1.91      jakob     224:                case '1':
                    225:                        options.protocol = SSH_PROTO_1;
                    226:                        break;
1.47      markus    227:                case '2':
                    228:                        options.protocol = SSH_PROTO_2;
                    229:                        break;
1.37      markus    230:                case '4':
1.196     djm       231:                        options.address_family = AF_INET;
1.37      markus    232:                        break;
                    233:                case '6':
1.196     djm       234:                        options.address_family = AF_INET6;
1.37      markus    235:                        break;
1.31      markus    236:                case 'n':
                    237:                        stdin_null_flag = 1;
                    238:                        break;
                    239:                case 'f':
                    240:                        fork_after_authentication_flag = 1;
                    241:                        stdin_null_flag = 1;
                    242:                        break;
                    243:                case 'x':
                    244:                        options.forward_x11 = 0;
                    245:                        break;
                    246:                case 'X':
                    247:                        options.forward_x11 = 1;
                    248:                        break;
1.202     markus    249:                case 'Y':
                    250:                        options.forward_x11 = 1;
                    251:                        options.forward_x11_trusted = 1;
                    252:                        break;
1.31      markus    253:                case 'g':
                    254:                        options.gateway_ports = 1;
                    255:                        break;
1.183     stevesk   256:                case 'P':       /* deprecated */
1.31      markus    257:                        options.use_privileged_port = 0;
                    258:                        break;
                    259:                case 'a':
                    260:                        options.forward_agent = 0;
1.53      markus    261:                        break;
                    262:                case 'A':
                    263:                        options.forward_agent = 1;
1.31      markus    264:                        break;
                    265:                case 'k':
1.204     dtucker   266:                        options.gss_deleg_creds = 0;
1.31      markus    267:                        break;
                    268:                case 'i':
                    269:                        if (stat(optarg, &st) < 0) {
1.128     fgsch     270:                                fprintf(stderr, "Warning: Identity file %s "
                    271:                                    "does not exist.\n", optarg);
1.31      markus    272:                                break;
                    273:                        }
1.128     fgsch     274:                        if (options.num_identity_files >=
                    275:                            SSH_MAX_IDENTITY_FILES)
                    276:                                fatal("Too many identity files specified "
                    277:                                    "(max %d)", SSH_MAX_IDENTITY_FILES);
                    278:                        options.identity_files[options.num_identity_files++] =
                    279:                            xstrdup(optarg);
1.31      markus    280:                        break;
1.127     markus    281:                case 'I':
                    282: #ifdef SMARTCARD
1.133     markus    283:                        options.smartcard_device = xstrdup(optarg);
1.137     jakob     284: #else
1.127     markus    285:                        fprintf(stderr, "no support for smartcards.\n");
1.137     jakob     286: #endif
1.127     markus    287:                        break;
1.31      markus    288:                case 't':
1.79      markus    289:                        if (tty_flag)
                    290:                                force_tty_flag = 1;
1.31      markus    291:                        tty_flag = 1;
                    292:                        break;
                    293:                case 'v':
1.197     markus    294:                        if (debug_flag == 0) {
1.66      markus    295:                                debug_flag = 1;
                    296:                                options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197     markus    297:                        } else {
                    298:                                if (options.log_level < SYSLOG_LEVEL_DEBUG3)
                    299:                                        options.log_level++;
1.66      markus    300:                                break;
1.197     markus    301:                        }
1.66      markus    302:                        /* fallthrough */
1.31      markus    303:                case 'V':
1.96      deraadt   304:                        fprintf(stderr,
1.193     djm       305:                            "%s, SSH protocols %d.%d/%d.%d, %s\n",
1.46      markus    306:                            SSH_VERSION,
                    307:                            PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
1.96      deraadt   308:                            PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2,
1.193     djm       309:                            SSLeay_version(SSLEAY_VERSION));
1.31      markus    310:                        if (opt == 'V')
                    311:                                exit(0);
                    312:                        break;
                    313:                case 'q':
                    314:                        options.log_level = SYSLOG_LEVEL_QUIET;
                    315:                        break;
                    316:                case 'e':
                    317:                        if (optarg[0] == '^' && optarg[2] == 0 &&
1.128     fgsch     318:                            (u_char) optarg[1] >= 64 &&
                    319:                            (u_char) optarg[1] < 128)
1.78      markus    320:                                options.escape_char = (u_char) optarg[1] & 31;
1.31      markus    321:                        else if (strlen(optarg) == 1)
1.78      markus    322:                                options.escape_char = (u_char) optarg[0];
1.31      markus    323:                        else if (strcmp(optarg, "none") == 0)
1.119     stevesk   324:                                options.escape_char = SSH_ESCAPECHAR_NONE;
1.31      markus    325:                        else {
1.128     fgsch     326:                                fprintf(stderr, "Bad escape character '%s'.\n",
                    327:                                    optarg);
1.31      markus    328:                                exit(1);
                    329:                        }
                    330:                        break;
                    331:                case 'c':
1.49      markus    332:                        if (ciphers_valid(optarg)) {
                    333:                                /* SSH2 only */
                    334:                                options.ciphers = xstrdup(optarg);
1.51      markus    335:                                options.cipher = SSH_CIPHER_ILLEGAL;
1.49      markus    336:                        } else {
                    337:                                /* SSH1 only */
1.74      markus    338:                                options.cipher = cipher_number(optarg);
                    339:                                if (options.cipher == -1) {
1.128     fgsch     340:                                        fprintf(stderr,
                    341:                                            "Unknown cipher type '%s'\n",
                    342:                                            optarg);
1.49      markus    343:                                        exit(1);
                    344:                                }
1.128     fgsch     345:                                if (options.cipher == SSH_CIPHER_3DES)
1.74      markus    346:                                        options.ciphers = "3des-cbc";
1.128     fgsch     347:                                else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74      markus    348:                                        options.ciphers = "blowfish-cbc";
1.128     fgsch     349:                                else
1.74      markus    350:                                        options.ciphers = (char *)-1;
1.95      markus    351:                        }
                    352:                        break;
                    353:                case 'm':
                    354:                        if (mac_valid(optarg))
                    355:                                options.macs = xstrdup(optarg);
                    356:                        else {
1.128     fgsch     357:                                fprintf(stderr, "Unknown mac type '%s'\n",
                    358:                                    optarg);
1.95      markus    359:                                exit(1);
1.31      markus    360:                        }
                    361:                        break;
                    362:                case 'p':
1.113     stevesk   363:                        options.port = a2port(optarg);
                    364:                        if (options.port == 0) {
1.109     markus    365:                                fprintf(stderr, "Bad port '%s'\n", optarg);
                    366:                                exit(1);
                    367:                        }
1.31      markus    368:                        break;
                    369:                case 'l':
                    370:                        options.user = optarg;
                    371:                        break;
1.141     stevesk   372:
                    373:                case 'L':
1.31      markus    374:                case 'R':
1.141     stevesk   375:                        if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]",
                    376:                            sfwd_port, buf, sfwd_host_port) != 3 &&
                    377:                            sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]",
                    378:                            sfwd_port, buf, sfwd_host_port) != 3) {
1.128     fgsch     379:                                fprintf(stderr,
1.141     stevesk   380:                                    "Bad forwarding specification '%s'\n",
1.128     fgsch     381:                                    optarg);
1.31      markus    382:                                usage();
                    383:                                /* NOTREACHED */
                    384:                        }
1.141     stevesk   385:                        if ((fwd_port = a2port(sfwd_port)) == 0 ||
1.165     markus    386:                            (fwd_host_port = a2port(sfwd_host_port)) == 0) {
1.128     fgsch     387:                                fprintf(stderr,
1.141     stevesk   388:                                    "Bad forwarding port(s) '%s'\n", optarg);
                    389:                                exit(1);
1.31      markus    390:                        }
1.141     stevesk   391:                        if (opt == 'L')
                    392:                                add_local_forward(&options, fwd_port, buf,
                    393:                                    fwd_host_port);
                    394:                        else if (opt == 'R')
                    395:                                add_remote_forward(&options, fwd_port, buf,
1.151     deraadt   396:                                    fwd_host_port);
1.31      markus    397:                        break;
1.108     markus    398:
                    399:                case 'D':
1.113     stevesk   400:                        fwd_port = a2port(optarg);
                    401:                        if (fwd_port == 0) {
1.128     fgsch     402:                                fprintf(stderr, "Bad dynamic port '%s'\n",
                    403:                                    optarg);
1.109     markus    404:                                exit(1);
                    405:                        }
1.200     markus    406:                        add_local_forward(&options, fwd_port, "socks", 0);
1.108     markus    407:                        break;
                    408:
1.31      markus    409:                case 'C':
                    410:                        options.compression = 1;
                    411:                        break;
1.45      markus    412:                case 'N':
                    413:                        no_shell_flag = 1;
                    414:                        no_tty_flag = 1;
                    415:                        break;
                    416:                case 'T':
                    417:                        no_tty_flag = 1;
                    418:                        break;
1.31      markus    419:                case 'o':
                    420:                        dummy = 1;
1.205     markus    421:                        line = xstrdup(optarg);
1.128     fgsch     422:                        if (process_config_line(&options, host ? host : "",
1.205     markus    423:                            line, "command-line", 0, &dummy) != 0)
1.31      markus    424:                                exit(1);
1.205     markus    425:                        xfree(line);
1.31      markus    426:                        break;
1.85      djm       427:                case 's':
                    428:                        subsystem_flag = 1;
1.117     markus    429:                        break;
                    430:                case 'b':
                    431:                        options.bind_address = optarg;
1.85      djm       432:                        break;
1.139     markus    433:                case 'F':
                    434:                        config = optarg;
                    435:                        break;
1.31      markus    436:                default:
                    437:                        usage();
1.1       deraadt   438:                }
1.31      markus    439:        }
                    440:
1.128     fgsch     441:        ac -= optind;
                    442:        av += optind;
                    443:
                    444:        if (ac > 0 && !host && **av != '-') {
1.188     markus    445:                if (strrchr(*av, '@')) {
1.128     fgsch     446:                        p = xstrdup(*av);
1.188     markus    447:                        cp = strrchr(p, '@');
1.128     fgsch     448:                        if (cp == NULL || cp == p)
                    449:                                usage();
                    450:                        options.user = p;
                    451:                        *cp = '\0';
                    452:                        host = ++cp;
                    453:                } else
                    454:                        host = *av;
1.189     millert   455:                if (ac > 1) {
                    456:                        optind = optreset = 1;
1.128     fgsch     457:                        goto again;
                    458:                }
1.189     millert   459:                ac--, av++;
1.128     fgsch     460:        }
                    461:
1.31      markus    462:        /* Check that we got a host name. */
                    463:        if (!host)
                    464:                usage();
                    465:
1.54      markus    466:        SSLeay_add_all_algorithms();
1.72      markus    467:        ERR_load_crypto_strings();
1.31      markus    468:
                    469:        /* Initialize the command to execute on remote host. */
                    470:        buffer_init(&command);
1.1       deraadt   471:
1.33      markus    472:        /*
                    473:         * Save the command to execute on the remote host in a buffer. There
                    474:         * is no limit on the length of the command, except by the maximum
                    475:         * packet size.  Also sets the tty flag if there is no command.
                    476:         */
1.128     fgsch     477:        if (!ac) {
1.31      markus    478:                /* No command specified - execute shell on a tty. */
                    479:                tty_flag = 1;
1.85      djm       480:                if (subsystem_flag) {
1.128     fgsch     481:                        fprintf(stderr,
                    482:                            "You must specify a subsystem to invoke.\n");
1.85      djm       483:                        usage();
                    484:                }
1.31      markus    485:        } else {
1.128     fgsch     486:                /* A command has been specified.  Store it into the buffer. */
                    487:                for (i = 0; i < ac; i++) {
                    488:                        if (i)
1.31      markus    489:                                buffer_append(&command, " ", 1);
                    490:                        buffer_append(&command, av[i], strlen(av[i]));
                    491:                }
                    492:        }
                    493:
                    494:        /* Cannot fork to background if no command. */
1.63      markus    495:        if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31      markus    496:                fatal("Cannot fork into background without a command to execute.");
                    497:
                    498:        /* Allocate a tty by default if no command specified. */
                    499:        if (buffer_len(&command) == 0)
                    500:                tty_flag = 1;
                    501:
1.180     deraadt   502:        /* Force no tty */
1.75      markus    503:        if (no_tty_flag)
                    504:                tty_flag = 0;
1.31      markus    505:        /* Do not allocate a tty if stdin is not a tty. */
1.79      markus    506:        if (!isatty(fileno(stdin)) && !force_tty_flag) {
1.31      markus    507:                if (tty_flag)
1.191     itojun    508:                        logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31      markus    509:                tty_flag = 0;
                    510:        }
1.45      markus    511:
1.101     markus    512:        /*
                    513:         * Initialize "log" output.  Since we are the client all output
                    514:         * actually goes to stderr.
                    515:         */
1.111     markus    516:        log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
                    517:            SYSLOG_FACILITY_USER, 1);
1.31      markus    518:
1.139     markus    519:        /*
                    520:         * Read per-user configuration file.  Ignore the system wide config
                    521:         * file if the user specifies a config file on the command line.
                    522:         */
                    523:        if (config != NULL) {
1.142     stevesk   524:                if (!read_config_file(config, host, &options))
                    525:                        fatal("Can't open user config file %.100s: "
                    526:                            "%.100s", config, strerror(errno));
1.139     markus    527:        } else  {
                    528:                snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
                    529:                    _PATH_SSH_USER_CONFFILE);
1.145     markus    530:                (void)read_config_file(buf, host, &options);
1.139     markus    531:
1.145     markus    532:                /* Read systemwide configuration file after use config. */
1.142     stevesk   533:                (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
1.139     markus    534:        }
1.31      markus    535:
                    536:        /* Fill configuration defaults. */
                    537:        fill_default_options(&options);
                    538:
1.196     djm       539:        channel_set_af(options.address_family);
                    540:
1.31      markus    541:        /* reinit */
1.101     markus    542:        log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31      markus    543:
                    544:        if (options.user == NULL)
                    545:                options.user = xstrdup(pw->pw_name);
                    546:
                    547:        if (options.hostname != NULL)
                    548:                host = options.hostname;
1.195     markus    549:
                    550:        /* force lowercase for hostkey matching */
                    551:        if (options.host_key_alias != NULL) {
                    552:                for (p = options.host_key_alias; *p; p++)
                    553:                        if (isupper(*p))
                    554:                                *p = tolower(*p);
                    555:        }
1.190     markus    556:
                    557:        if (options.proxy_command != NULL &&
                    558:            strcmp(options.proxy_command, "none") == 0)
                    559:                options.proxy_command = NULL;
1.31      markus    560:
1.77      markus    561:        /* Open a connection to the remote host. */
1.203     djm       562:        if (ssh_connect(host, &hostaddr, options.port,
1.196     djm       563:            options.address_family, options.connection_attempts,
1.177     markus    564:            original_effective_uid == 0 && options.use_privileged_port,
1.179     markus    565:            options.proxy_command) != 0)
1.178     markus    566:                exit(1);
1.31      markus    567:
1.33      markus    568:        /*
                    569:         * If we successfully made the connection, load the host private key
                    570:         * in case we will need it later for combined rsa-rhosts
                    571:         * authentication. This must be done before releasing extra
                    572:         * privileges, because the file is only readable by root.
1.174     markus    573:         * If we cannot access the private keys, load the public keys
                    574:         * instead and try to execute the ssh-keysign helper instead.
1.33      markus    575:         */
1.112     markus    576:        sensitive_data.nkeys = 0;
                    577:        sensitive_data.keys = NULL;
1.173     markus    578:        sensitive_data.external_keysign = 0;
1.178     markus    579:        if (options.rhosts_rsa_authentication ||
                    580:            options.hostbased_authentication) {
1.112     markus    581:                sensitive_data.nkeys = 3;
1.180     deraadt   582:                sensitive_data.keys = xmalloc(sensitive_data.nkeys *
                    583:                    sizeof(Key));
1.177     markus    584:
                    585:                PRIV_START;
1.112     markus    586:                sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.105     markus    587:                    _PATH_HOST_KEY_FILE, "", NULL);
1.112     markus    588:                sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
                    589:                    _PATH_HOST_DSA_KEY_FILE, "", NULL);
                    590:                sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
                    591:                    _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.177     markus    592:                PRIV_END;
1.173     markus    593:
1.181     markus    594:                if (options.hostbased_authentication == 1 &&
                    595:                    sensitive_data.keys[0] == NULL &&
1.173     markus    596:                    sensitive_data.keys[1] == NULL &&
                    597:                    sensitive_data.keys[2] == NULL) {
                    598:                        sensitive_data.keys[1] = key_load_public(
                    599:                            _PATH_HOST_DSA_KEY_FILE, NULL);
                    600:                        sensitive_data.keys[2] = key_load_public(
                    601:                            _PATH_HOST_RSA_KEY_FILE, NULL);
                    602:                        sensitive_data.external_keysign = 1;
                    603:                }
1.31      markus    604:        }
1.33      markus    605:        /*
                    606:         * Get rid of any extra privileges that we may have.  We will no
                    607:         * longer need them.  Also, extra privileges could make it very hard
                    608:         * to read identity files and other non-world-readable files from the
                    609:         * user's home directory if it happens to be on a NFS volume where
                    610:         * root is mapped to nobody.
                    611:         */
1.177     markus    612:        seteuid(original_real_uid);
                    613:        setuid(original_real_uid);
1.31      markus    614:
1.33      markus    615:        /*
                    616:         * Now that we are back to our own permissions, create ~/.ssh
                    617:         * directory if it doesn\'t already exist.
                    618:         */
1.138     jakob     619:        snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31      markus    620:        if (stat(buf, &st) < 0)
1.57      djm       621:                if (mkdir(buf, 0700) < 0)
1.31      markus    622:                        error("Could not create directory '%.200s'.", buf);
                    623:
1.104     markus    624:        /* load options.identity_files */
                    625:        load_public_identity_files();
                    626:
                    627:        /* Expand ~ in known host file names. */
                    628:        /* XXX mem-leaks: */
1.72      markus    629:        options.system_hostfile =
                    630:            tilde_expand_filename(options.system_hostfile, original_real_uid);
                    631:        options.user_hostfile =
                    632:            tilde_expand_filename(options.user_hostfile, original_real_uid);
                    633:        options.system_hostfile2 =
                    634:            tilde_expand_filename(options.system_hostfile2, original_real_uid);
                    635:        options.user_hostfile2 =
                    636:            tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149     markus    637:
                    638:        signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31      markus    639:
                    640:        /* Log into the remote system.  This never returns if the login fails. */
1.173     markus    641:        ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31      markus    642:
1.112     markus    643:        /* We no longer need the private host keys.  Clear them now. */
                    644:        if (sensitive_data.nkeys != 0) {
                    645:                for (i = 0; i < sensitive_data.nkeys; i++) {
                    646:                        if (sensitive_data.keys[i] != NULL) {
                    647:                                /* Destroys contents safely */
                    648:                                debug3("clear hostkey %d", i);
                    649:                                key_free(sensitive_data.keys[i]);
                    650:                                sensitive_data.keys[i] = NULL;
                    651:                        }
                    652:                }
                    653:                xfree(sensitive_data.keys);
1.134     markus    654:        }
                    655:        for (i = 0; i < options.num_identity_files; i++) {
                    656:                if (options.identity_files[i]) {
                    657:                        xfree(options.identity_files[i]);
                    658:                        options.identity_files[i] = NULL;
                    659:                }
                    660:                if (options.identity_keys[i]) {
                    661:                        key_free(options.identity_keys[i]);
                    662:                        options.identity_keys[i] = NULL;
                    663:                }
1.112     markus    664:        }
1.31      markus    665:
1.45      markus    666:        exit_status = compat20 ? ssh_session2() : ssh_session();
                    667:        packet_close();
1.186     djm       668:
                    669:        /*
1.203     djm       670:         * Send SIGHUP to proxy command if used. We don't wait() in
1.186     djm       671:         * case it hangs and instead rely on init to reap the child
                    672:         */
                    673:        if (proxy_command_pid > 1)
                    674:                kill(proxy_command_pid, SIGHUP);
                    675:
1.45      markus    676:        return exit_status;
                    677: }
                    678:
1.202     markus    679: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
                    680:
1.126     itojun    681: static void
1.150     stevesk   682: x11_get_proto(char **_proto, char **_data)
1.50      markus    683: {
1.202     markus    684:        char cmd[1024];
1.50      markus    685:        char line[512];
1.202     markus    686:        char xdisplay[512];
1.150     stevesk   687:        static char proto[512], data[512];
1.50      markus    688:        FILE *f;
1.202     markus    689:        int got_data = 0, generated = 0, do_unlink = 0, i;
                    690:        char *display, *xauthdir, *xauthfile;
1.182     stevesk   691:        struct stat st;
1.50      markus    692:
1.202     markus    693:        xauthdir = xauthfile = NULL;
1.150     stevesk   694:        *_proto = proto;
                    695:        *_data = data;
                    696:        proto[0] = data[0] = '\0';
1.202     markus    697:
1.182     stevesk   698:        if (!options.xauth_location ||
                    699:            (stat(options.xauth_location, &st) == -1)) {
                    700:                debug("No xauth program.");
                    701:        } else {
                    702:                if ((display = getenv("DISPLAY")) == NULL) {
                    703:                        debug("x11_get_proto: DISPLAY not set");
                    704:                        return;
                    705:                }
1.202     markus    706:                /*
                    707:                 * Handle FamilyLocal case where $DISPLAY does
                    708:                 * not match an authorization entry.  For this we
                    709:                 * just try "xauth list unix:displaynum.screennum".
                    710:                 * XXX: "localhost" match to determine FamilyLocal
                    711:                 *      is not perfect.
                    712:                 */
                    713:                if (strncmp(display, "localhost:", 10) == 0) {
                    714:                        snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
                    715:                            display + 10);
                    716:                        display = xdisplay;
                    717:                }
                    718:                if (options.forward_x11_trusted == 0) {
                    719:                        xauthdir = xmalloc(MAXPATHLEN);
                    720:                        xauthfile = xmalloc(MAXPATHLEN);
                    721:                        strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
                    722:                        if (mkdtemp(xauthdir) != NULL) {
                    723:                                do_unlink = 1;
                    724:                                snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
                    725:                                    xauthdir);
                    726:                                snprintf(cmd, sizeof(cmd),
                    727:                                    "%s -f %s generate %s " SSH_X11_PROTO
1.207     markus    728:                                    " untrusted timeout 1200 2>" _PATH_DEVNULL,
1.202     markus    729:                                    options.xauth_location, xauthfile, display);
                    730:                                debug2("x11_get_proto: %s", cmd);
                    731:                                if (system(cmd) == 0)
                    732:                                        generated = 1;
                    733:                        }
                    734:                }
                    735:                snprintf(cmd, sizeof(cmd),
                    736:                    "%s %s%s list %s . 2>" _PATH_DEVNULL,
                    737:                    options.xauth_location,
                    738:                    generated ? "-f " : "" ,
                    739:                    generated ? xauthfile : "",
                    740:                    display);
                    741:                debug2("x11_get_proto: %s", cmd);
                    742:                f = popen(cmd, "r");
1.55      markus    743:                if (f && fgets(line, sizeof(line), f) &&
1.150     stevesk   744:                    sscanf(line, "%*s %511s %511s", proto, data) == 2)
1.55      markus    745:                        got_data = 1;
                    746:                if (f)
                    747:                        pclose(f);
                    748:        }
1.202     markus    749:
                    750:        if (do_unlink) {
                    751:                unlink(xauthfile);
                    752:                rmdir(xauthdir);
                    753:        }
                    754:        if (xauthdir)
                    755:                xfree(xauthdir);
                    756:        if (xauthfile)
                    757:                xfree(xauthfile);
                    758:
1.50      markus    759:        /*
                    760:         * If we didn't get authentication data, just make up some
                    761:         * data.  The forwarding code will check the validity of the
                    762:         * response anyway, and substitute this data.  The X11
                    763:         * server, however, will ignore this fake data and use
                    764:         * whatever authentication mechanisms it was using otherwise
                    765:         * for the local connection.
                    766:         */
                    767:        if (!got_data) {
                    768:                u_int32_t rand = 0;
                    769:
1.202     markus    770:                logit("Warning: No xauth data; "
                    771:                    "using fake authentication data for X11 forwarding.");
                    772:                strlcpy(proto, SSH_X11_PROTO, sizeof proto);
1.50      markus    773:                for (i = 0; i < 16; i++) {
                    774:                        if (i % 4 == 0)
                    775:                                rand = arc4random();
1.202     markus    776:                        snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
                    777:                            rand & 0xff);
1.50      markus    778:                        rand >>= 8;
                    779:                }
                    780:        }
                    781: }
                    782:
1.126     itojun    783: static void
1.70      markus    784: ssh_init_forwarding(void)
                    785: {
1.86      markus    786:        int success = 0;
1.70      markus    787:        int i;
1.86      markus    788:
1.70      markus    789:        /* Initiate local TCP/IP port forwardings. */
                    790:        for (i = 0; i < options.num_local_forwards; i++) {
                    791:                debug("Connections to local port %d forwarded to remote address %.200s:%d",
                    792:                    options.local_forwards[i].port,
                    793:                    options.local_forwards[i].host,
                    794:                    options.local_forwards[i].host_port);
1.158     markus    795:                success += channel_setup_local_fwd_listener(
1.70      markus    796:                    options.local_forwards[i].port,
                    797:                    options.local_forwards[i].host,
                    798:                    options.local_forwards[i].host_port,
                    799:                    options.gateway_ports);
                    800:        }
1.86      markus    801:        if (i > 0 && success == 0)
                    802:                error("Could not request local forwarding.");
1.70      markus    803:
                    804:        /* Initiate remote TCP/IP port forwardings. */
                    805:        for (i = 0; i < options.num_remote_forwards; i++) {
                    806:                debug("Connections to remote port %d forwarded to local address %.200s:%d",
                    807:                    options.remote_forwards[i].port,
                    808:                    options.remote_forwards[i].host,
                    809:                    options.remote_forwards[i].host_port);
                    810:                channel_request_remote_forwarding(
                    811:                    options.remote_forwards[i].port,
                    812:                    options.remote_forwards[i].host,
                    813:                    options.remote_forwards[i].host_port);
                    814:        }
                    815: }
                    816:
1.126     itojun    817: static void
1.70      markus    818: check_agent_present(void)
                    819: {
                    820:        if (options.forward_agent) {
                    821:                /* Clear agent forwarding if we don\'t have an agent. */
1.185     stevesk   822:                if (!ssh_agent_present())
1.70      markus    823:                        options.forward_agent = 0;
                    824:        }
                    825: }
                    826:
1.126     itojun    827: static int
1.45      markus    828: ssh_session(void)
                    829: {
                    830:        int type;
                    831:        int interactive = 0;
                    832:        int have_tty = 0;
                    833:        struct winsize ws;
                    834:        char *cp;
                    835:
1.31      markus    836:        /* Enable compression if requested. */
                    837:        if (options.compression) {
                    838:                debug("Requesting compression at level %d.", options.compression_level);
                    839:
                    840:                if (options.compression_level < 1 || options.compression_level > 9)
                    841:                        fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
                    842:
                    843:                /* Send the request. */
                    844:                packet_start(SSH_CMSG_REQUEST_COMPRESSION);
                    845:                packet_put_int(options.compression_level);
                    846:                packet_send();
                    847:                packet_write_wait();
1.156     markus    848:                type = packet_read();
1.31      markus    849:                if (type == SSH_SMSG_SUCCESS)
                    850:                        packet_start_compression(options.compression_level);
                    851:                else if (type == SSH_SMSG_FAILURE)
1.191     itojun    852:                        logit("Warning: Remote host refused compression.");
1.31      markus    853:                else
                    854:                        packet_disconnect("Protocol error waiting for compression response.");
                    855:        }
                    856:        /* Allocate a pseudo tty if appropriate. */
                    857:        if (tty_flag) {
                    858:                debug("Requesting pty.");
                    859:
                    860:                /* Start the packet. */
                    861:                packet_start(SSH_CMSG_REQUEST_PTY);
                    862:
                    863:                /* Store TERM in the packet.  There is no limit on the
                    864:                   length of the string. */
                    865:                cp = getenv("TERM");
                    866:                if (!cp)
                    867:                        cp = "";
1.124     markus    868:                packet_put_cstring(cp);
1.31      markus    869:
                    870:                /* Store window size in the packet. */
                    871:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                    872:                        memset(&ws, 0, sizeof(ws));
                    873:                packet_put_int(ws.ws_row);
                    874:                packet_put_int(ws.ws_col);
                    875:                packet_put_int(ws.ws_xpixel);
                    876:                packet_put_int(ws.ws_ypixel);
                    877:
                    878:                /* Store tty modes in the packet. */
1.115     stevesk   879:                tty_make_modes(fileno(stdin), NULL);
1.31      markus    880:
                    881:                /* Send the packet, and wait for it to leave. */
                    882:                packet_send();
                    883:                packet_write_wait();
                    884:
                    885:                /* Read response from the server. */
1.156     markus    886:                type = packet_read();
1.43      markus    887:                if (type == SSH_SMSG_SUCCESS) {
1.31      markus    888:                        interactive = 1;
1.45      markus    889:                        have_tty = 1;
1.43      markus    890:                } else if (type == SSH_SMSG_FAILURE)
1.191     itojun    891:                        logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31      markus    892:                else
                    893:                        packet_disconnect("Protocol error waiting for pty request response.");
                    894:        }
                    895:        /* Request X11 forwarding if enabled and DISPLAY is set. */
                    896:        if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.150     stevesk   897:                char *proto, *data;
1.50      markus    898:                /* Get reasonable local authentication information. */
1.150     stevesk   899:                x11_get_proto(&proto, &data);
1.50      markus    900:                /* Request forwarding with authentication spoofing. */
1.31      markus    901:                debug("Requesting X11 forwarding with authentication spoofing.");
1.50      markus    902:                x11_request_forwarding_with_spoofing(0, proto, data);
1.31      markus    903:
                    904:                /* Read response from the server. */
1.156     markus    905:                type = packet_read();
1.31      markus    906:                if (type == SSH_SMSG_SUCCESS) {
                    907:                        interactive = 1;
1.50      markus    908:                } else if (type == SSH_SMSG_FAILURE) {
1.191     itojun    909:                        logit("Warning: Remote host denied X11 forwarding.");
1.50      markus    910:                } else {
1.31      markus    911:                        packet_disconnect("Protocol error waiting for X11 forwarding");
1.50      markus    912:                }
1.31      markus    913:        }
                    914:        /* Tell the packet module whether this is an interactive session. */
1.80      markus    915:        packet_set_interactive(interactive);
1.31      markus    916:
                    917:        /* Request authentication agent forwarding if appropriate. */
1.70      markus    918:        check_agent_present();
                    919:
1.31      markus    920:        if (options.forward_agent) {
                    921:                debug("Requesting authentication agent forwarding.");
                    922:                auth_request_forwarding();
                    923:
                    924:                /* Read response from the server. */
1.156     markus    925:                type = packet_read();
1.155     markus    926:                packet_check_eom();
1.31      markus    927:                if (type != SSH_SMSG_SUCCESS)
1.191     itojun    928:                        logit("Warning: Remote host denied authentication agent forwarding.");
1.31      markus    929:        }
                    930:
1.70      markus    931:        /* Initiate port forwardings. */
                    932:        ssh_init_forwarding();
1.34      markus    933:
                    934:        /* If requested, let ssh continue in the background. */
1.48      markus    935:        if (fork_after_authentication_flag)
1.34      markus    936:                if (daemon(1, 1) < 0)
                    937:                        fatal("daemon() failed: %.200s", strerror(errno));
1.31      markus    938:
1.33      markus    939:        /*
                    940:         * If a command was specified on the command line, execute the
                    941:         * command now. Otherwise request the server to start a shell.
                    942:         */
1.31      markus    943:        if (buffer_len(&command) > 0) {
                    944:                int len = buffer_len(&command);
                    945:                if (len > 900)
                    946:                        len = 900;
1.152     stevesk   947:                debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31      markus    948:                packet_start(SSH_CMSG_EXEC_CMD);
                    949:                packet_put_string(buffer_ptr(&command), buffer_len(&command));
                    950:                packet_send();
                    951:                packet_write_wait();
                    952:        } else {
                    953:                debug("Requesting shell.");
                    954:                packet_start(SSH_CMSG_EXEC_SHELL);
                    955:                packet_send();
                    956:                packet_write_wait();
                    957:        }
                    958:
                    959:        /* Enter the interactive session. */
1.119     stevesk   960:        return client_loop(have_tty, tty_flag ?
                    961:            options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45      markus    962: }
                    963:
1.126     itojun    964: static void
1.157     markus    965: client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89      markus    966: {
                    967:        int id, len;
                    968:
                    969:        id = packet_get_int();
                    970:        len = buffer_len(&command);
1.100     markus    971:        if (len > 900)
                    972:                len = 900;
1.155     markus    973:        packet_check_eom();
1.89      markus    974:        if (type == SSH2_MSG_CHANNEL_FAILURE)
                    975:                fatal("Request for subsystem '%.*s' failed on channel %d",
1.152     stevesk   976:                    len, (u_char *)buffer_ptr(&command), id);
1.170     markus    977: }
                    978:
                    979: void
1.206     markus    980: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170     markus    981: {
                    982:        int i;
                    983:
                    984:        i = client_global_request_id++;
1.206     markus    985:        if (i >= options.num_remote_forwards)
1.170     markus    986:                return;
                    987:        debug("remote forward %s for: listen %d, connect %s:%d",
                    988:            type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
                    989:            options.remote_forwards[i].port,
                    990:            options.remote_forwards[i].host,
                    991:            options.remote_forwards[i].host_port);
                    992:        if (type == SSH2_MSG_REQUEST_FAILURE)
1.191     itojun    993:                logit("Warning: remote port forwarding failed for listen port %d",
1.170     markus    994:                    options.remote_forwards[i].port);
1.89      markus    995: }
                    996:
1.143     markus    997: /* request pty/x11/agent/tcpfwd/shell for channel */
1.126     itojun    998: static void
1.143     markus    999: ssh_session2_setup(int id, void *arg)
1.45      markus   1000: {
                   1001:        int len;
1.80      markus   1002:        int interactive = 0;
1.115     stevesk  1003:        struct termios tio;
1.80      markus   1004:
1.187     markus   1005:        debug2("ssh_session2_setup: id %d", id);
1.31      markus   1006:
1.45      markus   1007:        if (tty_flag) {
                   1008:                struct winsize ws;
                   1009:                char *cp;
                   1010:                cp = getenv("TERM");
                   1011:                if (!cp)
                   1012:                        cp = "";
                   1013:                /* Store window size in the packet. */
                   1014:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                   1015:                        memset(&ws, 0, sizeof(ws));
                   1016:
                   1017:                channel_request_start(id, "pty-req", 0);
                   1018:                packet_put_cstring(cp);
                   1019:                packet_put_int(ws.ws_col);
                   1020:                packet_put_int(ws.ws_row);
                   1021:                packet_put_int(ws.ws_xpixel);
                   1022:                packet_put_int(ws.ws_ypixel);
1.115     stevesk  1023:                tio = get_saved_tio();
                   1024:                tty_make_modes(/*ignored*/ 0, &tio);
1.45      markus   1025:                packet_send();
1.80      markus   1026:                interactive = 1;
1.45      markus   1027:                /* XXX wait for reply */
                   1028:        }
1.50      markus   1029:        if (options.forward_x11 &&
                   1030:            getenv("DISPLAY") != NULL) {
1.150     stevesk  1031:                char *proto, *data;
1.50      markus   1032:                /* Get reasonable local authentication information. */
1.150     stevesk  1033:                x11_get_proto(&proto, &data);
1.50      markus   1034:                /* Request forwarding with authentication spoofing. */
                   1035:                debug("Requesting X11 forwarding with authentication spoofing.");
                   1036:                x11_request_forwarding_with_spoofing(id, proto, data);
1.80      markus   1037:                interactive = 1;
1.50      markus   1038:                /* XXX wait for reply */
                   1039:        }
                   1040:
1.70      markus   1041:        check_agent_present();
                   1042:        if (options.forward_agent) {
                   1043:                debug("Requesting authentication agent forwarding.");
                   1044:                channel_request_start(id, "auth-agent-req@openssh.com", 0);
                   1045:                packet_send();
                   1046:        }
                   1047:
1.45      markus   1048:        len = buffer_len(&command);
                   1049:        if (len > 0) {
                   1050:                if (len > 900)
                   1051:                        len = 900;
1.85      djm      1052:                if (subsystem_flag) {
1.152     stevesk  1053:                        debug("Sending subsystem: %.*s", len, (u_char *)buffer_ptr(&command));
1.89      markus   1054:                        channel_request_start(id, "subsystem", /*want reply*/ 1);
                   1055:                        /* register callback for reply */
1.172     deraadt  1056:                        /* XXX we assume that client_loop has already been called */
1.89      markus   1057:                        dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
                   1058:                        dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
1.85      djm      1059:                } else {
1.152     stevesk  1060:                        debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.85      djm      1061:                        channel_request_start(id, "exec", 0);
                   1062:                }
1.100     markus   1063:                packet_put_string(buffer_ptr(&command), buffer_len(&command));
1.45      markus   1064:                packet_send();
                   1065:        } else {
1.161     markus   1066:                channel_request_start(id, "shell", 0);
                   1067:                packet_send();
1.45      markus   1068:        }
1.90      markus   1069:
1.80      markus   1070:        packet_set_interactive(interactive);
1.45      markus   1071: }
                   1072:
1.143     markus   1073: /* open new channel for a session */
1.126     itojun   1074: static int
1.143     markus   1075: ssh_session2_open(void)
1.45      markus   1076: {
1.118     markus   1077:        Channel *c;
                   1078:        int window, packetmax, in, out, err;
1.60      markus   1079:
1.62      markus   1080:        if (stdin_null_flag) {
1.93      itojun   1081:                in = open(_PATH_DEVNULL, O_RDONLY);
1.62      markus   1082:        } else {
                   1083:                in = dup(STDIN_FILENO);
                   1084:        }
1.60      markus   1085:        out = dup(STDOUT_FILENO);
                   1086:        err = dup(STDERR_FILENO);
1.45      markus   1087:
                   1088:        if (in < 0 || out < 0 || err < 0)
1.62      markus   1089:                fatal("dup() in/out/err failed");
1.45      markus   1090:
1.69      markus   1091:        /* enable nonblocking unless tty */
                   1092:        if (!isatty(in))
                   1093:                set_nonblock(in);
                   1094:        if (!isatty(out))
                   1095:                set_nonblock(out);
                   1096:        if (!isatty(err))
                   1097:                set_nonblock(err);
                   1098:
1.65      markus   1099:        window = CHAN_SES_WINDOW_DEFAULT;
                   1100:        packetmax = CHAN_SES_PACKET_DEFAULT;
1.164     markus   1101:        if (tty_flag) {
                   1102:                window >>= 1;
                   1103:                packetmax >>= 1;
1.45      markus   1104:        }
1.118     markus   1105:        c = channel_new(
1.45      markus   1106:            "session", SSH_CHANNEL_OPENING, in, out, err,
1.65      markus   1107:            window, packetmax, CHAN_EXTENDED_WRITE,
1.192     markus   1108:            "client-session", /*nonblock*/0);
1.45      markus   1109:
1.143     markus   1110:        debug3("ssh_session2_open: channel_new: %d", c->self);
1.106     markus   1111:
1.122     markus   1112:        channel_send_open(c->self);
1.143     markus   1113:        if (!no_shell_flag)
1.160     markus   1114:                channel_register_confirm(c->self, ssh_session2_setup);
1.106     markus   1115:
1.118     markus   1116:        return c->self;
1.106     markus   1117: }
                   1118:
1.126     itojun   1119: static int
1.106     markus   1120: ssh_session2(void)
                   1121: {
1.143     markus   1122:        int id = -1;
1.106     markus   1123:
                   1124:        /* XXX should be pre-session */
                   1125:        ssh_init_forwarding();
                   1126:
1.143     markus   1127:        if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
                   1128:                id = ssh_session2_open();
1.106     markus   1129:
                   1130:        /* If requested, let ssh continue in the background. */
                   1131:        if (fork_after_authentication_flag)
                   1132:                if (daemon(1, 1) < 0)
                   1133:                        fatal("daemon() failed: %.200s", strerror(errno));
1.31      markus   1134:
1.119     stevesk  1135:        return client_loop(tty_flag, tty_flag ?
                   1136:            options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72      markus   1137: }
                   1138:
1.126     itojun   1139: static void
1.104     markus   1140: load_public_identity_files(void)
                   1141: {
                   1142:        char *filename;
1.167     markus   1143:        int i = 0;
1.104     markus   1144:        Key *public;
1.167     markus   1145: #ifdef SMARTCARD
                   1146:        Key **keys;
1.104     markus   1147:
1.133     markus   1148:        if (options.smartcard_device != NULL &&
1.167     markus   1149:            options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
                   1150:            (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
                   1151:                int count = 0;
                   1152:                for (i = 0; keys[i] != NULL; i++) {
                   1153:                        count++;
                   1154:                        memmove(&options.identity_files[1], &options.identity_files[0],
                   1155:                            sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
                   1156:                        memmove(&options.identity_keys[1], &options.identity_keys[0],
                   1157:                            sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
                   1158:                        options.num_identity_files++;
                   1159:                        options.identity_keys[0] = keys[i];
1.194     markus   1160:                        options.identity_files[0] = sc_get_key_label(keys[i]);
1.167     markus   1161:                }
1.168     markus   1162:                if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
                   1163:                        options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167     markus   1164:                i = count;
                   1165:                xfree(keys);
1.127     markus   1166:        }
1.136     jakob    1167: #endif /* SMARTCARD */
1.131     millert  1168:        for (; i < options.num_identity_files; i++) {
                   1169:                filename = tilde_expand_filename(options.identity_files[i],
                   1170:                    original_real_uid);
                   1171:                public = key_load_public(filename, NULL);
                   1172:                debug("identity file %s type %d", filename,
                   1173:                    public ? public->type : -1);
                   1174:                xfree(options.identity_files[i]);
                   1175:                options.identity_files[i] = filename;
                   1176:                options.identity_keys[i] = public;
                   1177:        }
1.1       deraadt  1178: }