xref: /titanic_50/usr/src/cmd/ssh/sshd/session.c (revision b9aa66a73c9016cf5c71fe80efe90ce9f2ca5c73)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
37c478bd9Sstevel@tonic-gate  *                    All rights reserved
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
67c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
77c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
87c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
97c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * SSH2 support by Markus Friedl.
127c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
157c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
167c478bd9Sstevel@tonic-gate  * are met:
177c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
187c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
197c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
207c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
217c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
247c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
257c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
267c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
277c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
287c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
297c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
307c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
317c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
327c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate /*
357a71c13fSZdenek Kotala  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
367c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "includes.h"
407c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: session.c,v 1.150 2002/09/16 19:55:33 stevesk Exp $");
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
437c478bd9Sstevel@tonic-gate #include <deflt.h>
447c478bd9Sstevel@tonic-gate #include <ulimit.h>
457c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
467c478bd9Sstevel@tonic-gate 
47a6e0e77dSjp161948 #ifdef HAVE_LIBGEN_H
48a6e0e77dSjp161948 #include <libgen.h>
49a6e0e77dSjp161948 #endif
50a6e0e77dSjp161948 
516f8d59d8SJan Pechanec #include <priv.h>
526f8d59d8SJan Pechanec 
537c478bd9Sstevel@tonic-gate #include "ssh.h"
547c478bd9Sstevel@tonic-gate #include "ssh1.h"
557c478bd9Sstevel@tonic-gate #include "ssh2.h"
567c478bd9Sstevel@tonic-gate #include "xmalloc.h"
577c478bd9Sstevel@tonic-gate #include "sshpty.h"
587c478bd9Sstevel@tonic-gate #include "packet.h"
597c478bd9Sstevel@tonic-gate #include "buffer.h"
607c478bd9Sstevel@tonic-gate #include "mpaux.h"
617c478bd9Sstevel@tonic-gate #include "uidswap.h"
627c478bd9Sstevel@tonic-gate #include "compat.h"
637c478bd9Sstevel@tonic-gate #include "channels.h"
647c478bd9Sstevel@tonic-gate #include "bufaux.h"
657c478bd9Sstevel@tonic-gate #include "auth.h"
667c478bd9Sstevel@tonic-gate #include "auth-options.h"
677c478bd9Sstevel@tonic-gate #include "pathnames.h"
687c478bd9Sstevel@tonic-gate #include "log.h"
697c478bd9Sstevel@tonic-gate #include "servconf.h"
707c478bd9Sstevel@tonic-gate #include "sshlogin.h"
717c478bd9Sstevel@tonic-gate #include "serverloop.h"
727c478bd9Sstevel@tonic-gate #include "canohost.h"
737c478bd9Sstevel@tonic-gate #include "session.h"
746f8d59d8SJan Pechanec #include "tildexpand.h"
756f8d59d8SJan Pechanec #include "misc.h"
766f8d59d8SJan Pechanec #include "sftp.h"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #ifdef USE_PAM
797c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
807c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #ifdef GSSAPI
837c478bd9Sstevel@tonic-gate #include "ssh-gss.h"
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
877c478bd9Sstevel@tonic-gate #include "altprivsep.h"
887c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
917c478bd9Sstevel@tonic-gate #include <windows.h>
927c478bd9Sstevel@tonic-gate #include <sys/cygwin.h>
937c478bd9Sstevel@tonic-gate #define is_winnt       (GetVersion() < 0x80000000)
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* func */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate Session *session_new(void);
997c478bd9Sstevel@tonic-gate void	session_set_fds(Session *, int, int, int);
1007c478bd9Sstevel@tonic-gate void	session_pty_cleanup(void *);
101a6e0e77dSjp161948 void	session_xauthfile_cleanup(void *s);
1027c478bd9Sstevel@tonic-gate void	session_proctitle(Session *);
1037c478bd9Sstevel@tonic-gate int	session_setup_x11fwd(Session *);
1047c478bd9Sstevel@tonic-gate void	do_exec_pty(Session *, const char *);
1057c478bd9Sstevel@tonic-gate void	do_exec_no_pty(Session *, const char *);
1067c478bd9Sstevel@tonic-gate void	do_exec(Session *, const char *);
1077c478bd9Sstevel@tonic-gate void	do_login(Session *, const char *);
1087c478bd9Sstevel@tonic-gate void	do_child(Session *, const char *);
1097c478bd9Sstevel@tonic-gate void	do_motd(void);
1107c478bd9Sstevel@tonic-gate int	check_quietlogin(Session *, const char *);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static void do_authenticated1(Authctxt *);
1137c478bd9Sstevel@tonic-gate static void do_authenticated2(Authctxt *);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static int  session_pty_req(Session *);
1167c478bd9Sstevel@tonic-gate static int  session_env_req(Session *s);
1177c478bd9Sstevel@tonic-gate static void session_free_env(char ***envp);
1186f8d59d8SJan Pechanec static void safely_chroot(const char *path, uid_t uid);
1196f8d59d8SJan Pechanec static void drop_privs(uid_t uid);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #ifdef USE_PAM
1227c478bd9Sstevel@tonic-gate static void session_do_pam(Session *, int);
1237c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* import */
1267c478bd9Sstevel@tonic-gate extern ServerOptions options;
1277c478bd9Sstevel@tonic-gate extern char *__progname;
1287c478bd9Sstevel@tonic-gate extern int log_stderr;
1297c478bd9Sstevel@tonic-gate extern int debug_flag;
1307c478bd9Sstevel@tonic-gate extern u_int utmp_len;
1317c478bd9Sstevel@tonic-gate extern void destroy_sensitive_data(void);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #ifdef GSSAPI
1347c478bd9Sstevel@tonic-gate extern Gssctxt *xxx_gssctxt;
1357c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* original command from peer. */
1387c478bd9Sstevel@tonic-gate const char *original_command = NULL;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* data */
1417c478bd9Sstevel@tonic-gate #define MAX_SESSIONS 10
1427c478bd9Sstevel@tonic-gate Session	sessions[MAX_SESSIONS];
1437c478bd9Sstevel@tonic-gate 
1446f8d59d8SJan Pechanec #define	SUBSYSTEM_NONE		0
1456f8d59d8SJan Pechanec #define	SUBSYSTEM_EXT		1
1466f8d59d8SJan Pechanec #define	SUBSYSTEM_INT_SFTP	2
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
1497c478bd9Sstevel@tonic-gate login_cap_t *lc;
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* Name and directory of socket for authentication agent forwarding. */
1537c478bd9Sstevel@tonic-gate static char *auth_sock_name = NULL;
1547c478bd9Sstevel@tonic-gate static char *auth_sock_dir = NULL;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /* removes the agent forwarding socket */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static void
1597c478bd9Sstevel@tonic-gate auth_sock_cleanup_proc(void *_pw)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct passwd *pw = _pw;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL) {
1647c478bd9Sstevel@tonic-gate 		temporarily_use_uid(pw);
1657c478bd9Sstevel@tonic-gate 		unlink(auth_sock_name);
1667c478bd9Sstevel@tonic-gate 		rmdir(auth_sock_dir);
1677c478bd9Sstevel@tonic-gate 		auth_sock_name = NULL;
1687c478bd9Sstevel@tonic-gate 		restore_uid();
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static int
1737c478bd9Sstevel@tonic-gate auth_input_request_forwarding(struct passwd * pw)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	Channel *nc;
1767c478bd9Sstevel@tonic-gate 	int sock;
1777c478bd9Sstevel@tonic-gate 	struct sockaddr_un sunaddr;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL) {
1807c478bd9Sstevel@tonic-gate 		error("authentication forwarding requested twice.");
1817c478bd9Sstevel@tonic-gate 		return 0;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* Temporarily drop privileged uid for mkdir/bind. */
1857c478bd9Sstevel@tonic-gate 	temporarily_use_uid(pw);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* Allocate a buffer for the socket name, and format the name. */
1887c478bd9Sstevel@tonic-gate 	auth_sock_name = xmalloc(MAXPATHLEN);
1897c478bd9Sstevel@tonic-gate 	auth_sock_dir = xmalloc(MAXPATHLEN);
1907c478bd9Sstevel@tonic-gate 	strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* Create private directory for socket */
1937c478bd9Sstevel@tonic-gate 	if (mkdtemp(auth_sock_dir) == NULL) {
1947c478bd9Sstevel@tonic-gate 		packet_send_debug("Agent forwarding disabled: "
1957c478bd9Sstevel@tonic-gate 		    "mkdtemp() failed: %.100s", strerror(errno));
1967c478bd9Sstevel@tonic-gate 		restore_uid();
1977c478bd9Sstevel@tonic-gate 		xfree(auth_sock_name);
1987c478bd9Sstevel@tonic-gate 		xfree(auth_sock_dir);
1997c478bd9Sstevel@tonic-gate 		auth_sock_name = NULL;
2007c478bd9Sstevel@tonic-gate 		auth_sock_dir = NULL;
2017c478bd9Sstevel@tonic-gate 		return 0;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
2047c478bd9Sstevel@tonic-gate 		 auth_sock_dir, (long) getpid());
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/* delete agent socket on fatal() */
2077c478bd9Sstevel@tonic-gate 	fatal_add_cleanup(auth_sock_cleanup_proc, pw);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/* Create the socket. */
2107c478bd9Sstevel@tonic-gate 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
2117c478bd9Sstevel@tonic-gate 	if (sock < 0)
2127c478bd9Sstevel@tonic-gate 		packet_disconnect("socket: %.100s", strerror(errno));
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* Bind it to the name. */
2157c478bd9Sstevel@tonic-gate 	memset(&sunaddr, 0, sizeof(sunaddr));
2167c478bd9Sstevel@tonic-gate 	sunaddr.sun_family = AF_UNIX;
2177c478bd9Sstevel@tonic-gate 	strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2207c478bd9Sstevel@tonic-gate 		packet_disconnect("bind: %.100s", strerror(errno));
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* Restore the privileged uid. */
2237c478bd9Sstevel@tonic-gate 	restore_uid();
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* Start listening on the socket. */
2267c478bd9Sstevel@tonic-gate 	if (listen(sock, 5) < 0)
2277c478bd9Sstevel@tonic-gate 		packet_disconnect("listen: %.100s", strerror(errno));
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* Allocate a channel for the authentication agent socket. */
2307c478bd9Sstevel@tonic-gate 	nc = channel_new("auth socket",
2317c478bd9Sstevel@tonic-gate 	    SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2327c478bd9Sstevel@tonic-gate 	    CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2337c478bd9Sstevel@tonic-gate 	    0, xstrdup("auth socket"), 1);
2347c478bd9Sstevel@tonic-gate 	strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
2357c478bd9Sstevel@tonic-gate 	return 1;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate void
2407c478bd9Sstevel@tonic-gate do_authenticated(Authctxt *authctxt)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	/* setup the channel layer */
2437c478bd9Sstevel@tonic-gate 	if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
2447c478bd9Sstevel@tonic-gate 		channel_permit_all_opens();
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (compat20)
2477c478bd9Sstevel@tonic-gate 		do_authenticated2(authctxt);
2487c478bd9Sstevel@tonic-gate 	else
2497c478bd9Sstevel@tonic-gate 		do_authenticated1(authctxt);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* remove agent socket */
2527c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL)
2537c478bd9Sstevel@tonic-gate 		auth_sock_cleanup_proc(authctxt->pw);
2547c478bd9Sstevel@tonic-gate #ifdef KRB4
2557c478bd9Sstevel@tonic-gate 	if (options.kerberos_ticket_cleanup)
2567c478bd9Sstevel@tonic-gate 		krb4_cleanup_proc(authctxt);
2577c478bd9Sstevel@tonic-gate #endif
2587c478bd9Sstevel@tonic-gate #ifdef KRB5
2597c478bd9Sstevel@tonic-gate 	if (options.kerberos_ticket_cleanup)
2607c478bd9Sstevel@tonic-gate 		krb5_cleanup_proc(authctxt);
2617c478bd9Sstevel@tonic-gate #endif
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate  * Prepares for an interactive session.  This is called after the user has
2667c478bd9Sstevel@tonic-gate  * been successfully authenticated.  During this message exchange, pseudo
2677c478bd9Sstevel@tonic-gate  * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
2687c478bd9Sstevel@tonic-gate  * are requested, etc.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate static void
2717c478bd9Sstevel@tonic-gate do_authenticated1(Authctxt *authctxt)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	Session *s;
2747c478bd9Sstevel@tonic-gate 	char *command;
2757c478bd9Sstevel@tonic-gate 	int success, type, screen_flag;
2767c478bd9Sstevel@tonic-gate 	int enable_compression_after_reply = 0;
2777c478bd9Sstevel@tonic-gate 	u_int proto_len, data_len, dlen, compression_level = 0;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	s = session_new();
2807c478bd9Sstevel@tonic-gate 	s->authctxt = authctxt;
2817c478bd9Sstevel@tonic-gate 	s->pw = authctxt->pw;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * We stay in this loop until the client requests to execute a shell
2857c478bd9Sstevel@tonic-gate 	 * or a command.
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	for (;;) {
2887c478bd9Sstevel@tonic-gate 		success = 0;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		/* Get a packet from the client. */
2917c478bd9Sstevel@tonic-gate 		type = packet_read();
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		/* Process the packet. */
2947c478bd9Sstevel@tonic-gate 		switch (type) {
2957c478bd9Sstevel@tonic-gate 		case SSH_CMSG_REQUEST_COMPRESSION:
2967c478bd9Sstevel@tonic-gate 			compression_level = packet_get_int();
2977c478bd9Sstevel@tonic-gate 			packet_check_eom();
2987c478bd9Sstevel@tonic-gate 			if (compression_level < 1 || compression_level > 9) {
2997c478bd9Sstevel@tonic-gate 				packet_send_debug("Received illegal compression level %d.",
3007c478bd9Sstevel@tonic-gate 				    compression_level);
3017c478bd9Sstevel@tonic-gate 				break;
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 			if (!options.compression) {
3047c478bd9Sstevel@tonic-gate 				debug2("compression disabled");
3057c478bd9Sstevel@tonic-gate 				break;
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 			/* Enable compression after we have responded with SUCCESS. */
3087c478bd9Sstevel@tonic-gate 			enable_compression_after_reply = 1;
3097c478bd9Sstevel@tonic-gate 			success = 1;
3107c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		case SSH_CMSG_REQUEST_PTY:
3137c478bd9Sstevel@tonic-gate 			success = session_pty_req(s);
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		case SSH_CMSG_X11_REQUEST_FORWARDING:
3177c478bd9Sstevel@tonic-gate 			s->auth_proto = packet_get_string(&proto_len);
3187c478bd9Sstevel@tonic-gate 			s->auth_data = packet_get_string(&data_len);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 			screen_flag = packet_get_protocol_flags() &
3217c478bd9Sstevel@tonic-gate 			    SSH_PROTOFLAG_SCREEN_NUMBER;
3227c478bd9Sstevel@tonic-gate 			debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 			if (packet_remaining() == 4) {
3257c478bd9Sstevel@tonic-gate 				if (!screen_flag)
3267c478bd9Sstevel@tonic-gate 					debug2("Buggy client: "
3277c478bd9Sstevel@tonic-gate 					    "X11 screen flag missing");
3287c478bd9Sstevel@tonic-gate 				s->screen = packet_get_int();
3297c478bd9Sstevel@tonic-gate 			} else {
3307c478bd9Sstevel@tonic-gate 				s->screen = 0;
3317c478bd9Sstevel@tonic-gate 			}
3327c478bd9Sstevel@tonic-gate 			packet_check_eom();
3337c478bd9Sstevel@tonic-gate 			success = session_setup_x11fwd(s);
3347c478bd9Sstevel@tonic-gate 			if (!success) {
3357c478bd9Sstevel@tonic-gate 				xfree(s->auth_proto);
3367c478bd9Sstevel@tonic-gate 				xfree(s->auth_data);
3377c478bd9Sstevel@tonic-gate 				s->auth_proto = NULL;
3387c478bd9Sstevel@tonic-gate 				s->auth_data = NULL;
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AGENT_REQUEST_FORWARDING:
3437c478bd9Sstevel@tonic-gate 			if (no_agent_forwarding_flag || compat13) {
3447c478bd9Sstevel@tonic-gate 				debug("Authentication agent forwarding not permitted for this authentication.");
3457c478bd9Sstevel@tonic-gate 				break;
3467c478bd9Sstevel@tonic-gate 			}
3477c478bd9Sstevel@tonic-gate 			debug("Received authentication agent forwarding request.");
3487c478bd9Sstevel@tonic-gate 			success = auth_input_request_forwarding(s->pw);
3497c478bd9Sstevel@tonic-gate 			break;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		case SSH_CMSG_PORT_FORWARD_REQUEST:
3527c478bd9Sstevel@tonic-gate 			if (no_port_forwarding_flag) {
3537c478bd9Sstevel@tonic-gate 				debug("Port forwarding not permitted for this authentication.");
3547c478bd9Sstevel@tonic-gate 				break;
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 			if (!options.allow_tcp_forwarding) {
3577c478bd9Sstevel@tonic-gate 				debug("Port forwarding not permitted.");
3587c478bd9Sstevel@tonic-gate 				break;
3597c478bd9Sstevel@tonic-gate 			}
3607c478bd9Sstevel@tonic-gate 			debug("Received TCP/IP port forwarding request.");
3617c478bd9Sstevel@tonic-gate 			channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
3627c478bd9Sstevel@tonic-gate 			success = 1;
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		case SSH_CMSG_MAX_PACKET_SIZE:
3667c478bd9Sstevel@tonic-gate 			if (packet_set_maxsize(packet_get_int()) > 0)
3677c478bd9Sstevel@tonic-gate 				success = 1;
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
3717c478bd9Sstevel@tonic-gate 		case SSH_CMSG_HAVE_KERBEROS_TGT:
3727c478bd9Sstevel@tonic-gate 			if (!options.kerberos_tgt_passing) {
3737c478bd9Sstevel@tonic-gate 				verbose("Kerberos TGT passing disabled.");
3747c478bd9Sstevel@tonic-gate 			} else {
3757c478bd9Sstevel@tonic-gate 				char *kdata = packet_get_string(&dlen);
3767c478bd9Sstevel@tonic-gate 				packet_check_eom();
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 				/* XXX - 0x41, see creds_to_radix version */
3797c478bd9Sstevel@tonic-gate 				if (kdata[0] != 0x41) {
3807c478bd9Sstevel@tonic-gate #ifdef KRB5
3817c478bd9Sstevel@tonic-gate 					krb5_data tgt;
3827c478bd9Sstevel@tonic-gate 					tgt.data = kdata;
3837c478bd9Sstevel@tonic-gate 					tgt.length = dlen;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 					if (auth_krb5_tgt(s->authctxt, &tgt))
3867c478bd9Sstevel@tonic-gate 						success = 1;
3877c478bd9Sstevel@tonic-gate 					else
3887c478bd9Sstevel@tonic-gate 						verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
3897c478bd9Sstevel@tonic-gate #endif /* KRB5 */
3907c478bd9Sstevel@tonic-gate 				} else {
3917c478bd9Sstevel@tonic-gate #ifdef AFS
3927c478bd9Sstevel@tonic-gate 					if (auth_krb4_tgt(s->authctxt, kdata))
3937c478bd9Sstevel@tonic-gate 						success = 1;
3947c478bd9Sstevel@tonic-gate 					else
3957c478bd9Sstevel@tonic-gate 						verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
3967c478bd9Sstevel@tonic-gate #endif /* AFS */
3977c478bd9Sstevel@tonic-gate 				}
3987c478bd9Sstevel@tonic-gate 				xfree(kdata);
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate #endif /* AFS || KRB5 */
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate #ifdef AFS
4047c478bd9Sstevel@tonic-gate 		case SSH_CMSG_HAVE_AFS_TOKEN:
4057c478bd9Sstevel@tonic-gate 			if (!options.afs_token_passing || !k_hasafs()) {
4067c478bd9Sstevel@tonic-gate 				verbose("AFS token passing disabled.");
4077c478bd9Sstevel@tonic-gate 			} else {
4087c478bd9Sstevel@tonic-gate 				/* Accept AFS token. */
4097c478bd9Sstevel@tonic-gate 				char *token = packet_get_string(&dlen);
4107c478bd9Sstevel@tonic-gate 				packet_check_eom();
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 				if (auth_afs_token(s->authctxt, token))
4137c478bd9Sstevel@tonic-gate 					success = 1;
4147c478bd9Sstevel@tonic-gate 				else
4157c478bd9Sstevel@tonic-gate 					verbose("AFS token refused for %.100s",
4167c478bd9Sstevel@tonic-gate 					    s->authctxt->user);
4177c478bd9Sstevel@tonic-gate 				xfree(token);
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate #endif /* AFS */
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		case SSH_CMSG_EXEC_SHELL:
4237c478bd9Sstevel@tonic-gate 		case SSH_CMSG_EXEC_CMD:
4247c478bd9Sstevel@tonic-gate 			if (type == SSH_CMSG_EXEC_CMD) {
4257c478bd9Sstevel@tonic-gate 				command = packet_get_string(&dlen);
4267c478bd9Sstevel@tonic-gate 				debug("Exec command '%.500s'", command);
4277c478bd9Sstevel@tonic-gate 				do_exec(s, command);
4287c478bd9Sstevel@tonic-gate 				xfree(command);
4297c478bd9Sstevel@tonic-gate 			} else {
4307c478bd9Sstevel@tonic-gate 				do_exec(s, NULL);
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			packet_check_eom();
4337c478bd9Sstevel@tonic-gate 			session_close(s);
4347c478bd9Sstevel@tonic-gate 			return;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		default:
4377c478bd9Sstevel@tonic-gate 			/*
4387c478bd9Sstevel@tonic-gate 			 * Any unknown messages in this phase are ignored,
4397c478bd9Sstevel@tonic-gate 			 * and a failure message is returned.
4407c478bd9Sstevel@tonic-gate 			 */
4417c478bd9Sstevel@tonic-gate 			log("Unknown packet type received after authentication: %d", type);
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 		packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
4447c478bd9Sstevel@tonic-gate 		packet_send();
4457c478bd9Sstevel@tonic-gate 		packet_write_wait();
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		/* Enable compression now that we have replied if appropriate. */
4487c478bd9Sstevel@tonic-gate 		if (enable_compression_after_reply) {
4497c478bd9Sstevel@tonic-gate 			enable_compression_after_reply = 0;
4507c478bd9Sstevel@tonic-gate 			packet_start_compression(compression_level);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate  * This is called to fork and execute a command when we have no tty.  This
4577c478bd9Sstevel@tonic-gate  * will call do_child from the child, and server_loop from the parent after
4587c478bd9Sstevel@tonic-gate  * setting up file descriptors and such.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate void
4617c478bd9Sstevel@tonic-gate do_exec_no_pty(Session *s, const char *command)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	pid_t pid;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate #ifdef USE_PIPES
4667c478bd9Sstevel@tonic-gate 	int pin[2], pout[2], perr[2];
4677c478bd9Sstevel@tonic-gate 	/* Allocate pipes for communicating with the program. */
4687c478bd9Sstevel@tonic-gate 	if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
4697c478bd9Sstevel@tonic-gate 		packet_disconnect("Could not create pipes: %.100s",
4707c478bd9Sstevel@tonic-gate 				  strerror(errno));
4717c478bd9Sstevel@tonic-gate #else /* USE_PIPES */
4727c478bd9Sstevel@tonic-gate 	int inout[2], err[2];
4737c478bd9Sstevel@tonic-gate 	/* Uses socket pairs to communicate with the program. */
4747c478bd9Sstevel@tonic-gate 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
4757c478bd9Sstevel@tonic-gate 	    socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
4767c478bd9Sstevel@tonic-gate 		packet_disconnect("Could not create socket pairs: %.100s",
4777c478bd9Sstevel@tonic-gate 				  strerror(errno));
4787c478bd9Sstevel@tonic-gate #endif /* USE_PIPES */
4797c478bd9Sstevel@tonic-gate 	if (s == NULL)
4807c478bd9Sstevel@tonic-gate 		fatal("do_exec_no_pty: no session");
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	session_proctitle(s);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* Fork the child. */
4857c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
4867c478bd9Sstevel@tonic-gate 		fatal_remove_all_cleanups();
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		/* Child.  Reinitialize the log since the pid has changed. */
4897c478bd9Sstevel@tonic-gate 		log_init(__progname, options.log_level, options.log_facility, log_stderr);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		/*
4927c478bd9Sstevel@tonic-gate 		 * Create a new session and process group since the 4.4BSD
4937c478bd9Sstevel@tonic-gate 		 * setlogin() affects the entire process group.
4947c478bd9Sstevel@tonic-gate 		 */
4957c478bd9Sstevel@tonic-gate 		if (setsid() < 0)
4967c478bd9Sstevel@tonic-gate 			error("setsid failed: %.100s", strerror(errno));
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate #ifdef USE_PIPES
4997c478bd9Sstevel@tonic-gate 		/*
5007c478bd9Sstevel@tonic-gate 		 * Redirect stdin.  We close the parent side of the socket
5017c478bd9Sstevel@tonic-gate 		 * pair, and make the child side the standard input.
5027c478bd9Sstevel@tonic-gate 		 */
5037c478bd9Sstevel@tonic-gate 		close(pin[1]);
5047c478bd9Sstevel@tonic-gate 		if (dup2(pin[0], 0) < 0)
5057c478bd9Sstevel@tonic-gate 			perror("dup2 stdin");
5067c478bd9Sstevel@tonic-gate 		close(pin[0]);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/* Redirect stdout. */
5097c478bd9Sstevel@tonic-gate 		close(pout[0]);
5107c478bd9Sstevel@tonic-gate 		if (dup2(pout[1], 1) < 0)
5117c478bd9Sstevel@tonic-gate 			perror("dup2 stdout");
5127c478bd9Sstevel@tonic-gate 		close(pout[1]);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		/* Redirect stderr. */
5157c478bd9Sstevel@tonic-gate 		close(perr[0]);
5167c478bd9Sstevel@tonic-gate 		if (dup2(perr[1], 2) < 0)
5177c478bd9Sstevel@tonic-gate 			perror("dup2 stderr");
5187c478bd9Sstevel@tonic-gate 		close(perr[1]);
5197c478bd9Sstevel@tonic-gate #else /* USE_PIPES */
5207c478bd9Sstevel@tonic-gate 		/*
5217c478bd9Sstevel@tonic-gate 		 * Redirect stdin, stdout, and stderr.  Stdin and stdout will
5227c478bd9Sstevel@tonic-gate 		 * use the same socket, as some programs (particularly rdist)
5237c478bd9Sstevel@tonic-gate 		 * seem to depend on it.
5247c478bd9Sstevel@tonic-gate 		 */
5257c478bd9Sstevel@tonic-gate 		close(inout[1]);
5267c478bd9Sstevel@tonic-gate 		close(err[1]);
5277c478bd9Sstevel@tonic-gate 		if (dup2(inout[0], 0) < 0)	/* stdin */
5287c478bd9Sstevel@tonic-gate 			perror("dup2 stdin");
5297c478bd9Sstevel@tonic-gate 		if (dup2(inout[0], 1) < 0)	/* stdout.  Note: same socket as stdin. */
5307c478bd9Sstevel@tonic-gate 			perror("dup2 stdout");
5317c478bd9Sstevel@tonic-gate 		if (dup2(err[0], 2) < 0)	/* stderr */
5327c478bd9Sstevel@tonic-gate 			perror("dup2 stderr");
5337c478bd9Sstevel@tonic-gate #endif /* USE_PIPES */
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate #ifdef _UNICOS
5367c478bd9Sstevel@tonic-gate 		cray_init_job(s->pw); /* set up cray jid and tmpdir */
5377c478bd9Sstevel@tonic-gate #endif
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		/* Do processing for the child (exec command etc). */
5407c478bd9Sstevel@tonic-gate 		do_child(s, command);
5417c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate #ifdef _UNICOS
5447c478bd9Sstevel@tonic-gate 	signal(WJSIGNAL, cray_job_termination_handler);
5457c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
5467c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
5477c478bd9Sstevel@tonic-gate 	if (is_winnt)
5487c478bd9Sstevel@tonic-gate 		cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
5497c478bd9Sstevel@tonic-gate #endif
5507c478bd9Sstevel@tonic-gate 	if (pid < 0)
5517c478bd9Sstevel@tonic-gate 		packet_disconnect("fork failed: %.100s", strerror(errno));
5526f8d59d8SJan Pechanec 
5537c478bd9Sstevel@tonic-gate 	s->pid = pid;
5547c478bd9Sstevel@tonic-gate 	/* Set interactive/non-interactive mode. */
5557c478bd9Sstevel@tonic-gate 	packet_set_interactive(s->display != NULL);
5567c478bd9Sstevel@tonic-gate #ifdef USE_PIPES
5577c478bd9Sstevel@tonic-gate 	/* We are the parent.  Close the child sides of the pipes. */
5587c478bd9Sstevel@tonic-gate 	close(pin[0]);
5597c478bd9Sstevel@tonic-gate 	close(pout[1]);
5607c478bd9Sstevel@tonic-gate 	close(perr[1]);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (compat20) {
5637c478bd9Sstevel@tonic-gate 		session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
5647a71c13fSZdenek Kotala 		if (s->is_subsystem)
5657a71c13fSZdenek Kotala                         close(perr[0]);
5667c478bd9Sstevel@tonic-gate 		/* Don't close channel before sending exit-status! */
5677c478bd9Sstevel@tonic-gate 		channel_set_wait_for_exit(s->chanid, 1);
5687c478bd9Sstevel@tonic-gate 	} else {
5697c478bd9Sstevel@tonic-gate 		/* Enter the interactive session. */
5707c478bd9Sstevel@tonic-gate 		server_loop(pid, pin[1], pout[0], perr[0]);
5717c478bd9Sstevel@tonic-gate 		/* server_loop has closed pin[1], pout[0], and perr[0]. */
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate #else /* USE_PIPES */
5747c478bd9Sstevel@tonic-gate 	/* We are the parent.  Close the child sides of the socket pairs. */
5757c478bd9Sstevel@tonic-gate 	close(inout[0]);
5767c478bd9Sstevel@tonic-gate 	close(err[0]);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Enter the interactive session.  Note: server_loop must be able to
5807c478bd9Sstevel@tonic-gate 	 * handle the case that fdin and fdout are the same.
5817c478bd9Sstevel@tonic-gate 	 */
5827c478bd9Sstevel@tonic-gate 	if (compat20) {
5837c478bd9Sstevel@tonic-gate 		session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
5847a71c13fSZdenek Kotala 		if (s->is_subsystem)
5857a71c13fSZdenek Kotala                         close(err[1]);
5867c478bd9Sstevel@tonic-gate 		/* Don't close channel before sending exit-status! */
5877c478bd9Sstevel@tonic-gate 		channel_set_wait_for_exit(s->chanid, 1);
5887c478bd9Sstevel@tonic-gate 	} else {
5897c478bd9Sstevel@tonic-gate 		server_loop(pid, inout[1], inout[1], err[1]);
5907c478bd9Sstevel@tonic-gate 		/* server_loop has closed inout[1] and err[1]. */
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate #endif /* USE_PIPES */
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * This is called to fork and execute a command when we have a tty.  This
5977c478bd9Sstevel@tonic-gate  * will call do_child from the child, and server_loop from the parent after
5987c478bd9Sstevel@tonic-gate  * setting up file descriptors, controlling tty, updating wtmp, utmp,
5997c478bd9Sstevel@tonic-gate  * lastlog, and other such operations.
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate void
6027c478bd9Sstevel@tonic-gate do_exec_pty(Session *s, const char *command)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	int fdout, ptyfd, ttyfd, ptymaster, pipe_fds[2];
6057c478bd9Sstevel@tonic-gate 	pid_t pid;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (s == NULL)
6087c478bd9Sstevel@tonic-gate 		fatal("do_exec_pty: no session");
6097c478bd9Sstevel@tonic-gate 	ptyfd = s->ptyfd;
6107c478bd9Sstevel@tonic-gate 	ttyfd = s->ttyfd;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate #ifdef USE_PAM
6137c478bd9Sstevel@tonic-gate 	session_do_pam(s, 1);	/* pam_open_session() */
6147c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	/*
6177c478bd9Sstevel@tonic-gate 	 * This pipe lets sshd wait for child to exec or exit.  This is
6187c478bd9Sstevel@tonic-gate 	 * particularly important for ALTPRIVSEP because the child is
6197c478bd9Sstevel@tonic-gate 	 * the one to call the monitor to request a record_login() and
6207c478bd9Sstevel@tonic-gate 	 * we don't want the child and the parent to compete for the
6217c478bd9Sstevel@tonic-gate 	 * monitor's attention.  But this is generic code and doesn't
6227c478bd9Sstevel@tonic-gate 	 * hurt to have here even if ALTPRIVSEP is not used.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 	if (pipe(pipe_fds) != 0)
6257c478bd9Sstevel@tonic-gate 		packet_disconnect("pipe failed: %.100s", strerror(errno));
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	(void) fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC);
6287c478bd9Sstevel@tonic-gate 	(void) fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* Fork the child. */
6317c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
6327c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[0]);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		fatal_remove_all_cleanups();
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		/* Child.  Reinitialize the log because the pid has changed. */
6377c478bd9Sstevel@tonic-gate 		log_init(__progname, options.log_level, options.log_facility, log_stderr);
6387c478bd9Sstevel@tonic-gate 		/* Close the master side of the pseudo tty. */
6397c478bd9Sstevel@tonic-gate 		close(ptyfd);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		/* Make the pseudo tty our controlling tty. */
6427c478bd9Sstevel@tonic-gate 		pty_make_controlling_tty(&ttyfd, s->tty);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		/* Redirect stdin/stdout/stderr from the pseudo tty. */
6457c478bd9Sstevel@tonic-gate 		if (dup2(ttyfd, 0) < 0)
6467c478bd9Sstevel@tonic-gate 			error("dup2 stdin: %s", strerror(errno));
6477c478bd9Sstevel@tonic-gate 		if (dup2(ttyfd, 1) < 0)
6487c478bd9Sstevel@tonic-gate 			error("dup2 stdout: %s", strerror(errno));
6497c478bd9Sstevel@tonic-gate 		if (dup2(ttyfd, 2) < 0)
6507c478bd9Sstevel@tonic-gate 			error("dup2 stderr: %s", strerror(errno));
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		/* Close the extra descriptor for the pseudo tty. */
6537c478bd9Sstevel@tonic-gate 		close(ttyfd);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		/* record login, etc. similar to login(1) */
6567c478bd9Sstevel@tonic-gate 		do_login(s, command);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		/*
659*b9aa66a7SJan Pechanec 		 * Close the pipe to the parent so it can re-enter its event
6607c478bd9Sstevel@tonic-gate 		 * loop and service the ptm; if enough debug messages get
6617c478bd9Sstevel@tonic-gate 		 * written to the pty before this happens there will be a
6627c478bd9Sstevel@tonic-gate 		 * deadlock.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate 		close(pipe_fds[1]);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		/* Do common processing for the child, such as execing the command. */
6677c478bd9Sstevel@tonic-gate 		do_child(s, command);
6687c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/* Wait for child to exec() or exit() */
6727c478bd9Sstevel@tonic-gate 	(void) close(pipe_fds[1]);
6737c478bd9Sstevel@tonic-gate 	(void) read(pipe_fds[0], &pipe_fds[1], sizeof(int));
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate #ifdef _UNICOS
6767c478bd9Sstevel@tonic-gate 	signal(WJSIGNAL, cray_job_termination_handler);
6777c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
6787c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
6797c478bd9Sstevel@tonic-gate 	if (is_winnt)
6807c478bd9Sstevel@tonic-gate 		cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
6817c478bd9Sstevel@tonic-gate #endif
6827c478bd9Sstevel@tonic-gate 	if (pid < 0)
6837c478bd9Sstevel@tonic-gate 		packet_disconnect("fork failed: %.100s", strerror(errno));
6847c478bd9Sstevel@tonic-gate 	s->pid = pid;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* Parent.  Close the slave side of the pseudo tty. */
6877c478bd9Sstevel@tonic-gate 	close(ttyfd);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * Create another descriptor of the pty master side for use as the
6917c478bd9Sstevel@tonic-gate 	 * standard input.  We could use the original descriptor, but this
6927c478bd9Sstevel@tonic-gate 	 * simplifies code in server_loop.  The descriptor is bidirectional.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	fdout = dup(ptyfd);
6957c478bd9Sstevel@tonic-gate 	if (fdout < 0)
6967c478bd9Sstevel@tonic-gate 		packet_disconnect("dup #1 failed: %.100s", strerror(errno));
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/* we keep a reference to the pty master */
6997c478bd9Sstevel@tonic-gate 	ptymaster = dup(ptyfd);
7007c478bd9Sstevel@tonic-gate 	if (ptymaster < 0)
7017c478bd9Sstevel@tonic-gate 		packet_disconnect("dup #2 failed: %.100s", strerror(errno));
7027c478bd9Sstevel@tonic-gate 	s->ptymaster = ptymaster;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/* Enter interactive session. */
7057c478bd9Sstevel@tonic-gate 	packet_set_interactive(1);
7067c478bd9Sstevel@tonic-gate 	if (compat20) {
7077c478bd9Sstevel@tonic-gate 		session_set_fds(s, ptyfd, fdout, -1);
7087c478bd9Sstevel@tonic-gate 		/* Don't close channel before sending exit-status! */
7097c478bd9Sstevel@tonic-gate 		channel_set_wait_for_exit(s->chanid, 1);
7107c478bd9Sstevel@tonic-gate 	} else {
7117c478bd9Sstevel@tonic-gate 		server_loop(pid, ptyfd, fdout, -1);
7127c478bd9Sstevel@tonic-gate 		/* server_loop _has_ closed ptyfd and fdout. */
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * This is called to fork and execute a command.  If another command is
7187c478bd9Sstevel@tonic-gate  * to be forced, execute that instead.
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate void
7217c478bd9Sstevel@tonic-gate do_exec(Session *s, const char *command)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	if (command)
7247c478bd9Sstevel@tonic-gate 		s->command = xstrdup(command);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (forced_command) {
7277c478bd9Sstevel@tonic-gate 		original_command = command;
7287c478bd9Sstevel@tonic-gate 		command = forced_command;
7297c478bd9Sstevel@tonic-gate 		debug("Forced command '%.900s'", command);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1)
7337c478bd9Sstevel@tonic-gate 		do_exec_pty(s, command);
7347c478bd9Sstevel@tonic-gate 	else
7357c478bd9Sstevel@tonic-gate 		do_exec_no_pty(s, command);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	original_command = NULL;
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /* administrative, login(1)-like work */
7427c478bd9Sstevel@tonic-gate void
7437c478bd9Sstevel@tonic-gate do_login(Session *s, const char *command)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	char *time_string;
7467c478bd9Sstevel@tonic-gate #ifndef ALTPRIVSEP
7477c478bd9Sstevel@tonic-gate 	struct passwd * pw = s->pw;
7487c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7497c478bd9Sstevel@tonic-gate 	pid_t pid = getpid();
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/* Record that there was a login on that tty from the remote host. */
7527c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
7537c478bd9Sstevel@tonic-gate 	debug3("Recording SSHv2 channel login in utmpx/wtmpx");
7547c478bd9Sstevel@tonic-gate 	altprivsep_record_login(pid, s->tty);
7557c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (check_quietlogin(s, command))
7587c478bd9Sstevel@tonic-gate 		return;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate #ifdef USE_PAM
7617c478bd9Sstevel@tonic-gate 		print_pam_messages();
7627c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
7637c478bd9Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
7647c478bd9Sstevel@tonic-gate 	if (aixloginmsg && *aixloginmsg)
7657c478bd9Sstevel@tonic-gate 		printf("%s\n", aixloginmsg);
7667c478bd9Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate #ifndef NO_SSH_LASTLOG
7697c478bd9Sstevel@tonic-gate 	if (options.print_lastlog && s->last_login_time != 0) {
7707c478bd9Sstevel@tonic-gate 		time_string = ctime(&s->last_login_time);
7717c478bd9Sstevel@tonic-gate 		if (strchr(time_string, '\n'))
7727c478bd9Sstevel@tonic-gate 			*strchr(time_string, '\n') = 0;
7737c478bd9Sstevel@tonic-gate 		if (strcmp(s->hostname, "") == 0)
7747c478bd9Sstevel@tonic-gate 			printf("Last login: %s\r\n", time_string);
7757c478bd9Sstevel@tonic-gate 		else
7767c478bd9Sstevel@tonic-gate 			printf("Last login: %s from %s\r\n", time_string,
7777c478bd9Sstevel@tonic-gate 			    s->hostname);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate #endif /* NO_SSH_LASTLOG */
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	do_motd();
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate  * Display the message of the day.
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate void
7887c478bd9Sstevel@tonic-gate do_motd(void)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	FILE *f;
7917c478bd9Sstevel@tonic-gate 	char buf[256];
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (options.print_motd) {
7947c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
7957c478bd9Sstevel@tonic-gate 		f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
7967c478bd9Sstevel@tonic-gate 		    "/etc/motd"), "r");
7977c478bd9Sstevel@tonic-gate #else
7987c478bd9Sstevel@tonic-gate 		f = fopen("/etc/motd", "r");
7997c478bd9Sstevel@tonic-gate #endif
8007c478bd9Sstevel@tonic-gate 		if (f) {
8017c478bd9Sstevel@tonic-gate 			while (fgets(buf, sizeof(buf), f))
8027c478bd9Sstevel@tonic-gate 				fputs(buf, stdout);
8037c478bd9Sstevel@tonic-gate 			fclose(f);
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate  * Check for quiet login, either .hushlogin or command given.
8117c478bd9Sstevel@tonic-gate  */
8127c478bd9Sstevel@tonic-gate int
8137c478bd9Sstevel@tonic-gate check_quietlogin(Session *s, const char *command)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	char buf[256];
8167c478bd9Sstevel@tonic-gate 	struct passwd *pw = s->pw;
8177c478bd9Sstevel@tonic-gate 	struct stat st;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	/* Return 1 if .hushlogin exists or a command given. */
8207c478bd9Sstevel@tonic-gate 	if (command != NULL)
8217c478bd9Sstevel@tonic-gate 		return 1;
8227c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
8237c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
8247c478bd9Sstevel@tonic-gate 	if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
8257c478bd9Sstevel@tonic-gate 		return 1;
8267c478bd9Sstevel@tonic-gate #else
8277c478bd9Sstevel@tonic-gate 	if (stat(buf, &st) >= 0)
8287c478bd9Sstevel@tonic-gate 		return 1;
8297c478bd9Sstevel@tonic-gate #endif
8307c478bd9Sstevel@tonic-gate 	return 0;
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * Sets the value of the given variable in the environment.  If the variable
8357c478bd9Sstevel@tonic-gate  * already exists, its value is overriden.
8367c478bd9Sstevel@tonic-gate  */
8377c478bd9Sstevel@tonic-gate void
8387c478bd9Sstevel@tonic-gate child_set_env(char ***envp, u_int *envsizep, const char *name,
8397c478bd9Sstevel@tonic-gate 	const char *value)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	u_int i, namelen;
8427c478bd9Sstevel@tonic-gate 	char **env;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	debug3("child_set_env(%s, %s)", name, value);
8457c478bd9Sstevel@tonic-gate 	/*
8467c478bd9Sstevel@tonic-gate 	 * Find the slot where the value should be stored.  If the variable
8477c478bd9Sstevel@tonic-gate 	 * already exists, we reuse the slot; otherwise we append a new slot
8487c478bd9Sstevel@tonic-gate 	 * at the end of the array, expanding if necessary.
8497c478bd9Sstevel@tonic-gate 	 */
8507c478bd9Sstevel@tonic-gate 	env = *envp;
8517c478bd9Sstevel@tonic-gate 	namelen = strlen(name);
8527c478bd9Sstevel@tonic-gate 	for (i = 0; env[i]; i++)
8537c478bd9Sstevel@tonic-gate 		if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 	if (env[i]) {
8567c478bd9Sstevel@tonic-gate 		/* Reuse the slot. */
8577c478bd9Sstevel@tonic-gate 		xfree(env[i]);
8587c478bd9Sstevel@tonic-gate 	} else {
8597c478bd9Sstevel@tonic-gate 		/* New variable.  Expand if necessary. */
8607c478bd9Sstevel@tonic-gate 		if (i >= (*envsizep) - 1) {
8617c478bd9Sstevel@tonic-gate 			if (*envsizep >= 1000)
8627c478bd9Sstevel@tonic-gate 				fatal("child_set_env: too many env vars,"
8637c478bd9Sstevel@tonic-gate 				    " skipping: %.100s", name);
8647c478bd9Sstevel@tonic-gate 			(*envsizep) += 50;
8657c478bd9Sstevel@tonic-gate 			env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
8667c478bd9Sstevel@tonic-gate 		}
8677c478bd9Sstevel@tonic-gate 		/* Need to set the NULL pointer at end of array beyond the new slot. */
8687c478bd9Sstevel@tonic-gate 		env[i + 1] = NULL;
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/* Allocate space and format the variable in the appropriate slot. */
8727c478bd9Sstevel@tonic-gate 	env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
8737c478bd9Sstevel@tonic-gate 	snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * Reads environment variables from the given file and adds/overrides them
8787c478bd9Sstevel@tonic-gate  * into the environment.  If the file does not exist, this does nothing.
8797c478bd9Sstevel@tonic-gate  * Otherwise, it must consist of empty lines, comments (line starts with '#')
8807c478bd9Sstevel@tonic-gate  * and assignments of the form name=value.  No other forms are allowed.
8817c478bd9Sstevel@tonic-gate  */
8827c478bd9Sstevel@tonic-gate static void
8837c478bd9Sstevel@tonic-gate read_environment_file(char ***env, u_int *envsize,
8847c478bd9Sstevel@tonic-gate 	const char *filename)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	FILE *f;
8877c478bd9Sstevel@tonic-gate 	char buf[4096];
8887c478bd9Sstevel@tonic-gate 	char *cp, *value;
8897c478bd9Sstevel@tonic-gate 	u_int lineno = 0;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	f = fopen(filename, "r");
8927c478bd9Sstevel@tonic-gate 	if (!f)
8937c478bd9Sstevel@tonic-gate 		return;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof(buf), f)) {
8967c478bd9Sstevel@tonic-gate 		if (++lineno > 1000)
8977c478bd9Sstevel@tonic-gate 			fatal("Too many lines in environment file %s", filename);
8987c478bd9Sstevel@tonic-gate 		for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
8997c478bd9Sstevel@tonic-gate 			;
9007c478bd9Sstevel@tonic-gate 		if (!*cp || *cp == '#' || *cp == '\n')
9017c478bd9Sstevel@tonic-gate 			continue;
9027c478bd9Sstevel@tonic-gate 		if (strchr(cp, '\n'))
9037c478bd9Sstevel@tonic-gate 			*strchr(cp, '\n') = '\0';
9047c478bd9Sstevel@tonic-gate 		value = strchr(cp, '=');
9057c478bd9Sstevel@tonic-gate 		if (value == NULL) {
9067c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Bad line %u in %.100s\n"),
9077c478bd9Sstevel@tonic-gate 				lineno, filename);
9087c478bd9Sstevel@tonic-gate 			continue;
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		/*
9117c478bd9Sstevel@tonic-gate 		 * Replace the equals sign by nul, and advance value to
9127c478bd9Sstevel@tonic-gate 		 * the value string.
9137c478bd9Sstevel@tonic-gate 		 */
9147c478bd9Sstevel@tonic-gate 		*value = '\0';
9157c478bd9Sstevel@tonic-gate 		value++;
9167c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, cp, value);
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 	fclose(f);
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate void copy_environment(char **source, char ***env, u_int *envsize)
9227c478bd9Sstevel@tonic-gate {
9237c478bd9Sstevel@tonic-gate 	char *var_name, *var_val;
9247c478bd9Sstevel@tonic-gate 	int i;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	if (source == NULL)
9277c478bd9Sstevel@tonic-gate 		return;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	for(i = 0; source[i] != NULL; i++) {
9307c478bd9Sstevel@tonic-gate 		var_name = xstrdup(source[i]);
9317c478bd9Sstevel@tonic-gate 		if ((var_val = strstr(var_name, "=")) == NULL) {
9327c478bd9Sstevel@tonic-gate 			xfree(var_name);
9337c478bd9Sstevel@tonic-gate 			continue;
9347c478bd9Sstevel@tonic-gate 		}
9357c478bd9Sstevel@tonic-gate 		*var_val++ = '\0';
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		debug3("Copy environment: %s=%s", var_name, var_val);
9387c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, var_name, var_val);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		xfree(var_name);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
9457c478bd9Sstevel@tonic-gate static
9467c478bd9Sstevel@tonic-gate void
9477c478bd9Sstevel@tonic-gate deflt_do_setup_env(Session *s, const char *shell, char ***env, u_int *envsize)
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 	int	flags;
9507c478bd9Sstevel@tonic-gate 	char	*ptr;
9517c478bd9Sstevel@tonic-gate 	mode_t	Umask = 022;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (defopen(_PATH_DEFAULT_LOGIN))
9547c478bd9Sstevel@tonic-gate 		return;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	/* Ignore case */
9577c478bd9Sstevel@tonic-gate 	flags = defcntl(DC_GETFLAGS, 0);
9587c478bd9Sstevel@tonic-gate 	TURNOFF(flags, DC_CASE);
9597c478bd9Sstevel@tonic-gate 	(void) defcntl(DC_SETFLAGS, flags);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/* TZ & HZ */
9627c478bd9Sstevel@tonic-gate 	if ((ptr = defread("TIMEZONE=")) != NULL)
9637c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "TZ", ptr);
9647c478bd9Sstevel@tonic-gate 	if ((ptr = defread("HZ=")) != NULL)
9657c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "HZ", ptr);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/* PATH */
9687c478bd9Sstevel@tonic-gate 	if (s->pw->pw_uid != 0 && (ptr = defread("PATH=")) != NULL)
9697c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "PATH", ptr);
9707c478bd9Sstevel@tonic-gate 	if (s->pw->pw_uid == 0 && (ptr = defread("SUPATH=")) != NULL)
9717c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "PATH", ptr);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	/* SHELL */
9747c478bd9Sstevel@tonic-gate 	if ((ptr = defread("ALTSHELL=")) != NULL) {
9757c478bd9Sstevel@tonic-gate 		if (strcasecmp("YES", ptr) == 0)
9767c478bd9Sstevel@tonic-gate 			child_set_env(env, envsize, "SHELL", shell);
9777c478bd9Sstevel@tonic-gate 		else
9787c478bd9Sstevel@tonic-gate 			child_set_env(env, envsize, "SHELL", "");
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	/* UMASK */
9827c478bd9Sstevel@tonic-gate 	if ((ptr = defread("UMASK=")) != NULL &&
9837c478bd9Sstevel@tonic-gate 	    sscanf(ptr, "%lo", &Umask) == 1 &&
9847c478bd9Sstevel@tonic-gate 	    Umask <= (mode_t)0777)
9857c478bd9Sstevel@tonic-gate 		(void) umask(Umask);
9867c478bd9Sstevel@tonic-gate 	else
9877c478bd9Sstevel@tonic-gate 		(void) umask(022);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	/* ULIMIT */
9907c478bd9Sstevel@tonic-gate 	if ((ptr = defread("ULIMIT=")) != NULL && atol(ptr) > 0L &&
9917c478bd9Sstevel@tonic-gate 	    ulimit(UL_SETFSIZE, atol(ptr)) < 0L)
9927c478bd9Sstevel@tonic-gate 		error("Could not set ULIMIT to %ld from %s\n", atol(ptr),
9937c478bd9Sstevel@tonic-gate 			_PATH_DEFAULT_LOGIN);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	(void) defopen(NULL);
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate static char **
10007c478bd9Sstevel@tonic-gate do_setup_env(Session *s, const char *shell)
10017c478bd9Sstevel@tonic-gate {
1002c2a2f8dcSjp161948 	char buf[256];
1003c2a2f8dcSjp161948 	char path_maildir[] = _PATH_MAILDIR;
100426ba1984Sjp161948 	u_int i, envsize, pm_len;
10057c478bd9Sstevel@tonic-gate 	char **env;
10067c478bd9Sstevel@tonic-gate 	struct passwd *pw = s->pw;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	/* Initialize the environment. */
10097c478bd9Sstevel@tonic-gate 	envsize = 100;
10107c478bd9Sstevel@tonic-gate 	env = xmalloc(envsize * sizeof(char *));
10117c478bd9Sstevel@tonic-gate 	env[0] = NULL;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
10147c478bd9Sstevel@tonic-gate 	/*
10157c478bd9Sstevel@tonic-gate 	 * The Windows environment contains some setting which are
10167c478bd9Sstevel@tonic-gate 	 * important for a running system. They must not be dropped.
10177c478bd9Sstevel@tonic-gate 	 */
10187c478bd9Sstevel@tonic-gate 	copy_environment(environ, &env, &envsize);
10197c478bd9Sstevel@tonic-gate #endif
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate #ifdef GSSAPI
10227c478bd9Sstevel@tonic-gate 	/* Allow any GSSAPI methods that we've used to alter
10237c478bd9Sstevel@tonic-gate 	 * the childs environment as they see fit
10247c478bd9Sstevel@tonic-gate 	 */
10257c478bd9Sstevel@tonic-gate 	ssh_gssapi_do_child(xxx_gssctxt, &env,&envsize);
10267c478bd9Sstevel@tonic-gate #endif
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/* Set basic environment. */
10297c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "USER", pw->pw_name);
10307c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
10317c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "HOME", pw->pw_dir);
10327c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
10337c478bd9Sstevel@tonic-gate 	if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
10347c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10357c478bd9Sstevel@tonic-gate 	else
10367c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "PATH", getenv("PATH"));
10377c478bd9Sstevel@tonic-gate #else /* HAVE_LOGIN_CAP */
10387c478bd9Sstevel@tonic-gate # ifndef HAVE_CYGWIN
10397c478bd9Sstevel@tonic-gate 	/*
10407c478bd9Sstevel@tonic-gate 	 * There's no standard path on Windows. The path contains
10417c478bd9Sstevel@tonic-gate 	 * important components pointing to the system directories,
10427c478bd9Sstevel@tonic-gate 	 * needed for loading shared libraries. So the path better
10437c478bd9Sstevel@tonic-gate 	 * remains intact here.
10447c478bd9Sstevel@tonic-gate 	 */
10457c478bd9Sstevel@tonic-gate #  ifdef SUPERUSER_PATH
10467c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "PATH",
10477c478bd9Sstevel@tonic-gate 	    s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
10487c478bd9Sstevel@tonic-gate #  else
10497c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10507c478bd9Sstevel@tonic-gate #  endif /* SUPERUSER_PATH */
10517c478bd9Sstevel@tonic-gate # endif /* HAVE_CYGWIN */
10527c478bd9Sstevel@tonic-gate #endif /* HAVE_LOGIN_CAP */
10537c478bd9Sstevel@tonic-gate 
105426ba1984Sjp161948 	pm_len = strlen(path_maildir);
105526ba1984Sjp161948 	if (path_maildir[pm_len - 1] == '/' && pm_len > 1)
105626ba1984Sjp161948 		path_maildir[pm_len - 1] = NULL;
10577c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof buf, "%.200s/%.50s",
105826ba1984Sjp161948 		 path_maildir, pw->pw_name);
10597c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "MAIL", buf);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	/* Normal systems set SHELL by default. */
10627c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "SHELL", shell);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
10657c478bd9Sstevel@tonic-gate 	deflt_do_setup_env(s, shell, &env, &envsize);
10667c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate #define PASS_ENV(x) \
10697c478bd9Sstevel@tonic-gate 	if (getenv(x)) \
10707c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, x, getenv(x));
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	if (getenv("TZ"))
10737c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "TZ", getenv("TZ"));
10747c478bd9Sstevel@tonic-gate 
1075a6e0e77dSjp161948 	if (s->auth_file != NULL)
1076a6e0e77dSjp161948 		child_set_env(&env, &envsize, "XAUTHORITY", s->auth_file);
1077a6e0e77dSjp161948 
10787c478bd9Sstevel@tonic-gate 	PASS_ENV("LANG")
10797c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_ALL")
10807c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_CTYPE")
10817c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_COLLATE")
10824dd46c65Sjp161948 	PASS_ENV("LC_TIME")
10837c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_NUMERIC")
10847c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_MONETARY")
10857c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_MESSAGES")
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate #undef PASS_ENV
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (s->env != NULL)
10907c478bd9Sstevel@tonic-gate 		copy_environment(s->env, &env, &envsize);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/* Set custom environment options from RSA authentication. */
10937c478bd9Sstevel@tonic-gate 	while (custom_environment) {
10947c478bd9Sstevel@tonic-gate 		struct envstring *ce = custom_environment;
10957c478bd9Sstevel@tonic-gate 		char *str = ce->s;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		for (i = 0; str[i] != '=' && str[i]; i++)
10987c478bd9Sstevel@tonic-gate 			;
10997c478bd9Sstevel@tonic-gate 		if (str[i] == '=') {
11007c478bd9Sstevel@tonic-gate 			str[i] = 0;
11017c478bd9Sstevel@tonic-gate 			child_set_env(&env, &envsize, str, str + i + 1);
11027c478bd9Sstevel@tonic-gate 		}
11037c478bd9Sstevel@tonic-gate 		custom_environment = ce->next;
11047c478bd9Sstevel@tonic-gate 		xfree(ce->s);
11057c478bd9Sstevel@tonic-gate 		xfree(ce);
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/* SSH_CLIENT deprecated */
11097c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof buf, "%.50s %d %d",
11107c478bd9Sstevel@tonic-gate 	    get_remote_ipaddr(), get_remote_port(), get_local_port());
11117c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "SSH_CLIENT", buf);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
11147c478bd9Sstevel@tonic-gate 	    get_remote_ipaddr(), get_remote_port(),
11157c478bd9Sstevel@tonic-gate 	    get_local_ipaddr(packet_get_connection_in()), get_local_port());
11167c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1)
11197c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "SSH_TTY", s->tty);
11207c478bd9Sstevel@tonic-gate 	if (s->term)
11217c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "TERM", s->term);
11227c478bd9Sstevel@tonic-gate 	if (s->display)
11237c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "DISPLAY", s->display);
11247c478bd9Sstevel@tonic-gate 	if (original_command)
11257c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
11267c478bd9Sstevel@tonic-gate 		    original_command);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate #ifdef _UNICOS
11297c478bd9Sstevel@tonic-gate 	if (cray_tmpdir[0] != '\0')
11307c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
11317c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate #ifdef _AIX
11347c478bd9Sstevel@tonic-gate 	{
11357c478bd9Sstevel@tonic-gate 		char *cp;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 		if ((cp = getenv("AUTHSTATE")) != NULL)
11387c478bd9Sstevel@tonic-gate 			child_set_env(&env, &envsize, "AUTHSTATE", cp);
11397c478bd9Sstevel@tonic-gate 		if ((cp = getenv("KRB5CCNAME")) != NULL)
11407c478bd9Sstevel@tonic-gate 			child_set_env(&env, &envsize, "KRB5CCNAME", cp);
11417c478bd9Sstevel@tonic-gate 		read_environment_file(&env, &envsize, "/etc/environment");
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate #endif
11447c478bd9Sstevel@tonic-gate #ifdef KRB4
11457c478bd9Sstevel@tonic-gate 	if (s->authctxt->krb4_ticket_file)
11467c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "KRBTKFILE",
11477c478bd9Sstevel@tonic-gate 		    s->authctxt->krb4_ticket_file);
11487c478bd9Sstevel@tonic-gate #endif
11497c478bd9Sstevel@tonic-gate #ifdef KRB5
11507c478bd9Sstevel@tonic-gate 	if (s->authctxt->krb5_ticket_file)
11517c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "KRB5CCNAME",
11527c478bd9Sstevel@tonic-gate 		    s->authctxt->krb5_ticket_file);
11537c478bd9Sstevel@tonic-gate #endif
11547c478bd9Sstevel@tonic-gate #ifdef USE_PAM
11557c478bd9Sstevel@tonic-gate 	/*
11567c478bd9Sstevel@tonic-gate 	 * Pull in any environment variables that may have
11577c478bd9Sstevel@tonic-gate 	 * been set by PAM.
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	{
11607c478bd9Sstevel@tonic-gate 		char **p;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		p = fetch_pam_environment(s->authctxt);
11637c478bd9Sstevel@tonic-gate 		copy_environment(p, &env, &envsize);
11647c478bd9Sstevel@tonic-gate 		free_pam_environment(p);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL)
11697c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
11707c478bd9Sstevel@tonic-gate 		    auth_sock_name);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	/* read $HOME/.ssh/environment. */
1173*b9aa66a7SJan Pechanec 	if (options.permit_user_env) {
11747c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
11757c478bd9Sstevel@tonic-gate 		    strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
11767c478bd9Sstevel@tonic-gate 		read_environment_file(&env, &envsize, buf);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	if (debug_flag) {
11797c478bd9Sstevel@tonic-gate 		/* dump the environment */
11807c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Environment:\n"));
11817c478bd9Sstevel@tonic-gate 		for (i = 0; env[i]; i++)
11827c478bd9Sstevel@tonic-gate 			fprintf(stderr, "  %.200s\n", env[i]);
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 	return env;
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
11887c478bd9Sstevel@tonic-gate  * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
11897c478bd9Sstevel@tonic-gate  * first in this order).
11907c478bd9Sstevel@tonic-gate  */
11917c478bd9Sstevel@tonic-gate static void
11927c478bd9Sstevel@tonic-gate do_rc_files(Session *s, const char *shell)
11937c478bd9Sstevel@tonic-gate {
11947c478bd9Sstevel@tonic-gate 	FILE *f = NULL;
11957c478bd9Sstevel@tonic-gate 	char cmd[1024];
11967c478bd9Sstevel@tonic-gate 	int do_xauth;
11977c478bd9Sstevel@tonic-gate 	struct stat st;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	do_xauth =
12007c478bd9Sstevel@tonic-gate 	    s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/* ignore _PATH_SSH_USER_RC for subsystems */
12037c478bd9Sstevel@tonic-gate 	if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
12047c478bd9Sstevel@tonic-gate 		snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
12057c478bd9Sstevel@tonic-gate 		    shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
12067c478bd9Sstevel@tonic-gate 		if (debug_flag)
12077c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Running %s\n", cmd);
12087c478bd9Sstevel@tonic-gate 		f = popen(cmd, "w");
12097c478bd9Sstevel@tonic-gate 		if (f) {
12107c478bd9Sstevel@tonic-gate 			if (do_xauth)
12117c478bd9Sstevel@tonic-gate 				fprintf(f, "%s %s\n", s->auth_proto,
12127c478bd9Sstevel@tonic-gate 				    s->auth_data);
12137c478bd9Sstevel@tonic-gate 			pclose(f);
12147c478bd9Sstevel@tonic-gate 		} else
12157c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not run %s\n",
12167c478bd9Sstevel@tonic-gate 			    _PATH_SSH_USER_RC);
12177c478bd9Sstevel@tonic-gate 	} else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
12187c478bd9Sstevel@tonic-gate 		if (debug_flag)
12197c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
12207c478bd9Sstevel@tonic-gate 			    _PATH_SSH_SYSTEM_RC);
12217c478bd9Sstevel@tonic-gate 		f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
12227c478bd9Sstevel@tonic-gate 		if (f) {
12237c478bd9Sstevel@tonic-gate 			if (do_xauth)
12247c478bd9Sstevel@tonic-gate 				fprintf(f, "%s %s\n", s->auth_proto,
12257c478bd9Sstevel@tonic-gate 				    s->auth_data);
12267c478bd9Sstevel@tonic-gate 			pclose(f);
12277c478bd9Sstevel@tonic-gate 		} else
12287c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not run %s\n",
12297c478bd9Sstevel@tonic-gate 			    _PATH_SSH_SYSTEM_RC);
12307c478bd9Sstevel@tonic-gate 	} else if (do_xauth && options.xauth_location != NULL) {
12317c478bd9Sstevel@tonic-gate 		/* Add authority data to .Xauthority if appropriate. */
12327c478bd9Sstevel@tonic-gate 		if (debug_flag) {
12337c478bd9Sstevel@tonic-gate 			fprintf(stderr,
12347c478bd9Sstevel@tonic-gate 			    "Running %.500s add "
12357c478bd9Sstevel@tonic-gate 			    "%.100s %.100s %.100s\n",
12367c478bd9Sstevel@tonic-gate 			    options.xauth_location, s->auth_display,
12377c478bd9Sstevel@tonic-gate 			    s->auth_proto, s->auth_data);
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		snprintf(cmd, sizeof cmd, "%s -q -",
12407c478bd9Sstevel@tonic-gate 		    options.xauth_location);
12417c478bd9Sstevel@tonic-gate 		f = popen(cmd, "w");
12427c478bd9Sstevel@tonic-gate 		if (f) {
12437c478bd9Sstevel@tonic-gate 			fprintf(f, "add %s %s %s\n",
12447c478bd9Sstevel@tonic-gate 			    s->auth_display, s->auth_proto,
12457c478bd9Sstevel@tonic-gate 			    s->auth_data);
12467c478bd9Sstevel@tonic-gate 			pclose(f);
12477c478bd9Sstevel@tonic-gate 		} else {
12487c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not run %s\n",
12497c478bd9Sstevel@tonic-gate 			    cmd);
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate }
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate static void
12557c478bd9Sstevel@tonic-gate do_nologin(struct passwd *pw)
12567c478bd9Sstevel@tonic-gate {
12577c478bd9Sstevel@tonic-gate 	FILE *f = NULL;
12587c478bd9Sstevel@tonic-gate 	char buf[1024];
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
12617c478bd9Sstevel@tonic-gate 	if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
12627c478bd9Sstevel@tonic-gate 		f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
12637c478bd9Sstevel@tonic-gate 		    _PATH_NOLOGIN), "r");
12647c478bd9Sstevel@tonic-gate #else
12657c478bd9Sstevel@tonic-gate 	if (pw->pw_uid)
12667c478bd9Sstevel@tonic-gate 		f = fopen(_PATH_NOLOGIN, "r");
12677c478bd9Sstevel@tonic-gate #endif
12687c478bd9Sstevel@tonic-gate 	if (f) {
12697c478bd9Sstevel@tonic-gate 		/* /etc/nologin exists.  Print its contents and exit. */
12707c478bd9Sstevel@tonic-gate 		log("User %.100s not allowed because %s exists",
12717c478bd9Sstevel@tonic-gate 		    pw->pw_name, _PATH_NOLOGIN);
12727c478bd9Sstevel@tonic-gate 		while (fgets(buf, sizeof(buf), f))
12737c478bd9Sstevel@tonic-gate 			fputs(buf, stderr);
12747c478bd9Sstevel@tonic-gate 		fclose(f);
12757c478bd9Sstevel@tonic-gate 		exit(254);
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
12796f8d59d8SJan Pechanec /* Chroot into ChrootDirectory if the option is set. */
12807c478bd9Sstevel@tonic-gate void
12816f8d59d8SJan Pechanec chroot_if_needed(struct passwd *pw)
12827c478bd9Sstevel@tonic-gate {
12836f8d59d8SJan Pechanec 	char *chroot_path, *tmp;
12847c478bd9Sstevel@tonic-gate 
12856f8d59d8SJan Pechanec 	if (chroot_requested(options.chroot_directory)) {
12866f8d59d8SJan Pechanec 		tmp = tilde_expand_filename(options.chroot_directory,
12876f8d59d8SJan Pechanec 		    pw->pw_uid);
12886f8d59d8SJan Pechanec 		chroot_path = percent_expand(tmp, "h", pw->pw_dir,
12896f8d59d8SJan Pechanec 		    "u", pw->pw_name, (char *)NULL);
12906f8d59d8SJan Pechanec 		safely_chroot(chroot_path, pw->pw_uid);
12916f8d59d8SJan Pechanec 		free(tmp);
12926f8d59d8SJan Pechanec 		free(chroot_path);
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate }
12956f8d59d8SJan Pechanec 
12967c478bd9Sstevel@tonic-gate /*
12976f8d59d8SJan Pechanec  * Chroot into a directory after checking it for safety: all path components
12986f8d59d8SJan Pechanec  * must be root-owned directories with strict permissions.
12997c478bd9Sstevel@tonic-gate  */
13006f8d59d8SJan Pechanec static void
13016f8d59d8SJan Pechanec safely_chroot(const char *path, uid_t uid)
13026f8d59d8SJan Pechanec {
13036f8d59d8SJan Pechanec 	const char *cp;
13046f8d59d8SJan Pechanec 	char component[MAXPATHLEN];
13056f8d59d8SJan Pechanec 	struct stat st;
13066f8d59d8SJan Pechanec 
13076f8d59d8SJan Pechanec 	if (*path != '/')
13086f8d59d8SJan Pechanec 		fatal("chroot path does not begin at root");
13096f8d59d8SJan Pechanec 	if (strlen(path) >= sizeof(component))
13106f8d59d8SJan Pechanec 		fatal("chroot path too long");
13116f8d59d8SJan Pechanec 
13126f8d59d8SJan Pechanec 	/*
13136f8d59d8SJan Pechanec 	 * Descend the path, checking that each component is a
13146f8d59d8SJan Pechanec 	 * root-owned directory with strict permissions.
13156f8d59d8SJan Pechanec 	 */
13166f8d59d8SJan Pechanec 	for (cp = path; cp != NULL;) {
13176f8d59d8SJan Pechanec 		if ((cp = strchr(cp, '/')) == NULL)
13186f8d59d8SJan Pechanec 			strlcpy(component, path, sizeof(component));
13196f8d59d8SJan Pechanec 		else {
13206f8d59d8SJan Pechanec 			cp++;
13216f8d59d8SJan Pechanec 			memcpy(component, path, cp - path);
13226f8d59d8SJan Pechanec 			component[cp - path] = '\0';
13237c478bd9Sstevel@tonic-gate 		}
13246f8d59d8SJan Pechanec 
13256f8d59d8SJan Pechanec 		debug3("%s: checking '%s'", __func__, component);
13266f8d59d8SJan Pechanec 
13276f8d59d8SJan Pechanec 		if (stat(component, &st) != 0)
13286f8d59d8SJan Pechanec 			fatal("%s: stat(\"%s\"): %s", __func__,
13296f8d59d8SJan Pechanec 			    component, strerror(errno));
13306f8d59d8SJan Pechanec 		if (st.st_uid != 0 || (st.st_mode & 022) != 0)
13316f8d59d8SJan Pechanec 			fatal("bad ownership or modes for chroot "
13326f8d59d8SJan Pechanec 			    "directory %s\"%s\"",
13336f8d59d8SJan Pechanec 			    cp == NULL ? "" : "component ", component);
13346f8d59d8SJan Pechanec 		if (!S_ISDIR(st.st_mode))
13356f8d59d8SJan Pechanec 			fatal("chroot path %s\"%s\" is not a directory",
13366f8d59d8SJan Pechanec 			    cp == NULL ? "" : "component ", component);
13376f8d59d8SJan Pechanec 	}
13386f8d59d8SJan Pechanec 
13396f8d59d8SJan Pechanec 	if (chdir(path) == -1)
13406f8d59d8SJan Pechanec 		fatal("Unable to chdir to chroot path \"%s\": "
13416f8d59d8SJan Pechanec 		    "%s", path, strerror(errno));
13426f8d59d8SJan Pechanec 	if (chroot(path) == -1)
13436f8d59d8SJan Pechanec 		fatal("chroot(\"%s\"): %s", path, strerror(errno));
13446f8d59d8SJan Pechanec 	if (chdir("/") == -1)
13456f8d59d8SJan Pechanec 		fatal("%s: chdir(/) after chroot: %s",
13466f8d59d8SJan Pechanec 		    __func__, strerror(errno));
13476f8d59d8SJan Pechanec 	verbose("Changed root directory to \"%s\"", path);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate static void
13517c478bd9Sstevel@tonic-gate launch_login(struct passwd *pw, const char *hostname)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	/* Launch login(1). */
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	execl(LOGIN_PROGRAM, "login", "-h", hostname,
13567c478bd9Sstevel@tonic-gate #ifdef xxxLOGIN_NEEDS_TERM
13577c478bd9Sstevel@tonic-gate 		    (s->term ? s->term : "unknown"),
13587c478bd9Sstevel@tonic-gate #endif /* LOGIN_NEEDS_TERM */
13597c478bd9Sstevel@tonic-gate #ifdef LOGIN_NO_ENDOPT
13607c478bd9Sstevel@tonic-gate 	    "-p", "-f", pw->pw_name, (char *)NULL);
13617c478bd9Sstevel@tonic-gate #else
13627c478bd9Sstevel@tonic-gate 	    "-p", "-f", "--", pw->pw_name, (char *)NULL);
13637c478bd9Sstevel@tonic-gate #endif
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/* Login couldn't be executed, die. */
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	perror("login");
13687c478bd9Sstevel@tonic-gate 	exit(1);
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate /*
13727c478bd9Sstevel@tonic-gate  * Performs common processing for the child, such as setting up the
13737c478bd9Sstevel@tonic-gate  * environment, closing extra file descriptors, setting the user and group
13747c478bd9Sstevel@tonic-gate  * ids, and executing the command or shell.
13757c478bd9Sstevel@tonic-gate  */
13766f8d59d8SJan Pechanec #define ARGV_MAX 10
13777c478bd9Sstevel@tonic-gate void
13787c478bd9Sstevel@tonic-gate do_child(Session *s, const char *command)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate 	extern char **environ;
13817c478bd9Sstevel@tonic-gate 	char **env;
13826f8d59d8SJan Pechanec 	char *argv[ARGV_MAX];
1383*b9aa66a7SJan Pechanec 	const char *shell, *shell0;
13847c478bd9Sstevel@tonic-gate 	struct passwd *pw = s->pw;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	/* remove hostkey from the child's memory */
13877c478bd9Sstevel@tonic-gate 	destroy_sensitive_data();
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	do_nologin(pw);
13906f8d59d8SJan Pechanec 	chroot_if_needed(pw);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	/*
13937c478bd9Sstevel@tonic-gate 	 * Get the shell from the password data.  An empty shell field is
13947c478bd9Sstevel@tonic-gate 	 * legal, and means /bin/sh.
13957c478bd9Sstevel@tonic-gate 	 */
13967c478bd9Sstevel@tonic-gate 	shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
13977c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
13987c478bd9Sstevel@tonic-gate 	shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
13997c478bd9Sstevel@tonic-gate #endif
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	env = do_setup_env(s, shell);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	/*
14047c478bd9Sstevel@tonic-gate 	 * Close the connection descriptors; note that this is the child, and
14057c478bd9Sstevel@tonic-gate 	 * the server will still have the socket open, and it is important
14067c478bd9Sstevel@tonic-gate 	 * that we do not shutdown it.  Note that the descriptors cannot be
14077c478bd9Sstevel@tonic-gate 	 * closed before building the environment, as we call
14087c478bd9Sstevel@tonic-gate 	 * get_remote_ipaddr there.
14097c478bd9Sstevel@tonic-gate 	 */
14107c478bd9Sstevel@tonic-gate 	if (packet_get_connection_in() == packet_get_connection_out())
14117c478bd9Sstevel@tonic-gate 		close(packet_get_connection_in());
14127c478bd9Sstevel@tonic-gate 	else {
14137c478bd9Sstevel@tonic-gate 		close(packet_get_connection_in());
14147c478bd9Sstevel@tonic-gate 		close(packet_get_connection_out());
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate 	/*
14177c478bd9Sstevel@tonic-gate 	 * Close all descriptors related to channels.  They will still remain
14187c478bd9Sstevel@tonic-gate 	 * open in the parent.
14197c478bd9Sstevel@tonic-gate 	 */
14207c478bd9Sstevel@tonic-gate 	/* XXX better use close-on-exec? -markus */
14217c478bd9Sstevel@tonic-gate 	channel_close_all();
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
14247c478bd9Sstevel@tonic-gate 	 * Close any extra file descriptors.  Note that there may still be
14257c478bd9Sstevel@tonic-gate 	 * descriptors left by system functions.  They will be closed later.
14267c478bd9Sstevel@tonic-gate 	 */
14277c478bd9Sstevel@tonic-gate 	endpwent();
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/*
14306f786aceSNobutomo Nakano 	 * Must switch to the new environment variables so that .ssh/rc,
14316f786aceSNobutomo Nakano 	 * /etc/ssh/sshrc, and xauth are run in the proper environment.
14326f786aceSNobutomo Nakano 	 */
14336f786aceSNobutomo Nakano 	environ = env;
14346f786aceSNobutomo Nakano 
14356f786aceSNobutomo Nakano 	/*
14366f786aceSNobutomo Nakano 	 * New environment has been installed. We need to update locale
14376f786aceSNobutomo Nakano 	 * so that error messages beyond this point have the proper
14386f786aceSNobutomo Nakano 	 * character encoding.
14396f786aceSNobutomo Nakano 	 */
14406f786aceSNobutomo Nakano 	(void) setlocale(LC_ALL, "");
14416f786aceSNobutomo Nakano 
14426f786aceSNobutomo Nakano 	/*
14437c478bd9Sstevel@tonic-gate 	 * Close any extra open file descriptors so that we don\'t have them
14447c478bd9Sstevel@tonic-gate 	 * hanging around in clients.  Note that we want to do this after
14457c478bd9Sstevel@tonic-gate 	 * initgroups, because at least on Solaris 2.3 it leaves file
14467c478bd9Sstevel@tonic-gate 	 * descriptors open.
14477c478bd9Sstevel@tonic-gate 	 */
14487c478bd9Sstevel@tonic-gate 	closefrom(STDERR_FILENO + 1);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate #ifdef AFS
14517c478bd9Sstevel@tonic-gate 	/* Try to get AFS tokens for the local cell. */
14527c478bd9Sstevel@tonic-gate 	if (k_hasafs()) {
14537c478bd9Sstevel@tonic-gate 		char cell[64];
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 		if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
14567c478bd9Sstevel@tonic-gate 			krb_afslog(cell, 0);
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		krb_afslog(0, 0);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate #endif /* AFS */
14617c478bd9Sstevel@tonic-gate 
14626f8d59d8SJan Pechanec 	/* Change current directory to the user's home directory. */
14637c478bd9Sstevel@tonic-gate 	if (chdir(pw->pw_dir) < 0) {
14646f8d59d8SJan Pechanec 		/* Suppress missing homedir warning for chroot case */
14656f8d59d8SJan Pechanec 		if (!chroot_requested(options.chroot_directory))
14666f8d59d8SJan Pechanec 			fprintf(stderr, "Could not chdir to home "
14676f8d59d8SJan Pechanec 			    "directory %s: %s\n", pw->pw_dir,
14686f8d59d8SJan Pechanec 			    strerror(errno));
14697c478bd9Sstevel@tonic-gate 	}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	do_rc_files(s, shell);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	/* restore SIGPIPE for child */
14747c478bd9Sstevel@tonic-gate 	signal(SIGPIPE,  SIG_DFL);
14757c478bd9Sstevel@tonic-gate 
14766f8d59d8SJan Pechanec 	if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
14776f8d59d8SJan Pechanec 		int i;
14786f8d59d8SJan Pechanec 		char *p, *args;
14796f8d59d8SJan Pechanec 		extern int optind, optreset;
14806f8d59d8SJan Pechanec 
14816f8d59d8SJan Pechanec 		/* This will set the E/P sets here, simulating exec(2). */
14826f8d59d8SJan Pechanec 		drop_privs(pw->pw_uid);
14836f8d59d8SJan Pechanec 
14846f8d59d8SJan Pechanec 		setproctitle("%s@internal-sftp-server", s->pw->pw_name);
14856f8d59d8SJan Pechanec 		args = xstrdup(command ? command : "sftp-server");
14866f8d59d8SJan Pechanec 
14876f8d59d8SJan Pechanec 		i = 0;
14886f8d59d8SJan Pechanec 		for ((p = strtok(args, " ")); p != NULL; (p = strtok(NULL, " "))) {
14896f8d59d8SJan Pechanec 			if (i < ARGV_MAX - 1)
14906f8d59d8SJan Pechanec 				argv[i++] = p;
14916f8d59d8SJan Pechanec 		}
14926f8d59d8SJan Pechanec 
14936f8d59d8SJan Pechanec 		argv[i] = NULL;
14946f8d59d8SJan Pechanec 		optind = optreset = 1;
14956f8d59d8SJan Pechanec 		__progname = argv[0];
14966f8d59d8SJan Pechanec 		exit(sftp_server_main(i, argv, s->pw));
14976f8d59d8SJan Pechanec 	}
14986f8d59d8SJan Pechanec 
14997c478bd9Sstevel@tonic-gate 	/* Get the last component of the shell name. */
15007c478bd9Sstevel@tonic-gate 	if ((shell0 = strrchr(shell, '/')) != NULL)
15017c478bd9Sstevel@tonic-gate 		shell0++;
15027c478bd9Sstevel@tonic-gate 	else
15037c478bd9Sstevel@tonic-gate 		shell0 = shell;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/*
15067c478bd9Sstevel@tonic-gate 	 * If we have no command, execute the shell.  In this case, the shell
15077c478bd9Sstevel@tonic-gate 	 * name to be passed in argv[0] is preceded by '-' to indicate that
15087c478bd9Sstevel@tonic-gate 	 * this is a login shell.
15097c478bd9Sstevel@tonic-gate 	 */
15107c478bd9Sstevel@tonic-gate 	if (!command) {
15117c478bd9Sstevel@tonic-gate 		char argv0[256];
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 		/* Start the shell.  Set initial character to '-'. */
15147c478bd9Sstevel@tonic-gate 		argv0[0] = '-';
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 		if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
15177c478bd9Sstevel@tonic-gate 		    >= sizeof(argv0) - 1) {
15187c478bd9Sstevel@tonic-gate 			errno = EINVAL;
15197c478bd9Sstevel@tonic-gate 			perror(shell);
15207c478bd9Sstevel@tonic-gate 			exit(1);
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		/* Execute the shell. */
15247c478bd9Sstevel@tonic-gate 		argv[0] = argv0;
15257c478bd9Sstevel@tonic-gate 		argv[1] = NULL;
15267c478bd9Sstevel@tonic-gate 		execve(shell, argv, env);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 		/* Executing the shell failed. */
15297c478bd9Sstevel@tonic-gate 		perror(shell);
15307c478bd9Sstevel@tonic-gate 		exit(1);
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 	/*
15337c478bd9Sstevel@tonic-gate 	 * Execute the command using the user's shell.  This uses the -c
15347c478bd9Sstevel@tonic-gate 	 * option to execute the command.
15357c478bd9Sstevel@tonic-gate 	 */
15367c478bd9Sstevel@tonic-gate 	argv[0] = (char *) shell0;
15377c478bd9Sstevel@tonic-gate 	argv[1] = "-c";
15387c478bd9Sstevel@tonic-gate 	argv[2] = (char *) command;
15397c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
15407c478bd9Sstevel@tonic-gate 	execve(shell, argv, env);
15417c478bd9Sstevel@tonic-gate 	perror(shell);
15427c478bd9Sstevel@tonic-gate 	exit(1);
15437c478bd9Sstevel@tonic-gate }
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate Session *
15467c478bd9Sstevel@tonic-gate session_new(void)
15477c478bd9Sstevel@tonic-gate {
15487c478bd9Sstevel@tonic-gate 	int i;
15497c478bd9Sstevel@tonic-gate 	static int did_init = 0;
15507c478bd9Sstevel@tonic-gate 	if (!did_init) {
15517c478bd9Sstevel@tonic-gate 		debug("session_new: init");
15527c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAX_SESSIONS; i++) {
15537c478bd9Sstevel@tonic-gate 			sessions[i].used = 0;
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 		did_init = 1;
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
15587c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
15597c478bd9Sstevel@tonic-gate 		if (! s->used) {
15607c478bd9Sstevel@tonic-gate 			memset(s, 0, sizeof(*s));
15617c478bd9Sstevel@tonic-gate 			s->chanid = -1;
15627c478bd9Sstevel@tonic-gate 			s->ptyfd = -1;
15637c478bd9Sstevel@tonic-gate 			s->ttyfd = -1;
15647c478bd9Sstevel@tonic-gate 			s->used = 1;
15657c478bd9Sstevel@tonic-gate 			s->self = i;
15667c478bd9Sstevel@tonic-gate 			s->env = NULL;
15677c478bd9Sstevel@tonic-gate 			debug("session_new: session %d", i);
15687c478bd9Sstevel@tonic-gate 			return s;
15697c478bd9Sstevel@tonic-gate 		}
15707c478bd9Sstevel@tonic-gate 	}
15717c478bd9Sstevel@tonic-gate 	return NULL;
15727c478bd9Sstevel@tonic-gate }
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate static void
15757c478bd9Sstevel@tonic-gate session_dump(void)
15767c478bd9Sstevel@tonic-gate {
15777c478bd9Sstevel@tonic-gate 	int i;
15787c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
15797c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
15807c478bd9Sstevel@tonic-gate 		debug("dump: used %d session %d %p channel %d pid %ld",
15817c478bd9Sstevel@tonic-gate 		    s->used,
15827c478bd9Sstevel@tonic-gate 		    s->self,
15837c478bd9Sstevel@tonic-gate 		    s,
15847c478bd9Sstevel@tonic-gate 		    s->chanid,
15857c478bd9Sstevel@tonic-gate 		    (long)s->pid);
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate int
15907c478bd9Sstevel@tonic-gate session_open(Authctxt *authctxt, int chanid)
15917c478bd9Sstevel@tonic-gate {
15927c478bd9Sstevel@tonic-gate 	Session *s = session_new();
15937c478bd9Sstevel@tonic-gate 	debug("session_open: channel %d", chanid);
15947c478bd9Sstevel@tonic-gate 	if (s == NULL) {
15957c478bd9Sstevel@tonic-gate 		error("no more sessions");
15967c478bd9Sstevel@tonic-gate 		return 0;
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 	s->authctxt = authctxt;
15997c478bd9Sstevel@tonic-gate 	s->pw = authctxt->pw;
16007c478bd9Sstevel@tonic-gate 	if (s->pw == NULL)
16017c478bd9Sstevel@tonic-gate 		fatal("no user for session %d", s->self);
16027c478bd9Sstevel@tonic-gate 	debug("session_open: session %d: link with channel %d", s->self, chanid);
16037c478bd9Sstevel@tonic-gate 	s->chanid = chanid;
16047c478bd9Sstevel@tonic-gate 	return 1;
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate #ifndef lint
16087c478bd9Sstevel@tonic-gate Session *
16097c478bd9Sstevel@tonic-gate session_by_tty(char *tty)
16107c478bd9Sstevel@tonic-gate {
16117c478bd9Sstevel@tonic-gate 	int i;
16127c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
16137c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
16147c478bd9Sstevel@tonic-gate 		if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
16157c478bd9Sstevel@tonic-gate 			debug("session_by_tty: session %d tty %s", i, tty);
16167c478bd9Sstevel@tonic-gate 			return s;
16177c478bd9Sstevel@tonic-gate 		}
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 	debug("session_by_tty: unknown tty %.100s", tty);
16207c478bd9Sstevel@tonic-gate 	session_dump();
16217c478bd9Sstevel@tonic-gate 	return NULL;
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate #endif /* lint */
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate static Session *
16267c478bd9Sstevel@tonic-gate session_by_channel(int id)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	int i;
16297c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
16307c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
16317c478bd9Sstevel@tonic-gate 		if (s->used && s->chanid == id) {
16327c478bd9Sstevel@tonic-gate 			debug("session_by_channel: session %d channel %d", i, id);
16337c478bd9Sstevel@tonic-gate 			return s;
16347c478bd9Sstevel@tonic-gate 		}
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 	debug("session_by_channel: unknown channel %d", id);
16377c478bd9Sstevel@tonic-gate 	session_dump();
16387c478bd9Sstevel@tonic-gate 	return NULL;
16397c478bd9Sstevel@tonic-gate }
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate static Session *
16427c478bd9Sstevel@tonic-gate session_by_pid(pid_t pid)
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate 	int i;
16457c478bd9Sstevel@tonic-gate 	debug("session_by_pid: pid %ld", (long)pid);
16467c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
16477c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
16487c478bd9Sstevel@tonic-gate 		if (s->used && s->pid == pid)
16497c478bd9Sstevel@tonic-gate 			return s;
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 	error("session_by_pid: unknown pid %ld", (long)pid);
16527c478bd9Sstevel@tonic-gate 	session_dump();
16537c478bd9Sstevel@tonic-gate 	return NULL;
16547c478bd9Sstevel@tonic-gate }
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate static int
16577c478bd9Sstevel@tonic-gate session_window_change_req(Session *s)
16587c478bd9Sstevel@tonic-gate {
16597c478bd9Sstevel@tonic-gate 	s->col = packet_get_int();
16607c478bd9Sstevel@tonic-gate 	s->row = packet_get_int();
16617c478bd9Sstevel@tonic-gate 	s->xpixel = packet_get_int();
16627c478bd9Sstevel@tonic-gate 	s->ypixel = packet_get_int();
16637c478bd9Sstevel@tonic-gate 	packet_check_eom();
16647c478bd9Sstevel@tonic-gate 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
16657c478bd9Sstevel@tonic-gate 	return 1;
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate static int
16697c478bd9Sstevel@tonic-gate session_pty_req(Session *s)
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate 	u_int len;
16727c478bd9Sstevel@tonic-gate 	int n_bytes;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	if (no_pty_flag) {
16757c478bd9Sstevel@tonic-gate 		debug("Allocating a pty not permitted for this authentication.");
16767c478bd9Sstevel@tonic-gate 		return 0;
16777c478bd9Sstevel@tonic-gate 	}
16787c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1) {
16797c478bd9Sstevel@tonic-gate 		packet_disconnect("Protocol error: you already have a pty.");
16807c478bd9Sstevel@tonic-gate 		return 0;
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 	/* Get the time and hostname when the user last logged in. */
16837c478bd9Sstevel@tonic-gate 	if (options.print_lastlog) {
16847c478bd9Sstevel@tonic-gate 		s->hostname[0] = '\0';
16857c478bd9Sstevel@tonic-gate 		s->last_login_time = get_last_login_time(s->pw->pw_uid,
16867c478bd9Sstevel@tonic-gate 		    s->pw->pw_name, s->hostname, sizeof(s->hostname));
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		/*
16897c478bd9Sstevel@tonic-gate 		 * PAM may update the last login date.
16907c478bd9Sstevel@tonic-gate 		 *
16917c478bd9Sstevel@tonic-gate 		 * Ideally PAM would also show the last login date as a
16927c478bd9Sstevel@tonic-gate 		 * PAM_TEXT_INFO conversation message, and then we could just
16937c478bd9Sstevel@tonic-gate 		 * always force the use of keyboard-interactive just so we can
16947c478bd9Sstevel@tonic-gate 		 * pass any such PAM prompts and messages from the account and
16957c478bd9Sstevel@tonic-gate 		 * session stacks, but skip pam_authenticate() if other userauth
16967c478bd9Sstevel@tonic-gate 		 * has succeeded and the user's password isn't expired.
16977c478bd9Sstevel@tonic-gate 		 *
16987c478bd9Sstevel@tonic-gate 		 * Unfortunately this depends on support for keyboard-
16997c478bd9Sstevel@tonic-gate 		 * interactive in the client, and support for lastlog messages
17007c478bd9Sstevel@tonic-gate 		 * in some PAM module.
17017c478bd9Sstevel@tonic-gate 		 *
17027c478bd9Sstevel@tonic-gate 		 * As it is Solaris updates the lastlog in PAM, but does
17037c478bd9Sstevel@tonic-gate 		 * not show the lastlog date in PAM.  If and when this state of
17047c478bd9Sstevel@tonic-gate 		 * affairs changes this hack can be reconsidered, and, maybe,
17057c478bd9Sstevel@tonic-gate 		 * removed.
17067c478bd9Sstevel@tonic-gate 		 *
17077c478bd9Sstevel@tonic-gate 		 * So we're stuck with a crude hack: get the lastlog
17087c478bd9Sstevel@tonic-gate 		 * time before calling pam_open_session() and store it
17097c478bd9Sstevel@tonic-gate 		 * in the Authctxt and then use it here once.  After
17107c478bd9Sstevel@tonic-gate 		 * that, if the client opens any more pty sessions we'll
17117c478bd9Sstevel@tonic-gate 		 * show the last lastlog entry since userauth.
17127c478bd9Sstevel@tonic-gate 		 */
17137c478bd9Sstevel@tonic-gate 		if (s->authctxt != NULL && s->authctxt->last_login_time > 0) {
17147c478bd9Sstevel@tonic-gate 			s->last_login_time = s->authctxt->last_login_time;
17157c478bd9Sstevel@tonic-gate 			(void) strlcpy(s->hostname,
17167c478bd9Sstevel@tonic-gate 				       s->authctxt->last_login_host,
17177c478bd9Sstevel@tonic-gate 				       sizeof(s->hostname));
17187c478bd9Sstevel@tonic-gate 			s->authctxt->last_login_time = 0;
17197c478bd9Sstevel@tonic-gate 			s->authctxt->last_login_host[0] = '\0';
17207c478bd9Sstevel@tonic-gate 		}
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	s->term = packet_get_string(&len);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	if (compat20) {
17267c478bd9Sstevel@tonic-gate 		s->col = packet_get_int();
17277c478bd9Sstevel@tonic-gate 		s->row = packet_get_int();
17287c478bd9Sstevel@tonic-gate 	} else {
17297c478bd9Sstevel@tonic-gate 		s->row = packet_get_int();
17307c478bd9Sstevel@tonic-gate 		s->col = packet_get_int();
17317c478bd9Sstevel@tonic-gate 	}
17327c478bd9Sstevel@tonic-gate 	s->xpixel = packet_get_int();
17337c478bd9Sstevel@tonic-gate 	s->ypixel = packet_get_int();
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (strcmp(s->term, "") == 0) {
17367c478bd9Sstevel@tonic-gate 		xfree(s->term);
17377c478bd9Sstevel@tonic-gate 		s->term = NULL;
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/* Allocate a pty and open it. */
17417c478bd9Sstevel@tonic-gate 	debug("Allocating pty.");
17429a8058b5Sjp161948 	if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
17437c478bd9Sstevel@tonic-gate 		if (s->term)
17447c478bd9Sstevel@tonic-gate 			xfree(s->term);
17457c478bd9Sstevel@tonic-gate 		s->term = NULL;
17467c478bd9Sstevel@tonic-gate 		s->ptyfd = -1;
17477c478bd9Sstevel@tonic-gate 		s->ttyfd = -1;
17487c478bd9Sstevel@tonic-gate 		error("session_pty_req: session %d alloc failed", s->self);
17497c478bd9Sstevel@tonic-gate 		return 0;
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 	debug("session_pty_req: session %d alloc %s", s->self, s->tty);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/* for SSH1 the tty modes length is not given */
17547c478bd9Sstevel@tonic-gate 	if (!compat20)
17557c478bd9Sstevel@tonic-gate 		n_bytes = packet_remaining();
17567c478bd9Sstevel@tonic-gate 	tty_parse_modes(s->ttyfd, &n_bytes);
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	/*
17597c478bd9Sstevel@tonic-gate 	 * Add a cleanup function to clear the utmp entry and record logout
17607c478bd9Sstevel@tonic-gate 	 * time in case we call fatal() (e.g., the connection gets closed).
17617c478bd9Sstevel@tonic-gate 	 */
17627c478bd9Sstevel@tonic-gate 	fatal_add_cleanup(session_pty_cleanup, (void *)s);
17637c478bd9Sstevel@tonic-gate 	pty_setowner(s->pw, s->tty);
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	/* Set window size from the packet. */
17667c478bd9Sstevel@tonic-gate 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	packet_check_eom();
17697c478bd9Sstevel@tonic-gate 	session_proctitle(s);
17707c478bd9Sstevel@tonic-gate 	return 1;
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate static int
17747c478bd9Sstevel@tonic-gate session_subsystem_req(Session *s)
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate 	struct stat st;
17777c478bd9Sstevel@tonic-gate 	u_int len;
17787c478bd9Sstevel@tonic-gate 	int success = 0;
17796f8d59d8SJan Pechanec 	char *prog, *cmd, *subsys = packet_get_string(&len);
17806f8d59d8SJan Pechanec 	u_int i;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	packet_check_eom();
17837c478bd9Sstevel@tonic-gate 	log("subsystem request for %.100s", subsys);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	for (i = 0; i < options.num_subsystems; i++) {
17867c478bd9Sstevel@tonic-gate 		if (strcmp(subsys, options.subsystem_name[i]) == 0) {
17876f8d59d8SJan Pechanec 			prog = options.subsystem_command[i];
17886f8d59d8SJan Pechanec 			cmd = options.subsystem_args[i];
17896f8d59d8SJan Pechanec 			if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
17906f8d59d8SJan Pechanec 				s->is_subsystem = SUBSYSTEM_INT_SFTP;
17916f8d59d8SJan Pechanec 			/*
17926f8d59d8SJan Pechanec 			 * We must stat(2) the subsystem before we chroot in
17936f8d59d8SJan Pechanec 			 * order to be able to send a proper error message.
17946f8d59d8SJan Pechanec 			 */
17956f8d59d8SJan Pechanec 			} else if (chroot_requested(options.chroot_directory)) {
17966f8d59d8SJan Pechanec 				char chdirsub[MAXPATHLEN];
17976f8d59d8SJan Pechanec 
17986f8d59d8SJan Pechanec 				strlcpy(chdirsub, options.chroot_directory,
17996f8d59d8SJan Pechanec 				    sizeof (chdirsub));
18006f8d59d8SJan Pechanec 				strlcat(chdirsub, "/", sizeof (chdirsub));
18016f8d59d8SJan Pechanec 				strlcat(chdirsub, prog, sizeof (chdirsub));
18026f8d59d8SJan Pechanec 				if (stat(chdirsub, &st) < 0) {
18036f8d59d8SJan Pechanec 					error("subsystem: cannot stat %s under "
18046f8d59d8SJan Pechanec 					    "chroot directory %s: %s", prog,
18056f8d59d8SJan Pechanec 					    options.chroot_directory,
18067c478bd9Sstevel@tonic-gate 					    strerror(errno));
18076f8d59d8SJan Pechanec 					if (strcmp(subsys, "sftp") == 0)
18086f8d59d8SJan Pechanec 						error("subsystem: please see "
18096f8d59d8SJan Pechanec 						    "the Subsystem option in "
18106f8d59d8SJan Pechanec 						    "sshd_config(4) for an "
18116f8d59d8SJan Pechanec 						    "explanation of '%s'.",
18126f8d59d8SJan Pechanec 						    INTERNAL_SFTP_NAME);
18137c478bd9Sstevel@tonic-gate 					break;
18147c478bd9Sstevel@tonic-gate 				}
18156f8d59d8SJan Pechanec 			} else if (stat(prog, &st) < 0) {
18166f8d59d8SJan Pechanec 				error("subsystem: cannot stat %s: %s", prog,
18176f8d59d8SJan Pechanec 				    strerror(errno));
18186f8d59d8SJan Pechanec 				break;
18196f8d59d8SJan Pechanec 			} else {
18206f8d59d8SJan Pechanec 				s->is_subsystem = SUBSYSTEM_EXT;
18216f8d59d8SJan Pechanec 			}
18227c478bd9Sstevel@tonic-gate 			debug("subsystem: exec() %s", cmd);
18237c478bd9Sstevel@tonic-gate 			do_exec(s, cmd);
18247c478bd9Sstevel@tonic-gate 			success = 1;
18257c478bd9Sstevel@tonic-gate 			break;
18267c478bd9Sstevel@tonic-gate 		}
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	if (!success)
18307c478bd9Sstevel@tonic-gate 		log("subsystem request for %.100s failed, subsystem not found",
18317c478bd9Sstevel@tonic-gate 		    subsys);
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	xfree(subsys);
18347c478bd9Sstevel@tonic-gate 	return success;
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
1837a6e0e77dSjp161948 /*
1838a6e0e77dSjp161948  * Serve "x11-req" channel request for X11 forwarding for the current session
1839a6e0e77dSjp161948  * channel.
1840a6e0e77dSjp161948  */
18417c478bd9Sstevel@tonic-gate static int
18427c478bd9Sstevel@tonic-gate session_x11_req(Session *s)
18437c478bd9Sstevel@tonic-gate {
1844c2a2f8dcSjp161948 	int success, fd;
1845c2a2f8dcSjp161948 	char xauthdir[] = "/tmp/ssh-xauth-XXXXXX";
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	s->single_connection = packet_get_char();
18487c478bd9Sstevel@tonic-gate 	s->auth_proto = packet_get_string(NULL);
18497c478bd9Sstevel@tonic-gate 	s->auth_data = packet_get_string(NULL);
18507c478bd9Sstevel@tonic-gate 	s->screen = packet_get_int();
18517c478bd9Sstevel@tonic-gate 	packet_check_eom();
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	success = session_setup_x11fwd(s);
18547c478bd9Sstevel@tonic-gate 	if (!success) {
18557c478bd9Sstevel@tonic-gate 		xfree(s->auth_proto);
18567c478bd9Sstevel@tonic-gate 		xfree(s->auth_data);
18577c478bd9Sstevel@tonic-gate 		s->auth_proto = NULL;
18587c478bd9Sstevel@tonic-gate 		s->auth_data = NULL;
1859743541abSjp161948 		return (success);
18607c478bd9Sstevel@tonic-gate 	}
1861a6e0e77dSjp161948 
1862a6e0e77dSjp161948 	/*
1863a6e0e77dSjp161948 	 * Create per session X authority file so that different sessions
1864a6e0e77dSjp161948 	 * don't contend for one common file. The reason for this is that
1865a6e0e77dSjp161948 	 * xauth(1) locking doesn't work too well over network filesystems.
1866a6e0e77dSjp161948 	 *
1867c2a2f8dcSjp161948 	 * If mkdtemp() or open() fails then s->auth_file remains NULL which
1868c2a2f8dcSjp161948 	 * means that we won't set XAUTHORITY variable in child's environment
1869c2a2f8dcSjp161948 	 * and xauth(1) will use the default location for the authority file.
1870a6e0e77dSjp161948 	 */
1871743541abSjp161948 	if (mkdtemp(xauthdir) != NULL) {
1872a6e0e77dSjp161948 		s->auth_file = xmalloc(MAXPATHLEN);
1873a6e0e77dSjp161948 		snprintf(s->auth_file, MAXPATHLEN, "%s/xauthfile",
1874a6e0e77dSjp161948 		    xauthdir);
1875a6e0e77dSjp161948 		/*
1876c2a2f8dcSjp161948 		 * we don't want that "creating new authority file" message to
1877c2a2f8dcSjp161948 		 * be printed by xauth(1) so we must create that file
1878c2a2f8dcSjp161948 		 * beforehand.
1879c2a2f8dcSjp161948 		 */
1880c2a2f8dcSjp161948 		if ((fd = open(s->auth_file, O_CREAT | O_EXCL | O_RDONLY,
1881c2a2f8dcSjp161948 		    S_IRUSR | S_IWUSR)) == -1) {
1882c2a2f8dcSjp161948 			error("failed to create the temporary X authority "
1883c2a2f8dcSjp161948 			    "file %s: %.100s; will use the default one",
1884c2a2f8dcSjp161948 			    s->auth_file, strerror(errno));
1885c2a2f8dcSjp161948 			xfree(s->auth_file);
1886c2a2f8dcSjp161948 			s->auth_file = NULL;
1887c2a2f8dcSjp161948 			if (rmdir(xauthdir) == -1) {
1888c2a2f8dcSjp161948 				error("cannot remove xauth directory %s: %.100s",
1889c2a2f8dcSjp161948 				    xauthdir, strerror(errno));
1890c2a2f8dcSjp161948 			}
1891c2a2f8dcSjp161948 		} else {
1892c2a2f8dcSjp161948 			close(fd);
1893c2a2f8dcSjp161948 			debug("temporary X authority file %s created",
1894c2a2f8dcSjp161948 			    s->auth_file);
1895c2a2f8dcSjp161948 
1896c2a2f8dcSjp161948 			/*
1897a6e0e77dSjp161948 			 * add a cleanup function to remove the temporary
1898a6e0e77dSjp161948 			 * xauth file in case we call fatal() (e.g., the
1899a6e0e77dSjp161948 			 * connection gets closed).
1900a6e0e77dSjp161948 			 */
1901a6e0e77dSjp161948 			fatal_add_cleanup(session_xauthfile_cleanup, (void *)s);
1902c2a2f8dcSjp161948 		}
1903c2a2f8dcSjp161948 	}
1904c2a2f8dcSjp161948 	else {
1905c2a2f8dcSjp161948 		error("failed to create a directory for the temporary X "
1906c2a2f8dcSjp161948 		    "authority file: %.100s; will use the default xauth file",
1907c2a2f8dcSjp161948 		    strerror(errno));
1908a6e0e77dSjp161948 	}
1909a6e0e77dSjp161948 
1910743541abSjp161948 	return (success);
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate static int
19147c478bd9Sstevel@tonic-gate session_shell_req(Session *s)
19157c478bd9Sstevel@tonic-gate {
19167c478bd9Sstevel@tonic-gate 	packet_check_eom();
19177c478bd9Sstevel@tonic-gate 	do_exec(s, NULL);
19187c478bd9Sstevel@tonic-gate 	return 1;
19197c478bd9Sstevel@tonic-gate }
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate static int
19227c478bd9Sstevel@tonic-gate session_exec_req(Session *s)
19237c478bd9Sstevel@tonic-gate {
19247c478bd9Sstevel@tonic-gate 	u_int len;
19257c478bd9Sstevel@tonic-gate 	char *command = packet_get_string(&len);
19267c478bd9Sstevel@tonic-gate 	packet_check_eom();
19277c478bd9Sstevel@tonic-gate 	do_exec(s, command);
19287c478bd9Sstevel@tonic-gate 	xfree(command);
19297c478bd9Sstevel@tonic-gate 	return 1;
19307c478bd9Sstevel@tonic-gate }
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate static int
19337c478bd9Sstevel@tonic-gate session_auth_agent_req(Session *s)
19347c478bd9Sstevel@tonic-gate {
19357c478bd9Sstevel@tonic-gate 	static int called = 0;
19367c478bd9Sstevel@tonic-gate 	packet_check_eom();
19377c478bd9Sstevel@tonic-gate 	if (no_agent_forwarding_flag) {
19387c478bd9Sstevel@tonic-gate 		debug("session_auth_agent_req: no_agent_forwarding_flag");
19397c478bd9Sstevel@tonic-gate 		return 0;
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 	if (called) {
19427c478bd9Sstevel@tonic-gate 		return 0;
19437c478bd9Sstevel@tonic-gate 	} else {
19447c478bd9Sstevel@tonic-gate 		called = 1;
19457c478bd9Sstevel@tonic-gate 		return auth_input_request_forwarding(s->pw);
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate }
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate static int
19507c478bd9Sstevel@tonic-gate session_loc_env_check(char *var, char *val)
19517c478bd9Sstevel@tonic-gate {
19527c478bd9Sstevel@tonic-gate 	char *current;
19537c478bd9Sstevel@tonic-gate 	int cat, ret;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	if (strcmp(var, "LANG") == 0)
19567c478bd9Sstevel@tonic-gate 		cat = LC_ALL;
19577c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_ALL") == 0)
19587c478bd9Sstevel@tonic-gate 		cat = LC_ALL;
19597c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_CTYPE") == 0)
19607c478bd9Sstevel@tonic-gate 		cat = LC_CTYPE;
19617c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_COLLATE") == 0)
19627c478bd9Sstevel@tonic-gate 		cat = LC_COLLATE;
19637c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_TIME") == 0)
19647c478bd9Sstevel@tonic-gate 		cat = LC_TIME;
19657c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_NUMERIC") == 0)
19667c478bd9Sstevel@tonic-gate 		cat = LC_NUMERIC;
19677c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_MONETARY") == 0)
19687c478bd9Sstevel@tonic-gate 		cat = LC_MONETARY;
19697c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_MESSAGES") == 0)
19707c478bd9Sstevel@tonic-gate 		cat = LC_MESSAGES;
19717c478bd9Sstevel@tonic-gate 
1972cd7d5fafSJan Pechanec 	current = setlocale(cat, NULL);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	ret = (setlocale(cat, val) != NULL);
19757c478bd9Sstevel@tonic-gate 	(void) setlocale(cat, current);
19767c478bd9Sstevel@tonic-gate 	return (ret);
19777c478bd9Sstevel@tonic-gate }
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate static int
19807c478bd9Sstevel@tonic-gate session_env_req(Session *s)
19817c478bd9Sstevel@tonic-gate {
19827c478bd9Sstevel@tonic-gate 	Channel *c;
19837c478bd9Sstevel@tonic-gate 	char *var, *val, *e;
19847c478bd9Sstevel@tonic-gate 	char **p;
19857c478bd9Sstevel@tonic-gate 	size_t len;
19867c478bd9Sstevel@tonic-gate 	int ret = 0;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	/* Get var/val from the rest of this packet */
19897c478bd9Sstevel@tonic-gate 	var = packet_get_string(NULL);
19907c478bd9Sstevel@tonic-gate 	val = packet_get_string(NULL);
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	/*
19937c478bd9Sstevel@tonic-gate 	 * We'll need the channel ID for the packet_send_debug messages,
19947c478bd9Sstevel@tonic-gate 	 * so get it now.
19957c478bd9Sstevel@tonic-gate 	 */
19967c478bd9Sstevel@tonic-gate 	if ((c = channel_lookup(s->chanid)) == NULL)
19977c478bd9Sstevel@tonic-gate 		goto done;	/* shouldn't happen! */
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	debug2("Received request for environment variable %s=%s", var, val);
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	/* For now allow only LANG and LC_* */
20027c478bd9Sstevel@tonic-gate 	if (strcmp(var, "LANG") != 0 && strncmp(var, "LC_", 3) != 0) {
20037c478bd9Sstevel@tonic-gate 		debug2("Rejecting request for environment variable %s", var);
20047c478bd9Sstevel@tonic-gate 		goto done;
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	if (!session_loc_env_check(var, val)) {
20087c478bd9Sstevel@tonic-gate 		packet_send_debug(gettext("Missing locale support for %s=%s"),
20097c478bd9Sstevel@tonic-gate 			var, val);
20107c478bd9Sstevel@tonic-gate 		goto done;
20117c478bd9Sstevel@tonic-gate 	}
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	packet_send_debug(gettext("Channel %d set: %s=%s"), c->remote_id,
20147c478bd9Sstevel@tonic-gate 		var, val);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	/*
20177c478bd9Sstevel@tonic-gate 	 * Always append new environment variables without regard to old
20187c478bd9Sstevel@tonic-gate 	 * ones being overriden.  The way these are actually added to
20197c478bd9Sstevel@tonic-gate 	 * the environment of the session process later settings
20207c478bd9Sstevel@tonic-gate 	 * override earlier ones; see copy_environment().
20217c478bd9Sstevel@tonic-gate 	 */
20227c478bd9Sstevel@tonic-gate 	if (s->env == NULL) {
20237c478bd9Sstevel@tonic-gate 		char **env;
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 		env = xmalloc(sizeof (char **) * 2);
20267c478bd9Sstevel@tonic-gate 		memset(env, 0, sizeof (char **) * 2);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 		s->env = env;
20297c478bd9Sstevel@tonic-gate 		p = env;
20307c478bd9Sstevel@tonic-gate 	} else {
20317c478bd9Sstevel@tonic-gate 		for (p = s->env; *p != NULL ; p++);
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		s->env = xrealloc(s->env, (p - s->env + 2) * sizeof (char **));
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 		for (p = s->env; *p != NULL ; p++);
20367c478bd9Sstevel@tonic-gate 	}
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	len = snprintf(NULL, 0, "%s=%s", var, val);
20397c478bd9Sstevel@tonic-gate 	e = xmalloc(len + 1);
20407c478bd9Sstevel@tonic-gate 	(void) snprintf(e, len + 1, "%s=%s", var, val);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	(*p++) = e;
20437c478bd9Sstevel@tonic-gate 	*p = NULL;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	ret = 1;
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate done:
20487c478bd9Sstevel@tonic-gate 	xfree(var);
20497c478bd9Sstevel@tonic-gate 	xfree(val);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	return (ret);
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate static void
20557c478bd9Sstevel@tonic-gate session_free_env(char ***envp)
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate 	char **env, **p;
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if (envp == NULL || *envp == NULL)
20607c478bd9Sstevel@tonic-gate 		return;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	env = *envp;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	*envp = NULL;
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	for (p = env; *p != NULL; p++)
20677c478bd9Sstevel@tonic-gate 		xfree(*p);
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	xfree(env);
20707c478bd9Sstevel@tonic-gate }
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate int
20737c478bd9Sstevel@tonic-gate session_input_channel_req(Channel *c, const char *rtype)
20747c478bd9Sstevel@tonic-gate {
20757c478bd9Sstevel@tonic-gate 	int success = 0;
20767c478bd9Sstevel@tonic-gate 	Session *s;
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	if ((s = session_by_channel(c->self)) == NULL) {
20797c478bd9Sstevel@tonic-gate 		log("session_input_channel_req: no session %d req %.100s",
20807c478bd9Sstevel@tonic-gate 		    c->self, rtype);
20817c478bd9Sstevel@tonic-gate 		return 0;
20827c478bd9Sstevel@tonic-gate 	}
20837c478bd9Sstevel@tonic-gate 	debug("session_input_channel_req: session %d req %s", s->self, rtype);
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	/*
20867c478bd9Sstevel@tonic-gate 	 * a session is in LARVAL state until a shell, a command
20877c478bd9Sstevel@tonic-gate 	 * or a subsystem is executed
20887c478bd9Sstevel@tonic-gate 	 */
20897c478bd9Sstevel@tonic-gate 	if (c->type == SSH_CHANNEL_LARVAL) {
20907c478bd9Sstevel@tonic-gate 		if (strcmp(rtype, "shell") == 0) {
20917c478bd9Sstevel@tonic-gate 			success = session_shell_req(s);
20927c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "exec") == 0) {
20937c478bd9Sstevel@tonic-gate 			success = session_exec_req(s);
20947c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "pty-req") == 0) {
20957c478bd9Sstevel@tonic-gate 			success =  session_pty_req(s);
20967c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "x11-req") == 0) {
20977c478bd9Sstevel@tonic-gate 			success = session_x11_req(s);
20987c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
20997c478bd9Sstevel@tonic-gate 			success = session_auth_agent_req(s);
21007c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "subsystem") == 0) {
21017c478bd9Sstevel@tonic-gate 			success = session_subsystem_req(s);
21027c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "env") == 0) {
21037c478bd9Sstevel@tonic-gate 			success = session_env_req(s);
21047c478bd9Sstevel@tonic-gate 		}
21057c478bd9Sstevel@tonic-gate 	}
21067c478bd9Sstevel@tonic-gate 	if (strcmp(rtype, "window-change") == 0) {
21077c478bd9Sstevel@tonic-gate 		success = session_window_change_req(s);
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	return success;
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate void
21137c478bd9Sstevel@tonic-gate session_set_fds(Session *s, int fdin, int fdout, int fderr)
21147c478bd9Sstevel@tonic-gate {
21157c478bd9Sstevel@tonic-gate 	if (!compat20)
21167c478bd9Sstevel@tonic-gate 		fatal("session_set_fds: called for proto != 2.0");
21177c478bd9Sstevel@tonic-gate 	/*
21187c478bd9Sstevel@tonic-gate 	 * now that have a child and a pipe to the child,
21197c478bd9Sstevel@tonic-gate 	 * we can activate our channel and register the fd's
21207c478bd9Sstevel@tonic-gate 	 */
21217c478bd9Sstevel@tonic-gate 	if (s->chanid == -1)
21227c478bd9Sstevel@tonic-gate 		fatal("no channel for session %d", s->self);
21237c478bd9Sstevel@tonic-gate 	channel_set_fds(s->chanid,
21247c478bd9Sstevel@tonic-gate 	    fdout, fdin, fderr,
21257c478bd9Sstevel@tonic-gate 	    fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
21267c478bd9Sstevel@tonic-gate 	    1,
21277c478bd9Sstevel@tonic-gate 	    CHAN_SES_WINDOW_DEFAULT);
21287c478bd9Sstevel@tonic-gate }
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate /*
21317c478bd9Sstevel@tonic-gate  * Function to perform pty cleanup. Also called if we get aborted abnormally
21327c478bd9Sstevel@tonic-gate  * (e.g., due to a dropped connection).
21337c478bd9Sstevel@tonic-gate  */
21347c478bd9Sstevel@tonic-gate void
21357c478bd9Sstevel@tonic-gate session_pty_cleanup2(void *session)
21367c478bd9Sstevel@tonic-gate {
21377c478bd9Sstevel@tonic-gate 	Session *s = session;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	if (s == NULL) {
21407c478bd9Sstevel@tonic-gate 		error("session_pty_cleanup: no session");
21417c478bd9Sstevel@tonic-gate 		return;
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 	if (s->ttyfd == -1)
21447c478bd9Sstevel@tonic-gate 		return;
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate #ifdef USE_PAM
21497c478bd9Sstevel@tonic-gate 	session_do_pam(s, 0);
21507c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	/* Record that the user has logged out. */
21537c478bd9Sstevel@tonic-gate 	if (s->pid != 0) {
2154*b9aa66a7SJan Pechanec 		debug3("Recording SSHv2 channel logout in utmpx/wtmpx");
21557c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
21567c478bd9Sstevel@tonic-gate 		altprivsep_record_logout(s->pid);
21577c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
21587c478bd9Sstevel@tonic-gate 	}
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	/* Release the pseudo-tty. */
21617c478bd9Sstevel@tonic-gate 	if (getuid() == 0)
21627c478bd9Sstevel@tonic-gate 		pty_release(s->tty);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 	/*
21657c478bd9Sstevel@tonic-gate 	 * Close the server side of the socket pairs.  We must do this after
21667c478bd9Sstevel@tonic-gate 	 * the pty cleanup, so that another process doesn't get this pty
21677c478bd9Sstevel@tonic-gate 	 * while we're still cleaning up.
21687c478bd9Sstevel@tonic-gate 	 */
21697c478bd9Sstevel@tonic-gate 	if (close(s->ptymaster) < 0)
21707c478bd9Sstevel@tonic-gate 		error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	/* unlink pty from session */
21737c478bd9Sstevel@tonic-gate 	s->ttyfd = -1;
21747c478bd9Sstevel@tonic-gate }
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate void
21777c478bd9Sstevel@tonic-gate session_pty_cleanup(void *session)
21787c478bd9Sstevel@tonic-gate {
21799a8058b5Sjp161948 	session_pty_cleanup2(session);
21807c478bd9Sstevel@tonic-gate }
21817c478bd9Sstevel@tonic-gate 
2182a6e0e77dSjp161948 /*
2183a6e0e77dSjp161948  * We use a different temporary X authority file per every session so we
2184a6e0e77dSjp161948  * should remove those files when fatal() is called.
2185a6e0e77dSjp161948  */
2186a6e0e77dSjp161948 void
2187a6e0e77dSjp161948 session_xauthfile_cleanup(void *session)
2188a6e0e77dSjp161948 {
2189a6e0e77dSjp161948 	Session *s = session;
2190a6e0e77dSjp161948 
2191a6e0e77dSjp161948 	if (s == NULL) {
2192a6e0e77dSjp161948 		error("session_xauthfile_cleanup: no session");
2193a6e0e77dSjp161948 		return;
2194a6e0e77dSjp161948 	}
2195a6e0e77dSjp161948 
2196a6e0e77dSjp161948 	debug("session_xauthfile_cleanup: session %d removing %s", s->self,
2197a6e0e77dSjp161948 	    s->auth_file);
2198a6e0e77dSjp161948 
2199a6e0e77dSjp161948 	if (unlink(s->auth_file) == -1) {
2200a6e0e77dSjp161948 		error("session_xauthfile_cleanup: cannot remove xauth file: "
2201a6e0e77dSjp161948 		    "%.100s", strerror(errno));
2202a6e0e77dSjp161948 		return;
2203a6e0e77dSjp161948 	}
2204a6e0e77dSjp161948 
2205a6e0e77dSjp161948 	/* dirname() will modify s->auth_file but that's ok */
2206a6e0e77dSjp161948 	if (rmdir(dirname(s->auth_file)) == -1) {
2207a6e0e77dSjp161948 		error("session_xauthfile_cleanup: "
2208a6e0e77dSjp161948 		    "cannot remove xauth directory: %.100s", strerror(errno));
2209a6e0e77dSjp161948 		return;
2210a6e0e77dSjp161948 	}
2211a6e0e77dSjp161948 }
2212a6e0e77dSjp161948 
22137c478bd9Sstevel@tonic-gate static char *
22147c478bd9Sstevel@tonic-gate sig2name(int sig)
22157c478bd9Sstevel@tonic-gate {
22167c478bd9Sstevel@tonic-gate #define SSH_SIG(x) if (sig == SIG ## x) return #x
22177c478bd9Sstevel@tonic-gate 	SSH_SIG(ABRT);
22187c478bd9Sstevel@tonic-gate 	SSH_SIG(ALRM);
22197c478bd9Sstevel@tonic-gate 	SSH_SIG(FPE);
22207c478bd9Sstevel@tonic-gate 	SSH_SIG(HUP);
22217c478bd9Sstevel@tonic-gate 	SSH_SIG(ILL);
22227c478bd9Sstevel@tonic-gate 	SSH_SIG(INT);
22237c478bd9Sstevel@tonic-gate 	SSH_SIG(KILL);
22247c478bd9Sstevel@tonic-gate 	SSH_SIG(PIPE);
22257c478bd9Sstevel@tonic-gate 	SSH_SIG(QUIT);
22267c478bd9Sstevel@tonic-gate 	SSH_SIG(SEGV);
22277c478bd9Sstevel@tonic-gate 	SSH_SIG(TERM);
22287c478bd9Sstevel@tonic-gate 	SSH_SIG(USR1);
22297c478bd9Sstevel@tonic-gate 	SSH_SIG(USR2);
22307c478bd9Sstevel@tonic-gate #undef	SSH_SIG
22317c478bd9Sstevel@tonic-gate 	return "SIG@openssh.com";
22327c478bd9Sstevel@tonic-gate }
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate static void
22357c478bd9Sstevel@tonic-gate session_exit_message(Session *s, int status)
22367c478bd9Sstevel@tonic-gate {
22377c478bd9Sstevel@tonic-gate 	Channel *c;
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	if ((c = channel_lookup(s->chanid)) == NULL)
22407c478bd9Sstevel@tonic-gate 		fatal("session_exit_message: session %d: no channel %d",
22417c478bd9Sstevel@tonic-gate 		    s->self, s->chanid);
22427c478bd9Sstevel@tonic-gate 	debug("session_exit_message: session %d channel %d pid %ld",
22437c478bd9Sstevel@tonic-gate 	    s->self, s->chanid, (long)s->pid);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
22467c478bd9Sstevel@tonic-gate 		channel_request_start(s->chanid, "exit-status", 0);
22477c478bd9Sstevel@tonic-gate 		packet_put_int(WEXITSTATUS(status));
22487c478bd9Sstevel@tonic-gate 		packet_send();
22497c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
22507c478bd9Sstevel@tonic-gate 		channel_request_start(s->chanid, "exit-signal", 0);
22517c478bd9Sstevel@tonic-gate 		packet_put_cstring(sig2name(WTERMSIG(status)));
22527c478bd9Sstevel@tonic-gate #ifdef WCOREDUMP
22537c478bd9Sstevel@tonic-gate 		packet_put_char(WCOREDUMP(status));
22547c478bd9Sstevel@tonic-gate #else /* WCOREDUMP */
22557c478bd9Sstevel@tonic-gate 		packet_put_char(0);
22567c478bd9Sstevel@tonic-gate #endif /* WCOREDUMP */
22577c478bd9Sstevel@tonic-gate 		packet_put_cstring("");
22587c478bd9Sstevel@tonic-gate 		packet_put_cstring("");
22597c478bd9Sstevel@tonic-gate 		packet_send();
22607c478bd9Sstevel@tonic-gate 	} else {
22617c478bd9Sstevel@tonic-gate 		/* Some weird exit cause.  Just exit. */
22627c478bd9Sstevel@tonic-gate 		packet_disconnect("wait returned status %04x.", status);
22637c478bd9Sstevel@tonic-gate 	}
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	/* Ok to close channel now */
22667c478bd9Sstevel@tonic-gate 	channel_set_wait_for_exit(s->chanid, 0);
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	/* disconnect channel */
22697c478bd9Sstevel@tonic-gate 	debug("session_exit_message: release channel %d", s->chanid);
22707c478bd9Sstevel@tonic-gate 	channel_cancel_cleanup(s->chanid);
22717c478bd9Sstevel@tonic-gate 	/*
22727c478bd9Sstevel@tonic-gate 	 * emulate a write failure with 'chan_write_failed', nobody will be
22737c478bd9Sstevel@tonic-gate 	 * interested in data we write.
22747c478bd9Sstevel@tonic-gate 	 * Note that we must not call 'chan_read_failed', since there could
22757c478bd9Sstevel@tonic-gate 	 * be some more data waiting in the pipe.
22767c478bd9Sstevel@tonic-gate 	 */
22777c478bd9Sstevel@tonic-gate 	if (c->ostate != CHAN_OUTPUT_CLOSED)
22787c478bd9Sstevel@tonic-gate 		chan_write_failed(c);
22797c478bd9Sstevel@tonic-gate 	s->chanid = -1;
22807c478bd9Sstevel@tonic-gate }
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate void
22837c478bd9Sstevel@tonic-gate session_close(Session *s)
22847c478bd9Sstevel@tonic-gate {
22857c478bd9Sstevel@tonic-gate 	debug("session_close: session %d pid %ld", s->self, (long)s->pid);
22867c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1) {
22877c478bd9Sstevel@tonic-gate 		fatal_remove_cleanup(session_pty_cleanup, (void *)s);
22887c478bd9Sstevel@tonic-gate 		session_pty_cleanup(s);
22897c478bd9Sstevel@tonic-gate 	}
2290a6e0e77dSjp161948 	if (s->auth_file != NULL) {
2291a6e0e77dSjp161948 		fatal_remove_cleanup(session_xauthfile_cleanup, (void *)s);
2292a6e0e77dSjp161948 		session_xauthfile_cleanup(s);
2293a6e0e77dSjp161948 		xfree(s->auth_file);
2294a6e0e77dSjp161948 	}
22957c478bd9Sstevel@tonic-gate 	if (s->term)
22967c478bd9Sstevel@tonic-gate 		xfree(s->term);
22977c478bd9Sstevel@tonic-gate 	if (s->display)
22987c478bd9Sstevel@tonic-gate 		xfree(s->display);
22997c478bd9Sstevel@tonic-gate 	if (s->auth_display)
23007c478bd9Sstevel@tonic-gate 		xfree(s->auth_display);
23017c478bd9Sstevel@tonic-gate 	if (s->auth_data)
23027c478bd9Sstevel@tonic-gate 		xfree(s->auth_data);
23037c478bd9Sstevel@tonic-gate 	if (s->auth_proto)
23047c478bd9Sstevel@tonic-gate 		xfree(s->auth_proto);
23057c478bd9Sstevel@tonic-gate 	if (s->command)
23067c478bd9Sstevel@tonic-gate 		xfree(s->command);
23077c478bd9Sstevel@tonic-gate 	session_free_env(&s->env);
23087c478bd9Sstevel@tonic-gate 	s->used = 0;
23097c478bd9Sstevel@tonic-gate 	session_proctitle(s);
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate void
23137c478bd9Sstevel@tonic-gate session_close_by_pid(pid_t pid, int status)
23147c478bd9Sstevel@tonic-gate {
23157c478bd9Sstevel@tonic-gate 	Session *s = session_by_pid(pid);
23167c478bd9Sstevel@tonic-gate 	if (s == NULL) {
23177c478bd9Sstevel@tonic-gate 		debug("session_close_by_pid: no session for pid %ld",
23187c478bd9Sstevel@tonic-gate 		    (long)pid);
23197c478bd9Sstevel@tonic-gate 		return;
23207c478bd9Sstevel@tonic-gate 	}
23217c478bd9Sstevel@tonic-gate 	if (s->chanid != -1)
23227c478bd9Sstevel@tonic-gate 		session_exit_message(s, status);
23237c478bd9Sstevel@tonic-gate 	session_close(s);
23247c478bd9Sstevel@tonic-gate }
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate /*
2327a6e0e77dSjp161948  * This is called when a channel dies before the session 'child' itself dies.
2328a6e0e77dSjp161948  * It can happen for example if we exit from an interactive shell before we
2329a6e0e77dSjp161948  * exit from forwarded X11 applications.
23307c478bd9Sstevel@tonic-gate  */
23317c478bd9Sstevel@tonic-gate void
23327c478bd9Sstevel@tonic-gate session_close_by_channel(int id, void *arg)
23337c478bd9Sstevel@tonic-gate {
23347c478bd9Sstevel@tonic-gate 	Session *s = session_by_channel(id);
23357c478bd9Sstevel@tonic-gate 	if (s == NULL) {
23367c478bd9Sstevel@tonic-gate 		debug("session_close_by_channel: no session for id %d", id);
23377c478bd9Sstevel@tonic-gate 		return;
23387c478bd9Sstevel@tonic-gate 	}
23397c478bd9Sstevel@tonic-gate 	debug("session_close_by_channel: channel %d child %ld",
23407c478bd9Sstevel@tonic-gate 	    id, (long)s->pid);
23417c478bd9Sstevel@tonic-gate 	if (s->pid != 0) {
23427c478bd9Sstevel@tonic-gate 		debug("session_close_by_channel: channel %d: has child", id);
23437c478bd9Sstevel@tonic-gate 		/*
23447c478bd9Sstevel@tonic-gate 		 * delay detach of session, but release pty, since
23457c478bd9Sstevel@tonic-gate 		 * the fd's to the child are already closed
23467c478bd9Sstevel@tonic-gate 		 */
23477c478bd9Sstevel@tonic-gate 		if (s->ttyfd != -1) {
23487c478bd9Sstevel@tonic-gate 			fatal_remove_cleanup(session_pty_cleanup, (void *)s);
23497c478bd9Sstevel@tonic-gate 			session_pty_cleanup(s);
23507c478bd9Sstevel@tonic-gate 		}
23517c478bd9Sstevel@tonic-gate 		return;
23527c478bd9Sstevel@tonic-gate 	}
23537c478bd9Sstevel@tonic-gate 	/* detach by removing callback */
23547c478bd9Sstevel@tonic-gate 	channel_cancel_cleanup(s->chanid);
23557c478bd9Sstevel@tonic-gate 	s->chanid = -1;
23567c478bd9Sstevel@tonic-gate 	session_close(s);
23577c478bd9Sstevel@tonic-gate }
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate void
23607c478bd9Sstevel@tonic-gate session_destroy_all(void (*closefunc)(Session *))
23617c478bd9Sstevel@tonic-gate {
23627c478bd9Sstevel@tonic-gate 	int i;
23637c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
23647c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
23657c478bd9Sstevel@tonic-gate 		if (s->used) {
23667c478bd9Sstevel@tonic-gate 			if (closefunc != NULL)
23677c478bd9Sstevel@tonic-gate 				closefunc(s);
23687c478bd9Sstevel@tonic-gate 			else
23697c478bd9Sstevel@tonic-gate 				session_close(s);
23707c478bd9Sstevel@tonic-gate 		}
23717c478bd9Sstevel@tonic-gate 	}
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate static char *
23757c478bd9Sstevel@tonic-gate session_tty_list(void)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	static char buf[1024];
23787c478bd9Sstevel@tonic-gate 	int i;
23797c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
23807c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
23817c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
23827c478bd9Sstevel@tonic-gate 		if (s->used && s->ttyfd != -1) {
23837c478bd9Sstevel@tonic-gate 			if (buf[0] != '\0')
23847c478bd9Sstevel@tonic-gate 				strlcat(buf, ",", sizeof buf);
23857c478bd9Sstevel@tonic-gate 			strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
23867c478bd9Sstevel@tonic-gate 		}
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate 	if (buf[0] == '\0')
23897c478bd9Sstevel@tonic-gate 		strlcpy(buf, "notty", sizeof buf);
23907c478bd9Sstevel@tonic-gate 	return buf;
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate void
23947c478bd9Sstevel@tonic-gate session_proctitle(Session *s)
23957c478bd9Sstevel@tonic-gate {
23967c478bd9Sstevel@tonic-gate 	if (s->pw == NULL)
23977c478bd9Sstevel@tonic-gate 		error("no user for session %d", s->self);
23987c478bd9Sstevel@tonic-gate 	else
23997c478bd9Sstevel@tonic-gate 		setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
24007c478bd9Sstevel@tonic-gate }
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate int
24037c478bd9Sstevel@tonic-gate session_setup_x11fwd(Session *s)
24047c478bd9Sstevel@tonic-gate {
24057c478bd9Sstevel@tonic-gate 	struct stat st;
24067c478bd9Sstevel@tonic-gate 	char display[512], auth_display[512];
24077c478bd9Sstevel@tonic-gate 	char hostname[MAXHOSTNAMELEN];
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	if (no_x11_forwarding_flag) {
24107c478bd9Sstevel@tonic-gate 		packet_send_debug("X11 forwarding disabled in user configuration file.");
24117c478bd9Sstevel@tonic-gate 		return 0;
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 	if (!options.x11_forwarding) {
24147c478bd9Sstevel@tonic-gate 		debug("X11 forwarding disabled in server configuration file.");
24157c478bd9Sstevel@tonic-gate 		return 0;
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 	if (!options.xauth_location ||
24187c478bd9Sstevel@tonic-gate 	    (stat(options.xauth_location, &st) == -1)) {
24197c478bd9Sstevel@tonic-gate 		packet_send_debug("No xauth program; cannot forward with spoofing.");
24207c478bd9Sstevel@tonic-gate 		return 0;
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 	if (s->display != NULL) {
24237c478bd9Sstevel@tonic-gate 		debug("X11 display already set.");
24247c478bd9Sstevel@tonic-gate 		return 0;
24257c478bd9Sstevel@tonic-gate 	}
24267c478bd9Sstevel@tonic-gate 	if (x11_create_display_inet(options.x11_display_offset,
24277c478bd9Sstevel@tonic-gate 	    options.x11_use_localhost, s->single_connection,
24287c478bd9Sstevel@tonic-gate 	    &s->display_number) == -1) {
24297c478bd9Sstevel@tonic-gate 		debug("x11_create_display_inet failed.");
24307c478bd9Sstevel@tonic-gate 		return 0;
24317c478bd9Sstevel@tonic-gate 	}
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	/* Set up a suitable value for the DISPLAY variable. */
24347c478bd9Sstevel@tonic-gate 	if (gethostname(hostname, sizeof(hostname)) < 0)
24357c478bd9Sstevel@tonic-gate 		fatal("gethostname: %.100s", strerror(errno));
24367c478bd9Sstevel@tonic-gate 	/*
24377c478bd9Sstevel@tonic-gate 	 * auth_display must be used as the displayname when the
24387c478bd9Sstevel@tonic-gate 	 * authorization entry is added with xauth(1).  This will be
24397c478bd9Sstevel@tonic-gate 	 * different than the DISPLAY string for localhost displays.
24407c478bd9Sstevel@tonic-gate 	 */
24417c478bd9Sstevel@tonic-gate 	if (options.x11_use_localhost) {
24427c478bd9Sstevel@tonic-gate 		snprintf(display, sizeof display, "localhost:%u.%u",
24437c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
24447c478bd9Sstevel@tonic-gate 		snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
24457c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
24467c478bd9Sstevel@tonic-gate 		s->display = xstrdup(display);
24477c478bd9Sstevel@tonic-gate 		s->auth_display = xstrdup(auth_display);
24487c478bd9Sstevel@tonic-gate 	} else {
24497c478bd9Sstevel@tonic-gate #ifdef IPADDR_IN_DISPLAY
24507c478bd9Sstevel@tonic-gate 		struct hostent *he;
24517c478bd9Sstevel@tonic-gate 		struct in_addr my_addr;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 		he = gethostbyname(hostname);
24547c478bd9Sstevel@tonic-gate 		if (he == NULL) {
24557c478bd9Sstevel@tonic-gate 			error("Can't get IP address for X11 DISPLAY.");
24567c478bd9Sstevel@tonic-gate 			packet_send_debug("Can't get IP address for X11 DISPLAY.");
24577c478bd9Sstevel@tonic-gate 			return 0;
24587c478bd9Sstevel@tonic-gate 		}
24597c478bd9Sstevel@tonic-gate 		memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
24607c478bd9Sstevel@tonic-gate 		snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
24617c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
24627c478bd9Sstevel@tonic-gate #else
24637c478bd9Sstevel@tonic-gate 		snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
24647c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
24657c478bd9Sstevel@tonic-gate #endif
24667c478bd9Sstevel@tonic-gate 		s->display = xstrdup(display);
24677c478bd9Sstevel@tonic-gate 		s->auth_display = xstrdup(display);
24687c478bd9Sstevel@tonic-gate 	}
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	return 1;
24717c478bd9Sstevel@tonic-gate }
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate #ifdef USE_PAM
24747c478bd9Sstevel@tonic-gate int session_do_pam_conv(int, struct pam_message **,
24757c478bd9Sstevel@tonic-gate 			struct pam_response **, void *);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate static struct pam_conv session_pam_conv = {
24787c478bd9Sstevel@tonic-gate 	session_do_pam_conv,
24797c478bd9Sstevel@tonic-gate 	NULL
24807c478bd9Sstevel@tonic-gate };
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate static void
24837c478bd9Sstevel@tonic-gate session_do_pam(Session *s, int do_open)
24847c478bd9Sstevel@tonic-gate {
24857c478bd9Sstevel@tonic-gate 	int pam_retval;
24867c478bd9Sstevel@tonic-gate 	char *where, *old_tty, *old_tty_copy = NULL;
24877c478bd9Sstevel@tonic-gate 	struct pam_conv old_conv, *old_conv_ptr;
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	if (!s || !s->authctxt || !s->authctxt->pam || !s->authctxt->pam->h)
24907c478bd9Sstevel@tonic-gate 		return;
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	/* Save current PAM item values */
24937c478bd9Sstevel@tonic-gate 	where = "getting PAM_CONV";
24947c478bd9Sstevel@tonic-gate 	pam_retval = pam_get_item(s->authctxt->pam->h, PAM_CONV,
24957c478bd9Sstevel@tonic-gate 				  (void **) &old_conv_ptr);
24967c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
24977c478bd9Sstevel@tonic-gate 		goto done;
24987c478bd9Sstevel@tonic-gate 	old_conv = *old_conv_ptr;
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	where = "getting PAM_TTY";
25017c478bd9Sstevel@tonic-gate 	pam_retval = pam_get_item(s->authctxt->pam->h, PAM_TTY,
25027c478bd9Sstevel@tonic-gate 				  (void **) &old_tty);
25037c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
25047c478bd9Sstevel@tonic-gate 		goto done;
25057c478bd9Sstevel@tonic-gate 	old_tty_copy = xstrdup(old_tty);
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	/* Change PAM_TTY and PAM_CONV items */
25087c478bd9Sstevel@tonic-gate 	where = "setting PAM_TTY";
25097c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, s->tty);
25107c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
25117c478bd9Sstevel@tonic-gate 		goto done;
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 	where = "setting PAM_CONV";
25147c478bd9Sstevel@tonic-gate 	session_pam_conv.appdata_ptr = s;
25157c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h,
25167c478bd9Sstevel@tonic-gate 				  PAM_CONV, &session_pam_conv);
25177c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
25187c478bd9Sstevel@tonic-gate 		goto done;
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	/* Call pam_open/close_session() */
25217c478bd9Sstevel@tonic-gate 	if (do_open) {
25227c478bd9Sstevel@tonic-gate 		where = "calling pam_open_session()";
25237c478bd9Sstevel@tonic-gate 		pam_retval = pam_open_session(s->authctxt->pam->h, 0);
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 	else {
25267c478bd9Sstevel@tonic-gate 		where = "calling pam_close_session()";
25277c478bd9Sstevel@tonic-gate 		pam_retval = pam_close_session(s->authctxt->pam->h, 0);
25287c478bd9Sstevel@tonic-gate 	}
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	/* Reset PAM_TTY and PAM_CONV items to previous values */
25317c478bd9Sstevel@tonic-gate 	where = "setting PAM_TTY";
25327c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, old_tty_copy);
25337c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
25347c478bd9Sstevel@tonic-gate 		goto done;
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	where = "setting PAM_CONV";
25377c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h, PAM_CONV, &old_conv);
25387c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
25397c478bd9Sstevel@tonic-gate 		goto done;
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	session_pam_conv.appdata_ptr = NULL;
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate done:
25447c478bd9Sstevel@tonic-gate 	if (old_tty_copy)
25457c478bd9Sstevel@tonic-gate 		xfree(old_tty_copy);
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	if (pam_retval == PAM_SUCCESS)
25487c478bd9Sstevel@tonic-gate 		return;
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	/* fatal()? probably not... */
25517c478bd9Sstevel@tonic-gate 	log("PAM failed[%d] while %s: %s", pam_retval, where,
25527c478bd9Sstevel@tonic-gate 	    PAM_STRERROR(s->authctxt->pam->h, pam_retval));
25537c478bd9Sstevel@tonic-gate }
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate int
25567c478bd9Sstevel@tonic-gate session_do_pam_conv(int num_prompts,
25577c478bd9Sstevel@tonic-gate 		    struct pam_message **prompts,
25587c478bd9Sstevel@tonic-gate 		    struct pam_response **resp,
25597c478bd9Sstevel@tonic-gate 		    void *app_data)
25607c478bd9Sstevel@tonic-gate {
25617c478bd9Sstevel@tonic-gate 	Session *s = (Session *) app_data;
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	struct pam_response *reply;
25647c478bd9Sstevel@tonic-gate 	int count;
25657c478bd9Sstevel@tonic-gate 	char *prompt;
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	if (channel_lookup(s->chanid) == NULL)
25687c478bd9Sstevel@tonic-gate 		return PAM_CONV_ERR;
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	/* PAM will free this later */
25717c478bd9Sstevel@tonic-gate 	reply = xmalloc(num_prompts * sizeof(*reply));
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	(void) memset(reply, 0, num_prompts * sizeof(*reply));
25747c478bd9Sstevel@tonic-gate 	for (count = 0; count < num_prompts; count++) {
25757c478bd9Sstevel@tonic-gate 		switch(PAM_MSG_MEMBER(prompts, count, msg_style)) {
25767c478bd9Sstevel@tonic-gate 		case PAM_TEXT_INFO:
25777c478bd9Sstevel@tonic-gate 			/* Write to stdout of channel */
25787c478bd9Sstevel@tonic-gate 			prompt = PAM_MSG_MEMBER(prompts, count, msg);
25797c478bd9Sstevel@tonic-gate 			if (prompt != NULL && s->ttyfd != -1) {
25807c478bd9Sstevel@tonic-gate 				debug2("session_do_pam_conv: text info "
25817c478bd9Sstevel@tonic-gate 				       "prompt: %s", prompt);
25827c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, prompt, strlen(prompt));
25837c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, "\n", 1);
25847c478bd9Sstevel@tonic-gate 			}
25857c478bd9Sstevel@tonic-gate 			reply[count].resp = xstrdup("");
25867c478bd9Sstevel@tonic-gate 			reply[count].resp_retcode = PAM_SUCCESS;
25877c478bd9Sstevel@tonic-gate 			break;
25887c478bd9Sstevel@tonic-gate 		case PAM_ERROR_MSG:
25897c478bd9Sstevel@tonic-gate 			/* Write to stderr of channel */
25907c478bd9Sstevel@tonic-gate 			prompt = PAM_MSG_MEMBER(prompts, count, msg);
25917c478bd9Sstevel@tonic-gate 			if (prompt != NULL && s->ttyfd != -1) {
25927c478bd9Sstevel@tonic-gate 				debug2("session_do_pam_conv: error "
25937c478bd9Sstevel@tonic-gate 				       "prompt: %s", prompt);
25947c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, prompt, strlen(prompt));
25957c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, "\n", 1);
25967c478bd9Sstevel@tonic-gate 			}
25977c478bd9Sstevel@tonic-gate 			reply[count].resp = xstrdup("");
25987c478bd9Sstevel@tonic-gate 			reply[count].resp_retcode = PAM_SUCCESS;
25997c478bd9Sstevel@tonic-gate 			break;
26007c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
26017c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
26027c478bd9Sstevel@tonic-gate 		    /*
26037c478bd9Sstevel@tonic-gate 		     * XXX Someday add support for echo on/off prompts
26047c478bd9Sstevel@tonic-gate 		     *     here on sessions with ttys.
26057c478bd9Sstevel@tonic-gate 		     */
26067c478bd9Sstevel@tonic-gate 		default:
26077c478bd9Sstevel@tonic-gate 			xfree(reply);
26087c478bd9Sstevel@tonic-gate 			return PAM_CONV_ERR;
26097c478bd9Sstevel@tonic-gate 		}
26107c478bd9Sstevel@tonic-gate 	}
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	*resp = reply;
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	return PAM_SUCCESS;
26157c478bd9Sstevel@tonic-gate }
26167c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate static void
26197c478bd9Sstevel@tonic-gate do_authenticated2(Authctxt *authctxt)
26207c478bd9Sstevel@tonic-gate {
26217c478bd9Sstevel@tonic-gate 	server_loop2(authctxt);
26227c478bd9Sstevel@tonic-gate }
26236f8d59d8SJan Pechanec 
26246f8d59d8SJan Pechanec /*
26256f8d59d8SJan Pechanec  * Drop the privileges. We need this for the in-process SFTP server only. For
26266f8d59d8SJan Pechanec  * the shell and the external subsystem the exec(2) call will do the P = E = I
26276f8d59d8SJan Pechanec  * assignment itself. Never change the privileges if the connecting user is
26286f8d59d8SJan Pechanec  * root. See privileges(5) if the terminology used here is not known to you.
26296f8d59d8SJan Pechanec  */
26306f8d59d8SJan Pechanec static void
26316f8d59d8SJan Pechanec drop_privs(uid_t uid)
26326f8d59d8SJan Pechanec {
26336f8d59d8SJan Pechanec 	priv_set_t *priv_inherit;
26346f8d59d8SJan Pechanec 
26356f8d59d8SJan Pechanec 	/* If root is connecting we are done. */
26366f8d59d8SJan Pechanec 	if (uid == 0)
26376f8d59d8SJan Pechanec 		return;
26386f8d59d8SJan Pechanec 
26396f8d59d8SJan Pechanec 	if ((priv_inherit = priv_allocset()) == NULL)
26406f8d59d8SJan Pechanec 		fatal("priv_allocset: %s", strerror(errno));
26416f8d59d8SJan Pechanec 	if (getppriv(PRIV_INHERITABLE, priv_inherit) != 0)
26426f8d59d8SJan Pechanec 		fatal("getppriv: %s", strerror(errno));
26436f8d59d8SJan Pechanec 
26446f8d59d8SJan Pechanec 	/*
26456f8d59d8SJan Pechanec 	 * This will limit E as well. Note that before this P was a
26466f8d59d8SJan Pechanec 	 * superset of I, see permanently_set_uid().
26476f8d59d8SJan Pechanec 	 */
26486f8d59d8SJan Pechanec 	if (setppriv(PRIV_SET, PRIV_PERMITTED, priv_inherit) == -1)
26496f8d59d8SJan Pechanec 		fatal("setppriv: %s", strerror(errno));
26506f8d59d8SJan Pechanec 
26516f8d59d8SJan Pechanec 	priv_freeset(priv_inherit);
26526f8d59d8SJan Pechanec 
26536f8d59d8SJan Pechanec 	/*
26546f8d59d8SJan Pechanec 	 * By manipulating the P set above we entered a PA mode which we
26556f8d59d8SJan Pechanec 	 * do not need to retain in.
26566f8d59d8SJan Pechanec 	 */
26576f8d59d8SJan Pechanec 	if (setpflags(PRIV_AWARE, 0) == -1)
26586f8d59d8SJan Pechanec 		fatal("setpflags: %s", strerror(errno));
26596f8d59d8SJan Pechanec }
2660