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

1.1       markus      1: /*
                      2:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      3:  *                    All rights reserved
1.37      deraadt     4:  *
                      5:  * As far as I am concerned, the code I have written for this software
                      6:  * can be used freely for any purpose.  Any derived versions of this
                      7:  * software must be clearly marked as such, and if the derived work is
                      8:  * incompatible with the protocol description in the RFC file, it must be
                      9:  * called by a name other than "ssh" or "Secure Shell".
                     10:  *
1.2       markus     11:  * SSH2 support by Markus Friedl.
1.95      markus     12:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.37      deraadt    13:  *
                     14:  * Redistribution and use in source and binary forms, with or without
                     15:  * modification, are permitted provided that the following conditions
                     16:  * are met:
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     24:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     25:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     26:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     27:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     28:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     29:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     30:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     31:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     32:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.2       markus     33:  */
1.1       markus     34:
                     35: #include "includes.h"
1.133   ! markus     36: RCSID("$OpenBSD: session.c,v 1.132 2002/03/19 10:49:35 markus Exp $");
1.1       markus     37:
1.51      markus     38: #include "ssh.h"
                     39: #include "ssh1.h"
                     40: #include "ssh2.h"
1.1       markus     41: #include "xmalloc.h"
1.59      djm        42: #include "sshpty.h"
1.1       markus     43: #include "packet.h"
                     44: #include "buffer.h"
                     45: #include "mpaux.h"
                     46: #include "uidswap.h"
                     47: #include "compat.h"
1.78      markus     48: #include "channels.h"
1.2       markus     49: #include "bufaux.h"
                     50: #include "auth.h"
1.19      markus     51: #include "auth-options.h"
1.50      markus     52: #include "pathnames.h"
1.51      markus     53: #include "log.h"
                     54: #include "servconf.h"
1.59      djm        55: #include "sshlogin.h"
1.51      markus     56: #include "serverloop.h"
                     57: #include "canohost.h"
1.55      itojun     58: #include "session.h"
1.130     provos     59: #include "monitor_wrap.h"
1.1       markus     60:
                     61: /* func */
                     62:
                     63: Session *session_new(void);
1.94      itojun     64: void   session_set_fds(Session *, int, int, int);
1.130     provos     65: void   session_pty_cleanup(void *);
1.94      itojun     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.94      itojun     74: int    check_quietlogin(Session *, const char *);
1.1       markus     75:
1.94      itojun     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.92      markus     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.129     provos     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);
1.79      markus    121:
1.87      markus    122:        /* remove agent socket */
1.79      markus    123:        if (auth_get_socket_name())
1.80      markus    124:                auth_sock_cleanup_proc(authctxt->pw);
1.96      dugsong   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.65      markus    133: }
                    134:
1.1       markus    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.94      itojun    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.117     markus    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.96      dugsong   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.117     markus    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.116     markus    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.112     deraadt   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.86      markus    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.116     markus    199:                        packet_check_eom();
1.81      markus    200:                        success = session_setup_x11fwd(s);
                    201:                        if (!success) {
                    202:                                xfree(s->auth_proto);
                    203:                                xfree(s->auth_data);
1.84      markus    204:                                s->auth_proto = NULL;
                    205:                                s->auth_data = NULL;
1.1       markus    206:                        }
                    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.112     deraadt   236:
1.96      dugsong   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.116     markus    243:                                packet_check_eom();
1.112     deraadt   244:
1.96      dugsong   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.112     deraadt   251:
1.96      dugsong   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.112     deraadt   269:
1.96      dugsong   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.116     markus    277:                                packet_check_eom();
1.112     deraadt   278:
1.96      dugsong   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.92      markus    294:                                do_exec(s, command);
                    295:                                xfree(command);
1.1       markus    296:                        } else {
1.92      markus    297:                                do_exec(s, NULL);
1.1       markus    298:                        }
1.116     markus    299:                        packet_check_eom();
1.82      markus    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.97      markus    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.103     markus    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.90      markus    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:
1.92      markus    537:        original_command = NULL;
1.90      markus    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;
1.35      markus    546:        char hostname[MAXHOSTNAMELEN];
1.24      markus    547:        socklen_t fromlen;
                    548:        struct sockaddr_storage from;
                    549:        time_t last_login_time;
                    550:        struct passwd * pw = s->pw;
                    551:        pid_t pid = getpid();
                    552:
                    553:        /*
                    554:         * Get IP address of client. If the connection is not a socket, let
                    555:         * the address be 0.0.0.0.
                    556:         */
                    557:        memset(&from, 0, sizeof(from));
                    558:        if (packet_connection_is_on_socket()) {
                    559:                fromlen = sizeof(from);
                    560:                if (getpeername(packet_get_connection_in(),
1.112     deraadt   561:                    (struct sockaddr *) & from, &fromlen) < 0) {
1.24      markus    562:                        debug("getpeername: %.100s", strerror(errno));
                    563:                        fatal_cleanup();
                    564:                }
                    565:        }
1.25      markus    566:
1.35      markus    567:        /* Get the time and hostname when the user last logged in. */
1.69      stevesk   568:        if (options.print_lastlog) {
                    569:                hostname[0] = '\0';
                    570:                last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
                    571:                    hostname, sizeof(hostname));
                    572:        }
1.35      markus    573:
1.24      markus    574:        /* Record that there was a login on that tty from the remote host. */
1.133   ! markus    575:        if (!use_privsep)
        !           576:                record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
        !           577:                    get_remote_name_or_ip(utmp_len,
        !           578:                    options.verify_reverse_mapping),
        !           579:                    (struct sockaddr *)&from);
1.24      markus    580:
1.73      djm       581:        if (check_quietlogin(s, command))
1.24      markus    582:                return;
1.73      djm       583:
1.69      stevesk   584:        if (options.print_lastlog && last_login_time != 0) {
1.24      markus    585:                time_string = ctime(&last_login_time);
                    586:                if (strchr(time_string, '\n'))
                    587:                        *strchr(time_string, '\n') = 0;
1.40      markus    588:                if (strcmp(hostname, "") == 0)
1.24      markus    589:                        printf("Last login: %s\r\n", time_string);
                    590:                else
1.36      markus    591:                        printf("Last login: %s from %s\r\n", time_string, hostname);
1.24      markus    592:        }
1.73      djm       593:
                    594:        do_motd();
                    595: }
                    596:
                    597: /*
                    598:  * Display the message of the day.
                    599:  */
                    600: void
                    601: do_motd(void)
                    602: {
                    603:        FILE *f;
                    604:        char buf[256];
                    605:
1.24      markus    606:        if (options.print_motd) {
1.28      millert   607: #ifdef HAVE_LOGIN_CAP
                    608:                f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
                    609:                    "/etc/motd"), "r");
                    610: #else
