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

1.592   ! jmc         1: /* $OpenBSD: ssh.c,v 1.591 2023/07/17 04:08:31 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:  *
1.529     dtucker    19:  * Modified to work with SSLeay by Niels Provos <provos@citi.umich.edu>
1.64      deraadt    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/time.h>
1.352     djm        50: #include <sys/wait.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.509     naddy      63: #include <stdarg.h>
1.288     stevesk    64: #include <unistd.h>
1.414     deraadt    65: #include <limits.h>
1.445     djm        66: #include <locale.h>
1.49      markus     67:
1.402     markus     68: #ifdef WITH_OPENSSL
1.49      markus     69: #include <openssl/evp.h>
1.72      markus     70: #include <openssl/err.h>
1.402     markus     71: #endif
1.1       deraadt    72:
1.293     deraadt    73: #include "xmalloc.h"
1.84      markus     74: #include "ssh.h"
                     75: #include "ssh2.h"
1.341     djm        76: #include "canohost.h"
1.84      markus     77: #include "compat.h"
                     78: #include "cipher.h"
1.1       deraadt    79: #include "packet.h"
1.482     markus     80: #include "sshbuf.h"
1.123     markus     81: #include "channels.h"
1.483     markus     82: #include "sshkey.h"
1.58      markus     83: #include "authfd.h"
1.49      markus     84: #include "authfile.h"
1.83      markus     85: #include "pathnames.h"
1.214     djm        86: #include "dispatch.h"
1.81      markus     87: #include "clientloop.h"
1.84      markus     88: #include "log.h"
1.406     millert    89: #include "misc.h"
1.84      markus     90: #include "readconf.h"
                     91: #include "sshconnect.h"
1.95      markus     92: #include "kex.h"
                     93: #include "mac.h"
1.213     deraadt    94: #include "sshpty.h"
1.212     djm        95: #include "match.h"
1.214     djm        96: #include "msg.h"
1.278     stevesk    97: #include "version.h"
1.412     djm        98: #include "ssherr.h"
1.420     djm        99: #include "myproposal.h"
1.49      markus    100:
1.333     markus    101: #ifdef ENABLE_PKCS11
                    102: #include "ssh-pkcs11.h"
1.137     jakob     103: #endif
1.498     djm       104:
1.49      markus    105: extern char *__progname;
1.1       deraadt   106:
1.316     djm       107: /* Flag indicating whether debug mode is on.  May be set on the command line. */
1.1       deraadt   108: int debug_flag = 0;
                    109:
1.359     djm       110: /* Flag indicating whether a tty should be requested */
1.1       deraadt   111: int tty_flag = 0;
                    112:
1.33      markus    113: /*
1.344     djm       114:  * Flag indicating that the current process should be backgrounded and
1.531     djm       115:  * a new mux-client launched in the foreground for ControlPersist.
1.344     djm       116:  */
                    117: int need_controlpersist_detach = 0;
                    118:
1.531     djm       119: /* Copies of flags for ControlPersist foreground mux-client */
1.560     djm       120: int ostdin_null_flag, osession_type, otty_flag, orequest_tty;
1.344     djm       121:
                    122: /*
1.33      markus    123:  * General data structure for command line options and options configurable
                    124:  * in configuration files.  See readconf.h.
                    125:  */
1.1       deraadt   126: Options options;
                    127:
1.139     markus    128: /* optional user configfile */
                    129: char *config = NULL;
                    130:
1.33      markus    131: /*
                    132:  * Name of the host we are connecting to.  This is the name given on the
1.503     jmc       133:  * command line, or the Hostname specified for the user-supplied name in a
1.33      markus    134:  * configuration file.
                    135:  */
1.1       deraadt   136: char *host;
                    137:
1.510     djm       138: /*
                    139:  * A config can specify a path to forward, overriding SSH_AUTH_SOCK. If this is
                    140:  * not NULL, forward the socket at this path instead.
                    141:  */
                    142: char *forward_agent_sock_path = NULL;
                    143:
1.22      provos    144: /* socket address the host resolves to */
1.37      markus    145: struct sockaddr_storage hostaddr;
1.1       deraadt   146:
1.112     markus    147: /* Private host keys. */
1.173     markus    148: Sensitive sensitive_data;
1.1       deraadt   149:
1.45      markus    150: /* command to be executed */
1.482     markus    151: struct sshbuf *command;
1.45      markus    152:
1.170     markus    153: /* # of replies received for global requests */
1.523     djm       154: static int forward_confirms_pending = -1;
1.170     markus    155:
1.313     djm       156: /* mux.c */
                    157: extern int muxserver_sock;
                    158: extern u_int muxclient_command;
                    159:
1.1       deraadt   160: /* Prints a help message to the user.  This function never returns. */
                    161:
1.126     itojun    162: static void
1.93      itojun    163: usage(void)
1.1       deraadt   164: {
1.208     markus    165:        fprintf(stderr,
1.592   ! jmc       166: "usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface] [-b bind_address]\n"
        !           167: "           [-c cipher_spec] [-D [bind_address:]port] [-E log_file]\n"
        !           168: "           [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file]\n"
        !           169: "           [-J destination] [-L address] [-l login_name] [-m mac_spec]\n"
        !           170: "           [-O ctl_cmd] [-o option] [-P tag] [-p port] [-Q query_option]\n"
        !           171: "           [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]\n"
        !           172: "           destination [command [argument ...]]\n"
1.208     markus    173:        );
1.257     dtucker   174:        exit(255);
1.1       deraadt   175: }
                    176:
1.544     djm       177: static int ssh_session2(struct ssh *, const struct ssh_conn_info *);
                    178: static void load_public_identity_files(const struct ssh_conn_info *);
1.352     djm       179: static void main_sigchld_handler(int);
1.312     djm       180:
1.361     djm       181: /* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
                    182: static void
                    183: tilde_expand_paths(char **paths, u_int num_paths)
                    184: {
                    185:        u_int i;
                    186:        char *cp;
                    187:
                    188:        for (i = 0; i < num_paths; i++) {
1.490     dtucker   189:                cp = tilde_expand_filename(paths[i], getuid());
1.378     djm       190:                free(paths[i]);
1.361     djm       191:                paths[i] = cp;
                    192:        }
                    193: }
                    194:
1.527     dtucker   195: /*
                    196:  * Expands the set of percent_expand options used by the majority of keywords
                    197:  * in the client that support percent expansion.
                    198:  * Caller must free returned string.
                    199:  */
                    200: static char *
1.544     djm       201: default_client_percent_expand(const char *str,
                    202:     const struct ssh_conn_info *cinfo)
1.527     dtucker   203: {
                    204:        return percent_expand(str,
1.546     djm       205:            DEFAULT_CLIENT_PERCENT_EXPAND_ARGS(cinfo),
1.527     dtucker   206:            (char *)NULL);
                    207: }
                    208:
1.400     djm       209: /*
1.528     dtucker   210:  * Expands the set of percent_expand options used by the majority of keywords
                    211:  * AND perform environment variable substitution.
                    212:  * Caller must free returned string.
                    213:  */
                    214: static char *
1.544     djm       215: default_client_percent_dollar_expand(const char *str,
                    216:     const struct ssh_conn_info *cinfo)
1.528     dtucker   217: {
                    218:        char *ret;
                    219:
                    220:        ret = percent_dollar_expand(str,
1.546     djm       221:            DEFAULT_CLIENT_PERCENT_EXPAND_ARGS(cinfo),
1.528     dtucker   222:            (char *)NULL);
                    223:        if (ret == NULL)
                    224:                fatal("invalid environment variable expansion");
                    225:        return ret;
                    226: }
                    227:
                    228: /*
1.400     djm       229:  * Attempt to resolve a host name / port to a set of addresses and
                    230:  * optionally return any CNAMEs encountered along the way.
                    231:  * Returns NULL on failure.
                    232:  * NB. this function must operate with a options having undefined members.
                    233:  */
1.385     djm       234: static struct addrinfo *
1.400     djm       235: resolve_host(const char *name, int port, int logerr, char *cname, size_t clen)
1.385     djm       236: {
                    237:        char strport[NI_MAXSERV];
1.579     djm       238:        const char *errstr = NULL;
1.385     djm       239:        struct addrinfo hints, *res;
1.501     dtucker   240:        int gaierr;
                    241:        LogLevel loglevel = SYSLOG_LEVEL_DEBUG1;
1.385     djm       242:
1.400     djm       243:        if (port <= 0)
                    244:                port = default_ssh_port();
1.521     markus    245:        if (cname != NULL)
                    246:                *cname = '\0';
1.568     djm       247:        debug3_f("lookup %s:%d", name, port);
1.400     djm       248:
1.427     djm       249:        snprintf(strport, sizeof strport, "%d", port);
1.398     tedu      250:        memset(&hints, 0, sizeof(hints));
1.400     djm       251:        hints.ai_family = options.address_family == -1 ?
                    252:            AF_UNSPEC : options.address_family;
1.385     djm       253:        hints.ai_socktype = SOCK_STREAM;
                    254:        if (cname != NULL)
                    255:                hints.ai_flags = AI_CANONNAME;
                    256:        if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
                    257:                if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
                    258:                        loglevel = SYSLOG_LEVEL_ERROR;
                    259:                do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
                    260:                    __progname, name, ssh_gai_strerror(gaierr));
                    261:                return NULL;
                    262:        }
                    263:        if (cname != NULL && res->ai_canonname != NULL) {
1.579     djm       264:                if (!valid_domain(res->ai_canonname, 0, &errstr)) {
                    265:                        error("ignoring bad CNAME \"%s\" for host \"%s\": %s",
                    266:                            res->ai_canonname, name, errstr);
                    267:                } else if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
1.540     djm       268:                        error_f("host \"%s\" cname \"%s\" too long (max %lu)",
                    269:                            name,  res->ai_canonname, (u_long)clen);
1.385     djm       270:                        if (clen > 0)
                    271:                                *cname = '\0';
                    272:                }
                    273:        }
                    274:        return res;
                    275: }
                    276:
1.470     djm       277: /* Returns non-zero if name can only be an address and not a hostname */
                    278: static int
                    279: is_addr_fast(const char *name)
                    280: {
                    281:        return (strchr(name, '%') != NULL || strchr(name, ':') != NULL ||
                    282:            strspn(name, "0123456789.") == strlen(name));
                    283: }
                    284:
                    285: /* Returns non-zero if name represents a valid, single address */
                    286: static int
                    287: is_addr(const char *name)
                    288: {
                    289:        char strport[NI_MAXSERV];
                    290:        struct addrinfo hints, *res;
                    291:
                    292:        if (is_addr_fast(name))
                    293:                return 1;
                    294:
                    295:        snprintf(strport, sizeof strport, "%u", default_ssh_port());
                    296:        memset(&hints, 0, sizeof(hints));
                    297:        hints.ai_family = options.address_family == -1 ?
                    298:            AF_UNSPEC : options.address_family;
                    299:        hints.ai_socktype = SOCK_STREAM;
                    300:        hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
                    301:        if (getaddrinfo(name, strport, &hints, &res) != 0)
                    302:                return 0;
                    303:        if (res == NULL || res->ai_next != NULL) {
                    304:                freeaddrinfo(res);
                    305:                return 0;
                    306:        }
                    307:        freeaddrinfo(res);
                    308:        return 1;
                    309: }
                    310:
1.385     djm       311: /*
1.413     djm       312:  * Attempt to resolve a numeric host address / port to a single address.
                    313:  * Returns a canonical address string.
                    314:  * Returns NULL on failure.
                    315:  * NB. this function must operate with a options having undefined members.
                    316:  */
                    317: static struct addrinfo *
                    318: resolve_addr(const char *name, int port, char *caddr, size_t clen)
                    319: {
                    320:        char addr[NI_MAXHOST], strport[NI_MAXSERV];
                    321:        struct addrinfo hints, *res;
                    322:        int gaierr;
                    323:
                    324:        if (port <= 0)
                    325:                port = default_ssh_port();
                    326:        snprintf(strport, sizeof strport, "%u", port);
                    327:        memset(&hints, 0, sizeof(hints));
                    328:        hints.ai_family = options.address_family == -1 ?
                    329:            AF_UNSPEC : options.address_family;
                    330:        hints.ai_socktype = SOCK_STREAM;
                    331:        hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
                    332:        if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
1.540     djm       333:                debug2_f("could not resolve name %.100s as address: %s",
                    334:                    name, ssh_gai_strerror(gaierr));
1.413     djm       335:                return NULL;
                    336:        }
                    337:        if (res == NULL) {
1.540     djm       338:                debug_f("getaddrinfo %.100s returned no addresses", name);
1.413     djm       339:                return NULL;
                    340:        }
                    341:        if (res->ai_next != NULL) {
1.540     djm       342:                debug_f("getaddrinfo %.100s returned multiple addresses", name);
1.413     djm       343:                goto fail;
                    344:        }
                    345:        if ((gaierr = getnameinfo(res->ai_addr, res->ai_addrlen,
                    346:            addr, sizeof(addr), NULL, 0, NI_NUMERICHOST)) != 0) {
1.540     djm       347:                debug_f("Could not format address for name %.100s: %s",
                    348:                    name, ssh_gai_strerror(gaierr));
1.413     djm       349:                goto fail;
                    350:        }
                    351:        if (strlcpy(caddr, addr, clen) >= clen) {
1.540     djm       352:                error_f("host \"%s\" addr \"%s\" too long (max %lu)",
                    353:                    name,  addr, (u_long)clen);
1.413     djm       354:                if (clen > 0)
                    355:                        *caddr = '\0';
                    356:  fail:
                    357:                freeaddrinfo(res);
                    358:                return NULL;
                    359:        }
                    360:        return res;
                    361: }
                    362:
                    363: /*
1.385     djm       364:  * Check whether the cname is a permitted replacement for the hostname
                    365:  * and perform the replacement if it is.
1.400     djm       366:  * NB. this function must operate with a options having undefined members.
1.385     djm       367:  */
                    368: static int
