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

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