1.24      markus    611:                f = fopen("/etc/motd", "r");
1.28      millert   612: #endif
1.24      markus    613:                if (f) {
                    614:                        while (fgets(buf, sizeof(buf), f))
                    615:                                fputs(buf, stdout);
                    616:                        fclose(f);
                    617:                }
1.2       markus    618:        }
1.73      djm       619: }
                    620:
                    621:
                    622: /*
                    623:  * Check for quiet login, either .hushlogin or command given.
                    624:  */
                    625: int
                    626: check_quietlogin(Session *s, const char *command)
                    627: {
                    628:        char buf[256];
1.96      dugsong   629:        struct passwd *pw = s->pw;
1.73      djm       630:        struct stat st;
                    631:
                    632:        /* Return 1 if .hushlogin exists or a command given. */
                    633:        if (command != NULL)
                    634:                return 1;
                    635:        snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
                    636: #ifdef HAVE_LOGIN_CAP
                    637:        if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
                    638:                return 1;
                    639: #else
                    640:        if (stat(buf, &st) >= 0)
                    641:                return 1;
                    642: #endif
                    643:        return 0;
1.1       markus    644: }
                    645:
                    646: /*
                    647:  * Sets the value of the given variable in the environment.  If the variable
                    648:  * already exists, its value is overriden.
                    649:  */
1.94      itojun    650: static void
1.45      markus    651: child_set_env(char ***envp, u_int *envsizep, const char *name,
1.112     deraadt   652:        const char *value)
1.1       markus    653: {
1.45      markus    654:        u_int i, namelen;
1.1       markus    655:        char **env;
                    656:
                    657:        /*
                    658:         * Find the slot where the value should be stored.  If the variable
                    659:         * already exists, we reuse the slot; otherwise we append a new slot
                    660:         * at the end of the array, expanding if necessary.
                    661:         */
                    662:        env = *envp;
                    663:        namelen = strlen(name);
                    664:        for (i = 0; env[i]; i++)
                    665:                if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
                    666:                        break;
                    667:        if (env[i]) {
                    668:                /* Reuse the slot. */
                    669:                xfree(env[i]);
                    670:        } else {
                    671:                /* New variable.  Expand if necessary. */
                    672:                if (i >= (*envsizep) - 1) {
                    673:                        (*envsizep) += 50;
                    674:                        env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
                    675:                }
                    676:                /* Need to set the NULL pointer at end of array beyond the new slot. */
                    677:                env[i + 1] = NULL;
                    678:        }
                    679:
                    680:        /* Allocate space and format the variable in the appropriate slot. */
                    681:        env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
                    682:        snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
                    683: }
                    684:
                    685: /*
                    686:  * Reads environment variables from the given file and adds/overrides them
                    687:  * into the environment.  If the file does not exist, this does nothing.
                    688:  * Otherwise, it must consist of empty lines, comments (line starts with '#')
                    689:  * and assignments of the form name=value.  No other forms are allowed.
                    690:  */
1.94      itojun    691: static void
1.45      markus    692: read_environment_file(char ***env, u_int *envsize,
1.112     deraadt   693:        const char *filename)
1.1       markus    694: {
                    695:        FILE *f;
                    696:        char buf[4096];
                    697:        char *cp, *value;
                    698:
                    699:        f = fopen(filename, "r");
                    700:        if (!f)
                    701:                return;
                    702:
                    703:        while (fgets(buf, sizeof(buf), f)) {
                    704:                for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
                    705:                        ;
                    706:                if (!*cp || *cp == '#' || *cp == '\n')
                    707:                        continue;
                    708:                if (strchr(cp, '\n'))
                    709:                        *strchr(cp, '\n') = '\0';
                    710:                value = strchr(cp, '=');
                    711:                if (value == NULL) {
                    712:                        fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
                    713:                        continue;
                    714:                }
1.14      deraadt   715:                /*
                    716:                 * Replace the equals sign by nul, and advance value to
                    717:                 * the value string.
                    718:                 */
1.1       markus    719:                *value = '\0';
                    720:                value++;
                    721:                child_set_env(env, envsize, cp, value);
                    722:        }
                    723:        fclose(f);
                    724: }
                    725:
1.127     markus    726: static char **
                    727: do_setup_env(Session *s, const char *shell)
1.1       markus    728: {
                    729:        char buf[256];
1.127     markus    730:        u_int i, envsize;
1.1       markus    731:        char **env;
1.127     markus    732:        struct passwd *pw = s->pw;
1.1       markus    733:
                    734:        /* Initialize the environment. */
                    735:        envsize = 100;
                    736:        env = xmalloc(envsize * sizeof(char *));
                    737:        env[0] = NULL;
                    738:
                    739:        if (!options.use_login) {
                    740:                /* Set basic environment. */
                    741:                child_set_env(&env, &envsize, "USER", pw->pw_name);
                    742:                child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
                    743:                child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1.28      millert   744: #ifdef HAVE_LOGIN_CAP
1.29      millert   745:                (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
                    746:                child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1.28      millert   747: #else
1.29      millert   748:                child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1.28      millert   749: #endif
1.1       markus    750:
                    751:                snprintf(buf, sizeof buf, "%.200s/%.50s",
                    752:                         _PATH_MAILDIR, pw->pw_name);
                    753:                child_set_env(&env, &envsize, "MAIL", buf);
                    754:
                    755:                /* Normal systems set SHELL by default. */
                    756:                child_set_env(&env, &envsize, "SHELL", shell);
                    757:        }
                    758:        if (getenv("TZ"))
                    759:                child_set_env(&env, &envsize, "TZ", getenv("TZ"));
                    760:
                    761:        /* Set custom environment options from RSA authentication. */
1.110     markus    762:        if (!options.use_login) {
                    763:                while (custom_environment) {
                    764:                        struct envstring *ce = custom_environment;
                    765:                        char *s = ce->s;
1.127     markus    766:
1.110     markus    767:                        for (i = 0; s[i] != '=' && s[i]; i++)
                    768:                                ;
                    769:                        if (s[i] == '=') {
                    770:                                s[i] = 0;
                    771:                                child_set_env(&env, &envsize, s, s + i + 1);
                    772:                        }
                    773:                        custom_environment = ce->next;
                    774:                        xfree(ce->s);
                    775:                        xfree(ce);
1.1       markus    776:                }
                    777:        }
                    778:
                    779:        snprintf(buf, sizeof buf, "%.50s %d %d",
1.127     markus    780:            get_remote_ipaddr(), get_remote_port(), get_local_port());
1.1       markus    781:        child_set_env(&env, &envsize, "SSH_CLIENT", buf);
                    782:
1.60      markus    783:        if (s->ttyfd != -1)
                    784:                child_set_env(&env, &envsize, "SSH_TTY", s->tty);
                    785:        if (s->term)
                    786:                child_set_env(&env, &envsize, "TERM", s->term);
                    787:        if (s->display)
                    788:                child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34      markus    789:        if (original_command)
                    790:                child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
                    791:                    original_command);
1.1       markus    792: #ifdef KRB4
1.96      dugsong   793:        if (s->authctxt->krb4_ticket_file)
                    794:                child_set_env(&env, &envsize, "KRBTKFILE",
1.112     deraadt   795:                    s->authctxt->krb4_ticket_file);
1.96      dugsong   796: #endif
                    797: #ifdef KRB5
                    798:        if (s->authctxt->krb5_ticket_file)
                    799:                child_set_env(&env, &envsize, "KRB5CCNAME",
1.112     deraadt   800:                    s->authctxt->krb5_ticket_file);
1.96      dugsong   801: #endif
1.1       markus    802:        if (auth_get_socket_name() != NULL)
                    803:                child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1.112     deraadt   804:                    auth_get_socket_name());
