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

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