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

1.333   ! markus      1: /* $OpenBSD: ssh.c,v 1.332 2010/01/26 01:28:35 djm Exp $ */
1.1       deraadt     2: /*
1.32      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Ssh client program.  This program can be used to log into a remote machine.
                      7:  * The software supports strong authentication, encryption, and forwarding
                      8:  * of X11, TCP/IP, and authentication connections.
                      9:  *
1.64      deraadt    10:  * As far as I am concerned, the code I have written for this software
                     11:  * can be used freely for any purpose.  Any derived versions of this
                     12:  * software must be clearly marked as such, and if the derived work is
                     13:  * incompatible with the protocol description in the RFC file, it must be
                     14:  * called by a name other than "ssh" or "Secure Shell".
                     15:  *
                     16:  * Copyright (c) 1999 Niels Provos.  All rights reserved.
1.202     markus     17:  * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl.  All rights reserved.
1.64      deraadt    18:  *
                     19:  * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
                     20:  * in Canada (German citizen).
                     21:  *
                     22:  * Redistribution and use in source and binary forms, with or without
                     23:  * modification, are permitted provided that the following conditions
                     24:  * are met:
                     25:  * 1. Redistributions of source code must retain the above copyright
                     26:  *    notice, this list of conditions and the following disclaimer.
                     27:  * 2. Redistributions in binary form must reproduce the above copyright
                     28:  *    notice, this list of conditions and the following disclaimer in the
                     29:  *    documentation and/or other materials provided with the distribution.
                     30:  *
                     31:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     32:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     33:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     34:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     35:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     36:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     37:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     38:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     39:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     40:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32      deraadt    41:  */
1.1       deraadt    42:
1.293     deraadt    43: #include <sys/types.h>
1.312     djm        44: #include <sys/ioctl.h>
1.326     dtucker    45: #include <sys/param.h>
1.312     djm        46: #include <sys/queue.h>
1.259     stevesk    47: #include <sys/resource.h>
1.280     stevesk    48: #include <sys/socket.h>
1.264     stevesk    49: #include <sys/stat.h>
1.312     djm        50: #include <sys/types.h>
                     51: #include <sys/time.h>
1.258     stevesk    52:
1.265     stevesk    53: #include <ctype.h>
1.285     stevesk    54: #include <errno.h>
1.281     stevesk    55: #include <fcntl.h>
1.286     stevesk    56: #include <netdb.h>
1.258     stevesk    57: #include <paths.h>
1.279     stevesk    58: #include <pwd.h>
1.263     stevesk    59: #include <signal.h>
1.287     stevesk    60: #include <stddef.h>
1.291     stevesk    61: #include <stdio.h>
1.290     stevesk    62: #include <stdlib.h>
1.289     stevesk    63: #include <string.h>
1.288     stevesk    64: #include <unistd.h>
1.49      markus     65:
                     66: #include <openssl/evp.h>
1.72      markus     67: #include <openssl/err.h>
1.1       deraadt    68:
1.293     deraadt    69: #include "xmalloc.h"
1.84      markus     70: #include "ssh.h"
                     71: #include "ssh1.h"
                     72: #include "ssh2.h"
                     73: #include "compat.h"
                     74: #include "cipher.h"
1.1       deraadt    75: #include "packet.h"
                     76: #include "buffer.h"
1.123     markus     77: #include "channels.h"
1.49      markus     78: #include "key.h"
1.58      markus     79: #include "authfd.h"
1.49      markus     80: #include "authfile.h"
1.83      markus     81: #include "pathnames.h"
1.214     djm        82: #include "dispatch.h"
1.81      markus     83: #include "clientloop.h"
1.84      markus     84: #include "log.h"
                     85: #include "readconf.h"
                     86: #include "sshconnect.h"
                     87: #include "misc.h"
1.95      markus     88: #include "kex.h"
                     89: #include "mac.h"
1.213     deraadt    90: #include "sshpty.h"
1.212     djm        91: #include "match.h"
1.214     djm        92: #include "msg.h"
1.225     dtucker    93: #include "uidswap.h"
1.327     andreas    94: #include "roaming.h"
1.278     stevesk    95: #include "version.h"
1.49      markus     96:
1.333   ! markus     97: #ifdef ENABLE_PKCS11
        !            98: #include "ssh-pkcs11.h"
1.137     jakob      99: #endif
1.127     markus    100:
1.49      markus    101: extern char *__progname;
1.1       deraadt   102:
1.316     djm       103: /* Flag indicating whether debug mode is on.  May be set on the command line. */
1.1       deraadt   104: int debug_flag = 0;
                    105:
1.46      markus    106: /* Flag indicating whether a tty should be allocated */
1.1       deraadt   107: int tty_flag = 0;
1.79      markus    108: int no_tty_flag = 0;
                    109: int force_tty_flag = 0;
1.1       deraadt   110:
1.45      markus    111: /* don't exec a shell */
                    112: int no_shell_flag = 0;
                    113:
1.33      markus    114: /*
                    115:  * Flag indicating that nothing should be read from stdin.  This can be set
                    116:  * on the command line.
                    117:  */
1.1       deraadt   118: int stdin_null_flag = 0;
                    119:
1.33      markus    120: /*
                    121:  * Flag indicating that ssh should fork after authentication.  This is useful
1.172     deraadt   122:  * so that the passphrase can be entered manually, and then ssh goes to the
1.33      markus    123:  * background.
                    124:  */
1.1       deraadt   125: int fork_after_authentication_flag = 0;
                    126:
1.331     dtucker   127: /* forward stdio to remote host and port */
                    128: char *stdio_forward_host = NULL;
                    129: int stdio_forward_port = 0;
                    130:
1.33      markus    131: /*
                    132:  * General data structure for command line options and options configurable
                    133:  * in configuration files.  See readconf.h.
                    134:  */
1.1       deraadt   135: Options options;
                    136:
1.139     markus    137: /* optional user configfile */
                    138: char *config = NULL;
                    139:
1.33      markus    140: /*
                    141:  * Name of the host we are connecting to.  This is the name given on the
                    142:  * command line, or the HostName specified for the user-supplied name in a
                    143:  * configuration file.
                    144:  */
1.1       deraadt   145: char *host;
                    146:
1.22      provos    147: /* socket address the host resolves to */
1.37      markus    148: struct sockaddr_storage hostaddr;
1.1       deraadt   149:
1.112     markus    150: /* Private host keys. */
1.173     markus    151: Sensitive sensitive_data;
1.1       deraadt   152:
1.10      dugsong   153: /* Original real UID. */
                    154: uid_t original_real_uid;
1.177     markus    155: uid_t original_effective_uid;
1.1       deraadt   156:
1.45      markus    157: /* command to be executed */
                    158: Buffer command;
                    159:
1.85      djm       160: /* Should we execute a command or invoke a subsystem? */
                    161: int subsystem_flag = 0;
                    162:
1.170     markus    163: /* # of replies received for global requests */
1.315     djm       164: static int remote_forward_confirms_received = 0;
1.170     markus    165:
1.186     djm       166: /* pid of proxycommand child process */
                    167: pid_t proxy_command_pid = 0;
1.313     djm       168:
                    169: /* mux.c */
                    170: extern int muxserver_sock;
                    171: extern u_int muxclient_command;
                    172:
1.186     djm       173:
1.1       deraadt   174: /* Prints a help message to the user.  This function never returns. */
                    175:
1.126     itojun    176: static void
1.93      itojun    177: usage(void)
1.1       deraadt   178: {
1.208     markus    179:        fprintf(stderr,
1.321     jmc       180: "usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
1.251     jmc       181: "           [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
1.233     jmc       182: "           [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
1.232     djm       183: "           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
1.233     jmc       184: "           [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
1.331     dtucker   185: "           [-W host:port] [-w local_tun[:remote_tun]]\n"
                    186: "           [user@]hostname [command]\n"
1.208     markus    187:        );
1.257     dtucker   188:        exit(255);
1.1       deraadt   189: }
                    190:
1.126     itojun    191: static int ssh_session(void);
                    192: static int ssh_session2(void);
                    193: static void load_public_identity_files(void);
1.312     djm       194:
                    195: /* from muxclient.c */
                    196: void muxclient(const char *);
                    197: void muxserver_listen(void);
1.45      markus    198:
1.32      deraadt   199: /*
                    200:  * Main program for the ssh client.
                    201:  */
1.2       provos    202: int
                    203: main(int ac, char **av)
1.1       deraadt   204: {
1.326     dtucker   205:        int i, r, opt, exit_status, use_syslog;
                    206:        char *p, *cp, *line, *argv0, buf[MAXPATHLEN];
1.31      markus    207:        struct stat st;
1.98      markus    208:        struct passwd *pw;
1.303     djm       209:        int dummy, timeout_ms;
1.144     stevesk   210:        extern int optind, optreset;
                    211:        extern char *optarg;
1.244     djm       212:        struct servent *sp;
1.232     djm       213:        Forward fwd;
1.250     djm       214:
                    215:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    216:        sanitise_stdfd();
1.31      markus    217:
1.33      markus    218:        /*
                    219:         * Save the original real uid.  It will be needed later (uid-swapping
                    220:         * may clobber the real uid).
                    221:         */
1.31      markus    222:        original_real_uid = getuid();
                    223:        original_effective_uid = geteuid();
                    224:
1.184     stevesk   225:        /*
                    226:         * Use uid-swapping to give up root privileges for the duration of
                    227:         * option processing.  We will re-instantiate the rights when we are
                    228:         * ready to create the privileged port, and will permanently drop
                    229:         * them when the port has been created (actually, when the connection
                    230:         * has been made, as we may need to create the port several times).
                    231:         */
                    232:        PRIV_END;
                    233:
1.31      markus    234:        /* If we are installed setuid root be careful to not drop core. */
                    235:        if (original_real_uid != original_effective_uid) {
                    236:                struct rlimit rlim;
                    237:                rlim.rlim_cur = rlim.rlim_max = 0;
                    238:                if (setrlimit(RLIMIT_CORE, &rlim) < 0)
                    239:                        fatal("setrlimit failed: %.100s", strerror(errno));
1.1       deraadt   240:        }
1.107     markus    241:        /* Get user data. */
                    242:        pw = getpwuid(original_real_uid);
                    243:        if (!pw) {
1.191     itojun    244:                logit("You don't exist, go away!");
1.257     dtucker   245:                exit(255);
1.107     markus    246:        }
                    247:        /* Take a copy of the returned structure. */
                    248:        pw = pwcopy(pw);
1.31      markus    249:
1.33      markus    250:        /*
                    251:         * Set our umask to something reasonable, as some files are created
                    252:         * with the default umask.  This will make them world-readable but
                    253:         * writable only by the owner, which is ok for all files for which we
                    254:         * don't set the modes explicitly.
                    255:         */
1.31      markus    256:        umask(022);
                    257:
1.316     djm       258:        /*
                    259:         * Initialize option structure to indicate that no values have been
                    260:         * set.
                    261:         */
1.31      markus    262:        initialize_options(&options);
                    263:
                    264:        /* Parse command-line arguments. */
                    265:        host = NULL;
1.320     djm       266:        use_syslog = 0;
1.325     markus    267:        argv0 = av[0];
1.31      markus    268:
1.266     djm       269:  again:
1.316     djm       270:        while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
1.331     dtucker   271:            "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) {
1.31      markus    272:                switch (opt) {
1.91      jakob     273:                case '1':
                    274:                        options.protocol = SSH_PROTO_1;
                    275:                        break;
1.47      markus    276:                case '2':
                    277:                        options.protocol = SSH_PROTO_2;
                    278:                        break;
1.37      markus    279:                case '4':
1.196     djm       280:                        options.address_family = AF_INET;
1.37      markus    281:                        break;
                    282:                case '6':
1.196     djm       283:                        options.address_family = AF_INET6;
1.37      markus    284:                        break;
1.31      markus    285:                case 'n':
                    286:                        stdin_null_flag = 1;
                    287:                        break;
                    288:                case 'f':
                    289:                        fork_after_authentication_flag = 1;
                    290:                        stdin_null_flag = 1;
                    291:                        break;
                    292:                case 'x':
                    293:                        options.forward_x11 = 0;
                    294:                        break;
                    295:                case 'X':
                    296:                        options.forward_x11 = 1;
                    297:                        break;
1.320     djm       298:                case 'y':
                    299:                        use_syslog = 1;
                    300:                        break;
1.202     markus    301:                case 'Y':
                    302:                        options.forward_x11 = 1;
                    303:                        options.forward_x11_trusted = 1;
                    304:                        break;
1.31      markus    305:                case 'g':
                    306:                        options.gateway_ports = 1;
                    307:                        break;
1.229     djm       308:                case 'O':
1.332     djm       309:                        if (stdio_forward_host != NULL)
                    310:                                fatal("Cannot specify multiplexing "
                    311:                                    "command with -W");
                    312:                        else if (muxclient_command != 0)
                    313:                                fatal("Multiplexing command already specified");
1.229     djm       314:                        if (strcmp(optarg, "check") == 0)
1.312     djm       315:                                muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
1.229     djm       316:                        else if (strcmp(optarg, "exit") == 0)
1.312     djm       317:                                muxclient_command = SSHMUX_COMMAND_TERMINATE;
1.229     djm       318:                        else
                    319:                                fatal("Invalid multiplex command.");
                    320:                        break;
1.183     stevesk   321:                case 'P':       /* deprecated */
1.31      markus    322:                        options.use_privileged_port = 0;
                    323:                        break;
                    324:                case 'a':
                    325:                        options.forward_agent = 0;
1.53      markus    326:                        break;
                    327:                case 'A':
                    328:                        options.forward_agent = 1;
1.31      markus    329:                        break;
                    330:                case 'k':
1.204     dtucker   331:                        options.gss_deleg_creds = 0;
1.297     djm       332:                        break;
                    333:                case 'K':
                    334:                        options.gss_authentication = 1;
                    335:                        options.gss_deleg_creds = 1;
1.31      markus    336:                        break;
                    337:                case 'i':
                    338:                        if (stat(optarg, &st) < 0) {
1.128     fgsch     339:                                fprintf(stderr, "Warning: Identity file %s "
1.231     otto      340:                                    "not accessible: %s.\n", optarg,
                    341:                                    strerror(errno));
1.31      markus    342:                                break;
                    343:                        }
1.128     fgsch     344:                        if (options.num_identity_files >=
                    345:                            SSH_MAX_IDENTITY_FILES)
                    346:                                fatal("Too many identity files specified "
                    347:                                    "(max %d)", SSH_MAX_IDENTITY_FILES);
                    348:                        options.identity_files[options.num_identity_files++] =
                    349:                            xstrdup(optarg);
1.31      markus    350:                        break;
1.127     markus    351:                case 'I':
1.333   ! markus    352: #ifdef ENABLE_PKCS11
        !           353:                        options.pkcs11_provider = xstrdup(optarg);
1.137     jakob     354: #else
1.333   ! markus    355:                        fprintf(stderr, "no support for PKCS#11.\n");
1.137     jakob     356: #endif
1.127     markus    357:                        break;
1.31      markus    358:                case 't':
1.79      markus    359:                        if (tty_flag)
                    360:                                force_tty_flag = 1;
1.31      markus    361:                        tty_flag = 1;
                    362:                        break;
                    363:                case 'v':
1.197     markus    364:                        if (debug_flag == 0) {
1.66      markus    365:                                debug_flag = 1;
                    366:                                options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197     markus    367:                        } else {
                    368:                                if (options.log_level < SYSLOG_LEVEL_DEBUG3)
                    369:                                        options.log_level++;
1.66      markus    370:                                break;
1.197     markus    371:                        }
1.227     deraadt   372:                        /* FALLTHROUGH */
1.31      markus    373:                case 'V':
1.209     markus    374:                        fprintf(stderr, "%s, %s\n",
                    375:                            SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31      markus    376:                        if (opt == 'V')
                    377:                                exit(0);
                    378:                        break;
1.255     reyk      379:                case 'w':
1.256     reyk      380:                        if (options.tun_open == -1)
                    381:                                options.tun_open = SSH_TUNMODE_DEFAULT;
1.255     reyk      382:                        options.tun_local = a2tun(optarg, &options.tun_remote);
1.256     reyk      383:                        if (options.tun_local == SSH_TUNID_ERR) {
1.316     djm       384:                                fprintf(stderr,
                    385:                                    "Bad tun device '%s'\n", optarg);
1.257     dtucker   386:                                exit(255);
1.255     reyk      387:                        }
                    388:                        break;
1.331     dtucker   389:                case 'W':
1.332     djm       390:                        if (stdio_forward_host != NULL)
                    391:                                fatal("stdio forward already specified");
                    392:                        if (muxclient_command != 0)
                    393:                                fatal("Cannot specify stdio forward with -O");
1.331     dtucker   394:                        if (parse_forward(&fwd, optarg, 1, 0)) {
                    395:                                stdio_forward_host = fwd.listen_host;
                    396:                                stdio_forward_port = fwd.listen_port;
                    397:                                xfree(fwd.connect_host);
                    398:                        } else {
                    399:                                fprintf(stderr,
                    400:                                    "Bad stdio forwarding specification '%s'\n",
                    401:                                    optarg);
                    402:                                exit(255);
                    403:                        }
                    404:                        no_tty_flag = 1;
                    405:                        no_shell_flag = 1;
                    406:                        options.clear_forwardings = 1;
                    407:                        options.exit_on_forward_failure = 1;
                    408:                        break;
1.31      markus    409:                case 'q':
                    410:                        options.log_level = SYSLOG_LEVEL_QUIET;
                    411:                        break;
                    412:                case 'e':
                    413:                        if (optarg[0] == '^' && optarg[2] == 0 &&
1.128     fgsch     414:                            (u_char) optarg[1] >= 64 &&
                    415:                            (u_char) optarg[1] < 128)
1.78      markus    416:                                options.escape_char = (u_char) optarg[1] & 31;
1.31      markus    417:                        else if (strlen(optarg) == 1)
1.78      markus    418:                                options.escape_char = (u_char) optarg[0];
1.31      markus    419:                        else if (strcmp(optarg, "none") == 0)
1.119     stevesk   420:                                options.escape_char = SSH_ESCAPECHAR_NONE;
1.31      markus    421:                        else {
1.128     fgsch     422:                                fprintf(stderr, "Bad escape character '%s'.\n",
                    423:                                    optarg);
1.257     dtucker   424:                                exit(255);
1.31      markus    425:                        }
                    426:                        break;
                    427:                case 'c':
1.49      markus    428:                        if (ciphers_valid(optarg)) {
                    429:                                /* SSH2 only */
                    430:                                options.ciphers = xstrdup(optarg);
1.224     markus    431:                                options.cipher = SSH_CIPHER_INVALID;
1.49      markus    432:                        } else {
                    433:                                /* SSH1 only */
1.74      markus    434:                                options.cipher = cipher_number(optarg);
                    435:                                if (options.cipher == -1) {
1.128     fgsch     436:                                        fprintf(stderr,
                    437:                                            "Unknown cipher type '%s'\n",
                    438:                                            optarg);
1.257     dtucker   439:                                        exit(255);
1.49      markus    440:                                }
1.128     fgsch     441:                                if (options.cipher == SSH_CIPHER_3DES)
1.74      markus    442:                                        options.ciphers = "3des-cbc";
1.128     fgsch     443:                                else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74      markus    444:                                        options.ciphers = "blowfish-cbc";
1.128     fgsch     445:                                else
1.74      markus    446:                                        options.ciphers = (char *)-1;
1.95      markus    447:                        }
                    448:                        break;
                    449:                case 'm':
                    450:                        if (mac_valid(optarg))
                    451:                                options.macs = xstrdup(optarg);
                    452:                        else {
1.128     fgsch     453:                                fprintf(stderr, "Unknown mac type '%s'\n",
                    454:                                    optarg);
1.257     dtucker   455:                                exit(255);
1.31      markus    456:                        }
                    457:                        break;
1.214     djm       458:                case 'M':
1.242     djm       459:                        if (options.control_master == SSHCTL_MASTER_YES)
                    460:                                options.control_master = SSHCTL_MASTER_ASK;
                    461:                        else
                    462:                                options.control_master = SSHCTL_MASTER_YES;
1.214     djm       463:                        break;
1.31      markus    464:                case 'p':
1.113     stevesk   465:                        options.port = a2port(optarg);
1.323     djm       466:                        if (options.port <= 0) {
1.109     markus    467:                                fprintf(stderr, "Bad port '%s'\n", optarg);
1.257     dtucker   468:                                exit(255);
1.109     markus    469:                        }
1.31      markus    470:                        break;
                    471:                case 'l':
                    472:                        options.user = optarg;
                    473:                        break;
1.141     stevesk   474:
                    475:                case 'L':
1.324     djm       476:                        if (parse_forward(&fwd, optarg, 0, 0))
1.232     djm       477:                                add_local_forward(&options, &fwd);
                    478:                        else {
1.128     fgsch     479:                                fprintf(stderr,
1.232     djm       480:                                    "Bad local forwarding specification '%s'\n",
1.128     fgsch     481:                                    optarg);
1.257     dtucker   482:                                exit(255);
1.31      markus    483:                        }
1.232     djm       484:                        break;
                    485:
                    486:                case 'R':
1.324     djm       487:                        if (parse_forward(&fwd, optarg, 0, 1)) {
1.232     djm       488:                                add_remote_forward(&options, &fwd);
                    489:                        } else {
1.128     fgsch     490:                                fprintf(stderr,
1.232     djm       491:                                    "Bad remote forwarding specification "
                    492:                                    "'%s'\n", optarg);
1.257     dtucker   493:                                exit(255);
1.31      markus    494:                        }
                    495:                        break;
1.108     markus    496:
                    497:                case 'D':
1.324     djm       498:                        if (parse_forward(&fwd, optarg, 1, 0)) {
1.322     stevesk   499:                                add_local_forward(&options, &fwd);
1.232     djm       500:                        } else {
1.322     stevesk   501:                                fprintf(stderr,
                    502:                                    "Bad dynamic forwarding specification "
                    503:                                    "'%s'\n", optarg);
1.257     dtucker   504:                                exit(255);
1.109     markus    505:                        }
1.108     markus    506:                        break;
                    507:
1.31      markus    508:                case 'C':
                    509:                        options.compression = 1;
                    510:                        break;
1.45      markus    511:                case 'N':
                    512:                        no_shell_flag = 1;
                    513:                        no_tty_flag = 1;
                    514:                        break;
                    515:                case 'T':
                    516:                        no_tty_flag = 1;
                    517:                        break;
1.31      markus    518:                case 'o':
                    519:                        dummy = 1;
1.205     markus    520:                        line = xstrdup(optarg);
1.128     fgsch     521:                        if (process_config_line(&options, host ? host : "",
1.205     markus    522:                            line, "command-line", 0, &dummy) != 0)
1.257     dtucker   523:                                exit(255);
1.205     markus    524:                        xfree(line);
1.31      markus    525:                        break;
1.85      djm       526:                case 's':
                    527:                        subsystem_flag = 1;
1.117     markus    528:                        break;
1.214     djm       529:                case 'S':
                    530:                        if (options.control_path != NULL)
                    531:                                free(options.control_path);
                    532:                        options.control_path = xstrdup(optarg);
                    533:                        break;
1.117     markus    534:                case 'b':
                    535:                        options.bind_address = optarg;
1.85      djm       536:                        break;
1.139     markus    537:                case 'F':
                    538:                        config = optarg;
                    539:                        break;
1.31      markus    540:                default:
                    541:                        usage();
1.1       deraadt   542:                }
1.31      markus    543:        }
                    544:
1.128     fgsch     545:        ac -= optind;
                    546:        av += optind;
                    547:
1.329     guenther  548:        if (ac > 0 && !host) {
1.188     markus    549:                if (strrchr(*av, '@')) {
1.128     fgsch     550:                        p = xstrdup(*av);
1.188     markus    551:                        cp = strrchr(p, '@');
1.128     fgsch     552:                        if (cp == NULL || cp == p)
                    553:                                usage();
                    554:                        options.user = p;
                    555:                        *cp = '\0';
                    556:                        host = ++cp;
                    557:                } else
                    558:                        host = *av;
1.189     millert   559:                if (ac > 1) {
                    560:                        optind = optreset = 1;
1.128     fgsch     561:                        goto again;
                    562:                }
1.189     millert   563:                ac--, av++;
1.128     fgsch     564:        }
                    565:
1.31      markus    566:        /* Check that we got a host name. */
                    567:        if (!host)
                    568:                usage();
                    569:
1.54      markus    570:        SSLeay_add_all_algorithms();
1.72      markus    571:        ERR_load_crypto_strings();
1.31      markus    572:
                    573:        /* Initialize the command to execute on remote host. */
                    574:        buffer_init(&command);
1.1       deraadt   575:
1.33      markus    576:        /*
                    577:         * Save the command to execute on the remote host in a buffer. There
                    578:         * is no limit on the length of the command, except by the maximum
                    579:         * packet size.  Also sets the tty flag if there is no command.
                    580:         */
1.128     fgsch     581:        if (!ac) {
1.31      markus    582:                /* No command specified - execute shell on a tty. */
                    583:                tty_flag = 1;
1.85      djm       584:                if (subsystem_flag) {
1.128     fgsch     585:                        fprintf(stderr,
                    586:                            "You must specify a subsystem to invoke.\n");
1.85      djm       587:                        usage();
                    588:                }
1.31      markus    589:        } else {
1.128     fgsch     590:                /* A command has been specified.  Store it into the buffer. */
                    591:                for (i = 0; i < ac; i++) {
                    592:                        if (i)
1.31      markus    593:                                buffer_append(&command, " ", 1);
                    594:                        buffer_append(&command, av[i], strlen(av[i]));
                    595:                }
                    596:        }
                    597:
                    598:        /* Cannot fork to background if no command. */
1.316     djm       599:        if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
                    600:            !no_shell_flag)
                    601:                fatal("Cannot fork into background without a command "
                    602:                    "to execute.");
