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

1.1       markus      1: /*
                      2:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      3:  *                    All rights reserved
1.37      deraadt     4:  *
                      5:  * As far as I am concerned, the code I have written for this software
                      6:  * can be used freely for any purpose.  Any derived versions of this
                      7:  * software must be clearly marked as such, and if the derived work is
                      8:  * incompatible with the protocol description in the RFC file, it must be
                      9:  * called by a name other than "ssh" or "Secure Shell".
                     10:  *
1.2       markus     11:  * SSH2 support by Markus Friedl.
1.58      deraadt    12:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.37      deraadt    13:  *
                     14:  * Redistribution and use in source and binary forms, with or without
                     15:  * modification, are permitted provided that the following conditions
                     16:  * are met:
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     24:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     25:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     26:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     27:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     28:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     29:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     30:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     31:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     32:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.2       markus     33:  */
1.1       markus     34:
                     35: #include "includes.h"
1.62    ! markus     36: RCSID("$OpenBSD: session.c,v 1.61 2001/03/16 19:06:30 markus Exp $");
1.1       markus     37:
1.51      markus     38: #include "ssh.h"
                     39: #include "ssh1.h"
                     40: #include "ssh2.h"
1.1       markus     41: #include "xmalloc.h"
1.59      djm        42: #include "sshpty.h"
1.1       markus     43: #include "packet.h"
                     44: #include "buffer.h"
                     45: #include "mpaux.h"
                     46: #include "uidswap.h"
                     47: #include "compat.h"
                     48: #include "channels.h"
                     49: #include "nchan.h"
1.2       markus     50: #include "bufaux.h"
                     51: #include "auth.h"
1.19      markus     52: #include "auth-options.h"
1.50      markus     53: #include "pathnames.h"
1.51      markus     54: #include "log.h"
                     55: #include "servconf.h"
1.59      djm        56: #include "sshlogin.h"
1.51      markus     57: #include "serverloop.h"
                     58: #include "canohost.h"
1.55      itojun     59: #include "session.h"
1.2       markus     60:
1.1       markus     61: /* types */
                     62:
                     63: #define TTYSZ 64
                     64: typedef struct Session Session;
                     65: struct Session {
                     66:        int     used;
                     67:        int     self;
1.7       markus     68:        int     extended;
1.1       markus     69:        struct  passwd *pw;
                     70:        pid_t   pid;
                     71:        /* tty */
                     72:        char    *term;
                     73:        int     ptyfd, ttyfd, ptymaster;
                     74:        int     row, col, xpixel, ypixel;
                     75:        char    tty[TTYSZ];
                     76:        /* X11 */
                     77:        char    *display;
                     78:        int     screen;
                     79:        char    *auth_proto;
                     80:        char    *auth_data;
1.7       markus     81:        int     single_connection;
1.1       markus     82:        /* proto 2 */
                     83:        int     chanid;
                     84: };
                     85:
                     86: /* func */
                     87:
                     88: Session *session_new(void);
                     89: void   session_set_fds(Session *s, int fdin, int fdout, int fderr);
                     90: void   session_pty_cleanup(Session *s);
1.9       markus     91: void   session_proctitle(Session *s);
1.1       markus     92: void   do_exec_pty(Session *s, const char *command, struct passwd * pw);
                     93: void   do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
1.41      markus     94: void   do_login(Session *s, const char *command);
1.60      markus     95: void   do_child(Session *s, const char *command);
1.1       markus     96:
                     97: /* import */
                     98: extern ServerOptions options;
                     99: extern char *__progname;
                    100: extern int log_stderr;
                    101: extern int debug_flag;
1.45      markus    102: extern u_int utmp_len;
1.1       markus    103:
1.21      markus    104: extern int startup_pipe;
                    105:
1.1       markus    106: /* Local Xauthority file. */
                    107: static char *xauthfile;
                    108:
1.34      markus    109: /* original command from peer. */
1.53      stevesk   110: char *original_command = NULL;
1.34      markus    111:
1.1       markus    112: /* data */
                    113: #define MAX_SESSIONS 10
                    114: Session        sessions[MAX_SESSIONS];
                    115:
1.28      millert   116: #ifdef HAVE_LOGIN_CAP
                    117: static login_cap_t *lc;
                    118: #endif
                    119:
1.1       markus    120: /*
                    121:  * Remove local Xauthority file.
                    122:  */
                    123: void
                    124: xauthfile_cleanup_proc(void *ignore)
                    125: {
                    126:        debug("xauthfile_cleanup_proc called");
                    127:
                    128:        if (xauthfile != NULL) {
                    129:                char *p;
                    130:                unlink(xauthfile);
                    131:                p = strrchr(xauthfile, '/');
                    132:                if (p != NULL) {
                    133:                        *p = '\0';
                    134:                        rmdir(xauthfile);
                    135:                }
                    136:                xfree(xauthfile);
                    137:                xauthfile = NULL;
                    138:        }
                    139: }
                    140:
                    141: /*
                    142:  * Function to perform cleanup if we get aborted abnormally (e.g., due to a
                    143:  * dropped connection).
                    144:  */
1.4       markus    145: void
1.1       markus    146: pty_cleanup_proc(void *session)
                    147: {
                    148:        Session *s=session;
                    149:        if (s == NULL)
                    150:                fatal("pty_cleanup_proc: no session");
                    151:        debug("pty_cleanup_proc: %s", s->tty);
                    152:
                    153:        if (s->pid != 0) {
                    154:                /* Record that the user has logged out. */
                    155:                record_logout(s->pid, s->tty);
                    156:        }
                    157:
                    158:        /* Release the pseudo-tty. */
                    159:        pty_release(s->tty);
                    160: }
                    161:
                    162: /*
                    163:  * Prepares for an interactive session.  This is called after the user has
                    164:  * been successfully authenticated.  During this message exchange, pseudo
                    165:  * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
                    166:  * are requested, etc.
                    167:  */
1.4       markus    168: void
1.1       markus    169: do_authenticated(struct passwd * pw)
                    170: {
                    171:        Session *s;
1.33      markus    172:        int type, fd;
1.1       markus    173:        int compression_level = 0, enable_compression_after_reply = 0;
                    174:        int have_pty = 0;
                    175:        char *command;
                    176:        int n_bytes;
                    177:        int plen;
1.45      markus    178:        u_int proto_len, data_len, dlen;
1.57      markus    179:        int screen_flag;
1.1       markus    180:
                    181:        /*
                    182:         * Cancel the alarm we set to limit the time taken for
                    183:         * authentication.
                    184:         */
                    185:        alarm(0);
1.23      deraadt   186:        if (startup_pipe != -1) {
                    187:                close(startup_pipe);
                    188:                startup_pipe = -1;
                    189:        }
1.1       markus    190:
1.61      markus    191:        s = session_new();
                    192:        s->pw = pw;
                    193:
1.39      markus    194:        if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
1.1       markus    195:                channel_permit_all_opens();
                    196:
1.28      millert   197: #ifdef HAVE_LOGIN_CAP
                    198:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    199:                error("unable to get login class");
                    200:                return;
                    201:        }
                    202: #endif
                    203:
1.1       markus    204:        /*
                    205:         * We stay in this loop until the client requests to execute a shell
                    206:         * or a command.
                    207:         */
                    208:        for (;;) {
                    209:                int success = 0;
                    210:
                    211:                /* Get a packet from the client. */
                    212:                type = packet_read(&plen);
                    213:
                    214:                /* Process the packet. */
                    215:                switch (type) {
                    216:                case SSH_CMSG_REQUEST_COMPRESSION:
                    217:                        packet_integrity_check(plen, 4, type);
                    218:                        compression_level = packet_get_int();
                    219:                        if (compression_level < 1 || compression_level > 9) {
                    220:                                packet_send_debug("Received illegal compression level %d.",
                    221:                                     compression_level);
                    222:                                break;
                    223:                        }
                    224:                        /* Enable compression after we have responded with SUCCESS. */
                    225:                        enable_compression_after_reply = 1;
                    226:                        success = 1;
                    227:                        break;
                    228:
                    229:                case SSH_CMSG_REQUEST_PTY:
                    230:                        if (no_pty_flag) {
                    231:                                debug("Allocating a pty not permitted for this authentication.");
                    232:                                break;
                    233:                        }
                    234:                        if (have_pty)
                    235:                                packet_disconnect("Protocol error: you already have a pty.");
                    236:
                    237:                        debug("Allocating pty.");
                    238:
                    239:                        /* Allocate a pty and open it. */
                    240:                        if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
                    241:                            sizeof(s->tty))) {
                    242:                                error("Failed to allocate pty.");
                    243:                                break;
                    244:                        }
                    245:                        fatal_add_cleanup(pty_cleanup_proc, (void *)s);
                    246:                        pty_setowner(pw, s->tty);
                    247:
                    248:                        /* Get TERM from the packet.  Note that the value may be of arbitrary length. */
                    249:                        s->term = packet_get_string(&dlen);
                    250:                        packet_integrity_check(dlen, strlen(s->term), type);
                    251:                        /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
                    252:                        /* Remaining bytes */
                    253:                        n_bytes = plen - (4 + dlen + 4 * 4);
                    254:
                    255:                        if (strcmp(s->term, "") == 0) {
                    256:                                xfree(s->term);
                    257:                                s->term = NULL;
                    258:                        }
                    259:                        /* Get window size from the packet. */
                    260:                        s->row = packet_get_int();
                    261:                        s->col = packet_get_int();
                    262:                        s->xpixel = packet_get_int();
                    263:                        s->ypixel = packet_get_int();
                    264:                        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
                    265:
                    266:                        /* Get tty modes from the packet. */
                    267:                        tty_parse_modes(s->ttyfd, &n_bytes);
                    268:                        packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
