[BACK]Return to session.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/session.c, Revision 1.295

1.295   ! djm         1: /* $OpenBSD: session.c,v 1.294 2018/03/03 03:15:51 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
1.37      deraadt     5:  *
                      6:  * As far as I am concerned, the code I have written for this software
                      7:  * can be used freely for any purpose.  Any derived versions of this
                      8:  * software must be clearly marked as such, and if the derived work is
                      9:  * incompatible with the protocol description in the RFC file, it must be
                     10:  * called by a name other than "ssh" or "Secure Shell".
                     11:  *
1.2       markus     12:  * SSH2 support by Markus Friedl.
1.95      markus     13:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.37      deraadt    14:  *
                     15:  * Redistribution and use in source and binary forms, with or without
                     16:  * modification, are permitted provided that the following conditions
                     17:  * are met:
                     18:  * 1. Redistributions of source code must retain the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer.
                     20:  * 2. Redistributions in binary form must reproduce the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer in the
                     22:  *    documentation and/or other materials provided with the distribution.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     25:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     26:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     27:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     28:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     29:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     30:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     31:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     32:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     33:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.2       markus     34:  */
1.1       markus     35:
1.193     stevesk    36: #include <sys/types.h>
                     37: #include <sys/wait.h>
1.194     stevesk    38: #include <sys/un.h>
1.196     stevesk    39: #include <sys/stat.h>
1.207     stevesk    40: #include <sys/socket.h>
1.236     djm        41: #include <sys/queue.h>
1.192     stevesk    42:
1.282     djm        43: #include <ctype.h>
1.209     stevesk    44: #include <errno.h>
1.253     djm        45: #include <fcntl.h>
1.204     stevesk    46: #include <grp.h>
1.223     djm        47: #include <login_cap.h>
1.272     djm        48: #include <netdb.h>
1.192     stevesk    49: #include <paths.h>
1.206     stevesk    50: #include <pwd.h>
1.195     stevesk    51: #include <signal.h>
1.215     stevesk    52: #include <stdio.h>
1.214     stevesk    53: #include <stdlib.h>
1.212     stevesk    54: #include <string.h>
1.211     stevesk    55: #include <unistd.h>
1.277     deraadt    56: #include <limits.h>
1.1       markus     57:
1.216     deraadt    58: #include "xmalloc.h"
1.51      markus     59: #include "ssh.h"
                     60: #include "ssh2.h"
1.59      djm        61: #include "sshpty.h"
1.1       markus     62: #include "packet.h"
                     63: #include "buffer.h"
1.173     djm        64: #include "match.h"
1.1       markus     65: #include "uidswap.h"
                     66: #include "compat.h"
1.78      markus     67: #include "channels.h"
1.216     deraadt    68: #include "key.h"
                     69: #include "cipher.h"
                     70: #include "kex.h"
                     71: #include "hostfile.h"
1.2       markus     72: #include "auth.h"
1.19      markus     73: #include "auth-options.h"
1.266     markus     74: #include "authfd.h"
1.50      markus     75: #include "pathnames.h"
1.51      markus     76: #include "log.h"
1.274     millert    77: #include "misc.h"
1.51      markus     78: #include "servconf.h"
1.59      djm        79: #include "sshlogin.h"
1.51      markus     80: #include "serverloop.h"
                     81: #include "canohost.h"
1.55      itojun     82: #include "session.h"
1.216     deraadt    83: #ifdef GSSAPI
                     84: #include "ssh-gss.h"
                     85: #endif
1.130     provos     86: #include "monitor_wrap.h"
1.225     markus     87: #include "sftp.h"
1.290     djm        88: #include "atomicio.h"
1.171     markus     89:
                     90: #ifdef KRB5
                     91: #include <kafs.h>
1.161     markus     92: #endif
                     93:
1.242     djm        94: #define IS_INTERNAL_SFTP(c) \
                     95:        (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
                     96:         (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
                     97:          c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
                     98:          c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
                     99:
1.1       markus    100: /* func */
                    101:
                    102: Session *session_new(void);
1.292     djm       103: void   session_set_fds(struct ssh *, Session *, int, int, int, int, int);
1.165     markus    104: void   session_pty_cleanup(Session *);
1.94      itojun    105: void   session_proctitle(Session *);
1.292     djm       106: int    session_setup_x11fwd(struct ssh *, Session *);
                    107: int    do_exec_pty(struct ssh *, Session *, const char *);
                    108: int    do_exec_no_pty(struct ssh *, Session *, const char *);
                    109: int    do_exec(struct ssh *, Session *, const char *);
                    110: void   do_login(struct ssh *, Session *, const char *);
                    111: void   do_child(struct ssh *, Session *, const char *);
1.73      djm       112: void   do_motd(void);
1.94      itojun    113: int    check_quietlogin(Session *, const char *);
1.1       markus    114:
1.292     djm       115: static void do_authenticated2(struct ssh *, Authctxt *);
1.94      itojun    116:
1.292     djm       117: static int session_pty_req(struct ssh *, Session *);
1.65      markus    118:
1.1       markus    119: /* import */
                    120: extern ServerOptions options;
                    121: extern char *__progname;
                    122: extern int debug_flag;
1.45      markus    123: extern u_int utmp_len;
1.21      markus    124: extern int startup_pipe;
1.74      markus    125: extern void destroy_sensitive_data(void);
1.179     dtucker   126: extern Buffer loginmsg;
1.294     djm       127: extern struct sshauthopt *auth_opts;
1.293     djm       128: char *tun_fwd_ifnames; /* serverloop.c */
1.21      markus    129:
1.34      markus    130: /* original command from peer. */
1.92      markus    131: const char *original_command = NULL;
1.34      markus    132:
1.1       markus    133: /* data */
1.237     djm       134: static int sessions_first_unused = -1;
                    135: static int sessions_nalloc = 0;
                    136: static Session *sessions = NULL;
1.1       markus    137:
1.248     djm       138: #define SUBSYSTEM_NONE                 0
                    139: #define SUBSYSTEM_EXT                  1
                    140: #define SUBSYSTEM_INT_SFTP             2
                    141: #define SUBSYSTEM_INT_SFTP_ERROR       3
1.225     markus    142:
1.129     provos    143: login_cap_t *lc;
1.28      millert   144:
1.165     markus    145: static int is_child = 0;
1.279     djm       146: static int in_chroot = 0;
1.165     markus    147:
1.290     djm       148: /* File containing userauth info, if ExposeAuthInfo set */
                    149: static char *auth_info_file = NULL;
                    150:
1.136     markus    151: /* Name and directory of socket for authentication agent forwarding. */
                    152: static char *auth_sock_name = NULL;
                    153: static char *auth_sock_dir = NULL;
                    154:
                    155: /* removes the agent forwarding socket */
                    156:
                    157: static void
1.165     markus    158: auth_sock_cleanup_proc(struct passwd *pw)
1.136     markus    159: {
                    160:        if (auth_sock_name != NULL) {
                    161:                temporarily_use_uid(pw);
                    162:                unlink(auth_sock_name);
                    163:                rmdir(auth_sock_dir);
                    164:                auth_sock_name = NULL;
                    165:                restore_uid();
                    166:        }
                    167: }
                    168:
                    169: static int
1.292     djm       170: auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw)
1.136     markus    171: {
                    172:        Channel *nc;
1.237     djm       173:        int sock = -1;
1.136     markus    174:
                    175:        if (auth_sock_name != NULL) {
                    176:                error("authentication forwarding requested twice.");
                    177:                return 0;
                    178:        }
                    179:
                    180:        /* Temporarily drop privileged uid for mkdir/bind. */
                    181:        temporarily_use_uid(pw);
                    182:
                    183:        /* Allocate a buffer for the socket name, and format the name. */
1.237     djm       184:        auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
1.136     markus    185:
                    186:        /* Create private directory for socket */
                    187:        if (mkdtemp(auth_sock_dir) == NULL) {
                    188:                packet_send_debug("Agent forwarding disabled: "
                    189:                    "mkdtemp() failed: %.100s", strerror(errno));
                    190:                restore_uid();
1.265     djm       191:                free(auth_sock_dir);
1.136     markus    192:                auth_sock_dir = NULL;
1.237     djm       193:                goto authsock_err;
1.136     markus    194:        }
1.237     djm       195:
                    196:        xasprintf(&auth_sock_name, "%s/agent.%ld",
                    197:            auth_sock_dir, (long) getpid());
1.136     markus    198:
1.274     millert   199:        /* Start a Unix listener on auth_sock_name. */
                    200:        sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
1.136     markus    201:
                    202:        /* Restore the privileged uid. */
                    203:        restore_uid();
                    204:
1.274     millert   205:        /* Check for socket/bind/listen failure. */
                    206:        if (sock < 0)
1.237     djm       207:                goto authsock_err;
1.136     markus    208:
                    209:        /* Allocate a channel for the authentication agent socket. */
1.292     djm       210:        nc = channel_new(ssh, "auth socket",
1.136     markus    211:            SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
                    212:            CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.156     markus    213:            0, "auth socket", 1);
1.245     djm       214:        nc->path = xstrdup(auth_sock_name);
1.136     markus    215:        return 1;
1.237     djm       216:
                    217:  authsock_err:
1.265     djm       218:        free(auth_sock_name);
1.237     djm       219:        if (auth_sock_dir != NULL) {
                    220:                rmdir(auth_sock_dir);
1.265     djm       221:                free(auth_sock_dir);
1.237     djm       222:        }
                    223:        if (sock != -1)
                    224:                close(sock);
                    225:        auth_sock_name = NULL;
                    226:        auth_sock_dir = NULL;
                    227:        return 0;
1.136     markus    228: }
                    229:
1.179     dtucker   230: static void
                    231: display_loginmsg(void)
                    232: {
1.183     djm       233:        if (buffer_len(&loginmsg) > 0) {
                    234:                buffer_append(&loginmsg, "\0", 1);
                    235:                printf("%s", (char *)buffer_ptr(&loginmsg));
                    236:                buffer_clear(&loginmsg);
                    237:        }
1.179     dtucker   238: }
1.136     markus    239:
1.290     djm       240: static void
                    241: prepare_auth_info_file(struct passwd *pw, struct sshbuf *info)
                    242: {
                    243:        int fd = -1, success = 0;
                    244:
                    245:        if (!options.expose_userauth_info || info == NULL)
                    246:                return;
                    247:
                    248:        temporarily_use_uid(pw);
                    249:        auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX");
                    250:        if ((fd = mkstemp(auth_info_file)) == -1) {
                    251:                error("%s: mkstemp: %s", __func__, strerror(errno));
                    252:                goto out;
                    253:        }
                    254:        if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info),
                    255:            sshbuf_len(info)) != sshbuf_len(info)) {
                    256:                error("%s: write: %s", __func__, strerror(errno));
                    257:                goto out;
                    258:        }
                    259:        if (close(fd) != 0) {
                    260:                error("%s: close: %s", __func__, strerror(errno));
                    261:                goto out;
                    262:        }
                    263:        success = 1;
                    264:  out:
                    265:        if (!success) {
                    266:                if (fd != -1)
                    267:                        close(fd);
                    268:                free(auth_info_file);
                    269:                auth_info_file = NULL;
                    270:        }
                    271:        restore_uid();
                    272: }
                    273:
1.294     djm       274: static void
                    275: set_permitopen_from_authopts(struct ssh *ssh, const struct sshauthopt *opts)
                    276: {
                    277:        char *tmp, *cp, *host;
                    278:        int port;
                    279:        size_t i;
                    280:
                    281:        if ((options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
                    282:                return;
                    283:        channel_clear_permitted_opens(ssh);
                    284:        for (i = 0; i < auth_opts->npermitopen; i++) {
                    285:                tmp = cp = xstrdup(auth_opts->permitopen[i]);
                    286:                /* This shouldn't fail as it has already been checked */
                    287:                if ((host = hpdelim(&cp)) == NULL)
                    288:                        fatal("%s: internal error: hpdelim", __func__);
                    289:                host = cleanhostname(host);
                    290:                if (cp == NULL || (port = permitopen_port(cp)) < 0)
                    291:                        fatal("%s: internal error: permitopen port",
                    292:                            __func__);
                    293:                channel_add_permitted_opens(ssh, host, port);
                    294:                free(tmp);
                    295:        }
                    296: }
                    297:
1.65      markus    298: void
1.292     djm       299: do_authenticated(struct ssh *ssh, Authctxt *authctxt)
1.65      markus    300: {
1.153     markus    301:        setproctitle("%s", authctxt->pw->pw_name);
                    302:
1.294     djm       303:        auth_log_authopts("active", auth_opts, 0);
                    304:
1.65      markus    305:        /* setup the channel layer */
1.274     millert   306:        /* XXX - streamlocal? */
1.294     djm       307:        set_permitopen_from_authopts(ssh, auth_opts);
                    308:        if (!auth_opts->permit_port_forwarding_flag ||
                    309:            options.disable_forwarding ||
1.261     djm       310:            (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
1.292     djm       311:                channel_disable_adm_local_opens(ssh);
1.261     djm       312:        else
1.292     djm       313:                channel_permit_all_opens(ssh);
1.252     dtucker   314:
                    315:        auth_debug_send();
1.65      markus    316:
1.290     djm       317:        prepare_auth_info_file(authctxt->pw, authctxt->session_info);
                    318:
1.292     djm       319:        do_authenticated2(ssh, authctxt);
1.290     djm       320:
1.292     djm       321:        do_cleanup(ssh, authctxt);
1.65      markus    322: }
                    323:
1.282     djm       324: /* Check untrusted xauth strings for metacharacters */
                    325: static int
                    326: xauth_valid_string(const char *s)
                    327: {
                    328:        size_t i;
                    329:
                    330:        for (i = 0; s[i] != '\0'; i++) {
                    331:                if (!isalnum((u_char)s[i]) &&
                    332:                    s[i] != '.' && s[i] != ':' && s[i] != '/' &&
                    333:                    s[i] != '-' && s[i] != '_')
                    334:                return 0;
                    335:        }
                    336:        return 1;
                    337: }
                    338:
1.269     dtucker   339: #define USE_PIPES 1
1.1       markus    340: /*
                    341:  * This is called to fork and execute a command when we have no tty.  This
                    342:  * will call do_child from the child, and server_loop from the parent after
                    343:  * setting up file descriptors and such.
                    344:  */
1.237     djm       345: int
1.292     djm       346: do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
1.1       markus    347: {
1.137     mpech     348:        pid_t pid;
1.238     markus    349: #ifdef USE_PIPES
                    350:        int pin[2], pout[2], perr[2];
                    351:
1.253     djm       352:        if (s == NULL)
                    353:                fatal("do_exec_no_pty: no session");
                    354:
1.238     markus    355:        /* Allocate pipes for communicating with the program. */
                    356:        if (pipe(pin) < 0) {
                    357:                error("%s: pipe in: %.100s", __func__, strerror(errno));
                    358:                return -1;
                    359:        }
                    360:        if (pipe(pout) < 0) {
                    361:                error("%s: pipe out: %.100s", __func__, strerror(errno));
                    362:                close(pin[0]);
                    363:                close(pin[1]);
                    364:                return -1;
                    365:        }
1.256     djm       366:        if (pipe(perr) < 0) {
                    367:                error("%s: pipe err: %.100s", __func__,
                    368:                    strerror(errno));
                    369:                close(pin[0]);
                    370:                close(pin[1]);
                    371:                close(pout[0]);
                    372:                close(pout[1]);
                    373:                return -1;
1.238     markus    374:        }
                    375: #else
1.237     djm       376:        int inout[2], err[2];
1.1       markus    377:
1.253     djm       378:        if (s == NULL)
                    379:                fatal("do_exec_no_pty: no session");
                    380:
1.1       markus    381:        /* Uses socket pairs to communicate with the program. */
1.237     djm       382:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
                    383:                error("%s: socketpair #1: %.100s", __func__, strerror(errno));
                    384:                return -1;
                    385:        }
1.256     djm       386:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
                    387:                error("%s: socketpair #2: %.100s", __func__,
                    388:                    strerror(errno));
                    389:                close(inout[0]);
                    390:                close(inout[1]);
                    391:                return -1;
1.237     djm       392:        }
1.238     markus    393: #endif
1.237     djm       394:
1.9       markus    395:        session_proctitle(s);
1.1       markus    396:
                    397:        /* Fork the child. */
1.237     djm       398:        switch ((pid = fork())) {
                    399:        case -1:
                    400:                error("%s: fork: %.100s", __func__, strerror(errno));
1.238     markus    401: #ifdef USE_PIPES
                    402:                close(pin[0]);
                    403:                close(pin[1]);
                    404:                close(pout[0]);
                    405:                close(pout[1]);
1.256     djm       406:                close(perr[0]);
1.238     markus    407:                close(perr[1]);
                    408: #else
1.237     djm       409:                close(inout[0]);
                    410:                close(inout[1]);
                    411:                close(err[0]);
1.256     djm       412:                close(err[1]);
1.238     markus    413: #endif
1.237     djm       414:                return -1;
                    415:        case 0:
1.165     markus    416:                is_child = 1;
1.144     markus    417:
1.1       markus    418:                /*
                    419:                 * Create a new session and process group since the 4.4BSD
                    420:                 * setlogin() affects the entire process group.
                    421:                 */
                    422:                if (setsid() < 0)
                    423:                        error("setsid failed: %.100s", strerror(errno));
                    424:
1.238     markus    425: #ifdef USE_PIPES
                    426:                /*
                    427:                 * Redirect stdin.  We close the parent side of the socket
                    428:                 * pair, and make the child side the standard input.
                    429:                 */
                    430:                close(pin[1]);
                    431:                if (dup2(pin[0], 0) < 0)
                    432:                        perror("dup2 stdin");
                    433:                close(pin[0]);
                    434:
                    435:                /* Redirect stdout. */
                    436:                close(pout[0]);
                    437:                if (dup2(pout[1], 1) < 0)
                    438:                        perror("dup2 stdout");
                    439:                close(pout[1]);
                    440:
                    441:                /* Redirect stderr. */
1.256     djm       442:                close(perr[0]);
1.238     markus    443:                if (dup2(perr[1], 2) < 0)
                    444:                        perror("dup2 stderr");
                    445:                close(perr[1]);
                    446: #else
1.1       markus    447:                /*
                    448:                 * Redirect stdin, stdout, and stderr.  Stdin and stdout will
                    449:                 * use the same socket, as some programs (particularly rdist)
                    450:                 * seem to depend on it.
                    451:                 */
                    452:                close(inout[1]);
1.256     djm       453:                close(err[1]);
1.1       markus    454:                if (dup2(inout[0], 0) < 0)      /* stdin */
                    455:                        perror("dup2 stdin");
1.237     djm       456:                if (dup2(inout[0], 1) < 0)      /* stdout (same as stdin) */
1.1       markus    457:                        perror("dup2 stdout");
1.238     markus    458:                close(inout[0]);
1.1       markus    459:                if (dup2(err[0], 2) < 0)        /* stderr */
                    460:                        perror("dup2 stderr");
1.238     markus    461:                close(err[0]);
                    462: #endif
1.1       markus    463:
                    464:                /* Do processing for the child (exec command etc). */
1.292     djm       465:                do_child(ssh, s, command);
1.1       markus    466:                /* NOTREACHED */
1.237     djm       467:        default:
                    468:                break;
1.1       markus    469:        }
1.237     djm       470:
1.1       markus    471:        s->pid = pid;
1.47      markus    472:        /* Set interactive/non-interactive mode. */
1.257     djm       473:        packet_set_interactive(s->display != NULL,
                    474:            options.ip_qos_interactive, options.ip_qos_bulk);
1.1       markus    475:
1.238     markus    476: #ifdef USE_PIPES
                    477:        /* We are the parent.  Close the child sides of the pipes. */
                    478:        close(pin[0]);
                    479:        close(pout[1]);
                    480:        close(perr[1]);
                    481:
1.292     djm       482:        session_set_fds(ssh, s, pin[1], pout[0], perr[0],
1.283     markus    483:            s->is_subsystem, 0);
1.238     markus    484: #else
1.1       markus    485:        /* We are the parent.  Close the child sides of the socket pairs. */
                    486:        close(inout[0]);
                    487:        close(err[0]);
                    488:
                    489:        /*
                    490:         * Enter the interactive session.  Note: server_loop must be able to
                    491:         * handle the case that fdin and fdout are the same.
                    492:         */
1.283     markus    493:        session_set_fds(s, inout[1], inout[1], err[1],
                    494:            s->is_subsystem, 0);
1.238     markus    495: #endif
1.237     djm       496:        return 0;
1.1       markus    497: }
                    498:
                    499: /*
                    500:  * This is called to fork and execute a command when we have a tty.  This
                    501:  * will call do_child from the child, and server_loop from the parent after
                    502:  * setting up file descriptors, controlling tty, updating wtmp, utmp,
                    503:  * lastlog, and other such operations.
                    504:  */
1.237     djm       505: int
1.292     djm       506: do_exec_pty(struct ssh *ssh, Session *s, const char *command)
1.1       markus    507: {
                    508:        int fdout, ptyfd, ttyfd, ptymaster;
                    509:        pid_t pid;
                    510:
                    511:        if (s == NULL)
                    512:                fatal("do_exec_pty: no session");
                    513:        ptyfd = s->ptyfd;
                    514:        ttyfd = s->ttyfd;
                    515:
1.237     djm       516:        /*
                    517:         * Create another descriptor of the pty master side for use as the
                    518:         * standard input.  We could use the original descriptor, but this
                    519:         * simplifies code in server_loop.  The descriptor is bidirectional.
                    520:         * Do this before forking (and cleanup in the child) so as to
                    521:         * detect and gracefully fail out-of-fd conditions.
                    522:         */
                    523:        if ((fdout = dup(ptyfd)) < 0) {
                    524:                error("%s: dup #1: %s", __func__, strerror(errno));
                    525:                close(ttyfd);
                    526:                close(ptyfd);
                    527:                return -1;
                    528:        }
                    529:        /* we keep a reference to the pty master */
                    530:        if ((ptymaster = dup(ptyfd)) < 0) {
                    531:                error("%s: dup #2: %s", __func__, strerror(errno));
                    532:                close(ttyfd);
                    533:                close(ptyfd);
                    534:                close(fdout);
                    535:                return -1;
                    536:        }
                    537:
1.1       markus    538:        /* Fork the child. */
1.237     djm       539:        switch ((pid = fork())) {
                    540:        case -1:
                    541:                error("%s: fork: %.100s", __func__, strerror(errno));
                    542:                close(fdout);
                    543:                close(ptymaster);
                    544:                close(ttyfd);
                    545:                close(ptyfd);
                    546:                return -1;
                    547:        case 0:
1.165     markus    548:                is_child = 1;
1.97      markus    549:
1.237     djm       550:                close(fdout);
                    551:                close(ptymaster);
                    552:
1.1       markus    553:                /* Close the master side of the pseudo tty. */
                    554:                close(ptyfd);
                    555:
                    556:                /* Make the pseudo tty our controlling tty. */
                    557:                pty_make_controlling_tty(&ttyfd, s->tty);
                    558:
1.103     markus    559:                /* Redirect stdin/stdout/stderr from the pseudo tty. */
                    560:                if (dup2(ttyfd, 0) < 0)
                    561:                        error("dup2 stdin: %s", strerror(errno));
                    562:                if (dup2(ttyfd, 1) < 0)
                    563:                        error("dup2 stdout: %s", strerror(errno));
                    564:                if (dup2(ttyfd, 2) < 0)
                    565:                        error("dup2 stderr: %s", strerror(errno));
1.1       markus    566:
                    567:                /* Close the extra descriptor for the pseudo tty. */
                    568:                close(ttyfd);
                    569:
1.24      markus    570:                /* record login, etc. similar to login(1) */
1.292     djm       571:                do_login(ssh, s, command);
1.228     djm       572:
1.237     djm       573:                /*
                    574:                 * Do common processing for the child, such as execing
                    575:                 * the command.
                    576:                 */
1.292     djm       577:                do_child(ssh, s, command);
1.1       markus    578:                /* NOTREACHED */
1.237     djm       579:        default:
                    580:                break;
1.1       markus    581:        }
                    582:        s->pid = pid;
                    583:
                    584:        /* Parent.  Close the slave side of the pseudo tty. */
                    585:        close(ttyfd);
                    586:
1.237     djm       587:        /* Enter interactive session. */
1.1       markus    588:        s->ptymaster = ptymaster;
1.257     djm       589:        packet_set_interactive(1,
                    590:            options.ip_qos_interactive, options.ip_qos_bulk);
1.292     djm       591:        session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
1.237     djm       592:        return 0;
1.24      markus    593: }
                    594:
1.90      markus    595: /*
                    596:  * This is called to fork and execute a command.  If another command is
                    597:  * to be forced, execute that instead.
                    598:  */
1.237     djm       599: int
1.292     djm       600: do_exec(struct ssh *ssh, Session *s, const char *command)
1.90      markus    601: {
1.237     djm       602:        int ret;
1.280     djm       603:        const char *forced = NULL, *tty = NULL;
                    604:        char session_type[1024];
1.237     djm       605:
1.210     dtucker   606:        if (options.adm_forced_command) {
                    607:                original_command = command;
                    608:                command = options.adm_forced_command;
1.267     djm       609:                forced = "(config)";
1.294     djm       610:        } else if (auth_opts->force_command != NULL) {
1.90      markus    611:                original_command = command;
1.294     djm       612:                command = auth_opts->force_command;
1.267     djm       613:                forced = "(key-option)";
                    614:        }
                    615:        if (forced != NULL) {
1.248     djm       616:                if (IS_INTERNAL_SFTP(command)) {
                    617:                        s->is_subsystem = s->is_subsystem ?
                    618:                            SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
                    619:                } else if (s->is_subsystem)
1.225     markus    620:                        s->is_subsystem = SUBSYSTEM_EXT;
1.267     djm       621:                snprintf(session_type, sizeof(session_type),
                    622:                    "forced-command %s '%.900s'", forced, command);
                    623:        } else if (s->is_subsystem) {
                    624:                snprintf(session_type, sizeof(session_type),
                    625:                    "subsystem '%.900s'", s->subsys);
                    626:        } else if (command == NULL) {
                    627:                snprintf(session_type, sizeof(session_type), "shell");
                    628:        } else {
                    629:                /* NB. we don't log unforced commands to preserve privacy */
                    630:                snprintf(session_type, sizeof(session_type), "command");
1.90      markus    631:        }
1.163     markus    632:
1.267     djm       633:        if (s->ttyfd != -1) {
                    634:                tty = s->tty;
                    635:                if (strncmp(tty, "/dev/", 5) == 0)
                    636:                        tty += 5;
                    637:        }
                    638:
1.280     djm       639:        verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
1.267     djm       640:            session_type,
                    641:            tty == NULL ? "" : " on ",
                    642:            tty == NULL ? "" : tty,
                    643:            s->pw->pw_name,
1.281     djm       644:            ssh_remote_ipaddr(ssh),
                    645:            ssh_remote_port(ssh),
1.280     djm       646:            s->self);
1.267     djm       647:
1.163     markus    648: #ifdef GSSAPI
                    649:        if (options.gss_authentication) {
                    650:                temporarily_use_uid(s->pw);
                    651:                ssh_gssapi_storecreds();
                    652:                restore_uid();
                    653:        }
                    654: #endif
1.90      markus    655:        if (s->ttyfd != -1)
1.292     djm       656:                ret = do_exec_pty(ssh, s, command);
1.90      markus    657:        else
1.292     djm       658:                ret = do_exec_no_pty(ssh, s, command);
1.90      markus    659:
1.92      markus    660:        original_command = NULL;
1.179     dtucker   661:
                    662:        /*
                    663:         * Clear loginmsg: it's the child's responsibility to display
                    664:         * it to the user, otherwise multiple sessions may accumulate
                    665:         * multiple copies of the login messages.
                    666:         */
                    667:        buffer_clear(&loginmsg);
1.237     djm       668:
                    669:        return ret;
1.90      markus    670: }
                    671:
                    672:
1.24      markus    673: /* administrative, login(1)-like work */
                    674: void
1.292     djm       675: do_login(struct ssh *ssh, Session *s, const char *command)
1.24      markus    676: {
                    677:        socklen_t fromlen;
                    678:        struct sockaddr_storage from;
                    679:        struct passwd * pw = s->pw;
                    680:        pid_t pid = getpid();
                    681:
                    682:        /*
                    683:         * Get IP address of client. If the connection is not a socket, let
                    684:         * the address be 0.0.0.0.
                    685:         */
                    686:        memset(&from, 0, sizeof(from));
1.148     stevesk   687:        fromlen = sizeof(from);
1.24      markus    688:        if (packet_connection_is_on_socket()) {
                    689:                if (getpeername(packet_get_connection_in(),
1.200     deraadt   690:                    (struct sockaddr *)&from, &fromlen) < 0) {
1.24      markus    691:                        debug("getpeername: %.100s", strerror(errno));
1.165     markus    692:                        cleanup_exit(255);
1.24      markus    693:                }
                    694:        }
1.25      markus    695:
1.24      markus    696:        /* Record that there was a login on that tty from the remote host. */
1.133     markus    697:        if (!use_privsep)
                    698:                record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
1.281     djm       699:                    session_get_remote_name_or_ip(ssh, utmp_len,
1.158     markus    700:                    options.use_dns),
1.148     stevesk   701:                    (struct sockaddr *)&from, fromlen);
1.24      markus    702:
1.73      djm       703:        if (check_quietlogin(s, command))
1.24      markus    704:                return;
1.73      djm       705:
1.179     dtucker   706:        display_loginmsg();
1.73      djm       707:
                    708:        do_motd();
                    709: }
                    710:
                    711: /*
                    712:  * Display the message of the day.
                    713:  */
                    714: void
                    715: do_motd(void)
                    716: {
                    717:        FILE *f;
                    718:        char buf[256];
                    719:
1.24      markus    720:        if (options.print_motd) {
1.28      millert   721:                f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
                    722:                    "/etc/motd"), "r");
