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

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