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

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