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

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