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

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.
1.202     markus     16:  * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl.  All rights reserved.
1.64      deraadt    17:  *
                     18:  * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
                     19:  * in Canada (German citizen).
                     20:  *
                     21:  * Redistribution and use in source and binary forms, with or without
                     22:  * modification, are permitted provided that the following conditions
                     23:  * are met:
                     24:  * 1. Redistributions of source code must retain the above copyright
                     25:  *    notice, this list of conditions and the following disclaimer.
                     26:  * 2. Redistributions in binary form must reproduce the above copyright
                     27:  *    notice, this list of conditions and the following disclaimer in the
                     28:  *    documentation and/or other materials provided with the distribution.
                     29:  *
                     30:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     31:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     32:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     33:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     34:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     35:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     36:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     37:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     38:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     39:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32      deraadt    40:  */
1.1       deraadt    41:
                     42: #include "includes.h"
1.209   ! markus     43: RCSID("$OpenBSD: ssh.c,v 1.208 2004/03/10 09:45:06 markus Exp $");
1.49      markus     44:
                     45: #include <openssl/evp.h>
1.72      markus     46: #include <openssl/err.h>
1.1       deraadt    47:
1.84      markus     48: #include "ssh.h"
                     49: #include "ssh1.h"
                     50: #include "ssh2.h"
                     51: #include "compat.h"
                     52: #include "cipher.h"
1.1       deraadt    53: #include "xmalloc.h"
                     54: #include "packet.h"
                     55: #include "buffer.h"
1.123     markus     56: #include "channels.h"
1.49      markus     57: #include "key.h"
1.58      markus     58: #include "authfd.h"
1.49      markus     59: #include "authfile.h"
1.83      markus     60: #include "pathnames.h"
1.81      markus     61: #include "clientloop.h"
1.84      markus     62: #include "log.h"
                     63: #include "readconf.h"
                     64: #include "sshconnect.h"
                     65: #include "tildexpand.h"
1.89      markus     66: #include "dispatch.h"
1.84      markus     67: #include "misc.h"
1.95      markus     68: #include "kex.h"
                     69: #include "mac.h"
1.115     stevesk    70: #include "sshtty.h"
1.49      markus     71:
1.127     markus     72: #ifdef SMARTCARD
                     73: #include "scard.h"
1.137     jakob      74: #endif
1.127     markus     75:
1.49      markus     76: extern char *__progname;
1.1       deraadt    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
1.172     deraadt    97:  * so that the passphrase can be entered manually, and then ssh goes to the
1.33      markus     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.139     markus    108: /* optional user configfile */
                    109: char *config = NULL;
                    110:
1.33      markus    111: /*
                    112:  * Name of the host we are connecting to.  This is the name given on the
                    113:  * command line, or the HostName specified for the user-supplied name in a
                    114:  * configuration file.
                    115:  */
1.1       deraadt   116: char *host;
                    117:
1.22      provos    118: /* socket address the host resolves to */
1.37      markus    119: struct sockaddr_storage hostaddr;
1.1       deraadt   120:
1.112     markus    121: /* Private host keys. */
1.173     markus    122: Sensitive sensitive_data;
1.1       deraadt   123:
1.10      dugsong   124: /* Original real UID. */
                    125: uid_t original_real_uid;
1.177     markus    126: uid_t original_effective_uid;
1.1       deraadt   127:
1.45      markus    128: /* command to be executed */
                    129: Buffer command;
                    130:
1.85      djm       131: /* Should we execute a command or invoke a subsystem? */
                    132: int subsystem_flag = 0;
                    133:
1.170     markus    134: /* # of replies received for global requests */
                    135: static int client_global_request_id = 0;
                    136:
1.186     djm       137: /* pid of proxycommand child process */
                    138: pid_t proxy_command_pid = 0;
                    139:
1.1       deraadt   140: /* Prints a help message to the user.  This function never returns. */
                    141:
1.126     itojun    142: static void
1.93      itojun    143: usage(void)
1.1       deraadt   144: {
1.208     markus    145:        fprintf(stderr,
                    146: "usage: ssh [-1246AaCfghkNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
                    147: "           [-D port] [-e escape_char] [-F configfile] [-i identity_file]\n"
                    148: "           [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]\n"
                    149: "           [-p port] [-R port:host:hostport] [user@]hostname [command]\n"
                    150:        );
1.31      markus    151:        exit(1);
1.1       deraadt   152: }
                    153:
1.126     itojun    154: static int ssh_session(void);
                    155: static int ssh_session2(void);
                    156: static void load_public_identity_files(void);
1.45      markus    157:
1.32      deraadt   158: /*
                    159:  * Main program for the ssh client.
                    160:  */
1.2       provos    161: int
                    162: main(int ac, char **av)
1.1       deraadt   163: {
1.178     markus    164:        int i, opt, exit_status;
1.35      markus    165:        u_short fwd_port, fwd_host_port;
1.141     stevesk   166:        char sfwd_port[6], sfwd_host_port[6];
1.205     markus    167:        char *p, *cp, *line, buf[256];
1.31      markus    168:        struct stat st;
1.98      markus    169:        struct passwd *pw;
1.45      markus    170:        int dummy;
1.144     stevesk   171:        extern int optind, optreset;
                    172:        extern char *optarg;
1.31      markus    173:
1.33      markus    174:        /*
                    175:         * Save the original real uid.  It will be needed later (uid-swapping
                    176:         * may clobber the real uid).
                    177:         */
1.31      markus    178:        original_real_uid = getuid();
                    179:        original_effective_uid = geteuid();
                    180:
1.184     stevesk   181:        /*
                    182:         * Use uid-swapping to give up root privileges for the duration of
                    183:         * option processing.  We will re-instantiate the rights when we are
                    184:         * ready to create the privileged port, and will permanently drop
                    185:         * them when the port has been created (actually, when the connection
                    186:         * has been made, as we may need to create the port several times).
                    187:         */
                    188:        PRIV_END;
                    189:
1.31      markus    190:        /* If we are installed setuid root be careful to not drop core. */
                    191:        if (original_real_uid != original_effective_uid) {
                    192:                struct rlimit rlim;
                    193:                rlim.rlim_cur = rlim.rlim_max = 0;
                    194:                if (setrlimit(RLIMIT_CORE, &rlim) < 0)
                    195:                        fatal("setrlimit failed: %.100s", strerror(errno));
1.1       deraadt   196:        }
1.107     markus    197:        /* Get user data. */
                    198:        pw = getpwuid(original_real_uid);
                    199:        if (!pw) {
1.191     itojun    200:                logit("You don't exist, go away!");
1.107     markus    201:                exit(1);
                    202:        }
                    203:        /* Take a copy of the returned structure. */
                    204:        pw = pwcopy(pw);
1.31      markus    205:
1.33      markus    206:        /*
                    207:         * Set our umask to something reasonable, as some files are created
                    208:         * with the default umask.  This will make them world-readable but
                    209:         * writable only by the owner, which is ok for all files for which we
                    210:         * don't set the modes explicitly.
                    211:         */
1.31      markus    212:        umask(022);
                    213:
                    214:        /* Initialize option structure to indicate that no values have been set. */
                    215:        initialize_options(&options);
                    216:
                    217:        /* Parse command-line arguments. */
                    218:        host = NULL;
                    219:
1.128     fgsch     220: again:
                    221:        while ((opt = getopt(ac, av,
1.202     markus    222:            "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVXY")) != -1) {
1.31      markus    223:                switch (opt) {
1.91      jakob     224:                case '1':
                    225:                        options.protocol = SSH_PROTO_1;
                    226:                        break;
1.47      markus    227:                case '2':
                    228:                        options.protocol = SSH_PROTO_2;
                    229:                        break;
1.37      markus    230:                case '4':
1.196     djm       231:                        options.address_family = AF_INET;
1.37      markus    232:                        break;
                    233:                case '6':
1.196     djm       234:                        options.address_family = AF_INET6;
1.37      markus    235:                        break;
1.31      markus    236:                case 'n':
                    237:                        stdin_null_flag = 1;
                    238:                        break;
                    239:                case 'f':
                    240:                        fork_after_authentication_flag = 1;
                    241:                        stdin_null_flag = 1;
                    242:                        break;
                    243:                case 'x':
                    244:                        options.forward_x11 = 0;
                    245:                        break;
                    246:                case 'X':
                    247:                        options.forward_x11 = 1;
                    248:                        break;
1.202     markus    249:                case 'Y':
                    250:                        options.forward_x11 = 1;
                    251:                        options.forward_x11_trusted = 1;
                    252:                        break;
1.31      markus    253:                case 'g':
                    254:                        options.gateway_ports = 1;
                    255:                        break;
1.183     stevesk   256:                case 'P':       /* deprecated */
1.31      markus    257:                        options.use_privileged_port = 0;
                    258:                        break;
                    259:                case 'a':
                    260:                        options.forward_agent = 0;
1.53      markus    261:                        break;
                    262:                case 'A':
                    263:                        options.forward_agent = 1;
1.31      markus    264:                        break;
                    265:                case 'k':
1.204     dtucker   266:                        options.gss_deleg_creds = 0;
1.31      markus    267:                        break;
                    268:                case 'i':
                    269:                        if (stat(optarg, &st) < 0) {
1.128     fgsch     270:                                fprintf(stderr, "Warning: Identity file %s "
                    271:                                    "does not exist.\n", optarg);
1.31      markus    272:                                break;
                    273:                        }
1.128     fgsch     274:                        if (options.num_identity_files >=
                    275:                            SSH_MAX_IDENTITY_FILES)
                    276:                                fatal("Too many identity files specified "
                    277:                                    "(max %d)", SSH_MAX_IDENTITY_FILES);
                    278:                        options.identity_files[options.num_identity_files++] =
                    279:                            xstrdup(optarg);
1.31      markus    280:                        break;
1.127     markus    281:                case 'I':
                    282: #ifdef SMARTCARD
1.133     markus    283:                        options.smartcard_device = xstrdup(optarg);
1.137     jakob     284: #else
1.127     markus    285:                        fprintf(stderr, "no support for smartcards.\n");
1.137     jakob     286: #endif
1.127     markus    287:                        break;
1.31      markus    288:                case 't':
1.79      markus    289:                        if (tty_flag)
                    290:                                force_tty_flag = 1;
1.31      markus    291:                        tty_flag = 1;
                    292:                        break;
                    293:                case 'v':
1.197     markus    294:                        if (debug_flag == 0) {
1.66      markus    295:                                debug_flag = 1;
                    296:                                options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197     markus    297:                        } else {
                    298:                                if (options.log_level < SYSLOG_LEVEL_DEBUG3)
                    299:                                        options.log_level++;
1.66      markus    300:                                break;
1.197     markus    301:                        }
1.66      markus    302:                        /* fallthrough */
1.31      markus    303:                case 'V':
1.209   ! markus    304:                        fprintf(stderr, "%s, %s\n",
        !           305:                            SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31      markus    306:                        if (opt == 'V')
                    307:                                exit(0);
                    308:                        break;
                    309:                case 'q':
                    310:                        options.log_level = SYSLOG_LEVEL_QUIET;
                    311:                        break;
                    312:                case 'e':
                    313:                        if (optarg[0] == '^' && optarg[2] == 0 &&
1.128     fgsch     314:                            (u_char) optarg[1] >= 64 &&
                    315:                            (u_char) optarg[1] < 128)
1.78      markus    316:                                options.escape_char = (u_char) optarg[1] & 31;
1.31      markus    317:                        else if (strlen(optarg) == 1)
1.78      markus    318:                                options.escape_char = (u_char) optarg[0];
1.31      markus    319:                        else if (strcmp(optarg, "none") == 0)
1.119     stevesk   320:                                options.escape_char = SSH_ESCAPECHAR_NONE;
1.31      markus    321:                        else {
1.128     fgsch     322:                                fprintf(stderr, "Bad escape character '%s'.\n",
                    323:                                    optarg);
1.31      markus    324:                                exit(1);
                    325:                        }
                    326:                        break;
                    327:                case 'c':
1.49      markus    328:                        if (ciphers_valid(optarg)) {
                    329:                                /* SSH2 only */
                    330:                                options.ciphers = xstrdup(optarg);
1.51      markus    331:                                options.cipher = SSH_CIPHER_ILLEGAL;
1.49      markus    332:                        } else {
                    333:                                /* SSH1 only */
1.74      markus    334:                                options.cipher = cipher_number(optarg);
                    335:                                if (options.cipher == -1) {
1.128     fgsch     336:                                        fprintf(stderr,
                    337:                                            "Unknown cipher type '%s'\n",
                    338:                                            optarg);
1.49      markus    339:                                        exit(1);
                    340:                                }
1.128     fgsch     341:                                if (options.cipher == SSH_CIPHER_3DES)
1.74      markus    342:                                        options.ciphers = "3des-cbc";
1.128     fgsch     343:                                else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74      markus    344:                                        options.ciphers = "blowfish-cbc";
1.128     fgsch     345:                                else
1.74      markus    346:                                        options.ciphers = (char *)-1;
1.95      markus    347:                        }
                    348:                        break;
                    349:                case 'm':
                    350:                        if (mac_valid(optarg))
                    351:                                options.macs = xstrdup(optarg);
                    352:                        else {
1.128     fgsch     353:                                fprintf(stderr, "Unknown mac type '%s'\n",
                    354:                                    optarg);
1.95      markus    355:                                exit(1);
1.31      markus    356:                        }
                    357:                        break;
                    358:                case 'p':
1.113     stevesk   359:                        options.port = a2port(optarg);
                    360:                        if (options.port == 0) {
1.109     markus    361:                                fprintf(stderr, "Bad port '%s'\n", optarg);
                    362:                                exit(1);
                    363:                        }
1.31      markus    364:                        break;
                    365:                case 'l':
                    366:                        options.user = optarg;
                    367:                        break;
1.141     stevesk   368:
                    369:                case 'L':
1.31      markus    370:                case 'R':
1.141     stevesk   371:                        if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]",
                    372:                            sfwd_port, buf, sfwd_host_port) != 3 &&
                    373:                            sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]",
                    374:                            sfwd_port, buf, sfwd_host_port) != 3) {
1.128     fgsch     375:                                fprintf(stderr,
1.141     stevesk   376:                                    "Bad forwarding specification '%s'\n",
1.128     fgsch     377:                                    optarg);
1.31      markus    378:                                usage();
                    379:                                /* NOTREACHED */
                    380:                        }