1.10      markus    269:
                    270:                        session_proctitle(s);
1.1       markus    271:
                    272:                        /* Indicate that we now have a pty. */
                    273:                        success = 1;
                    274:                        have_pty = 1;
                    275:                        break;
                    276:
                    277:                case SSH_CMSG_X11_REQUEST_FORWARDING:
                    278:                        if (!options.x11_forwarding) {
                    279:                                packet_send_debug("X11 forwarding disabled in server configuration file.");
                    280:                                break;
                    281:                        }
1.16      markus    282:                        if (!options.xauth_location) {
                    283:                                packet_send_debug("No xauth program; cannot forward with spoofing.");
                    284:                                break;
                    285:                        }
1.1       markus    286:                        if (no_x11_forwarding_flag) {
                    287:                                packet_send_debug("X11 forwarding not permitted for this authentication.");
                    288:                                break;
                    289:                        }
                    290:                        debug("Received request for X11 forwarding with auth spoofing.");
                    291:                        if (s->display != NULL)
                    292:                                packet_disconnect("Protocol error: X11 display already set.");
                    293:
                    294:                        s->auth_proto = packet_get_string(&proto_len);
                    295:                        s->auth_data = packet_get_string(&data_len);
                    296:
1.57      markus    297:                        screen_flag = packet_get_protocol_flags() &
                    298:                            SSH_PROTOFLAG_SCREEN_NUMBER;
                    299:                        debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
                    300:
                    301:                        if (packet_remaining() == 4) {
                    302:                                if (!screen_flag)
                    303:                                        debug2("Buggy client: "
                    304:                                            "X11 screen flag missing");
1.56      markus    305:                                packet_integrity_check(plen,
                    306:                                    4 + proto_len + 4 + data_len + 4, type);
1.1       markus    307:                                s->screen = packet_get_int();
1.56      markus    308:                        } else {
                    309:                                packet_integrity_check(plen,
                    310:                                    4 + proto_len + 4 + data_len, type);
1.1       markus    311:                                s->screen = 0;
1.56      markus    312:                        }
1.1       markus    313:                        s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
                    314:
                    315:                        if (s->display == NULL)
                    316:                                break;
                    317:
                    318:                        /* Setup to always have a local .Xauthority. */
                    319:                        xauthfile = xmalloc(MAXPATHLEN);
                    320:                        strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
                    321:                        temporarily_use_uid(pw->pw_uid);
                    322:                        if (mkdtemp(xauthfile) == NULL) {
                    323:                                restore_uid();
                    324:                                error("private X11 dir: mkdtemp %s failed: %s",
                    325:                                    xauthfile, strerror(errno));
                    326:                                xfree(xauthfile);
                    327:                                xauthfile = NULL;
1.7       markus    328:                                /* XXXX remove listening channels */
1.1       markus    329:                                break;
                    330:                        }
                    331:                        strlcat(xauthfile, "/cookies", MAXPATHLEN);
1.33      markus    332:                        fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
                    333:                        if (fd >= 0)
                    334:                                close(fd);
1.1       markus    335:                        restore_uid();
                    336:                        fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
                    337:                        success = 1;
                    338:                        break;
                    339:
                    340:                case SSH_CMSG_AGENT_REQUEST_FORWARDING:
                    341:                        if (no_agent_forwarding_flag || compat13) {
                    342:                                debug("Authentication agent forwarding not permitted for this authentication.");
                    343:                                break;
                    344:                        }
                    345:                        debug("Received authentication agent forwarding request.");
1.15      markus    346:                        success = auth_input_request_forwarding(pw);
1.1       markus    347:                        break;
                    348:
                    349:                case SSH_CMSG_PORT_FORWARD_REQUEST:
                    350:                        if (no_port_forwarding_flag) {
                    351:                                debug("Port forwarding not permitted for this authentication.");
1.39      markus    352:                                break;
                    353:                        }
                    354:                        if (!options.allow_tcp_forwarding) {
                    355:                                debug("Port forwarding not permitted.");
1.1       markus    356:                                break;
                    357:                        }
                    358:                        debug("Received TCP/IP port forwarding request.");
1.12      markus    359:                        channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
1.1       markus    360:                        success = 1;
                    361:                        break;
                    362:
                    363:                case SSH_CMSG_MAX_PACKET_SIZE:
                    364:                        if (packet_set_maxsize(packet_get_int()) > 0)
                    365:                                success = 1;
                    366:                        break;
                    367:
                    368:                case SSH_CMSG_EXEC_SHELL:
                    369:                case SSH_CMSG_EXEC_CMD:
                    370:                        if (type == SSH_CMSG_EXEC_CMD) {
                    371:                                command = packet_get_string(&dlen);
                    372:                                debug("Exec command '%.500s'", command);
                    373:                                packet_integrity_check(plen, 4 + dlen, type);
                    374:                        } else {
                    375:                                command = NULL;
                    376:                                packet_integrity_check(plen, 0, type);
                    377:                        }
                    378:                        if (forced_command != NULL) {
1.34      markus    379:                                original_command = command;
1.1       markus    380:                                command = forced_command;
                    381:                                debug("Forced command '%.500s'", forced_command);
                    382:                        }
                    383:                        if (have_pty)
                    384:                                do_exec_pty(s, command, pw);
                    385:                        else
                    386:                                do_exec_no_pty(s, command, pw);
                    387:
                    388:                        if (command != NULL)
                    389:                                xfree(command);
                    390:                        /* Cleanup user's local Xauthority file. */
                    391:                        if (xauthfile)
                    392:                                xauthfile_cleanup_proc(NULL);
                    393:                        return;
                    394:
                    395:                default:
                    396:                        /*
                    397:                         * Any unknown messages in this phase are ignored,
                    398:                         * and a failure message is returned.
                    399:                         */
                    400:                        log("Unknown packet type received after authentication: %d", type);
                    401:                }
                    402:                packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
                    403:                packet_send();
                    404:                packet_write_wait();
                    405:
                    406:                /* Enable compression now that we have replied if appropriate. */
                    407:                if (enable_compression_after_reply) {
                    408:                        enable_compression_after_reply = 0;
                    409:                        packet_start_compression(compression_level);
                    410:                }
                    411:        }
                    412: }
                    413:
                    414: /*
                    415:  * This is called to fork and execute a command when we have no tty.  This
                    416:  * will call do_child from the child, and server_loop from the parent after
                    417:  * setting up file descriptors and such.
                    418:  */
