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

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