1.141     stevesk   381:                        if ((fwd_port = a2port(sfwd_port)) == 0 ||
1.165     markus    382:                            (fwd_host_port = a2port(sfwd_host_port)) == 0) {
1.128     fgsch     383:                                fprintf(stderr,
1.141     stevesk   384:                                    "Bad forwarding port(s) '%s'\n", optarg);
                    385:                                exit(1);
1.31      markus    386:                        }
1.141     stevesk   387:                        if (opt == 'L')
                    388:                                add_local_forward(&options, fwd_port, buf,
                    389:                                    fwd_host_port);
                    390:                        else if (opt == 'R')
                    391:                                add_remote_forward(&options, fwd_port, buf,
1.151     deraadt   392:                                    fwd_host_port);
1.31      markus    393:                        break;
1.108     markus    394:
                    395:                case 'D':
1.113     stevesk   396:                        fwd_port = a2port(optarg);
                    397:                        if (fwd_port == 0) {
1.128     fgsch     398:                                fprintf(stderr, "Bad dynamic port '%s'\n",
                    399:                                    optarg);
1.109     markus    400:                                exit(1);
                    401:                        }
1.200     markus    402:                        add_local_forward(&options, fwd_port, "socks", 0);
1.108     markus    403:                        break;
                    404:
1.31      markus    405:                case 'C':
                    406:                        options.compression = 1;
                    407:                        break;
1.45      markus    408:                case 'N':
                    409:                        no_shell_flag = 1;
                    410:                        no_tty_flag = 1;
                    411:                        break;
                    412:                case 'T':
                    413:                        no_tty_flag = 1;
                    414:                        break;
1.31      markus    415:                case 'o':
                    416:                        dummy = 1;
1.205     markus    417:                        line = xstrdup(optarg);
1.128     fgsch     418:                        if (process_config_line(&options, host ? host : "",
1.205     markus    419:                            line, "command-line", 0, &dummy) != 0)
1.31      markus    420:                                exit(1);
1.205     markus    421:                        xfree(line);
1.31      markus    422:                        break;
1.85      djm       423:                case 's':
                    424:                        subsystem_flag = 1;
1.117     markus    425:                        break;
                    426:                case 'b':
                    427:                        options.bind_address = optarg;
1.85      djm       428:                        break;
1.139     markus    429:                case 'F':
                    430:                        config = optarg;
                    431:                        break;
1.31      markus    432:                default:
                    433:                        usage();
1.1       deraadt   434:                }
1.31      markus    435:        }
                    436:
1.128     fgsch     437:        ac -= optind;
                    438:        av += optind;
                    439:
                    440:        if (ac > 0 && !host && **av != '-') {
1.188     markus    441:                if (strrchr(*av, '@')) {
1.128     fgsch     442:                        p = xstrdup(*av);
1.188     markus    443:                        cp = strrchr(p, '@');
1.128     fgsch     444:                        if (cp == NULL || cp == p)
                    445:                                usage();
                    446:                        options.user = p;
                    447:                        *cp = '\0';
                    448:                        host = ++cp;
                    449:                } else
                    450:                        host = *av;
1.189     millert   451:                if (ac > 1) {
                    452:                        optind = optreset = 1;
1.128     fgsch     453:                        goto again;
                    454:                }
1.189     millert   455:                ac--, av++;
1.128     fgsch     456:        }
                    457:
1.31      markus    458:        /* Check that we got a host name. */
                    459:        if (!host)
                    460:                usage();
                    461:
1.54      markus    462:        SSLeay_add_all_algorithms();
1.72      markus    463:        ERR_load_crypto_strings();
1.31      markus    464:
                    465:        /* Initialize the command to execute on remote host. */
                    466:        buffer_init(&command);
1.1       deraadt   467:
1.33      markus    468:        /*
                    469:         * Save the command to execute on the remote host in a buffer. There
                    470:         * is no limit on the length of the command, except by the maximum
                    471:         * packet size.  Also sets the tty flag if there is no command.
                    472:         */
1.128     fgsch     473:        if (!ac) {
1.31      markus    474:                /* No command specified - execute shell on a tty. */
                    475:                tty_flag = 1;
1.85      djm       476:                if (subsystem_flag) {
1.128     fgsch     477:                        fprintf(stderr,
                    478:                            "You must specify a subsystem to invoke.\n");
1.85      djm       479:                        usage();
                    480:                }
1.31      markus    481:        } else {
1.128     fgsch     482:                /* A command has been specified.  Store it into the buffer. */
                    483:                for (i = 0; i < ac; i++) {
                    484:                        if (i)
1.31      markus    485:                                buffer_append(&command, " ", 1);
                    486:                        buffer_append(&command, av[i], strlen(av[i]));
                    487:                }
                    488:        }
                    489:
                    490:        /* Cannot fork to background if no command. */
1.63      markus    491:        if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31      markus    492:                fatal("Cannot fork into background without a command to execute.");
                    493:
                    494:        /* Allocate a tty by default if no command specified. */
                    495:        if (buffer_len(&command) == 0)
                    496:                tty_flag = 1;
                    497:
1.180     deraadt   498:        /* Force no tty */
1.75      markus    499:        if (no_tty_flag)
                    500:                tty_flag = 0;
1.31      markus    501:        /* Do not allocate a tty if stdin is not a tty. */
1.79      markus    502:        if (!isatty(fileno(stdin)) && !force_tty_flag) {
1.31      markus    503:                if (tty_flag)
1.191     itojun    504:                        logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31      markus    505:                tty_flag = 0;
                    506:        }
1.45      markus    507:
1.101     markus    508:        /*
                    509:         * Initialize "log" output.  Since we are the client all output
                    510:         * actually goes to stderr.
                    511:         */
1.111     markus    512:        log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
                    513:            SYSLOG_FACILITY_USER, 1);
1.31      markus    514:
1.139     markus    515:        /*
                    516:         * Read per-user configuration file.  Ignore the system wide config
                    517:         * file if the user specifies a config file on the command line.
                    518:         */
                    519:        if (config != NULL) {
1.142     stevesk   520:                if (!read_config_file(config, host, &options))
                    521:                        fatal("Can't open user config file %.100s: "
                    522:                            "%.100s", config, strerror(errno));
1.139     markus    523:        } else  {
                    524:                snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
                    525:                    _PATH_SSH_USER_CONFFILE);
1.145     markus    526:                (void)read_config_file(buf, host, &options);
1.139     markus    527:
1.145     markus    528:                /* Read systemwide configuration file after use config. */
1.142     stevesk   529:                (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
1.139     markus    530:        }
1.31      markus    531:
                    532:        /* Fill configuration defaults. */
                    533:        fill_default_options(&options);
                    534:
1.196     djm       535:        channel_set_af(options.address_family);
                    536:
1.31      markus    537:        /* reinit */
1.101     markus    538:        log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31      markus    539:
                    540:        if (options.user == NULL)
                    541:                options.user = xstrdup(pw->pw_name);
                    542:
                    543:        if (options.hostname != NULL)
                    544:                host = options.hostname;
1.195     markus    545:
                    546:        /* force lowercase for hostkey matching */
                    547:        if (options.host_key_alias != NULL) {
                    548:                for (p = options.host_key_alias; *p; p++)
                    549:                        if (isupper(*p))
                    550:                                *p = tolower(*p);
                    551:        }
1.190     markus    552:
                    553:        if (options.proxy_command != NULL &&
                    554:            strcmp(options.proxy_command, "none") == 0)
                    555:                options.proxy_command = NULL;
1.31      markus    556:
1.77      markus    557:        /* Open a connection to the remote host. */
1.203     djm       558:        if (ssh_connect(host, &hostaddr, options.port,
1.196     djm       559:            options.address_family, options.connection_attempts,
1.177     markus    560:            original_effective_uid == 0 && options.use_privileged_port,
1.179     markus    561:            options.proxy_command) != 0)
1.178     markus    562:                exit(1);
1.31      markus    563:
1.33      markus    564:        /*
                    565:         * If we successfully made the connection, load the host private key
                    566:         * in case we will need it later for combined rsa-rhosts
                    567:         * authentication. This must be done before releasing extra
                    568:         * privileges, because the file is only readable by root.
1.174     markus    569:         * If we cannot access the private keys, load the public keys
                    570:         * instead and try to execute the ssh-keysign helper instead.
1.33      markus    571:         */
1.112     markus    572:        sensitive_data.nkeys = 0;
                    573:        sensitive_data.keys = NULL;
1.173     markus    574:        sensitive_data.external_keysign = 0;
1.178     markus    575:        if (options.rhosts_rsa_authentication ||
                    576:            options.hostbased_authentication) {
1.112     markus    577:                sensitive_data.nkeys = 3;
1.180     deraadt   578:                sensitive_data.keys = xmalloc(sensitive_data.nkeys *
                    579:                    sizeof(Key));
1.177     markus    580:
                    581:                PRIV_START;
1.112     markus    582:                sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.105     markus    583:                    _PATH_HOST_KEY_FILE, "", NULL);
1.112     markus    584:                sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
                    585:                    _PATH_HOST_DSA_KEY_FILE, "", NULL);
                    586:                sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
                    587:                    _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.177     markus    588:                PRIV_END;
1.173     markus    589:
1.181     markus    590:                if (options.hostbased_authentication == 1 &&
                    591:                    sensitive_data.keys[0] == NULL &&
1.173     markus    592:                    sensitive_data.keys[1] == NULL &&
                    593:                    sensitive_data.keys[2] == NULL) {
                    594:                        sensitive_data.keys[1] = key_load_public(
                    595:                            _PATH_HOST_DSA_KEY_FILE, NULL);
                    596:                        sensitive_data.keys[2] = key_load_public(
                    597:                            _PATH_HOST_RSA_KEY_FILE, NULL);
                    598:                        sensitive_data.external_keysign = 1;
                    599:                }
1.31      markus    600:        }
1.33      markus    601:        /*
                    602:         * Get rid of any extra privileges that we may have.  We will no
                    603:         * longer need them.  Also, extra privileges could make it very hard
                    604:         * to read identity files and other non-world-readable files from the
                    605:         * user's home directory if it happens to be on a NFS volume where
                    606:         * root is mapped to nobody.
                    607:         */
1.177     markus    608:        seteuid(original_real_uid);
                    609:        setuid(original_real_uid);
1.31      markus    610:
1.33      markus    611:        /*
                    612:         * Now that we are back to our own permissions, create ~/.ssh
                    613:         * directory if it doesn\'t already exist.
                    614:         */
1.138     jakob     615:        snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31      markus    616:        if (stat(buf, &st) < 0)
1.57      djm       617:                if (mkdir(buf, 0700) < 0)
1.31      markus    618:                        error("Could not create directory '%.200s'.", buf);
                    619:
1.104     markus    620:        /* load options.identity_files */
                    621:        load_public_identity_files();
                    622:
                    623:        /* Expand ~ in known host file names. */
                    624:        /* XXX mem-leaks: */
1.72      markus    625:        options.system_hostfile =
                    626:            tilde_expand_filename(options.system_hostfile, original_real_uid);
                    627:        options.user_hostfile =
                    628:            tilde_expand_filename(options.user_hostfile, original_real_uid);
                    629:        options.system_hostfile2 =
                    630:            tilde_expand_filename(options.system_hostfile2, original_real_uid);
                    631:        options.user_hostfile2 =
                    632:            tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149     markus    633:
                    634:        signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31      markus    635:
                    636:        /* Log into the remote system.  This never returns if the login fails. */
1.173     markus    637:        ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31      markus    638:
1.112     markus    639:        /* We no longer need the private host keys.  Clear them now. */
                    640:        if (sensitive_data.nkeys != 0) {
                    641:                for (i = 0; i < sensitive_data.nkeys; i++) {
                    642:                        if (sensitive_data.keys[i] != NULL) {
                    643:                                /* Destroys contents safely */
                    644:                                debug3("clear hostkey %d", i);
                    645:                                key_free(sensitive_data.keys[i]);
                    646:                                sensitive_data.keys[i] = NULL;
                    647:                        }
                    648:                }
                    649:                xfree(sensitive_data.keys);
1.134     markus    650:        }
                    651:        for (i = 0; i < options.num_identity_files; i++) {
                    652:                if (options.identity_files[i]) {
                    653:                        xfree(options.identity_files[i]);
                    654:                        options.identity_files[i] = NULL;
                    655:                }
                    656:                if (options.identity_keys[i]) {
                    657:                        key_free(options.identity_keys[i]);
                    658:                        options.identity_keys[i] = NULL;
                    659:                }
1.112     markus    660:        }
1.31      markus    661:
1.45      markus    662:        exit_status = compat20 ? ssh_session2() : ssh_session();
                    663:        packet_close();
1.186     djm       664:
                    665:        /*
1.203     djm       666:         * Send SIGHUP to proxy command if used. We don't wait() in
1.186     djm       667:         * case it hangs and instead rely on init to reap the child
                    668:         */
                    669:        if (proxy_command_pid > 1)
                    670:                kill(proxy_command_pid, SIGHUP);
                    671:
1.45      markus    672:        return exit_status;
                    673: }
                    674:
1.202     markus    675: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
                    676:
1.126     itojun    677: static void
1.150     stevesk   678: x11_get_proto(char **_proto, char **_data)
1.50      markus    679: {
1.202     markus    680:        char cmd[1024];
1.50      markus    681:        char line[512];
1.202     markus    682:        char xdisplay[512];
1.150     stevesk   683:        static char proto[512], data[512];
1.50      markus    684:        FILE *f;
1.202     markus    685:        int got_data = 0, generated = 0, do_unlink = 0, i;
                    686:        char *display, *xauthdir, *xauthfile;
1.182     stevesk   687:        struct stat st;
1.50      markus    688:
1.202     markus    689:        xauthdir = xauthfile = NULL;
1.150     stevesk   690:        *_proto = proto;
                    691:        *_data = data;
                    692:        proto[0] = data[0] = '\0';
1.202     markus    693:
1.182     stevesk   694:        if (!options.xauth_location ||
                    695:            (stat(options.xauth_location, &st) == -1)) {
                    696:                debug("No xauth program.");
                    697:        } else {
                    698:                if ((display = getenv("DISPLAY")) == NULL) {
                    699:                        debug("x11_get_proto: DISPLAY not set");
                    700:                        return;
                    701:                }
1.202     markus    702:                /*
                    703:                 * Handle FamilyLocal case where $DISPLAY does
                    704:                 * not match an authorization entry.  For this we
                    705:                 * just try "xauth list unix:displaynum.screennum".
                    706:                 * XXX: "localhost" match to determine FamilyLocal
                    707:                 *      is not perfect.
                    708:                 */
                    709:                if (strncmp(display, "localhost:", 10) == 0) {
                    710:                        snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
                    711:                            display + 10);
                    712:                        display = xdisplay;
                    713:                }
                    714:                if (options.forward_x11_trusted == 0) {
                    715:                        xauthdir = xmalloc(MAXPATHLEN);
                    716:                        xauthfile = xmalloc(MAXPATHLEN);
                    717:                        strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
                    718:                        if (mkdtemp(xauthdir) != NULL) {
                    719:                                do_unlink = 1;
                    720:                                snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
                    721:                                    xauthdir);
                    722:                                snprintf(cmd, sizeof(cmd),
                    723:                                    "%s -f %s generate %s " SSH_X11_PROTO
1.207     markus    724:                                    " untrusted timeout 1200 2>" _PATH_DEVNULL,
1.202     markus    725:                                    options.xauth_location, xauthfile, display);
                    726:                                debug2("x11_get_proto: %s", cmd);
                    727:                                if (system(cmd) == 0)
                    728:                                        generated = 1;
                    729:                        }
                    730:                }
                    731:                snprintf(cmd, sizeof(cmd),
                    732:                    "%s %s%s list %s . 2>" _PATH_DEVNULL,
                    733:                    options.xauth_location,
                    734:                    generated ? "-f " : "" ,
                    735:                    generated ? xauthfile : "",
                    736:                    display);
                    737:                debug2("x11_get_proto: %s", cmd);
                    738:                f = popen(cmd, "r");
1.55      markus    739:                if (f && fgets(line, sizeof(line), f) &&
1.150     stevesk   740:                    sscanf(line, "%*s %511s %511s", proto, data) == 2)
1.55      markus    741:                        got_data = 1;
                    742:                if (f)
                    743:                        pclose(f);
                    744:        }
1.202     markus    745:
                    746:        if (do_unlink) {
                    747:                unlink(xauthfile);
                    748:                rmdir(xauthdir);
                    749:        }
                    750:        if (xauthdir)
                    751:                xfree(xauthdir);
                    752:        if (xauthfile)
                    753:                xfree(xauthfile);
                    754:
1.50      markus    755:        /*
                    756:         * If we didn't get authentication data, just make up some
                    757:         * data.  The forwarding code will check the validity of the
                    758:         * response anyway, and substitute this data.  The X11
                    759:         * server, however, will ignore this fake data and use
                    760:         * whatever authentication mechanisms it was using otherwise
                    761:         * for the local connection.
                    762:         */
                    763:        if (!got_data) {
                    764:                u_int32_t rand = 0;
                    765:
1.202     markus    766:                logit("Warning: No xauth data; "
                    767:                    "using fake authentication data for X11 forwarding.");
                    768:                strlcpy(proto, SSH_X11_PROTO, sizeof proto);
1.50      markus    769:                for (i = 0; i < 16; i++) {
                    770:                        if (i % 4 == 0)
                    771:                                rand = arc4random();
1.202     markus    772:                        snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
                    773:                            rand & 0xff);
1.50      markus    774:                        rand >>= 8;
                    775:                }
                    776:        }
                    777: }
                    778:
1.126     itojun    779: static void
1.70      markus    780: ssh_init_forwarding(void)
                    781: {
1.86      markus    782:        int success = 0;
1.70      markus    783:        int i;
1.86      markus    784:
1.70      markus    785:        /* Initiate local TCP/IP port forwardings. */
                    786:        for (i = 0; i < options.num_local_forwards; i++) {
                    787:                debug("Connections to local port %d forwarded to remote address %.200s:%d",
                    788:                    options.local_forwards[i].port,
                    789:                    options.local_forwards[i].host,
                    790:                    options.local_forwards[i].host_port);
1.158     markus    791:                success += channel_setup_local_fwd_listener(
1.70      markus    792:                    options.local_forwards[i].port,
                    793:                    options.local_forwards[i].host,
                    794:                    options.local_forwards[i].host_port,
                    795:                    options.gateway_ports);
                    796:        }
1.86      markus    797:        if (i > 0 && success == 0)
                    798:                error("Could not request local forwarding.");
1.70      markus    799:
                    800:        /* Initiate remote TCP/IP port forwardings. */
                    801:        for (i = 0; i < options.num_remote_forwards; i++) {
                    802:                debug("Connections to remote port %d forwarded to local address %.200s:%d",
                    803:                    options.remote_forwards[i].port,
                    804:                    options.remote_forwards[i].host,
                    805:                    options.remote_forwards[i].host_port);
                    806:                channel_request_remote_forwarding(
                    807:                    options.remote_forwards[i].port,
                    808:                    options.remote_forwards[i].host,
                    809:                    options.remote_forwards[i].host_port);
                    810:        }
                    811: }
                    812:
1.126     itojun    813: static void
1.70      markus    814: check_agent_present(void)
                    815: {
                    816:        if (options.forward_agent) {
                    817:                /* Clear agent forwarding if we don\'t have an agent. */
1.185     stevesk   818:                if (!ssh_agent_present())
1.70      markus    819:                        options.forward_agent = 0;
                    820:        }
                    821: }
                    822:
1.126     itojun    823: static int
1.45      markus    824: ssh_session(void)
                    825: {
                    826:        int type;
                    827:        int interactive = 0;
                    828:        int have_tty = 0;
                    829:        struct winsize ws;
                    830:        char *cp;
                    831:
1.31      markus    832:        /* Enable compression if requested. */
                    833:        if (options.compression) {
                    834:                debug("Requesting compression at level %d.", options.compression_level);
                    835:
                    836:                if (options.compression_level < 1 || options.compression_level > 9)
                    837:                        fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
                    838:
                    839:                /* Send the request. */
                    840:                packet_start(SSH_CMSG_REQUEST_COMPRESSION);
                    841:                packet_put_int(options.compression_level);
                    842:                packet_send();
                    843:                packet_write_wait();
1.156     markus    844:                type = packet_read();
1.31      markus    845:                if (type == SSH_SMSG_SUCCESS)
                    846:                        packet_start_compression(options.compression_level);
                    847:                else if (type == SSH_SMSG_FAILURE)
1.191     itojun    848:                        logit("Warning: Remote host refused compression.");
1.31      markus    849:                else
                    850:                        packet_disconnect("Protocol error waiting for compression response.");
                    851:        }
                    852:        /* Allocate a pseudo tty if appropriate. */
                    853:        if (tty_flag) {
                    854:                debug("Requesting pty.");
                    855:
                    856:                /* Start the packet. */
                    857:                packet_start(SSH_CMSG_REQUEST_PTY);
                    858:
                    859:                /* Store TERM in the packet.  There is no limit on the
                    860:                   length of the string. */
                    861:                cp = getenv("TERM");
                    862:                if (!cp)
                    863:                        cp = "";
1.124     markus    864:                packet_put_cstring(cp);
1.31      markus    865:
                    866:                /* Store window size in the packet. */
                    867:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                    868:                        memset(&ws, 0, sizeof(ws));
                    869:                packet_put_int(ws.ws_row);
                    870:                packet_put_int(ws.ws_col);
                    871:                packet_put_int(ws.ws_xpixel);
                    872:                packet_put_int(ws.ws_ypixel);
                    873:
                    874:                /* Store tty modes in the packet. */
1.115     stevesk   875:                tty_make_modes(fileno(stdin), NULL);
1.31      markus    876:
                    877:                /* Send the packet, and wait for it to leave. */
                    878:                packet_send();
                    879:                packet_write_wait();
                    880:
                    881:                /* Read response from the server. */
1.156     markus    882:                type = packet_read();
1.43      markus    883:                if (type == SSH_SMSG_SUCCESS) {
1.31      markus    884:                        interactive = 1;
1.45      markus    885:                        have_tty = 1;
1.43      markus    886:                } else if (type == SSH_SMSG_FAILURE)
1.191     itojun    887:                        logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31      markus    888:                else
                    889:                        packet_disconnect("Protocol error waiting for pty request response.");
                    890:        }
                    891:        /* Request X11 forwarding if enabled and DISPLAY is set. */
                    892:        if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.150     stevesk   893:                char *proto, *data;
1.50      markus    894:                /* Get reasonable local authentication information. */
1.150     stevesk   895:                x11_get_proto(&proto, &data);
1.50      markus    896:                /* Request forwarding with authentication spoofing. */
1.31      markus    897:                debug("Requesting X11 forwarding with authentication spoofing.");
1.50      markus    898:                x11_request_forwarding_with_spoofing(0, proto, data);
1.31      markus    899:
                    900:                /* Read response from the server. */
1.156     markus    901:                type = packet_read();
1.31      markus    902:                if (type == SSH_SMSG_SUCCESS) {
                    903:                        interactive = 1;
1.50      markus    904:                } else if (type == SSH_SMSG_FAILURE) {
1.191     itojun    905:                        logit("Warning: Remote host denied X11 forwarding.");
1.50      markus    906:                } else {
1.31      markus    907:                        packet_disconnect("Protocol error waiting for X11 forwarding");
1.50      markus    908:                }
1.31      markus    909:        }
                    910:        /* Tell the packet module whether this is an interactive session. */
1.80      markus    911:        packet_set_interactive(interactive);
1.31      markus    912:
                    913:        /* Request authentication agent forwarding if appropriate. */
1.70      markus    914:        check_agent_present();
                    915:
1.31      markus    916:        if (options.forward_agent) {
                    917:                debug("Requesting authentication agent forwarding.");
                    918:                auth_request_forwarding();
                    919:
                    920:                /* Read response from the server. */
1.156     markus    921:                type = packet_read();
1.155     markus    922:                packet_check_eom();
1.31      markus    923:                if (type != SSH_SMSG_SUCCESS)
1.191     itojun    924:                        logit("Warning: Remote host denied authentication agent forwarding.");
1.31      markus    925:        }
                    926:
1.70      markus    927:        /* Initiate port forwardings. */
                    928:        ssh_init_forwarding();
1.34      markus    929:
                    930:        /* If requested, let ssh continue in the background. */
1.48      markus    931:        if (fork_after_authentication_flag)
1.34      markus    932:                if (daemon(1, 1) < 0)
                    933:                        fatal("daemon() failed: %.200s", strerror(errno));
1.31      markus    934:
1.33      markus    935:        /*
                    936:         * If a command was specified on the command line, execute the
                    937:         * command now. Otherwise request the server to start a shell.
                    938:         */
1.31      markus    939:        if (buffer_len(&command) > 0) {
                    940:                int len = buffer_len(&command);
                    941:                if (len > 900)
                    942:                        len = 900;
1.152     stevesk   943:                debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31      markus    944:                packet_start(SSH_CMSG_EXEC_CMD);
                    945:                packet_put_string(buffer_ptr(&command), buffer_len(&command));
                    946:                packet_send();
                    947:                packet_write_wait();
                    948:        } else {
                    949:                debug("Requesting shell.");
                    950:                packet_start(SSH_CMSG_EXEC_SHELL);
                    951:                packet_send();
                    952:                packet_write_wait();
                    953:        }
                    954:
                    955:        /* Enter the interactive session. */
1.119     stevesk   956:        return client_loop(have_tty, tty_flag ?
                    957:            options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45      markus    958: }
                    959:
1.126     itojun    960: static void
1.157     markus    961: client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89      markus    962: {
                    963:        int id, len;
                    964:
                    965:        id = packet_get_int();
                    966:        len = buffer_len(&command);
1.100     markus    967:        if (len > 900)
                    968:                len = 900;
1.155     markus    969:        packet_check_eom();
1.89      markus    970:        if (type == SSH2_MSG_CHANNEL_FAILURE)
                    971:                fatal("Request for subsystem '%.*s' failed on channel %d",
1.152     stevesk   972:                    len, (u_char *)buffer_ptr(&command), id);
1.170     markus    973: }
                    974:
                    975: void
1.206     markus    976: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170     markus    977: {
                    978:        int i;
                    979:
                    980:        i = client_global_request_id++;
1.206     markus    981:        if (i >= options.num_remote_forwards)
1.170     markus    982:                return;
                    983:        debug("remote forward %s for: listen %d, connect %s:%d",
                    984:            type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
                    985:            options.remote_forwards[i].port,
                    986:            options.remote_forwards[i].host,
                    987:            options.remote_forwards[i].host_port);
                    988:        if (type == SSH2_MSG_REQUEST_FAILURE)
1.191     itojun    989:                logit("Warning: remote port forwarding failed for listen port %d",
1.170     markus    990:                    options.remote_forwards[i].port);
1.89      markus    991: }
                    992:
1.143     markus    993: /* request pty/x11/agent/tcpfwd/shell for channel */
1.126     itojun    994: static void
1.143     markus    995: ssh_session2_setup(int id, void *arg)
1.45      markus    996: {
                    997:        int len;
1.80      markus    998:        int interactive = 0;
1.115     stevesk   999:        struct termios tio;
1.80      markus   1000:
1.187     markus   1001:        debug2("ssh_session2_setup: id %d", id);
1.31      markus   1002:
1.45      markus   1003:        if (tty_flag) {
                   1004:                struct winsize ws;
                   1005:                char *cp;
                   1006:                cp = getenv("TERM");
                   1007:                if (!cp)
                   1008:                        cp = "";
                   1009:                /* Store window size in the packet. */
                   1010:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                   1011:                        memset(&ws, 0, sizeof(ws));
                   1012:
                   1013:                channel_request_start(id, "pty-req", 0);
                   1014:                packet_put_cstring(cp);
                   1015:                packet_put_int(ws.ws_col);
                   1016:                packet_put_int(ws.ws_row);
                   1017:                packet_put_int(ws.ws_xpixel);
                   1018:                packet_put_int(ws.ws_ypixel);
1.115     stevesk  1019:                tio = get_saved_tio();
                   1020:                tty_make_modes(/*ignored*/ 0, &tio);
1.45      markus   1021:                packet_send();
1.80      markus   1022:                interactive = 1;
1.45      markus   1023:                /* XXX wait for reply */
                   1024:        }
1.50      markus   1025:        if (options.forward_x11 &&
                   1026:            getenv("DISPLAY") != NULL) {
1.150     stevesk  1027:                char *proto, *data;
1.50      markus   1028:                /* Get reasonable local authentication information. */
1.150     stevesk  1029:                x11_get_proto(&proto, &data);
1.50      markus   1030:                /* Request forwarding with authentication spoofing. */
                   1031:                debug("Requesting X11 forwarding with authentication spoofing.");
                   1032:                x11_request_forwarding_with_spoofing(id, proto, data);
1.80      markus   1033:                interactive = 1;
1.50      markus   1034:                /* XXX wait for reply */
                   1035:        }
                   1036:
1.70      markus   1037:        check_agent_present();
                   1038:        if (options.forward_agent) {
                   1039:                debug("Requesting authentication agent forwarding.");
                   1040:                channel_request_start(id, "auth-agent-req@openssh.com", 0);
                   1041:                packet_send();
                   1042:        }
                   1043:
1.45      markus   1044:        len = buffer_len(&command);
                   1045:        if (len > 0) {
                   1046:                if (len > 900)
                   1047:                        len = 900;
1.85      djm      1048:                if (subsystem_flag) {
1.152     stevesk  1049:                        debug("Sending subsystem: %.*s", len, (u_char *)buffer_ptr(&command));
1.89      markus   1050:                        channel_request_start(id, "subsystem", /*want reply*/ 1);
                   1051:                        /* register callback for reply */
1.172     deraadt  1052:                        /* XXX we assume that client_loop has already been called */
1.89      markus   1053:                        dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
                   1054:                        dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
1.85      djm      1055:                } else {
1.152     stevesk  1056:                        debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.85      djm      1057:                        channel_request_start(id, "exec", 0);
                   1058:                }
1.100     markus   1059:                packet_put_string(buffer_ptr(&command), buffer_len(&command));
1.45      markus   1060:                packet_send();
                   1061:        } else {
1.161     markus   1062:                channel_request_start(id, "shell", 0);
                   1063:                packet_send();
1.45      markus   1064:        }
1.90      markus   1065:
1.80      markus   1066:        packet_set_interactive(interactive);
1.45      markus   1067: }
                   1068:
1.143     markus   1069: /* open new channel for a session */
1.126     itojun   1070: static int
1.143     markus   1071: ssh_session2_open(void)
1.45      markus   1072: {
1.118     markus   1073:        Channel *c;
                   1074:        int window, packetmax, in, out, err;
1.60      markus   1075:
1.62      markus   1076:        if (stdin_null_flag) {
1.93      itojun   1077:                in = open(_PATH_DEVNULL, O_RDONLY);
1.62      markus   1078:        } else {
                   1079:                in = dup(STDIN_FILENO);
                   1080:        }
1.60      markus   1081:        out = dup(STDOUT_FILENO);
                   1082:        err = dup(STDERR_FILENO);
1.45      markus   1083:
                   1084:        if (in < 0 || out < 0 || err < 0)
1.62      markus   1085:                fatal("dup() in/out/err failed");
1.45      markus   1086:
1.69      markus   1087:        /* enable nonblocking unless tty */
                   1088:        if (!isatty(in))
                   1089:                set_nonblock(in);
                   1090:        if (!isatty(out))
                   1091:                set_nonblock(out);
                   1092:        if (!isatty(err))
                   1093:                set_nonblock(err);
                   1094:
1.65      markus   1095:        window = CHAN_SES_WINDOW_DEFAULT;
                   1096:        packetmax = CHAN_SES_PACKET_DEFAULT;
1.164     markus   1097:        if (tty_flag) {
                   1098:                window >>= 1;
                   1099:                packetmax >>= 1;
1.45      markus   1100:        }
1.118     markus   1101:        c = channel_new(
1.45      markus   1102:            "session", SSH_CHANNEL_OPENING, in, out, err,
1.65      markus   1103:            window, packetmax, CHAN_EXTENDED_WRITE,
1.192     markus   1104:            "client-session", /*nonblock*/0);
1.45      markus   1105:
1.143     markus   1106:        debug3("ssh_session2_open: channel_new: %d", c->self);
1.106     markus   1107:
1.122     markus   1108:        channel_send_open(c->self);
1.143     markus   1109:        if (!no_shell_flag)
1.160     markus   1110:                channel_register_confirm(c->self, ssh_session2_setup);
1.106     markus   1111:
1.118     markus   1112:        return c->self;
1.106     markus   1113: }
                   1114:
1.126     itojun   1115: static int
1.106     markus   1116: ssh_session2(void)
                   1117: {
1.143     markus   1118:        int id = -1;
1.106     markus   1119:
                   1120:        /* XXX should be pre-session */
                   1121:        ssh_init_forwarding();
                   1122:
1.143     markus   1123:        if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
                   1124:                id = ssh_session2_open();
1.106     markus   1125:
                   1126:        /* If requested, let ssh continue in the background. */
                   1127:        if (fork_after_authentication_flag)
                   1128:                if (daemon(1, 1) < 0)
                   1129:                        fatal("daemon() failed: %.200s", strerror(errno));
1.31      markus   1130:
1.119     stevesk  1131:        return client_loop(tty_flag, tty_flag ?
                   1132:            options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72      markus   1133: }
                   1134:
1.126     itojun   1135: static void
1.104     markus   1136: load_public_identity_files(void)
                   1137: {
                   1138:        char *filename;
1.167     markus   1139:        int i = 0;
1.104     markus   1140:        Key *public;
1.167     markus   1141: #ifdef SMARTCARD
                   1142:        Key **keys;
1.104     markus   1143:
1.133     markus   1144:        if (options.smartcard_device != NULL &&
1.167     markus   1145:            options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
                   1146:            (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
                   1147:                int count = 0;
                   1148:                for (i = 0; keys[i] != NULL; i++) {
                   1149:                        count++;
                   1150:                        memmove(&options.identity_files[1], &options.identity_files[0],
                   1151:                            sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
                   1152:                        memmove(&options.identity_keys[1], &options.identity_keys[0],
                   1153:                            sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
                   1154:                        options.num_identity_files++;
                   1155:                        options.identity_keys[0] = keys[i];
1.194     markus   1156:                        options.identity_files[0] = sc_get_key_label(keys[i]);
1.167     markus   1157:                }
1.168     markus   1158:                if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
                   1159:                        options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167     markus   1160:                i = count;
                   1161:                xfree(keys);
1.127     markus   1162:        }
1.136     jakob    1163: #endif /* SMARTCARD */
1.131     millert  1164:        for (; i < options.num_identity_files; i++) {
                   1165:                filename = tilde_expand_filename(options.identity_files[i],
                   1166:                    original_real_uid);
                   1167:                public = key_load_public(filename, NULL);
                   1168:                debug("identity file %s type %d", filename,
                   1169:                    public ? public->type : -1);
                   1170:                xfree(options.identity_files[i]);
                   1171:                options.identity_files[i] = filename;
                   1172:                options.identity_keys[i] = public;
                   1173:        }
1.1       deraadt  1174: }