1.1       markus    805:
                    806:        /* read $HOME/.ssh/environment. */
                    807:        if (!options.use_login) {
1.14      deraadt   808:                snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
                    809:                    pw->pw_dir);
1.1       markus    810:                read_environment_file(&env, &envsize, buf);
                    811:        }
                    812:        if (debug_flag) {
                    813:                /* dump the environment */
                    814:                fprintf(stderr, "Environment:\n");
                    815:                for (i = 0; env[i]; i++)
                    816:                        fprintf(stderr, "  %.200s\n", env[i]);
                    817:        }
1.127     markus    818:        return env;
                    819: }
                    820:
                    821: /*
                    822:  * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
                    823:  * first in this order).
                    824:  */
                    825: static void
                    826: do_rc_files(Session *s, const char *shell)
                    827: {
                    828:        FILE *f = NULL;
                    829:        char cmd[1024];
                    830:        int do_xauth;
                    831:        struct stat st;
                    832:
                    833:        do_xauth =
                    834:            s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
                    835:
                    836:        /* ignore _PATH_SSH_USER_RC for subsystems */
                    837:        if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
                    838:                snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
                    839:                    shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
                    840:                if (debug_flag)
                    841:                        fprintf(stderr, "Running %s\n", cmd);
                    842:                f = popen(cmd, "w");
                    843:                if (f) {
                    844:                        if (do_xauth)
                    845:                                fprintf(f, "%s %s\n", s->auth_proto,
                    846:                                    s->auth_data);
                    847:                        pclose(f);
                    848:                } else
                    849:                        fprintf(stderr, "Could not run %s\n",
                    850:                            _PATH_SSH_USER_RC);
                    851:        } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
                    852:                if (debug_flag)
                    853:                        fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
                    854:                            _PATH_SSH_SYSTEM_RC);
                    855:                f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
                    856:                if (f) {
                    857:                        if (do_xauth)
                    858:                                fprintf(f, "%s %s\n", s->auth_proto,
                    859:                                    s->auth_data);
                    860:                        pclose(f);
                    861:                } else
                    862:                        fprintf(stderr, "Could not run %s\n",
                    863:                            _PATH_SSH_SYSTEM_RC);
                    864:        } else if (do_xauth && options.xauth_location != NULL) {
                    865:                /* Add authority data to .Xauthority if appropriate. */
                    866:                if (debug_flag) {
                    867:                        fprintf(stderr,
                    868:                            "Running %.100s add "
                    869:                            "%.100s %.100s %.100s\n",
                    870:                            options.xauth_location, s->auth_display,
                    871:                            s->auth_proto, s->auth_data);
                    872:                }
                    873:                snprintf(cmd, sizeof cmd, "%s -q -",
                    874:                    options.xauth_location);
                    875:                f = popen(cmd, "w");
                    876:                if (f) {
                    877:                        fprintf(f, "add %s %s %s\n",
                    878:                            s->auth_display, s->auth_proto,
                    879:                            s->auth_data);
                    880:                        pclose(f);
                    881:                } else {
                    882:                        fprintf(stderr, "Could not run %s\n",
                    883:                            cmd);
                    884:                }
                    885:        }
                    886: }
                    887:
                    888: static void
                    889: do_nologin(struct passwd *pw)
                    890: {
                    891:        FILE *f = NULL;
                    892:        char buf[1024];
                    893:
                    894: #ifdef HAVE_LOGIN_CAP
                    895:        if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
                    896:                f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
                    897:                    _PATH_NOLOGIN), "r");
                    898: #else
                    899:        if (pw->pw_uid)
                    900:                f = fopen(_PATH_NOLOGIN, "r");
                    901: #endif
                    902:        if (f) {
                    903:                /* /etc/nologin exists.  Print its contents and exit. */
                    904:                while (fgets(buf, sizeof(buf), f))
                    905:                        fputs(buf, stderr);
                    906:                fclose(f);
                    907:                exit(254);
                    908:        }
                    909: }
                    910:
                    911: /* Set login name, uid, gid, and groups. */
