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 /*
35fbaf5b3eSJan Pechanec * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
367c478bd9Sstevel@tonic-gate * Use is subject to license terms.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include "includes.h"
407c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: session.c,v 1.150 2002/09/16 19:55:33 stevesk Exp $");
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
437c478bd9Sstevel@tonic-gate #include <deflt.h>
447c478bd9Sstevel@tonic-gate #include <ulimit.h>
457c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
467c478bd9Sstevel@tonic-gate
47a6e0e77dSjp161948 #ifdef HAVE_LIBGEN_H
48a6e0e77dSjp161948 #include <libgen.h>
49a6e0e77dSjp161948 #endif
50a6e0e77dSjp161948
516f8d59d8SJan Pechanec #include <priv.h>
526f8d59d8SJan Pechanec
537c478bd9Sstevel@tonic-gate #include "ssh.h"
547c478bd9Sstevel@tonic-gate #include "ssh1.h"
557c478bd9Sstevel@tonic-gate #include "ssh2.h"
567c478bd9Sstevel@tonic-gate #include "xmalloc.h"
577c478bd9Sstevel@tonic-gate #include "sshpty.h"
587c478bd9Sstevel@tonic-gate #include "packet.h"
597c478bd9Sstevel@tonic-gate #include "buffer.h"
607c478bd9Sstevel@tonic-gate #include "mpaux.h"
617c478bd9Sstevel@tonic-gate #include "uidswap.h"
627c478bd9Sstevel@tonic-gate #include "compat.h"
637c478bd9Sstevel@tonic-gate #include "channels.h"
647c478bd9Sstevel@tonic-gate #include "bufaux.h"
657c478bd9Sstevel@tonic-gate #include "auth.h"
667c478bd9Sstevel@tonic-gate #include "auth-options.h"
677c478bd9Sstevel@tonic-gate #include "pathnames.h"
687c478bd9Sstevel@tonic-gate #include "log.h"
697c478bd9Sstevel@tonic-gate #include "servconf.h"
707c478bd9Sstevel@tonic-gate #include "sshlogin.h"
717c478bd9Sstevel@tonic-gate #include "serverloop.h"
727c478bd9Sstevel@tonic-gate #include "canohost.h"
737c478bd9Sstevel@tonic-gate #include "session.h"
746f8d59d8SJan Pechanec #include "tildexpand.h"
756f8d59d8SJan Pechanec #include "misc.h"
766f8d59d8SJan Pechanec #include "sftp.h"
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate #ifdef USE_PAM
797c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
807c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate #ifdef GSSAPI
837c478bd9Sstevel@tonic-gate #include "ssh-gss.h"
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
877c478bd9Sstevel@tonic-gate #include "altprivsep.h"
887c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
917c478bd9Sstevel@tonic-gate #include <windows.h>
927c478bd9Sstevel@tonic-gate #include <sys/cygwin.h>
937c478bd9Sstevel@tonic-gate #define is_winnt (GetVersion() < 0x80000000)
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /* func */
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate Session *session_new(void);
997c478bd9Sstevel@tonic-gate void session_set_fds(Session *, int, int, int);
1007c478bd9Sstevel@tonic-gate void session_pty_cleanup(void *);
101a6e0e77dSjp161948 void session_xauthfile_cleanup(void *s);
1027c478bd9Sstevel@tonic-gate void session_proctitle(Session *);
1037c478bd9Sstevel@tonic-gate int session_setup_x11fwd(Session *);
1047c478bd9Sstevel@tonic-gate void do_exec_pty(Session *, const char *);
1057c478bd9Sstevel@tonic-gate void do_exec_no_pty(Session *, const char *);
1067c478bd9Sstevel@tonic-gate void do_exec(Session *, const char *);
1077c478bd9Sstevel@tonic-gate void do_login(Session *, const char *);
1087c478bd9Sstevel@tonic-gate void do_child(Session *, const char *);
1097c478bd9Sstevel@tonic-gate void do_motd(void);
1107c478bd9Sstevel@tonic-gate int check_quietlogin(Session *, const char *);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate static void do_authenticated1(Authctxt *);
1137c478bd9Sstevel@tonic-gate static void do_authenticated2(Authctxt *);
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate static int session_pty_req(Session *);
1167c478bd9Sstevel@tonic-gate static int session_env_req(Session *s);
1177c478bd9Sstevel@tonic-gate static void session_free_env(char ***envp);
1186f8d59d8SJan Pechanec static void safely_chroot(const char *path, uid_t uid);
1196f8d59d8SJan Pechanec static void drop_privs(uid_t uid);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate #ifdef USE_PAM
1227c478bd9Sstevel@tonic-gate static void session_do_pam(Session *, int);
1237c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /* import */
1267c478bd9Sstevel@tonic-gate extern ServerOptions options;
1277c478bd9Sstevel@tonic-gate extern char *__progname;
1287c478bd9Sstevel@tonic-gate extern int log_stderr;
1297c478bd9Sstevel@tonic-gate extern int debug_flag;
1307c478bd9Sstevel@tonic-gate extern u_int utmp_len;
1317c478bd9Sstevel@tonic-gate extern void destroy_sensitive_data(void);
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate #ifdef GSSAPI
1347c478bd9Sstevel@tonic-gate extern Gssctxt *xxx_gssctxt;
1357c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /* original command from peer. */
1387c478bd9Sstevel@tonic-gate const char *original_command = NULL;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /* data */
1417c478bd9Sstevel@tonic-gate #define MAX_SESSIONS 10
1427c478bd9Sstevel@tonic-gate Session sessions[MAX_SESSIONS];
1437c478bd9Sstevel@tonic-gate
1446f8d59d8SJan Pechanec #define SUBSYSTEM_NONE 0
1456f8d59d8SJan Pechanec #define SUBSYSTEM_EXT 1
1466f8d59d8SJan Pechanec #define SUBSYSTEM_INT_SFTP 2
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
1497c478bd9Sstevel@tonic-gate login_cap_t *lc;
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate /* Name and directory of socket for authentication agent forwarding. */
1537c478bd9Sstevel@tonic-gate static char *auth_sock_name = NULL;
1547c478bd9Sstevel@tonic-gate static char *auth_sock_dir = NULL;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /* removes the agent forwarding socket */
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate static void
auth_sock_cleanup_proc(void * _pw)1597c478bd9Sstevel@tonic-gate auth_sock_cleanup_proc(void *_pw)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate struct passwd *pw = _pw;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate if (auth_sock_name != NULL) {
1647c478bd9Sstevel@tonic-gate temporarily_use_uid(pw);
1657c478bd9Sstevel@tonic-gate unlink(auth_sock_name);
1667c478bd9Sstevel@tonic-gate rmdir(auth_sock_dir);
1677c478bd9Sstevel@tonic-gate auth_sock_name = NULL;
1687c478bd9Sstevel@tonic-gate restore_uid();
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate static int
auth_input_request_forwarding(struct passwd * pw)1737c478bd9Sstevel@tonic-gate auth_input_request_forwarding(struct passwd * pw)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate Channel *nc;
1767c478bd9Sstevel@tonic-gate int sock;
1777c478bd9Sstevel@tonic-gate struct sockaddr_un sunaddr;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate if (auth_sock_name != NULL) {
1807c478bd9Sstevel@tonic-gate error("authentication forwarding requested twice.");
1817c478bd9Sstevel@tonic-gate return 0;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate /* Temporarily drop privileged uid for mkdir/bind. */
1857c478bd9Sstevel@tonic-gate temporarily_use_uid(pw);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /* Allocate a buffer for the socket name, and format the name. */
1887c478bd9Sstevel@tonic-gate auth_sock_name = xmalloc(MAXPATHLEN);
1897c478bd9Sstevel@tonic-gate auth_sock_dir = xmalloc(MAXPATHLEN);
1907c478bd9Sstevel@tonic-gate strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /* Create private directory for socket */
1937c478bd9Sstevel@tonic-gate if (mkdtemp(auth_sock_dir) == NULL) {
1947c478bd9Sstevel@tonic-gate packet_send_debug("Agent forwarding disabled: "
1957c478bd9Sstevel@tonic-gate "mkdtemp() failed: %.100s", strerror(errno));
1967c478bd9Sstevel@tonic-gate restore_uid();
1977c478bd9Sstevel@tonic-gate xfree(auth_sock_name);
1987c478bd9Sstevel@tonic-gate xfree(auth_sock_dir);
1997c478bd9Sstevel@tonic-gate auth_sock_name = NULL;
2007c478bd9Sstevel@tonic-gate auth_sock_dir = NULL;
2017c478bd9Sstevel@tonic-gate return 0;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
2047c478bd9Sstevel@tonic-gate auth_sock_dir, (long) getpid());
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /* delete agent socket on fatal() */
2077c478bd9Sstevel@tonic-gate fatal_add_cleanup(auth_sock_cleanup_proc, pw);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /* Create the socket. */
2107c478bd9Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0);
2117c478bd9Sstevel@tonic-gate if (sock < 0)
2127c478bd9Sstevel@tonic-gate packet_disconnect("socket: %.100s", strerror(errno));
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /* Bind it to the name. */
2157c478bd9Sstevel@tonic-gate memset(&sunaddr, 0, sizeof(sunaddr));
2167c478bd9Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX;
2177c478bd9Sstevel@tonic-gate strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2207c478bd9Sstevel@tonic-gate packet_disconnect("bind: %.100s", strerror(errno));
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate /* Restore the privileged uid. */
2237c478bd9Sstevel@tonic-gate restore_uid();
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /* Start listening on the socket. */
2267c478bd9Sstevel@tonic-gate if (listen(sock, 5) < 0)
2277c478bd9Sstevel@tonic-gate packet_disconnect("listen: %.100s", strerror(errno));
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate /* Allocate a channel for the authentication agent socket. */
2307c478bd9Sstevel@tonic-gate nc = channel_new("auth socket",
2317c478bd9Sstevel@tonic-gate SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2327c478bd9Sstevel@tonic-gate CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2337c478bd9Sstevel@tonic-gate 0, xstrdup("auth socket"), 1);
2347c478bd9Sstevel@tonic-gate strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
2357c478bd9Sstevel@tonic-gate return 1;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate void
do_authenticated(Authctxt * authctxt)2407c478bd9Sstevel@tonic-gate do_authenticated(Authctxt *authctxt)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate /* setup the channel layer */
2437c478bd9Sstevel@tonic-gate if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
2447c478bd9Sstevel@tonic-gate channel_permit_all_opens();
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (compat20)
2477c478bd9Sstevel@tonic-gate do_authenticated2(authctxt);
2487c478bd9Sstevel@tonic-gate else
2497c478bd9Sstevel@tonic-gate do_authenticated1(authctxt);
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /* remove agent socket */
2527c478bd9Sstevel@tonic-gate if (auth_sock_name != NULL)
2537c478bd9Sstevel@tonic-gate auth_sock_cleanup_proc(authctxt->pw);
2547c478bd9Sstevel@tonic-gate #ifdef KRB4
2557c478bd9Sstevel@tonic-gate if (options.kerberos_ticket_cleanup)
2567c478bd9Sstevel@tonic-gate krb4_cleanup_proc(authctxt);
2577c478bd9Sstevel@tonic-gate #endif
2587c478bd9Sstevel@tonic-gate #ifdef KRB5
2597c478bd9Sstevel@tonic-gate if (options.kerberos_ticket_cleanup)
2607c478bd9Sstevel@tonic-gate krb5_cleanup_proc(authctxt);
2617c478bd9Sstevel@tonic-gate #endif
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate * Prepares for an interactive session. This is called after the user has
2667c478bd9Sstevel@tonic-gate * been successfully authenticated. During this message exchange, pseudo
2677c478bd9Sstevel@tonic-gate * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
2687c478bd9Sstevel@tonic-gate * are requested, etc.
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate static void
do_authenticated1(Authctxt * authctxt)2717c478bd9Sstevel@tonic-gate do_authenticated1(Authctxt *authctxt)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate Session *s;
2747c478bd9Sstevel@tonic-gate char *command;
2757c478bd9Sstevel@tonic-gate int success, type, screen_flag;
2767c478bd9Sstevel@tonic-gate int enable_compression_after_reply = 0;
2777c478bd9Sstevel@tonic-gate u_int proto_len, data_len, dlen, compression_level = 0;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate s = session_new();
2807c478bd9Sstevel@tonic-gate s->authctxt = authctxt;
2817c478bd9Sstevel@tonic-gate s->pw = authctxt->pw;
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate * We stay in this loop until the client requests to execute a shell
2857c478bd9Sstevel@tonic-gate * or a command.
2867c478bd9Sstevel@tonic-gate */
2877c478bd9Sstevel@tonic-gate for (;;) {
2887c478bd9Sstevel@tonic-gate success = 0;
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate /* Get a packet from the client. */
2917c478bd9Sstevel@tonic-gate type = packet_read();
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /* Process the packet. */
2947c478bd9Sstevel@tonic-gate switch (type) {
2957c478bd9Sstevel@tonic-gate case SSH_CMSG_REQUEST_COMPRESSION:
2967c478bd9Sstevel@tonic-gate compression_level = packet_get_int();
2977c478bd9Sstevel@tonic-gate packet_check_eom();
2987c478bd9Sstevel@tonic-gate if (compression_level < 1 || compression_level > 9) {
2997c478bd9Sstevel@tonic-gate packet_send_debug("Received illegal compression level %d.",
3007c478bd9Sstevel@tonic-gate compression_level);
3017c478bd9Sstevel@tonic-gate break;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate if (!options.compression) {
3047c478bd9Sstevel@tonic-gate debug2("compression disabled");
3057c478bd9Sstevel@tonic-gate break;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate /* Enable compression after we have responded with SUCCESS. */
3087c478bd9Sstevel@tonic-gate enable_compression_after_reply = 1;
3097c478bd9Sstevel@tonic-gate success = 1;
3107c478bd9Sstevel@tonic-gate break;
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate case SSH_CMSG_REQUEST_PTY:
3137c478bd9Sstevel@tonic-gate success = session_pty_req(s);
3147c478bd9Sstevel@tonic-gate break;
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate case SSH_CMSG_X11_REQUEST_FORWARDING:
3177c478bd9Sstevel@tonic-gate s->auth_proto = packet_get_string(&proto_len);
3187c478bd9Sstevel@tonic-gate s->auth_data = packet_get_string(&data_len);
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate screen_flag = packet_get_protocol_flags() &
3217c478bd9Sstevel@tonic-gate SSH_PROTOFLAG_SCREEN_NUMBER;
3227c478bd9Sstevel@tonic-gate debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate if (packet_remaining() == 4) {
3257c478bd9Sstevel@tonic-gate if (!screen_flag)
3267c478bd9Sstevel@tonic-gate debug2("Buggy client: "
3277c478bd9Sstevel@tonic-gate "X11 screen flag missing");
3287c478bd9Sstevel@tonic-gate s->screen = packet_get_int();
3297c478bd9Sstevel@tonic-gate } else {
3307c478bd9Sstevel@tonic-gate s->screen = 0;
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate packet_check_eom();
3337c478bd9Sstevel@tonic-gate success = session_setup_x11fwd(s);
3347c478bd9Sstevel@tonic-gate if (!success) {
3357c478bd9Sstevel@tonic-gate xfree(s->auth_proto);
3367c478bd9Sstevel@tonic-gate xfree(s->auth_data);
3377c478bd9Sstevel@tonic-gate s->auth_proto = NULL;
3387c478bd9Sstevel@tonic-gate s->auth_data = NULL;
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate break;
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate case SSH_CMSG_AGENT_REQUEST_FORWARDING:
3437c478bd9Sstevel@tonic-gate if (no_agent_forwarding_flag || compat13) {
3447c478bd9Sstevel@tonic-gate debug("Authentication agent forwarding not permitted for this authentication.");
3457c478bd9Sstevel@tonic-gate break;
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate debug("Received authentication agent forwarding request.");
3487c478bd9Sstevel@tonic-gate success = auth_input_request_forwarding(s->pw);
3497c478bd9Sstevel@tonic-gate break;
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate case SSH_CMSG_PORT_FORWARD_REQUEST:
3527c478bd9Sstevel@tonic-gate if (no_port_forwarding_flag) {
3537c478bd9Sstevel@tonic-gate debug("Port forwarding not permitted for this authentication.");
3547c478bd9Sstevel@tonic-gate break;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate if (!options.allow_tcp_forwarding) {
3577c478bd9Sstevel@tonic-gate debug("Port forwarding not permitted.");
3587c478bd9Sstevel@tonic-gate break;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate debug("Received TCP/IP port forwarding request.");
3617c478bd9Sstevel@tonic-gate channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
3627c478bd9Sstevel@tonic-gate success = 1;
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate case SSH_CMSG_MAX_PACKET_SIZE:
3667c478bd9Sstevel@tonic-gate if (packet_set_maxsize(packet_get_int()) > 0)
3677c478bd9Sstevel@tonic-gate success = 1;
3687c478bd9Sstevel@tonic-gate break;
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
3717c478bd9Sstevel@tonic-gate case SSH_CMSG_HAVE_KERBEROS_TGT:
3727c478bd9Sstevel@tonic-gate if (!options.kerberos_tgt_passing) {
3737c478bd9Sstevel@tonic-gate verbose("Kerberos TGT passing disabled.");
3747c478bd9Sstevel@tonic-gate } else {
3757c478bd9Sstevel@tonic-gate char *kdata = packet_get_string(&dlen);
3767c478bd9Sstevel@tonic-gate packet_check_eom();
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate /* XXX - 0x41, see creds_to_radix version */
3797c478bd9Sstevel@tonic-gate if (kdata[0] != 0x41) {
3807c478bd9Sstevel@tonic-gate #ifdef KRB5
3817c478bd9Sstevel@tonic-gate krb5_data tgt;
3827c478bd9Sstevel@tonic-gate tgt.data = kdata;
3837c478bd9Sstevel@tonic-gate tgt.length = dlen;
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate if (auth_krb5_tgt(s->authctxt, &tgt))
3867c478bd9Sstevel@tonic-gate success = 1;
3877c478bd9Sstevel@tonic-gate else
3887c478bd9Sstevel@tonic-gate verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
3897c478bd9Sstevel@tonic-gate #endif /* KRB5 */
3907c478bd9Sstevel@tonic-gate } else {
3917c478bd9Sstevel@tonic-gate #ifdef AFS
3927c478bd9Sstevel@tonic-gate if (auth_krb4_tgt(s->authctxt, kdata))
3937c478bd9Sstevel@tonic-gate success = 1;
3947c478bd9Sstevel@tonic-gate else
3957c478bd9Sstevel@tonic-gate verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
3967c478bd9Sstevel@tonic-gate #endif /* AFS */
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate xfree(kdata);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate break;
4017c478bd9Sstevel@tonic-gate #endif /* AFS || KRB5 */
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate #ifdef AFS
4047c478bd9Sstevel@tonic-gate case SSH_CMSG_HAVE_AFS_TOKEN:
4057c478bd9Sstevel@tonic-gate if (!options.afs_token_passing || !k_hasafs()) {
4067c478bd9Sstevel@tonic-gate verbose("AFS token passing disabled.");
4077c478bd9Sstevel@tonic-gate } else {
4087c478bd9Sstevel@tonic-gate /* Accept AFS token. */
4097c478bd9Sstevel@tonic-gate char *token = packet_get_string(&dlen);
4107c478bd9Sstevel@tonic-gate packet_check_eom();
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate if (auth_afs_token(s->authctxt, token))
4137c478bd9Sstevel@tonic-gate success = 1;
4147c478bd9Sstevel@tonic-gate else
4157c478bd9Sstevel@tonic-gate verbose("AFS token refused for %.100s",
4167c478bd9Sstevel@tonic-gate s->authctxt->user);
4177c478bd9Sstevel@tonic-gate xfree(token);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate break;
4207c478bd9Sstevel@tonic-gate #endif /* AFS */
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate case SSH_CMSG_EXEC_SHELL:
4237c478bd9Sstevel@tonic-gate case SSH_CMSG_EXEC_CMD:
4247c478bd9Sstevel@tonic-gate if (type == SSH_CMSG_EXEC_CMD) {
4257c478bd9Sstevel@tonic-gate command = packet_get_string(&dlen);
4267c478bd9Sstevel@tonic-gate debug("Exec command '%.500s'", command);
4277c478bd9Sstevel@tonic-gate do_exec(s, command);
4287c478bd9Sstevel@tonic-gate xfree(command);
4297c478bd9Sstevel@tonic-gate } else {
4307c478bd9Sstevel@tonic-gate do_exec(s, NULL);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate packet_check_eom();
4337c478bd9Sstevel@tonic-gate session_close(s);
4347c478bd9Sstevel@tonic-gate return;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate default:
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate * Any unknown messages in this phase are ignored,
4397c478bd9Sstevel@tonic-gate * and a failure message is returned.
4407c478bd9Sstevel@tonic-gate */
4417c478bd9Sstevel@tonic-gate log("Unknown packet type received after authentication: %d", type);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
4447c478bd9Sstevel@tonic-gate packet_send();
4457c478bd9Sstevel@tonic-gate packet_write_wait();
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /* Enable compression now that we have replied if appropriate. */
4487c478bd9Sstevel@tonic-gate if (enable_compression_after_reply) {
4497c478bd9Sstevel@tonic-gate enable_compression_after_reply = 0;
4507c478bd9Sstevel@tonic-gate packet_start_compression(compression_level);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate * This is called to fork and execute a command when we have no tty. This
4577c478bd9Sstevel@tonic-gate * will call do_child from the child, and server_loop from the parent after
4587c478bd9Sstevel@tonic-gate * setting up file descriptors and such.
4597c478bd9Sstevel@tonic-gate */
4607c478bd9Sstevel@tonic-gate void
do_exec_no_pty(Session * s,const char * command)4617c478bd9Sstevel@tonic-gate do_exec_no_pty(Session *s, const char *command)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate pid_t pid;
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate int inout[2], err[2];
4667c478bd9Sstevel@tonic-gate /* Uses socket pairs to communicate with the program. */
4677c478bd9Sstevel@tonic-gate if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
4687c478bd9Sstevel@tonic-gate socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
4697c478bd9Sstevel@tonic-gate packet_disconnect("Could not create socket pairs: %.100s",
4707c478bd9Sstevel@tonic-gate strerror(errno));
4717c478bd9Sstevel@tonic-gate if (s == NULL)
4727c478bd9Sstevel@tonic-gate fatal("do_exec_no_pty: no session");
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate session_proctitle(s);
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate /* Fork the child. */
4777c478bd9Sstevel@tonic-gate if ((pid = fork()) == 0) {
4787c478bd9Sstevel@tonic-gate fatal_remove_all_cleanups();
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate /* Child. Reinitialize the log since the pid has changed. */
4817c478bd9Sstevel@tonic-gate log_init(__progname, options.log_level, options.log_facility, log_stderr);
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate * Create a new session and process group since the 4.4BSD
4857c478bd9Sstevel@tonic-gate * setlogin() affects the entire process group.
4867c478bd9Sstevel@tonic-gate */
4877c478bd9Sstevel@tonic-gate if (setsid() < 0)
4887c478bd9Sstevel@tonic-gate error("setsid failed: %.100s", strerror(errno));
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * Redirect stdin, stdout, and stderr. Stdin and stdout will
4927c478bd9Sstevel@tonic-gate * use the same socket, as some programs (particularly rdist)
4937c478bd9Sstevel@tonic-gate * seem to depend on it.
4947c478bd9Sstevel@tonic-gate */
4957c478bd9Sstevel@tonic-gate close(inout[1]);
4967c478bd9Sstevel@tonic-gate close(err[1]);
4977c478bd9Sstevel@tonic-gate if (dup2(inout[0], 0) < 0) /* stdin */
4987c478bd9Sstevel@tonic-gate perror("dup2 stdin");
4997c478bd9Sstevel@tonic-gate if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
5007c478bd9Sstevel@tonic-gate perror("dup2 stdout");
501d92fc072SZdenek Kotala if (s->is_subsystem) {
502d92fc072SZdenek Kotala /*
503d92fc072SZdenek Kotala * Redirect the subsystem's stderr to /dev/null. We might send it
504d92fc072SZdenek Kotala * over to the other side but changing that might break existing
505d92fc072SZdenek Kotala * SSH clients.
506d92fc072SZdenek Kotala */
507d92fc072SZdenek Kotala close(err[0]);
508d92fc072SZdenek Kotala if ((err[0] = open(_PATH_DEVNULL, O_WRONLY)) == -1)
509d92fc072SZdenek Kotala fatal("Cannot open /dev/null: %.100s", strerror(errno));
510d92fc072SZdenek Kotala }
5117c478bd9Sstevel@tonic-gate if (dup2(err[0], 2) < 0) /* stderr */
5127c478bd9Sstevel@tonic-gate perror("dup2 stderr");
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate #ifdef _UNICOS
5157c478bd9Sstevel@tonic-gate cray_init_job(s->pw); /* set up cray jid and tmpdir */
5167c478bd9Sstevel@tonic-gate #endif
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate /* Do processing for the child (exec command etc). */
5197c478bd9Sstevel@tonic-gate do_child(s, command);
5207c478bd9Sstevel@tonic-gate /* NOTREACHED */
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate #ifdef _UNICOS
5237c478bd9Sstevel@tonic-gate signal(WJSIGNAL, cray_job_termination_handler);
5247c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
5257c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
5267c478bd9Sstevel@tonic-gate if (is_winnt)
5277c478bd9Sstevel@tonic-gate cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
5287c478bd9Sstevel@tonic-gate #endif
5297c478bd9Sstevel@tonic-gate if (pid < 0)
5307c478bd9Sstevel@tonic-gate packet_disconnect("fork failed: %.100s", strerror(errno));
5316f8d59d8SJan Pechanec
5327c478bd9Sstevel@tonic-gate s->pid = pid;
5337c478bd9Sstevel@tonic-gate /* Set interactive/non-interactive mode. */
5347c478bd9Sstevel@tonic-gate packet_set_interactive(s->display != NULL);
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate /* We are the parent. Close the child sides of the socket pairs. */
5377c478bd9Sstevel@tonic-gate close(inout[0]);
5387c478bd9Sstevel@tonic-gate close(err[0]);
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate * Enter the interactive session. Note: server_loop must be able to
5427c478bd9Sstevel@tonic-gate * handle the case that fdin and fdout are the same.
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate if (compat20) {
5457c478bd9Sstevel@tonic-gate session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
5467a71c13fSZdenek Kotala if (s->is_subsystem)
5477a71c13fSZdenek Kotala close(err[1]);
5487c478bd9Sstevel@tonic-gate /* Don't close channel before sending exit-status! */
5497c478bd9Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 1);
5507c478bd9Sstevel@tonic-gate } else {
5517c478bd9Sstevel@tonic-gate server_loop(pid, inout[1], inout[1], err[1]);
5527c478bd9Sstevel@tonic-gate /* server_loop has closed inout[1] and err[1]. */
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate * This is called to fork and execute a command when we have a tty. This
5587c478bd9Sstevel@tonic-gate * will call do_child from the child, and server_loop from the parent after
5597c478bd9Sstevel@tonic-gate * setting up file descriptors, controlling tty, updating wtmp, utmp,
5607c478bd9Sstevel@tonic-gate * lastlog, and other such operations.
5617c478bd9Sstevel@tonic-gate */
5627c478bd9Sstevel@tonic-gate void
do_exec_pty(Session * s,const char * command)5637c478bd9Sstevel@tonic-gate do_exec_pty(Session *s, const char *command)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate int fdout, ptyfd, ttyfd, ptymaster, pipe_fds[2];
5667c478bd9Sstevel@tonic-gate pid_t pid;
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate if (s == NULL)
5697c478bd9Sstevel@tonic-gate fatal("do_exec_pty: no session");
5707c478bd9Sstevel@tonic-gate ptyfd = s->ptyfd;
5717c478bd9Sstevel@tonic-gate ttyfd = s->ttyfd;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate #ifdef USE_PAM
5747c478bd9Sstevel@tonic-gate session_do_pam(s, 1); /* pam_open_session() */
5757c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate * This pipe lets sshd wait for child to exec or exit. This is
5797c478bd9Sstevel@tonic-gate * particularly important for ALTPRIVSEP because the child is
5807c478bd9Sstevel@tonic-gate * the one to call the monitor to request a record_login() and
5817c478bd9Sstevel@tonic-gate * we don't want the child and the parent to compete for the
5827c478bd9Sstevel@tonic-gate * monitor's attention. But this is generic code and doesn't
5837c478bd9Sstevel@tonic-gate * hurt to have here even if ALTPRIVSEP is not used.
5847c478bd9Sstevel@tonic-gate */
5857c478bd9Sstevel@tonic-gate if (pipe(pipe_fds) != 0)
5867c478bd9Sstevel@tonic-gate packet_disconnect("pipe failed: %.100s", strerror(errno));
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate (void) fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC);
5897c478bd9Sstevel@tonic-gate (void) fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC);
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate /* Fork the child. */
5927c478bd9Sstevel@tonic-gate if ((pid = fork()) == 0) {
5937c478bd9Sstevel@tonic-gate (void) close(pipe_fds[0]);
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate fatal_remove_all_cleanups();
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate /* Child. Reinitialize the log because the pid has changed. */
5987c478bd9Sstevel@tonic-gate log_init(__progname, options.log_level, options.log_facility, log_stderr);
5997c478bd9Sstevel@tonic-gate /* Close the master side of the pseudo tty. */
6007c478bd9Sstevel@tonic-gate close(ptyfd);
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate /* Make the pseudo tty our controlling tty. */
6037c478bd9Sstevel@tonic-gate pty_make_controlling_tty(&ttyfd, s->tty);
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate /* Redirect stdin/stdout/stderr from the pseudo tty. */
6067c478bd9Sstevel@tonic-gate if (dup2(ttyfd, 0) < 0)
6077c478bd9Sstevel@tonic-gate error("dup2 stdin: %s", strerror(errno));
6087c478bd9Sstevel@tonic-gate if (dup2(ttyfd, 1) < 0)
6097c478bd9Sstevel@tonic-gate error("dup2 stdout: %s", strerror(errno));
6107c478bd9Sstevel@tonic-gate if (dup2(ttyfd, 2) < 0)
6117c478bd9Sstevel@tonic-gate error("dup2 stderr: %s", strerror(errno));
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate /* Close the extra descriptor for the pseudo tty. */
6147c478bd9Sstevel@tonic-gate close(ttyfd);
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate /* record login, etc. similar to login(1) */
6177c478bd9Sstevel@tonic-gate do_login(s, command);
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate /*
620b9aa66a7SJan Pechanec * Close the pipe to the parent so it can re-enter its event
6217c478bd9Sstevel@tonic-gate * loop and service the ptm; if enough debug messages get
6227c478bd9Sstevel@tonic-gate * written to the pty before this happens there will be a
6237c478bd9Sstevel@tonic-gate * deadlock.
6247c478bd9Sstevel@tonic-gate */
6257c478bd9Sstevel@tonic-gate close(pipe_fds[1]);
6267c478bd9Sstevel@tonic-gate
6272810e2a6SHuie-Ying Lee /*
6282810e2a6SHuie-Ying Lee * do_motd() was called originally in do_login(). However,
6292810e2a6SHuie-Ying Lee * when the /etc/motd file is large, a deadlock would happen,
6302810e2a6SHuie-Ying Lee * because
6312810e2a6SHuie-Ying Lee * - The child is blocked at fputs() to pty, when pty buffer
6322810e2a6SHuie-Ying Lee * is full.
6332810e2a6SHuie-Ying Lee * - The parent can not consume the pty buffer, because it is
6342810e2a6SHuie-Ying Lee * still blocked at read(pipe_fds[0]).
6352810e2a6SHuie-Ying Lee *
6362810e2a6SHuie-Ying Lee * To resolve the deadlock issue, we defer do_motd() after
6372810e2a6SHuie-Ying Lee * close(pipe_fds[1]).
6382810e2a6SHuie-Ying Lee */
6392810e2a6SHuie-Ying Lee do_motd();
6402810e2a6SHuie-Ying Lee
6417c478bd9Sstevel@tonic-gate /* Do common processing for the child, such as execing the command. */
6427c478bd9Sstevel@tonic-gate do_child(s, command);
6437c478bd9Sstevel@tonic-gate /* NOTREACHED */
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /* Wait for child to exec() or exit() */
6477c478bd9Sstevel@tonic-gate (void) close(pipe_fds[1]);
6487c478bd9Sstevel@tonic-gate (void) read(pipe_fds[0], &pipe_fds[1], sizeof(int));
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate #ifdef _UNICOS
6517c478bd9Sstevel@tonic-gate signal(WJSIGNAL, cray_job_termination_handler);
6527c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
6537c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
6547c478bd9Sstevel@tonic-gate if (is_winnt)
6557c478bd9Sstevel@tonic-gate cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
6567c478bd9Sstevel@tonic-gate #endif
6577c478bd9Sstevel@tonic-gate if (pid < 0)
6587c478bd9Sstevel@tonic-gate packet_disconnect("fork failed: %.100s", strerror(errno));
6597c478bd9Sstevel@tonic-gate s->pid = pid;
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /* Parent. Close the slave side of the pseudo tty. */
6627c478bd9Sstevel@tonic-gate close(ttyfd);
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate * Create another descriptor of the pty master side for use as the
6667c478bd9Sstevel@tonic-gate * standard input. We could use the original descriptor, but this
6677c478bd9Sstevel@tonic-gate * simplifies code in server_loop. The descriptor is bidirectional.
6687c478bd9Sstevel@tonic-gate */
6697c478bd9Sstevel@tonic-gate fdout = dup(ptyfd);
6707c478bd9Sstevel@tonic-gate if (fdout < 0)
6717c478bd9Sstevel@tonic-gate packet_disconnect("dup #1 failed: %.100s", strerror(errno));
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate /* we keep a reference to the pty master */
6747c478bd9Sstevel@tonic-gate ptymaster = dup(ptyfd);
6757c478bd9Sstevel@tonic-gate if (ptymaster < 0)
6767c478bd9Sstevel@tonic-gate packet_disconnect("dup #2 failed: %.100s", strerror(errno));
6777c478bd9Sstevel@tonic-gate s->ptymaster = ptymaster;
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate /* Enter interactive session. */
6807c478bd9Sstevel@tonic-gate packet_set_interactive(1);
6817c478bd9Sstevel@tonic-gate if (compat20) {
6827c478bd9Sstevel@tonic-gate session_set_fds(s, ptyfd, fdout, -1);
6837c478bd9Sstevel@tonic-gate /* Don't close channel before sending exit-status! */
6847c478bd9Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 1);
6857c478bd9Sstevel@tonic-gate } else {
6867c478bd9Sstevel@tonic-gate server_loop(pid, ptyfd, fdout, -1);
6877c478bd9Sstevel@tonic-gate /* server_loop _has_ closed ptyfd and fdout. */
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate * This is called to fork and execute a command. If another command is
6937c478bd9Sstevel@tonic-gate * to be forced, execute that instead.
6947c478bd9Sstevel@tonic-gate */
6957c478bd9Sstevel@tonic-gate void
do_exec(Session * s,const char * command)6967c478bd9Sstevel@tonic-gate do_exec(Session *s, const char *command)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate if (command)
6997c478bd9Sstevel@tonic-gate s->command = xstrdup(command);
7007c478bd9Sstevel@tonic-gate
7017c478bd9Sstevel@tonic-gate if (forced_command) {
7027c478bd9Sstevel@tonic-gate original_command = command;
7037c478bd9Sstevel@tonic-gate command = forced_command;
7047c478bd9Sstevel@tonic-gate debug("Forced command '%.900s'", command);
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate if (s->ttyfd != -1)
7087c478bd9Sstevel@tonic-gate do_exec_pty(s, command);
7097c478bd9Sstevel@tonic-gate else
7107c478bd9Sstevel@tonic-gate do_exec_no_pty(s, command);
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate original_command = NULL;
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate /* administrative, login(1)-like work */
7177c478bd9Sstevel@tonic-gate void
do_login(Session * s,const char * command)7187c478bd9Sstevel@tonic-gate do_login(Session *s, const char *command)
7197c478bd9Sstevel@tonic-gate {
720*2de0a7d6SDan McDonald char *time_string;
7217c478bd9Sstevel@tonic-gate #ifndef ALTPRIVSEP
7227c478bd9Sstevel@tonic-gate struct passwd * pw = s->pw;
7237c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7247c478bd9Sstevel@tonic-gate pid_t pid = getpid();
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate /* Record that there was a login on that tty from the remote host. */
7277c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
7287c478bd9Sstevel@tonic-gate debug3("Recording SSHv2 channel login in utmpx/wtmpx");
7297c478bd9Sstevel@tonic-gate altprivsep_record_login(pid, s->tty);
7307c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate if (check_quietlogin(s, command))
7337c478bd9Sstevel@tonic-gate return;
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate #ifdef USE_PAM
7367c478bd9Sstevel@tonic-gate print_pam_messages();
7377c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
7387c478bd9Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
7397c478bd9Sstevel@tonic-gate if (aixloginmsg && *aixloginmsg)
7407c478bd9Sstevel@tonic-gate printf("%s\n", aixloginmsg);
7417c478bd9Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
742*2de0a7d6SDan McDonald
743*2de0a7d6SDan McDonald #ifndef NO_SSH_LASTLOG
744*2de0a7d6SDan McDonald if (options.print_lastlog && s->last_login_time != 0) {
745*2de0a7d6SDan McDonald time_string = ctime(&s->last_login_time);
746*2de0a7d6SDan McDonald if (strchr(time_string, '\n'))
747*2de0a7d6SDan McDonald *strchr(time_string, '\n') = 0;
748*2de0a7d6SDan McDonald if (strcmp(s->hostname, "") == 0)
749*2de0a7d6SDan McDonald printf("Last login: %s\r\n", time_string);
750*2de0a7d6SDan McDonald else
751*2de0a7d6SDan McDonald printf("Last login: %s from %s\r\n", time_string,
752*2de0a7d6SDan McDonald s->hostname);
753*2de0a7d6SDan McDonald }
754*2de0a7d6SDan McDonald #endif /* NO_SSH_LASTLOG */
755*2de0a7d6SDan McDonald
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate /*
7597c478bd9Sstevel@tonic-gate * Display the message of the day.
7607c478bd9Sstevel@tonic-gate */
7617c478bd9Sstevel@tonic-gate void
do_motd(void)7627c478bd9Sstevel@tonic-gate do_motd(void)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate FILE *f;
7657c478bd9Sstevel@tonic-gate char buf[256];
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate if (options.print_motd) {
7687c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
7697c478bd9Sstevel@tonic-gate f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
7707c478bd9Sstevel@tonic-gate "/etc/motd"), "r");
7717c478bd9Sstevel@tonic-gate #else
7727c478bd9Sstevel@tonic-gate f = fopen("/etc/motd", "r");
7737c478bd9Sstevel@tonic-gate #endif
7747c478bd9Sstevel@tonic-gate if (f) {
7757c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f))
7767c478bd9Sstevel@tonic-gate fputs(buf, stdout);
7777c478bd9Sstevel@tonic-gate fclose(f);
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate /*
7847c478bd9Sstevel@tonic-gate * Check for quiet login, either .hushlogin or command given.
7857c478bd9Sstevel@tonic-gate */
7867c478bd9Sstevel@tonic-gate int
check_quietlogin(Session * s,const char * command)7877c478bd9Sstevel@tonic-gate check_quietlogin(Session *s, const char *command)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate char buf[256];
7907c478bd9Sstevel@tonic-gate struct passwd *pw = s->pw;
7917c478bd9Sstevel@tonic-gate struct stat st;
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate /* Return 1 if .hushlogin exists or a command given. */
7947c478bd9Sstevel@tonic-gate if (command != NULL)
7957c478bd9Sstevel@tonic-gate return 1;
7967c478bd9Sstevel@tonic-gate snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
7977c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
7987c478bd9Sstevel@tonic-gate if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
7997c478bd9Sstevel@tonic-gate return 1;
8007c478bd9Sstevel@tonic-gate #else
8017c478bd9Sstevel@tonic-gate if (stat(buf, &st) >= 0)
8027c478bd9Sstevel@tonic-gate return 1;
8037c478bd9Sstevel@tonic-gate #endif
8047c478bd9Sstevel@tonic-gate return 0;
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate * Sets the value of the given variable in the environment. If the variable
8097c478bd9Sstevel@tonic-gate * already exists, its value is overriden.
8107c478bd9Sstevel@tonic-gate */
8117c478bd9Sstevel@tonic-gate void
child_set_env(char *** envp,u_int * envsizep,const char * name,const char * value)8127c478bd9Sstevel@tonic-gate child_set_env(char ***envp, u_int *envsizep, const char *name,
8137c478bd9Sstevel@tonic-gate const char *value)
8147c478bd9Sstevel@tonic-gate {
815d8a94255SErik Trauschke debug3("child_set_env(%s, %s)", name, value);
816d8a94255SErik Trauschke child_set_env_silent(envp, envsizep, name, value);
817d8a94255SErik Trauschke }
818d8a94255SErik Trauschke
819d8a94255SErik Trauschke
820d8a94255SErik Trauschke void
child_set_env_silent(char *** envp,u_int * envsizep,const char * name,const char * value)821d8a94255SErik Trauschke child_set_env_silent(char ***envp, u_int *envsizep, const char *name,
822d8a94255SErik Trauschke const char *value)
823d8a94255SErik Trauschke {
8247c478bd9Sstevel@tonic-gate u_int i, namelen;
8257c478bd9Sstevel@tonic-gate char **env;
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate * Find the slot where the value should be stored. If the variable
8297c478bd9Sstevel@tonic-gate * already exists, we reuse the slot; otherwise we append a new slot
8307c478bd9Sstevel@tonic-gate * at the end of the array, expanding if necessary.
8317c478bd9Sstevel@tonic-gate */
8327c478bd9Sstevel@tonic-gate env = *envp;
8337c478bd9Sstevel@tonic-gate namelen = strlen(name);
8347c478bd9Sstevel@tonic-gate for (i = 0; env[i]; i++)
8357c478bd9Sstevel@tonic-gate if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
8367c478bd9Sstevel@tonic-gate break;
8377c478bd9Sstevel@tonic-gate if (env[i]) {
8387c478bd9Sstevel@tonic-gate /* Reuse the slot. */
8397c478bd9Sstevel@tonic-gate xfree(env[i]);
8407c478bd9Sstevel@tonic-gate } else {
8417c478bd9Sstevel@tonic-gate /* New variable. Expand if necessary. */
8427c478bd9Sstevel@tonic-gate if (i >= (*envsizep) - 1) {
8437c478bd9Sstevel@tonic-gate if (*envsizep >= 1000)
8447c478bd9Sstevel@tonic-gate fatal("child_set_env: too many env vars,"
8457c478bd9Sstevel@tonic-gate " skipping: %.100s", name);
8467c478bd9Sstevel@tonic-gate (*envsizep) += 50;
8477c478bd9Sstevel@tonic-gate env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate /* Need to set the NULL pointer at end of array beyond the new slot. */
8507c478bd9Sstevel@tonic-gate env[i + 1] = NULL;
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate
8537c478bd9Sstevel@tonic-gate /* Allocate space and format the variable in the appropriate slot. */
8547c478bd9Sstevel@tonic-gate env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
8557c478bd9Sstevel@tonic-gate snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate * Reads environment variables from the given file and adds/overrides them
8607c478bd9Sstevel@tonic-gate * into the environment. If the file does not exist, this does nothing.
8617c478bd9Sstevel@tonic-gate * Otherwise, it must consist of empty lines, comments (line starts with '#')
8627c478bd9Sstevel@tonic-gate * and assignments of the form name=value. No other forms are allowed.
8637c478bd9Sstevel@tonic-gate */
8647c478bd9Sstevel@tonic-gate static void
read_environment_file(char *** env,u_int * envsize,const char * filename)8657c478bd9Sstevel@tonic-gate read_environment_file(char ***env, u_int *envsize,
8667c478bd9Sstevel@tonic-gate const char *filename)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate FILE *f;
8697c478bd9Sstevel@tonic-gate char buf[4096];
8707c478bd9Sstevel@tonic-gate char *cp, *value;
8717c478bd9Sstevel@tonic-gate u_int lineno = 0;
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate f = fopen(filename, "r");
8747c478bd9Sstevel@tonic-gate if (!f)
8757c478bd9Sstevel@tonic-gate return;
8767c478bd9Sstevel@tonic-gate
8777c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f)) {
8787c478bd9Sstevel@tonic-gate if (++lineno > 1000)
8797c478bd9Sstevel@tonic-gate fatal("Too many lines in environment file %s", filename);
8807c478bd9Sstevel@tonic-gate for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
8817c478bd9Sstevel@tonic-gate ;
8827c478bd9Sstevel@tonic-gate if (!*cp || *cp == '#' || *cp == '\n')
8837c478bd9Sstevel@tonic-gate continue;
8847c478bd9Sstevel@tonic-gate if (strchr(cp, '\n'))
8857c478bd9Sstevel@tonic-gate *strchr(cp, '\n') = '\0';
8867c478bd9Sstevel@tonic-gate value = strchr(cp, '=');
8877c478bd9Sstevel@tonic-gate if (value == NULL) {
8887c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Bad line %u in %.100s\n"),
8897c478bd9Sstevel@tonic-gate lineno, filename);
8907c478bd9Sstevel@tonic-gate continue;
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate /*
8937c478bd9Sstevel@tonic-gate * Replace the equals sign by nul, and advance value to
8947c478bd9Sstevel@tonic-gate * the value string.
8957c478bd9Sstevel@tonic-gate */
8967c478bd9Sstevel@tonic-gate *value = '\0';
8977c478bd9Sstevel@tonic-gate value++;
8987c478bd9Sstevel@tonic-gate child_set_env(env, envsize, cp, value);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate fclose(f);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate
copy_environment(char ** source,char *** env,u_int * envsize)9037c478bd9Sstevel@tonic-gate void copy_environment(char **source, char ***env, u_int *envsize)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate char *var_name, *var_val;
9067c478bd9Sstevel@tonic-gate int i;
9077c478bd9Sstevel@tonic-gate
9087c478bd9Sstevel@tonic-gate if (source == NULL)
9097c478bd9Sstevel@tonic-gate return;
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate for(i = 0; source[i] != NULL; i++) {
9127c478bd9Sstevel@tonic-gate var_name = xstrdup(source[i]);
9137c478bd9Sstevel@tonic-gate if ((var_val = strstr(var_name, "=")) == NULL) {
9147c478bd9Sstevel@tonic-gate xfree(var_name);
9157c478bd9Sstevel@tonic-gate continue;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate *var_val++ = '\0';
9187c478bd9Sstevel@tonic-gate
9197c478bd9Sstevel@tonic-gate debug3("Copy environment: %s=%s", var_name, var_val);
9207c478bd9Sstevel@tonic-gate child_set_env(env, envsize, var_name, var_val);
9217c478bd9Sstevel@tonic-gate
9227c478bd9Sstevel@tonic-gate xfree(var_name);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
9277c478bd9Sstevel@tonic-gate static
9287c478bd9Sstevel@tonic-gate void
deflt_do_setup_env(Session * s,const char * shell,char *** env,u_int * envsize)9297c478bd9Sstevel@tonic-gate deflt_do_setup_env(Session *s, const char *shell, char ***env, u_int *envsize)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate int flags;
9327c478bd9Sstevel@tonic-gate char *ptr;
9337c478bd9Sstevel@tonic-gate mode_t Umask = 022;
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate if (defopen(_PATH_DEFAULT_LOGIN))
9367c478bd9Sstevel@tonic-gate return;
9377c478bd9Sstevel@tonic-gate
9387c478bd9Sstevel@tonic-gate /* Ignore case */
9397c478bd9Sstevel@tonic-gate flags = defcntl(DC_GETFLAGS, 0);
9407c478bd9Sstevel@tonic-gate TURNOFF(flags, DC_CASE);
9417c478bd9Sstevel@tonic-gate (void) defcntl(DC_SETFLAGS, flags);
9427c478bd9Sstevel@tonic-gate
9437c478bd9Sstevel@tonic-gate /* TZ & HZ */
9447c478bd9Sstevel@tonic-gate if ((ptr = defread("TIMEZONE=")) != NULL)
9457c478bd9Sstevel@tonic-gate child_set_env(env, envsize, "TZ", ptr);
9467c478bd9Sstevel@tonic-gate if ((ptr = defread("HZ=")) != NULL)
9477c478bd9Sstevel@tonic-gate child_set_env(env, envsize, "HZ", ptr);
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate /* PATH */
9507c478bd9Sstevel@tonic-gate if (s->pw->pw_uid != 0 && (ptr = defread("PATH=")) != NULL)
9517c478bd9Sstevel@tonic-gate child_set_env(env, envsize, "PATH", ptr);
9527c478bd9Sstevel@tonic-gate if (s->pw->pw_uid == 0 && (ptr = defread("SUPATH=")) != NULL)
9537c478bd9Sstevel@tonic-gate child_set_env(env, envsize, "PATH", ptr);
9547c478bd9Sstevel@tonic-gate
9557c478bd9Sstevel@tonic-gate /* SHELL */
9567c478bd9Sstevel@tonic-gate if ((ptr = defread("ALTSHELL=")) != NULL) {
9577c478bd9Sstevel@tonic-gate if (strcasecmp("YES", ptr) == 0)
9587c478bd9Sstevel@tonic-gate child_set_env(env, envsize, "SHELL", shell);
9597c478bd9Sstevel@tonic-gate else
9607c478bd9Sstevel@tonic-gate child_set_env(env, envsize, "SHELL", "");
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate /* UMASK */
9647c478bd9Sstevel@tonic-gate if ((ptr = defread("UMASK=")) != NULL &&
9657c478bd9Sstevel@tonic-gate sscanf(ptr, "%lo", &Umask) == 1 &&
9667c478bd9Sstevel@tonic-gate Umask <= (mode_t)0777)
9677c478bd9Sstevel@tonic-gate (void) umask(Umask);
9687c478bd9Sstevel@tonic-gate else
9697c478bd9Sstevel@tonic-gate (void) umask(022);
9707c478bd9Sstevel@tonic-gate
9717c478bd9Sstevel@tonic-gate /* ULIMIT */
9727c478bd9Sstevel@tonic-gate if ((ptr = defread("ULIMIT=")) != NULL && atol(ptr) > 0L &&
9737c478bd9Sstevel@tonic-gate ulimit(UL_SETFSIZE, atol(ptr)) < 0L)
9747c478bd9Sstevel@tonic-gate error("Could not set ULIMIT to %ld from %s\n", atol(ptr),
9757c478bd9Sstevel@tonic-gate _PATH_DEFAULT_LOGIN);
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate (void) defopen(NULL);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
9807c478bd9Sstevel@tonic-gate
9817c478bd9Sstevel@tonic-gate static char **
do_setup_env(Session * s,const char * shell)9827c478bd9Sstevel@tonic-gate do_setup_env(Session *s, const char *shell)
9837c478bd9Sstevel@tonic-gate {
984c2a2f8dcSjp161948 char buf[256];
985c2a2f8dcSjp161948 char path_maildir[] = _PATH_MAILDIR;
98626ba1984Sjp161948 u_int i, envsize, pm_len;
9877c478bd9Sstevel@tonic-gate char **env;
9887c478bd9Sstevel@tonic-gate struct passwd *pw = s->pw;
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate /* Initialize the environment. */
9917c478bd9Sstevel@tonic-gate envsize = 100;
9927c478bd9Sstevel@tonic-gate env = xmalloc(envsize * sizeof(char *));
9937c478bd9Sstevel@tonic-gate env[0] = NULL;
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate * The Windows environment contains some setting which are
9987c478bd9Sstevel@tonic-gate * important for a running system. They must not be dropped.
9997c478bd9Sstevel@tonic-gate */
10007c478bd9Sstevel@tonic-gate copy_environment(environ, &env, &envsize);
10017c478bd9Sstevel@tonic-gate #endif
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate #ifdef GSSAPI
10047c478bd9Sstevel@tonic-gate /* Allow any GSSAPI methods that we've used to alter
10057c478bd9Sstevel@tonic-gate * the childs environment as they see fit
10067c478bd9Sstevel@tonic-gate */
10077c478bd9Sstevel@tonic-gate ssh_gssapi_do_child(xxx_gssctxt, &env,&envsize);
10087c478bd9Sstevel@tonic-gate #endif
10097c478bd9Sstevel@tonic-gate
10107c478bd9Sstevel@tonic-gate /* Set basic environment. */
10117c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "USER", pw->pw_name);
10127c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
10137c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "HOME", pw->pw_dir);
10147c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
10157c478bd9Sstevel@tonic-gate if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
10167c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10177c478bd9Sstevel@tonic-gate else
10187c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH", getenv("PATH"));
10197c478bd9Sstevel@tonic-gate #else /* HAVE_LOGIN_CAP */
10207c478bd9Sstevel@tonic-gate # ifndef HAVE_CYGWIN
10217c478bd9Sstevel@tonic-gate /*
10227c478bd9Sstevel@tonic-gate * There's no standard path on Windows. The path contains
10237c478bd9Sstevel@tonic-gate * important components pointing to the system directories,
10247c478bd9Sstevel@tonic-gate * needed for loading shared libraries. So the path better
10257c478bd9Sstevel@tonic-gate * remains intact here.
10267c478bd9Sstevel@tonic-gate */
10277c478bd9Sstevel@tonic-gate # ifdef SUPERUSER_PATH
10287c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH",
10297c478bd9Sstevel@tonic-gate s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
10307c478bd9Sstevel@tonic-gate # else
10317c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10327c478bd9Sstevel@tonic-gate # endif /* SUPERUSER_PATH */
10337c478bd9Sstevel@tonic-gate # endif /* HAVE_CYGWIN */
10347c478bd9Sstevel@tonic-gate #endif /* HAVE_LOGIN_CAP */
10357c478bd9Sstevel@tonic-gate
103626ba1984Sjp161948 pm_len = strlen(path_maildir);
103726ba1984Sjp161948 if (path_maildir[pm_len - 1] == '/' && pm_len > 1)
103826ba1984Sjp161948 path_maildir[pm_len - 1] = NULL;
10397c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.200s/%.50s",
104026ba1984Sjp161948 path_maildir, pw->pw_name);
10417c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "MAIL", buf);
10427c478bd9Sstevel@tonic-gate
10437c478bd9Sstevel@tonic-gate /* Normal systems set SHELL by default. */
10447c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "SHELL", shell);
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
10477c478bd9Sstevel@tonic-gate deflt_do_setup_env(s, shell, &env, &envsize);
10487c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
10497c478bd9Sstevel@tonic-gate
10507c478bd9Sstevel@tonic-gate #define PASS_ENV(x) \
10517c478bd9Sstevel@tonic-gate if (getenv(x)) \
10527c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, x, getenv(x));
10537c478bd9Sstevel@tonic-gate
10547c478bd9Sstevel@tonic-gate if (getenv("TZ"))
10557c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "TZ", getenv("TZ"));
10567c478bd9Sstevel@tonic-gate
1057a6e0e77dSjp161948 if (s->auth_file != NULL)
1058a6e0e77dSjp161948 child_set_env(&env, &envsize, "XAUTHORITY", s->auth_file);
1059a6e0e77dSjp161948
10607c478bd9Sstevel@tonic-gate PASS_ENV("LANG")
10617c478bd9Sstevel@tonic-gate PASS_ENV("LC_ALL")
10627c478bd9Sstevel@tonic-gate PASS_ENV("LC_CTYPE")
10637c478bd9Sstevel@tonic-gate PASS_ENV("LC_COLLATE")
10644dd46c65Sjp161948 PASS_ENV("LC_TIME")
10657c478bd9Sstevel@tonic-gate PASS_ENV("LC_NUMERIC")
10667c478bd9Sstevel@tonic-gate PASS_ENV("LC_MONETARY")
10677c478bd9Sstevel@tonic-gate PASS_ENV("LC_MESSAGES")
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate #undef PASS_ENV
10707c478bd9Sstevel@tonic-gate
10717c478bd9Sstevel@tonic-gate if (s->env != NULL)
10727c478bd9Sstevel@tonic-gate copy_environment(s->env, &env, &envsize);
10737c478bd9Sstevel@tonic-gate
10747c478bd9Sstevel@tonic-gate /* Set custom environment options from RSA authentication. */
10757c478bd9Sstevel@tonic-gate while (custom_environment) {
10767c478bd9Sstevel@tonic-gate struct envstring *ce = custom_environment;
10777c478bd9Sstevel@tonic-gate char *str = ce->s;
10787c478bd9Sstevel@tonic-gate
10797c478bd9Sstevel@tonic-gate for (i = 0; str[i] != '=' && str[i]; i++)
10807c478bd9Sstevel@tonic-gate ;
10817c478bd9Sstevel@tonic-gate if (str[i] == '=') {
10827c478bd9Sstevel@tonic-gate str[i] = 0;
10837c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, str, str + i + 1);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate custom_environment = ce->next;
10867c478bd9Sstevel@tonic-gate xfree(ce->s);
10877c478bd9Sstevel@tonic-gate xfree(ce);
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate
10907c478bd9Sstevel@tonic-gate /* SSH_CLIENT deprecated */
10917c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.50s %d %d",
10927c478bd9Sstevel@tonic-gate get_remote_ipaddr(), get_remote_port(), get_local_port());
10937c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_CLIENT", buf);
10947c478bd9Sstevel@tonic-gate
10957c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
10967c478bd9Sstevel@tonic-gate get_remote_ipaddr(), get_remote_port(),
10977c478bd9Sstevel@tonic-gate get_local_ipaddr(packet_get_connection_in()), get_local_port());
10987c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
10997c478bd9Sstevel@tonic-gate
11007c478bd9Sstevel@tonic-gate if (s->ttyfd != -1)
11017c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_TTY", s->tty);
11027c478bd9Sstevel@tonic-gate if (s->term)
11037c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "TERM", s->term);
11047c478bd9Sstevel@tonic-gate if (s->display)
11057c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "DISPLAY", s->display);
11067c478bd9Sstevel@tonic-gate if (original_command)
11077c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
11087c478bd9Sstevel@tonic-gate original_command);
11097c478bd9Sstevel@tonic-gate
11107c478bd9Sstevel@tonic-gate #ifdef _UNICOS
11117c478bd9Sstevel@tonic-gate if (cray_tmpdir[0] != '\0')
11127c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
11137c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
11147c478bd9Sstevel@tonic-gate
11157c478bd9Sstevel@tonic-gate #ifdef _AIX
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate char *cp;
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate if ((cp = getenv("AUTHSTATE")) != NULL)
11207c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "AUTHSTATE", cp);
11217c478bd9Sstevel@tonic-gate if ((cp = getenv("KRB5CCNAME")) != NULL)
11227c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "KRB5CCNAME", cp);
11237c478bd9Sstevel@tonic-gate read_environment_file(&env, &envsize, "/etc/environment");
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate #endif
11267c478bd9Sstevel@tonic-gate #ifdef KRB4
11277c478bd9Sstevel@tonic-gate if (s->authctxt->krb4_ticket_file)
11287c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "KRBTKFILE",
11297c478bd9Sstevel@tonic-gate s->authctxt->krb4_ticket_file);
11307c478bd9Sstevel@tonic-gate #endif
11317c478bd9Sstevel@tonic-gate #ifdef KRB5
11327c478bd9Sstevel@tonic-gate if (s->authctxt->krb5_ticket_file)
11337c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, "KRB5CCNAME",
11347c478bd9Sstevel@tonic-gate s->authctxt->krb5_ticket_file);
11357c478bd9Sstevel@tonic-gate #endif
11367c478bd9Sstevel@tonic-gate #ifdef USE_PAM
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate * Pull in any environment variables that may have
11397c478bd9Sstevel@tonic-gate * been set by PAM.
11407c478bd9Sstevel@tonic-gate */
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate char **p;
11437c478bd9Sstevel@tonic-gate
11447c478bd9Sstevel@tonic-gate p = fetch_pam_environment(s->authctxt);
11457c478bd9Sstevel@tonic-gate copy_environment(p, &env, &envsize);
11467c478bd9Sstevel@tonic-gate free_pam_environment(p);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
11497c478bd9Sstevel@tonic-gate
11507c478bd9Sstevel@tonic-gate if (auth_sock_name != NULL)
11517c478bd9Sstevel@tonic-gate child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
11527c478bd9Sstevel@tonic-gate auth_sock_name);
11537c478bd9Sstevel@tonic-gate
11547c478bd9Sstevel@tonic-gate /* read $HOME/.ssh/environment. */
1155b9aa66a7SJan Pechanec if (options.permit_user_env) {
11567c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
11577c478bd9Sstevel@tonic-gate strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
11587c478bd9Sstevel@tonic-gate read_environment_file(&env, &envsize, buf);
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate if (debug_flag) {
11617c478bd9Sstevel@tonic-gate /* dump the environment */
11627c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Environment:\n"));
11637c478bd9Sstevel@tonic-gate for (i = 0; env[i]; i++)
11647c478bd9Sstevel@tonic-gate fprintf(stderr, " %.200s\n", env[i]);
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate return env;
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate
11697c478bd9Sstevel@tonic-gate /*
11707c478bd9Sstevel@tonic-gate * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
11717c478bd9Sstevel@tonic-gate * first in this order).
11727c478bd9Sstevel@tonic-gate */
11737c478bd9Sstevel@tonic-gate static void
do_rc_files(Session * s,const char * shell)11747c478bd9Sstevel@tonic-gate do_rc_files(Session *s, const char *shell)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate FILE *f = NULL;
11777c478bd9Sstevel@tonic-gate char cmd[1024];
11787c478bd9Sstevel@tonic-gate int do_xauth;
11797c478bd9Sstevel@tonic-gate struct stat st;
11807c478bd9Sstevel@tonic-gate
11817c478bd9Sstevel@tonic-gate do_xauth =
11827c478bd9Sstevel@tonic-gate s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
11837c478bd9Sstevel@tonic-gate
11847c478bd9Sstevel@tonic-gate /* ignore _PATH_SSH_USER_RC for subsystems */
11857c478bd9Sstevel@tonic-gate if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
11867c478bd9Sstevel@tonic-gate snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
11877c478bd9Sstevel@tonic-gate shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
11887c478bd9Sstevel@tonic-gate if (debug_flag)
11897c478bd9Sstevel@tonic-gate fprintf(stderr, "Running %s\n", cmd);
11907c478bd9Sstevel@tonic-gate f = popen(cmd, "w");
11917c478bd9Sstevel@tonic-gate if (f) {
11927c478bd9Sstevel@tonic-gate if (do_xauth)
11937c478bd9Sstevel@tonic-gate fprintf(f, "%s %s\n", s->auth_proto,
11947c478bd9Sstevel@tonic-gate s->auth_data);
11957c478bd9Sstevel@tonic-gate pclose(f);
11967c478bd9Sstevel@tonic-gate } else
11977c478bd9Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n",
11987c478bd9Sstevel@tonic-gate _PATH_SSH_USER_RC);
11997c478bd9Sstevel@tonic-gate } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
12007c478bd9Sstevel@tonic-gate if (debug_flag)
12017c478bd9Sstevel@tonic-gate fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
12027c478bd9Sstevel@tonic-gate _PATH_SSH_SYSTEM_RC);
12037c478bd9Sstevel@tonic-gate f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
12047c478bd9Sstevel@tonic-gate if (f) {
12057c478bd9Sstevel@tonic-gate if (do_xauth)
12067c478bd9Sstevel@tonic-gate fprintf(f, "%s %s\n", s->auth_proto,
12077c478bd9Sstevel@tonic-gate s->auth_data);
12087c478bd9Sstevel@tonic-gate pclose(f);
12097c478bd9Sstevel@tonic-gate } else
12107c478bd9Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n",
12117c478bd9Sstevel@tonic-gate _PATH_SSH_SYSTEM_RC);
12127c478bd9Sstevel@tonic-gate } else if (do_xauth && options.xauth_location != NULL) {
12137c478bd9Sstevel@tonic-gate /* Add authority data to .Xauthority if appropriate. */
12147c478bd9Sstevel@tonic-gate if (debug_flag) {
12157c478bd9Sstevel@tonic-gate fprintf(stderr,
12167c478bd9Sstevel@tonic-gate "Running %.500s add "
12177c478bd9Sstevel@tonic-gate "%.100s %.100s %.100s\n",
12187c478bd9Sstevel@tonic-gate options.xauth_location, s->auth_display,
12197c478bd9Sstevel@tonic-gate s->auth_proto, s->auth_data);
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate snprintf(cmd, sizeof cmd, "%s -q -",
12227c478bd9Sstevel@tonic-gate options.xauth_location);
12237c478bd9Sstevel@tonic-gate f = popen(cmd, "w");
12247c478bd9Sstevel@tonic-gate if (f) {
12257c478bd9Sstevel@tonic-gate fprintf(f, "add %s %s %s\n",
12267c478bd9Sstevel@tonic-gate s->auth_display, s->auth_proto,
12277c478bd9Sstevel@tonic-gate s->auth_data);
12287c478bd9Sstevel@tonic-gate pclose(f);
12297c478bd9Sstevel@tonic-gate } else {
12307c478bd9Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n",
12317c478bd9Sstevel@tonic-gate cmd);
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate }
12357c478bd9Sstevel@tonic-gate
1236fbaf5b3eSJan Pechanec /* Disallow logins if /etc/nologin exists. This does not apply to root. */
12377c478bd9Sstevel@tonic-gate static void
do_nologin(struct passwd * pw)12387c478bd9Sstevel@tonic-gate do_nologin(struct passwd *pw)
12397c478bd9Sstevel@tonic-gate {
12407c478bd9Sstevel@tonic-gate FILE *f = NULL;
12417c478bd9Sstevel@tonic-gate char buf[1024];
1242fbaf5b3eSJan Pechanec struct stat sb;
12437c478bd9Sstevel@tonic-gate
1244fbaf5b3eSJan Pechanec if (pw->pw_uid == 0)
1245fbaf5b3eSJan Pechanec return;
1246fbaf5b3eSJan Pechanec
1247fbaf5b3eSJan Pechanec if (stat(_PATH_NOLOGIN, &sb) == -1)
1248fbaf5b3eSJan Pechanec return;
1249fbaf5b3eSJan Pechanec
1250fbaf5b3eSJan Pechanec /* /etc/nologin exists. Print its contents if we can and exit. */
1251fbaf5b3eSJan Pechanec log("User %.100s not allowed because %s exists.", pw->pw_name,
1252fbaf5b3eSJan Pechanec _PATH_NOLOGIN);
1253fbaf5b3eSJan Pechanec if ((f = fopen(_PATH_NOLOGIN, "r")) != NULL) {
12547c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f))
12557c478bd9Sstevel@tonic-gate fputs(buf, stderr);
12567c478bd9Sstevel@tonic-gate fclose(f);
12577c478bd9Sstevel@tonic-gate }
1258fbaf5b3eSJan Pechanec exit(254);
12597c478bd9Sstevel@tonic-gate }
12607c478bd9Sstevel@tonic-gate
12616f8d59d8SJan Pechanec /* Chroot into ChrootDirectory if the option is set. */
12627c478bd9Sstevel@tonic-gate void
chroot_if_needed(struct passwd * pw)12636f8d59d8SJan Pechanec chroot_if_needed(struct passwd *pw)
12647c478bd9Sstevel@tonic-gate {
12656f8d59d8SJan Pechanec char *chroot_path, *tmp;
12667c478bd9Sstevel@tonic-gate
12676f8d59d8SJan Pechanec if (chroot_requested(options.chroot_directory)) {
12686f8d59d8SJan Pechanec tmp = tilde_expand_filename(options.chroot_directory,
12696f8d59d8SJan Pechanec pw->pw_uid);
12706f8d59d8SJan Pechanec chroot_path = percent_expand(tmp, "h", pw->pw_dir,
12716f8d59d8SJan Pechanec "u", pw->pw_name, (char *)NULL);
12726f8d59d8SJan Pechanec safely_chroot(chroot_path, pw->pw_uid);
12736f8d59d8SJan Pechanec free(tmp);
12746f8d59d8SJan Pechanec free(chroot_path);
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate }
12776f8d59d8SJan Pechanec
12787c478bd9Sstevel@tonic-gate /*
12796f8d59d8SJan Pechanec * Chroot into a directory after checking it for safety: all path components
12806f8d59d8SJan Pechanec * must be root-owned directories with strict permissions.
12817c478bd9Sstevel@tonic-gate */
12826f8d59d8SJan Pechanec static void
safely_chroot(const char * path,uid_t uid)12836f8d59d8SJan Pechanec safely_chroot(const char *path, uid_t uid)
12846f8d59d8SJan Pechanec {
12856f8d59d8SJan Pechanec const char *cp;
12866f8d59d8SJan Pechanec char component[MAXPATHLEN];
12876f8d59d8SJan Pechanec struct stat st;
12886f8d59d8SJan Pechanec
12896f8d59d8SJan Pechanec if (*path != '/')
12906f8d59d8SJan Pechanec fatal("chroot path does not begin at root");
12916f8d59d8SJan Pechanec if (strlen(path) >= sizeof(component))
12926f8d59d8SJan Pechanec fatal("chroot path too long");
12936f8d59d8SJan Pechanec
12946f8d59d8SJan Pechanec /*
12956f8d59d8SJan Pechanec * Descend the path, checking that each component is a
12966f8d59d8SJan Pechanec * root-owned directory with strict permissions.
12976f8d59d8SJan Pechanec */
12986f8d59d8SJan Pechanec for (cp = path; cp != NULL;) {
12996f8d59d8SJan Pechanec if ((cp = strchr(cp, '/')) == NULL)
13006f8d59d8SJan Pechanec strlcpy(component, path, sizeof(component));
13016f8d59d8SJan Pechanec else {
13026f8d59d8SJan Pechanec cp++;
13036f8d59d8SJan Pechanec memcpy(component, path, cp - path);
13046f8d59d8SJan Pechanec component[cp - path] = '\0';
13057c478bd9Sstevel@tonic-gate }
13066f8d59d8SJan Pechanec
13076f8d59d8SJan Pechanec debug3("%s: checking '%s'", __func__, component);
13086f8d59d8SJan Pechanec
13096f8d59d8SJan Pechanec if (stat(component, &st) != 0)
13106f8d59d8SJan Pechanec fatal("%s: stat(\"%s\"): %s", __func__,
13116f8d59d8SJan Pechanec component, strerror(errno));
13126f8d59d8SJan Pechanec if (st.st_uid != 0 || (st.st_mode & 022) != 0)
13136f8d59d8SJan Pechanec fatal("bad ownership or modes for chroot "
13146f8d59d8SJan Pechanec "directory %s\"%s\"",
13156f8d59d8SJan Pechanec cp == NULL ? "" : "component ", component);
13166f8d59d8SJan Pechanec if (!S_ISDIR(st.st_mode))
13176f8d59d8SJan Pechanec fatal("chroot path %s\"%s\" is not a directory",
13186f8d59d8SJan Pechanec cp == NULL ? "" : "component ", component);
13196f8d59d8SJan Pechanec }
13206f8d59d8SJan Pechanec
13216f8d59d8SJan Pechanec if (chdir(path) == -1)
13226f8d59d8SJan Pechanec fatal("Unable to chdir to chroot path \"%s\": "
13236f8d59d8SJan Pechanec "%s", path, strerror(errno));
13246f8d59d8SJan Pechanec if (chroot(path) == -1)
13256f8d59d8SJan Pechanec fatal("chroot(\"%s\"): %s", path, strerror(errno));
13266f8d59d8SJan Pechanec if (chdir("/") == -1)
13276f8d59d8SJan Pechanec fatal("%s: chdir(/) after chroot: %s",
13286f8d59d8SJan Pechanec __func__, strerror(errno));
13296f8d59d8SJan Pechanec verbose("Changed root directory to \"%s\"", path);
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate
13327c478bd9Sstevel@tonic-gate static void
launch_login(struct passwd * pw,const char * hostname)13337c478bd9Sstevel@tonic-gate launch_login(struct passwd *pw, const char *hostname)
13347c478bd9Sstevel@tonic-gate {
13357c478bd9Sstevel@tonic-gate /* Launch login(1). */
13367c478bd9Sstevel@tonic-gate
13377c478bd9Sstevel@tonic-gate execl(LOGIN_PROGRAM, "login", "-h", hostname,
13387c478bd9Sstevel@tonic-gate #ifdef xxxLOGIN_NEEDS_TERM
13397c478bd9Sstevel@tonic-gate (s->term ? s->term : "unknown"),
13407c478bd9Sstevel@tonic-gate #endif /* LOGIN_NEEDS_TERM */
13417c478bd9Sstevel@tonic-gate #ifdef LOGIN_NO_ENDOPT
13427c478bd9Sstevel@tonic-gate "-p", "-f", pw->pw_name, (char *)NULL);
13437c478bd9Sstevel@tonic-gate #else
13447c478bd9Sstevel@tonic-gate "-p", "-f", "--", pw->pw_name, (char *)NULL);
13457c478bd9Sstevel@tonic-gate #endif
13467c478bd9Sstevel@tonic-gate
13477c478bd9Sstevel@tonic-gate /* Login couldn't be executed, die. */
13487c478bd9Sstevel@tonic-gate
13497c478bd9Sstevel@tonic-gate perror("login");
13507c478bd9Sstevel@tonic-gate exit(1);
13517c478bd9Sstevel@tonic-gate }
13527c478bd9Sstevel@tonic-gate
13537c478bd9Sstevel@tonic-gate /*
13547c478bd9Sstevel@tonic-gate * Performs common processing for the child, such as setting up the
13557c478bd9Sstevel@tonic-gate * environment, closing extra file descriptors, setting the user and group
13567c478bd9Sstevel@tonic-gate * ids, and executing the command or shell.
13577c478bd9Sstevel@tonic-gate */
13586f8d59d8SJan Pechanec #define ARGV_MAX 10
13597c478bd9Sstevel@tonic-gate void
do_child(Session * s,const char * command)13607c478bd9Sstevel@tonic-gate do_child(Session *s, const char *command)
13617c478bd9Sstevel@tonic-gate {
13627c478bd9Sstevel@tonic-gate extern char **environ;
13637c478bd9Sstevel@tonic-gate char **env;
13646f8d59d8SJan Pechanec char *argv[ARGV_MAX];
1365b9aa66a7SJan Pechanec const char *shell, *shell0;
13667c478bd9Sstevel@tonic-gate struct passwd *pw = s->pw;
13677c478bd9Sstevel@tonic-gate
13687c478bd9Sstevel@tonic-gate /* remove hostkey from the child's memory */
13697c478bd9Sstevel@tonic-gate destroy_sensitive_data();
13707c478bd9Sstevel@tonic-gate
13717c478bd9Sstevel@tonic-gate do_nologin(pw);
13726f8d59d8SJan Pechanec chroot_if_needed(pw);
13737c478bd9Sstevel@tonic-gate
13747c478bd9Sstevel@tonic-gate /*
13757c478bd9Sstevel@tonic-gate * Get the shell from the password data. An empty shell field is
13767c478bd9Sstevel@tonic-gate * legal, and means /bin/sh.
13777c478bd9Sstevel@tonic-gate */
13787c478bd9Sstevel@tonic-gate shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
13797c478bd9Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
13807c478bd9Sstevel@tonic-gate shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
13817c478bd9Sstevel@tonic-gate #endif
13827c478bd9Sstevel@tonic-gate
13837c478bd9Sstevel@tonic-gate env = do_setup_env(s, shell);
13847c478bd9Sstevel@tonic-gate
13857c478bd9Sstevel@tonic-gate /*
13867c478bd9Sstevel@tonic-gate * Close the connection descriptors; note that this is the child, and
13877c478bd9Sstevel@tonic-gate * the server will still have the socket open, and it is important
13887c478bd9Sstevel@tonic-gate * that we do not shutdown it. Note that the descriptors cannot be
13897c478bd9Sstevel@tonic-gate * closed before building the environment, as we call
13907c478bd9Sstevel@tonic-gate * get_remote_ipaddr there.
13917c478bd9Sstevel@tonic-gate */
13927c478bd9Sstevel@tonic-gate if (packet_get_connection_in() == packet_get_connection_out())
13937c478bd9Sstevel@tonic-gate close(packet_get_connection_in());
13947c478bd9Sstevel@tonic-gate else {
13957c478bd9Sstevel@tonic-gate close(packet_get_connection_in());
13967c478bd9Sstevel@tonic-gate close(packet_get_connection_out());
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate /*
13997c478bd9Sstevel@tonic-gate * Close all descriptors related to channels. They will still remain
14007c478bd9Sstevel@tonic-gate * open in the parent.
14017c478bd9Sstevel@tonic-gate */
14027c478bd9Sstevel@tonic-gate /* XXX better use close-on-exec? -markus */
14037c478bd9Sstevel@tonic-gate channel_close_all();
14047c478bd9Sstevel@tonic-gate
14057c478bd9Sstevel@tonic-gate /*
14067c478bd9Sstevel@tonic-gate * Close any extra file descriptors. Note that there may still be
14077c478bd9Sstevel@tonic-gate * descriptors left by system functions. They will be closed later.
14087c478bd9Sstevel@tonic-gate */
14097c478bd9Sstevel@tonic-gate endpwent();
14107c478bd9Sstevel@tonic-gate
14117c478bd9Sstevel@tonic-gate /*
14126f786aceSNobutomo Nakano * Must switch to the new environment variables so that .ssh/rc,
14136f786aceSNobutomo Nakano * /etc/ssh/sshrc, and xauth are run in the proper environment.
14146f786aceSNobutomo Nakano */
14156f786aceSNobutomo Nakano environ = env;
14166f786aceSNobutomo Nakano
14176f786aceSNobutomo Nakano /*
14186f786aceSNobutomo Nakano * New environment has been installed. We need to update locale
14196f786aceSNobutomo Nakano * so that error messages beyond this point have the proper
14206f786aceSNobutomo Nakano * character encoding.
14216f786aceSNobutomo Nakano */
14226f786aceSNobutomo Nakano (void) setlocale(LC_ALL, "");
14236f786aceSNobutomo Nakano
14246f786aceSNobutomo Nakano /*
14257c478bd9Sstevel@tonic-gate * Close any extra open file descriptors so that we don\'t have them
14267c478bd9Sstevel@tonic-gate * hanging around in clients. Note that we want to do this after
14277c478bd9Sstevel@tonic-gate * initgroups, because at least on Solaris 2.3 it leaves file
14287c478bd9Sstevel@tonic-gate * descriptors open.
14297c478bd9Sstevel@tonic-gate */
14307c478bd9Sstevel@tonic-gate closefrom(STDERR_FILENO + 1);
14317c478bd9Sstevel@tonic-gate
14327c478bd9Sstevel@tonic-gate #ifdef AFS
14337c478bd9Sstevel@tonic-gate /* Try to get AFS tokens for the local cell. */
14347c478bd9Sstevel@tonic-gate if (k_hasafs()) {
14357c478bd9Sstevel@tonic-gate char cell[64];
14367c478bd9Sstevel@tonic-gate
14377c478bd9Sstevel@tonic-gate if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
14387c478bd9Sstevel@tonic-gate krb_afslog(cell, 0);
14397c478bd9Sstevel@tonic-gate
14407c478bd9Sstevel@tonic-gate krb_afslog(0, 0);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate #endif /* AFS */
14437c478bd9Sstevel@tonic-gate
14446f8d59d8SJan Pechanec /* Change current directory to the user's home directory. */
14457c478bd9Sstevel@tonic-gate if (chdir(pw->pw_dir) < 0) {
14466f8d59d8SJan Pechanec /* Suppress missing homedir warning for chroot case */
14476f8d59d8SJan Pechanec if (!chroot_requested(options.chroot_directory))
14486f8d59d8SJan Pechanec fprintf(stderr, "Could not chdir to home "
14496f8d59d8SJan Pechanec "directory %s: %s\n", pw->pw_dir,
14506f8d59d8SJan Pechanec strerror(errno));
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate
14537c478bd9Sstevel@tonic-gate do_rc_files(s, shell);
14547c478bd9Sstevel@tonic-gate
14557c478bd9Sstevel@tonic-gate /* restore SIGPIPE for child */
14567c478bd9Sstevel@tonic-gate signal(SIGPIPE, SIG_DFL);
14577c478bd9Sstevel@tonic-gate
14586f8d59d8SJan Pechanec if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
14596f8d59d8SJan Pechanec int i;
14606f8d59d8SJan Pechanec char *p, *args;
14616f8d59d8SJan Pechanec extern int optind, optreset;
14626f8d59d8SJan Pechanec
14636f8d59d8SJan Pechanec /* This will set the E/P sets here, simulating exec(2). */
14646f8d59d8SJan Pechanec drop_privs(pw->pw_uid);
14656f8d59d8SJan Pechanec
14666f8d59d8SJan Pechanec setproctitle("%s@internal-sftp-server", s->pw->pw_name);
14676f8d59d8SJan Pechanec args = xstrdup(command ? command : "sftp-server");
14686f8d59d8SJan Pechanec
14696f8d59d8SJan Pechanec i = 0;
14706f8d59d8SJan Pechanec for ((p = strtok(args, " ")); p != NULL; (p = strtok(NULL, " "))) {
14716f8d59d8SJan Pechanec if (i < ARGV_MAX - 1)
14726f8d59d8SJan Pechanec argv[i++] = p;
14736f8d59d8SJan Pechanec }
14746f8d59d8SJan Pechanec
14756f8d59d8SJan Pechanec argv[i] = NULL;
14766f8d59d8SJan Pechanec optind = optreset = 1;
14776f8d59d8SJan Pechanec __progname = argv[0];
14786f8d59d8SJan Pechanec exit(sftp_server_main(i, argv, s->pw));
14796f8d59d8SJan Pechanec }
14806f8d59d8SJan Pechanec
14817c478bd9Sstevel@tonic-gate /* Get the last component of the shell name. */
14827c478bd9Sstevel@tonic-gate if ((shell0 = strrchr(shell, '/')) != NULL)
14837c478bd9Sstevel@tonic-gate shell0++;
14847c478bd9Sstevel@tonic-gate else
14857c478bd9Sstevel@tonic-gate shell0 = shell;
14867c478bd9Sstevel@tonic-gate
14877c478bd9Sstevel@tonic-gate /*
14887c478bd9Sstevel@tonic-gate * If we have no command, execute the shell. In this case, the shell
14897c478bd9Sstevel@tonic-gate * name to be passed in argv[0] is preceded by '-' to indicate that
14907c478bd9Sstevel@tonic-gate * this is a login shell.
14917c478bd9Sstevel@tonic-gate */
14927c478bd9Sstevel@tonic-gate if (!command) {
14937c478bd9Sstevel@tonic-gate char argv0[256];
14947c478bd9Sstevel@tonic-gate
14957c478bd9Sstevel@tonic-gate /* Start the shell. Set initial character to '-'. */
14967c478bd9Sstevel@tonic-gate argv0[0] = '-';
14977c478bd9Sstevel@tonic-gate
14987c478bd9Sstevel@tonic-gate if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
14997c478bd9Sstevel@tonic-gate >= sizeof(argv0) - 1) {
15007c478bd9Sstevel@tonic-gate errno = EINVAL;
15017c478bd9Sstevel@tonic-gate perror(shell);
15027c478bd9Sstevel@tonic-gate exit(1);
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate
15057c478bd9Sstevel@tonic-gate /* Execute the shell. */
15067c478bd9Sstevel@tonic-gate argv[0] = argv0;
15077c478bd9Sstevel@tonic-gate argv[1] = NULL;
15087c478bd9Sstevel@tonic-gate execve(shell, argv, env);
15097c478bd9Sstevel@tonic-gate
15107c478bd9Sstevel@tonic-gate /* Executing the shell failed. */
15117c478bd9Sstevel@tonic-gate perror(shell);
15127c478bd9Sstevel@tonic-gate exit(1);
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate /*
15157c478bd9Sstevel@tonic-gate * Execute the command using the user's shell. This uses the -c
15167c478bd9Sstevel@tonic-gate * option to execute the command.
15177c478bd9Sstevel@tonic-gate */
15187c478bd9Sstevel@tonic-gate argv[0] = (char *) shell0;
15197c478bd9Sstevel@tonic-gate argv[1] = "-c";
15207c478bd9Sstevel@tonic-gate argv[2] = (char *) command;
15217c478bd9Sstevel@tonic-gate argv[3] = NULL;
15227c478bd9Sstevel@tonic-gate execve(shell, argv, env);
15237c478bd9Sstevel@tonic-gate perror(shell);
15247c478bd9Sstevel@tonic-gate exit(1);
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate
15277c478bd9Sstevel@tonic-gate Session *
session_new(void)15287c478bd9Sstevel@tonic-gate session_new(void)
15297c478bd9Sstevel@tonic-gate {
15307c478bd9Sstevel@tonic-gate int i;
15317c478bd9Sstevel@tonic-gate static int did_init = 0;
15327c478bd9Sstevel@tonic-gate if (!did_init) {
15337c478bd9Sstevel@tonic-gate debug("session_new: init");
15347c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15357c478bd9Sstevel@tonic-gate sessions[i].used = 0;
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate did_init = 1;
15387c478bd9Sstevel@tonic-gate }
15397c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15407c478bd9Sstevel@tonic-gate Session *s = &sessions[i];
15417c478bd9Sstevel@tonic-gate if (! s->used) {
15427c478bd9Sstevel@tonic-gate memset(s, 0, sizeof(*s));
15437c478bd9Sstevel@tonic-gate s->chanid = -1;
15447c478bd9Sstevel@tonic-gate s->ptyfd = -1;
15457c478bd9Sstevel@tonic-gate s->ttyfd = -1;
15467c478bd9Sstevel@tonic-gate s->used = 1;
15477c478bd9Sstevel@tonic-gate s->self = i;
15487c478bd9Sstevel@tonic-gate s->env = NULL;
15497c478bd9Sstevel@tonic-gate debug("session_new: session %d", i);
15507c478bd9Sstevel@tonic-gate return s;
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate return NULL;
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate
15567c478bd9Sstevel@tonic-gate static void
session_dump(void)15577c478bd9Sstevel@tonic-gate session_dump(void)
15587c478bd9Sstevel@tonic-gate {
15597c478bd9Sstevel@tonic-gate int i;
15607c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15617c478bd9Sstevel@tonic-gate Session *s = &sessions[i];
15627c478bd9Sstevel@tonic-gate debug("dump: used %d session %d %p channel %d pid %ld",
15637c478bd9Sstevel@tonic-gate s->used,
15647c478bd9Sstevel@tonic-gate s->self,
15657c478bd9Sstevel@tonic-gate s,
15667c478bd9Sstevel@tonic-gate s->chanid,
15677c478bd9Sstevel@tonic-gate (long)s->pid);
15687c478bd9Sstevel@tonic-gate }
15697c478bd9Sstevel@tonic-gate }
15707c478bd9Sstevel@tonic-gate
15717c478bd9Sstevel@tonic-gate int
session_open(Authctxt * authctxt,int chanid)15727c478bd9Sstevel@tonic-gate session_open(Authctxt *authctxt, int chanid)
15737c478bd9Sstevel@tonic-gate {
15747c478bd9Sstevel@tonic-gate Session *s = session_new();
15757c478bd9Sstevel@tonic-gate debug("session_open: channel %d", chanid);
15767c478bd9Sstevel@tonic-gate if (s == NULL) {
15777c478bd9Sstevel@tonic-gate error("no more sessions");
15787c478bd9Sstevel@tonic-gate return 0;
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate s->authctxt = authctxt;
15817c478bd9Sstevel@tonic-gate s->pw = authctxt->pw;
15827c478bd9Sstevel@tonic-gate if (s->pw == NULL)
15837c478bd9Sstevel@tonic-gate fatal("no user for session %d", s->self);
15847c478bd9Sstevel@tonic-gate debug("session_open: session %d: link with channel %d", s->self, chanid);
15857c478bd9Sstevel@tonic-gate s->chanid = chanid;
15867c478bd9Sstevel@tonic-gate return 1;
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate
15897c478bd9Sstevel@tonic-gate #ifndef lint
15907c478bd9Sstevel@tonic-gate Session *
session_by_tty(char * tty)15917c478bd9Sstevel@tonic-gate session_by_tty(char *tty)
15927c478bd9Sstevel@tonic-gate {
15937c478bd9Sstevel@tonic-gate int i;
15947c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15957c478bd9Sstevel@tonic-gate Session *s = &sessions[i];
15967c478bd9Sstevel@tonic-gate if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
15977c478bd9Sstevel@tonic-gate debug("session_by_tty: session %d tty %s", i, tty);
15987c478bd9Sstevel@tonic-gate return s;
15997c478bd9Sstevel@tonic-gate }
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate debug("session_by_tty: unknown tty %.100s", tty);
16027c478bd9Sstevel@tonic-gate session_dump();
16037c478bd9Sstevel@tonic-gate return NULL;
16047c478bd9Sstevel@tonic-gate }
16057c478bd9Sstevel@tonic-gate #endif /* lint */
16067c478bd9Sstevel@tonic-gate
16077c478bd9Sstevel@tonic-gate static Session *
session_by_channel(int id)16087c478bd9Sstevel@tonic-gate session_by_channel(int id)
16097c478bd9Sstevel@tonic-gate {
16107c478bd9Sstevel@tonic-gate int i;
16117c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
16127c478bd9Sstevel@tonic-gate Session *s = &sessions[i];
16137c478bd9Sstevel@tonic-gate if (s->used && s->chanid == id) {
16147c478bd9Sstevel@tonic-gate debug("session_by_channel: session %d channel %d", i, id);
16157c478bd9Sstevel@tonic-gate return s;
16167c478bd9Sstevel@tonic-gate }
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate debug("session_by_channel: unknown channel %d", id);
16197c478bd9Sstevel@tonic-gate session_dump();
16207c478bd9Sstevel@tonic-gate return NULL;
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate
16237c478bd9Sstevel@tonic-gate static Session *
session_by_pid(pid_t pid)16247c478bd9Sstevel@tonic-gate session_by_pid(pid_t pid)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate int i;
16277c478bd9Sstevel@tonic-gate debug("session_by_pid: pid %ld", (long)pid);
16287c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
16297c478bd9Sstevel@tonic-gate Session *s = &sessions[i];
16307c478bd9Sstevel@tonic-gate if (s->used && s->pid == pid)
16317c478bd9Sstevel@tonic-gate return s;
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate error("session_by_pid: unknown pid %ld", (long)pid);
16347c478bd9Sstevel@tonic-gate session_dump();
16357c478bd9Sstevel@tonic-gate return NULL;
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate
16387c478bd9Sstevel@tonic-gate static int
session_window_change_req(Session * s)16397c478bd9Sstevel@tonic-gate session_window_change_req(Session *s)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate s->col = packet_get_int();
16427c478bd9Sstevel@tonic-gate s->row = packet_get_int();
16437c478bd9Sstevel@tonic-gate s->xpixel = packet_get_int();
16447c478bd9Sstevel@tonic-gate s->ypixel = packet_get_int();
16457c478bd9Sstevel@tonic-gate packet_check_eom();
16467c478bd9Sstevel@tonic-gate pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
16477c478bd9Sstevel@tonic-gate return 1;
16487c478bd9Sstevel@tonic-gate }
16497c478bd9Sstevel@tonic-gate
16507c478bd9Sstevel@tonic-gate static int
session_pty_req(Session * s)16517c478bd9Sstevel@tonic-gate session_pty_req(Session *s)
16527c478bd9Sstevel@tonic-gate {
16537c478bd9Sstevel@tonic-gate u_int len;
16547c478bd9Sstevel@tonic-gate int n_bytes;
16557c478bd9Sstevel@tonic-gate
16567c478bd9Sstevel@tonic-gate if (no_pty_flag) {
16577c478bd9Sstevel@tonic-gate debug("Allocating a pty not permitted for this authentication.");
16587c478bd9Sstevel@tonic-gate return 0;
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate if (s->ttyfd != -1) {
16617c478bd9Sstevel@tonic-gate packet_disconnect("Protocol error: you already have a pty.");
16627c478bd9Sstevel@tonic-gate return 0;
16637c478bd9Sstevel@tonic-gate }
1664*2de0a7d6SDan McDonald /* Get the time and hostname when the user last logged in. */
1665*2de0a7d6SDan McDonald if (options.print_lastlog) {
1666*2de0a7d6SDan McDonald s->hostname[0] = '\0';
1667*2de0a7d6SDan McDonald s->last_login_time = get_last_login_time(s->pw->pw_uid,
1668*2de0a7d6SDan McDonald s->pw->pw_name, s->hostname, sizeof(s->hostname));
1669*2de0a7d6SDan McDonald
1670*2de0a7d6SDan McDonald /*
1671*2de0a7d6SDan McDonald * PAM may update the last login date.
1672*2de0a7d6SDan McDonald *
1673*2de0a7d6SDan McDonald * Ideally PAM would also show the last login date as a
1674*2de0a7d6SDan McDonald * PAM_TEXT_INFO conversation message, and then we could just
1675*2de0a7d6SDan McDonald * always force the use of keyboard-interactive just so we can
1676*2de0a7d6SDan McDonald * pass any such PAM prompts and messages from the account and
1677*2de0a7d6SDan McDonald * session stacks, but skip pam_authenticate() if other userauth
1678*2de0a7d6SDan McDonald * has succeeded and the user's password isn't expired.
1679*2de0a7d6SDan McDonald *
1680*2de0a7d6SDan McDonald * Unfortunately this depends on support for keyboard-
1681*2de0a7d6SDan McDonald * interactive in the client, and support for lastlog messages
1682*2de0a7d6SDan McDonald * in some PAM module.
1683*2de0a7d6SDan McDonald *
1684*2de0a7d6SDan McDonald * As it is Solaris updates the lastlog in PAM, but does
1685*2de0a7d6SDan McDonald * not show the lastlog date in PAM. If and when this state of
1686*2de0a7d6SDan McDonald * affairs changes this hack can be reconsidered, and, maybe,
1687*2de0a7d6SDan McDonald * removed.
1688*2de0a7d6SDan McDonald *
1689*2de0a7d6SDan McDonald * So we're stuck with a crude hack: get the lastlog
1690*2de0a7d6SDan McDonald * time before calling pam_open_session() and store it
1691*2de0a7d6SDan McDonald * in the Authctxt and then use it here once. After
1692*2de0a7d6SDan McDonald * that, if the client opens any more pty sessions we'll
1693*2de0a7d6SDan McDonald * show the last lastlog entry since userauth.
1694*2de0a7d6SDan McDonald */
1695*2de0a7d6SDan McDonald if (s->authctxt != NULL && s->authctxt->last_login_time > 0) {
1696*2de0a7d6SDan McDonald s->last_login_time = s->authctxt->last_login_time;
1697*2de0a7d6SDan McDonald (void) strlcpy(s->hostname,
1698*2de0a7d6SDan McDonald s->authctxt->last_login_host,
1699*2de0a7d6SDan McDonald sizeof(s->hostname));
1700*2de0a7d6SDan McDonald s->authctxt->last_login_time = 0;
1701*2de0a7d6SDan McDonald s->authctxt->last_login_host[0] = '\0';
1702*2de0a7d6SDan McDonald }
1703*2de0a7d6SDan McDonald }
17047c478bd9Sstevel@tonic-gate
17057c478bd9Sstevel@tonic-gate s->term = packet_get_string(&len);
17067c478bd9Sstevel@tonic-gate
17077c478bd9Sstevel@tonic-gate if (compat20) {
17087c478bd9Sstevel@tonic-gate s->col = packet_get_int();
17097c478bd9Sstevel@tonic-gate s->row = packet_get_int();
17107c478bd9Sstevel@tonic-gate } else {
17117c478bd9Sstevel@tonic-gate s->row = packet_get_int();
17127c478bd9Sstevel@tonic-gate s->col = packet_get_int();
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate s->xpixel = packet_get_int();
17157c478bd9Sstevel@tonic-gate s->ypixel = packet_get_int();
17167c478bd9Sstevel@tonic-gate
17177c478bd9Sstevel@tonic-gate if (strcmp(s->term, "") == 0) {
17187c478bd9Sstevel@tonic-gate xfree(s->term);
17197c478bd9Sstevel@tonic-gate s->term = NULL;
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate
17227c478bd9Sstevel@tonic-gate /* Allocate a pty and open it. */
17237c478bd9Sstevel@tonic-gate debug("Allocating pty.");
17249a8058b5Sjp161948 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
17257c478bd9Sstevel@tonic-gate if (s->term)
17267c478bd9Sstevel@tonic-gate xfree(s->term);
17277c478bd9Sstevel@tonic-gate s->term = NULL;
17287c478bd9Sstevel@tonic-gate s->ptyfd = -1;
17297c478bd9Sstevel@tonic-gate s->ttyfd = -1;
17307c478bd9Sstevel@tonic-gate error("session_pty_req: session %d alloc failed", s->self);
17317c478bd9Sstevel@tonic-gate return 0;
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate debug("session_pty_req: session %d alloc %s", s->self, s->tty);
17347c478bd9Sstevel@tonic-gate
17357c478bd9Sstevel@tonic-gate /* for SSH1 the tty modes length is not given */
17367c478bd9Sstevel@tonic-gate if (!compat20)
17377c478bd9Sstevel@tonic-gate n_bytes = packet_remaining();
17387c478bd9Sstevel@tonic-gate tty_parse_modes(s->ttyfd, &n_bytes);
17397c478bd9Sstevel@tonic-gate
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate * Add a cleanup function to clear the utmp entry and record logout
17427c478bd9Sstevel@tonic-gate * time in case we call fatal() (e.g., the connection gets closed).
17437c478bd9Sstevel@tonic-gate */
17447c478bd9Sstevel@tonic-gate fatal_add_cleanup(session_pty_cleanup, (void *)s);
17457c478bd9Sstevel@tonic-gate pty_setowner(s->pw, s->tty);
17467c478bd9Sstevel@tonic-gate
17477c478bd9Sstevel@tonic-gate /* Set window size from the packet. */
17487c478bd9Sstevel@tonic-gate pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
17497c478bd9Sstevel@tonic-gate
17507c478bd9Sstevel@tonic-gate packet_check_eom();
17517c478bd9Sstevel@tonic-gate session_proctitle(s);
17527c478bd9Sstevel@tonic-gate return 1;
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate
17557c478bd9Sstevel@tonic-gate static int
session_subsystem_req(Session * s)17567c478bd9Sstevel@tonic-gate session_subsystem_req(Session *s)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate struct stat st;
17597c478bd9Sstevel@tonic-gate u_int len;
17607c478bd9Sstevel@tonic-gate int success = 0;
17616f8d59d8SJan Pechanec char *prog, *cmd, *subsys = packet_get_string(&len);
17626f8d59d8SJan Pechanec u_int i;
17637c478bd9Sstevel@tonic-gate
17647c478bd9Sstevel@tonic-gate packet_check_eom();
17657c478bd9Sstevel@tonic-gate log("subsystem request for %.100s", subsys);
17667c478bd9Sstevel@tonic-gate
17677c478bd9Sstevel@tonic-gate for (i = 0; i < options.num_subsystems; i++) {
17687c478bd9Sstevel@tonic-gate if (strcmp(subsys, options.subsystem_name[i]) == 0) {
17696f8d59d8SJan Pechanec prog = options.subsystem_command[i];
17706f8d59d8SJan Pechanec cmd = options.subsystem_args[i];
17716f8d59d8SJan Pechanec if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
17726f8d59d8SJan Pechanec s->is_subsystem = SUBSYSTEM_INT_SFTP;
17736f8d59d8SJan Pechanec /*
17746f8d59d8SJan Pechanec * We must stat(2) the subsystem before we chroot in
17756f8d59d8SJan Pechanec * order to be able to send a proper error message.
17766f8d59d8SJan Pechanec */
17776f8d59d8SJan Pechanec } else if (chroot_requested(options.chroot_directory)) {
17786f8d59d8SJan Pechanec char chdirsub[MAXPATHLEN];
17796f8d59d8SJan Pechanec
17806f8d59d8SJan Pechanec strlcpy(chdirsub, options.chroot_directory,
17816f8d59d8SJan Pechanec sizeof (chdirsub));
17826f8d59d8SJan Pechanec strlcat(chdirsub, "/", sizeof (chdirsub));
17836f8d59d8SJan Pechanec strlcat(chdirsub, prog, sizeof (chdirsub));
17846f8d59d8SJan Pechanec if (stat(chdirsub, &st) < 0) {
17856f8d59d8SJan Pechanec error("subsystem: cannot stat %s under "
17866f8d59d8SJan Pechanec "chroot directory %s: %s", prog,
17876f8d59d8SJan Pechanec options.chroot_directory,
17887c478bd9Sstevel@tonic-gate strerror(errno));
17896f8d59d8SJan Pechanec if (strcmp(subsys, "sftp") == 0)
17906f8d59d8SJan Pechanec error("subsystem: please see "
17916f8d59d8SJan Pechanec "the Subsystem option in "
17926f8d59d8SJan Pechanec "sshd_config(4) for an "
17936f8d59d8SJan Pechanec "explanation of '%s'.",
17946f8d59d8SJan Pechanec INTERNAL_SFTP_NAME);
17957c478bd9Sstevel@tonic-gate break;
17967c478bd9Sstevel@tonic-gate }
17976f8d59d8SJan Pechanec } else if (stat(prog, &st) < 0) {
17986f8d59d8SJan Pechanec error("subsystem: cannot stat %s: %s", prog,
17996f8d59d8SJan Pechanec strerror(errno));
18006f8d59d8SJan Pechanec break;
18016f8d59d8SJan Pechanec } else {
18026f8d59d8SJan Pechanec s->is_subsystem = SUBSYSTEM_EXT;
18036f8d59d8SJan Pechanec }
18047c478bd9Sstevel@tonic-gate debug("subsystem: exec() %s", cmd);
18057c478bd9Sstevel@tonic-gate do_exec(s, cmd);
18067c478bd9Sstevel@tonic-gate success = 1;
18077c478bd9Sstevel@tonic-gate break;
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate
18117c478bd9Sstevel@tonic-gate if (!success)
18127c478bd9Sstevel@tonic-gate log("subsystem request for %.100s failed, subsystem not found",
18137c478bd9Sstevel@tonic-gate subsys);
18147c478bd9Sstevel@tonic-gate
18157c478bd9Sstevel@tonic-gate xfree(subsys);
18167c478bd9Sstevel@tonic-gate return success;
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate
1819a6e0e77dSjp161948 /*
1820a6e0e77dSjp161948 * Serve "x11-req" channel request for X11 forwarding for the current session
1821a6e0e77dSjp161948 * channel.
1822a6e0e77dSjp161948 */
18237c478bd9Sstevel@tonic-gate static int
session_x11_req(Session * s)18247c478bd9Sstevel@tonic-gate session_x11_req(Session *s)
18257c478bd9Sstevel@tonic-gate {
1826c2a2f8dcSjp161948 int success, fd;
1827c2a2f8dcSjp161948 char xauthdir[] = "/tmp/ssh-xauth-XXXXXX";
18287c478bd9Sstevel@tonic-gate
18297c478bd9Sstevel@tonic-gate s->single_connection = packet_get_char();
18307c478bd9Sstevel@tonic-gate s->auth_proto = packet_get_string(NULL);
18317c478bd9Sstevel@tonic-gate s->auth_data = packet_get_string(NULL);
18327c478bd9Sstevel@tonic-gate s->screen = packet_get_int();
18337c478bd9Sstevel@tonic-gate packet_check_eom();
18347c478bd9Sstevel@tonic-gate
18357c478bd9Sstevel@tonic-gate success = session_setup_x11fwd(s);
18367c478bd9Sstevel@tonic-gate if (!success) {
18377c478bd9Sstevel@tonic-gate xfree(s->auth_proto);
18387c478bd9Sstevel@tonic-gate xfree(s->auth_data);
18397c478bd9Sstevel@tonic-gate s->auth_proto = NULL;
18407c478bd9Sstevel@tonic-gate s->auth_data = NULL;
1841743541abSjp161948 return (success);
18427c478bd9Sstevel@tonic-gate }
1843a6e0e77dSjp161948
1844a6e0e77dSjp161948 /*
1845a6e0e77dSjp161948 * Create per session X authority file so that different sessions
1846a6e0e77dSjp161948 * don't contend for one common file. The reason for this is that
1847a6e0e77dSjp161948 * xauth(1) locking doesn't work too well over network filesystems.
1848a6e0e77dSjp161948 *
1849c2a2f8dcSjp161948 * If mkdtemp() or open() fails then s->auth_file remains NULL which
1850c2a2f8dcSjp161948 * means that we won't set XAUTHORITY variable in child's environment
1851c2a2f8dcSjp161948 * and xauth(1) will use the default location for the authority file.
1852a6e0e77dSjp161948 */
1853743541abSjp161948 if (mkdtemp(xauthdir) != NULL) {
1854a6e0e77dSjp161948 s->auth_file = xmalloc(MAXPATHLEN);
1855a6e0e77dSjp161948 snprintf(s->auth_file, MAXPATHLEN, "%s/xauthfile",
1856a6e0e77dSjp161948 xauthdir);
1857a6e0e77dSjp161948 /*
1858c2a2f8dcSjp161948 * we don't want that "creating new authority file" message to
1859c2a2f8dcSjp161948 * be printed by xauth(1) so we must create that file
1860c2a2f8dcSjp161948 * beforehand.
1861c2a2f8dcSjp161948 */
1862c2a2f8dcSjp161948 if ((fd = open(s->auth_file, O_CREAT | O_EXCL | O_RDONLY,
1863c2a2f8dcSjp161948 S_IRUSR | S_IWUSR)) == -1) {
1864c2a2f8dcSjp161948 error("failed to create the temporary X authority "
1865c2a2f8dcSjp161948 "file %s: %.100s; will use the default one",
1866c2a2f8dcSjp161948 s->auth_file, strerror(errno));
1867c2a2f8dcSjp161948 xfree(s->auth_file);
1868c2a2f8dcSjp161948 s->auth_file = NULL;
1869c2a2f8dcSjp161948 if (rmdir(xauthdir) == -1) {
1870c2a2f8dcSjp161948 error("cannot remove xauth directory %s: %.100s",
1871c2a2f8dcSjp161948 xauthdir, strerror(errno));
1872c2a2f8dcSjp161948 }
1873c2a2f8dcSjp161948 } else {
1874c2a2f8dcSjp161948 close(fd);
1875c2a2f8dcSjp161948 debug("temporary X authority file %s created",
1876c2a2f8dcSjp161948 s->auth_file);
1877c2a2f8dcSjp161948
1878c2a2f8dcSjp161948 /*
1879a6e0e77dSjp161948 * add a cleanup function to remove the temporary
1880a6e0e77dSjp161948 * xauth file in case we call fatal() (e.g., the
1881a6e0e77dSjp161948 * connection gets closed).
1882a6e0e77dSjp161948 */
1883a6e0e77dSjp161948 fatal_add_cleanup(session_xauthfile_cleanup, (void *)s);
1884c2a2f8dcSjp161948 }
1885c2a2f8dcSjp161948 }
1886c2a2f8dcSjp161948 else {
1887c2a2f8dcSjp161948 error("failed to create a directory for the temporary X "
1888c2a2f8dcSjp161948 "authority file: %.100s; will use the default xauth file",
1889c2a2f8dcSjp161948 strerror(errno));
1890a6e0e77dSjp161948 }
1891a6e0e77dSjp161948
1892743541abSjp161948 return (success);
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate
18957c478bd9Sstevel@tonic-gate static int
session_shell_req(Session * s)18967c478bd9Sstevel@tonic-gate session_shell_req(Session *s)
18977c478bd9Sstevel@tonic-gate {
18987c478bd9Sstevel@tonic-gate packet_check_eom();
18997c478bd9Sstevel@tonic-gate do_exec(s, NULL);
19007c478bd9Sstevel@tonic-gate return 1;
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate
19037c478bd9Sstevel@tonic-gate static int
session_exec_req(Session * s)19047c478bd9Sstevel@tonic-gate session_exec_req(Session *s)
19057c478bd9Sstevel@tonic-gate {
19067c478bd9Sstevel@tonic-gate u_int len;
19077c478bd9Sstevel@tonic-gate char *command = packet_get_string(&len);
19087c478bd9Sstevel@tonic-gate packet_check_eom();
19097c478bd9Sstevel@tonic-gate do_exec(s, command);
19107c478bd9Sstevel@tonic-gate xfree(command);
19117c478bd9Sstevel@tonic-gate return 1;
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate
19147c478bd9Sstevel@tonic-gate static int
session_auth_agent_req(Session * s)19157c478bd9Sstevel@tonic-gate session_auth_agent_req(Session *s)
19167c478bd9Sstevel@tonic-gate {
19177c478bd9Sstevel@tonic-gate static int called = 0;
19187c478bd9Sstevel@tonic-gate packet_check_eom();
19197c478bd9Sstevel@tonic-gate if (no_agent_forwarding_flag) {
19207c478bd9Sstevel@tonic-gate debug("session_auth_agent_req: no_agent_forwarding_flag");
19217c478bd9Sstevel@tonic-gate return 0;
19227c478bd9Sstevel@tonic-gate }
19237c478bd9Sstevel@tonic-gate if (called) {
19247c478bd9Sstevel@tonic-gate return 0;
19257c478bd9Sstevel@tonic-gate } else {
19267c478bd9Sstevel@tonic-gate called = 1;
19277c478bd9Sstevel@tonic-gate return auth_input_request_forwarding(s->pw);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate
19317c478bd9Sstevel@tonic-gate static int
session_loc_env_check(char * var,char * val)19327c478bd9Sstevel@tonic-gate session_loc_env_check(char *var, char *val)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate char *current;
19357c478bd9Sstevel@tonic-gate int cat, ret;
19367c478bd9Sstevel@tonic-gate
19377c478bd9Sstevel@tonic-gate if (strcmp(var, "LANG") == 0)
19387c478bd9Sstevel@tonic-gate cat = LC_ALL;
19397c478bd9Sstevel@tonic-gate else if (strcmp(var, "LC_ALL") == 0)
19407c478bd9Sstevel@tonic-gate cat = LC_ALL;
19417c478bd9Sstevel@tonic-gate else if (strcmp(var, "LC_CTYPE") == 0)
19427c478bd9Sstevel@tonic-gate cat = LC_CTYPE;
19437c478bd9Sstevel@tonic-gate else if (strcmp(var, "LC_COLLATE") == 0)
19447c478bd9Sstevel@tonic-gate cat = LC_COLLATE;
19457c478bd9Sstevel@tonic-gate else if (strcmp(var, "LC_TIME") == 0)
19467c478bd9Sstevel@tonic-gate cat = LC_TIME;
19477c478bd9Sstevel@tonic-gate else if (strcmp(var, "LC_NUMERIC") == 0)
19487c478bd9Sstevel@tonic-gate cat = LC_NUMERIC;
19497c478bd9Sstevel@tonic-gate else if (strcmp(var, "LC_MONETARY") == 0)
19507c478bd9Sstevel@tonic-gate cat = LC_MONETARY;
19517c478bd9Sstevel@tonic-gate else if (strcmp(var, "LC_MESSAGES") == 0)
19527c478bd9Sstevel@tonic-gate cat = LC_MESSAGES;
19537c478bd9Sstevel@tonic-gate
1954cd7d5fafSJan Pechanec current = setlocale(cat, NULL);
19557c478bd9Sstevel@tonic-gate
19567c478bd9Sstevel@tonic-gate ret = (setlocale(cat, val) != NULL);
19577c478bd9Sstevel@tonic-gate (void) setlocale(cat, current);
19587c478bd9Sstevel@tonic-gate return (ret);
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate
19617c478bd9Sstevel@tonic-gate static int
session_env_req(Session * s)19627c478bd9Sstevel@tonic-gate session_env_req(Session *s)
19637c478bd9Sstevel@tonic-gate {
19647c478bd9Sstevel@tonic-gate Channel *c;
19657c478bd9Sstevel@tonic-gate char *var, *val, *e;
19667c478bd9Sstevel@tonic-gate char **p;
19677c478bd9Sstevel@tonic-gate size_t len;
19687c478bd9Sstevel@tonic-gate int ret = 0;
19697c478bd9Sstevel@tonic-gate
19707c478bd9Sstevel@tonic-gate /* Get var/val from the rest of this packet */
19717c478bd9Sstevel@tonic-gate var = packet_get_string(NULL);
19727c478bd9Sstevel@tonic-gate val = packet_get_string(NULL);
19737c478bd9Sstevel@tonic-gate
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate * We'll need the channel ID for the packet_send_debug messages,
19767c478bd9Sstevel@tonic-gate * so get it now.
19777c478bd9Sstevel@tonic-gate */
19787c478bd9Sstevel@tonic-gate if ((c = channel_lookup(s->chanid)) == NULL)
19797c478bd9Sstevel@tonic-gate goto done; /* shouldn't happen! */
19807c478bd9Sstevel@tonic-gate
19817c478bd9Sstevel@tonic-gate debug2("Received request for environment variable %s=%s", var, val);
19827c478bd9Sstevel@tonic-gate
19837c478bd9Sstevel@tonic-gate /* For now allow only LANG and LC_* */
19847c478bd9Sstevel@tonic-gate if (strcmp(var, "LANG") != 0 && strncmp(var, "LC_", 3) != 0) {
19857c478bd9Sstevel@tonic-gate debug2("Rejecting request for environment variable %s", var);
19867c478bd9Sstevel@tonic-gate goto done;
19877c478bd9Sstevel@tonic-gate }
19887c478bd9Sstevel@tonic-gate
19897c478bd9Sstevel@tonic-gate if (!session_loc_env_check(var, val)) {
19907c478bd9Sstevel@tonic-gate packet_send_debug(gettext("Missing locale support for %s=%s"),
19917c478bd9Sstevel@tonic-gate var, val);
19927c478bd9Sstevel@tonic-gate goto done;
19937c478bd9Sstevel@tonic-gate }
19947c478bd9Sstevel@tonic-gate
19957c478bd9Sstevel@tonic-gate packet_send_debug(gettext("Channel %d set: %s=%s"), c->remote_id,
19967c478bd9Sstevel@tonic-gate var, val);
19977c478bd9Sstevel@tonic-gate
19987c478bd9Sstevel@tonic-gate /*
19997c478bd9Sstevel@tonic-gate * Always append new environment variables without regard to old
20007c478bd9Sstevel@tonic-gate * ones being overriden. The way these are actually added to
20017c478bd9Sstevel@tonic-gate * the environment of the session process later settings
20027c478bd9Sstevel@tonic-gate * override earlier ones; see copy_environment().
20037c478bd9Sstevel@tonic-gate */
20047c478bd9Sstevel@tonic-gate if (s->env == NULL) {
20057c478bd9Sstevel@tonic-gate char **env;
20067c478bd9Sstevel@tonic-gate
20077c478bd9Sstevel@tonic-gate env = xmalloc(sizeof (char **) * 2);
20087c478bd9Sstevel@tonic-gate memset(env, 0, sizeof (char **) * 2);
20097c478bd9Sstevel@tonic-gate
20107c478bd9Sstevel@tonic-gate s->env = env;
20117c478bd9Sstevel@tonic-gate p = env;
20127c478bd9Sstevel@tonic-gate } else {
20137c478bd9Sstevel@tonic-gate for (p = s->env; *p != NULL ; p++);
20147c478bd9Sstevel@tonic-gate
20157c478bd9Sstevel@tonic-gate s->env = xrealloc(s->env, (p - s->env + 2) * sizeof (char **));
20167c478bd9Sstevel@tonic-gate
20177c478bd9Sstevel@tonic-gate for (p = s->env; *p != NULL ; p++);
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate
20207c478bd9Sstevel@tonic-gate len = snprintf(NULL, 0, "%s=%s", var, val);
20217c478bd9Sstevel@tonic-gate e = xmalloc(len + 1);
20227c478bd9Sstevel@tonic-gate (void) snprintf(e, len + 1, "%s=%s", var, val);
20237c478bd9Sstevel@tonic-gate
20247c478bd9Sstevel@tonic-gate (*p++) = e;
20257c478bd9Sstevel@tonic-gate *p = NULL;
20267c478bd9Sstevel@tonic-gate
20277c478bd9Sstevel@tonic-gate ret = 1;
20287c478bd9Sstevel@tonic-gate
20297c478bd9Sstevel@tonic-gate done:
20307c478bd9Sstevel@tonic-gate xfree(var);
20317c478bd9Sstevel@tonic-gate xfree(val);
20327c478bd9Sstevel@tonic-gate
20337c478bd9Sstevel@tonic-gate return (ret);
20347c478bd9Sstevel@tonic-gate }
20357c478bd9Sstevel@tonic-gate
20367c478bd9Sstevel@tonic-gate static void
session_free_env(char *** envp)20377c478bd9Sstevel@tonic-gate session_free_env(char ***envp)
20387c478bd9Sstevel@tonic-gate {
20397c478bd9Sstevel@tonic-gate char **env, **p;
20407c478bd9Sstevel@tonic-gate
20417c478bd9Sstevel@tonic-gate if (envp == NULL || *envp == NULL)
20427c478bd9Sstevel@tonic-gate return;
20437c478bd9Sstevel@tonic-gate
20447c478bd9Sstevel@tonic-gate env = *envp;
20457c478bd9Sstevel@tonic-gate
20467c478bd9Sstevel@tonic-gate *envp = NULL;
20477c478bd9Sstevel@tonic-gate
20487c478bd9Sstevel@tonic-gate for (p = env; *p != NULL; p++)
20497c478bd9Sstevel@tonic-gate xfree(*p);
20507c478bd9Sstevel@tonic-gate
20517c478bd9Sstevel@tonic-gate xfree(env);
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate
20547c478bd9Sstevel@tonic-gate int
session_input_channel_req(Channel * c,const char * rtype)20557c478bd9Sstevel@tonic-gate session_input_channel_req(Channel *c, const char *rtype)
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate int success = 0;
20587c478bd9Sstevel@tonic-gate Session *s;
20597c478bd9Sstevel@tonic-gate
20607c478bd9Sstevel@tonic-gate if ((s = session_by_channel(c->self)) == NULL) {
20617c478bd9Sstevel@tonic-gate log("session_input_channel_req: no session %d req %.100s",
20627c478bd9Sstevel@tonic-gate c->self, rtype);
20637c478bd9Sstevel@tonic-gate return 0;
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate debug("session_input_channel_req: session %d req %s", s->self, rtype);
20667c478bd9Sstevel@tonic-gate
20677c478bd9Sstevel@tonic-gate /*
20687c478bd9Sstevel@tonic-gate * a session is in LARVAL state until a shell, a command
20697c478bd9Sstevel@tonic-gate * or a subsystem is executed
20707c478bd9Sstevel@tonic-gate */
20717c478bd9Sstevel@tonic-gate if (c->type == SSH_CHANNEL_LARVAL) {
20727c478bd9Sstevel@tonic-gate if (strcmp(rtype, "shell") == 0) {
20737c478bd9Sstevel@tonic-gate success = session_shell_req(s);
20747c478bd9Sstevel@tonic-gate } else if (strcmp(rtype, "exec") == 0) {
20757c478bd9Sstevel@tonic-gate success = session_exec_req(s);
20767c478bd9Sstevel@tonic-gate } else if (strcmp(rtype, "pty-req") == 0) {
20777c478bd9Sstevel@tonic-gate success = session_pty_req(s);
20787c478bd9Sstevel@tonic-gate } else if (strcmp(rtype, "x11-req") == 0) {
20797c478bd9Sstevel@tonic-gate success = session_x11_req(s);
20807c478bd9Sstevel@tonic-gate } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
20817c478bd9Sstevel@tonic-gate success = session_auth_agent_req(s);
20827c478bd9Sstevel@tonic-gate } else if (strcmp(rtype, "subsystem") == 0) {
20837c478bd9Sstevel@tonic-gate success = session_subsystem_req(s);
20847c478bd9Sstevel@tonic-gate } else if (strcmp(rtype, "env") == 0) {
20857c478bd9Sstevel@tonic-gate success = session_env_req(s);
20867c478bd9Sstevel@tonic-gate }
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate if (strcmp(rtype, "window-change") == 0) {
20897c478bd9Sstevel@tonic-gate success = session_window_change_req(s);
20907c478bd9Sstevel@tonic-gate }
20917c478bd9Sstevel@tonic-gate return success;
20927c478bd9Sstevel@tonic-gate }
20937c478bd9Sstevel@tonic-gate
20947c478bd9Sstevel@tonic-gate void
session_set_fds(Session * s,int fdin,int fdout,int fderr)20957c478bd9Sstevel@tonic-gate session_set_fds(Session *s, int fdin, int fdout, int fderr)
20967c478bd9Sstevel@tonic-gate {
20977c478bd9Sstevel@tonic-gate if (!compat20)
20987c478bd9Sstevel@tonic-gate fatal("session_set_fds: called for proto != 2.0");
20997c478bd9Sstevel@tonic-gate /*
21007c478bd9Sstevel@tonic-gate * now that have a child and a pipe to the child,
21017c478bd9Sstevel@tonic-gate * we can activate our channel and register the fd's
21027c478bd9Sstevel@tonic-gate */
21037c478bd9Sstevel@tonic-gate if (s->chanid == -1)
21047c478bd9Sstevel@tonic-gate fatal("no channel for session %d", s->self);
21057c478bd9Sstevel@tonic-gate channel_set_fds(s->chanid,
21067c478bd9Sstevel@tonic-gate fdout, fdin, fderr,
21077c478bd9Sstevel@tonic-gate fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
21087c478bd9Sstevel@tonic-gate 1,
21097c478bd9Sstevel@tonic-gate CHAN_SES_WINDOW_DEFAULT);
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate
21127c478bd9Sstevel@tonic-gate /*
21137c478bd9Sstevel@tonic-gate * Function to perform pty cleanup. Also called if we get aborted abnormally
21147c478bd9Sstevel@tonic-gate * (e.g., due to a dropped connection).
21157c478bd9Sstevel@tonic-gate */
21167c478bd9Sstevel@tonic-gate void
session_pty_cleanup2(void * session)21177c478bd9Sstevel@tonic-gate session_pty_cleanup2(void *session)
21187c478bd9Sstevel@tonic-gate {
21197c478bd9Sstevel@tonic-gate Session *s = session;
21207c478bd9Sstevel@tonic-gate
21217c478bd9Sstevel@tonic-gate if (s == NULL) {
21227c478bd9Sstevel@tonic-gate error("session_pty_cleanup: no session");
21237c478bd9Sstevel@tonic-gate return;
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate if (s->ttyfd == -1)
21267c478bd9Sstevel@tonic-gate return;
21277c478bd9Sstevel@tonic-gate
21287c478bd9Sstevel@tonic-gate debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
21297c478bd9Sstevel@tonic-gate
21307c478bd9Sstevel@tonic-gate #ifdef USE_PAM
21317c478bd9Sstevel@tonic-gate session_do_pam(s, 0);
21327c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
21337c478bd9Sstevel@tonic-gate
21347c478bd9Sstevel@tonic-gate /* Record that the user has logged out. */
21357c478bd9Sstevel@tonic-gate if (s->pid != 0) {
2136b9aa66a7SJan Pechanec debug3("Recording SSHv2 channel logout in utmpx/wtmpx");
21377c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
21387c478bd9Sstevel@tonic-gate altprivsep_record_logout(s->pid);
21397c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate
21427c478bd9Sstevel@tonic-gate /* Release the pseudo-tty. */
21437c478bd9Sstevel@tonic-gate if (getuid() == 0)
21447c478bd9Sstevel@tonic-gate pty_release(s->tty);
21457c478bd9Sstevel@tonic-gate
21467c478bd9Sstevel@tonic-gate /*
21477c478bd9Sstevel@tonic-gate * Close the server side of the socket pairs. We must do this after
21487c478bd9Sstevel@tonic-gate * the pty cleanup, so that another process doesn't get this pty
21497c478bd9Sstevel@tonic-gate * while we're still cleaning up.
21507c478bd9Sstevel@tonic-gate */
21517c478bd9Sstevel@tonic-gate if (close(s->ptymaster) < 0)
21527c478bd9Sstevel@tonic-gate error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
21537c478bd9Sstevel@tonic-gate
21547c478bd9Sstevel@tonic-gate /* unlink pty from session */
21557c478bd9Sstevel@tonic-gate s->ttyfd = -1;
21567c478bd9Sstevel@tonic-gate }
21577c478bd9Sstevel@tonic-gate
21587c478bd9Sstevel@tonic-gate void
session_pty_cleanup(void * session)21597c478bd9Sstevel@tonic-gate session_pty_cleanup(void *session)
21607c478bd9Sstevel@tonic-gate {
21619a8058b5Sjp161948 session_pty_cleanup2(session);
21627c478bd9Sstevel@tonic-gate }
21637c478bd9Sstevel@tonic-gate
2164a6e0e77dSjp161948 /*
2165a6e0e77dSjp161948 * We use a different temporary X authority file per every session so we
2166a6e0e77dSjp161948 * should remove those files when fatal() is called.
2167a6e0e77dSjp161948 */
2168a6e0e77dSjp161948 void
session_xauthfile_cleanup(void * session)2169a6e0e77dSjp161948 session_xauthfile_cleanup(void *session)
2170a6e0e77dSjp161948 {
2171a6e0e77dSjp161948 Session *s = session;
2172a6e0e77dSjp161948
2173a6e0e77dSjp161948 if (s == NULL) {
2174a6e0e77dSjp161948 error("session_xauthfile_cleanup: no session");
2175a6e0e77dSjp161948 return;
2176a6e0e77dSjp161948 }
2177a6e0e77dSjp161948
2178a6e0e77dSjp161948 debug("session_xauthfile_cleanup: session %d removing %s", s->self,
2179a6e0e77dSjp161948 s->auth_file);
2180a6e0e77dSjp161948
2181a6e0e77dSjp161948 if (unlink(s->auth_file) == -1) {
2182a6e0e77dSjp161948 error("session_xauthfile_cleanup: cannot remove xauth file: "
2183a6e0e77dSjp161948 "%.100s", strerror(errno));
2184a6e0e77dSjp161948 return;
2185a6e0e77dSjp161948 }
2186a6e0e77dSjp161948
2187a6e0e77dSjp161948 /* dirname() will modify s->auth_file but that's ok */
2188a6e0e77dSjp161948 if (rmdir(dirname(s->auth_file)) == -1) {
2189a6e0e77dSjp161948 error("session_xauthfile_cleanup: "
2190a6e0e77dSjp161948 "cannot remove xauth directory: %.100s", strerror(errno));
2191a6e0e77dSjp161948 return;
2192a6e0e77dSjp161948 }
2193a6e0e77dSjp161948 }
2194a6e0e77dSjp161948
21957c478bd9Sstevel@tonic-gate static char *
sig2name(int sig)21967c478bd9Sstevel@tonic-gate sig2name(int sig)
21977c478bd9Sstevel@tonic-gate {
21987c478bd9Sstevel@tonic-gate #define SSH_SIG(x) if (sig == SIG ## x) return #x
21997c478bd9Sstevel@tonic-gate SSH_SIG(ABRT);
22007c478bd9Sstevel@tonic-gate SSH_SIG(ALRM);
22017c478bd9Sstevel@tonic-gate SSH_SIG(FPE);
22027c478bd9Sstevel@tonic-gate SSH_SIG(HUP);
22037c478bd9Sstevel@tonic-gate SSH_SIG(ILL);
22047c478bd9Sstevel@tonic-gate SSH_SIG(INT);
22057c478bd9Sstevel@tonic-gate SSH_SIG(KILL);
22067c478bd9Sstevel@tonic-gate SSH_SIG(PIPE);
22077c478bd9Sstevel@tonic-gate SSH_SIG(QUIT);
22087c478bd9Sstevel@tonic-gate SSH_SIG(SEGV);
22097c478bd9Sstevel@tonic-gate SSH_SIG(TERM);
22107c478bd9Sstevel@tonic-gate SSH_SIG(USR1);
22117c478bd9Sstevel@tonic-gate SSH_SIG(USR2);
22127c478bd9Sstevel@tonic-gate #undef SSH_SIG
22137c478bd9Sstevel@tonic-gate return "SIG@openssh.com";
22147c478bd9Sstevel@tonic-gate }
22157c478bd9Sstevel@tonic-gate
22167c478bd9Sstevel@tonic-gate static void
session_exit_message(Session * s,int status)22177c478bd9Sstevel@tonic-gate session_exit_message(Session *s, int status)
22187c478bd9Sstevel@tonic-gate {
22197c478bd9Sstevel@tonic-gate Channel *c;
22207c478bd9Sstevel@tonic-gate
22217c478bd9Sstevel@tonic-gate if ((c = channel_lookup(s->chanid)) == NULL)
22227c478bd9Sstevel@tonic-gate fatal("session_exit_message: session %d: no channel %d",
22237c478bd9Sstevel@tonic-gate s->self, s->chanid);
22247c478bd9Sstevel@tonic-gate debug("session_exit_message: session %d channel %d pid %ld",
22257c478bd9Sstevel@tonic-gate s->self, s->chanid, (long)s->pid);
22267c478bd9Sstevel@tonic-gate
22277c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) {
22287c478bd9Sstevel@tonic-gate channel_request_start(s->chanid, "exit-status", 0);
22297c478bd9Sstevel@tonic-gate packet_put_int(WEXITSTATUS(status));
22307c478bd9Sstevel@tonic-gate packet_send();
22317c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) {
22327c478bd9Sstevel@tonic-gate channel_request_start(s->chanid, "exit-signal", 0);
22337c478bd9Sstevel@tonic-gate packet_put_cstring(sig2name(WTERMSIG(status)));
22347c478bd9Sstevel@tonic-gate #ifdef WCOREDUMP
22357c478bd9Sstevel@tonic-gate packet_put_char(WCOREDUMP(status));
22367c478bd9Sstevel@tonic-gate #else /* WCOREDUMP */
22377c478bd9Sstevel@tonic-gate packet_put_char(0);
22387c478bd9Sstevel@tonic-gate #endif /* WCOREDUMP */
22397c478bd9Sstevel@tonic-gate packet_put_cstring("");
22407c478bd9Sstevel@tonic-gate packet_put_cstring("");
22417c478bd9Sstevel@tonic-gate packet_send();
22427c478bd9Sstevel@tonic-gate } else {
22437c478bd9Sstevel@tonic-gate /* Some weird exit cause. Just exit. */
22447c478bd9Sstevel@tonic-gate packet_disconnect("wait returned status %04x.", status);
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate
22477c478bd9Sstevel@tonic-gate /* Ok to close channel now */
22487c478bd9Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 0);
22497c478bd9Sstevel@tonic-gate
22507c478bd9Sstevel@tonic-gate /* disconnect channel */
22517c478bd9Sstevel@tonic-gate debug("session_exit_message: release channel %d", s->chanid);
22527c478bd9Sstevel@tonic-gate channel_cancel_cleanup(s->chanid);
22537c478bd9Sstevel@tonic-gate /*
22547c478bd9Sstevel@tonic-gate * emulate a write failure with 'chan_write_failed', nobody will be
22557c478bd9Sstevel@tonic-gate * interested in data we write.
22567c478bd9Sstevel@tonic-gate * Note that we must not call 'chan_read_failed', since there could
22577c478bd9Sstevel@tonic-gate * be some more data waiting in the pipe.
22587c478bd9Sstevel@tonic-gate */
22597c478bd9Sstevel@tonic-gate if (c->ostate != CHAN_OUTPUT_CLOSED)
22607c478bd9Sstevel@tonic-gate chan_write_failed(c);
22617c478bd9Sstevel@tonic-gate s->chanid = -1;
22627c478bd9Sstevel@tonic-gate }
22637c478bd9Sstevel@tonic-gate
22647c478bd9Sstevel@tonic-gate void
session_close(Session * s)22657c478bd9Sstevel@tonic-gate session_close(Session *s)
22667c478bd9Sstevel@tonic-gate {
22677c478bd9Sstevel@tonic-gate debug("session_close: session %d pid %ld", s->self, (long)s->pid);
22687c478bd9Sstevel@tonic-gate if (s->ttyfd != -1) {
22697c478bd9Sstevel@tonic-gate fatal_remove_cleanup(session_pty_cleanup, (void *)s);
22707c478bd9Sstevel@tonic-gate session_pty_cleanup(s);
22717c478bd9Sstevel@tonic-gate }
2272a6e0e77dSjp161948 if (s->auth_file != NULL) {
2273a6e0e77dSjp161948 fatal_remove_cleanup(session_xauthfile_cleanup, (void *)s);
2274a6e0e77dSjp161948 session_xauthfile_cleanup(s);
2275a6e0e77dSjp161948 xfree(s->auth_file);
2276a6e0e77dSjp161948 }
22777c478bd9Sstevel@tonic-gate if (s->term)
22787c478bd9Sstevel@tonic-gate xfree(s->term);
22797c478bd9Sstevel@tonic-gate if (s->display)
22807c478bd9Sstevel@tonic-gate xfree(s->display);
22817c478bd9Sstevel@tonic-gate if (s->auth_display)
22827c478bd9Sstevel@tonic-gate xfree(s->auth_display);
22837c478bd9Sstevel@tonic-gate if (s->auth_data)
22847c478bd9Sstevel@tonic-gate xfree(s->auth_data);
22857c478bd9Sstevel@tonic-gate if (s->auth_proto)
22867c478bd9Sstevel@tonic-gate xfree(s->auth_proto);
22877c478bd9Sstevel@tonic-gate if (s->command)
22887c478bd9Sstevel@tonic-gate xfree(s->command);
22897c478bd9Sstevel@tonic-gate session_free_env(&s->env);
22907c478bd9Sstevel@tonic-gate s->used = 0;
22917c478bd9Sstevel@tonic-gate session_proctitle(s);
22927c478bd9Sstevel@tonic-gate }
22937c478bd9Sstevel@tonic-gate
22947c478bd9Sstevel@tonic-gate void
session_close_by_pid(pid_t pid,int status)22957c478bd9Sstevel@tonic-gate session_close_by_pid(pid_t pid, int status)
22967c478bd9Sstevel@tonic-gate {
22977c478bd9Sstevel@tonic-gate Session *s = session_by_pid(pid);
22987c478bd9Sstevel@tonic-gate if (s == NULL) {
22997c478bd9Sstevel@tonic-gate debug("session_close_by_pid: no session for pid %ld",
23007c478bd9Sstevel@tonic-gate (long)pid);
23017c478bd9Sstevel@tonic-gate return;
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate if (s->chanid != -1)
23047c478bd9Sstevel@tonic-gate session_exit_message(s, status);
23057c478bd9Sstevel@tonic-gate session_close(s);
23067c478bd9Sstevel@tonic-gate }
23077c478bd9Sstevel@tonic-gate
23087c478bd9Sstevel@tonic-gate /*
2309a6e0e77dSjp161948 * This is called when a channel dies before the session 'child' itself dies.
2310a6e0e77dSjp161948 * It can happen for example if we exit from an interactive shell before we
2311a6e0e77dSjp161948 * exit from forwarded X11 applications.
23127c478bd9Sstevel@tonic-gate */
23137c478bd9Sstevel@tonic-gate void
session_close_by_channel(int id,void * arg)23147c478bd9Sstevel@tonic-gate session_close_by_channel(int id, void *arg)
23157c478bd9Sstevel@tonic-gate {
23167c478bd9Sstevel@tonic-gate Session *s = session_by_channel(id);
23177c478bd9Sstevel@tonic-gate if (s == NULL) {
23187c478bd9Sstevel@tonic-gate debug("session_close_by_channel: no session for id %d", id);
23197c478bd9Sstevel@tonic-gate return;
23207c478bd9Sstevel@tonic-gate }
23217c478bd9Sstevel@tonic-gate debug("session_close_by_channel: channel %d child %ld",
23227c478bd9Sstevel@tonic-gate id, (long)s->pid);
23237c478bd9Sstevel@tonic-gate if (s->pid != 0) {
23247c478bd9Sstevel@tonic-gate debug("session_close_by_channel: channel %d: has child", id);
23257c478bd9Sstevel@tonic-gate /*
23267c478bd9Sstevel@tonic-gate * delay detach of session, but release pty, since
23277c478bd9Sstevel@tonic-gate * the fd's to the child are already closed
23287c478bd9Sstevel@tonic-gate */
23297c478bd9Sstevel@tonic-gate if (s->ttyfd != -1) {
23307c478bd9Sstevel@tonic-gate fatal_remove_cleanup(session_pty_cleanup, (void *)s);
23317c478bd9Sstevel@tonic-gate session_pty_cleanup(s);
23327c478bd9Sstevel@tonic-gate }
23337c478bd9Sstevel@tonic-gate return;
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate /* detach by removing callback */
23367c478bd9Sstevel@tonic-gate channel_cancel_cleanup(s->chanid);
23377c478bd9Sstevel@tonic-gate s->chanid = -1;
23387c478bd9Sstevel@tonic-gate session_close(s);
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate
23417c478bd9Sstevel@tonic-gate void
session_destroy_all(void (* closefunc)(Session *))23427c478bd9Sstevel@tonic-gate session_destroy_all(void (*closefunc)(Session *))
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate int i;
23457c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
23467c478bd9Sstevel@tonic-gate Session *s = &sessions[i];
23477c478bd9Sstevel@tonic-gate if (s->used) {
23487c478bd9Sstevel@tonic-gate if (closefunc != NULL)
23497c478bd9Sstevel@tonic-gate closefunc(s);
23507c478bd9Sstevel@tonic-gate else
23517c478bd9Sstevel@tonic-gate session_close(s);
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate }
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate
23567c478bd9Sstevel@tonic-gate static char *
session_tty_list(void)23577c478bd9Sstevel@tonic-gate session_tty_list(void)
23587c478bd9Sstevel@tonic-gate {
23597c478bd9Sstevel@tonic-gate static char buf[1024];
23607c478bd9Sstevel@tonic-gate int i;
23617c478bd9Sstevel@tonic-gate buf[0] = '\0';
23627c478bd9Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
23637c478bd9Sstevel@tonic-gate Session *s = &sessions[i];
23647c478bd9Sstevel@tonic-gate if (s->used && s->ttyfd != -1) {
23657c478bd9Sstevel@tonic-gate if (buf[0] != '\0')
23667c478bd9Sstevel@tonic-gate strlcat(buf, ",", sizeof buf);
23677c478bd9Sstevel@tonic-gate strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate }
23707c478bd9Sstevel@tonic-gate if (buf[0] == '\0')
23717c478bd9Sstevel@tonic-gate strlcpy(buf, "notty", sizeof buf);
23727c478bd9Sstevel@tonic-gate return buf;
23737c478bd9Sstevel@tonic-gate }
23747c478bd9Sstevel@tonic-gate
23757c478bd9Sstevel@tonic-gate void
session_proctitle(Session * s)23767c478bd9Sstevel@tonic-gate session_proctitle(Session *s)
23777c478bd9Sstevel@tonic-gate {
23787c478bd9Sstevel@tonic-gate if (s->pw == NULL)
23797c478bd9Sstevel@tonic-gate error("no user for session %d", s->self);
23807c478bd9Sstevel@tonic-gate else
23817c478bd9Sstevel@tonic-gate setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
23827c478bd9Sstevel@tonic-gate }
23837c478bd9Sstevel@tonic-gate
23847c478bd9Sstevel@tonic-gate int
session_setup_x11fwd(Session * s)23857c478bd9Sstevel@tonic-gate session_setup_x11fwd(Session *s)
23867c478bd9Sstevel@tonic-gate {
23877c478bd9Sstevel@tonic-gate struct stat st;
23887c478bd9Sstevel@tonic-gate char display[512], auth_display[512];
23897c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
23907c478bd9Sstevel@tonic-gate
23917c478bd9Sstevel@tonic-gate if (no_x11_forwarding_flag) {
23927c478bd9Sstevel@tonic-gate packet_send_debug("X11 forwarding disabled in user configuration file.");
23937c478bd9Sstevel@tonic-gate return 0;
23947c478bd9Sstevel@tonic-gate }
23957c478bd9Sstevel@tonic-gate if (!options.x11_forwarding) {
23967c478bd9Sstevel@tonic-gate debug("X11 forwarding disabled in server configuration file.");
23977c478bd9Sstevel@tonic-gate return 0;
23987c478bd9Sstevel@tonic-gate }
23997c478bd9Sstevel@tonic-gate if (!options.xauth_location ||
24007c478bd9Sstevel@tonic-gate (stat(options.xauth_location, &st) == -1)) {
24017c478bd9Sstevel@tonic-gate packet_send_debug("No xauth program; cannot forward with spoofing.");
24027c478bd9Sstevel@tonic-gate return 0;
24037c478bd9Sstevel@tonic-gate }
24047c478bd9Sstevel@tonic-gate if (s->display != NULL) {
24057c478bd9Sstevel@tonic-gate debug("X11 display already set.");
24067c478bd9Sstevel@tonic-gate return 0;
24077c478bd9Sstevel@tonic-gate }
24087c478bd9Sstevel@tonic-gate if (x11_create_display_inet(options.x11_display_offset,
24097c478bd9Sstevel@tonic-gate options.x11_use_localhost, s->single_connection,
24107c478bd9Sstevel@tonic-gate &s->display_number) == -1) {
24117c478bd9Sstevel@tonic-gate debug("x11_create_display_inet failed.");
24127c478bd9Sstevel@tonic-gate return 0;
24137c478bd9Sstevel@tonic-gate }
24147c478bd9Sstevel@tonic-gate
24157c478bd9Sstevel@tonic-gate /* Set up a suitable value for the DISPLAY variable. */
24167c478bd9Sstevel@tonic-gate if (gethostname(hostname, sizeof(hostname)) < 0)
24177c478bd9Sstevel@tonic-gate fatal("gethostname: %.100s", strerror(errno));
24187c478bd9Sstevel@tonic-gate /*
24197c478bd9Sstevel@tonic-gate * auth_display must be used as the displayname when the
24207c478bd9Sstevel@tonic-gate * authorization entry is added with xauth(1). This will be
24217c478bd9Sstevel@tonic-gate * different than the DISPLAY string for localhost displays.
24227c478bd9Sstevel@tonic-gate */
24237c478bd9Sstevel@tonic-gate if (options.x11_use_localhost) {
24247c478bd9Sstevel@tonic-gate snprintf(display, sizeof display, "localhost:%u.%u",
24257c478bd9Sstevel@tonic-gate s->display_number, s->screen);
24267c478bd9Sstevel@tonic-gate snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
24277c478bd9Sstevel@tonic-gate s->display_number, s->screen);
24287c478bd9Sstevel@tonic-gate s->display = xstrdup(display);
24297c478bd9Sstevel@tonic-gate s->auth_display = xstrdup(auth_display);
24307c478bd9Sstevel@tonic-gate } else {
24317c478bd9Sstevel@tonic-gate #ifdef IPADDR_IN_DISPLAY
24327c478bd9Sstevel@tonic-gate struct hostent *he;
24337c478bd9Sstevel@tonic-gate struct in_addr my_addr;
24347c478bd9Sstevel@tonic-gate
24357c478bd9Sstevel@tonic-gate he = gethostbyname(hostname);
24367c478bd9Sstevel@tonic-gate if (he == NULL) {
24377c478bd9Sstevel@tonic-gate error("Can't get IP address for X11 DISPLAY.");
24387c478bd9Sstevel@tonic-gate packet_send_debug("Can't get IP address for X11 DISPLAY.");
24397c478bd9Sstevel@tonic-gate return 0;
24407c478bd9Sstevel@tonic-gate }
24417c478bd9Sstevel@tonic-gate memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
24427c478bd9Sstevel@tonic-gate snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
24437c478bd9Sstevel@tonic-gate s->display_number, s->screen);
24447c478bd9Sstevel@tonic-gate #else
24457c478bd9Sstevel@tonic-gate snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
24467c478bd9Sstevel@tonic-gate s->display_number, s->screen);
24477c478bd9Sstevel@tonic-gate #endif
24487c478bd9Sstevel@tonic-gate s->display = xstrdup(display);
24497c478bd9Sstevel@tonic-gate s->auth_display = xstrdup(display);
24507c478bd9Sstevel@tonic-gate }
24517c478bd9Sstevel@tonic-gate
24527c478bd9Sstevel@tonic-gate return 1;
24537c478bd9Sstevel@tonic-gate }
24547c478bd9Sstevel@tonic-gate
24557c478bd9Sstevel@tonic-gate #ifdef USE_PAM
24567c478bd9Sstevel@tonic-gate int session_do_pam_conv(int, struct pam_message **,
24577c478bd9Sstevel@tonic-gate struct pam_response **, void *);
24587c478bd9Sstevel@tonic-gate
24597c478bd9Sstevel@tonic-gate static struct pam_conv session_pam_conv = {
24607c478bd9Sstevel@tonic-gate session_do_pam_conv,
24617c478bd9Sstevel@tonic-gate NULL
24627c478bd9Sstevel@tonic-gate };
24637c478bd9Sstevel@tonic-gate
24647c478bd9Sstevel@tonic-gate static void
session_do_pam(Session * s,int do_open)24657c478bd9Sstevel@tonic-gate session_do_pam(Session *s, int do_open)
24667c478bd9Sstevel@tonic-gate {
24677c478bd9Sstevel@tonic-gate int pam_retval;
24687c478bd9Sstevel@tonic-gate char *where, *old_tty, *old_tty_copy = NULL;
24697c478bd9Sstevel@tonic-gate struct pam_conv old_conv, *old_conv_ptr;
24707c478bd9Sstevel@tonic-gate
24717c478bd9Sstevel@tonic-gate if (!s || !s->authctxt || !s->authctxt->pam || !s->authctxt->pam->h)
24727c478bd9Sstevel@tonic-gate return;
24737c478bd9Sstevel@tonic-gate
24747c478bd9Sstevel@tonic-gate /* Save current PAM item values */
24757c478bd9Sstevel@tonic-gate where = "getting PAM_CONV";
24767c478bd9Sstevel@tonic-gate pam_retval = pam_get_item(s->authctxt->pam->h, PAM_CONV,
24777c478bd9Sstevel@tonic-gate (void **) &old_conv_ptr);
24787c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
24797c478bd9Sstevel@tonic-gate goto done;
24807c478bd9Sstevel@tonic-gate old_conv = *old_conv_ptr;
24817c478bd9Sstevel@tonic-gate
24827c478bd9Sstevel@tonic-gate where = "getting PAM_TTY";
24837c478bd9Sstevel@tonic-gate pam_retval = pam_get_item(s->authctxt->pam->h, PAM_TTY,
24847c478bd9Sstevel@tonic-gate (void **) &old_tty);
24857c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
24867c478bd9Sstevel@tonic-gate goto done;
24877c478bd9Sstevel@tonic-gate old_tty_copy = xstrdup(old_tty);
24887c478bd9Sstevel@tonic-gate
24897c478bd9Sstevel@tonic-gate /* Change PAM_TTY and PAM_CONV items */
24907c478bd9Sstevel@tonic-gate where = "setting PAM_TTY";
24917c478bd9Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, s->tty);
24927c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
24937c478bd9Sstevel@tonic-gate goto done;
24947c478bd9Sstevel@tonic-gate
24957c478bd9Sstevel@tonic-gate where = "setting PAM_CONV";
24967c478bd9Sstevel@tonic-gate session_pam_conv.appdata_ptr = s;
24977c478bd9Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h,
24987c478bd9Sstevel@tonic-gate PAM_CONV, &session_pam_conv);
24997c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
25007c478bd9Sstevel@tonic-gate goto done;
25017c478bd9Sstevel@tonic-gate
2502*2de0a7d6SDan McDonald /* Call pam_open/close_session() */
2503*2de0a7d6SDan McDonald if (do_open) {
2504*2de0a7d6SDan McDonald where = "calling pam_open_session()";
2505*2de0a7d6SDan McDonald pam_retval = pam_open_session(s->authctxt->pam->h, 0);
2506*2de0a7d6SDan McDonald }
2507*2de0a7d6SDan McDonald else {
25087c478bd9Sstevel@tonic-gate where = "calling pam_close_session()";
25097c478bd9Sstevel@tonic-gate pam_retval = pam_close_session(s->authctxt->pam->h, 0);
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate
25127c478bd9Sstevel@tonic-gate /* Reset PAM_TTY and PAM_CONV items to previous values */
25137c478bd9Sstevel@tonic-gate where = "setting PAM_TTY";
25147c478bd9Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, old_tty_copy);
25157c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
25167c478bd9Sstevel@tonic-gate goto done;
25177c478bd9Sstevel@tonic-gate
25187c478bd9Sstevel@tonic-gate where = "setting PAM_CONV";
25197c478bd9Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_CONV, &old_conv);
25207c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
25217c478bd9Sstevel@tonic-gate goto done;
25227c478bd9Sstevel@tonic-gate
25237c478bd9Sstevel@tonic-gate session_pam_conv.appdata_ptr = NULL;
25247c478bd9Sstevel@tonic-gate
25257c478bd9Sstevel@tonic-gate done:
25267c478bd9Sstevel@tonic-gate if (old_tty_copy)
25277c478bd9Sstevel@tonic-gate xfree(old_tty_copy);
25287c478bd9Sstevel@tonic-gate
25297c478bd9Sstevel@tonic-gate if (pam_retval == PAM_SUCCESS)
25307c478bd9Sstevel@tonic-gate return;
25317c478bd9Sstevel@tonic-gate
25327c478bd9Sstevel@tonic-gate /* fatal()? probably not... */
25337c478bd9Sstevel@tonic-gate log("PAM failed[%d] while %s: %s", pam_retval, where,
25347c478bd9Sstevel@tonic-gate PAM_STRERROR(s->authctxt->pam->h, pam_retval));
25357c478bd9Sstevel@tonic-gate }
25367c478bd9Sstevel@tonic-gate
25377c478bd9Sstevel@tonic-gate int
session_do_pam_conv(int num_prompts,struct pam_message ** prompts,struct pam_response ** resp,void * app_data)25387c478bd9Sstevel@tonic-gate session_do_pam_conv(int num_prompts,
25397c478bd9Sstevel@tonic-gate struct pam_message **prompts,
25407c478bd9Sstevel@tonic-gate struct pam_response **resp,
25417c478bd9Sstevel@tonic-gate void *app_data)
25427c478bd9Sstevel@tonic-gate {
25437c478bd9Sstevel@tonic-gate Session *s = (Session *) app_data;
25447c478bd9Sstevel@tonic-gate
25457c478bd9Sstevel@tonic-gate struct pam_response *reply;
25467c478bd9Sstevel@tonic-gate int count;
25477c478bd9Sstevel@tonic-gate char *prompt;
25487c478bd9Sstevel@tonic-gate
25497c478bd9Sstevel@tonic-gate if (channel_lookup(s->chanid) == NULL)
25507c478bd9Sstevel@tonic-gate return PAM_CONV_ERR;
25517c478bd9Sstevel@tonic-gate
25527c478bd9Sstevel@tonic-gate /* PAM will free this later */
25537c478bd9Sstevel@tonic-gate reply = xmalloc(num_prompts * sizeof(*reply));
25547c478bd9Sstevel@tonic-gate
25557c478bd9Sstevel@tonic-gate (void) memset(reply, 0, num_prompts * sizeof(*reply));
25567c478bd9Sstevel@tonic-gate for (count = 0; count < num_prompts; count++) {
25577c478bd9Sstevel@tonic-gate switch(PAM_MSG_MEMBER(prompts, count, msg_style)) {
25587c478bd9Sstevel@tonic-gate case PAM_TEXT_INFO:
25597c478bd9Sstevel@tonic-gate /* Write to stdout of channel */
25607c478bd9Sstevel@tonic-gate prompt = PAM_MSG_MEMBER(prompts, count, msg);
25617c478bd9Sstevel@tonic-gate if (prompt != NULL && s->ttyfd != -1) {
25627c478bd9Sstevel@tonic-gate debug2("session_do_pam_conv: text info "
25637c478bd9Sstevel@tonic-gate "prompt: %s", prompt);
25647c478bd9Sstevel@tonic-gate (void) write(s->ttyfd, prompt, strlen(prompt));
25657c478bd9Sstevel@tonic-gate (void) write(s->ttyfd, "\n", 1);
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate reply[count].resp = xstrdup("");
25687c478bd9Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS;
25697c478bd9Sstevel@tonic-gate break;
25707c478bd9Sstevel@tonic-gate case PAM_ERROR_MSG:
25717c478bd9Sstevel@tonic-gate /* Write to stderr of channel */
25727c478bd9Sstevel@tonic-gate prompt = PAM_MSG_MEMBER(prompts, count, msg);
25737c478bd9Sstevel@tonic-gate if (prompt != NULL && s->ttyfd != -1) {
25747c478bd9Sstevel@tonic-gate debug2("session_do_pam_conv: error "
25757c478bd9Sstevel@tonic-gate "prompt: %s", prompt);
25767c478bd9Sstevel@tonic-gate (void) write(s->ttyfd, prompt, strlen(prompt));
25777c478bd9Sstevel@tonic-gate (void) write(s->ttyfd, "\n", 1);
25787c478bd9Sstevel@tonic-gate }
25797c478bd9Sstevel@tonic-gate reply[count].resp = xstrdup("");
25807c478bd9Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS;
25817c478bd9Sstevel@tonic-gate break;
25827c478bd9Sstevel@tonic-gate case PAM_PROMPT_ECHO_ON:
25837c478bd9Sstevel@tonic-gate case PAM_PROMPT_ECHO_OFF:
25847c478bd9Sstevel@tonic-gate /*
25857c478bd9Sstevel@tonic-gate * XXX Someday add support for echo on/off prompts
25867c478bd9Sstevel@tonic-gate * here on sessions with ttys.
25877c478bd9Sstevel@tonic-gate */
25887c478bd9Sstevel@tonic-gate default:
25897c478bd9Sstevel@tonic-gate xfree(reply);
25907c478bd9Sstevel@tonic-gate return PAM_CONV_ERR;
25917c478bd9Sstevel@tonic-gate }
25927c478bd9Sstevel@tonic-gate }
25937c478bd9Sstevel@tonic-gate
25947c478bd9Sstevel@tonic-gate *resp = reply;
25957c478bd9Sstevel@tonic-gate
25967c478bd9Sstevel@tonic-gate return PAM_SUCCESS;
25977c478bd9Sstevel@tonic-gate }
25987c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
25997c478bd9Sstevel@tonic-gate
26007c478bd9Sstevel@tonic-gate static void
do_authenticated2(Authctxt * authctxt)26017c478bd9Sstevel@tonic-gate do_authenticated2(Authctxt *authctxt)
26027c478bd9Sstevel@tonic-gate {
26037c478bd9Sstevel@tonic-gate server_loop2(authctxt);
26047c478bd9Sstevel@tonic-gate }
26056f8d59d8SJan Pechanec
26066f8d59d8SJan Pechanec /*
26076f8d59d8SJan Pechanec * Drop the privileges. We need this for the in-process SFTP server only. For
26086f8d59d8SJan Pechanec * the shell and the external subsystem the exec(2) call will do the P = E = I
26096f8d59d8SJan Pechanec * assignment itself. Never change the privileges if the connecting user is
26106f8d59d8SJan Pechanec * root. See privileges(5) if the terminology used here is not known to you.
26116f8d59d8SJan Pechanec */
26126f8d59d8SJan Pechanec static void
drop_privs(uid_t uid)26136f8d59d8SJan Pechanec drop_privs(uid_t uid)
26146f8d59d8SJan Pechanec {
26156f8d59d8SJan Pechanec priv_set_t *priv_inherit;
26166f8d59d8SJan Pechanec
26176f8d59d8SJan Pechanec /* If root is connecting we are done. */
26186f8d59d8SJan Pechanec if (uid == 0)
26196f8d59d8SJan Pechanec return;
26206f8d59d8SJan Pechanec
26216f8d59d8SJan Pechanec if ((priv_inherit = priv_allocset()) == NULL)
26226f8d59d8SJan Pechanec fatal("priv_allocset: %s", strerror(errno));
26236f8d59d8SJan Pechanec if (getppriv(PRIV_INHERITABLE, priv_inherit) != 0)
26246f8d59d8SJan Pechanec fatal("getppriv: %s", strerror(errno));
26256f8d59d8SJan Pechanec
26266f8d59d8SJan Pechanec /*
26276f8d59d8SJan Pechanec * This will limit E as well. Note that before this P was a
26286f8d59d8SJan Pechanec * superset of I, see permanently_set_uid().
26296f8d59d8SJan Pechanec */
26306f8d59d8SJan Pechanec if (setppriv(PRIV_SET, PRIV_PERMITTED, priv_inherit) == -1)
26316f8d59d8SJan Pechanec fatal("setppriv: %s", strerror(errno));
26326f8d59d8SJan Pechanec
26336f8d59d8SJan Pechanec priv_freeset(priv_inherit);
26346f8d59d8SJan Pechanec
26356f8d59d8SJan Pechanec /*
26366f8d59d8SJan Pechanec * By manipulating the P set above we entered a PA mode which we
26376f8d59d8SJan Pechanec * do not need to retain in.
26386f8d59d8SJan Pechanec */
26396f8d59d8SJan Pechanec if (setpflags(PRIV_AWARE, 0) == -1)
26406f8d59d8SJan Pechanec fatal("setpflags: %s", strerror(errno));
26416f8d59d8SJan Pechanec }
2642