1.4       markus    419: void
1.1       markus    420: do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
                    421: {
                    422:        int pid;
                    423:
                    424: #ifdef USE_PIPES
                    425:        int pin[2], pout[2], perr[2];
                    426:        /* Allocate pipes for communicating with the program. */
                    427:        if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
                    428:                packet_disconnect("Could not create pipes: %.100s",
                    429:                                  strerror(errno));
                    430: #else /* USE_PIPES */
                    431:        int inout[2], err[2];
                    432:        /* Uses socket pairs to communicate with the program. */
                    433:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
                    434:            socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
                    435:                packet_disconnect("Could not create socket pairs: %.100s",
                    436:                                  strerror(errno));
                    437: #endif /* USE_PIPES */
                    438:        if (s == NULL)
                    439:                fatal("do_exec_no_pty: no session");
                    440:
1.9       markus    441:        session_proctitle(s);
1.1       markus    442:
                    443:        /* Fork the child. */
                    444:        if ((pid = fork()) == 0) {
                    445:                /* Child.  Reinitialize the log since the pid has changed. */
                    446:                log_init(__progname, options.log_level, options.log_facility, log_stderr);
                    447:
                    448:                /*
                    449:                 * Create a new session and process group since the 4.4BSD
                    450:                 * setlogin() affects the entire process group.
                    451:                 */
                    452:                if (setsid() < 0)
                    453:                        error("setsid failed: %.100s", strerror(errno));
                    454:
                    455: #ifdef USE_PIPES
                    456:                /*
                    457:                 * Redirect stdin.  We close the parent side of the socket
                    458:                 * pair, and make the child side the standard input.
                    459:                 */
                    460:                close(pin[1]);
                    461:                if (dup2(pin[0], 0) < 0)
                    462:                        perror("dup2 stdin");
                    463:                close(pin[0]);
                    464:
                    465:                /* Redirect stdout. */
                    466:                close(pout[0]);
                    467:                if (dup2(pout[1], 1) < 0)
                    468:                        perror("dup2 stdout");
                    469:                close(pout[1]);
                    470:
                    471:                /* Redirect stderr. */
                    472:                close(perr[0]);
                    473:                if (dup2(perr[1], 2) < 0)
                    474:                        perror("dup2 stderr");
                    475:                close(perr[1]);
                    476: #else /* USE_PIPES */
                    477:                /*
                    478:                 * Redirect stdin, stdout, and stderr.  Stdin and stdout will
                    479:                 * use the same socket, as some programs (particularly rdist)
                    480:                 * seem to depend on it.
                    481:                 */
                    482:                close(inout[1]);
                    483:                close(err[1]);
                    484:                if (dup2(inout[0], 0) < 0)      /* stdin */
                    485:                        perror("dup2 stdin");
                    486:                if (dup2(inout[0], 1) < 0)      /* stdout.  Note: same socket as stdin. */
                    487:                        perror("dup2 stdout");
                    488:                if (dup2(err[0], 2) < 0)        /* stderr */
                    489:                        perror("dup2 stderr");
                    490: #endif /* USE_PIPES */
                    491:
                    492:                /* Do processing for the child (exec command etc). */
1.60      markus    493:                do_child(s, command);
1.1       markus    494:                /* NOTREACHED */
                    495:        }
                    496:        if (pid < 0)
                    497:                packet_disconnect("fork failed: %.100s", strerror(errno));
                    498:        s->pid = pid;
1.47      markus    499:        /* Set interactive/non-interactive mode. */
1.48      markus    500:        packet_set_interactive(s->display != NULL);
1.1       markus    501: #ifdef USE_PIPES
                    502:        /* We are the parent.  Close the child sides of the pipes. */
                    503:        close(pin[0]);
                    504:        close(pout[1]);
                    505:        close(perr[1]);
                    506:
1.2       markus    507:        if (compat20) {
1.7       markus    508:                session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
1.2       markus    509:        } else {
                    510:                /* Enter the interactive session. */
                    511:                server_loop(pid, pin[1], pout[0], perr[0]);
                    512:                /* server_loop has closed pin[1], pout[1], and perr[1]. */
                    513:        }
1.1       markus    514: #else /* USE_PIPES */
                    515:        /* We are the parent.  Close the child sides of the socket pairs. */
                    516:        close(inout[0]);
                    517:        close(err[0]);
                    518:
                    519:        /*
                    520:         * Enter the interactive session.  Note: server_loop must be able to
                    521:         * handle the case that fdin and fdout are the same.
                    522:         */
1.2       markus    523:        if (compat20) {
1.7       markus    524:                session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
1.2       markus    525:        } else {
                    526:                server_loop(pid, inout[1], inout[1], err[1]);
                    527:                /* server_loop has closed inout[1] and err[1]. */
                    528:        }
1.1       markus    529: #endif /* USE_PIPES */
                    530: }
                    531:
                    532: /*
                    533:  * This is called to fork and execute a command when we have a tty.  This
                    534:  * will call do_child from the child, and server_loop from the parent after
                    535:  * setting up file descriptors, controlling tty, updating wtmp, utmp,
                    536:  * lastlog, and other such operations.
                    537:  */
1.4       markus    538: void
1.1       markus    539: do_exec_pty(Session *s, const char *command, struct passwd * pw)
                    540: {
                    541:        int fdout, ptyfd, ttyfd, ptymaster;
                    542:        pid_t pid;
                    543:
                    544:        if (s == NULL)
                    545:                fatal("do_exec_pty: no session");
                    546:        ptyfd = s->ptyfd;
                    547:        ttyfd = s->ttyfd;
                    548:
                    549:        /* Fork the child. */
                    550:        if ((pid = fork()) == 0) {
1.24      markus    551:                /* Child.  Reinitialize the log because the pid has changed. */
1.1       markus    552:                log_init(__progname, options.log_level, options.log_facility, log_stderr);
                    553:
                    554:                /* Close the master side of the pseudo tty. */
                    555:                close(ptyfd);
                    556:
                    557:                /* Make the pseudo tty our controlling tty. */
                    558:                pty_make_controlling_tty(&ttyfd, s->tty);
                    559:
                    560:                /* Redirect stdin from the pseudo tty. */
                    561:                if (dup2(ttyfd, fileno(stdin)) < 0)
                    562:                        error("dup2 stdin failed: %.100s", strerror(errno));
                    563:
                    564:                /* Redirect stdout to the pseudo tty. */
                    565:                if (dup2(ttyfd, fileno(stdout)) < 0)
                    566:                        error("dup2 stdin failed: %.100s", strerror(errno));
                    567:
                    568:                /* Redirect stderr to the pseudo tty. */
                    569:                if (dup2(ttyfd, fileno(stderr)) < 0)
                    570:                        error("dup2 stdin failed: %.100s", strerror(errno));
                    571:
                    572:                /* Close the extra descriptor for the pseudo tty. */
                    573:                close(ttyfd);
                    574:
1.24      markus    575:                /* record login, etc. similar to login(1) */
1.41      markus    576:                if (!(options.use_login && command == NULL))
                    577:                        do_login(s, command);
1.1       markus    578:
                    579:                /* Do common processing for the child, such as execing the command. */
1.60      markus    580:                do_child(s, command);
1.1       markus    581:                /* NOTREACHED */
                    582:        }
                    583:        if (pid < 0)
                    584:                packet_disconnect("fork failed: %.100s", strerror(errno));
                    585:        s->pid = pid;
                    586:
                    587:        /* Parent.  Close the slave side of the pseudo tty. */
                    588:        close(ttyfd);
                    589:
                    590:        /*
                    591:         * Create another descriptor of the pty master side for use as the
                    592:         * standard input.  We could use the original descriptor, but this
                    593:         * simplifies code in server_loop.  The descriptor is bidirectional.
                    594:         */
                    595:        fdout = dup(ptyfd);
                    596:        if (fdout < 0)
                    597:                packet_disconnect("dup #1 failed: %.100s", strerror(errno));
                    598:
                    599:        /* we keep a reference to the pty master */
                    600:        ptymaster = dup(ptyfd);
                    601:        if (ptymaster < 0)
                    602:                packet_disconnect("dup #2 failed: %.100s", strerror(errno));
                    603:        s->ptymaster = ptymaster;
                    604:
                    605:        /* Enter interactive session. */
1.47      markus    606:        packet_set_interactive(1);
1.2       markus    607:        if (compat20) {
                    608:                session_set_fds(s, ptyfd, fdout, -1);
                    609:        } else {
                    610:                server_loop(pid, ptyfd, fdout, -1);
                    611:                /* server_loop _has_ closed ptyfd and fdout. */
                    612:                session_pty_cleanup(s);
1.24      markus    613:        }
                    614: }
                    615:
1.25      markus    616: const char *
                    617: get_remote_name_or_ip(void)
                    618: {
                    619:        static const char *remote = "";
                    620:        if (utmp_len > 0)
1.52      markus    621:                remote = get_canonical_hostname(options.reverse_mapping_check);
1.25      markus    622:        if (utmp_len == 0 || strlen(remote) > utmp_len)
                    623:                remote = get_remote_ipaddr();
                    624:        return remote;
                    625: }
                    626:
1.24      markus    627: /* administrative, login(1)-like work */
                    628: void
