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*11802SJan.Pechanec@Sun.COM * Copyright 2010 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 #ifdef HAVE_DEFOPEN
430Sstevel@tonic-gate #include <deflt.h>
440Sstevel@tonic-gate #include <ulimit.h>
450Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
460Sstevel@tonic-gate
473109Sjp161948 #ifdef HAVE_LIBGEN_H
483109Sjp161948 #include <libgen.h>
493109Sjp161948 #endif
503109Sjp161948
519139SJan.Pechanec@Sun.COM #include <priv.h>
529139SJan.Pechanec@Sun.COM
530Sstevel@tonic-gate #include "ssh.h"
540Sstevel@tonic-gate #include "ssh1.h"
550Sstevel@tonic-gate #include "ssh2.h"
560Sstevel@tonic-gate #include "xmalloc.h"
570Sstevel@tonic-gate #include "sshpty.h"
580Sstevel@tonic-gate #include "packet.h"
590Sstevel@tonic-gate #include "buffer.h"
600Sstevel@tonic-gate #include "mpaux.h"
610Sstevel@tonic-gate #include "uidswap.h"
620Sstevel@tonic-gate #include "compat.h"
630Sstevel@tonic-gate #include "channels.h"
640Sstevel@tonic-gate #include "bufaux.h"
650Sstevel@tonic-gate #include "auth.h"
660Sstevel@tonic-gate #include "auth-options.h"
670Sstevel@tonic-gate #include "pathnames.h"
680Sstevel@tonic-gate #include "log.h"
690Sstevel@tonic-gate #include "servconf.h"
700Sstevel@tonic-gate #include "sshlogin.h"
710Sstevel@tonic-gate #include "serverloop.h"
720Sstevel@tonic-gate #include "canohost.h"
730Sstevel@tonic-gate #include "session.h"
749139SJan.Pechanec@Sun.COM #include "tildexpand.h"
759139SJan.Pechanec@Sun.COM #include "misc.h"
769139SJan.Pechanec@Sun.COM #include "sftp.h"
770Sstevel@tonic-gate
780Sstevel@tonic-gate #ifdef USE_PAM
790Sstevel@tonic-gate #include <security/pam_appl.h>
800Sstevel@tonic-gate #endif /* USE_PAM */
810Sstevel@tonic-gate
820Sstevel@tonic-gate #ifdef GSSAPI
830Sstevel@tonic-gate #include "ssh-gss.h"
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate
860Sstevel@tonic-gate #ifdef ALTPRIVSEP
870Sstevel@tonic-gate #include "altprivsep.h"
880Sstevel@tonic-gate #endif /* ALTPRIVSEP */
890Sstevel@tonic-gate
900Sstevel@tonic-gate #ifdef HAVE_CYGWIN
910Sstevel@tonic-gate #include <windows.h>
920Sstevel@tonic-gate #include <sys/cygwin.h>
930Sstevel@tonic-gate #define is_winnt (GetVersion() < 0x80000000)
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* func */
970Sstevel@tonic-gate
980Sstevel@tonic-gate Session *session_new(void);
990Sstevel@tonic-gate void session_set_fds(Session *, int, int, int);
1000Sstevel@tonic-gate void session_pty_cleanup(void *);
1013109Sjp161948 void session_xauthfile_cleanup(void *s);
1020Sstevel@tonic-gate void session_proctitle(Session *);
1030Sstevel@tonic-gate int session_setup_x11fwd(Session *);
1040Sstevel@tonic-gate void do_exec_pty(Session *, const char *);
1050Sstevel@tonic-gate void do_exec_no_pty(Session *, const char *);
1060Sstevel@tonic-gate void do_exec(Session *, const char *);
1070Sstevel@tonic-gate void do_login(Session *, const char *);
1080Sstevel@tonic-gate void do_child(Session *, const char *);
1090Sstevel@tonic-gate void do_motd(void);
1100Sstevel@tonic-gate int check_quietlogin(Session *, const char *);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate static void do_authenticated1(Authctxt *);
1130Sstevel@tonic-gate static void do_authenticated2(Authctxt *);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static int session_pty_req(Session *);
1160Sstevel@tonic-gate static int session_env_req(Session *s);
1170Sstevel@tonic-gate static void session_free_env(char ***envp);
1189139SJan.Pechanec@Sun.COM static void safely_chroot(const char *path, uid_t uid);
1199139SJan.Pechanec@Sun.COM static void drop_privs(uid_t uid);
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate #ifdef USE_PAM
1220Sstevel@tonic-gate static void session_do_pam(Session *, int);
1230Sstevel@tonic-gate #endif /* USE_PAM */
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /* import */
1260Sstevel@tonic-gate extern ServerOptions options;
1270Sstevel@tonic-gate extern char *__progname;
1280Sstevel@tonic-gate extern int log_stderr;
1290Sstevel@tonic-gate extern int debug_flag;
1300Sstevel@tonic-gate extern u_int utmp_len;
1310Sstevel@tonic-gate extern void destroy_sensitive_data(void);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate #ifdef GSSAPI
1340Sstevel@tonic-gate extern Gssctxt *xxx_gssctxt;
1350Sstevel@tonic-gate #endif /* GSSAPI */
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /* original command from peer. */
1380Sstevel@tonic-gate const char *original_command = NULL;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /* data */
1410Sstevel@tonic-gate #define MAX_SESSIONS 10
1420Sstevel@tonic-gate Session sessions[MAX_SESSIONS];
1430Sstevel@tonic-gate
1449139SJan.Pechanec@Sun.COM #define SUBSYSTEM_NONE 0
1459139SJan.Pechanec@Sun.COM #define SUBSYSTEM_EXT 1
1469139SJan.Pechanec@Sun.COM #define SUBSYSTEM_INT_SFTP 2
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
1490Sstevel@tonic-gate login_cap_t *lc;
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate /* Name and directory of socket for authentication agent forwarding. */
1530Sstevel@tonic-gate static char *auth_sock_name = NULL;
1540Sstevel@tonic-gate static char *auth_sock_dir = NULL;
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /* removes the agent forwarding socket */
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate static void
auth_sock_cleanup_proc(void * _pw)1590Sstevel@tonic-gate auth_sock_cleanup_proc(void *_pw)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate struct passwd *pw = _pw;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if (auth_sock_name != NULL) {
1640Sstevel@tonic-gate temporarily_use_uid(pw);
1650Sstevel@tonic-gate unlink(auth_sock_name);
1660Sstevel@tonic-gate rmdir(auth_sock_dir);
1670Sstevel@tonic-gate auth_sock_name = NULL;
1680Sstevel@tonic-gate restore_uid();
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate static int
auth_input_request_forwarding(struct passwd * pw)1730Sstevel@tonic-gate auth_input_request_forwarding(struct passwd * pw)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate Channel *nc;
1760Sstevel@tonic-gate int sock;
1770Sstevel@tonic-gate struct sockaddr_un sunaddr;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if (auth_sock_name != NULL) {
1800Sstevel@tonic-gate error("authentication forwarding requested twice.");
1810Sstevel@tonic-gate return 0;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /* Temporarily drop privileged uid for mkdir/bind. */
1850Sstevel@tonic-gate temporarily_use_uid(pw);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /* Allocate a buffer for the socket name, and format the name. */
1880Sstevel@tonic-gate auth_sock_name = xmalloc(MAXPATHLEN);
1890Sstevel@tonic-gate auth_sock_dir = xmalloc(MAXPATHLEN);
1900Sstevel@tonic-gate strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /* Create private directory for socket */
1930Sstevel@tonic-gate if (mkdtemp(auth_sock_dir) == NULL) {
1940Sstevel@tonic-gate packet_send_debug("Agent forwarding disabled: "
1950Sstevel@tonic-gate "mkdtemp() failed: %.100s", strerror(errno));
1960Sstevel@tonic-gate restore_uid();
1970Sstevel@tonic-gate xfree(auth_sock_name);
1980Sstevel@tonic-gate xfree(auth_sock_dir);
1990Sstevel@tonic-gate auth_sock_name = NULL;
2000Sstevel@tonic-gate auth_sock_dir = NULL;
2010Sstevel@tonic-gate return 0;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
2040Sstevel@tonic-gate auth_sock_dir, (long) getpid());
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate /* delete agent socket on fatal() */
2070Sstevel@tonic-gate fatal_add_cleanup(auth_sock_cleanup_proc, pw);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /* Create the socket. */
2100Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0);
2110Sstevel@tonic-gate if (sock < 0)
2120Sstevel@tonic-gate packet_disconnect("socket: %.100s", strerror(errno));
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /* Bind it to the name. */
2150Sstevel@tonic-gate memset(&sunaddr, 0, sizeof(sunaddr));
2160Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX;
2170Sstevel@tonic-gate strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2200Sstevel@tonic-gate packet_disconnect("bind: %.100s", strerror(errno));
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /* Restore the privileged uid. */
2230Sstevel@tonic-gate restore_uid();
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /* Start listening on the socket. */
2260Sstevel@tonic-gate if (listen(sock, 5) < 0)
2270Sstevel@tonic-gate packet_disconnect("listen: %.100s", strerror(errno));
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate /* Allocate a channel for the authentication agent socket. */
2300Sstevel@tonic-gate nc = channel_new("auth socket",
2310Sstevel@tonic-gate SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2320Sstevel@tonic-gate CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2330Sstevel@tonic-gate 0, xstrdup("auth socket"), 1);
2340Sstevel@tonic-gate strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
2350Sstevel@tonic-gate return 1;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate void
do_authenticated(Authctxt * authctxt)2400Sstevel@tonic-gate do_authenticated(Authctxt *authctxt)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate /* setup the channel layer */
2430Sstevel@tonic-gate if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
2440Sstevel@tonic-gate channel_permit_all_opens();
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate if (compat20)
2470Sstevel@tonic-gate do_authenticated2(authctxt);
2480Sstevel@tonic-gate else
2490Sstevel@tonic-gate do_authenticated1(authctxt);
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate /* remove agent socket */
2520Sstevel@tonic-gate if (auth_sock_name != NULL)
2530Sstevel@tonic-gate auth_sock_cleanup_proc(authctxt->pw);
2540Sstevel@tonic-gate #ifdef KRB4
2550Sstevel@tonic-gate if (options.kerberos_ticket_cleanup)
2560Sstevel@tonic-gate krb4_cleanup_proc(authctxt);
2570Sstevel@tonic-gate #endif
2580Sstevel@tonic-gate #ifdef KRB5
2590Sstevel@tonic-gate if (options.kerberos_ticket_cleanup)
2600Sstevel@tonic-gate krb5_cleanup_proc(authctxt);
2610Sstevel@tonic-gate #endif
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate * Prepares for an interactive session. This is called after the user has
2660Sstevel@tonic-gate * been successfully authenticated. During this message exchange, pseudo
2670Sstevel@tonic-gate * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
2680Sstevel@tonic-gate * are requested, etc.
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate static void
do_authenticated1(Authctxt * authctxt)2710Sstevel@tonic-gate do_authenticated1(Authctxt *authctxt)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate Session *s;
2740Sstevel@tonic-gate char *command;
2750Sstevel@tonic-gate int success, type, screen_flag;
2760Sstevel@tonic-gate int enable_compression_after_reply = 0;
2770Sstevel@tonic-gate u_int proto_len, data_len, dlen, compression_level = 0;
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate s = session_new();
2800Sstevel@tonic-gate s->authctxt = authctxt;
2810Sstevel@tonic-gate s->pw = authctxt->pw;
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate * We stay in this loop until the client requests to execute a shell
2850Sstevel@tonic-gate * or a command.
2860Sstevel@tonic-gate */
2870Sstevel@tonic-gate for (;;) {
2880Sstevel@tonic-gate success = 0;
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /* Get a packet from the client. */
2910Sstevel@tonic-gate type = packet_read();
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /* Process the packet. */
2940Sstevel@tonic-gate switch (type) {
2950Sstevel@tonic-gate case SSH_CMSG_REQUEST_COMPRESSION:
2960Sstevel@tonic-gate compression_level = packet_get_int();
2970Sstevel@tonic-gate packet_check_eom();
2980Sstevel@tonic-gate if (compression_level < 1 || compression_level > 9) {
2990Sstevel@tonic-gate packet_send_debug("Received illegal compression level %d.",
3000Sstevel@tonic-gate compression_level);
3010Sstevel@tonic-gate break;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate if (!options.compression) {
3040Sstevel@tonic-gate debug2("compression disabled");
3050Sstevel@tonic-gate break;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate /* Enable compression after we have responded with SUCCESS. */
3080Sstevel@tonic-gate enable_compression_after_reply = 1;
3090Sstevel@tonic-gate success = 1;
3100Sstevel@tonic-gate break;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate case SSH_CMSG_REQUEST_PTY:
3130Sstevel@tonic-gate success = session_pty_req(s);
3140Sstevel@tonic-gate break;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate case SSH_CMSG_X11_REQUEST_FORWARDING:
3170Sstevel@tonic-gate s->auth_proto = packet_get_string(&proto_len);
3180Sstevel@tonic-gate s->auth_data = packet_get_string(&data_len);
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate screen_flag = packet_get_protocol_flags() &
3210Sstevel@tonic-gate SSH_PROTOFLAG_SCREEN_NUMBER;
3220Sstevel@tonic-gate debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate if (packet_remaining() == 4) {
3250Sstevel@tonic-gate if (!screen_flag)
3260Sstevel@tonic-gate debug2("Buggy client: "
3270Sstevel@tonic-gate "X11 screen flag missing");
3280Sstevel@tonic-gate s->screen = packet_get_int();
3290Sstevel@tonic-gate } else {
3300Sstevel@tonic-gate s->screen = 0;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate packet_check_eom();
3330Sstevel@tonic-gate success = session_setup_x11fwd(s);
3340Sstevel@tonic-gate if (!success) {
3350Sstevel@tonic-gate xfree(s->auth_proto);
3360Sstevel@tonic-gate xfree(s->auth_data);
3370Sstevel@tonic-gate s->auth_proto = NULL;
3380Sstevel@tonic-gate s->auth_data = NULL;
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate break;
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate case SSH_CMSG_AGENT_REQUEST_FORWARDING:
3430Sstevel@tonic-gate if (no_agent_forwarding_flag || compat13) {
3440Sstevel@tonic-gate debug("Authentication agent forwarding not permitted for this authentication.");
3450Sstevel@tonic-gate break;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate debug("Received authentication agent forwarding request.");
3480Sstevel@tonic-gate success = auth_input_request_forwarding(s->pw);
3490Sstevel@tonic-gate break;
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate case SSH_CMSG_PORT_FORWARD_REQUEST:
3520Sstevel@tonic-gate if (no_port_forwarding_flag) {
3530Sstevel@tonic-gate debug("Port forwarding not permitted for this authentication.");
3540Sstevel@tonic-gate break;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate if (!options.allow_tcp_forwarding) {
3570Sstevel@tonic-gate debug("Port forwarding not permitted.");
3580Sstevel@tonic-gate break;
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate debug("Received TCP/IP port forwarding request.");
3610Sstevel@tonic-gate channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
3620Sstevel@tonic-gate success = 1;
3630Sstevel@tonic-gate break;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate case SSH_CMSG_MAX_PACKET_SIZE:
3660Sstevel@tonic-gate if (packet_set_maxsize(packet_get_int()) > 0)
3670Sstevel@tonic-gate success = 1;
3680Sstevel@tonic-gate break;
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
3710Sstevel@tonic-gate case SSH_CMSG_HAVE_KERBEROS_TGT:
3720Sstevel@tonic-gate if (!options.kerberos_tgt_passing) {
3730Sstevel@tonic-gate verbose("Kerberos TGT passing disabled.");
3740Sstevel@tonic-gate } else {
3750Sstevel@tonic-gate char *kdata = packet_get_string(&dlen);
3760Sstevel@tonic-gate packet_check_eom();
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate /* XXX - 0x41, see creds_to_radix version */
3790Sstevel@tonic-gate if (kdata[0] != 0x41) {
3800Sstevel@tonic-gate #ifdef KRB5
3810Sstevel@tonic-gate krb5_data tgt;
3820Sstevel@tonic-gate tgt.data = kdata;
3830Sstevel@tonic-gate tgt.length = dlen;
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate if (auth_krb5_tgt(s->authctxt, &tgt))
3860Sstevel@tonic-gate success = 1;
3870Sstevel@tonic-gate else
3880Sstevel@tonic-gate verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
3890Sstevel@tonic-gate #endif /* KRB5 */
3900Sstevel@tonic-gate } else {
3910Sstevel@tonic-gate #ifdef AFS
3920Sstevel@tonic-gate if (auth_krb4_tgt(s->authctxt, kdata))
3930Sstevel@tonic-gate success = 1;
3940Sstevel@tonic-gate else
3950Sstevel@tonic-gate verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
3960Sstevel@tonic-gate #endif /* AFS */
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate xfree(kdata);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate break;
4010Sstevel@tonic-gate #endif /* AFS || KRB5 */
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate #ifdef AFS
4040Sstevel@tonic-gate case SSH_CMSG_HAVE_AFS_TOKEN:
4050Sstevel@tonic-gate if (!options.afs_token_passing || !k_hasafs()) {
4060Sstevel@tonic-gate verbose("AFS token passing disabled.");
4070Sstevel@tonic-gate } else {
4080Sstevel@tonic-gate /* Accept AFS token. */
4090Sstevel@tonic-gate char *token = packet_get_string(&dlen);
4100Sstevel@tonic-gate packet_check_eom();
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate if (auth_afs_token(s->authctxt, token))
4130Sstevel@tonic-gate success = 1;
4140Sstevel@tonic-gate else
4150Sstevel@tonic-gate verbose("AFS token refused for %.100s",
4160Sstevel@tonic-gate s->authctxt->user);
4170Sstevel@tonic-gate xfree(token);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate break;
4200Sstevel@tonic-gate #endif /* AFS */
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate case SSH_CMSG_EXEC_SHELL:
4230Sstevel@tonic-gate case SSH_CMSG_EXEC_CMD:
4240Sstevel@tonic-gate if (type == SSH_CMSG_EXEC_CMD) {
4250Sstevel@tonic-gate command = packet_get_string(&dlen);
4260Sstevel@tonic-gate debug("Exec command '%.500s'", command);
4270Sstevel@tonic-gate do_exec(s, command);
4280Sstevel@tonic-gate xfree(command);
4290Sstevel@tonic-gate } else {
4300Sstevel@tonic-gate do_exec(s, NULL);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate packet_check_eom();
4330Sstevel@tonic-gate session_close(s);
4340Sstevel@tonic-gate return;
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate default:
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate * Any unknown messages in this phase are ignored,
4390Sstevel@tonic-gate * and a failure message is returned.
4400Sstevel@tonic-gate */
4410Sstevel@tonic-gate log("Unknown packet type received after authentication: %d", type);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
4440Sstevel@tonic-gate packet_send();
4450Sstevel@tonic-gate packet_write_wait();
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate /* Enable compression now that we have replied if appropriate. */
4480Sstevel@tonic-gate if (enable_compression_after_reply) {
4490Sstevel@tonic-gate enable_compression_after_reply = 0;
4500Sstevel@tonic-gate packet_start_compression(compression_level);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * This is called to fork and execute a command when we have no tty. This
4570Sstevel@tonic-gate * will call do_child from the child, and server_loop from the parent after
4580Sstevel@tonic-gate * setting up file descriptors and such.
4590Sstevel@tonic-gate */
4600Sstevel@tonic-gate void
do_exec_no_pty(Session * s,const char * command)4610Sstevel@tonic-gate do_exec_no_pty(Session *s, const char *command)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate pid_t pid;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate int inout[2], err[2];
4660Sstevel@tonic-gate /* Uses socket pairs to communicate with the program. */
4670Sstevel@tonic-gate if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
4680Sstevel@tonic-gate socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
4690Sstevel@tonic-gate packet_disconnect("Could not create socket pairs: %.100s",
4700Sstevel@tonic-gate strerror(errno));
4710Sstevel@tonic-gate if (s == NULL)
4720Sstevel@tonic-gate fatal("do_exec_no_pty: no session");
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate session_proctitle(s);
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate /* Fork the child. */
4770Sstevel@tonic-gate if ((pid = fork()) == 0) {
4780Sstevel@tonic-gate fatal_remove_all_cleanups();
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate /* Child. Reinitialize the log since the pid has changed. */
4810Sstevel@tonic-gate log_init(__progname, options.log_level, options.log_facility, log_stderr);
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate * Create a new session and process group since the 4.4BSD
4850Sstevel@tonic-gate * setlogin() affects the entire process group.
4860Sstevel@tonic-gate */
4870Sstevel@tonic-gate if (setsid() < 0)
4880Sstevel@tonic-gate error("setsid failed: %.100s", strerror(errno));
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate /*
4910Sstevel@tonic-gate * Redirect stdin, stdout, and stderr. Stdin and stdout will
4920Sstevel@tonic-gate * use the same socket, as some programs (particularly rdist)
4930Sstevel@tonic-gate * seem to depend on it.
4940Sstevel@tonic-gate */
4950Sstevel@tonic-gate close(inout[1]);
4960Sstevel@tonic-gate close(err[1]);
4970Sstevel@tonic-gate if (dup2(inout[0], 0) < 0) /* stdin */
4980Sstevel@tonic-gate perror("dup2 stdin");
4990Sstevel@tonic-gate if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
5000Sstevel@tonic-gate perror("dup2 stdout");
50110876SZdenek.Kotala@Sun.COM if (s->is_subsystem) {
50210876SZdenek.Kotala@Sun.COM /*
50310876SZdenek.Kotala@Sun.COM * Redirect the subsystem's stderr to /dev/null. We might send it
50410876SZdenek.Kotala@Sun.COM * over to the other side but changing that might break existing
50510876SZdenek.Kotala@Sun.COM * SSH clients.
50610876SZdenek.Kotala@Sun.COM */
50710876SZdenek.Kotala@Sun.COM close(err[0]);
50810876SZdenek.Kotala@Sun.COM if ((err[0] = open(_PATH_DEVNULL, O_WRONLY)) == -1)
50910876SZdenek.Kotala@Sun.COM fatal("Cannot open /dev/null: %.100s", strerror(errno));
51010876SZdenek.Kotala@Sun.COM }
5110Sstevel@tonic-gate if (dup2(err[0], 2) < 0) /* stderr */
5120Sstevel@tonic-gate perror("dup2 stderr");
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate #ifdef _UNICOS
5150Sstevel@tonic-gate cray_init_job(s->pw); /* set up cray jid and tmpdir */
5160Sstevel@tonic-gate #endif
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate /* Do processing for the child (exec command etc). */
5190Sstevel@tonic-gate do_child(s, command);
5200Sstevel@tonic-gate /* NOTREACHED */
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate #ifdef _UNICOS
5230Sstevel@tonic-gate signal(WJSIGNAL, cray_job_termination_handler);
5240Sstevel@tonic-gate #endif /* _UNICOS */
5250Sstevel@tonic-gate #ifdef HAVE_CYGWIN
5260Sstevel@tonic-gate if (is_winnt)
5270Sstevel@tonic-gate cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
5280Sstevel@tonic-gate #endif
5290Sstevel@tonic-gate if (pid < 0)
5300Sstevel@tonic-gate packet_disconnect("fork failed: %.100s", strerror(errno));
5319139SJan.Pechanec@Sun.COM
5320Sstevel@tonic-gate s->pid = pid;
5330Sstevel@tonic-gate /* Set interactive/non-interactive mode. */
5340Sstevel@tonic-gate packet_set_interactive(s->display != NULL);
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate /* We are the parent. Close the child sides of the socket pairs. */
5370Sstevel@tonic-gate close(inout[0]);
5380Sstevel@tonic-gate close(err[0]);
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate * Enter the interactive session. Note: server_loop must be able to
5420Sstevel@tonic-gate * handle the case that fdin and fdout are the same.
5430Sstevel@tonic-gate */
5440Sstevel@tonic-gate if (compat20) {
5450Sstevel@tonic-gate session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
5468911SZdenek.Kotala@Sun.COM if (s->is_subsystem)
5478911SZdenek.Kotala@Sun.COM close(err[1]);
5480Sstevel@tonic-gate /* Don't close channel before sending exit-status! */
5490Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 1);
5500Sstevel@tonic-gate } else {
5510Sstevel@tonic-gate server_loop(pid, inout[1], inout[1], err[1]);
5520Sstevel@tonic-gate /* server_loop has closed inout[1] and err[1]. */
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate * This is called to fork and execute a command when we have a tty. This
5580Sstevel@tonic-gate * will call do_child from the child, and server_loop from the parent after
5590Sstevel@tonic-gate * setting up file descriptors, controlling tty, updating wtmp, utmp,
5600Sstevel@tonic-gate * lastlog, and other such operations.
5610Sstevel@tonic-gate */
5620Sstevel@tonic-gate void
do_exec_pty(Session * s,const char * command)5630Sstevel@tonic-gate do_exec_pty(Session *s, const char *command)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate int fdout, ptyfd, ttyfd, ptymaster, pipe_fds[2];
5660Sstevel@tonic-gate pid_t pid;
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate if (s == NULL)
5690Sstevel@tonic-gate fatal("do_exec_pty: no session");
5700Sstevel@tonic-gate ptyfd = s->ptyfd;
5710Sstevel@tonic-gate ttyfd = s->ttyfd;
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate #ifdef USE_PAM
5740Sstevel@tonic-gate session_do_pam(s, 1); /* pam_open_session() */
5750Sstevel@tonic-gate #endif /* USE_PAM */
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate * This pipe lets sshd wait for child to exec or exit. This is
5790Sstevel@tonic-gate * particularly important for ALTPRIVSEP because the child is
5800Sstevel@tonic-gate * the one to call the monitor to request a record_login() and
5810Sstevel@tonic-gate * we don't want the child and the parent to compete for the
5820Sstevel@tonic-gate * monitor's attention. But this is generic code and doesn't
5830Sstevel@tonic-gate * hurt to have here even if ALTPRIVSEP is not used.
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate if (pipe(pipe_fds) != 0)
5860Sstevel@tonic-gate packet_disconnect("pipe failed: %.100s", strerror(errno));
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate (void) fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC);
5890Sstevel@tonic-gate (void) fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC);
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /* Fork the child. */
5920Sstevel@tonic-gate if ((pid = fork()) == 0) {
5930Sstevel@tonic-gate (void) close(pipe_fds[0]);
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate fatal_remove_all_cleanups();
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate /* Child. Reinitialize the log because the pid has changed. */
5980Sstevel@tonic-gate log_init(__progname, options.log_level, options.log_facility, log_stderr);
5990Sstevel@tonic-gate /* Close the master side of the pseudo tty. */
6000Sstevel@tonic-gate close(ptyfd);
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate /* Make the pseudo tty our controlling tty. */
6030Sstevel@tonic-gate pty_make_controlling_tty(&ttyfd, s->tty);
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate /* Redirect stdin/stdout/stderr from the pseudo tty. */
6060Sstevel@tonic-gate if (dup2(ttyfd, 0) < 0)
6070Sstevel@tonic-gate error("dup2 stdin: %s", strerror(errno));
6080Sstevel@tonic-gate if (dup2(ttyfd, 1) < 0)
6090Sstevel@tonic-gate error("dup2 stdout: %s", strerror(errno));
6100Sstevel@tonic-gate if (dup2(ttyfd, 2) < 0)
6110Sstevel@tonic-gate error("dup2 stderr: %s", strerror(errno));
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate /* Close the extra descriptor for the pseudo tty. */
6140Sstevel@tonic-gate close(ttyfd);
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate /* record login, etc. similar to login(1) */
6179845SJan.Pechanec@Sun.COM do_login(s, command);
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate /*
6209845SJan.Pechanec@Sun.COM * Close the pipe to the parent so it can re-enter its event
6210Sstevel@tonic-gate * loop and service the ptm; if enough debug messages get
6220Sstevel@tonic-gate * written to the pty before this happens there will be a
6230Sstevel@tonic-gate * deadlock.
6240Sstevel@tonic-gate */
6250Sstevel@tonic-gate close(pipe_fds[1]);
6260Sstevel@tonic-gate
62711293SHuie-Ying.Lee@Sun.COM /*
62811293SHuie-Ying.Lee@Sun.COM * do_motd() was called originally in do_login(). However,
62911293SHuie-Ying.Lee@Sun.COM * when the /etc/motd file is large, a deadlock would happen,
63011293SHuie-Ying.Lee@Sun.COM * because
63111293SHuie-Ying.Lee@Sun.COM * - The child is blocked at fputs() to pty, when pty buffer
63211293SHuie-Ying.Lee@Sun.COM * is full.
63311293SHuie-Ying.Lee@Sun.COM * - The parent can not consume the pty buffer, because it is
63411293SHuie-Ying.Lee@Sun.COM * still blocked at read(pipe_fds[0]).
63511293SHuie-Ying.Lee@Sun.COM *
63611293SHuie-Ying.Lee@Sun.COM * To resolve the deadlock issue, we defer do_motd() after
63711293SHuie-Ying.Lee@Sun.COM * close(pipe_fds[1]).
63811293SHuie-Ying.Lee@Sun.COM */
63911293SHuie-Ying.Lee@Sun.COM do_motd();
64011293SHuie-Ying.Lee@Sun.COM
6410Sstevel@tonic-gate /* Do common processing for the child, such as execing the command. */
6420Sstevel@tonic-gate do_child(s, command);
6430Sstevel@tonic-gate /* NOTREACHED */
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate /* Wait for child to exec() or exit() */
6470Sstevel@tonic-gate (void) close(pipe_fds[1]);
6480Sstevel@tonic-gate (void) read(pipe_fds[0], &pipe_fds[1], sizeof(int));
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate #ifdef _UNICOS
6510Sstevel@tonic-gate signal(WJSIGNAL, cray_job_termination_handler);
6520Sstevel@tonic-gate #endif /* _UNICOS */
6530Sstevel@tonic-gate #ifdef HAVE_CYGWIN
6540Sstevel@tonic-gate if (is_winnt)
6550Sstevel@tonic-gate cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
6560Sstevel@tonic-gate #endif
6570Sstevel@tonic-gate if (pid < 0)
6580Sstevel@tonic-gate packet_disconnect("fork failed: %.100s", strerror(errno));
6590Sstevel@tonic-gate s->pid = pid;
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate /* Parent. Close the slave side of the pseudo tty. */
6620Sstevel@tonic-gate close(ttyfd);
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate * Create another descriptor of the pty master side for use as the
6660Sstevel@tonic-gate * standard input. We could use the original descriptor, but this
6670Sstevel@tonic-gate * simplifies code in server_loop. The descriptor is bidirectional.
6680Sstevel@tonic-gate */
6690Sstevel@tonic-gate fdout = dup(ptyfd);
6700Sstevel@tonic-gate if (fdout < 0)
6710Sstevel@tonic-gate packet_disconnect("dup #1 failed: %.100s", strerror(errno));
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /* we keep a reference to the pty master */
6740Sstevel@tonic-gate ptymaster = dup(ptyfd);
6750Sstevel@tonic-gate if (ptymaster < 0)
6760Sstevel@tonic-gate packet_disconnect("dup #2 failed: %.100s", strerror(errno));
6770Sstevel@tonic-gate s->ptymaster = ptymaster;
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate /* Enter interactive session. */
6800Sstevel@tonic-gate packet_set_interactive(1);
6810Sstevel@tonic-gate if (compat20) {
6820Sstevel@tonic-gate session_set_fds(s, ptyfd, fdout, -1);
6830Sstevel@tonic-gate /* Don't close channel before sending exit-status! */
6840Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 1);
6850Sstevel@tonic-gate } else {
6860Sstevel@tonic-gate server_loop(pid, ptyfd, fdout, -1);
6870Sstevel@tonic-gate /* server_loop _has_ closed ptyfd and fdout. */
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate /*
6920Sstevel@tonic-gate * This is called to fork and execute a command. If another command is
6930Sstevel@tonic-gate * to be forced, execute that instead.
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate void
do_exec(Session * s,const char * command)6960Sstevel@tonic-gate do_exec(Session *s, const char *command)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate if (command)
6990Sstevel@tonic-gate s->command = xstrdup(command);
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate if (forced_command) {
7020Sstevel@tonic-gate original_command = command;
7030Sstevel@tonic-gate command = forced_command;
7040Sstevel@tonic-gate debug("Forced command '%.900s'", command);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate if (s->ttyfd != -1)
7080Sstevel@tonic-gate do_exec_pty(s, command);
7090Sstevel@tonic-gate else
7100Sstevel@tonic-gate do_exec_no_pty(s, command);
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate original_command = NULL;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate /* administrative, login(1)-like work */
7170Sstevel@tonic-gate void
do_login(Session * s,const char * command)7180Sstevel@tonic-gate do_login(Session *s, const char *command)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate char *time_string;
7210Sstevel@tonic-gate #ifndef ALTPRIVSEP
7220Sstevel@tonic-gate struct passwd * pw = s->pw;
7230Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7240Sstevel@tonic-gate pid_t pid = getpid();
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate /* Record that there was a login on that tty from the remote host. */
7270Sstevel@tonic-gate #ifdef ALTPRIVSEP
7280Sstevel@tonic-gate debug3("Recording SSHv2 channel login in utmpx/wtmpx");
7290Sstevel@tonic-gate altprivsep_record_login(pid, s->tty);
7300Sstevel@tonic-gate #endif /* ALTPRIVSEP*/
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate if (check_quietlogin(s, command))
7330Sstevel@tonic-gate return;
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate #ifdef USE_PAM
7360Sstevel@tonic-gate print_pam_messages();
7370Sstevel@tonic-gate #endif /* USE_PAM */
7380Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
7390Sstevel@tonic-gate if (aixloginmsg && *aixloginmsg)
7400Sstevel@tonic-gate printf("%s\n", aixloginmsg);
7410Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate #ifndef NO_SSH_LASTLOG
7440Sstevel@tonic-gate if (options.print_lastlog && s->last_login_time != 0) {
7450Sstevel@tonic-gate time_string = ctime(&s->last_login_time);
7460Sstevel@tonic-gate if (strchr(time_string, '\n'))
7470Sstevel@tonic-gate *strchr(time_string, '\n') = 0;
7480Sstevel@tonic-gate if (strcmp(s->hostname, "") == 0)
7490Sstevel@tonic-gate printf("Last login: %s\r\n", time_string);
7500Sstevel@tonic-gate else
7510Sstevel@tonic-gate printf("Last login: %s from %s\r\n", time_string,
7520Sstevel@tonic-gate s->hostname);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate #endif /* NO_SSH_LASTLOG */
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate * Display the message of the day.
7600Sstevel@tonic-gate */
7610Sstevel@tonic-gate void
do_motd(void)7620Sstevel@tonic-gate do_motd(void)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate FILE *f;
7650Sstevel@tonic-gate char buf[256];
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate if (options.print_motd) {
7680Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
7690Sstevel@tonic-gate f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
7700Sstevel@tonic-gate "/etc/motd"), "r");
7710Sstevel@tonic-gate #else
7720Sstevel@tonic-gate f = fopen("/etc/motd", "r");
7730Sstevel@tonic-gate #endif
7740Sstevel@tonic-gate if (f) {
7750Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f))
7760Sstevel@tonic-gate fputs(buf, stdout);
7770Sstevel@tonic-gate fclose(f);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate /*
7840Sstevel@tonic-gate * Check for quiet login, either .hushlogin or command given.
7850Sstevel@tonic-gate */
7860Sstevel@tonic-gate int
check_quietlogin(Session * s,const char * command)7870Sstevel@tonic-gate check_quietlogin(Session *s, const char *command)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate char buf[256];
7900Sstevel@tonic-gate struct passwd *pw = s->pw;
7910Sstevel@tonic-gate struct stat st;
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate /* Return 1 if .hushlogin exists or a command given. */
7940Sstevel@tonic-gate if (command != NULL)
7950Sstevel@tonic-gate return 1;
7960Sstevel@tonic-gate snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
7970Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
7980Sstevel@tonic-gate if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
7990Sstevel@tonic-gate return 1;
8000Sstevel@tonic-gate #else
8010Sstevel@tonic-gate if (stat(buf, &st) >= 0)
8020Sstevel@tonic-gate return 1;
8030Sstevel@tonic-gate #endif
8040Sstevel@tonic-gate return 0;
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate /*
8080Sstevel@tonic-gate * Sets the value of the given variable in the environment. If the variable
8090Sstevel@tonic-gate * already exists, its value is overriden.
8100Sstevel@tonic-gate */
8110Sstevel@tonic-gate void
child_set_env(char *** envp,u_int * envsizep,const char * name,const char * value)8120Sstevel@tonic-gate child_set_env(char ***envp, u_int *envsizep, const char *name,
8130Sstevel@tonic-gate const char *value)
8140Sstevel@tonic-gate {
81511251SErik.Trauschke@Sun.COM debug3("child_set_env(%s, %s)", name, value);
81611251SErik.Trauschke@Sun.COM child_set_env_silent(envp, envsizep, name, value);
81711251SErik.Trauschke@Sun.COM }
81811251SErik.Trauschke@Sun.COM
81911251SErik.Trauschke@Sun.COM
82011251SErik.Trauschke@Sun.COM void
child_set_env_silent(char *** envp,u_int * envsizep,const char * name,const char * value)82111251SErik.Trauschke@Sun.COM child_set_env_silent(char ***envp, u_int *envsizep, const char *name,
82211251SErik.Trauschke@Sun.COM const char *value)
82311251SErik.Trauschke@Sun.COM {
8240Sstevel@tonic-gate u_int i, namelen;
8250Sstevel@tonic-gate char **env;
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate * Find the slot where the value should be stored. If the variable
8290Sstevel@tonic-gate * already exists, we reuse the slot; otherwise we append a new slot
8300Sstevel@tonic-gate * at the end of the array, expanding if necessary.
8310Sstevel@tonic-gate */
8320Sstevel@tonic-gate env = *envp;
8330Sstevel@tonic-gate namelen = strlen(name);
8340Sstevel@tonic-gate for (i = 0; env[i]; i++)
8350Sstevel@tonic-gate if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
8360Sstevel@tonic-gate break;
8370Sstevel@tonic-gate if (env[i]) {
8380Sstevel@tonic-gate /* Reuse the slot. */
8390Sstevel@tonic-gate xfree(env[i]);
8400Sstevel@tonic-gate } else {
8410Sstevel@tonic-gate /* New variable. Expand if necessary. */
8420Sstevel@tonic-gate if (i >= (*envsizep) - 1) {
8430Sstevel@tonic-gate if (*envsizep >= 1000)
8440Sstevel@tonic-gate fatal("child_set_env: too many env vars,"
8450Sstevel@tonic-gate " skipping: %.100s", name);
8460Sstevel@tonic-gate (*envsizep) += 50;
8470Sstevel@tonic-gate env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate /* Need to set the NULL pointer at end of array beyond the new slot. */
8500Sstevel@tonic-gate env[i + 1] = NULL;
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate /* Allocate space and format the variable in the appropriate slot. */
8540Sstevel@tonic-gate env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
8550Sstevel@tonic-gate snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate /*
8590Sstevel@tonic-gate * Reads environment variables from the given file and adds/overrides them
8600Sstevel@tonic-gate * into the environment. If the file does not exist, this does nothing.
8610Sstevel@tonic-gate * Otherwise, it must consist of empty lines, comments (line starts with '#')
8620Sstevel@tonic-gate * and assignments of the form name=value. No other forms are allowed.
8630Sstevel@tonic-gate */
8640Sstevel@tonic-gate static void
read_environment_file(char *** env,u_int * envsize,const char * filename)8650Sstevel@tonic-gate read_environment_file(char ***env, u_int *envsize,
8660Sstevel@tonic-gate const char *filename)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate FILE *f;
8690Sstevel@tonic-gate char buf[4096];
8700Sstevel@tonic-gate char *cp, *value;
8710Sstevel@tonic-gate u_int lineno = 0;
8720Sstevel@tonic-gate
8730Sstevel@tonic-gate f = fopen(filename, "r");
8740Sstevel@tonic-gate if (!f)
8750Sstevel@tonic-gate return;
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f)) {
8780Sstevel@tonic-gate if (++lineno > 1000)
8790Sstevel@tonic-gate fatal("Too many lines in environment file %s", filename);
8800Sstevel@tonic-gate for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
8810Sstevel@tonic-gate ;
8820Sstevel@tonic-gate if (!*cp || *cp == '#' || *cp == '\n')
8830Sstevel@tonic-gate continue;
8840Sstevel@tonic-gate if (strchr(cp, '\n'))
8850Sstevel@tonic-gate *strchr(cp, '\n') = '\0';
8860Sstevel@tonic-gate value = strchr(cp, '=');
8870Sstevel@tonic-gate if (value == NULL) {
8880Sstevel@tonic-gate fprintf(stderr, gettext("Bad line %u in %.100s\n"),
8890Sstevel@tonic-gate lineno, filename);
8900Sstevel@tonic-gate continue;
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate /*
8930Sstevel@tonic-gate * Replace the equals sign by nul, and advance value to
8940Sstevel@tonic-gate * the value string.
8950Sstevel@tonic-gate */
8960Sstevel@tonic-gate *value = '\0';
8970Sstevel@tonic-gate value++;
8980Sstevel@tonic-gate child_set_env(env, envsize, cp, value);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate fclose(f);
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate
copy_environment(char ** source,char *** env,u_int * envsize)9030Sstevel@tonic-gate void copy_environment(char **source, char ***env, u_int *envsize)
9040Sstevel@tonic-gate {
9050Sstevel@tonic-gate char *var_name, *var_val;
9060Sstevel@tonic-gate int i;
9070Sstevel@tonic-gate
9080Sstevel@tonic-gate if (source == NULL)
9090Sstevel@tonic-gate return;
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate for(i = 0; source[i] != NULL; i++) {
9120Sstevel@tonic-gate var_name = xstrdup(source[i]);
9130Sstevel@tonic-gate if ((var_val = strstr(var_name, "=")) == NULL) {
9140Sstevel@tonic-gate xfree(var_name);
9150Sstevel@tonic-gate continue;
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate *var_val++ = '\0';
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate debug3("Copy environment: %s=%s", var_name, var_val);
9200Sstevel@tonic-gate child_set_env(env, envsize, var_name, var_val);
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate xfree(var_name);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
9270Sstevel@tonic-gate static
9280Sstevel@tonic-gate void
deflt_do_setup_env(Session * s,const char * shell,char *** env,u_int * envsize)9290Sstevel@tonic-gate deflt_do_setup_env(Session *s, const char *shell, char ***env, u_int *envsize)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate int flags;
9320Sstevel@tonic-gate char *ptr;
9330Sstevel@tonic-gate mode_t Umask = 022;
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate if (defopen(_PATH_DEFAULT_LOGIN))
9360Sstevel@tonic-gate return;
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate /* Ignore case */
9390Sstevel@tonic-gate flags = defcntl(DC_GETFLAGS, 0);
9400Sstevel@tonic-gate TURNOFF(flags, DC_CASE);
9410Sstevel@tonic-gate (void) defcntl(DC_SETFLAGS, flags);
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate /* TZ & HZ */
9440Sstevel@tonic-gate if ((ptr = defread("TIMEZONE=")) != NULL)
9450Sstevel@tonic-gate child_set_env(env, envsize, "TZ", ptr);
9460Sstevel@tonic-gate if ((ptr = defread("HZ=")) != NULL)
9470Sstevel@tonic-gate child_set_env(env, envsize, "HZ", ptr);
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate /* PATH */
9500Sstevel@tonic-gate if (s->pw->pw_uid != 0 && (ptr = defread("PATH=")) != NULL)
9510Sstevel@tonic-gate child_set_env(env, envsize, "PATH", ptr);
9520Sstevel@tonic-gate if (s->pw->pw_uid == 0 && (ptr = defread("SUPATH=")) != NULL)
9530Sstevel@tonic-gate child_set_env(env, envsize, "PATH", ptr);
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate /* SHELL */
9560Sstevel@tonic-gate if ((ptr = defread("ALTSHELL=")) != NULL) {
9570Sstevel@tonic-gate if (strcasecmp("YES", ptr) == 0)
9580Sstevel@tonic-gate child_set_env(env, envsize, "SHELL", shell);
9590Sstevel@tonic-gate else
9600Sstevel@tonic-gate child_set_env(env, envsize, "SHELL", "");
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate /* UMASK */
9640Sstevel@tonic-gate if ((ptr = defread("UMASK=")) != NULL &&
9650Sstevel@tonic-gate sscanf(ptr, "%lo", &Umask) == 1 &&
9660Sstevel@tonic-gate Umask <= (mode_t)0777)
9670Sstevel@tonic-gate (void) umask(Umask);
9680Sstevel@tonic-gate else
9690Sstevel@tonic-gate (void) umask(022);
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate /* ULIMIT */
9720Sstevel@tonic-gate if ((ptr = defread("ULIMIT=")) != NULL && atol(ptr) > 0L &&
9730Sstevel@tonic-gate ulimit(UL_SETFSIZE, atol(ptr)) < 0L)
9740Sstevel@tonic-gate error("Could not set ULIMIT to %ld from %s\n", atol(ptr),
9750Sstevel@tonic-gate _PATH_DEFAULT_LOGIN);
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate (void) defopen(NULL);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
9800Sstevel@tonic-gate
9810Sstevel@tonic-gate static char **
do_setup_env(Session * s,const char * shell)9820Sstevel@tonic-gate do_setup_env(Session *s, const char *shell)
9830Sstevel@tonic-gate {
9843154Sjp161948 char buf[256];
9853154Sjp161948 char path_maildir[] = _PATH_MAILDIR;
9862757Sjp161948 u_int i, envsize, pm_len;
9870Sstevel@tonic-gate char **env;
9880Sstevel@tonic-gate struct passwd *pw = s->pw;
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate /* Initialize the environment. */
9910Sstevel@tonic-gate envsize = 100;
9920Sstevel@tonic-gate env = xmalloc(envsize * sizeof(char *));
9930Sstevel@tonic-gate env[0] = NULL;
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate #ifdef HAVE_CYGWIN
9960Sstevel@tonic-gate /*
9970Sstevel@tonic-gate * The Windows environment contains some setting which are
9980Sstevel@tonic-gate * important for a running system. They must not be dropped.
9990Sstevel@tonic-gate */
10000Sstevel@tonic-gate copy_environment(environ, &env, &envsize);
10010Sstevel@tonic-gate #endif
10020Sstevel@tonic-gate
10030Sstevel@tonic-gate #ifdef GSSAPI
10040Sstevel@tonic-gate /* Allow any GSSAPI methods that we've used to alter
10050Sstevel@tonic-gate * the childs environment as they see fit
10060Sstevel@tonic-gate */
10070Sstevel@tonic-gate ssh_gssapi_do_child(xxx_gssctxt, &env,&envsize);
10080Sstevel@tonic-gate #endif
10090Sstevel@tonic-gate
10109845SJan.Pechanec@Sun.COM /* Set basic environment. */
10119845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "USER", pw->pw_name);
10129845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
10139845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "HOME", pw->pw_dir);
10140Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
10159845SJan.Pechanec@Sun.COM if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
10169845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10179845SJan.Pechanec@Sun.COM else
10189845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "PATH", getenv("PATH"));
10190Sstevel@tonic-gate #else /* HAVE_LOGIN_CAP */
10200Sstevel@tonic-gate # ifndef HAVE_CYGWIN
10219845SJan.Pechanec@Sun.COM /*
10229845SJan.Pechanec@Sun.COM * There's no standard path on Windows. The path contains
10239845SJan.Pechanec@Sun.COM * important components pointing to the system directories,
10249845SJan.Pechanec@Sun.COM * needed for loading shared libraries. So the path better
10259845SJan.Pechanec@Sun.COM * remains intact here.
10269845SJan.Pechanec@Sun.COM */
10270Sstevel@tonic-gate # ifdef SUPERUSER_PATH
10289845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "PATH",
10299845SJan.Pechanec@Sun.COM s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
10300Sstevel@tonic-gate # else
10319845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
10320Sstevel@tonic-gate # endif /* SUPERUSER_PATH */
10330Sstevel@tonic-gate # endif /* HAVE_CYGWIN */
10340Sstevel@tonic-gate #endif /* HAVE_LOGIN_CAP */
10350Sstevel@tonic-gate
10369845SJan.Pechanec@Sun.COM pm_len = strlen(path_maildir);
10379845SJan.Pechanec@Sun.COM if (path_maildir[pm_len - 1] == '/' && pm_len > 1)
10389845SJan.Pechanec@Sun.COM path_maildir[pm_len - 1] = NULL;
10399845SJan.Pechanec@Sun.COM snprintf(buf, sizeof buf, "%.200s/%.50s",
10409845SJan.Pechanec@Sun.COM path_maildir, pw->pw_name);
10419845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "MAIL", buf);
10420Sstevel@tonic-gate
10439845SJan.Pechanec@Sun.COM /* Normal systems set SHELL by default. */
10449845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, "SHELL", shell);
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
10479845SJan.Pechanec@Sun.COM deflt_do_setup_env(s, shell, &env, &envsize);
10480Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate #define PASS_ENV(x) \
10510Sstevel@tonic-gate if (getenv(x)) \
10520Sstevel@tonic-gate child_set_env(&env, &envsize, x, getenv(x));
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate if (getenv("TZ"))
10550Sstevel@tonic-gate child_set_env(&env, &envsize, "TZ", getenv("TZ"));
10560Sstevel@tonic-gate
10573109Sjp161948 if (s->auth_file != NULL)
10583109Sjp161948 child_set_env(&env, &envsize, "XAUTHORITY", s->auth_file);
10593109Sjp161948
10600Sstevel@tonic-gate PASS_ENV("LANG")
10610Sstevel@tonic-gate PASS_ENV("LC_ALL")
10620Sstevel@tonic-gate PASS_ENV("LC_CTYPE")
10630Sstevel@tonic-gate PASS_ENV("LC_COLLATE")
10644181Sjp161948 PASS_ENV("LC_TIME")
10650Sstevel@tonic-gate PASS_ENV("LC_NUMERIC")
10660Sstevel@tonic-gate PASS_ENV("LC_MONETARY")
10670Sstevel@tonic-gate PASS_ENV("LC_MESSAGES")
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate #undef PASS_ENV
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate if (s->env != NULL)
10720Sstevel@tonic-gate copy_environment(s->env, &env, &envsize);
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate /* Set custom environment options from RSA authentication. */
10759845SJan.Pechanec@Sun.COM while (custom_environment) {
10769845SJan.Pechanec@Sun.COM struct envstring *ce = custom_environment;
10779845SJan.Pechanec@Sun.COM char *str = ce->s;
10780Sstevel@tonic-gate
10799845SJan.Pechanec@Sun.COM for (i = 0; str[i] != '=' && str[i]; i++)
10809845SJan.Pechanec@Sun.COM ;
10819845SJan.Pechanec@Sun.COM if (str[i] == '=') {
10829845SJan.Pechanec@Sun.COM str[i] = 0;
10839845SJan.Pechanec@Sun.COM child_set_env(&env, &envsize, str, str + i + 1);
10840Sstevel@tonic-gate }
10859845SJan.Pechanec@Sun.COM custom_environment = ce->next;
10869845SJan.Pechanec@Sun.COM xfree(ce->s);
10879845SJan.Pechanec@Sun.COM xfree(ce);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate
10900Sstevel@tonic-gate /* SSH_CLIENT deprecated */
10910Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.50s %d %d",
10920Sstevel@tonic-gate get_remote_ipaddr(), get_remote_port(), get_local_port());
10930Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_CLIENT", buf);
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
10960Sstevel@tonic-gate get_remote_ipaddr(), get_remote_port(),
10970Sstevel@tonic-gate get_local_ipaddr(packet_get_connection_in()), get_local_port());
10980Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
10990Sstevel@tonic-gate
11000Sstevel@tonic-gate if (s->ttyfd != -1)
11010Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_TTY", s->tty);
11020Sstevel@tonic-gate if (s->term)
11030Sstevel@tonic-gate child_set_env(&env, &envsize, "TERM", s->term);
11040Sstevel@tonic-gate if (s->display)
11050Sstevel@tonic-gate child_set_env(&env, &envsize, "DISPLAY", s->display);
11060Sstevel@tonic-gate if (original_command)
11070Sstevel@tonic-gate child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
11080Sstevel@tonic-gate original_command);
11090Sstevel@tonic-gate
11100Sstevel@tonic-gate #ifdef _UNICOS
11110Sstevel@tonic-gate if (cray_tmpdir[0] != '\0')
11120Sstevel@tonic-gate child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
11130Sstevel@tonic-gate #endif /* _UNICOS */
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate #ifdef _AIX
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate char *cp;
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate if ((cp = getenv("AUTHSTATE")) != NULL)
11200Sstevel@tonic-gate child_set_env(&env, &envsize, "AUTHSTATE", cp);
11210Sstevel@tonic-gate if ((cp = getenv("KRB5CCNAME")) != NULL)
11220Sstevel@tonic-gate child_set_env(&env, &envsize, "KRB5CCNAME", cp);
11230Sstevel@tonic-gate read_environment_file(&env, &envsize, "/etc/environment");
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate #endif
11260Sstevel@tonic-gate #ifdef KRB4
11270Sstevel@tonic-gate if (s->authctxt->krb4_ticket_file)
11280Sstevel@tonic-gate child_set_env(&env, &envsize, "KRBTKFILE",
11290Sstevel@tonic-gate s->authctxt->krb4_ticket_file);
11300Sstevel@tonic-gate #endif
11310Sstevel@tonic-gate #ifdef KRB5
11320Sstevel@tonic-gate if (s->authctxt->krb5_ticket_file)
11330Sstevel@tonic-gate child_set_env(&env, &envsize, "KRB5CCNAME",
11340Sstevel@tonic-gate s->authctxt->krb5_ticket_file);
11350Sstevel@tonic-gate #endif
11360Sstevel@tonic-gate #ifdef USE_PAM
11370Sstevel@tonic-gate /*
11380Sstevel@tonic-gate * Pull in any environment variables that may have
11390Sstevel@tonic-gate * been set by PAM.
11400Sstevel@tonic-gate */
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate char **p;
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate p = fetch_pam_environment(s->authctxt);
11450Sstevel@tonic-gate copy_environment(p, &env, &envsize);
11460Sstevel@tonic-gate free_pam_environment(p);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate #endif /* USE_PAM */
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate if (auth_sock_name != NULL)
11510Sstevel@tonic-gate child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
11520Sstevel@tonic-gate auth_sock_name);
11530Sstevel@tonic-gate
11540Sstevel@tonic-gate /* read $HOME/.ssh/environment. */
11559845SJan.Pechanec@Sun.COM if (options.permit_user_env) {
11560Sstevel@tonic-gate snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
11570Sstevel@tonic-gate strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
11580Sstevel@tonic-gate read_environment_file(&env, &envsize, buf);
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate if (debug_flag) {
11610Sstevel@tonic-gate /* dump the environment */
11620Sstevel@tonic-gate fprintf(stderr, gettext("Environment:\n"));
11630Sstevel@tonic-gate for (i = 0; env[i]; i++)
11640Sstevel@tonic-gate fprintf(stderr, " %.200s\n", env[i]);
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate return env;
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate
11690Sstevel@tonic-gate /*
11700Sstevel@tonic-gate * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
11710Sstevel@tonic-gate * first in this order).
11720Sstevel@tonic-gate */
11730Sstevel@tonic-gate static void
do_rc_files(Session * s,const char * shell)11740Sstevel@tonic-gate do_rc_files(Session *s, const char *shell)
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate FILE *f = NULL;
11770Sstevel@tonic-gate char cmd[1024];
11780Sstevel@tonic-gate int do_xauth;
11790Sstevel@tonic-gate struct stat st;
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate do_xauth =
11820Sstevel@tonic-gate s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate /* ignore _PATH_SSH_USER_RC for subsystems */
11850Sstevel@tonic-gate if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
11860Sstevel@tonic-gate snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
11870Sstevel@tonic-gate shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
11880Sstevel@tonic-gate if (debug_flag)
11890Sstevel@tonic-gate fprintf(stderr, "Running %s\n", cmd);
11900Sstevel@tonic-gate f = popen(cmd, "w");
11910Sstevel@tonic-gate if (f) {
11920Sstevel@tonic-gate if (do_xauth)
11930Sstevel@tonic-gate fprintf(f, "%s %s\n", s->auth_proto,
11940Sstevel@tonic-gate s->auth_data);
11950Sstevel@tonic-gate pclose(f);
11960Sstevel@tonic-gate } else
11970Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n",
11980Sstevel@tonic-gate _PATH_SSH_USER_RC);
11990Sstevel@tonic-gate } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
12000Sstevel@tonic-gate if (debug_flag)
12010Sstevel@tonic-gate fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
12020Sstevel@tonic-gate _PATH_SSH_SYSTEM_RC);
12030Sstevel@tonic-gate f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
12040Sstevel@tonic-gate if (f) {
12050Sstevel@tonic-gate if (do_xauth)
12060Sstevel@tonic-gate fprintf(f, "%s %s\n", s->auth_proto,
12070Sstevel@tonic-gate s->auth_data);
12080Sstevel@tonic-gate pclose(f);
12090Sstevel@tonic-gate } else
12100Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n",
12110Sstevel@tonic-gate _PATH_SSH_SYSTEM_RC);
12120Sstevel@tonic-gate } else if (do_xauth && options.xauth_location != NULL) {
12130Sstevel@tonic-gate /* Add authority data to .Xauthority if appropriate. */
12140Sstevel@tonic-gate if (debug_flag) {
12150Sstevel@tonic-gate fprintf(stderr,
12160Sstevel@tonic-gate "Running %.500s add "
12170Sstevel@tonic-gate "%.100s %.100s %.100s\n",
12180Sstevel@tonic-gate options.xauth_location, s->auth_display,
12190Sstevel@tonic-gate s->auth_proto, s->auth_data);
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate snprintf(cmd, sizeof cmd, "%s -q -",
12220Sstevel@tonic-gate options.xauth_location);
12230Sstevel@tonic-gate f = popen(cmd, "w");
12240Sstevel@tonic-gate if (f) {
12250Sstevel@tonic-gate fprintf(f, "add %s %s %s\n",
12260Sstevel@tonic-gate s->auth_display, s->auth_proto,
12270Sstevel@tonic-gate s->auth_data);
12280Sstevel@tonic-gate pclose(f);
12290Sstevel@tonic-gate } else {
12300Sstevel@tonic-gate fprintf(stderr, "Could not run %s\n",
12310Sstevel@tonic-gate cmd);
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate
1236*11802SJan.Pechanec@Sun.COM /* Disallow logins if /etc/nologin exists. This does not apply to root. */
12370Sstevel@tonic-gate static void
do_nologin(struct passwd * pw)12380Sstevel@tonic-gate do_nologin(struct passwd *pw)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate FILE *f = NULL;
12410Sstevel@tonic-gate char buf[1024];
1242*11802SJan.Pechanec@Sun.COM struct stat sb;
12430Sstevel@tonic-gate
1244*11802SJan.Pechanec@Sun.COM if (pw->pw_uid == 0)
1245*11802SJan.Pechanec@Sun.COM return;
1246*11802SJan.Pechanec@Sun.COM
1247*11802SJan.Pechanec@Sun.COM if (stat(_PATH_NOLOGIN, &sb) == -1)
1248*11802SJan.Pechanec@Sun.COM return;
1249*11802SJan.Pechanec@Sun.COM
1250*11802SJan.Pechanec@Sun.COM /* /etc/nologin exists. Print its contents if we can and exit. */
1251*11802SJan.Pechanec@Sun.COM log("User %.100s not allowed because %s exists.", pw->pw_name,
1252*11802SJan.Pechanec@Sun.COM _PATH_NOLOGIN);
1253*11802SJan.Pechanec@Sun.COM if ((f = fopen(_PATH_NOLOGIN, "r")) != NULL) {
12540Sstevel@tonic-gate while (fgets(buf, sizeof(buf), f))
12550Sstevel@tonic-gate fputs(buf, stderr);
12560Sstevel@tonic-gate fclose(f);
12570Sstevel@tonic-gate }
1258*11802SJan.Pechanec@Sun.COM exit(254);
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate
12619139SJan.Pechanec@Sun.COM /* Chroot into ChrootDirectory if the option is set. */
12620Sstevel@tonic-gate void
chroot_if_needed(struct passwd * pw)12639139SJan.Pechanec@Sun.COM chroot_if_needed(struct passwd *pw)
12640Sstevel@tonic-gate {
12659139SJan.Pechanec@Sun.COM char *chroot_path, *tmp;
12669139SJan.Pechanec@Sun.COM
12679139SJan.Pechanec@Sun.COM if (chroot_requested(options.chroot_directory)) {
12689139SJan.Pechanec@Sun.COM tmp = tilde_expand_filename(options.chroot_directory,
12699139SJan.Pechanec@Sun.COM pw->pw_uid);
12709139SJan.Pechanec@Sun.COM chroot_path = percent_expand(tmp, "h", pw->pw_dir,
12719139SJan.Pechanec@Sun.COM "u", pw->pw_name, (char *)NULL);
12729139SJan.Pechanec@Sun.COM safely_chroot(chroot_path, pw->pw_uid);
12739139SJan.Pechanec@Sun.COM free(tmp);
12749139SJan.Pechanec@Sun.COM free(chroot_path);
12759139SJan.Pechanec@Sun.COM }
12769139SJan.Pechanec@Sun.COM }
12779139SJan.Pechanec@Sun.COM
12789139SJan.Pechanec@Sun.COM /*
12799139SJan.Pechanec@Sun.COM * Chroot into a directory after checking it for safety: all path components
12809139SJan.Pechanec@Sun.COM * must be root-owned directories with strict permissions.
12819139SJan.Pechanec@Sun.COM */
12829139SJan.Pechanec@Sun.COM static void
safely_chroot(const char * path,uid_t uid)12839139SJan.Pechanec@Sun.COM safely_chroot(const char *path, uid_t uid)
12849139SJan.Pechanec@Sun.COM {
12859139SJan.Pechanec@Sun.COM const char *cp;
12869139SJan.Pechanec@Sun.COM char component[MAXPATHLEN];
12879139SJan.Pechanec@Sun.COM struct stat st;
12889139SJan.Pechanec@Sun.COM
12899139SJan.Pechanec@Sun.COM if (*path != '/')
12909139SJan.Pechanec@Sun.COM fatal("chroot path does not begin at root");
12919139SJan.Pechanec@Sun.COM if (strlen(path) >= sizeof(component))
12929139SJan.Pechanec@Sun.COM fatal("chroot path too long");
12930Sstevel@tonic-gate
12949139SJan.Pechanec@Sun.COM /*
12959139SJan.Pechanec@Sun.COM * Descend the path, checking that each component is a
12969139SJan.Pechanec@Sun.COM * root-owned directory with strict permissions.
12979139SJan.Pechanec@Sun.COM */
12989139SJan.Pechanec@Sun.COM for (cp = path; cp != NULL;) {
12999139SJan.Pechanec@Sun.COM if ((cp = strchr(cp, '/')) == NULL)
13009139SJan.Pechanec@Sun.COM strlcpy(component, path, sizeof(component));
13019139SJan.Pechanec@Sun.COM else {
13029139SJan.Pechanec@Sun.COM cp++;
13039139SJan.Pechanec@Sun.COM memcpy(component, path, cp - path);
13049139SJan.Pechanec@Sun.COM component[cp - path] = '\0';
13050Sstevel@tonic-gate }
13069139SJan.Pechanec@Sun.COM
13079139SJan.Pechanec@Sun.COM debug3("%s: checking '%s'", __func__, component);
13089139SJan.Pechanec@Sun.COM
13099139SJan.Pechanec@Sun.COM if (stat(component, &st) != 0)
13109139SJan.Pechanec@Sun.COM fatal("%s: stat(\"%s\"): %s", __func__,
13119139SJan.Pechanec@Sun.COM component, strerror(errno));
13129139SJan.Pechanec@Sun.COM if (st.st_uid != 0 || (st.st_mode & 022) != 0)
13139139SJan.Pechanec@Sun.COM fatal("bad ownership or modes for chroot "
13149139SJan.Pechanec@Sun.COM "directory %s\"%s\"",
13159139SJan.Pechanec@Sun.COM cp == NULL ? "" : "component ", component);
13169139SJan.Pechanec@Sun.COM if (!S_ISDIR(st.st_mode))
13179139SJan.Pechanec@Sun.COM fatal("chroot path %s\"%s\" is not a directory",
13189139SJan.Pechanec@Sun.COM cp == NULL ? "" : "component ", component);
13190Sstevel@tonic-gate }
13209139SJan.Pechanec@Sun.COM
13219139SJan.Pechanec@Sun.COM if (chdir(path) == -1)
13229139SJan.Pechanec@Sun.COM fatal("Unable to chdir to chroot path \"%s\": "
13239139SJan.Pechanec@Sun.COM "%s", path, strerror(errno));
13249139SJan.Pechanec@Sun.COM if (chroot(path) == -1)
13259139SJan.Pechanec@Sun.COM fatal("chroot(\"%s\"): %s", path, strerror(errno));
13269139SJan.Pechanec@Sun.COM if (chdir("/") == -1)
13279139SJan.Pechanec@Sun.COM fatal("%s: chdir(/) after chroot: %s",
13289139SJan.Pechanec@Sun.COM __func__, strerror(errno));
13299139SJan.Pechanec@Sun.COM verbose("Changed root directory to \"%s\"", path);
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate static void
launch_login(struct passwd * pw,const char * hostname)13330Sstevel@tonic-gate launch_login(struct passwd *pw, const char *hostname)
13340Sstevel@tonic-gate {
13350Sstevel@tonic-gate /* Launch login(1). */
13360Sstevel@tonic-gate
13370Sstevel@tonic-gate execl(LOGIN_PROGRAM, "login", "-h", hostname,
13380Sstevel@tonic-gate #ifdef xxxLOGIN_NEEDS_TERM
13390Sstevel@tonic-gate (s->term ? s->term : "unknown"),
13400Sstevel@tonic-gate #endif /* LOGIN_NEEDS_TERM */
13410Sstevel@tonic-gate #ifdef LOGIN_NO_ENDOPT
13420Sstevel@tonic-gate "-p", "-f", pw->pw_name, (char *)NULL);
13430Sstevel@tonic-gate #else
13440Sstevel@tonic-gate "-p", "-f", "--", pw->pw_name, (char *)NULL);
13450Sstevel@tonic-gate #endif
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate /* Login couldn't be executed, die. */
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate perror("login");
13500Sstevel@tonic-gate exit(1);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate * Performs common processing for the child, such as setting up the
13550Sstevel@tonic-gate * environment, closing extra file descriptors, setting the user and group
13560Sstevel@tonic-gate * ids, and executing the command or shell.
13570Sstevel@tonic-gate */
13589139SJan.Pechanec@Sun.COM #define ARGV_MAX 10
13590Sstevel@tonic-gate void
do_child(Session * s,const char * command)13600Sstevel@tonic-gate do_child(Session *s, const char *command)
13610Sstevel@tonic-gate {
13620Sstevel@tonic-gate extern char **environ;
13630Sstevel@tonic-gate char **env;
13649139SJan.Pechanec@Sun.COM char *argv[ARGV_MAX];
13659845SJan.Pechanec@Sun.COM const char *shell, *shell0;
13660Sstevel@tonic-gate struct passwd *pw = s->pw;
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate /* remove hostkey from the child's memory */
13690Sstevel@tonic-gate destroy_sensitive_data();
13700Sstevel@tonic-gate
13719845SJan.Pechanec@Sun.COM do_nologin(pw);
13729845SJan.Pechanec@Sun.COM chroot_if_needed(pw);
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate /*
13750Sstevel@tonic-gate * Get the shell from the password data. An empty shell field is
13760Sstevel@tonic-gate * legal, and means /bin/sh.
13770Sstevel@tonic-gate */
13780Sstevel@tonic-gate shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
13790Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
13800Sstevel@tonic-gate shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
13810Sstevel@tonic-gate #endif
13820Sstevel@tonic-gate
13830Sstevel@tonic-gate env = do_setup_env(s, shell);
13840Sstevel@tonic-gate
13850Sstevel@tonic-gate /*
13860Sstevel@tonic-gate * Close the connection descriptors; note that this is the child, and
13870Sstevel@tonic-gate * the server will still have the socket open, and it is important
13880Sstevel@tonic-gate * that we do not shutdown it. Note that the descriptors cannot be
13890Sstevel@tonic-gate * closed before building the environment, as we call
13900Sstevel@tonic-gate * get_remote_ipaddr there.
13910Sstevel@tonic-gate */
13920Sstevel@tonic-gate if (packet_get_connection_in() == packet_get_connection_out())
13930Sstevel@tonic-gate close(packet_get_connection_in());
13940Sstevel@tonic-gate else {
13950Sstevel@tonic-gate close(packet_get_connection_in());
13960Sstevel@tonic-gate close(packet_get_connection_out());
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate * Close all descriptors related to channels. They will still remain
14000Sstevel@tonic-gate * open in the parent.
14010Sstevel@tonic-gate */
14020Sstevel@tonic-gate /* XXX better use close-on-exec? -markus */
14030Sstevel@tonic-gate channel_close_all();
14040Sstevel@tonic-gate
14050Sstevel@tonic-gate /*
14060Sstevel@tonic-gate * Close any extra file descriptors. Note that there may still be
14070Sstevel@tonic-gate * descriptors left by system functions. They will be closed later.
14080Sstevel@tonic-gate */
14090Sstevel@tonic-gate endpwent();
14100Sstevel@tonic-gate
14110Sstevel@tonic-gate /*
14129600SNobutomo.Nakano@Sun.COM * Must switch to the new environment variables so that .ssh/rc,
14139600SNobutomo.Nakano@Sun.COM * /etc/ssh/sshrc, and xauth are run in the proper environment.
14149600SNobutomo.Nakano@Sun.COM */
14159600SNobutomo.Nakano@Sun.COM environ = env;
14169600SNobutomo.Nakano@Sun.COM
14179600SNobutomo.Nakano@Sun.COM /*
14189600SNobutomo.Nakano@Sun.COM * New environment has been installed. We need to update locale
14199600SNobutomo.Nakano@Sun.COM * so that error messages beyond this point have the proper
14209600SNobutomo.Nakano@Sun.COM * character encoding.
14219600SNobutomo.Nakano@Sun.COM */
14229600SNobutomo.Nakano@Sun.COM (void) setlocale(LC_ALL, "");
14239600SNobutomo.Nakano@Sun.COM
14249600SNobutomo.Nakano@Sun.COM /*
14250Sstevel@tonic-gate * Close any extra open file descriptors so that we don\'t have them
14260Sstevel@tonic-gate * hanging around in clients. Note that we want to do this after
14270Sstevel@tonic-gate * initgroups, because at least on Solaris 2.3 it leaves file
14280Sstevel@tonic-gate * descriptors open.
14290Sstevel@tonic-gate */
14300Sstevel@tonic-gate closefrom(STDERR_FILENO + 1);
14310Sstevel@tonic-gate
14320Sstevel@tonic-gate #ifdef AFS
14330Sstevel@tonic-gate /* Try to get AFS tokens for the local cell. */
14340Sstevel@tonic-gate if (k_hasafs()) {
14350Sstevel@tonic-gate char cell[64];
14360Sstevel@tonic-gate
14370Sstevel@tonic-gate if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
14380Sstevel@tonic-gate krb_afslog(cell, 0);
14390Sstevel@tonic-gate
14400Sstevel@tonic-gate krb_afslog(0, 0);
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate #endif /* AFS */
14430Sstevel@tonic-gate
14449139SJan.Pechanec@Sun.COM /* Change current directory to the user's home directory. */
14450Sstevel@tonic-gate if (chdir(pw->pw_dir) < 0) {
14469139SJan.Pechanec@Sun.COM /* Suppress missing homedir warning for chroot case */
14479139SJan.Pechanec@Sun.COM if (!chroot_requested(options.chroot_directory))
14489139SJan.Pechanec@Sun.COM fprintf(stderr, "Could not chdir to home "
14499139SJan.Pechanec@Sun.COM "directory %s: %s\n", pw->pw_dir,
14509139SJan.Pechanec@Sun.COM strerror(errno));
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate
14539845SJan.Pechanec@Sun.COM do_rc_files(s, shell);
14540Sstevel@tonic-gate
14550Sstevel@tonic-gate /* restore SIGPIPE for child */
14560Sstevel@tonic-gate signal(SIGPIPE, SIG_DFL);
14570Sstevel@tonic-gate
14589139SJan.Pechanec@Sun.COM if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
14599139SJan.Pechanec@Sun.COM int i;
14609139SJan.Pechanec@Sun.COM char *p, *args;
14619139SJan.Pechanec@Sun.COM extern int optind, optreset;
14629139SJan.Pechanec@Sun.COM
14639139SJan.Pechanec@Sun.COM /* This will set the E/P sets here, simulating exec(2). */
14649139SJan.Pechanec@Sun.COM drop_privs(pw->pw_uid);
14659139SJan.Pechanec@Sun.COM
14669139SJan.Pechanec@Sun.COM setproctitle("%s@internal-sftp-server", s->pw->pw_name);
14679139SJan.Pechanec@Sun.COM args = xstrdup(command ? command : "sftp-server");
14689139SJan.Pechanec@Sun.COM
14699139SJan.Pechanec@Sun.COM i = 0;
14709139SJan.Pechanec@Sun.COM for ((p = strtok(args, " ")); p != NULL; (p = strtok(NULL, " "))) {
14719139SJan.Pechanec@Sun.COM if (i < ARGV_MAX - 1)
14729139SJan.Pechanec@Sun.COM argv[i++] = p;
14739139SJan.Pechanec@Sun.COM }
14749139SJan.Pechanec@Sun.COM
14759139SJan.Pechanec@Sun.COM argv[i] = NULL;
14769139SJan.Pechanec@Sun.COM optind = optreset = 1;
14779139SJan.Pechanec@Sun.COM __progname = argv[0];
14789139SJan.Pechanec@Sun.COM exit(sftp_server_main(i, argv, s->pw));
14799139SJan.Pechanec@Sun.COM }
14809139SJan.Pechanec@Sun.COM
14810Sstevel@tonic-gate /* Get the last component of the shell name. */
14820Sstevel@tonic-gate if ((shell0 = strrchr(shell, '/')) != NULL)
14830Sstevel@tonic-gate shell0++;
14840Sstevel@tonic-gate else
14850Sstevel@tonic-gate shell0 = shell;
14860Sstevel@tonic-gate
14870Sstevel@tonic-gate /*
14880Sstevel@tonic-gate * If we have no command, execute the shell. In this case, the shell
14890Sstevel@tonic-gate * name to be passed in argv[0] is preceded by '-' to indicate that
14900Sstevel@tonic-gate * this is a login shell.
14910Sstevel@tonic-gate */
14920Sstevel@tonic-gate if (!command) {
14930Sstevel@tonic-gate char argv0[256];
14940Sstevel@tonic-gate
14950Sstevel@tonic-gate /* Start the shell. Set initial character to '-'. */
14960Sstevel@tonic-gate argv0[0] = '-';
14970Sstevel@tonic-gate
14980Sstevel@tonic-gate if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
14990Sstevel@tonic-gate >= sizeof(argv0) - 1) {
15000Sstevel@tonic-gate errno = EINVAL;
15010Sstevel@tonic-gate perror(shell);
15020Sstevel@tonic-gate exit(1);
15030Sstevel@tonic-gate }
15040Sstevel@tonic-gate
15050Sstevel@tonic-gate /* Execute the shell. */
15060Sstevel@tonic-gate argv[0] = argv0;
15070Sstevel@tonic-gate argv[1] = NULL;
15080Sstevel@tonic-gate execve(shell, argv, env);
15090Sstevel@tonic-gate
15100Sstevel@tonic-gate /* Executing the shell failed. */
15110Sstevel@tonic-gate perror(shell);
15120Sstevel@tonic-gate exit(1);
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate /*
15150Sstevel@tonic-gate * Execute the command using the user's shell. This uses the -c
15160Sstevel@tonic-gate * option to execute the command.
15170Sstevel@tonic-gate */
15180Sstevel@tonic-gate argv[0] = (char *) shell0;
15190Sstevel@tonic-gate argv[1] = "-c";
15200Sstevel@tonic-gate argv[2] = (char *) command;
15210Sstevel@tonic-gate argv[3] = NULL;
15220Sstevel@tonic-gate execve(shell, argv, env);
15230Sstevel@tonic-gate perror(shell);
15240Sstevel@tonic-gate exit(1);
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate Session *
session_new(void)15280Sstevel@tonic-gate session_new(void)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate int i;
15310Sstevel@tonic-gate static int did_init = 0;
15320Sstevel@tonic-gate if (!did_init) {
15330Sstevel@tonic-gate debug("session_new: init");
15340Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15350Sstevel@tonic-gate sessions[i].used = 0;
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate did_init = 1;
15380Sstevel@tonic-gate }
15390Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15400Sstevel@tonic-gate Session *s = &sessions[i];
15410Sstevel@tonic-gate if (! s->used) {
15420Sstevel@tonic-gate memset(s, 0, sizeof(*s));
15430Sstevel@tonic-gate s->chanid = -1;
15440Sstevel@tonic-gate s->ptyfd = -1;
15450Sstevel@tonic-gate s->ttyfd = -1;
15460Sstevel@tonic-gate s->used = 1;
15470Sstevel@tonic-gate s->self = i;
15480Sstevel@tonic-gate s->env = NULL;
15490Sstevel@tonic-gate debug("session_new: session %d", i);
15500Sstevel@tonic-gate return s;
15510Sstevel@tonic-gate }
15520Sstevel@tonic-gate }
15530Sstevel@tonic-gate return NULL;
15540Sstevel@tonic-gate }
15550Sstevel@tonic-gate
15560Sstevel@tonic-gate static void
session_dump(void)15570Sstevel@tonic-gate session_dump(void)
15580Sstevel@tonic-gate {
15590Sstevel@tonic-gate int i;
15600Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15610Sstevel@tonic-gate Session *s = &sessions[i];
15620Sstevel@tonic-gate debug("dump: used %d session %d %p channel %d pid %ld",
15630Sstevel@tonic-gate s->used,
15640Sstevel@tonic-gate s->self,
15650Sstevel@tonic-gate s,
15660Sstevel@tonic-gate s->chanid,
15670Sstevel@tonic-gate (long)s->pid);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate
15710Sstevel@tonic-gate int
session_open(Authctxt * authctxt,int chanid)15720Sstevel@tonic-gate session_open(Authctxt *authctxt, int chanid)
15730Sstevel@tonic-gate {
15740Sstevel@tonic-gate Session *s = session_new();
15750Sstevel@tonic-gate debug("session_open: channel %d", chanid);
15760Sstevel@tonic-gate if (s == NULL) {
15770Sstevel@tonic-gate error("no more sessions");
15780Sstevel@tonic-gate return 0;
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate s->authctxt = authctxt;
15810Sstevel@tonic-gate s->pw = authctxt->pw;
15820Sstevel@tonic-gate if (s->pw == NULL)
15830Sstevel@tonic-gate fatal("no user for session %d", s->self);
15840Sstevel@tonic-gate debug("session_open: session %d: link with channel %d", s->self, chanid);
15850Sstevel@tonic-gate s->chanid = chanid;
15860Sstevel@tonic-gate return 1;
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate #ifndef lint
15900Sstevel@tonic-gate Session *
session_by_tty(char * tty)15910Sstevel@tonic-gate session_by_tty(char *tty)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate int i;
15940Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
15950Sstevel@tonic-gate Session *s = &sessions[i];
15960Sstevel@tonic-gate if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
15970Sstevel@tonic-gate debug("session_by_tty: session %d tty %s", i, tty);
15980Sstevel@tonic-gate return s;
15990Sstevel@tonic-gate }
16000Sstevel@tonic-gate }
16010Sstevel@tonic-gate debug("session_by_tty: unknown tty %.100s", tty);
16020Sstevel@tonic-gate session_dump();
16030Sstevel@tonic-gate return NULL;
16040Sstevel@tonic-gate }
16050Sstevel@tonic-gate #endif /* lint */
16060Sstevel@tonic-gate
16070Sstevel@tonic-gate static Session *
session_by_channel(int id)16080Sstevel@tonic-gate session_by_channel(int id)
16090Sstevel@tonic-gate {
16100Sstevel@tonic-gate int i;
16110Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
16120Sstevel@tonic-gate Session *s = &sessions[i];
16130Sstevel@tonic-gate if (s->used && s->chanid == id) {
16140Sstevel@tonic-gate debug("session_by_channel: session %d channel %d", i, id);
16150Sstevel@tonic-gate return s;
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate debug("session_by_channel: unknown channel %d", id);
16190Sstevel@tonic-gate session_dump();
16200Sstevel@tonic-gate return NULL;
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate
16230Sstevel@tonic-gate static Session *
session_by_pid(pid_t pid)16240Sstevel@tonic-gate session_by_pid(pid_t pid)
16250Sstevel@tonic-gate {
16260Sstevel@tonic-gate int i;
16270Sstevel@tonic-gate debug("session_by_pid: pid %ld", (long)pid);
16280Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
16290Sstevel@tonic-gate Session *s = &sessions[i];
16300Sstevel@tonic-gate if (s->used && s->pid == pid)
16310Sstevel@tonic-gate return s;
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate error("session_by_pid: unknown pid %ld", (long)pid);
16340Sstevel@tonic-gate session_dump();
16350Sstevel@tonic-gate return NULL;
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate
16380Sstevel@tonic-gate static int
session_window_change_req(Session * s)16390Sstevel@tonic-gate session_window_change_req(Session *s)
16400Sstevel@tonic-gate {
16410Sstevel@tonic-gate s->col = packet_get_int();
16420Sstevel@tonic-gate s->row = packet_get_int();
16430Sstevel@tonic-gate s->xpixel = packet_get_int();
16440Sstevel@tonic-gate s->ypixel = packet_get_int();
16450Sstevel@tonic-gate packet_check_eom();
16460Sstevel@tonic-gate pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
16470Sstevel@tonic-gate return 1;
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate
16500Sstevel@tonic-gate static int
session_pty_req(Session * s)16510Sstevel@tonic-gate session_pty_req(Session *s)
16520Sstevel@tonic-gate {
16530Sstevel@tonic-gate u_int len;
16540Sstevel@tonic-gate int n_bytes;
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate if (no_pty_flag) {
16570Sstevel@tonic-gate debug("Allocating a pty not permitted for this authentication.");
16580Sstevel@tonic-gate return 0;
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate if (s->ttyfd != -1) {
16610Sstevel@tonic-gate packet_disconnect("Protocol error: you already have a pty.");
16620Sstevel@tonic-gate return 0;
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate /* Get the time and hostname when the user last logged in. */
16650Sstevel@tonic-gate if (options.print_lastlog) {
16660Sstevel@tonic-gate s->hostname[0] = '\0';
16670Sstevel@tonic-gate s->last_login_time = get_last_login_time(s->pw->pw_uid,
16680Sstevel@tonic-gate s->pw->pw_name, s->hostname, sizeof(s->hostname));
16690Sstevel@tonic-gate
16700Sstevel@tonic-gate /*
16710Sstevel@tonic-gate * PAM may update the last login date.
16720Sstevel@tonic-gate *
16730Sstevel@tonic-gate * Ideally PAM would also show the last login date as a
16740Sstevel@tonic-gate * PAM_TEXT_INFO conversation message, and then we could just
16750Sstevel@tonic-gate * always force the use of keyboard-interactive just so we can
16760Sstevel@tonic-gate * pass any such PAM prompts and messages from the account and
16770Sstevel@tonic-gate * session stacks, but skip pam_authenticate() if other userauth
16780Sstevel@tonic-gate * has succeeded and the user's password isn't expired.
16790Sstevel@tonic-gate *
16800Sstevel@tonic-gate * Unfortunately this depends on support for keyboard-
16810Sstevel@tonic-gate * interactive in the client, and support for lastlog messages
16820Sstevel@tonic-gate * in some PAM module.
16830Sstevel@tonic-gate *
16840Sstevel@tonic-gate * As it is Solaris updates the lastlog in PAM, but does
16850Sstevel@tonic-gate * not show the lastlog date in PAM. If and when this state of
16860Sstevel@tonic-gate * affairs changes this hack can be reconsidered, and, maybe,
16870Sstevel@tonic-gate * removed.
16880Sstevel@tonic-gate *
16890Sstevel@tonic-gate * So we're stuck with a crude hack: get the lastlog
16900Sstevel@tonic-gate * time before calling pam_open_session() and store it
16910Sstevel@tonic-gate * in the Authctxt and then use it here once. After
16920Sstevel@tonic-gate * that, if the client opens any more pty sessions we'll
16930Sstevel@tonic-gate * show the last lastlog entry since userauth.
16940Sstevel@tonic-gate */
16950Sstevel@tonic-gate if (s->authctxt != NULL && s->authctxt->last_login_time > 0) {
16960Sstevel@tonic-gate s->last_login_time = s->authctxt->last_login_time;
16970Sstevel@tonic-gate (void) strlcpy(s->hostname,
16980Sstevel@tonic-gate s->authctxt->last_login_host,
16990Sstevel@tonic-gate sizeof(s->hostname));
17000Sstevel@tonic-gate s->authctxt->last_login_time = 0;
17010Sstevel@tonic-gate s->authctxt->last_login_host[0] = '\0';
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate
17050Sstevel@tonic-gate s->term = packet_get_string(&len);
17060Sstevel@tonic-gate
17070Sstevel@tonic-gate if (compat20) {
17080Sstevel@tonic-gate s->col = packet_get_int();
17090Sstevel@tonic-gate s->row = packet_get_int();
17100Sstevel@tonic-gate } else {
17110Sstevel@tonic-gate s->row = packet_get_int();
17120Sstevel@tonic-gate s->col = packet_get_int();
17130Sstevel@tonic-gate }
17140Sstevel@tonic-gate s->xpixel = packet_get_int();
17150Sstevel@tonic-gate s->ypixel = packet_get_int();
17160Sstevel@tonic-gate
17170Sstevel@tonic-gate if (strcmp(s->term, "") == 0) {
17180Sstevel@tonic-gate xfree(s->term);
17190Sstevel@tonic-gate s->term = NULL;
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate /* Allocate a pty and open it. */
17230Sstevel@tonic-gate debug("Allocating pty.");
17245562Sjp161948 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
17250Sstevel@tonic-gate if (s->term)
17260Sstevel@tonic-gate xfree(s->term);
17270Sstevel@tonic-gate s->term = NULL;
17280Sstevel@tonic-gate s->ptyfd = -1;
17290Sstevel@tonic-gate s->ttyfd = -1;
17300Sstevel@tonic-gate error("session_pty_req: session %d alloc failed", s->self);
17310Sstevel@tonic-gate return 0;
17320Sstevel@tonic-gate }
17330Sstevel@tonic-gate debug("session_pty_req: session %d alloc %s", s->self, s->tty);
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate /* for SSH1 the tty modes length is not given */
17360Sstevel@tonic-gate if (!compat20)
17370Sstevel@tonic-gate n_bytes = packet_remaining();
17380Sstevel@tonic-gate tty_parse_modes(s->ttyfd, &n_bytes);
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate /*
17410Sstevel@tonic-gate * Add a cleanup function to clear the utmp entry and record logout
17420Sstevel@tonic-gate * time in case we call fatal() (e.g., the connection gets closed).
17430Sstevel@tonic-gate */
17440Sstevel@tonic-gate fatal_add_cleanup(session_pty_cleanup, (void *)s);
17455562Sjp161948 pty_setowner(s->pw, s->tty);
17460Sstevel@tonic-gate
17470Sstevel@tonic-gate /* Set window size from the packet. */
17480Sstevel@tonic-gate pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
17490Sstevel@tonic-gate
17500Sstevel@tonic-gate packet_check_eom();
17510Sstevel@tonic-gate session_proctitle(s);
17520Sstevel@tonic-gate return 1;
17530Sstevel@tonic-gate }
17540Sstevel@tonic-gate
17550Sstevel@tonic-gate static int
session_subsystem_req(Session * s)17560Sstevel@tonic-gate session_subsystem_req(Session *s)
17570Sstevel@tonic-gate {
17580Sstevel@tonic-gate struct stat st;
17590Sstevel@tonic-gate u_int len;
17600Sstevel@tonic-gate int success = 0;
17619139SJan.Pechanec@Sun.COM char *prog, *cmd, *subsys = packet_get_string(&len);
17629139SJan.Pechanec@Sun.COM u_int i;
17630Sstevel@tonic-gate
17640Sstevel@tonic-gate packet_check_eom();
17650Sstevel@tonic-gate log("subsystem request for %.100s", subsys);
17660Sstevel@tonic-gate
17670Sstevel@tonic-gate for (i = 0; i < options.num_subsystems; i++) {
17680Sstevel@tonic-gate if (strcmp(subsys, options.subsystem_name[i]) == 0) {
17699139SJan.Pechanec@Sun.COM prog = options.subsystem_command[i];
17709139SJan.Pechanec@Sun.COM cmd = options.subsystem_args[i];
17719139SJan.Pechanec@Sun.COM if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
17729139SJan.Pechanec@Sun.COM s->is_subsystem = SUBSYSTEM_INT_SFTP;
17739139SJan.Pechanec@Sun.COM /*
17749139SJan.Pechanec@Sun.COM * We must stat(2) the subsystem before we chroot in
17759139SJan.Pechanec@Sun.COM * order to be able to send a proper error message.
17769139SJan.Pechanec@Sun.COM */
17779139SJan.Pechanec@Sun.COM } else if (chroot_requested(options.chroot_directory)) {
17789139SJan.Pechanec@Sun.COM char chdirsub[MAXPATHLEN];
17799139SJan.Pechanec@Sun.COM
17809139SJan.Pechanec@Sun.COM strlcpy(chdirsub, options.chroot_directory,
17819139SJan.Pechanec@Sun.COM sizeof (chdirsub));
17829139SJan.Pechanec@Sun.COM strlcat(chdirsub, "/", sizeof (chdirsub));
17839139SJan.Pechanec@Sun.COM strlcat(chdirsub, prog, sizeof (chdirsub));
17849139SJan.Pechanec@Sun.COM if (stat(chdirsub, &st) < 0) {
17859139SJan.Pechanec@Sun.COM error("subsystem: cannot stat %s under "
17869139SJan.Pechanec@Sun.COM "chroot directory %s: %s", prog,
17879139SJan.Pechanec@Sun.COM options.chroot_directory,
17889139SJan.Pechanec@Sun.COM strerror(errno));
17899139SJan.Pechanec@Sun.COM if (strcmp(subsys, "sftp") == 0)
17909139SJan.Pechanec@Sun.COM error("subsystem: please see "
17919139SJan.Pechanec@Sun.COM "the Subsystem option in "
17929139SJan.Pechanec@Sun.COM "sshd_config(4) for an "
17939139SJan.Pechanec@Sun.COM "explanation of '%s'.",
17949139SJan.Pechanec@Sun.COM INTERNAL_SFTP_NAME);
17959139SJan.Pechanec@Sun.COM break;
17969139SJan.Pechanec@Sun.COM }
17979139SJan.Pechanec@Sun.COM } else if (stat(prog, &st) < 0) {
17989139SJan.Pechanec@Sun.COM error("subsystem: cannot stat %s: %s", prog,
17990Sstevel@tonic-gate strerror(errno));
18000Sstevel@tonic-gate break;
18019139SJan.Pechanec@Sun.COM } else {
18029139SJan.Pechanec@Sun.COM s->is_subsystem = SUBSYSTEM_EXT;
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate debug("subsystem: exec() %s", cmd);
18050Sstevel@tonic-gate do_exec(s, cmd);
18060Sstevel@tonic-gate success = 1;
18070Sstevel@tonic-gate break;
18080Sstevel@tonic-gate }
18090Sstevel@tonic-gate }
18100Sstevel@tonic-gate
18110Sstevel@tonic-gate if (!success)
18120Sstevel@tonic-gate log("subsystem request for %.100s failed, subsystem not found",
18130Sstevel@tonic-gate subsys);
18140Sstevel@tonic-gate
18150Sstevel@tonic-gate xfree(subsys);
18160Sstevel@tonic-gate return success;
18170Sstevel@tonic-gate }
18180Sstevel@tonic-gate
18193109Sjp161948 /*
18203109Sjp161948 * Serve "x11-req" channel request for X11 forwarding for the current session
18213109Sjp161948 * channel.
18223109Sjp161948 */
18230Sstevel@tonic-gate static int
session_x11_req(Session * s)18240Sstevel@tonic-gate session_x11_req(Session *s)
18250Sstevel@tonic-gate {
18263154Sjp161948 int success, fd;
18273154Sjp161948 char xauthdir[] = "/tmp/ssh-xauth-XXXXXX";
18280Sstevel@tonic-gate
18290Sstevel@tonic-gate s->single_connection = packet_get_char();
18300Sstevel@tonic-gate s->auth_proto = packet_get_string(NULL);
18310Sstevel@tonic-gate s->auth_data = packet_get_string(NULL);
18320Sstevel@tonic-gate s->screen = packet_get_int();
18330Sstevel@tonic-gate packet_check_eom();
18340Sstevel@tonic-gate
18350Sstevel@tonic-gate success = session_setup_x11fwd(s);
18360Sstevel@tonic-gate if (!success) {
18370Sstevel@tonic-gate xfree(s->auth_proto);
18380Sstevel@tonic-gate xfree(s->auth_data);
18390Sstevel@tonic-gate s->auth_proto = NULL;
18400Sstevel@tonic-gate s->auth_data = NULL;
18415600Sjp161948 return (success);
18420Sstevel@tonic-gate }
18433109Sjp161948
18443109Sjp161948 /*
18453109Sjp161948 * Create per session X authority file so that different sessions
18463109Sjp161948 * don't contend for one common file. The reason for this is that
18473109Sjp161948 * xauth(1) locking doesn't work too well over network filesystems.
18483109Sjp161948 *
18493154Sjp161948 * If mkdtemp() or open() fails then s->auth_file remains NULL which
18503154Sjp161948 * means that we won't set XAUTHORITY variable in child's environment
18513154Sjp161948 * and xauth(1) will use the default location for the authority file.
18523109Sjp161948 */
18535600Sjp161948 if (mkdtemp(xauthdir) != NULL) {
18543109Sjp161948 s->auth_file = xmalloc(MAXPATHLEN);
18553109Sjp161948 snprintf(s->auth_file, MAXPATHLEN, "%s/xauthfile",
18563109Sjp161948 xauthdir);
18573109Sjp161948 /*
18583154Sjp161948 * we don't want that "creating new authority file" message to
18593154Sjp161948 * be printed by xauth(1) so we must create that file
18603154Sjp161948 * beforehand.
18613109Sjp161948 */
18623154Sjp161948 if ((fd = open(s->auth_file, O_CREAT | O_EXCL | O_RDONLY,
18633154Sjp161948 S_IRUSR | S_IWUSR)) == -1) {
18643154Sjp161948 error("failed to create the temporary X authority "
18653154Sjp161948 "file %s: %.100s; will use the default one",
18663154Sjp161948 s->auth_file, strerror(errno));
18673154Sjp161948 xfree(s->auth_file);
18683154Sjp161948 s->auth_file = NULL;
18693154Sjp161948 if (rmdir(xauthdir) == -1) {
18703154Sjp161948 error("cannot remove xauth directory %s: %.100s",
18713154Sjp161948 xauthdir, strerror(errno));
18723154Sjp161948 }
18733154Sjp161948 } else {
18743154Sjp161948 close(fd);
18753154Sjp161948 debug("temporary X authority file %s created",
18763154Sjp161948 s->auth_file);
18773154Sjp161948
18783154Sjp161948 /*
18793154Sjp161948 * add a cleanup function to remove the temporary
18803154Sjp161948 * xauth file in case we call fatal() (e.g., the
18813154Sjp161948 * connection gets closed).
18823154Sjp161948 */
18833154Sjp161948 fatal_add_cleanup(session_xauthfile_cleanup, (void *)s);
18843154Sjp161948 }
18853154Sjp161948 }
18863154Sjp161948 else {
18873154Sjp161948 error("failed to create a directory for the temporary X "
18883154Sjp161948 "authority file: %.100s; will use the default xauth file",
18893154Sjp161948 strerror(errno));
18903109Sjp161948 }
18913109Sjp161948
18925600Sjp161948 return (success);
18930Sstevel@tonic-gate }
18940Sstevel@tonic-gate
18950Sstevel@tonic-gate static int
session_shell_req(Session * s)18960Sstevel@tonic-gate session_shell_req(Session *s)
18970Sstevel@tonic-gate {
18980Sstevel@tonic-gate packet_check_eom();
18990Sstevel@tonic-gate do_exec(s, NULL);
19000Sstevel@tonic-gate return 1;
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate
19030Sstevel@tonic-gate static int
session_exec_req(Session * s)19040Sstevel@tonic-gate session_exec_req(Session *s)
19050Sstevel@tonic-gate {
19060Sstevel@tonic-gate u_int len;
19070Sstevel@tonic-gate char *command = packet_get_string(&len);
19080Sstevel@tonic-gate packet_check_eom();
19090Sstevel@tonic-gate do_exec(s, command);
19100Sstevel@tonic-gate xfree(command);
19110Sstevel@tonic-gate return 1;
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate
19140Sstevel@tonic-gate static int
session_auth_agent_req(Session * s)19150Sstevel@tonic-gate session_auth_agent_req(Session *s)
19160Sstevel@tonic-gate {
19170Sstevel@tonic-gate static int called = 0;
19180Sstevel@tonic-gate packet_check_eom();
19190Sstevel@tonic-gate if (no_agent_forwarding_flag) {
19200Sstevel@tonic-gate debug("session_auth_agent_req: no_agent_forwarding_flag");
19210Sstevel@tonic-gate return 0;
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate if (called) {
19240Sstevel@tonic-gate return 0;
19250Sstevel@tonic-gate } else {
19260Sstevel@tonic-gate called = 1;
19270Sstevel@tonic-gate return auth_input_request_forwarding(s->pw);
19280Sstevel@tonic-gate }
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate
19310Sstevel@tonic-gate static int
session_loc_env_check(char * var,char * val)19320Sstevel@tonic-gate session_loc_env_check(char *var, char *val)
19330Sstevel@tonic-gate {
19340Sstevel@tonic-gate char *current;
19350Sstevel@tonic-gate int cat, ret;
19360Sstevel@tonic-gate
19370Sstevel@tonic-gate if (strcmp(var, "LANG") == 0)
19380Sstevel@tonic-gate cat = LC_ALL;
19390Sstevel@tonic-gate else if (strcmp(var, "LC_ALL") == 0)
19400Sstevel@tonic-gate cat = LC_ALL;
19410Sstevel@tonic-gate else if (strcmp(var, "LC_CTYPE") == 0)
19420Sstevel@tonic-gate cat = LC_CTYPE;
19430Sstevel@tonic-gate else if (strcmp(var, "LC_COLLATE") == 0)
19440Sstevel@tonic-gate cat = LC_COLLATE;
19450Sstevel@tonic-gate else if (strcmp(var, "LC_TIME") == 0)
19460Sstevel@tonic-gate cat = LC_TIME;
19470Sstevel@tonic-gate else if (strcmp(var, "LC_NUMERIC") == 0)
19480Sstevel@tonic-gate cat = LC_NUMERIC;
19490Sstevel@tonic-gate else if (strcmp(var, "LC_MONETARY") == 0)
19500Sstevel@tonic-gate cat = LC_MONETARY;
19510Sstevel@tonic-gate else if (strcmp(var, "LC_MESSAGES") == 0)
19520Sstevel@tonic-gate cat = LC_MESSAGES;
19530Sstevel@tonic-gate
19547574SJan.Pechanec@Sun.COM current = setlocale(cat, NULL);
19550Sstevel@tonic-gate
19560Sstevel@tonic-gate ret = (setlocale(cat, val) != NULL);
19570Sstevel@tonic-gate (void) setlocale(cat, current);
19580Sstevel@tonic-gate return (ret);
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate
19610Sstevel@tonic-gate static int
session_env_req(Session * s)19620Sstevel@tonic-gate session_env_req(Session *s)
19630Sstevel@tonic-gate {
19640Sstevel@tonic-gate Channel *c;
19650Sstevel@tonic-gate char *var, *val, *e;
19660Sstevel@tonic-gate char **p;
19670Sstevel@tonic-gate size_t len;
19680Sstevel@tonic-gate int ret = 0;
19690Sstevel@tonic-gate
19700Sstevel@tonic-gate /* Get var/val from the rest of this packet */
19710Sstevel@tonic-gate var = packet_get_string(NULL);
19720Sstevel@tonic-gate val = packet_get_string(NULL);
19730Sstevel@tonic-gate
19740Sstevel@tonic-gate /*
19750Sstevel@tonic-gate * We'll need the channel ID for the packet_send_debug messages,
19760Sstevel@tonic-gate * so get it now.
19770Sstevel@tonic-gate */
19780Sstevel@tonic-gate if ((c = channel_lookup(s->chanid)) == NULL)
19790Sstevel@tonic-gate goto done; /* shouldn't happen! */
19800Sstevel@tonic-gate
19810Sstevel@tonic-gate debug2("Received request for environment variable %s=%s", var, val);
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate /* For now allow only LANG and LC_* */
19840Sstevel@tonic-gate if (strcmp(var, "LANG") != 0 && strncmp(var, "LC_", 3) != 0) {
19850Sstevel@tonic-gate debug2("Rejecting request for environment variable %s", var);
19860Sstevel@tonic-gate goto done;
19870Sstevel@tonic-gate }
19880Sstevel@tonic-gate
19890Sstevel@tonic-gate if (!session_loc_env_check(var, val)) {
19900Sstevel@tonic-gate packet_send_debug(gettext("Missing locale support for %s=%s"),
19910Sstevel@tonic-gate var, val);
19920Sstevel@tonic-gate goto done;
19930Sstevel@tonic-gate }
19940Sstevel@tonic-gate
19950Sstevel@tonic-gate packet_send_debug(gettext("Channel %d set: %s=%s"), c->remote_id,
19960Sstevel@tonic-gate var, val);
19970Sstevel@tonic-gate
19980Sstevel@tonic-gate /*
19990Sstevel@tonic-gate * Always append new environment variables without regard to old
20000Sstevel@tonic-gate * ones being overriden. The way these are actually added to
20010Sstevel@tonic-gate * the environment of the session process later settings
20020Sstevel@tonic-gate * override earlier ones; see copy_environment().
20030Sstevel@tonic-gate */
20040Sstevel@tonic-gate if (s->env == NULL) {
20050Sstevel@tonic-gate char **env;
20060Sstevel@tonic-gate
20070Sstevel@tonic-gate env = xmalloc(sizeof (char **) * 2);
20080Sstevel@tonic-gate memset(env, 0, sizeof (char **) * 2);
20090Sstevel@tonic-gate
20100Sstevel@tonic-gate s->env = env;
20110Sstevel@tonic-gate p = env;
20120Sstevel@tonic-gate } else {
20130Sstevel@tonic-gate for (p = s->env; *p != NULL ; p++);
20140Sstevel@tonic-gate
20150Sstevel@tonic-gate s->env = xrealloc(s->env, (p - s->env + 2) * sizeof (char **));
20160Sstevel@tonic-gate
20170Sstevel@tonic-gate for (p = s->env; *p != NULL ; p++);
20180Sstevel@tonic-gate }
20190Sstevel@tonic-gate
20200Sstevel@tonic-gate len = snprintf(NULL, 0, "%s=%s", var, val);
20210Sstevel@tonic-gate e = xmalloc(len + 1);
20220Sstevel@tonic-gate (void) snprintf(e, len + 1, "%s=%s", var, val);
20230Sstevel@tonic-gate
20240Sstevel@tonic-gate (*p++) = e;
20250Sstevel@tonic-gate *p = NULL;
20260Sstevel@tonic-gate
20270Sstevel@tonic-gate ret = 1;
20280Sstevel@tonic-gate
20290Sstevel@tonic-gate done:
20300Sstevel@tonic-gate xfree(var);
20310Sstevel@tonic-gate xfree(val);
20320Sstevel@tonic-gate
20330Sstevel@tonic-gate return (ret);
20340Sstevel@tonic-gate }
20350Sstevel@tonic-gate
20360Sstevel@tonic-gate static void
session_free_env(char *** envp)20370Sstevel@tonic-gate session_free_env(char ***envp)
20380Sstevel@tonic-gate {
20390Sstevel@tonic-gate char **env, **p;
20400Sstevel@tonic-gate
20410Sstevel@tonic-gate if (envp == NULL || *envp == NULL)
20420Sstevel@tonic-gate return;
20430Sstevel@tonic-gate
20440Sstevel@tonic-gate env = *envp;
20450Sstevel@tonic-gate
20460Sstevel@tonic-gate *envp = NULL;
20470Sstevel@tonic-gate
20480Sstevel@tonic-gate for (p = env; *p != NULL; p++)
20490Sstevel@tonic-gate xfree(*p);
20500Sstevel@tonic-gate
20510Sstevel@tonic-gate xfree(env);
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate
20540Sstevel@tonic-gate int
session_input_channel_req(Channel * c,const char * rtype)20550Sstevel@tonic-gate session_input_channel_req(Channel *c, const char *rtype)
20560Sstevel@tonic-gate {
20570Sstevel@tonic-gate int success = 0;
20580Sstevel@tonic-gate Session *s;
20590Sstevel@tonic-gate
20600Sstevel@tonic-gate if ((s = session_by_channel(c->self)) == NULL) {
20610Sstevel@tonic-gate log("session_input_channel_req: no session %d req %.100s",
20620Sstevel@tonic-gate c->self, rtype);
20630Sstevel@tonic-gate return 0;
20640Sstevel@tonic-gate }
20650Sstevel@tonic-gate debug("session_input_channel_req: session %d req %s", s->self, rtype);
20660Sstevel@tonic-gate
20670Sstevel@tonic-gate /*
20680Sstevel@tonic-gate * a session is in LARVAL state until a shell, a command
20690Sstevel@tonic-gate * or a subsystem is executed
20700Sstevel@tonic-gate */
20710Sstevel@tonic-gate if (c->type == SSH_CHANNEL_LARVAL) {
20720Sstevel@tonic-gate if (strcmp(rtype, "shell") == 0) {
20730Sstevel@tonic-gate success = session_shell_req(s);
20740Sstevel@tonic-gate } else if (strcmp(rtype, "exec") == 0) {
20750Sstevel@tonic-gate success = session_exec_req(s);
20760Sstevel@tonic-gate } else if (strcmp(rtype, "pty-req") == 0) {
20770Sstevel@tonic-gate success = session_pty_req(s);
20780Sstevel@tonic-gate } else if (strcmp(rtype, "x11-req") == 0) {
20790Sstevel@tonic-gate success = session_x11_req(s);
20800Sstevel@tonic-gate } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
20810Sstevel@tonic-gate success = session_auth_agent_req(s);
20820Sstevel@tonic-gate } else if (strcmp(rtype, "subsystem") == 0) {
20830Sstevel@tonic-gate success = session_subsystem_req(s);
20840Sstevel@tonic-gate } else if (strcmp(rtype, "env") == 0) {
20850Sstevel@tonic-gate success = session_env_req(s);
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate }
20880Sstevel@tonic-gate if (strcmp(rtype, "window-change") == 0) {
20890Sstevel@tonic-gate success = session_window_change_req(s);
20900Sstevel@tonic-gate }
20910Sstevel@tonic-gate return success;
20920Sstevel@tonic-gate }
20930Sstevel@tonic-gate
20940Sstevel@tonic-gate void
session_set_fds(Session * s,int fdin,int fdout,int fderr)20950Sstevel@tonic-gate session_set_fds(Session *s, int fdin, int fdout, int fderr)
20960Sstevel@tonic-gate {
20970Sstevel@tonic-gate if (!compat20)
20980Sstevel@tonic-gate fatal("session_set_fds: called for proto != 2.0");
20990Sstevel@tonic-gate /*
21000Sstevel@tonic-gate * now that have a child and a pipe to the child,
21010Sstevel@tonic-gate * we can activate our channel and register the fd's
21020Sstevel@tonic-gate */
21030Sstevel@tonic-gate if (s->chanid == -1)
21040Sstevel@tonic-gate fatal("no channel for session %d", s->self);
21050Sstevel@tonic-gate channel_set_fds(s->chanid,
21060Sstevel@tonic-gate fdout, fdin, fderr,
21070Sstevel@tonic-gate fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
21080Sstevel@tonic-gate 1,
21090Sstevel@tonic-gate CHAN_SES_WINDOW_DEFAULT);
21100Sstevel@tonic-gate }
21110Sstevel@tonic-gate
21120Sstevel@tonic-gate /*
21130Sstevel@tonic-gate * Function to perform pty cleanup. Also called if we get aborted abnormally
21140Sstevel@tonic-gate * (e.g., due to a dropped connection).
21150Sstevel@tonic-gate */
21160Sstevel@tonic-gate void
session_pty_cleanup2(void * session)21170Sstevel@tonic-gate session_pty_cleanup2(void *session)
21180Sstevel@tonic-gate {
21190Sstevel@tonic-gate Session *s = session;
21200Sstevel@tonic-gate
21210Sstevel@tonic-gate if (s == NULL) {
21220Sstevel@tonic-gate error("session_pty_cleanup: no session");
21230Sstevel@tonic-gate return;
21240Sstevel@tonic-gate }
21250Sstevel@tonic-gate if (s->ttyfd == -1)
21260Sstevel@tonic-gate return;
21270Sstevel@tonic-gate
21280Sstevel@tonic-gate debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
21290Sstevel@tonic-gate
21300Sstevel@tonic-gate #ifdef USE_PAM
21310Sstevel@tonic-gate session_do_pam(s, 0);
21320Sstevel@tonic-gate #endif /* USE_PAM */
21330Sstevel@tonic-gate
21340Sstevel@tonic-gate /* Record that the user has logged out. */
21350Sstevel@tonic-gate if (s->pid != 0) {
21369845SJan.Pechanec@Sun.COM debug3("Recording SSHv2 channel logout in utmpx/wtmpx");
21370Sstevel@tonic-gate #ifdef ALTPRIVSEP
21380Sstevel@tonic-gate altprivsep_record_logout(s->pid);
21390Sstevel@tonic-gate #endif /* ALTPRIVSEP */
21400Sstevel@tonic-gate }
21410Sstevel@tonic-gate
21420Sstevel@tonic-gate /* Release the pseudo-tty. */
21430Sstevel@tonic-gate if (getuid() == 0)
21440Sstevel@tonic-gate pty_release(s->tty);
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate /*
21470Sstevel@tonic-gate * Close the server side of the socket pairs. We must do this after
21480Sstevel@tonic-gate * the pty cleanup, so that another process doesn't get this pty
21490Sstevel@tonic-gate * while we're still cleaning up.
21500Sstevel@tonic-gate */
21510Sstevel@tonic-gate if (close(s->ptymaster) < 0)
21520Sstevel@tonic-gate error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
21530Sstevel@tonic-gate
21540Sstevel@tonic-gate /* unlink pty from session */
21550Sstevel@tonic-gate s->ttyfd = -1;
21560Sstevel@tonic-gate }
21570Sstevel@tonic-gate
21580Sstevel@tonic-gate void
session_pty_cleanup(void * session)21590Sstevel@tonic-gate session_pty_cleanup(void *session)
21600Sstevel@tonic-gate {
21615562Sjp161948 session_pty_cleanup2(session);
21620Sstevel@tonic-gate }
21630Sstevel@tonic-gate
21643109Sjp161948 /*
21653109Sjp161948 * We use a different temporary X authority file per every session so we
21663109Sjp161948 * should remove those files when fatal() is called.
21673109Sjp161948 */
21683109Sjp161948 void
session_xauthfile_cleanup(void * session)21693109Sjp161948 session_xauthfile_cleanup(void *session)
21703109Sjp161948 {
21713109Sjp161948 Session *s = session;
21723109Sjp161948
21733109Sjp161948 if (s == NULL) {
21743109Sjp161948 error("session_xauthfile_cleanup: no session");
21753109Sjp161948 return;
21763109Sjp161948 }
21773109Sjp161948
21783109Sjp161948 debug("session_xauthfile_cleanup: session %d removing %s", s->self,
21793109Sjp161948 s->auth_file);
21803109Sjp161948
21813109Sjp161948 if (unlink(s->auth_file) == -1) {
21823109Sjp161948 error("session_xauthfile_cleanup: cannot remove xauth file: "
21833109Sjp161948 "%.100s", strerror(errno));
21843109Sjp161948 return;
21853109Sjp161948 }
21863109Sjp161948
21873109Sjp161948 /* dirname() will modify s->auth_file but that's ok */
21883109Sjp161948 if (rmdir(dirname(s->auth_file)) == -1) {
21893109Sjp161948 error("session_xauthfile_cleanup: "
21903109Sjp161948 "cannot remove xauth directory: %.100s", strerror(errno));
21913109Sjp161948 return;
21923109Sjp161948 }
21933109Sjp161948 }
21943109Sjp161948
21950Sstevel@tonic-gate static char *
sig2name(int sig)21960Sstevel@tonic-gate sig2name(int sig)
21970Sstevel@tonic-gate {
21980Sstevel@tonic-gate #define SSH_SIG(x) if (sig == SIG ## x) return #x
21990Sstevel@tonic-gate SSH_SIG(ABRT);
22000Sstevel@tonic-gate SSH_SIG(ALRM);
22010Sstevel@tonic-gate SSH_SIG(FPE);
22020Sstevel@tonic-gate SSH_SIG(HUP);
22030Sstevel@tonic-gate SSH_SIG(ILL);
22040Sstevel@tonic-gate SSH_SIG(INT);
22050Sstevel@tonic-gate SSH_SIG(KILL);
22060Sstevel@tonic-gate SSH_SIG(PIPE);
22070Sstevel@tonic-gate SSH_SIG(QUIT);
22080Sstevel@tonic-gate SSH_SIG(SEGV);
22090Sstevel@tonic-gate SSH_SIG(TERM);
22100Sstevel@tonic-gate SSH_SIG(USR1);
22110Sstevel@tonic-gate SSH_SIG(USR2);
22120Sstevel@tonic-gate #undef SSH_SIG
22130Sstevel@tonic-gate return "SIG@openssh.com";
22140Sstevel@tonic-gate }
22150Sstevel@tonic-gate
22160Sstevel@tonic-gate static void
session_exit_message(Session * s,int status)22170Sstevel@tonic-gate session_exit_message(Session *s, int status)
22180Sstevel@tonic-gate {
22190Sstevel@tonic-gate Channel *c;
22200Sstevel@tonic-gate
22210Sstevel@tonic-gate if ((c = channel_lookup(s->chanid)) == NULL)
22220Sstevel@tonic-gate fatal("session_exit_message: session %d: no channel %d",
22230Sstevel@tonic-gate s->self, s->chanid);
22240Sstevel@tonic-gate debug("session_exit_message: session %d channel %d pid %ld",
22250Sstevel@tonic-gate s->self, s->chanid, (long)s->pid);
22260Sstevel@tonic-gate
22270Sstevel@tonic-gate if (WIFEXITED(status)) {
22280Sstevel@tonic-gate channel_request_start(s->chanid, "exit-status", 0);
22290Sstevel@tonic-gate packet_put_int(WEXITSTATUS(status));
22300Sstevel@tonic-gate packet_send();
22310Sstevel@tonic-gate } else if (WIFSIGNALED(status)) {
22320Sstevel@tonic-gate channel_request_start(s->chanid, "exit-signal", 0);
22330Sstevel@tonic-gate packet_put_cstring(sig2name(WTERMSIG(status)));
22340Sstevel@tonic-gate #ifdef WCOREDUMP
22350Sstevel@tonic-gate packet_put_char(WCOREDUMP(status));
22360Sstevel@tonic-gate #else /* WCOREDUMP */
22370Sstevel@tonic-gate packet_put_char(0);
22380Sstevel@tonic-gate #endif /* WCOREDUMP */
22390Sstevel@tonic-gate packet_put_cstring("");
22400Sstevel@tonic-gate packet_put_cstring("");
22410Sstevel@tonic-gate packet_send();
22420Sstevel@tonic-gate } else {
22430Sstevel@tonic-gate /* Some weird exit cause. Just exit. */
22440Sstevel@tonic-gate packet_disconnect("wait returned status %04x.", status);
22450Sstevel@tonic-gate }
22460Sstevel@tonic-gate
22470Sstevel@tonic-gate /* Ok to close channel now */
22480Sstevel@tonic-gate channel_set_wait_for_exit(s->chanid, 0);
22490Sstevel@tonic-gate
22500Sstevel@tonic-gate /* disconnect channel */
22510Sstevel@tonic-gate debug("session_exit_message: release channel %d", s->chanid);
22520Sstevel@tonic-gate channel_cancel_cleanup(s->chanid);
22530Sstevel@tonic-gate /*
22540Sstevel@tonic-gate * emulate a write failure with 'chan_write_failed', nobody will be
22550Sstevel@tonic-gate * interested in data we write.
22560Sstevel@tonic-gate * Note that we must not call 'chan_read_failed', since there could
22570Sstevel@tonic-gate * be some more data waiting in the pipe.
22580Sstevel@tonic-gate */
22590Sstevel@tonic-gate if (c->ostate != CHAN_OUTPUT_CLOSED)
22600Sstevel@tonic-gate chan_write_failed(c);
22610Sstevel@tonic-gate s->chanid = -1;
22620Sstevel@tonic-gate }
22630Sstevel@tonic-gate
22640Sstevel@tonic-gate void
session_close(Session * s)22650Sstevel@tonic-gate session_close(Session *s)
22660Sstevel@tonic-gate {
22670Sstevel@tonic-gate debug("session_close: session %d pid %ld", s->self, (long)s->pid);
22680Sstevel@tonic-gate if (s->ttyfd != -1) {
22690Sstevel@tonic-gate fatal_remove_cleanup(session_pty_cleanup, (void *)s);
22700Sstevel@tonic-gate session_pty_cleanup(s);
22710Sstevel@tonic-gate }
22723109Sjp161948 if (s->auth_file != NULL) {
22733109Sjp161948 fatal_remove_cleanup(session_xauthfile_cleanup, (void *)s);
22743109Sjp161948 session_xauthfile_cleanup(s);
22753109Sjp161948 xfree(s->auth_file);
22763109Sjp161948 }
22770Sstevel@tonic-gate if (s->term)
22780Sstevel@tonic-gate xfree(s->term);
22790Sstevel@tonic-gate if (s->display)
22800Sstevel@tonic-gate xfree(s->display);
22810Sstevel@tonic-gate if (s->auth_display)
22820Sstevel@tonic-gate xfree(s->auth_display);
22830Sstevel@tonic-gate if (s->auth_data)
22840Sstevel@tonic-gate xfree(s->auth_data);
22850Sstevel@tonic-gate if (s->auth_proto)
22860Sstevel@tonic-gate xfree(s->auth_proto);
22870Sstevel@tonic-gate if (s->command)
22880Sstevel@tonic-gate xfree(s->command);
22890Sstevel@tonic-gate session_free_env(&s->env);
22900Sstevel@tonic-gate s->used = 0;
22910Sstevel@tonic-gate session_proctitle(s);
22920Sstevel@tonic-gate }
22930Sstevel@tonic-gate
22940Sstevel@tonic-gate void
session_close_by_pid(pid_t pid,int status)22950Sstevel@tonic-gate session_close_by_pid(pid_t pid, int status)
22960Sstevel@tonic-gate {
22970Sstevel@tonic-gate Session *s = session_by_pid(pid);
22980Sstevel@tonic-gate if (s == NULL) {
22990Sstevel@tonic-gate debug("session_close_by_pid: no session for pid %ld",
23000Sstevel@tonic-gate (long)pid);
23010Sstevel@tonic-gate return;
23020Sstevel@tonic-gate }
23030Sstevel@tonic-gate if (s->chanid != -1)
23040Sstevel@tonic-gate session_exit_message(s, status);
23050Sstevel@tonic-gate session_close(s);
23060Sstevel@tonic-gate }
23070Sstevel@tonic-gate
23080Sstevel@tonic-gate /*
23093109Sjp161948 * This is called when a channel dies before the session 'child' itself dies.
23103109Sjp161948 * It can happen for example if we exit from an interactive shell before we
23113109Sjp161948 * exit from forwarded X11 applications.
23120Sstevel@tonic-gate */
23130Sstevel@tonic-gate void
session_close_by_channel(int id,void * arg)23140Sstevel@tonic-gate session_close_by_channel(int id, void *arg)
23150Sstevel@tonic-gate {
23160Sstevel@tonic-gate Session *s = session_by_channel(id);
23170Sstevel@tonic-gate if (s == NULL) {
23180Sstevel@tonic-gate debug("session_close_by_channel: no session for id %d", id);
23190Sstevel@tonic-gate return;
23200Sstevel@tonic-gate }
23210Sstevel@tonic-gate debug("session_close_by_channel: channel %d child %ld",
23220Sstevel@tonic-gate id, (long)s->pid);
23230Sstevel@tonic-gate if (s->pid != 0) {
23240Sstevel@tonic-gate debug("session_close_by_channel: channel %d: has child", id);
23250Sstevel@tonic-gate /*
23260Sstevel@tonic-gate * delay detach of session, but release pty, since
23270Sstevel@tonic-gate * the fd's to the child are already closed
23280Sstevel@tonic-gate */
23290Sstevel@tonic-gate if (s->ttyfd != -1) {
23300Sstevel@tonic-gate fatal_remove_cleanup(session_pty_cleanup, (void *)s);
23310Sstevel@tonic-gate session_pty_cleanup(s);
23320Sstevel@tonic-gate }
23330Sstevel@tonic-gate return;
23340Sstevel@tonic-gate }
23350Sstevel@tonic-gate /* detach by removing callback */
23360Sstevel@tonic-gate channel_cancel_cleanup(s->chanid);
23370Sstevel@tonic-gate s->chanid = -1;
23380Sstevel@tonic-gate session_close(s);
23390Sstevel@tonic-gate }
23400Sstevel@tonic-gate
23410Sstevel@tonic-gate void
session_destroy_all(void (* closefunc)(Session *))23420Sstevel@tonic-gate session_destroy_all(void (*closefunc)(Session *))
23430Sstevel@tonic-gate {
23440Sstevel@tonic-gate int i;
23450Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
23460Sstevel@tonic-gate Session *s = &sessions[i];
23470Sstevel@tonic-gate if (s->used) {
23480Sstevel@tonic-gate if (closefunc != NULL)
23490Sstevel@tonic-gate closefunc(s);
23500Sstevel@tonic-gate else
23510Sstevel@tonic-gate session_close(s);
23520Sstevel@tonic-gate }
23530Sstevel@tonic-gate }
23540Sstevel@tonic-gate }
23550Sstevel@tonic-gate
23560Sstevel@tonic-gate static char *
session_tty_list(void)23570Sstevel@tonic-gate session_tty_list(void)
23580Sstevel@tonic-gate {
23590Sstevel@tonic-gate static char buf[1024];
23600Sstevel@tonic-gate int i;
23610Sstevel@tonic-gate buf[0] = '\0';
23620Sstevel@tonic-gate for (i = 0; i < MAX_SESSIONS; i++) {
23630Sstevel@tonic-gate Session *s = &sessions[i];
23640Sstevel@tonic-gate if (s->used && s->ttyfd != -1) {
23650Sstevel@tonic-gate if (buf[0] != '\0')
23660Sstevel@tonic-gate strlcat(buf, ",", sizeof buf);
23670Sstevel@tonic-gate strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate }
23700Sstevel@tonic-gate if (buf[0] == '\0')
23710Sstevel@tonic-gate strlcpy(buf, "notty", sizeof buf);
23720Sstevel@tonic-gate return buf;
23730Sstevel@tonic-gate }
23740Sstevel@tonic-gate
23750Sstevel@tonic-gate void
session_proctitle(Session * s)23760Sstevel@tonic-gate session_proctitle(Session *s)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate if (s->pw == NULL)
23790Sstevel@tonic-gate error("no user for session %d", s->self);
23800Sstevel@tonic-gate else
23810Sstevel@tonic-gate setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
23820Sstevel@tonic-gate }
23830Sstevel@tonic-gate
23840Sstevel@tonic-gate int
session_setup_x11fwd(Session * s)23850Sstevel@tonic-gate session_setup_x11fwd(Session *s)
23860Sstevel@tonic-gate {
23870Sstevel@tonic-gate struct stat st;
23880Sstevel@tonic-gate char display[512], auth_display[512];
23890Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate if (no_x11_forwarding_flag) {
23920Sstevel@tonic-gate packet_send_debug("X11 forwarding disabled in user configuration file.");
23930Sstevel@tonic-gate return 0;
23940Sstevel@tonic-gate }
23950Sstevel@tonic-gate if (!options.x11_forwarding) {
23960Sstevel@tonic-gate debug("X11 forwarding disabled in server configuration file.");
23970Sstevel@tonic-gate return 0;
23980Sstevel@tonic-gate }
23990Sstevel@tonic-gate if (!options.xauth_location ||
24000Sstevel@tonic-gate (stat(options.xauth_location, &st) == -1)) {
24010Sstevel@tonic-gate packet_send_debug("No xauth program; cannot forward with spoofing.");
24020Sstevel@tonic-gate return 0;
24030Sstevel@tonic-gate }
24040Sstevel@tonic-gate if (s->display != NULL) {
24050Sstevel@tonic-gate debug("X11 display already set.");
24060Sstevel@tonic-gate return 0;
24070Sstevel@tonic-gate }
24080Sstevel@tonic-gate if (x11_create_display_inet(options.x11_display_offset,
24090Sstevel@tonic-gate options.x11_use_localhost, s->single_connection,
24100Sstevel@tonic-gate &s->display_number) == -1) {
24110Sstevel@tonic-gate debug("x11_create_display_inet failed.");
24120Sstevel@tonic-gate return 0;
24130Sstevel@tonic-gate }
24140Sstevel@tonic-gate
24150Sstevel@tonic-gate /* Set up a suitable value for the DISPLAY variable. */
24160Sstevel@tonic-gate if (gethostname(hostname, sizeof(hostname)) < 0)
24170Sstevel@tonic-gate fatal("gethostname: %.100s", strerror(errno));
24180Sstevel@tonic-gate /*
24190Sstevel@tonic-gate * auth_display must be used as the displayname when the
24200Sstevel@tonic-gate * authorization entry is added with xauth(1). This will be
24210Sstevel@tonic-gate * different than the DISPLAY string for localhost displays.
24220Sstevel@tonic-gate */
24230Sstevel@tonic-gate if (options.x11_use_localhost) {
24240Sstevel@tonic-gate snprintf(display, sizeof display, "localhost:%u.%u",
24250Sstevel@tonic-gate s->display_number, s->screen);
24260Sstevel@tonic-gate snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
24270Sstevel@tonic-gate s->display_number, s->screen);
24280Sstevel@tonic-gate s->display = xstrdup(display);
24290Sstevel@tonic-gate s->auth_display = xstrdup(auth_display);
24300Sstevel@tonic-gate } else {
24310Sstevel@tonic-gate #ifdef IPADDR_IN_DISPLAY
24320Sstevel@tonic-gate struct hostent *he;
24330Sstevel@tonic-gate struct in_addr my_addr;
24340Sstevel@tonic-gate
24350Sstevel@tonic-gate he = gethostbyname(hostname);
24360Sstevel@tonic-gate if (he == NULL) {
24370Sstevel@tonic-gate error("Can't get IP address for X11 DISPLAY.");
24380Sstevel@tonic-gate packet_send_debug("Can't get IP address for X11 DISPLAY.");
24390Sstevel@tonic-gate return 0;
24400Sstevel@tonic-gate }
24410Sstevel@tonic-gate memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
24420Sstevel@tonic-gate snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
24430Sstevel@tonic-gate s->display_number, s->screen);
24440Sstevel@tonic-gate #else
24450Sstevel@tonic-gate snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
24460Sstevel@tonic-gate s->display_number, s->screen);
24470Sstevel@tonic-gate #endif
24480Sstevel@tonic-gate s->display = xstrdup(display);
24490Sstevel@tonic-gate s->auth_display = xstrdup(display);
24500Sstevel@tonic-gate }
24510Sstevel@tonic-gate
24520Sstevel@tonic-gate return 1;
24530Sstevel@tonic-gate }
24540Sstevel@tonic-gate
24550Sstevel@tonic-gate #ifdef USE_PAM
24560Sstevel@tonic-gate int session_do_pam_conv(int, struct pam_message **,
24570Sstevel@tonic-gate struct pam_response **, void *);
24580Sstevel@tonic-gate
24590Sstevel@tonic-gate static struct pam_conv session_pam_conv = {
24600Sstevel@tonic-gate session_do_pam_conv,
24610Sstevel@tonic-gate NULL
24620Sstevel@tonic-gate };
24630Sstevel@tonic-gate
24640Sstevel@tonic-gate static void
session_do_pam(Session * s,int do_open)24650Sstevel@tonic-gate session_do_pam(Session *s, int do_open)
24660Sstevel@tonic-gate {
24670Sstevel@tonic-gate int pam_retval;
24680Sstevel@tonic-gate char *where, *old_tty, *old_tty_copy = NULL;
24690Sstevel@tonic-gate struct pam_conv old_conv, *old_conv_ptr;
24700Sstevel@tonic-gate
24710Sstevel@tonic-gate if (!s || !s->authctxt || !s->authctxt->pam || !s->authctxt->pam->h)
24720Sstevel@tonic-gate return;
24730Sstevel@tonic-gate
24740Sstevel@tonic-gate /* Save current PAM item values */
24750Sstevel@tonic-gate where = "getting PAM_CONV";
24760Sstevel@tonic-gate pam_retval = pam_get_item(s->authctxt->pam->h, PAM_CONV,
24770Sstevel@tonic-gate (void **) &old_conv_ptr);
24780Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
24790Sstevel@tonic-gate goto done;
24800Sstevel@tonic-gate old_conv = *old_conv_ptr;
24810Sstevel@tonic-gate
24820Sstevel@tonic-gate where = "getting PAM_TTY";
24830Sstevel@tonic-gate pam_retval = pam_get_item(s->authctxt->pam->h, PAM_TTY,
24840Sstevel@tonic-gate (void **) &old_tty);
24850Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
24860Sstevel@tonic-gate goto done;
24870Sstevel@tonic-gate old_tty_copy = xstrdup(old_tty);
24880Sstevel@tonic-gate
24890Sstevel@tonic-gate /* Change PAM_TTY and PAM_CONV items */
24900Sstevel@tonic-gate where = "setting PAM_TTY";
24910Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, s->tty);
24920Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
24930Sstevel@tonic-gate goto done;
24940Sstevel@tonic-gate
24950Sstevel@tonic-gate where = "setting PAM_CONV";
24960Sstevel@tonic-gate session_pam_conv.appdata_ptr = s;
24970Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h,
24980Sstevel@tonic-gate PAM_CONV, &session_pam_conv);
24990Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
25000Sstevel@tonic-gate goto done;
25010Sstevel@tonic-gate
25020Sstevel@tonic-gate /* Call pam_open/close_session() */
25030Sstevel@tonic-gate if (do_open) {
25040Sstevel@tonic-gate where = "calling pam_open_session()";
25050Sstevel@tonic-gate pam_retval = pam_open_session(s->authctxt->pam->h, 0);
25060Sstevel@tonic-gate }
25070Sstevel@tonic-gate else {
25080Sstevel@tonic-gate where = "calling pam_close_session()";
25090Sstevel@tonic-gate pam_retval = pam_close_session(s->authctxt->pam->h, 0);
25100Sstevel@tonic-gate }
25110Sstevel@tonic-gate
25120Sstevel@tonic-gate /* Reset PAM_TTY and PAM_CONV items to previous values */
25130Sstevel@tonic-gate where = "setting PAM_TTY";
25140Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, old_tty_copy);
25150Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
25160Sstevel@tonic-gate goto done;
25170Sstevel@tonic-gate
25180Sstevel@tonic-gate where = "setting PAM_CONV";
25190Sstevel@tonic-gate pam_retval = pam_set_item(s->authctxt->pam->h, PAM_CONV, &old_conv);
25200Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS)
25210Sstevel@tonic-gate goto done;
25220Sstevel@tonic-gate
25230Sstevel@tonic-gate session_pam_conv.appdata_ptr = NULL;
25240Sstevel@tonic-gate
25250Sstevel@tonic-gate done:
25260Sstevel@tonic-gate if (old_tty_copy)
25270Sstevel@tonic-gate xfree(old_tty_copy);
25280Sstevel@tonic-gate
25290Sstevel@tonic-gate if (pam_retval == PAM_SUCCESS)
25300Sstevel@tonic-gate return;
25310Sstevel@tonic-gate
25320Sstevel@tonic-gate /* fatal()? probably not... */
25330Sstevel@tonic-gate log("PAM failed[%d] while %s: %s", pam_retval, where,
25340Sstevel@tonic-gate PAM_STRERROR(s->authctxt->pam->h, pam_retval));
25350Sstevel@tonic-gate }
25360Sstevel@tonic-gate
25370Sstevel@tonic-gate int
session_do_pam_conv(int num_prompts,struct pam_message ** prompts,struct pam_response ** resp,void * app_data)25380Sstevel@tonic-gate session_do_pam_conv(int num_prompts,
25390Sstevel@tonic-gate struct pam_message **prompts,
25400Sstevel@tonic-gate struct pam_response **resp,
25410Sstevel@tonic-gate void *app_data)
25420Sstevel@tonic-gate {
25430Sstevel@tonic-gate Session *s = (Session *) app_data;
25440Sstevel@tonic-gate
25450Sstevel@tonic-gate struct pam_response *reply;
25460Sstevel@tonic-gate int count;
25470Sstevel@tonic-gate char *prompt;
25480Sstevel@tonic-gate
25490Sstevel@tonic-gate if (channel_lookup(s->chanid) == NULL)
25500Sstevel@tonic-gate return PAM_CONV_ERR;
25510Sstevel@tonic-gate
25520Sstevel@tonic-gate /* PAM will free this later */
25530Sstevel@tonic-gate reply = xmalloc(num_prompts * sizeof(*reply));
25540Sstevel@tonic-gate
25550Sstevel@tonic-gate (void) memset(reply, 0, num_prompts * sizeof(*reply));
25560Sstevel@tonic-gate for (count = 0; count < num_prompts; count++) {
25570Sstevel@tonic-gate switch(PAM_MSG_MEMBER(prompts, count, msg_style)) {
25580Sstevel@tonic-gate case PAM_TEXT_INFO:
25590Sstevel@tonic-gate /* Write to stdout of channel */
25600Sstevel@tonic-gate prompt = PAM_MSG_MEMBER(prompts, count, msg);
25610Sstevel@tonic-gate if (prompt != NULL && s->ttyfd != -1) {
25620Sstevel@tonic-gate debug2("session_do_pam_conv: text info "
25630Sstevel@tonic-gate "prompt: %s", prompt);
25640Sstevel@tonic-gate (void) write(s->ttyfd, prompt, strlen(prompt));
25650Sstevel@tonic-gate (void) write(s->ttyfd, "\n", 1);
25660Sstevel@tonic-gate }
25670Sstevel@tonic-gate reply[count].resp = xstrdup("");
25680Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS;
25690Sstevel@tonic-gate break;
25700Sstevel@tonic-gate case PAM_ERROR_MSG:
25710Sstevel@tonic-gate /* Write to stderr of channel */
25720Sstevel@tonic-gate prompt = PAM_MSG_MEMBER(prompts, count, msg);
25730Sstevel@tonic-gate if (prompt != NULL && s->ttyfd != -1) {
25740Sstevel@tonic-gate debug2("session_do_pam_conv: error "
25750Sstevel@tonic-gate "prompt: %s", prompt);
25760Sstevel@tonic-gate (void) write(s->ttyfd, prompt, strlen(prompt));
25770Sstevel@tonic-gate (void) write(s->ttyfd, "\n", 1);
25780Sstevel@tonic-gate }
25790Sstevel@tonic-gate reply[count].resp = xstrdup("");
25800Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS;
25810Sstevel@tonic-gate break;
25820Sstevel@tonic-gate case PAM_PROMPT_ECHO_ON:
25830Sstevel@tonic-gate case PAM_PROMPT_ECHO_OFF:
25840Sstevel@tonic-gate /*
25850Sstevel@tonic-gate * XXX Someday add support for echo on/off prompts
25860Sstevel@tonic-gate * here on sessions with ttys.
25870Sstevel@tonic-gate */
25880Sstevel@tonic-gate default:
25890Sstevel@tonic-gate xfree(reply);
25900Sstevel@tonic-gate return PAM_CONV_ERR;
25910Sstevel@tonic-gate }
25920Sstevel@tonic-gate }
25930Sstevel@tonic-gate
25940Sstevel@tonic-gate *resp = reply;
25950Sstevel@tonic-gate
25960Sstevel@tonic-gate return PAM_SUCCESS;
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate #endif /* USE_PAM */
25990Sstevel@tonic-gate
26000Sstevel@tonic-gate static void
do_authenticated2(Authctxt * authctxt)26010Sstevel@tonic-gate do_authenticated2(Authctxt *authctxt)
26020Sstevel@tonic-gate {
26030Sstevel@tonic-gate server_loop2(authctxt);
26040Sstevel@tonic-gate }
26059139SJan.Pechanec@Sun.COM
26069139SJan.Pechanec@Sun.COM /*
26079139SJan.Pechanec@Sun.COM * Drop the privileges. We need this for the in-process SFTP server only. For
26089139SJan.Pechanec@Sun.COM * the shell and the external subsystem the exec(2) call will do the P = E = I
26099139SJan.Pechanec@Sun.COM * assignment itself. Never change the privileges if the connecting user is
26109139SJan.Pechanec@Sun.COM * root. See privileges(5) if the terminology used here is not known to you.
26119139SJan.Pechanec@Sun.COM */
26129139SJan.Pechanec@Sun.COM static void
drop_privs(uid_t uid)26139139SJan.Pechanec@Sun.COM drop_privs(uid_t uid)
26149139SJan.Pechanec@Sun.COM {
26159139SJan.Pechanec@Sun.COM priv_set_t *priv_inherit;
26169139SJan.Pechanec@Sun.COM
26179139SJan.Pechanec@Sun.COM /* If root is connecting we are done. */
26189139SJan.Pechanec@Sun.COM if (uid == 0)
26199139SJan.Pechanec@Sun.COM return;
26209139SJan.Pechanec@Sun.COM
26219139SJan.Pechanec@Sun.COM if ((priv_inherit = priv_allocset()) == NULL)
26229139SJan.Pechanec@Sun.COM fatal("priv_allocset: %s", strerror(errno));
26239139SJan.Pechanec@Sun.COM if (getppriv(PRIV_INHERITABLE, priv_inherit) != 0)
26249139SJan.Pechanec@Sun.COM fatal("getppriv: %s", strerror(errno));
26259139SJan.Pechanec@Sun.COM
26269139SJan.Pechanec@Sun.COM /*
26279139SJan.Pechanec@Sun.COM * This will limit E as well. Note that before this P was a
26289139SJan.Pechanec@Sun.COM * superset of I, see permanently_set_uid().
26299139SJan.Pechanec@Sun.COM */
26309139SJan.Pechanec@Sun.COM if (setppriv(PRIV_SET, PRIV_PERMITTED, priv_inherit) == -1)
26319139SJan.Pechanec@Sun.COM fatal("setppriv: %s", strerror(errno));
26329139SJan.Pechanec@Sun.COM
26339139SJan.Pechanec@Sun.COM priv_freeset(priv_inherit);
26349139SJan.Pechanec@Sun.COM
26359139SJan.Pechanec@Sun.COM /*
26369139SJan.Pechanec@Sun.COM * By manipulating the P set above we entered a PA mode which we
26379139SJan.Pechanec@Sun.COM * do not need to retain in.
26389139SJan.Pechanec@Sun.COM */
26399139SJan.Pechanec@Sun.COM if (setpflags(PRIV_AWARE, 0) == -1)
26409139SJan.Pechanec@Sun.COM fatal("setpflags: %s", strerror(errno));
26419139SJan.Pechanec@Sun.COM }
2642