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

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