1.24      markus    723:                if (f) {
                    724:                        while (fgets(buf, sizeof(buf), f))
                    725:                                fputs(buf, stdout);
                    726:                        fclose(f);
                    727:                }
1.2       markus    728:        }
1.73      djm       729: }
                    730:
                    731:
                    732: /*
                    733:  * Check for quiet login, either .hushlogin or command given.
                    734:  */
                    735: int
                    736: check_quietlogin(Session *s, const char *command)
                    737: {
                    738:        char buf[256];
1.96      dugsong   739:        struct passwd *pw = s->pw;
1.73      djm       740:        struct stat st;
                    741:
                    742:        /* Return 1 if .hushlogin exists or a command given. */
                    743:        if (command != NULL)
                    744:                return 1;
                    745:        snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
                    746:        if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
                    747:                return 1;
                    748:        return 0;
1.1       markus    749: }
                    750:
                    751: /*
                    752:  * Reads environment variables from the given file and adds/overrides them
                    753:  * into the environment.  If the file does not exist, this does nothing.
                    754:  * Otherwise, it must consist of empty lines, comments (line starts with '#')
                    755:  * and assignments of the form name=value.  No other forms are allowed.
                    756:  */
1.94      itojun    757: static void
1.45      markus    758: read_environment_file(char ***env, u_int *envsize,
1.112     deraadt   759:        const char *filename)
1.1       markus    760: {
                    761:        FILE *f;
                    762:        char buf[4096];
                    763:        char *cp, *value;
1.142     deraadt   764:        u_int lineno = 0;
1.1       markus    765:
                    766:        f = fopen(filename, "r");
                    767:        if (!f)
                    768:                return;
                    769:
                    770:        while (fgets(buf, sizeof(buf), f)) {
1.142     deraadt   771:                if (++lineno > 1000)
                    772:                        fatal("Too many lines in environment file %s", filename);
1.1       markus    773:                for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
                    774:                        ;
                    775:                if (!*cp || *cp == '#' || *cp == '\n')
                    776:                        continue;
1.224     gilles    777:
                    778:                cp[strcspn(cp, "\n")] = '\0';
                    779:
1.1       markus    780:                value = strchr(cp, '=');
                    781:                if (value == NULL) {
1.142     deraadt   782:                        fprintf(stderr, "Bad line %u in %.100s\n", lineno,
                    783:                            filename);
1.1       markus    784:                        continue;
                    785:                }
1.14      deraadt   786:                /*
                    787:                 * Replace the equals sign by nul, and advance value to
                    788:                 * the value string.
                    789:                 */
1.1       markus    790:                *value = '\0';
                    791:                value++;
                    792:                child_set_env(env, envsize, cp, value);
                    793:        }
                    794:        fclose(f);
                    795: }
                    796:
1.127     markus    797: static char **
1.292     djm       798: do_setup_env(struct ssh *ssh, Session *s, const char *shell)
1.1       markus    799: {
                    800:        char buf[256];
1.294     djm       801:        size_t n;
1.127     markus    802:        u_int i, envsize;
1.294     djm       803:        char *ocp, *cp, **env, *laddr;
1.127     markus    804:        struct passwd *pw = s->pw;
1.1       markus    805:
                    806:        /* Initialize the environment. */
                    807:        envsize = 100;
1.220     djm       808:        env = xcalloc(envsize, sizeof(char *));
1.1       markus    809:        env[0] = NULL;
                    810:
1.161     markus    811: #ifdef GSSAPI
1.168     djm       812:        /* Allow any GSSAPI methods that we've used to alter
1.161     markus    813:         * the childs environment as they see fit
                    814:         */
                    815:        ssh_gssapi_do_child(&env, &envsize);
                    816: #endif
                    817:
1.284     djm       818:        /* Set basic environment. */
                    819:        for (i = 0; i < s->num_env; i++)
                    820:                child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
                    821:
                    822:        child_set_env(&env, &envsize, "USER", pw->pw_name);
                    823:        child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
                    824:        child_set_env(&env, &envsize, "HOME", pw->pw_dir);
                    825:        if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
                    826:                child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
                    827:        else
                    828:                child_set_env(&env, &envsize, "PATH", getenv("PATH"));
                    829:
                    830:        snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
                    831:        child_set_env(&env, &envsize, "MAIL", buf);
                    832:
                    833:        /* Normal systems set SHELL by default. */
                    834:        child_set_env(&env, &envsize, "SHELL", shell);
1.1       markus    835:
                    836:        if (getenv("TZ"))
                    837:                child_set_env(&env, &envsize, "TZ", getenv("TZ"));
                    838:
1.294     djm       839:        /* Set custom environment options from pubkey authentication. */
                    840:        if (options.permit_user_env) {
                    841:                for (n = 0 ; n < auth_opts->nenv; n++) {
                    842:                        ocp = xstrdup(auth_opts->env[n]);
                    843:                        cp = strchr(ocp, '=');
                    844:                        if (*cp == '=') {
                    845:                                *cp = '\0';
                    846:                                child_set_env(&env, &envsize, ocp, cp + 1);
                    847:                        }
                    848:                        free(ocp);
                    849:                }
1.1       markus    850:        }
                    851:
1.149     stevesk   852:        /* SSH_CLIENT deprecated */
1.1       markus    853:        snprintf(buf, sizeof buf, "%.50s %d %d",
1.281     djm       854:            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
                    855:            ssh_local_port(ssh));
1.1       markus    856:        child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1.149     stevesk   857:
1.154     markus    858:        laddr = get_local_ipaddr(packet_get_connection_in());
1.149     stevesk   859:        snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1.281     djm       860:            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
                    861:            laddr, ssh_local_port(ssh));
