Home | History | Annotate | Line # | Download | only in dist
session.c revision 1.44
      1  1.42       kim /*	$NetBSD: session.c,v 1.44 2025/10/11 15:45:07 christos Exp $	*/
      2  1.44  christos /* $OpenBSD: session.c,v 1.344 2025/09/25 02:15:39 jsg Exp $ */
      3  1.38  christos 
      4   1.1  christos /*
      5   1.1  christos  * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
      6   1.1  christos  *                    All rights reserved
      7   1.1  christos  *
      8   1.1  christos  * As far as I am concerned, the code I have written for this software
      9   1.1  christos  * can be used freely for any purpose.  Any derived versions of this
     10   1.1  christos  * software must be clearly marked as such, and if the derived work is
     11   1.1  christos  * incompatible with the protocol description in the RFC file, it must be
     12   1.1  christos  * called by a name other than "ssh" or "Secure Shell".
     13   1.1  christos  *
     14   1.1  christos  * SSH2 support by Markus Friedl.
     15   1.1  christos  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
     16   1.1  christos  *
     17   1.1  christos  * Redistribution and use in source and binary forms, with or without
     18   1.1  christos  * modification, are permitted provided that the following conditions
     19   1.1  christos  * are met:
     20   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     21   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     22   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     23   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     24   1.1  christos  *    documentation and/or other materials provided with the distribution.
     25   1.1  christos  *
     26   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36   1.1  christos  */
     37   1.1  christos 
     38   1.2  christos #include "includes.h"
     39  1.42       kim __RCSID("$NetBSD: session.c,v 1.44 2025/10/11 15:45:07 christos Exp $");
     40   1.1  christos #include <sys/types.h>
     41   1.1  christos #include <sys/wait.h>
     42   1.1  christos #include <sys/un.h>
     43   1.1  christos #include <sys/stat.h>
     44   1.1  christos #include <sys/socket.h>
     45   1.1  christos #include <sys/queue.h>
     46   1.1  christos 
     47  1.20  christos #include <ctype.h>
     48   1.1  christos #include <errno.h>
     49   1.4      adam #include <fcntl.h>
     50   1.1  christos #include <grp.h>
     51   1.1  christos #include <login_cap.h>
     52  1.13  christos #include <netdb.h>
     53   1.1  christos #include <paths.h>
     54   1.1  christos #include <pwd.h>
     55   1.1  christos #include <signal.h>
     56   1.1  christos #include <stdio.h>
     57   1.1  christos #include <stdlib.h>
     58   1.1  christos #include <string.h>
     59  1.30  christos #include <stdarg.h>
     60   1.1  christos #include <unistd.h>
     61  1.14  christos #include <limits.h>
     62   1.1  christos 
     63   1.1  christos #include "xmalloc.h"
     64   1.1  christos #include "ssh.h"
     65   1.1  christos #include "ssh2.h"
     66   1.1  christos #include "sshpty.h"
     67   1.1  christos #include "packet.h"
     68  1.26  christos #include "sshbuf.h"
     69  1.26  christos #include "ssherr.h"
     70   1.1  christos #include "match.h"
     71   1.1  christos #include "uidswap.h"
     72   1.1  christos #include "channels.h"
     73  1.26  christos #include "sshkey.h"
     74   1.1  christos #include "cipher.h"
     75   1.1  christos #include "kex.h"
     76   1.1  christos #include "hostfile.h"
     77   1.1  christos #include "auth.h"
     78   1.1  christos #include "auth-options.h"
     79  1.12  christos #include "authfd.h"
     80   1.1  christos #include "pathnames.h"
     81   1.1  christos #include "log.h"
     82  1.13  christos #include "misc.h"
     83   1.1  christos #include "servconf.h"
     84   1.1  christos #include "sshlogin.h"
     85   1.1  christos #include "serverloop.h"
     86   1.1  christos #include "canohost.h"
     87   1.1  christos #include "session.h"
     88   1.1  christos #ifdef GSSAPI
     89   1.1  christos #include "ssh-gss.h"
     90   1.1  christos #endif
     91   1.1  christos #include "monitor_wrap.h"
     92   1.1  christos #include "sftp.h"
     93  1.23  christos #include "atomicio.h"
     94   1.1  christos 
     95   1.1  christos #ifdef KRB5
     96   1.6     elric #include <krb5/kafs.h>
     97   1.1  christos #endif
     98   1.1  christos 
     99   1.1  christos #define IS_INTERNAL_SFTP(c) \
    100   1.1  christos 	(!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
    101   1.1  christos 	 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
    102   1.1  christos 	  c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
    103   1.1  christos 	  c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
    104   1.1  christos 
    105   1.1  christos /* func */
    106   1.1  christos 
    107   1.1  christos Session *session_new(void);
    108  1.23  christos void	session_set_fds(struct ssh *, Session *, int, int, int, int, int);
    109   1.1  christos void	session_pty_cleanup(Session *);
    110   1.1  christos void	session_proctitle(Session *);
    111  1.23  christos int	session_setup_x11fwd(struct ssh *, Session *);
    112  1.23  christos int	do_exec_pty(struct ssh *, Session *, const char *);
    113  1.23  christos int	do_exec_no_pty(struct ssh *, Session *, const char *);
    114  1.23  christos int	do_exec(struct ssh *, Session *, const char *);
    115  1.23  christos void	do_login(struct ssh *, Session *, const char *);
    116  1.23  christos __dead void	do_child(struct ssh *, Session *, const char *);
    117   1.1  christos void	do_motd(void);
    118   1.1  christos int	check_quietlogin(Session *, const char *);
    119   1.1  christos 
    120  1.23  christos static void do_authenticated2(struct ssh *, Authctxt *);
    121   1.1  christos 
    122  1.23  christos static int session_pty_req(struct ssh *, Session *);
    123   1.1  christos 
    124   1.1  christos /* import */
    125   1.1  christos extern ServerOptions options;
    126   1.1  christos extern char *__progname;
    127   1.1  christos extern int debug_flag;
    128  1.26  christos extern struct sshbuf *loginmsg;
    129  1.24  christos extern struct sshauthopt *auth_opts;
    130  1.28  christos extern char *tun_fwd_ifnames; /* serverloop.c */
    131   1.1  christos 
    132   1.1  christos /* original command from peer. */
    133   1.1  christos const char *original_command = NULL;
    134   1.1  christos 
    135   1.1  christos /* data */
    136   1.1  christos static int sessions_first_unused = -1;
    137   1.1  christos static int sessions_nalloc = 0;
    138   1.1  christos static Session *sessions = NULL;
    139   1.1  christos 
    140   1.4      adam #define SUBSYSTEM_NONE			0
    141   1.4      adam #define SUBSYSTEM_EXT			1
    142   1.4      adam #define SUBSYSTEM_INT_SFTP		2
    143   1.4      adam #define SUBSYSTEM_INT_SFTP_ERROR	3
    144   1.1  christos 
    145   1.2  christos #ifdef HAVE_LOGIN_CAP
    146   1.1  christos login_cap_t *lc;
    147   1.2  christos #endif
    148   1.1  christos 
    149   1.1  christos static int is_child = 0;
    150  1.18  christos static int in_chroot = 0;
    151   1.1  christos 
    152  1.23  christos /* File containing userauth info, if ExposeAuthInfo set */
    153  1.23  christos static char *auth_info_file = NULL;
    154  1.23  christos 
    155   1.1  christos /* Name and directory of socket for authentication agent forwarding. */
    156   1.1  christos static char *auth_sock_name = NULL;
    157   1.1  christos 
    158   1.1  christos /* removes the agent forwarding socket */
    159   1.1  christos 
    160   1.1  christos static void
    161   1.1  christos auth_sock_cleanup_proc(struct passwd *pw)
    162   1.1  christos {
    163   1.1  christos 	if (auth_sock_name != NULL) {
    164   1.1  christos 		temporarily_use_uid(pw);
    165   1.1  christos 		unlink(auth_sock_name);
    166   1.1  christos 		auth_sock_name = NULL;
    167   1.1  christos 		restore_uid();
    168   1.1  christos 	}
    169   1.1  christos }
    170   1.1  christos 
    171   1.1  christos static int
    172  1.23  christos auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw)
    173   1.1  christos {
    174   1.1  christos 	Channel *nc;
    175   1.1  christos 	int sock = -1;
    176   1.1  christos 
    177   1.1  christos 	if (auth_sock_name != NULL) {
    178   1.1  christos 		error("authentication forwarding requested twice.");
    179   1.1  christos 		return 0;
    180   1.1  christos 	}
    181   1.1  christos 
    182   1.1  christos 	/* Temporarily drop privileged uid for mkdir/bind. */
    183   1.1  christos 	temporarily_use_uid(pw);
    184   1.1  christos 
    185  1.44  christos 	if (agent_listener(pw->pw_dir, "sshd", &sock, &auth_sock_name) != 0) {
    186  1.44  christos 		/* a more detailed error is already logged */
    187  1.28  christos 		ssh_packet_send_debug(ssh, "Agent forwarding disabled: "
    188  1.44  christos 		    "couldn't create listener socket");
    189   1.1  christos 		restore_uid();
    190   1.1  christos 		goto authsock_err;
    191   1.1  christos 	}
    192   1.1  christos 	restore_uid();
    193   1.1  christos 
    194   1.1  christos 	/* Allocate a channel for the authentication agent socket. */
    195  1.37  christos 	nc = channel_new(ssh, "auth-listener",
    196   1.1  christos 	    SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
    197   1.1  christos 	    CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
    198   1.1  christos 	    0, "auth socket", 1);
    199   1.1  christos 	nc->path = xstrdup(auth_sock_name);
    200   1.1  christos 	return 1;
    201   1.1  christos 
    202   1.1  christos  authsock_err:
    203  1.12  christos 	free(auth_sock_name);
    204   1.1  christos 	if (sock != -1)
    205   1.1  christos 		close(sock);
    206   1.1  christos 	auth_sock_name = NULL;
    207   1.1  christos 	return 0;
    208   1.1  christos }
    209   1.1  christos 
    210   1.1  christos static void
    211   1.1  christos display_loginmsg(void)
    212   1.1  christos {
    213  1.26  christos 	int r;
    214  1.26  christos 
    215  1.26  christos 	if (sshbuf_len(loginmsg) == 0)
    216  1.26  christos 		return;
    217  1.26  christos 	if ((r = sshbuf_put_u8(loginmsg, 0)) != 0)
    218  1.33  christos 		fatal_fr(r, "sshbuf_put_u8");
    219  1.26  christos 	printf("%s", (const char *)sshbuf_ptr(loginmsg));
    220  1.26  christos 	sshbuf_reset(loginmsg);
    221   1.1  christos }
    222   1.1  christos 
    223  1.23  christos static void
    224  1.23  christos prepare_auth_info_file(struct passwd *pw, struct sshbuf *info)
    225  1.23  christos {
    226  1.23  christos 	int fd = -1, success = 0;
    227  1.23  christos 
    228  1.23  christos 	if (!options.expose_userauth_info || info == NULL)
    229  1.23  christos 		return;
    230  1.23  christos 
    231  1.23  christos 	temporarily_use_uid(pw);
    232  1.23  christos 	auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX");
    233  1.23  christos 	if ((fd = mkstemp(auth_info_file)) == -1) {
    234  1.33  christos 		error_f("mkstemp: %s", strerror(errno));
    235  1.23  christos 		goto out;
    236  1.23  christos 	}
    237  1.23  christos 	if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info),
    238  1.23  christos 	    sshbuf_len(info)) != sshbuf_len(info)) {
    239  1.33  christos 		error_f("write: %s", strerror(errno));
    240  1.23  christos 		goto out;
    241  1.23  christos 	}
    242  1.23  christos 	if (close(fd) != 0) {
    243  1.33  christos 		error_f("close: %s", strerror(errno));
    244  1.23  christos 		goto out;
    245  1.23  christos 	}
    246  1.23  christos 	success = 1;
    247  1.23  christos  out:
    248  1.23  christos 	if (!success) {
    249  1.23  christos 		if (fd != -1)
    250  1.23  christos 			close(fd);
    251  1.23  christos 		free(auth_info_file);
    252  1.23  christos 		auth_info_file = NULL;
    253  1.23  christos 	}
    254  1.23  christos 	restore_uid();
    255  1.23  christos }
    256  1.23  christos 
    257  1.24  christos static void
    258  1.26  christos set_fwdpermit_from_authopts(struct ssh *ssh, const struct sshauthopt *opts)
    259  1.24  christos {
    260  1.24  christos 	char *tmp, *cp, *host;
    261  1.24  christos 	int port;
    262  1.24  christos 	size_t i;
    263  1.24  christos 
    264  1.26  christos 	if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0) {
    265  1.26  christos 		channel_clear_permission(ssh, FORWARD_USER, FORWARD_LOCAL);
    266  1.26  christos 		for (i = 0; i < auth_opts->npermitopen; i++) {
    267  1.26  christos 			tmp = cp = xstrdup(auth_opts->permitopen[i]);
    268  1.26  christos 			/* This shouldn't fail as it has already been checked */
    269  1.36  christos 			if ((host = hpdelim2(&cp, NULL)) == NULL)
    270  1.33  christos 				fatal_f("internal error: hpdelim");
    271  1.26  christos 			host = cleanhostname(host);
    272  1.26  christos 			if (cp == NULL || (port = permitopen_port(cp)) < 0)
    273  1.33  christos 				fatal_f("internal error: permitopen port");
    274  1.26  christos 			channel_add_permission(ssh,
    275  1.26  christos 			    FORWARD_USER, FORWARD_LOCAL, host, port);
    276  1.26  christos 			free(tmp);
    277  1.26  christos 		}
    278  1.26  christos 	}
    279  1.26  christos 	if ((options.allow_tcp_forwarding & FORWARD_REMOTE) != 0) {
    280  1.26  christos 		channel_clear_permission(ssh, FORWARD_USER, FORWARD_REMOTE);
    281  1.26  christos 		for (i = 0; i < auth_opts->npermitlisten; i++) {
    282  1.26  christos 			tmp = cp = xstrdup(auth_opts->permitlisten[i]);
    283  1.26  christos 			/* This shouldn't fail as it has already been checked */
    284  1.26  christos 			if ((host = hpdelim(&cp)) == NULL)
    285  1.33  christos 				fatal_f("internal error: hpdelim");
    286  1.26  christos 			host = cleanhostname(host);
    287  1.26  christos 			if (cp == NULL || (port = permitopen_port(cp)) < 0)
    288  1.33  christos 				fatal_f("internal error: permitlisten port");
    289  1.26  christos 			channel_add_permission(ssh,
    290  1.26  christos 			    FORWARD_USER, FORWARD_REMOTE, host, port);
    291  1.26  christos 			free(tmp);
    292  1.26  christos 		}
    293  1.24  christos 	}
    294  1.24  christos }
    295  1.24  christos 
    296   1.1  christos void
    297  1.23  christos do_authenticated(struct ssh *ssh, Authctxt *authctxt)
    298   1.1  christos {
    299   1.1  christos 	setproctitle("%s", authctxt->pw->pw_name);
    300   1.1  christos 
    301  1.24  christos 	auth_log_authopts("active", auth_opts, 0);
    302  1.24  christos 
    303   1.1  christos 	/* setup the channel layer */
    304  1.13  christos 	/* XXX - streamlocal? */
    305  1.26  christos 	set_fwdpermit_from_authopts(ssh, auth_opts);
    306  1.26  christos 
    307  1.24  christos 	if (!auth_opts->permit_port_forwarding_flag ||
    308  1.26  christos 	    options.disable_forwarding) {
    309  1.26  christos 		channel_disable_admin(ssh, FORWARD_LOCAL);
    310  1.26  christos 		channel_disable_admin(ssh, FORWARD_REMOTE);
    311  1.26  christos 	} else {
    312  1.26  christos 		if ((options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
    313  1.26  christos 			channel_disable_admin(ssh, FORWARD_LOCAL);
    314  1.26  christos 		else
    315  1.26  christos 			channel_permit_all(ssh, FORWARD_LOCAL);
    316  1.26  christos 		if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0)
    317  1.26  christos 			channel_disable_admin(ssh, FORWARD_REMOTE);
    318  1.26  christos 		else
    319  1.26  christos 			channel_permit_all(ssh, FORWARD_REMOTE);
    320  1.26  christos 	}
    321  1.28  christos 	auth_debug_send(ssh);
    322   1.4      adam 
    323  1.23  christos 	prepare_auth_info_file(authctxt->pw, authctxt->session_info);
    324  1.23  christos 
    325  1.23  christos 	do_authenticated2(ssh, authctxt);
    326  1.23  christos 
    327  1.23  christos 	do_cleanup(ssh, authctxt);
    328   1.1  christos }
    329   1.1  christos 
    330  1.20  christos /* Check untrusted xauth strings for metacharacters */
    331  1.20  christos static int
    332  1.20  christos xauth_valid_string(const char *s)
    333  1.20  christos {
    334  1.20  christos 	size_t i;
    335  1.20  christos 
    336  1.20  christos 	for (i = 0; s[i] != '\0'; i++) {
    337  1.20  christos 		if (!isalnum((u_char)s[i]) &&
    338  1.20  christos 		    s[i] != '.' && s[i] != ':' && s[i] != '/' &&
    339  1.20  christos 		    s[i] != '-' && s[i] != '_')
    340  1.26  christos 			return 0;
    341  1.20  christos 	}
    342  1.20  christos 	return 1;
    343  1.20  christos }
    344  1.20  christos 
    345  1.13  christos #define USE_PIPES 1
    346   1.1  christos /*
    347   1.1  christos  * This is called to fork and execute a command when we have no tty.  This
    348   1.1  christos  * will call do_child from the child, and server_loop from the parent after
    349   1.1  christos  * setting up file descriptors and such.
    350   1.1  christos  */
    351   1.1  christos int
    352  1.23  christos do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
    353   1.1  christos {
    354   1.1  christos 	pid_t pid;
    355   1.1  christos #ifdef USE_PIPES
    356   1.1  christos 	int pin[2], pout[2], perr[2];
    357   1.1  christos 
    358   1.4      adam 	if (s == NULL)
    359   1.4      adam 		fatal("do_exec_no_pty: no session");
    360   1.4      adam 
    361   1.1  christos 	/* Allocate pipes for communicating with the program. */
    362  1.29  christos 	if (pipe(pin) == -1) {
    363  1.33  christos 		error_f("pipe in: %.100s", strerror(errno));
    364   1.1  christos 		return -1;
    365   1.1  christos 	}
    366  1.29  christos 	if (pipe(pout) == -1) {
    367  1.33  christos 		error_f("pipe out: %.100s", strerror(errno));
    368   1.1  christos 		close(pin[0]);
    369   1.1  christos 		close(pin[1]);
    370   1.1  christos 		return -1;
    371   1.1  christos 	}
    372  1.29  christos 	if (pipe(perr) == -1) {
    373  1.33  christos 		error_f("pipe err: %.100s", strerror(errno));
    374   1.1  christos 		close(pin[0]);
    375   1.1  christos 		close(pin[1]);
    376   1.1  christos 		close(pout[0]);
    377   1.1  christos 		close(pout[1]);
    378   1.1  christos 		return -1;
    379   1.1  christos 	}
    380   1.1  christos #else
    381   1.1  christos 	int inout[2], err[2];
    382   1.1  christos 
    383   1.4      adam 	if (s == NULL)
    384   1.4      adam 		fatal("do_exec_no_pty: no session");
    385   1.4      adam 
    386   1.1  christos 	/* Uses socket pairs to communicate with the program. */
    387  1.29  christos 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) {
    388  1.33  christos 		error_f("socketpair #1: %.100s", strerror(errno));
    389   1.1  christos 		return -1;
    390   1.1  christos 	}
    391  1.29  christos 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) == -1) {
    392  1.33  christos 		error_f("socketpair #2: %.100s", strerror(errno));
    393   1.1  christos 		close(inout[0]);
    394   1.1  christos 		close(inout[1]);
    395   1.1  christos 		return -1;
    396   1.1  christos 	}
    397   1.1  christos #endif
    398   1.1  christos 
    399   1.1  christos 	session_proctitle(s);
    400   1.1  christos 
    401   1.1  christos 	/* Fork the child. */
    402   1.1  christos 	switch ((pid = fork())) {
    403   1.1  christos 	case -1:
    404  1.33  christos 		error_f("fork: %.100s", strerror(errno));
    405   1.1  christos #ifdef USE_PIPES
    406   1.1  christos 		close(pin[0]);
    407   1.1  christos 		close(pin[1]);
    408   1.1  christos 		close(pout[0]);
    409   1.1  christos 		close(pout[1]);
    410   1.1  christos 		close(perr[0]);
    411   1.1  christos 		close(perr[1]);
    412   1.1  christos #else
    413   1.1  christos 		close(inout[0]);
    414   1.1  christos 		close(inout[1]);
    415   1.1  christos 		close(err[0]);
    416   1.1  christos 		close(err[1]);
    417   1.1  christos #endif
    418   1.1  christos 		return -1;
    419   1.1  christos 	case 0:
    420   1.1  christos 		is_child = 1;
    421   1.1  christos 
    422   1.1  christos 		/*
    423   1.1  christos 		 * Create a new session and process group since the 4.4BSD
    424   1.1  christos 		 * setlogin() affects the entire process group.
    425   1.1  christos 		 */
    426  1.29  christos 		if (setsid() == -1)
    427   1.1  christos 			error("setsid failed: %.100s", strerror(errno));
    428   1.1  christos 
    429   1.1  christos #ifdef USE_PIPES
    430   1.1  christos 		/*
    431   1.1  christos 		 * Redirect stdin.  We close the parent side of the socket
    432   1.1  christos 		 * pair, and make the child side the standard input.
    433   1.1  christos 		 */
    434   1.1  christos 		close(pin[1]);
    435  1.29  christos 		if (dup2(pin[0], 0) == -1)
    436   1.1  christos 			perror("dup2 stdin");
    437   1.1  christos 		close(pin[0]);
    438   1.1  christos 
    439   1.1  christos 		/* Redirect stdout. */
    440   1.1  christos 		close(pout[0]);
    441  1.29  christos 		if (dup2(pout[1], 1) == -1)
    442   1.1  christos 			perror("dup2 stdout");
    443   1.1  christos 		close(pout[1]);
    444   1.1  christos 
    445   1.1  christos 		/* Redirect stderr. */
    446   1.1  christos 		close(perr[0]);
    447  1.29  christos 		if (dup2(perr[1], 2) == -1)
    448   1.1  christos 			perror("dup2 stderr");
    449   1.1  christos 		close(perr[1]);
    450   1.1  christos #else
    451   1.1  christos 		/*
    452   1.1  christos 		 * Redirect stdin, stdout, and stderr.  Stdin and stdout will
    453   1.1  christos 		 * use the same socket, as some programs (particularly rdist)
    454   1.1  christos 		 * seem to depend on it.
    455   1.1  christos 		 */
    456   1.1  christos 		close(inout[1]);
    457   1.1  christos 		close(err[1]);
    458  1.29  christos 		if (dup2(inout[0], 0) == -1)	/* stdin */
    459   1.1  christos 			perror("dup2 stdin");
    460  1.29  christos 		if (dup2(inout[0], 1) == -1)	/* stdout (same as stdin) */
    461   1.1  christos 			perror("dup2 stdout");
    462   1.1  christos 		close(inout[0]);
    463  1.29  christos 		if (dup2(err[0], 2) == -1)	/* stderr */
    464   1.1  christos 			perror("dup2 stderr");
    465   1.1  christos 		close(err[0]);
    466   1.1  christos #endif
    467   1.1  christos 
    468   1.1  christos 		/* Do processing for the child (exec command etc). */
    469  1.23  christos 		do_child(ssh, s, command);
    470   1.1  christos 		/* NOTREACHED */
    471   1.1  christos 	default:
    472   1.1  christos 		break;
    473   1.1  christos 	}
    474   1.1  christos 
    475   1.1  christos 	s->pid = pid;
    476   1.1  christos 
    477   1.1  christos #ifdef USE_PIPES
    478   1.1  christos 	/* We are the parent.  Close the child sides of the pipes. */
    479   1.1  christos 	close(pin[0]);
    480   1.1  christos 	close(pout[1]);
    481   1.1  christos 	close(perr[1]);
    482   1.1  christos 
    483  1.23  christos 	session_set_fds(ssh, s, pin[1], pout[0], perr[0],
    484  1.21  christos 	    s->is_subsystem, 0);
    485   1.1  christos #else
    486   1.1  christos 	/* We are the parent.  Close the child sides of the socket pairs. */
    487   1.1  christos 	close(inout[0]);
    488   1.1  christos 	close(err[0]);
    489   1.1  christos 
    490   1.1  christos 	/*
    491   1.1  christos 	 * Enter the interactive session.  Note: server_loop must be able to
    492   1.1  christos 	 * handle the case that fdin and fdout are the same.
    493   1.1  christos 	 */
    494  1.28  christos 	session_set_fds(ssh, s, inout[1], inout[1], err[1],
    495  1.21  christos 	    s->is_subsystem, 0);
    496   1.1  christos #endif
    497   1.1  christos 	return 0;
    498   1.1  christos }
    499   1.1  christos 
    500   1.1  christos /*
    501   1.1  christos  * This is called to fork and execute a command when we have a tty.  This
    502   1.1  christos  * will call do_child from the child, and server_loop from the parent after
    503   1.1  christos  * setting up file descriptors, controlling tty, updating wtmp, utmp,
    504   1.1  christos  * lastlog, and other such operations.
    505   1.1  christos  */
    506   1.1  christos int
    507  1.23  christos do_exec_pty(struct ssh *ssh, Session *s, const char *command)
    508   1.1  christos {
    509   1.1  christos 	int fdout, ptyfd, ttyfd, ptymaster;
    510   1.1  christos 	pid_t pid;
    511   1.1  christos 
    512   1.1  christos 	if (s == NULL)
    513   1.1  christos 		fatal("do_exec_pty: no session");
    514   1.1  christos 	ptyfd = s->ptyfd;
    515   1.1  christos 	ttyfd = s->ttyfd;
    516   1.1  christos 
    517   1.1  christos 	/*
    518   1.1  christos 	 * Create another descriptor of the pty master side for use as the
    519   1.1  christos 	 * standard input.  We could use the original descriptor, but this
    520   1.1  christos 	 * simplifies code in server_loop.  The descriptor is bidirectional.
    521   1.1  christos 	 * Do this before forking (and cleanup in the child) so as to
    522   1.1  christos 	 * detect and gracefully fail out-of-fd conditions.
    523   1.1  christos 	 */
    524  1.29  christos 	if ((fdout = dup(ptyfd)) == -1) {
    525  1.33  christos 		error_f("dup #1: %s", strerror(errno));
    526   1.1  christos 		close(ttyfd);
    527   1.1  christos 		close(ptyfd);
    528   1.1  christos 		return -1;
    529   1.1  christos 	}
    530   1.1  christos 	/* we keep a reference to the pty master */
    531  1.29  christos 	if ((ptymaster = dup(ptyfd)) == -1) {
    532  1.33  christos 		error_f("dup #2: %s", strerror(errno));
    533   1.1  christos 		close(ttyfd);
    534   1.1  christos 		close(ptyfd);
    535   1.1  christos 		close(fdout);
    536   1.1  christos 		return -1;
    537   1.1  christos 	}
    538   1.1  christos 
    539   1.1  christos 	/* Fork the child. */
    540   1.1  christos 	switch ((pid = fork())) {
    541   1.1  christos 	case -1:
    542  1.33  christos 		error_f("fork: %.100s", strerror(errno));
    543   1.1  christos 		close(fdout);
    544   1.1  christos 		close(ptymaster);
    545   1.1  christos 		close(ttyfd);
    546   1.1  christos 		close(ptyfd);
    547   1.1  christos 		return -1;
    548   1.1  christos 	case 0:
    549   1.1  christos 		is_child = 1;
    550   1.1  christos 
    551   1.1  christos 		close(fdout);
    552   1.1  christos 		close(ptymaster);
    553   1.1  christos 
    554   1.1  christos 		/* Close the master side of the pseudo tty. */
    555   1.1  christos 		close(ptyfd);
    556   1.1  christos 
    557   1.1  christos 		/* Make the pseudo tty our controlling tty. */
    558   1.1  christos 		pty_make_controlling_tty(&ttyfd, s->tty);
    559   1.1  christos 
    560   1.1  christos 		/* Redirect stdin/stdout/stderr from the pseudo tty. */
    561  1.29  christos 		if (dup2(ttyfd, 0) == -1)
    562   1.1  christos 			error("dup2 stdin: %s", strerror(errno));
    563  1.29  christos 		if (dup2(ttyfd, 1) == -1)
    564   1.1  christos 			error("dup2 stdout: %s", strerror(errno));
    565  1.29  christos 		if (dup2(ttyfd, 2) == -1)
    566   1.1  christos 			error("dup2 stderr: %s", strerror(errno));
    567   1.1  christos 
    568   1.1  christos 		/* Close the extra descriptor for the pseudo tty. */
    569   1.1  christos 		close(ttyfd);
    570   1.1  christos 
    571   1.1  christos 		/* record login, etc. similar to login(1) */
    572  1.23  christos 		do_login(ssh, s, command);
    573   1.1  christos 
    574   1.1  christos 		/*
    575   1.1  christos 		 * Do common processing for the child, such as execing
    576   1.1  christos 		 * the command.
    577   1.1  christos 		 */
    578  1.23  christos 		do_child(ssh, s, command);
    579   1.1  christos 		/* NOTREACHED */
    580   1.1  christos 	default:
    581   1.1  christos 		break;
    582   1.1  christos 	}
    583   1.1  christos 	s->pid = pid;
    584   1.1  christos 
    585   1.1  christos 	/* Parent.  Close the slave side of the pseudo tty. */
    586   1.1  christos 	close(ttyfd);
    587   1.1  christos 
    588   1.1  christos 	/* Enter interactive session. */
    589   1.1  christos 	s->ptymaster = ptymaster;
    590  1.23  christos 	session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
    591   1.1  christos 	return 0;
    592   1.1  christos }
    593   1.1  christos 
    594   1.1  christos /*
    595   1.1  christos  * This is called to fork and execute a command.  If another command is
    596   1.1  christos  * to be forced, execute that instead.
    597   1.1  christos  */
    598   1.1  christos int
    599  1.23  christos do_exec(struct ssh *ssh, Session *s, const char *command)
    600   1.1  christos {
    601   1.1  christos 	int ret;
    602  1.18  christos 	const char *forced = NULL, *tty = NULL;
    603  1.18  christos 	char session_type[1024];
    604   1.1  christos 
    605   1.1  christos 	if (options.adm_forced_command) {
    606   1.1  christos 		original_command = command;
    607   1.1  christos 		command = options.adm_forced_command;
    608  1.13  christos 		forced = "(config)";
    609  1.24  christos 	} else if (auth_opts->force_command != NULL) {
    610   1.1  christos 		original_command = command;
    611  1.24  christos 		command = auth_opts->force_command;
    612  1.13  christos 		forced = "(key-option)";
    613  1.13  christos 	}
    614  1.28  christos 	s->forced = 0;
    615  1.13  christos 	if (forced != NULL) {
    616  1.28  christos 		s->forced = 1;
    617   1.4      adam 		if (IS_INTERNAL_SFTP(command)) {
    618   1.4      adam 			s->is_subsystem = s->is_subsystem ?
    619   1.4      adam 			    SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
    620   1.4      adam 		} else if (s->is_subsystem)
    621   1.1  christos 			s->is_subsystem = SUBSYSTEM_EXT;
    622  1.13  christos 		snprintf(session_type, sizeof(session_type),
    623  1.13  christos 		    "forced-command %s '%.900s'", forced, command);
    624  1.13  christos 	} else if (s->is_subsystem) {
    625  1.13  christos 		snprintf(session_type, sizeof(session_type),
    626  1.13  christos 		    "subsystem '%.900s'", s->subsys);
    627  1.13  christos 	} else if (command == NULL) {
    628  1.13  christos 		snprintf(session_type, sizeof(session_type), "shell");
    629  1.13  christos 	} else {
    630  1.13  christos 		/* NB. we don't log unforced commands to preserve privacy */
    631  1.13  christos 		snprintf(session_type, sizeof(session_type), "command");
    632  1.13  christos 	}
    633  1.13  christos 
    634  1.13  christos 	if (s->ttyfd != -1) {
    635  1.13  christos 		tty = s->tty;
    636  1.13  christos 		if (strncmp(tty, "/dev/", 5) == 0)
    637  1.13  christos 			tty += 5;
    638   1.1  christos 	}
    639   1.1  christos 
    640  1.18  christos 	verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
    641  1.13  christos 	    session_type,
    642  1.13  christos 	    tty == NULL ? "" : " on ",
    643  1.13  christos 	    tty == NULL ? "" : tty,
    644  1.13  christos 	    s->pw->pw_name,
    645  1.20  christos 	    ssh_remote_ipaddr(ssh),
    646  1.20  christos 	    ssh_remote_port(ssh),
    647  1.18  christos 	    s->self);
    648  1.13  christos 
    649   1.1  christos #ifdef GSSAPI
    650   1.1  christos 	if (options.gss_authentication) {
    651   1.1  christos 		temporarily_use_uid(s->pw);
    652   1.1  christos 		ssh_gssapi_storecreds();
    653   1.1  christos 		restore_uid();
    654   1.1  christos 	}
    655   1.1  christos #endif
    656   1.1  christos 	if (s->ttyfd != -1)
    657  1.23  christos 		ret = do_exec_pty(ssh, s, command);
    658   1.1  christos 	else
    659  1.23  christos 		ret = do_exec_no_pty(ssh, s, command);
    660   1.1  christos 
    661   1.1  christos 	original_command = NULL;
    662   1.1  christos 
    663   1.1  christos 	/*
    664   1.1  christos 	 * Clear loginmsg: it's the child's responsibility to display
    665   1.1  christos 	 * it to the user, otherwise multiple sessions may accumulate
    666   1.1  christos 	 * multiple copies of the login messages.
    667   1.1  christos 	 */
    668  1.26  christos 	sshbuf_reset(loginmsg);
    669   1.1  christos 
    670   1.1  christos 	return ret;
    671   1.1  christos }
    672   1.1  christos 
    673   1.1  christos 
    674   1.1  christos /* administrative, login(1)-like work */
    675   1.1  christos void
    676  1.23  christos do_login(struct ssh *ssh, Session *s, const char *command)
    677   1.1  christos {
    678   1.1  christos 	socklen_t fromlen;
    679   1.1  christos 	struct sockaddr_storage from;
    680   1.1  christos 
    681   1.1  christos 	/*
    682   1.1  christos 	 * Get IP address of client. If the connection is not a socket, let
    683   1.1  christos 	 * the address be 0.0.0.0.
    684   1.1  christos 	 */
    685   1.1  christos 	memset(&from, 0, sizeof(from));
    686   1.1  christos 	fromlen = sizeof(from);
    687  1.28  christos 	if (ssh_packet_connection_is_on_socket(ssh)) {
    688  1.28  christos 		if (getpeername(ssh_packet_get_connection_in(ssh),
    689  1.29  christos 		    (struct sockaddr *)&from, &fromlen) == -1) {
    690   1.1  christos 			debug("getpeername: %.100s", strerror(errno));
    691  1.41  christos 			cleanup_exit(255);
    692   1.1  christos 		}
    693   1.1  christos 	}
    694   1.1  christos 
    695   1.1  christos 	if (check_quietlogin(s, command))
    696   1.1  christos 		return;
    697   1.1  christos 
    698   1.1  christos 	display_loginmsg();
    699   1.1  christos 
    700   1.1  christos 	do_motd();
    701   1.1  christos }
    702   1.1  christos 
    703   1.1  christos /*
    704   1.1  christos  * Display the message of the day.
    705   1.1  christos  */
    706   1.1  christos void
    707   1.1  christos do_motd(void)
    708   1.1  christos {
    709   1.1  christos 	FILE *f;
    710   1.1  christos 	char buf[256];
    711   1.1  christos 
    712   1.1  christos 	if (options.print_motd) {
    713   1.2  christos #ifdef HAVE_LOGIN_CAP
    714   1.7  christos 		f = fopen(login_getcapstr(lc, "welcome", __UNCONST("/etc/motd"),
    715   1.7  christos 		    __UNCONST("/etc/motd")), "r");
    716   1.2  christos #else
    717   1.2  christos 		f = fopen("/etc/motd", "r");
    718   1.2  christos #endif
    719   1.1  christos 		if (f) {
    720   1.1  christos 			while (fgets(buf, sizeof(buf), f))
    721   1.1  christos 				fputs(buf, stdout);
    722   1.1  christos 			fclose(f);
    723   1.1  christos 		}
    724   1.1  christos 	}
    725   1.1  christos }
    726   1.1  christos 
    727   1.1  christos 
    728   1.1  christos /*
    729   1.1  christos  * Check for quiet login, either .hushlogin or command given.
    730   1.1  christos  */
    731   1.1  christos int
    732   1.1  christos check_quietlogin(Session *s, const char *command)
    733   1.1  christos {
    734   1.1  christos 	char buf[256];
    735   1.1  christos 	struct passwd *pw = s->pw;
    736   1.1  christos 	struct stat st;
    737   1.1  christos 
    738   1.1  christos 	/* Return 1 if .hushlogin exists or a command given. */
    739   1.1  christos 	if (command != NULL)
    740   1.1  christos 		return 1;
    741   1.1  christos 	snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
    742   1.2  christos #ifdef HAVE_LOGIN_CAP
    743   1.1  christos 	if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
    744   1.1  christos 		return 1;
    745   1.2  christos #else
    746   1.2  christos 	if (stat(buf, &st) >= 0)
    747   1.2  christos 		return 1;
    748   1.2  christos #endif
    749   1.1  christos 	return 0;
    750   1.1  christos }
    751   1.1  christos 
    752   1.1  christos /*
    753   1.1  christos  * Reads environment variables from the given file and adds/overrides them
    754   1.1  christos  * into the environment.  If the file does not exist, this does nothing.
    755   1.1  christos  * Otherwise, it must consist of empty lines, comments (line starts with '#')
    756   1.1  christos  * and assignments of the form name=value.  No other forms are allowed.
    757  1.32  christos  * If allowlist is not NULL, then it is interpreted as a pattern list and
    758  1.26  christos  * only variable names that match it will be accepted.
    759   1.1  christos  */
    760   1.1  christos static void
    761   1.1  christos read_environment_file(char ***env, u_int *envsize,
    762  1.32  christos 	const char *filename, const char *allowlist)
    763   1.1  christos {
    764   1.1  christos 	FILE *f;
    765  1.26  christos 	char *line = NULL, *cp, *value;
    766  1.26  christos 	size_t linesize = 0;
    767   1.1  christos 	u_int lineno = 0;
    768   1.1  christos 
    769   1.1  christos 	f = fopen(filename, "r");
    770   1.1  christos 	if (!f)
    771   1.1  christos 		return;
    772   1.1  christos 
    773  1.26  christos 	while (getline(&line, &linesize, f) != -1) {
    774   1.1  christos 		if (++lineno > 1000)
    775   1.1  christos 			fatal("Too many lines in environment file %s", filename);
    776  1.26  christos 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
    777   1.1  christos 			;
    778   1.1  christos 		if (!*cp || *cp == '#' || *cp == '\n')
    779   1.1  christos 			continue;
    780   1.1  christos 
    781   1.1  christos 		cp[strcspn(cp, "\n")] = '\0';
    782   1.1  christos 
    783   1.1  christos 		value = strchr(cp, '=');
    784   1.1  christos 		if (value == NULL) {
    785   1.1  christos 			fprintf(stderr, "Bad line %u in %.100s\n", lineno,
    786   1.1  christos 			    filename);
    787   1.1  christos 			continue;
    788   1.1  christos 		}
    789   1.1  christos 		/*
    790   1.1  christos 		 * Replace the equals sign by nul, and advance value to
    791   1.1  christos 		 * the value string.
    792   1.1  christos 		 */
    793   1.1  christos 		*value = '\0';
    794   1.1  christos 		value++;
    795  1.32  christos 		if (allowlist != NULL &&
    796  1.32  christos 		    match_pattern_list(cp, allowlist, 0) != 1)
    797  1.26  christos 			continue;
    798   1.1  christos 		child_set_env(env, envsize, cp, value);
    799   1.1  christos 	}
    800  1.26  christos 	free(line);
    801   1.1  christos 	fclose(f);
    802   1.1  christos }
    803   1.1  christos 
    804  1.40  christos #if defined(USE_PAM) || defined(HAVE_CYGWIN)
    805  1.40  christos static void
    806  1.40  christos copy_environment_denylist(char **source, char ***env, u_int *envsize,
    807  1.40  christos     const char *denylist)
    808   1.2  christos {
    809   1.2  christos 	char *var_name, *var_val;
    810   1.2  christos 	int i;
    811   1.2  christos 
    812   1.2  christos 	if (source == NULL)
    813   1.2  christos 		return;
    814   1.2  christos 
    815  1.40  christos 	for(i = 0; source[i] != NULL; i++) {
    816   1.2  christos 		var_name = xstrdup(source[i]);
    817   1.2  christos 		if ((var_val = strstr(var_name, "=")) == NULL) {
    818  1.12  christos 			free(var_name);
    819   1.2  christos 			continue;
    820   1.2  christos 		}
    821   1.2  christos 		*var_val++ = '\0';
    822   1.2  christos 
    823  1.40  christos 		if (denylist == NULL ||
    824  1.40  christos 		    match_pattern_list(var_name, denylist, 0) != 1) {
    825  1.40  christos 			debug3("Copy environment: %s=%s", var_name, var_val);
    826  1.40  christos 			child_set_env(env, envsize, var_name, var_val);
    827  1.40  christos 		}
    828   1.2  christos 
    829  1.12  christos 		free(var_name);
    830   1.2  christos 	}
    831   1.2  christos }
    832  1.40  christos #endif /* defined(USE_PAM) || defined(HAVE_CYGWIN) */
    833   1.2  christos 
    834   1.1  christos static char **
    835  1.23  christos do_setup_env(struct ssh *ssh, Session *s, const char *shell)
    836   1.1  christos {
    837   1.1  christos 	char buf[256];
    838  1.24  christos 	size_t n;
    839   1.1  christos 	u_int i, envsize;
    840  1.26  christos 	char *ocp, *cp, *value, **env, *laddr;
    841   1.1  christos 	struct passwd *pw = s->pw;
    842   1.1  christos 
    843   1.1  christos 	/* Initialize the environment. */
    844   1.1  christos 	envsize = 100;
    845   1.1  christos 	env = xcalloc(envsize, sizeof(char *));
    846   1.1  christos 	env[0] = NULL;
    847   1.1  christos 
    848   1.1  christos #ifdef GSSAPI
    849   1.1  christos 	/* Allow any GSSAPI methods that we've used to alter
    850  1.31  christos 	 * the child's environment as they see fit
    851   1.1  christos 	 */
    852   1.1  christos 	ssh_gssapi_do_child(&env, &envsize);
    853   1.1  christos #endif
    854   1.1  christos 
    855  1.21  christos 	/* Set basic environment. */
    856  1.21  christos 	for (i = 0; i < s->num_env; i++)
    857  1.21  christos 		child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
    858  1.21  christos 
    859  1.21  christos 	child_set_env(&env, &envsize, "USER", pw->pw_name);
    860  1.21  christos 	child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
    861  1.21  christos 	child_set_env(&env, &envsize, "HOME", pw->pw_dir);
    862  1.21  christos 	if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
    863   1.2  christos 		child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
    864  1.21  christos 	else
    865  1.21  christos 		child_set_env(&env, &envsize, "PATH", getenv("PATH"));
    866  1.21  christos 
    867  1.21  christos 	snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
    868  1.21  christos 	child_set_env(&env, &envsize, "MAIL", buf);
    869   1.1  christos 
    870  1.21  christos 	/* Normal systems set SHELL by default. */
    871  1.21  christos 	child_set_env(&env, &envsize, "SHELL", shell);
    872   1.1  christos 
    873   1.1  christos 	if (getenv("TZ"))
    874   1.1  christos 		child_set_env(&env, &envsize, "TZ", getenv("TZ"));
    875  1.26  christos 	if (s->term)
    876  1.26  christos 		child_set_env(&env, &envsize, "TERM", s->term);
    877  1.26  christos 	if (s->display)
    878  1.26  christos 		child_set_env(&env, &envsize, "DISPLAY", s->display);
    879  1.26  christos #ifdef KRB5
    880  1.26  christos 	if (s->authctxt->krb5_ticket_file)
    881  1.26  christos 		child_set_env(&env, &envsize, "KRB5CCNAME",
    882  1.26  christos 		    s->authctxt->krb5_ticket_file);
    883  1.26  christos #endif
    884  1.26  christos 	if (auth_sock_name != NULL)
    885  1.26  christos 		child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
    886  1.26  christos 		    auth_sock_name);
    887  1.26  christos 
    888   1.1  christos 
    889  1.24  christos 	/* Set custom environment options from pubkey authentication. */
    890  1.24  christos 	if (options.permit_user_env) {
    891  1.24  christos 		for (n = 0 ; n < auth_opts->nenv; n++) {
    892  1.24  christos 			ocp = xstrdup(auth_opts->env[n]);
    893  1.24  christos 			cp = strchr(ocp, '=');
    894  1.33  christos 			if (cp != NULL) {
    895  1.24  christos 				*cp = '\0';
    896  1.32  christos 				/* Apply PermitUserEnvironment allowlist */
    897  1.32  christos 				if (options.permit_user_env_allowlist == NULL ||
    898  1.26  christos 				    match_pattern_list(ocp,
    899  1.32  christos 				    options.permit_user_env_allowlist, 0) == 1)
    900  1.26  christos 					child_set_env(&env, &envsize,
    901  1.26  christos 					    ocp, cp + 1);
    902  1.24  christos 			}
    903  1.24  christos 			free(ocp);
    904  1.24  christos 		}
    905   1.1  christos 	}
    906   1.1  christos 
    907  1.26  christos 	/* read $HOME/.ssh/environment. */
    908  1.26  christos 	if (options.permit_user_env) {
    909  1.33  christos 		snprintf(buf, sizeof buf, "%.200s/%s/environment",
    910  1.33  christos 		    pw->pw_dir, _PATH_SSH_USER_DIR);
    911  1.26  christos 		read_environment_file(&env, &envsize, buf,
    912  1.32  christos 		    options.permit_user_env_allowlist);
    913  1.26  christos 	}
    914  1.26  christos 
    915  1.40  christos #ifdef USE_PAM
    916  1.40  christos 	/*
    917  1.40  christos 	 * Pull in any environment variables that may have
    918  1.40  christos 	 * been set by PAM.
    919  1.40  christos 	 */
    920  1.40  christos 	if (options.use_pam) {
    921  1.40  christos 		char **p;
    922  1.40  christos 
    923  1.40  christos 		/*
    924  1.40  christos 		 * Don't allow PAM-internal env vars to leak
    925  1.40  christos 		 * back into the session environment.
    926  1.40  christos 		 */
    927  1.40  christos #define PAM_ENV_DENYLIST  "SSH_AUTH_INFO*,SSH_CONNECTION*"
    928  1.40  christos 		p = fetch_pam_child_environment();
    929  1.40  christos 		copy_environment_denylist(p, &env, &envsize,
    930  1.40  christos 		    PAM_ENV_DENYLIST);
    931  1.40  christos 		free_pam_environment(p);
    932  1.40  christos 
    933  1.40  christos 		p = fetch_pam_environment();
    934  1.40  christos 		copy_environment_denylist(p, &env, &envsize,
    935  1.40  christos 		    PAM_ENV_DENYLIST);
    936  1.40  christos 		free_pam_environment(p);
    937  1.40  christos 	}
    938  1.40  christos #endif /* USE_PAM */
    939  1.40  christos 
    940  1.26  christos 	/* Environment specified by admin */
    941  1.26  christos 	for (i = 0; i < options.num_setenv; i++) {
    942  1.26  christos 		cp = xstrdup(options.setenv[i]);
    943  1.26  christos 		if ((value = strchr(cp, '=')) == NULL) {
    944  1.26  christos 			/* shouldn't happen; vars are checked in servconf.c */
    945  1.26  christos 			fatal("Invalid config SetEnv: %s", options.setenv[i]);
    946  1.26  christos 		}
    947  1.26  christos 		*value++ = '\0';
    948  1.26  christos 		child_set_env(&env, &envsize, cp, value);
    949  1.37  christos 		free(cp);
    950  1.26  christos 	}
    951  1.26  christos 
    952   1.1  christos 	/* SSH_CLIENT deprecated */
    953   1.1  christos 	snprintf(buf, sizeof buf, "%.50s %d %d",
    954  1.20  christos 	    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
    955  1.20  christos 	    ssh_local_port(ssh));
    956   1.1  christos 	child_set_env(&env, &envsize, "SSH_CLIENT", buf);
    957   1.1  christos 
    958  1.28  christos 	laddr = get_local_ipaddr(ssh_packet_get_connection_in(ssh));
    959   1.1  christos 	snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
    960  1.20  christos 	    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
    961  1.20  christos 	    laddr, ssh_local_port(ssh));
    962  1.12  christos 	free(laddr);
    963   1.1  christos 	child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
    964   1.1  christos 
    965  1.24  christos 	if (tun_fwd_ifnames != NULL)
    966  1.24  christos 		child_set_env(&env, &envsize, "SSH_TUNNEL", tun_fwd_ifnames);
    967  1.23  christos 	if (auth_info_file != NULL)
    968  1.23  christos 		child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file);
    969   1.1  christos 	if (s->ttyfd != -1)
    970   1.1  christos 		child_set_env(&env, &envsize, "SSH_TTY", s->tty);
    971   1.1  christos 	if (original_command)
    972   1.1  christos 		child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
    973   1.1  christos 		    original_command);
    974   1.2  christos #ifdef KRB4
    975   1.2  christos 	if (s->authctxt->krb4_ticket_file)
    976   1.2  christos 		child_set_env(&env, &envsize, "KRBTKFILE",
    977   1.2  christos 		    s->authctxt->krb4_ticket_file);
    978   1.2  christos #endif
    979   1.1  christos #ifdef KRB5
    980   1.1  christos 	if (s->authctxt->krb5_ticket_file)
    981   1.1  christos 		child_set_env(&env, &envsize, "KRB5CCNAME",
    982   1.1  christos 		    s->authctxt->krb5_ticket_file);
    983   1.1  christos #endif
    984   1.2  christos 
    985   1.2  christos 
    986   1.1  christos 	if (debug_flag) {
    987   1.1  christos 		/* dump the environment */
    988   1.1  christos 		fprintf(stderr, "Environment:\n");
    989   1.1  christos 		for (i = 0; env[i]; i++)
    990   1.1  christos 			fprintf(stderr, "  %.200s\n", env[i]);
    991   1.1  christos 	}
    992   1.1  christos 	return env;
    993   1.1  christos }
    994   1.1  christos 
    995   1.1  christos /*
    996   1.1  christos  * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
    997   1.1  christos  * first in this order).
    998   1.1  christos  */
    999   1.1  christos static void
   1000  1.24  christos do_rc_files(struct ssh *ssh, Session *s, const char *shell)
   1001   1.1  christos {
   1002   1.1  christos 	FILE *f = NULL;
   1003  1.32  christos 	char *cmd = NULL, *user_rc = NULL;
   1004   1.1  christos 	int do_xauth;
   1005   1.1  christos 	struct stat st;
   1006   1.1  christos 
   1007   1.1  christos 	do_xauth =
   1008   1.1  christos 	    s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
   1009  1.32  christos 	xasprintf(&user_rc, "%s/%s", s->pw->pw_dir, _PATH_SSH_USER_RC);
   1010   1.1  christos 
   1011   1.1  christos 	/* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
   1012   1.1  christos 	if (!s->is_subsystem && options.adm_forced_command == NULL &&
   1013  1.24  christos 	    auth_opts->permit_user_rc && options.permit_user_rc &&
   1014  1.32  christos 	    stat(user_rc, &st) >= 0) {
   1015  1.32  christos 		if (xasprintf(&cmd, "%s -c '%s %s'", shell, _PATH_BSHELL,
   1016  1.32  christos 		    user_rc) == -1)
   1017  1.33  christos 			fatal_f("xasprintf: %s", strerror(errno));
   1018   1.1  christos 		if (debug_flag)
   1019   1.1  christos 			fprintf(stderr, "Running %s\n", cmd);
   1020   1.1  christos 		f = popen(cmd, "w");
   1021   1.1  christos 		if (f) {
   1022   1.1  christos 			if (do_xauth)
   1023   1.1  christos 				fprintf(f, "%s %s\n", s->auth_proto,
   1024   1.1  christos 				    s->auth_data);
   1025   1.1  christos 			pclose(f);
   1026   1.1  christos 		} else
   1027   1.1  christos 			fprintf(stderr, "Could not run %s\n",
   1028  1.32  christos 			    user_rc);
   1029   1.1  christos 	} else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
   1030   1.1  christos 		if (debug_flag)
   1031   1.1  christos 			fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
   1032   1.1  christos 			    _PATH_SSH_SYSTEM_RC);
   1033   1.1  christos 		f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
   1034   1.1  christos 		if (f) {
   1035   1.1  christos 			if (do_xauth)
   1036   1.1  christos 				fprintf(f, "%s %s\n", s->auth_proto,
   1037   1.1  christos 				    s->auth_data);
   1038   1.1  christos 			pclose(f);
   1039   1.1  christos 		} else
   1040   1.1  christos 			fprintf(stderr, "Could not run %s\n",
   1041   1.1  christos 			    _PATH_SSH_SYSTEM_RC);
   1042   1.1  christos 	} else if (do_xauth && options.xauth_location != NULL) {
   1043   1.1  christos 		/* Add authority data to .Xauthority if appropriate. */
   1044   1.1  christos 		if (debug_flag) {
   1045   1.1  christos 			fprintf(stderr,
   1046   1.1  christos 			    "Running %.500s remove %.100s\n",
   1047   1.1  christos 			    options.xauth_location, s->auth_display);
   1048   1.1  christos 			fprintf(stderr,
   1049   1.1  christos 			    "%.500s add %.100s %.100s %.100s\n",
   1050   1.1  christos 			    options.xauth_location, s->auth_display,
   1051   1.1  christos 			    s->auth_proto, s->auth_data);
   1052   1.1  christos 		}
   1053  1.32  christos 		if (xasprintf(&cmd, "%s -q -", options.xauth_location) == -1)
   1054  1.33  christos 			fatal_f("xasprintf: %s", strerror(errno));
   1055   1.1  christos 		f = popen(cmd, "w");
   1056   1.1  christos 		if (f) {
   1057   1.1  christos 			fprintf(f, "remove %s\n",
   1058   1.1  christos 			    s->auth_display);
   1059   1.1  christos 			fprintf(f, "add %s %s %s\n",
   1060   1.1  christos 			    s->auth_display, s->auth_proto,
   1061   1.1  christos 			    s->auth_data);
   1062   1.1  christos 			pclose(f);
   1063   1.1  christos 		} else {
   1064   1.1  christos 			fprintf(stderr, "Could not run %s\n",
   1065   1.1  christos 			    cmd);
   1066   1.1  christos 		}
   1067   1.1  christos 	}
   1068  1.32  christos 	free(cmd);
   1069  1.32  christos 	free(user_rc);
   1070   1.1  christos }
   1071   1.1  christos 
   1072   1.1  christos static void
   1073   1.1  christos do_nologin(struct passwd *pw)
   1074   1.1  christos {
   1075   1.1  christos 	FILE *f = NULL;
   1076   1.7  christos 	char buf[1024], *nl, *def_nl = __UNCONST(_PATH_NOLOGIN);
   1077   1.4      adam 	struct stat sb;
   1078   1.1  christos 
   1079   1.2  christos #ifdef HAVE_LOGIN_CAP
   1080  1.10  christos 	if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
   1081   1.4      adam 		return;
   1082   1.4      adam 	nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
   1083  1.17     seanb #else
   1084  1.17     seanb 	if (pw->pw_uid == 0)
   1085  1.17     seanb 		return;
   1086  1.17     seanb 	nl = def_nl;
   1087  1.17     seanb #endif
   1088   1.4      adam 	if (stat(nl, &sb) == -1) {
   1089   1.4      adam 		if (nl != def_nl)
   1090  1.12  christos 			free(nl);
   1091   1.4      adam 		return;
   1092   1.4      adam 	}
   1093  1.17     seanb 
   1094   1.4      adam 	/* /etc/nologin exists.  Print its contents if we can and exit. */
   1095   1.4      adam 	logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
   1096   1.4      adam 	if ((f = fopen(nl, "r")) != NULL) {
   1097   1.1  christos 		while (fgets(buf, sizeof(buf), f))
   1098   1.1  christos 			fputs(buf, stderr);
   1099   1.1  christos 		fclose(f);
   1100   1.1  christos 	}
   1101   1.4      adam 	exit(254);
   1102   1.1  christos }
   1103   1.1  christos 
   1104   1.1  christos /*
   1105   1.1  christos  * Chroot into a directory after checking it for safety: all path components
   1106   1.1  christos  * must be root-owned directories with strict permissions.
   1107   1.1  christos  */
   1108   1.1  christos static void
   1109   1.1  christos safely_chroot(const char *path, uid_t uid)
   1110   1.1  christos {
   1111   1.1  christos 	const char *cp;
   1112  1.14  christos 	char component[PATH_MAX];
   1113   1.1  christos 	struct stat st;
   1114   1.1  christos 
   1115  1.28  christos 	if (!path_absolute(path))
   1116   1.1  christos 		fatal("chroot path does not begin at root");
   1117   1.1  christos 	if (strlen(path) >= sizeof(component))
   1118   1.1  christos 		fatal("chroot path too long");
   1119   1.1  christos 
   1120   1.1  christos 	/*
   1121   1.1  christos 	 * Descend the path, checking that each component is a
   1122   1.1  christos 	 * root-owned directory with strict permissions.
   1123   1.1  christos 	 */
   1124   1.1  christos 	for (cp = path; cp != NULL;) {
   1125   1.1  christos 		if ((cp = strchr(cp, '/')) == NULL)
   1126   1.1  christos 			strlcpy(component, path, sizeof(component));
   1127   1.1  christos 		else {
   1128   1.1  christos 			cp++;
   1129   1.1  christos 			memcpy(component, path, cp - path);
   1130   1.1  christos 			component[cp - path] = '\0';
   1131   1.1  christos 		}
   1132  1.39  christos 
   1133  1.33  christos 		debug3_f("checking '%s'", component);
   1134   1.1  christos 
   1135   1.1  christos 		if (stat(component, &st) != 0)
   1136  1.33  christos 			fatal_f("stat(\"%s\"): %s",
   1137   1.1  christos 			    component, strerror(errno));
   1138   1.1  christos 		if (st.st_uid != 0 || (st.st_mode & 022) != 0)
   1139   1.1  christos 			fatal("bad ownership or modes for chroot "
   1140  1.28  christos 			    "directory %s\"%s\"",
   1141   1.1  christos 			    cp == NULL ? "" : "component ", component);
   1142   1.1  christos 		if (!S_ISDIR(st.st_mode))
   1143   1.1  christos 			fatal("chroot path %s\"%s\" is not a directory",
   1144   1.1  christos 			    cp == NULL ? "" : "component ", component);
   1145   1.1  christos 
   1146   1.1  christos 	}
   1147   1.1  christos 
   1148   1.1  christos 	if (chdir(path) == -1)
   1149   1.1  christos 		fatal("Unable to chdir to chroot path \"%s\": "
   1150   1.1  christos 		    "%s", path, strerror(errno));
   1151   1.1  christos 	if (chroot(path) == -1)
   1152   1.1  christos 		fatal("chroot(\"%s\"): %s", path, strerror(errno));
   1153   1.1  christos 	if (chdir("/") == -1)
   1154  1.33  christos 		fatal_f("chdir(/) after chroot: %s", strerror(errno));
   1155   1.1  christos 	verbose("Changed root directory to \"%s\"", path);
   1156   1.1  christos }
   1157   1.1  christos 
   1158   1.1  christos /* Set login name, uid, gid, and groups. */
   1159   1.1  christos void
   1160   1.1  christos do_setusercontext(struct passwd *pw)
   1161   1.1  christos {
   1162  1.26  christos 	char uidstr[32], *chroot_path, *tmp;
   1163   1.1  christos 
   1164   1.1  christos 	if (getuid() == 0 || geteuid() == 0) {
   1165   1.2  christos #ifdef HAVE_LOGIN_CAP
   1166   1.2  christos # ifdef USE_PAM
   1167   1.2  christos 		if (options.use_pam) {
   1168  1.40  christos 			do_pam_setcred();
   1169   1.2  christos 		}
   1170   1.2  christos # endif /* USE_PAM */
   1171   1.1  christos 		/* Prepare groups */
   1172   1.1  christos 		if (setusercontext(lc, pw, pw->pw_uid,
   1173   1.1  christos 		    (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
   1174   1.1  christos 			perror("unable to set user context");
   1175   1.1  christos 			exit(1);
   1176   1.1  christos 		}
   1177   1.2  christos #else
   1178   1.1  christos 
   1179   1.2  christos 		if (setlogin(pw->pw_name) < 0)
   1180   1.2  christos 			error("setlogin failed: %s", strerror(errno));
   1181   1.2  christos 		if (setgid(pw->pw_gid) < 0) {
   1182   1.2  christos 			perror("setgid");
   1183   1.2  christos 			exit(1);
   1184   1.2  christos 		}
   1185   1.2  christos 		/* Initialize the group list. */
   1186   1.2  christos 		if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
   1187   1.2  christos 			perror("initgroups");
   1188   1.2  christos 			exit(1);
   1189   1.2  christos 		}
   1190   1.2  christos 		endgrent();
   1191   1.2  christos # ifdef USE_PAM
   1192   1.2  christos 		/*
   1193   1.2  christos 		 * PAM credentials may take the form of supplementary groups.
   1194   1.2  christos 		 * These will have been wiped by the above initgroups() call.
   1195   1.2  christos 		 * Reestablish them here.
   1196   1.2  christos 		 */
   1197   1.2  christos 		if (options.use_pam) {
   1198  1.40  christos 			do_pam_setcred();
   1199   1.2  christos 		}
   1200   1.2  christos # endif /* USE_PAM */
   1201   1.2  christos #endif
   1202  1.18  christos 		if (!in_chroot && options.chroot_directory != NULL &&
   1203   1.1  christos 		    strcasecmp(options.chroot_directory, "none") != 0) {
   1204  1.34  christos 			tmp = tilde_expand_filename(options.chroot_directory,
   1205   1.1  christos 			    pw->pw_uid);
   1206  1.26  christos 			snprintf(uidstr, sizeof(uidstr), "%llu",
   1207  1.26  christos 			    (unsigned long long)pw->pw_uid);
   1208   1.1  christos 			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
   1209  1.26  christos 			    "u", pw->pw_name, "U", uidstr, (char *)NULL);
   1210   1.1  christos 			safely_chroot(chroot_path, pw->pw_uid);
   1211   1.1  christos 			free(tmp);
   1212   1.1  christos 			free(chroot_path);
   1213  1.12  christos 			/* Make sure we don't attempt to chroot again */
   1214  1.12  christos 			free(options.chroot_directory);
   1215  1.12  christos 			options.chroot_directory = NULL;
   1216  1.18  christos 			in_chroot = 1;
   1217   1.1  christos 		}
   1218   1.1  christos 
   1219   1.2  christos #ifdef HAVE_LOGIN_CAP
   1220   1.1  christos 		/* Set UID */
   1221   1.1  christos 		if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
   1222   1.1  christos 			perror("unable to set user context (setuser)");
   1223   1.1  christos 			exit(1);
   1224   1.1  christos 		}
   1225   1.2  christos #else
   1226   1.2  christos 		/* Permanently switch to the desired uid. */
   1227   1.2  christos 		permanently_set_uid(pw);
   1228   1.2  christos #endif
   1229  1.12  christos 	} else if (options.chroot_directory != NULL &&
   1230  1.12  christos 	    strcasecmp(options.chroot_directory, "none") != 0) {
   1231  1.12  christos 		fatal("server lacks privileges to chroot to ChrootDirectory");
   1232   1.1  christos 	}
   1233  1.12  christos 
   1234   1.1  christos 	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
   1235   1.1  christos 		fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
   1236   1.1  christos }
   1237   1.1  christos 
   1238   1.8     joerg __dead static void
   1239   1.1  christos do_pwchange(Session *s)
   1240   1.1  christos {
   1241   1.1  christos 	fflush(NULL);
   1242   1.1  christos 	fprintf(stderr, "WARNING: Your password has expired.\n");
   1243   1.1  christos 	if (s->ttyfd != -1) {
   1244   1.1  christos 		fprintf(stderr,
   1245  1.43  christos 		    "You must change your password now and log in again!\n");
   1246   1.1  christos 		execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
   1247   1.1  christos 		perror("passwd");
   1248   1.1  christos 	} else {
   1249   1.1  christos 		fprintf(stderr,
   1250   1.1  christos 		    "Password change required but no TTY available.\n");
   1251   1.1  christos 	}
   1252   1.1  christos 	exit(1);
   1253   1.1  christos }
   1254   1.1  christos 
   1255   1.1  christos static void
   1256  1.23  christos child_close_fds(struct ssh *ssh)
   1257   1.1  christos {
   1258  1.14  christos 	extern int auth_sock;
   1259  1.12  christos 
   1260  1.14  christos 	if (auth_sock != -1) {
   1261  1.14  christos 		close(auth_sock);
   1262  1.14  christos 		auth_sock = -1;
   1263  1.12  christos 	}
   1264  1.12  christos 
   1265  1.28  christos 	if (ssh_packet_get_connection_in(ssh) ==
   1266  1.28  christos 	    ssh_packet_get_connection_out(ssh))
   1267  1.28  christos 		close(ssh_packet_get_connection_in(ssh));
   1268   1.1  christos 	else {
   1269  1.28  christos 		close(ssh_packet_get_connection_in(ssh));
   1270  1.28  christos 		close(ssh_packet_get_connection_out(ssh));
   1271   1.1  christos 	}
   1272   1.1  christos 	/*
   1273   1.1  christos 	 * Close all descriptors related to channels.  They will still remain
   1274   1.1  christos 	 * open in the parent.
   1275   1.1  christos 	 */
   1276   1.1  christos 	/* XXX better use close-on-exec? -markus */
   1277  1.23  christos 	channel_close_all(ssh);
   1278   1.1  christos 
   1279   1.1  christos 	/*
   1280   1.1  christos 	 * Close any extra file descriptors.  Note that there may still be
   1281   1.1  christos 	 * descriptors left by system functions.  They will be closed later.
   1282   1.1  christos 	 */
   1283   1.1  christos 	endpwent();
   1284   1.1  christos 
   1285  1.32  christos 	/* Stop directing logs to a high-numbered fd before we close it */
   1286  1.32  christos 	log_redirect_stderr_to(NULL);
   1287  1.32  christos 
   1288   1.1  christos 	/*
   1289   1.1  christos 	 * Close any extra open file descriptors so that we don't have them
   1290   1.1  christos 	 * hanging around in clients.  Note that we want to do this after
   1291   1.1  christos 	 * initgroups, because at least on Solaris 2.3 it leaves file
   1292   1.1  christos 	 * descriptors open.
   1293   1.1  christos 	 */
   1294  1.16  christos 	(void)closefrom(STDERR_FILENO + 1);
   1295   1.1  christos }
   1296   1.1  christos 
   1297   1.1  christos /*
   1298   1.1  christos  * Performs common processing for the child, such as setting up the
   1299   1.1  christos  * environment, closing extra file descriptors, setting the user and group
   1300   1.1  christos  * ids, and executing the command or shell.
   1301   1.1  christos  */
   1302   1.1  christos #define ARGV_MAX 10
   1303   1.1  christos void
   1304  1.23  christos do_child(struct ssh *ssh, Session *s, const char *command)
   1305   1.1  christos {
   1306   1.1  christos 	extern char **environ;
   1307  1.28  christos 	char **env, *argv[ARGV_MAX], remote_id[512];
   1308  1.21  christos 	const char *shell, *shell0;
   1309   1.1  christos 	struct passwd *pw = s->pw;
   1310   1.1  christos 	int r = 0;
   1311   1.1  christos 
   1312  1.28  christos 	sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
   1313  1.28  christos 
   1314  1.43  christos 	/* remove keys from memory */
   1315  1.28  christos 	ssh_packet_clear_keys(ssh);
   1316   1.1  christos 
   1317   1.1  christos 	/* Force a password change */
   1318   1.1  christos 	if (s->authctxt->force_pwchange) {
   1319   1.1  christos 		do_setusercontext(pw);
   1320  1.23  christos 		child_close_fds(ssh);
   1321   1.1  christos 		do_pwchange(s);
   1322   1.1  christos 	}
   1323   1.1  christos 
   1324   1.1  christos 	/*
   1325   1.1  christos 	 * Login(1) does this as well, and it needs uid 0 for the "-h"
   1326   1.1  christos 	 * switch, so we let login(1) to this for us.
   1327   1.1  christos 	 */
   1328  1.40  christos 	/* When PAM is enabled we rely on it to do the nologin check */
   1329  1.40  christos 	if (!options.use_pam)
   1330  1.40  christos 		do_nologin(pw);
   1331  1.40  christos 	do_setusercontext(pw);
   1332  1.40  christos 	/*
   1333  1.40  christos 	 * PAM session modules in do_setusercontext may have
   1334  1.40  christos 	 * generated messages, so if this in an interactive
   1335  1.40  christos 	 * login then display them too.
   1336  1.40  christos 	 */
   1337  1.40  christos 	if (!check_quietlogin(s, command))
   1338  1.40  christos 		display_loginmsg();
   1339  1.40  christos 
   1340   1.2  christos #ifdef USE_PAM
   1341   1.2  christos 	if (options.use_pam && !is_pam_session_open()) {
   1342   1.2  christos 		debug3("PAM session not opened, exiting");
   1343   1.2  christos 		display_loginmsg();
   1344   1.2  christos 		exit(254);
   1345   1.1  christos 	}
   1346   1.2  christos #endif
   1347   1.1  christos 
   1348   1.1  christos 	/*
   1349   1.1  christos 	 * Get the shell from the password data.  An empty shell field is
   1350   1.1  christos 	 * legal, and means /bin/sh.
   1351   1.1  christos 	 */
   1352   1.1  christos 	shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
   1353   1.1  christos 
   1354   1.1  christos 	/*
   1355   1.1  christos 	 * Make sure $SHELL points to the shell from the password file,
   1356   1.1  christos 	 * even if shell is overridden from login.conf
   1357   1.1  christos 	 */
   1358  1.23  christos 	env = do_setup_env(ssh, s, shell);
   1359   1.1  christos 
   1360   1.2  christos #ifdef HAVE_LOGIN_CAP
   1361   1.7  christos 	shell = login_getcapstr(lc, "shell", __UNCONST(shell),
   1362   1.7  christos 	    __UNCONST(shell));
   1363   1.2  christos #endif
   1364   1.1  christos 
   1365   1.1  christos 	/*
   1366   1.1  christos 	 * Close the connection descriptors; note that this is the child, and
   1367   1.1  christos 	 * the server will still have the socket open, and it is important
   1368   1.1  christos 	 * that we do not shutdown it.  Note that the descriptors cannot be
   1369   1.1  christos 	 * closed before building the environment, as we call
   1370  1.20  christos 	 * ssh_remote_ipaddr there.
   1371   1.1  christos 	 */
   1372  1.23  christos 	child_close_fds(ssh);
   1373   1.1  christos 
   1374   1.1  christos 	/*
   1375   1.1  christos 	 * Must take new environment into use so that .ssh/rc,
   1376   1.1  christos 	 * /etc/ssh/sshrc and xauth are run in the proper environment.
   1377   1.1  christos 	 */
   1378   1.1  christos 	environ = env;
   1379   1.1  christos 
   1380   1.1  christos #ifdef KRB5
   1381   1.1  christos 	/*
   1382   1.1  christos 	 * At this point, we check to see if AFS is active and if we have
   1383   1.1  christos 	 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
   1384   1.1  christos 	 * if we can (and need to) extend the ticket into an AFS token. If
   1385   1.1  christos 	 * we don't do this, we run into potential problems if the user's
   1386   1.1  christos 	 * home directory is in AFS and it's not world-readable.
   1387   1.1  christos 	 */
   1388   1.1  christos 
   1389   1.1  christos 	if (options.kerberos_get_afs_token && k_hasafs() &&
   1390   1.1  christos 	    (s->authctxt->krb5_ctx != NULL)) {
   1391   1.1  christos 		char cell[64];
   1392   1.1  christos 
   1393   1.1  christos 		debug("Getting AFS token");
   1394   1.1  christos 
   1395   1.1  christos 		k_setpag();
   1396   1.1  christos 
   1397   1.1  christos 		if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
   1398   1.1  christos 			krb5_afslog(s->authctxt->krb5_ctx,
   1399   1.1  christos 			    s->authctxt->krb5_fwd_ccache, cell, NULL);
   1400   1.1  christos 
   1401   1.1  christos 		krb5_afslog_home(s->authctxt->krb5_ctx,
   1402   1.1  christos 		    s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
   1403   1.1  christos 	}
   1404   1.1  christos #endif
   1405   1.1  christos 
   1406   1.1  christos 	/* Change current directory to the user's home directory. */
   1407  1.29  christos 	if (chdir(pw->pw_dir) == -1) {
   1408   1.1  christos 		/* Suppress missing homedir warning for chroot case */
   1409   1.1  christos 		r = login_getcapbool(lc, "requirehome", 0);
   1410  1.18  christos 		if (r || !in_chroot) {
   1411   1.1  christos 			fprintf(stderr, "Could not chdir to home "
   1412   1.1  christos 			    "directory %s: %s\n", pw->pw_dir,
   1413   1.1  christos 			    strerror(errno));
   1414  1.18  christos 		}
   1415   1.1  christos 		if (r)
   1416   1.1  christos 			exit(1);
   1417   1.1  christos 	}
   1418   1.1  christos 
   1419  1.16  christos 	(void)closefrom(STDERR_FILENO + 1);
   1420   1.1  christos 
   1421  1.24  christos 	do_rc_files(ssh, s, shell);
   1422   1.1  christos 
   1423   1.1  christos 	/* restore SIGPIPE for child */
   1424  1.30  christos 	ssh_signal(SIGPIPE, SIG_DFL);
   1425   1.1  christos 
   1426   1.4      adam 	if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
   1427  1.28  christos 		error("Connection from %s: refusing non-sftp session",
   1428  1.28  christos 		    remote_id);
   1429   1.4      adam 		printf("This service allows sftp connections only.\n");
   1430   1.4      adam 		fflush(NULL);
   1431   1.4      adam 		exit(1);
   1432   1.4      adam 	} else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
   1433   1.1  christos 		extern int optind, optreset;
   1434   1.1  christos 		int i;
   1435   1.1  christos 		char *p, *args;
   1436   1.1  christos 
   1437   1.3  christos 		setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
   1438   1.1  christos 		args = xstrdup(command ? command : "sftp-server");
   1439   1.1  christos 		for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
   1440   1.1  christos 			if (i < ARGV_MAX - 1)
   1441   1.1  christos 				argv[i++] = p;
   1442   1.1  christos 		argv[i] = NULL;
   1443   1.1  christos 		optind = optreset = 1;
   1444   1.1  christos 		__progname = argv[0];
   1445   1.1  christos 		exit(sftp_server_main(i, argv, s->pw));
   1446   1.1  christos 	}
   1447   1.1  christos 
   1448   1.4      adam 	fflush(NULL);
   1449   1.4      adam 
   1450   1.1  christos 	/* Get the last component of the shell name. */
   1451   1.1  christos 	if ((shell0 = strrchr(shell, '/')) != NULL)
   1452   1.1  christos 		shell0++;
   1453   1.1  christos 	else
   1454   1.1  christos 		shell0 = shell;
   1455   1.1  christos 
   1456   1.1  christos 	/*
   1457   1.1  christos 	 * If we have no command, execute the shell.  In this case, the shell
   1458   1.1  christos 	 * name to be passed in argv[0] is preceded by '-' to indicate that
   1459   1.1  christos 	 * this is a login shell.
   1460   1.1  christos 	 */
   1461   1.1  christos 	if (!command) {
   1462   1.1  christos 		char argv0[256];
   1463   1.1  christos 
   1464   1.1  christos 		/* Start the shell.  Set initial character to '-'. */
   1465   1.1  christos 		argv0[0] = '-';
   1466   1.1  christos 
   1467   1.1  christos 		if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
   1468   1.1  christos 		    >= sizeof(argv0) - 1) {
   1469   1.1  christos 			errno = EINVAL;
   1470   1.1  christos 			perror(shell);
   1471   1.1  christos 			exit(1);
   1472   1.1  christos 		}
   1473   1.1  christos 
   1474   1.1  christos 		/* Execute the shell. */
   1475   1.1  christos 		argv[0] = argv0;
   1476   1.1  christos 		argv[1] = NULL;
   1477   1.1  christos 		execve(shell, argv, env);
   1478   1.1  christos 
   1479   1.1  christos 		/* Executing the shell failed. */
   1480   1.1  christos 		perror(shell);
   1481   1.1  christos 		exit(1);
   1482   1.1  christos 	}
   1483   1.1  christos 	/*
   1484   1.1  christos 	 * Execute the command using the user's shell.  This uses the -c
   1485   1.1  christos 	 * option to execute the command.
   1486   1.1  christos 	 */
   1487   1.7  christos 	argv[0] = __UNCONST(shell0);
   1488   1.7  christos 	argv[1] = __UNCONST("-c");
   1489   1.7  christos 	argv[2] = __UNCONST(command);
   1490   1.1  christos 	argv[3] = NULL;
   1491   1.1  christos 	execve(shell, argv, env);
   1492   1.1  christos 	perror(shell);
   1493   1.1  christos 	exit(1);
   1494   1.1  christos }
   1495   1.1  christos 
   1496   1.1  christos void
   1497   1.1  christos session_unused(int id)
   1498   1.1  christos {
   1499  1.33  christos 	debug3_f("session id %d unused", id);
   1500   1.1  christos 	if (id >= options.max_sessions ||
   1501   1.1  christos 	    id >= sessions_nalloc) {
   1502  1.33  christos 		fatal_f("insane session id %d (max %d nalloc %d)",
   1503  1.33  christos 		    id, options.max_sessions, sessions_nalloc);
   1504   1.1  christos 	}
   1505  1.13  christos 	memset(&sessions[id], 0, sizeof(*sessions));
   1506   1.1  christos 	sessions[id].self = id;
   1507   1.1  christos 	sessions[id].used = 0;
   1508   1.1  christos 	sessions[id].chanid = -1;
   1509   1.1  christos 	sessions[id].ptyfd = -1;
   1510   1.1  christos 	sessions[id].ttyfd = -1;
   1511   1.1  christos 	sessions[id].ptymaster = -1;
   1512   1.1  christos 	sessions[id].x11_chanids = NULL;
   1513   1.1  christos 	sessions[id].next_unused = sessions_first_unused;
   1514   1.1  christos 	sessions_first_unused = id;
   1515   1.1  christos }
   1516   1.1  christos 
   1517   1.1  christos Session *
   1518   1.1  christos session_new(void)
   1519   1.1  christos {
   1520   1.1  christos 	Session *s, *tmp;
   1521   1.1  christos 
   1522   1.1  christos 	if (sessions_first_unused == -1) {
   1523   1.1  christos 		if (sessions_nalloc >= options.max_sessions)
   1524   1.1  christos 			return NULL;
   1525  1.33  christos 		debug2_f("allocate (allocated %d max %d)",
   1526  1.33  christos 		    sessions_nalloc, options.max_sessions);
   1527  1.23  christos 		tmp = xrecallocarray(sessions, sessions_nalloc,
   1528  1.23  christos 		    sessions_nalloc + 1, sizeof(*sessions));
   1529   1.1  christos 		if (tmp == NULL) {
   1530  1.33  christos 			error_f("cannot allocate %d sessions",
   1531  1.33  christos 			    sessions_nalloc + 1);
   1532   1.1  christos 			return NULL;
   1533   1.1  christos 		}
   1534   1.1  christos 		sessions = tmp;
   1535   1.1  christos 		session_unused(sessions_nalloc++);
   1536   1.1  christos 	}
   1537   1.1  christos 
   1538   1.1  christos 	if (sessions_first_unused >= sessions_nalloc ||
   1539   1.1  christos 	    sessions_first_unused < 0) {
   1540  1.33  christos 		fatal_f("insane first_unused %d max %d nalloc %d",
   1541  1.33  christos 		    sessions_first_unused, options.max_sessions,
   1542   1.1  christos 		    sessions_nalloc);
   1543   1.1  christos 	}
   1544   1.1  christos 
   1545   1.1  christos 	s = &sessions[sessions_first_unused];
   1546  1.33  christos 	if (s->used)
   1547  1.33  christos 		fatal_f("session %d already used", sessions_first_unused);
   1548   1.1  christos 	sessions_first_unused = s->next_unused;
   1549   1.1  christos 	s->used = 1;
   1550   1.1  christos 	s->next_unused = -1;
   1551   1.1  christos 	debug("session_new: session %d", s->self);
   1552   1.1  christos 
   1553   1.1  christos 	return s;
   1554   1.1  christos }
   1555   1.1  christos 
   1556   1.1  christos static void
   1557   1.1  christos session_dump(void)
   1558   1.1  christos {
   1559   1.1  christos 	int i;
   1560   1.1  christos 	for (i = 0; i < sessions_nalloc; i++) {
   1561   1.1  christos 		Session *s = &sessions[i];
   1562   1.1  christos 
   1563  1.35  christos 		debug("dump: used %d next_unused %d session %d "
   1564   1.1  christos 		    "channel %d pid %ld",
   1565   1.1  christos 		    s->used,
   1566   1.1  christos 		    s->next_unused,
   1567   1.1  christos 		    s->self,
   1568   1.1  christos 		    s->chanid,
   1569   1.1  christos 		    (long)s->pid);
   1570   1.1  christos 	}
   1571   1.1  christos }
   1572   1.1  christos 
   1573   1.1  christos int
   1574   1.1  christos session_open(Authctxt *authctxt, int chanid)
   1575   1.1  christos {
   1576   1.1  christos 	Session *s = session_new();
   1577   1.1  christos 	debug("session_open: channel %d", chanid);
   1578   1.1  christos 	if (s == NULL) {
   1579   1.1  christos 		error("no more sessions");
   1580   1.1  christos 		return 0;
   1581   1.1  christos 	}
   1582   1.1  christos 	s->authctxt = authctxt;
   1583   1.1  christos 	s->pw = authctxt->pw;
   1584   1.1  christos 	if (s->pw == NULL || !authctxt->valid)
   1585   1.1  christos 		fatal("no user for session %d", s->self);
   1586   1.1  christos 	debug("session_open: session %d: link with channel %d", s->self, chanid);
   1587   1.1  christos 	s->chanid = chanid;
   1588   1.1  christos 	return 1;
   1589   1.1  christos }
   1590   1.1  christos 
   1591   1.1  christos Session *
   1592   1.1  christos session_by_tty(char *tty)
   1593   1.1  christos {
   1594   1.1  christos 	int i;
   1595   1.1  christos 	for (i = 0; i < sessions_nalloc; i++) {
   1596   1.1  christos 		Session *s = &sessions[i];
   1597   1.1  christos 		if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
   1598   1.1  christos 			debug("session_by_tty: session %d tty %s", i, tty);
   1599   1.1  christos 			return s;
   1600   1.1  christos 		}
   1601   1.1  christos 	}
   1602   1.1  christos 	debug("session_by_tty: unknown tty %.100s", tty);
   1603   1.1  christos 	session_dump();
   1604   1.1  christos 	return NULL;
   1605   1.1  christos }
   1606   1.1  christos 
   1607   1.1  christos static Session *
   1608   1.1  christos session_by_channel(int id)
   1609   1.1  christos {
   1610   1.1  christos 	int i;
   1611   1.1  christos 	for (i = 0; i < sessions_nalloc; i++) {
   1612   1.1  christos 		Session *s = &sessions[i];
   1613   1.1  christos 		if (s->used && s->chanid == id) {
   1614   1.1  christos 			debug("session_by_channel: session %d channel %d",
   1615   1.1  christos 			    i, id);
   1616   1.1  christos 			return s;
   1617   1.1  christos 		}
   1618   1.1  christos 	}
   1619   1.1  christos 	debug("session_by_channel: unknown channel %d", id);
   1620   1.1  christos 	session_dump();
   1621   1.1  christos 	return NULL;
   1622   1.1  christos }
   1623   1.1  christos 
   1624   1.1  christos static Session *
   1625   1.1  christos session_by_x11_channel(int id)
   1626   1.1  christos {
   1627   1.1  christos 	int i, j;
   1628   1.1  christos 
   1629   1.1  christos 	for (i = 0; i < sessions_nalloc; i++) {
   1630   1.1  christos 		Session *s = &sessions[i];
   1631   1.1  christos 
   1632   1.1  christos 		if (s->x11_chanids == NULL || !s->used)
   1633   1.1  christos 			continue;
   1634   1.1  christos 		for (j = 0; s->x11_chanids[j] != -1; j++) {
   1635   1.1  christos 			if (s->x11_chanids[j] == id) {
   1636   1.1  christos 				debug("session_by_x11_channel: session %d "
   1637   1.1  christos 				    "channel %d", s->self, id);
   1638   1.1  christos 				return s;
   1639   1.1  christos 			}
   1640   1.1  christos 		}
   1641   1.1  christos 	}
   1642   1.1  christos 	debug("session_by_x11_channel: unknown channel %d", id);
   1643   1.1  christos 	session_dump();
   1644   1.1  christos 	return NULL;
   1645   1.1  christos }
   1646   1.1  christos 
   1647   1.1  christos static Session *
   1648   1.1  christos session_by_pid(pid_t pid)
   1649   1.1  christos {
   1650   1.1  christos 	int i;
   1651   1.1  christos 	debug("session_by_pid: pid %ld", (long)pid);
   1652   1.1  christos 	for (i = 0; i < sessions_nalloc; i++) {
   1653   1.1  christos 		Session *s = &sessions[i];
   1654   1.1  christos 		if (s->used && s->pid == pid)
   1655   1.1  christos 			return s;
   1656   1.1  christos 	}
   1657   1.1  christos 	error("session_by_pid: unknown pid %ld", (long)pid);
   1658   1.1  christos 	session_dump();
   1659   1.1  christos 	return NULL;
   1660   1.1  christos }
   1661   1.1  christos 
   1662   1.1  christos static int
   1663  1.23  christos session_window_change_req(struct ssh *ssh, Session *s)
   1664   1.1  christos {
   1665  1.28  christos 	int r;
   1666  1.28  christos 
   1667  1.28  christos 	if ((r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
   1668  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
   1669  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
   1670  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0 ||
   1671  1.28  christos 	    (r = sshpkt_get_end(ssh)) != 0)
   1672  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1673   1.1  christos 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
   1674   1.1  christos 	return 1;
   1675   1.1  christos }
   1676   1.1  christos 
   1677   1.1  christos static int
   1678  1.23  christos session_pty_req(struct ssh *ssh, Session *s)
   1679   1.1  christos {
   1680  1.28  christos 	int r;
   1681   1.1  christos 
   1682  1.24  christos 	if (!auth_opts->permit_pty_flag || !options.permit_tty) {
   1683  1.24  christos 		debug("Allocating a pty not permitted for this connection.");
   1684   1.1  christos 		return 0;
   1685   1.1  christos 	}
   1686   1.1  christos 	if (s->ttyfd != -1) {
   1687  1.28  christos 		ssh_packet_disconnect(ssh, "Protocol error: you already have a pty.");
   1688   1.1  christos 		return 0;
   1689   1.1  christos 	}
   1690   1.1  christos 
   1691  1.28  christos 	if ((r = sshpkt_get_cstring(ssh, &s->term, NULL)) != 0 ||
   1692  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
   1693  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
   1694  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
   1695  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0)
   1696  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1697   1.1  christos 
   1698   1.1  christos 	if (strcmp(s->term, "") == 0) {
   1699  1.12  christos 		free(s->term);
   1700   1.1  christos 		s->term = NULL;
   1701   1.1  christos 	}
   1702   1.1  christos 
   1703   1.1  christos 	/* Allocate a pty and open it. */
   1704   1.1  christos 	debug("Allocating pty.");
   1705  1.40  christos 	if (!mm_pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
   1706  1.12  christos 		free(s->term);
   1707   1.1  christos 		s->term = NULL;
   1708   1.1  christos 		s->ptyfd = -1;
   1709   1.1  christos 		s->ttyfd = -1;
   1710   1.1  christos 		error("session_pty_req: session %d alloc failed", s->self);
   1711   1.1  christos 		return 0;
   1712   1.1  christos 	}
   1713   1.1  christos 	debug("session_pty_req: session %d alloc %s", s->self, s->tty);
   1714   1.1  christos 
   1715  1.26  christos 	ssh_tty_parse_modes(ssh, s->ttyfd);
   1716   1.1  christos 
   1717  1.28  christos 	if ((r = sshpkt_get_end(ssh)) != 0)
   1718  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1719  1.28  christos 
   1720   1.1  christos 	/* Set window size from the packet. */
   1721   1.1  christos 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
   1722   1.1  christos 
   1723   1.1  christos 	session_proctitle(s);
   1724   1.1  christos 	return 1;
   1725   1.1  christos }
   1726   1.1  christos 
   1727   1.1  christos static int
   1728  1.23  christos session_subsystem_req(struct ssh *ssh, Session *s)
   1729   1.1  christos {
   1730   1.1  christos 	struct stat st;
   1731  1.28  christos 	int r, success = 0;
   1732  1.37  christos 	char *prog, *cmd, *type;
   1733   1.1  christos 	u_int i;
   1734   1.1  christos 
   1735  1.28  christos 	if ((r = sshpkt_get_cstring(ssh, &s->subsys, NULL)) != 0 ||
   1736  1.28  christos 	    (r = sshpkt_get_end(ssh)) != 0)
   1737  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1738  1.13  christos 	debug2("subsystem request for %.100s by user %s", s->subsys,
   1739   1.4      adam 	    s->pw->pw_name);
   1740   1.1  christos 
   1741   1.1  christos 	for (i = 0; i < options.num_subsystems; i++) {
   1742  1.13  christos 		if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
   1743   1.1  christos 			prog = options.subsystem_command[i];
   1744   1.1  christos 			cmd = options.subsystem_args[i];
   1745   1.4      adam 			if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
   1746   1.1  christos 				s->is_subsystem = SUBSYSTEM_INT_SFTP;
   1747   1.4      adam 				debug("subsystem: %s", prog);
   1748   1.1  christos 			} else {
   1749  1.29  christos 				if (stat(prog, &st) == -1)
   1750   1.4      adam 					debug("subsystem: cannot stat %s: %s",
   1751   1.4      adam 					    prog, strerror(errno));
   1752   1.1  christos 				s->is_subsystem = SUBSYSTEM_EXT;
   1753   1.4      adam 				debug("subsystem: exec() %s", cmd);
   1754   1.1  christos 			}
   1755  1.37  christos 			xasprintf(&type, "session:subsystem:%s",
   1756  1.37  christos 			    options.subsystem_name[i]);
   1757  1.37  christos 			channel_set_xtype(ssh, s->chanid, type);
   1758  1.37  christos 			free(type);
   1759  1.23  christos 			success = do_exec(ssh, s, cmd) == 0;
   1760   1.1  christos 			break;
   1761   1.1  christos 		}
   1762   1.1  christos 	}
   1763   1.1  christos 
   1764   1.1  christos 	if (!success)
   1765  1.13  christos 		logit("subsystem request for %.100s by user %s failed, "
   1766  1.13  christos 		    "subsystem not found", s->subsys, s->pw->pw_name);
   1767   1.1  christos 
   1768   1.1  christos 	return success;
   1769   1.1  christos }
   1770   1.1  christos 
   1771   1.1  christos static int
   1772  1.23  christos session_x11_req(struct ssh *ssh, Session *s)
   1773   1.1  christos {
   1774  1.28  christos 	int r, success;
   1775  1.28  christos 	u_char single_connection = 0;
   1776   1.1  christos 
   1777   1.1  christos 	if (s->auth_proto != NULL || s->auth_data != NULL) {
   1778   1.1  christos 		error("session_x11_req: session %d: "
   1779   1.1  christos 		    "x11 forwarding already active", s->self);
   1780   1.1  christos 		return 0;
   1781   1.1  christos 	}
   1782  1.28  christos 	if ((r = sshpkt_get_u8(ssh, &single_connection)) != 0 ||
   1783  1.28  christos 	    (r = sshpkt_get_cstring(ssh, &s->auth_proto, NULL)) != 0 ||
   1784  1.28  christos 	    (r = sshpkt_get_cstring(ssh, &s->auth_data, NULL)) != 0 ||
   1785  1.28  christos 	    (r = sshpkt_get_u32(ssh, &s->screen)) != 0 ||
   1786  1.28  christos 	    (r = sshpkt_get_end(ssh)) != 0)
   1787  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1788  1.28  christos 
   1789  1.28  christos 	s->single_connection = single_connection;
   1790   1.1  christos 
   1791  1.20  christos 	if (xauth_valid_string(s->auth_proto) &&
   1792  1.20  christos 	    xauth_valid_string(s->auth_data))
   1793  1.23  christos 		success = session_setup_x11fwd(ssh, s);
   1794  1.20  christos 	else {
   1795  1.20  christos 		success = 0;
   1796  1.20  christos 		error("Invalid X11 forwarding data");
   1797  1.20  christos 	}
   1798   1.1  christos 	if (!success) {
   1799  1.12  christos 		free(s->auth_proto);
   1800  1.12  christos 		free(s->auth_data);
   1801   1.1  christos 		s->auth_proto = NULL;
   1802   1.1  christos 		s->auth_data = NULL;
   1803   1.1  christos 	}
   1804   1.1  christos 	return success;
   1805   1.1  christos }
   1806   1.1  christos 
   1807   1.1  christos static int
   1808  1.23  christos session_shell_req(struct ssh *ssh, Session *s)
   1809   1.1  christos {
   1810  1.28  christos 	int r;
   1811  1.28  christos 
   1812  1.28  christos 	if ((r = sshpkt_get_end(ssh)) != 0)
   1813  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1814  1.37  christos 
   1815  1.37  christos 	channel_set_xtype(ssh, s->chanid, "session:shell");
   1816  1.37  christos 
   1817  1.23  christos 	return do_exec(ssh, s, NULL) == 0;
   1818   1.1  christos }
   1819   1.1  christos 
   1820   1.1  christos static int
   1821  1.23  christos session_exec_req(struct ssh *ssh, Session *s)
   1822   1.1  christos {
   1823  1.28  christos 	u_int success;
   1824  1.28  christos 	int r;
   1825  1.28  christos 	char *command = NULL;
   1826  1.28  christos 
   1827  1.28  christos 	if ((r = sshpkt_get_cstring(ssh, &command, NULL)) != 0 ||
   1828  1.28  christos 	    (r = sshpkt_get_end(ssh)) != 0)
   1829  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1830   1.1  christos 
   1831  1.37  christos 	channel_set_xtype(ssh, s->chanid, "session:command");
   1832  1.37  christos 
   1833  1.23  christos 	success = do_exec(ssh, s, command) == 0;
   1834  1.12  christos 	free(command);
   1835   1.1  christos 	return success;
   1836   1.1  christos }
   1837   1.1  christos 
   1838   1.1  christos static int
   1839  1.23  christos session_break_req(struct ssh *ssh, Session *s)
   1840   1.1  christos {
   1841  1.28  christos 	int r;
   1842   1.1  christos 
   1843  1.28  christos 	if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* ignore */
   1844  1.28  christos 	    (r = sshpkt_get_end(ssh)) != 0)
   1845  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1846   1.1  christos 
   1847  1.29  christos 	if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) == -1)
   1848   1.1  christos 		return 0;
   1849   1.1  christos 	return 1;
   1850   1.1  christos }
   1851   1.1  christos 
   1852   1.1  christos static int
   1853  1.23  christos session_env_req(struct ssh *ssh, Session *s)
   1854   1.1  christos {
   1855   1.1  christos 	char *name, *val;
   1856  1.28  christos 	u_int i;
   1857  1.28  christos 	int r;
   1858   1.1  christos 
   1859  1.28  christos 	if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
   1860  1.28  christos 	    (r = sshpkt_get_cstring(ssh, &val, NULL)) != 0 ||
   1861  1.28  christos 	    (r = sshpkt_get_end(ssh)) != 0)
   1862  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1863   1.1  christos 
   1864   1.1  christos 	/* Don't set too many environment variables */
   1865   1.1  christos 	if (s->num_env > 128) {
   1866   1.1  christos 		debug2("Ignoring env request %s: too many env vars", name);
   1867   1.1  christos 		goto fail;
   1868   1.1  christos 	}
   1869   1.1  christos 
   1870   1.1  christos 	for (i = 0; i < options.num_accept_env; i++) {
   1871   1.1  christos 		if (match_pattern(name, options.accept_env[i])) {
   1872   1.1  christos 			debug2("Setting env %d: %s=%s", s->num_env, name, val);
   1873  1.23  christos 			s->env = xrecallocarray(s->env, s->num_env,
   1874  1.23  christos 			    s->num_env + 1, sizeof(*s->env));
   1875   1.1  christos 			s->env[s->num_env].name = name;
   1876   1.1  christos 			s->env[s->num_env].val = val;
   1877   1.1  christos 			s->num_env++;
   1878   1.1  christos 			return (1);
   1879   1.1  christos 		}
   1880   1.1  christos 	}
   1881   1.1  christos 	debug2("Ignoring env request %s: disallowed name", name);
   1882   1.1  christos 
   1883   1.1  christos  fail:
   1884  1.12  christos 	free(name);
   1885  1.12  christos 	free(val);
   1886   1.1  christos 	return (0);
   1887   1.1  christos }
   1888   1.1  christos 
   1889  1.28  christos /*
   1890  1.28  christos  * Conversion of signals from ssh channel request names.
   1891  1.28  christos  * Subset of signals from RFC 4254 section 6.10C, with SIGINFO as
   1892  1.28  christos  * local extension.
   1893  1.28  christos  */
   1894  1.28  christos static int
   1895  1.28  christos name2sig(char *name)
   1896  1.28  christos {
   1897  1.28  christos #define SSH_SIG(x) if (strcmp(name, #x) == 0) return SIG ## x
   1898  1.28  christos 	SSH_SIG(HUP);
   1899  1.28  christos 	SSH_SIG(INT);
   1900  1.28  christos 	SSH_SIG(KILL);
   1901  1.28  christos 	SSH_SIG(QUIT);
   1902  1.28  christos 	SSH_SIG(TERM);
   1903  1.28  christos 	SSH_SIG(USR1);
   1904  1.28  christos 	SSH_SIG(USR2);
   1905  1.28  christos #undef	SSH_SIG
   1906  1.28  christos 	if (strcmp(name, "INFO (at) openssh.com") == 0)
   1907  1.28  christos 		return SIGINFO;
   1908  1.28  christos 	return -1;
   1909  1.28  christos }
   1910  1.28  christos 
   1911  1.28  christos static int
   1912  1.28  christos session_signal_req(struct ssh *ssh, Session *s)
   1913  1.28  christos {
   1914  1.28  christos 	char *signame = NULL;
   1915  1.28  christos 	int r, sig, success = 0;
   1916  1.28  christos 
   1917  1.28  christos 	if ((r = sshpkt_get_cstring(ssh, &signame, NULL)) != 0 ||
   1918  1.28  christos 	    (r = sshpkt_get_end(ssh)) != 0) {
   1919  1.33  christos 		error_fr(r, "parse");
   1920  1.28  christos 		goto out;
   1921  1.28  christos 	}
   1922  1.28  christos 	if ((sig = name2sig(signame)) == -1) {
   1923  1.33  christos 		error_f("unsupported signal \"%s\"", signame);
   1924  1.28  christos 		goto out;
   1925  1.28  christos 	}
   1926  1.28  christos 	if (s->pid <= 0) {
   1927  1.33  christos 		error_f("no pid for session %d", s->self);
   1928  1.28  christos 		goto out;
   1929  1.28  christos 	}
   1930  1.28  christos 	if (s->forced || s->is_subsystem) {
   1931  1.33  christos 		error_f("refusing to send signal %s to %s session",
   1932  1.28  christos 		    signame, s->forced ? "forced-command" : "subsystem");
   1933  1.28  christos 		goto out;
   1934  1.28  christos 	}
   1935  1.28  christos 
   1936  1.33  christos 	debug_f("signal %s, killpg(%ld, %d)", signame, (long)s->pid, sig);
   1937  1.28  christos 	temporarily_use_uid(s->pw);
   1938  1.28  christos 	r = killpg(s->pid, sig);
   1939  1.28  christos 	restore_uid();
   1940  1.28  christos 	if (r != 0) {
   1941  1.33  christos 		error_f("killpg(%ld, %d): %s", (long)s->pid,
   1942  1.28  christos 		    sig, strerror(errno));
   1943  1.28  christos 		goto out;
   1944  1.28  christos 	}
   1945  1.28  christos 
   1946  1.28  christos 	/* success */
   1947  1.28  christos 	success = 1;
   1948  1.28  christos  out:
   1949  1.28  christos 	free(signame);
   1950  1.28  christos 	return success;
   1951  1.28  christos }
   1952  1.28  christos 
   1953   1.1  christos static int
   1954  1.23  christos session_auth_agent_req(struct ssh *ssh, Session *s)
   1955   1.1  christos {
   1956   1.1  christos 	static int called = 0;
   1957  1.28  christos 	int r;
   1958  1.24  christos 
   1959  1.28  christos 	if ((r = sshpkt_get_end(ssh)) != 0)
   1960  1.28  christos 		sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
   1961  1.24  christos 	if (!auth_opts->permit_agent_forwarding_flag ||
   1962  1.42       kim 	    !options.allow_agent_forwarding ||
   1963  1.42       kim 	    options.disable_forwarding) {
   1964  1.33  christos 		debug_f("agent forwarding disabled");
   1965   1.1  christos 		return 0;
   1966   1.1  christos 	}
   1967   1.1  christos 	if (called) {
   1968   1.1  christos 		return 0;
   1969   1.1  christos 	} else {
   1970   1.1  christos 		called = 1;
   1971  1.23  christos 		return auth_input_request_forwarding(ssh, s->pw);
   1972   1.1  christos 	}
   1973   1.1  christos }
   1974   1.1  christos 
   1975   1.1  christos int
   1976  1.23  christos session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype)
   1977   1.1  christos {
   1978   1.1  christos 	int success = 0;
   1979   1.1  christos 	Session *s;
   1980   1.1  christos 
   1981   1.1  christos 	if ((s = session_by_channel(c->self)) == NULL) {
   1982  1.33  christos 		logit_f("no session %d req %.100s", c->self, rtype);
   1983   1.1  christos 		return 0;
   1984   1.1  christos 	}
   1985  1.33  christos 	debug_f("session %d req %s", s->self, rtype);
   1986   1.1  christos 
   1987   1.1  christos 	/*
   1988   1.1  christos 	 * a session is in LARVAL state until a shell, a command
   1989   1.1  christos 	 * or a subsystem is executed
   1990   1.1  christos 	 */
   1991   1.1  christos 	if (c->type == SSH_CHANNEL_LARVAL) {
   1992   1.1  christos 		if (strcmp(rtype, "shell") == 0) {
   1993  1.23  christos 			success = session_shell_req(ssh, s);
   1994   1.1  christos 		} else if (strcmp(rtype, "exec") == 0) {
   1995  1.23  christos 			success = session_exec_req(ssh, s);
   1996   1.1  christos 		} else if (strcmp(rtype, "pty-req") == 0) {
   1997  1.23  christos 			success = session_pty_req(ssh, s);
   1998   1.1  christos 		} else if (strcmp(rtype, "x11-req") == 0) {
   1999  1.23  christos 			success = session_x11_req(ssh, s);
   2000   1.1  christos 		} else if (strcmp(rtype, "auth-agent-req (at) openssh.com") == 0) {
   2001  1.23  christos 			success = session_auth_agent_req(ssh, s);
   2002   1.1  christos 		} else if (strcmp(rtype, "subsystem") == 0) {
   2003  1.23  christos 			success = session_subsystem_req(ssh, s);
   2004   1.1  christos 		} else if (strcmp(rtype, "env") == 0) {
   2005  1.23  christos 			success = session_env_req(ssh, s);
   2006   1.1  christos 		}
   2007   1.1  christos 	}
   2008   1.1  christos 	if (strcmp(rtype, "window-change") == 0) {
   2009  1.23  christos 		success = session_window_change_req(ssh, s);
   2010   1.1  christos 	} else if (strcmp(rtype, "break") == 0) {
   2011  1.23  christos 		success = session_break_req(ssh, s);
   2012  1.28  christos 	} else if (strcmp(rtype, "signal") == 0) {
   2013  1.28  christos 		success = session_signal_req(ssh, s);
   2014   1.1  christos 	}
   2015   1.1  christos 
   2016   1.1  christos 	return success;
   2017   1.1  christos }
   2018   1.1  christos 
   2019   1.1  christos void
   2020  1.23  christos session_set_fds(struct ssh *ssh, Session *s,
   2021  1.23  christos     int fdin, int fdout, int fderr, int ignore_fderr, int is_tty)
   2022   1.1  christos {
   2023   1.1  christos 	/*
   2024   1.1  christos 	 * now that have a child and a pipe to the child,
   2025   1.1  christos 	 * we can activate our channel and register the fd's
   2026   1.1  christos 	 */
   2027   1.1  christos 	if (s->chanid == -1)
   2028   1.1  christos 		fatal("no channel for session %d", s->self);
   2029   1.2  christos 	if(options.hpn_disabled)
   2030  1.23  christos 	channel_set_fds(ssh, s->chanid,
   2031   1.1  christos 	    fdout, fdin, fderr,
   2032   1.4      adam 	    ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
   2033   1.1  christos 	    1, is_tty, CHAN_SES_WINDOW_DEFAULT);
   2034   1.2  christos 	else
   2035  1.23  christos 		channel_set_fds(ssh, s->chanid,
   2036   1.2  christos 		    fdout, fdin, fderr,
   2037   1.5      adam 	            ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
   2038   1.2  christos 		    1, is_tty, options.hpn_buffer_size);
   2039   1.1  christos }
   2040   1.1  christos 
   2041   1.1  christos /*
   2042   1.1  christos  * Function to perform pty cleanup. Also called if we get aborted abnormally
   2043   1.1  christos  * (e.g., due to a dropped connection).
   2044   1.1  christos  */
   2045   1.1  christos void
   2046   1.1  christos session_pty_cleanup2(Session *s)
   2047   1.1  christos {
   2048   1.1  christos 	if (s == NULL) {
   2049  1.33  christos 		error_f("no session");
   2050   1.1  christos 		return;
   2051   1.1  christos 	}
   2052   1.1  christos 	if (s->ttyfd == -1)
   2053   1.1  christos 		return;
   2054   1.1  christos 
   2055  1.33  christos 	debug_f("session %d release %s", s->self, s->tty);
   2056   1.1  christos 
   2057   1.1  christos 	/* Record that the user has logged out. */
   2058   1.1  christos 	if (s->pid != 0)
   2059   1.1  christos 		record_logout(s->pid, s->tty);
   2060   1.1  christos 
   2061   1.1  christos 	/* Release the pseudo-tty. */
   2062   1.1  christos 	if (getuid() == 0)
   2063   1.1  christos 		pty_release(s->tty);
   2064   1.1  christos 
   2065   1.1  christos 	/*
   2066   1.1  christos 	 * Close the server side of the socket pairs.  We must do this after
   2067   1.1  christos 	 * the pty cleanup, so that another process doesn't get this pty
   2068   1.1  christos 	 * while we're still cleaning up.
   2069   1.1  christos 	 */
   2070  1.29  christos 	if (s->ptymaster != -1 && close(s->ptymaster) == -1)
   2071   1.1  christos 		error("close(s->ptymaster/%d): %s",
   2072   1.1  christos 		    s->ptymaster, strerror(errno));
   2073   1.1  christos 
   2074   1.1  christos 	/* unlink pty from session */
   2075   1.1  christos 	s->ttyfd = -1;
   2076   1.1  christos }
   2077   1.1  christos 
   2078   1.1  christos void
   2079   1.1  christos session_pty_cleanup(Session *s)
   2080   1.1  christos {
   2081  1.40  christos 	mm_session_pty_cleanup2(s);
   2082   1.1  christos }
   2083   1.1  christos 
   2084   1.7  christos static const char *
   2085   1.1  christos sig2name(int sig)
   2086   1.1  christos {
   2087   1.1  christos #define SSH_SIG(x) if (sig == SIG ## x) return #x
   2088   1.1  christos 	SSH_SIG(ABRT);
   2089   1.1  christos 	SSH_SIG(ALRM);
   2090   1.1  christos 	SSH_SIG(FPE);
   2091   1.1  christos 	SSH_SIG(HUP);
   2092   1.1  christos 	SSH_SIG(ILL);
   2093   1.1  christos 	SSH_SIG(INT);
   2094   1.1  christos 	SSH_SIG(KILL);
   2095   1.1  christos 	SSH_SIG(PIPE);
   2096   1.1  christos 	SSH_SIG(QUIT);
   2097   1.1  christos 	SSH_SIG(SEGV);
   2098   1.1  christos 	SSH_SIG(TERM);
   2099   1.1  christos 	SSH_SIG(USR1);
   2100   1.1  christos 	SSH_SIG(USR2);
   2101   1.1  christos #undef	SSH_SIG
   2102   1.1  christos 	return "SIG (at) openssh.com";
   2103   1.1  christos }
   2104   1.1  christos 
   2105   1.1  christos static void
   2106  1.23  christos session_close_x11(struct ssh *ssh, int id)
   2107   1.1  christos {
   2108   1.1  christos 	Channel *c;
   2109   1.1  christos 
   2110  1.23  christos 	if ((c = channel_by_id(ssh, id)) == NULL) {
   2111  1.33  christos 		debug_f("x11 channel %d missing", id);
   2112   1.1  christos 	} else {
   2113   1.1  christos 		/* Detach X11 listener */
   2114  1.33  christos 		debug_f("detach x11 channel %d", id);
   2115  1.23  christos 		channel_cancel_cleanup(ssh, id);
   2116   1.1  christos 		if (c->ostate != CHAN_OUTPUT_CLOSED)
   2117  1.23  christos 			chan_mark_dead(ssh, c);
   2118   1.1  christos 	}
   2119   1.1  christos }
   2120   1.1  christos 
   2121   1.1  christos static void
   2122  1.37  christos session_close_single_x11(struct ssh *ssh, int id, int force, void *arg)
   2123   1.1  christos {
   2124   1.1  christos 	Session *s;
   2125   1.1  christos 	u_int i;
   2126   1.1  christos 
   2127  1.33  christos 	debug3_f("channel %d", id);
   2128  1.23  christos 	channel_cancel_cleanup(ssh, id);
   2129   1.1  christos 	if ((s = session_by_x11_channel(id)) == NULL)
   2130  1.33  christos 		fatal_f("no x11 channel %d", id);
   2131   1.1  christos 	for (i = 0; s->x11_chanids[i] != -1; i++) {
   2132  1.33  christos 		debug_f("session %d: closing channel %d",
   2133  1.33  christos 		    s->self, s->x11_chanids[i]);
   2134   1.1  christos 		/*
   2135   1.1  christos 		 * The channel "id" is already closing, but make sure we
   2136   1.1  christos 		 * close all of its siblings.
   2137   1.1  christos 		 */
   2138   1.1  christos 		if (s->x11_chanids[i] != id)
   2139  1.23  christos 			session_close_x11(ssh, s->x11_chanids[i]);
   2140   1.1  christos 	}
   2141  1.12  christos 	free(s->x11_chanids);
   2142   1.1  christos 	s->x11_chanids = NULL;
   2143  1.12  christos 	free(s->display);
   2144  1.12  christos 	s->display = NULL;
   2145  1.12  christos 	free(s->auth_proto);
   2146  1.12  christos 	s->auth_proto = NULL;
   2147  1.12  christos 	free(s->auth_data);
   2148  1.12  christos 	s->auth_data = NULL;
   2149  1.12  christos 	free(s->auth_display);
   2150  1.12  christos 	s->auth_display = NULL;
   2151   1.1  christos }
   2152   1.1  christos 
   2153   1.1  christos static void
   2154  1.23  christos session_exit_message(struct ssh *ssh, Session *s, int status)
   2155   1.1  christos {
   2156   1.1  christos 	Channel *c;
   2157  1.28  christos 	int r;
   2158  1.38  christos 	char *note = NULL;
   2159   1.1  christos 
   2160  1.23  christos 	if ((c = channel_lookup(ssh, s->chanid)) == NULL)
   2161  1.33  christos 		fatal_f("session %d: no channel %d", s->self, s->chanid);
   2162   1.1  christos 
   2163   1.1  christos 	if (WIFEXITED(status)) {
   2164  1.23  christos 		channel_request_start(ssh, s->chanid, "exit-status", 0);
   2165  1.28  christos 		if ((r = sshpkt_put_u32(ssh, WEXITSTATUS(status))) != 0 ||
   2166  1.28  christos 		    (r = sshpkt_send(ssh)) != 0)
   2167  1.28  christos 			sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
   2168  1.38  christos 		xasprintf(&note, "exit %d", WEXITSTATUS(status));
   2169   1.1  christos 	} else if (WIFSIGNALED(status)) {
   2170  1.23  christos 		channel_request_start(ssh, s->chanid, "exit-signal", 0);
   2171  1.28  christos 		if ((r = sshpkt_put_cstring(ssh, sig2name(WTERMSIG(status)))) != 0 ||
   2172  1.28  christos 		    (r = sshpkt_put_u8(ssh, WCOREDUMP(status)? 1 : 0)) != 0 ||
   2173  1.28  christos 		    (r = sshpkt_put_cstring(ssh, "")) != 0 ||
   2174  1.28  christos 		    (r = sshpkt_put_cstring(ssh, "")) != 0 ||
   2175  1.28  christos 		    (r = sshpkt_send(ssh)) != 0)
   2176  1.28  christos 			sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
   2177  1.38  christos 		xasprintf(&note, "signal %d%s", WTERMSIG(status),
   2178  1.38  christos 		    WCOREDUMP(status) ? " core dumped" : "");
   2179   1.1  christos 	} else {
   2180   1.1  christos 		/* Some weird exit cause.  Just exit. */
   2181  1.38  christos 		ssh_packet_disconnect(ssh, "wait returned status %04x.",
   2182  1.38  christos 		    status);
   2183   1.1  christos 	}
   2184   1.1  christos 
   2185  1.38  christos 	debug_f("session %d channel %d pid %ld %s", s->self, s->chanid,
   2186  1.38  christos 	    (long)s->pid, note == NULL ? "UNKNOWN" : note);
   2187  1.38  christos 	free(note);
   2188  1.38  christos 
   2189   1.1  christos 	/* disconnect channel */
   2190  1.33  christos 	debug_f("release channel %d", s->chanid);
   2191   1.1  christos 
   2192   1.1  christos 	/*
   2193   1.1  christos 	 * Adjust cleanup callback attachment to send close messages when
   2194   1.1  christos 	 * the channel gets EOF. The session will be then be closed
   2195  1.31  christos 	 * by session_close_by_channel when the child sessions close their fds.
   2196   1.1  christos 	 */
   2197  1.23  christos 	channel_register_cleanup(ssh, c->self, session_close_by_channel, 1);
   2198   1.1  christos 
   2199   1.1  christos 	/*
   2200   1.1  christos 	 * emulate a write failure with 'chan_write_failed', nobody will be
   2201   1.1  christos 	 * interested in data we write.
   2202   1.1  christos 	 * Note that we must not call 'chan_read_failed', since there could
   2203   1.1  christos 	 * be some more data waiting in the pipe.
   2204   1.1  christos 	 */
   2205   1.1  christos 	if (c->ostate != CHAN_OUTPUT_CLOSED)
   2206  1.23  christos 		chan_write_failed(ssh, c);
   2207   1.1  christos }
   2208   1.1  christos 
   2209   1.1  christos void
   2210  1.23  christos session_close(struct ssh *ssh, Session *s)
   2211   1.1  christos {
   2212   1.1  christos 	u_int i;
   2213   1.1  christos 
   2214  1.18  christos 	verbose("Close session: user %s from %.200s port %d id %d",
   2215  1.18  christos 	    s->pw->pw_name,
   2216  1.20  christos 	    ssh_remote_ipaddr(ssh),
   2217  1.20  christos 	    ssh_remote_port(ssh),
   2218  1.18  christos 	    s->self);
   2219  1.18  christos 
   2220   1.1  christos 	if (s->ttyfd != -1)
   2221   1.1  christos 		session_pty_cleanup(s);
   2222  1.12  christos 	free(s->term);
   2223  1.12  christos 	free(s->display);
   2224  1.12  christos 	free(s->x11_chanids);
   2225  1.12  christos 	free(s->auth_display);
   2226  1.12  christos 	free(s->auth_data);
   2227  1.12  christos 	free(s->auth_proto);
   2228  1.13  christos 	free(s->subsys);
   2229   1.1  christos 	if (s->env != NULL) {
   2230   1.1  christos 		for (i = 0; i < s->num_env; i++) {
   2231  1.12  christos 			free(s->env[i].name);
   2232  1.12  christos 			free(s->env[i].val);
   2233   1.1  christos 		}
   2234  1.12  christos 		free(s->env);
   2235   1.1  christos 	}
   2236   1.1  christos 	session_proctitle(s);
   2237   1.1  christos 	session_unused(s->self);
   2238   1.1  christos }
   2239   1.1  christos 
   2240   1.1  christos void
   2241  1.23  christos session_close_by_pid(struct ssh *ssh, pid_t pid, int status)
   2242   1.1  christos {
   2243   1.1  christos 	Session *s = session_by_pid(pid);
   2244   1.1  christos 	if (s == NULL) {
   2245  1.33  christos 		debug_f("no session for pid %ld", (long)pid);
   2246   1.1  christos 		return;
   2247   1.1  christos 	}
   2248   1.1  christos 	if (s->chanid != -1)
   2249  1.23  christos 		session_exit_message(ssh, s, status);
   2250   1.1  christos 	if (s->ttyfd != -1)
   2251   1.1  christos 		session_pty_cleanup(s);
   2252   1.1  christos 	s->pid = 0;
   2253   1.1  christos }
   2254   1.1  christos 
   2255   1.1  christos /*
   2256   1.1  christos  * this is called when a channel dies before
   2257   1.1  christos  * the session 'child' itself dies
   2258   1.1  christos  */
   2259   1.1  christos void
   2260  1.37  christos session_close_by_channel(struct ssh *ssh, int id, int force, void *arg)
   2261   1.1  christos {
   2262   1.1  christos 	Session *s = session_by_channel(id);
   2263   1.1  christos 	u_int i;
   2264   1.1  christos 
   2265   1.1  christos 	if (s == NULL) {
   2266  1.33  christos 		debug_f("no session for id %d", id);
   2267   1.1  christos 		return;
   2268   1.1  christos 	}
   2269  1.33  christos 	debug_f("channel %d child %ld", id, (long)s->pid);
   2270   1.1  christos 	if (s->pid != 0) {
   2271  1.33  christos 		debug_f("channel %d: has child, ttyfd %d", id, s->ttyfd);
   2272   1.1  christos 		/*
   2273  1.37  christos 		 * delay detach of session (unless this is a forced close),
   2274  1.37  christos 		 * but release pty, since the fd's to the child are already
   2275  1.37  christos 		 * closed
   2276   1.1  christos 		 */
   2277   1.1  christos 		if (s->ttyfd != -1)
   2278   1.1  christos 			session_pty_cleanup(s);
   2279  1.37  christos 		if (!force)
   2280  1.37  christos 			return;
   2281   1.1  christos 	}
   2282   1.1  christos 	/* detach by removing callback */
   2283  1.23  christos 	channel_cancel_cleanup(ssh, s->chanid);
   2284   1.1  christos 
   2285   1.1  christos 	/* Close any X11 listeners associated with this session */
   2286   1.1  christos 	if (s->x11_chanids != NULL) {
   2287   1.1  christos 		for (i = 0; s->x11_chanids[i] != -1; i++) {
   2288  1.23  christos 			session_close_x11(ssh, s->x11_chanids[i]);
   2289   1.1  christos 			s->x11_chanids[i] = -1;
   2290   1.1  christos 		}
   2291   1.1  christos 	}
   2292   1.1  christos 
   2293   1.1  christos 	s->chanid = -1;
   2294  1.23  christos 	session_close(ssh, s);
   2295   1.1  christos }
   2296   1.1  christos 
   2297   1.1  christos void
   2298  1.23  christos session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
   2299   1.1  christos {
   2300   1.1  christos 	int i;
   2301   1.1  christos 	for (i = 0; i < sessions_nalloc; i++) {
   2302   1.1  christos 		Session *s = &sessions[i];
   2303   1.1  christos 		if (s->used) {
   2304   1.1  christos 			if (closefunc != NULL)
   2305   1.1  christos 				closefunc(s);
   2306   1.1  christos 			else
   2307  1.23  christos 				session_close(ssh, s);
   2308   1.1  christos 		}
   2309   1.1  christos 	}
   2310   1.1  christos }
   2311   1.1  christos 
   2312   1.1  christos static char *
   2313   1.1  christos session_tty_list(void)
   2314   1.1  christos {
   2315   1.1  christos 	static char buf[1024];
   2316   1.1  christos 	int i;
   2317   1.1  christos 	buf[0] = '\0';
   2318   1.1  christos 	for (i = 0; i < sessions_nalloc; i++) {
   2319   1.1  christos 		Session *s = &sessions[i];
   2320   1.1  christos 		if (s->used && s->ttyfd != -1) {
   2321   1.2  christos 			char *p;
   2322   1.1  christos 			if (buf[0] != '\0')
   2323   1.1  christos 				strlcat(buf, ",", sizeof buf);
   2324   1.2  christos 			if ((p = strstr(s->tty, "/pts/")) != NULL)
   2325   1.2  christos 				p++;
   2326   1.2  christos 			else {
   2327   1.2  christos 				if ((p = strrchr(s->tty, '/')) != NULL)
   2328   1.2  christos 					p++;
   2329   1.2  christos 				else
   2330   1.2  christos 					p = s->tty;
   2331   1.2  christos 			}
   2332   1.2  christos 			strlcat(buf, p, sizeof buf);
   2333   1.1  christos 		}
   2334   1.1  christos 	}
   2335   1.1  christos 	if (buf[0] == '\0')
   2336   1.1  christos 		strlcpy(buf, "notty", sizeof buf);
   2337   1.1  christos 	return buf;
   2338   1.1  christos }
   2339   1.1  christos 
   2340   1.1  christos void
   2341   1.1  christos session_proctitle(Session *s)
   2342   1.1  christos {
   2343   1.1  christos 	if (s->pw == NULL)
   2344   1.1  christos 		error("no user for session %d", s->self);
   2345   1.1  christos 	else
   2346   1.1  christos 		setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
   2347   1.1  christos }
   2348   1.1  christos 
   2349   1.1  christos int
   2350  1.23  christos session_setup_x11fwd(struct ssh *ssh, Session *s)
   2351   1.1  christos {
   2352   1.1  christos 	struct stat st;
   2353   1.1  christos 	char display[512], auth_display[512];
   2354  1.13  christos 	char hostname[NI_MAXHOST];
   2355   1.1  christos 	u_int i;
   2356   1.1  christos 
   2357  1.24  christos 	if (!auth_opts->permit_x11_forwarding_flag) {
   2358  1.28  christos 		ssh_packet_send_debug(ssh, "X11 forwarding disabled by key options.");
   2359   1.1  christos 		return 0;
   2360   1.1  christos 	}
   2361  1.42       kim 	if (!options.x11_forwarding || options.disable_forwarding) {
   2362   1.1  christos 		debug("X11 forwarding disabled in server configuration file.");
   2363   1.1  christos 		return 0;
   2364   1.1  christos 	}
   2365  1.14  christos 	if (options.xauth_location == NULL ||
   2366   1.1  christos 	    (stat(options.xauth_location, &st) == -1)) {
   2367  1.28  christos 		ssh_packet_send_debug(ssh, "No xauth program; cannot forward X11.");
   2368   1.1  christos 		return 0;
   2369   1.1  christos 	}
   2370   1.1  christos 	if (s->display != NULL) {
   2371   1.1  christos 		debug("X11 display already set.");
   2372   1.1  christos 		return 0;
   2373   1.1  christos 	}
   2374  1.23  christos 	if (x11_create_display_inet(ssh, options.x11_display_offset,
   2375   1.1  christos 	    options.x11_use_localhost, s->single_connection,
   2376   1.1  christos 	    &s->display_number, &s->x11_chanids) == -1) {
   2377   1.1  christos 		debug("x11_create_display_inet failed.");
   2378   1.1  christos 		return 0;
   2379   1.1  christos 	}
   2380   1.1  christos 	for (i = 0; s->x11_chanids[i] != -1; i++) {
   2381  1.23  christos 		channel_register_cleanup(ssh, s->x11_chanids[i],
   2382   1.1  christos 		    session_close_single_x11, 0);
   2383   1.1  christos 	}
   2384   1.1  christos 
   2385   1.1  christos 	/* Set up a suitable value for the DISPLAY variable. */
   2386  1.29  christos 	if (gethostname(hostname, sizeof(hostname)) == -1)
   2387   1.1  christos 		fatal("gethostname: %.100s", strerror(errno));
   2388   1.1  christos 	/*
   2389   1.1  christos 	 * auth_display must be used as the displayname when the
   2390   1.1  christos 	 * authorization entry is added with xauth(1).  This will be
   2391   1.1  christos 	 * different than the DISPLAY string for localhost displays.
   2392   1.1  christos 	 */
   2393   1.1  christos 	if (options.x11_use_localhost) {
   2394   1.1  christos 		snprintf(display, sizeof display, "localhost:%u.%u",
   2395   1.1  christos 		    s->display_number, s->screen);
   2396   1.1  christos 		snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
   2397   1.1  christos 		    s->display_number, s->screen);
   2398   1.1  christos 		s->display = xstrdup(display);
   2399   1.1  christos 		s->auth_display = xstrdup(auth_display);
   2400   1.1  christos 	} else {
   2401   1.1  christos 		snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
   2402   1.1  christos 		    s->display_number, s->screen);
   2403   1.1  christos 		s->display = xstrdup(display);
   2404   1.1  christos 		s->auth_display = xstrdup(display);
   2405   1.1  christos 	}
   2406   1.1  christos 
   2407   1.1  christos 	return 1;
   2408   1.1  christos }
   2409   1.1  christos 
   2410   1.1  christos static void
   2411  1.23  christos do_authenticated2(struct ssh *ssh, Authctxt *authctxt)
   2412   1.1  christos {
   2413  1.23  christos 	server_loop2(ssh, authctxt);
   2414   1.1  christos }
   2415   1.1  christos 
   2416   1.1  christos void
   2417  1.23  christos do_cleanup(struct ssh *ssh, Authctxt *authctxt)
   2418   1.1  christos {
   2419   1.1  christos 	static int called = 0;
   2420   1.1  christos 
   2421   1.1  christos 	debug("do_cleanup");
   2422   1.1  christos 
   2423   1.1  christos 	/* no cleanup if we're in the child for login shell */
   2424   1.1  christos 	if (is_child)
   2425   1.1  christos 		return;
   2426   1.1  christos 
   2427   1.1  christos 	/* avoid double cleanup */
   2428   1.1  christos 	if (called)
   2429   1.1  christos 		return;
   2430   1.1  christos 	called = 1;
   2431   1.1  christos 
   2432   1.1  christos 	if (authctxt == NULL || !authctxt->authenticated)
   2433   1.1  christos 		return;
   2434   1.2  christos #ifdef KRB4
   2435   1.2  christos 	if (options.kerberos_ticket_cleanup)
   2436   1.2  christos 		krb4_cleanup_proc(authctxt);
   2437   1.2  christos #endif
   2438   1.1  christos #ifdef KRB5
   2439   1.1  christos 	if (options.kerberos_ticket_cleanup &&
   2440   1.1  christos 	    authctxt->krb5_ctx)
   2441   1.1  christos 		krb5_cleanup_proc(authctxt);
   2442   1.1  christos #endif
   2443   1.1  christos 
   2444   1.1  christos #ifdef GSSAPI
   2445  1.21  christos 	if (options.gss_cleanup_creds)
   2446   1.1  christos 		ssh_gssapi_cleanup_creds();
   2447   1.1  christos #endif
   2448   1.1  christos 
   2449   1.2  christos #ifdef USE_PAM
   2450   1.2  christos 	if (options.use_pam) {
   2451   1.2  christos 		sshpam_cleanup();
   2452   1.2  christos 		sshpam_thread_cleanup();
   2453   1.2  christos 	}
   2454   1.2  christos #endif
   2455   1.2  christos 
   2456   1.1  christos 	/* remove agent socket */
   2457   1.1  christos 	auth_sock_cleanup_proc(authctxt->pw);
   2458   1.1  christos 
   2459  1.23  christos 	/* remove userauth info */
   2460  1.23  christos 	if (auth_info_file != NULL) {
   2461  1.23  christos 		temporarily_use_uid(authctxt->pw);
   2462  1.23  christos 		unlink(auth_info_file);
   2463  1.23  christos 		restore_uid();
   2464  1.23  christos 		free(auth_info_file);
   2465  1.23  christos 		auth_info_file = NULL;
   2466  1.23  christos 	}
   2467  1.23  christos 
   2468   1.1  christos 	/*
   2469   1.1  christos 	 * Cleanup ptys/utmp only if privsep is disabled,
   2470   1.1  christos 	 * or if running in monitor.
   2471   1.1  christos 	 */
   2472  1.40  christos 	if (mm_is_monitor())
   2473  1.23  christos 		session_destroy_all(ssh, session_pty_cleanup2);
   2474   1.1  christos }
   2475  1.20  christos 
   2476  1.20  christos /* Return a name for the remote host that fits inside utmp_size */
   2477  1.20  christos 
   2478  1.20  christos const char *
   2479  1.20  christos session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
   2480  1.20  christos {
   2481  1.20  christos 	const char *remote = "";
   2482  1.20  christos 
   2483  1.20  christos 	if (utmp_size > 0)
   2484  1.20  christos 		remote = auth_get_canonical_hostname(ssh, use_dns);
   2485  1.20  christos 	if (utmp_size == 0 || strlen(remote) > utmp_size)
   2486  1.20  christos 		remote = ssh_remote_ipaddr(ssh);
   2487  1.20  christos 	return remote;
   2488  1.20  christos }
   2489  1.20  christos 
   2490