1.130     provos    912: void
1.127     markus    913: do_setusercontext(struct passwd *pw)
                    914: {
                    915:        if (getuid() == 0 || geteuid() == 0) {
                    916: #ifdef HAVE_LOGIN_CAP
                    917:                if (setusercontext(lc, pw, pw->pw_uid,
                    918:                    (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
                    919:                        perror("unable to set user context");
                    920:                        exit(1);
                    921:                }
                    922: #else
                    923:                if (setlogin(pw->pw_name) < 0)
                    924:                        error("setlogin failed: %s", strerror(errno));
                    925:                if (setgid(pw->pw_gid) < 0) {
                    926:                        perror("setgid");
                    927:                        exit(1);
                    928:                }
                    929:                /* Initialize the group list. */
                    930:                if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
                    931:                        perror("initgroups");
                    932:                        exit(1);
                    933:                }
                    934:                endgrent();
                    935:
                    936:                /* Permanently switch to the desired uid. */
                    937:                permanently_set_uid(pw);
                    938: #endif
                    939:        }
                    940:        if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
                    941:                fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
                    942: }
                    943:
1.131     markus    944: static void
1.130     provos    945: launch_login(struct passwd *pw, const char *hostname)
                    946: {
                    947:        /* Launch login(1). */
                    948:
                    949:        execl("/usr/bin/login", "login", "-h", hostname,
                    950:            "-p", "-f", "--", pw->pw_name, (char *)NULL);
                    951:
                    952:        /* Login couldn't be executed, die. */
                    953:
                    954:        perror("login");
                    955:        exit(1);
                    956: }
                    957:
1.127     markus    958: /*
                    959:  * Performs common processing for the child, such as setting up the
                    960:  * environment, closing extra file descriptors, setting the user and group
                    961:  * ids, and executing the command or shell.
                    962:  */
                    963: void
                    964: do_child(Session *s, const char *command)
                    965: {
                    966:        extern char **environ;
                    967:        char **env;
                    968:        char *argv[10];
                    969:        const char *shell, *shell0, *hostname = NULL;
                    970:        struct passwd *pw = s->pw;
                    971:        u_int i;
                    972:
                    973:        /* remove hostkey from the child's memory */
                    974:        destroy_sensitive_data();
                    975:
                    976:        /* login(1) is only called if we execute the login shell */
                    977:        if (options.use_login && command != NULL)
                    978:                options.use_login = 0;
                    979:
                    980:        /*
                    981:         * Login(1) does this as well, and it needs uid 0 for the "-h"
                    982:         * switch, so we let login(1) to this for us.
                    983:         */
                    984:        if (!options.use_login) {
                    985:                do_nologin(pw);
                    986:                do_setusercontext(pw);
                    987:        }
                    988:
                    989:        /*
                    990:         * Get the shell from the password data.  An empty shell field is
                    991:         * legal, and means /bin/sh.
                    992:         */
                    993:        shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
                    994: #ifdef HAVE_LOGIN_CAP
                    995:        shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
                    996: #endif
                    997:
                    998:        env = do_setup_env(s, shell);
                    999:
1.26      millert  1000:        /* we have to stash the hostname before we close our socket. */
                   1001:        if (options.use_login)
1.70      stevesk  1002:                hostname = get_remote_name_or_ip(utmp_len,
1.120     markus   1003:                    options.verify_reverse_mapping);
1.1       markus   1004:        /*
                   1005:         * Close the connection descriptors; note that this is the child, and
                   1006:         * the server will still have the socket open, and it is important
                   1007:         * that we do not shutdown it.  Note that the descriptors cannot be
                   1008:         * closed before building the environment, as we call
                   1009:         * get_remote_ipaddr there.
                   1010:         */
                   1011:        if (packet_get_connection_in() == packet_get_connection_out())
                   1012:                close(packet_get_connection_in());
                   1013:        else {
                   1014:                close(packet_get_connection_in());
                   1015:                close(packet_get_connection_out());
                   1016:        }
                   1017:        /*
                   1018:         * Close all descriptors related to channels.  They will still remain
                   1019:         * open in the parent.
                   1020:         */
                   1021:        /* XXX better use close-on-exec? -markus */
                   1022:        channel_close_all();
                   1023:
                   1024:        /*
                   1025:         * Close any extra file descriptors.  Note that there may still be
                   1026:         * descriptors left by system functions.  They will be closed later.
                   1027:         */
                   1028:        endpwent();
                   1029:
                   1030:        /*
                   1031:         * Close any extra open file descriptors so that we don\'t have them
                   1032:         * hanging around in clients.  Note that we want to do this after
                   1033:         * initgroups, because at least on Solaris 2.3 it leaves file
                   1034:         * descriptors open.
                   1035:         */
                   1036:        for (i = 3; i < 64; i++)
                   1037:                close(i);
                   1038:
                   1039:        /*
1.125     deraadt  1040:         * Must take new environment into use so that .ssh/rc,
                   1041:         * /etc/ssh/sshrc and xauth are run in the proper environment.
1.1       markus   1042:         */
                   1043:        environ = env;
1.102     markus   1044:
                   1045: #ifdef AFS
                   1046:        /* Try to get AFS tokens for the local cell. */
                   1047:        if (k_hasafs()) {
                   1048:                char cell[64];
1.112     deraadt  1049:
1.102     markus   1050:                if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
                   1051:                        krb_afslog(cell, 0);
1.112     deraadt  1052:
1.102     markus   1053:                krb_afslog(0, 0);
                   1054:        }
                   1055: #endif /* AFS */
1.104     markus   1056:
                   1057:        /* Change current directory to the user\'s home directory. */
                   1058:        if (chdir(pw->pw_dir) < 0) {
                   1059:                fprintf(stderr, "Could not chdir to home directory %s: %s\n",
                   1060:                    pw->pw_dir, strerror(errno));
                   1061: #ifdef HAVE_LOGIN_CAP
                   1062:                if (login_getcapbool(lc, "requirehome", 0))
                   1063:                        exit(1);
                   1064: #endif
                   1065:        }
1.1       markus   1066:
1.127     markus   1067:        if (!options.use_login)
                   1068:                do_rc_files(s, shell);
1.67      markus   1069:
                   1070:        /* restore SIGPIPE for child */
                   1071:        signal(SIGPIPE,  SIG_DFL);
                   1072:
1.127     markus   1073:        if (options.use_login) {
1.130     provos   1074:                launch_login(pw, hostname);
                   1075:                /* NEVERREACHED */
1.127     markus   1076:        }
                   1077:
                   1078:        /* Get the last component of the shell name. */
                   1079:        if ((shell0 = strrchr(shell, '/')) != NULL)
                   1080:                shell0++;
                   1081:        else
                   1082:                shell0 = shell;
                   1083:
1.1       markus   1084:        /*
                   1085:         * If we have no command, execute the shell.  In this case, the shell
                   1086:         * name to be passed in argv[0] is preceded by '-' to indicate that
                   1087:         * this is a login shell.
                   1088:         */
                   1089:        if (!command) {
1.127     markus   1090:                char argv0[256];
1.1       markus   1091:
1.127     markus   1092:                /* Start the shell.  Set initial character to '-'. */
                   1093:                argv0[0] = '-';
1.1       markus   1094:
1.127     markus   1095:                if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1.128     markus   1096:                    >= sizeof(argv0) - 1) {
1.127     markus   1097:                        errno = EINVAL;
1.1       markus   1098:                        perror(shell);
                   1099:                        exit(1);
1.127     markus   1100:                }
1.1       markus   1101:
1.127     markus   1102:                /* Execute the shell. */
                   1103:                argv[0] = argv0;
                   1104:                argv[1] = NULL;
                   1105:                execve(shell, argv, env);
                   1106:
                   1107:                /* Executing the shell failed. */
                   1108:                perror(shell);
                   1109:                exit(1);
1.1       markus   1110:        }
                   1111:        /*
                   1112:         * Execute the command using the user's shell.  This uses the -c
                   1113:         * option to execute the command.
                   1114:         */
1.127     markus   1115:        argv[0] = (char *) shell0;
1.1       markus   1116:        argv[1] = "-c";
                   1117:        argv[2] = (char *) command;
                   1118:        argv[3] = NULL;
                   1119:        execve(shell, argv, env);
                   1120:        perror(shell);
                   1121:        exit(1);
                   1122: }
                   1123:
                   1124: Session *
                   1125: session_new(void)
                   1126: {
                   1127:        int i;
                   1128:        static int did_init = 0;
                   1129:        if (!did_init) {
                   1130:                debug("session_new: init");
1.112     deraadt  1131:                for (i = 0; i < MAX_SESSIONS; i++) {
1.1       markus   1132:                        sessions[i].used = 0;
                   1133:                }
                   1134:                did_init = 1;
                   1135:        }
1.112     deraadt  1136:        for (i = 0; i < MAX_SESSIONS; i++) {
1.1       markus   1137:                Session *s = &sessions[i];
                   1138:                if (! s->used) {
1.68      djm      1139:                        memset(s, 0, sizeof(*s));
1.1       markus   1140:                        s->chanid = -1;
                   1141:                        s->ptyfd = -1;
                   1142:                        s->ttyfd = -1;
                   1143:                        s->used = 1;
1.85      markus   1144:                        s->self = i;
1.1       markus   1145:                        debug("session_new: session %d", i);
                   1146:                        return s;
                   1147:                }
                   1148:        }
                   1149:        return NULL;
                   1150: }
                   1151:
1.94      itojun   1152: static void
1.1       markus   1153: session_dump(void)
                   1154: {
                   1155:        int i;
1.112     deraadt  1156:        for (i = 0; i < MAX_SESSIONS; i++) {
1.1       markus   1157:                Session *s = &sessions[i];
                   1158:                debug("dump: used %d session %d %p channel %d pid %d",
                   1159:                    s->used,
                   1160:                    s->self,
                   1161:                    s,
                   1162:                    s->chanid,
                   1163:                    s->pid);
                   1164:        }
                   1165: }
                   1166:
1.2       markus   1167: int
1.97      markus   1168: session_open(Authctxt *authctxt, int chanid)
1.2       markus   1169: {
                   1170:        Session *s = session_new();
                   1171:        debug("session_open: channel %d", chanid);
                   1172:        if (s == NULL) {
                   1173:                error("no more sessions");
                   1174:                return 0;
                   1175:        }
1.97      markus   1176:        s->authctxt = authctxt;
                   1177:        s->pw = authctxt->pw;
1.7       markus   1178:        if (s->pw == NULL)
1.54      stevesk  1179:                fatal("no user for session %d", s->self);
1.2       markus   1180:        debug("session_open: session %d: link with channel %d", s->self, chanid);
                   1181:        s->chanid = chanid;
                   1182:        return 1;
                   1183: }
                   1184:
1.130     provos   1185: Session *
                   1186: session_by_tty(char *tty)
                   1187: {
                   1188:        int i;
                   1189:        for (i = 0; i < MAX_SESSIONS; i++) {
                   1190:                Session *s = &sessions[i];
                   1191:                if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
                   1192:                        debug("session_by_tty: session %d tty %s", i, tty);
                   1193:                        return s;
                   1194:                }
                   1195:        }
                   1196:        debug("session_by_tty: unknown tty %.100s", tty);
                   1197:        session_dump();
                   1198:        return NULL;
                   1199: }
                   1200:
1.94      itojun   1201: static Session *
1.2       markus   1202: session_by_channel(int id)
                   1203: {
                   1204:        int i;
1.112     deraadt  1205:        for (i = 0; i < MAX_SESSIONS; i++) {
1.2       markus   1206:                Session *s = &sessions[i];
                   1207:                if (s->used && s->chanid == id) {
                   1208:                        debug("session_by_channel: session %d channel %d", i, id);
                   1209:                        return s;
                   1210:                }
                   1211:        }
                   1212:        debug("session_by_channel: unknown channel %d", id);
                   1213:        session_dump();
                   1214:        return NULL;
                   1215: }
                   1216:
1.94      itojun   1217: static Session *
1.2       markus   1218: session_by_pid(pid_t pid)
                   1219: {
                   1220:        int i;
                   1221:        debug("session_by_pid: pid %d", pid);
1.112     deraadt  1222:        for (i = 0; i < MAX_SESSIONS; i++) {
1.2       markus   1223:                Session *s = &sessions[i];
                   1224:                if (s->used && s->pid == pid)
                   1225:                        return s;
                   1226:        }
                   1227:        error("session_by_pid: unknown pid %d", pid);
                   1228:        session_dump();
                   1229:        return NULL;
                   1230: }
                   1231:
1.94      itojun   1232: static int
1.2       markus   1233: session_window_change_req(Session *s)
                   1234: {
                   1235:        s->col = packet_get_int();
                   1236:        s->row = packet_get_int();
                   1237:        s->xpixel = packet_get_int();
                   1238:        s->ypixel = packet_get_int();
1.116     markus   1239:        packet_check_eom();
1.2       markus   1240:        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
                   1241:        return 1;
                   1242: }
                   1243:
1.94      itojun   1244: static int
1.2       markus   1245: session_pty_req(Session *s)
                   1246: {
1.45      markus   1247:        u_int len;
1.72      stevesk  1248:        int n_bytes;
1.132     markus   1249:
1.86      markus   1250:        if (no_pty_flag) {
                   1251:                debug("Allocating a pty not permitted for this authentication.");
1.19      markus   1252:                return 0;
1.86      markus   1253:        }
                   1254:        if (s->ttyfd != -1) {
                   1255:                packet_disconnect("Protocol error: you already have a pty.");
1.3       markus   1256:                return 0;
1.86      markus   1257:        }
                   1258:
1.2       markus   1259:        s->term = packet_get_string(&len);
1.86      markus   1260:
                   1261:        if (compat20) {
                   1262:                s->col = packet_get_int();
                   1263:                s->row = packet_get_int();
                   1264:        } else {
                   1265:                s->row = packet_get_int();
                   1266:                s->col = packet_get_int();
                   1267:        }
1.2       markus   1268:        s->xpixel = packet_get_int();
                   1269:        s->ypixel = packet_get_int();
                   1270:
                   1271:        if (strcmp(s->term, "") == 0) {
                   1272:                xfree(s->term);
                   1273:                s->term = NULL;
                   1274:        }
1.86      markus   1275:
1.2       markus   1276:        /* Allocate a pty and open it. */
1.86      markus   1277:        debug("Allocating pty.");
1.130     provos   1278:        if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
1.86      markus   1279:                if (s->term)
                   1280:                        xfree(s->term);
1.2       markus   1281:                s->term = NULL;
                   1282:                s->ptyfd = -1;
                   1283:                s->ttyfd = -1;
                   1284:                error("session_pty_req: session %d alloc failed", s->self);
1.3       markus   1285:                return 0;
1.2       markus   1286:        }
                   1287:        debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1.86      markus   1288:
                   1289:        /* for SSH1 the tty modes length is not given */
                   1290:        if (!compat20)
                   1291:                n_bytes = packet_remaining();
                   1292:        tty_parse_modes(s->ttyfd, &n_bytes);
                   1293:
1.2       markus   1294:        /*
                   1295:         * Add a cleanup function to clear the utmp entry and record logout
                   1296:         * time in case we call fatal() (e.g., the connection gets closed).
                   1297:         */
1.85      markus   1298:        fatal_add_cleanup(session_pty_cleanup, (void *)s);
1.130     provos   1299:        if (!use_privsep)
                   1300:                pty_setowner(s->pw, s->tty);
1.86      markus   1301:
                   1302:        /* Set window size from the packet. */
1.2       markus   1303:        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
                   1304:
1.116     markus   1305:        packet_check_eom();
1.9       markus   1306:        session_proctitle(s);
1.2       markus   1307:        return 1;
                   1308: }
                   1309:
1.94      itojun   1310: static int
1.7       markus   1311: session_subsystem_req(Session *s)
                   1312: {
1.105     markus   1313:        struct stat st;
1.45      markus   1314:        u_int len;
1.7       markus   1315:        int success = 0;
1.105     markus   1316:        char *cmd, *subsys = packet_get_string(&len);
1.18      jakob    1317:        int i;
1.7       markus   1318:
1.116     markus   1319:        packet_check_eom();
1.121     stevesk  1320:        log("subsystem request for %.100s", subsys);
1.18      jakob    1321:
                   1322:        for (i = 0; i < options.num_subsystems; i++) {
1.105     markus   1323:                if (strcmp(subsys, options.subsystem_name[i]) == 0) {
                   1324:                        cmd = options.subsystem_command[i];
                   1325:                        if (stat(cmd, &st) < 0) {
                   1326:                                error("subsystem: cannot stat %s: %s", cmd,
                   1327:                                    strerror(errno));
                   1328:                                break;
                   1329:                        }
                   1330:                        debug("subsystem: exec() %s", cmd);
1.64      markus   1331:                        s->is_subsystem = 1;
1.105     markus   1332:                        do_exec(s, cmd);
1.18      jakob    1333:                        success = 1;
1.122     markus   1334:                        break;
1.18      jakob    1335:                }
                   1336:        }
                   1337:
                   1338:        if (!success)
1.121     stevesk  1339:                log("subsystem request for %.100s failed, subsystem not found",
1.105     markus   1340:                    subsys);
1.7       markus   1341:
                   1342:        xfree(subsys);
                   1343:        return success;
                   1344: }
                   1345:
1.94      itojun   1346: static int
1.7       markus   1347: session_x11_req(Session *s)
                   1348: {
1.81      markus   1349:        int success;
1.7       markus   1350:
                   1351:        s->single_connection = packet_get_char();
                   1352:        s->auth_proto = packet_get_string(NULL);
                   1353:        s->auth_data = packet_get_string(NULL);
                   1354:        s->screen = packet_get_int();
1.116     markus   1355:        packet_check_eom();
1.7       markus   1356:
1.81      markus   1357:        success = session_setup_x11fwd(s);
                   1358:        if (!success) {
1.7       markus   1359:                xfree(s->auth_proto);
                   1360:                xfree(s->auth_data);
1.84      markus   1361:                s->auth_proto = NULL;
                   1362:                s->auth_data = NULL;
1.7       markus   1363:        }
1.81      markus   1364:        return success;
1.7       markus   1365: }
                   1366:
1.94      itojun   1367: static int
1.19      markus   1368: session_shell_req(Session *s)
                   1369: {
1.116     markus   1370:        packet_check_eom();
1.90      markus   1371:        do_exec(s, NULL);
1.19      markus   1372:        return 1;
                   1373: }
                   1374:
1.94      itojun   1375: static int
1.19      markus   1376: session_exec_req(Session *s)
                   1377: {
1.45      markus   1378:        u_int len;
1.19      markus   1379:        char *command = packet_get_string(&len);
1.116     markus   1380:        packet_check_eom();
1.90      markus   1381:        do_exec(s, command);
1.92      markus   1382:        xfree(command);
1.19      markus   1383:        return 1;
                   1384: }
                   1385:
1.94      itojun   1386: static int
1.43      markus   1387: session_auth_agent_req(Session *s)
                   1388: {
                   1389:        static int called = 0;
1.116     markus   1390:        packet_check_eom();
1.44      markus   1391:        if (no_agent_forwarding_flag) {
                   1392:                debug("session_auth_agent_req: no_agent_forwarding_flag");
                   1393:                return 0;
                   1394:        }
1.43      markus   1395:        if (called) {
                   1396:                return 0;
                   1397:        } else {
                   1398:                called = 1;
                   1399:                return auth_input_request_forwarding(s->pw);
                   1400:        }
                   1401: }
                   1402:
1.123     markus   1403: int
                   1404: session_input_channel_req(Channel *c, const char *rtype)
1.2       markus   1405: {
                   1406:        int success = 0;
                   1407:        Session *s;
                   1408:
1.123     markus   1409:        if ((s = session_by_channel(c->self)) == NULL) {
                   1410:                log("session_input_channel_req: no session %d req %.100s",
                   1411:                    c->self, rtype);
                   1412:                return 0;
                   1413:        }
                   1414:        debug("session_input_channel_req: session %d req %s", s->self, rtype);
1.2       markus   1415:
                   1416:        /*
1.64      markus   1417:         * a session is in LARVAL state until a shell, a command
                   1418:         * or a subsystem is executed
1.2       markus   1419:         */
                   1420:        if (c->type == SSH_CHANNEL_LARVAL) {
                   1421:                if (strcmp(rtype, "shell") == 0) {
1.19      markus   1422:                        success = session_shell_req(s);
1.2       markus   1423:                } else if (strcmp(rtype, "exec") == 0) {
1.19      markus   1424:                        success = session_exec_req(s);
1.2       markus   1425:                } else if (strcmp(rtype, "pty-req") == 0) {
1.3       markus   1426:                        success =  session_pty_req(s);
1.7       markus   1427:                } else if (strcmp(rtype, "x11-req") == 0) {
                   1428:                        success = session_x11_req(s);
1.43      markus   1429:                } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
                   1430:                        success = session_auth_agent_req(s);
1.7       markus   1431:                } else if (strcmp(rtype, "subsystem") == 0) {
                   1432:                        success = session_subsystem_req(s);
1.2       markus   1433:                }
                   1434:        }
                   1435:        if (strcmp(rtype, "window-change") == 0) {
                   1436:                success = session_window_change_req(s);
                   1437:        }
