10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 30Sstevel@tonic-gate * All rights reserved 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 60Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 70Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 80Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 90Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 100Sstevel@tonic-gate * 110Sstevel@tonic-gate * SSH2 support by Markus Friedl. 120Sstevel@tonic-gate * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 150Sstevel@tonic-gate * modification, are permitted provided that the following conditions 160Sstevel@tonic-gate * are met: 170Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 180Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 190Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 200Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 210Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 240Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 250Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 260Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 270Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 280Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 290Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 300Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 310Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 320Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 330Sstevel@tonic-gate */ 340Sstevel@tonic-gate /* 35*2757Sjp161948 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 360Sstevel@tonic-gate * Use is subject to license terms. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include "includes.h" 400Sstevel@tonic-gate RCSID("$OpenBSD: session.c,v 1.150 2002/09/16 19:55:33 stevesk Exp $"); 410Sstevel@tonic-gate 420Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 430Sstevel@tonic-gate 440Sstevel@tonic-gate #ifdef HAVE_DEFOPEN 450Sstevel@tonic-gate #include <deflt.h> 460Sstevel@tonic-gate #include <ulimit.h> 470Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */ 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include "ssh.h" 500Sstevel@tonic-gate #include "ssh1.h" 510Sstevel@tonic-gate #include "ssh2.h" 520Sstevel@tonic-gate #include "xmalloc.h" 530Sstevel@tonic-gate #include "sshpty.h" 540Sstevel@tonic-gate #include "packet.h" 550Sstevel@tonic-gate #include "buffer.h" 560Sstevel@tonic-gate #include "mpaux.h" 570Sstevel@tonic-gate #include "uidswap.h" 580Sstevel@tonic-gate #include "compat.h" 590Sstevel@tonic-gate #include "channels.h" 600Sstevel@tonic-gate #include "bufaux.h" 610Sstevel@tonic-gate #include "auth.h" 620Sstevel@tonic-gate #include "auth-options.h" 630Sstevel@tonic-gate #include "pathnames.h" 640Sstevel@tonic-gate #include "log.h" 650Sstevel@tonic-gate #include "servconf.h" 660Sstevel@tonic-gate #include "sshlogin.h" 670Sstevel@tonic-gate #include "serverloop.h" 680Sstevel@tonic-gate #include "canohost.h" 690Sstevel@tonic-gate #include "session.h" 700Sstevel@tonic-gate #include "monitor_wrap.h" 710Sstevel@tonic-gate 720Sstevel@tonic-gate #ifdef USE_PAM 730Sstevel@tonic-gate #include <security/pam_appl.h> 740Sstevel@tonic-gate #endif /* USE_PAM */ 750Sstevel@tonic-gate 760Sstevel@tonic-gate #ifdef GSSAPI 770Sstevel@tonic-gate #include "ssh-gss.h" 780Sstevel@tonic-gate #endif 790Sstevel@tonic-gate 800Sstevel@tonic-gate #ifdef ALTPRIVSEP 810Sstevel@tonic-gate #include "altprivsep.h" 820Sstevel@tonic-gate #endif /* ALTPRIVSEP */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate #ifdef HAVE_CYGWIN 850Sstevel@tonic-gate #include <windows.h> 860Sstevel@tonic-gate #include <sys/cygwin.h> 870Sstevel@tonic-gate #define is_winnt (GetVersion() < 0x80000000) 880Sstevel@tonic-gate #endif 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* func */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate Session *session_new(void); 930Sstevel@tonic-gate void session_set_fds(Session *, int, int, int); 940Sstevel@tonic-gate void session_pty_cleanup(void *); 950Sstevel@tonic-gate void session_proctitle(Session *); 960Sstevel@tonic-gate int session_setup_x11fwd(Session *); 970Sstevel@tonic-gate void do_exec_pty(Session *, const char *); 980Sstevel@tonic-gate void do_exec_no_pty(Session *, const char *); 990Sstevel@tonic-gate void do_exec(Session *, const char *); 1000Sstevel@tonic-gate void do_login(Session *, const char *); 1010Sstevel@tonic-gate #ifdef LOGIN_NEEDS_UTMPX 1020Sstevel@tonic-gate static void do_pre_login(Session *s); 1030Sstevel@tonic-gate #endif 1040Sstevel@tonic-gate void do_child(Session *, const char *); 1050Sstevel@tonic-gate void do_motd(void); 1060Sstevel@tonic-gate int check_quietlogin(Session *, const char *); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate static void do_authenticated1(Authctxt *); 1090Sstevel@tonic-gate static void do_authenticated2(Authctxt *); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate static int session_pty_req(Session *); 1120Sstevel@tonic-gate static int session_env_req(Session *s); 1130Sstevel@tonic-gate static void session_free_env(char ***envp); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate #ifdef USE_PAM 1160Sstevel@tonic-gate static void session_do_pam(Session *, int); 1170Sstevel@tonic-gate #endif /* USE_PAM */ 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* import */ 1200Sstevel@tonic-gate extern ServerOptions options; 1210Sstevel@tonic-gate extern char *__progname; 1220Sstevel@tonic-gate extern int log_stderr; 1230Sstevel@tonic-gate extern int debug_flag; 1240Sstevel@tonic-gate extern u_int utmp_len; 1250Sstevel@tonic-gate extern void destroy_sensitive_data(void); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #ifdef GSSAPI 1280Sstevel@tonic-gate extern Gssctxt *xxx_gssctxt; 1290Sstevel@tonic-gate #endif /* GSSAPI */ 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* original command from peer. */ 1320Sstevel@tonic-gate const char *original_command = NULL; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* data */ 1350Sstevel@tonic-gate #define MAX_SESSIONS 10 1360Sstevel@tonic-gate Session sessions[MAX_SESSIONS]; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 1390Sstevel@tonic-gate char *aixloginmsg; 1400Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 1430Sstevel@tonic-gate login_cap_t *lc; 1440Sstevel@tonic-gate #endif 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* Name and directory of socket for authentication agent forwarding. */ 1470Sstevel@tonic-gate static char *auth_sock_name = NULL; 1480Sstevel@tonic-gate static char *auth_sock_dir = NULL; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* removes the agent forwarding socket */ 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate static void 1530Sstevel@tonic-gate auth_sock_cleanup_proc(void *_pw) 1540Sstevel@tonic-gate { 1550Sstevel@tonic-gate struct passwd *pw = _pw; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate if (auth_sock_name != NULL) { 1580Sstevel@tonic-gate temporarily_use_uid(pw); 1590Sstevel@tonic-gate unlink(auth_sock_name); 1600Sstevel@tonic-gate rmdir(auth_sock_dir); 1610Sstevel@tonic-gate auth_sock_name = NULL; 1620Sstevel@tonic-gate restore_uid(); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static int 1670Sstevel@tonic-gate auth_input_request_forwarding(struct passwd * pw) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate Channel *nc; 1700Sstevel@tonic-gate int sock; 1710Sstevel@tonic-gate struct sockaddr_un sunaddr; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate if (auth_sock_name != NULL) { 1740Sstevel@tonic-gate error("authentication forwarding requested twice."); 1750Sstevel@tonic-gate return 0; 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /* Temporarily drop privileged uid for mkdir/bind. */ 1790Sstevel@tonic-gate temporarily_use_uid(pw); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* Allocate a buffer for the socket name, and format the name. */ 1820Sstevel@tonic-gate auth_sock_name = xmalloc(MAXPATHLEN); 1830Sstevel@tonic-gate auth_sock_dir = xmalloc(MAXPATHLEN); 1840Sstevel@tonic-gate strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* Create private directory for socket */ 1870Sstevel@tonic-gate if (mkdtemp(auth_sock_dir) == NULL) { 1880Sstevel@tonic-gate packet_send_debug("Agent forwarding disabled: " 1890Sstevel@tonic-gate "mkdtemp() failed: %.100s", strerror(errno)); 1900Sstevel@tonic-gate restore_uid(); 1910Sstevel@tonic-gate xfree(auth_sock_name); 1920Sstevel@tonic-gate xfree(auth_sock_dir); 1930Sstevel@tonic-gate auth_sock_name = NULL; 1940Sstevel@tonic-gate auth_sock_dir = NULL; 1950Sstevel@tonic-gate return 0; 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld", 1980Sstevel@tonic-gate auth_sock_dir, (long) getpid()); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate /* delete agent socket on fatal() */ 2010Sstevel@tonic-gate fatal_add_cleanup(auth_sock_cleanup_proc, pw); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* Create the socket. */ 2040Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0); 2050Sstevel@tonic-gate if (sock < 0) 2060Sstevel@tonic-gate packet_disconnect("socket: %.100s", strerror(errno)); 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* Bind it to the name. */ 2090Sstevel@tonic-gate memset(&sunaddr, 0, sizeof(sunaddr)); 2100Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX; 2110Sstevel@tonic-gate strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path)); 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) 2140Sstevel@tonic-gate packet_disconnect("bind: %.100s", strerror(errno)); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* Restore the privileged uid. */ 2170Sstevel@tonic-gate restore_uid(); 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* Start listening on the socket. */ 2200Sstevel@tonic-gate if (listen(sock, 5) < 0) 2210Sstevel@tonic-gate packet_disconnect("listen: %.100s", strerror(errno)); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* Allocate a channel for the authentication agent socket. */ 2240Sstevel@tonic-gate nc = channel_new("auth socket", 2250Sstevel@tonic-gate SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1, 2260Sstevel@tonic-gate CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 2270Sstevel@tonic-gate 0, xstrdup("auth socket"), 1); 2280Sstevel@tonic-gate strlcpy(nc->path, auth_sock_name, sizeof(nc->path)); 2290Sstevel@tonic-gate return 1; 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate void 2340Sstevel@tonic-gate do_authenticated(Authctxt *authctxt) 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate /* setup the channel layer */ 2370Sstevel@tonic-gate if (!no_port_forwarding_flag && options.allow_tcp_forwarding) 2380Sstevel@tonic-gate channel_permit_all_opens(); 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate if (compat20) 2410Sstevel@tonic-gate do_authenticated2(authctxt); 2420Sstevel@tonic-gate else 2430Sstevel@tonic-gate do_authenticated1(authctxt); 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* remove agent socket */ 2460Sstevel@tonic-gate if (auth_sock_name != NULL) 2470Sstevel@tonic-gate auth_sock_cleanup_proc(authctxt->pw); 2480Sstevel@tonic-gate #ifdef KRB4 2490Sstevel@tonic-gate if (options.kerberos_ticket_cleanup) 2500Sstevel@tonic-gate krb4_cleanup_proc(authctxt); 2510Sstevel@tonic-gate #endif 2520Sstevel@tonic-gate #ifdef KRB5 2530Sstevel@tonic-gate if (options.kerberos_ticket_cleanup) 2540Sstevel@tonic-gate krb5_cleanup_proc(authctxt); 2550Sstevel@tonic-gate #endif 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Prepares for an interactive session. This is called after the user has 2600Sstevel@tonic-gate * been successfully authenticated. During this message exchange, pseudo 2610Sstevel@tonic-gate * terminals are allocated, X11, TCP/IP, and authentication agent forwardings 2620Sstevel@tonic-gate * are requested, etc. 2630Sstevel@tonic-gate */ 2640Sstevel@tonic-gate static void 2650Sstevel@tonic-gate do_authenticated1(Authctxt *authctxt) 2660Sstevel@tonic-gate { 2670Sstevel@tonic-gate Session *s; 2680Sstevel@tonic-gate char *command; 2690Sstevel@tonic-gate int success, type, screen_flag; 2700Sstevel@tonic-gate int enable_compression_after_reply = 0; 2710Sstevel@tonic-gate u_int proto_len, data_len, dlen, compression_level = 0; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate s = session_new(); 2740Sstevel@tonic-gate s->authctxt = authctxt; 2750Sstevel@tonic-gate s->pw = authctxt->pw; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * We stay in this loop until the client requests to execute a shell 2790Sstevel@tonic-gate * or a command. 2800Sstevel@tonic-gate */ 2810Sstevel@tonic-gate for (;;) { 2820Sstevel@tonic-gate success = 0; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate /* Get a packet from the client. */ 2850Sstevel@tonic-gate type = packet_read(); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate /* Process the packet. */ 2880Sstevel@tonic-gate switch (type) { 2890Sstevel@tonic-gate case SSH_CMSG_REQUEST_COMPRESSION: 2900Sstevel@tonic-gate compression_level = packet_get_int(); 2910Sstevel@tonic-gate packet_check_eom(); 2920Sstevel@tonic-gate if (compression_level < 1 || compression_level > 9) { 2930Sstevel@tonic-gate packet_send_debug("Received illegal compression level %d.", 2940Sstevel@tonic-gate compression_level); 2950Sstevel@tonic-gate break; 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate if (!options.compression) { 2980Sstevel@tonic-gate debug2("compression disabled"); 2990Sstevel@tonic-gate break; 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate /* Enable compression after we have responded with SUCCESS. */ 3020Sstevel@tonic-gate enable_compression_after_reply = 1; 3030Sstevel@tonic-gate success = 1; 3040Sstevel@tonic-gate break; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate case SSH_CMSG_REQUEST_PTY: 3070Sstevel@tonic-gate success = session_pty_req(s); 3080Sstevel@tonic-gate break; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate case SSH_CMSG_X11_REQUEST_FORWARDING: 3110Sstevel@tonic-gate s->auth_proto = packet_get_string(&proto_len); 3120Sstevel@tonic-gate s->auth_data = packet_get_string(&data_len); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate screen_flag = packet_get_protocol_flags() & 3150Sstevel@tonic-gate SSH_PROTOFLAG_SCREEN_NUMBER; 3160Sstevel@tonic-gate debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate if (packet_remaining() == 4) { 3190Sstevel@tonic-gate if (!screen_flag) 3200Sstevel@tonic-gate debug2("Buggy client: " 3210Sstevel@tonic-gate "X11 screen flag missing"); 3220Sstevel@tonic-gate s->screen = packet_get_int(); 3230Sstevel@tonic-gate } else { 3240Sstevel@tonic-gate s->screen = 0; 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate packet_check_eom(); 3270Sstevel@tonic-gate success = session_setup_x11fwd(s); 3280Sstevel@tonic-gate if (!success) { 3290Sstevel@tonic-gate xfree(s->auth_proto); 3300Sstevel@tonic-gate xfree(s->auth_data); 3310Sstevel@tonic-gate s->auth_proto = NULL; 3320Sstevel@tonic-gate s->auth_data = NULL; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate break; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate case SSH_CMSG_AGENT_REQUEST_FORWARDING: 3370Sstevel@tonic-gate if (no_agent_forwarding_flag || compat13) { 3380Sstevel@tonic-gate debug("Authentication agent forwarding not permitted for this authentication."); 3390Sstevel@tonic-gate break; 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate debug("Received authentication agent forwarding request."); 3420Sstevel@tonic-gate success = auth_input_request_forwarding(s->pw); 3430Sstevel@tonic-gate break; 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate case SSH_CMSG_PORT_FORWARD_REQUEST: 3460Sstevel@tonic-gate if (no_port_forwarding_flag) { 3470Sstevel@tonic-gate debug("Port forwarding not permitted for this authentication."); 3480Sstevel@tonic-gate break; 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate if (!options.allow_tcp_forwarding) { 3510Sstevel@tonic-gate debug("Port forwarding not permitted."); 3520Sstevel@tonic-gate break; 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate debug("Received TCP/IP port forwarding request."); 3550Sstevel@tonic-gate channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports); 3560Sstevel@tonic-gate success = 1; 3570Sstevel@tonic-gate break; 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate case SSH_CMSG_MAX_PACKET_SIZE: 3600Sstevel@tonic-gate if (packet_set_maxsize(packet_get_int()) > 0) 3610Sstevel@tonic-gate success = 1; 3620Sstevel@tonic-gate break; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate #if defined(AFS) || defined(KRB5) 3650Sstevel@tonic-gate case SSH_CMSG_HAVE_KERBEROS_TGT: 3660Sstevel@tonic-gate if (!options.kerberos_tgt_passing) { 3670Sstevel@tonic-gate verbose("Kerberos TGT passing disabled."); 3680Sstevel@tonic-gate } else { 3690Sstevel@tonic-gate char *kdata = packet_get_string(&dlen); 3700Sstevel@tonic-gate packet_check_eom(); 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* XXX - 0x41, see creds_to_radix version */ 3730Sstevel@tonic-gate if (kdata[0] != 0x41) { 3740Sstevel@tonic-gate #ifdef KRB5 3750Sstevel@tonic-gate krb5_data tgt; 3760Sstevel@tonic-gate tgt.data = kdata; 3770Sstevel@tonic-gate tgt.length = dlen; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate if (auth_krb5_tgt(s->authctxt, &tgt)) 3800Sstevel@tonic-gate success = 1; 3810Sstevel@tonic-gate else 3820Sstevel@tonic-gate verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user); 3830Sstevel@tonic-gate #endif /* KRB5 */ 3840Sstevel@tonic-gate } else { 3850Sstevel@tonic-gate #ifdef AFS 3860Sstevel@tonic-gate if (auth_krb4_tgt(s->authctxt, kdata)) 3870Sstevel@tonic-gate success = 1; 3880Sstevel@tonic-gate else 3890Sstevel@tonic-gate verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user); 3900Sstevel@tonic-gate #endif /* AFS */ 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate xfree(kdata); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate break; 3950Sstevel@tonic-gate #endif /* AFS || KRB5 */ 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate #ifdef AFS 3980Sstevel@tonic-gate case SSH_CMSG_HAVE_AFS_TOKEN: 3990Sstevel@tonic-gate if (!options.afs_token_passing || !k_hasafs()) { 4000Sstevel@tonic-gate verbose("AFS token passing disabled."); 4010Sstevel@tonic-gate } else { 4020Sstevel@tonic-gate /* Accept AFS token. */ 4030Sstevel@tonic-gate char *token = packet_get_string(&dlen); 4040Sstevel@tonic-gate packet_check_eom(); 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate if (auth_afs_token(s->authctxt, token)) 4070Sstevel@tonic-gate success = 1; 4080Sstevel@tonic-gate else 4090Sstevel@tonic-gate verbose("AFS token refused for %.100s", 4100Sstevel@tonic-gate s->authctxt->user); 4110Sstevel@tonic-gate xfree(token); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate break; 4140Sstevel@tonic-gate #endif /* AFS */ 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate case SSH_CMSG_EXEC_SHELL: 4170Sstevel@tonic-gate case SSH_CMSG_EXEC_CMD: 4180Sstevel@tonic-gate if (type == SSH_CMSG_EXEC_CMD) { 4190Sstevel@tonic-gate command = packet_get_string(&dlen); 4200Sstevel@tonic-gate debug("Exec command '%.500s'", command); 4210Sstevel@tonic-gate do_exec(s, command); 4220Sstevel@tonic-gate xfree(command); 4230Sstevel@tonic-gate } else { 4240Sstevel@tonic-gate do_exec(s, NULL); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate packet_check_eom(); 4270Sstevel@tonic-gate session_close(s); 4280Sstevel@tonic-gate return; 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate default: 4310Sstevel@tonic-gate /* 4320Sstevel@tonic-gate * Any unknown messages in this phase are ignored, 4330Sstevel@tonic-gate * and a failure message is returned. 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate log("Unknown packet type received after authentication: %d", type); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); 4380Sstevel@tonic-gate packet_send(); 4390Sstevel@tonic-gate packet_write_wait(); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* Enable compression now that we have replied if appropriate. */ 4420Sstevel@tonic-gate if (enable_compression_after_reply) { 4430Sstevel@tonic-gate enable_compression_after_reply = 0; 4440Sstevel@tonic-gate packet_start_compression(compression_level); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * This is called to fork and execute a command when we have no tty. This 4510Sstevel@tonic-gate * will call do_child from the child, and server_loop from the parent after 4520Sstevel@tonic-gate * setting up file descriptors and such. 4530Sstevel@tonic-gate */ 4540Sstevel@tonic-gate void 4550Sstevel@tonic-gate do_exec_no_pty(Session *s, const char *command) 4560Sstevel@tonic-gate { 4570Sstevel@tonic-gate pid_t pid; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate #ifdef USE_PIPES 4600Sstevel@tonic-gate int pin[2], pout[2], perr[2]; 4610Sstevel@tonic-gate /* Allocate pipes for communicating with the program. */ 4620Sstevel@tonic-gate if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) 4630Sstevel@tonic-gate packet_disconnect("Could not create pipes: %.100s", 4640Sstevel@tonic-gate strerror(errno)); 4650Sstevel@tonic-gate #else /* USE_PIPES */ 4660Sstevel@tonic-gate int inout[2], err[2]; 4670Sstevel@tonic-gate /* Uses socket pairs to communicate with the program. */ 4680Sstevel@tonic-gate if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || 4690Sstevel@tonic-gate socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) 4700Sstevel@tonic-gate packet_disconnect("Could not create socket pairs: %.100s", 4710Sstevel@tonic-gate strerror(errno)); 4720Sstevel@tonic-gate #endif /* USE_PIPES */ 4730Sstevel@tonic-gate if (s == NULL) 4740Sstevel@tonic-gate fatal("do_exec_no_pty: no session"); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate session_proctitle(s); 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* Fork the child. */ 4790Sstevel@tonic-gate if ((pid = fork()) == 0) { 4800Sstevel@tonic-gate fatal_remove_all_cleanups(); 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate /* Child. Reinitialize the log since the pid has changed. */ 4830Sstevel@tonic-gate log_init(__progname, options.log_level, options.log_facility, log_stderr); 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate /* 4860Sstevel@tonic-gate * Create a new session and process group since the 4.4BSD 4870Sstevel@tonic-gate * setlogin() affects the entire process group. 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate if (setsid() < 0) 4900Sstevel@tonic-gate error("setsid failed: %.100s", strerror(errno)); 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate #ifdef USE_PIPES 4930Sstevel@tonic-gate /* 4940Sstevel@tonic-gate * Redirect stdin. We close the parent side of the socket 4950Sstevel@tonic-gate * pair, and make the child side the standard input. 4960Sstevel@tonic-gate */ 4970Sstevel@tonic-gate close(pin[1]); 4980Sstevel@tonic-gate if (dup2(pin[0], 0) < 0) 4990Sstevel@tonic-gate perror("dup2 stdin"); 5000Sstevel@tonic-gate close(pin[0]); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* Redirect stdout. */ 5030Sstevel@tonic-gate close(pout[0]); 5040Sstevel@tonic-gate if (dup2(pout[1], 1) < 0) 5050Sstevel@tonic-gate perror("dup2 stdout"); 5060Sstevel@tonic-gate close(pout[1]); 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate /* Redirect stderr. */ 5090Sstevel@tonic-gate close(perr[0]); 5100Sstevel@tonic-gate if (dup2(perr[1], 2) < 0) 5110Sstevel@tonic-gate perror("dup2 stderr"); 5120Sstevel@tonic-gate close(perr[1]); 5130Sstevel@tonic-gate #else /* USE_PIPES */ 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * Redirect stdin, stdout, and stderr. Stdin and stdout will 5160Sstevel@tonic-gate * use the same socket, as some programs (particularly rdist) 5170Sstevel@tonic-gate * seem to depend on it. 5180Sstevel@tonic-gate */ 5190Sstevel@tonic-gate close(inout[1]); 5200Sstevel@tonic-gate close(err[1]); 5210Sstevel@tonic-gate if (dup2(inout[0], 0) < 0) /* stdin */ 5220Sstevel@tonic-gate perror("dup2 stdin"); 5230Sstevel@tonic-gate if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ 5240Sstevel@tonic-gate perror("dup2 stdout"); 5250Sstevel@tonic-gate if (dup2(err[0], 2) < 0) /* stderr */ 5260Sstevel@tonic-gate perror("dup2 stderr"); 5270Sstevel@tonic-gate #endif /* USE_PIPES */ 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate #ifdef _UNICOS 5300Sstevel@tonic-gate cray_init_job(s->pw); /* set up cray jid and tmpdir */ 5310Sstevel@tonic-gate #endif 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /* Do processing for the child (exec command etc). */ 5340Sstevel@tonic-gate do_child(s, command); 5350Sstevel@tonic-gate /* NOTREACHED */ 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate #ifdef _UNICOS 5380Sstevel@tonic-gate signal(WJSIGNAL, cray_job_termination_handler); 5390Sstevel@tonic-gate #endif /* _UNICOS */ 5400Sstevel@tonic-gate #ifdef HAVE_CYGWIN 5410Sstevel@tonic-gate if (is_winnt) 5420Sstevel@tonic-gate cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); 5430Sstevel@tonic-gate #endif 5440Sstevel@tonic-gate if (pid < 0) 5450Sstevel@tonic-gate packet_disconnect("fork failed: %.100s", strerror(errno)); 5460Sstevel@tonic-gate s->pid = pid; 5470Sstevel@tonic-gate /* Set interactive/non-interactive mode. */ 5480Sstevel@tonic-gate packet_set_interactive(s->display != NULL); 5490Sstevel@tonic-gate #ifdef USE_PIPES 5500Sstevel@tonic-gate /* We are the parent. Close the child sides of the pipes. */ 5510Sstevel@tonic-gate close(pin[0]); 5520Sstevel@tonic-gate close(pout[1]); 5530Sstevel@tonic-gate close(perr[1]); 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if (compat20) { 5560Sstevel@tonic-gate session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]); 5570Sstevel@tonic-gate /* Don't close channel before sending exit-status! */ 5580Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 1); 5590Sstevel@tonic-gate } else { 5600Sstevel@tonic-gate /* Enter the interactive session. */ 5610Sstevel@tonic-gate server_loop(pid, pin[1], pout[0], perr[0]); 5620Sstevel@tonic-gate /* server_loop has closed pin[1], pout[0], and perr[0]. */ 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate #else /* USE_PIPES */ 5650Sstevel@tonic-gate /* We are the parent. Close the child sides of the socket pairs. */ 5660Sstevel@tonic-gate close(inout[0]); 5670Sstevel@tonic-gate close(err[0]); 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate /* 5700Sstevel@tonic-gate * Enter the interactive session. Note: server_loop must be able to 5710Sstevel@tonic-gate * handle the case that fdin and fdout are the same. 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate if (compat20) { 5740Sstevel@tonic-gate session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]); 5750Sstevel@tonic-gate /* Don't close channel before sending exit-status! */ 5760Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 1); 5770Sstevel@tonic-gate } else { 5780Sstevel@tonic-gate server_loop(pid, inout[1], inout[1], err[1]); 5790Sstevel@tonic-gate /* server_loop has closed inout[1] and err[1]. */ 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate #endif /* USE_PIPES */ 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate /* 5850Sstevel@tonic-gate * This is called to fork and execute a command when we have a tty. This 5860Sstevel@tonic-gate * will call do_child from the child, and server_loop from the parent after 5870Sstevel@tonic-gate * setting up file descriptors, controlling tty, updating wtmp, utmp, 5880Sstevel@tonic-gate * lastlog, and other such operations. 5890Sstevel@tonic-gate */ 5900Sstevel@tonic-gate void 5910Sstevel@tonic-gate do_exec_pty(Session *s, const char *command) 5920Sstevel@tonic-gate { 5930Sstevel@tonic-gate int fdout, ptyfd, ttyfd, ptymaster, pipe_fds[2]; 5940Sstevel@tonic-gate pid_t pid; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate if (s == NULL) 5970Sstevel@tonic-gate fatal("do_exec_pty: no session"); 5980Sstevel@tonic-gate ptyfd = s->ptyfd; 5990Sstevel@tonic-gate ttyfd = s->ttyfd; 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate #ifdef USE_PAM 6020Sstevel@tonic-gate session_do_pam(s, 1); /* pam_open_session() */ 6030Sstevel@tonic-gate #endif /* USE_PAM */ 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate /* 6060Sstevel@tonic-gate * This pipe lets sshd wait for child to exec or exit. This is 6070Sstevel@tonic-gate * particularly important for ALTPRIVSEP because the child is 6080Sstevel@tonic-gate * the one to call the monitor to request a record_login() and 6090Sstevel@tonic-gate * we don't want the child and the parent to compete for the 6100Sstevel@tonic-gate * monitor's attention. But this is generic code and doesn't 6110Sstevel@tonic-gate * hurt to have here even if ALTPRIVSEP is not used. 6120Sstevel@tonic-gate */ 6130Sstevel@tonic-gate if (pipe(pipe_fds) != 0) 6140Sstevel@tonic-gate packet_disconnect("pipe failed: %.100s", strerror(errno)); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate (void) fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC); 6170Sstevel@tonic-gate (void) fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC); 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* Fork the child. */ 6200Sstevel@tonic-gate if ((pid = fork()) == 0) { 6210Sstevel@tonic-gate (void) close(pipe_fds[0]); 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate fatal_remove_all_cleanups(); 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate /* Child. Reinitialize the log because the pid has changed. */ 6260Sstevel@tonic-gate log_init(__progname, options.log_level, options.log_facility, log_stderr); 6270Sstevel@tonic-gate /* Close the master side of the pseudo tty. */ 6280Sstevel@tonic-gate close(ptyfd); 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* Make the pseudo tty our controlling tty. */ 6310Sstevel@tonic-gate pty_make_controlling_tty(&ttyfd, s->tty); 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate /* Redirect stdin/stdout/stderr from the pseudo tty. */ 6340Sstevel@tonic-gate if (dup2(ttyfd, 0) < 0) 6350Sstevel@tonic-gate error("dup2 stdin: %s", strerror(errno)); 6360Sstevel@tonic-gate if (dup2(ttyfd, 1) < 0) 6370Sstevel@tonic-gate error("dup2 stdout: %s", strerror(errno)); 6380Sstevel@tonic-gate if (dup2(ttyfd, 2) < 0) 6390Sstevel@tonic-gate error("dup2 stderr: %s", strerror(errno)); 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* Close the extra descriptor for the pseudo tty. */ 6420Sstevel@tonic-gate close(ttyfd); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* record login, etc. similar to login(1) */ 6450Sstevel@tonic-gate #if !defined(HAVE_OSF_SIA) 6460Sstevel@tonic-gate if (!(options.use_login && command == NULL)) { 6470Sstevel@tonic-gate #ifdef _UNICOS 6480Sstevel@tonic-gate cray_init_job(s->pw); /* set up cray jid and tmpdir */ 6490Sstevel@tonic-gate #endif /* _UNICOS */ 6500Sstevel@tonic-gate do_login(s, command); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate # ifdef LOGIN_NEEDS_UTMPX 6530Sstevel@tonic-gate else 6540Sstevel@tonic-gate do_pre_login(s); 6550Sstevel@tonic-gate # endif 6560Sstevel@tonic-gate #endif /* !HAVE_OSF_SIA */ 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * do_pre_login() will have completed the record_login(), so 6600Sstevel@tonic-gate * close the pipe to the parent so it can re-enter its event 6610Sstevel@tonic-gate * loop and service the ptm; if enough debug messages get 6620Sstevel@tonic-gate * written to the pty before this happens there will be a 6630Sstevel@tonic-gate * deadlock. 6640Sstevel@tonic-gate */ 6650Sstevel@tonic-gate close(pipe_fds[1]); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate /* Do common processing for the child, such as execing the command. */ 6680Sstevel@tonic-gate do_child(s, command); 6690Sstevel@tonic-gate /* NOTREACHED */ 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate /* Wait for child to exec() or exit() */ 6730Sstevel@tonic-gate (void) close(pipe_fds[1]); 6740Sstevel@tonic-gate (void) read(pipe_fds[0], &pipe_fds[1], sizeof(int)); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate #ifdef _UNICOS 6770Sstevel@tonic-gate signal(WJSIGNAL, cray_job_termination_handler); 6780Sstevel@tonic-gate #endif /* _UNICOS */ 6790Sstevel@tonic-gate #ifdef HAVE_CYGWIN 6800Sstevel@tonic-gate if (is_winnt) 6810Sstevel@tonic-gate cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); 6820Sstevel@tonic-gate #endif 6830Sstevel@tonic-gate if (pid < 0) 6840Sstevel@tonic-gate packet_disconnect("fork failed: %.100s", strerror(errno)); 6850Sstevel@tonic-gate s->pid = pid; 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate /* Parent. Close the slave side of the pseudo tty. */ 6880Sstevel@tonic-gate close(ttyfd); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate /* 6910Sstevel@tonic-gate * Create another descriptor of the pty master side for use as the 6920Sstevel@tonic-gate * standard input. We could use the original descriptor, but this 6930Sstevel@tonic-gate * simplifies code in server_loop. The descriptor is bidirectional. 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate fdout = dup(ptyfd); 6960Sstevel@tonic-gate if (fdout < 0) 6970Sstevel@tonic-gate packet_disconnect("dup #1 failed: %.100s", strerror(errno)); 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate /* we keep a reference to the pty master */ 7000Sstevel@tonic-gate ptymaster = dup(ptyfd); 7010Sstevel@tonic-gate if (ptymaster < 0) 7020Sstevel@tonic-gate packet_disconnect("dup #2 failed: %.100s", strerror(errno)); 7030Sstevel@tonic-gate s->ptymaster = ptymaster; 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate /* Enter interactive session. */ 7060Sstevel@tonic-gate packet_set_interactive(1); 7070Sstevel@tonic-gate if (compat20) { 7080Sstevel@tonic-gate session_set_fds(s, ptyfd, fdout, -1); 7090Sstevel@tonic-gate /* Don't close channel before sending exit-status! */ 7100Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 1); 7110Sstevel@tonic-gate } else { 7120Sstevel@tonic-gate server_loop(pid, ptyfd, fdout, -1); 7130Sstevel@tonic-gate /* server_loop _has_ closed ptyfd and fdout. */ 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate #ifdef LOGIN_NEEDS_UTMPX 7180Sstevel@tonic-gate static void 7190Sstevel@tonic-gate do_pre_login(Session *s) 7200Sstevel@tonic-gate { 7210Sstevel@tonic-gate socklen_t fromlen; 7220Sstevel@tonic-gate struct sockaddr_storage from; 7230Sstevel@tonic-gate pid_t pid = getpid(); 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate /* 7260Sstevel@tonic-gate * Get IP address of client. If the connection is not a socket, let 7270Sstevel@tonic-gate * the address be 0.0.0.0. 7280Sstevel@tonic-gate */ 7290Sstevel@tonic-gate memset(&from, 0, sizeof(from)); 7300Sstevel@tonic-gate fromlen = sizeof(from); 7310Sstevel@tonic-gate if (packet_connection_is_on_socket()) { 7320Sstevel@tonic-gate if (getpeername(packet_get_connection_in(), 7330Sstevel@tonic-gate (struct sockaddr *) & from, &fromlen) < 0) { 7340Sstevel@tonic-gate debug("getpeername: %.100s", strerror(errno)); 7350Sstevel@tonic-gate fatal_cleanup(); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate record_utmp_only(pid, s->tty, s->pw->pw_name, 7400Sstevel@tonic-gate get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping), 7410Sstevel@tonic-gate (struct sockaddr *)&from); 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate #endif 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * This is called to fork and execute a command. If another command is 7470Sstevel@tonic-gate * to be forced, execute that instead. 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate void 7500Sstevel@tonic-gate do_exec(Session *s, const char *command) 7510Sstevel@tonic-gate { 7520Sstevel@tonic-gate if (command) 7530Sstevel@tonic-gate s->command = xstrdup(command); 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate if (forced_command) { 7560Sstevel@tonic-gate original_command = command; 7570Sstevel@tonic-gate command = forced_command; 7580Sstevel@tonic-gate debug("Forced command '%.900s'", command); 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate if (s->ttyfd != -1) 7620Sstevel@tonic-gate do_exec_pty(s, command); 7630Sstevel@tonic-gate else 7640Sstevel@tonic-gate do_exec_no_pty(s, command); 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate original_command = NULL; 7670Sstevel@tonic-gate } 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate /* administrative, login(1)-like work */ 7710Sstevel@tonic-gate void 7720Sstevel@tonic-gate do_login(Session *s, const char *command) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate char *time_string; 7750Sstevel@tonic-gate #ifndef ALTPRIVSEP 7760Sstevel@tonic-gate struct passwd * pw = s->pw; 7770Sstevel@tonic-gate #endif /* ALTPRIVSEP*/ 7780Sstevel@tonic-gate pid_t pid = getpid(); 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* Record that there was a login on that tty from the remote host. */ 7810Sstevel@tonic-gate #ifdef ALTPRIVSEP 7820Sstevel@tonic-gate debug3("Recording SSHv2 channel login in utmpx/wtmpx"); 7830Sstevel@tonic-gate altprivsep_record_login(pid, s->tty); 7840Sstevel@tonic-gate #else /* ALTPRIVSEP*/ 7850Sstevel@tonic-gate if (!use_privsep) { 7860Sstevel@tonic-gate debug3("Recording SSHv2 channel login in utmpx/wtmpx"); 7870Sstevel@tonic-gate record_login(pid, s->tty, NULL, pw->pw_name); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate #endif /* ALTPRIVSEP*/ 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate if (check_quietlogin(s, command)) 7920Sstevel@tonic-gate return; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate #ifdef USE_PAM 7950Sstevel@tonic-gate print_pam_messages(); 7960Sstevel@tonic-gate #endif /* USE_PAM */ 7970Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE 7980Sstevel@tonic-gate if (aixloginmsg && *aixloginmsg) 7990Sstevel@tonic-gate printf("%s\n", aixloginmsg); 8000Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */ 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate #ifndef NO_SSH_LASTLOG 8030Sstevel@tonic-gate if (options.print_lastlog && s->last_login_time != 0) { 8040Sstevel@tonic-gate time_string = ctime(&s->last_login_time); 8050Sstevel@tonic-gate if (strchr(time_string, '\n')) 8060Sstevel@tonic-gate *strchr(time_string, '\n') = 0; 8070Sstevel@tonic-gate if (strcmp(s->hostname, "") == 0) 8080Sstevel@tonic-gate printf("Last login: %s\r\n", time_string); 8090Sstevel@tonic-gate else 8100Sstevel@tonic-gate printf("Last login: %s from %s\r\n", time_string, 8110Sstevel@tonic-gate s->hostname); 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate #endif /* NO_SSH_LASTLOG */ 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate do_motd(); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * Display the message of the day. 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate void 8220Sstevel@tonic-gate do_motd(void) 8230Sstevel@tonic-gate { 8240Sstevel@tonic-gate FILE *f; 8250Sstevel@tonic-gate char buf[256]; 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate if (options.print_motd) { 8280Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 8290Sstevel@tonic-gate f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", 8300Sstevel@tonic-gate "/etc/motd"), "r"); 8310Sstevel@tonic-gate #else 8320Sstevel@tonic-gate f = fopen("/etc/motd", "r"); 8330Sstevel@tonic-gate #endif 8340Sstevel@tonic-gate if (f) { 8350Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f)) 8360Sstevel@tonic-gate fputs(buf, stdout); 8370Sstevel@tonic-gate fclose(f); 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /* 8440Sstevel@tonic-gate * Check for quiet login, either .hushlogin or command given. 8450Sstevel@tonic-gate */ 8460Sstevel@tonic-gate int 8470Sstevel@tonic-gate check_quietlogin(Session *s, const char *command) 8480Sstevel@tonic-gate { 8490Sstevel@tonic-gate char buf[256]; 8500Sstevel@tonic-gate struct passwd *pw = s->pw; 8510Sstevel@tonic-gate struct stat st; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate /* Return 1 if .hushlogin exists or a command given. */ 8540Sstevel@tonic-gate if (command != NULL) 8550Sstevel@tonic-gate return 1; 8560Sstevel@tonic-gate snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); 8570Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 8580Sstevel@tonic-gate if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) 8590Sstevel@tonic-gate return 1; 8600Sstevel@tonic-gate #else 8610Sstevel@tonic-gate if (stat(buf, &st) >= 0) 8620Sstevel@tonic-gate return 1; 8630Sstevel@tonic-gate #endif 8640Sstevel@tonic-gate return 0; 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * Sets the value of the given variable in the environment. If the variable 8690Sstevel@tonic-gate * already exists, its value is overriden. 8700Sstevel@tonic-gate */ 8710Sstevel@tonic-gate void 8720Sstevel@tonic-gate child_set_env(char ***envp, u_int *envsizep, const char *name, 8730Sstevel@tonic-gate const char *value) 8740Sstevel@tonic-gate { 8750Sstevel@tonic-gate u_int i, namelen; 8760Sstevel@tonic-gate char **env; 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate debug3("child_set_env(%s, %s)", name, value); 8790Sstevel@tonic-gate /* 8800Sstevel@tonic-gate * Find the slot where the value should be stored. If the variable 8810Sstevel@tonic-gate * already exists, we reuse the slot; otherwise we append a new slot 8820Sstevel@tonic-gate * at the end of the array, expanding if necessary. 8830Sstevel@tonic-gate */ 8840Sstevel@tonic-gate env = *envp; 8850Sstevel@tonic-gate namelen = strlen(name); 8860Sstevel@tonic-gate for (i = 0; env[i]; i++) 8870Sstevel@tonic-gate if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') 8880Sstevel@tonic-gate break; 8890Sstevel@tonic-gate if (env[i]) { 8900Sstevel@tonic-gate /* Reuse the slot. */ 8910Sstevel@tonic-gate xfree(env[i]); 8920Sstevel@tonic-gate } else { 8930Sstevel@tonic-gate /* New variable. Expand if necessary. */ 8940Sstevel@tonic-gate if (i >= (*envsizep) - 1) { 8950Sstevel@tonic-gate if (*envsizep >= 1000) 8960Sstevel@tonic-gate fatal("child_set_env: too many env vars," 8970Sstevel@tonic-gate " skipping: %.100s", name); 8980Sstevel@tonic-gate (*envsizep) += 50; 8990Sstevel@tonic-gate env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); 9000Sstevel@tonic-gate } 9010Sstevel@tonic-gate /* Need to set the NULL pointer at end of array beyond the new slot. */ 9020Sstevel@tonic-gate env[i + 1] = NULL; 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* Allocate space and format the variable in the appropriate slot. */ 9060Sstevel@tonic-gate env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); 9070Sstevel@tonic-gate snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate /* 9110Sstevel@tonic-gate * Reads environment variables from the given file and adds/overrides them 9120Sstevel@tonic-gate * into the environment. If the file does not exist, this does nothing. 9130Sstevel@tonic-gate * Otherwise, it must consist of empty lines, comments (line starts with '#') 9140Sstevel@tonic-gate * and assignments of the form name=value. No other forms are allowed. 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate static void 9170Sstevel@tonic-gate read_environment_file(char ***env, u_int *envsize, 9180Sstevel@tonic-gate const char *filename) 9190Sstevel@tonic-gate { 9200Sstevel@tonic-gate FILE *f; 9210Sstevel@tonic-gate char buf[4096]; 9220Sstevel@tonic-gate char *cp, *value; 9230Sstevel@tonic-gate u_int lineno = 0; 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate f = fopen(filename, "r"); 9260Sstevel@tonic-gate if (!f) 9270Sstevel@tonic-gate return; 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f)) { 9300Sstevel@tonic-gate if (++lineno > 1000) 9310Sstevel@tonic-gate fatal("Too many lines in environment file %s", filename); 9320Sstevel@tonic-gate for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) 9330Sstevel@tonic-gate ; 9340Sstevel@tonic-gate if (!*cp || *cp == '#' || *cp == '\n') 9350Sstevel@tonic-gate continue; 9360Sstevel@tonic-gate if (strchr(cp, '\n')) 9370Sstevel@tonic-gate *strchr(cp, '\n') = '\0'; 9380Sstevel@tonic-gate value = strchr(cp, '='); 9390Sstevel@tonic-gate if (value == NULL) { 9400Sstevel@tonic-gate fprintf(stderr, gettext("Bad line %u in %.100s\n"), 9410Sstevel@tonic-gate lineno, filename); 9420Sstevel@tonic-gate continue; 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate /* 9450Sstevel@tonic-gate * Replace the equals sign by nul, and advance value to 9460Sstevel@tonic-gate * the value string. 9470Sstevel@tonic-gate */ 9480Sstevel@tonic-gate *value = '\0'; 9490Sstevel@tonic-gate value++; 9500Sstevel@tonic-gate child_set_env(env, envsize, cp, value); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate fclose(f); 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate void copy_environment(char **source, char ***env, u_int *envsize) 9560Sstevel@tonic-gate { 9570Sstevel@tonic-gate char *var_name, *var_val; 9580Sstevel@tonic-gate int i; 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate if (source == NULL) 9610Sstevel@tonic-gate return; 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate for(i = 0; source[i] != NULL; i++) { 9640Sstevel@tonic-gate var_name = xstrdup(source[i]); 9650Sstevel@tonic-gate if ((var_val = strstr(var_name, "=")) == NULL) { 9660Sstevel@tonic-gate xfree(var_name); 9670Sstevel@tonic-gate continue; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate *var_val++ = '\0'; 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate debug3("Copy environment: %s=%s", var_name, var_val); 9720Sstevel@tonic-gate child_set_env(env, envsize, var_name, var_val); 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate xfree(var_name); 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate #ifdef HAVE_DEFOPEN 9790Sstevel@tonic-gate static 9800Sstevel@tonic-gate void 9810Sstevel@tonic-gate deflt_do_setup_env(Session *s, const char *shell, char ***env, u_int *envsize) 9820Sstevel@tonic-gate { 9830Sstevel@tonic-gate int flags; 9840Sstevel@tonic-gate char *ptr; 9850Sstevel@tonic-gate mode_t Umask = 022; 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate if (defopen(_PATH_DEFAULT_LOGIN)) 9880Sstevel@tonic-gate return; 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate /* Ignore case */ 9910Sstevel@tonic-gate flags = defcntl(DC_GETFLAGS, 0); 9920Sstevel@tonic-gate TURNOFF(flags, DC_CASE); 9930Sstevel@tonic-gate (void) defcntl(DC_SETFLAGS, flags); 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* TZ & HZ */ 9960Sstevel@tonic-gate if ((ptr = defread("TIMEZONE=")) != NULL) 9970Sstevel@tonic-gate child_set_env(env, envsize, "TZ", ptr); 9980Sstevel@tonic-gate if ((ptr = defread("HZ=")) != NULL) 9990Sstevel@tonic-gate child_set_env(env, envsize, "HZ", ptr); 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate /* PATH */ 10020Sstevel@tonic-gate if (s->pw->pw_uid != 0 && (ptr = defread("PATH=")) != NULL) 10030Sstevel@tonic-gate child_set_env(env, envsize, "PATH", ptr); 10040Sstevel@tonic-gate if (s->pw->pw_uid == 0 && (ptr = defread("SUPATH=")) != NULL) 10050Sstevel@tonic-gate child_set_env(env, envsize, "PATH", ptr); 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate /* SHELL */ 10080Sstevel@tonic-gate if ((ptr = defread("ALTSHELL=")) != NULL) { 10090Sstevel@tonic-gate if (strcasecmp("YES", ptr) == 0) 10100Sstevel@tonic-gate child_set_env(env, envsize, "SHELL", shell); 10110Sstevel@tonic-gate else 10120Sstevel@tonic-gate child_set_env(env, envsize, "SHELL", ""); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate /* UMASK */ 10160Sstevel@tonic-gate if ((ptr = defread("UMASK=")) != NULL && 10170Sstevel@tonic-gate sscanf(ptr, "%lo", &Umask) == 1 && 10180Sstevel@tonic-gate Umask <= (mode_t)0777) 10190Sstevel@tonic-gate (void) umask(Umask); 10200Sstevel@tonic-gate else 10210Sstevel@tonic-gate (void) umask(022); 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate /* ULIMIT */ 10240Sstevel@tonic-gate if ((ptr = defread("ULIMIT=")) != NULL && atol(ptr) > 0L && 10250Sstevel@tonic-gate ulimit(UL_SETFSIZE, atol(ptr)) < 0L) 10260Sstevel@tonic-gate error("Could not set ULIMIT to %ld from %s\n", atol(ptr), 10270Sstevel@tonic-gate _PATH_DEFAULT_LOGIN); 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate (void) defopen(NULL); 10300Sstevel@tonic-gate } 10310Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */ 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate static char ** 10340Sstevel@tonic-gate do_setup_env(Session *s, const char *shell) 10350Sstevel@tonic-gate { 1036*2757Sjp161948 char buf[256], *path_maildir = _PATH_MAILDIR; 1037*2757Sjp161948 u_int i, envsize, pm_len; 10380Sstevel@tonic-gate char **env; 10390Sstevel@tonic-gate struct passwd *pw = s->pw; 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate /* Initialize the environment. */ 10420Sstevel@tonic-gate envsize = 100; 10430Sstevel@tonic-gate env = xmalloc(envsize * sizeof(char *)); 10440Sstevel@tonic-gate env[0] = NULL; 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate #ifdef HAVE_CYGWIN 10470Sstevel@tonic-gate /* 10480Sstevel@tonic-gate * The Windows environment contains some setting which are 10490Sstevel@tonic-gate * important for a running system. They must not be dropped. 10500Sstevel@tonic-gate */ 10510Sstevel@tonic-gate copy_environment(environ, &env, &envsize); 10520Sstevel@tonic-gate #endif 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate #ifdef GSSAPI 10550Sstevel@tonic-gate /* Allow any GSSAPI methods that we've used to alter 10560Sstevel@tonic-gate * the childs environment as they see fit 10570Sstevel@tonic-gate */ 10580Sstevel@tonic-gate ssh_gssapi_do_child(xxx_gssctxt, &env,&envsize); 10590Sstevel@tonic-gate #endif 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate if (!options.use_login) { 10620Sstevel@tonic-gate /* Set basic environment. */ 10630Sstevel@tonic-gate child_set_env(&env, &envsize, "USER", pw->pw_name); 10640Sstevel@tonic-gate child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); 10650Sstevel@tonic-gate child_set_env(&env, &envsize, "HOME", pw->pw_dir); 10660Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 10670Sstevel@tonic-gate if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0) 10680Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); 10690Sstevel@tonic-gate else 10700Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH", getenv("PATH")); 10710Sstevel@tonic-gate #else /* HAVE_LOGIN_CAP */ 10720Sstevel@tonic-gate # ifndef HAVE_CYGWIN 10730Sstevel@tonic-gate /* 10740Sstevel@tonic-gate * There's no standard path on Windows. The path contains 10750Sstevel@tonic-gate * important components pointing to the system directories, 10760Sstevel@tonic-gate * needed for loading shared libraries. So the path better 10770Sstevel@tonic-gate * remains intact here. 10780Sstevel@tonic-gate */ 10790Sstevel@tonic-gate # ifdef SUPERUSER_PATH 10800Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH", 10810Sstevel@tonic-gate s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH); 10820Sstevel@tonic-gate # else 10830Sstevel@tonic-gate child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); 10840Sstevel@tonic-gate # endif /* SUPERUSER_PATH */ 10850Sstevel@tonic-gate # endif /* HAVE_CYGWIN */ 10860Sstevel@tonic-gate #endif /* HAVE_LOGIN_CAP */ 10870Sstevel@tonic-gate 1088*2757Sjp161948 pm_len = strlen(path_maildir); 1089*2757Sjp161948 if (path_maildir[pm_len - 1] == '/' && pm_len > 1) 1090*2757Sjp161948 path_maildir[pm_len - 1] = NULL; 10910Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.200s/%.50s", 1092*2757Sjp161948 path_maildir, pw->pw_name); 10930Sstevel@tonic-gate child_set_env(&env, &envsize, "MAIL", buf); 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate /* Normal systems set SHELL by default. */ 10960Sstevel@tonic-gate child_set_env(&env, &envsize, "SHELL", shell); 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate #ifdef HAVE_DEFOPEN 10990Sstevel@tonic-gate deflt_do_setup_env(s, shell, &env, &envsize); 11000Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */ 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate #define PASS_ENV(x) \ 11040Sstevel@tonic-gate if (getenv(x)) \ 11050Sstevel@tonic-gate child_set_env(&env, &envsize, x, getenv(x)); 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate if (getenv("TZ")) 11080Sstevel@tonic-gate child_set_env(&env, &envsize, "TZ", getenv("TZ")); 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate PASS_ENV("LANG") 11110Sstevel@tonic-gate PASS_ENV("LC_ALL") 11120Sstevel@tonic-gate PASS_ENV("LC_CTYPE") 11130Sstevel@tonic-gate PASS_ENV("LC_COLLATE") 11140Sstevel@tonic-gate PASS_ENV("LC_CTIME") 11150Sstevel@tonic-gate PASS_ENV("LC_NUMERIC") 11160Sstevel@tonic-gate PASS_ENV("LC_MONETARY") 11170Sstevel@tonic-gate PASS_ENV("LC_MESSAGES") 11180Sstevel@tonic-gate 11190Sstevel@tonic-gate #undef PASS_ENV 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate if (s->env != NULL) 11220Sstevel@tonic-gate copy_environment(s->env, &env, &envsize); 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate /* Set custom environment options from RSA authentication. */ 11250Sstevel@tonic-gate if (!options.use_login) { 11260Sstevel@tonic-gate while (custom_environment) { 11270Sstevel@tonic-gate struct envstring *ce = custom_environment; 11280Sstevel@tonic-gate char *str = ce->s; 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate for (i = 0; str[i] != '=' && str[i]; i++) 11310Sstevel@tonic-gate ; 11320Sstevel@tonic-gate if (str[i] == '=') { 11330Sstevel@tonic-gate str[i] = 0; 11340Sstevel@tonic-gate child_set_env(&env, &envsize, str, str + i + 1); 11350Sstevel@tonic-gate } 11360Sstevel@tonic-gate custom_environment = ce->next; 11370Sstevel@tonic-gate xfree(ce->s); 11380Sstevel@tonic-gate xfree(ce); 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate } 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate /* SSH_CLIENT deprecated */ 11430Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.50s %d %d", 11440Sstevel@tonic-gate get_remote_ipaddr(), get_remote_port(), get_local_port()); 11450Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_CLIENT", buf); 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.50s %d %.50s %d", 11480Sstevel@tonic-gate get_remote_ipaddr(), get_remote_port(), 11490Sstevel@tonic-gate get_local_ipaddr(packet_get_connection_in()), get_local_port()); 11500Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_CONNECTION", buf); 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate if (s->ttyfd != -1) 11530Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_TTY", s->tty); 11540Sstevel@tonic-gate if (s->term) 11550Sstevel@tonic-gate child_set_env(&env, &envsize, "TERM", s->term); 11560Sstevel@tonic-gate if (s->display) 11570Sstevel@tonic-gate child_set_env(&env, &envsize, "DISPLAY", s->display); 11580Sstevel@tonic-gate if (original_command) 11590Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", 11600Sstevel@tonic-gate original_command); 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate #ifdef _UNICOS 11630Sstevel@tonic-gate if (cray_tmpdir[0] != '\0') 11640Sstevel@tonic-gate child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir); 11650Sstevel@tonic-gate #endif /* _UNICOS */ 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate #ifdef _AIX 11680Sstevel@tonic-gate { 11690Sstevel@tonic-gate char *cp; 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate if ((cp = getenv("AUTHSTATE")) != NULL) 11720Sstevel@tonic-gate child_set_env(&env, &envsize, "AUTHSTATE", cp); 11730Sstevel@tonic-gate if ((cp = getenv("KRB5CCNAME")) != NULL) 11740Sstevel@tonic-gate child_set_env(&env, &envsize, "KRB5CCNAME", cp); 11750Sstevel@tonic-gate read_environment_file(&env, &envsize, "/etc/environment"); 11760Sstevel@tonic-gate } 11770Sstevel@tonic-gate #endif 11780Sstevel@tonic-gate #ifdef KRB4 11790Sstevel@tonic-gate if (s->authctxt->krb4_ticket_file) 11800Sstevel@tonic-gate child_set_env(&env, &envsize, "KRBTKFILE", 11810Sstevel@tonic-gate s->authctxt->krb4_ticket_file); 11820Sstevel@tonic-gate #endif 11830Sstevel@tonic-gate #ifdef KRB5 11840Sstevel@tonic-gate if (s->authctxt->krb5_ticket_file) 11850Sstevel@tonic-gate child_set_env(&env, &envsize, "KRB5CCNAME", 11860Sstevel@tonic-gate s->authctxt->krb5_ticket_file); 11870Sstevel@tonic-gate #endif 11880Sstevel@tonic-gate #ifdef USE_PAM 11890Sstevel@tonic-gate /* 11900Sstevel@tonic-gate * Pull in any environment variables that may have 11910Sstevel@tonic-gate * been set by PAM. 11920Sstevel@tonic-gate */ 11930Sstevel@tonic-gate { 11940Sstevel@tonic-gate char **p; 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate p = fetch_pam_environment(s->authctxt); 11970Sstevel@tonic-gate copy_environment(p, &env, &envsize); 11980Sstevel@tonic-gate free_pam_environment(p); 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate #endif /* USE_PAM */ 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate if (auth_sock_name != NULL) 12030Sstevel@tonic-gate child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, 12040Sstevel@tonic-gate auth_sock_name); 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate /* read $HOME/.ssh/environment. */ 12070Sstevel@tonic-gate if (options.permit_user_env && !options.use_login) { 12080Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.200s/.ssh/environment", 12090Sstevel@tonic-gate strcmp(pw->pw_dir, "/") ? pw->pw_dir : ""); 12100Sstevel@tonic-gate read_environment_file(&env, &envsize, buf); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate if (debug_flag) { 12130Sstevel@tonic-gate /* dump the environment */ 12140Sstevel@tonic-gate fprintf(stderr, gettext("Environment:\n")); 12150Sstevel@tonic-gate for (i = 0; env[i]; i++) 12160Sstevel@tonic-gate fprintf(stderr, " %.200s\n", env[i]); 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate return env; 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate /* 12220Sstevel@tonic-gate * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found 12230Sstevel@tonic-gate * first in this order). 12240Sstevel@tonic-gate */ 12250Sstevel@tonic-gate static void 12260Sstevel@tonic-gate do_rc_files(Session *s, const char *shell) 12270Sstevel@tonic-gate { 12280Sstevel@tonic-gate FILE *f = NULL; 12290Sstevel@tonic-gate char cmd[1024]; 12300Sstevel@tonic-gate int do_xauth; 12310Sstevel@tonic-gate struct stat st; 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate do_xauth = 12340Sstevel@tonic-gate s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate /* ignore _PATH_SSH_USER_RC for subsystems */ 12370Sstevel@tonic-gate if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) { 12380Sstevel@tonic-gate snprintf(cmd, sizeof cmd, "%s -c '%s %s'", 12390Sstevel@tonic-gate shell, _PATH_BSHELL, _PATH_SSH_USER_RC); 12400Sstevel@tonic-gate if (debug_flag) 12410Sstevel@tonic-gate fprintf(stderr, "Running %s\n", cmd); 12420Sstevel@tonic-gate f = popen(cmd, "w"); 12430Sstevel@tonic-gate if (f) { 12440Sstevel@tonic-gate if (do_xauth) 12450Sstevel@tonic-gate fprintf(f, "%s %s\n", s->auth_proto, 12460Sstevel@tonic-gate s->auth_data); 12470Sstevel@tonic-gate pclose(f); 12480Sstevel@tonic-gate } else 12490Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n", 12500Sstevel@tonic-gate _PATH_SSH_USER_RC); 12510Sstevel@tonic-gate } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { 12520Sstevel@tonic-gate if (debug_flag) 12530Sstevel@tonic-gate fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, 12540Sstevel@tonic-gate _PATH_SSH_SYSTEM_RC); 12550Sstevel@tonic-gate f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); 12560Sstevel@tonic-gate if (f) { 12570Sstevel@tonic-gate if (do_xauth) 12580Sstevel@tonic-gate fprintf(f, "%s %s\n", s->auth_proto, 12590Sstevel@tonic-gate s->auth_data); 12600Sstevel@tonic-gate pclose(f); 12610Sstevel@tonic-gate } else 12620Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n", 12630Sstevel@tonic-gate _PATH_SSH_SYSTEM_RC); 12640Sstevel@tonic-gate } else if (do_xauth && options.xauth_location != NULL) { 12650Sstevel@tonic-gate /* Add authority data to .Xauthority if appropriate. */ 12660Sstevel@tonic-gate if (debug_flag) { 12670Sstevel@tonic-gate fprintf(stderr, 12680Sstevel@tonic-gate "Running %.500s add " 12690Sstevel@tonic-gate "%.100s %.100s %.100s\n", 12700Sstevel@tonic-gate options.xauth_location, s->auth_display, 12710Sstevel@tonic-gate s->auth_proto, s->auth_data); 12720Sstevel@tonic-gate } 12730Sstevel@tonic-gate snprintf(cmd, sizeof cmd, "%s -q -", 12740Sstevel@tonic-gate options.xauth_location); 12750Sstevel@tonic-gate f = popen(cmd, "w"); 12760Sstevel@tonic-gate if (f) { 12770Sstevel@tonic-gate fprintf(f, "add %s %s %s\n", 12780Sstevel@tonic-gate s->auth_display, s->auth_proto, 12790Sstevel@tonic-gate s->auth_data); 12800Sstevel@tonic-gate pclose(f); 12810Sstevel@tonic-gate } else { 12820Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n", 12830Sstevel@tonic-gate cmd); 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate } 12860Sstevel@tonic-gate } 12870Sstevel@tonic-gate 12880Sstevel@tonic-gate static void 12890Sstevel@tonic-gate do_nologin(struct passwd *pw) 12900Sstevel@tonic-gate { 12910Sstevel@tonic-gate FILE *f = NULL; 12920Sstevel@tonic-gate char buf[1024]; 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 12950Sstevel@tonic-gate if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid) 12960Sstevel@tonic-gate f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN, 12970Sstevel@tonic-gate _PATH_NOLOGIN), "r"); 12980Sstevel@tonic-gate #else 12990Sstevel@tonic-gate if (pw->pw_uid) 13000Sstevel@tonic-gate f = fopen(_PATH_NOLOGIN, "r"); 13010Sstevel@tonic-gate #endif 13020Sstevel@tonic-gate if (f) { 13030Sstevel@tonic-gate /* /etc/nologin exists. Print its contents and exit. */ 13040Sstevel@tonic-gate log("User %.100s not allowed because %s exists", 13050Sstevel@tonic-gate pw->pw_name, _PATH_NOLOGIN); 13060Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f)) 13070Sstevel@tonic-gate fputs(buf, stderr); 13080Sstevel@tonic-gate fclose(f); 13090Sstevel@tonic-gate exit(254); 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate /* Set login name, uid, gid, and groups. */ 13140Sstevel@tonic-gate void 13150Sstevel@tonic-gate do_setusercontext(struct passwd *pw) 13160Sstevel@tonic-gate { 13170Sstevel@tonic-gate #ifdef HAVE_CYGWIN 13180Sstevel@tonic-gate if (is_winnt) { 13190Sstevel@tonic-gate #else /* HAVE_CYGWIN */ 13200Sstevel@tonic-gate if (getuid() == 0 || geteuid() == 0) { 13210Sstevel@tonic-gate #endif /* HAVE_CYGWIN */ 13220Sstevel@tonic-gate #ifdef HAVE_SETPCRED 13230Sstevel@tonic-gate setpcred(pw->pw_name); 13240Sstevel@tonic-gate #endif /* HAVE_SETPCRED */ 13250Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 13260Sstevel@tonic-gate # ifdef __bsdi__ 13270Sstevel@tonic-gate setpgid(0, 0); 13280Sstevel@tonic-gate # endif 13290Sstevel@tonic-gate if (setusercontext(lc, pw, pw->pw_uid, 13300Sstevel@tonic-gate (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) { 13310Sstevel@tonic-gate perror("unable to set user context"); 13320Sstevel@tonic-gate exit(1); 13330Sstevel@tonic-gate } 13340Sstevel@tonic-gate #else 13350Sstevel@tonic-gate # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID) 13360Sstevel@tonic-gate /* Sets login uid for accounting */ 13370Sstevel@tonic-gate if (getluid() == -1 && setluid(pw->pw_uid) == -1) 13380Sstevel@tonic-gate error("setluid: %s", strerror(errno)); 13390Sstevel@tonic-gate # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */ 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate if (setlogin(pw->pw_name) < 0) 13420Sstevel@tonic-gate error("setlogin failed: %s", strerror(errno)); 13430Sstevel@tonic-gate if (setgid(pw->pw_gid) < 0) { 13440Sstevel@tonic-gate perror("setgid"); 13450Sstevel@tonic-gate exit(1); 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate /* Initialize the group list. */ 13480Sstevel@tonic-gate if (initgroups(pw->pw_name, pw->pw_gid) < 0) { 13490Sstevel@tonic-gate perror("initgroups"); 13500Sstevel@tonic-gate exit(1); 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate endgrent(); 13530Sstevel@tonic-gate # if 0 13540Sstevel@tonic-gate # ifdef USE_PAM 13550Sstevel@tonic-gate /* 13560Sstevel@tonic-gate * PAM credentials may take the form of supplementary groups. 13570Sstevel@tonic-gate * These will have been wiped by the above initgroups() call. 13580Sstevel@tonic-gate * Reestablish them here. 13590Sstevel@tonic-gate */ 13600Sstevel@tonic-gate do_pam_setcred(0); 13610Sstevel@tonic-gate # endif /* USE_PAM */ 13620Sstevel@tonic-gate # endif /* 0 */ 13630Sstevel@tonic-gate # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) 13640Sstevel@tonic-gate irix_setusercontext(pw); 13650Sstevel@tonic-gate # endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */ 13660Sstevel@tonic-gate # ifdef _AIX 13670Sstevel@tonic-gate aix_usrinfo(pw); 13680Sstevel@tonic-gate # endif /* _AIX */ 13690Sstevel@tonic-gate /* Permanently switch to the desired uid. */ 13700Sstevel@tonic-gate permanently_set_uid(pw); 13710Sstevel@tonic-gate #endif 13720Sstevel@tonic-gate } 13730Sstevel@tonic-gate if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) 13740Sstevel@tonic-gate fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); 13750Sstevel@tonic-gate } 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate static void 13780Sstevel@tonic-gate launch_login(struct passwd *pw, const char *hostname) 13790Sstevel@tonic-gate { 13800Sstevel@tonic-gate /* Launch login(1). */ 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate execl(LOGIN_PROGRAM, "login", "-h", hostname, 13830Sstevel@tonic-gate #ifdef xxxLOGIN_NEEDS_TERM 13840Sstevel@tonic-gate (s->term ? s->term : "unknown"), 13850Sstevel@tonic-gate #endif /* LOGIN_NEEDS_TERM */ 13860Sstevel@tonic-gate #ifdef LOGIN_NO_ENDOPT 13870Sstevel@tonic-gate "-p", "-f", pw->pw_name, (char *)NULL); 13880Sstevel@tonic-gate #else 13890Sstevel@tonic-gate "-p", "-f", "--", pw->pw_name, (char *)NULL); 13900Sstevel@tonic-gate #endif 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate /* Login couldn't be executed, die. */ 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate perror("login"); 13950Sstevel@tonic-gate exit(1); 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate /* 13990Sstevel@tonic-gate * Performs common processing for the child, such as setting up the 14000Sstevel@tonic-gate * environment, closing extra file descriptors, setting the user and group 14010Sstevel@tonic-gate * ids, and executing the command or shell. 14020Sstevel@tonic-gate */ 14030Sstevel@tonic-gate void 14040Sstevel@tonic-gate do_child(Session *s, const char *command) 14050Sstevel@tonic-gate { 14060Sstevel@tonic-gate extern char **environ; 14070Sstevel@tonic-gate char **env; 14080Sstevel@tonic-gate char *argv[10]; 14090Sstevel@tonic-gate const char *shell, *shell0, *hostname = NULL; 14100Sstevel@tonic-gate struct passwd *pw = s->pw; 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate /* remove hostkey from the child's memory */ 14130Sstevel@tonic-gate destroy_sensitive_data(); 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate /* login(1) is only called if we execute the login shell */ 14160Sstevel@tonic-gate if (options.use_login && command != NULL) 14170Sstevel@tonic-gate options.use_login = 0; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate #ifdef _UNICOS 14200Sstevel@tonic-gate cray_setup(pw->pw_uid, pw->pw_name, command); 14210Sstevel@tonic-gate #endif /* _UNICOS */ 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate /* 14240Sstevel@tonic-gate * Login(1) does this as well, and it needs uid 0 for the "-h" 14250Sstevel@tonic-gate * switch, so we let login(1) to this for us. 14260Sstevel@tonic-gate */ 14270Sstevel@tonic-gate if (!options.use_login) { 14280Sstevel@tonic-gate #ifdef HAVE_OSF_SIA 14290Sstevel@tonic-gate session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty); 14300Sstevel@tonic-gate if (!check_quietlogin(s, command)) 14310Sstevel@tonic-gate do_motd(); 14320Sstevel@tonic-gate #else /* HAVE_OSF_SIA */ 14330Sstevel@tonic-gate do_nologin(pw); 14340Sstevel@tonic-gate do_setusercontext(pw); 14350Sstevel@tonic-gate #endif /* HAVE_OSF_SIA */ 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * Get the shell from the password data. An empty shell field is 14400Sstevel@tonic-gate * legal, and means /bin/sh. 14410Sstevel@tonic-gate */ 14420Sstevel@tonic-gate shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; 14430Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 14440Sstevel@tonic-gate shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); 14450Sstevel@tonic-gate #endif 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate env = do_setup_env(s, shell); 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate /* we have to stash the hostname before we close our socket. */ 14500Sstevel@tonic-gate if (options.use_login) 14510Sstevel@tonic-gate hostname = get_remote_name_or_ip(utmp_len, 14520Sstevel@tonic-gate options.verify_reverse_mapping); 14530Sstevel@tonic-gate /* 14540Sstevel@tonic-gate * Close the connection descriptors; note that this is the child, and 14550Sstevel@tonic-gate * the server will still have the socket open, and it is important 14560Sstevel@tonic-gate * that we do not shutdown it. Note that the descriptors cannot be 14570Sstevel@tonic-gate * closed before building the environment, as we call 14580Sstevel@tonic-gate * get_remote_ipaddr there. 14590Sstevel@tonic-gate */ 14600Sstevel@tonic-gate if (packet_get_connection_in() == packet_get_connection_out()) 14610Sstevel@tonic-gate close(packet_get_connection_in()); 14620Sstevel@tonic-gate else { 14630Sstevel@tonic-gate close(packet_get_connection_in()); 14640Sstevel@tonic-gate close(packet_get_connection_out()); 14650Sstevel@tonic-gate } 14660Sstevel@tonic-gate /* 14670Sstevel@tonic-gate * Close all descriptors related to channels. They will still remain 14680Sstevel@tonic-gate * open in the parent. 14690Sstevel@tonic-gate */ 14700Sstevel@tonic-gate /* XXX better use close-on-exec? -markus */ 14710Sstevel@tonic-gate channel_close_all(); 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate /* 14740Sstevel@tonic-gate * Close any extra file descriptors. Note that there may still be 14750Sstevel@tonic-gate * descriptors left by system functions. They will be closed later. 14760Sstevel@tonic-gate */ 14770Sstevel@tonic-gate endpwent(); 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate /* 14800Sstevel@tonic-gate * Close any extra open file descriptors so that we don\'t have them 14810Sstevel@tonic-gate * hanging around in clients. Note that we want to do this after 14820Sstevel@tonic-gate * initgroups, because at least on Solaris 2.3 it leaves file 14830Sstevel@tonic-gate * descriptors open. 14840Sstevel@tonic-gate */ 14850Sstevel@tonic-gate closefrom(STDERR_FILENO + 1); 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * Must take new environment into use so that .ssh/rc, 14890Sstevel@tonic-gate * /etc/ssh/sshrc and xauth are run in the proper environment. 14900Sstevel@tonic-gate */ 14910Sstevel@tonic-gate environ = env; 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate #ifdef AFS 14940Sstevel@tonic-gate /* Try to get AFS tokens for the local cell. */ 14950Sstevel@tonic-gate if (k_hasafs()) { 14960Sstevel@tonic-gate char cell[64]; 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) 14990Sstevel@tonic-gate krb_afslog(cell, 0); 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate krb_afslog(0, 0); 15020Sstevel@tonic-gate } 15030Sstevel@tonic-gate #endif /* AFS */ 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate /* Change current directory to the user\'s home directory. */ 15060Sstevel@tonic-gate if (chdir(pw->pw_dir) < 0) { 15070Sstevel@tonic-gate fprintf(stderr, 15080Sstevel@tonic-gate gettext("Could not chdir to home directory %s: %s\n"), 15090Sstevel@tonic-gate pw->pw_dir, strerror(errno)); 15100Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP 15110Sstevel@tonic-gate if (login_getcapbool(lc, "requirehome", 0)) 15120Sstevel@tonic-gate exit(1); 15130Sstevel@tonic-gate #endif 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate if (!options.use_login) 15170Sstevel@tonic-gate do_rc_files(s, shell); 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate /* restore SIGPIPE for child */ 15200Sstevel@tonic-gate signal(SIGPIPE, SIG_DFL); 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate if (options.use_login) { 15230Sstevel@tonic-gate launch_login(pw, hostname); 15240Sstevel@tonic-gate /* NEVERREACHED */ 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate 15270Sstevel@tonic-gate /* Get the last component of the shell name. */ 15280Sstevel@tonic-gate if ((shell0 = strrchr(shell, '/')) != NULL) 15290Sstevel@tonic-gate shell0++; 15300Sstevel@tonic-gate else 15310Sstevel@tonic-gate shell0 = shell; 15320Sstevel@tonic-gate 15330Sstevel@tonic-gate /* 15340Sstevel@tonic-gate * If we have no command, execute the shell. In this case, the shell 15350Sstevel@tonic-gate * name to be passed in argv[0] is preceded by '-' to indicate that 15360Sstevel@tonic-gate * this is a login shell. 15370Sstevel@tonic-gate */ 15380Sstevel@tonic-gate if (!command) { 15390Sstevel@tonic-gate char argv0[256]; 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate /* Start the shell. Set initial character to '-'. */ 15420Sstevel@tonic-gate argv0[0] = '-'; 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1) 15450Sstevel@tonic-gate >= sizeof(argv0) - 1) { 15460Sstevel@tonic-gate errno = EINVAL; 15470Sstevel@tonic-gate perror(shell); 15480Sstevel@tonic-gate exit(1); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate /* Execute the shell. */ 15520Sstevel@tonic-gate argv[0] = argv0; 15530Sstevel@tonic-gate argv[1] = NULL; 15540Sstevel@tonic-gate execve(shell, argv, env); 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate /* Executing the shell failed. */ 15570Sstevel@tonic-gate perror(shell); 15580Sstevel@tonic-gate exit(1); 15590Sstevel@tonic-gate } 15600Sstevel@tonic-gate /* 15610Sstevel@tonic-gate * Execute the command using the user's shell. This uses the -c 15620Sstevel@tonic-gate * option to execute the command. 15630Sstevel@tonic-gate */ 15640Sstevel@tonic-gate argv[0] = (char *) shell0; 15650Sstevel@tonic-gate argv[1] = "-c"; 15660Sstevel@tonic-gate argv[2] = (char *) command; 15670Sstevel@tonic-gate argv[3] = NULL; 15680Sstevel@tonic-gate execve(shell, argv, env); 15690Sstevel@tonic-gate perror(shell); 15700Sstevel@tonic-gate exit(1); 15710Sstevel@tonic-gate } 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate Session * 15740Sstevel@tonic-gate session_new(void) 15750Sstevel@tonic-gate { 15760Sstevel@tonic-gate int i; 15770Sstevel@tonic-gate static int did_init = 0; 15780Sstevel@tonic-gate if (!did_init) { 15790Sstevel@tonic-gate debug("session_new: init"); 15800Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 15810Sstevel@tonic-gate sessions[i].used = 0; 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate did_init = 1; 15840Sstevel@tonic-gate } 15850Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 15860Sstevel@tonic-gate Session *s = &sessions[i]; 15870Sstevel@tonic-gate if (! s->used) { 15880Sstevel@tonic-gate memset(s, 0, sizeof(*s)); 15890Sstevel@tonic-gate s->chanid = -1; 15900Sstevel@tonic-gate s->ptyfd = -1; 15910Sstevel@tonic-gate s->ttyfd = -1; 15920Sstevel@tonic-gate s->used = 1; 15930Sstevel@tonic-gate s->self = i; 15940Sstevel@tonic-gate s->env = NULL; 15950Sstevel@tonic-gate debug("session_new: session %d", i); 15960Sstevel@tonic-gate return s; 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate return NULL; 16000Sstevel@tonic-gate } 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate static void 16030Sstevel@tonic-gate session_dump(void) 16040Sstevel@tonic-gate { 16050Sstevel@tonic-gate int i; 16060Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 16070Sstevel@tonic-gate Session *s = &sessions[i]; 16080Sstevel@tonic-gate debug("dump: used %d session %d %p channel %d pid %ld", 16090Sstevel@tonic-gate s->used, 16100Sstevel@tonic-gate s->self, 16110Sstevel@tonic-gate s, 16120Sstevel@tonic-gate s->chanid, 16130Sstevel@tonic-gate (long)s->pid); 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate } 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate int 16180Sstevel@tonic-gate session_open(Authctxt *authctxt, int chanid) 16190Sstevel@tonic-gate { 16200Sstevel@tonic-gate Session *s = session_new(); 16210Sstevel@tonic-gate debug("session_open: channel %d", chanid); 16220Sstevel@tonic-gate if (s == NULL) { 16230Sstevel@tonic-gate error("no more sessions"); 16240Sstevel@tonic-gate return 0; 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate s->authctxt = authctxt; 16270Sstevel@tonic-gate s->pw = authctxt->pw; 16280Sstevel@tonic-gate if (s->pw == NULL) 16290Sstevel@tonic-gate fatal("no user for session %d", s->self); 16300Sstevel@tonic-gate debug("session_open: session %d: link with channel %d", s->self, chanid); 16310Sstevel@tonic-gate s->chanid = chanid; 16320Sstevel@tonic-gate return 1; 16330Sstevel@tonic-gate } 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate #ifndef lint 16360Sstevel@tonic-gate Session * 16370Sstevel@tonic-gate session_by_tty(char *tty) 16380Sstevel@tonic-gate { 16390Sstevel@tonic-gate int i; 16400Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 16410Sstevel@tonic-gate Session *s = &sessions[i]; 16420Sstevel@tonic-gate if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) { 16430Sstevel@tonic-gate debug("session_by_tty: session %d tty %s", i, tty); 16440Sstevel@tonic-gate return s; 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate debug("session_by_tty: unknown tty %.100s", tty); 16480Sstevel@tonic-gate session_dump(); 16490Sstevel@tonic-gate return NULL; 16500Sstevel@tonic-gate } 16510Sstevel@tonic-gate #endif /* lint */ 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate static Session * 16540Sstevel@tonic-gate session_by_channel(int id) 16550Sstevel@tonic-gate { 16560Sstevel@tonic-gate int i; 16570Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 16580Sstevel@tonic-gate Session *s = &sessions[i]; 16590Sstevel@tonic-gate if (s->used && s->chanid == id) { 16600Sstevel@tonic-gate debug("session_by_channel: session %d channel %d", i, id); 16610Sstevel@tonic-gate return s; 16620Sstevel@tonic-gate } 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate debug("session_by_channel: unknown channel %d", id); 16650Sstevel@tonic-gate session_dump(); 16660Sstevel@tonic-gate return NULL; 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate static Session * 16700Sstevel@tonic-gate session_by_pid(pid_t pid) 16710Sstevel@tonic-gate { 16720Sstevel@tonic-gate int i; 16730Sstevel@tonic-gate debug("session_by_pid: pid %ld", (long)pid); 16740Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 16750Sstevel@tonic-gate Session *s = &sessions[i]; 16760Sstevel@tonic-gate if (s->used && s->pid == pid) 16770Sstevel@tonic-gate return s; 16780Sstevel@tonic-gate } 16790Sstevel@tonic-gate error("session_by_pid: unknown pid %ld", (long)pid); 16800Sstevel@tonic-gate session_dump(); 16810Sstevel@tonic-gate return NULL; 16820Sstevel@tonic-gate } 16830Sstevel@tonic-gate 16840Sstevel@tonic-gate static int 16850Sstevel@tonic-gate session_window_change_req(Session *s) 16860Sstevel@tonic-gate { 16870Sstevel@tonic-gate s->col = packet_get_int(); 16880Sstevel@tonic-gate s->row = packet_get_int(); 16890Sstevel@tonic-gate s->xpixel = packet_get_int(); 16900Sstevel@tonic-gate s->ypixel = packet_get_int(); 16910Sstevel@tonic-gate packet_check_eom(); 16920Sstevel@tonic-gate pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); 16930Sstevel@tonic-gate return 1; 16940Sstevel@tonic-gate } 16950Sstevel@tonic-gate 16960Sstevel@tonic-gate static int 16970Sstevel@tonic-gate session_pty_req(Session *s) 16980Sstevel@tonic-gate { 16990Sstevel@tonic-gate u_int len; 17000Sstevel@tonic-gate int n_bytes; 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate if (no_pty_flag) { 17030Sstevel@tonic-gate debug("Allocating a pty not permitted for this authentication."); 17040Sstevel@tonic-gate return 0; 17050Sstevel@tonic-gate } 17060Sstevel@tonic-gate if (s->ttyfd != -1) { 17070Sstevel@tonic-gate packet_disconnect("Protocol error: you already have a pty."); 17080Sstevel@tonic-gate return 0; 17090Sstevel@tonic-gate } 17100Sstevel@tonic-gate /* Get the time and hostname when the user last logged in. */ 17110Sstevel@tonic-gate if (options.print_lastlog) { 17120Sstevel@tonic-gate s->hostname[0] = '\0'; 17130Sstevel@tonic-gate s->last_login_time = get_last_login_time(s->pw->pw_uid, 17140Sstevel@tonic-gate s->pw->pw_name, s->hostname, sizeof(s->hostname)); 17150Sstevel@tonic-gate 17160Sstevel@tonic-gate /* 17170Sstevel@tonic-gate * PAM may update the last login date. 17180Sstevel@tonic-gate * 17190Sstevel@tonic-gate * Ideally PAM would also show the last login date as a 17200Sstevel@tonic-gate * PAM_TEXT_INFO conversation message, and then we could just 17210Sstevel@tonic-gate * always force the use of keyboard-interactive just so we can 17220Sstevel@tonic-gate * pass any such PAM prompts and messages from the account and 17230Sstevel@tonic-gate * session stacks, but skip pam_authenticate() if other userauth 17240Sstevel@tonic-gate * has succeeded and the user's password isn't expired. 17250Sstevel@tonic-gate * 17260Sstevel@tonic-gate * Unfortunately this depends on support for keyboard- 17270Sstevel@tonic-gate * interactive in the client, and support for lastlog messages 17280Sstevel@tonic-gate * in some PAM module. 17290Sstevel@tonic-gate * 17300Sstevel@tonic-gate * As it is Solaris updates the lastlog in PAM, but does 17310Sstevel@tonic-gate * not show the lastlog date in PAM. If and when this state of 17320Sstevel@tonic-gate * affairs changes this hack can be reconsidered, and, maybe, 17330Sstevel@tonic-gate * removed. 17340Sstevel@tonic-gate * 17350Sstevel@tonic-gate * So we're stuck with a crude hack: get the lastlog 17360Sstevel@tonic-gate * time before calling pam_open_session() and store it 17370Sstevel@tonic-gate * in the Authctxt and then use it here once. After 17380Sstevel@tonic-gate * that, if the client opens any more pty sessions we'll 17390Sstevel@tonic-gate * show the last lastlog entry since userauth. 17400Sstevel@tonic-gate */ 17410Sstevel@tonic-gate if (s->authctxt != NULL && s->authctxt->last_login_time > 0) { 17420Sstevel@tonic-gate s->last_login_time = s->authctxt->last_login_time; 17430Sstevel@tonic-gate (void) strlcpy(s->hostname, 17440Sstevel@tonic-gate s->authctxt->last_login_host, 17450Sstevel@tonic-gate sizeof(s->hostname)); 17460Sstevel@tonic-gate s->authctxt->last_login_time = 0; 17470Sstevel@tonic-gate s->authctxt->last_login_host[0] = '\0'; 17480Sstevel@tonic-gate } 17490Sstevel@tonic-gate } 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate s->term = packet_get_string(&len); 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate if (compat20) { 17540Sstevel@tonic-gate s->col = packet_get_int(); 17550Sstevel@tonic-gate s->row = packet_get_int(); 17560Sstevel@tonic-gate } else { 17570Sstevel@tonic-gate s->row = packet_get_int(); 17580Sstevel@tonic-gate s->col = packet_get_int(); 17590Sstevel@tonic-gate } 17600Sstevel@tonic-gate s->xpixel = packet_get_int(); 17610Sstevel@tonic-gate s->ypixel = packet_get_int(); 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate if (strcmp(s->term, "") == 0) { 17640Sstevel@tonic-gate xfree(s->term); 17650Sstevel@tonic-gate s->term = NULL; 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate /* Allocate a pty and open it. */ 17690Sstevel@tonic-gate debug("Allocating pty."); 17700Sstevel@tonic-gate if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) { 17710Sstevel@tonic-gate if (s->term) 17720Sstevel@tonic-gate xfree(s->term); 17730Sstevel@tonic-gate s->term = NULL; 17740Sstevel@tonic-gate s->ptyfd = -1; 17750Sstevel@tonic-gate s->ttyfd = -1; 17760Sstevel@tonic-gate error("session_pty_req: session %d alloc failed", s->self); 17770Sstevel@tonic-gate return 0; 17780Sstevel@tonic-gate } 17790Sstevel@tonic-gate debug("session_pty_req: session %d alloc %s", s->self, s->tty); 17800Sstevel@tonic-gate 17810Sstevel@tonic-gate /* for SSH1 the tty modes length is not given */ 17820Sstevel@tonic-gate if (!compat20) 17830Sstevel@tonic-gate n_bytes = packet_remaining(); 17840Sstevel@tonic-gate tty_parse_modes(s->ttyfd, &n_bytes); 17850Sstevel@tonic-gate 17860Sstevel@tonic-gate /* 17870Sstevel@tonic-gate * Add a cleanup function to clear the utmp entry and record logout 17880Sstevel@tonic-gate * time in case we call fatal() (e.g., the connection gets closed). 17890Sstevel@tonic-gate */ 17900Sstevel@tonic-gate fatal_add_cleanup(session_pty_cleanup, (void *)s); 17910Sstevel@tonic-gate if (!use_privsep) 17920Sstevel@tonic-gate pty_setowner(s->pw, s->tty); 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate /* Set window size from the packet. */ 17950Sstevel@tonic-gate pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); 17960Sstevel@tonic-gate 17970Sstevel@tonic-gate packet_check_eom(); 17980Sstevel@tonic-gate session_proctitle(s); 17990Sstevel@tonic-gate return 1; 18000Sstevel@tonic-gate } 18010Sstevel@tonic-gate 18020Sstevel@tonic-gate static int 18030Sstevel@tonic-gate session_subsystem_req(Session *s) 18040Sstevel@tonic-gate { 18050Sstevel@tonic-gate struct stat st; 18060Sstevel@tonic-gate u_int len; 18070Sstevel@tonic-gate int success = 0; 18080Sstevel@tonic-gate char *cmd, *subsys = packet_get_string(&len); 18090Sstevel@tonic-gate int i; 18100Sstevel@tonic-gate 18110Sstevel@tonic-gate packet_check_eom(); 18120Sstevel@tonic-gate log("subsystem request for %.100s", subsys); 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate for (i = 0; i < options.num_subsystems; i++) { 18150Sstevel@tonic-gate if (strcmp(subsys, options.subsystem_name[i]) == 0) { 18160Sstevel@tonic-gate cmd = options.subsystem_command[i]; 18170Sstevel@tonic-gate if (stat(cmd, &st) < 0) { 18180Sstevel@tonic-gate error("subsystem: cannot stat %s: %s", cmd, 18190Sstevel@tonic-gate strerror(errno)); 18200Sstevel@tonic-gate break; 18210Sstevel@tonic-gate } 18220Sstevel@tonic-gate debug("subsystem: exec() %s", cmd); 18230Sstevel@tonic-gate s->is_subsystem = 1; 18240Sstevel@tonic-gate do_exec(s, cmd); 18250Sstevel@tonic-gate success = 1; 18260Sstevel@tonic-gate break; 18270Sstevel@tonic-gate } 18280Sstevel@tonic-gate } 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate if (!success) 18310Sstevel@tonic-gate log("subsystem request for %.100s failed, subsystem not found", 18320Sstevel@tonic-gate subsys); 18330Sstevel@tonic-gate 18340Sstevel@tonic-gate xfree(subsys); 18350Sstevel@tonic-gate return success; 18360Sstevel@tonic-gate } 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate static int 18390Sstevel@tonic-gate session_x11_req(Session *s) 18400Sstevel@tonic-gate { 18410Sstevel@tonic-gate int success; 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate s->single_connection = packet_get_char(); 18440Sstevel@tonic-gate s->auth_proto = packet_get_string(NULL); 18450Sstevel@tonic-gate s->auth_data = packet_get_string(NULL); 18460Sstevel@tonic-gate s->screen = packet_get_int(); 18470Sstevel@tonic-gate packet_check_eom(); 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate success = session_setup_x11fwd(s); 18500Sstevel@tonic-gate if (!success) { 18510Sstevel@tonic-gate xfree(s->auth_proto); 18520Sstevel@tonic-gate xfree(s->auth_data); 18530Sstevel@tonic-gate s->auth_proto = NULL; 18540Sstevel@tonic-gate s->auth_data = NULL; 18550Sstevel@tonic-gate } 18560Sstevel@tonic-gate return success; 18570Sstevel@tonic-gate } 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate static int 18600Sstevel@tonic-gate session_shell_req(Session *s) 18610Sstevel@tonic-gate { 18620Sstevel@tonic-gate packet_check_eom(); 18630Sstevel@tonic-gate do_exec(s, NULL); 18640Sstevel@tonic-gate return 1; 18650Sstevel@tonic-gate } 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate static int 18680Sstevel@tonic-gate session_exec_req(Session *s) 18690Sstevel@tonic-gate { 18700Sstevel@tonic-gate u_int len; 18710Sstevel@tonic-gate char *command = packet_get_string(&len); 18720Sstevel@tonic-gate packet_check_eom(); 18730Sstevel@tonic-gate do_exec(s, command); 18740Sstevel@tonic-gate xfree(command); 18750Sstevel@tonic-gate return 1; 18760Sstevel@tonic-gate } 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate static int 18790Sstevel@tonic-gate session_auth_agent_req(Session *s) 18800Sstevel@tonic-gate { 18810Sstevel@tonic-gate static int called = 0; 18820Sstevel@tonic-gate packet_check_eom(); 18830Sstevel@tonic-gate if (no_agent_forwarding_flag) { 18840Sstevel@tonic-gate debug("session_auth_agent_req: no_agent_forwarding_flag"); 18850Sstevel@tonic-gate return 0; 18860Sstevel@tonic-gate } 18870Sstevel@tonic-gate if (called) { 18880Sstevel@tonic-gate return 0; 18890Sstevel@tonic-gate } else { 18900Sstevel@tonic-gate called = 1; 18910Sstevel@tonic-gate return auth_input_request_forwarding(s->pw); 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate } 18940Sstevel@tonic-gate 18950Sstevel@tonic-gate static int 18960Sstevel@tonic-gate session_loc_env_check(char *var, char *val) 18970Sstevel@tonic-gate { 18980Sstevel@tonic-gate char *current; 18990Sstevel@tonic-gate int cat, ret; 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate if (strcmp(var, "LANG") == 0) 19020Sstevel@tonic-gate cat = LC_ALL; 19030Sstevel@tonic-gate else if (strcmp(var, "LC_ALL") == 0) 19040Sstevel@tonic-gate cat = LC_ALL; 19050Sstevel@tonic-gate else if (strcmp(var, "LC_CTYPE") == 0) 19060Sstevel@tonic-gate cat = LC_CTYPE; 19070Sstevel@tonic-gate else if (strcmp(var, "LC_COLLATE") == 0) 19080Sstevel@tonic-gate cat = LC_COLLATE; 19090Sstevel@tonic-gate else if (strcmp(var, "LC_TIME") == 0) 19100Sstevel@tonic-gate cat = LC_TIME; 19110Sstevel@tonic-gate else if (strcmp(var, "LC_NUMERIC") == 0) 19120Sstevel@tonic-gate cat = LC_NUMERIC; 19130Sstevel@tonic-gate else if (strcmp(var, "LC_MONETARY") == 0) 19140Sstevel@tonic-gate cat = LC_MONETARY; 19150Sstevel@tonic-gate else if (strcmp(var, "LC_MESSAGES") == 0) 19160Sstevel@tonic-gate cat = LC_MESSAGES; 19170Sstevel@tonic-gate 19180Sstevel@tonic-gate if ((current = setlocale(cat, NULL)) != NULL) 19190Sstevel@tonic-gate current = xstrdup(current); 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate ret = (setlocale(cat, val) != NULL); 19220Sstevel@tonic-gate (void) setlocale(cat, current); 19230Sstevel@tonic-gate return (ret); 19240Sstevel@tonic-gate } 19250Sstevel@tonic-gate 19260Sstevel@tonic-gate static int 19270Sstevel@tonic-gate session_env_req(Session *s) 19280Sstevel@tonic-gate { 19290Sstevel@tonic-gate Channel *c; 19300Sstevel@tonic-gate char *var, *val, *e; 19310Sstevel@tonic-gate char **p; 19320Sstevel@tonic-gate size_t len; 19330Sstevel@tonic-gate int ret = 0; 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate /* Get var/val from the rest of this packet */ 19360Sstevel@tonic-gate var = packet_get_string(NULL); 19370Sstevel@tonic-gate val = packet_get_string(NULL); 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate /* 19400Sstevel@tonic-gate * We'll need the channel ID for the packet_send_debug messages, 19410Sstevel@tonic-gate * so get it now. 19420Sstevel@tonic-gate */ 19430Sstevel@tonic-gate if ((c = channel_lookup(s->chanid)) == NULL) 19440Sstevel@tonic-gate goto done; /* shouldn't happen! */ 19450Sstevel@tonic-gate 19460Sstevel@tonic-gate debug2("Received request for environment variable %s=%s", var, val); 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate /* For now allow only LANG and LC_* */ 19490Sstevel@tonic-gate if (strcmp(var, "LANG") != 0 && strncmp(var, "LC_", 3) != 0) { 19500Sstevel@tonic-gate debug2("Rejecting request for environment variable %s", var); 19510Sstevel@tonic-gate goto done; 19520Sstevel@tonic-gate } 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate if (!session_loc_env_check(var, val)) { 19550Sstevel@tonic-gate packet_send_debug(gettext("Missing locale support for %s=%s"), 19560Sstevel@tonic-gate var, val); 19570Sstevel@tonic-gate goto done; 19580Sstevel@tonic-gate } 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate packet_send_debug(gettext("Channel %d set: %s=%s"), c->remote_id, 19610Sstevel@tonic-gate var, val); 19620Sstevel@tonic-gate 19630Sstevel@tonic-gate /* 19640Sstevel@tonic-gate * Always append new environment variables without regard to old 19650Sstevel@tonic-gate * ones being overriden. The way these are actually added to 19660Sstevel@tonic-gate * the environment of the session process later settings 19670Sstevel@tonic-gate * override earlier ones; see copy_environment(). 19680Sstevel@tonic-gate */ 19690Sstevel@tonic-gate if (s->env == NULL) { 19700Sstevel@tonic-gate char **env; 19710Sstevel@tonic-gate 19720Sstevel@tonic-gate env = xmalloc(sizeof (char **) * 2); 19730Sstevel@tonic-gate memset(env, 0, sizeof (char **) * 2); 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate s->env = env; 19760Sstevel@tonic-gate p = env; 19770Sstevel@tonic-gate } else { 19780Sstevel@tonic-gate for (p = s->env; *p != NULL ; p++); 19790Sstevel@tonic-gate 19800Sstevel@tonic-gate s->env = xrealloc(s->env, (p - s->env + 2) * sizeof (char **)); 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate for (p = s->env; *p != NULL ; p++); 19830Sstevel@tonic-gate } 19840Sstevel@tonic-gate 19850Sstevel@tonic-gate len = snprintf(NULL, 0, "%s=%s", var, val); 19860Sstevel@tonic-gate e = xmalloc(len + 1); 19870Sstevel@tonic-gate (void) snprintf(e, len + 1, "%s=%s", var, val); 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate (*p++) = e; 19900Sstevel@tonic-gate *p = NULL; 19910Sstevel@tonic-gate 19920Sstevel@tonic-gate ret = 1; 19930Sstevel@tonic-gate 19940Sstevel@tonic-gate done: 19950Sstevel@tonic-gate xfree(var); 19960Sstevel@tonic-gate xfree(val); 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate return (ret); 19990Sstevel@tonic-gate } 20000Sstevel@tonic-gate 20010Sstevel@tonic-gate static void 20020Sstevel@tonic-gate session_free_env(char ***envp) 20030Sstevel@tonic-gate { 20040Sstevel@tonic-gate char **env, **p; 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate if (envp == NULL || *envp == NULL) 20070Sstevel@tonic-gate return; 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate env = *envp; 20100Sstevel@tonic-gate 20110Sstevel@tonic-gate *envp = NULL; 20120Sstevel@tonic-gate 20130Sstevel@tonic-gate for (p = env; *p != NULL; p++) 20140Sstevel@tonic-gate xfree(*p); 20150Sstevel@tonic-gate 20160Sstevel@tonic-gate xfree(env); 20170Sstevel@tonic-gate } 20180Sstevel@tonic-gate 20190Sstevel@tonic-gate int 20200Sstevel@tonic-gate session_input_channel_req(Channel *c, const char *rtype) 20210Sstevel@tonic-gate { 20220Sstevel@tonic-gate int success = 0; 20230Sstevel@tonic-gate Session *s; 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate if ((s = session_by_channel(c->self)) == NULL) { 20260Sstevel@tonic-gate log("session_input_channel_req: no session %d req %.100s", 20270Sstevel@tonic-gate c->self, rtype); 20280Sstevel@tonic-gate return 0; 20290Sstevel@tonic-gate } 20300Sstevel@tonic-gate debug("session_input_channel_req: session %d req %s", s->self, rtype); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* 20330Sstevel@tonic-gate * a session is in LARVAL state until a shell, a command 20340Sstevel@tonic-gate * or a subsystem is executed 20350Sstevel@tonic-gate */ 20360Sstevel@tonic-gate if (c->type == SSH_CHANNEL_LARVAL) { 20370Sstevel@tonic-gate if (strcmp(rtype, "shell") == 0) { 20380Sstevel@tonic-gate success = session_shell_req(s); 20390Sstevel@tonic-gate } else if (strcmp(rtype, "exec") == 0) { 20400Sstevel@tonic-gate success = session_exec_req(s); 20410Sstevel@tonic-gate } else if (strcmp(rtype, "pty-req") == 0) { 20420Sstevel@tonic-gate success = session_pty_req(s); 20430Sstevel@tonic-gate } else if (strcmp(rtype, "x11-req") == 0) { 20440Sstevel@tonic-gate success = session_x11_req(s); 20450Sstevel@tonic-gate } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { 20460Sstevel@tonic-gate success = session_auth_agent_req(s); 20470Sstevel@tonic-gate } else if (strcmp(rtype, "subsystem") == 0) { 20480Sstevel@tonic-gate success = session_subsystem_req(s); 20490Sstevel@tonic-gate } else if (strcmp(rtype, "env") == 0) { 20500Sstevel@tonic-gate success = session_env_req(s); 20510Sstevel@tonic-gate } 20520Sstevel@tonic-gate } 20530Sstevel@tonic-gate if (strcmp(rtype, "window-change") == 0) { 20540Sstevel@tonic-gate success = session_window_change_req(s); 20550Sstevel@tonic-gate } 20560Sstevel@tonic-gate return success; 20570Sstevel@tonic-gate } 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate void 20600Sstevel@tonic-gate session_set_fds(Session *s, int fdin, int fdout, int fderr) 20610Sstevel@tonic-gate { 20620Sstevel@tonic-gate if (!compat20) 20630Sstevel@tonic-gate fatal("session_set_fds: called for proto != 2.0"); 20640Sstevel@tonic-gate /* 20650Sstevel@tonic-gate * now that have a child and a pipe to the child, 20660Sstevel@tonic-gate * we can activate our channel and register the fd's 20670Sstevel@tonic-gate */ 20680Sstevel@tonic-gate if (s->chanid == -1) 20690Sstevel@tonic-gate fatal("no channel for session %d", s->self); 20700Sstevel@tonic-gate channel_set_fds(s->chanid, 20710Sstevel@tonic-gate fdout, fdin, fderr, 20720Sstevel@tonic-gate fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, 20730Sstevel@tonic-gate 1, 20740Sstevel@tonic-gate CHAN_SES_WINDOW_DEFAULT); 20750Sstevel@tonic-gate } 20760Sstevel@tonic-gate 20770Sstevel@tonic-gate /* 20780Sstevel@tonic-gate * Function to perform pty cleanup. Also called if we get aborted abnormally 20790Sstevel@tonic-gate * (e.g., due to a dropped connection). 20800Sstevel@tonic-gate */ 20810Sstevel@tonic-gate void 20820Sstevel@tonic-gate session_pty_cleanup2(void *session) 20830Sstevel@tonic-gate { 20840Sstevel@tonic-gate Session *s = session; 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate if (s == NULL) { 20870Sstevel@tonic-gate error("session_pty_cleanup: no session"); 20880Sstevel@tonic-gate return; 20890Sstevel@tonic-gate } 20900Sstevel@tonic-gate if (s->ttyfd == -1) 20910Sstevel@tonic-gate return; 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate debug("session_pty_cleanup: session %d release %s", s->self, s->tty); 20940Sstevel@tonic-gate 20950Sstevel@tonic-gate #ifdef USE_PAM 20960Sstevel@tonic-gate session_do_pam(s, 0); 20970Sstevel@tonic-gate #endif /* USE_PAM */ 20980Sstevel@tonic-gate 20990Sstevel@tonic-gate /* Record that the user has logged out. */ 21000Sstevel@tonic-gate if (s->pid != 0) { 21010Sstevel@tonic-gate debug3("Recording SSHv2 channel login in utmpx/wtmpx"); 21020Sstevel@tonic-gate #ifdef ALTPRIVSEP 21030Sstevel@tonic-gate altprivsep_record_logout(s->pid); 21040Sstevel@tonic-gate #else /* ALTPRIVSEP */ 21050Sstevel@tonic-gate record_logout(s->pid, s->tty, NULL, s->pw->pw_name); 21060Sstevel@tonic-gate #endif /* ALTPRIVSEP */ 21070Sstevel@tonic-gate } 21080Sstevel@tonic-gate 21090Sstevel@tonic-gate /* Release the pseudo-tty. */ 21100Sstevel@tonic-gate if (getuid() == 0) 21110Sstevel@tonic-gate pty_release(s->tty); 21120Sstevel@tonic-gate 21130Sstevel@tonic-gate /* 21140Sstevel@tonic-gate * Close the server side of the socket pairs. We must do this after 21150Sstevel@tonic-gate * the pty cleanup, so that another process doesn't get this pty 21160Sstevel@tonic-gate * while we're still cleaning up. 21170Sstevel@tonic-gate */ 21180Sstevel@tonic-gate if (close(s->ptymaster) < 0) 21190Sstevel@tonic-gate error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno)); 21200Sstevel@tonic-gate 21210Sstevel@tonic-gate /* unlink pty from session */ 21220Sstevel@tonic-gate s->ttyfd = -1; 21230Sstevel@tonic-gate } 21240Sstevel@tonic-gate 21250Sstevel@tonic-gate void 21260Sstevel@tonic-gate session_pty_cleanup(void *session) 21270Sstevel@tonic-gate { 21280Sstevel@tonic-gate PRIVSEP(session_pty_cleanup2(session)); 21290Sstevel@tonic-gate } 21300Sstevel@tonic-gate 21310Sstevel@tonic-gate static char * 21320Sstevel@tonic-gate sig2name(int sig) 21330Sstevel@tonic-gate { 21340Sstevel@tonic-gate #define SSH_SIG(x) if (sig == SIG ## x) return #x 21350Sstevel@tonic-gate SSH_SIG(ABRT); 21360Sstevel@tonic-gate SSH_SIG(ALRM); 21370Sstevel@tonic-gate SSH_SIG(FPE); 21380Sstevel@tonic-gate SSH_SIG(HUP); 21390Sstevel@tonic-gate SSH_SIG(ILL); 21400Sstevel@tonic-gate SSH_SIG(INT); 21410Sstevel@tonic-gate SSH_SIG(KILL); 21420Sstevel@tonic-gate SSH_SIG(PIPE); 21430Sstevel@tonic-gate SSH_SIG(QUIT); 21440Sstevel@tonic-gate SSH_SIG(SEGV); 21450Sstevel@tonic-gate SSH_SIG(TERM); 21460Sstevel@tonic-gate SSH_SIG(USR1); 21470Sstevel@tonic-gate SSH_SIG(USR2); 21480Sstevel@tonic-gate #undef SSH_SIG 21490Sstevel@tonic-gate return "SIG@openssh.com"; 21500Sstevel@tonic-gate } 21510Sstevel@tonic-gate 21520Sstevel@tonic-gate static void 21530Sstevel@tonic-gate session_exit_message(Session *s, int status) 21540Sstevel@tonic-gate { 21550Sstevel@tonic-gate Channel *c; 21560Sstevel@tonic-gate 21570Sstevel@tonic-gate if ((c = channel_lookup(s->chanid)) == NULL) 21580Sstevel@tonic-gate fatal("session_exit_message: session %d: no channel %d", 21590Sstevel@tonic-gate s->self, s->chanid); 21600Sstevel@tonic-gate debug("session_exit_message: session %d channel %d pid %ld", 21610Sstevel@tonic-gate s->self, s->chanid, (long)s->pid); 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate if (WIFEXITED(status)) { 21640Sstevel@tonic-gate channel_request_start(s->chanid, "exit-status", 0); 21650Sstevel@tonic-gate packet_put_int(WEXITSTATUS(status)); 21660Sstevel@tonic-gate packet_send(); 21670Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 21680Sstevel@tonic-gate channel_request_start(s->chanid, "exit-signal", 0); 21690Sstevel@tonic-gate packet_put_cstring(sig2name(WTERMSIG(status))); 21700Sstevel@tonic-gate #ifdef WCOREDUMP 21710Sstevel@tonic-gate packet_put_char(WCOREDUMP(status)); 21720Sstevel@tonic-gate #else /* WCOREDUMP */ 21730Sstevel@tonic-gate packet_put_char(0); 21740Sstevel@tonic-gate #endif /* WCOREDUMP */ 21750Sstevel@tonic-gate packet_put_cstring(""); 21760Sstevel@tonic-gate packet_put_cstring(""); 21770Sstevel@tonic-gate packet_send(); 21780Sstevel@tonic-gate } else { 21790Sstevel@tonic-gate /* Some weird exit cause. Just exit. */ 21800Sstevel@tonic-gate packet_disconnect("wait returned status %04x.", status); 21810Sstevel@tonic-gate } 21820Sstevel@tonic-gate 21830Sstevel@tonic-gate /* Ok to close channel now */ 21840Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 0); 21850Sstevel@tonic-gate 21860Sstevel@tonic-gate /* disconnect channel */ 21870Sstevel@tonic-gate debug("session_exit_message: release channel %d", s->chanid); 21880Sstevel@tonic-gate channel_cancel_cleanup(s->chanid); 21890Sstevel@tonic-gate /* 21900Sstevel@tonic-gate * emulate a write failure with 'chan_write_failed', nobody will be 21910Sstevel@tonic-gate * interested in data we write. 21920Sstevel@tonic-gate * Note that we must not call 'chan_read_failed', since there could 21930Sstevel@tonic-gate * be some more data waiting in the pipe. 21940Sstevel@tonic-gate */ 21950Sstevel@tonic-gate if (c->ostate != CHAN_OUTPUT_CLOSED) 21960Sstevel@tonic-gate chan_write_failed(c); 21970Sstevel@tonic-gate s->chanid = -1; 21980Sstevel@tonic-gate } 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate void 22010Sstevel@tonic-gate session_close(Session *s) 22020Sstevel@tonic-gate { 22030Sstevel@tonic-gate debug("session_close: session %d pid %ld", s->self, (long)s->pid); 22040Sstevel@tonic-gate if (s->ttyfd != -1) { 22050Sstevel@tonic-gate fatal_remove_cleanup(session_pty_cleanup, (void *)s); 22060Sstevel@tonic-gate session_pty_cleanup(s); 22070Sstevel@tonic-gate } 22080Sstevel@tonic-gate if (s->term) 22090Sstevel@tonic-gate xfree(s->term); 22100Sstevel@tonic-gate if (s->display) 22110Sstevel@tonic-gate xfree(s->display); 22120Sstevel@tonic-gate if (s->auth_display) 22130Sstevel@tonic-gate xfree(s->auth_display); 22140Sstevel@tonic-gate if (s->auth_data) 22150Sstevel@tonic-gate xfree(s->auth_data); 22160Sstevel@tonic-gate if (s->auth_proto) 22170Sstevel@tonic-gate xfree(s->auth_proto); 22180Sstevel@tonic-gate if (s->command) 22190Sstevel@tonic-gate xfree(s->command); 22200Sstevel@tonic-gate session_free_env(&s->env); 22210Sstevel@tonic-gate s->used = 0; 22220Sstevel@tonic-gate session_proctitle(s); 22230Sstevel@tonic-gate } 22240Sstevel@tonic-gate 22250Sstevel@tonic-gate void 22260Sstevel@tonic-gate session_close_by_pid(pid_t pid, int status) 22270Sstevel@tonic-gate { 22280Sstevel@tonic-gate Session *s = session_by_pid(pid); 22290Sstevel@tonic-gate if (s == NULL) { 22300Sstevel@tonic-gate debug("session_close_by_pid: no session for pid %ld", 22310Sstevel@tonic-gate (long)pid); 22320Sstevel@tonic-gate return; 22330Sstevel@tonic-gate } 22340Sstevel@tonic-gate if (s->chanid != -1) 22350Sstevel@tonic-gate session_exit_message(s, status); 22360Sstevel@tonic-gate session_close(s); 22370Sstevel@tonic-gate } 22380Sstevel@tonic-gate 22390Sstevel@tonic-gate /* 22400Sstevel@tonic-gate * this is called when a channel dies before 22410Sstevel@tonic-gate * the session 'child' itself dies 22420Sstevel@tonic-gate */ 22430Sstevel@tonic-gate void 22440Sstevel@tonic-gate session_close_by_channel(int id, void *arg) 22450Sstevel@tonic-gate { 22460Sstevel@tonic-gate Session *s = session_by_channel(id); 22470Sstevel@tonic-gate if (s == NULL) { 22480Sstevel@tonic-gate debug("session_close_by_channel: no session for id %d", id); 22490Sstevel@tonic-gate return; 22500Sstevel@tonic-gate } 22510Sstevel@tonic-gate debug("session_close_by_channel: channel %d child %ld", 22520Sstevel@tonic-gate id, (long)s->pid); 22530Sstevel@tonic-gate if (s->pid != 0) { 22540Sstevel@tonic-gate debug("session_close_by_channel: channel %d: has child", id); 22550Sstevel@tonic-gate /* 22560Sstevel@tonic-gate * delay detach of session, but release pty, since 22570Sstevel@tonic-gate * the fd's to the child are already closed 22580Sstevel@tonic-gate */ 22590Sstevel@tonic-gate if (s->ttyfd != -1) { 22600Sstevel@tonic-gate fatal_remove_cleanup(session_pty_cleanup, (void *)s); 22610Sstevel@tonic-gate session_pty_cleanup(s); 22620Sstevel@tonic-gate } 22630Sstevel@tonic-gate return; 22640Sstevel@tonic-gate } 22650Sstevel@tonic-gate /* detach by removing callback */ 22660Sstevel@tonic-gate channel_cancel_cleanup(s->chanid); 22670Sstevel@tonic-gate s->chanid = -1; 22680Sstevel@tonic-gate session_close(s); 22690Sstevel@tonic-gate } 22700Sstevel@tonic-gate 22710Sstevel@tonic-gate void 22720Sstevel@tonic-gate session_destroy_all(void (*closefunc)(Session *)) 22730Sstevel@tonic-gate { 22740Sstevel@tonic-gate int i; 22750Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 22760Sstevel@tonic-gate Session *s = &sessions[i]; 22770Sstevel@tonic-gate if (s->used) { 22780Sstevel@tonic-gate if (closefunc != NULL) 22790Sstevel@tonic-gate closefunc(s); 22800Sstevel@tonic-gate else 22810Sstevel@tonic-gate session_close(s); 22820Sstevel@tonic-gate } 22830Sstevel@tonic-gate } 22840Sstevel@tonic-gate } 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate static char * 22870Sstevel@tonic-gate session_tty_list(void) 22880Sstevel@tonic-gate { 22890Sstevel@tonic-gate static char buf[1024]; 22900Sstevel@tonic-gate int i; 22910Sstevel@tonic-gate buf[0] = '\0'; 22920Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) { 22930Sstevel@tonic-gate Session *s = &sessions[i]; 22940Sstevel@tonic-gate if (s->used && s->ttyfd != -1) { 22950Sstevel@tonic-gate if (buf[0] != '\0') 22960Sstevel@tonic-gate strlcat(buf, ",", sizeof buf); 22970Sstevel@tonic-gate strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf); 22980Sstevel@tonic-gate } 22990Sstevel@tonic-gate } 23000Sstevel@tonic-gate if (buf[0] == '\0') 23010Sstevel@tonic-gate strlcpy(buf, "notty", sizeof buf); 23020Sstevel@tonic-gate return buf; 23030Sstevel@tonic-gate } 23040Sstevel@tonic-gate 23050Sstevel@tonic-gate void 23060Sstevel@tonic-gate session_proctitle(Session *s) 23070Sstevel@tonic-gate { 23080Sstevel@tonic-gate if (s->pw == NULL) 23090Sstevel@tonic-gate error("no user for session %d", s->self); 23100Sstevel@tonic-gate else 23110Sstevel@tonic-gate setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate 23140Sstevel@tonic-gate int 23150Sstevel@tonic-gate session_setup_x11fwd(Session *s) 23160Sstevel@tonic-gate { 23170Sstevel@tonic-gate struct stat st; 23180Sstevel@tonic-gate char display[512], auth_display[512]; 23190Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN]; 23200Sstevel@tonic-gate 23210Sstevel@tonic-gate if (no_x11_forwarding_flag) { 23220Sstevel@tonic-gate packet_send_debug("X11 forwarding disabled in user configuration file."); 23230Sstevel@tonic-gate return 0; 23240Sstevel@tonic-gate } 23250Sstevel@tonic-gate if (!options.x11_forwarding) { 23260Sstevel@tonic-gate debug("X11 forwarding disabled in server configuration file."); 23270Sstevel@tonic-gate return 0; 23280Sstevel@tonic-gate } 23290Sstevel@tonic-gate if (!options.xauth_location || 23300Sstevel@tonic-gate (stat(options.xauth_location, &st) == -1)) { 23310Sstevel@tonic-gate packet_send_debug("No xauth program; cannot forward with spoofing."); 23320Sstevel@tonic-gate return 0; 23330Sstevel@tonic-gate } 23340Sstevel@tonic-gate if (options.use_login) { 23350Sstevel@tonic-gate packet_send_debug("X11 forwarding disabled; " 23360Sstevel@tonic-gate "not compatible with UseLogin=yes."); 23370Sstevel@tonic-gate return 0; 23380Sstevel@tonic-gate } 23390Sstevel@tonic-gate if (s->display != NULL) { 23400Sstevel@tonic-gate debug("X11 display already set."); 23410Sstevel@tonic-gate return 0; 23420Sstevel@tonic-gate } 23430Sstevel@tonic-gate if (x11_create_display_inet(options.x11_display_offset, 23440Sstevel@tonic-gate options.x11_use_localhost, s->single_connection, 23450Sstevel@tonic-gate &s->display_number) == -1) { 23460Sstevel@tonic-gate debug("x11_create_display_inet failed."); 23470Sstevel@tonic-gate return 0; 23480Sstevel@tonic-gate } 23490Sstevel@tonic-gate 23500Sstevel@tonic-gate /* Set up a suitable value for the DISPLAY variable. */ 23510Sstevel@tonic-gate if (gethostname(hostname, sizeof(hostname)) < 0) 23520Sstevel@tonic-gate fatal("gethostname: %.100s", strerror(errno)); 23530Sstevel@tonic-gate /* 23540Sstevel@tonic-gate * auth_display must be used as the displayname when the 23550Sstevel@tonic-gate * authorization entry is added with xauth(1). This will be 23560Sstevel@tonic-gate * different than the DISPLAY string for localhost displays. 23570Sstevel@tonic-gate */ 23580Sstevel@tonic-gate if (options.x11_use_localhost) { 23590Sstevel@tonic-gate snprintf(display, sizeof display, "localhost:%u.%u", 23600Sstevel@tonic-gate s->display_number, s->screen); 23610Sstevel@tonic-gate snprintf(auth_display, sizeof auth_display, "unix:%u.%u", 23620Sstevel@tonic-gate s->display_number, s->screen); 23630Sstevel@tonic-gate s->display = xstrdup(display); 23640Sstevel@tonic-gate s->auth_display = xstrdup(auth_display); 23650Sstevel@tonic-gate } else { 23660Sstevel@tonic-gate #ifdef IPADDR_IN_DISPLAY 23670Sstevel@tonic-gate struct hostent *he; 23680Sstevel@tonic-gate struct in_addr my_addr; 23690Sstevel@tonic-gate 23700Sstevel@tonic-gate he = gethostbyname(hostname); 23710Sstevel@tonic-gate if (he == NULL) { 23720Sstevel@tonic-gate error("Can't get IP address for X11 DISPLAY."); 23730Sstevel@tonic-gate packet_send_debug("Can't get IP address for X11 DISPLAY."); 23740Sstevel@tonic-gate return 0; 23750Sstevel@tonic-gate } 23760Sstevel@tonic-gate memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); 23770Sstevel@tonic-gate snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr), 23780Sstevel@tonic-gate s->display_number, s->screen); 23790Sstevel@tonic-gate #else 23800Sstevel@tonic-gate snprintf(display, sizeof display, "%.400s:%u.%u", hostname, 23810Sstevel@tonic-gate s->display_number, s->screen); 23820Sstevel@tonic-gate #endif 23830Sstevel@tonic-gate s->display = xstrdup(display); 23840Sstevel@tonic-gate s->auth_display = xstrdup(display); 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate return 1; 23880Sstevel@tonic-gate } 23890Sstevel@tonic-gate 23900Sstevel@tonic-gate #ifdef USE_PAM 23910Sstevel@tonic-gate int session_do_pam_conv(int, struct pam_message **, 23920Sstevel@tonic-gate struct pam_response **, void *); 23930Sstevel@tonic-gate 23940Sstevel@tonic-gate static struct pam_conv session_pam_conv = { 23950Sstevel@tonic-gate session_do_pam_conv, 23960Sstevel@tonic-gate NULL 23970Sstevel@tonic-gate }; 23980Sstevel@tonic-gate 23990Sstevel@tonic-gate static void 24000Sstevel@tonic-gate session_do_pam(Session *s, int do_open) 24010Sstevel@tonic-gate { 24020Sstevel@tonic-gate int pam_retval; 24030Sstevel@tonic-gate char *where, *old_tty, *old_tty_copy = NULL; 24040Sstevel@tonic-gate struct pam_conv old_conv, *old_conv_ptr; 24050Sstevel@tonic-gate 24060Sstevel@tonic-gate if (!s || !s->authctxt || !s->authctxt->pam || !s->authctxt->pam->h) 24070Sstevel@tonic-gate return; 24080Sstevel@tonic-gate 24090Sstevel@tonic-gate /* Save current PAM item values */ 24100Sstevel@tonic-gate where = "getting PAM_CONV"; 24110Sstevel@tonic-gate pam_retval = pam_get_item(s->authctxt->pam->h, PAM_CONV, 24120Sstevel@tonic-gate (void **) &old_conv_ptr); 24130Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 24140Sstevel@tonic-gate goto done; 24150Sstevel@tonic-gate old_conv = *old_conv_ptr; 24160Sstevel@tonic-gate 24170Sstevel@tonic-gate where = "getting PAM_TTY"; 24180Sstevel@tonic-gate pam_retval = pam_get_item(s->authctxt->pam->h, PAM_TTY, 24190Sstevel@tonic-gate (void **) &old_tty); 24200Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 24210Sstevel@tonic-gate goto done; 24220Sstevel@tonic-gate old_tty_copy = xstrdup(old_tty); 24230Sstevel@tonic-gate 24240Sstevel@tonic-gate /* Change PAM_TTY and PAM_CONV items */ 24250Sstevel@tonic-gate where = "setting PAM_TTY"; 24260Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, s->tty); 24270Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 24280Sstevel@tonic-gate goto done; 24290Sstevel@tonic-gate 24300Sstevel@tonic-gate where = "setting PAM_CONV"; 24310Sstevel@tonic-gate session_pam_conv.appdata_ptr = s; 24320Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, 24330Sstevel@tonic-gate PAM_CONV, &session_pam_conv); 24340Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 24350Sstevel@tonic-gate goto done; 24360Sstevel@tonic-gate 24370Sstevel@tonic-gate /* Call pam_open/close_session() */ 24380Sstevel@tonic-gate if (do_open) { 24390Sstevel@tonic-gate where = "calling pam_open_session()"; 24400Sstevel@tonic-gate pam_retval = pam_open_session(s->authctxt->pam->h, 0); 24410Sstevel@tonic-gate } 24420Sstevel@tonic-gate else { 24430Sstevel@tonic-gate where = "calling pam_close_session()"; 24440Sstevel@tonic-gate pam_retval = pam_close_session(s->authctxt->pam->h, 0); 24450Sstevel@tonic-gate } 24460Sstevel@tonic-gate 24470Sstevel@tonic-gate /* Reset PAM_TTY and PAM_CONV items to previous values */ 24480Sstevel@tonic-gate where = "setting PAM_TTY"; 24490Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, old_tty_copy); 24500Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 24510Sstevel@tonic-gate goto done; 24520Sstevel@tonic-gate 24530Sstevel@tonic-gate where = "setting PAM_CONV"; 24540Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_CONV, &old_conv); 24550Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 24560Sstevel@tonic-gate goto done; 24570Sstevel@tonic-gate 24580Sstevel@tonic-gate session_pam_conv.appdata_ptr = NULL; 24590Sstevel@tonic-gate 24600Sstevel@tonic-gate done: 24610Sstevel@tonic-gate if (old_tty_copy) 24620Sstevel@tonic-gate xfree(old_tty_copy); 24630Sstevel@tonic-gate 24640Sstevel@tonic-gate if (pam_retval == PAM_SUCCESS) 24650Sstevel@tonic-gate return; 24660Sstevel@tonic-gate 24670Sstevel@tonic-gate /* fatal()? probably not... */ 24680Sstevel@tonic-gate log("PAM failed[%d] while %s: %s", pam_retval, where, 24690Sstevel@tonic-gate PAM_STRERROR(s->authctxt->pam->h, pam_retval)); 24700Sstevel@tonic-gate } 24710Sstevel@tonic-gate 24720Sstevel@tonic-gate int 24730Sstevel@tonic-gate session_do_pam_conv(int num_prompts, 24740Sstevel@tonic-gate struct pam_message **prompts, 24750Sstevel@tonic-gate struct pam_response **resp, 24760Sstevel@tonic-gate void *app_data) 24770Sstevel@tonic-gate { 24780Sstevel@tonic-gate Session *s = (Session *) app_data; 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate struct pam_response *reply; 24810Sstevel@tonic-gate int count; 24820Sstevel@tonic-gate char *prompt; 24830Sstevel@tonic-gate 24840Sstevel@tonic-gate if (channel_lookup(s->chanid) == NULL) 24850Sstevel@tonic-gate return PAM_CONV_ERR; 24860Sstevel@tonic-gate 24870Sstevel@tonic-gate /* PAM will free this later */ 24880Sstevel@tonic-gate reply = xmalloc(num_prompts * sizeof(*reply)); 24890Sstevel@tonic-gate 24900Sstevel@tonic-gate (void) memset(reply, 0, num_prompts * sizeof(*reply)); 24910Sstevel@tonic-gate for (count = 0; count < num_prompts; count++) { 24920Sstevel@tonic-gate switch(PAM_MSG_MEMBER(prompts, count, msg_style)) { 24930Sstevel@tonic-gate case PAM_TEXT_INFO: 24940Sstevel@tonic-gate /* Write to stdout of channel */ 24950Sstevel@tonic-gate prompt = PAM_MSG_MEMBER(prompts, count, msg); 24960Sstevel@tonic-gate if (prompt != NULL && s->ttyfd != -1) { 24970Sstevel@tonic-gate debug2("session_do_pam_conv: text info " 24980Sstevel@tonic-gate "prompt: %s", prompt); 24990Sstevel@tonic-gate (void) write(s->ttyfd, prompt, strlen(prompt)); 25000Sstevel@tonic-gate (void) write(s->ttyfd, "\n", 1); 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate reply[count].resp = xstrdup(""); 25030Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS; 25040Sstevel@tonic-gate break; 25050Sstevel@tonic-gate case PAM_ERROR_MSG: 25060Sstevel@tonic-gate /* Write to stderr of channel */ 25070Sstevel@tonic-gate prompt = PAM_MSG_MEMBER(prompts, count, msg); 25080Sstevel@tonic-gate if (prompt != NULL && s->ttyfd != -1) { 25090Sstevel@tonic-gate debug2("session_do_pam_conv: error " 25100Sstevel@tonic-gate "prompt: %s", prompt); 25110Sstevel@tonic-gate (void) write(s->ttyfd, prompt, strlen(prompt)); 25120Sstevel@tonic-gate (void) write(s->ttyfd, "\n", 1); 25130Sstevel@tonic-gate } 25140Sstevel@tonic-gate reply[count].resp = xstrdup(""); 25150Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS; 25160Sstevel@tonic-gate break; 25170Sstevel@tonic-gate case PAM_PROMPT_ECHO_ON: 25180Sstevel@tonic-gate case PAM_PROMPT_ECHO_OFF: 25190Sstevel@tonic-gate /* 25200Sstevel@tonic-gate * XXX Someday add support for echo on/off prompts 25210Sstevel@tonic-gate * here on sessions with ttys. 25220Sstevel@tonic-gate */ 25230Sstevel@tonic-gate default: 25240Sstevel@tonic-gate xfree(reply); 25250Sstevel@tonic-gate return PAM_CONV_ERR; 25260Sstevel@tonic-gate } 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate 25290Sstevel@tonic-gate *resp = reply; 25300Sstevel@tonic-gate 25310Sstevel@tonic-gate return PAM_SUCCESS; 25320Sstevel@tonic-gate } 25330Sstevel@tonic-gate #endif /* USE_PAM */ 25340Sstevel@tonic-gate 25350Sstevel@tonic-gate static void 25360Sstevel@tonic-gate do_authenticated2(Authctxt *authctxt) 25370Sstevel@tonic-gate { 25380Sstevel@tonic-gate server_loop2(authctxt); 25390Sstevel@tonic-gate } 2540