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

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