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

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