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

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