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

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