1.265     djm       862:        free(laddr);
1.149     stevesk   863:        child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1.1       markus    864:
1.293     djm       865:        if (tun_fwd_ifnames != NULL)
                    866:                child_set_env(&env, &envsize, "SSH_TUNNEL", tun_fwd_ifnames);
1.290     djm       867:        if (auth_info_file != NULL)
                    868:                child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file);
1.60      markus    869:        if (s->ttyfd != -1)
                    870:                child_set_env(&env, &envsize, "SSH_TTY", s->tty);
                    871:        if (s->term)
                    872:                child_set_env(&env, &envsize, "TERM", s->term);
                    873:        if (s->display)
                    874:                child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34      markus    875:        if (original_command)
                    876:                child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
                    877:                    original_command);
1.96      dugsong   878: #ifdef KRB5
                    879:        if (s->authctxt->krb5_ticket_file)
                    880:                child_set_env(&env, &envsize, "KRB5CCNAME",
1.112     deraadt   881:                    s->authctxt->krb5_ticket_file);
1.96      dugsong   882: #endif
1.136     markus    883:        if (auth_sock_name != NULL)
1.1       markus    884:                child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1.136     markus    885:                    auth_sock_name);
1.1       markus    886:
                    887:        /* read $HOME/.ssh/environment. */
1.284     djm       888:        if (options.permit_user_env) {
1.14      deraadt   889:                snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
                    890:                    pw->pw_dir);
1.1       markus    891:                read_environment_file(&env, &envsize, buf);
                    892:        }
                    893:        if (debug_flag) {
                    894:                /* dump the environment */
                    895:                fprintf(stderr, "Environment:\n");
                    896:                for (i = 0; env[i]; i++)
                    897:                        fprintf(stderr, "  %.200s\n", env[i]);
                    898:        }
1.127     markus    899:        return env;
                    900: }
                    901:
                    902: /*
                    903:  * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
                    904:  * first in this order).
                    905:  */
                    906: static void
1.294     djm       907: do_rc_files(struct ssh *ssh, Session *s, const char *shell)
1.127     markus    908: {
                    909:        FILE *f = NULL;
                    910:        char cmd[1024];
                    911:        int do_xauth;
                    912:        struct stat st;
                    913:
                    914:        do_xauth =
                    915:            s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
                    916:
1.231     djm       917:        /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
1.232     djm       918:        if (!s->is_subsystem && options.adm_forced_command == NULL &&
1.294     djm       919:            auth_opts->permit_user_rc && options.permit_user_rc &&
1.273     djm       920:            stat(_PATH_SSH_USER_RC, &st) >= 0) {
1.127     markus    921:                snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
                    922:                    shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
                    923:                if (debug_flag)
                    924:                        fprintf(stderr, "Running %s\n", cmd);
                    925:                f = popen(cmd, "w");
                    926:                if (f) {
                    927:                        if (do_xauth)
                    928:                                fprintf(f, "%s %s\n", s->auth_proto,
                    929:                                    s->auth_data);
                    930:                        pclose(f);
                    931:                } else
                    932:                        fprintf(stderr, "Could not run %s\n",
                    933:                            _PATH_SSH_USER_RC);
                    934:        } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
                    935:                if (debug_flag)
                    936:                        fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
                    937:                            _PATH_SSH_SYSTEM_RC);
                    938:                f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
                    939:                if (f) {
                    940:                        if (do_xauth)
                    941:                                fprintf(f, "%s %s\n", s->auth_proto,
                    942:                                    s->auth_data);
                    943:                        pclose(f);
                    944:                } else
                    945:                        fprintf(stderr, "Could not run %s\n",
                    946:                            _PATH_SSH_SYSTEM_RC);
                    947:        } else if (do_xauth && options.xauth_location != NULL) {
                    948:                /* Add authority data to .Xauthority if appropriate. */
                    949:                if (debug_flag) {
                    950:                        fprintf(stderr,
1.151     stevesk   951:                            "Running %.500s remove %.100s\n",
1.165     markus    952:                            options.xauth_location, s->auth_display);
1.151     stevesk   953:                        fprintf(stderr,
                    954:                            "%.500s add %.100s %.100s %.100s\n",
1.127     markus    955:                            options.xauth_location, s->auth_display,
                    956:                            s->auth_proto, s->auth_data);
                    957:                }
                    958:                snprintf(cmd, sizeof cmd, "%s -q -",
                    959:                    options.xauth_location);
                    960:                f = popen(cmd, "w");
                    961:                if (f) {
1.151     stevesk   962:                        fprintf(f, "remove %s\n",
                    963:                            s->auth_display);
1.127     markus    964:                        fprintf(f, "add %s %s %s\n",
                    965:                            s->auth_display, s->auth_proto,
                    966:                            s->auth_data);
                    967:                        pclose(f);
                    968:                } else {
                    969:                        fprintf(stderr, "Could not run %s\n",
                    970:                            cmd);
                    971:                }
                    972:        }
                    973: }
                    974:
                    975: static void
                    976: do_nologin(struct passwd *pw)
                    977: {
                    978:        FILE *f = NULL;
1.251     dtucker   979:        char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
                    980:        struct stat sb;
1.127     markus    981:
1.260     guenther  982:        if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
1.251     dtucker   983:                return;
                    984:        nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
                    985:
                    986:        if (stat(nl, &sb) == -1) {
                    987:                if (nl != def_nl)
1.265     djm       988:                        free(nl);
1.251     dtucker   989:                return;
                    990:        }
                    991:
                    992:        /* /etc/nologin exists.  Print its contents if we can and exit. */
                    993:        logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
                    994:        if ((f = fopen(nl, "r")) != NULL) {
1.127     markus    995:                while (fgets(buf, sizeof(buf), f))
                    996:                        fputs(buf, stderr);
                    997:                fclose(f);
                    998:        }
1.251     dtucker   999:        exit(254);
1.127     markus   1000: }
                   1001:
1.226     djm      1002: /*
                   1003:  * Chroot into a directory after checking it for safety: all path components
                   1004:  * must be root-owned directories with strict permissions.
                   1005:  */
                   1006: static void
                   1007: safely_chroot(const char *path, uid_t uid)
                   1008: {
                   1009:        const char *cp;
1.277     deraadt  1010:        char component[PATH_MAX];
1.226     djm      1011:        struct stat st;
                   1012:
                   1013:        if (*path != '/')
                   1014:                fatal("chroot path does not begin at root");
                   1015:        if (strlen(path) >= sizeof(component))
                   1016:                fatal("chroot path too long");
                   1017:
                   1018:        /*
                   1019:         * Descend the path, checking that each component is a
                   1020:         * root-owned directory with strict permissions.
                   1021:         */
                   1022:        for (cp = path; cp != NULL;) {
                   1023:                if ((cp = strchr(cp, '/')) == NULL)
                   1024:                        strlcpy(component, path, sizeof(component));
                   1025:                else {
                   1026:                        cp++;
                   1027:                        memcpy(component, path, cp - path);
                   1028:                        component[cp - path] = '\0';
                   1029:                }
                   1030:
                   1031:                debug3("%s: checking '%s'", __func__, component);
                   1032:
                   1033:                if (stat(component, &st) != 0)
                   1034:                        fatal("%s: stat(\"%s\"): %s", __func__,
                   1035:                            component, strerror(errno));
                   1036:                if (st.st_uid != 0 || (st.st_mode & 022) != 0)
                   1037:                        fatal("bad ownership or modes for chroot "
                   1038:                            "directory %s\"%s\"",
                   1039:                            cp == NULL ? "" : "component ", component);
                   1040:                if (!S_ISDIR(st.st_mode))
                   1041:                        fatal("chroot path %s\"%s\" is not a directory",
                   1042:                            cp == NULL ? "" : "component ", component);
                   1043:
                   1044:        }
                   1045:
                   1046:        if (chdir(path) == -1)
                   1047:                fatal("Unable to chdir to chroot path \"%s\": "
                   1048:                    "%s", path, strerror(errno));
                   1049:        if (chroot(path) == -1)
                   1050:                fatal("chroot(\"%s\"): %s", path, strerror(errno));
                   1051:        if (chdir("/") == -1)
                   1052:                fatal("%s: chdir(/) after chroot: %s",
                   1053:                    __func__, strerror(errno));
                   1054:        verbose("Changed root directory to \"%s\"", path);
                   1055: }
                   1056:
1.127     markus   1057: /* Set login name, uid, gid, and groups. */
1.130     provos   1058: void
1.127     markus   1059: do_setusercontext(struct passwd *pw)
                   1060: {
1.295   ! djm      1061:        char uidstr[32], *chroot_path, *tmp;
1.227     djm      1062:
1.127     markus   1063:        if (getuid() == 0 || geteuid() == 0) {
1.226     djm      1064:                /* Prepare groups */
1.127     markus   1065:                if (setusercontext(lc, pw, pw->pw_uid,
1.226     djm      1066:                    (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
1.127     markus   1067:                        perror("unable to set user context");
                   1068:                        exit(1);
                   1069:                }
1.226     djm      1070:
1.279     djm      1071:                if (!in_chroot && options.chroot_directory != NULL &&
1.226     djm      1072:                    strcasecmp(options.chroot_directory, "none") != 0) {
1.227     djm      1073:                         tmp = tilde_expand_filename(options.chroot_directory,
                   1074:                            pw->pw_uid);
1.295   ! djm      1075:                        snprintf(uidstr, sizeof(uidstr), "%llu",
        !          1076:                            (unsigned long long)pw->pw_uid);
1.227     djm      1077:                        chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1.295   ! djm      1078:                            "u", pw->pw_name, "U", uidstr, (char *)NULL);
1.226     djm      1079:                        safely_chroot(chroot_path, pw->pw_uid);
1.227     djm      1080:                        free(tmp);
1.226     djm      1081:                        free(chroot_path);
1.264     djm      1082:                        /* Make sure we don't attempt to chroot again */
                   1083:                        free(options.chroot_directory);
                   1084:                        options.chroot_directory = NULL;
1.279     djm      1085:                        in_chroot = 1;
1.226     djm      1086:                }
                   1087:
                   1088:                /* Set UID */
                   1089:                if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
                   1090:                        perror("unable to set user context (setuser)");
                   1091:                        exit(1);
                   1092:                }
1.264     djm      1093:        } else if (options.chroot_directory != NULL &&
                   1094:            strcasecmp(options.chroot_directory, "none") != 0) {
                   1095:                fatal("server lacks privileges to chroot to ChrootDirectory");
1.263     dtucker  1096:        }
1.264     djm      1097:
1.127     markus   1098:        if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
                   1099:                fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
                   1100: }
                   1101:
1.131     markus   1102: static void
1.172     markus   1103: do_pwchange(Session *s)
                   1104: {
1.179     dtucker  1105:        fflush(NULL);
1.172     markus   1106:        fprintf(stderr, "WARNING: Your password has expired.\n");
                   1107:        if (s->ttyfd != -1) {
1.178     deraadt  1108:                fprintf(stderr,
1.172     markus   1109:                    "You must change your password now and login again!\n");
                   1110:                execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
                   1111:                perror("passwd");
                   1112:        } else {
                   1113:                fprintf(stderr,
                   1114:                    "Password change required but no TTY available.\n");
                   1115:        }
                   1116:        exit(1);
                   1117: }
                   1118:
                   1119: static void