1.443     djm       369: check_follow_cname(int direct, char **namep, const char *cname)
1.385     djm       370: {
                    371:        int i;
                    372:        struct allowed_cname *rule;
                    373:
1.568     djm       374:        if (*cname == '\0' || !config_has_permitted_cnames(&options) ||
1.385     djm       375:            strcmp(*namep, cname) == 0)
                    376:                return 0;
1.386     djm       377:        if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
1.385     djm       378:                return 0;
                    379:        /*
1.386     djm       380:         * Don't attempt to canonicalize names that will be interpreted by
1.443     djm       381:         * a proxy or jump host unless the user specifically requests so.
1.385     djm       382:         */
1.443     djm       383:        if (!direct &&
1.386     djm       384:            options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
1.385     djm       385:                return 0;
1.540     djm       386:        debug3_f("check \"%s\" CNAME \"%s\"", *namep, cname);
1.385     djm       387:        for (i = 0; i < options.num_permitted_cnames; i++) {
                    388:                rule = options.permitted_cnames + i;
1.418     djm       389:                if (match_pattern_list(*namep, rule->source_list, 1) != 1 ||
                    390:                    match_pattern_list(cname, rule->target_list, 1) != 1)
1.385     djm       391:                        continue;
1.386     djm       392:                verbose("Canonicalized DNS aliased hostname "
1.385     djm       393:                    "\"%s\" => \"%s\"", *namep, cname);
                    394:                free(*namep);
                    395:                *namep = xstrdup(cname);
                    396:                return 1;
                    397:        }
                    398:        return 0;
                    399: }
                    400:
                    401: /*
                    402:  * Attempt to resolve the supplied hostname after applying the user's
1.387     djm       403:  * canonicalization rules. Returns the address list for the host or NULL
                    404:  * if no name was found after canonicalization.
1.400     djm       405:  * NB. this function must operate with a options having undefined members.
1.385     djm       406:  */
                    407: static struct addrinfo *
1.400     djm       408: resolve_canonicalize(char **hostp, int port)
1.385     djm       409: {
1.443     djm       410:        int i, direct, ndots;
1.413     djm       411:        char *cp, *fullhost, newname[NI_MAXHOST];
1.385     djm       412:        struct addrinfo *addrs;
                    413:
                    414:        /*
1.470     djm       415:         * Attempt to canonicalise addresses, regardless of
                    416:         * whether hostname canonicalisation was requested
1.385     djm       417:         */
1.413     djm       418:        if ((addrs = resolve_addr(*hostp, port,
                    419:            newname, sizeof(newname))) != NULL) {
1.540     djm       420:                debug2_f("hostname %.100s is address", *hostp);
1.413     djm       421:                if (strcasecmp(*hostp, newname) != 0) {
1.540     djm       422:                        debug2_f("canonicalised address \"%s\" => \"%s\"",
                    423:                            *hostp, newname);
1.413     djm       424:                        free(*hostp);
                    425:                        *hostp = xstrdup(newname);
                    426:                }
                    427:                return addrs;
                    428:        }
                    429:
1.470     djm       430:        /*
                    431:         * If this looks like an address but didn't parse as one, it might
                    432:         * be an address with an invalid interface scope. Skip further
                    433:         * attempts at canonicalisation.
                    434:         */
                    435:        if (is_addr_fast(*hostp)) {
1.540     djm       436:                debug_f("hostname %.100s is an unrecognised address", *hostp);
1.470     djm       437:                return NULL;
                    438:        }
                    439:
                    440:        if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
                    441:                return NULL;
                    442:
                    443:        /*
                    444:         * Don't attempt to canonicalize names that will be interpreted by
                    445:         * a proxy unless the user specifically requests so.
                    446:         */
                    447:        direct = option_clear_or_none(options.proxy_command) &&
1.588     djm       448:            option_clear_or_none(options.jump_host);
1.470     djm       449:        if (!direct &&
                    450:            options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
                    451:                return NULL;
                    452:
1.428     djm       453:        /* If domain name is anchored, then resolve it now */
                    454:        if ((*hostp)[strlen(*hostp) - 1] == '.') {
1.540     djm       455:                debug3_f("name is fully qualified");
1.428     djm       456:                fullhost = xstrdup(*hostp);
                    457:                if ((addrs = resolve_host(fullhost, port, 0,
                    458:                    newname, sizeof(newname))) != NULL)
                    459:                        goto found;
                    460:                free(fullhost);
                    461:                goto notfound;
                    462:        }
                    463:
1.387     djm       464:        /* Don't apply canonicalization to sufficiently-qualified hostnames */
1.385     djm       465:        ndots = 0;
                    466:        for (cp = *hostp; *cp != '\0'; cp++) {
                    467:                if (*cp == '.')
                    468:                        ndots++;
                    469:        }
1.386     djm       470:        if (ndots > options.canonicalize_max_dots) {
1.540     djm       471:                debug3_f("not canonicalizing hostname \"%s\" (max dots %d)",
                    472:                    *hostp, options.canonicalize_max_dots);
1.385     djm       473:                return NULL;
                    474:        }
                    475:        /* Attempt each supplied suffix */
                    476:        for (i = 0; i < options.num_canonical_domains; i++) {
1.559     djm       477:                if (strcasecmp(options.canonical_domains[i], "none") == 0)
                    478:                        break;
1.385     djm       479:                xasprintf(&fullhost, "%s.%s.", *hostp,
                    480:                    options.canonical_domains[i]);
1.540     djm       481:                debug3_f("attempting \"%s\" => \"%s\"", *hostp, fullhost);
1.400     djm       482:                if ((addrs = resolve_host(fullhost, port, 0,
1.413     djm       483:                    newname, sizeof(newname))) == NULL) {
1.385     djm       484:                        free(fullhost);
                    485:                        continue;
                    486:                }
1.428     djm       487:  found:
1.385     djm       488:                /* Remove trailing '.' */
                    489:                fullhost[strlen(fullhost) - 1] = '\0';
                    490:                /* Follow CNAME if requested */
1.443     djm       491:                if (!check_follow_cname(direct, &fullhost, newname)) {
1.386     djm       492:                        debug("Canonicalized hostname \"%s\" => \"%s\"",
1.385     djm       493:                            *hostp, fullhost);
                    494:                }
                    495:                free(*hostp);
                    496:                *hostp = fullhost;
                    497:                return addrs;
                    498:        }
1.428     djm       499:  notfound:
1.386     djm       500:        if (!options.canonicalize_fallback_local)
1.400     djm       501:                fatal("%s: Could not resolve host \"%s\"", __progname, *hostp);
1.540     djm       502:        debug2_f("host %s not found in any suffix", *hostp);
1.385     djm       503:        return NULL;
                    504: }
                    505:
1.32      deraadt   506: /*
1.576     djm       507:  * Check the result of hostkey loading, ignoring some errors and either
                    508:  * discarding the key or fatal()ing for others.
1.483     markus    509:  */
                    510: static void
1.576     djm       511: check_load(int r, struct sshkey **k, const char *path, const char *message)
1.483     markus    512: {
                    513:        switch (r) {
                    514:        case 0:
1.576     djm       515:                /* Check RSA keys size and discard if undersized */
                    516:                if (k != NULL && *k != NULL &&
                    517:                    (r = sshkey_check_rsa_length(*k,
                    518:                    options.required_rsa_size)) != 0) {
                    519:                        error_r(r, "load %s \"%s\"", message, path);
                    520:                        free(*k);
                    521:                        *k = NULL;
                    522:                }
1.483     markus    523:                break;
                    524:        case SSH_ERR_INTERNAL_ERROR:
                    525:        case SSH_ERR_ALLOC_FAIL:
1.540     djm       526:                fatal_r(r, "load %s \"%s\"", message, path);
1.483     markus    527:        case SSH_ERR_SYSTEM_ERROR:
                    528:                /* Ignore missing files */
                    529:                if (errno == ENOENT)
                    530:                        break;
                    531:                /* FALLTHROUGH */
                    532:        default:
1.540     djm       533:                error_r(r, "load %s \"%s\"", message, path);
1.483     markus    534:                break;
                    535:        }
                    536: }
                    537:
                    538: /*
1.400     djm       539:  * Read per-user configuration file.  Ignore the system wide config
                    540:  * file if the user specifies a config file on the command line.
                    541:  */
                    542: static void
1.496     djm       543: process_config_files(const char *host_name, struct passwd *pw, int final_pass,
                    544:     int *want_final_pass)
1.400     djm       545: {
1.414     deraadt   546:        char buf[PATH_MAX];
1.400     djm       547:        int r;
                    548:
                    549:        if (config != NULL) {
                    550:                if (strcasecmp(config, "none") != 0 &&
1.466     djm       551:                    !read_config_file(config, pw, host, host_name, &options,
1.496     djm       552:                    SSHCONF_USERCONF | (final_pass ? SSHCONF_FINAL : 0),
                    553:                    want_final_pass))
1.400     djm       554:                        fatal("Can't open user config file %.100s: "
                    555:                            "%.100s", config, strerror(errno));
                    556:        } else {
                    557:                r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
                    558:                    _PATH_SSH_USER_CONFFILE);
                    559:                if (r > 0 && (size_t)r < sizeof(buf))
1.466     djm       560:                        (void)read_config_file(buf, pw, host, host_name,
1.408     djm       561:                            &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF |
1.496     djm       562:                            (final_pass ? SSHCONF_FINAL : 0), want_final_pass);
1.400     djm       563:
                    564:                /* Read systemwide configuration file after user config. */
1.408     djm       565:                (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw,
1.466     djm       566:                    host, host_name, &options,
1.496     djm       567:                    final_pass ? SSHCONF_FINAL : 0, want_final_pass);
1.408     djm       568:        }
                    569: }
                    570:
                    571: /* Rewrite the port number in an addrinfo list of addresses */
                    572: static void
                    573: set_addrinfo_port(struct addrinfo *addrs, int port)
                    574: {
                    575:        struct addrinfo *addr;
                    576:
                    577:        for (addr = addrs; addr != NULL; addr = addr->ai_next) {
                    578:                switch (addr->ai_family) {
                    579:                case AF_INET:
                    580:                        ((struct sockaddr_in *)addr->ai_addr)->
                    581:                            sin_port = htons(port);
                    582:                        break;
                    583:                case AF_INET6:
                    584:                        ((struct sockaddr_in6 *)addr->ai_addr)->
                    585:                            sin6_port = htons(port);
                    586:                        break;
                    587:                }
1.400     djm       588:        }
                    589: }
                    590:
1.544     djm       591: static void
                    592: ssh_conn_info_free(struct ssh_conn_info *cinfo)
                    593: {
                    594:        if (cinfo == NULL)
                    595:                return;
                    596:        free(cinfo->conn_hash_hex);
                    597:        free(cinfo->shorthost);
                    598:        free(cinfo->uidstr);
                    599:        free(cinfo->keyalias);
                    600:        free(cinfo->thishost);
                    601:        free(cinfo->host_arg);
                    602:        free(cinfo->portstr);
                    603:        free(cinfo->remhost);
                    604:        free(cinfo->remuser);
                    605:        free(cinfo->homedir);
                    606:        free(cinfo->locuser);
                    607:        free(cinfo);
                    608: }
                    609:
1.400     djm       610: /*
1.32      deraadt   611:  * Main program for the ssh client.
                    612:  */
1.2       provos    613: int
                    614: main(int ac, char **av)
1.1       deraadt   615: {
1.437     djm       616:        struct ssh *ssh = NULL;
1.462     djm       617:        int i, r, opt, exit_status, use_syslog, direct, timeout_ms;
1.496     djm       618:        int was_addr, config_test = 0, opt_terminated = 0, want_final_pass = 0;
1.583     dtucker   619:        char *p, *cp, *line, *argv0, *logfile;
1.544     djm       620:        char cname[NI_MAXHOST], thishost[NI_MAXHOST];
1.31      markus    621:        struct stat st;
1.98      markus    622:        struct passwd *pw;
1.144     stevesk   623:        extern int optind, optreset;
                    624:        extern char *optarg;
1.406     millert   625:        struct Forward fwd;
1.385     djm       626:        struct addrinfo *addrs = NULL;
1.513     dtucker   627:        size_t n, len;
1.532     dtucker   628:        u_int j;
1.544     djm       629:        struct ssh_conn_info *cinfo = NULL;
1.250     djm       630:
                    631:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    632:        sanitise_stdfd();
1.31      markus    633:
1.33      markus    634:        /*
1.346     djm       635:         * Discard other fds that are hanging around. These can cause problem
                    636:         * with backgrounded ssh processes started by ControlPersist.
                    637:         */
                    638:        closefrom(STDERR_FILENO + 1);
                    639:
1.487     dtucker   640:        if (getuid() != geteuid())
                    641:                fatal("ssh setuid not supported.");
                    642:        if (getgid() != getegid())
                    643:                fatal("ssh setgid not supported.");
1.184     stevesk   644:
1.107     markus    645:        /* Get user data. */
1.490     dtucker   646:        pw = getpwuid(getuid());
1.107     markus    647:        if (!pw) {
1.490     dtucker   648:                logit("No user exists for uid %lu", (u_long)getuid());
1.257     dtucker   649:                exit(255);
1.107     markus    650:        }
                    651:        /* Take a copy of the returned structure. */
                    652:        pw = pwcopy(pw);
1.31      markus    653:
1.33      markus    654:        /*
                    655:         * Set our umask to something reasonable, as some files are created
                    656:         * with the default umask.  This will make them world-readable but
                    657:         * writable only by the owner, which is ok for all files for which we
                    658:         * don't set the modes explicitly.
                    659:         */
1.577     djm       660:        umask(022 | umask(077));
1.445     djm       661:
                    662:        setlocale(LC_CTYPE, "");
1.31      markus    663:
1.316     djm       664:        /*
                    665:         * Initialize option structure to indicate that no values have been
                    666:         * set.
                    667:         */
1.31      markus    668:        initialize_options(&options);
                    669:
1.463     djm       670:        /*
                    671:         * Prepare main ssh transport/connection structures
                    672:         */
                    673:        if ((ssh = ssh_alloc_session_state()) == NULL)
                    674:                fatal("Couldn't allocate session state");
                    675:        channel_init_channels(ssh);
                    676:
1.31      markus    677:        /* Parse command-line arguments. */
                    678:        host = NULL;
1.320     djm       679:        use_syslog = 0;
1.375     dtucker   680:        logfile = NULL;
1.325     markus    681:        argv0 = av[0];
1.31      markus    682:
1.266     djm       683:  again:
1.316     djm       684:        while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
1.591     djm       685:            "AB:CD:E:F:GI:J:KL:MNO:P:Q:R:S:TVw:W:XYy")) != -1) { /* HUZdhjruz */
1.31      markus    686:                switch (opt) {
1.91      jakob     687:                case '1':
1.454     djm       688:                        fatal("SSH protocol v.1 is no longer supported");
1.91      jakob     689:                        break;
1.47      markus    690:                case '2':
1.454     djm       691:                        /* Ignored */
1.47      markus    692:                        break;
1.37      markus    693:                case '4':
1.196     djm       694:                        options.address_family = AF_INET;
1.37      markus    695:                        break;
                    696:                case '6':
1.196     djm       697:                        options.address_family = AF_INET6;
1.37      markus    698:                        break;
1.31      markus    699:                case 'n':
1.563     djm       700:                        options.stdin_null = 1;
1.31      markus    701:                        break;
                    702:                case 'f':
1.564     djm       703:                        options.fork_after_authentication = 1;
1.563     djm       704:                        options.stdin_null = 1;
1.31      markus    705:                        break;
                    706:                case 'x':
                    707:                        options.forward_x11 = 0;
                    708:                        break;
                    709:                case 'X':
                    710:                        options.forward_x11 = 1;
                    711:                        break;
1.320     djm       712:                case 'y':
                    713:                        use_syslog = 1;
                    714:                        break;
1.375     dtucker   715:                case 'E':
1.422     dtucker   716:                        logfile = optarg;
1.375     dtucker   717:                        break;
1.408     djm       718:                case 'G':
                    719:                        config_test = 1;
                    720:                        break;
1.202     markus    721:                case 'Y':
                    722:                        options.forward_x11 = 1;
                    723:                        options.forward_x11_trusted = 1;
                    724:                        break;
1.31      markus    725:                case 'g':
1.406     millert   726:                        options.fwd_opts.gateway_ports = 1;
1.31      markus    727:                        break;
1.229     djm       728:                case 'O':
1.441     dtucker   729:                        if (options.stdio_forward_host != NULL)
1.332     djm       730:                                fatal("Cannot specify multiplexing "
                    731:                                    "command with -W");
                    732:                        else if (muxclient_command != 0)
                    733:                                fatal("Multiplexing command already specified");
1.229     djm       734:                        if (strcmp(optarg, "check") == 0)
1.312     djm       735:                                muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
1.338     markus    736:                        else if (strcmp(optarg, "forward") == 0)
                    737:                                muxclient_command = SSHMUX_COMMAND_FORWARD;
1.229     djm       738:                        else if (strcmp(optarg, "exit") == 0)
1.312     djm       739:                                muxclient_command = SSHMUX_COMMAND_TERMINATE;
1.357     djm       740:                        else if (strcmp(optarg, "stop") == 0)
                    741:                                muxclient_command = SSHMUX_COMMAND_STOP;
1.365     djm       742:                        else if (strcmp(optarg, "cancel") == 0)
                    743:                                muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
1.447     markus    744:                        else if (strcmp(optarg, "proxy") == 0)
                    745:                                muxclient_command = SSHMUX_COMMAND_PROXY;
1.229     djm       746:                        else
                    747:                                fatal("Invalid multiplex command.");
                    748:                        break;
1.591     djm       749:                case 'P':
                    750:                        if (options.tag == NULL)
                    751:                                options.tag = xstrdup(optarg);
1.376     djm       752:                        break;
1.394     deraadt   753:                case 'Q':
1.376     djm       754:                        cp = NULL;
1.519     dtucker   755:                        if (strcmp(optarg, "cipher") == 0 ||
                    756:                            strcasecmp(optarg, "Ciphers") == 0)
1.393     djm       757:                                cp = cipher_alg_list('\n', 0);
1.394     deraadt   758:                        else if (strcmp(optarg, "cipher-auth") == 0)
1.393     djm       759:                                cp = cipher_alg_list('\n', 1);
1.519     dtucker   760:                        else if (strcmp(optarg, "mac") == 0 ||
                    761:                            strcasecmp(optarg, "MACs") == 0)
1.392     dtucker   762:                                cp = mac_alg_list('\n');
1.519     dtucker   763:                        else if (strcmp(optarg, "kex") == 0 ||
                    764:                            strcasecmp(optarg, "KexAlgorithms") == 0)
1.392     dtucker   765:                                cp = kex_alg_list('\n');
1.394     deraadt   766:                        else if (strcmp(optarg, "key") == 0)
1.451     djm       767:                                cp = sshkey_alg_list(0, 0, 0, '\n');
1.396     markus    768:                        else if (strcmp(optarg, "key-cert") == 0)
1.451     djm       769:                                cp = sshkey_alg_list(1, 0, 0, '\n');
1.396     markus    770:                        else if (strcmp(optarg, "key-plain") == 0)
1.451     djm       771:                                cp = sshkey_alg_list(0, 1, 0, '\n');
1.589     djm       772:                        else if (strcmp(optarg, "key-ca-sign") == 0 ||
                    773:                            strcasecmp(optarg, "CASignatureAlgorithms") == 0)
                    774:                                cp = sshkey_alg_list(0, 1, 1, '\n');
1.519     dtucker   775:                        else if (strcmp(optarg, "key-sig") == 0 ||
1.547     dtucker   776:                            strcasecmp(optarg, "PubkeyAcceptedKeyTypes") == 0 || /* deprecated name */
                    777:                            strcasecmp(optarg, "PubkeyAcceptedAlgorithms") == 0 ||
1.519     dtucker   778:                            strcasecmp(optarg, "HostKeyAlgorithms") == 0 ||
1.548     dtucker   779:                            strcasecmp(optarg, "HostbasedKeyTypes") == 0 || /* deprecated name */
                    780:                            strcasecmp(optarg, "HostbasedAcceptedKeyTypes") == 0 || /* deprecated name */
                    781:                            strcasecmp(optarg, "HostbasedAcceptedAlgorithms") == 0)
1.519     dtucker   782:                                cp = sshkey_alg_list(0, 0, 1, '\n');
1.491     djm       783:                        else if (strcmp(optarg, "sig") == 0)
1.492     djm       784:                                cp = sshkey_alg_list(0, 1, 1, '\n');
1.491     djm       785:                        else if (strcmp(optarg, "protocol-version") == 0)
1.416     djm       786:                                cp = xstrdup("2");
1.513     dtucker   787:                        else if (strcmp(optarg, "compression") == 0) {
                    788:                                cp = xstrdup(compression_alg_list(0));
                    789:                                len = strlen(cp);
                    790:                                for (n = 0; n < len; n++)
                    791:                                        if (cp[n] == ',')
                    792:                                                cp[n] = '\n';
                    793:                        } else if (strcmp(optarg, "help") == 0) {
1.491     djm       794:                                cp = xstrdup(
1.513     dtucker   795:                                    "cipher\ncipher-auth\ncompression\nkex\n"
1.519     dtucker   796:                                    "key\nkey-cert\nkey-plain\nkey-sig\nmac\n"
1.491     djm       797:                                    "protocol-version\nsig");
1.416     djm       798:                        }
1.376     djm       799:                        if (cp == NULL)
                    800:                                fatal("Unsupported query \"%s\"", optarg);
                    801:                        printf("%s\n", cp);
                    802:                        free(cp);
                    803:                        exit(0);
1.31      markus    804:                        break;
                    805:                case 'a':
                    806:                        options.forward_agent = 0;
1.53      markus    807:                        break;
                    808:                case 'A':
                    809:                        options.forward_agent = 1;
1.31      markus    810:                        break;
                    811:                case 'k':
1.204     dtucker   812:                        options.gss_deleg_creds = 0;
1.297     djm       813:                        break;
                    814:                case 'K':
                    815:                        options.gss_authentication = 1;
                    816:                        options.gss_deleg_creds = 1;
1.31      markus    817:                        break;
                    818:                case 'i':
1.490     dtucker   819:                        p = tilde_expand_filename(optarg, getuid());
1.505     deraadt   820:                        if (stat(p, &st) == -1)
1.128     fgsch     821:                                fprintf(stderr, "Warning: Identity file %s "
1.429     dtucker   822:                                    "not accessible: %s.\n", p,
1.231     otto      823:                                    strerror(errno));
1.429     dtucker   824:                        else
                    825:                                add_identity_file(&options, NULL, p, 1);
                    826:                        free(p);
1.31      markus    827:                        break;
1.127     markus    828:                case 'I':
1.333     markus    829: #ifdef ENABLE_PKCS11
1.422     dtucker   830:                        free(options.pkcs11_provider);
1.333     markus    831:                        options.pkcs11_provider = xstrdup(optarg);
1.137     jakob     832: #else
1.333     markus    833:                        fprintf(stderr, "no support for PKCS#11.\n");
1.137     jakob     834: #endif
1.127     markus    835:                        break;
1.443     djm       836:                case 'J':
1.504     djm       837:                        if (options.jump_host != NULL) {
                    838:                                fatal("Only a single -J option is permitted "
                    839:                                    "(use commas to separate multiple "
                    840:                                    "jump hops)");
                    841:                        }
1.443     djm       842:                        if (options.proxy_command != NULL)
                    843:                                fatal("Cannot specify -J with ProxyCommand");
                    844:                        if (parse_jump(optarg, &options, 1) == -1)
                    845:                                fatal("Invalid -J argument");
                    846:                        options.proxy_command = xstrdup("none");
                    847:                        break;
1.31      markus    848:                case 't':
1.359     djm       849:                        if (options.request_tty == REQUEST_TTY_YES)
                    850:                                options.request_tty = REQUEST_TTY_FORCE;
                    851:                        else
                    852:                                options.request_tty = REQUEST_TTY_YES;
1.31      markus    853:                        break;
                    854:                case 'v':
1.197     markus    855:                        if (debug_flag == 0) {
1.66      markus    856:                                debug_flag = 1;
                    857:                                options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197     markus    858:                        } else {
1.443     djm       859:                                if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
                    860:                                        debug_flag++;
1.197     markus    861:                                        options.log_level++;
1.443     djm       862:                                }
1.197     markus    863:                        }
1.375     dtucker   864:                        break;
1.31      markus    865:                case 'V':
1.209     markus    866:                        fprintf(stderr, "%s, %s\n",
1.541     dtucker   867:                            SSH_VERSION, SSH_OPENSSL_VERSION);
1.584     millert   868:                        exit(0);
1.31      markus    869:                        break;
1.255     reyk      870:                case 'w':
1.256     reyk      871:                        if (options.tun_open == -1)
                    872:                                options.tun_open = SSH_TUNMODE_DEFAULT;
1.255     reyk      873:                        options.tun_local = a2tun(optarg, &options.tun_remote);
1.256     reyk      874:                        if (options.tun_local == SSH_TUNID_ERR) {
1.316     djm       875:                                fprintf(stderr,
                    876:                                    "Bad tun device '%s'\n", optarg);
1.257     dtucker   877:                                exit(255);
1.255     reyk      878:                        }
                    879:                        break;
1.331     dtucker   880:                case 'W':
1.441     dtucker   881:                        if (options.stdio_forward_host != NULL)
1.332     djm       882:                                fatal("stdio forward already specified");
                    883:                        if (muxclient_command != 0)
                    884:                                fatal("Cannot specify stdio forward with -O");
1.331     dtucker   885:                        if (parse_forward(&fwd, optarg, 1, 0)) {
1.590     dlg       886:                                options.stdio_forward_host =
                    887:                                    fwd.listen_port == PORT_STREAMLOCAL ?
                    888:                                    fwd.listen_path : fwd.listen_host;
1.441     dtucker   889:                                options.stdio_forward_port = fwd.listen_port;
1.378     djm       890:                                free(fwd.connect_host);
1.331     dtucker   891:                        } else {
                    892:                                fprintf(stderr,
                    893:                                    "Bad stdio forwarding specification '%s'\n",
                    894:                                    optarg);
                    895:                                exit(255);
                    896:                        }
1.359     djm       897:                        options.request_tty = REQUEST_TTY_NO;
1.560     djm       898:                        options.session_type = SESSION_TYPE_NONE;
1.331     dtucker   899:                        break;
1.31      markus    900:                case 'q':
                    901:                        options.log_level = SYSLOG_LEVEL_QUIET;
                    902:                        break;
                    903:                case 'e':
                    904:                        if (optarg[0] == '^' && optarg[2] == 0 &&
1.128     fgsch     905:                            (u_char) optarg[1] >= 64 &&
                    906:                            (u_char) optarg[1] < 128)
1.78      markus    907:                                options.escape_char = (u_char) optarg[1] & 31;
1.31      markus    908:                        else if (strlen(optarg) == 1)
1.78      markus    909:                                options.escape_char = (u_char) optarg[0];
1.31      markus    910:                        else if (strcmp(optarg, "none") == 0)
1.119     stevesk   911:                                options.escape_char = SSH_ESCAPECHAR_NONE;
1.31      markus    912:                        else {
1.128     fgsch     913:                                fprintf(stderr, "Bad escape character '%s'.\n",
                    914:                                    optarg);
1.257     dtucker   915:                                exit(255);
1.31      markus    916:                        }
                    917:                        break;
                    918:                case 'c':
1.506     naddy     919:                        if (!ciphers_valid(*optarg == '+' || *optarg == '^' ?
1.420     djm       920:                            optarg + 1 : optarg)) {
                    921:                                fprintf(stderr, "Unknown cipher type '%s'\n",
                    922:                                    optarg);
                    923:                                exit(255);
1.95      markus    924:                        }
1.456     djm       925:                        free(options.ciphers);
                    926:                        options.ciphers = xstrdup(optarg);
1.95      markus    927:                        break;
                    928:                case 'm':
1.422     dtucker   929:                        if (mac_valid(optarg)) {
                    930:                                free(options.macs);
1.95      markus    931:                                options.macs = xstrdup(optarg);
1.422     dtucker   932:                        } else {
1.128     fgsch     933:                                fprintf(stderr, "Unknown mac type '%s'\n",
                    934:                                    optarg);
1.257     dtucker   935:                                exit(255);
1.31      markus    936:                        }
                    937:                        break;
1.214     djm       938:                case 'M':
1.242     djm       939:                        if (options.control_master == SSHCTL_MASTER_YES)
                    940:                                options.control_master = SSHCTL_MASTER_ASK;
                    941:                        else
                    942:                                options.control_master = SSHCTL_MASTER_YES;
1.214     djm       943:                        break;
1.31      markus    944:                case 'p':
1.465     millert   945:                        if (options.port == -1) {
                    946:                                options.port = a2port(optarg);
                    947:                                if (options.port <= 0) {
                    948:                                        fprintf(stderr, "Bad port '%s'\n",
                    949:                                            optarg);
                    950:                                        exit(255);
                    951:                                }
1.109     markus    952:                        }
1.31      markus    953:                        break;
                    954:                case 'l':
1.465     millert   955:                        if (options.user == NULL)
                    956:                                options.user = optarg;
1.31      markus    957:                        break;
1.141     stevesk   958:
                    959:                case 'L':
1.324     djm       960:                        if (parse_forward(&fwd, optarg, 0, 0))
1.232     djm       961:                                add_local_forward(&options, &fwd);
                    962:                        else {
1.128     fgsch     963:                                fprintf(stderr,
1.232     djm       964:                                    "Bad local forwarding specification '%s'\n",
1.128     fgsch     965:                                    optarg);
1.257     dtucker   966:                                exit(255);
1.31      markus    967:                        }
1.232     djm       968:                        break;
                    969:
                    970:                case 'R':
1.464     markus    971:                        if (parse_forward(&fwd, optarg, 0, 1) ||
                    972:                            parse_forward(&fwd, optarg, 1, 1)) {
1.232     djm       973:                                add_remote_forward(&options, &fwd);
                    974:                        } else {
1.128     fgsch     975:                                fprintf(stderr,
1.232     djm       976:                                    "Bad remote forwarding specification "
                    977:                                    "'%s'\n", optarg);
1.257     dtucker   978:                                exit(255);
1.31      markus    979:                        }
                    980:                        break;
1.108     markus    981:
                    982:                case 'D':
1.324     djm       983:                        if (parse_forward(&fwd, optarg, 1, 0)) {
1.322     stevesk   984:                                add_local_forward(&options, &fwd);
1.232     djm       985:                        } else {
1.322     stevesk   986:                                fprintf(stderr,
                    987:                                    "Bad dynamic forwarding specification "
                    988:                                    "'%s'\n", optarg);
1.257     dtucker   989:                                exit(255);
1.109     markus    990:                        }
1.108     markus    991:                        break;
                    992:
1.31      markus    993:                case 'C':
1.513     dtucker   994: #ifdef WITH_ZLIB
1.31      markus    995:                        options.compression = 1;
1.513     dtucker   996: #else
                    997:                        error("Compression not supported, disabling.");
                    998: #endif
1.31      markus    999:                        break;
1.45      markus   1000:                case 'N':
1.560     djm      1001:                        if (options.session_type != -1 &&
                   1002:                            options.session_type != SESSION_TYPE_NONE)
                   1003:                                fatal("Cannot specify -N with -s/SessionType");
                   1004:                        options.session_type = SESSION_TYPE_NONE;
1.359     djm      1005:                        options.request_tty = REQUEST_TTY_NO;
1.45      markus   1006:                        break;
                   1007:                case 'T':
1.359     djm      1008:                        options.request_tty = REQUEST_TTY_NO;
1.45      markus   1009:                        break;
1.31      markus   1010:                case 'o':
1.205     markus   1011:                        line = xstrdup(optarg);
1.408     djm      1012:                        if (process_config_line(&options, pw,
                   1013:                            host ? host : "", host ? host : "", line,
                   1014:                            "command-line", 0, NULL, SSHCONF_USERCONF) != 0)
1.257     dtucker  1015:                                exit(255);
1.378     djm      1016:                        free(line);
1.31      markus   1017:                        break;
1.85      djm      1018:                case 's':
1.560     djm      1019:                        if (options.session_type != -1 &&
                   1020:                            options.session_type != SESSION_TYPE_SUBSYSTEM)
                   1021:                                fatal("Cannot specify -s with -N/SessionType");
                   1022:                        options.session_type = SESSION_TYPE_SUBSYSTEM;
1.117     markus   1023:                        break;
1.214     djm      1024:                case 'S':
1.431     mmcc     1025:                        free(options.control_path);
1.214     djm      1026:                        options.control_path = xstrdup(optarg);
                   1027:                        break;
1.117     markus   1028:                case 'b':
                   1029:                        options.bind_address = optarg;
1.474     djm      1030:                        break;
                   1031:                case 'B':
                   1032:                        options.bind_interface = optarg;
1.85      djm      1033:                        break;
1.139     markus   1034:                case 'F':
                   1035:                        config = optarg;
                   1036:                        break;
1.31      markus   1037:                default:
                   1038:                        usage();
1.1       deraadt  1039:                }
1.31      markus   1040:        }
                   1041:
1.462     djm      1042:        if (optind > 1 && strcmp(av[optind - 1], "--") == 0)
                   1043:                opt_terminated = 1;
                   1044:
1.128     fgsch    1045:        ac -= optind;
                   1046:        av += optind;
                   1047:
1.329     guenther 1048:        if (ac > 0 && !host) {
1.465     millert  1049:                int tport;
                   1050:                char *tuser;
                   1051:                switch (parse_ssh_uri(*av, &tuser, &host, &tport)) {
                   1052:                case -1:
                   1053:                        usage();
                   1054:                        break;
                   1055:                case 0:
                   1056:                        if (options.user == NULL) {
                   1057:                                options.user = tuser;
                   1058:                                tuser = NULL;
                   1059:                        }
                   1060:                        free(tuser);
                   1061:                        if (options.port == -1 && tport != -1)
                   1062:                                options.port = tport;
                   1063:                        break;
                   1064:                default:
1.128     fgsch    1065:                        p = xstrdup(*av);
1.188     markus   1066:                        cp = strrchr(p, '@');
1.465     millert  1067:                        if (cp != NULL) {
                   1068:                                if (cp == p)
                   1069:                                        usage();
                   1070:                                if (options.user == NULL) {
                   1071:                                        options.user = p;
                   1072:                                        p = NULL;
                   1073:                                }
                   1074:                                *cp++ = '\0';
                   1075:                                host = xstrdup(cp);
                   1076:                                free(p);
                   1077:                        } else
                   1078:                                host = p;
                   1079:                        break;
                   1080:                }
1.462     djm      1081:                if (ac > 1 && !opt_terminated) {
1.189     millert  1082:                        optind = optreset = 1;
1.128     fgsch    1083:                        goto again;
                   1084:                }
1.189     millert  1085:                ac--, av++;
1.128     fgsch    1086:        }
                   1087:
1.31      markus   1088:        /* Check that we got a host name. */
                   1089:        if (!host)
                   1090:                usage();
                   1091:
1.583     dtucker  1092:        options.host_arg = xstrdup(host);
1.385     djm      1093:
1.402     markus   1094: #ifdef WITH_OPENSSL
1.350     djm      1095:        OpenSSL_add_all_algorithms();
1.72      markus   1096:        ERR_load_crypto_strings();
1.402     markus   1097: #endif
1.31      markus   1098:
                   1099:        /* Initialize the command to execute on remote host. */
1.482     markus   1100:        if ((command = sshbuf_new()) == NULL)
                   1101:                fatal("sshbuf_new failed");
1.1       deraadt  1102:
1.33      markus   1103:        /*
                   1104:         * Save the command to execute on the remote host in a buffer. There
                   1105:         * is no limit on the length of the command, except by the maximum
                   1106:         * packet size.  Also sets the tty flag if there is no command.
                   1107:         */
1.128     fgsch    1108:        if (!ac) {
1.31      markus   1109:                /* No command specified - execute shell on a tty. */
1.560     djm      1110:                if (options.session_type == SESSION_TYPE_SUBSYSTEM) {
1.128     fgsch    1111:                        fprintf(stderr,
                   1112:                            "You must specify a subsystem to invoke.\n");
1.85      djm      1113:                        usage();
                   1114:                }
1.31      markus   1115:        } else {
1.128     fgsch    1116:                /* A command has been specified.  Store it into the buffer. */
                   1117:                for (i = 0; i < ac; i++) {
1.482     markus   1118:                        if ((r = sshbuf_putf(command, "%s%s",
                   1119:                            i ? " " : "", av[i])) != 0)
1.540     djm      1120:                                fatal_fr(r, "buffer error");
1.31      markus   1121:                }
                   1122:        }
                   1123:
1.575     djm      1124:        ssh_signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
                   1125:
1.101     markus   1126:        /*
                   1127:         * Initialize "log" output.  Since we are the client all output
1.375     dtucker  1128:         * goes to stderr unless otherwise specified by -y or -E.
1.101     markus   1129:         */
1.375     dtucker  1130:        if (use_syslog && logfile != NULL)
                   1131:                fatal("Can't specify both -y and -E");
1.422     dtucker  1132:        if (logfile != NULL)
1.375     dtucker  1133:                log_redirect_stderr_to(logfile);
1.325     markus   1134:        log_init(argv0,
1.468     djm      1135:            options.log_level == SYSLOG_LEVEL_NOT_SET ?
1.452     dtucker  1136:            SYSLOG_LEVEL_INFO : options.log_level,
1.468     djm      1137:            options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1.452     dtucker  1138:            SYSLOG_FACILITY_USER : options.log_facility,
                   1139:            !use_syslog);
1.375     dtucker  1140:
                   1141:        if (debug_flag)