1.41      markus    629: do_login(Session *s, const char *command)
1.24      markus    630: {
                    631:        FILE *f;
                    632:        char *time_string;
                    633:        char buf[256];
1.35      markus    634:        char hostname[MAXHOSTNAMELEN];
1.24      markus    635:        socklen_t fromlen;
                    636:        struct sockaddr_storage from;
                    637:        struct stat st;
                    638:        time_t last_login_time;
                    639:        struct passwd * pw = s->pw;
                    640:        pid_t pid = getpid();
                    641:
                    642:        /*
                    643:         * Get IP address of client. If the connection is not a socket, let
                    644:         * the address be 0.0.0.0.
                    645:         */
                    646:        memset(&from, 0, sizeof(from));
                    647:        if (packet_connection_is_on_socket()) {
                    648:                fromlen = sizeof(from);
                    649:                if (getpeername(packet_get_connection_in(),
                    650:                     (struct sockaddr *) & from, &fromlen) < 0) {
                    651:                        debug("getpeername: %.100s", strerror(errno));
                    652:                        fatal_cleanup();
                    653:                }
                    654:        }
1.25      markus    655:
1.35      markus    656:        /* Get the time and hostname when the user last logged in. */
1.36      markus    657:        hostname[0] = '\0';
1.35      markus    658:        last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
                    659:            hostname, sizeof(hostname));
                    660:
1.24      markus    661:        /* Record that there was a login on that tty from the remote host. */
                    662:        record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
1.25      markus    663:            get_remote_name_or_ip(), (struct sockaddr *)&from);
1.24      markus    664:
1.41      markus    665:        /* Done if .hushlogin exists or a command given. */
                    666:        if (command != NULL)
                    667:                return;
1.24      markus    668:        snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
1.28      millert   669: #ifdef HAVE_LOGIN_CAP
                    670:        if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
                    671: #else
1.24      markus    672:        if (stat(buf, &st) >= 0)
1.28      millert   673: #endif
1.24      markus    674:                return;
                    675:        if (last_login_time != 0) {
                    676:                time_string = ctime(&last_login_time);
                    677:                if (strchr(time_string, '\n'))
                    678:                        *strchr(time_string, '\n') = 0;
1.40      markus    679:                if (strcmp(hostname, "") == 0)
1.24      markus    680:                        printf("Last login: %s\r\n", time_string);
                    681:                else
1.36      markus    682:                        printf("Last login: %s from %s\r\n", time_string, hostname);
1.24      markus    683:        }
                    684:        if (options.print_motd) {
1.28      millert   685: #ifdef HAVE_LOGIN_CAP
                    686:                f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
                    687:                    "/etc/motd"), "r");
                    688: #else
1.24      markus    689:                f = fopen("/etc/motd", "r");
1.28      millert   690: #endif
1.24      markus    691:                if (f) {
                    692:                        while (fgets(buf, sizeof(buf), f))
                    693:                                fputs(buf, stdout);
                    694:                        fclose(f);
                    695:                }
1.2       markus    696:        }
1.1       markus    697: }
                    698:
                    699: /*
                    700:  * Sets the value of the given variable in the environment.  If the variable
                    701:  * already exists, its value is overriden.
                    702:  */
1.4       markus    703: void
1.45      markus    704: child_set_env(char ***envp, u_int *envsizep, const char *name,
1.1       markus    705:              const char *value)
                    706: {
1.45      markus    707:        u_int i, namelen;
1.1       markus    708:        char **env;
                    709:
                    710:        /*
                    711:         * Find the slot where the value should be stored.  If the variable
                    712:         * already exists, we reuse the slot; otherwise we append a new slot
                    713:         * at the end of the array, expanding if necessary.
                    714:         */
                    715:        env = *envp;
                    716:        namelen = strlen(name);
                    717:        for (i = 0; env[i]; i++)
                    718:                if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
                    719:                        break;
                    720:        if (env[i]) {
                    721:                /* Reuse the slot. */
                    722:                xfree(env[i]);
                    723:        } else {
                    724:                /* New variable.  Expand if necessary. */
                    725:                if (i >= (*envsizep) - 1) {
                    726:                        (*envsizep) += 50;
                    727:                        env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
                    728:                }
                    729:                /* Need to set the NULL pointer at end of array beyond the new slot. */
                    730:                env[i + 1] = NULL;
                    731:        }
                    732:
                    733:        /* Allocate space and format the variable in the appropriate slot. */
                    734:        env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
                    735:        snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
                    736: }
                    737:
                    738: /*
                    739:  * Reads environment variables from the given file and adds/overrides them
                    740:  * into the environment.  If the file does not exist, this does nothing.
                    741:  * Otherwise, it must consist of empty lines, comments (line starts with '#')
                    742:  * and assignments of the form name=value.  No other forms are allowed.
                    743:  */
1.4       markus    744: void
1.45      markus    745: read_environment_file(char ***env, u_int *envsize,
1.1       markus    746:                      const char *filename)
                    747: {
                    748:        FILE *f;
                    749:        char buf[4096];
                    750:        char *cp, *value;
                    751:
                    752:        f = fopen(filename, "r");
                    753:        if (!f)
                    754:                return;
                    755:
                    756:        while (fgets(buf, sizeof(buf), f)) {
                    757:                for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
                    758:                        ;
                    759:                if (!*cp || *cp == '#' || *cp == '\n')
                    760:                        continue;
                    761:                if (strchr(cp, '\n'))
                    762:                        *strchr(cp, '\n') = '\0';
                    763:                value = strchr(cp, '=');
                    764:                if (value == NULL) {
                    765:                        fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
                    766:                        continue;
                    767:                }
1.14      deraadt   768:                /*
                    769:                 * Replace the equals sign by nul, and advance value to
                    770:                 * the value string.
                    771:                 */
1.1       markus    772:                *value = '\0';
                    773:                value++;
                    774:                child_set_env(env, envsize, cp, value);
                    775:        }
                    776:        fclose(f);
                    777: }
                    778:
                    779: /*
                    780:  * Performs common processing for the child, such as setting up the
                    781:  * environment, closing extra file descriptors, setting the user and group
                    782:  * ids, and executing the command or shell.
                    783:  */
1.4       markus    784: void
1.60      markus    785: do_child(Session *s, const char *command)
1.1       markus    786: {
1.32      markus    787:        const char *shell, *hostname = NULL, *cp = NULL;
1.60      markus    788:        struct passwd * pw = s->pw;
1.1       markus    789:        char buf[256];
1.16      markus    790:        char cmd[1024];
1.27      millert   791:        FILE *f = NULL;
1.45      markus    792:        u_int envsize, i;
1.1       markus    793:        char **env;
                    794:        extern char **environ;
                    795:        struct stat st;
                    796:        char *argv[10];
1.60      markus    797:        int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
1.17      markus    798:
                    799:        /* login(1) is only called if we execute the login shell */
                    800:        if (options.use_login && command != NULL)
                    801:                options.use_login = 0;
1.1       markus    802:
1.27      millert   803:        if (!options.use_login) {
1.28      millert   804: #ifdef HAVE_LOGIN_CAP
                    805:                if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
                    806:                        f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
                    807:                            _PATH_NOLOGIN), "r");
                    808: #else
1.27      millert   809:                if (pw->pw_uid)
                    810:                        f = fopen(_PATH_NOLOGIN, "r");
1.28      millert   811: #endif
1.27      millert   812:                if (f) {
                    813:                        /* /etc/nologin exists.  Print its contents and exit. */
                    814:                        while (fgets(buf, sizeof(buf), f))
                    815:                                fputs(buf, stderr);
                    816:                        fclose(f);
1.1       markus    817:                        exit(254);
1.27      millert   818:                }
1.1       markus    819:        }
1.28      millert   820:        /* Set login name, uid, gid, and groups. */
1.1       markus    821:        /* Login(1) does this as well, and it needs uid 0 for the "-h"
                    822:           switch, so we let login(1) to this for us. */
                    823:        if (!options.use_login) {
                    824:                if (getuid() == 0 || geteuid() == 0) {
1.28      millert   825: #ifdef HAVE_LOGIN_CAP
                    826:                        if (setusercontext(lc, pw, pw->pw_uid,
                    827:                            (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
                    828:                                perror("unable to set user context");
                    829:                                exit(1);
                    830:                        }
1.62    ! markus    831: #ifdef BSD_AUTH
        !           832:                        if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
        !           833:                                error("approval failure for %s", pw->pw_name);
        !           834:                                fprintf(stderr, "Approval failure");
        !           835:                                exit(1);
        !           836:                        }
        !           837: #endif
1.28      millert   838: #else
                    839:                        if (setlogin(pw->pw_name) < 0)
                    840:                                error("setlogin failed: %s", strerror(errno));
1.1       markus    841:                        if (setgid(pw->pw_gid) < 0) {
                    842:                                perror("setgid");
                    843:                                exit(1);
                    844:                        }
                    845:                        /* Initialize the group list. */
                    846:                        if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
                    847:                                perror("initgroups");
                    848:                                exit(1);
                    849:                        }
                    850:                        endgrent();
                    851:
                    852:                        /* Permanently switch to the desired uid. */
                    853:                        permanently_set_uid(pw->pw_uid);
1.28      millert   854: #endif
1.1       markus    855:                }
                    856:                if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1.31      deraadt   857:                        fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1.1       markus    858:        }
                    859:        /*
                    860:         * Get the shell from the password data.  An empty shell field is
                    861:         * legal, and means /bin/sh.
                    862:         */
                    863:        shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.28      millert   864: #ifdef HAVE_LOGIN_CAP
                    865:        shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
                    866: #endif
1.1       markus    867:
                    868: #ifdef AFS
                    869:        /* Try to get AFS tokens for the local cell. */
                    870:        if (k_hasafs()) {
                    871:                char cell[64];
                    872:
                    873:                if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
                    874:                        krb_afslog(cell, 0);
                    875:
                    876:                krb_afslog(0, 0);
                    877:        }
                    878: #endif /* AFS */
                    879:
                    880:        /* Initialize the environment. */
                    881:        envsize = 100;
                    882:        env = xmalloc(envsize * sizeof(char *));
                    883:        env[0] = NULL;
                    884:
                    885:        if (!options.use_login) {
                    886:                /* Set basic environment. */
                    887:                child_set_env(&env, &envsize, "USER", pw->pw_name);
                    888:                child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
                    889:                child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1.28      millert   890: #ifdef HAVE_LOGIN_CAP
1.29      millert   891:                (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
                    892:                child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1.28      millert   893: #else
1.29      millert   894:                child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1.28      millert   895: #endif
1.1       markus    896:
                    897:                snprintf(buf, sizeof buf, "%.200s/%.50s",
                    898:                         _PATH_MAILDIR, pw->pw_name);
                    899:                child_set_env(&env, &envsize, "MAIL", buf);
                    900:
                    901:                /* Normal systems set SHELL by default. */
                    902:                child_set_env(&env, &envsize, "SHELL", shell);
                    903:        }
                    904:        if (getenv("TZ"))
                    905:                child_set_env(&env, &envsize, "TZ", getenv("TZ"));
                    906:
                    907:        /* Set custom environment options from RSA authentication. */
                    908:        while (custom_environment) {
                    909:                struct envstring *ce = custom_environment;
                    910:                char *s = ce->s;
                    911:                int i;
                    912:                for (i = 0; s[i] != '=' && s[i]; i++);
                    913:                if (s[i] == '=') {
                    914:                        s[i] = 0;
                    915:                        child_set_env(&env, &envsize, s, s + i + 1);
                    916:                }
                    917:                custom_environment = ce->next;
                    918:                xfree(ce->s);
                    919:                xfree(ce);
                    920:        }
                    921:
                    922:        snprintf(buf, sizeof buf, "%.50s %d %d",
                    923:                 get_remote_ipaddr(), get_remote_port(), get_local_port());
                    924:        child_set_env(&env, &envsize, "SSH_CLIENT", buf);
                    925:
1.60      markus    926:        if (s->ttyfd != -1)
                    927:                child_set_env(&env, &envsize, "SSH_TTY", s->tty);
                    928:        if (s->term)
                    929:                child_set_env(&env, &envsize, "TERM", s->term);
                    930:        if (s->display)
                    931:                child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34      markus    932:        if (original_command)
                    933:                child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
                    934:                    original_command);
1.1       markus    935:
                    936: #ifdef KRB4
                    937:        {
                    938:                extern char *ticket;
                    939:
                    940:                if (ticket)
                    941:                        child_set_env(&env, &envsize, "KRBTKFILE", ticket);
                    942:        }
                    943: #endif /* KRB4 */
                    944:
                    945:        if (xauthfile)
                    946:                child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
                    947:        if (auth_get_socket_name() != NULL)
                    948:                child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
                    949:                              auth_get_socket_name());
                    950:
                    951:        /* read $HOME/.ssh/environment. */
                    952:        if (!options.use_login) {
1.14      deraadt   953:                snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
                    954:                    pw->pw_dir);
1.1       markus    955:                read_environment_file(&env, &envsize, buf);
                    956:        }
                    957:        if (debug_flag) {
                    958:                /* dump the environment */
                    959:                fprintf(stderr, "Environment:\n");
                    960:                for (i = 0; env[i]; i++)
                    961:                        fprintf(stderr, "  %.200s\n", env[i]);
                    962:        }
1.26      millert   963:        /* we have to stash the hostname before we close our socket. */
                    964:        if (options.use_login)
                    965:                hostname = get_remote_name_or_ip();
1.1       markus    966:        /*
                    967:         * Close the connection descriptors; note that this is the child, and
                    968:         * the server will still have the socket open, and it is important
                    969:         * that we do not shutdown it.  Note that the descriptors cannot be
                    970:         * closed before building the environment, as we call
                    971:         * get_remote_ipaddr there.
                    972:         */
                    973:        if (packet_get_connection_in() == packet_get_connection_out())
                    974:                close(packet_get_connection_in());
                    975:        else {
                    976:                close(packet_get_connection_in());
                    977:                close(packet_get_connection_out());
                    978:        }
                    979:        /*
                    980:         * Close all descriptors related to channels.  They will still remain
                    981:         * open in the parent.
                    982:         */
                    983:        /* XXX better use close-on-exec? -markus */
                    984:        channel_close_all();
                    985:
                    986:        /*
                    987:         * Close any extra file descriptors.  Note that there may still be
                    988:         * descriptors left by system functions.  They will be closed later.
                    989:         */
                    990:        endpwent();
                    991:
                    992:        /*
                    993:         * Close any extra open file descriptors so that we don\'t have them
                    994:         * hanging around in clients.  Note that we want to do this after
                    995:         * initgroups, because at least on Solaris 2.3 it leaves file
                    996:         * descriptors open.
                    997:         */
                    998:        for (i = 3; i < 64; i++)
                    999:                close(i);
                   1000:
                   1001:        /* Change current directory to the user\'s home directory. */
1.28      millert  1002:        if (chdir(pw->pw_dir) < 0) {
1.1       markus   1003:                fprintf(stderr, "Could not chdir to home directory %s: %s\n",
                   1004:                        pw->pw_dir, strerror(errno));
1.28      millert  1005: #ifdef HAVE_LOGIN_CAP
                   1006:                if (login_getcapbool(lc, "requirehome", 0))
                   1007:                        exit(1);
                   1008: #endif
                   1009:        }
1.1       markus   1010:
                   1011:        /*
                   1012:         * Must take new environment into use so that .ssh/rc, /etc/sshrc and
                   1013:         * xauth are run in the proper environment.
                   1014:         */
                   1015:        environ = env;
                   1016:
                   1017:        /*
                   1018:         * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
                   1019:         * in this order).
                   1020:         */
                   1021:        if (!options.use_login) {
1.50      markus   1022:                if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
1.1       markus   1023:                        if (debug_flag)
1.60      markus   1024:                                fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
                   1025:                                    _PATH_SSH_USER_RC);
1.50      markus   1026:                        f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
1.1       markus   1027:                        if (f) {
1.60      markus   1028:                                if (do_xauth)
                   1029:                                        fprintf(f, "%s %s\n", s->auth_proto,
                   1030:                                            s->auth_data);
1.1       markus   1031:                                pclose(f);
                   1032:                        } else
1.60      markus   1033:                                fprintf(stderr, "Could not run %s\n",
                   1034:                                    _PATH_SSH_USER_RC);
1.50      markus   1035:                } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1.1       markus   1036:                        if (debug_flag)
1.60      markus   1037:                                fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
                   1038:                                    _PATH_SSH_SYSTEM_RC);
1.50      markus   1039:                        f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1.1       markus   1040:                        if (f) {
1.60      markus   1041:                                if (do_xauth)
                   1042:                                        fprintf(f, "%s %s\n", s->auth_proto,
                   1043:                                            s->auth_data);
1.1       markus   1044:                                pclose(f);
                   1045:                        } else
