xref: /titanic_50/usr/src/cmd/ssh/sshd/session.c (revision 26ba198477055398633f319757f934b7ce73784e)
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 /*
35*26ba1984Sjp161948  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
457c478bd9Sstevel@tonic-gate #include <deflt.h>
467c478bd9Sstevel@tonic-gate #include <ulimit.h>
477c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include "ssh.h"
507c478bd9Sstevel@tonic-gate #include "ssh1.h"
517c478bd9Sstevel@tonic-gate #include "ssh2.h"
527c478bd9Sstevel@tonic-gate #include "xmalloc.h"
537c478bd9Sstevel@tonic-gate #include "sshpty.h"
547c478bd9Sstevel@tonic-gate #include "packet.h"
557c478bd9Sstevel@tonic-gate #include "buffer.h"
567c478bd9Sstevel@tonic-gate #include "mpaux.h"
577c478bd9Sstevel@tonic-gate #include "uidswap.h"
587c478bd9Sstevel@tonic-gate #include "compat.h"
597c478bd9Sstevel@tonic-gate #include "channels.h"
607c478bd9Sstevel@tonic-gate #include "bufaux.h"
617c478bd9Sstevel@tonic-gate #include "auth.h"
627c478bd9Sstevel@tonic-gate #include "auth-options.h"
637c478bd9Sstevel@tonic-gate #include "pathnames.h"
647c478bd9Sstevel@tonic-gate #include "log.h"
657c478bd9Sstevel@tonic-gate #include "servconf.h"
667c478bd9Sstevel@tonic-gate #include "sshlogin.h"
677c478bd9Sstevel@tonic-gate #include "serverloop.h"
687c478bd9Sstevel@tonic-gate #include "canohost.h"
697c478bd9Sstevel@tonic-gate #include "session.h"
707c478bd9Sstevel@tonic-gate #include "monitor_wrap.h"
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #ifdef USE_PAM
737c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
747c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef GSSAPI
777c478bd9Sstevel@tonic-gate #include "ssh-gss.h"
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
817c478bd9Sstevel@tonic-gate #include "altprivsep.h"
827c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
857c478bd9Sstevel@tonic-gate #include <windows.h>
867c478bd9Sstevel@tonic-gate #include <sys/cygwin.h>
877c478bd9Sstevel@tonic-gate #define is_winnt       (GetVersion() < 0x80000000)
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* func */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate Session *session_new(void);
937c478bd9Sstevel@tonic-gate void	session_set_fds(Session *, int, int, int);
947c478bd9Sstevel@tonic-gate void	session_pty_cleanup(void *);
957c478bd9Sstevel@tonic-gate void	session_proctitle(Session *);
967c478bd9Sstevel@tonic-gate int	session_setup_x11fwd(Session *);
977c478bd9Sstevel@tonic-gate void	do_exec_pty(Session *, const char *);
987c478bd9Sstevel@tonic-gate void	do_exec_no_pty(Session *, const char *);
997c478bd9Sstevel@tonic-gate void	do_exec(Session *, const char *);
1007c478bd9Sstevel@tonic-gate void	do_login(Session *, const char *);
1017c478bd9Sstevel@tonic-gate #ifdef LOGIN_NEEDS_UTMPX
1027c478bd9Sstevel@tonic-gate static void	do_pre_login(Session *s);
1037c478bd9Sstevel@tonic-gate #endif
1047c478bd9Sstevel@tonic-gate void	do_child(Session *, const char *);
1057c478bd9Sstevel@tonic-gate void	do_motd(void);
1067c478bd9Sstevel@tonic-gate int	check_quietlogin(Session *, const char *);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static void do_authenticated1(Authctxt *);
1097c478bd9Sstevel@tonic-gate static void do_authenticated2(Authctxt *);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static int  session_pty_req(Session *);
1127c478bd9Sstevel@tonic-gate static int  session_env_req(Session *s);
1137c478bd9Sstevel@tonic-gate static void session_free_env(char ***envp);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #ifdef USE_PAM
1167c478bd9Sstevel@tonic-gate static void session_do_pam(Session *, int);
1177c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /* import */
1207c478bd9Sstevel@tonic-gate extern ServerOptions options;
1217c478bd9Sstevel@tonic-gate extern char *__progname;
1227c478bd9Sstevel@tonic-gate extern int log_stderr;
1237c478bd9Sstevel@tonic-gate extern int debug_flag;
1247c478bd9Sstevel@tonic-gate extern u_int utmp_len;
1257c478bd9Sstevel@tonic-gate extern void destroy_sensitive_data(void);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #ifdef GSSAPI
1287c478bd9Sstevel@tonic-gate extern Gssctxt *xxx_gssctxt;
1297c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* original command from peer. */
1327c478bd9Sstevel@tonic-gate const char *original_command = NULL;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* data */
1357c478bd9Sstevel@tonic-gate #define MAX_SESSIONS 10
1367c478bd9Sstevel@tonic-gate Session	sessions[MAX_SESSIONS];
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
1397c478bd9Sstevel@tonic-gate char *aixloginmsg;
1407c478bd9Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
1437c478bd9Sstevel@tonic-gate login_cap_t *lc;
1447c478bd9Sstevel@tonic-gate #endif
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* Name and directory of socket for authentication agent forwarding. */
1477c478bd9Sstevel@tonic-gate static char *auth_sock_name = NULL;
1487c478bd9Sstevel@tonic-gate static char *auth_sock_dir = NULL;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* removes the agent forwarding socket */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static void
1537c478bd9Sstevel@tonic-gate auth_sock_cleanup_proc(void *_pw)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	struct passwd *pw = _pw;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL) {
1587c478bd9Sstevel@tonic-gate 		temporarily_use_uid(pw);
1597c478bd9Sstevel@tonic-gate 		unlink(auth_sock_name);
1607c478bd9Sstevel@tonic-gate 		rmdir(auth_sock_dir);
1617c478bd9Sstevel@tonic-gate 		auth_sock_name = NULL;
1627c478bd9Sstevel@tonic-gate 		restore_uid();
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static int
1677c478bd9Sstevel@tonic-gate auth_input_request_forwarding(struct passwd * pw)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	Channel *nc;
1707c478bd9Sstevel@tonic-gate 	int sock;
1717c478bd9Sstevel@tonic-gate 	struct sockaddr_un sunaddr;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL) {
1747c478bd9Sstevel@tonic-gate 		error("authentication forwarding requested twice.");
1757c478bd9Sstevel@tonic-gate 		return 0;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* Temporarily drop privileged uid for mkdir/bind. */
1797c478bd9Sstevel@tonic-gate 	temporarily_use_uid(pw);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* Allocate a buffer for the socket name, and format the name. */
1827c478bd9Sstevel@tonic-gate 	auth_sock_name = xmalloc(MAXPATHLEN);
1837c478bd9Sstevel@tonic-gate 	auth_sock_dir = xmalloc(MAXPATHLEN);
1847c478bd9Sstevel@tonic-gate 	strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/* Create private directory for socket */
1877c478bd9Sstevel@tonic-gate 	if (mkdtemp(auth_sock_dir) == NULL) {
1887c478bd9Sstevel@tonic-gate 		packet_send_debug("Agent forwarding disabled: "
1897c478bd9Sstevel@tonic-gate 		    "mkdtemp() failed: %.100s", strerror(errno));
1907c478bd9Sstevel@tonic-gate 		restore_uid();
1917c478bd9Sstevel@tonic-gate 		xfree(auth_sock_name);
1927c478bd9Sstevel@tonic-gate 		xfree(auth_sock_dir);
1937c478bd9Sstevel@tonic-gate 		auth_sock_name = NULL;
1947c478bd9Sstevel@tonic-gate 		auth_sock_dir = NULL;
1957c478bd9Sstevel@tonic-gate 		return 0;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
1987c478bd9Sstevel@tonic-gate 		 auth_sock_dir, (long) getpid());
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* delete agent socket on fatal() */
2017c478bd9Sstevel@tonic-gate 	fatal_add_cleanup(auth_sock_cleanup_proc, pw);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* Create the socket. */
2047c478bd9Sstevel@tonic-gate 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
2057c478bd9Sstevel@tonic-gate 	if (sock < 0)
2067c478bd9Sstevel@tonic-gate 		packet_disconnect("socket: %.100s", strerror(errno));
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* Bind it to the name. */
2097c478bd9Sstevel@tonic-gate 	memset(&sunaddr, 0, sizeof(sunaddr));
2107c478bd9Sstevel@tonic-gate 	sunaddr.sun_family = AF_UNIX;
2117c478bd9Sstevel@tonic-gate 	strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2147c478bd9Sstevel@tonic-gate 		packet_disconnect("bind: %.100s", strerror(errno));
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* Restore the privileged uid. */
2177c478bd9Sstevel@tonic-gate 	restore_uid();
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* Start listening on the socket. */
2207c478bd9Sstevel@tonic-gate 	if (listen(sock, 5) < 0)
2217c478bd9Sstevel@tonic-gate 		packet_disconnect("listen: %.100s", strerror(errno));
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* Allocate a channel for the authentication agent socket. */
2247c478bd9Sstevel@tonic-gate 	nc = channel_new("auth socket",
2257c478bd9Sstevel@tonic-gate 	    SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2267c478bd9Sstevel@tonic-gate 	    CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2277c478bd9Sstevel@tonic-gate 	    0, xstrdup("auth socket"), 1);
2287c478bd9Sstevel@tonic-gate 	strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
2297c478bd9Sstevel@tonic-gate 	return 1;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate void
2347c478bd9Sstevel@tonic-gate do_authenticated(Authctxt *authctxt)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	/* setup the channel layer */
2377c478bd9Sstevel@tonic-gate 	if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
2387c478bd9Sstevel@tonic-gate 		channel_permit_all_opens();
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (compat20)
2417c478bd9Sstevel@tonic-gate 		do_authenticated2(authctxt);
2427c478bd9Sstevel@tonic-gate 	else
2437c478bd9Sstevel@tonic-gate 		do_authenticated1(authctxt);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* remove agent socket */
2467c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL)
2477c478bd9Sstevel@tonic-gate 		auth_sock_cleanup_proc(authctxt->pw);
2487c478bd9Sstevel@tonic-gate #ifdef KRB4
2497c478bd9Sstevel@tonic-gate 	if (options.kerberos_ticket_cleanup)
2507c478bd9Sstevel@tonic-gate 		krb4_cleanup_proc(authctxt);
2517c478bd9Sstevel@tonic-gate #endif
2527c478bd9Sstevel@tonic-gate #ifdef KRB5
2537c478bd9Sstevel@tonic-gate 	if (options.kerberos_ticket_cleanup)
2547c478bd9Sstevel@tonic-gate 		krb5_cleanup_proc(authctxt);
2557c478bd9Sstevel@tonic-gate #endif
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Prepares for an interactive session.  This is called after the user has
2607c478bd9Sstevel@tonic-gate  * been successfully authenticated.  During this message exchange, pseudo
2617c478bd9Sstevel@tonic-gate  * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
2627c478bd9Sstevel@tonic-gate  * are requested, etc.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate static void
2657c478bd9Sstevel@tonic-gate do_authenticated1(Authctxt *authctxt)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	Session *s;
2687c478bd9Sstevel@tonic-gate 	char *command;
2697c478bd9Sstevel@tonic-gate 	int success, type, screen_flag;
2707c478bd9Sstevel@tonic-gate 	int enable_compression_after_reply = 0;
2717c478bd9Sstevel@tonic-gate 	u_int proto_len, data_len, dlen, compression_level = 0;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	s = session_new();
2747c478bd9Sstevel@tonic-gate 	s->authctxt = authctxt;
2757c478bd9Sstevel@tonic-gate 	s->pw = authctxt->pw;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * We stay in this loop until the client requests to execute a shell
2797c478bd9Sstevel@tonic-gate 	 * or a command.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	for (;;) {
2827c478bd9Sstevel@tonic-gate 		success = 0;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		/* Get a packet from the client. */
2857c478bd9Sstevel@tonic-gate 		type = packet_read();
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		/* Process the packet. */
2887c478bd9Sstevel@tonic-gate 		switch (type) {
2897c478bd9Sstevel@tonic-gate 		case SSH_CMSG_REQUEST_COMPRESSION:
2907c478bd9Sstevel@tonic-gate 			compression_level = packet_get_int();
2917c478bd9Sstevel@tonic-gate 			packet_check_eom();
2927c478bd9Sstevel@tonic-gate 			if (compression_level < 1 || compression_level > 9) {
2937c478bd9Sstevel@tonic-gate 				packet_send_debug("Received illegal compression level %d.",
2947c478bd9Sstevel@tonic-gate 				    compression_level);
2957c478bd9Sstevel@tonic-gate 				break;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			if (!options.compression) {
2987c478bd9Sstevel@tonic-gate 				debug2("compression disabled");
2997c478bd9Sstevel@tonic-gate 				break;
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			/* Enable compression after we have responded with SUCCESS. */
3027c478bd9Sstevel@tonic-gate 			enable_compression_after_reply = 1;
3037c478bd9Sstevel@tonic-gate 			success = 1;
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		case SSH_CMSG_REQUEST_PTY:
3077c478bd9Sstevel@tonic-gate 			success = session_pty_req(s);
3087c478bd9Sstevel@tonic-gate 			break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		case SSH_CMSG_X11_REQUEST_FORWARDING:
3117c478bd9Sstevel@tonic-gate 			s->auth_proto = packet_get_string(&proto_len);
3127c478bd9Sstevel@tonic-gate 			s->auth_data = packet_get_string(&data_len);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			screen_flag = packet_get_protocol_flags() &
3157c478bd9Sstevel@tonic-gate 			    SSH_PROTOFLAG_SCREEN_NUMBER;
3167c478bd9Sstevel@tonic-gate 			debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			if (packet_remaining() == 4) {
3197c478bd9Sstevel@tonic-gate 				if (!screen_flag)
3207c478bd9Sstevel@tonic-gate 					debug2("Buggy client: "
3217c478bd9Sstevel@tonic-gate 					    "X11 screen flag missing");
3227c478bd9Sstevel@tonic-gate 				s->screen = packet_get_int();
3237c478bd9Sstevel@tonic-gate 			} else {
3247c478bd9Sstevel@tonic-gate 				s->screen = 0;
3257c478bd9Sstevel@tonic-gate 			}
3267c478bd9Sstevel@tonic-gate 			packet_check_eom();
3277c478bd9Sstevel@tonic-gate 			success = session_setup_x11fwd(s);
3287c478bd9Sstevel@tonic-gate 			if (!success) {
3297c478bd9Sstevel@tonic-gate 				xfree(s->auth_proto);
3307c478bd9Sstevel@tonic-gate 				xfree(s->auth_data);
3317c478bd9Sstevel@tonic-gate 				s->auth_proto = NULL;
3327c478bd9Sstevel@tonic-gate 				s->auth_data = NULL;
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 			break;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AGENT_REQUEST_FORWARDING:
3377c478bd9Sstevel@tonic-gate 			if (no_agent_forwarding_flag || compat13) {
3387c478bd9Sstevel@tonic-gate 				debug("Authentication agent forwarding not permitted for this authentication.");
3397c478bd9Sstevel@tonic-gate 				break;
3407c478bd9Sstevel@tonic-gate 			}
3417c478bd9Sstevel@tonic-gate 			debug("Received authentication agent forwarding request.");
3427c478bd9Sstevel@tonic-gate 			success = auth_input_request_forwarding(s->pw);
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		case SSH_CMSG_PORT_FORWARD_REQUEST:
3467c478bd9Sstevel@tonic-gate 			if (no_port_forwarding_flag) {
3477c478bd9Sstevel@tonic-gate 				debug("Port forwarding not permitted for this authentication.");
3487c478bd9Sstevel@tonic-gate 				break;
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 			if (!options.allow_tcp_forwarding) {
3517c478bd9Sstevel@tonic-gate 				debug("Port forwarding not permitted.");
3527c478bd9Sstevel@tonic-gate 				break;
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 			debug("Received TCP/IP port forwarding request.");
3557c478bd9Sstevel@tonic-gate 			channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
3567c478bd9Sstevel@tonic-gate 			success = 1;
3577c478bd9Sstevel@tonic-gate 			break;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		case SSH_CMSG_MAX_PACKET_SIZE:
3607c478bd9Sstevel@tonic-gate 			if (packet_set_maxsize(packet_get_int()) > 0)
3617c478bd9Sstevel@tonic-gate 				success = 1;
3627c478bd9Sstevel@tonic-gate 			break;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
3657c478bd9Sstevel@tonic-gate 		case SSH_CMSG_HAVE_KERBEROS_TGT:
3667c478bd9Sstevel@tonic-gate 			if (!options.kerberos_tgt_passing) {
3677c478bd9Sstevel@tonic-gate 				verbose("Kerberos TGT passing disabled.");
3687c478bd9Sstevel@tonic-gate 			} else {
3697c478bd9Sstevel@tonic-gate 				char *kdata = packet_get_string(&dlen);
3707c478bd9Sstevel@tonic-gate 				packet_check_eom();
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 				/* XXX - 0x41, see creds_to_radix version */
3737c478bd9Sstevel@tonic-gate 				if (kdata[0] != 0x41) {
3747c478bd9Sstevel@tonic-gate #ifdef KRB5
3757c478bd9Sstevel@tonic-gate 					krb5_data tgt;
3767c478bd9Sstevel@tonic-gate 					tgt.data = kdata;
3777c478bd9Sstevel@tonic-gate 					tgt.length = dlen;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 					if (auth_krb5_tgt(s->authctxt, &tgt))
3807c478bd9Sstevel@tonic-gate 						success = 1;
3817c478bd9Sstevel@tonic-gate 					else
3827c478bd9Sstevel@tonic-gate 						verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
3837c478bd9Sstevel@tonic-gate #endif /* KRB5 */
3847c478bd9Sstevel@tonic-gate 				} else {
3857c478bd9Sstevel@tonic-gate #ifdef AFS
3867c478bd9Sstevel@tonic-gate 					if (auth_krb4_tgt(s->authctxt, kdata))
3877c478bd9Sstevel@tonic-gate 						success = 1;
3887c478bd9Sstevel@tonic-gate 					else
3897c478bd9Sstevel@tonic-gate 						verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
3907c478bd9Sstevel@tonic-gate #endif /* AFS */
3917c478bd9Sstevel@tonic-gate 				}
3927c478bd9Sstevel@tonic-gate 				xfree(kdata);
3937c478bd9Sstevel@tonic-gate 			}
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate #endif /* AFS || KRB5 */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate #ifdef AFS
3987c478bd9Sstevel@tonic-gate 		case SSH_CMSG_HAVE_AFS_TOKEN:
3997c478bd9Sstevel@tonic-gate 			if (!options.afs_token_passing || !k_hasafs()) {
4007c478bd9Sstevel@tonic-gate 				verbose("AFS token passing disabled.");
4017c478bd9Sstevel@tonic-gate 			} else {
4027c478bd9Sstevel@tonic-gate 				/* Accept AFS token. */
4037c478bd9Sstevel@tonic-gate 				char *token = packet_get_string(&dlen);
4047c478bd9Sstevel@tonic-gate 				packet_check_eom();
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 				if (auth_afs_token(s->authctxt, token))
4077c478bd9Sstevel@tonic-gate 					success = 1;
4087c478bd9Sstevel@tonic-gate 				else
4097c478bd9Sstevel@tonic-gate 					verbose("AFS token refused for %.100s",
4107c478bd9Sstevel@tonic-gate 					    s->authctxt->user);
4117c478bd9Sstevel@tonic-gate 				xfree(token);
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 			break;
4147c478bd9Sstevel@tonic-gate #endif /* AFS */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		case SSH_CMSG_EXEC_SHELL:
4177c478bd9Sstevel@tonic-gate 		case SSH_CMSG_EXEC_CMD:
4187c478bd9Sstevel@tonic-gate 			if (type == SSH_CMSG_EXEC_CMD) {
4197c478bd9Sstevel@tonic-gate 				command = packet_get_string(&dlen);
4207c478bd9Sstevel@tonic-gate 				debug("Exec command '%.500s'", command);
4217c478bd9Sstevel@tonic-gate 				do_exec(s, command);
4227c478bd9Sstevel@tonic-gate 				xfree(command);
4237c478bd9Sstevel@tonic-gate 			} else {
4247c478bd9Sstevel@tonic-gate 				do_exec(s, NULL);
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 			packet_check_eom();
4277c478bd9Sstevel@tonic-gate 			session_close(s);
4287c478bd9Sstevel@tonic-gate 			return;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		default:
4317c478bd9Sstevel@tonic-gate 			/*
4327c478bd9Sstevel@tonic-gate 			 * Any unknown messages in this phase are ignored,
4337c478bd9Sstevel@tonic-gate 			 * and a failure message is returned.
4347c478bd9Sstevel@tonic-gate 			 */
4357c478bd9Sstevel@tonic-gate 			log("Unknown packet type received after authentication: %d", type);
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 		packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
4387c478bd9Sstevel@tonic-gate 		packet_send();
4397c478bd9Sstevel@tonic-gate 		packet_write_wait();
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		/* Enable compression now that we have replied if appropriate. */
4427c478bd9Sstevel@tonic-gate 		if (enable_compression_after_reply) {
4437c478bd9Sstevel@tonic-gate 			enable_compression_after_reply = 0;
4447c478bd9Sstevel@tonic-gate 			packet_start_compression(compression_level);
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * This is called to fork and execute a command when we have no tty.  This
4517c478bd9Sstevel@tonic-gate  * will call do_child from the child, and server_loop from the parent after
4527c478bd9Sstevel@tonic-gate  * setting up file descriptors and such.
4537c478bd9Sstevel@tonic-gate  */
4547c478bd9Sstevel@tonic-gate void
4557c478bd9Sstevel@tonic-gate do_exec_no_pty(Session *s, const char *command)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	pid_t pid;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate #ifdef USE_PIPES
4607c478bd9Sstevel@tonic-gate 	int pin[2], pout[2], perr[2];
4617c478bd9Sstevel@tonic-gate 	/* Allocate pipes for communicating with the program. */
4627c478bd9Sstevel@tonic-gate 	if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
4637c478bd9Sstevel@tonic-gate 		packet_disconnect("Could not create pipes: %.100s",
4647c478bd9Sstevel@tonic-gate 				  strerror(errno));
4657c478bd9Sstevel@tonic-gate #else /* USE_PIPES */
4667c478bd9Sstevel@tonic-gate 	int inout[2], err[2];
4677c478bd9Sstevel@tonic-gate 	/* Uses socket pairs to communicate with the program. */
4687c478bd9Sstevel@tonic-gate 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
4697c478bd9Sstevel@tonic-gate 	    socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
4707c478bd9Sstevel@tonic-gate 		packet_disconnect("Could not create socket pairs: %.100s",
4717c478bd9Sstevel@tonic-gate 				  strerror(errno));
4727c478bd9Sstevel@tonic-gate #endif /* USE_PIPES */
4737c478bd9Sstevel@tonic-gate 	if (s == NULL)
4747c478bd9Sstevel@tonic-gate 		fatal("do_exec_no_pty: no session");
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	session_proctitle(s);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/* Fork the child. */
4797c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
4807c478bd9Sstevel@tonic-gate 		fatal_remove_all_cleanups();
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		/* Child.  Reinitialize the log since the pid has changed. */
4837c478bd9Sstevel@tonic-gate 		log_init(__progname, options.log_level, options.log_facility, log_stderr);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		/*
4867c478bd9Sstevel@tonic-gate 		 * Create a new session and process group since the 4.4BSD
4877c478bd9Sstevel@tonic-gate 		 * setlogin() affects the entire process group.
4887c478bd9Sstevel@tonic-gate 		 */
4897c478bd9Sstevel@tonic-gate 		if (setsid() < 0)
4907c478bd9Sstevel@tonic-gate 			error("setsid failed: %.100s", strerror(errno));
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate #ifdef USE_PIPES
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * Redirect stdin.  We close the parent side of the socket
4957c478bd9Sstevel@tonic-gate 		 * pair, and make the child side the standard input.
4967c478bd9Sstevel@tonic-gate 		 */
4977c478bd9Sstevel@tonic-gate 		close(pin[1]);
4987c478bd9Sstevel@tonic-gate 		if (dup2(pin[0], 0) < 0)
4997c478bd9Sstevel@tonic-gate 			perror("dup2 stdin");
5007c478bd9Sstevel@tonic-gate 		close(pin[0]);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		/* Redirect stdout. */
5037c478bd9Sstevel@tonic-gate 		close(pout[0]);
5047c478bd9Sstevel@tonic-gate 		if (dup2(pout[1], 1) < 0)
5057c478bd9Sstevel@tonic-gate 			perror("dup2 stdout");
5067c478bd9Sstevel@tonic-gate 		close(pout[1]);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/* Redirect stderr. */
5097c478bd9Sstevel@tonic-gate 		close(perr[0]);
5107c478bd9Sstevel@tonic-gate 		if (dup2(perr[1], 2) < 0)
5117c478bd9Sstevel@tonic-gate 			perror("dup2 stderr");
5127c478bd9Sstevel@tonic-gate 		close(perr[1]);
5137c478bd9Sstevel@tonic-gate #else /* USE_PIPES */
5147c478bd9Sstevel@tonic-gate 		/*
5157c478bd9Sstevel@tonic-gate 		 * Redirect stdin, stdout, and stderr.  Stdin and stdout will
5167c478bd9Sstevel@tonic-gate 		 * use the same socket, as some programs (particularly rdist)
5177c478bd9Sstevel@tonic-gate 		 * seem to depend on it.
5187c478bd9Sstevel@tonic-gate 		 */
5197c478bd9Sstevel@tonic-gate 		close(inout[1]);
5207c478bd9Sstevel@tonic-gate 		close(err[1]);
5217c478bd9Sstevel@tonic-gate 		if (dup2(inout[0], 0) < 0)	/* stdin */
5227c478bd9Sstevel@tonic-gate 			perror("dup2 stdin");
5237c478bd9Sstevel@tonic-gate 		if (dup2(inout[0], 1) < 0)	/* stdout.  Note: same socket as stdin. */
5247c478bd9Sstevel@tonic-gate 			perror("dup2 stdout");
5257c478bd9Sstevel@tonic-gate 		if (dup2(err[0], 2) < 0)	/* stderr */
5267c478bd9Sstevel@tonic-gate 			perror("dup2 stderr");
5277c478bd9Sstevel@tonic-gate #endif /* USE_PIPES */
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate #ifdef _UNICOS
5307c478bd9Sstevel@tonic-gate 		cray_init_job(s->pw); /* set up cray jid and tmpdir */
5317c478bd9Sstevel@tonic-gate #endif
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		/* Do processing for the child (exec command etc). */
5347c478bd9Sstevel@tonic-gate 		do_child(s, command);
5357c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate #ifdef _UNICOS
5387c478bd9Sstevel@tonic-gate 	signal(WJSIGNAL, cray_job_termination_handler);
5397c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
5407c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
5417c478bd9Sstevel@tonic-gate 	if (is_winnt)
5427c478bd9Sstevel@tonic-gate 		cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate 	if (pid < 0)
5457c478bd9Sstevel@tonic-gate 		packet_disconnect("fork failed: %.100s", strerror(errno));
5467c478bd9Sstevel@tonic-gate 	s->pid = pid;
5477c478bd9Sstevel@tonic-gate 	/* Set interactive/non-interactive mode. */
5487c478bd9Sstevel@tonic-gate 	packet_set_interactive(s->display != NULL);
5497c478bd9Sstevel@tonic-gate #ifdef USE_PIPES
5507c478bd9Sstevel@tonic-gate 	/* We are the parent.  Close the child sides of the pipes. */
5517c478bd9Sstevel@tonic-gate 	close(pin[0]);
5527c478bd9Sstevel@tonic-gate 	close(pout[1]);
5537c478bd9Sstevel@tonic-gate 	close(perr[1]);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (compat20) {
5567c478bd9Sstevel@tonic-gate 		session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
5577c478bd9Sstevel@tonic-gate 		/* Don't close channel before sending exit-status! */
5587c478bd9Sstevel@tonic-gate 		channel_set_wait_for_exit(s->chanid, 1);
5597c478bd9Sstevel@tonic-gate 	} else {
5607c478bd9Sstevel@tonic-gate 		/* Enter the interactive session. */
5617c478bd9Sstevel@tonic-gate 		server_loop(pid, pin[1], pout[0], perr[0]);
5627c478bd9Sstevel@tonic-gate 		/* server_loop has closed pin[1], pout[0], and perr[0]. */
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate #else /* USE_PIPES */
5657c478bd9Sstevel@tonic-gate 	/* We are the parent.  Close the child sides of the socket pairs. */
5667c478bd9Sstevel@tonic-gate 	close(inout[0]);
5677c478bd9Sstevel@tonic-gate 	close(err[0]);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * Enter the interactive session.  Note: server_loop must be able to
5717c478bd9Sstevel@tonic-gate 	 * handle the case that fdin and fdout are the same.
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	if (compat20) {
5747c478bd9Sstevel@tonic-gate 		session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
5757c478bd9Sstevel@tonic-gate 		/* Don't close channel before sending exit-status! */
5767c478bd9Sstevel@tonic-gate 		channel_set_wait_for_exit(s->chanid, 1);
5777c478bd9Sstevel@tonic-gate 	} else {
5787c478bd9Sstevel@tonic-gate 		server_loop(pid, inout[1], inout[1], err[1]);
5797c478bd9Sstevel@tonic-gate 		/* server_loop has closed inout[1] and err[1]. */
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate #endif /* USE_PIPES */
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * This is called to fork and execute a command when we have a tty.  This
5867c478bd9Sstevel@tonic-gate  * will call do_child from the child, and server_loop from the parent after
5877c478bd9Sstevel@tonic-gate  * setting up file descriptors, controlling tty, updating wtmp, utmp,
5887c478bd9Sstevel@tonic-gate  * lastlog, and other such operations.
5897c478bd9Sstevel@tonic-gate  */
5907c478bd9Sstevel@tonic-gate void
5917c478bd9Sstevel@tonic-gate do_exec_pty(Session *s, const char *command)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	int fdout, ptyfd, ttyfd, ptymaster, pipe_fds[2];
5947c478bd9Sstevel@tonic-gate 	pid_t pid;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (s == NULL)
5977c478bd9Sstevel@tonic-gate 		fatal("do_exec_pty: no session");
5987c478bd9Sstevel@tonic-gate 	ptyfd = s->ptyfd;
5997c478bd9Sstevel@tonic-gate 	ttyfd = s->ttyfd;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate #ifdef USE_PAM
6027c478bd9Sstevel@tonic-gate 	session_do_pam(s, 1);	/* pam_open_session() */
6037c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/*
6067c478bd9Sstevel@tonic-gate 	 * This pipe lets sshd wait for child to exec or exit.  This is
6077c478bd9Sstevel@tonic-gate 	 * particularly important for ALTPRIVSEP because the child is
6087c478bd9Sstevel@tonic-gate 	 * the one to call the monitor to request a record_login() and
6097c478bd9Sstevel@tonic-gate 	 * we don't want the child and the parent to compete for the
6107c478bd9Sstevel@tonic-gate 	 * monitor's attention.  But this is generic code and doesn't
6117c478bd9Sstevel@tonic-gate 	 * hurt to have here even if ALTPRIVSEP is not used.
6127c478bd9Sstevel@tonic-gate 	 */
6137c478bd9Sstevel@tonic-gate 	if (pipe(pipe_fds) != 0)
6147c478bd9Sstevel@tonic-gate 		packet_disconnect("pipe failed: %.100s", strerror(errno));
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	(void) fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC);
6177c478bd9Sstevel@tonic-gate 	(void) fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/* Fork the child. */
6207c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
6217c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[0]);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 		fatal_remove_all_cleanups();
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		/* Child.  Reinitialize the log because the pid has changed. */
6267c478bd9Sstevel@tonic-gate 		log_init(__progname, options.log_level, options.log_facility, log_stderr);
6277c478bd9Sstevel@tonic-gate 		/* Close the master side of the pseudo tty. */
6287c478bd9Sstevel@tonic-gate 		close(ptyfd);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		/* Make the pseudo tty our controlling tty. */
6317c478bd9Sstevel@tonic-gate 		pty_make_controlling_tty(&ttyfd, s->tty);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		/* Redirect stdin/stdout/stderr from the pseudo tty. */
6347c478bd9Sstevel@tonic-gate 		if (dup2(ttyfd, 0) < 0)
6357c478bd9Sstevel@tonic-gate 			error("dup2 stdin: %s", strerror(errno));
6367c478bd9Sstevel@tonic-gate 		if (dup2(ttyfd, 1) < 0)
6377c478bd9Sstevel@tonic-gate 			error("dup2 stdout: %s", strerror(errno));
6387c478bd9Sstevel@tonic-gate 		if (dup2(ttyfd, 2) < 0)
6397c478bd9Sstevel@tonic-gate 			error("dup2 stderr: %s", strerror(errno));
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		/* Close the extra descriptor for the pseudo tty. */
6427c478bd9Sstevel@tonic-gate 		close(ttyfd);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		/* record login, etc. similar to login(1) */
6457c478bd9Sstevel@tonic-gate #if !defined(HAVE_OSF_SIA)
6467c478bd9Sstevel@tonic-gate 		if (!(options.use_login && command == NULL)) {
6477c478bd9Sstevel@tonic-gate #ifdef _UNICOS
6487c478bd9Sstevel@tonic-gate 			cray_init_job(s->pw); /* set up cray jid and tmpdir */
6497c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
6507c478bd9Sstevel@tonic-gate 			do_login(s, command);
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate # ifdef LOGIN_NEEDS_UTMPX
6537c478bd9Sstevel@tonic-gate 		else
6547c478bd9Sstevel@tonic-gate 			do_pre_login(s);
6557c478bd9Sstevel@tonic-gate # endif
6567c478bd9Sstevel@tonic-gate #endif /* !HAVE_OSF_SIA */
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		/*
6597c478bd9Sstevel@tonic-gate 		 * do_pre_login() will have completed the record_login(), so
6607c478bd9Sstevel@tonic-gate 		 * close the pipe to the parent so it can re-enter its event
6617c478bd9Sstevel@tonic-gate 		 * loop and service the ptm; if enough debug messages get
6627c478bd9Sstevel@tonic-gate 		 * written to the pty before this happens there will be a
6637c478bd9Sstevel@tonic-gate 		 * deadlock.
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		close(pipe_fds[1]);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/* Do common processing for the child, such as execing the command. */
6687c478bd9Sstevel@tonic-gate 		do_child(s, command);
6697c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/* Wait for child to exec() or exit() */
6737c478bd9Sstevel@tonic-gate 	(void) close(pipe_fds[1]);
6747c478bd9Sstevel@tonic-gate 	(void) read(pipe_fds[0], &pipe_fds[1], sizeof(int));
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate #ifdef _UNICOS
6777c478bd9Sstevel@tonic-gate 	signal(WJSIGNAL, cray_job_termination_handler);
6787c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
6797c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
6807c478bd9Sstevel@tonic-gate 	if (is_winnt)
6817c478bd9Sstevel@tonic-gate 		cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
6827c478bd9Sstevel@tonic-gate #endif
6837c478bd9Sstevel@tonic-gate 	if (pid < 0)
6847c478bd9Sstevel@tonic-gate 		packet_disconnect("fork failed: %.100s", strerror(errno));
6857c478bd9Sstevel@tonic-gate 	s->pid = pid;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* Parent.  Close the slave side of the pseudo tty. */
6887c478bd9Sstevel@tonic-gate 	close(ttyfd);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Create another descriptor of the pty master side for use as the
6927c478bd9Sstevel@tonic-gate 	 * standard input.  We could use the original descriptor, but this
6937c478bd9Sstevel@tonic-gate 	 * simplifies code in server_loop.  The descriptor is bidirectional.
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	fdout = dup(ptyfd);
6967c478bd9Sstevel@tonic-gate 	if (fdout < 0)
6977c478bd9Sstevel@tonic-gate 		packet_disconnect("dup #1 failed: %.100s", strerror(errno));
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/* we keep a reference to the pty master */
7007c478bd9Sstevel@tonic-gate 	ptymaster = dup(ptyfd);
7017c478bd9Sstevel@tonic-gate 	if (ptymaster < 0)
7027c478bd9Sstevel@tonic-gate 		packet_disconnect("dup #2 failed: %.100s", strerror(errno));
7037c478bd9Sstevel@tonic-gate 	s->ptymaster = ptymaster;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/* Enter interactive session. */
7067c478bd9Sstevel@tonic-gate 	packet_set_interactive(1);
7077c478bd9Sstevel@tonic-gate 	if (compat20) {
7087c478bd9Sstevel@tonic-gate 		session_set_fds(s, ptyfd, fdout, -1);
7097c478bd9Sstevel@tonic-gate 		/* Don't close channel before sending exit-status! */
7107c478bd9Sstevel@tonic-gate 		channel_set_wait_for_exit(s->chanid, 1);
7117c478bd9Sstevel@tonic-gate 	} else {
7127c478bd9Sstevel@tonic-gate 		server_loop(pid, ptyfd, fdout, -1);
7137c478bd9Sstevel@tonic-gate 		/* server_loop _has_ closed ptyfd and fdout. */
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate #ifdef LOGIN_NEEDS_UTMPX
7187c478bd9Sstevel@tonic-gate static void
7197c478bd9Sstevel@tonic-gate do_pre_login(Session *s)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	socklen_t fromlen;
7227c478bd9Sstevel@tonic-gate 	struct sockaddr_storage from;
7237c478bd9Sstevel@tonic-gate 	pid_t pid = getpid();
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/*
7267c478bd9Sstevel@tonic-gate 	 * Get IP address of client. If the connection is not a socket, let
7277c478bd9Sstevel@tonic-gate 	 * the address be 0.0.0.0.
7287c478bd9Sstevel@tonic-gate 	 */
7297c478bd9Sstevel@tonic-gate 	memset(&from, 0, sizeof(from));
7307c478bd9Sstevel@tonic-gate 	fromlen = sizeof(from);
7317c478bd9Sstevel@tonic-gate 	if (packet_connection_is_on_socket()) {
7327c478bd9Sstevel@tonic-gate 		if (getpeername(packet_get_connection_in(),
7337c478bd9Sstevel@tonic-gate 		    (struct sockaddr *) & from, &fromlen) < 0) {
7347c478bd9Sstevel@tonic-gate 			debug("getpeername: %.100s", strerror(errno));
7357c478bd9Sstevel@tonic-gate 			fatal_cleanup();
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	record_utmp_only(pid, s->tty, s->pw->pw_name,
7407c478bd9Sstevel@tonic-gate 	    get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
7417c478bd9Sstevel@tonic-gate 	    (struct sockaddr *)&from);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate #endif
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate  * This is called to fork and execute a command.  If another command is
7477c478bd9Sstevel@tonic-gate  * to be forced, execute that instead.
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate void
7507c478bd9Sstevel@tonic-gate do_exec(Session *s, const char *command)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	if (command)
7537c478bd9Sstevel@tonic-gate 		s->command = xstrdup(command);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (forced_command) {
7567c478bd9Sstevel@tonic-gate 		original_command = command;
7577c478bd9Sstevel@tonic-gate 		command = forced_command;
7587c478bd9Sstevel@tonic-gate 		debug("Forced command '%.900s'", command);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1)
7627c478bd9Sstevel@tonic-gate 		do_exec_pty(s, command);
7637c478bd9Sstevel@tonic-gate 	else
7647c478bd9Sstevel@tonic-gate 		do_exec_no_pty(s, command);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	original_command = NULL;
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate /* administrative, login(1)-like work */
7717c478bd9Sstevel@tonic-gate void
7727c478bd9Sstevel@tonic-gate do_login(Session *s, const char *command)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	char *time_string;
7757c478bd9Sstevel@tonic-gate #ifndef ALTPRIVSEP
7767c478bd9Sstevel@tonic-gate 	struct passwd * pw = s->pw;
7777c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7787c478bd9Sstevel@tonic-gate 	pid_t pid = getpid();
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/* Record that there was a login on that tty from the remote host. */
7817c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
7827c478bd9Sstevel@tonic-gate 	debug3("Recording SSHv2 channel login in utmpx/wtmpx");
7837c478bd9Sstevel@tonic-gate 	altprivsep_record_login(pid, s->tty);
7847c478bd9Sstevel@tonic-gate #else /* ALTPRIVSEP*/
7857c478bd9Sstevel@tonic-gate 	if (!use_privsep) {
7867c478bd9Sstevel@tonic-gate 		debug3("Recording SSHv2 channel login in utmpx/wtmpx");
7877c478bd9Sstevel@tonic-gate 		record_login(pid, s->tty, NULL, pw->pw_name);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	if (check_quietlogin(s, command))
7927c478bd9Sstevel@tonic-gate 		return;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate #ifdef USE_PAM
7957c478bd9Sstevel@tonic-gate 		print_pam_messages();
7967c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
7977c478bd9Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
7987c478bd9Sstevel@tonic-gate 	if (aixloginmsg && *aixloginmsg)
7997c478bd9Sstevel@tonic-gate 		printf("%s\n", aixloginmsg);
8007c478bd9Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate #ifndef NO_SSH_LASTLOG
8037c478bd9Sstevel@tonic-gate 	if (options.print_lastlog && s->last_login_time != 0) {
8047c478bd9Sstevel@tonic-gate 		time_string = ctime(&s->last_login_time);
8057c478bd9Sstevel@tonic-gate 		if (strchr(time_string, '\n'))
8067c478bd9Sstevel@tonic-gate 			*strchr(time_string, '\n') = 0;
8077c478bd9Sstevel@tonic-gate 		if (strcmp(s->hostname, "") == 0)
8087c478bd9Sstevel@tonic-gate 			printf("Last login: %s\r\n", time_string);
8097c478bd9Sstevel@tonic-gate 		else
8107c478bd9Sstevel@tonic-gate 			printf("Last login: %s from %s\r\n", time_string,
8117c478bd9Sstevel@tonic-gate 			    s->hostname);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate #endif /* NO_SSH_LASTLOG */
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	do_motd();
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * Display the message of the day.
8207c478bd9Sstevel@tonic-gate  */
8217c478bd9Sstevel@tonic-gate void
8227c478bd9Sstevel@tonic-gate do_motd(void)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate 	FILE *f;
8257c478bd9Sstevel@tonic-gate 	char buf[256];
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (options.print_motd) {
8287c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
8297c478bd9Sstevel@tonic-gate 		f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
8307c478bd9Sstevel@tonic-gate 		    "/etc/motd"), "r");
8317c478bd9Sstevel@tonic-gate #else
8327c478bd9Sstevel@tonic-gate 		f = fopen("/etc/motd", "r");
8337c478bd9Sstevel@tonic-gate #endif
8347c478bd9Sstevel@tonic-gate 		if (f) {
8357c478bd9Sstevel@tonic-gate 			while (fgets(buf, sizeof(buf), f))
8367c478bd9Sstevel@tonic-gate 				fputs(buf, stdout);
8377c478bd9Sstevel@tonic-gate 			fclose(f);
8387c478bd9Sstevel@tonic-gate 		}
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * Check for quiet login, either .hushlogin or command given.
8457c478bd9Sstevel@tonic-gate  */
8467c478bd9Sstevel@tonic-gate int
8477c478bd9Sstevel@tonic-gate check_quietlogin(Session *s, const char *command)
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	char buf[256];
8507c478bd9Sstevel@tonic-gate 	struct passwd *pw = s->pw;
8517c478bd9Sstevel@tonic-gate 	struct stat st;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/* Return 1 if .hushlogin exists or a command given. */
8547c478bd9Sstevel@tonic-gate 	if (command != NULL)
8557c478bd9Sstevel@tonic-gate 		return 1;
8567c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
8577c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
8587c478bd9Sstevel@tonic-gate 	if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
8597c478bd9Sstevel@tonic-gate 		return 1;
8607c478bd9Sstevel@tonic-gate #else
8617c478bd9Sstevel@tonic-gate 	if (stat(buf, &st) >= 0)
8627c478bd9Sstevel@tonic-gate 		return 1;
8637c478bd9Sstevel@tonic-gate #endif
8647c478bd9Sstevel@tonic-gate 	return 0;
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate /*
8687c478bd9Sstevel@tonic-gate  * Sets the value of the given variable in the environment.  If the variable
8697c478bd9Sstevel@tonic-gate  * already exists, its value is overriden.
8707c478bd9Sstevel@tonic-gate  */
8717c478bd9Sstevel@tonic-gate void
8727c478bd9Sstevel@tonic-gate child_set_env(char ***envp, u_int *envsizep, const char *name,
8737c478bd9Sstevel@tonic-gate 	const char *value)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	u_int i, namelen;
8767c478bd9Sstevel@tonic-gate 	char **env;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	debug3("child_set_env(%s, %s)", name, value);
8797c478bd9Sstevel@tonic-gate 	/*
8807c478bd9Sstevel@tonic-gate 	 * Find the slot where the value should be stored.  If the variable
8817c478bd9Sstevel@tonic-gate 	 * already exists, we reuse the slot; otherwise we append a new slot
8827c478bd9Sstevel@tonic-gate 	 * at the end of the array, expanding if necessary.
8837c478bd9Sstevel@tonic-gate 	 */
8847c478bd9Sstevel@tonic-gate 	env = *envp;
8857c478bd9Sstevel@tonic-gate 	namelen = strlen(name);
8867c478bd9Sstevel@tonic-gate 	for (i = 0; env[i]; i++)
8877c478bd9Sstevel@tonic-gate 		if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
8887c478bd9Sstevel@tonic-gate 			break;
8897c478bd9Sstevel@tonic-gate 	if (env[i]) {
8907c478bd9Sstevel@tonic-gate 		/* Reuse the slot. */
8917c478bd9Sstevel@tonic-gate 		xfree(env[i]);
8927c478bd9Sstevel@tonic-gate 	} else {
8937c478bd9Sstevel@tonic-gate 		/* New variable.  Expand if necessary. */
8947c478bd9Sstevel@tonic-gate 		if (i >= (*envsizep) - 1) {
8957c478bd9Sstevel@tonic-gate 			if (*envsizep >= 1000)
8967c478bd9Sstevel@tonic-gate 				fatal("child_set_env: too many env vars,"
8977c478bd9Sstevel@tonic-gate 				    " skipping: %.100s", name);
8987c478bd9Sstevel@tonic-gate 			(*envsizep) += 50;
8997c478bd9Sstevel@tonic-gate 			env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		/* Need to set the NULL pointer at end of array beyond the new slot. */
9027c478bd9Sstevel@tonic-gate 		env[i + 1] = NULL;
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	/* Allocate space and format the variable in the appropriate slot. */
9067c478bd9Sstevel@tonic-gate 	env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
9077c478bd9Sstevel@tonic-gate 	snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * Reads environment variables from the given file and adds/overrides them
9127c478bd9Sstevel@tonic-gate  * into the environment.  If the file does not exist, this does nothing.
9137c478bd9Sstevel@tonic-gate  * Otherwise, it must consist of empty lines, comments (line starts with '#')
9147c478bd9Sstevel@tonic-gate  * and assignments of the form name=value.  No other forms are allowed.
9157c478bd9Sstevel@tonic-gate  */
9167c478bd9Sstevel@tonic-gate static void
9177c478bd9Sstevel@tonic-gate read_environment_file(char ***env, u_int *envsize,
9187c478bd9Sstevel@tonic-gate 	const char *filename)
9197c478bd9Sstevel@tonic-gate {
9207c478bd9Sstevel@tonic-gate 	FILE *f;
9217c478bd9Sstevel@tonic-gate 	char buf[4096];
9227c478bd9Sstevel@tonic-gate 	char *cp, *value;
9237c478bd9Sstevel@tonic-gate 	u_int lineno = 0;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	f = fopen(filename, "r");
9267c478bd9Sstevel@tonic-gate 	if (!f)
9277c478bd9Sstevel@tonic-gate 		return;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof(buf), f)) {
9307c478bd9Sstevel@tonic-gate 		if (++lineno > 1000)
9317c478bd9Sstevel@tonic-gate 			fatal("Too many lines in environment file %s", filename);
9327c478bd9Sstevel@tonic-gate 		for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
9337c478bd9Sstevel@tonic-gate 			;
9347c478bd9Sstevel@tonic-gate 		if (!*cp || *cp == '#' || *cp == '\n')
9357c478bd9Sstevel@tonic-gate 			continue;
9367c478bd9Sstevel@tonic-gate 		if (strchr(cp, '\n'))
9377c478bd9Sstevel@tonic-gate 			*strchr(cp, '\n') = '\0';
9387c478bd9Sstevel@tonic-gate 		value = strchr(cp, '=');
9397c478bd9Sstevel@tonic-gate 		if (value == NULL) {
9407c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Bad line %u in %.100s\n"),
9417c478bd9Sstevel@tonic-gate 				lineno, filename);
9427c478bd9Sstevel@tonic-gate 			continue;
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 		/*
9457c478bd9Sstevel@tonic-gate 		 * Replace the equals sign by nul, and advance value to
9467c478bd9Sstevel@tonic-gate 		 * the value string.
9477c478bd9Sstevel@tonic-gate 		 */
9487c478bd9Sstevel@tonic-gate 		*value = '\0';
9497c478bd9Sstevel@tonic-gate 		value++;
9507c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, cp, value);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 	fclose(f);
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate void copy_environment(char **source, char ***env, u_int *envsize)
9567c478bd9Sstevel@tonic-gate {
9577c478bd9Sstevel@tonic-gate 	char *var_name, *var_val;
9587c478bd9Sstevel@tonic-gate 	int i;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (source == NULL)
9617c478bd9Sstevel@tonic-gate 		return;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	for(i = 0; source[i] != NULL; i++) {
9647c478bd9Sstevel@tonic-gate 		var_name = xstrdup(source[i]);
9657c478bd9Sstevel@tonic-gate 		if ((var_val = strstr(var_name, "=")) == NULL) {
9667c478bd9Sstevel@tonic-gate 			xfree(var_name);
9677c478bd9Sstevel@tonic-gate 			continue;
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 		*var_val++ = '\0';
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		debug3("Copy environment: %s=%s", var_name, var_val);
9727c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, var_name, var_val);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		xfree(var_name);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
9797c478bd9Sstevel@tonic-gate static
9807c478bd9Sstevel@tonic-gate void
9817c478bd9Sstevel@tonic-gate deflt_do_setup_env(Session *s, const char *shell, char ***env, u_int *envsize)
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	int	flags;
9847c478bd9Sstevel@tonic-gate 	char	*ptr;
9857c478bd9Sstevel@tonic-gate 	mode_t	Umask = 022;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (defopen(_PATH_DEFAULT_LOGIN))
9887c478bd9Sstevel@tonic-gate 		return;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/* Ignore case */
9917c478bd9Sstevel@tonic-gate 	flags = defcntl(DC_GETFLAGS, 0);
9927c478bd9Sstevel@tonic-gate 	TURNOFF(flags, DC_CASE);
9937c478bd9Sstevel@tonic-gate 	(void) defcntl(DC_SETFLAGS, flags);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	/* TZ & HZ */
9967c478bd9Sstevel@tonic-gate 	if ((ptr = defread("TIMEZONE=")) != NULL)
9977c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "TZ", ptr);
9987c478bd9Sstevel@tonic-gate 	if ((ptr = defread("HZ=")) != NULL)
9997c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "HZ", ptr);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/* PATH */
10027c478bd9Sstevel@tonic-gate 	if (s->pw->pw_uid != 0 && (ptr = defread("PATH=")) != NULL)
10037c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "PATH", ptr);
10047c478bd9Sstevel@tonic-gate 	if (s->pw->pw_uid == 0 && (ptr = defread("SUPATH=")) != NULL)
10057c478bd9Sstevel@tonic-gate 		child_set_env(env, envsize, "PATH", ptr);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	/* SHELL */
10087c478bd9Sstevel@tonic-gate 	if ((ptr = defread("ALTSHELL=")) != NULL) {
10097c478bd9Sstevel@tonic-gate 		if (strcasecmp("YES", ptr) == 0)
10107c478bd9Sstevel@tonic-gate 			child_set_env(env, envsize, "SHELL", shell);
10117c478bd9Sstevel@tonic-gate 		else
10127c478bd9Sstevel@tonic-gate 			child_set_env(env, envsize, "SHELL", "");
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/* UMASK */
10167c478bd9Sstevel@tonic-gate 	if ((ptr = defread("UMASK=")) != NULL &&
10177c478bd9Sstevel@tonic-gate 	    sscanf(ptr, "%lo", &Umask) == 1 &&
10187c478bd9Sstevel@tonic-gate 	    Umask <= (mode_t)0777)
10197c478bd9Sstevel@tonic-gate 		(void) umask(Umask);
10207c478bd9Sstevel@tonic-gate 	else
10217c478bd9Sstevel@tonic-gate 		(void) umask(022);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/* ULIMIT */
10247c478bd9Sstevel@tonic-gate 	if ((ptr = defread("ULIMIT=")) != NULL && atol(ptr) > 0L &&
10257c478bd9Sstevel@tonic-gate 	    ulimit(UL_SETFSIZE, atol(ptr)) < 0L)
10267c478bd9Sstevel@tonic-gate 		error("Could not set ULIMIT to %ld from %s\n", atol(ptr),
10277c478bd9Sstevel@tonic-gate 			_PATH_DEFAULT_LOGIN);
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	(void) defopen(NULL);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate static char **
10347c478bd9Sstevel@tonic-gate do_setup_env(Session *s, const char *shell)
10357c478bd9Sstevel@tonic-gate {
1036*26ba1984Sjp161948 	char buf[256], *path_maildir = _PATH_MAILDIR;
1037*26ba1984Sjp161948 	u_int i, envsize, pm_len;
10387c478bd9Sstevel@tonic-gate 	char **env;
10397c478bd9Sstevel@tonic-gate 	struct passwd *pw = s->pw;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/* Initialize the environment. */
10427c478bd9Sstevel@tonic-gate 	envsize = 100;
10437c478bd9Sstevel@tonic-gate 	env = xmalloc(envsize * sizeof(char *));
10447c478bd9Sstevel@tonic-gate 	env[0] = NULL;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
10477c478bd9Sstevel@tonic-gate 	/*
10487c478bd9Sstevel@tonic-gate 	 * The Windows environment contains some setting which are
10497c478bd9Sstevel@tonic-gate 	 * important for a running system. They must not be dropped.
10507c478bd9Sstevel@tonic-gate 	 */
10517c478bd9Sstevel@tonic-gate 	copy_environment(environ, &env, &envsize);
10527c478bd9Sstevel@tonic-gate #endif
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate #ifdef GSSAPI
10557c478bd9Sstevel@tonic-gate 	/* Allow any GSSAPI methods that we've used to alter
10567c478bd9Sstevel@tonic-gate 	 * the childs environment as they see fit
10577c478bd9Sstevel@tonic-gate 	 */
10587c478bd9Sstevel@tonic-gate 	ssh_gssapi_do_child(xxx_gssctxt, &env,&envsize);
10597c478bd9Sstevel@tonic-gate #endif
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	if (!options.use_login) {
10627c478bd9Sstevel@tonic-gate 		/* Set basic environment. */
10637c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "USER", pw->pw_name);
10647c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
10657c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "HOME", pw->pw_dir);
10667c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
10677c478bd9Sstevel@tonic-gate 		if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
10687c478bd9Sstevel@tonic-gate 			child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10697c478bd9Sstevel@tonic-gate 		else
10707c478bd9Sstevel@tonic-gate 			child_set_env(&env, &envsize, "PATH", getenv("PATH"));
10717c478bd9Sstevel@tonic-gate #else /* HAVE_LOGIN_CAP */
10727c478bd9Sstevel@tonic-gate # ifndef HAVE_CYGWIN
10737c478bd9Sstevel@tonic-gate 		/*
10747c478bd9Sstevel@tonic-gate 		 * There's no standard path on Windows. The path contains
10757c478bd9Sstevel@tonic-gate 		 * important components pointing to the system directories,
10767c478bd9Sstevel@tonic-gate 		 * needed for loading shared libraries. So the path better
10777c478bd9Sstevel@tonic-gate 		 * remains intact here.
10787c478bd9Sstevel@tonic-gate 		 */
10797c478bd9Sstevel@tonic-gate #  ifdef SUPERUSER_PATH
10807c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "PATH",
10817c478bd9Sstevel@tonic-gate 		    s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
10827c478bd9Sstevel@tonic-gate #  else
10837c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10847c478bd9Sstevel@tonic-gate #  endif /* SUPERUSER_PATH */
10857c478bd9Sstevel@tonic-gate # endif /* HAVE_CYGWIN */
10867c478bd9Sstevel@tonic-gate #endif /* HAVE_LOGIN_CAP */
10877c478bd9Sstevel@tonic-gate 
1088*26ba1984Sjp161948 		pm_len = strlen(path_maildir);
1089*26ba1984Sjp161948 		if (path_maildir[pm_len - 1] == '/' && pm_len > 1)
1090*26ba1984Sjp161948 			path_maildir[pm_len - 1] = NULL;
10917c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof buf, "%.200s/%.50s",
1092*26ba1984Sjp161948 			 path_maildir, pw->pw_name);
10937c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "MAIL", buf);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		/* Normal systems set SHELL by default. */
10967c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "SHELL", shell);
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
10997c478bd9Sstevel@tonic-gate 		deflt_do_setup_env(s, shell, &env, &envsize);
11007c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate #define PASS_ENV(x) \
11047c478bd9Sstevel@tonic-gate 	if (getenv(x)) \
11057c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, x, getenv(x));
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	if (getenv("TZ"))
11087c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "TZ", getenv("TZ"));
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	PASS_ENV("LANG")
11117c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_ALL")
11127c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_CTYPE")
11137c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_COLLATE")
11147c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_CTIME")
11157c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_NUMERIC")
11167c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_MONETARY")
11177c478bd9Sstevel@tonic-gate 	PASS_ENV("LC_MESSAGES")
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate #undef PASS_ENV
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (s->env != NULL)
11227c478bd9Sstevel@tonic-gate 		copy_environment(s->env, &env, &envsize);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/* Set custom environment options from RSA authentication. */
11257c478bd9Sstevel@tonic-gate 	if (!options.use_login) {
11267c478bd9Sstevel@tonic-gate 		while (custom_environment) {
11277c478bd9Sstevel@tonic-gate 			struct envstring *ce = custom_environment;
11287c478bd9Sstevel@tonic-gate 			char *str = ce->s;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 			for (i = 0; str[i] != '=' && str[i]; i++)
11317c478bd9Sstevel@tonic-gate 				;
11327c478bd9Sstevel@tonic-gate 			if (str[i] == '=') {
11337c478bd9Sstevel@tonic-gate 				str[i] = 0;
11347c478bd9Sstevel@tonic-gate 				child_set_env(&env, &envsize, str, str + i + 1);
11357c478bd9Sstevel@tonic-gate 			}
11367c478bd9Sstevel@tonic-gate 			custom_environment = ce->next;
11377c478bd9Sstevel@tonic-gate 			xfree(ce->s);
11387c478bd9Sstevel@tonic-gate 			xfree(ce);
11397c478bd9Sstevel@tonic-gate 		}
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/* SSH_CLIENT deprecated */
11437c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof buf, "%.50s %d %d",
11447c478bd9Sstevel@tonic-gate 	    get_remote_ipaddr(), get_remote_port(), get_local_port());
11457c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "SSH_CLIENT", buf);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
11487c478bd9Sstevel@tonic-gate 	    get_remote_ipaddr(), get_remote_port(),
11497c478bd9Sstevel@tonic-gate 	    get_local_ipaddr(packet_get_connection_in()), get_local_port());
11507c478bd9Sstevel@tonic-gate 	child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1)
11537c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "SSH_TTY", s->tty);
11547c478bd9Sstevel@tonic-gate 	if (s->term)
11557c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "TERM", s->term);
11567c478bd9Sstevel@tonic-gate 	if (s->display)
11577c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "DISPLAY", s->display);
11587c478bd9Sstevel@tonic-gate 	if (original_command)
11597c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
11607c478bd9Sstevel@tonic-gate 		    original_command);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate #ifdef _UNICOS
11637c478bd9Sstevel@tonic-gate 	if (cray_tmpdir[0] != '\0')
11647c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
11657c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate #ifdef _AIX
11687c478bd9Sstevel@tonic-gate 	{
11697c478bd9Sstevel@tonic-gate 		char *cp;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 		if ((cp = getenv("AUTHSTATE")) != NULL)
11727c478bd9Sstevel@tonic-gate 			child_set_env(&env, &envsize, "AUTHSTATE", cp);
11737c478bd9Sstevel@tonic-gate 		if ((cp = getenv("KRB5CCNAME")) != NULL)
11747c478bd9Sstevel@tonic-gate 			child_set_env(&env, &envsize, "KRB5CCNAME", cp);
11757c478bd9Sstevel@tonic-gate 		read_environment_file(&env, &envsize, "/etc/environment");
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate #endif
11787c478bd9Sstevel@tonic-gate #ifdef KRB4
11797c478bd9Sstevel@tonic-gate 	if (s->authctxt->krb4_ticket_file)
11807c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "KRBTKFILE",
11817c478bd9Sstevel@tonic-gate 		    s->authctxt->krb4_ticket_file);
11827c478bd9Sstevel@tonic-gate #endif
11837c478bd9Sstevel@tonic-gate #ifdef KRB5
11847c478bd9Sstevel@tonic-gate 	if (s->authctxt->krb5_ticket_file)
11857c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, "KRB5CCNAME",
11867c478bd9Sstevel@tonic-gate 		    s->authctxt->krb5_ticket_file);
11877c478bd9Sstevel@tonic-gate #endif
11887c478bd9Sstevel@tonic-gate #ifdef USE_PAM
11897c478bd9Sstevel@tonic-gate 	/*
11907c478bd9Sstevel@tonic-gate 	 * Pull in any environment variables that may have
11917c478bd9Sstevel@tonic-gate 	 * been set by PAM.
11927c478bd9Sstevel@tonic-gate 	 */
11937c478bd9Sstevel@tonic-gate 	{
11947c478bd9Sstevel@tonic-gate 		char **p;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 		p = fetch_pam_environment(s->authctxt);
11977c478bd9Sstevel@tonic-gate 		copy_environment(p, &env, &envsize);
11987c478bd9Sstevel@tonic-gate 		free_pam_environment(p);
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (auth_sock_name != NULL)
12037c478bd9Sstevel@tonic-gate 		child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
12047c478bd9Sstevel@tonic-gate 		    auth_sock_name);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	/* read $HOME/.ssh/environment. */
12077c478bd9Sstevel@tonic-gate 	if (options.permit_user_env && !options.use_login) {
12087c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
12097c478bd9Sstevel@tonic-gate 		    strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
12107c478bd9Sstevel@tonic-gate 		read_environment_file(&env, &envsize, buf);
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate 	if (debug_flag) {
12137c478bd9Sstevel@tonic-gate 		/* dump the environment */
12147c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Environment:\n"));
12157c478bd9Sstevel@tonic-gate 		for (i = 0; env[i]; i++)
12167c478bd9Sstevel@tonic-gate 			fprintf(stderr, "  %.200s\n", env[i]);
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 	return env;
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
12237c478bd9Sstevel@tonic-gate  * first in this order).
12247c478bd9Sstevel@tonic-gate  */
12257c478bd9Sstevel@tonic-gate static void
12267c478bd9Sstevel@tonic-gate do_rc_files(Session *s, const char *shell)
12277c478bd9Sstevel@tonic-gate {
12287c478bd9Sstevel@tonic-gate 	FILE *f = NULL;
12297c478bd9Sstevel@tonic-gate 	char cmd[1024];
12307c478bd9Sstevel@tonic-gate 	int do_xauth;
12317c478bd9Sstevel@tonic-gate 	struct stat st;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	do_xauth =
12347c478bd9Sstevel@tonic-gate 	    s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/* ignore _PATH_SSH_USER_RC for subsystems */
12377c478bd9Sstevel@tonic-gate 	if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
12387c478bd9Sstevel@tonic-gate 		snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
12397c478bd9Sstevel@tonic-gate 		    shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
12407c478bd9Sstevel@tonic-gate 		if (debug_flag)
12417c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Running %s\n", cmd);
12427c478bd9Sstevel@tonic-gate 		f = popen(cmd, "w");
12437c478bd9Sstevel@tonic-gate 		if (f) {
12447c478bd9Sstevel@tonic-gate 			if (do_xauth)
12457c478bd9Sstevel@tonic-gate 				fprintf(f, "%s %s\n", s->auth_proto,
12467c478bd9Sstevel@tonic-gate 				    s->auth_data);
12477c478bd9Sstevel@tonic-gate 			pclose(f);
12487c478bd9Sstevel@tonic-gate 		} else
12497c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not run %s\n",
12507c478bd9Sstevel@tonic-gate 			    _PATH_SSH_USER_RC);
12517c478bd9Sstevel@tonic-gate 	} else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
12527c478bd9Sstevel@tonic-gate 		if (debug_flag)
12537c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
12547c478bd9Sstevel@tonic-gate 			    _PATH_SSH_SYSTEM_RC);
12557c478bd9Sstevel@tonic-gate 		f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
12567c478bd9Sstevel@tonic-gate 		if (f) {
12577c478bd9Sstevel@tonic-gate 			if (do_xauth)
12587c478bd9Sstevel@tonic-gate 				fprintf(f, "%s %s\n", s->auth_proto,
12597c478bd9Sstevel@tonic-gate 				    s->auth_data);
12607c478bd9Sstevel@tonic-gate 			pclose(f);
12617c478bd9Sstevel@tonic-gate 		} else
12627c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not run %s\n",
12637c478bd9Sstevel@tonic-gate 			    _PATH_SSH_SYSTEM_RC);
12647c478bd9Sstevel@tonic-gate 	} else if (do_xauth && options.xauth_location != NULL) {
12657c478bd9Sstevel@tonic-gate 		/* Add authority data to .Xauthority if appropriate. */
12667c478bd9Sstevel@tonic-gate 		if (debug_flag) {
12677c478bd9Sstevel@tonic-gate 			fprintf(stderr,
12687c478bd9Sstevel@tonic-gate 			    "Running %.500s add "
12697c478bd9Sstevel@tonic-gate 			    "%.100s %.100s %.100s\n",
12707c478bd9Sstevel@tonic-gate 			    options.xauth_location, s->auth_display,
12717c478bd9Sstevel@tonic-gate 			    s->auth_proto, s->auth_data);
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 		snprintf(cmd, sizeof cmd, "%s -q -",
12747c478bd9Sstevel@tonic-gate 		    options.xauth_location);
12757c478bd9Sstevel@tonic-gate 		f = popen(cmd, "w");
12767c478bd9Sstevel@tonic-gate 		if (f) {
12777c478bd9Sstevel@tonic-gate 			fprintf(f, "add %s %s %s\n",
12787c478bd9Sstevel@tonic-gate 			    s->auth_display, s->auth_proto,
12797c478bd9Sstevel@tonic-gate 			    s->auth_data);
12807c478bd9Sstevel@tonic-gate 			pclose(f);
12817c478bd9Sstevel@tonic-gate 		} else {
12827c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not run %s\n",
12837c478bd9Sstevel@tonic-gate 			    cmd);
12847c478bd9Sstevel@tonic-gate 		}
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate static void
12897c478bd9Sstevel@tonic-gate do_nologin(struct passwd *pw)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	FILE *f = NULL;
12927c478bd9Sstevel@tonic-gate 	char buf[1024];
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
12957c478bd9Sstevel@tonic-gate 	if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
12967c478bd9Sstevel@tonic-gate 		f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
12977c478bd9Sstevel@tonic-gate 		    _PATH_NOLOGIN), "r");
12987c478bd9Sstevel@tonic-gate #else
12997c478bd9Sstevel@tonic-gate 	if (pw->pw_uid)
13007c478bd9Sstevel@tonic-gate 		f = fopen(_PATH_NOLOGIN, "r");
13017c478bd9Sstevel@tonic-gate #endif
13027c478bd9Sstevel@tonic-gate 	if (f) {
13037c478bd9Sstevel@tonic-gate 		/* /etc/nologin exists.  Print its contents and exit. */
13047c478bd9Sstevel@tonic-gate 		log("User %.100s not allowed because %s exists",
13057c478bd9Sstevel@tonic-gate 		    pw->pw_name, _PATH_NOLOGIN);
13067c478bd9Sstevel@tonic-gate 		while (fgets(buf, sizeof(buf), f))
13077c478bd9Sstevel@tonic-gate 			fputs(buf, stderr);
13087c478bd9Sstevel@tonic-gate 		fclose(f);
13097c478bd9Sstevel@tonic-gate 		exit(254);
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate /* Set login name, uid, gid, and groups. */
13147c478bd9Sstevel@tonic-gate void
13157c478bd9Sstevel@tonic-gate do_setusercontext(struct passwd *pw)
13167c478bd9Sstevel@tonic-gate {
13177c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
13187c478bd9Sstevel@tonic-gate 	if (is_winnt) {
13197c478bd9Sstevel@tonic-gate #else /* HAVE_CYGWIN */
13207c478bd9Sstevel@tonic-gate 	if (getuid() == 0 || geteuid() == 0) {
13217c478bd9Sstevel@tonic-gate #endif /* HAVE_CYGWIN */
13227c478bd9Sstevel@tonic-gate #ifdef HAVE_SETPCRED
13237c478bd9Sstevel@tonic-gate 		setpcred(pw->pw_name);
13247c478bd9Sstevel@tonic-gate #endif /* HAVE_SETPCRED */
13257c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
13267c478bd9Sstevel@tonic-gate # ifdef __bsdi__
13277c478bd9Sstevel@tonic-gate 		setpgid(0, 0);
13287c478bd9Sstevel@tonic-gate # endif
13297c478bd9Sstevel@tonic-gate 		if (setusercontext(lc, pw, pw->pw_uid,
13307c478bd9Sstevel@tonic-gate 		    (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
13317c478bd9Sstevel@tonic-gate 			perror("unable to set user context");
13327c478bd9Sstevel@tonic-gate 			exit(1);
13337c478bd9Sstevel@tonic-gate 		}
13347c478bd9Sstevel@tonic-gate #else
13357c478bd9Sstevel@tonic-gate # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
13367c478bd9Sstevel@tonic-gate 		/* Sets login uid for accounting */
13377c478bd9Sstevel@tonic-gate 		if (getluid() == -1 && setluid(pw->pw_uid) == -1)
13387c478bd9Sstevel@tonic-gate 			error("setluid: %s", strerror(errno));
13397c478bd9Sstevel@tonic-gate # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 		if (setlogin(pw->pw_name) < 0)
13427c478bd9Sstevel@tonic-gate 			error("setlogin failed: %s", strerror(errno));
13437c478bd9Sstevel@tonic-gate 		if (setgid(pw->pw_gid) < 0) {
13447c478bd9Sstevel@tonic-gate 			perror("setgid");
13457c478bd9Sstevel@tonic-gate 			exit(1);
13467c478bd9Sstevel@tonic-gate 		}
13477c478bd9Sstevel@tonic-gate 		/* Initialize the group list. */
13487c478bd9Sstevel@tonic-gate 		if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
13497c478bd9Sstevel@tonic-gate 			perror("initgroups");
13507c478bd9Sstevel@tonic-gate 			exit(1);
13517c478bd9Sstevel@tonic-gate 		}
13527c478bd9Sstevel@tonic-gate 		endgrent();
13537c478bd9Sstevel@tonic-gate # if 0
13547c478bd9Sstevel@tonic-gate # ifdef USE_PAM
13557c478bd9Sstevel@tonic-gate 		/*
13567c478bd9Sstevel@tonic-gate 		 * PAM credentials may take the form of supplementary groups.
13577c478bd9Sstevel@tonic-gate 		 * These will have been wiped by the above initgroups() call.
13587c478bd9Sstevel@tonic-gate 		 * Reestablish them here.
13597c478bd9Sstevel@tonic-gate 		 */
13607c478bd9Sstevel@tonic-gate 		do_pam_setcred(0);
13617c478bd9Sstevel@tonic-gate # endif /* USE_PAM */
13627c478bd9Sstevel@tonic-gate # endif /* 0 */
13637c478bd9Sstevel@tonic-gate # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
13647c478bd9Sstevel@tonic-gate 		irix_setusercontext(pw);
13657c478bd9Sstevel@tonic-gate #  endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
13667c478bd9Sstevel@tonic-gate # ifdef _AIX
13677c478bd9Sstevel@tonic-gate 		aix_usrinfo(pw);
13687c478bd9Sstevel@tonic-gate # endif /* _AIX */
13697c478bd9Sstevel@tonic-gate 		/* Permanently switch to the desired uid. */
13707c478bd9Sstevel@tonic-gate 		permanently_set_uid(pw);
13717c478bd9Sstevel@tonic-gate #endif
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
13747c478bd9Sstevel@tonic-gate 		fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate static void
13787c478bd9Sstevel@tonic-gate launch_login(struct passwd *pw, const char *hostname)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate 	/* Launch login(1). */
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	execl(LOGIN_PROGRAM, "login", "-h", hostname,
13837c478bd9Sstevel@tonic-gate #ifdef xxxLOGIN_NEEDS_TERM
13847c478bd9Sstevel@tonic-gate 		    (s->term ? s->term : "unknown"),
13857c478bd9Sstevel@tonic-gate #endif /* LOGIN_NEEDS_TERM */
13867c478bd9Sstevel@tonic-gate #ifdef LOGIN_NO_ENDOPT
13877c478bd9Sstevel@tonic-gate 	    "-p", "-f", pw->pw_name, (char *)NULL);
13887c478bd9Sstevel@tonic-gate #else
13897c478bd9Sstevel@tonic-gate 	    "-p", "-f", "--", pw->pw_name, (char *)NULL);
13907c478bd9Sstevel@tonic-gate #endif
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	/* Login couldn't be executed, die. */
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	perror("login");
13957c478bd9Sstevel@tonic-gate 	exit(1);
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate /*
13997c478bd9Sstevel@tonic-gate  * Performs common processing for the child, such as setting up the
14007c478bd9Sstevel@tonic-gate  * environment, closing extra file descriptors, setting the user and group
14017c478bd9Sstevel@tonic-gate  * ids, and executing the command or shell.
14027c478bd9Sstevel@tonic-gate  */
14037c478bd9Sstevel@tonic-gate void
14047c478bd9Sstevel@tonic-gate do_child(Session *s, const char *command)
14057c478bd9Sstevel@tonic-gate {
14067c478bd9Sstevel@tonic-gate 	extern char **environ;
14077c478bd9Sstevel@tonic-gate 	char **env;
14087c478bd9Sstevel@tonic-gate 	char *argv[10];
14097c478bd9Sstevel@tonic-gate 	const char *shell, *shell0, *hostname = NULL;
14107c478bd9Sstevel@tonic-gate 	struct passwd *pw = s->pw;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	/* remove hostkey from the child's memory */
14137c478bd9Sstevel@tonic-gate 	destroy_sensitive_data();
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/* login(1) is only called if we execute the login shell */
14167c478bd9Sstevel@tonic-gate 	if (options.use_login && command != NULL)
14177c478bd9Sstevel@tonic-gate 		options.use_login = 0;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate #ifdef _UNICOS
14207c478bd9Sstevel@tonic-gate 	cray_setup(pw->pw_uid, pw->pw_name, command);
14217c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
14247c478bd9Sstevel@tonic-gate 	 * Login(1) does this as well, and it needs uid 0 for the "-h"
14257c478bd9Sstevel@tonic-gate 	 * switch, so we let login(1) to this for us.
14267c478bd9Sstevel@tonic-gate 	 */
14277c478bd9Sstevel@tonic-gate 	if (!options.use_login) {
14287c478bd9Sstevel@tonic-gate #ifdef HAVE_OSF_SIA
14297c478bd9Sstevel@tonic-gate 		session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
14307c478bd9Sstevel@tonic-gate 		if (!check_quietlogin(s, command))
14317c478bd9Sstevel@tonic-gate 			do_motd();
14327c478bd9Sstevel@tonic-gate #else /* HAVE_OSF_SIA */
14337c478bd9Sstevel@tonic-gate 		do_nologin(pw);
14347c478bd9Sstevel@tonic-gate 		do_setusercontext(pw);
14357c478bd9Sstevel@tonic-gate #endif /* HAVE_OSF_SIA */
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	/*
14397c478bd9Sstevel@tonic-gate 	 * Get the shell from the password data.  An empty shell field is
14407c478bd9Sstevel@tonic-gate 	 * legal, and means /bin/sh.
14417c478bd9Sstevel@tonic-gate 	 */
14427c478bd9Sstevel@tonic-gate 	shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
14437c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
14447c478bd9Sstevel@tonic-gate 	shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
14457c478bd9Sstevel@tonic-gate #endif
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	env = do_setup_env(s, shell);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	/* we have to stash the hostname before we close our socket. */
14507c478bd9Sstevel@tonic-gate 	if (options.use_login)
14517c478bd9Sstevel@tonic-gate 		hostname = get_remote_name_or_ip(utmp_len,
14527c478bd9Sstevel@tonic-gate 		    options.verify_reverse_mapping);
14537c478bd9Sstevel@tonic-gate 	/*
14547c478bd9Sstevel@tonic-gate 	 * Close the connection descriptors; note that this is the child, and
14557c478bd9Sstevel@tonic-gate 	 * the server will still have the socket open, and it is important
14567c478bd9Sstevel@tonic-gate 	 * that we do not shutdown it.  Note that the descriptors cannot be
14577c478bd9Sstevel@tonic-gate 	 * closed before building the environment, as we call
14587c478bd9Sstevel@tonic-gate 	 * get_remote_ipaddr there.
14597c478bd9Sstevel@tonic-gate 	 */
14607c478bd9Sstevel@tonic-gate 	if (packet_get_connection_in() == packet_get_connection_out())
14617c478bd9Sstevel@tonic-gate 		close(packet_get_connection_in());
14627c478bd9Sstevel@tonic-gate 	else {
14637c478bd9Sstevel@tonic-gate 		close(packet_get_connection_in());
14647c478bd9Sstevel@tonic-gate 		close(packet_get_connection_out());
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * Close all descriptors related to channels.  They will still remain
14687c478bd9Sstevel@tonic-gate 	 * open in the parent.
14697c478bd9Sstevel@tonic-gate 	 */
14707c478bd9Sstevel@tonic-gate 	/* XXX better use close-on-exec? -markus */
14717c478bd9Sstevel@tonic-gate 	channel_close_all();
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	/*
14747c478bd9Sstevel@tonic-gate 	 * Close any extra file descriptors.  Note that there may still be
14757c478bd9Sstevel@tonic-gate 	 * descriptors left by system functions.  They will be closed later.
14767c478bd9Sstevel@tonic-gate 	 */
14777c478bd9Sstevel@tonic-gate 	endpwent();
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	/*
14807c478bd9Sstevel@tonic-gate 	 * Close any extra open file descriptors so that we don\'t have them
14817c478bd9Sstevel@tonic-gate 	 * hanging around in clients.  Note that we want to do this after
14827c478bd9Sstevel@tonic-gate 	 * initgroups, because at least on Solaris 2.3 it leaves file
14837c478bd9Sstevel@tonic-gate 	 * descriptors open.
14847c478bd9Sstevel@tonic-gate 	 */
14857c478bd9Sstevel@tonic-gate 	closefrom(STDERR_FILENO + 1);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	/*
14887c478bd9Sstevel@tonic-gate 	 * Must take new environment into use so that .ssh/rc,
14897c478bd9Sstevel@tonic-gate 	 * /etc/ssh/sshrc and xauth are run in the proper environment.
14907c478bd9Sstevel@tonic-gate 	 */
14917c478bd9Sstevel@tonic-gate 	environ = env;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate #ifdef AFS
14947c478bd9Sstevel@tonic-gate 	/* Try to get AFS tokens for the local cell. */
14957c478bd9Sstevel@tonic-gate 	if (k_hasafs()) {
14967c478bd9Sstevel@tonic-gate 		char cell[64];
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
14997c478bd9Sstevel@tonic-gate 			krb_afslog(cell, 0);
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 		krb_afslog(0, 0);
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate #endif /* AFS */
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/* Change current directory to the user\'s home directory. */
15067c478bd9Sstevel@tonic-gate 	if (chdir(pw->pw_dir) < 0) {
15077c478bd9Sstevel@tonic-gate 		fprintf(stderr,
15087c478bd9Sstevel@tonic-gate 		    gettext("Could not chdir to home directory %s: %s\n"),
15097c478bd9Sstevel@tonic-gate 		    pw->pw_dir, strerror(errno));
15107c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
15117c478bd9Sstevel@tonic-gate 		if (login_getcapbool(lc, "requirehome", 0))
15127c478bd9Sstevel@tonic-gate 			exit(1);
15137c478bd9Sstevel@tonic-gate #endif
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (!options.use_login)
15177c478bd9Sstevel@tonic-gate 		do_rc_files(s, shell);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	/* restore SIGPIPE for child */
15207c478bd9Sstevel@tonic-gate 	signal(SIGPIPE,  SIG_DFL);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	if (options.use_login) {
15237c478bd9Sstevel@tonic-gate 		launch_login(pw, hostname);
15247c478bd9Sstevel@tonic-gate 		/* NEVERREACHED */
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/* Get the last component of the shell name. */
15287c478bd9Sstevel@tonic-gate 	if ((shell0 = strrchr(shell, '/')) != NULL)
15297c478bd9Sstevel@tonic-gate 		shell0++;
15307c478bd9Sstevel@tonic-gate 	else
15317c478bd9Sstevel@tonic-gate 		shell0 = shell;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	/*
15347c478bd9Sstevel@tonic-gate 	 * If we have no command, execute the shell.  In this case, the shell
15357c478bd9Sstevel@tonic-gate 	 * name to be passed in argv[0] is preceded by '-' to indicate that
15367c478bd9Sstevel@tonic-gate 	 * this is a login shell.
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	if (!command) {
15397c478bd9Sstevel@tonic-gate 		char argv0[256];
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 		/* Start the shell.  Set initial character to '-'. */
15427c478bd9Sstevel@tonic-gate 		argv0[0] = '-';
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 		if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
15457c478bd9Sstevel@tonic-gate 		    >= sizeof(argv0) - 1) {
15467c478bd9Sstevel@tonic-gate 			errno = EINVAL;
15477c478bd9Sstevel@tonic-gate 			perror(shell);
15487c478bd9Sstevel@tonic-gate 			exit(1);
15497c478bd9Sstevel@tonic-gate 		}
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 		/* Execute the shell. */
15527c478bd9Sstevel@tonic-gate 		argv[0] = argv0;
15537c478bd9Sstevel@tonic-gate 		argv[1] = NULL;
15547c478bd9Sstevel@tonic-gate 		execve(shell, argv, env);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		/* Executing the shell failed. */
15577c478bd9Sstevel@tonic-gate 		perror(shell);
15587c478bd9Sstevel@tonic-gate 		exit(1);
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 	/*
15617c478bd9Sstevel@tonic-gate 	 * Execute the command using the user's shell.  This uses the -c
15627c478bd9Sstevel@tonic-gate 	 * option to execute the command.
15637c478bd9Sstevel@tonic-gate 	 */
15647c478bd9Sstevel@tonic-gate 	argv[0] = (char *) shell0;
15657c478bd9Sstevel@tonic-gate 	argv[1] = "-c";
15667c478bd9Sstevel@tonic-gate 	argv[2] = (char *) command;
15677c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
15687c478bd9Sstevel@tonic-gate 	execve(shell, argv, env);
15697c478bd9Sstevel@tonic-gate 	perror(shell);
15707c478bd9Sstevel@tonic-gate 	exit(1);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate Session *
15747c478bd9Sstevel@tonic-gate session_new(void)
15757c478bd9Sstevel@tonic-gate {
15767c478bd9Sstevel@tonic-gate 	int i;
15777c478bd9Sstevel@tonic-gate 	static int did_init = 0;
15787c478bd9Sstevel@tonic-gate 	if (!did_init) {
15797c478bd9Sstevel@tonic-gate 		debug("session_new: init");
15807c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAX_SESSIONS; i++) {
15817c478bd9Sstevel@tonic-gate 			sessions[i].used = 0;
15827c478bd9Sstevel@tonic-gate 		}
15837c478bd9Sstevel@tonic-gate 		did_init = 1;
15847c478bd9Sstevel@tonic-gate 	}
15857c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
15867c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
15877c478bd9Sstevel@tonic-gate 		if (! s->used) {
15887c478bd9Sstevel@tonic-gate 			memset(s, 0, sizeof(*s));
15897c478bd9Sstevel@tonic-gate 			s->chanid = -1;
15907c478bd9Sstevel@tonic-gate 			s->ptyfd = -1;
15917c478bd9Sstevel@tonic-gate 			s->ttyfd = -1;
15927c478bd9Sstevel@tonic-gate 			s->used = 1;
15937c478bd9Sstevel@tonic-gate 			s->self = i;
15947c478bd9Sstevel@tonic-gate 			s->env = NULL;
15957c478bd9Sstevel@tonic-gate 			debug("session_new: session %d", i);
15967c478bd9Sstevel@tonic-gate 			return s;
15977c478bd9Sstevel@tonic-gate 		}
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 	return NULL;
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate static void
16037c478bd9Sstevel@tonic-gate session_dump(void)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	int i;
16067c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
16077c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
16087c478bd9Sstevel@tonic-gate 		debug("dump: used %d session %d %p channel %d pid %ld",
16097c478bd9Sstevel@tonic-gate 		    s->used,
16107c478bd9Sstevel@tonic-gate 		    s->self,
16117c478bd9Sstevel@tonic-gate 		    s,
16127c478bd9Sstevel@tonic-gate 		    s->chanid,
16137c478bd9Sstevel@tonic-gate 		    (long)s->pid);
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate int
16187c478bd9Sstevel@tonic-gate session_open(Authctxt *authctxt, int chanid)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	Session *s = session_new();
16217c478bd9Sstevel@tonic-gate 	debug("session_open: channel %d", chanid);
16227c478bd9Sstevel@tonic-gate 	if (s == NULL) {
16237c478bd9Sstevel@tonic-gate 		error("no more sessions");
16247c478bd9Sstevel@tonic-gate 		return 0;
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 	s->authctxt = authctxt;
16277c478bd9Sstevel@tonic-gate 	s->pw = authctxt->pw;
16287c478bd9Sstevel@tonic-gate 	if (s->pw == NULL)
16297c478bd9Sstevel@tonic-gate 		fatal("no user for session %d", s->self);
16307c478bd9Sstevel@tonic-gate 	debug("session_open: session %d: link with channel %d", s->self, chanid);
16317c478bd9Sstevel@tonic-gate 	s->chanid = chanid;
16327c478bd9Sstevel@tonic-gate 	return 1;
16337c478bd9Sstevel@tonic-gate }
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate #ifndef lint
16367c478bd9Sstevel@tonic-gate Session *
16377c478bd9Sstevel@tonic-gate session_by_tty(char *tty)
16387c478bd9Sstevel@tonic-gate {
16397c478bd9Sstevel@tonic-gate 	int i;
16407c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
16417c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
16427c478bd9Sstevel@tonic-gate 		if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
16437c478bd9Sstevel@tonic-gate 			debug("session_by_tty: session %d tty %s", i, tty);
16447c478bd9Sstevel@tonic-gate 			return s;
16457c478bd9Sstevel@tonic-gate 		}
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate 	debug("session_by_tty: unknown tty %.100s", tty);
16487c478bd9Sstevel@tonic-gate 	session_dump();
16497c478bd9Sstevel@tonic-gate 	return NULL;
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate #endif /* lint */
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate static Session *
16547c478bd9Sstevel@tonic-gate session_by_channel(int id)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	int i;
16577c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
16587c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
16597c478bd9Sstevel@tonic-gate 		if (s->used && s->chanid == id) {
16607c478bd9Sstevel@tonic-gate 			debug("session_by_channel: session %d channel %d", i, id);
16617c478bd9Sstevel@tonic-gate 			return s;
16627c478bd9Sstevel@tonic-gate 		}
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 	debug("session_by_channel: unknown channel %d", id);
16657c478bd9Sstevel@tonic-gate 	session_dump();
16667c478bd9Sstevel@tonic-gate 	return NULL;
16677c478bd9Sstevel@tonic-gate }
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate static Session *
16707c478bd9Sstevel@tonic-gate session_by_pid(pid_t pid)
16717c478bd9Sstevel@tonic-gate {
16727c478bd9Sstevel@tonic-gate 	int i;
16737c478bd9Sstevel@tonic-gate 	debug("session_by_pid: pid %ld", (long)pid);
16747c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
16757c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
16767c478bd9Sstevel@tonic-gate 		if (s->used && s->pid == pid)
16777c478bd9Sstevel@tonic-gate 			return s;
16787c478bd9Sstevel@tonic-gate 	}
16797c478bd9Sstevel@tonic-gate 	error("session_by_pid: unknown pid %ld", (long)pid);
16807c478bd9Sstevel@tonic-gate 	session_dump();
16817c478bd9Sstevel@tonic-gate 	return NULL;
16827c478bd9Sstevel@tonic-gate }
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate static int
16857c478bd9Sstevel@tonic-gate session_window_change_req(Session *s)
16867c478bd9Sstevel@tonic-gate {
16877c478bd9Sstevel@tonic-gate 	s->col = packet_get_int();
16887c478bd9Sstevel@tonic-gate 	s->row = packet_get_int();
16897c478bd9Sstevel@tonic-gate 	s->xpixel = packet_get_int();
16907c478bd9Sstevel@tonic-gate 	s->ypixel = packet_get_int();
16917c478bd9Sstevel@tonic-gate 	packet_check_eom();
16927c478bd9Sstevel@tonic-gate 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
16937c478bd9Sstevel@tonic-gate 	return 1;
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate static int
16977c478bd9Sstevel@tonic-gate session_pty_req(Session *s)
16987c478bd9Sstevel@tonic-gate {
16997c478bd9Sstevel@tonic-gate 	u_int len;
17007c478bd9Sstevel@tonic-gate 	int n_bytes;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	if (no_pty_flag) {
17037c478bd9Sstevel@tonic-gate 		debug("Allocating a pty not permitted for this authentication.");
17047c478bd9Sstevel@tonic-gate 		return 0;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1) {
17077c478bd9Sstevel@tonic-gate 		packet_disconnect("Protocol error: you already have a pty.");
17087c478bd9Sstevel@tonic-gate 		return 0;
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 	/* Get the time and hostname when the user last logged in. */
17117c478bd9Sstevel@tonic-gate 	if (options.print_lastlog) {
17127c478bd9Sstevel@tonic-gate 		s->hostname[0] = '\0';
17137c478bd9Sstevel@tonic-gate 		s->last_login_time = get_last_login_time(s->pw->pw_uid,
17147c478bd9Sstevel@tonic-gate 		    s->pw->pw_name, s->hostname, sizeof(s->hostname));
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 		/*
17177c478bd9Sstevel@tonic-gate 		 * PAM may update the last login date.
17187c478bd9Sstevel@tonic-gate 		 *
17197c478bd9Sstevel@tonic-gate 		 * Ideally PAM would also show the last login date as a
17207c478bd9Sstevel@tonic-gate 		 * PAM_TEXT_INFO conversation message, and then we could just
17217c478bd9Sstevel@tonic-gate 		 * always force the use of keyboard-interactive just so we can
17227c478bd9Sstevel@tonic-gate 		 * pass any such PAM prompts and messages from the account and
17237c478bd9Sstevel@tonic-gate 		 * session stacks, but skip pam_authenticate() if other userauth
17247c478bd9Sstevel@tonic-gate 		 * has succeeded and the user's password isn't expired.
17257c478bd9Sstevel@tonic-gate 		 *
17267c478bd9Sstevel@tonic-gate 		 * Unfortunately this depends on support for keyboard-
17277c478bd9Sstevel@tonic-gate 		 * interactive in the client, and support for lastlog messages
17287c478bd9Sstevel@tonic-gate 		 * in some PAM module.
17297c478bd9Sstevel@tonic-gate 		 *
17307c478bd9Sstevel@tonic-gate 		 * As it is Solaris updates the lastlog in PAM, but does
17317c478bd9Sstevel@tonic-gate 		 * not show the lastlog date in PAM.  If and when this state of
17327c478bd9Sstevel@tonic-gate 		 * affairs changes this hack can be reconsidered, and, maybe,
17337c478bd9Sstevel@tonic-gate 		 * removed.
17347c478bd9Sstevel@tonic-gate 		 *
17357c478bd9Sstevel@tonic-gate 		 * So we're stuck with a crude hack: get the lastlog
17367c478bd9Sstevel@tonic-gate 		 * time before calling pam_open_session() and store it
17377c478bd9Sstevel@tonic-gate 		 * in the Authctxt and then use it here once.  After
17387c478bd9Sstevel@tonic-gate 		 * that, if the client opens any more pty sessions we'll
17397c478bd9Sstevel@tonic-gate 		 * show the last lastlog entry since userauth.
17407c478bd9Sstevel@tonic-gate 		 */
17417c478bd9Sstevel@tonic-gate 		if (s->authctxt != NULL && s->authctxt->last_login_time > 0) {
17427c478bd9Sstevel@tonic-gate 			s->last_login_time = s->authctxt->last_login_time;
17437c478bd9Sstevel@tonic-gate 			(void) strlcpy(s->hostname,
17447c478bd9Sstevel@tonic-gate 				       s->authctxt->last_login_host,
17457c478bd9Sstevel@tonic-gate 				       sizeof(s->hostname));
17467c478bd9Sstevel@tonic-gate 			s->authctxt->last_login_time = 0;
17477c478bd9Sstevel@tonic-gate 			s->authctxt->last_login_host[0] = '\0';
17487c478bd9Sstevel@tonic-gate 		}
17497c478bd9Sstevel@tonic-gate 	}
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	s->term = packet_get_string(&len);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	if (compat20) {
17547c478bd9Sstevel@tonic-gate 		s->col = packet_get_int();
17557c478bd9Sstevel@tonic-gate 		s->row = packet_get_int();
17567c478bd9Sstevel@tonic-gate 	} else {
17577c478bd9Sstevel@tonic-gate 		s->row = packet_get_int();
17587c478bd9Sstevel@tonic-gate 		s->col = packet_get_int();
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 	s->xpixel = packet_get_int();
17617c478bd9Sstevel@tonic-gate 	s->ypixel = packet_get_int();
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	if (strcmp(s->term, "") == 0) {
17647c478bd9Sstevel@tonic-gate 		xfree(s->term);
17657c478bd9Sstevel@tonic-gate 		s->term = NULL;
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/* Allocate a pty and open it. */
17697c478bd9Sstevel@tonic-gate 	debug("Allocating pty.");
17707c478bd9Sstevel@tonic-gate 	if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
17717c478bd9Sstevel@tonic-gate 		if (s->term)
17727c478bd9Sstevel@tonic-gate 			xfree(s->term);
17737c478bd9Sstevel@tonic-gate 		s->term = NULL;
17747c478bd9Sstevel@tonic-gate 		s->ptyfd = -1;
17757c478bd9Sstevel@tonic-gate 		s->ttyfd = -1;
17767c478bd9Sstevel@tonic-gate 		error("session_pty_req: session %d alloc failed", s->self);
17777c478bd9Sstevel@tonic-gate 		return 0;
17787c478bd9Sstevel@tonic-gate 	}
17797c478bd9Sstevel@tonic-gate 	debug("session_pty_req: session %d alloc %s", s->self, s->tty);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	/* for SSH1 the tty modes length is not given */
17827c478bd9Sstevel@tonic-gate 	if (!compat20)
17837c478bd9Sstevel@tonic-gate 		n_bytes = packet_remaining();
17847c478bd9Sstevel@tonic-gate 	tty_parse_modes(s->ttyfd, &n_bytes);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/*
17877c478bd9Sstevel@tonic-gate 	 * Add a cleanup function to clear the utmp entry and record logout
17887c478bd9Sstevel@tonic-gate 	 * time in case we call fatal() (e.g., the connection gets closed).
17897c478bd9Sstevel@tonic-gate 	 */
17907c478bd9Sstevel@tonic-gate 	fatal_add_cleanup(session_pty_cleanup, (void *)s);
17917c478bd9Sstevel@tonic-gate 	if (!use_privsep)
17927c478bd9Sstevel@tonic-gate 		pty_setowner(s->pw, s->tty);
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	/* Set window size from the packet. */
17957c478bd9Sstevel@tonic-gate 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	packet_check_eom();
17987c478bd9Sstevel@tonic-gate 	session_proctitle(s);
17997c478bd9Sstevel@tonic-gate 	return 1;
18007c478bd9Sstevel@tonic-gate }
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate static int
18037c478bd9Sstevel@tonic-gate session_subsystem_req(Session *s)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	struct stat st;
18067c478bd9Sstevel@tonic-gate 	u_int len;
18077c478bd9Sstevel@tonic-gate 	int success = 0;
18087c478bd9Sstevel@tonic-gate 	char *cmd, *subsys = packet_get_string(&len);
18097c478bd9Sstevel@tonic-gate 	int i;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	packet_check_eom();
18127c478bd9Sstevel@tonic-gate 	log("subsystem request for %.100s", subsys);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	for (i = 0; i < options.num_subsystems; i++) {
18157c478bd9Sstevel@tonic-gate 		if (strcmp(subsys, options.subsystem_name[i]) == 0) {
18167c478bd9Sstevel@tonic-gate 			cmd = options.subsystem_command[i];
18177c478bd9Sstevel@tonic-gate 			if (stat(cmd, &st) < 0) {
18187c478bd9Sstevel@tonic-gate 				error("subsystem: cannot stat %s: %s", cmd,
18197c478bd9Sstevel@tonic-gate 				    strerror(errno));
18207c478bd9Sstevel@tonic-gate 				break;
18217c478bd9Sstevel@tonic-gate 			}
18227c478bd9Sstevel@tonic-gate 			debug("subsystem: exec() %s", cmd);
18237c478bd9Sstevel@tonic-gate 			s->is_subsystem = 1;
18247c478bd9Sstevel@tonic-gate 			do_exec(s, cmd);
18257c478bd9Sstevel@tonic-gate 			success = 1;
18267c478bd9Sstevel@tonic-gate 			break;
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if (!success)
18317c478bd9Sstevel@tonic-gate 		log("subsystem request for %.100s failed, subsystem not found",
18327c478bd9Sstevel@tonic-gate 		    subsys);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	xfree(subsys);
18357c478bd9Sstevel@tonic-gate 	return success;
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate static int
18397c478bd9Sstevel@tonic-gate session_x11_req(Session *s)
18407c478bd9Sstevel@tonic-gate {
18417c478bd9Sstevel@tonic-gate 	int success;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	s->single_connection = packet_get_char();
18447c478bd9Sstevel@tonic-gate 	s->auth_proto = packet_get_string(NULL);
18457c478bd9Sstevel@tonic-gate 	s->auth_data = packet_get_string(NULL);
18467c478bd9Sstevel@tonic-gate 	s->screen = packet_get_int();
18477c478bd9Sstevel@tonic-gate 	packet_check_eom();
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	success = session_setup_x11fwd(s);
18507c478bd9Sstevel@tonic-gate 	if (!success) {
18517c478bd9Sstevel@tonic-gate 		xfree(s->auth_proto);
18527c478bd9Sstevel@tonic-gate 		xfree(s->auth_data);
18537c478bd9Sstevel@tonic-gate 		s->auth_proto = NULL;
18547c478bd9Sstevel@tonic-gate 		s->auth_data = NULL;
18557c478bd9Sstevel@tonic-gate 	}
18567c478bd9Sstevel@tonic-gate 	return success;
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate static int
18607c478bd9Sstevel@tonic-gate session_shell_req(Session *s)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	packet_check_eom();
18637c478bd9Sstevel@tonic-gate 	do_exec(s, NULL);
18647c478bd9Sstevel@tonic-gate 	return 1;
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate static int
18687c478bd9Sstevel@tonic-gate session_exec_req(Session *s)
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	u_int len;
18717c478bd9Sstevel@tonic-gate 	char *command = packet_get_string(&len);
18727c478bd9Sstevel@tonic-gate 	packet_check_eom();
18737c478bd9Sstevel@tonic-gate 	do_exec(s, command);
18747c478bd9Sstevel@tonic-gate 	xfree(command);
18757c478bd9Sstevel@tonic-gate 	return 1;
18767c478bd9Sstevel@tonic-gate }
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate static int
18797c478bd9Sstevel@tonic-gate session_auth_agent_req(Session *s)
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	static int called = 0;
18827c478bd9Sstevel@tonic-gate 	packet_check_eom();
18837c478bd9Sstevel@tonic-gate 	if (no_agent_forwarding_flag) {
18847c478bd9Sstevel@tonic-gate 		debug("session_auth_agent_req: no_agent_forwarding_flag");
18857c478bd9Sstevel@tonic-gate 		return 0;
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 	if (called) {
18887c478bd9Sstevel@tonic-gate 		return 0;
18897c478bd9Sstevel@tonic-gate 	} else {
18907c478bd9Sstevel@tonic-gate 		called = 1;
18917c478bd9Sstevel@tonic-gate 		return auth_input_request_forwarding(s->pw);
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate static int
18967c478bd9Sstevel@tonic-gate session_loc_env_check(char *var, char *val)
18977c478bd9Sstevel@tonic-gate {
18987c478bd9Sstevel@tonic-gate 	char *current;
18997c478bd9Sstevel@tonic-gate 	int cat, ret;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	if (strcmp(var, "LANG") == 0)
19027c478bd9Sstevel@tonic-gate 		cat = LC_ALL;
19037c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_ALL") == 0)
19047c478bd9Sstevel@tonic-gate 		cat = LC_ALL;
19057c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_CTYPE") == 0)
19067c478bd9Sstevel@tonic-gate 		cat = LC_CTYPE;
19077c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_COLLATE") == 0)
19087c478bd9Sstevel@tonic-gate 		cat = LC_COLLATE;
19097c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_TIME") == 0)
19107c478bd9Sstevel@tonic-gate 		cat = LC_TIME;
19117c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_NUMERIC") == 0)
19127c478bd9Sstevel@tonic-gate 		cat = LC_NUMERIC;
19137c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_MONETARY") == 0)
19147c478bd9Sstevel@tonic-gate 		cat = LC_MONETARY;
19157c478bd9Sstevel@tonic-gate 	else if (strcmp(var, "LC_MESSAGES") == 0)
19167c478bd9Sstevel@tonic-gate 		cat = LC_MESSAGES;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	if ((current = setlocale(cat, NULL)) != NULL)
19197c478bd9Sstevel@tonic-gate 		current = xstrdup(current);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	ret = (setlocale(cat, val) != NULL);
19227c478bd9Sstevel@tonic-gate 	(void) setlocale(cat, current);
19237c478bd9Sstevel@tonic-gate 	return (ret);
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate static int
19277c478bd9Sstevel@tonic-gate session_env_req(Session *s)
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	Channel *c;
19307c478bd9Sstevel@tonic-gate 	char *var, *val, *e;
19317c478bd9Sstevel@tonic-gate 	char **p;
19327c478bd9Sstevel@tonic-gate 	size_t len;
19337c478bd9Sstevel@tonic-gate 	int ret = 0;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	/* Get var/val from the rest of this packet */
19367c478bd9Sstevel@tonic-gate 	var = packet_get_string(NULL);
19377c478bd9Sstevel@tonic-gate 	val = packet_get_string(NULL);
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	/*
19407c478bd9Sstevel@tonic-gate 	 * We'll need the channel ID for the packet_send_debug messages,
19417c478bd9Sstevel@tonic-gate 	 * so get it now.
19427c478bd9Sstevel@tonic-gate 	 */
19437c478bd9Sstevel@tonic-gate 	if ((c = channel_lookup(s->chanid)) == NULL)
19447c478bd9Sstevel@tonic-gate 		goto done;	/* shouldn't happen! */
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	debug2("Received request for environment variable %s=%s", var, val);
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	/* For now allow only LANG and LC_* */
19497c478bd9Sstevel@tonic-gate 	if (strcmp(var, "LANG") != 0 && strncmp(var, "LC_", 3) != 0) {
19507c478bd9Sstevel@tonic-gate 		debug2("Rejecting request for environment variable %s", var);
19517c478bd9Sstevel@tonic-gate 		goto done;
19527c478bd9Sstevel@tonic-gate 	}
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (!session_loc_env_check(var, val)) {
19557c478bd9Sstevel@tonic-gate 		packet_send_debug(gettext("Missing locale support for %s=%s"),
19567c478bd9Sstevel@tonic-gate 			var, val);
19577c478bd9Sstevel@tonic-gate 		goto done;
19587c478bd9Sstevel@tonic-gate 	}
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	packet_send_debug(gettext("Channel %d set: %s=%s"), c->remote_id,
19617c478bd9Sstevel@tonic-gate 		var, val);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	/*
19647c478bd9Sstevel@tonic-gate 	 * Always append new environment variables without regard to old
19657c478bd9Sstevel@tonic-gate 	 * ones being overriden.  The way these are actually added to
19667c478bd9Sstevel@tonic-gate 	 * the environment of the session process later settings
19677c478bd9Sstevel@tonic-gate 	 * override earlier ones; see copy_environment().
19687c478bd9Sstevel@tonic-gate 	 */
19697c478bd9Sstevel@tonic-gate 	if (s->env == NULL) {
19707c478bd9Sstevel@tonic-gate 		char **env;
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 		env = xmalloc(sizeof (char **) * 2);
19737c478bd9Sstevel@tonic-gate 		memset(env, 0, sizeof (char **) * 2);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 		s->env = env;
19767c478bd9Sstevel@tonic-gate 		p = env;
19777c478bd9Sstevel@tonic-gate 	} else {
19787c478bd9Sstevel@tonic-gate 		for (p = s->env; *p != NULL ; p++);
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 		s->env = xrealloc(s->env, (p - s->env + 2) * sizeof (char **));
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		for (p = s->env; *p != NULL ; p++);
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	len = snprintf(NULL, 0, "%s=%s", var, val);
19867c478bd9Sstevel@tonic-gate 	e = xmalloc(len + 1);
19877c478bd9Sstevel@tonic-gate 	(void) snprintf(e, len + 1, "%s=%s", var, val);
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	(*p++) = e;
19907c478bd9Sstevel@tonic-gate 	*p = NULL;
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	ret = 1;
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate done:
19957c478bd9Sstevel@tonic-gate 	xfree(var);
19967c478bd9Sstevel@tonic-gate 	xfree(val);
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	return (ret);
19997c478bd9Sstevel@tonic-gate }
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate static void
20027c478bd9Sstevel@tonic-gate session_free_env(char ***envp)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate 	char **env, **p;
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	if (envp == NULL || *envp == NULL)
20077c478bd9Sstevel@tonic-gate 		return;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	env = *envp;
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	*envp = NULL;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	for (p = env; *p != NULL; p++)
20147c478bd9Sstevel@tonic-gate 		xfree(*p);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	xfree(env);
20177c478bd9Sstevel@tonic-gate }
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate int
20207c478bd9Sstevel@tonic-gate session_input_channel_req(Channel *c, const char *rtype)
20217c478bd9Sstevel@tonic-gate {
20227c478bd9Sstevel@tonic-gate 	int success = 0;
20237c478bd9Sstevel@tonic-gate 	Session *s;
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	if ((s = session_by_channel(c->self)) == NULL) {
20267c478bd9Sstevel@tonic-gate 		log("session_input_channel_req: no session %d req %.100s",
20277c478bd9Sstevel@tonic-gate 		    c->self, rtype);
20287c478bd9Sstevel@tonic-gate 		return 0;
20297c478bd9Sstevel@tonic-gate 	}
20307c478bd9Sstevel@tonic-gate 	debug("session_input_channel_req: session %d req %s", s->self, rtype);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	/*
20337c478bd9Sstevel@tonic-gate 	 * a session is in LARVAL state until a shell, a command
20347c478bd9Sstevel@tonic-gate 	 * or a subsystem is executed
20357c478bd9Sstevel@tonic-gate 	 */
20367c478bd9Sstevel@tonic-gate 	if (c->type == SSH_CHANNEL_LARVAL) {
20377c478bd9Sstevel@tonic-gate 		if (strcmp(rtype, "shell") == 0) {
20387c478bd9Sstevel@tonic-gate 			success = session_shell_req(s);
20397c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "exec") == 0) {
20407c478bd9Sstevel@tonic-gate 			success = session_exec_req(s);
20417c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "pty-req") == 0) {
20427c478bd9Sstevel@tonic-gate 			success =  session_pty_req(s);
20437c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "x11-req") == 0) {
20447c478bd9Sstevel@tonic-gate 			success = session_x11_req(s);
20457c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
20467c478bd9Sstevel@tonic-gate 			success = session_auth_agent_req(s);
20477c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "subsystem") == 0) {
20487c478bd9Sstevel@tonic-gate 			success = session_subsystem_req(s);
20497c478bd9Sstevel@tonic-gate 		} else if (strcmp(rtype, "env") == 0) {
20507c478bd9Sstevel@tonic-gate 			success = session_env_req(s);
20517c478bd9Sstevel@tonic-gate 		}
20527c478bd9Sstevel@tonic-gate 	}
20537c478bd9Sstevel@tonic-gate 	if (strcmp(rtype, "window-change") == 0) {
20547c478bd9Sstevel@tonic-gate 		success = session_window_change_req(s);
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 	return success;
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate void
20607c478bd9Sstevel@tonic-gate session_set_fds(Session *s, int fdin, int fdout, int fderr)
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate 	if (!compat20)
20637c478bd9Sstevel@tonic-gate 		fatal("session_set_fds: called for proto != 2.0");
20647c478bd9Sstevel@tonic-gate 	/*
20657c478bd9Sstevel@tonic-gate 	 * now that have a child and a pipe to the child,
20667c478bd9Sstevel@tonic-gate 	 * we can activate our channel and register the fd's
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 	if (s->chanid == -1)
20697c478bd9Sstevel@tonic-gate 		fatal("no channel for session %d", s->self);
20707c478bd9Sstevel@tonic-gate 	channel_set_fds(s->chanid,
20717c478bd9Sstevel@tonic-gate 	    fdout, fdin, fderr,
20727c478bd9Sstevel@tonic-gate 	    fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
20737c478bd9Sstevel@tonic-gate 	    1,
20747c478bd9Sstevel@tonic-gate 	    CHAN_SES_WINDOW_DEFAULT);
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate /*
20787c478bd9Sstevel@tonic-gate  * Function to perform pty cleanup. Also called if we get aborted abnormally
20797c478bd9Sstevel@tonic-gate  * (e.g., due to a dropped connection).
20807c478bd9Sstevel@tonic-gate  */
20817c478bd9Sstevel@tonic-gate void
20827c478bd9Sstevel@tonic-gate session_pty_cleanup2(void *session)
20837c478bd9Sstevel@tonic-gate {
20847c478bd9Sstevel@tonic-gate 	Session *s = session;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if (s == NULL) {
20877c478bd9Sstevel@tonic-gate 		error("session_pty_cleanup: no session");
20887c478bd9Sstevel@tonic-gate 		return;
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 	if (s->ttyfd == -1)
20917c478bd9Sstevel@tonic-gate 		return;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate #ifdef USE_PAM
20967c478bd9Sstevel@tonic-gate 	session_do_pam(s, 0);
20977c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	/* Record that the user has logged out. */
21007c478bd9Sstevel@tonic-gate 	if (s->pid != 0) {
21017c478bd9Sstevel@tonic-gate 		debug3("Recording SSHv2 channel login in utmpx/wtmpx");
21027c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
21037c478bd9Sstevel@tonic-gate 		altprivsep_record_logout(s->pid);
21047c478bd9Sstevel@tonic-gate #else /* ALTPRIVSEP */
21057c478bd9Sstevel@tonic-gate 		record_logout(s->pid, s->tty, NULL, s->pw->pw_name);
21067c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
21077c478bd9Sstevel@tonic-gate 	}
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	/* Release the pseudo-tty. */
21107c478bd9Sstevel@tonic-gate 	if (getuid() == 0)
21117c478bd9Sstevel@tonic-gate 		pty_release(s->tty);
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	/*
21147c478bd9Sstevel@tonic-gate 	 * Close the server side of the socket pairs.  We must do this after
21157c478bd9Sstevel@tonic-gate 	 * the pty cleanup, so that another process doesn't get this pty
21167c478bd9Sstevel@tonic-gate 	 * while we're still cleaning up.
21177c478bd9Sstevel@tonic-gate 	 */
21187c478bd9Sstevel@tonic-gate 	if (close(s->ptymaster) < 0)
21197c478bd9Sstevel@tonic-gate 		error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	/* unlink pty from session */
21227c478bd9Sstevel@tonic-gate 	s->ttyfd = -1;
21237c478bd9Sstevel@tonic-gate }
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate void
21267c478bd9Sstevel@tonic-gate session_pty_cleanup(void *session)
21277c478bd9Sstevel@tonic-gate {
21287c478bd9Sstevel@tonic-gate 	PRIVSEP(session_pty_cleanup2(session));
21297c478bd9Sstevel@tonic-gate }
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate static char *
21327c478bd9Sstevel@tonic-gate sig2name(int sig)
21337c478bd9Sstevel@tonic-gate {
21347c478bd9Sstevel@tonic-gate #define SSH_SIG(x) if (sig == SIG ## x) return #x
21357c478bd9Sstevel@tonic-gate 	SSH_SIG(ABRT);
21367c478bd9Sstevel@tonic-gate 	SSH_SIG(ALRM);
21377c478bd9Sstevel@tonic-gate 	SSH_SIG(FPE);
21387c478bd9Sstevel@tonic-gate 	SSH_SIG(HUP);
21397c478bd9Sstevel@tonic-gate 	SSH_SIG(ILL);
21407c478bd9Sstevel@tonic-gate 	SSH_SIG(INT);
21417c478bd9Sstevel@tonic-gate 	SSH_SIG(KILL);
21427c478bd9Sstevel@tonic-gate 	SSH_SIG(PIPE);
21437c478bd9Sstevel@tonic-gate 	SSH_SIG(QUIT);
21447c478bd9Sstevel@tonic-gate 	SSH_SIG(SEGV);
21457c478bd9Sstevel@tonic-gate 	SSH_SIG(TERM);
21467c478bd9Sstevel@tonic-gate 	SSH_SIG(USR1);
21477c478bd9Sstevel@tonic-gate 	SSH_SIG(USR2);
21487c478bd9Sstevel@tonic-gate #undef	SSH_SIG
21497c478bd9Sstevel@tonic-gate 	return "SIG@openssh.com";
21507c478bd9Sstevel@tonic-gate }
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate static void
21537c478bd9Sstevel@tonic-gate session_exit_message(Session *s, int status)
21547c478bd9Sstevel@tonic-gate {
21557c478bd9Sstevel@tonic-gate 	Channel *c;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	if ((c = channel_lookup(s->chanid)) == NULL)
21587c478bd9Sstevel@tonic-gate 		fatal("session_exit_message: session %d: no channel %d",
21597c478bd9Sstevel@tonic-gate 		    s->self, s->chanid);
21607c478bd9Sstevel@tonic-gate 	debug("session_exit_message: session %d channel %d pid %ld",
21617c478bd9Sstevel@tonic-gate 	    s->self, s->chanid, (long)s->pid);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
21647c478bd9Sstevel@tonic-gate 		channel_request_start(s->chanid, "exit-status", 0);
21657c478bd9Sstevel@tonic-gate 		packet_put_int(WEXITSTATUS(status));
21667c478bd9Sstevel@tonic-gate 		packet_send();
21677c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
21687c478bd9Sstevel@tonic-gate 		channel_request_start(s->chanid, "exit-signal", 0);
21697c478bd9Sstevel@tonic-gate 		packet_put_cstring(sig2name(WTERMSIG(status)));
21707c478bd9Sstevel@tonic-gate #ifdef WCOREDUMP
21717c478bd9Sstevel@tonic-gate 		packet_put_char(WCOREDUMP(status));
21727c478bd9Sstevel@tonic-gate #else /* WCOREDUMP */
21737c478bd9Sstevel@tonic-gate 		packet_put_char(0);
21747c478bd9Sstevel@tonic-gate #endif /* WCOREDUMP */
21757c478bd9Sstevel@tonic-gate 		packet_put_cstring("");
21767c478bd9Sstevel@tonic-gate 		packet_put_cstring("");
21777c478bd9Sstevel@tonic-gate 		packet_send();
21787c478bd9Sstevel@tonic-gate 	} else {
21797c478bd9Sstevel@tonic-gate 		/* Some weird exit cause.  Just exit. */
21807c478bd9Sstevel@tonic-gate 		packet_disconnect("wait returned status %04x.", status);
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	/* Ok to close channel now */
21847c478bd9Sstevel@tonic-gate 	channel_set_wait_for_exit(s->chanid, 0);
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	/* disconnect channel */
21877c478bd9Sstevel@tonic-gate 	debug("session_exit_message: release channel %d", s->chanid);
21887c478bd9Sstevel@tonic-gate 	channel_cancel_cleanup(s->chanid);
21897c478bd9Sstevel@tonic-gate 	/*
21907c478bd9Sstevel@tonic-gate 	 * emulate a write failure with 'chan_write_failed', nobody will be
21917c478bd9Sstevel@tonic-gate 	 * interested in data we write.
21927c478bd9Sstevel@tonic-gate 	 * Note that we must not call 'chan_read_failed', since there could
21937c478bd9Sstevel@tonic-gate 	 * be some more data waiting in the pipe.
21947c478bd9Sstevel@tonic-gate 	 */
21957c478bd9Sstevel@tonic-gate 	if (c->ostate != CHAN_OUTPUT_CLOSED)
21967c478bd9Sstevel@tonic-gate 		chan_write_failed(c);
21977c478bd9Sstevel@tonic-gate 	s->chanid = -1;
21987c478bd9Sstevel@tonic-gate }
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate void
22017c478bd9Sstevel@tonic-gate session_close(Session *s)
22027c478bd9Sstevel@tonic-gate {
22037c478bd9Sstevel@tonic-gate 	debug("session_close: session %d pid %ld", s->self, (long)s->pid);
22047c478bd9Sstevel@tonic-gate 	if (s->ttyfd != -1) {
22057c478bd9Sstevel@tonic-gate 		fatal_remove_cleanup(session_pty_cleanup, (void *)s);
22067c478bd9Sstevel@tonic-gate 		session_pty_cleanup(s);
22077c478bd9Sstevel@tonic-gate 	}
22087c478bd9Sstevel@tonic-gate 	if (s->term)
22097c478bd9Sstevel@tonic-gate 		xfree(s->term);
22107c478bd9Sstevel@tonic-gate 	if (s->display)
22117c478bd9Sstevel@tonic-gate 		xfree(s->display);
22127c478bd9Sstevel@tonic-gate 	if (s->auth_display)
22137c478bd9Sstevel@tonic-gate 		xfree(s->auth_display);
22147c478bd9Sstevel@tonic-gate 	if (s->auth_data)
22157c478bd9Sstevel@tonic-gate 		xfree(s->auth_data);
22167c478bd9Sstevel@tonic-gate 	if (s->auth_proto)
22177c478bd9Sstevel@tonic-gate 		xfree(s->auth_proto);
22187c478bd9Sstevel@tonic-gate 	if (s->command)
22197c478bd9Sstevel@tonic-gate 		xfree(s->command);
22207c478bd9Sstevel@tonic-gate 	session_free_env(&s->env);
22217c478bd9Sstevel@tonic-gate 	s->used = 0;
22227c478bd9Sstevel@tonic-gate 	session_proctitle(s);
22237c478bd9Sstevel@tonic-gate }
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate void
22267c478bd9Sstevel@tonic-gate session_close_by_pid(pid_t pid, int status)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	Session *s = session_by_pid(pid);
22297c478bd9Sstevel@tonic-gate 	if (s == NULL) {
22307c478bd9Sstevel@tonic-gate 		debug("session_close_by_pid: no session for pid %ld",
22317c478bd9Sstevel@tonic-gate 		    (long)pid);
22327c478bd9Sstevel@tonic-gate 		return;
22337c478bd9Sstevel@tonic-gate 	}
22347c478bd9Sstevel@tonic-gate 	if (s->chanid != -1)
22357c478bd9Sstevel@tonic-gate 		session_exit_message(s, status);
22367c478bd9Sstevel@tonic-gate 	session_close(s);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate /*
22407c478bd9Sstevel@tonic-gate  * this is called when a channel dies before
22417c478bd9Sstevel@tonic-gate  * the session 'child' itself dies
22427c478bd9Sstevel@tonic-gate  */
22437c478bd9Sstevel@tonic-gate void
22447c478bd9Sstevel@tonic-gate session_close_by_channel(int id, void *arg)
22457c478bd9Sstevel@tonic-gate {
22467c478bd9Sstevel@tonic-gate 	Session *s = session_by_channel(id);
22477c478bd9Sstevel@tonic-gate 	if (s == NULL) {
22487c478bd9Sstevel@tonic-gate 		debug("session_close_by_channel: no session for id %d", id);
22497c478bd9Sstevel@tonic-gate 		return;
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 	debug("session_close_by_channel: channel %d child %ld",
22527c478bd9Sstevel@tonic-gate 	    id, (long)s->pid);
22537c478bd9Sstevel@tonic-gate 	if (s->pid != 0) {
22547c478bd9Sstevel@tonic-gate 		debug("session_close_by_channel: channel %d: has child", id);
22557c478bd9Sstevel@tonic-gate 		/*
22567c478bd9Sstevel@tonic-gate 		 * delay detach of session, but release pty, since
22577c478bd9Sstevel@tonic-gate 		 * the fd's to the child are already closed
22587c478bd9Sstevel@tonic-gate 		 */
22597c478bd9Sstevel@tonic-gate 		if (s->ttyfd != -1) {
22607c478bd9Sstevel@tonic-gate 			fatal_remove_cleanup(session_pty_cleanup, (void *)s);
22617c478bd9Sstevel@tonic-gate 			session_pty_cleanup(s);
22627c478bd9Sstevel@tonic-gate 		}
22637c478bd9Sstevel@tonic-gate 		return;
22647c478bd9Sstevel@tonic-gate 	}
22657c478bd9Sstevel@tonic-gate 	/* detach by removing callback */
22667c478bd9Sstevel@tonic-gate 	channel_cancel_cleanup(s->chanid);
22677c478bd9Sstevel@tonic-gate 	s->chanid = -1;
22687c478bd9Sstevel@tonic-gate 	session_close(s);
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate void
22727c478bd9Sstevel@tonic-gate session_destroy_all(void (*closefunc)(Session *))
22737c478bd9Sstevel@tonic-gate {
22747c478bd9Sstevel@tonic-gate 	int i;
22757c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
22767c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
22777c478bd9Sstevel@tonic-gate 		if (s->used) {
22787c478bd9Sstevel@tonic-gate 			if (closefunc != NULL)
22797c478bd9Sstevel@tonic-gate 				closefunc(s);
22807c478bd9Sstevel@tonic-gate 			else
22817c478bd9Sstevel@tonic-gate 				session_close(s);
22827c478bd9Sstevel@tonic-gate 		}
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate static char *
22877c478bd9Sstevel@tonic-gate session_tty_list(void)
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate 	static char buf[1024];
22907c478bd9Sstevel@tonic-gate 	int i;
22917c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
22927c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SESSIONS; i++) {
22937c478bd9Sstevel@tonic-gate 		Session *s = &sessions[i];
22947c478bd9Sstevel@tonic-gate 		if (s->used && s->ttyfd != -1) {
22957c478bd9Sstevel@tonic-gate 			if (buf[0] != '\0')
22967c478bd9Sstevel@tonic-gate 				strlcat(buf, ",", sizeof buf);
22977c478bd9Sstevel@tonic-gate 			strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate 	if (buf[0] == '\0')
23017c478bd9Sstevel@tonic-gate 		strlcpy(buf, "notty", sizeof buf);
23027c478bd9Sstevel@tonic-gate 	return buf;
23037c478bd9Sstevel@tonic-gate }
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate void
23067c478bd9Sstevel@tonic-gate session_proctitle(Session *s)
23077c478bd9Sstevel@tonic-gate {
23087c478bd9Sstevel@tonic-gate 	if (s->pw == NULL)
23097c478bd9Sstevel@tonic-gate 		error("no user for session %d", s->self);
23107c478bd9Sstevel@tonic-gate 	else
23117c478bd9Sstevel@tonic-gate 		setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
23127c478bd9Sstevel@tonic-gate }
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate int
23157c478bd9Sstevel@tonic-gate session_setup_x11fwd(Session *s)
23167c478bd9Sstevel@tonic-gate {
23177c478bd9Sstevel@tonic-gate 	struct stat st;
23187c478bd9Sstevel@tonic-gate 	char display[512], auth_display[512];
23197c478bd9Sstevel@tonic-gate 	char hostname[MAXHOSTNAMELEN];
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	if (no_x11_forwarding_flag) {
23227c478bd9Sstevel@tonic-gate 		packet_send_debug("X11 forwarding disabled in user configuration file.");
23237c478bd9Sstevel@tonic-gate 		return 0;
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 	if (!options.x11_forwarding) {
23267c478bd9Sstevel@tonic-gate 		debug("X11 forwarding disabled in server configuration file.");
23277c478bd9Sstevel@tonic-gate 		return 0;
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 	if (!options.xauth_location ||
23307c478bd9Sstevel@tonic-gate 	    (stat(options.xauth_location, &st) == -1)) {
23317c478bd9Sstevel@tonic-gate 		packet_send_debug("No xauth program; cannot forward with spoofing.");
23327c478bd9Sstevel@tonic-gate 		return 0;
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate 	if (options.use_login) {
23357c478bd9Sstevel@tonic-gate 		packet_send_debug("X11 forwarding disabled; "
23367c478bd9Sstevel@tonic-gate 		    "not compatible with UseLogin=yes.");
23377c478bd9Sstevel@tonic-gate 		return 0;
23387c478bd9Sstevel@tonic-gate 	}
23397c478bd9Sstevel@tonic-gate 	if (s->display != NULL) {
23407c478bd9Sstevel@tonic-gate 		debug("X11 display already set.");
23417c478bd9Sstevel@tonic-gate 		return 0;
23427c478bd9Sstevel@tonic-gate 	}
23437c478bd9Sstevel@tonic-gate 	if (x11_create_display_inet(options.x11_display_offset,
23447c478bd9Sstevel@tonic-gate 	    options.x11_use_localhost, s->single_connection,
23457c478bd9Sstevel@tonic-gate 	    &s->display_number) == -1) {
23467c478bd9Sstevel@tonic-gate 		debug("x11_create_display_inet failed.");
23477c478bd9Sstevel@tonic-gate 		return 0;
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	/* Set up a suitable value for the DISPLAY variable. */
23517c478bd9Sstevel@tonic-gate 	if (gethostname(hostname, sizeof(hostname)) < 0)
23527c478bd9Sstevel@tonic-gate 		fatal("gethostname: %.100s", strerror(errno));
23537c478bd9Sstevel@tonic-gate 	/*
23547c478bd9Sstevel@tonic-gate 	 * auth_display must be used as the displayname when the
23557c478bd9Sstevel@tonic-gate 	 * authorization entry is added with xauth(1).  This will be
23567c478bd9Sstevel@tonic-gate 	 * different than the DISPLAY string for localhost displays.
23577c478bd9Sstevel@tonic-gate 	 */
23587c478bd9Sstevel@tonic-gate 	if (options.x11_use_localhost) {
23597c478bd9Sstevel@tonic-gate 		snprintf(display, sizeof display, "localhost:%u.%u",
23607c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
23617c478bd9Sstevel@tonic-gate 		snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
23627c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
23637c478bd9Sstevel@tonic-gate 		s->display = xstrdup(display);
23647c478bd9Sstevel@tonic-gate 		s->auth_display = xstrdup(auth_display);
23657c478bd9Sstevel@tonic-gate 	} else {
23667c478bd9Sstevel@tonic-gate #ifdef IPADDR_IN_DISPLAY
23677c478bd9Sstevel@tonic-gate 		struct hostent *he;
23687c478bd9Sstevel@tonic-gate 		struct in_addr my_addr;
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 		he = gethostbyname(hostname);
23717c478bd9Sstevel@tonic-gate 		if (he == NULL) {
23727c478bd9Sstevel@tonic-gate 			error("Can't get IP address for X11 DISPLAY.");
23737c478bd9Sstevel@tonic-gate 			packet_send_debug("Can't get IP address for X11 DISPLAY.");
23747c478bd9Sstevel@tonic-gate 			return 0;
23757c478bd9Sstevel@tonic-gate 		}
23767c478bd9Sstevel@tonic-gate 		memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
23777c478bd9Sstevel@tonic-gate 		snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
23787c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
23797c478bd9Sstevel@tonic-gate #else
23807c478bd9Sstevel@tonic-gate 		snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
23817c478bd9Sstevel@tonic-gate 		    s->display_number, s->screen);
23827c478bd9Sstevel@tonic-gate #endif
23837c478bd9Sstevel@tonic-gate 		s->display = xstrdup(display);
23847c478bd9Sstevel@tonic-gate 		s->auth_display = xstrdup(display);
23857c478bd9Sstevel@tonic-gate 	}
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	return 1;
23887c478bd9Sstevel@tonic-gate }
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate #ifdef USE_PAM
23917c478bd9Sstevel@tonic-gate int session_do_pam_conv(int, struct pam_message **,
23927c478bd9Sstevel@tonic-gate 			struct pam_response **, void *);
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate static struct pam_conv session_pam_conv = {
23957c478bd9Sstevel@tonic-gate 	session_do_pam_conv,
23967c478bd9Sstevel@tonic-gate 	NULL
23977c478bd9Sstevel@tonic-gate };
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate static void
24007c478bd9Sstevel@tonic-gate session_do_pam(Session *s, int do_open)
24017c478bd9Sstevel@tonic-gate {
24027c478bd9Sstevel@tonic-gate 	int pam_retval;
24037c478bd9Sstevel@tonic-gate 	char *where, *old_tty, *old_tty_copy = NULL;
24047c478bd9Sstevel@tonic-gate 	struct pam_conv old_conv, *old_conv_ptr;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	if (!s || !s->authctxt || !s->authctxt->pam || !s->authctxt->pam->h)
24077c478bd9Sstevel@tonic-gate 		return;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	/* Save current PAM item values */
24107c478bd9Sstevel@tonic-gate 	where = "getting PAM_CONV";
24117c478bd9Sstevel@tonic-gate 	pam_retval = pam_get_item(s->authctxt->pam->h, PAM_CONV,
24127c478bd9Sstevel@tonic-gate 				  (void **) &old_conv_ptr);
24137c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
24147c478bd9Sstevel@tonic-gate 		goto done;
24157c478bd9Sstevel@tonic-gate 	old_conv = *old_conv_ptr;
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	where = "getting PAM_TTY";
24187c478bd9Sstevel@tonic-gate 	pam_retval = pam_get_item(s->authctxt->pam->h, PAM_TTY,
24197c478bd9Sstevel@tonic-gate 				  (void **) &old_tty);
24207c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
24217c478bd9Sstevel@tonic-gate 		goto done;
24227c478bd9Sstevel@tonic-gate 	old_tty_copy = xstrdup(old_tty);
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	/* Change PAM_TTY and PAM_CONV items */
24257c478bd9Sstevel@tonic-gate 	where = "setting PAM_TTY";
24267c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, s->tty);
24277c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
24287c478bd9Sstevel@tonic-gate 		goto done;
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	where = "setting PAM_CONV";
24317c478bd9Sstevel@tonic-gate 	session_pam_conv.appdata_ptr = s;
24327c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h,
24337c478bd9Sstevel@tonic-gate 				  PAM_CONV, &session_pam_conv);
24347c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
24357c478bd9Sstevel@tonic-gate 		goto done;
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	/* Call pam_open/close_session() */
24387c478bd9Sstevel@tonic-gate 	if (do_open) {
24397c478bd9Sstevel@tonic-gate 		where = "calling pam_open_session()";
24407c478bd9Sstevel@tonic-gate 		pam_retval = pam_open_session(s->authctxt->pam->h, 0);
24417c478bd9Sstevel@tonic-gate 	}
24427c478bd9Sstevel@tonic-gate 	else {
24437c478bd9Sstevel@tonic-gate 		where = "calling pam_close_session()";
24447c478bd9Sstevel@tonic-gate 		pam_retval = pam_close_session(s->authctxt->pam->h, 0);
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	/* Reset PAM_TTY and PAM_CONV items to previous values */
24487c478bd9Sstevel@tonic-gate 	where = "setting PAM_TTY";
24497c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, old_tty_copy);
24507c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
24517c478bd9Sstevel@tonic-gate 		goto done;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	where = "setting PAM_CONV";
24547c478bd9Sstevel@tonic-gate 	pam_retval = pam_set_item(s->authctxt->pam->h, PAM_CONV, &old_conv);
24557c478bd9Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
24567c478bd9Sstevel@tonic-gate 		goto done;
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	session_pam_conv.appdata_ptr = NULL;
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate done:
24617c478bd9Sstevel@tonic-gate 	if (old_tty_copy)
24627c478bd9Sstevel@tonic-gate 		xfree(old_tty_copy);
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	if (pam_retval == PAM_SUCCESS)
24657c478bd9Sstevel@tonic-gate 		return;
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	/* fatal()? probably not... */
24687c478bd9Sstevel@tonic-gate 	log("PAM failed[%d] while %s: %s", pam_retval, where,
24697c478bd9Sstevel@tonic-gate 	    PAM_STRERROR(s->authctxt->pam->h, pam_retval));
24707c478bd9Sstevel@tonic-gate }
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate int
24737c478bd9Sstevel@tonic-gate session_do_pam_conv(int num_prompts,
24747c478bd9Sstevel@tonic-gate 		    struct pam_message **prompts,
24757c478bd9Sstevel@tonic-gate 		    struct pam_response **resp,
24767c478bd9Sstevel@tonic-gate 		    void *app_data)
24777c478bd9Sstevel@tonic-gate {
24787c478bd9Sstevel@tonic-gate 	Session *s = (Session *) app_data;
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 	struct pam_response *reply;
24817c478bd9Sstevel@tonic-gate 	int count;
24827c478bd9Sstevel@tonic-gate 	char *prompt;
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	if (channel_lookup(s->chanid) == NULL)
24857c478bd9Sstevel@tonic-gate 		return PAM_CONV_ERR;
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	/* PAM will free this later */
24887c478bd9Sstevel@tonic-gate 	reply = xmalloc(num_prompts * sizeof(*reply));
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	(void) memset(reply, 0, num_prompts * sizeof(*reply));
24917c478bd9Sstevel@tonic-gate 	for (count = 0; count < num_prompts; count++) {
24927c478bd9Sstevel@tonic-gate 		switch(PAM_MSG_MEMBER(prompts, count, msg_style)) {
24937c478bd9Sstevel@tonic-gate 		case PAM_TEXT_INFO:
24947c478bd9Sstevel@tonic-gate 			/* Write to stdout of channel */
24957c478bd9Sstevel@tonic-gate 			prompt = PAM_MSG_MEMBER(prompts, count, msg);
24967c478bd9Sstevel@tonic-gate 			if (prompt != NULL && s->ttyfd != -1) {
24977c478bd9Sstevel@tonic-gate 				debug2("session_do_pam_conv: text info "
24987c478bd9Sstevel@tonic-gate 				       "prompt: %s", prompt);
24997c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, prompt, strlen(prompt));
25007c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, "\n", 1);
25017c478bd9Sstevel@tonic-gate 			}
25027c478bd9Sstevel@tonic-gate 			reply[count].resp = xstrdup("");
25037c478bd9Sstevel@tonic-gate 			reply[count].resp_retcode = PAM_SUCCESS;
25047c478bd9Sstevel@tonic-gate 			break;
25057c478bd9Sstevel@tonic-gate 		case PAM_ERROR_MSG:
25067c478bd9Sstevel@tonic-gate 			/* Write to stderr of channel */
25077c478bd9Sstevel@tonic-gate 			prompt = PAM_MSG_MEMBER(prompts, count, msg);
25087c478bd9Sstevel@tonic-gate 			if (prompt != NULL && s->ttyfd != -1) {
25097c478bd9Sstevel@tonic-gate 				debug2("session_do_pam_conv: error "
25107c478bd9Sstevel@tonic-gate 				       "prompt: %s", prompt);
25117c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, prompt, strlen(prompt));
25127c478bd9Sstevel@tonic-gate 				(void) write(s->ttyfd, "\n", 1);
25137c478bd9Sstevel@tonic-gate 			}
25147c478bd9Sstevel@tonic-gate 			reply[count].resp = xstrdup("");
25157c478bd9Sstevel@tonic-gate 			reply[count].resp_retcode = PAM_SUCCESS;
25167c478bd9Sstevel@tonic-gate 			break;
25177c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
25187c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
25197c478bd9Sstevel@tonic-gate 		    /*
25207c478bd9Sstevel@tonic-gate 		     * XXX Someday add support for echo on/off prompts
25217c478bd9Sstevel@tonic-gate 		     *     here on sessions with ttys.
25227c478bd9Sstevel@tonic-gate 		     */
25237c478bd9Sstevel@tonic-gate 		default:
25247c478bd9Sstevel@tonic-gate 			xfree(reply);
25257c478bd9Sstevel@tonic-gate 			return PAM_CONV_ERR;
25267c478bd9Sstevel@tonic-gate 		}
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	*resp = reply;
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	return PAM_SUCCESS;
25327c478bd9Sstevel@tonic-gate }
25337c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate static void
25367c478bd9Sstevel@tonic-gate do_authenticated2(Authctxt *authctxt)
25377c478bd9Sstevel@tonic-gate {
25387c478bd9Sstevel@tonic-gate 	server_loop2(authctxt);
25397c478bd9Sstevel@tonic-gate }
2540