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

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