1.292     djm      1120: child_close_fds(struct ssh *ssh)
1.172     markus   1121: {
1.276     djm      1122:        extern int auth_sock;
1.266     markus   1123:
1.276     djm      1124:        if (auth_sock != -1) {
                   1125:                close(auth_sock);
                   1126:                auth_sock = -1;
1.266     markus   1127:        }
                   1128:
1.172     markus   1129:        if (packet_get_connection_in() == packet_get_connection_out())
                   1130:                close(packet_get_connection_in());
                   1131:        else {
                   1132:                close(packet_get_connection_in());
                   1133:                close(packet_get_connection_out());
                   1134:        }
                   1135:        /*
                   1136:         * Close all descriptors related to channels.  They will still remain
                   1137:         * open in the parent.
                   1138:         */
                   1139:        /* XXX better use close-on-exec? -markus */
1.292     djm      1140:        channel_close_all(ssh);
1.172     markus   1141:
                   1142:        /*
                   1143:         * Close any extra file descriptors.  Note that there may still be
                   1144:         * descriptors left by system functions.  They will be closed later.
                   1145:         */
                   1146:        endpwent();
                   1147:
                   1148:        /*
1.188     djm      1149:         * Close any extra open file descriptors so that we don't have them
1.172     markus   1150:         * hanging around in clients.  Note that we want to do this after
                   1151:         * initgroups, because at least on Solaris 2.3 it leaves file
                   1152:         * descriptors open.
                   1153:         */
1.258     djm      1154:        closefrom(STDERR_FILENO + 1);
1.172     markus   1155: }
                   1156:
1.127     markus   1157: /*
                   1158:  * Performs common processing for the child, such as setting up the
                   1159:  * environment, closing extra file descriptors, setting the user and group
                   1160:  * ids, and executing the command or shell.
                   1161:  */
1.225     markus   1162: #define ARGV_MAX 10
1.127     markus   1163: void
1.292     djm      1164: do_child(struct ssh *ssh, Session *s, const char *command)
1.127     markus   1165: {
                   1166:        extern char **environ;
                   1167:        char **env;
1.225     markus   1168:        char *argv[ARGV_MAX];
1.284     djm      1169:        const char *shell, *shell0;
1.127     markus   1170:        struct passwd *pw = s->pw;
1.239     djm      1171:        int r = 0;
1.127     markus   1172:
                   1173:        /* remove hostkey from the child's memory */
                   1174:        destroy_sensitive_data();
1.287     markus   1175:        packet_clear_keys();
1.127     markus   1176:
1.172     markus   1177:        /* Force a password change */
                   1178:        if (s->authctxt->force_pwchange) {
                   1179:                do_setusercontext(pw);
1.292     djm      1180:                child_close_fds(ssh);
1.172     markus   1181:                do_pwchange(s);
                   1182:                exit(1);
                   1183:        }
                   1184:
1.127     markus   1185:        /*
                   1186:         * Login(1) does this as well, and it needs uid 0 for the "-h"
                   1187:         * switch, so we let login(1) to this for us.
                   1188:         */
1.284     djm      1189:        do_nologin(pw);
                   1190:        do_setusercontext(pw);
1.127     markus   1191:
                   1192:        /*
                   1193:         * Get the shell from the password data.  An empty shell field is
                   1194:         * legal, and means /bin/sh.
                   1195:         */
                   1196:        shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.152     markus   1197:
                   1198:        /*
                   1199:         * Make sure $SHELL points to the shell from the password file,
                   1200:         * even if shell is overridden from login.conf
                   1201:         */
1.292     djm      1202:        env = do_setup_env(ssh, s, shell);
1.152     markus   1203:
1.127     markus   1204:        shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
                   1205:
1.1       markus   1206:        /*
                   1207:         * Close the connection descriptors; note that this is the child, and
                   1208:         * the server will still have the socket open, and it is important
                   1209:         * that we do not shutdown it.  Note that the descriptors cannot be
                   1210:         * closed before building the environment, as we call
1.281     djm      1211:         * ssh_remote_ipaddr there.
1.1       markus   1212:         */
1.292     djm      1213:        child_close_fds(ssh);
1.1       markus   1214:
                   1215:        /*
1.125     deraadt  1216:         * Must take new environment into use so that .ssh/rc,
                   1217:         * /etc/ssh/sshrc and xauth are run in the proper environment.
1.1       markus   1218:         */
                   1219:        environ = env;
1.170     jakob    1220:
                   1221: #ifdef KRB5
                   1222:        /*
                   1223:         * At this point, we check to see if AFS is active and if we have
                   1224:         * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
                   1225:         * if we can (and need to) extend the ticket into an AFS token. If
                   1226:         * we don't do this, we run into potential problems if the user's
                   1227:         * home directory is in AFS and it's not world-readable.
                   1228:         */
                   1229:
                   1230:        if (options.kerberos_get_afs_token && k_hasafs() &&
1.185     djm      1231:            (s->authctxt->krb5_ctx != NULL)) {
1.170     jakob    1232:                char cell[64];
                   1233:
                   1234:                debug("Getting AFS token");
                   1235:
                   1236:                k_setpag();
                   1237:
                   1238:                if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
                   1239:                        krb5_afslog(s->authctxt->krb5_ctx,
                   1240:                            s->authctxt->krb5_fwd_ccache, cell, NULL);
                   1241:
                   1242:                krb5_afslog_home(s->authctxt->krb5_ctx,
                   1243:                    s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
                   1244:        }
                   1245: #endif
1.104     markus   1246:
1.188     djm      1247:        /* Change current directory to the user's home directory. */
1.104     markus   1248:        if (chdir(pw->pw_dir) < 0) {
1.239     djm      1249:                /* Suppress missing homedir warning for chroot case */
                   1250:                r = login_getcapbool(lc, "requirehome", 0);
1.279     djm      1251:                if (r || !in_chroot) {
1.239     djm      1252:                        fprintf(stderr, "Could not chdir to home "
                   1253:                            "directory %s: %s\n", pw->pw_dir,
                   1254:                            strerror(errno));
1.279     djm      1255:                }
1.239     djm      1256:                if (r)
1.104     markus   1257:                        exit(1);
                   1258:        }
1.230     djm      1259:
                   1260:        closefrom(STDERR_FILENO + 1);
1.1       markus   1261:
1.294     djm      1262:        do_rc_files(ssh, s, shell);
1.67      markus   1263:
                   1264:        /* restore SIGPIPE for child */
1.217     stevesk  1265:        signal(SIGPIPE, SIG_DFL);
1.67      markus   1266:
1.248     djm      1267:        if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
                   1268:                printf("This service allows sftp connections only.\n");
                   1269:                fflush(NULL);
                   1270:                exit(1);
                   1271:        } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
1.225     markus   1272:                extern int optind, optreset;
                   1273:                int i;
                   1274:                char *p, *args;
                   1275:
1.246     stevesk  1276:                setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
1.243     millert  1277:                args = xstrdup(command ? command : "sftp-server");
1.225     markus   1278:                for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
                   1279:                        if (i < ARGV_MAX - 1)
                   1280:                                argv[i++] = p;
                   1281:                argv[i] = NULL;
                   1282:                optind = optreset = 1;
                   1283:                __progname = argv[0];
1.226     djm      1284:                exit(sftp_server_main(i, argv, s->pw));
1.225     markus   1285:        }
1.247     djm      1286:
                   1287:        fflush(NULL);
1.225     markus   1288:
1.127     markus   1289:        /* Get the last component of the shell name. */
                   1290:        if ((shell0 = strrchr(shell, '/')) != NULL)
                   1291:                shell0++;
                   1292:        else
                   1293:                shell0 = shell;
                   1294:
1.1       markus   1295:        /*
                   1296:         * If we have no command, execute the shell.  In this case, the shell
                   1297:         * name to be passed in argv[0] is preceded by '-' to indicate that
                   1298:         * this is a login shell.
                   1299:         */
                   1300:        if (!command) {
1.127     markus   1301:                char argv0[256];
1.1       markus   1302:
1.127     markus   1303:                /* Start the shell.  Set initial character to '-'. */
                   1304:                argv0[0] = '-';
1.1       markus   1305:
1.127     markus   1306:                if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1.128     markus   1307:                    >= sizeof(argv0) - 1) {
1.127     markus   1308:                        errno = EINVAL;
1.1       markus   1309:                        perror(shell);
                   1310:                        exit(1);
1.127     markus   1311:                }
1.1       markus   1312:
1.127     markus   1313:                /* Execute the shell. */
                   1314:                argv[0] = argv0;
                   1315:                argv[1] = NULL;
                   1316:                execve(shell, argv, env);
                   1317:
                   1318:                /* Executing the shell failed. */
                   1319:                perror(shell);
                   1320:                exit(1);
1.1       markus   1321:        }
                   1322:        /*
                   1323:         * Execute the command using the user's shell.  This uses the -c
                   1324:         * option to execute the command.
                   1325:         */
1.127     markus   1326:        argv[0] = (char *) shell0;
1.1       markus   1327:        argv[1] = "-c";
                   1328:        argv[2] = (char *) command;
                   1329:        argv[3] = NULL;
                   1330:        execve(shell, argv, env);
                   1331:        perror(shell);
                   1332:        exit(1);
                   1333: }
                   1334:
1.237     djm      1335: void
                   1336: session_unused(int id)
                   1337: {
                   1338:        debug3("%s: session id %d unused", __func__, id);
                   1339:        if (id >= options.max_sessions ||
                   1340:            id >= sessions_nalloc) {
                   1341:                fatal("%s: insane session id %d (max %d nalloc %d)",
                   1342:                    __func__, id, options.max_sessions, sessions_nalloc);
                   1343:        }
1.270     tedu     1344:        memset(&sessions[id], 0, sizeof(*sessions));
1.237     djm      1345:        sessions[id].self = id;
                   1346:        sessions[id].used = 0;
                   1347:        sessions[id].chanid = -1;
                   1348:        sessions[id].ptyfd = -1;
                   1349:        sessions[id].ttyfd = -1;
                   1350:        sessions[id].ptymaster = -1;
                   1351:        sessions[id].x11_chanids = NULL;
                   1352:        sessions[id].next_unused = sessions_first_unused;
                   1353:        sessions_first_unused = id;
                   1354: }
                   1355:
1.1       markus   1356: Session *
                   1357: session_new(void)
                   1358: {
1.237     djm      1359:        Session *s, *tmp;
                   1360:
                   1361:        if (sessions_first_unused == -1) {
                   1362:                if (sessions_nalloc >= options.max_sessions)
                   1363:                        return NULL;
                   1364:                debug2("%s: allocate (allocated %d max %d)",
                   1365:                    __func__, sessions_nalloc, options.max_sessions);
1.288     deraadt  1366:                tmp = xrecallocarray(sessions, sessions_nalloc,
                   1367:                    sessions_nalloc + 1, sizeof(*sessions));
1.237     djm      1368:                if (tmp == NULL) {
                   1369:                        error("%s: cannot allocate %d sessions",
                   1370:                            __func__, sessions_nalloc + 1);
                   1371:                        return NULL;
                   1372:                }
                   1373:                sessions = tmp;
                   1374:                session_unused(sessions_nalloc++);
                   1375:        }
                   1376:
                   1377:        if (sessions_first_unused >= sessions_nalloc ||
                   1378:            sessions_first_unused < 0) {
                   1379:                fatal("%s: insane first_unused %d max %d nalloc %d",
                   1380:                    __func__, sessions_first_unused, options.max_sessions,
                   1381:                    sessions_nalloc);
                   1382:        }
                   1383:
                   1384:        s = &sessions[sessions_first_unused];
                   1385:        if (s->used) {
                   1386:                fatal("%s: session %d already used",
                   1387:                    __func__, sessions_first_unused);
                   1388:        }
                   1389:        sessions_first_unused = s->next_unused;
                   1390:        s->used = 1;
                   1391:        s->next_unused = -1;
                   1392:        debug("session_new: session %d", s->self);
                   1393:
                   1394:        return s;
1.1       markus   1395: }
                   1396:
1.94      itojun   1397: static void
1.1       markus   1398: session_dump(void)
                   1399: {
                   1400:        int i;
1.237     djm      1401:        for (i = 0; i < sessions_nalloc; i++) {
1.1       markus   1402:                Session *s = &sessions[i];
1.237     djm      1403:
                   1404:                debug("dump: used %d next_unused %d session %d %p "
                   1405:                    "channel %d pid %ld",
1.1       markus   1406:                    s->used,
1.237     djm      1407:                    s->next_unused,
1.1       markus   1408:                    s->self,
                   1409:                    s,
                   1410:                    s->chanid,
1.137     mpech    1411:                    (long)s->pid);
1.1       markus   1412:        }
                   1413: }
                   1414:
1.2       markus   1415: int
1.97      markus   1416: session_open(Authctxt *authctxt, int chanid)
1.2       markus   1417: {
                   1418:        Session *s = session_new();
                   1419:        debug("session_open: channel %d", chanid);
                   1420:        if (s == NULL) {
                   1421:                error("no more sessions");
                   1422:                return 0;
                   1423:        }
1.97      markus   1424:        s->authctxt = authctxt;
                   1425:        s->pw = authctxt->pw;
1.167     djm      1426:        if (s->pw == NULL || !authctxt->valid)
1.54      stevesk  1427:                fatal("no user for session %d", s->self);
1.2       markus   1428:        debug("session_open: session %d: link with channel %d", s->self, chanid);
                   1429:        s->chanid = chanid;
                   1430:        return 1;
                   1431: }
                   1432:
1.130     provos   1433: Session *
                   1434: session_by_tty(char *tty)
                   1435: {
                   1436:        int i;
1.237     djm      1437:        for (i = 0; i < sessions_nalloc; i++) {
1.130     provos   1438:                Session *s = &sessions[i];
                   1439:                if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
                   1440:                        debug("session_by_tty: session %d tty %s", i, tty);
                   1441:                        return s;
                   1442:                }
                   1443:        }
                   1444:        debug("session_by_tty: unknown tty %.100s", tty);
                   1445:        session_dump();
                   1446:        return NULL;
                   1447: }
                   1448:
1.94      itojun   1449: static Session *
1.2       markus   1450: session_by_channel(int id)
                   1451: {
                   1452:        int i;
1.237     djm      1453:        for (i = 0; i < sessions_nalloc; i++) {
1.2       markus   1454:                Session *s = &sessions[i];
                   1455:                if (s->used && s->chanid == id) {
1.237     djm      1456:                        debug("session_by_channel: session %d channel %d",
                   1457:                            i, id);
1.2       markus   1458:                        return s;
                   1459:                }
                   1460:        }
                   1461:        debug("session_by_channel: unknown channel %d", id);
                   1462:        session_dump();
                   1463:        return NULL;
                   1464: }
                   1465:
1.94      itojun   1466: static Session *
1.184     djm      1467: session_by_x11_channel(int id)
                   1468: {
                   1469:        int i, j;
                   1470:
1.237     djm      1471:        for (i = 0; i < sessions_nalloc; i++) {
1.184     djm      1472:                Session *s = &sessions[i];
                   1473:
                   1474:                if (s->x11_chanids == NULL || !s->used)
                   1475:                        continue;
                   1476:                for (j = 0; s->x11_chanids[j] != -1; j++) {
                   1477:                        if (s->x11_chanids[j] == id) {
                   1478:                                debug("session_by_x11_channel: session %d "
                   1479:                                    "channel %d", s->self, id);
                   1480:                                return s;
                   1481:                        }
                   1482:                }
                   1483:        }
                   1484:        debug("session_by_x11_channel: unknown channel %d", id);
                   1485:        session_dump();
                   1486:        return NULL;
                   1487: }
                   1488:
                   1489: static Session *
1.2       markus   1490: session_by_pid(pid_t pid)
                   1491: {
                   1492:        int i;
1.137     mpech    1493:        debug("session_by_pid: pid %ld", (long)pid);
1.237     djm      1494:        for (i = 0; i < sessions_nalloc; i++) {
1.2       markus   1495:                Session *s = &sessions[i];
                   1496:                if (s->used && s->pid == pid)
                   1497:                        return s;
                   1498:        }
1.137     mpech    1499:        error("session_by_pid: unknown pid %ld", (long)pid);
1.2       markus   1500:        session_dump();
                   1501:        return NULL;
                   1502: }
                   1503:
1.94      itojun   1504: static int
1.292     djm      1505: session_window_change_req(struct ssh *ssh, Session *s)
1.2       markus   1506: {
                   1507:        s->col = packet_get_int();
                   1508:        s->row = packet_get_int();
                   1509:        s->xpixel = packet_get_int();
                   1510:        s->ypixel = packet_get_int();
1.116     markus   1511:        packet_check_eom();
1.2       markus   1512:        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
                   1513:        return 1;
                   1514: }
                   1515:
1.94      itojun   1516: static int
1.292     djm      1517: session_pty_req(struct ssh *ssh, Session *s)
1.2       markus   1518: {
1.45      markus   1519:        u_int len;
1.72      stevesk  1520:        int n_bytes;
1.132     markus   1521:
1.294     djm      1522:        if (!auth_opts->permit_pty_flag || !options.permit_tty) {
                   1523:                debug("Allocating a pty not permitted for this connection.");
1.19      markus   1524:                return 0;
1.86      markus   1525:        }
                   1526:        if (s->ttyfd != -1) {
                   1527:                packet_disconnect("Protocol error: you already have a pty.");
1.3       markus   1528:                return 0;
1.86      markus   1529:        }
                   1530:
1.2       markus   1531:        s->term = packet_get_string(&len);
1.283     markus   1532:        s->col = packet_get_int();
                   1533:        s->row = packet_get_int();
1.2       markus   1534:        s->xpixel = packet_get_int();
                   1535:        s->ypixel = packet_get_int();
                   1536:
                   1537:        if (strcmp(s->term, "") == 0) {
1.265     djm      1538:                free(s->term);
1.2       markus   1539:                s->term = NULL;
                   1540:        }
1.86      markus   1541:
1.2       markus   1542:        /* Allocate a pty and open it. */
1.86      markus   1543:        debug("Allocating pty.");
1.237     djm      1544:        if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
                   1545:            sizeof(s->tty)))) {
1.265     djm      1546:                free(s->term);
1.2       markus   1547:                s->term = NULL;
                   1548:                s->ptyfd = -1;
                   1549:                s->ttyfd = -1;
                   1550:                error("session_pty_req: session %d alloc failed", s->self);
1.3       markus   1551:                return 0;
1.2       markus   1552:        }
                   1553:        debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1.86      markus   1554:
1.283     markus   1555:        n_bytes = packet_remaining();
1.86      markus   1556:        tty_parse_modes(s->ttyfd, &n_bytes);
                   1557:
1.130     provos   1558:        if (!use_privsep)
                   1559:                pty_setowner(s->pw, s->tty);
1.86      markus   1560:
                   1561:        /* Set window size from the packet. */
1.2       markus   1562:        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
                   1563:
1.116     markus   1564:        packet_check_eom();
1.9       markus   1565:        session_proctitle(s);
1.2       markus   1566:        return 1;
                   1567: }
                   1568:
1.94      itojun   1569: static int
1.292     djm      1570: session_subsystem_req(struct ssh *ssh, Session *s)
1.7       markus   1571: {
1.105     markus   1572:        struct stat st;
1.45      markus   1573:        u_int len;
1.7       markus   1574:        int success = 0;
1.267     djm      1575:        char *prog, *cmd;
1.182     djm      1576:        u_int i;
1.7       markus   1577:
1.267     djm      1578:        s->subsys = packet_get_string(&len);
1.116     markus   1579:        packet_check_eom();
1.267     djm      1580:        debug2("subsystem request for %.100s by user %s", s->subsys,
1.255     djm      1581:            s->pw->pw_name);
1.18      jakob    1582:
                   1583:        for (i = 0; i < options.num_subsystems; i++) {
1.267     djm      1584:                if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
1.205     djm      1585:                        prog = options.subsystem_command[i];
                   1586:                        cmd = options.subsystem_args[i];
1.249     dtucker  1587:                        if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
1.225     markus   1588:                                s->is_subsystem = SUBSYSTEM_INT_SFTP;
1.249     dtucker  1589:                                debug("subsystem: %s", prog);
1.225     markus   1590:                        } else {
1.249     dtucker  1591:                                if (stat(prog, &st) < 0)
                   1592:                                        debug("subsystem: cannot stat %s: %s",
                   1593:                                            prog, strerror(errno));
1.225     markus   1594:                                s->is_subsystem = SUBSYSTEM_EXT;
1.249     dtucker  1595:                                debug("subsystem: exec() %s", cmd);
1.105     markus   1596:                        }
1.292     djm      1597:                        success = do_exec(ssh, s, cmd) == 0;
1.122     markus   1598:                        break;
1.18      jakob    1599:                }
                   1600:        }
                   1601:
                   1602:        if (!success)
1.267     djm      1603:                logit("subsystem request for %.100s by user %s failed, "
                   1604:                    "subsystem not found", s->subsys, s->pw->pw_name);
1.7       markus   1605:
                   1606:        return success;
                   1607: }
                   1608:
1.94      itojun   1609: static int
1.292     djm      1610: session_x11_req(struct ssh *ssh, Session *s)
1.7       markus   1611: {
1.81      markus   1612:        int success;
1.7       markus   1613:
1.184     djm      1614:        if (s->auth_proto != NULL || s->auth_data != NULL) {
                   1615:                error("session_x11_req: session %d: "
1.190     stevesk  1616:                    "x11 forwarding already active", s->self);
1.184     djm      1617:                return 0;
                   1618:        }
1.7       markus   1619:        s->single_connection = packet_get_char();
                   1620:        s->auth_proto = packet_get_string(NULL);
                   1621:        s->auth_data = packet_get_string(NULL);
                   1622:        s->screen = packet_get_int();
1.116     markus   1623:        packet_check_eom();
1.7       markus   1624:
1.282     djm      1625:        if (xauth_valid_string(s->auth_proto) &&
                   1626:            xauth_valid_string(s->auth_data))
1.292     djm      1627:                success = session_setup_x11fwd(ssh, s);
1.282     djm      1628:        else {
                   1629:                success = 0;
                   1630:                error("Invalid X11 forwarding data");
                   1631:        }
1.81      markus   1632:        if (!success) {
1.265     djm      1633:                free(s->auth_proto);
                   1634:                free(s->auth_data);
1.84      markus   1635:                s->auth_proto = NULL;
                   1636:                s->auth_data = NULL;
1.7       markus   1637:        }
1.81      markus   1638:        return success;
1.7       markus   1639: }
                   1640:
1.94      itojun   1641: static int
1.292     djm      1642: session_shell_req(struct ssh *ssh, Session *s)
1.19      markus   1643: {
1.116     markus   1644:        packet_check_eom();
1.292     djm      1645:        return do_exec(ssh, s, NULL) == 0;
1.19      markus   1646: }
                   1647:
1.94      itojun   1648: static int
1.292     djm      1649: session_exec_req(struct ssh *ssh, Session *s)
1.19      markus   1650: {
1.237     djm      1651:        u_int len, success;
                   1652:
1.19      markus   1653:        char *command = packet_get_string(&len);
1.116     markus   1654:        packet_check_eom();
1.292     djm      1655:        success = do_exec(ssh, s, command) == 0;
1.265     djm      1656:        free(command);
1.237     djm      1657:        return success;
1.19      markus   1658: }
                   1659:
1.94      itojun   1660: static int
1.292     djm      1661: session_break_req(struct ssh *ssh, Session *s)
1.157     markus   1662: {
                   1663:
1.175     deraadt  1664:        packet_get_int();       /* ignored */
1.157     markus   1665:        packet_check_eom();
                   1666:
1.259     djm      1667:        if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
1.157     markus   1668:                return 0;
                   1669:        return 1;
                   1670: }
                   1671:
                   1672: static int
