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

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.116   ! markus     42: RCSID("$OpenBSD: ssh.c,v 1.115 2001/04/14 16:33:20 stevesk 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.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.31      markus    151:        fprintf(stderr, "  -a          Disable authentication agent forwarding.\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.31      markus    156:        fprintf(stderr, "  -x          Disable X11 connection forwarding.\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:
                    169:        fprintf(stderr, "  -c cipher   Select encryption algorithm: "
1.99      deraadt   170:            "``3des'', ``blowfish''\n");
1.102     stevesk   171:        fprintf(stderr, "  -m macs     Specify MAC algorithms for protocol version 2.\n");
1.31      markus    172:        fprintf(stderr, "  -p port     Connect to this port.  Server must be on the same port.\n");
                    173:        fprintf(stderr, "  -L listen-port:host:port   Forward local port to remote address\n");
                    174:        fprintf(stderr, "  -R listen-port:host:port   Forward remote port to local address\n");
1.76      markus    175:        fprintf(stderr, "              These cause %s to listen for connections on a port, and\n", __progname);
1.31      markus    176:        fprintf(stderr, "              forward them to the other side by connecting to host:port.\n");
                    177:        fprintf(stderr, "  -C          Enable compression.\n");
1.45      markus    178:        fprintf(stderr, "  -N          Do not execute a shell or command.\n");
1.31      markus    179:        fprintf(stderr, "  -g          Allow remote hosts to connect to forwarded ports.\n");
1.92      jakob     180:        fprintf(stderr, "  -1          Force protocol version 1.\n");
                    181:        fprintf(stderr, "  -2          Force protocol version 2.\n");
1.37      markus    182:        fprintf(stderr, "  -4          Use IPv4 only.\n");
                    183:        fprintf(stderr, "  -6          Use IPv6 only.\n");
1.31      markus    184:        fprintf(stderr, "  -o 'option' Process the option as if it was read from a configuration file.\n");
1.85      djm       185:        fprintf(stderr, "  -s          Invoke command (mandatory) as SSH2 subsystem.\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.113     stevesk   239:        char *optarg, *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;
                    295:                        if ((cp = strchr(av[optind], '@'))) {
1.48      markus    296:                                if(cp == av[optind])
                    297:                                        usage();
1.31      markus    298:                                options.user = av[optind];
                    299:                                *cp = '\0';
                    300:                                host = ++cp;
                    301:                        } else
                    302:                                host = av[optind];
                    303:                        continue;
                    304:                }
                    305:                opt = av[optind][1];
                    306:                if (!opt)
                    307:                        usage();
1.108     markus    308:                if (strchr("eilcmpLRDo", opt)) {   /* options with arguments */
1.31      markus    309:                        optarg = av[optind] + 2;
                    310:                        if (strcmp(optarg, "") == 0) {
                    311:                                if (optind >= ac - 1)
                    312:                                        usage();
                    313:                                optarg = av[++optind];
                    314:                        }
                    315:                } else {
                    316:                        if (av[optind][2])
                    317:                                usage();
                    318:                        optarg = NULL;
                    319:                }
                    320:                switch (opt) {
1.91      jakob     321:                case '1':
                    322:                        options.protocol = SSH_PROTO_1;
                    323:                        break;
1.47      markus    324:                case '2':
                    325:                        options.protocol = SSH_PROTO_2;
                    326:                        break;
1.37      markus    327:                case '4':
                    328:                        IPv4or6 = AF_INET;
                    329:                        break;
                    330:                case '6':
                    331:                        IPv4or6 = AF_INET6;
                    332:                        break;
1.31      markus    333:                case 'n':
                    334:                        stdin_null_flag = 1;
                    335:                        break;
                    336:                case 'f':
                    337:                        fork_after_authentication_flag = 1;
                    338:                        stdin_null_flag = 1;
                    339:                        break;
                    340:                case 'x':
                    341:                        options.forward_x11 = 0;
                    342:                        break;
                    343:                case 'X':
                    344:                        options.forward_x11 = 1;
                    345:                        break;
                    346:                case 'g':
                    347:                        options.gateway_ports = 1;
                    348:                        break;
                    349:                case 'P':
                    350:                        options.use_privileged_port = 0;
                    351:                        break;
                    352:                case 'a':
                    353:                        options.forward_agent = 0;
1.53      markus    354:                        break;
                    355:                case 'A':
                    356:                        options.forward_agent = 1;
1.31      markus    357:                        break;
1.9       dugsong   358: #ifdef AFS
1.31      markus    359:                case 'k':
                    360:                        options.kerberos_tgt_passing = 0;
                    361:                        options.afs_token_passing = 0;
                    362:                        break;
1.1       deraadt   363: #endif
1.31      markus    364:                case 'i':
                    365:                        if (stat(optarg, &st) < 0) {
                    366:                                fprintf(stderr, "Warning: Identity file %s does not exist.\n",
1.72      markus    367:                                    optarg);
1.31      markus    368:                                break;
                    369:                        }
                    370:                        if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
                    371:                                fatal("Too many identity files specified (max %d)",
1.72      markus    372:                                    SSH_MAX_IDENTITY_FILES);
                    373:                        options.identity_files[options.num_identity_files++] = xstrdup(optarg);
1.31      markus    374:                        break;
                    375:                case 't':
1.79      markus    376:                        if (tty_flag)
                    377:                                force_tty_flag = 1;
1.31      markus    378:                        tty_flag = 1;
                    379:                        break;
                    380:                case 'v':
1.66      markus    381:                        if (0 == debug_flag) {
                    382:                                debug_flag = 1;
                    383:                                options.log_level = SYSLOG_LEVEL_DEBUG1;
                    384:                        } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
                    385:                                options.log_level++;
                    386:                                break;
                    387:                        } else {
1.103     millert   388:                                fatal("Too high debugging level.");
1.66      markus    389:                        }
                    390:                        /* fallthrough */
1.31      markus    391:                case 'V':
1.96      deraadt   392:                        fprintf(stderr,
                    393:                            "%s, SSH protocols %d.%d/%d.%d, OpenSSL 0x%8.8lx\n",
1.46      markus    394:                            SSH_VERSION,
                    395:                            PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
1.96      deraadt   396:                            PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2,
                    397:                            SSLeay());
1.31      markus    398:                        if (opt == 'V')
                    399:                                exit(0);
                    400:                        break;
                    401:                case 'q':
                    402:                        options.log_level = SYSLOG_LEVEL_QUIET;
                    403:                        break;
                    404:                case 'e':
                    405:                        if (optarg[0] == '^' && optarg[2] == 0 &&
1.78      markus    406:                            (u_char) optarg[1] >= 64 && (u_char) optarg[1] < 128)
                    407:                                options.escape_char = (u_char) optarg[1] & 31;
1.31      markus    408:                        else if (strlen(optarg) == 1)
1.78      markus    409:                                options.escape_char = (u_char) optarg[0];
1.31      markus    410:                        else if (strcmp(optarg, "none") == 0)
                    411:                                options.escape_char = -2;
                    412:                        else {
                    413:                                fprintf(stderr, "Bad escape character '%s'.\n", optarg);
                    414:                                exit(1);
                    415:                        }
                    416:                        break;
                    417:                case 'c':
1.49      markus    418:                        if (ciphers_valid(optarg)) {
                    419:                                /* SSH2 only */
                    420:                                options.ciphers = xstrdup(optarg);
1.51      markus    421:                                options.cipher = SSH_CIPHER_ILLEGAL;
1.49      markus    422:                        } else {
                    423:                                /* SSH1 only */
1.74      markus    424:                                options.cipher = cipher_number(optarg);
                    425:                                if (options.cipher == -1) {
1.49      markus    426:                                        fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
                    427:                                        exit(1);
                    428:                                }
1.74      markus    429:                                if (options.cipher == SSH_CIPHER_3DES) {
                    430:                                        options.ciphers = "3des-cbc";
                    431:                                } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
                    432:                                        options.ciphers = "blowfish-cbc";
                    433:                                } else {
                    434:                                        options.ciphers = (char *)-1;
                    435:                                }
1.95      markus    436:                        }
                    437:                        break;
                    438:                case 'm':
                    439:                        if (mac_valid(optarg))
                    440:                                options.macs = xstrdup(optarg);
                    441:                        else {
                    442:                                fprintf(stderr, "Unknown mac type '%s'\n", optarg);
                    443:                                exit(1);
1.31      markus    444:                        }
                    445:                        break;
                    446:                case 'p':
1.113     stevesk   447:                        options.port = a2port(optarg);
                    448:                        if (options.port == 0) {
1.109     markus    449:                                fprintf(stderr, "Bad port '%s'\n", optarg);
                    450:                                exit(1);
                    451:                        }
1.31      markus    452:                        break;
                    453:                case 'l':
                    454:                        options.user = optarg;
                    455:                        break;
                    456:                case 'R':
1.37      markus    457:                        if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
                    458:                            &fwd_host_port) != 3 &&
                    459:                            sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
                    460:                            &fwd_host_port) != 3) {
1.31      markus    461:                                fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
                    462:                                usage();
                    463:                                /* NOTREACHED */
                    464:                        }
                    465:                        add_remote_forward(&options, fwd_port, buf, fwd_host_port);
                    466:                        break;
                    467:                case 'L':
