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