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