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

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