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

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