1.31      markus    603:
                    604:        /* Allocate a tty by default if no command specified. */
                    605:        if (buffer_len(&command) == 0)
                    606:                tty_flag = 1;
                    607:
1.180     deraadt   608:        /* Force no tty */
1.75      markus    609:        if (no_tty_flag)
                    610:                tty_flag = 0;
1.31      markus    611:        /* Do not allocate a tty if stdin is not a tty. */
1.236     djm       612:        if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
1.31      markus    613:                if (tty_flag)
1.316     djm       614:                        logit("Pseudo-terminal will not be allocated because "
                    615:                            "stdin is not a terminal.");
1.31      markus    616:                tty_flag = 0;
                    617:        }
1.45      markus    618:
1.101     markus    619:        /*
                    620:         * Initialize "log" output.  Since we are the client all output
                    621:         * actually goes to stderr.
                    622:         */
1.325     markus    623:        log_init(argv0,
1.316     djm       624:            options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
1.320     djm       625:            SYSLOG_FACILITY_USER, !use_syslog);
1.31      markus    626:
1.139     markus    627:        /*
                    628:         * Read per-user configuration file.  Ignore the system wide config
                    629:         * file if the user specifies a config file on the command line.
                    630:         */
                    631:        if (config != NULL) {
1.211     djm       632:                if (!read_config_file(config, host, &options, 0))
1.142     stevesk   633:                        fatal("Can't open user config file %.100s: "
                    634:                            "%.100s", config, strerror(errno));
1.295     stevesk   635:        } else {
1.326     dtucker   636:                r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
1.139     markus    637:                    _PATH_SSH_USER_CONFFILE);
1.326     dtucker   638:                if (r > 0 && (size_t)r < sizeof(buf))
                    639:                        (void)read_config_file(buf, host, &options, 1);
