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

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