1.123     markus   1438:        return success;
1.2       markus   1439: }
                   1440:
                   1441: void
                   1442: session_set_fds(Session *s, int fdin, int fdout, int fderr)
                   1443: {
                   1444:        if (!compat20)
                   1445:                fatal("session_set_fds: called for proto != 2.0");
                   1446:        /*
                   1447:         * now that have a child and a pipe to the child,
                   1448:         * we can activate our channel and register the fd's
                   1449:         */
                   1450:        if (s->chanid == -1)
                   1451:                fatal("no channel for session %d", s->self);
                   1452:        channel_set_fds(s->chanid,
                   1453:            fdout, fdin, fderr,
1.42      markus   1454:            fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1.126     markus   1455:            1,
                   1456:            CHAN_SES_WINDOW_DEFAULT);
1.2       markus   1457: }
                   1458:
1.85      markus   1459: /*
                   1460:  * Function to perform pty cleanup. Also called if we get aborted abnormally
                   1461:  * (e.g., due to a dropped connection).
                   1462:  */
1.130     provos   1463: void
                   1464: session_pty_cleanup2(void *session)
1.1       markus   1465: {
1.85      markus   1466:        Session *s = session;
                   1467:
                   1468:        if (s == NULL) {
                   1469:                error("session_pty_cleanup: no session");
                   1470:                return;
                   1471:        }
                   1472:        if (s->ttyfd == -1)
1.1       markus   1473:                return;
                   1474:
1.54      stevesk  1475:        debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1       markus   1476:
                   1477:        /* Record that the user has logged out. */
1.85      markus   1478:        if (s->pid != 0)
                   1479:                record_logout(s->pid, s->tty);
1.1       markus   1480:
                   1481:        /* Release the pseudo-tty. */
1.130     provos   1482:        if (getuid() == 0)
                   1483:                pty_release(s->tty);
1.1       markus   1484:
                   1485:        /*
                   1486:         * Close the server side of the socket pairs.  We must do this after
                   1487:         * the pty cleanup, so that another process doesn't get this pty
                   1488:         * while we're still cleaning up.
                   1489:         */
                   1490:        if (close(s->ptymaster) < 0)
1.130     provos   1491:                error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
1.108     markus   1492:
                   1493:        /* unlink pty from session */
                   1494:        s->ttyfd = -1;
1.2       markus   1495: }
                   1496:
1.130     provos   1497: void
                   1498: session_pty_cleanup(void *session)
                   1499: {
                   1500:        PRIVSEP(session_pty_cleanup2(session));
                   1501: }
                   1502:
1.94      itojun   1503: static void
1.2       markus   1504: session_exit_message(Session *s, int status)
                   1505: {
                   1506:        Channel *c;
1.124     markus   1507:
                   1508:        if ((c = channel_lookup(s->chanid)) == NULL)
1.85      markus   1509:                fatal("session_exit_message: session %d: no channel %d",
1.2       markus   1510:                    s->self, s->chanid);
                   1511:        debug("session_exit_message: session %d channel %d pid %d",
                   1512:            s->self, s->chanid, s->pid);
                   1513:
                   1514:        if (WIFEXITED(status)) {
1.124     markus   1515:                channel_request_start(s->chanid, "exit-status", 0);
1.2       markus   1516:                packet_put_int(WEXITSTATUS(status));
                   1517:                packet_send();
                   1518:        } else if (WIFSIGNALED(status)) {
1.124     markus   1519:                channel_request_start(s->chanid, "exit-signal", 0);
1.2       markus   1520:                packet_put_int(WTERMSIG(status));
                   1521:                packet_put_char(WCOREDUMP(status));
                   1522:                packet_put_cstring("");
                   1523:                packet_put_cstring("");
                   1524:                packet_send();
                   1525:        } else {
                   1526:                /* Some weird exit cause.  Just exit. */
                   1527:                packet_disconnect("wait returned status %04x.", status);
                   1528:        }
                   1529:
                   1530:        /* disconnect channel */
                   1531:        debug("session_exit_message: release channel %d", s->chanid);
                   1532:        channel_cancel_cleanup(s->chanid);
1.5       markus   1533:        /*
                   1534:         * emulate a write failure with 'chan_write_failed', nobody will be
                   1535:         * interested in data we write.
                   1536:         * Note that we must not call 'chan_read_failed', since there could
                   1537:         * be some more data waiting in the pipe.
                   1538:         */
1.8       markus   1539:        if (c->ostate != CHAN_OUTPUT_CLOSED)
                   1540:                chan_write_failed(c);
1.2       markus   1541:        s->chanid = -1;
                   1542: }
                   1543:
1.130     provos   1544: void
1.85      markus   1545: session_close(Session *s)
1.2       markus   1546: {
1.85      markus   1547:        debug("session_close: session %d pid %d", s->self, s->pid);
                   1548:        if (s->ttyfd != -1) {
                   1549:                fatal_remove_cleanup(session_pty_cleanup, (void *)s);
                   1550:                session_pty_cleanup(s);
                   1551:        }
1.2       markus   1552:        if (s->term)
                   1553:                xfree(s->term);
                   1554:        if (s->display)
                   1555:                xfree(s->display);
1.118     stevesk  1556:        if (s->auth_display)
                   1557:                xfree(s->auth_display);
1.2       markus   1558:        if (s->auth_data)
                   1559:                xfree(s->auth_data);
                   1560:        if (s->auth_proto)
                   1561:                xfree(s->auth_proto);
                   1562:        s->used = 0;
1.9       markus   1563:        session_proctitle(s);
1.2       markus   1564: }
                   1565:
                   1566: void
                   1567: session_close_by_pid(pid_t pid, int status)
                   1568: {
                   1569:        Session *s = session_by_pid(pid);
                   1570:        if (s == NULL) {
1.89      markus   1571:                debug("session_close_by_pid: no session for pid %d", pid);
1.2       markus   1572:                return;
                   1573:        }
                   1574:        if (s->chanid != -1)
                   1575:                session_exit_message(s, status);
                   1576:        session_close(s);
1.98      markus   1577: }
                   1578:
1.2       markus   1579: /*
                   1580:  * this is called when a channel dies before
                   1581:  * the session 'child' itself dies
                   1582:  */
                   1583: void
                   1584: session_close_by_channel(int id, void *arg)
                   1585: {
                   1586:        Session *s = session_by_channel(id);
                   1587:        if (s == NULL) {
1.107     markus   1588:                debug("session_close_by_channel: no session for id %d", id);
1.2       markus   1589:                return;
                   1590:        }
1.107     markus   1591:        debug("session_close_by_channel: channel %d child %d", id, s->pid);
                   1592:        if (s->pid != 0) {
                   1593:                debug("session_close_by_channel: channel %d: has child", id);
1.108     markus   1594:                /*
                   1595:                 * delay detach of session, but release pty, since
                   1596:                 * the fd's to the child are already closed
                   1597:                 */
                   1598:                if (s->ttyfd != -1) {
                   1599:                        fatal_remove_cleanup(session_pty_cleanup, (void *)s);
                   1600:                        session_pty_cleanup(s);
                   1601:                }
1.107     markus   1602:                return;
                   1603:        }
                   1604:        /* detach by removing callback */
1.2       markus   1605:        channel_cancel_cleanup(s->chanid);
                   1606:        s->chanid = -1;
1.106     markus   1607:        session_close(s);
                   1608: }
                   1609:
                   1610: void
1.130     provos   1611: session_destroy_all(void (*closefunc)(Session *))
1.106     markus   1612: {
                   1613:        int i;
1.112     deraadt  1614:        for (i = 0; i < MAX_SESSIONS; i++) {
1.106     markus   1615:                Session *s = &sessions[i];
1.130     provos   1616:                if (s->used) {
                   1617:                        if (closefunc != NULL)
                   1618:                                closefunc(s);
                   1619:                        else
                   1620:                                session_close(s);
                   1621:                }
1.2       markus   1622:        }
1.9       markus   1623: }
                   1624:
1.94      itojun   1625: static char *
1.9       markus   1626: session_tty_list(void)
                   1627: {
                   1628:        static char buf[1024];
                   1629:        int i;
                   1630:        buf[0] = '\0';
1.112     deraadt  1631:        for (i = 0; i < MAX_SESSIONS; i++) {
1.9       markus   1632:                Session *s = &sessions[i];
                   1633:                if (s->used && s->ttyfd != -1) {
                   1634:                        if (buf[0] != '\0')
                   1635:                                strlcat(buf, ",", sizeof buf);
                   1636:                        strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
                   1637:                }
                   1638:        }
                   1639:        if (buf[0] == '\0')
                   1640:                strlcpy(buf, "notty", sizeof buf);
                   1641:        return buf;
                   1642: }
                   1643:
                   1644: void
                   1645: session_proctitle(Session *s)
                   1646: {
                   1647:        if (s->pw == NULL)
                   1648:                error("no user for session %d", s->self);
                   1649:        else
                   1650:                setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.81      markus   1651: }
                   1652:
                   1653: int
                   1654: session_setup_x11fwd(Session *s)
                   1655: {
                   1656:        struct stat st;
1.109     stevesk  1657:        char display[512], auth_display[512];
                   1658:        char hostname[MAXHOSTNAMELEN];
1.81      markus   1659:
                   1660:        if (no_x11_forwarding_flag) {
                   1661:                packet_send_debug("X11 forwarding disabled in user configuration file.");
                   1662:                return 0;
                   1663:        }
                   1664:        if (!options.x11_forwarding) {
                   1665:                debug("X11 forwarding disabled in server configuration file.");
                   1666:                return 0;
                   1667:        }
                   1668:        if (!options.xauth_location ||
                   1669:            (stat(options.xauth_location, &st) == -1)) {
                   1670:                packet_send_debug("No xauth program; cannot forward with spoofing.");
1.91      markus   1671:                return 0;
                   1672:        }
                   1673:        if (options.use_login) {
                   1674:                packet_send_debug("X11 forwarding disabled; "
                   1675:                    "not compatible with UseLogin=yes.");
1.81      markus   1676:                return 0;
                   1677:        }
1.87      markus   1678:        if (s->display != NULL) {
1.81      markus   1679:                debug("X11 display already set.");
                   1680:                return 0;
                   1681:        }
1.109     stevesk  1682:        s->display_number = x11_create_display_inet(options.x11_display_offset,
1.119     stevesk  1683:            options.x11_use_localhost, s->single_connection);
1.109     stevesk  1684:        if (s->display_number == -1) {
1.87      markus   1685:                debug("x11_create_display_inet failed.");
1.81      markus   1686:                return 0;
                   1687:        }
1.109     stevesk  1688:
                   1689:        /* Set up a suitable value for the DISPLAY variable. */
                   1690:        if (gethostname(hostname, sizeof(hostname)) < 0)
                   1691:                fatal("gethostname: %.100s", strerror(errno));
                   1692:        /*
                   1693:         * auth_display must be used as the displayname when the
                   1694:         * authorization entry is added with xauth(1).  This will be
                   1695:         * different than the DISPLAY string for localhost displays.
                   1696:         */
1.119     stevesk  1697:        if (options.x11_use_localhost) {
1.109     stevesk  1698:                snprintf(display, sizeof display, "localhost:%d.%d",
                   1699:                    s->display_number, s->screen);
1.118     stevesk  1700:                snprintf(auth_display, sizeof auth_display, "unix:%d.%d",
                   1701:                    s->display_number, s->screen);
1.109     stevesk  1702:                s->display = xstrdup(display);
1.118     stevesk  1703:                s->auth_display = xstrdup(auth_display);
1.109     stevesk  1704:        } else {
                   1705:                snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
                   1706:                    s->display_number, s->screen);
                   1707:                s->display = xstrdup(display);
1.118     stevesk  1708:                s->auth_display = xstrdup(display);
1.109     stevesk  1709:        }
                   1710:
1.81      markus   1711:        return 1;
1.2       markus   1712: }
                   1713:
1.94      itojun   1714: static void
1.49      markus   1715: do_authenticated2(Authctxt *authctxt)
1.2       markus   1716: {
1.97      markus   1717:        server_loop2(authctxt);
1.1       markus   1718: }