1.60      markus   1046:                                fprintf(stderr, "Could not run %s\n",
                   1047:                                    _PATH_SSH_SYSTEM_RC);
                   1048:                } else if (do_xauth && options.xauth_location != NULL) {
1.1       markus   1049:                        /* Add authority data to .Xauthority if appropriate. */
1.60      markus   1050:                        char *screen = strchr(s->display, ':');
                   1051:
                   1052:                        if (debug_flag) {
                   1053:                                fprintf(stderr,
                   1054:                                    "Running %.100s add "
                   1055:                                    "%.100s %.100s %.100s\n",
                   1056:                                    options.xauth_location, s->display,
                   1057:                                    s->auth_proto, s->auth_data);
                   1058:                                if (screen != NULL)
1.14      deraadt  1059:                                        fprintf(stderr,
1.60      markus   1060:                                            "Adding %.*s/unix%s %s %s\n",
                   1061:                                            (int)(screen - s->display),
                   1062:                                            s->display, screen,
                   1063:                                            s->auth_proto, s->auth_data);
                   1064:                        }
                   1065:                        snprintf(cmd, sizeof cmd, "%s -q -",
                   1066:                            options.xauth_location);
                   1067:                        f = popen(cmd, "w");
                   1068:                        if (f) {
                   1069:                                fprintf(f, "add %s %s %s\n", s->display,
                   1070:                                    s->auth_proto, s->auth_data);
                   1071:                                if (screen != NULL)
                   1072:                                        fprintf(f, "add %.*s/unix%s %s %s\n",
                   1073:                                            (int)(screen - s->display),
                   1074:                                            s->display, screen,
                   1075:                                            s->auth_proto,
                   1076:                                            s->auth_data);
                   1077:                                pclose(f);
                   1078:                        } else {
                   1079:                                fprintf(stderr, "Could not run %s\n",
                   1080:                                    cmd);
1.1       markus   1081:                        }
                   1082:                }
                   1083:                /* Get the last component of the shell name. */
                   1084:                cp = strrchr(shell, '/');
                   1085:                if (cp)
                   1086:                        cp++;
                   1087:                else
                   1088:                        cp = shell;
                   1089:        }
                   1090:        /*
                   1091:         * If we have no command, execute the shell.  In this case, the shell
                   1092:         * name to be passed in argv[0] is preceded by '-' to indicate that
                   1093:         * this is a login shell.
                   1094:         */
                   1095:        if (!command) {
                   1096:                if (!options.use_login) {
                   1097:                        char buf[256];
                   1098:
                   1099:                        /*
                   1100:                         * Check for mail if we have a tty and it was enabled
                   1101:                         * in server options.
                   1102:                         */
1.60      markus   1103:                        if (s->ttyfd != -1 && options.check_mail) {
1.1       markus   1104:                                char *mailbox;
                   1105:                                struct stat mailstat;
1.60      markus   1106:
1.1       markus   1107:                                mailbox = getenv("MAIL");
                   1108:                                if (mailbox != NULL) {
1.14      deraadt  1109:                                        if (stat(mailbox, &mailstat) != 0 ||
                   1110:                                            mailstat.st_size == 0)
1.1       markus   1111:                                                printf("No mail.\n");
                   1112:                                        else if (mailstat.st_mtime < mailstat.st_atime)
                   1113:                                                printf("You have mail.\n");
                   1114:                                        else
                   1115:                                                printf("You have new mail.\n");
                   1116:                                }
                   1117:                        }
                   1118:                        /* Start the shell.  Set initial character to '-'. */
                   1119:                        buf[0] = '-';
                   1120:                        strncpy(buf + 1, cp, sizeof(buf) - 1);
                   1121:                        buf[sizeof(buf) - 1] = 0;
                   1122:
                   1123:                        /* Execute the shell. */
                   1124:                        argv[0] = buf;
                   1125:                        argv[1] = NULL;
                   1126:                        execve(shell, argv, env);
                   1127:
                   1128:                        /* Executing the shell failed. */
                   1129:                        perror(shell);
                   1130:                        exit(1);
                   1131:
                   1132:                } else {
                   1133:                        /* Launch login(1). */
                   1134:
1.26      millert  1135:                        execl("/usr/bin/login", "login", "-h", hostname,
1.25      markus   1136:                             "-p", "-f", "--", pw->pw_name, NULL);
1.1       markus   1137:
                   1138:                        /* Login couldn't be executed, die. */
                   1139:
                   1140:                        perror("login");
                   1141:                        exit(1);
                   1142:                }
                   1143:        }
                   1144:        /*
                   1145:         * Execute the command using the user's shell.  This uses the -c
                   1146:         * option to execute the command.
                   1147:         */
                   1148:        argv[0] = (char *) cp;
                   1149:        argv[1] = "-c";
                   1150:        argv[2] = (char *) command;
                   1151:        argv[3] = NULL;
                   1152:        execve(shell, argv, env);
                   1153:        perror(shell);
                   1154:        exit(1);
                   1155: }
                   1156:
                   1157: Session *
                   1158: session_new(void)
                   1159: {
                   1160:        int i;
                   1161:        static int did_init = 0;
                   1162:        if (!did_init) {
                   1163:                debug("session_new: init");
                   1164:                for(i = 0; i < MAX_SESSIONS; i++) {
                   1165:                        sessions[i].used = 0;
                   1166:                        sessions[i].self = i;
                   1167:                }
                   1168:                did_init = 1;
                   1169:        }
                   1170:        for(i = 0; i < MAX_SESSIONS; i++) {
                   1171:                Session *s = &sessions[i];
                   1172:                if (! s->used) {
                   1173:                        s->pid = 0;
1.7       markus   1174:                        s->extended = 0;
1.1       markus   1175:                        s->chanid = -1;
                   1176:                        s->ptyfd = -1;
                   1177:                        s->ttyfd = -1;
                   1178:                        s->term = NULL;
                   1179:                        s->pw = NULL;
                   1180:                        s->display = NULL;
                   1181:                        s->screen = 0;
                   1182:                        s->auth_data = NULL;
                   1183:                        s->auth_proto = NULL;
                   1184:                        s->used = 1;
1.7       markus   1185:                        s->pw = NULL;
1.1       markus   1186:                        debug("session_new: session %d", i);
                   1187:                        return s;
                   1188:                }
                   1189:        }
                   1190:        return NULL;
                   1191: }
                   1192:
                   1193: void
                   1194: session_dump(void)
                   1195: {
                   1196:        int i;
                   1197:        for(i = 0; i < MAX_SESSIONS; i++) {
                   1198:                Session *s = &sessions[i];
                   1199:                debug("dump: used %d session %d %p channel %d pid %d",
                   1200:                    s->used,
                   1201:                    s->self,
                   1202:                    s,
                   1203:                    s->chanid,
                   1204:                    s->pid);
                   1205:        }
                   1206: }
                   1207:
1.2       markus   1208: int
                   1209: session_open(int chanid)
                   1210: {
                   1211:        Session *s = session_new();
                   1212:        debug("session_open: channel %d", chanid);
                   1213:        if (s == NULL) {
                   1214:                error("no more sessions");
                   1215:                return 0;
                   1216:        }
1.7       markus   1217:        s->pw = auth_get_user();
                   1218:        if (s->pw == NULL)
1.54      stevesk  1219:                fatal("no user for session %d", s->self);
1.2       markus   1220:        debug("session_open: session %d: link with channel %d", s->self, chanid);
                   1221:        s->chanid = chanid;
                   1222:        return 1;
                   1223: }
                   1224:
                   1225: Session *
                   1226: session_by_channel(int id)
                   1227: {
                   1228:        int i;
                   1229:        for(i = 0; i < MAX_SESSIONS; i++) {
                   1230:                Session *s = &sessions[i];
                   1231:                if (s->used && s->chanid == id) {
                   1232:                        debug("session_by_channel: session %d channel %d", i, id);
                   1233:                        return s;
                   1234:                }
                   1235:        }
                   1236:        debug("session_by_channel: unknown channel %d", id);
                   1237:        session_dump();
                   1238:        return NULL;
                   1239: }
                   1240:
                   1241: Session *
                   1242: session_by_pid(pid_t pid)
                   1243: {
                   1244:        int i;
                   1245:        debug("session_by_pid: pid %d", pid);
                   1246:        for(i = 0; i < MAX_SESSIONS; i++) {
                   1247:                Session *s = &sessions[i];
                   1248:                if (s->used && s->pid == pid)
                   1249:                        return s;
                   1250:        }
                   1251:        error("session_by_pid: unknown pid %d", pid);
                   1252:        session_dump();
                   1253:        return NULL;
                   1254: }
                   1255:
                   1256: int
                   1257: session_window_change_req(Session *s)
                   1258: {
                   1259:        s->col = packet_get_int();
                   1260:        s->row = packet_get_int();
                   1261:        s->xpixel = packet_get_int();
                   1262:        s->ypixel = packet_get_int();
1.3       markus   1263:        packet_done();
1.2       markus   1264:        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
                   1265:        return 1;
                   1266: }
                   1267:
                   1268: int
                   1269: session_pty_req(Session *s)
                   1270: {
1.45      markus   1271:        u_int len;
1.3       markus   1272:        char *term_modes;       /* encoded terminal modes */
1.2       markus   1273:
1.19      markus   1274:        if (no_pty_flag)
                   1275:                return 0;
1.2       markus   1276:        if (s->ttyfd != -1)
1.3       markus   1277:                return 0;
1.2       markus   1278:        s->term = packet_get_string(&len);
                   1279:        s->col = packet_get_int();
                   1280:        s->row = packet_get_int();
                   1281:        s->xpixel = packet_get_int();
                   1282:        s->ypixel = packet_get_int();
1.3       markus   1283:        term_modes = packet_get_string(&len);
                   1284:        packet_done();
1.2       markus   1285:
                   1286:        if (strcmp(s->term, "") == 0) {
                   1287:                xfree(s->term);
                   1288:                s->term = NULL;
                   1289:        }
                   1290:        /* Allocate a pty and open it. */
                   1291:        if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
                   1292:                xfree(s->term);
                   1293:                s->term = NULL;
                   1294:                s->ptyfd = -1;
                   1295:                s->ttyfd = -1;
                   1296:                error("session_pty_req: session %d alloc failed", s->self);
1.3       markus   1297:                xfree(term_modes);
                   1298:                return 0;
1.2       markus   1299:        }
                   1300:        debug("session_pty_req: session %d alloc %s", s->self, s->tty);
                   1301:        /*
                   1302:         * Add a cleanup function to clear the utmp entry and record logout
                   1303:         * time in case we call fatal() (e.g., the connection gets closed).
                   1304:         */
                   1305:        fatal_add_cleanup(pty_cleanup_proc, (void *)s);
                   1306:        pty_setowner(s->pw, s->tty);
                   1307:        /* Get window size from the packet. */
                   1308:        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
                   1309:
1.9       markus   1310:        session_proctitle(s);
                   1311:
1.3       markus   1312:        /* XXX parse and set terminal modes */
                   1313:        xfree(term_modes);
1.2       markus   1314:        return 1;
                   1315: }
                   1316:
1.7       markus   1317: int
                   1318: session_subsystem_req(Session *s)
                   1319: {
1.45      markus   1320:        u_int len;
1.7       markus   1321:        int success = 0;
                   1322:        char *subsys = packet_get_string(&len);
1.18      jakob    1323:        int i;
1.7       markus   1324:
                   1325:        packet_done();
                   1326:        log("subsystem request for %s", subsys);
1.18      jakob    1327:
                   1328:        for (i = 0; i < options.num_subsystems; i++) {
                   1329:                if(strcmp(subsys, options.subsystem_name[i]) == 0) {
                   1330:                        debug("subsystem: exec() %s", options.subsystem_command[i]);
                   1331:                        do_exec_no_pty(s, options.subsystem_command[i], s->pw);
                   1332:                        success = 1;
                   1333:                }
                   1334:        }
                   1335:
                   1336:        if (!success)
                   1337:                log("subsystem request for %s failed, subsystem not found", subsys);
1.7       markus   1338:
                   1339:        xfree(subsys);
                   1340:        return success;
                   1341: }
                   1342:
                   1343: int
                   1344: session_x11_req(Session *s)
                   1345: {
1.33      markus   1346:        int fd;
1.22      deraadt  1347:        if (no_x11_forwarding_flag) {
1.19      markus   1348:                debug("X11 forwarding disabled in user configuration file.");
                   1349:                return 0;
                   1350:        }
1.7       markus   1351:        if (!options.x11_forwarding) {
                   1352:                debug("X11 forwarding disabled in server configuration file.");
                   1353:                return 0;
                   1354:        }
                   1355:        if (xauthfile != NULL) {
                   1356:                debug("X11 fwd already started.");
                   1357:                return 0;
                   1358:        }
                   1359:
                   1360:        debug("Received request for X11 forwarding with auth spoofing.");
                   1361:        if (s->display != NULL)
                   1362:                packet_disconnect("Protocol error: X11 display already set.");
                   1363:
                   1364:        s->single_connection = packet_get_char();
                   1365:        s->auth_proto = packet_get_string(NULL);
                   1366:        s->auth_data = packet_get_string(NULL);
                   1367:        s->screen = packet_get_int();
                   1368:        packet_done();
                   1369:
                   1370:        s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
                   1371:        if (s->display == NULL) {
                   1372:                xfree(s->auth_proto);
                   1373:                xfree(s->auth_data);
                   1374:                return 0;
                   1375:        }
                   1376:        xauthfile = xmalloc(MAXPATHLEN);
                   1377:        strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
                   1378:        temporarily_use_uid(s->pw->pw_uid);
                   1379:        if (mkdtemp(xauthfile) == NULL) {
                   1380:                restore_uid();
                   1381:                error("private X11 dir: mkdtemp %s failed: %s",
                   1382:                    xauthfile, strerror(errno));
                   1383:                xfree(xauthfile);
                   1384:                xauthfile = NULL;
                   1385:                xfree(s->auth_proto);
                   1386:                xfree(s->auth_data);
                   1387:                /* XXXX remove listening channels */
                   1388:                return 0;
                   1389:        }
                   1390:        strlcat(xauthfile, "/cookies", MAXPATHLEN);
1.33      markus   1391:        fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
                   1392:        if (fd >= 0)
                   1393:                close(fd);
1.7       markus   1394:        restore_uid();
                   1395:        fatal_add_cleanup(xauthfile_cleanup_proc, s);
                   1396:        return 1;
                   1397: }
                   1398:
1.19      markus   1399: int
                   1400: session_shell_req(Session *s)
                   1401: {
                   1402:        /* if forced_command == NULL, the shell is execed */
                   1403:        char *shell = forced_command;
                   1404:        packet_done();
                   1405:        s->extended = 1;
                   1406:        if (s->ttyfd == -1)
                   1407:                do_exec_no_pty(s, shell, s->pw);
                   1408:        else
                   1409:                do_exec_pty(s, shell, s->pw);
                   1410:        return 1;
                   1411: }
                   1412:
                   1413: int
                   1414: session_exec_req(Session *s)
                   1415: {
1.45      markus   1416:        u_int len;
1.19      markus   1417:        char *command = packet_get_string(&len);
                   1418:        packet_done();
                   1419:        if (forced_command) {
1.34      markus   1420:                original_command = command;
1.19      markus   1421:                command = forced_command;
                   1422:                debug("Forced command '%.500s'", forced_command);
                   1423:        }
                   1424:        s->extended = 1;
                   1425:        if (s->ttyfd == -1)
                   1426:                do_exec_no_pty(s, command, s->pw);
                   1427:        else
                   1428:                do_exec_pty(s, command, s->pw);
                   1429:        if (forced_command == NULL)
                   1430:                xfree(command);
                   1431:        return 1;
                   1432: }
                   1433:
1.43      markus   1434: int
                   1435: session_auth_agent_req(Session *s)
                   1436: {
                   1437:        static int called = 0;
                   1438:        packet_done();
1.44      markus   1439:        if (no_agent_forwarding_flag) {
                   1440:                debug("session_auth_agent_req: no_agent_forwarding_flag");
                   1441:                return 0;
                   1442:        }
1.43      markus   1443:        if (called) {
                   1444:                return 0;
                   1445:        } else {
                   1446:                called = 1;
                   1447:                return auth_input_request_forwarding(s->pw);
                   1448:        }
                   1449: }
                   1450:
1.2       markus   1451: void
                   1452: session_input_channel_req(int id, void *arg)
                   1453: {
1.45      markus   1454:        u_int len;
1.2       markus   1455:        int reply;
                   1456:        int success = 0;
                   1457:        char *rtype;
                   1458:        Session *s;
                   1459:        Channel *c;
                   1460:
                   1461:        rtype = packet_get_string(&len);
                   1462:        reply = packet_get_char();
                   1463:
                   1464:        s = session_by_channel(id);
                   1465:        if (s == NULL)
                   1466:                fatal("session_input_channel_req: channel %d: no session", id);
                   1467:        c = channel_lookup(id);
                   1468:        if (c == NULL)
                   1469:                fatal("session_input_channel_req: channel %d: bad channel", id);
                   1470:
                   1471:        debug("session_input_channel_req: session %d channel %d request %s reply %d",
                   1472:            s->self, id, rtype, reply);
                   1473:
                   1474:        /*
                   1475:         * a session is in LARVAL state until a shell
                   1476:         * or programm is executed
                   1477:         */
                   1478:        if (c->type == SSH_CHANNEL_LARVAL) {
                   1479:                if (strcmp(rtype, "shell") == 0) {
1.19      markus   1480:                        success = session_shell_req(s);
1.2       markus   1481:                } else if (strcmp(rtype, "exec") == 0) {
1.19      markus   1482:                        success = session_exec_req(s);
1.2       markus   1483:                } else if (strcmp(rtype, "pty-req") == 0) {
1.3       markus   1484:                        success =  session_pty_req(s);
1.7       markus   1485:                } else if (strcmp(rtype, "x11-req") == 0) {
                   1486:                        success = session_x11_req(s);
1.43      markus   1487:                } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
                   1488:                        success = session_auth_agent_req(s);
1.7       markus   1489:                } else if (strcmp(rtype, "subsystem") == 0) {
                   1490:                        success = session_subsystem_req(s);
1.2       markus   1491:                }
                   1492:        }
                   1493:        if (strcmp(rtype, "window-change") == 0) {
                   1494:                success = session_window_change_req(s);
                   1495:        }
                   1496:
                   1497:        if (reply) {
                   1498:                packet_start(success ?
                   1499:                    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
                   1500:                packet_put_int(c->remote_id);
                   1501:                packet_send();
                   1502:        }
                   1503:        xfree(rtype);
                   1504: }
                   1505:
                   1506: void
                   1507: session_set_fds(Session *s, int fdin, int fdout, int fderr)
                   1508: {
                   1509:        if (!compat20)
                   1510:                fatal("session_set_fds: called for proto != 2.0");
                   1511:        /*
                   1512:         * now that have a child and a pipe to the child,
                   1513:         * we can activate our channel and register the fd's
                   1514:         */
                   1515:        if (s->chanid == -1)
                   1516:                fatal("no channel for session %d", s->self);
                   1517:        channel_set_fds(s->chanid,
                   1518:            fdout, fdin, fderr,
1.42      markus   1519:            fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
                   1520:            1);
1.2       markus   1521: }
                   1522:
1.1       markus   1523: void
                   1524: session_pty_cleanup(Session *s)
                   1525: {
                   1526:        if (s == NULL || s->ttyfd == -1)
                   1527:                return;
                   1528:
1.54      stevesk  1529:        debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1       markus   1530:
                   1531:        /* Cancel the cleanup function. */
                   1532:        fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
                   1533:
                   1534:        /* Record that the user has logged out. */
                   1535:        record_logout(s->pid, s->tty);
                   1536:
                   1537:        /* Release the pseudo-tty. */
                   1538:        pty_release(s->tty);
                   1539:
                   1540:        /*
                   1541:         * Close the server side of the socket pairs.  We must do this after
                   1542:         * the pty cleanup, so that another process doesn't get this pty
                   1543:         * while we're still cleaning up.
                   1544:         */
                   1545:        if (close(s->ptymaster) < 0)
                   1546:                error("close(s->ptymaster): %s", strerror(errno));
1.2       markus   1547: }
                   1548:
                   1549: void
                   1550: session_exit_message(Session *s, int status)
                   1551: {
                   1552:        Channel *c;
                   1553:        if (s == NULL)
                   1554:                fatal("session_close: no session");
                   1555:        c = channel_lookup(s->chanid);
                   1556:        if (c == NULL)
                   1557:                fatal("session_close: session %d: no channel %d",
                   1558:                    s->self, s->chanid);
                   1559:        debug("session_exit_message: session %d channel %d pid %d",
                   1560:            s->self, s->chanid, s->pid);
                   1561:
                   1562:        if (WIFEXITED(status)) {
                   1563:                channel_request_start(s->chanid,
                   1564:                    "exit-status", 0);
                   1565:                packet_put_int(WEXITSTATUS(status));
                   1566:                packet_send();
                   1567:        } else if (WIFSIGNALED(status)) {
                   1568:                channel_request_start(s->chanid,
                   1569:                    "exit-signal", 0);
                   1570:                packet_put_int(WTERMSIG(status));
                   1571:                packet_put_char(WCOREDUMP(status));
                   1572:                packet_put_cstring("");
                   1573:                packet_put_cstring("");
                   1574:                packet_send();
                   1575:        } else {
                   1576:                /* Some weird exit cause.  Just exit. */
                   1577:                packet_disconnect("wait returned status %04x.", status);
                   1578:        }
                   1579:
                   1580:        /* disconnect channel */
                   1581:        debug("session_exit_message: release channel %d", s->chanid);
                   1582:        channel_cancel_cleanup(s->chanid);
1.5       markus   1583:        /*
                   1584:         * emulate a write failure with 'chan_write_failed', nobody will be
                   1585:         * interested in data we write.
                   1586:         * Note that we must not call 'chan_read_failed', since there could
                   1587:         * be some more data waiting in the pipe.
                   1588:         */
1.8       markus   1589:        if (c->ostate != CHAN_OUTPUT_CLOSED)
                   1590:                chan_write_failed(c);
1.2       markus   1591:        s->chanid = -1;
                   1592: }
                   1593:
                   1594: void
                   1595: session_free(Session *s)
                   1596: {
                   1597:        debug("session_free: session %d pid %d", s->self, s->pid);
                   1598:        if (s->term)
                   1599:                xfree(s->term);
                   1600:        if (s->display)
                   1601:                xfree(s->display);
                   1602:        if (s->auth_data)
                   1603:                xfree(s->auth_data);
                   1604:        if (s->auth_proto)
                   1605:                xfree(s->auth_proto);
                   1606:        s->used = 0;
                   1607: }
                   1608:
                   1609: void
                   1610: session_close(Session *s)
                   1611: {
                   1612:        session_pty_cleanup(s);
                   1613:        session_free(s);
1.9       markus   1614:        session_proctitle(s);
1.2       markus   1615: }
                   1616:
                   1617: void
                   1618: session_close_by_pid(pid_t pid, int status)
                   1619: {
                   1620:        Session *s = session_by_pid(pid);
                   1621:        if (s == NULL) {
                   1622:                debug("session_close_by_pid: no session for pid %d", s->pid);
                   1623:                return;
                   1624:        }
                   1625:        if (s->chanid != -1)
                   1626:                session_exit_message(s, status);
                   1627:        session_close(s);
                   1628: }
                   1629:
                   1630: /*
                   1631:  * this is called when a channel dies before
                   1632:  * the session 'child' itself dies
                   1633:  */
                   1634: void
                   1635: session_close_by_channel(int id, void *arg)
                   1636: {
                   1637:        Session *s = session_by_channel(id);
                   1638:        if (s == NULL) {
                   1639:                debug("session_close_by_channel: no session for channel %d", id);
                   1640:                return;
                   1641:        }
                   1642:        /* disconnect channel */
                   1643:        channel_cancel_cleanup(s->chanid);
                   1644:        s->chanid = -1;
                   1645:
                   1646:        debug("session_close_by_channel: channel %d kill %d", id, s->pid);
                   1647:        if (s->pid == 0) {
                   1648:                /* close session immediately */
                   1649:                session_close(s);
                   1650:        } else {
                   1651:                /* notify child, delay session cleanup */
                   1652:                if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
                   1653:                        error("session_close_by_channel: kill %d: %s",
                   1654:                            s->pid, strerror(errno));
                   1655:        }
1.9       markus   1656: }
                   1657:
                   1658: char *
                   1659: session_tty_list(void)
                   1660: {
                   1661:        static char buf[1024];
                   1662:        int i;
                   1663:        buf[0] = '\0';
                   1664:        for(i = 0; i < MAX_SESSIONS; i++) {
                   1665:                Session *s = &sessions[i];
                   1666:                if (s->used && s->ttyfd != -1) {
                   1667:                        if (buf[0] != '\0')
                   1668:                                strlcat(buf, ",", sizeof buf);
                   1669:                        strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
                   1670:                }
                   1671:        }
                   1672:        if (buf[0] == '\0')
                   1673:                strlcpy(buf, "notty", sizeof buf);
                   1674:        return buf;
                   1675: }
                   1676:
                   1677: void
                   1678: session_proctitle(Session *s)
                   1679: {
                   1680:        if (s->pw == NULL)
                   1681:                error("no user for session %d", s->self);
                   1682:        else
                   1683:                setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.2       markus   1684: }
                   1685:
                   1686: void
1.49      markus   1687: do_authenticated2(Authctxt *authctxt)
1.2       markus   1688: {
                   1689:        /*
                   1690:         * Cancel the alarm we set to limit the time taken for
                   1691:         * authentication.
                   1692:         */
                   1693:        alarm(0);
1.23      deraadt  1694:        if (startup_pipe != -1) {
                   1695:                close(startup_pipe);
                   1696:                startup_pipe = -1;
                   1697:        }
1.61      markus   1698:        if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
                   1699:                channel_permit_all_opens();
1.28      millert  1700: #ifdef HAVE_LOGIN_CAP
1.49      markus   1701:        if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
1.28      millert  1702:                error("unable to get login class");
                   1703:                return;
                   1704:        }
                   1705: #endif
1.2       markus   1706:        server_loop2();
1.7       markus   1707:        if (xauthfile)
                   1708:                xauthfile_cleanup_proc(NULL);
1.1       markus   1709: }