1.37      markus    468:                        if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
                    469:                            &fwd_host_port) != 3 &&
                    470:                            sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
                    471:                            &fwd_host_port) != 3) {
1.31      markus    472:                                fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
                    473:                                usage();
                    474:                                /* NOTREACHED */
                    475:                        }
                    476:                        add_local_forward(&options, fwd_port, buf, fwd_host_port);
                    477:                        break;
1.108     markus    478:
                    479:                case 'D':
1.113     stevesk   480:                        fwd_port = a2port(optarg);
                    481:                        if (fwd_port == 0) {
                    482:                                fprintf(stderr, "Bad dynamic port '%s'\n", optarg);
1.109     markus    483:                                exit(1);
                    484:                        }
1.108     markus    485:                        add_local_forward(&options, fwd_port, "socks4", 0);
                    486:                        break;
                    487:
1.31      markus    488:                case 'C':
                    489:                        options.compression = 1;
                    490:                        break;
1.45      markus    491:                case 'N':
                    492:                        no_shell_flag = 1;
                    493:                        no_tty_flag = 1;
                    494:                        break;
                    495:                case 'T':
                    496:                        no_tty_flag = 1;
                    497:                        break;
1.31      markus    498:                case 'o':
                    499:                        dummy = 1;
                    500:                        if (process_config_line(&options, host ? host : "", optarg,
                    501:                                         "command-line", 0, &dummy) != 0)
                    502:                                exit(1);
                    503:                        break;
1.85      djm       504:                case 's':
                    505:                        subsystem_flag = 1;
                    506:                        break;
1.31      markus    507:                default:
                    508:                        usage();
1.1       deraadt   509:                }
1.31      markus    510:        }
                    511:
                    512:        /* Check that we got a host name. */
                    513:        if (!host)
                    514:                usage();
                    515:
1.54      markus    516:        SSLeay_add_all_algorithms();
1.72      markus    517:        ERR_load_crypto_strings();
1.31      markus    518:
                    519:        /* Initialize the command to execute on remote host. */
                    520:        buffer_init(&command);
1.1       deraadt   521:
1.33      markus    522:        /*
                    523:         * Save the command to execute on the remote host in a buffer. There
                    524:         * is no limit on the length of the command, except by the maximum
                    525:         * packet size.  Also sets the tty flag if there is no command.
                    526:         */
1.31      markus    527:        if (optind == ac) {
                    528:                /* No command specified - execute shell on a tty. */
                    529:                tty_flag = 1;
1.85      djm       530:                if (subsystem_flag) {
1.114     stevesk   531:                        fprintf(stderr, "You must specify a subsystem to invoke.\n");
1.85      djm       532:                        usage();
                    533:                }
1.31      markus    534:        } else {
                    535:                /* A command has been specified.  Store it into the
                    536:                   buffer. */
                    537:                for (i = optind; i < ac; i++) {
                    538:                        if (i > optind)
                    539:                                buffer_append(&command, " ", 1);
                    540:                        buffer_append(&command, av[i], strlen(av[i]));
                    541:                }
                    542:        }
                    543:
                    544:        /* Cannot fork to background if no command. */
1.63      markus    545:        if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31      markus    546:                fatal("Cannot fork into background without a command to execute.");
                    547:
                    548:        /* Allocate a tty by default if no command specified. */
                    549:        if (buffer_len(&command) == 0)
                    550:                tty_flag = 1;
                    551:
1.75      markus    552:        /* Force no tty*/
                    553:        if (no_tty_flag)
                    554:                tty_flag = 0;
1.31      markus    555:        /* Do not allocate a tty if stdin is not a tty. */
1.79      markus    556:        if (!isatty(fileno(stdin)) && !force_tty_flag) {
1.31      markus    557:                if (tty_flag)
1.103     millert   558:                        log("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31      markus    559:                tty_flag = 0;
                    560:        }
1.45      markus    561:
1.101     markus    562:        /*
                    563:         * Initialize "log" output.  Since we are the client all output
                    564:         * actually goes to stderr.
                    565:         */
1.111     markus    566:        log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
                    567:            SYSLOG_FACILITY_USER, 1);
1.31      markus    568:
                    569:        /* Read per-user configuration file. */
1.83      markus    570:        snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_CONFFILE);
1.31      markus    571:        read_config_file(buf, host, &options);
                    572:
                    573:        /* Read systemwide configuration file. */
1.83      markus    574:        read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
1.31      markus    575:
                    576:        /* Fill configuration defaults. */
                    577:        fill_default_options(&options);
                    578:
                    579:        /* reinit */
1.101     markus    580:        log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31      markus    581:
                    582:        if (options.user == NULL)
                    583:                options.user = xstrdup(pw->pw_name);
                    584:
                    585:        if (options.hostname != NULL)
                    586:                host = options.hostname;
                    587:
                    588:        /* Disable rhosts authentication if not running as root. */
                    589:        if (original_effective_uid != 0 || !options.use_privileged_port) {
1.77      markus    590:                debug("Rhosts Authentication disabled, "
1.71      markus    591:                    "originating port will not be trusted.");
1.31      markus    592:                options.rhosts_authentication = 0;
                    593:        }
1.33      markus    594:        /*
                    595:         * If using rsh has been selected, exec it now (without trying
                    596:         * anything else).  Note that we must release privileges first.
                    597:         */
1.31      markus    598:        if (options.use_rsh) {
1.33      markus    599:                /*
                    600:                 * Restore our superuser privileges.  This must be done
                    601:                 * before permanently setting the uid.
                    602:                 */
1.31      markus    603:                restore_uid();
                    604:
                    605:                /* Switch to the original uid permanently. */
1.107     markus    606:                permanently_set_uid(pw);
1.31      markus    607:
                    608:                /* Execute rsh. */
                    609:                rsh_connect(host, options.user, &command);
                    610:                fatal("rsh_connect returned");
                    611:        }
                    612:        /* Restore our superuser privileges. */
                    613:        restore_uid();
                    614:
1.77      markus    615:        /* Open a connection to the remote host. */
1.31      markus    616:
                    617:        ok = ssh_connect(host, &hostaddr, options.port,
1.77      markus    618:            options.connection_attempts,
                    619:            original_effective_uid != 0 || !options.use_privileged_port,
1.107     markus    620:            pw, options.proxy_command);
1.31      markus    621:
1.33      markus    622:        /*
                    623:         * If we successfully made the connection, load the host private key
                    624:         * in case we will need it later for combined rsa-rhosts
                    625:         * authentication. This must be done before releasing extra
                    626:         * privileges, because the file is only readable by root.
                    627:         */
1.112     markus    628:        sensitive_data.nkeys = 0;
                    629:        sensitive_data.keys = NULL;
                    630:        if (ok && (options.rhosts_rsa_authentication ||
                    631:            options.hostbased_authentication)) {
                    632:                sensitive_data.nkeys = 3;
                    633:                sensitive_data.keys = xmalloc(sensitive_data.nkeys*sizeof(Key));
                    634:                sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.105     markus    635:                    _PATH_HOST_KEY_FILE, "", NULL);
1.112     markus    636:                sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
                    637:                    _PATH_HOST_DSA_KEY_FILE, "", NULL);
                    638:                sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
                    639:                    _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.31      markus    640:        }
1.33      markus    641:        /*
                    642:         * Get rid of any extra privileges that we may have.  We will no
                    643:         * longer need them.  Also, extra privileges could make it very hard
                    644:         * to read identity files and other non-world-readable files from the
                    645:         * user's home directory if it happens to be on a NFS volume where
                    646:         * root is mapped to nobody.
                    647:         */
                    648:
                    649:        /*
                    650:         * Note that some legacy systems need to postpone the following call
                    651:         * to permanently_set_uid() until the private hostkey is destroyed
                    652:         * with RSA_free().  Otherwise the calling user could ptrace() the
                    653:         * process, read the private hostkey and impersonate the host.
                    654:         * OpenBSD does not allow ptracing of setuid processes.
                    655:         */
1.107     markus    656:        permanently_set_uid(pw);
1.31      markus    657:
1.33      markus    658:        /*
                    659:         * Now that we are back to our own permissions, create ~/.ssh
                    660:         * directory if it doesn\'t already exist.
                    661:         */
1.83      markus    662:        snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_DIR);
1.31      markus    663:        if (stat(buf, &st) < 0)
1.57      djm       664:                if (mkdir(buf, 0700) < 0)
1.31      markus    665:                        error("Could not create directory '%.200s'.", buf);
                    666:
                    667:        /* Check if the connection failed, and try "rsh" if appropriate. */
                    668:        if (!ok) {
                    669:                if (options.port != 0)
1.35      markus    670:                        log("Secure connection to %.100s on port %hu refused%.100s.",
1.31      markus    671:                            host, options.port,
                    672:                            options.fallback_to_rsh ? "; reverting to insecure method" : "");
                    673:                else
                    674:                        log("Secure connection to %.100s refused%.100s.", host,
                    675:                            options.fallback_to_rsh ? "; reverting to insecure method" : "");
                    676:
                    677:                if (options.fallback_to_rsh) {
                    678:                        rsh_connect(host, options.user, &command);
                    679:                        fatal("rsh_connect returned");
                    680:                }
                    681:                exit(1);
                    682:        }
1.104     markus    683:        /* load options.identity_files */
                    684:        load_public_identity_files();
                    685:
                    686:        /* Expand ~ in known host file names. */
                    687:        /* XXX mem-leaks: */
