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

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