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

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