1.72      markus    688:        options.system_hostfile =
                    689:            tilde_expand_filename(options.system_hostfile, original_real_uid);
                    690:        options.user_hostfile =
                    691:            tilde_expand_filename(options.user_hostfile, original_real_uid);
                    692:        options.system_hostfile2 =
                    693:            tilde_expand_filename(options.system_hostfile2, original_real_uid);
                    694:        options.user_hostfile2 =
                    695:            tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.31      markus    696:
                    697:        /* Log into the remote system.  This never returns if the login fails. */
1.112     markus    698:        ssh_login(sensitive_data.keys, sensitive_data.nkeys,
                    699:            host, (struct sockaddr *)&hostaddr, pw);
1.31      markus    700:
1.112     markus    701:        /* We no longer need the private host keys.  Clear them now. */
                    702:        if (sensitive_data.nkeys != 0) {
                    703:                for (i = 0; i < sensitive_data.nkeys; i++) {
                    704:                        if (sensitive_data.keys[i] != NULL) {
                    705:                                /* Destroys contents safely */
                    706:                                debug3("clear hostkey %d", i);
                    707:                                key_free(sensitive_data.keys[i]);
                    708:                                sensitive_data.keys[i] = NULL;
                    709:                        }
                    710:                }
                    711:                xfree(sensitive_data.keys);
                    712:        }
1.31      markus    713:
1.45      markus    714:        exit_status = compat20 ? ssh_session2() : ssh_session();
                    715:        packet_close();
                    716:        return exit_status;
                    717: }
                    718:
1.50      markus    719: void
                    720: x11_get_proto(char *proto, int proto_len, char *data, int data_len)
                    721: {
                    722:        char line[512];
                    723:        FILE *f;
                    724:        int got_data = 0, i;
                    725:
1.55      markus    726:        if (options.xauth_location) {
                    727:                /* Try to get Xauthority information for the display. */
1.93      itojun    728:                snprintf(line, sizeof line, "%.100s list %.200s 2>" _PATH_DEVNULL,
1.55      markus    729:                    options.xauth_location, getenv("DISPLAY"));
                    730:                f = popen(line, "r");
                    731:                if (f && fgets(line, sizeof(line), f) &&
                    732:                    sscanf(line, "%*s %s %s", proto, data) == 2)
                    733:                        got_data = 1;
                    734:                if (f)
                    735:                        pclose(f);
                    736:        }
1.50      markus    737:        /*
                    738:         * If we didn't get authentication data, just make up some
                    739:         * data.  The forwarding code will check the validity of the
                    740:         * response anyway, and substitute this data.  The X11
                    741:         * server, however, will ignore this fake data and use
                    742:         * whatever authentication mechanisms it was using otherwise
                    743:         * for the local connection.
                    744:         */
                    745:        if (!got_data) {
                    746:                u_int32_t rand = 0;
                    747:
                    748:                strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
                    749:                for (i = 0; i < 16; i++) {
                    750:                        if (i % 4 == 0)
                    751:                                rand = arc4random();
                    752:                        snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
                    753:                        rand >>= 8;
                    754:                }
                    755:        }
                    756: }
                    757:
1.70      markus    758: void
                    759: ssh_init_forwarding(void)
                    760: {
1.86      markus    761:        int success = 0;
1.70      markus    762:        int i;
1.86      markus    763:
1.70      markus    764:        /* Initiate local TCP/IP port forwardings. */
                    765:        for (i = 0; i < options.num_local_forwards; i++) {
                    766:                debug("Connections to local port %d forwarded to remote address %.200s:%d",
                    767:                    options.local_forwards[i].port,
                    768:                    options.local_forwards[i].host,
                    769:                    options.local_forwards[i].host_port);
1.86      markus    770:                success += channel_request_local_forwarding(
1.70      markus    771:                    options.local_forwards[i].port,
                    772:                    options.local_forwards[i].host,
                    773:                    options.local_forwards[i].host_port,
                    774:                    options.gateway_ports);
                    775:        }
1.86      markus    776:        if (i > 0 && success == 0)
                    777:                error("Could not request local forwarding.");
1.70      markus    778:
                    779:        /* Initiate remote TCP/IP port forwardings. */
                    780:        for (i = 0; i < options.num_remote_forwards; i++) {
                    781:                debug("Connections to remote port %d forwarded to local address %.200s:%d",
                    782:                    options.remote_forwards[i].port,
                    783:                    options.remote_forwards[i].host,
                    784:                    options.remote_forwards[i].host_port);
                    785:                channel_request_remote_forwarding(
                    786:                    options.remote_forwards[i].port,
                    787:                    options.remote_forwards[i].host,
                    788:                    options.remote_forwards[i].host_port);
                    789:        }
                    790: }
                    791:
                    792: void
                    793: check_agent_present(void)
                    794: {
                    795:        if (options.forward_agent) {
                    796:                /* Clear agent forwarding if we don\'t have an agent. */
                    797:                int authfd = ssh_get_authentication_socket();
                    798:                if (authfd < 0)
                    799:                        options.forward_agent = 0;
                    800:                else
                    801:                        ssh_close_authentication_socket(authfd);
                    802:        }
                    803: }
                    804:
1.45      markus    805: int
                    806: ssh_session(void)
                    807: {
                    808:        int type;
                    809:        int plen;
                    810:        int interactive = 0;
                    811:        int have_tty = 0;
                    812:        struct winsize ws;
                    813:        char *cp;
                    814:
1.31      markus    815:        /* Enable compression if requested. */
                    816:        if (options.compression) {
                    817:                debug("Requesting compression at level %d.", options.compression_level);
                    818:
                    819:                if (options.compression_level < 1 || options.compression_level > 9)
                    820:                        fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
                    821:
                    822:                /* Send the request. */
                    823:                packet_start(SSH_CMSG_REQUEST_COMPRESSION);
                    824:                packet_put_int(options.compression_level);
                    825:                packet_send();
                    826:                packet_write_wait();
                    827:                type = packet_read(&plen);
                    828:                if (type == SSH_SMSG_SUCCESS)
                    829:                        packet_start_compression(options.compression_level);
                    830:                else if (type == SSH_SMSG_FAILURE)
                    831:                        log("Warning: Remote host refused compression.");
                    832:                else
                    833:                        packet_disconnect("Protocol error waiting for compression response.");
                    834:        }
                    835:        /* Allocate a pseudo tty if appropriate. */
                    836:        if (tty_flag) {
                    837:                debug("Requesting pty.");
                    838:
                    839:                /* Start the packet. */
                    840:                packet_start(SSH_CMSG_REQUEST_PTY);
                    841:
                    842:                /* Store TERM in the packet.  There is no limit on the
                    843:                   length of the string. */
                    844:                cp = getenv("TERM");
                    845:                if (!cp)
                    846:                        cp = "";
                    847:                packet_put_string(cp, strlen(cp));
                    848:
                    849:                /* Store window size in the packet. */
                    850:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                    851:                        memset(&ws, 0, sizeof(ws));
                    852:                packet_put_int(ws.ws_row);
                    853:                packet_put_int(ws.ws_col);
                    854:                packet_put_int(ws.ws_xpixel);
                    855:                packet_put_int(ws.ws_ypixel);
                    856:
                    857:                /* Store tty modes in the packet. */
1.115     stevesk   858:                tty_make_modes(fileno(stdin), NULL);
1.31      markus    859:
                    860:                /* Send the packet, and wait for it to leave. */
                    861:                packet_send();
                    862:                packet_write_wait();
                    863:
                    864:                /* Read response from the server. */
                    865:                type = packet_read(&plen);
1.43      markus    866:                if (type == SSH_SMSG_SUCCESS) {
1.31      markus    867:                        interactive = 1;
1.45      markus    868:                        have_tty = 1;
1.43      markus    869:                } else if (type == SSH_SMSG_FAILURE)
1.31      markus    870:                        log("Warning: Remote host failed or refused to allocate a pseudo tty.");
                    871:                else
                    872:                        packet_disconnect("Protocol error waiting for pty request response.");
                    873:        }
                    874:        /* Request X11 forwarding if enabled and DISPLAY is set. */
                    875:        if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.50      markus    876:                char proto[512], data[512];
                    877:                /* Get reasonable local authentication information. */
                    878:                x11_get_proto(proto, sizeof proto, data, sizeof data);
                    879:                /* Request forwarding with authentication spoofing. */
1.31      markus    880:                debug("Requesting X11 forwarding with authentication spoofing.");
1.50      markus    881:                x11_request_forwarding_with_spoofing(0, proto, data);
1.31      markus    882:
                    883:                /* Read response from the server. */
                    884:                type = packet_read(&plen);
                    885:                if (type == SSH_SMSG_SUCCESS) {
                    886:                        interactive = 1;
1.50      markus    887:                } else if (type == SSH_SMSG_FAILURE) {
1.31      markus    888:                        log("Warning: Remote host denied X11 forwarding.");
1.50      markus    889:                } else {
1.31      markus    890:                        packet_disconnect("Protocol error waiting for X11 forwarding");
1.50      markus    891:                }
1.31      markus    892:        }
                    893:        /* Tell the packet module whether this is an interactive session. */
1.80      markus    894:        packet_set_interactive(interactive);
1.31      markus    895:
                    896:        /* Request authentication agent forwarding if appropriate. */
1.70      markus    897:        check_agent_present();
                    898:
1.31      markus    899:        if (options.forward_agent) {
                    900:                debug("Requesting authentication agent forwarding.");
                    901:                auth_request_forwarding();
                    902:
                    903:                /* Read response from the server. */
                    904:                type = packet_read(&plen);
                    905:                packet_integrity_check(plen, 0, type);
                    906:                if (type != SSH_SMSG_SUCCESS)
                    907:                        log("Warning: Remote host denied authentication agent forwarding.");
                    908:        }
                    909:
1.70      markus    910:        /* Initiate port forwardings. */
                    911:        ssh_init_forwarding();
1.34      markus    912:
                    913:        /* If requested, let ssh continue in the background. */
1.48      markus    914:        if (fork_after_authentication_flag)
1.34      markus    915:                if (daemon(1, 1) < 0)
                    916:                        fatal("daemon() failed: %.200s", strerror(errno));
1.31      markus    917:
1.33      markus    918:        /*
                    919:         * If a command was specified on the command line, execute the
                    920:         * command now. Otherwise request the server to start a shell.
                    921:         */
1.31      markus    922:        if (buffer_len(&command) > 0) {
                    923:                int len = buffer_len(&command);
                    924:                if (len > 900)
                    925:                        len = 900;
                    926:                debug("Sending command: %.*s", len, buffer_ptr(&command));
                    927:                packet_start(SSH_CMSG_EXEC_CMD);
                    928:                packet_put_string(buffer_ptr(&command), buffer_len(&command));
                    929:                packet_send();
                    930:                packet_write_wait();
                    931:        } else {
                    932:                debug("Requesting shell.");
                    933:                packet_start(SSH_CMSG_EXEC_SHELL);
                    934:                packet_send();
                    935:                packet_write_wait();
                    936:        }
                    937:
                    938:        /* Enter the interactive session. */
1.60      markus    939:        return client_loop(have_tty, tty_flag ? options.escape_char : -1, 0);
1.45      markus    940: }
                    941:
                    942: void
1.89      markus    943: client_subsystem_reply(int type, int plen, void *ctxt)
                    944: {
                    945:        int id, len;
                    946:
                    947:        id = packet_get_int();
                    948:        len = buffer_len(&command);
1.100     markus    949:        if (len > 900)
                    950:                len = 900;
1.89      markus    951:        packet_done();
                    952:        if (type == SSH2_MSG_CHANNEL_FAILURE)
                    953:                fatal("Request for subsystem '%.*s' failed on channel %d",
                    954:                    len, buffer_ptr(&command), id);
                    955: }
                    956:
                    957: void
1.70      markus    958: ssh_session2_callback(int id, void *arg)
1.45      markus    959: {
                    960:        int len;
1.80      markus    961:        int interactive = 0;
1.115     stevesk   962:        struct termios tio;
1.80      markus    963:
1.87      deraadt   964:        debug("client_init id %d arg %ld", id, (long)arg);
1.31      markus    965:
1.45      markus    966:        if (tty_flag) {
                    967:                struct winsize ws;
                    968:                char *cp;
                    969:                cp = getenv("TERM");
                    970:                if (!cp)
                    971:                        cp = "";
                    972:                /* Store window size in the packet. */
                    973:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                    974:                        memset(&ws, 0, sizeof(ws));
                    975:
                    976:                channel_request_start(id, "pty-req", 0);
                    977:                packet_put_cstring(cp);
                    978:                packet_put_int(ws.ws_col);
                    979:                packet_put_int(ws.ws_row);
                    980:                packet_put_int(ws.ws_xpixel);
                    981:                packet_put_int(ws.ws_ypixel);
1.115     stevesk   982:                tio = get_saved_tio();
                    983:                tty_make_modes(/*ignored*/ 0, &tio);
1.45      markus    984:                packet_send();
1.80      markus    985:                interactive = 1;
1.45      markus    986:                /* XXX wait for reply */
                    987:        }
1.50      markus    988:        if (options.forward_x11 &&
                    989:            getenv("DISPLAY") != NULL) {
                    990:                char proto[512], data[512];
                    991:                /* Get reasonable local authentication information. */
                    992:                x11_get_proto(proto, sizeof proto, data, sizeof data);
                    993:                /* Request forwarding with authentication spoofing. */
                    994:                debug("Requesting X11 forwarding with authentication spoofing.");
                    995:                x11_request_forwarding_with_spoofing(id, proto, data);
1.80      markus    996:                interactive = 1;
1.50      markus    997:                /* XXX wait for reply */
                    998:        }
                    999:
1.70      markus   1000:        check_agent_present();
                   1001:        if (options.forward_agent) {
                   1002:                debug("Requesting authentication agent forwarding.");
                   1003:                channel_request_start(id, "auth-agent-req@openssh.com", 0);
                   1004:                packet_send();
                   1005:        }
                   1006:
1.45      markus   1007:        len = buffer_len(&command);
                   1008:        if (len > 0) {
                   1009:                if (len > 900)
                   1010:                        len = 900;
1.85      djm      1011:                if (subsystem_flag) {
                   1012:                        debug("Sending subsystem: %.*s", len, buffer_ptr(&command));
1.89      markus   1013:                        channel_request_start(id, "subsystem", /*want reply*/ 1);
                   1014:                        /* register callback for reply */
                   1015:                        /* XXX we asume that client_loop has already been called */
                   1016:                        dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
                   1017:                        dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
1.85      djm      1018:                } else {
                   1019:                        debug("Sending command: %.*s", len, buffer_ptr(&command));
                   1020:                        channel_request_start(id, "exec", 0);
                   1021:                }
1.100     markus   1022:                packet_put_string(buffer_ptr(&command), buffer_len(&command));
1.45      markus   1023:                packet_send();
                   1024:        } else {
                   1025:                channel_request(id, "shell", 0);
                   1026:        }
                   1027:        /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
1.90      markus   1028:
1.45      markus   1029:        /* register different callback, etc. XXX */
1.80      markus   1030:        packet_set_interactive(interactive);
1.45      markus   1031: }
                   1032:
                   1033: int
