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

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