1.541     dtucker  1142:                logit("%s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
1.31      markus   1143:
1.400     djm      1144:        /* Parse the configuration files */
1.583     dtucker  1145:        process_config_files(options.host_arg, pw, 0, &want_final_pass);
1.496     djm      1146:        if (want_final_pass)
                   1147:                debug("configuration requests final Match pass");
1.400     djm      1148:
                   1149:        /* Hostname canonicalisation needs a few options filled. */
                   1150:        fill_default_options_for_canonicalization(&options);
                   1151:
                   1152:        /* If the user has replaced the hostname then take it into use now */
                   1153:        if (options.hostname != NULL) {
                   1154:                /* NB. Please keep in sync with readconf.c:match_cfg_line() */
                   1155:                cp = percent_expand(options.hostname,
                   1156:                    "h", host, (char *)NULL);
                   1157:                free(host);
                   1158:                host = cp;
1.408     djm      1159:                free(options.hostname);
                   1160:                options.hostname = xstrdup(host);
1.400     djm      1161:        }
                   1162:
1.470     djm      1163:        /* Don't lowercase addresses, they will be explicitly canonicalised */
                   1164:        if ((was_addr = is_addr(host)) == 0)
                   1165:                lowercase(host);
                   1166:
                   1167:        /*
                   1168:         * Try to canonicalize if requested by configuration or the
                   1169:         * hostname is an address.
                   1170:         */
                   1171:        if (options.canonicalize_hostname != SSH_CANONICALISE_NO || was_addr)
1.400     djm      1172:                addrs = resolve_canonicalize(&host, options.port);
                   1173:
1.139     markus   1174:        /*
1.401     djm      1175:         * If CanonicalizePermittedCNAMEs have been specified but
                   1176:         * other canonicalization did not happen (by not being requested
                   1177:         * or by failing with fallback) then the hostname may still be changed
1.468     djm      1178:         * as a result of CNAME following.
1.401     djm      1179:         *
                   1180:         * Try to resolve the bare hostname name using the system resolver's
                   1181:         * usual search rules and then apply the CNAME follow rules.
                   1182:         *
                   1183:         * Skip the lookup if a ProxyCommand is being used unless the user
                   1184:         * has specifically requested canonicalisation for this case via
                   1185:         * CanonicalizeHostname=always
1.139     markus   1186:         */
1.443     djm      1187:        direct = option_clear_or_none(options.proxy_command) &&
1.588     djm      1188:            option_clear_or_none(options.jump_host);
1.568     djm      1189:        if (addrs == NULL && config_has_permitted_cnames(&options) && (direct ||
1.443     djm      1190:            options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
1.403     djm      1191:                if ((addrs = resolve_host(host, options.port,
1.493     djm      1192:                    direct, cname, sizeof(cname))) == NULL) {
1.403     djm      1193:                        /* Don't fatal proxied host names not in the DNS */
1.493     djm      1194:                        if (direct)
1.403     djm      1195:                                cleanup_exit(255); /* logged in resolve_host */
                   1196:                } else
1.443     djm      1197:                        check_follow_cname(direct, &host, cname);
1.400     djm      1198:        }
1.139     markus   1199:
1.400     djm      1200:        /*
1.408     djm      1201:         * If canonicalisation is enabled then re-parse the configuration
                   1202:         * files as new stanzas may match.
1.400     djm      1203:         */
1.496     djm      1204:        if (options.canonicalize_hostname != 0 && !want_final_pass) {
                   1205:                debug("hostname canonicalisation enabled, "
                   1206:                    "will re-parse configuration");
                   1207:                want_final_pass = 1;
                   1208:        }
                   1209:
                   1210:        if (want_final_pass) {
                   1211:                debug("re-parsing configuration");
1.408     djm      1212:                free(options.hostname);
                   1213:                options.hostname = xstrdup(host);
1.583     dtucker  1214:                process_config_files(options.host_arg, pw, 1, NULL);
1.408     djm      1215:                /*
                   1216:                 * Address resolution happens early with canonicalisation
                   1217:                 * enabled and the port number may have changed since, so
                   1218:                 * reset it in address list
                   1219:                 */
                   1220:                if (addrs != NULL && options.port > 0)
                   1221:                        set_addrinfo_port(addrs, options.port);
1.139     markus   1222:        }
1.31      markus   1223:
                   1224:        /* Fill configuration defaults. */
1.543     djm      1225:        if (fill_default_options(&options) != 0)
                   1226:                cleanup_exit(255);
1.443     djm      1227:
1.534     dtucker  1228:        if (options.user == NULL)
                   1229:                options.user = xstrdup(pw->pw_name);
                   1230:
1.443     djm      1231:        /*
                   1232:         * If ProxyJump option specified, then construct a ProxyCommand now.
                   1233:         */
                   1234:        if (options.jump_host != NULL) {
                   1235:                char port_s[8];
1.534     dtucker  1236:                const char *jumpuser = options.jump_user, *sshbin = argv0;
1.520     dtucker  1237:                int port = options.port, jumpport = options.jump_port;
                   1238:
                   1239:                if (port <= 0)
                   1240:                        port = default_ssh_port();
                   1241:                if (jumpport <= 0)
                   1242:                        jumpport = default_ssh_port();
1.534     dtucker  1243:                if (jumpuser == NULL)
                   1244:                        jumpuser = options.user;
                   1245:                if (strcmp(options.jump_host, host) == 0 && port == jumpport &&
                   1246:                    strcmp(options.user, jumpuser) == 0)
1.520     dtucker  1247:                        fatal("jumphost loop via %s", options.jump_host);
1.478     djm      1248:
                   1249:                /*
                   1250:                 * Try to use SSH indicated by argv[0], but fall back to
                   1251:                 * "ssh" if it appears unavailable.
                   1252:                 */
                   1253:                if (strchr(argv0, '/') != NULL && access(argv0, X_OK) != 0)
                   1254:                        sshbin = "ssh";
1.443     djm      1255:
                   1256:                /* Consistency check */
                   1257:                if (options.proxy_command != NULL)
                   1258:                        fatal("inconsistent options: ProxyCommand+ProxyJump");
                   1259:                /* Never use FD passing for ProxyJump */
                   1260:                options.proxy_use_fdpass = 0;
                   1261:                snprintf(port_s, sizeof(port_s), "%d", options.jump_port);
                   1262:                xasprintf(&options.proxy_command,
1.478     djm      1263:                    "%s%s%s%s%s%s%s%s%s%s%.*s -W '[%%h]:%%p' %s",
                   1264:                    sshbin,
1.443     djm      1265:                    /* Optional "-l user" argument if jump_user set */
                   1266:                    options.jump_user == NULL ? "" : " -l ",
                   1267:                    options.jump_user == NULL ? "" : options.jump_user,
                   1268:                    /* Optional "-p port" argument if jump_port set */
                   1269:                    options.jump_port <= 0 ? "" : " -p ",
                   1270:                    options.jump_port <= 0 ? "" : port_s,
                   1271:                    /* Optional additional jump hosts ",..." */
                   1272:                    options.jump_extra == NULL ? "" : " -J ",
                   1273:                    options.jump_extra == NULL ? "" : options.jump_extra,
1.571     jsg      1274:                    /* Optional "-F" argument if -F specified */
1.443     djm      1275:                    config == NULL ? "" : " -F ",
                   1276:                    config == NULL ? "" : config,
                   1277:                    /* Optional "-v" arguments if -v set */
                   1278:                    debug_flag ? " -" : "",
                   1279:                    debug_flag, "vvv",
                   1280:                    /* Mandatory hostname */
                   1281:                    options.jump_host);
                   1282:                debug("Setting implicit ProxyCommand from ProxyJump: %s",
                   1283:                    options.proxy_command);
                   1284:        }
1.31      markus   1285:
1.400     djm      1286:        if (options.port == 0)
                   1287:                options.port = default_ssh_port();
1.463     djm      1288:        channel_set_af(ssh, options.address_family);
1.196     djm      1289:
1.383     djm      1290:        /* Tidy and check options */
                   1291:        if (options.host_key_alias != NULL)
                   1292:                lowercase(options.host_key_alias);
                   1293:        if (options.proxy_command != NULL &&
                   1294:            strcmp(options.proxy_command, "-") == 0 &&
                   1295:            options.proxy_use_fdpass)
                   1296:                fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
1.516     markus   1297:        if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
                   1298:                if (options.control_persist && options.control_path != NULL) {
                   1299:                        debug("UpdateHostKeys=ask is incompatible with "
                   1300:                            "ControlPersist; disabling");
                   1301:                        options.update_hostkeys = 0;
                   1302:                } else if (sshbuf_len(command) != 0 ||
                   1303:                    options.remote_command != NULL ||
                   1304:                    options.request_tty == REQUEST_TTY_NO) {
                   1305:                        debug("UpdateHostKeys=ask is incompatible with "
                   1306:                            "remote command execution; disabling");
1.517     djm      1307:                        options.update_hostkeys = 0;
                   1308:                } else if (options.log_level < SYSLOG_LEVEL_INFO) {
                   1309:                        /* no point logging anything; user won't see it */
1.516     markus   1310:                        options.update_hostkeys = 0;
                   1311:                }
1.415     djm      1312:        }
1.430     djm      1313:        if (options.connection_attempts <= 0)
                   1314:                fatal("Invalid number of ConnectionAttempts");
                   1315:
1.482     markus   1316:        if (sshbuf_len(command) != 0 && options.remote_command != NULL)
1.461     bluhm    1317:                fatal("Cannot execute command-line and remote command.");
                   1318:
                   1319:        /* Cannot fork to background if no command. */
1.564     djm      1320:        if (options.fork_after_authentication && sshbuf_len(command) == 0 &&
1.562     djm      1321:            options.remote_command == NULL &&
                   1322:            options.session_type != SESSION_TYPE_NONE)
1.461     bluhm    1323:                fatal("Cannot fork into background without a command "
                   1324:                    "to execute.");
                   1325:
1.31      markus   1326:        /* reinit */
1.452     dtucker  1327:        log_init(argv0, options.log_level, options.log_facility, !use_syslog);
1.559     djm      1328:        for (j = 0; j < options.num_log_verbose; j++) {
                   1329:                if (strcasecmp(options.log_verbose[j], "none") == 0)
                   1330:                        break;
1.539     djm      1331:                log_verbose_add(options.log_verbose[j]);
1.559     djm      1332:        }
1.370     djm      1333:
                   1334:        if (options.request_tty == REQUEST_TTY_YES ||
                   1335:            options.request_tty == REQUEST_TTY_FORCE)
                   1336:                tty_flag = 1;
                   1337:
                   1338:        /* Allocate a tty by default if no command specified. */
1.482     markus   1339:        if (sshbuf_len(command) == 0 && options.remote_command == NULL)
1.370     djm      1340:                tty_flag = options.request_tty != REQUEST_TTY_NO;
                   1341:
                   1342:        /* Force no tty */
1.447     markus   1343:        if (options.request_tty == REQUEST_TTY_NO ||
1.570     djm      1344:            (muxclient_command && muxclient_command != SSHMUX_COMMAND_PROXY) ||
                   1345:            options.session_type == SESSION_TYPE_NONE)
1.370     djm      1346:                tty_flag = 0;
                   1347:        /* Do not allocate a tty if stdin is not a tty. */
1.563     djm      1348:        if ((!isatty(fileno(stdin)) || options.stdin_null) &&
1.370     djm      1349:            options.request_tty != REQUEST_TTY_FORCE) {
                   1350:                if (tty_flag)
                   1351:                        logit("Pseudo-terminal will not be allocated because "
                   1352:                            "stdin is not a terminal.");
                   1353:                tty_flag = 0;
                   1354:        }
1.343     djm      1355:
1.466     djm      1356:        /* Set up strings used to percent_expand() arguments */
1.544     djm      1357:        cinfo = xcalloc(1, sizeof(*cinfo));
1.358     djm      1358:        if (gethostname(thishost, sizeof(thishost)) == -1)
                   1359:                fatal("gethostname: %s", strerror(errno));
1.544     djm      1360:        cinfo->thishost = xstrdup(thishost);
                   1361:        thishost[strcspn(thishost, ".")] = '\0';
                   1362:        cinfo->shorthost = xstrdup(thishost);
                   1363:        xasprintf(&cinfo->portstr, "%d", options.port);
                   1364:        xasprintf(&cinfo->uidstr, "%llu",
1.479     djm      1365:            (unsigned long long)pw->pw_uid);
1.544     djm      1366:        cinfo->keyalias = xstrdup(options.host_key_alias ?
1.583     dtucker  1367:            options.host_key_alias : options.host_arg);
1.544     djm      1368:        cinfo->conn_hash_hex = ssh_connection_hash(cinfo->thishost, host,
                   1369:            cinfo->portstr, options.user);
1.583     dtucker  1370:        cinfo->host_arg = xstrdup(options.host_arg);
1.544     djm      1371:        cinfo->remhost = xstrdup(host);
                   1372:        cinfo->remuser = xstrdup(options.user);
                   1373:        cinfo->homedir = xstrdup(pw->pw_dir);
                   1374:        cinfo->locuser = xstrdup(pw->pw_name);
1.405     djm      1375:
1.466     djm      1376:        /*
                   1377:         * Expand tokens in arguments. NB. LocalCommand is expanded later,
                   1378:         * after port-forwarding is set up, so it may pick up any local
                   1379:         * tunnel interface name allocated.
                   1380:         */
1.461     bluhm    1381:        if (options.remote_command != NULL) {
                   1382:                debug3("expanding RemoteCommand: %s", options.remote_command);
                   1383:                cp = options.remote_command;
1.527     dtucker  1384:                options.remote_command = default_client_percent_expand(cp,
1.544     djm      1385:                    cinfo);
1.461     bluhm    1386:                debug3("expanded RemoteCommand: %s", options.remote_command);
                   1387:                free(cp);
1.482     markus   1388:                if ((r = sshbuf_put(command, options.remote_command,
                   1389:                    strlen(options.remote_command))) != 0)
1.540     djm      1390:                        fatal_fr(r, "buffer error");
1.304     dtucker  1391:        }
1.31      markus   1392:
1.214     djm      1393:        if (options.control_path != NULL) {
1.490     dtucker  1394:                cp = tilde_expand_filename(options.control_path, getuid());
1.378     djm      1395:                free(options.control_path);
1.528     dtucker  1396:                options.control_path = default_client_percent_dollar_expand(cp,
1.544     djm      1397:                    cinfo);
1.378     djm      1398:                free(cp);
1.214     djm      1399:        }
1.408     djm      1400:
1.522     dtucker  1401:        if (options.identity_agent != NULL) {
                   1402:                p = tilde_expand_filename(options.identity_agent, getuid());
1.544     djm      1403:                cp = default_client_percent_dollar_expand(p, cinfo);
1.522     dtucker  1404:                free(p);
                   1405:                free(options.identity_agent);
                   1406:                options.identity_agent = cp;
1.586     dtucker  1407:        }
                   1408:
                   1409:        if (options.revoked_host_keys != NULL) {
                   1410:                p = tilde_expand_filename(options.revoked_host_keys, getuid());
                   1411:                cp = default_client_percent_dollar_expand(p, cinfo);
                   1412:                free(p);
                   1413:                free(options.revoked_host_keys);
                   1414:                options.revoked_host_keys = cp;
1.522     dtucker  1415:        }
                   1416:
                   1417:        if (options.forward_agent_sock_path != NULL) {
                   1418:                p = tilde_expand_filename(options.forward_agent_sock_path,
                   1419:                    getuid());
1.544     djm      1420:                cp = default_client_percent_dollar_expand(p, cinfo);
1.522     dtucker  1421:                free(p);
                   1422:                free(options.forward_agent_sock_path);
                   1423:                options.forward_agent_sock_path = cp;
1.552     djm      1424:                if (stat(options.forward_agent_sock_path, &st) != 0) {
                   1425:                        error("Cannot forward agent socket path \"%s\": %s",
                   1426:                            options.forward_agent_sock_path, strerror(errno));
                   1427:                        if (options.exit_on_forward_failure)
                   1428:                                cleanup_exit(255);
                   1429:                }
1.532     dtucker  1430:        }
                   1431:
1.545     djm      1432:        if (options.num_system_hostfiles > 0 &&
                   1433:            strcasecmp(options.system_hostfiles[0], "none") == 0) {
                   1434:                if (options.num_system_hostfiles > 1)
                   1435:                        fatal("Invalid GlobalKnownHostsFiles: \"none\" "
                   1436:                            "appears with other entries");
                   1437:                free(options.system_hostfiles[0]);
                   1438:                options.system_hostfiles[0] = NULL;
                   1439:                options.num_system_hostfiles = 0;
                   1440:        }
                   1441:
                   1442:        if (options.num_user_hostfiles > 0 &&
                   1443:            strcasecmp(options.user_hostfiles[0], "none") == 0) {
                   1444:                if (options.num_user_hostfiles > 1)
                   1445:                        fatal("Invalid UserKnownHostsFiles: \"none\" "
                   1446:                            "appears with other entries");
                   1447:                free(options.user_hostfiles[0]);
                   1448:                options.user_hostfiles[0] = NULL;
                   1449:                options.num_user_hostfiles = 0;
                   1450:        }
1.532     dtucker  1451:        for (j = 0; j < options.num_user_hostfiles; j++) {
1.545     djm      1452:                if (options.user_hostfiles[j] == NULL)
                   1453:                        continue;
                   1454:                cp = tilde_expand_filename(options.user_hostfiles[j], getuid());
                   1455:                p = default_client_percent_dollar_expand(cp, cinfo);
                   1456:                if (strcmp(options.user_hostfiles[j], p) != 0)
                   1457:                        debug3("expanded UserKnownHostsFile '%s' -> "
                   1458:                            "'%s'", options.user_hostfiles[j], p);
                   1459:                free(options.user_hostfiles[j]);
                   1460:                free(cp);
                   1461:                options.user_hostfiles[j] = p;
1.522     dtucker  1462:        }
                   1463:
1.527     dtucker  1464:        for (i = 0; i < options.num_local_forwards; i++) {
                   1465:                if (options.local_forwards[i].listen_path != NULL) {
                   1466:                        cp = options.local_forwards[i].listen_path;
                   1467:                        p = options.local_forwards[i].listen_path =
1.544     djm      1468:                            default_client_percent_expand(cp, cinfo);
1.527     dtucker  1469:                        if (strcmp(cp, p) != 0)
                   1470:                                debug3("expanded LocalForward listen path "
                   1471:                                    "'%s' -> '%s'", cp, p);
                   1472:                        free(cp);
                   1473:                }
                   1474:                if (options.local_forwards[i].connect_path != NULL) {
                   1475:                        cp = options.local_forwards[i].connect_path;
                   1476:                        p = options.local_forwards[i].connect_path =
1.544     djm      1477:                            default_client_percent_expand(cp, cinfo);
1.527     dtucker  1478:                        if (strcmp(cp, p) != 0)
                   1479:                                debug3("expanded LocalForward connect path "
                   1480:                                    "'%s' -> '%s'", cp, p);
                   1481:                        free(cp);
                   1482:                }
                   1483:        }
                   1484:
                   1485:        for (i = 0; i < options.num_remote_forwards; i++) {
                   1486:                if (options.remote_forwards[i].listen_path != NULL) {
                   1487:                        cp = options.remote_forwards[i].listen_path;
                   1488:                        p = options.remote_forwards[i].listen_path =
1.544     djm      1489:                            default_client_percent_expand(cp, cinfo);
1.527     dtucker  1490:                        if (strcmp(cp, p) != 0)
                   1491:                                debug3("expanded RemoteForward listen path "
                   1492:                                    "'%s' -> '%s'", cp, p);
                   1493:                        free(cp);
                   1494:                }
                   1495:                if (options.remote_forwards[i].connect_path != NULL) {
                   1496:                        cp = options.remote_forwards[i].connect_path;
                   1497:                        p = options.remote_forwards[i].connect_path =
1.544     djm      1498:                            default_client_percent_expand(cp, cinfo);
1.527     dtucker  1499:                        if (strcmp(cp, p) != 0)
                   1500:                                debug3("expanded RemoteForward connect path "
                   1501:                                    "'%s' -> '%s'", cp, p);
                   1502:                        free(cp);
                   1503:                }
                   1504:        }
                   1505:
1.408     djm      1506:        if (config_test) {
                   1507:                dump_client_config(&options, host);
                   1508:                exit(0);
1.508     djm      1509:        }
                   1510:
                   1511:        /* Expand SecurityKeyProvider if it refers to an environment variable */
                   1512:        if (options.sk_provider != NULL && *options.sk_provider == '$' &&
                   1513:            strlen(options.sk_provider) > 1) {
                   1514:                if ((cp = getenv(options.sk_provider + 1)) == NULL) {
1.518     naddy    1515:                        debug("Authenticator provider %s did not resolve; "
1.508     djm      1516:                            "disabling", options.sk_provider);
                   1517:                        free(options.sk_provider);
                   1518:                        options.sk_provider = NULL;
                   1519:                } else {
                   1520:                        debug2("resolved SecurityKeyProvider %s => %s",
                   1521:                            options.sk_provider, cp);
                   1522:                        free(options.sk_provider);
                   1523:                        options.sk_provider = xstrdup(cp);
                   1524:                }
1.408     djm      1525:        }
1.405     djm      1526:
1.312     djm      1527:        if (muxclient_command != 0 && options.control_path == NULL)
1.240     djm      1528:                fatal("No ControlPath specified for \"-O\" command");
1.447     markus   1529:        if (options.control_path != NULL) {
                   1530:                int sock;
                   1531:                if ((sock = muxclient(options.control_path)) >= 0) {
1.463     djm      1532:                        ssh_packet_set_connection(ssh, sock, sock);
1.499     djm      1533:                        ssh_packet_set_mux(ssh);
1.447     markus   1534:                        goto skip_connect;
                   1535:                }
                   1536:        }
1.401     djm      1537:
                   1538:        /*
                   1539:         * If hostname canonicalisation was not enabled, then we may not
                   1540:         * have yet resolved the hostname. Do so now.
                   1541:         */
                   1542:        if (addrs == NULL && options.proxy_command == NULL) {
1.421     djm      1543:                debug2("resolving \"%s\" port %d", host, options.port);
1.401     djm      1544:                if ((addrs = resolve_host(host, options.port, 1,
                   1545:                    cname, sizeof(cname))) == NULL)
                   1546:                        cleanup_exit(255); /* resolve_host logs the error */
                   1547:        }
1.214     djm      1548:
1.542     dtucker  1549:        if (options.connection_timeout >= INT_MAX/1000)
                   1550:                timeout_ms = INT_MAX;
                   1551:        else
                   1552:                timeout_ms = options.connection_timeout * 1000;
1.303     djm      1553:
1.77      markus   1554:        /* Open a connection to the remote host. */
1.583     dtucker  1555:        if (ssh_connect(ssh, host, options.host_arg, addrs, &hostaddr,
                   1556:            options.port, options.connection_attempts,
1.488     dtucker  1557:            &timeout_ms, options.tcp_keep_alive) != 0)
1.257     dtucker  1558:                exit(255);
1.31      markus   1559:
1.391     djm      1560:        if (addrs != NULL)
                   1561:                freeaddrinfo(addrs);
                   1562:
1.499     djm      1563:        ssh_packet_set_timeout(ssh, options.server_alive_interval,
1.385     djm      1564:            options.server_alive_count_max);
                   1565:
1.303     djm      1566:        if (timeout_ms > 0)
                   1567:                debug3("timeout: %d ms remain after connect", timeout_ms);
                   1568:
1.33      markus   1569:        /*
1.485     dtucker  1570:         * If we successfully made the connection and we have hostbased auth
                   1571:         * enabled, load the public keys so we can later use the ssh-keysign
                   1572:         * helper to sign challenges.
1.33      markus   1573:         */
1.112     markus   1574:        sensitive_data.nkeys = 0;
                   1575:        sensitive_data.keys = NULL;
1.457     djm      1576:        if (options.hostbased_authentication) {
1.581     dtucker  1577:                int loaded = 0;
                   1578:
1.486     dtucker  1579:                sensitive_data.nkeys = 10;
1.274     deraadt  1580:                sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.578     jsg      1581:                    sizeof(*sensitive_data.keys));
1.483     markus   1582:
                   1583:                /* XXX check errors? */
1.486     dtucker  1584: #define L_PUBKEY(p,o) do { \
                   1585:        if ((o) >= sensitive_data.nkeys) \
1.540     djm      1586:                fatal_f("pubkey out of array bounds"); \
1.483     markus   1587:        check_load(sshkey_load_public(p, &(sensitive_data.keys[o]), NULL), \
1.576     djm      1588:            &(sensitive_data.keys[o]), p, "pubkey"); \
1.581     dtucker  1589:        if (sensitive_data.keys[o] != NULL) { \
1.572     djm      1590:                debug2("hostbased key %d: %s key from \"%s\"", o, \
                   1591:                    sshkey_ssh_name(sensitive_data.keys[o]), p); \
1.581     dtucker  1592:                loaded++; \
                   1593:        } \
1.486     dtucker  1594: } while (0)
                   1595: #define L_CERT(p,o) do { \
                   1596:        if ((o) >= sensitive_data.nkeys) \