1.139     markus    640:
1.145     markus    641:                /* Read systemwide configuration file after use config. */
1.223     deraadt   642:                (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
1.210     djm       643:                    &options, 0);
1.139     markus    644:        }
1.31      markus    645:
                    646:        /* Fill configuration defaults. */
                    647:        fill_default_options(&options);
                    648:
1.196     djm       649:        channel_set_af(options.address_family);
                    650:
1.31      markus    651:        /* reinit */
1.325     markus    652:        log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
1.31      markus    653:
                    654:        if (options.user == NULL)
                    655:                options.user = xstrdup(pw->pw_name);
                    656:
1.317     dtucker   657:        /* Get default port if port has not been set. */
                    658:        if (options.port == 0) {
                    659:                sp = getservbyname(SSH_SERVICE_NAME, "tcp");
                    660:                options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
                    661:        }
                    662:
                    663:        if (options.local_command != NULL) {
                    664:                char thishost[NI_MAXHOST];
                    665:
                    666:                if (gethostname(thishost, sizeof(thishost)) == -1)
                    667:                        fatal("gethostname: %s", strerror(errno));
                    668:                snprintf(buf, sizeof(buf), "%d", options.port);
                    669:                debug3("expanding LocalCommand: %s", options.local_command);
                    670:                cp = options.local_command;
                    671:                options.local_command = percent_expand(cp, "d", pw->pw_dir,
                    672:                    "h", options.hostname? options.hostname : host,
                    673:                     "l", thishost, "n", host, "r", options.user, "p", buf,
                    674:                     "u", pw->pw_name, (char *)NULL);
                    675:                debug3("expanded LocalCommand: %s", options.local_command);
                    676:                xfree(cp);
                    677:        }
                    678:
1.31      markus    679:        if (options.hostname != NULL)
                    680:                host = options.hostname;
1.195     markus    681:
                    682:        /* force lowercase for hostkey matching */
                    683:        if (options.host_key_alias != NULL) {
                    684:                for (p = options.host_key_alias; *p; p++)
                    685:                        if (isupper(*p))
1.271     deraadt   686:                                *p = (char)tolower(*p);
1.246     djm       687:        }
                    688:
1.190     markus    689:        if (options.proxy_command != NULL &&
1.304     dtucker   690:            strcmp(options.proxy_command, "none") == 0) {
                    691:                xfree(options.proxy_command);
1.190     markus    692:                options.proxy_command = NULL;
1.304     dtucker   693:        }
1.245     djm       694:        if (options.control_path != NULL &&
1.304     dtucker   695:            strcmp(options.control_path, "none") == 0) {
                    696:                xfree(options.control_path);
1.245     djm       697:                options.control_path = NULL;
1.304     dtucker   698:        }
1.31      markus    699:
1.214     djm       700:        if (options.control_path != NULL) {
1.275     djm       701:                char thishost[NI_MAXHOST];
1.261     djm       702:
1.275     djm       703:                if (gethostname(thishost, sizeof(thishost)) == -1)
1.261     djm       704:                        fatal("gethostname: %s", strerror(errno));
1.241     djm       705:                snprintf(buf, sizeof(buf), "%d", options.port);
                    706:                cp = tilde_expand_filename(options.control_path,
                    707:                    original_real_uid);
1.304     dtucker   708:                xfree(options.control_path);
1.241     djm       709:                options.control_path = percent_expand(cp, "p", buf, "h", host,
1.275     djm       710:                    "r", options.user, "l", thishost, (char *)NULL);
1.241     djm       711:                xfree(cp);
1.214     djm       712:        }
1.312     djm       713:        if (muxclient_command != 0 && options.control_path == NULL)
1.240     djm       714:                fatal("No ControlPath specified for \"-O\" command");
1.242     djm       715:        if (options.control_path != NULL)
1.312     djm       716:                muxclient(options.control_path);
1.214     djm       717:
1.303     djm       718:        timeout_ms = options.connection_timeout * 1000;
                    719:
1.77      markus    720:        /* Open a connection to the remote host. */
1.203     djm       721:        if (ssh_connect(host, &hostaddr, options.port,
1.303     djm       722:            options.address_family, options.connection_attempts, &timeout_ms,
                    723:            options.tcp_keep_alive,
1.177     markus    724:            original_effective_uid == 0 && options.use_privileged_port,
1.179     markus    725:            options.proxy_command) != 0)
1.257     dtucker   726:                exit(255);
1.31      markus    727:
1.303     djm       728:        if (timeout_ms > 0)
                    729:                debug3("timeout: %d ms remain after connect", timeout_ms);
                    730:
1.33      markus    731:        /*
                    732:         * If we successfully made the connection, load the host private key
                    733:         * in case we will need it later for combined rsa-rhosts
                    734:         * authentication. This must be done before releasing extra
                    735:         * privileges, because the file is only readable by root.
1.174     markus    736:         * If we cannot access the private keys, load the public keys
                    737:         * instead and try to execute the ssh-keysign helper instead.
1.33      markus    738:         */
1.112     markus    739:        sensitive_data.nkeys = 0;
                    740:        sensitive_data.keys = NULL;
1.173     markus    741:        sensitive_data.external_keysign = 0;
1.178     markus    742:        if (options.rhosts_rsa_authentication ||
                    743:            options.hostbased_authentication) {
1.112     markus    744:                sensitive_data.nkeys = 3;
1.274     deraadt   745:                sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180     deraadt   746:                    sizeof(Key));
1.177     markus    747:
                    748:                PRIV_START;
1.112     markus    749:                sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.276     dtucker   750:                    _PATH_HOST_KEY_FILE, "", NULL, NULL);
1.112     markus    751:                sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
1.276     dtucker   752:                    _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
1.112     markus    753:                sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
1.276     dtucker   754:                    _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
1.177     markus    755:                PRIV_END;
1.173     markus    756:
1.181     markus    757:                if (options.hostbased_authentication == 1 &&
                    758:                    sensitive_data.keys[0] == NULL &&
1.173     markus    759:                    sensitive_data.keys[1] == NULL &&
                    760:                    sensitive_data.keys[2] == NULL) {
                    761:                        sensitive_data.keys[1] = key_load_public(
                    762:                            _PATH_HOST_DSA_KEY_FILE, NULL);
                    763:                        sensitive_data.keys[2] = key_load_public(
                    764:                            _PATH_HOST_RSA_KEY_FILE, NULL);
                    765:                        sensitive_data.external_keysign = 1;
                    766:                }
1.31      markus    767:        }
1.33      markus    768:        /*
                    769:         * Get rid of any extra privileges that we may have.  We will no
                    770:         * longer need them.  Also, extra privileges could make it very hard
                    771:         * to read identity files and other non-world-readable files from the
                    772:         * user's home directory if it happens to be on a NFS volume where
                    773:         * root is mapped to nobody.
                    774:         */
1.225     dtucker   775:        if (original_effective_uid == 0) {
                    776:                PRIV_START;
                    777:                permanently_set_uid(pw);
                    778:        }
1.31      markus    779:
1.33      markus    780:        /*
                    781:         * Now that we are back to our own permissions, create ~/.ssh
1.254     djm       782:         * directory if it doesn't already exist.
1.33      markus    783:         */
1.326     dtucker   784:        r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1.316     djm       785:            strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.326     dtucker   786:        if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0)
1.57      djm       787:                if (mkdir(buf, 0700) < 0)
1.31      markus    788:                        error("Could not create directory '%.200s'.", buf);
                    789:
1.104     markus    790:        /* load options.identity_files */
                    791:        load_public_identity_files();
                    792:
                    793:        /* Expand ~ in known host file names. */
                    794:        /* XXX mem-leaks: */
1.72      markus    795:        options.system_hostfile =
                    796:            tilde_expand_filename(options.system_hostfile, original_real_uid);
                    797:        options.user_hostfile =
                    798:            tilde_expand_filename(options.user_hostfile, original_real_uid);
                    799:        options.system_hostfile2 =
                    800:            tilde_expand_filename(options.system_hostfile2, original_real_uid);
                    801:        options.user_hostfile2 =
                    802:            tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149     markus    803:
                    804:        signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31      markus    805:
1.316     djm       806:        /* Log into the remote system.  Never returns if the login fails. */
1.303     djm       807:        ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
                    808:            pw, timeout_ms);
1.31      markus    809:
1.112     markus    810:        /* We no longer need the private host keys.  Clear them now. */
                    811:        if (sensitive_data.nkeys != 0) {
                    812:                for (i = 0; i < sensitive_data.nkeys; i++) {
                    813:                        if (sensitive_data.keys[i] != NULL) {
                    814:                                /* Destroys contents safely */
                    815:                                debug3("clear hostkey %d", i);
                    816:                                key_free(sensitive_data.keys[i]);
                    817:                                sensitive_data.keys[i] = NULL;
                    818:                        }
                    819:                }
                    820:                xfree(sensitive_data.keys);
1.134     markus    821:        }
                    822:        for (i = 0; i < options.num_identity_files; i++) {
                    823:                if (options.identity_files[i]) {
                    824:                        xfree(options.identity_files[i]);
                    825:                        options.identity_files[i] = NULL;
                    826:                }
                    827:                if (options.identity_keys[i]) {
                    828:                        key_free(options.identity_keys[i]);
                    829:                        options.identity_keys[i] = NULL;
                    830:                }
1.112     markus    831:        }
1.31      markus    832:
1.45      markus    833:        exit_status = compat20 ? ssh_session2() : ssh_session();
                    834:        packet_close();
1.186     djm       835:
1.312     djm       836:        if (options.control_path != NULL && muxserver_sock != -1)
1.214     djm       837:                unlink(options.control_path);
                    838:
1.186     djm       839:        /*
1.203     djm       840:         * Send SIGHUP to proxy command if used. We don't wait() in
1.186     djm       841:         * case it hangs and instead rely on init to reap the child
                    842:         */
                    843:        if (proxy_command_pid > 1)
                    844:                kill(proxy_command_pid, SIGHUP);
                    845:
1.45      markus    846:        return exit_status;
                    847: }
                    848:
1.315     djm       849: /* Callback for remote forward global requests */
                    850: static void
                    851: ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
                    852: {
                    853:        Forward *rfwd = (Forward *)ctxt;
                    854:
1.324     djm       855:        /* XXX verbose() on failure? */
1.315     djm       856:        debug("remote forward %s for: listen %d, connect %s:%d",
                    857:            type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
                    858:            rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
1.324     djm       859:        if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) {
                    860:                logit("Allocated port %u for remote forward to %s:%d",
                    861:                        packet_get_int(),
                    862:                        rfwd->connect_host, rfwd->connect_port);
                    863:        }
                    864:
1.315     djm       865:        if (type == SSH2_MSG_REQUEST_FAILURE) {
                    866:                if (options.exit_on_forward_failure)
                    867:                        fatal("Error: remote port forwarding failed for "
                    868:                            "listen port %d", rfwd->listen_port);
                    869:                else
                    870:                        logit("Warning: remote port forwarding failed for "
                    871:                            "listen port %d", rfwd->listen_port);
                    872:        }
1.318     djm       873:        if (++remote_forward_confirms_received == options.num_remote_forwards) {
1.315     djm       874:                debug("All remote forwarding requests processed");
1.318     djm       875:                if (fork_after_authentication_flag) {
                    876:                        fork_after_authentication_flag = 0;
                    877:                        if (daemon(1, 1) < 0)
                    878:                                fatal("daemon() failed: %.200s",
                    879:                                    strerror(errno));
                    880:                }
                    881:        }
1.315     djm       882: }
                    883:
1.126     itojun    884: static void
1.331     dtucker   885: client_cleanup_stdio_fwd(int id, void *arg)
                    886: {
                    887:        debug("stdio forwarding: done");
                    888:        cleanup_exit(0);
                    889: }
                    890:
                    891: static int
                    892: client_setup_stdio_fwd(const char *host_to_connect, u_short port_to_connect)
                    893: {
                    894:        Channel *c;
1.332     djm       895:        int in, out;
1.331     dtucker   896:
                    897:        debug3("client_setup_stdio_fwd %s:%d", host_to_connect,
                    898:            port_to_connect);
1.332     djm       899:
                    900:        in = dup(STDIN_FILENO);
                    901:        out = dup(STDOUT_FILENO);
                    902:        if (in < 0 || out < 0)
                    903:                fatal("channel_connect_stdio_fwd: dup() in/out failed");
                    904:
                    905:        if ((c = channel_connect_stdio_fwd(host_to_connect, port_to_connect,
                    906:            in, out)) == NULL)
1.331     dtucker   907:                return 0;
                    908:        channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
                    909:        return 1;
                    910: }
                    911:
                    912: static void
1.70      markus    913: ssh_init_forwarding(void)
                    914: {
1.86      markus    915:        int success = 0;
1.70      markus    916:        int i;
1.331     dtucker   917:
                    918:        if (stdio_forward_host != NULL) {
                    919:                if (!compat20) {
                    920:                        fatal("stdio forwarding require Protocol 2");
                    921:                }
                    922:                if (!client_setup_stdio_fwd(stdio_forward_host,
                    923:                    stdio_forward_port))
                    924:                        fatal("Failed to connect in stdio forward mode.");
                    925:        }
1.86      markus    926:
1.70      markus    927:        /* Initiate local TCP/IP port forwardings. */
                    928:        for (i = 0; i < options.num_local_forwards; i++) {
1.232     djm       929:                debug("Local connections to %.200s:%d forwarded to remote "
                    930:                    "address %.200s:%d",
1.234     deraadt   931:                    (options.local_forwards[i].listen_host == NULL) ?
                    932:                    (options.gateway_ports ? "*" : "LOCALHOST") :
1.232     djm       933:                    options.local_forwards[i].listen_host,
                    934:                    options.local_forwards[i].listen_port,
                    935:                    options.local_forwards[i].connect_host,
                    936:                    options.local_forwards[i].connect_port);
1.158     markus    937:                success += channel_setup_local_fwd_listener(
1.232     djm       938:                    options.local_forwards[i].listen_host,
                    939:                    options.local_forwards[i].listen_port,
                    940:                    options.local_forwards[i].connect_host,
                    941:                    options.local_forwards[i].connect_port,
1.70      markus    942:                    options.gateway_ports);
                    943:        }
1.283     markus    944:        if (i > 0 && success != i && options.exit_on_forward_failure)
                    945:                fatal("Could not request local forwarding.");
1.86      markus    946:        if (i > 0 && success == 0)
                    947:                error("Could not request local forwarding.");
1.70      markus    948:
                    949:        /* Initiate remote TCP/IP port forwardings. */
                    950:        for (i = 0; i < options.num_remote_forwards; i++) {
1.232     djm       951:                debug("Remote connections from %.200s:%d forwarded to "
                    952:                    "local address %.200s:%d",
1.248     djm       953:                    (options.remote_forwards[i].listen_host == NULL) ?
1.253     djm       954:                    "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232     djm       955:                    options.remote_forwards[i].listen_port,
                    956:                    options.remote_forwards[i].connect_host,
                    957:                    options.remote_forwards[i].connect_port);
1.283     markus    958:                if (channel_request_remote_forwarding(
1.232     djm       959:                    options.remote_forwards[i].listen_host,
                    960:                    options.remote_forwards[i].listen_port,
                    961:                    options.remote_forwards[i].connect_host,
1.283     markus    962:                    options.remote_forwards[i].connect_port) < 0) {
                    963:                        if (options.exit_on_forward_failure)
                    964:                                fatal("Could not request remote forwarding.");
                    965:                        else
                    966:                                logit("Warning: Could not request remote "
                    967:                                    "forwarding.");
                    968:                }
1.315     djm       969:                client_register_global_confirm(ssh_confirm_remote_forward,
                    970:                    &options.remote_forwards[i]);
1.70      markus    971:        }
1.301     djm       972:
                    973:        /* Initiate tunnel forwarding. */
                    974:        if (options.tun_open != SSH_TUNMODE_NO) {
                    975:                if (client_request_tun_fwd(options.tun_open,
                    976:                    options.tun_local, options.tun_remote) == -1) {
                    977:                        if (options.exit_on_forward_failure)
                    978:                                fatal("Could not request tunnel forwarding.");
                    979:                        else
                    980:                                error("Could not request tunnel forwarding.");
                    981:                }
                    982:        }
1.70      markus    983: }
                    984:
1.126     itojun    985: static void
1.70      markus    986: check_agent_present(void)
                    987: {
                    988:        if (options.forward_agent) {
1.254     djm       989:                /* Clear agent forwarding if we don't have an agent. */
1.185     stevesk   990:                if (!ssh_agent_present())
1.70      markus    991:                        options.forward_agent = 0;
                    992:        }
                    993: }
                    994:
1.126     itojun    995: static int
1.45      markus    996: ssh_session(void)
                    997: {
                    998:        int type;
                    999:        int interactive = 0;
                   1000:        int have_tty = 0;
                   1001:        struct winsize ws;
                   1002:        char *cp;
1.243     djm      1003:        const char *display;
1.45      markus   1004:
1.31      markus   1005:        /* Enable compression if requested. */
                   1006:        if (options.compression) {
1.316     djm      1007:                debug("Requesting compression at level %d.",
                   1008:                    options.compression_level);
1.31      markus   1009:
1.316     djm      1010:                if (options.compression_level < 1 ||
                   1011:                    options.compression_level > 9)
                   1012:                        fatal("Compression level must be from 1 (fast) to "
                   1013:                            "9 (slow, best).");
1.31      markus   1014:
                   1015:                /* Send the request. */
                   1016:                packet_start(SSH_CMSG_REQUEST_COMPRESSION);
                   1017:                packet_put_int(options.compression_level);
                   1018:                packet_send();
                   1019:                packet_write_wait();
1.156     markus   1020:                type = packet_read();
1.31      markus   1021:                if (type == SSH_SMSG_SUCCESS)
                   1022:                        packet_start_compression(options.compression_level);
                   1023:                else if (type == SSH_SMSG_FAILURE)
1.191     itojun   1024:                        logit("Warning: Remote host refused compression.");
1.31      markus   1025:                else
1.316     djm      1026:                        packet_disconnect("Protocol error waiting for "
                   1027:                            "compression response.");
1.31      markus   1028:        }
                   1029:        /* Allocate a pseudo tty if appropriate. */
                   1030:        if (tty_flag) {
                   1031:                debug("Requesting pty.");
                   1032:
                   1033:                /* Start the packet. */
                   1034:                packet_start(SSH_CMSG_REQUEST_PTY);
                   1035:
                   1036:                /* Store TERM in the packet.  There is no limit on the
                   1037:                   length of the string. */
                   1038:                cp = getenv("TERM");
                   1039:                if (!cp)
                   1040:                        cp = "";
1.124     markus   1041:                packet_put_cstring(cp);
1.31      markus   1042:
                   1043:                /* Store window size in the packet. */
                   1044:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                   1045:                        memset(&ws, 0, sizeof(ws));
1.269     deraadt  1046:                packet_put_int((u_int)ws.ws_row);
                   1047:                packet_put_int((u_int)ws.ws_col);
                   1048:                packet_put_int((u_int)ws.ws_xpixel);
                   1049:                packet_put_int((u_int)ws.ws_ypixel);
1.31      markus   1050:
                   1051:                /* Store tty modes in the packet. */
1.115     stevesk  1052:                tty_make_modes(fileno(stdin), NULL);
1.31      markus   1053:
                   1054:                /* Send the packet, and wait for it to leave. */
                   1055:                packet_send();
                   1056:                packet_write_wait();
                   1057:
                   1058:                /* Read response from the server. */
1.156     markus   1059:                type = packet_read();
1.43      markus   1060:                if (type == SSH_SMSG_SUCCESS) {
1.31      markus   1061:                        interactive = 1;
1.45      markus   1062:                        have_tty = 1;
1.43      markus   1063:                } else if (type == SSH_SMSG_FAILURE)
1.316     djm      1064:                        logit("Warning: Remote host failed or refused to "
                   1065:                            "allocate a pseudo tty.");
1.31      markus   1066:                else
1.316     djm      1067:                        packet_disconnect("Protocol error waiting for pty "
                   1068:                            "request response.");
1.31      markus   1069:        }
                   1070:        /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243     djm      1071:        display = getenv("DISPLAY");
                   1072:        if (options.forward_x11 && display != NULL) {
1.150     stevesk  1073:                char *proto, *data;
1.50      markus   1074:                /* Get reasonable local authentication information. */
1.243     djm      1075:                client_x11_get_proto(display, options.xauth_location,
                   1076:                    options.forward_x11_trusted, &proto, &data);
1.50      markus   1077:                /* Request forwarding with authentication spoofing. */
1.316     djm      1078:                debug("Requesting X11 forwarding with authentication "
                   1079:                    "spoofing.");
1.243     djm      1080:                x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31      markus   1081:
                   1082:                /* Read response from the server. */
1.156     markus   1083:                type = packet_read();
1.31      markus   1084:                if (type == SSH_SMSG_SUCCESS) {
                   1085:                        interactive = 1;
1.50      markus   1086:                } else if (type == SSH_SMSG_FAILURE) {
1.191     itojun   1087:                        logit("Warning: Remote host denied X11 forwarding.");
1.50      markus   1088:                } else {
1.316     djm      1089:                        packet_disconnect("Protocol error waiting for X11 "
                   1090:                            "forwarding");
1.50      markus   1091:                }
1.31      markus   1092:        }
                   1093:        /* Tell the packet module whether this is an interactive session. */
1.80      markus   1094:        packet_set_interactive(interactive);
1.31      markus   1095:
                   1096:        /* Request authentication agent forwarding if appropriate. */
1.70      markus   1097:        check_agent_present();
                   1098:
1.31      markus   1099:        if (options.forward_agent) {
                   1100:                debug("Requesting authentication agent forwarding.");
                   1101:                auth_request_forwarding();
                   1102:
                   1103:                /* Read response from the server. */
1.156     markus   1104:                type = packet_read();
1.155     markus   1105:                packet_check_eom();
1.31      markus   1106:                if (type != SSH_SMSG_SUCCESS)
1.191     itojun   1107:                        logit("Warning: Remote host denied authentication agent forwarding.");
1.31      markus   1108:        }
                   1109:
1.70      markus   1110:        /* Initiate port forwardings. */
                   1111:        ssh_init_forwarding();
1.305     dtucker  1112:
                   1113:        /* Execute a local command */
                   1114:        if (options.local_command != NULL &&
                   1115:            options.permit_local_command)
                   1116:                ssh_local_cmd(options.local_command);
1.34      markus   1117:
1.318     djm      1118:        /*
                   1119:         * If requested and we are not interested in replies to remote
                   1120:         * forwarding requests, then let ssh continue in the background.
                   1121:         */
                   1122:        if (fork_after_authentication_flag &&
                   1123:            (!options.exit_on_forward_failure ||
                   1124:            options.num_remote_forwards == 0)) {
                   1125:                fork_after_authentication_flag = 0;
1.34      markus   1126:                if (daemon(1, 1) < 0)
                   1127:                        fatal("daemon() failed: %.200s", strerror(errno));
1.318     djm      1128:        }
1.31      markus   1129:
1.33      markus   1130:        /*
                   1131:         * If a command was specified on the command line, execute the
                   1132:         * command now. Otherwise request the server to start a shell.
                   1133:         */
1.31      markus   1134:        if (buffer_len(&command) > 0) {
                   1135:                int len = buffer_len(&command);
                   1136:                if (len > 900)
                   1137:                        len = 900;
1.316     djm      1138:                debug("Sending command: %.*s", len,
                   1139:                    (u_char *)buffer_ptr(&command));
1.31      markus   1140:                packet_start(SSH_CMSG_EXEC_CMD);
                   1141:                packet_put_string(buffer_ptr(&command), buffer_len(&command));
                   1142:                packet_send();
                   1143:                packet_write_wait();
                   1144:        } else {
                   1145:                debug("Requesting shell.");
                   1146:                packet_start(SSH_CMSG_EXEC_SHELL);
                   1147:                packet_send();
                   1148:                packet_write_wait();
                   1149:        }
                   1150:
                   1151:        /* Enter the interactive session. */
1.119     stevesk  1152:        return client_loop(have_tty, tty_flag ?
                   1153:            options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.89      markus   1154: }
                   1155:
1.214     djm      1156: /* request pty/x11/agent/tcpfwd/shell for channel */
                   1157: static void
                   1158: ssh_session2_setup(int id, void *arg)
                   1159: {
1.215     djm      1160:        extern char **environ;
1.243     djm      1161:        const char *display;
                   1162:        int interactive = tty_flag;
1.215     djm      1163:
1.248     djm      1164:        display = getenv("DISPLAY");
1.243     djm      1165:        if (options.forward_x11 && display != NULL) {
1.150     stevesk  1166:                char *proto, *data;
1.50      markus   1167:                /* Get reasonable local authentication information. */
1.243     djm      1168:                client_x11_get_proto(display, options.xauth_location,
                   1169:                    options.forward_x11_trusted, &proto, &data);
1.50      markus   1170:                /* Request forwarding with authentication spoofing. */
1.316     djm      1171:                debug("Requesting X11 forwarding with authentication "
                   1172:                    "spoofing.");
1.243     djm      1173:                x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80      markus   1174:                interactive = 1;
1.50      markus   1175:                /* XXX wait for reply */
                   1176:        }
                   1177:
1.70      markus   1178:        check_agent_present();
                   1179:        if (options.forward_agent) {
                   1180:                debug("Requesting authentication agent forwarding.");
                   1181:                channel_request_start(id, "auth-agent-req@openssh.com", 0);
                   1182:                packet_send();
1.212     djm      1183:        }
                   1184:
1.214     djm      1185:        client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.311     djm      1186:            NULL, fileno(stdin), &command, environ);
1.90      markus   1187:
1.80      markus   1188:        packet_set_interactive(interactive);
1.45      markus   1189: }
                   1190:
1.143     markus   1191: /* open new channel for a session */
1.126     itojun   1192: static int
1.143     markus   1193: ssh_session2_open(void)
1.45      markus   1194: {
1.118     markus   1195:        Channel *c;
                   1196:        int window, packetmax, in, out, err;
1.60      markus   1197:
1.62      markus   1198:        if (stdin_null_flag) {
1.93      itojun   1199:                in = open(_PATH_DEVNULL, O_RDONLY);
1.62      markus   1200:        } else {
                   1201:                in = dup(STDIN_FILENO);
                   1202:        }
1.60      markus   1203:        out = dup(STDOUT_FILENO);
                   1204:        err = dup(STDERR_FILENO);
1.45      markus   1205:
                   1206:        if (in < 0 || out < 0 || err < 0)
1.62      markus   1207:                fatal("dup() in/out/err failed");
1.45      markus   1208:
1.69      markus   1209:        /* enable nonblocking unless tty */
                   1210:        if (!isatty(in))
                   1211:                set_nonblock(in);
                   1212:        if (!isatty(out))
                   1213:                set_nonblock(out);
                   1214:        if (!isatty(err))
                   1215:                set_nonblock(err);
                   1216:
1.65      markus   1217:        window = CHAN_SES_WINDOW_DEFAULT;
                   1218:        packetmax = CHAN_SES_PACKET_DEFAULT;
1.164     markus   1219:        if (tty_flag) {
                   1220:                window >>= 1;
                   1221:                packetmax >>= 1;
1.45      markus   1222:        }
1.118     markus   1223:        c = channel_new(
1.45      markus   1224:            "session", SSH_CHANNEL_OPENING, in, out, err,
1.65      markus   1225:            window, packetmax, CHAN_EXTENDED_WRITE,
1.192     markus   1226:            "client-session", /*nonblock*/0);
1.45      markus   1227:
1.143     markus   1228:        debug3("ssh_session2_open: channel_new: %d", c->self);
1.106     markus   1229:
1.122     markus   1230:        channel_send_open(c->self);
1.143     markus   1231:        if (!no_shell_flag)
1.310     djm      1232:                channel_register_open_confirm(c->self,
                   1233:                    ssh_session2_setup, NULL);
1.106     markus   1234:
1.118     markus   1235:        return c->self;
1.106     markus   1236: }
                   1237:
1.126     itojun   1238: static int
1.106     markus   1239: ssh_session2(void)
                   1240: {
1.143     markus   1241:        int id = -1;
1.106     markus   1242:
                   1243:        /* XXX should be pre-session */
                   1244:        ssh_init_forwarding();
                   1245:
1.143     markus   1246:        if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
                   1247:                id = ssh_session2_open();
1.314     djm      1248:
                   1249:        /* If we don't expect to open a new session, then disallow it */
1.319     markus   1250:        if (options.control_master == SSHCTL_MASTER_NO &&
                   1251:            (datafellows & SSH_NEW_OPENSSH)) {
1.314     djm      1252:                debug("Requesting no-more-sessions@openssh.com");
                   1253:                packet_start(SSH2_MSG_GLOBAL_REQUEST);
                   1254:                packet_put_cstring("no-more-sessions@openssh.com");
                   1255:                packet_put_char(0);
                   1256:                packet_send();
                   1257:        }
1.255     reyk     1258:
                   1259:        /* Execute a local command */
                   1260:        if (options.local_command != NULL &&
                   1261:            options.permit_local_command)
                   1262:                ssh_local_cmd(options.local_command);
1.301     djm      1263:
                   1264:        /* Start listening for multiplex clients */
1.312     djm      1265:        muxserver_listen();
1.106     markus   1266:
                   1267:        /* If requested, let ssh continue in the background. */
1.318     djm      1268:        if (fork_after_authentication_flag) {
                   1269:                fork_after_authentication_flag = 0;
1.106     markus   1270:                if (daemon(1, 1) < 0)
                   1271:                        fatal("daemon() failed: %.200s", strerror(errno));
1.318     djm      1272:        }
1.327     andreas  1273:
                   1274:        if (options.use_roaming)
                   1275:                request_roaming();
1.31      markus   1276:
1.119     stevesk  1277:        return client_loop(tty_flag, tty_flag ?
                   1278:            options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72      markus   1279: }
                   1280:
1.126     itojun   1281: static void
1.104     markus   1282: load_public_identity_files(void)
                   1283: {
1.275     djm      1284:        char *filename, *cp, thishost[NI_MAXHOST];
1.306     deraadt  1285:        char *pwdir = NULL, *pwname = NULL;
1.167     markus   1286:        int i = 0;
1.104     markus   1287:        Key *public;
1.275     djm      1288:        struct passwd *pw;
1.333   ! markus   1289: #ifdef ENABLE_PKCS11
1.167     markus   1290:        Key **keys;
1.333   ! markus   1291:        int nkeys;
1.104     markus   1292:
1.333   ! markus   1293:        if (options.pkcs11_provider != NULL &&
1.167     markus   1294:            options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.333   ! markus   1295:            (pkcs11_init(!options.batch_mode) == 0) &&
        !          1296:            (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
        !          1297:            &keys)) > 0) {
1.167     markus   1298:                int count = 0;
1.333   ! markus   1299:                for (i = 0; i < nkeys; i++) {
1.167     markus   1300:                        count++;
1.316     djm      1301:                        memmove(&options.identity_files[1],
                   1302:                            &options.identity_files[0],
1.167     markus   1303:                            sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1.316     djm      1304:                        memmove(&options.identity_keys[1],
                   1305:                            &options.identity_keys[0],
1.167     markus   1306:                            sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
                   1307:                        options.num_identity_files++;
                   1308:                        options.identity_keys[0] = keys[i];
1.333   ! markus   1309:                        options.identity_files[0] =
        !          1310:                            xstrdup(options.pkcs11_provider); /* XXX */
1.167     markus   1311:                }
1.168     markus   1312:                if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
                   1313:                        options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167     markus   1314:                i = count;
                   1315:                xfree(keys);
1.333   ! markus   1316:                /* XXX leaks some keys */
1.127     markus   1317:        }
1.333   ! markus   1318: #endif /* ENABLE_PKCS11 */
1.275     djm      1319:        if ((pw = getpwuid(original_real_uid)) == NULL)
                   1320:                fatal("load_public_identity_files: getpwuid failed");
1.307     dtucker  1321:        pwname = xstrdup(pw->pw_name);
                   1322:        pwdir = xstrdup(pw->pw_dir);
1.275     djm      1323:        if (gethostname(thishost, sizeof(thishost)) == -1)
                   1324:                fatal("load_public_identity_files: gethostname: %s",
                   1325:                    strerror(errno));
1.131     millert  1326:        for (; i < options.num_identity_files; i++) {
1.275     djm      1327:                cp = tilde_expand_filename(options.identity_files[i],
1.131     millert  1328:                    original_real_uid);
1.306     deraadt  1329:                filename = percent_expand(cp, "d", pwdir,
                   1330:                    "u", pwname, "l", thishost, "h", host,
1.275     djm      1331:                    "r", options.user, (char *)NULL);
                   1332:                xfree(cp);
1.131     millert  1333:                public = key_load_public(filename, NULL);
                   1334:                debug("identity file %s type %d", filename,
                   1335:                    public ? public->type : -1);
                   1336:                xfree(options.identity_files[i]);
                   1337:                options.identity_files[i] = filename;
                   1338:                options.identity_keys[i] = public;
                   1339:        }
1.306     deraadt  1340:        bzero(pwname, strlen(pwname));
1.307     dtucker  1341:        xfree(pwname);
1.306     deraadt  1342:        bzero(pwdir, strlen(pwdir));
1.307     dtucker  1343:        xfree(pwdir);
1.214     djm      1344: }