1.106     markus   1034: ssh_session2_command(void)
1.45      markus   1035: {
1.106     markus   1036:        int id, window, packetmax;
1.60      markus   1037:        int in, out, err;
                   1038:
1.62      markus   1039:        if (stdin_null_flag) {
1.93      itojun   1040:                in = open(_PATH_DEVNULL, O_RDONLY);
1.62      markus   1041:        } else {
                   1042:                in = dup(STDIN_FILENO);
                   1043:        }
1.60      markus   1044:        out = dup(STDOUT_FILENO);
                   1045:        err = dup(STDERR_FILENO);
1.45      markus   1046:
                   1047:        if (in < 0 || out < 0 || err < 0)
1.62      markus   1048:                fatal("dup() in/out/err failed");
1.45      markus   1049:
1.69      markus   1050:        /* enable nonblocking unless tty */
                   1051:        if (!isatty(in))
                   1052:                set_nonblock(in);
                   1053:        if (!isatty(out))
                   1054:                set_nonblock(out);
                   1055:        if (!isatty(err))
                   1056:                set_nonblock(err);
                   1057:
1.65      markus   1058:        window = CHAN_SES_WINDOW_DEFAULT;
                   1059:        packetmax = CHAN_SES_PACKET_DEFAULT;
                   1060:        if (!tty_flag) {
1.45      markus   1061:                window *= 2;
1.65      markus   1062:                packetmax *=2;
1.45      markus   1063:        }
                   1064:        id = channel_new(
                   1065:            "session", SSH_CHANNEL_OPENING, in, out, err,
1.65      markus   1066:            window, packetmax, CHAN_EXTENDED_WRITE,
1.69      markus   1067:            xstrdup("client-session"), /*nonblock*/0);
1.45      markus   1068:
1.106     markus   1069: debug("channel_new: %d", id);
                   1070:
1.45      markus   1071:        channel_open(id);
1.70      markus   1072:        channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION,
                   1073:             ssh_session2_callback, (void *)0);
1.106     markus   1074:
                   1075:        return id;
                   1076: }
                   1077:
                   1078: int
                   1079: ssh_session2(void)
                   1080: {
                   1081:        int id;
                   1082:
                   1083:        /* XXX should be pre-session */
                   1084:        ssh_init_forwarding();
                   1085:
                   1086:        id = no_shell_flag ? -1 : ssh_session2_command();
                   1087:
                   1088:        /* If requested, let ssh continue in the background. */
                   1089:        if (fork_after_authentication_flag)
                   1090:                if (daemon(1, 1) < 0)
                   1091:                        fatal("daemon() failed: %.200s", strerror(errno));
1.31      markus   1092:
1.60      markus   1093:        return client_loop(tty_flag, tty_flag ? options.escape_char : -1, id);
1.72      markus   1094: }
                   1095:
1.104     markus   1096: void
                   1097: load_public_identity_files(void)
                   1098: {
                   1099:        char *filename;
                   1100:        Key *public;
                   1101:        int i;
                   1102:
                   1103:        for (i = 0; i < options.num_identity_files; i++) {
                   1104:                filename = tilde_expand_filename(options.identity_files[i],
                   1105:                    original_real_uid);
1.105     markus   1106:                public = key_load_public(filename, NULL);
1.104     markus   1107:                debug("identity file %s type %d", filename,
                   1108:                    public ? public->type : -1);
                   1109:                xfree(options.identity_files[i]);
                   1110:                options.identity_files[i] = filename;
                   1111:                options.identity_keys[i] = public;
                   1112:        }
1.1       deraadt  1113: }