1.540     djm      1597:                fatal_f("cert out of array bounds"); \
1.576     djm      1598:        check_load(sshkey_load_cert(p, &(sensitive_data.keys[o])), \
                   1599:            &(sensitive_data.keys[o]), p, "cert"); \
1.581     dtucker  1600:        if (sensitive_data.keys[o] != NULL) { \
1.572     djm      1601:                debug2("hostbased key %d: %s cert from \"%s\"", o, \
                   1602:                    sshkey_ssh_name(sensitive_data.keys[o]), p); \
1.581     dtucker  1603:                loaded++; \
                   1604:        } \
1.486     dtucker  1605: } while (0)
1.177     markus   1606:
1.485     dtucker  1607:                if (options.hostbased_authentication == 1) {
1.486     dtucker  1608:                        L_CERT(_PATH_HOST_ECDSA_KEY_FILE, 0);
                   1609:                        L_CERT(_PATH_HOST_ED25519_KEY_FILE, 1);
                   1610:                        L_CERT(_PATH_HOST_RSA_KEY_FILE, 2);
                   1611:                        L_CERT(_PATH_HOST_DSA_KEY_FILE, 3);
                   1612:                        L_PUBKEY(_PATH_HOST_ECDSA_KEY_FILE, 4);
                   1613:                        L_PUBKEY(_PATH_HOST_ED25519_KEY_FILE, 5);
                   1614:                        L_PUBKEY(_PATH_HOST_RSA_KEY_FILE, 6);
                   1615:                        L_PUBKEY(_PATH_HOST_DSA_KEY_FILE, 7);
                   1616:                        L_CERT(_PATH_HOST_XMSS_KEY_FILE, 8);
                   1617:                        L_PUBKEY(_PATH_HOST_XMSS_KEY_FILE, 9);
1.581     dtucker  1618:                        if (loaded == 0)
                   1619:                                debug("HostbasedAuthentication enabled but no "
                   1620:                                   "local public host keys could be loaded.");
1.173     markus   1621:                }
1.367     djm      1622:        }
1.31      markus   1623:
1.104     markus   1624:        /* load options.identity_files */
1.544     djm      1625:        load_public_identity_files(cinfo);
1.439     markus   1626:
1.476     djm      1627:        /* optionally set the SSH_AUTHSOCKET_ENV_NAME variable */
1.440     markus   1628:        if (options.identity_agent &&
                   1629:            strcmp(options.identity_agent, SSH_AUTHSOCKET_ENV_NAME) != 0) {
1.439     markus   1630:                if (strcmp(options.identity_agent, "none") == 0) {
                   1631:                        unsetenv(SSH_AUTHSOCKET_ENV_NAME);
                   1632:                } else {
1.522     dtucker  1633:                        cp = options.identity_agent;
1.528     dtucker  1634:                        /* legacy (limited) format */
                   1635:                        if (cp[0] == '$' && cp[1] != '{') {
1.494     djm      1636:                                if (!valid_env_name(cp + 1)) {
                   1637:                                        fatal("Invalid IdentityAgent "
                   1638:                                            "environment variable name %s", cp);
                   1639:                                }
                   1640:                                if ((p = getenv(cp + 1)) == NULL)
                   1641:                                        unsetenv(SSH_AUTHSOCKET_ENV_NAME);
                   1642:                                else
                   1643:                                        setenv(SSH_AUTHSOCKET_ENV_NAME, p, 1);
                   1644:                        } else {
                   1645:                                /* identity_agent specifies a path directly */
                   1646:                                setenv(SSH_AUTHSOCKET_ENV_NAME, cp, 1);
                   1647:                        }
1.510     djm      1648:                }
                   1649:        }
                   1650:
1.522     dtucker  1651:        if (options.forward_agent && options.forward_agent_sock_path != NULL) {
1.526     djm      1652:                cp = options.forward_agent_sock_path;
1.510     djm      1653:                if (cp[0] == '$') {
                   1654:                        if (!valid_env_name(cp + 1)) {
                   1655:                                fatal("Invalid ForwardAgent environment variable name %s", cp);
                   1656:                        }
                   1657:                        if ((p = getenv(cp + 1)) != NULL)
1.566     dtucker  1658:                                forward_agent_sock_path = xstrdup(p);
1.510     djm      1659:                        else
                   1660:                                options.forward_agent = 0;
                   1661:                        free(cp);
                   1662:                } else {
                   1663:                        forward_agent_sock_path = cp;
1.439     markus   1664:                }
                   1665:        }
1.104     markus   1666:
                   1667:        /* Expand ~ in known host file names. */
1.361     djm      1668:        tilde_expand_paths(options.system_hostfiles,
                   1669:            options.num_system_hostfiles);
                   1670:        tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
1.149     markus   1671:
1.512     dtucker  1672:        ssh_signal(SIGCHLD, main_sigchld_handler);
1.31      markus   1673:
1.316     djm      1674:        /* Log into the remote system.  Never returns if the login fails. */
1.497     djm      1675:        ssh_login(ssh, &sensitive_data, host, (struct sockaddr *)&hostaddr,
1.546     djm      1676:            options.port, pw, timeout_ms, cinfo);
1.31      markus   1677:
1.112     markus   1678:        /* We no longer need the private host keys.  Clear them now. */
                   1679:        if (sensitive_data.nkeys != 0) {
                   1680:                for (i = 0; i < sensitive_data.nkeys; i++) {
                   1681:                        if (sensitive_data.keys[i] != NULL) {
                   1682:                                /* Destroys contents safely */
                   1683:                                debug3("clear hostkey %d", i);
1.483     markus   1684:                                sshkey_free(sensitive_data.keys[i]);
1.112     markus   1685:                                sensitive_data.keys[i] = NULL;
                   1686:                        }
                   1687:                }
1.378     djm      1688:                free(sensitive_data.keys);
1.134     markus   1689:        }
                   1690:        for (i = 0; i < options.num_identity_files; i++) {
1.378     djm      1691:                free(options.identity_files[i]);
                   1692:                options.identity_files[i] = NULL;
1.134     markus   1693:                if (options.identity_keys[i]) {
1.483     markus   1694:                        sshkey_free(options.identity_keys[i]);
1.134     markus   1695:                        options.identity_keys[i] = NULL;
                   1696:                }
1.112     markus   1697:        }
1.426     djm      1698:        for (i = 0; i < options.num_certificate_files; i++) {
                   1699:                free(options.certificate_files[i]);
                   1700:                options.certificate_files[i] = NULL;
                   1701:        }
1.553     djm      1702:
                   1703: #ifdef ENABLE_PKCS11
                   1704:        (void)pkcs11_del_provider(options.pkcs11_provider);
                   1705: #endif
1.31      markus   1706:
1.447     markus   1707:  skip_connect:
1.544     djm      1708:        exit_status = ssh_session2(ssh, cinfo);
                   1709:        ssh_conn_info_free(cinfo);
1.499     djm      1710:        ssh_packet_close(ssh);
1.186     djm      1711:
1.312     djm      1712:        if (options.control_path != NULL && muxserver_sock != -1)
1.214     djm      1713:                unlink(options.control_path);
                   1714:
1.353     djm      1715:        /* Kill ProxyCommand if it is running. */
                   1716:        ssh_kill_proxy_command();
1.186     djm      1717:
1.45      markus   1718:        return exit_status;
                   1719: }
                   1720:
1.344     djm      1721: static void
                   1722: control_persist_detach(void)
                   1723: {
                   1724:        pid_t pid;
                   1725:
1.540     djm      1726:        debug_f("backgrounding master process");
1.344     djm      1727:
1.473     djm      1728:        /*
                   1729:         * master (current process) into the background, and make the
                   1730:         * foreground process a client of the backgrounded master.
                   1731:         */
1.344     djm      1732:        switch ((pid = fork())) {
                   1733:        case -1:
1.540     djm      1734:                fatal_f("fork: %s", strerror(errno));
1.344     djm      1735:        case 0:
                   1736:                /* Child: master process continues mainloop */
1.473     djm      1737:                break;
                   1738:        default:
1.531     djm      1739:                /* Parent: set up mux client to connect to backgrounded master */
1.540     djm      1740:                debug2_f("background process is %ld", (long)pid);
1.563     djm      1741:                options.stdin_null = ostdin_null_flag;
1.359     djm      1742:                options.request_tty = orequest_tty;
1.344     djm      1743:                tty_flag = otty_flag;
1.561     djm      1744:                options.session_type = osession_type;
1.473     djm      1745:                close(muxserver_sock);
                   1746:                muxserver_sock = -1;
1.351     markus   1747:                options.control_master = SSHCTL_MASTER_NO;
1.587     dtucker  1748:                (void)muxclient(options.control_path);
1.344     djm      1749:                /* muxclient() doesn't return on success. */
1.473     djm      1750:                fatal("Failed to connect to new control master");
                   1751:        }
1.537     djm      1752:        if (stdfd_devnull(1, 1, !(log_is_on_stderr() && debug_flag)) == -1)
1.540     djm      1753:                error_f("stdfd_devnull failed");
1.381     djm      1754:        daemon(1, 1);
1.362     djm      1755:        setproctitle("%s [mux]", options.control_path);
1.344     djm      1756: }
                   1757:
                   1758: /* Do fork() after authentication. Used by "ssh -f" */
                   1759: static void
                   1760: fork_postauth(void)
                   1761: {
                   1762:        if (need_controlpersist_detach)
                   1763:                control_persist_detach();
                   1764:        debug("forking to background");
1.564     djm      1765:        options.fork_after_authentication = 0;
1.505     deraadt  1766:        if (daemon(1, 1) == -1)
1.344     djm      1767:                fatal("daemon() failed: %.200s", strerror(errno));
1.537     djm      1768:        if (stdfd_devnull(1, 1, !(log_is_on_stderr() && debug_flag)) == -1)
1.540     djm      1769:                error_f("stdfd_devnull failed");
1.344     djm      1770: }
                   1771:
1.523     djm      1772: static void
                   1773: forwarding_success(void)
                   1774: {
1.524     djm      1775:        if (forward_confirms_pending == -1)
                   1776:                return;
                   1777:        if (--forward_confirms_pending == 0) {
1.540     djm      1778:                debug_f("all expected forwarding replies received");
1.564     djm      1779:                if (options.fork_after_authentication)
1.523     djm      1780:                        fork_postauth();
1.524     djm      1781:        } else {
1.540     djm      1782:                debug2_f("%d expected forwarding replies remaining",
                   1783:                    forward_confirms_pending);
1.523     djm      1784:        }
                   1785: }
                   1786:
1.315     djm      1787: /* Callback for remote forward global requests */
                   1788: static void
1.463     djm      1789: ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
1.315     djm      1790: {
1.406     millert  1791:        struct Forward *rfwd = (struct Forward *)ctxt;
1.499     djm      1792:        u_int port;
                   1793:        int r;
1.315     djm      1794:
1.324     djm      1795:        /* XXX verbose() on failure? */
1.404     markus   1796:        debug("remote forward %s for: listen %s%s%d, connect %s:%d",
1.315     djm      1797:            type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.406     millert  1798:            rfwd->listen_path ? rfwd->listen_path :
                   1799:            rfwd->listen_host ? rfwd->listen_host : "",
                   1800:            (rfwd->listen_path || rfwd->listen_host) ? ":" : "",
                   1801:            rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
                   1802:            rfwd->connect_host, rfwd->connect_port);
                   1803:        if (rfwd->listen_path == NULL && rfwd->listen_port == 0) {
1.366     markus   1804:                if (type == SSH2_MSG_REQUEST_SUCCESS) {
1.499     djm      1805:                        if ((r = sshpkt_get_u32(ssh, &port)) != 0)
1.540     djm      1806:                                fatal_fr(r, "parse packet");
1.499     djm      1807:                        if (port > 65535) {
                   1808:                                error("Invalid allocated port %u for remote "
                   1809:                                    "forward to %s:%d", port,
                   1810:                                    rfwd->connect_host, rfwd->connect_port);
                   1811:                                /* Ensure failure processing runs below */
                   1812:                                type = SSH2_MSG_REQUEST_FAILURE;
                   1813:                                channel_update_permission(ssh,
                   1814:                                    rfwd->handle, -1);
                   1815:                        } else {
                   1816:                                rfwd->allocated_port = (int)port;
                   1817:                                logit("Allocated port %u for remote "
                   1818:                                    "forward to %s:%d",
1.569     dtucker  1819:                                    rfwd->allocated_port, rfwd->connect_path ?
                   1820:                                    rfwd->connect_path : rfwd->connect_host,
1.499     djm      1821:                                    rfwd->connect_port);
                   1822:                                channel_update_permission(ssh,
                   1823:                                    rfwd->handle, rfwd->allocated_port);
                   1824:                        }
1.366     markus   1825:                } else {
1.480     djm      1826:                        channel_update_permission(ssh, rfwd->handle, -1);
1.366     markus   1827:                }
1.324     djm      1828:        }
1.468     djm      1829:
1.315     djm      1830:        if (type == SSH2_MSG_REQUEST_FAILURE) {
1.406     millert  1831:                if (options.exit_on_forward_failure) {
                   1832:                        if (rfwd->listen_path != NULL)
                   1833:                                fatal("Error: remote port forwarding failed "
                   1834:                                    "for listen path %s", rfwd->listen_path);
                   1835:                        else
                   1836:                                fatal("Error: remote port forwarding failed "
                   1837:                                    "for listen port %d", rfwd->listen_port);
                   1838:                } else {
                   1839:                        if (rfwd->listen_path != NULL)
                   1840:                                logit("Warning: remote port forwarding failed "
                   1841:                                    "for listen path %s", rfwd->listen_path);
                   1842:                        else
                   1843:                                logit("Warning: remote port forwarding failed "
                   1844:                                    "for listen port %d", rfwd->listen_port);
                   1845:                }
1.315     djm      1846:        }
1.523     djm      1847:        forwarding_success();
1.315     djm      1848: }
                   1849:
1.126     itojun   1850: static void
1.582     djm      1851: client_cleanup_stdio_fwd(struct ssh *ssh, int id, int force, void *arg)
1.331     dtucker  1852: {
                   1853:        debug("stdio forwarding: done");
                   1854:        cleanup_exit(0);
                   1855: }
                   1856:
1.368     djm      1857: static void
1.463     djm      1858: ssh_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
1.407     djm      1859: {
                   1860:        if (!success)
                   1861:                fatal("stdio forwarding failed");
                   1862: }
                   1863:
                   1864: static void
1.523     djm      1865: ssh_tun_confirm(struct ssh *ssh, int id, int success, void *arg)
                   1866: {
                   1867:        if (!success) {
                   1868:                error("Tunnel forwarding failed");
                   1869:                if (options.exit_on_forward_failure)
                   1870:                        cleanup_exit(255);
                   1871:        }
                   1872:
1.540     djm      1873:        debug_f("tunnel forward established, id=%d", id);
1.523     djm      1874:        forwarding_success();
                   1875: }
                   1876:
                   1877: static void
1.463     djm      1878: ssh_init_stdio_forwarding(struct ssh *ssh)
1.331     dtucker  1879: {
                   1880:        Channel *c;
1.332     djm      1881:        int in, out;
1.331     dtucker  1882:
1.441     dtucker  1883:        if (options.stdio_forward_host == NULL)
1.368     djm      1884:                return;
                   1885:
1.540     djm      1886:        debug3_f("%s:%d", options.stdio_forward_host,
1.441     dtucker  1887:            options.stdio_forward_port);
1.332     djm      1888:
1.505     deraadt  1889:        if ((in = dup(STDIN_FILENO)) == -1 ||
                   1890:            (out = dup(STDOUT_FILENO)) == -1)
1.557     djm      1891:                fatal_f("dup() in/out failed");
1.463     djm      1892:        if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host,
1.557     djm      1893:            options.stdio_forward_port, in, out,
                   1894:            CHANNEL_NONBLOCK_STDIO)) == NULL)
1.540     djm      1895:                fatal_f("channel_connect_stdio_fwd failed");
1.463     djm      1896:        channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0);
                   1897:        channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL);
1.331     dtucker  1898: }
                   1899:
                   1900: static void
1.551     markus   1901: ssh_init_forward_permissions(struct ssh *ssh, const char *what, char **opens,
                   1902:     u_int num_opens)
                   1903: {
                   1904:        u_int i;
                   1905:        int port;
1.573     dtucker  1906:        char *addr, *arg, *oarg;
1.551     markus   1907:        int where = FORWARD_LOCAL;
                   1908:
                   1909:        channel_clear_permission(ssh, FORWARD_ADM, where);
                   1910:        if (num_opens == 0)
                   1911:                return; /* permit any */
                   1912:
                   1913:        /* handle keywords: "any" / "none" */
                   1914:        if (num_opens == 1 && strcmp(opens[0], "any") == 0)
                   1915:                return;
                   1916:        if (num_opens == 1 && strcmp(opens[0], "none") == 0) {
                   1917:                channel_disable_admin(ssh, where);
                   1918:                return;
                   1919:        }
                   1920:        /* Otherwise treat it as a list of permitted host:port */
                   1921:        for (i = 0; i < num_opens; i++) {
                   1922:                oarg = arg = xstrdup(opens[i]);
1.573     dtucker  1923:                addr = hpdelim(&arg);
                   1924:                if (addr == NULL)
1.551     markus   1925:                        fatal_f("missing host in %s", what);
                   1926:                addr = cleanhostname(addr);
                   1927:                if (arg == NULL || ((port = permitopen_port(arg)) < 0))
                   1928:                        fatal_f("bad port number in %s", what);
                   1929:                /* Send it to channels layer */
                   1930:                channel_add_permission(ssh, FORWARD_ADM,
                   1931:                    where, addr, port);
                   1932:                free(oarg);
                   1933:        }
                   1934: }
                   1935:
                   1936: static void
1.466     djm      1937: ssh_init_forwarding(struct ssh *ssh, char **ifname)
1.70      markus   1938: {
1.86      markus   1939:        int success = 0;
1.70      markus   1940:        int i;
1.551     markus   1941:
                   1942:        ssh_init_forward_permissions(ssh, "permitremoteopen",
                   1943:            options.permitted_remote_opens,
                   1944:            options.num_permitted_remote_opens);
1.331     dtucker  1945:
1.524     djm      1946:        if (options.exit_on_forward_failure)
                   1947:                forward_confirms_pending = 0; /* track pending requests */
1.70      markus   1948:        /* Initiate local TCP/IP port forwardings. */
                   1949:        for (i = 0; i < options.num_local_forwards; i++) {
1.232     djm      1950:                debug("Local connections to %.200s:%d forwarded to remote "
                   1951:                    "address %.200s:%d",
1.406     millert  1952:                    (options.local_forwards[i].listen_path != NULL) ?
                   1953:                    options.local_forwards[i].listen_path :
1.234     deraadt  1954:                    (options.local_forwards[i].listen_host == NULL) ?
1.406     millert  1955:                    (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
1.232     djm      1956:                    options.local_forwards[i].listen_host,
                   1957:                    options.local_forwards[i].listen_port,
1.406     millert  1958:                    (options.local_forwards[i].connect_path != NULL) ?
                   1959:                    options.local_forwards[i].connect_path :
1.232     djm      1960:                    options.local_forwards[i].connect_host,
                   1961:                    options.local_forwards[i].connect_port);
1.463     djm      1962:                success += channel_setup_local_fwd_listener(ssh,
1.406     millert  1963:                    &options.local_forwards[i], &options.fwd_opts);
1.70      markus   1964:        }
1.283     markus   1965:        if (i > 0 && success != i && options.exit_on_forward_failure)
                   1966:                fatal("Could not request local forwarding.");
1.86      markus   1967:        if (i > 0 && success == 0)
                   1968:                error("Could not request local forwarding.");
1.70      markus   1969:
                   1970:        /* Initiate remote TCP/IP port forwardings. */
                   1971:        for (i = 0; i < options.num_remote_forwards; i++) {
1.232     djm      1972:                debug("Remote connections from %.200s:%d forwarded to "
                   1973:                    "local address %.200s:%d",
1.406     millert  1974:                    (options.remote_forwards[i].listen_path != NULL) ?
                   1975:                    options.remote_forwards[i].listen_path :
1.248     djm      1976:                    (options.remote_forwards[i].listen_host == NULL) ?
1.253     djm      1977:                    "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232     djm      1978:                    options.remote_forwards[i].listen_port,
1.406     millert  1979:                    (options.remote_forwards[i].connect_path != NULL) ?
                   1980:                    options.remote_forwards[i].connect_path :
1.232     djm      1981:                    options.remote_forwards[i].connect_host,
                   1982:                    options.remote_forwards[i].connect_port);
1.523     djm      1983:                if ((options.remote_forwards[i].handle =
1.463     djm      1984:                    channel_request_remote_forwarding(ssh,
1.523     djm      1985:                    &options.remote_forwards[i])) >= 0) {
1.463     djm      1986:                        client_register_global_confirm(
                   1987:                            ssh_confirm_remote_forward,
1.366     markus   1988:                            &options.remote_forwards[i]);
1.523     djm      1989:                        forward_confirms_pending++;
                   1990:                } else if (options.exit_on_forward_failure)
                   1991:                        fatal("Could not request remote forwarding.");
                   1992:                else
                   1993:                        logit("Warning: Could not request remote forwarding.");
1.70      markus   1994:        }
1.301     djm      1995:
                   1996:        /* Initiate tunnel forwarding. */
                   1997:        if (options.tun_open != SSH_TUNMODE_NO) {
1.466     djm      1998:                if ((*ifname = client_request_tun_fwd(ssh,
                   1999:                    options.tun_open, options.tun_local,
1.523     djm      2000:                    options.tun_remote, ssh_tun_confirm, NULL)) != NULL)
                   2001:                        forward_confirms_pending++;
                   2002:                else if (options.exit_on_forward_failure)
                   2003:                        fatal("Could not request tunnel forwarding.");
                   2004:                else
                   2005:                        error("Could not request tunnel forwarding.");
1.524     djm      2006:        }
                   2007:        if (forward_confirms_pending > 0) {
1.540     djm      2008:                debug_f("expecting replies for %d forwards",
1.524     djm      2009:                    forward_confirms_pending);
1.468     djm      2010:        }
1.70      markus   2011: }
                   2012:
1.126     itojun   2013: static void
1.70      markus   2014: check_agent_present(void)
                   2015: {
1.412     djm      2016:        int r;
                   2017:
1.70      markus   2018:        if (options.forward_agent) {
1.254     djm      2019:                /* Clear agent forwarding if we don't have an agent. */
1.412     djm      2020:                if ((r = ssh_get_authentication_socket(NULL)) != 0) {
1.70      markus   2021:                        options.forward_agent = 0;
1.412     djm      2022:                        if (r != SSH_ERR_AGENT_NOT_PRESENT)
1.540     djm      2023:                                debug_r(r, "ssh_get_authentication_socket");
1.412     djm      2024:                }
1.70      markus   2025:        }
                   2026: }
                   2027:
1.214     djm      2028: static void
1.463     djm      2029: ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg)
1.214     djm      2030: {
1.215     djm      2031:        extern char **environ;
1.558     djm      2032:        const char *display, *term;
1.499     djm      2033:        int r, interactive = tty_flag;
1.433     djm      2034:        char *proto = NULL, *data = NULL;
1.337     djm      2035:
                   2036:        if (!success)
1.580     djm      2037:                return; /* No need for error message, channels code sends one */
1.215     djm      2038:
1.248     djm      2039:        display = getenv("DISPLAY");
1.417     djm      2040:        if (display == NULL && options.forward_x11)
                   2041:                debug("X11 forwarding requested but DISPLAY not set");
1.463     djm      2042:        if (options.forward_x11 && client_x11_get_proto(ssh, display,
1.433     djm      2043:            options.xauth_location, options.forward_x11_trusted,
                   2044:            options.forward_x11_timeout, &proto, &data) == 0) {
1.50      markus   2045:                /* Request forwarding with authentication spoofing. */
1.316     djm      2046:                debug("Requesting X11 forwarding with authentication "
                   2047:                    "spoofing.");
1.463     djm      2048:                x11_request_forwarding_with_spoofing(ssh, id, display, proto,
1.363     djm      2049:                    data, 1);
1.463     djm      2050:                client_expect_confirm(ssh, id, "X11 forwarding", CONFIRM_WARN);
1.363     djm      2051:                /* XXX exit_on_forward_failure */
1.80      markus   2052:                interactive = 1;
1.50      markus   2053:        }
                   2054:
1.70      markus   2055:        check_agent_present();
                   2056:        if (options.forward_agent) {
                   2057:                debug("Requesting authentication agent forwarding.");
1.463     djm      2058:                channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
1.499     djm      2059:                if ((r = sshpkt_send(ssh)) != 0)
1.540     djm      2060:                        fatal_fr(r, "send packet");
1.212     djm      2061:        }
1.369     dtucker  2062:
                   2063:        /* Tell the packet module whether this is an interactive session. */
1.499     djm      2064:        ssh_packet_set_interactive(ssh, interactive,
1.369     dtucker  2065:            options.ip_qos_interactive, options.ip_qos_bulk);
1.212     djm      2066:
1.558     djm      2067:        if ((term = lookup_env_in_list("TERM", options.setenv,
                   2068:            options.num_setenv)) == NULL || *term == '\0')
                   2069:                term = getenv("TERM");
1.562     djm      2070:        client_session2_setup(ssh, id, tty_flag,
                   2071:            options.session_type == SESSION_TYPE_SUBSYSTEM, term,
1.482     markus   2072:            NULL, fileno(stdin), command, environ);
1.45      markus   2073: }
                   2074:
1.143     markus   2075: /* open new channel for a session */
1.126     itojun   2076: static int
1.463     djm      2077: ssh_session2_open(struct ssh *ssh)
1.45      markus   2078: {
1.118     markus   2079:        Channel *c;
                   2080:        int window, packetmax, in, out, err;
1.60      markus   2081:
1.563     djm      2082:        if (options.stdin_null) {
1.93      itojun   2083:                in = open(_PATH_DEVNULL, O_RDONLY);
1.62      markus   2084:        } else {
                   2085:                in = dup(STDIN_FILENO);
                   2086:        }
1.60      markus   2087:        out = dup(STDOUT_FILENO);
                   2088:        err = dup(STDERR_FILENO);
1.45      markus   2089:
1.505     deraadt  2090:        if (in == -1 || out == -1 || err == -1)
1.62      markus   2091:                fatal("dup() in/out/err failed");
1.45      markus   2092:
1.65      markus   2093:        window = CHAN_SES_WINDOW_DEFAULT;
                   2094:        packetmax = CHAN_SES_PACKET_DEFAULT;
1.164     markus   2095:        if (tty_flag) {
                   2096:                window >>= 1;
                   2097:                packetmax >>= 1;
1.45      markus   2098:        }
1.463     djm      2099:        c = channel_new(ssh,
1.45      markus   2100:            "session", SSH_CHANNEL_OPENING, in, out, err,
1.65      markus   2101:            window, packetmax, CHAN_EXTENDED_WRITE,
1.557     djm      2102:            "client-session", CHANNEL_NONBLOCK_STDIO);
1.45      markus   2103:
1.540     djm      2104:        debug3_f("channel_new: %d", c->self);
1.106     markus   2105:
1.463     djm      2106:        channel_send_open(ssh, c->self);
1.560     djm      2107:        if (options.session_type != SESSION_TYPE_NONE)
1.463     djm      2108:                channel_register_open_confirm(ssh, c->self,
1.310     djm      2109:                    ssh_session2_setup, NULL);
1.106     markus   2110:
1.118     markus   2111:        return c->self;
1.106     markus   2112: }
                   2113:
1.126     itojun   2114: static int
1.544     djm      2115: ssh_session2(struct ssh *ssh, const struct ssh_conn_info *cinfo)
1.106     markus   2116: {
1.537     djm      2117:        int r, id = -1;
1.466     djm      2118:        char *cp, *tun_fwd_ifname = NULL;
1.106     markus   2119:
                   2120:        /* XXX should be pre-session */
1.368     djm      2121:        if (!options.control_persist)
1.463     djm      2122:                ssh_init_stdio_forwarding(ssh);
1.466     djm      2123:
                   2124:        ssh_init_forwarding(ssh, &tun_fwd_ifname);
                   2125:
                   2126:        if (options.local_command != NULL) {
                   2127:                debug3("expanding LocalCommand: %s", options.local_command);
                   2128:                cp = options.local_command;
                   2129:                options.local_command = percent_expand(cp,
1.546     djm      2130:                    DEFAULT_CLIENT_PERCENT_EXPAND_ARGS(cinfo),
1.466     djm      2131:                    "T", tun_fwd_ifname == NULL ? "NONE" : tun_fwd_ifname,
                   2132:                    (char *)NULL);
                   2133:                debug3("expanded LocalCommand: %s", options.local_command);
                   2134:                free(cp);
                   2135:        }
1.106     markus   2136:
1.344     djm      2137:        /* Start listening for multiplex clients */
1.499     djm      2138:        if (!ssh_packet_get_mux(ssh))
1.463     djm      2139:                muxserver_listen(ssh);
1.344     djm      2140:
1.473     djm      2141:        /*
1.368     djm      2142:         * If we are in control persist mode and have a working mux listen
                   2143:         * socket, then prepare to background ourselves and have a foreground
1.531     djm      2144:         * client attach as a control client.
1.368     djm      2145:         * NB. we must save copies of the flags that we override for
1.531     djm      2146:         * the backgrounding, since we defer attachment of the client until
1.344     djm      2147:         * after the connection is fully established (in particular,
                   2148:         * async rfwd replies have been received for ExitOnForwardFailure).
                   2149:         */
1.473     djm      2150:        if (options.control_persist && muxserver_sock != -1) {
1.563     djm      2151:                ostdin_null_flag = options.stdin_null;
1.560     djm      2152:                osession_type = options.session_type;
1.359     djm      2153:                orequest_tty = options.request_tty;
1.344     djm      2154:                otty_flag = tty_flag;
1.563     djm      2155:                options.stdin_null = 1;
1.560     djm      2156:                options.session_type = SESSION_TYPE_NONE;
1.473     djm      2157:                tty_flag = 0;
1.564     djm      2158:                if (!options.fork_after_authentication &&
1.562     djm      2159:                    (osession_type != SESSION_TYPE_NONE ||
                   2160:                    options.stdio_forward_host != NULL))
1.344     djm      2161:                        need_controlpersist_detach = 1;
1.564     djm      2162:                options.fork_after_authentication = 1;
1.473     djm      2163:        }
1.368     djm      2164:        /*
                   2165:         * ControlPersist mux listen socket setup failed, attempt the
                   2166:         * stdio forward setup that we skipped earlier.
                   2167:         */
                   2168:        if (options.control_persist && muxserver_sock == -1)
1.463     djm      2169:                ssh_init_stdio_forwarding(ssh);
1.344     djm      2170:
1.560     djm      2171:        if (options.session_type != SESSION_TYPE_NONE)
1.463     djm      2172:                id = ssh_session2_open(ssh);
1.379     djm      2173:        else {
1.499     djm      2174:                ssh_packet_set_interactive(ssh,
1.379     djm      2175:                    options.control_master == SSHCTL_MASTER_NO,
                   2176:                    options.ip_qos_interactive, options.ip_qos_bulk);
                   2177:        }
1.314     djm      2178:
                   2179:        /* If we don't expect to open a new session, then disallow it */
1.319     markus   2180:        if (options.control_master == SSHCTL_MASTER_NO &&
1.549     djm      2181:            (ssh->compat & SSH_NEW_OPENSSH)) {
1.314     djm      2182:                debug("Requesting no-more-sessions@openssh.com");
1.499     djm      2183:                if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
                   2184:                    (r = sshpkt_put_cstring(ssh,
                   2185:                    "no-more-sessions@openssh.com")) != 0 ||
                   2186:                    (r = sshpkt_put_u8(ssh, 0)) != 0 ||
                   2187:                    (r = sshpkt_send(ssh)) != 0)
1.540     djm      2188:                        fatal_fr(r, "send packet");
1.314     djm      2189:        }
1.255     reyk     2190:
                   2191:        /* Execute a local command */
                   2192:        if (options.local_command != NULL &&
                   2193:            options.permit_local_command)
                   2194:                ssh_local_cmd(options.local_command);
1.467     djm      2195:
                   2196:        /*
                   2197:         * stdout is now owned by the session channel; clobber it here
                   2198:         * so future channel closes are propagated to the local fd.
                   2199:         * NB. this can only happen after LocalCommand has completed,
                   2200:         * as it may want to write to stdout.
                   2201:         */
1.537     djm      2202:        if (!need_controlpersist_detach && stdfd_devnull(0, 1, 0) == -1)
1.540     djm      2203:                error_f("stdfd_devnull failed");
1.301     djm      2204:
1.342     djm      2205:        /*
                   2206:         * If requested and we are not interested in replies to remote
                   2207:         * forwarding requests, then let ssh continue in the background.
                   2208:         */
1.564     djm      2209:        if (options.fork_after_authentication) {
1.344     djm      2210:                if (options.exit_on_forward_failure &&
                   2211:                    options.num_remote_forwards > 0) {
                   2212:                        debug("deferring postauth fork until remote forward "
                   2213:                            "confirmation received");
                   2214:                } else
                   2215:                        fork_postauth();
1.318     djm      2216:        }
1.31      markus   2217:
1.463     djm      2218:        return client_loop(ssh, tty_flag, tty_flag ?
1.119     stevesk  2219:            options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72      markus   2220: }
                   2221:
1.426     djm      2222: /* Loads all IdentityFile and CertificateFile keys */
1.126     itojun   2223: static void
1.544     djm      2224: load_public_identity_files(const struct ssh_conn_info *cinfo)
1.104     markus   2225: {
1.466     djm      2226:        char *filename, *cp;
1.460     markus   2227:        struct sshkey *public;
1.426     djm      2228:        int i;
                   2229:        u_int n_ids, n_certs;
1.335     djm      2230:        char *identity_files[SSH_MAX_IDENTITY_FILES];
1.460     markus   2231:        struct sshkey *identity_keys[SSH_MAX_IDENTITY_FILES];
1.484     djm      2232:        int identity_file_userprovided[SSH_MAX_IDENTITY_FILES];
1.426     djm      2233:        char *certificate_files[SSH_MAX_CERTIFICATE_FILES];
                   2234:        struct sshkey *certificates[SSH_MAX_CERTIFICATE_FILES];
1.484     djm      2235:        int certificate_file_userprovided[SSH_MAX_CERTIFICATE_FILES];
1.333     markus   2236: #ifdef ENABLE_PKCS11
1.514     djm      2237:        struct sshkey **keys = NULL;
                   2238:        char **comments = NULL;
1.333     markus   2239:        int nkeys;
1.335     djm      2240: #endif /* PKCS11 */
1.104     markus   2241:
1.426     djm      2242:        n_ids = n_certs = 0;
1.398     tedu     2243:        memset(identity_files, 0, sizeof(identity_files));
                   2244:        memset(identity_keys, 0, sizeof(identity_keys));
1.484     djm      2245:        memset(identity_file_userprovided, 0,
                   2246:            sizeof(identity_file_userprovided));
1.426     djm      2247:        memset(certificate_files, 0, sizeof(certificate_files));
                   2248:        memset(certificates, 0, sizeof(certificates));
1.484     djm      2249:        memset(certificate_file_userprovided, 0,
                   2250:            sizeof(certificate_file_userprovided));
1.335     djm      2251:
                   2252: #ifdef ENABLE_PKCS11
1.333     markus   2253:        if (options.pkcs11_provider != NULL &&
1.167     markus   2254:            options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.333     markus   2255:            (pkcs11_init(!options.batch_mode) == 0) &&
                   2256:            (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1.514     djm      2257:            &keys, &comments)) > 0) {
1.333     markus   2258:                for (i = 0; i < nkeys; i++) {
1.335     djm      2259:                        if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1.483     markus   2260:                                sshkey_free(keys[i]);
1.514     djm      2261:                                free(comments[i]);
1.335     djm      2262:                                continue;
                   2263:                        }
                   2264:                        identity_keys[n_ids] = keys[i];
1.514     djm      2265:                        identity_files[n_ids] = comments[i]; /* transferred */
1.335     djm      2266:                        n_ids++;
1.167     markus   2267:                }
1.378     djm      2268:                free(keys);
1.514     djm      2269:                free(comments);
1.127     markus   2270:        }
1.333     markus   2271: #endif /* ENABLE_PKCS11 */
1.335     djm      2272:        for (i = 0; i < options.num_identity_files; i++) {
1.373     djm      2273:                if (n_ids >= SSH_MAX_IDENTITY_FILES ||
                   2274:                    strcasecmp(options.identity_files[i], "none") == 0) {
1.378     djm      2275:                        free(options.identity_files[i]);
1.426     djm      2276:                        options.identity_files[i] = NULL;
1.335     djm      2277:                        continue;
                   2278:                }
1.490     dtucker  2279:                cp = tilde_expand_filename(options.identity_files[i], getuid());
1.544     djm      2280:                filename = default_client_percent_dollar_expand(cp, cinfo);
1.378     djm      2281:                free(cp);
1.483     markus   2282:                check_load(sshkey_load_public(filename, &public, NULL),
1.576     djm      2283:                    &public, filename, "pubkey");
1.131     millert  2284:                debug("identity file %s type %d", filename,
                   2285:                    public ? public->type : -1);
1.378     djm      2286:                free(options.identity_files[i]);
1.335     djm      2287:                identity_files[n_ids] = filename;
                   2288:                identity_keys[n_ids] = public;
1.484     djm      2289:                identity_file_userprovided[n_ids] =
                   2290:                    options.identity_file_userprovided[i];
1.335     djm      2291:                if (++n_ids >= SSH_MAX_IDENTITY_FILES)
                   2292:                        continue;
                   2293:
1.426     djm      2294:                /*
                   2295:                 * If no certificates have been explicitly listed then try
                   2296:                 * to add the default certificate variant too.
                   2297:                 */
                   2298:                if (options.num_certificate_files != 0)
                   2299:                        continue;
1.335     djm      2300:                xasprintf(&cp, "%s-cert", filename);
1.483     markus   2301:                check_load(sshkey_load_public(cp, &public, NULL),
1.576     djm      2302:                    &public, filename, "pubkey");
1.335     djm      2303:                debug("identity file %s type %d", cp,
                   2304:                    public ? public->type : -1);
                   2305:                if (public == NULL) {
1.378     djm      2306:                        free(cp);
1.335     djm      2307:                        continue;
                   2308:                }
1.483     markus   2309:                if (!sshkey_is_cert(public)) {
1.540     djm      2310:                        debug_f("key %s type %s is not a certificate",
                   2311:                            cp, sshkey_type(public));
1.483     markus   2312:                        sshkey_free(public);
1.378     djm      2313:                        free(cp);
1.335     djm      2314:                        continue;
                   2315:                }
1.448     djm      2316:                /* NB. leave filename pointing to private key */
                   2317:                identity_files[n_ids] = xstrdup(filename);
1.335     djm      2318:                identity_keys[n_ids] = public;
1.484     djm      2319:                identity_file_userprovided[n_ids] =
                   2320:                    options.identity_file_userprovided[i];
1.335     djm      2321:                n_ids++;
                   2322:        }
1.426     djm      2323:
                   2324:        if (options.num_certificate_files > SSH_MAX_CERTIFICATE_FILES)
1.540     djm      2325:                fatal_f("too many certificates");
1.426     djm      2326:        for (i = 0; i < options.num_certificate_files; i++) {
                   2327:                cp = tilde_expand_filename(options.certificate_files[i],
1.490     dtucker  2328:                    getuid());
1.544     djm      2329:                filename = default_client_percent_dollar_expand(cp, cinfo);
1.426     djm      2330:                free(cp);
                   2331:
1.483     markus   2332:                check_load(sshkey_load_public(filename, &public, NULL),
1.576     djm      2333:                    &public, filename, "certificate");
1.426     djm      2334:                debug("certificate file %s type %d", filename,
                   2335:                    public ? public->type : -1);
                   2336:                free(options.certificate_files[i]);
                   2337:                options.certificate_files[i] = NULL;
                   2338:                if (public == NULL) {
                   2339:                        free(filename);
                   2340:                        continue;
                   2341:                }
1.483     markus   2342:                if (!sshkey_is_cert(public)) {
1.540     djm      2343:                        debug_f("key %s type %s is not a certificate",
                   2344:                            filename, sshkey_type(public));
1.483     markus   2345:                        sshkey_free(public);
1.426     djm      2346:                        free(filename);
                   2347:                        continue;
                   2348:                }
                   2349:                certificate_files[n_certs] = filename;
                   2350:                certificates[n_certs] = public;
1.484     djm      2351:                certificate_file_userprovided[n_certs] =
                   2352:                    options.certificate_file_userprovided[i];
1.426     djm      2353:                ++n_certs;
                   2354:        }
                   2355:
1.335     djm      2356:        options.num_identity_files = n_ids;
                   2357:        memcpy(options.identity_files, identity_files, sizeof(identity_files));
                   2358:        memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1.484     djm      2359:        memcpy(options.identity_file_userprovided,
                   2360:            identity_file_userprovided, sizeof(identity_file_userprovided));
1.426     djm      2361:
                   2362:        options.num_certificate_files = n_certs;
                   2363:        memcpy(options.certificate_files,
                   2364:            certificate_files, sizeof(certificate_files));
                   2365:        memcpy(options.certificates, certificates, sizeof(certificates));
1.484     djm      2366:        memcpy(options.certificate_file_userprovided,
                   2367:            certificate_file_userprovided,
                   2368:            sizeof(certificate_file_userprovided));
1.214     djm      2369: }
1.352     djm      2370:
                   2371: static void
                   2372: main_sigchld_handler(int sig)
                   2373: {
                   2374:        int save_errno = errno;
                   2375:        pid_t pid;
                   2376:        int status;
                   2377:
                   2378:        while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1.505     deraadt  2379:            (pid == -1 && errno == EINTR))
1.352     djm      2380:                ;
                   2381:        errno = save_errno;
                   2382: }