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

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