1.292     djm      1673: session_env_req(struct ssh *ssh, Session *s)
1.173     djm      1674: {
                   1675:        char *name, *val;
                   1676:        u_int name_len, val_len, i;
                   1677:
1.271     djm      1678:        name = packet_get_cstring(&name_len);
                   1679:        val = packet_get_cstring(&val_len);
1.173     djm      1680:        packet_check_eom();
                   1681:
                   1682:        /* Don't set too many environment variables */
                   1683:        if (s->num_env > 128) {
                   1684:                debug2("Ignoring env request %s: too many env vars", name);
                   1685:                goto fail;
                   1686:        }
                   1687:
                   1688:        for (i = 0; i < options.num_accept_env; i++) {
                   1689:                if (match_pattern(name, options.accept_env[i])) {
                   1690:                        debug2("Setting env %d: %s=%s", s->num_env, name, val);
1.288     deraadt  1691:                        s->env = xrecallocarray(s->env, s->num_env,
                   1692:                            s->num_env + 1, sizeof(*s->env));
1.173     djm      1693:                        s->env[s->num_env].name = name;
                   1694:                        s->env[s->num_env].val = val;
                   1695:                        s->num_env++;
                   1696:                        return (1);
                   1697:                }
                   1698:        }
                   1699:        debug2("Ignoring env request %s: disallowed name", name);
                   1700:
                   1701:  fail:
1.265     djm      1702:        free(name);
                   1703:        free(val);
1.173     djm      1704:        return (0);
                   1705: }
                   1706:
                   1707: static int
1.292     djm      1708: session_auth_agent_req(struct ssh *ssh, Session *s)
1.43      markus   1709: {
                   1710:        static int called = 0;
1.294     djm      1711:
1.116     markus   1712:        packet_check_eom();
1.294     djm      1713:        if (!auth_opts->permit_agent_forwarding_flag ||
                   1714:            !options.allow_agent_forwarding) {
                   1715:                debug("%s: agent forwarding disabled", __func__);
1.44      markus   1716:                return 0;
                   1717:        }
1.43      markus   1718:        if (called) {
                   1719:                return 0;
                   1720:        } else {
                   1721:                called = 1;
1.292     djm      1722:                return auth_input_request_forwarding(ssh, s->pw);
1.43      markus   1723:        }
                   1724: }
                   1725:
1.123     markus   1726: int
1.292     djm      1727: session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype)
1.2       markus   1728: {
                   1729:        int success = 0;
                   1730:        Session *s;
                   1731:
1.123     markus   1732:        if ((s = session_by_channel(c->self)) == NULL) {
1.292     djm      1733:                logit("%s: no session %d req %.100s", __func__, c->self, rtype);
1.123     markus   1734:                return 0;
                   1735:        }
1.292     djm      1736:        debug("%s: session %d req %s", __func__, s->self, rtype);
1.2       markus   1737:
                   1738:        /*
1.64      markus   1739:         * a session is in LARVAL state until a shell, a command
                   1740:         * or a subsystem is executed
1.2       markus   1741:         */
                   1742:        if (c->type == SSH_CHANNEL_LARVAL) {
                   1743:                if (strcmp(rtype, "shell") == 0) {
1.292     djm      1744:                        success = session_shell_req(ssh, s);
1.2       markus   1745:                } else if (strcmp(rtype, "exec") == 0) {
1.292     djm      1746:                        success = session_exec_req(ssh, s);
1.2       markus   1747:                } else if (strcmp(rtype, "pty-req") == 0) {
1.292     djm      1748:                        success = session_pty_req(ssh, s);
1.7       markus   1749:                } else if (strcmp(rtype, "x11-req") == 0) {
1.292     djm      1750:                        success = session_x11_req(ssh, s);
1.43      markus   1751:                } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1.292     djm      1752:                        success = session_auth_agent_req(ssh, s);
1.7       markus   1753:                } else if (strcmp(rtype, "subsystem") == 0) {
1.292     djm      1754:                        success = session_subsystem_req(ssh, s);
1.173     djm      1755:                } else if (strcmp(rtype, "env") == 0) {
1.292     djm      1756:                        success = session_env_req(ssh, s);
1.2       markus   1757:                }
                   1758:        }
                   1759:        if (strcmp(rtype, "window-change") == 0) {
1.292     djm      1760:                success = session_window_change_req(ssh, s);
1.177     djm      1761:        } else if (strcmp(rtype, "break") == 0) {
1.292     djm      1762:                success = session_break_req(ssh, s);
1.2       markus   1763:        }
1.177     djm      1764:
1.123     markus   1765:        return success;
1.2       markus   1766: }
                   1767:
                   1768: void
1.292     djm      1769: session_set_fds(struct ssh *ssh, Session *s,
                   1770:     int fdin, int fdout, int fderr, int ignore_fderr, int is_tty)
1.2       markus   1771: {
                   1772:        /*
                   1773:         * now that have a child and a pipe to the child,
                   1774:         * we can activate our channel and register the fd's
                   1775:         */
                   1776:        if (s->chanid == -1)
                   1777:                fatal("no channel for session %d", s->self);
1.292     djm      1778:        channel_set_fds(ssh, s->chanid,
1.2       markus   1779:            fdout, fdin, fderr,
1.256     djm      1780:            ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1.241     dtucker  1781:            1, is_tty, CHAN_SES_WINDOW_DEFAULT);
1.2       markus   1782: }
                   1783:
1.85      markus   1784: /*
                   1785:  * Function to perform pty cleanup. Also called if we get aborted abnormally
                   1786:  * (e.g., due to a dropped connection).
                   1787:  */
1.130     provos   1788: void
1.165     markus   1789: session_pty_cleanup2(Session *s)
1.1       markus   1790: {
1.85      markus   1791:        if (s == NULL) {
                   1792:                error("session_pty_cleanup: no session");
                   1793:                return;
                   1794:        }
                   1795:        if (s->ttyfd == -1)
1.1       markus   1796:                return;
                   1797:
1.54      stevesk  1798:        debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1       markus   1799:
                   1800:        /* Record that the user has logged out. */
1.85      markus   1801:        if (s->pid != 0)
                   1802:                record_logout(s->pid, s->tty);
1.1       markus   1803:
                   1804:        /* Release the pseudo-tty. */
1.130     provos   1805:        if (getuid() == 0)
                   1806:                pty_release(s->tty);
1.1       markus   1807:
                   1808:        /*
                   1809:         * Close the server side of the socket pairs.  We must do this after
                   1810:         * the pty cleanup, so that another process doesn't get this pty
                   1811:         * while we're still cleaning up.
                   1812:         */
1.237     djm      1813:        if (s->ptymaster != -1 && close(s->ptymaster) < 0)
                   1814:                error("close(s->ptymaster/%d): %s",
                   1815:                    s->ptymaster, strerror(errno));
1.108     markus   1816:
                   1817:        /* unlink pty from session */
                   1818:        s->ttyfd = -1;
1.2       markus   1819: }
                   1820:
1.130     provos   1821: void
1.165     markus   1822: session_pty_cleanup(Session *s)
1.130     provos   1823: {
1.165     markus   1824:        PRIVSEP(session_pty_cleanup2(s));
1.130     provos   1825: }
                   1826:
1.147     markus   1827: static char *
                   1828: sig2name(int sig)
                   1829: {
                   1830: #define SSH_SIG(x) if (sig == SIG ## x) return #x
                   1831:        SSH_SIG(ABRT);
                   1832:        SSH_SIG(ALRM);
                   1833:        SSH_SIG(FPE);
                   1834:        SSH_SIG(HUP);
                   1835:        SSH_SIG(ILL);
                   1836:        SSH_SIG(INT);
                   1837:        SSH_SIG(KILL);
                   1838:        SSH_SIG(PIPE);
                   1839:        SSH_SIG(QUIT);
                   1840:        SSH_SIG(SEGV);
                   1841:        SSH_SIG(TERM);
                   1842:        SSH_SIG(USR1);
                   1843:        SSH_SIG(USR2);
                   1844: #undef SSH_SIG
                   1845:        return "SIG@openssh.com";
                   1846: }
                   1847:
1.94      itojun   1848: static void
1.292     djm      1849: session_close_x11(struct ssh *ssh, int id)
1.184     djm      1850: {
                   1851:        Channel *c;
                   1852:
1.292     djm      1853:        if ((c = channel_by_id(ssh, id)) == NULL) {
                   1854:                debug("%s: x11 channel %d missing", __func__, id);
1.184     djm      1855:        } else {
                   1856:                /* Detach X11 listener */
1.292     djm      1857:                debug("%s: detach x11 channel %d", __func__, id);
                   1858:                channel_cancel_cleanup(ssh, id);
1.184     djm      1859:                if (c->ostate != CHAN_OUTPUT_CLOSED)
1.292     djm      1860:                        chan_mark_dead(ssh, c);
1.184     djm      1861:        }
                   1862: }
                   1863:
                   1864: static void
1.292     djm      1865: session_close_single_x11(struct ssh *ssh, int id, void *arg)
1.184     djm      1866: {
                   1867:        Session *s;
                   1868:        u_int i;
                   1869:
1.292     djm      1870:        debug3("%s: channel %d", __func__, id);
                   1871:        channel_cancel_cleanup(ssh, id);
1.221     stevesk  1872:        if ((s = session_by_x11_channel(id)) == NULL)
1.292     djm      1873:                fatal("%s: no x11 channel %d", __func__, id);
1.184     djm      1874:        for (i = 0; s->x11_chanids[i] != -1; i++) {
1.292     djm      1875:                debug("%s: session %d: closing channel %d",
                   1876:                    __func__, s->self, s->x11_chanids[i]);
1.184     djm      1877:                /*
                   1878:                 * The channel "id" is already closing, but make sure we
                   1879:                 * close all of its siblings.
                   1880:                 */
                   1881:                if (s->x11_chanids[i] != id)
1.292     djm      1882:                        session_close_x11(ssh, s->x11_chanids[i]);
1.184     djm      1883:        }
1.265     djm      1884:        free(s->x11_chanids);
1.184     djm      1885:        s->x11_chanids = NULL;
1.265     djm      1886:        free(s->display);
                   1887:        s->display = NULL;
                   1888:        free(s->auth_proto);
                   1889:        s->auth_proto = NULL;
                   1890:        free(s->auth_data);
                   1891:        s->auth_data = NULL;
                   1892:        free(s->auth_display);
                   1893:        s->auth_display = NULL;
1.184     djm      1894: }
                   1895:
                   1896: static void
1.292     djm      1897: session_exit_message(struct ssh *ssh, Session *s, int status)
1.2       markus   1898: {
                   1899:        Channel *c;
1.124     markus   1900:
1.292     djm      1901:        if ((c = channel_lookup(ssh, s->chanid)) == NULL)
                   1902:                fatal("%s: session %d: no channel %d",
                   1903:                    __func__, s->self, s->chanid);
                   1904:        debug("%s: session %d channel %d pid %ld",
                   1905:            __func__, s->self, s->chanid, (long)s->pid);
1.2       markus   1906:
                   1907:        if (WIFEXITED(status)) {
1.292     djm      1908:                channel_request_start(ssh, s->chanid, "exit-status", 0);
1.2       markus   1909:                packet_put_int(WEXITSTATUS(status));
                   1910:                packet_send();
                   1911:        } else if (WIFSIGNALED(status)) {
1.292     djm      1912:                channel_request_start(ssh, s->chanid, "exit-signal", 0);
1.147     markus   1913:                packet_put_cstring(sig2name(WTERMSIG(status)));
1.229     markus   1914:                packet_put_char(WCOREDUMP(status)? 1 : 0);
1.2       markus   1915:                packet_put_cstring("");
                   1916:                packet_put_cstring("");
                   1917:                packet_send();
                   1918:        } else {
                   1919:                /* Some weird exit cause.  Just exit. */
                   1920:                packet_disconnect("wait returned status %04x.", status);
                   1921:        }
                   1922:
                   1923:        /* disconnect channel */
1.292     djm      1924:        debug("%s: release channel %d", __func__, s->chanid);
1.187     djm      1925:
                   1926:        /*
                   1927:         * Adjust cleanup callback attachment to send close messages when
1.199     deraadt  1928:         * the channel gets EOF. The session will be then be closed
1.187     djm      1929:         * by session_close_by_channel when the childs close their fds.
                   1930:         */
1.292     djm      1931:        channel_register_cleanup(ssh, c->self, session_close_by_channel, 1);
1.187     djm      1932:
1.5       markus   1933:        /*
                   1934:         * emulate a write failure with 'chan_write_failed', nobody will be
                   1935:         * interested in data we write.
                   1936:         * Note that we must not call 'chan_read_failed', since there could
                   1937:         * be some more data waiting in the pipe.
                   1938:         */
1.8       markus   1939:        if (c->ostate != CHAN_OUTPUT_CLOSED)
1.292     djm      1940:                chan_write_failed(ssh, c);
1.2       markus   1941: }
                   1942:
1.130     provos   1943: void
1.292     djm      1944: session_close(struct ssh *ssh, Session *s)
1.2       markus   1945: {
1.182     djm      1946:        u_int i;
1.173     djm      1947:
1.280     djm      1948:        verbose("Close session: user %s from %.200s port %d id %d",
                   1949:            s->pw->pw_name,
1.281     djm      1950:            ssh_remote_ipaddr(ssh),
                   1951:            ssh_remote_port(ssh),
1.280     djm      1952:            s->self);
                   1953:
1.165     markus   1954:        if (s->ttyfd != -1)
1.85      markus   1955:                session_pty_cleanup(s);
1.265     djm      1956:        free(s->term);
                   1957:        free(s->display);
                   1958:        free(s->x11_chanids);
                   1959:        free(s->auth_display);
                   1960:        free(s->auth_data);
                   1961:        free(s->auth_proto);
1.267     djm      1962:        free(s->subsys);
1.219     djm      1963:        if (s->env != NULL) {
                   1964:                for (i = 0; i < s->num_env; i++) {
1.265     djm      1965:                        free(s->env[i].name);
                   1966:                        free(s->env[i].val);
1.219     djm      1967:                }
1.265     djm      1968:                free(s->env);
1.173     djm      1969:        }
1.9       markus   1970:        session_proctitle(s);
1.237     djm      1971:        session_unused(s->self);
1.2       markus   1972: }
                   1973:
                   1974: void
1.292     djm      1975: session_close_by_pid(struct ssh *ssh, pid_t pid, int status)
1.2       markus   1976: {
                   1977:        Session *s = session_by_pid(pid);
                   1978:        if (s == NULL) {
1.292     djm      1979:                debug("%s: no session for pid %ld", __func__, (long)pid);
1.2       markus   1980:                return;
                   1981:        }
                   1982:        if (s->chanid != -1)
1.292     djm      1983:                session_exit_message(ssh, s, status);
1.187     djm      1984:        if (s->ttyfd != -1)
                   1985:                session_pty_cleanup(s);
1.197     djm      1986:        s->pid = 0;
1.98      markus   1987: }
                   1988:
1.2       markus   1989: /*
                   1990:  * this is called when a channel dies before
                   1991:  * the session 'child' itself dies
                   1992:  */
                   1993: void
1.292     djm      1994: session_close_by_channel(struct ssh *ssh, int id, void *arg)
1.2       markus   1995: {
                   1996:        Session *s = session_by_channel(id);
1.187     djm      1997:        u_int i;
1.184     djm      1998:
1.2       markus   1999:        if (s == NULL) {
1.292     djm      2000:                debug("%s: no session for id %d", __func__, id);
1.2       markus   2001:                return;
                   2002:        }
1.292     djm      2003:        debug("%s: channel %d child %ld", __func__, id, (long)s->pid);
1.107     markus   2004:        if (s->pid != 0) {
1.292     djm      2005:                debug("%s: channel %d: has child", __func__, id);
1.108     markus   2006:                /*
                   2007:                 * delay detach of session, but release pty, since
                   2008:                 * the fd's to the child are already closed
                   2009:                 */
1.165     markus   2010:                if (s->ttyfd != -1)
1.108     markus   2011:                        session_pty_cleanup(s);
1.107     markus   2012:                return;
                   2013:        }
                   2014:        /* detach by removing callback */
1.292     djm      2015:        channel_cancel_cleanup(ssh, s->chanid);
1.187     djm      2016:
                   2017:        /* Close any X11 listeners associated with this session */
                   2018:        if (s->x11_chanids != NULL) {
                   2019:                for (i = 0; s->x11_chanids[i] != -1; i++) {
1.292     djm      2020:                        session_close_x11(ssh, s->x11_chanids[i]);
1.187     djm      2021:                        s->x11_chanids[i] = -1;
                   2022:                }
                   2023:        }
                   2024:
1.2       markus   2025:        s->chanid = -1;
1.292     djm      2026:        session_close(ssh, s);
1.106     markus   2027: }
                   2028:
                   2029: void
1.292     djm      2030: session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
1.106     markus   2031: {
                   2032:        int i;
1.237     djm      2033:        for (i = 0; i < sessions_nalloc; i++) {
1.106     markus   2034:                Session *s = &sessions[i];
1.130     provos   2035:                if (s->used) {
                   2036:                        if (closefunc != NULL)
                   2037:                                closefunc(s);
                   2038:                        else
1.292     djm      2039:                                session_close(ssh, s);
1.130     provos   2040:                }
1.2       markus   2041:        }
1.9       markus   2042: }
                   2043:
1.94      itojun   2044: static char *
1.9       markus   2045: session_tty_list(void)
                   2046: {
                   2047:        static char buf[1024];
                   2048:        int i;
                   2049:        buf[0] = '\0';
1.237     djm      2050:        for (i = 0; i < sessions_nalloc; i++) {
1.9       markus   2051:                Session *s = &sessions[i];
                   2052:                if (s->used && s->ttyfd != -1) {
                   2053:                        if (buf[0] != '\0')
                   2054:                                strlcat(buf, ",", sizeof buf);
                   2055:                        strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
                   2056:                }
                   2057:        }
                   2058:        if (buf[0] == '\0')
                   2059:                strlcpy(buf, "notty", sizeof buf);
                   2060:        return buf;
                   2061: }
                   2062:
                   2063: void
                   2064: session_proctitle(Session *s)
                   2065: {
                   2066:        if (s->pw == NULL)
                   2067:                error("no user for session %d", s->self);
                   2068:        else
                   2069:                setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.81      markus   2070: }
                   2071:
                   2072: int
1.292     djm      2073: session_setup_x11fwd(struct ssh *ssh, Session *s)
1.81      markus   2074: {
                   2075:        struct stat st;
1.109     stevesk  2076:        char display[512], auth_display[512];
1.272     djm      2077:        char hostname[NI_MAXHOST];
1.184     djm      2078:        u_int i;
1.81      markus   2079:
1.294     djm      2080:        if (!auth_opts->permit_x11_forwarding_flag) {
                   2081:                packet_send_debug("X11 forwarding disabled by key options.");
1.81      markus   2082:                return 0;
                   2083:        }
                   2084:        if (!options.x11_forwarding) {
                   2085:                debug("X11 forwarding disabled in server configuration file.");
                   2086:                return 0;
                   2087:        }
1.275     djm      2088:        if (options.xauth_location == NULL ||
1.81      markus   2089:            (stat(options.xauth_location, &st) == -1)) {
1.294     djm      2090:                packet_send_debug("No xauth program; cannot forward X11.");
1.81      markus   2091:                return 0;
                   2092:        }
1.87      markus   2093:        if (s->display != NULL) {
1.81      markus   2094:                debug("X11 display already set.");
                   2095:                return 0;
                   2096:        }
1.292     djm      2097:        if (x11_create_display_inet(ssh, options.x11_display_offset,
1.140     deraadt  2098:            options.x11_use_localhost, s->single_connection,
1.184     djm      2099:            &s->display_number, &s->x11_chanids) == -1) {
1.87      markus   2100:                debug("x11_create_display_inet failed.");
1.81      markus   2101:                return 0;
1.184     djm      2102:        }
                   2103:        for (i = 0; s->x11_chanids[i] != -1; i++) {
1.292     djm      2104:                channel_register_cleanup(ssh, s->x11_chanids[i],
1.187     djm      2105:                    session_close_single_x11, 0);
1.81      markus   2106:        }
1.109     stevesk  2107:
                   2108:        /* Set up a suitable value for the DISPLAY variable. */
                   2109:        if (gethostname(hostname, sizeof(hostname)) < 0)
                   2110:                fatal("gethostname: %.100s", strerror(errno));
                   2111:        /*
                   2112:         * auth_display must be used as the displayname when the
                   2113:         * authorization entry is added with xauth(1).  This will be
                   2114:         * different than the DISPLAY string for localhost displays.
                   2115:         */
1.119     stevesk  2116:        if (options.x11_use_localhost) {
1.140     deraadt  2117:                snprintf(display, sizeof display, "localhost:%u.%u",
1.109     stevesk  2118:                    s->display_number, s->screen);
1.140     deraadt  2119:                snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
1.118     stevesk  2120:                    s->display_number, s->screen);
1.109     stevesk  2121:                s->display = xstrdup(display);
1.118     stevesk  2122:                s->auth_display = xstrdup(auth_display);
1.109     stevesk  2123:        } else {
1.140     deraadt  2124:                snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
1.109     stevesk  2125:                    s->display_number, s->screen);
                   2126:                s->display = xstrdup(display);
1.118     stevesk  2127:                s->auth_display = xstrdup(display);
1.109     stevesk  2128:        }
                   2129:
1.81      markus   2130:        return 1;
1.2       markus   2131: }
                   2132:
1.94      itojun   2133: static void
1.292     djm      2134: do_authenticated2(struct ssh *ssh, Authctxt *authctxt)
1.2       markus   2135: {
1.292     djm      2136:        server_loop2(ssh, authctxt);
1.165     markus   2137: }
                   2138:
                   2139: void
1.292     djm      2140: do_cleanup(struct ssh *ssh, Authctxt *authctxt)
1.165     markus   2141: {
                   2142:        static int called = 0;
                   2143:
                   2144:        debug("do_cleanup");
                   2145:
                   2146:        /* no cleanup if we're in the child for login shell */
                   2147:        if (is_child)
                   2148:                return;
                   2149:
                   2150:        /* avoid double cleanup */
                   2151:        if (called)
                   2152:                return;
                   2153:        called = 1;
                   2154:
1.218     markus   2155:        if (authctxt == NULL || !authctxt->authenticated)
1.165     markus   2156:                return;
                   2157: #ifdef KRB5
                   2158:        if (options.kerberos_ticket_cleanup &&
                   2159:            authctxt->krb5_ctx)
                   2160:                krb5_cleanup_proc(authctxt);
1.161     markus   2161: #endif
1.165     markus   2162:
                   2163: #ifdef GSSAPI
1.283     markus   2164:        if (options.gss_cleanup_creds)
1.165     markus   2165:                ssh_gssapi_cleanup_creds();
                   2166: #endif
                   2167:
                   2168:        /* remove agent socket */
                   2169:        auth_sock_cleanup_proc(authctxt->pw);
1.290     djm      2170:
                   2171:        /* remove userauth info */
                   2172:        if (auth_info_file != NULL) {
                   2173:                temporarily_use_uid(authctxt->pw);
                   2174:                unlink(auth_info_file);
                   2175:                restore_uid();
                   2176:                free(auth_info_file);
                   2177:                auth_info_file = NULL;
                   2178:        }
1.165     markus   2179:
                   2180:        /*
                   2181:         * Cleanup ptys/utmp only if privsep is disabled,
                   2182:         * or if running in monitor.
                   2183:         */
                   2184:        if (!use_privsep || mm_is_monitor())
1.292     djm      2185:                session_destroy_all(ssh, session_pty_cleanup2);
1.1       markus   2186: }
1.281     djm      2187:
                   2188: /* Return a name for the remote host that fits inside utmp_size */
                   2189:
                   2190: const char *
                   2191: session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
                   2192: {
                   2193:        const char *remote = "";
                   2194:
                   2195:        if (utmp_size > 0)
                   2196:                remote = auth_get_canonical_hostname(ssh, use_dns);
                   2197:        if (utmp_size == 0 || strlen(remote) > utmp_size)
                   2198:                remote = ssh_remote_ipaddr(ssh);
                   2199:        return remote;
                   2200: }
                   2201: