Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1 | /* $OpenBSD: session.c,v 1.226 2008/02/08 23:24:07 djm Exp $ */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 5 | * |
| 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
| 11 | * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 12 | * SSH2 support by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 13 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * 1. Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer in the |
| 22 | * documentation and/or other materials provided with the distribution. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 25 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 26 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 27 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 34 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 35 | |
| 36 | #include "includes.h" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 37 | |
| 38 | #include <sys/types.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 39 | #include <sys/param.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 40 | #ifdef HAVE_SYS_STAT_H |
| 41 | # include <sys/stat.h> |
| 42 | #endif |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 43 | #include <sys/socket.h> |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 44 | #include <sys/un.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 45 | #include <sys/wait.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 46 | |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 47 | #include <arpa/inet.h> |
| 48 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 49 | #include <errno.h> |
Damien Miller | 427a1d5 | 2006-07-10 20:20:33 +1000 | [diff] [blame] | 50 | #include <grp.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 51 | #ifdef HAVE_PATHS_H |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 52 | #include <paths.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 53 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 54 | #include <pwd.h> |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 55 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 56 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 57 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 58 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 59 | #include <string.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 60 | #include <unistd.h> |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 61 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 62 | #include "xmalloc.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 63 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 64 | #include "ssh1.h" |
| 65 | #include "ssh2.h" |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 66 | #include "sshpty.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 67 | #include "packet.h" |
| 68 | #include "buffer.h" |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 69 | #include "match.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 70 | #include "uidswap.h" |
| 71 | #include "compat.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 72 | #include "channels.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 73 | #include "key.h" |
| 74 | #include "cipher.h" |
| 75 | #ifdef GSSAPI |
| 76 | #include "ssh-gss.h" |
| 77 | #endif |
| 78 | #include "hostfile.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 79 | #include "auth.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 80 | #include "auth-options.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "pathnames.h" |
| 82 | #include "log.h" |
| 83 | #include "servconf.h" |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 84 | #include "sshlogin.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 85 | #include "serverloop.h" |
| 86 | #include "canohost.h" |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 87 | #include "misc.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 88 | #include "session.h" |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 89 | #include "kex.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 90 | #include "monitor_wrap.h" |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 91 | #include "sftp.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 92 | |
Darren Tucker | 3c78c5e | 2004-01-23 22:03:10 +1100 | [diff] [blame] | 93 | #if defined(KRB5) && defined(USE_AFS) |
Damien Miller | 8f341f8 | 2004-01-21 11:00:46 +1100 | [diff] [blame] | 94 | #include <kafs.h> |
| 95 | #endif |
| 96 | |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 97 | /* Magic name for internal sftp-server */ |
| 98 | #define INTERNAL_SFTP_NAME "internal-sftp" |
| 99 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 100 | /* func */ |
| 101 | |
| 102 | Session *session_new(void); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 103 | void session_set_fds(Session *, int, int, int); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 104 | void session_pty_cleanup(Session *); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 105 | void session_proctitle(Session *); |
| 106 | int session_setup_x11fwd(Session *); |
| 107 | void do_exec_pty(Session *, const char *); |
| 108 | void do_exec_no_pty(Session *, const char *); |
| 109 | void do_exec(Session *, const char *); |
| 110 | void do_login(Session *, const char *); |
Kevin Steves | a0957d6 | 2001-09-27 19:50:26 +0000 | [diff] [blame] | 111 | #ifdef LOGIN_NEEDS_UTMPX |
| 112 | static void do_pre_login(Session *s); |
| 113 | #endif |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 114 | void do_child(Session *, const char *); |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 115 | void do_motd(void); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 116 | int check_quietlogin(Session *, const char *); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 117 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 118 | static void do_authenticated1(Authctxt *); |
| 119 | static void do_authenticated2(Authctxt *); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 120 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 121 | static int session_pty_req(Session *); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 122 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 123 | /* import */ |
| 124 | extern ServerOptions options; |
| 125 | extern char *__progname; |
| 126 | extern int log_stderr; |
| 127 | extern int debug_flag; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 128 | extern u_int utmp_len; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 129 | extern int startup_pipe; |
Ben Lindstrom | 005dd22 | 2001-04-18 15:29:33 +0000 | [diff] [blame] | 130 | extern void destroy_sensitive_data(void); |
Darren Tucker | b9aa0a0 | 2003-07-08 22:59:59 +1000 | [diff] [blame] | 131 | extern Buffer loginmsg; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 132 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 133 | /* original command from peer. */ |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 134 | const char *original_command = NULL; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 135 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 136 | /* data */ |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 137 | #define MAX_SESSIONS 20 |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 138 | Session sessions[MAX_SESSIONS]; |
Damien Miller | 15e7d4b | 2000-09-29 10:57:35 +1100 | [diff] [blame] | 139 | |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 140 | #define SUBSYSTEM_NONE 0 |
| 141 | #define SUBSYSTEM_EXT 1 |
| 142 | #define SUBSYSTEM_INT_SFTP 2 |
| 143 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 144 | #ifdef HAVE_LOGIN_CAP |
Ben Lindstrom | b481e13 | 2002-03-22 01:35:47 +0000 | [diff] [blame] | 145 | login_cap_t *lc; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 146 | #endif |
| 147 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 148 | static int is_child = 0; |
| 149 | |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 150 | /* Name and directory of socket for authentication agent forwarding. */ |
| 151 | static char *auth_sock_name = NULL; |
| 152 | static char *auth_sock_dir = NULL; |
| 153 | |
| 154 | /* removes the agent forwarding socket */ |
| 155 | |
| 156 | static void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 157 | auth_sock_cleanup_proc(struct passwd *pw) |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 158 | { |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 159 | if (auth_sock_name != NULL) { |
| 160 | temporarily_use_uid(pw); |
| 161 | unlink(auth_sock_name); |
| 162 | rmdir(auth_sock_dir); |
| 163 | auth_sock_name = NULL; |
| 164 | restore_uid(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static int |
| 169 | auth_input_request_forwarding(struct passwd * pw) |
| 170 | { |
| 171 | Channel *nc; |
| 172 | int sock; |
| 173 | struct sockaddr_un sunaddr; |
| 174 | |
| 175 | if (auth_sock_name != NULL) { |
| 176 | error("authentication forwarding requested twice."); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* Temporarily drop privileged uid for mkdir/bind. */ |
| 181 | temporarily_use_uid(pw); |
| 182 | |
| 183 | /* Allocate a buffer for the socket name, and format the name. */ |
| 184 | auth_sock_name = xmalloc(MAXPATHLEN); |
| 185 | auth_sock_dir = xmalloc(MAXPATHLEN); |
Darren Tucker | 072a7b1 | 2003-10-15 16:10:25 +1000 | [diff] [blame] | 186 | strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN); |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 187 | |
| 188 | /* Create private directory for socket */ |
| 189 | if (mkdtemp(auth_sock_dir) == NULL) { |
| 190 | packet_send_debug("Agent forwarding disabled: " |
| 191 | "mkdtemp() failed: %.100s", strerror(errno)); |
| 192 | restore_uid(); |
| 193 | xfree(auth_sock_name); |
| 194 | xfree(auth_sock_dir); |
| 195 | auth_sock_name = NULL; |
| 196 | auth_sock_dir = NULL; |
| 197 | return 0; |
| 198 | } |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 199 | snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld", |
| 200 | auth_sock_dir, (long) getpid()); |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 201 | |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 202 | /* Create the socket. */ |
| 203 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 204 | if (sock < 0) |
| 205 | packet_disconnect("socket: %.100s", strerror(errno)); |
| 206 | |
| 207 | /* Bind it to the name. */ |
| 208 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 209 | sunaddr.sun_family = AF_UNIX; |
| 210 | strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path)); |
| 211 | |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 212 | if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 213 | packet_disconnect("bind: %.100s", strerror(errno)); |
| 214 | |
| 215 | /* Restore the privileged uid. */ |
| 216 | restore_uid(); |
| 217 | |
| 218 | /* Start listening on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 219 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 220 | packet_disconnect("listen: %.100s", strerror(errno)); |
| 221 | |
| 222 | /* Allocate a channel for the authentication agent socket. */ |
| 223 | nc = channel_new("auth socket", |
| 224 | SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1, |
| 225 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 226 | 0, "auth socket", 1); |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 227 | strlcpy(nc->path, auth_sock_name, sizeof(nc->path)); |
| 228 | return 1; |
| 229 | } |
| 230 | |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 231 | static void |
| 232 | display_loginmsg(void) |
| 233 | { |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 234 | if (buffer_len(&loginmsg) > 0) { |
| 235 | buffer_append(&loginmsg, "\0", 1); |
| 236 | printf("%s", (char *)buffer_ptr(&loginmsg)); |
| 237 | buffer_clear(&loginmsg); |
| 238 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 239 | } |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 240 | |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 241 | void |
| 242 | do_authenticated(Authctxt *authctxt) |
| 243 | { |
Damien Miller | 97f39ae | 2003-02-24 11:57:01 +1100 | [diff] [blame] | 244 | setproctitle("%s", authctxt->pw->pw_name); |
| 245 | |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 246 | /* setup the channel layer */ |
| 247 | if (!no_port_forwarding_flag && options.allow_tcp_forwarding) |
| 248 | channel_permit_all_opens(); |
| 249 | |
| 250 | if (compat20) |
| 251 | do_authenticated2(authctxt); |
| 252 | else |
| 253 | do_authenticated1(authctxt); |
Ben Lindstrom | 838394c | 2001-06-09 01:11:59 +0000 | [diff] [blame] | 254 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 255 | do_cleanup(authctxt); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 258 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 259 | * Prepares for an interactive session. This is called after the user has |
| 260 | * been successfully authenticated. During this message exchange, pseudo |
| 261 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings |
| 262 | * are requested, etc. |
| 263 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 264 | static void |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 265 | do_authenticated1(Authctxt *authctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 266 | { |
| 267 | Session *s; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 268 | char *command; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 269 | int success, type, screen_flag; |
Ben Lindstrom | e23f4a3 | 2002-06-23 21:40:16 +0000 | [diff] [blame] | 270 | int enable_compression_after_reply = 0; |
| 271 | u_int proto_len, data_len, dlen, compression_level = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 272 | |
| 273 | s = session_new(); |
Darren Tucker | 172a5e8 | 2005-01-20 10:55:46 +1100 | [diff] [blame] | 274 | if (s == NULL) { |
| 275 | error("no more sessions"); |
| 276 | return; |
| 277 | } |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 278 | s->authctxt = authctxt; |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 279 | s->pw = authctxt->pw; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 280 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 281 | /* |
| 282 | * We stay in this loop until the client requests to execute a shell |
| 283 | * or a command. |
| 284 | */ |
| 285 | for (;;) { |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 286 | success = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 287 | |
| 288 | /* Get a packet from the client. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 289 | type = packet_read(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 290 | |
| 291 | /* Process the packet. */ |
| 292 | switch (type) { |
| 293 | case SSH_CMSG_REQUEST_COMPRESSION: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 294 | compression_level = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 295 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 296 | if (compression_level < 1 || compression_level > 9) { |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 297 | packet_send_debug("Received invalid compression level %d.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 298 | compression_level); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 299 | break; |
| 300 | } |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 301 | if (options.compression == COMP_NONE) { |
Ben Lindstrom | 23e0f66 | 2002-06-21 01:09:47 +0000 | [diff] [blame] | 302 | debug2("compression disabled"); |
| 303 | break; |
| 304 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 305 | /* Enable compression after we have responded with SUCCESS. */ |
| 306 | enable_compression_after_reply = 1; |
| 307 | success = 1; |
| 308 | break; |
| 309 | |
| 310 | case SSH_CMSG_REQUEST_PTY: |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 311 | success = session_pty_req(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 312 | break; |
| 313 | |
| 314 | case SSH_CMSG_X11_REQUEST_FORWARDING: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 315 | s->auth_proto = packet_get_string(&proto_len); |
| 316 | s->auth_data = packet_get_string(&data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 317 | |
Ben Lindstrom | 7603b2d | 2001-02-26 20:13:32 +0000 | [diff] [blame] | 318 | screen_flag = packet_get_protocol_flags() & |
| 319 | SSH_PROTOFLAG_SCREEN_NUMBER; |
| 320 | debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag); |
| 321 | |
| 322 | if (packet_remaining() == 4) { |
| 323 | if (!screen_flag) |
| 324 | debug2("Buggy client: " |
| 325 | "X11 screen flag missing"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 326 | s->screen = packet_get_int(); |
Ben Lindstrom | 8dcdeb8 | 2001-02-16 16:02:14 +0000 | [diff] [blame] | 327 | } else { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 328 | s->screen = 0; |
Ben Lindstrom | 8dcdeb8 | 2001-02-16 16:02:14 +0000 | [diff] [blame] | 329 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 330 | packet_check_eom(); |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 331 | success = session_setup_x11fwd(s); |
| 332 | if (!success) { |
| 333 | xfree(s->auth_proto); |
| 334 | xfree(s->auth_data); |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 335 | s->auth_proto = NULL; |
| 336 | s->auth_data = NULL; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 337 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 338 | break; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 339 | |
| 340 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: |
| 341 | if (no_agent_forwarding_flag || compat13) { |
| 342 | debug("Authentication agent forwarding not permitted for this authentication."); |
| 343 | break; |
| 344 | } |
| 345 | debug("Received authentication agent forwarding request."); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 346 | success = auth_input_request_forwarding(s->pw); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 347 | break; |
| 348 | |
| 349 | case SSH_CMSG_PORT_FORWARD_REQUEST: |
| 350 | if (no_port_forwarding_flag) { |
| 351 | debug("Port forwarding not permitted for this authentication."); |
| 352 | break; |
| 353 | } |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 354 | if (!options.allow_tcp_forwarding) { |
| 355 | debug("Port forwarding not permitted."); |
| 356 | break; |
| 357 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 358 | debug("Received TCP/IP port forwarding request."); |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 359 | if (channel_input_port_forward_request(s->pw->pw_uid == 0, |
| 360 | options.gateway_ports) < 0) { |
| 361 | debug("Port forwarding failed."); |
| 362 | break; |
| 363 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 364 | success = 1; |
| 365 | break; |
| 366 | |
| 367 | case SSH_CMSG_MAX_PACKET_SIZE: |
| 368 | if (packet_set_maxsize(packet_get_int()) > 0) |
| 369 | success = 1; |
| 370 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 371 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 372 | case SSH_CMSG_EXEC_SHELL: |
| 373 | case SSH_CMSG_EXEC_CMD: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 374 | if (type == SSH_CMSG_EXEC_CMD) { |
| 375 | command = packet_get_string(&dlen); |
| 376 | debug("Exec command '%.500s'", command); |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 377 | do_exec(s, command); |
| 378 | xfree(command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 379 | } else { |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 380 | do_exec(s, NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 381 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 382 | packet_check_eom(); |
Ben Lindstrom | cb3929d | 2001-06-09 01:34:15 +0000 | [diff] [blame] | 383 | session_close(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 384 | return; |
| 385 | |
| 386 | default: |
| 387 | /* |
| 388 | * Any unknown messages in this phase are ignored, |
| 389 | * and a failure message is returned. |
| 390 | */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 391 | logit("Unknown packet type received after authentication: %d", type); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 392 | } |
| 393 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); |
| 394 | packet_send(); |
| 395 | packet_write_wait(); |
| 396 | |
| 397 | /* Enable compression now that we have replied if appropriate. */ |
| 398 | if (enable_compression_after_reply) { |
| 399 | enable_compression_after_reply = 0; |
| 400 | packet_start_compression(compression_level); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * This is called to fork and execute a command when we have no tty. This |
| 407 | * will call do_child from the child, and server_loop from the parent after |
| 408 | * setting up file descriptors and such. |
| 409 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 410 | void |
Ben Lindstrom | b4c961d | 2001-03-22 01:25:37 +0000 | [diff] [blame] | 411 | do_exec_no_pty(Session *s, const char *command) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 412 | { |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 413 | pid_t pid; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 414 | |
| 415 | #ifdef USE_PIPES |
| 416 | int pin[2], pout[2], perr[2]; |
| 417 | /* Allocate pipes for communicating with the program. */ |
| 418 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) |
| 419 | packet_disconnect("Could not create pipes: %.100s", |
| 420 | strerror(errno)); |
| 421 | #else /* USE_PIPES */ |
| 422 | int inout[2], err[2]; |
| 423 | /* Uses socket pairs to communicate with the program. */ |
| 424 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || |
| 425 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) |
| 426 | packet_disconnect("Could not create socket pairs: %.100s", |
| 427 | strerror(errno)); |
| 428 | #endif /* USE_PIPES */ |
| 429 | if (s == NULL) |
| 430 | fatal("do_exec_no_pty: no session"); |
| 431 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 432 | session_proctitle(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 433 | |
Damien Miller | c594633 | 2001-02-28 11:46:11 +1100 | [diff] [blame] | 434 | #if defined(USE_PAM) |
Darren Tucker | 1825f26 | 2004-02-24 00:01:27 +1100 | [diff] [blame] | 435 | if (options.use_pam && !use_privsep) |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 436 | do_pam_setcred(1); |
Kevin Steves | ff793a2 | 2001-02-21 16:36:51 +0000 | [diff] [blame] | 437 | #endif /* USE_PAM */ |
| 438 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 439 | /* Fork the child. */ |
| 440 | if ((pid = fork()) == 0) { |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 441 | is_child = 1; |
Ben Lindstrom | 264ee30 | 2002-07-23 21:01:56 +0000 | [diff] [blame] | 442 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 443 | /* Child. Reinitialize the log since the pid has changed. */ |
| 444 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
| 445 | |
| 446 | /* |
| 447 | * Create a new session and process group since the 4.4BSD |
| 448 | * setlogin() affects the entire process group. |
| 449 | */ |
| 450 | if (setsid() < 0) |
| 451 | error("setsid failed: %.100s", strerror(errno)); |
| 452 | |
| 453 | #ifdef USE_PIPES |
| 454 | /* |
| 455 | * Redirect stdin. We close the parent side of the socket |
| 456 | * pair, and make the child side the standard input. |
| 457 | */ |
| 458 | close(pin[1]); |
| 459 | if (dup2(pin[0], 0) < 0) |
| 460 | perror("dup2 stdin"); |
| 461 | close(pin[0]); |
| 462 | |
| 463 | /* Redirect stdout. */ |
| 464 | close(pout[0]); |
| 465 | if (dup2(pout[1], 1) < 0) |
| 466 | perror("dup2 stdout"); |
| 467 | close(pout[1]); |
| 468 | |
| 469 | /* Redirect stderr. */ |
| 470 | close(perr[0]); |
| 471 | if (dup2(perr[1], 2) < 0) |
| 472 | perror("dup2 stderr"); |
| 473 | close(perr[1]); |
| 474 | #else /* USE_PIPES */ |
| 475 | /* |
| 476 | * Redirect stdin, stdout, and stderr. Stdin and stdout will |
| 477 | * use the same socket, as some programs (particularly rdist) |
| 478 | * seem to depend on it. |
| 479 | */ |
| 480 | close(inout[1]); |
| 481 | close(err[1]); |
| 482 | if (dup2(inout[0], 0) < 0) /* stdin */ |
| 483 | perror("dup2 stdin"); |
| 484 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ |
| 485 | perror("dup2 stdout"); |
| 486 | if (dup2(err[0], 2) < 0) /* stderr */ |
| 487 | perror("dup2 stderr"); |
| 488 | #endif /* USE_PIPES */ |
| 489 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 490 | #ifdef _UNICOS |
| 491 | cray_init_job(s->pw); /* set up cray jid and tmpdir */ |
| 492 | #endif |
| 493 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 494 | /* Do processing for the child (exec command etc). */ |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 495 | do_child(s, command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 496 | /* NOTREACHED */ |
| 497 | } |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 498 | #ifdef _UNICOS |
| 499 | signal(WJSIGNAL, cray_job_termination_handler); |
| 500 | #endif /* _UNICOS */ |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 501 | #ifdef HAVE_CYGWIN |
| 502 | if (is_winnt) |
| 503 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); |
| 504 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 505 | if (pid < 0) |
| 506 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 507 | s->pid = pid; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 508 | /* Set interactive/non-interactive mode. */ |
| 509 | packet_set_interactive(s->display != NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 510 | #ifdef USE_PIPES |
| 511 | /* We are the parent. Close the child sides of the pipes. */ |
| 512 | close(pin[0]); |
| 513 | close(pout[1]); |
| 514 | close(perr[1]); |
| 515 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 516 | if (compat20) { |
Darren Tucker | 723e945 | 2004-06-22 12:57:08 +1000 | [diff] [blame] | 517 | if (s->is_subsystem) { |
| 518 | close(perr[0]); |
| 519 | perr[0] = -1; |
| 520 | } |
| 521 | session_set_fds(s, pin[1], pout[0], perr[0]); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 522 | } else { |
| 523 | /* Enter the interactive session. */ |
| 524 | server_loop(pid, pin[1], pout[0], perr[0]); |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 525 | /* server_loop has closed pin[1], pout[0], and perr[0]. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 526 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 527 | #else /* USE_PIPES */ |
| 528 | /* We are the parent. Close the child sides of the socket pairs. */ |
| 529 | close(inout[0]); |
| 530 | close(err[0]); |
| 531 | |
| 532 | /* |
Darren Tucker | b385059 | 2004-03-27 16:44:21 +1100 | [diff] [blame] | 533 | * Clear loginmsg, since it's the child's responsibility to display |
| 534 | * it to the user, otherwise multiple sessions may accumulate |
| 535 | * multiple copies of the login messages. |
| 536 | */ |
| 537 | buffer_clear(&loginmsg); |
| 538 | |
| 539 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 540 | * Enter the interactive session. Note: server_loop must be able to |
| 541 | * handle the case that fdin and fdout are the same. |
| 542 | */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 543 | if (compat20) { |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 544 | session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 545 | } else { |
| 546 | server_loop(pid, inout[1], inout[1], err[1]); |
| 547 | /* server_loop has closed inout[1] and err[1]. */ |
| 548 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 549 | #endif /* USE_PIPES */ |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * This is called to fork and execute a command when we have a tty. This |
| 554 | * will call do_child from the child, and server_loop from the parent after |
| 555 | * setting up file descriptors, controlling tty, updating wtmp, utmp, |
| 556 | * lastlog, and other such operations. |
| 557 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 558 | void |
Ben Lindstrom | b4c961d | 2001-03-22 01:25:37 +0000 | [diff] [blame] | 559 | do_exec_pty(Session *s, const char *command) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 560 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 561 | int fdout, ptyfd, ttyfd, ptymaster; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 562 | pid_t pid; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 563 | |
| 564 | if (s == NULL) |
| 565 | fatal("do_exec_pty: no session"); |
| 566 | ptyfd = s->ptyfd; |
| 567 | ttyfd = s->ttyfd; |
| 568 | |
Damien Miller | c594633 | 2001-02-28 11:46:11 +1100 | [diff] [blame] | 569 | #if defined(USE_PAM) |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 570 | if (options.use_pam) { |
Damien Miller | 341c6e6 | 2003-09-02 23:18:52 +1000 | [diff] [blame] | 571 | do_pam_set_tty(s->tty); |
Darren Tucker | ef3a4a2 | 2004-02-06 15:30:50 +1100 | [diff] [blame] | 572 | if (!use_privsep) |
| 573 | do_pam_setcred(1); |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 574 | } |
Damien Miller | 5a76131 | 2001-02-27 09:28:23 +1100 | [diff] [blame] | 575 | #endif |
Kevin Steves | ff793a2 | 2001-02-21 16:36:51 +0000 | [diff] [blame] | 576 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 577 | /* Fork the child. */ |
| 578 | if ((pid = fork()) == 0) { |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 579 | is_child = 1; |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 580 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 581 | /* Child. Reinitialize the log because the pid has changed. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 582 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 583 | /* Close the master side of the pseudo tty. */ |
| 584 | close(ptyfd); |
| 585 | |
| 586 | /* Make the pseudo tty our controlling tty. */ |
| 587 | pty_make_controlling_tty(&ttyfd, s->tty); |
| 588 | |
Damien Miller | 9c75142 | 2001-10-10 15:02:46 +1000 | [diff] [blame] | 589 | /* Redirect stdin/stdout/stderr from the pseudo tty. */ |
| 590 | if (dup2(ttyfd, 0) < 0) |
| 591 | error("dup2 stdin: %s", strerror(errno)); |
| 592 | if (dup2(ttyfd, 1) < 0) |
| 593 | error("dup2 stdout: %s", strerror(errno)); |
| 594 | if (dup2(ttyfd, 2) < 0) |
| 595 | error("dup2 stderr: %s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 596 | |
| 597 | /* Close the extra descriptor for the pseudo tty. */ |
| 598 | close(ttyfd); |
| 599 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 600 | /* record login, etc. similar to login(1) */ |
Damien Miller | 364a9bd | 2001-04-16 18:37:05 +1000 | [diff] [blame] | 601 | #ifndef HAVE_OSF_SIA |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 602 | if (!(options.use_login && command == NULL)) { |
| 603 | #ifdef _UNICOS |
| 604 | cray_init_job(s->pw); /* set up cray jid and tmpdir */ |
| 605 | #endif /* _UNICOS */ |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 606 | do_login(s, command); |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 607 | } |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 608 | # ifdef LOGIN_NEEDS_UTMPX |
| 609 | else |
| 610 | do_pre_login(s); |
| 611 | # endif |
Damien Miller | 364a9bd | 2001-04-16 18:37:05 +1000 | [diff] [blame] | 612 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 613 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 614 | /* Do common processing for the child, such as execing the command. */ |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 615 | do_child(s, command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 616 | /* NOTREACHED */ |
| 617 | } |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 618 | #ifdef _UNICOS |
| 619 | signal(WJSIGNAL, cray_job_termination_handler); |
| 620 | #endif /* _UNICOS */ |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 621 | #ifdef HAVE_CYGWIN |
| 622 | if (is_winnt) |
| 623 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); |
| 624 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 625 | if (pid < 0) |
| 626 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 627 | s->pid = pid; |
| 628 | |
| 629 | /* Parent. Close the slave side of the pseudo tty. */ |
| 630 | close(ttyfd); |
| 631 | |
| 632 | /* |
| 633 | * Create another descriptor of the pty master side for use as the |
| 634 | * standard input. We could use the original descriptor, but this |
| 635 | * simplifies code in server_loop. The descriptor is bidirectional. |
| 636 | */ |
| 637 | fdout = dup(ptyfd); |
| 638 | if (fdout < 0) |
| 639 | packet_disconnect("dup #1 failed: %.100s", strerror(errno)); |
| 640 | |
| 641 | /* we keep a reference to the pty master */ |
| 642 | ptymaster = dup(ptyfd); |
| 643 | if (ptymaster < 0) |
| 644 | packet_disconnect("dup #2 failed: %.100s", strerror(errno)); |
| 645 | s->ptymaster = ptymaster; |
| 646 | |
| 647 | /* Enter interactive session. */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 648 | packet_set_interactive(1); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 649 | if (compat20) { |
| 650 | session_set_fds(s, ptyfd, fdout, -1); |
| 651 | } else { |
| 652 | server_loop(pid, ptyfd, fdout, -1); |
| 653 | /* server_loop _has_ closed ptyfd and fdout. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 654 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 655 | } |
| 656 | |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 657 | #ifdef LOGIN_NEEDS_UTMPX |
Damien Miller | 599d8eb | 2001-09-15 12:25:53 +1000 | [diff] [blame] | 658 | static void |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 659 | do_pre_login(Session *s) |
| 660 | { |
| 661 | socklen_t fromlen; |
| 662 | struct sockaddr_storage from; |
| 663 | pid_t pid = getpid(); |
| 664 | |
| 665 | /* |
| 666 | * Get IP address of client. If the connection is not a socket, let |
| 667 | * the address be 0.0.0.0. |
| 668 | */ |
| 669 | memset(&from, 0, sizeof(from)); |
Damien Miller | ebc2306 | 2002-09-04 16:45:09 +1000 | [diff] [blame] | 670 | fromlen = sizeof(from); |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 671 | if (packet_connection_is_on_socket()) { |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 672 | if (getpeername(packet_get_connection_in(), |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 673 | (struct sockaddr *)&from, &fromlen) < 0) { |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 674 | debug("getpeername: %.100s", strerror(errno)); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 675 | cleanup_exit(255); |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | record_utmp_only(pid, s->tty, s->pw->pw_name, |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 680 | get_remote_name_or_ip(utmp_len, options.use_dns), |
Kevin Steves | 678ee51 | 2003-01-01 23:43:55 +0000 | [diff] [blame] | 681 | (struct sockaddr *)&from, fromlen); |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 682 | } |
| 683 | #endif |
| 684 | |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 685 | /* |
| 686 | * This is called to fork and execute a command. If another command is |
| 687 | * to be forced, execute that instead. |
| 688 | */ |
| 689 | void |
| 690 | do_exec(Session *s, const char *command) |
| 691 | { |
Damien Miller | e275443 | 2006-07-24 14:06:47 +1000 | [diff] [blame] | 692 | if (options.adm_forced_command) { |
| 693 | original_command = command; |
| 694 | command = options.adm_forced_command; |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 695 | if (strcmp(INTERNAL_SFTP_NAME, command) == 0) |
| 696 | s->is_subsystem = SUBSYSTEM_INT_SFTP; |
| 697 | else if (s->is_subsystem) |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 698 | s->is_subsystem = SUBSYSTEM_EXT; |
Damien Miller | e275443 | 2006-07-24 14:06:47 +1000 | [diff] [blame] | 699 | debug("Forced command (config) '%.900s'", command); |
| 700 | } else if (forced_command) { |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 701 | original_command = command; |
| 702 | command = forced_command; |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 703 | if (strcmp(INTERNAL_SFTP_NAME, command) == 0) |
| 704 | s->is_subsystem = SUBSYSTEM_INT_SFTP; |
| 705 | else if (s->is_subsystem) |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 706 | s->is_subsystem = SUBSYSTEM_EXT; |
Damien Miller | e275443 | 2006-07-24 14:06:47 +1000 | [diff] [blame] | 707 | debug("Forced command (key option) '%.900s'", command); |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 710 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 711 | if (command != NULL) |
| 712 | PRIVSEP(audit_run_command(command)); |
| 713 | else if (s->ttyfd == -1) { |
| 714 | char *shell = s->pw->pw_shell; |
| 715 | |
| 716 | if (shell[0] == '\0') /* empty shell means /bin/sh */ |
| 717 | shell =_PATH_BSHELL; |
| 718 | PRIVSEP(audit_run_command(shell)); |
| 719 | } |
| 720 | #endif |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 721 | if (s->ttyfd != -1) |
| 722 | do_exec_pty(s, command); |
| 723 | else |
| 724 | do_exec_no_pty(s, command); |
| 725 | |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 726 | original_command = NULL; |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 727 | |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 728 | /* |
| 729 | * Clear loginmsg: it's the child's responsibility to display |
| 730 | * it to the user, otherwise multiple sessions may accumulate |
| 731 | * multiple copies of the login messages. |
| 732 | */ |
| 733 | buffer_clear(&loginmsg); |
| 734 | } |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 735 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 736 | /* administrative, login(1)-like work */ |
| 737 | void |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 738 | do_login(Session *s, const char *command) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 739 | { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 740 | socklen_t fromlen; |
| 741 | struct sockaddr_storage from; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 742 | struct passwd * pw = s->pw; |
| 743 | pid_t pid = getpid(); |
| 744 | |
| 745 | /* |
| 746 | * Get IP address of client. If the connection is not a socket, let |
| 747 | * the address be 0.0.0.0. |
| 748 | */ |
| 749 | memset(&from, 0, sizeof(from)); |
Kevin Steves | 678ee51 | 2003-01-01 23:43:55 +0000 | [diff] [blame] | 750 | fromlen = sizeof(from); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 751 | if (packet_connection_is_on_socket()) { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 752 | if (getpeername(packet_get_connection_in(), |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 753 | (struct sockaddr *) & from, &fromlen) < 0) { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 754 | debug("getpeername: %.100s", strerror(errno)); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 755 | cleanup_exit(255); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | /* Record that there was a login on that tty from the remote host. */ |
Ben Lindstrom | 2bf56e2 | 2002-04-02 20:32:46 +0000 | [diff] [blame] | 760 | if (!use_privsep) |
| 761 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, |
| 762 | get_remote_name_or_ip(utmp_len, |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 763 | options.use_dns), |
Damien Miller | ebc2306 | 2002-09-04 16:45:09 +1000 | [diff] [blame] | 764 | (struct sockaddr *)&from, fromlen); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 765 | |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 766 | #ifdef USE_PAM |
| 767 | /* |
| 768 | * If password change is needed, do it now. |
| 769 | * This needs to occur before the ~/.hushlogin check. |
| 770 | */ |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 771 | if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) { |
| 772 | display_loginmsg(); |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 773 | do_pam_chauthtok(); |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 774 | s->authctxt->force_pwchange = 0; |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 775 | /* XXX - signal [net] parent to enable forwardings */ |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 776 | } |
| 777 | #endif |
| 778 | |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 779 | if (check_quietlogin(s, command)) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 780 | return; |
| 781 | |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 782 | display_loginmsg(); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 783 | |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 784 | do_motd(); |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Display the message of the day. |
| 789 | */ |
| 790 | void |
| 791 | do_motd(void) |
| 792 | { |
| 793 | FILE *f; |
| 794 | char buf[256]; |
| 795 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 796 | if (options.print_motd) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 797 | #ifdef HAVE_LOGIN_CAP |
| 798 | f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", |
| 799 | "/etc/motd"), "r"); |
| 800 | #else |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 801 | f = fopen("/etc/motd", "r"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 802 | #endif |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 803 | if (f) { |
| 804 | while (fgets(buf, sizeof(buf), f)) |
| 805 | fputs(buf, stdout); |
| 806 | fclose(f); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 811 | |
| 812 | /* |
| 813 | * Check for quiet login, either .hushlogin or command given. |
| 814 | */ |
| 815 | int |
| 816 | check_quietlogin(Session *s, const char *command) |
| 817 | { |
| 818 | char buf[256]; |
| 819 | struct passwd *pw = s->pw; |
| 820 | struct stat st; |
| 821 | |
| 822 | /* Return 1 if .hushlogin exists or a command given. */ |
| 823 | if (command != NULL) |
| 824 | return 1; |
| 825 | snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); |
| 826 | #ifdef HAVE_LOGIN_CAP |
| 827 | if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) |
| 828 | return 1; |
| 829 | #else |
| 830 | if (stat(buf, &st) >= 0) |
| 831 | return 1; |
| 832 | #endif |
| 833 | return 0; |
| 834 | } |
| 835 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 836 | /* |
| 837 | * Sets the value of the given variable in the environment. If the variable |
| 838 | * already exists, its value is overriden. |
| 839 | */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 840 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 841 | child_set_env(char ***envp, u_int *envsizep, const char *name, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 842 | const char *value) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 843 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 844 | char **env; |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 845 | u_int envsize; |
| 846 | u_int i, namelen; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 847 | |
| 848 | /* |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 849 | * If we're passed an uninitialized list, allocate a single null |
| 850 | * entry before continuing. |
| 851 | */ |
| 852 | if (*envp == NULL && *envsizep == 0) { |
| 853 | *envp = xmalloc(sizeof(char *)); |
| 854 | *envp[0] = NULL; |
| 855 | *envsizep = 1; |
| 856 | } |
| 857 | |
| 858 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 859 | * Find the slot where the value should be stored. If the variable |
| 860 | * already exists, we reuse the slot; otherwise we append a new slot |
| 861 | * at the end of the array, expanding if necessary. |
| 862 | */ |
| 863 | env = *envp; |
| 864 | namelen = strlen(name); |
| 865 | for (i = 0; env[i]; i++) |
| 866 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') |
| 867 | break; |
| 868 | if (env[i]) { |
| 869 | /* Reuse the slot. */ |
| 870 | xfree(env[i]); |
| 871 | } else { |
| 872 | /* New variable. Expand if necessary. */ |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 873 | envsize = *envsizep; |
| 874 | if (i >= envsize - 1) { |
| 875 | if (envsize >= 1000) |
| 876 | fatal("child_set_env: too many env vars"); |
| 877 | envsize += 50; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 878 | env = (*envp) = xrealloc(env, envsize, sizeof(char *)); |
Darren Tucker | fb16b24 | 2003-09-22 21:04:23 +1000 | [diff] [blame] | 879 | *envsizep = envsize; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 880 | } |
| 881 | /* Need to set the NULL pointer at end of array beyond the new slot. */ |
| 882 | env[i + 1] = NULL; |
| 883 | } |
| 884 | |
| 885 | /* Allocate space and format the variable in the appropriate slot. */ |
| 886 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); |
| 887 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Reads environment variables from the given file and adds/overrides them |
| 892 | * into the environment. If the file does not exist, this does nothing. |
| 893 | * Otherwise, it must consist of empty lines, comments (line starts with '#') |
| 894 | * and assignments of the form name=value. No other forms are allowed. |
| 895 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 896 | static void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 897 | read_environment_file(char ***env, u_int *envsize, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 898 | const char *filename) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 899 | { |
| 900 | FILE *f; |
| 901 | char buf[4096]; |
| 902 | char *cp, *value; |
Damien Miller | 990070a | 2002-06-26 23:51:06 +1000 | [diff] [blame] | 903 | u_int lineno = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 904 | |
| 905 | f = fopen(filename, "r"); |
| 906 | if (!f) |
| 907 | return; |
| 908 | |
| 909 | while (fgets(buf, sizeof(buf), f)) { |
Damien Miller | 990070a | 2002-06-26 23:51:06 +1000 | [diff] [blame] | 910 | if (++lineno > 1000) |
| 911 | fatal("Too many lines in environment file %s", filename); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 912 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
| 913 | ; |
| 914 | if (!*cp || *cp == '#' || *cp == '\n') |
| 915 | continue; |
Damien Miller | 14b017d | 2007-09-17 16:09:15 +1000 | [diff] [blame] | 916 | |
| 917 | cp[strcspn(cp, "\n")] = '\0'; |
| 918 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 919 | value = strchr(cp, '='); |
| 920 | if (value == NULL) { |
Damien Miller | 990070a | 2002-06-26 23:51:06 +1000 | [diff] [blame] | 921 | fprintf(stderr, "Bad line %u in %.100s\n", lineno, |
| 922 | filename); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 923 | continue; |
| 924 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 925 | /* |
| 926 | * Replace the equals sign by nul, and advance value to |
| 927 | * the value string. |
| 928 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 929 | *value = '\0'; |
| 930 | value++; |
| 931 | child_set_env(env, envsize, cp, value); |
| 932 | } |
| 933 | fclose(f); |
| 934 | } |
| 935 | |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 936 | #ifdef HAVE_ETC_DEFAULT_LOGIN |
| 937 | /* |
| 938 | * Return named variable from specified environment, or NULL if not present. |
| 939 | */ |
| 940 | static char * |
| 941 | child_get_env(char **env, const char *name) |
| 942 | { |
| 943 | int i; |
| 944 | size_t len; |
| 945 | |
| 946 | len = strlen(name); |
| 947 | for (i=0; env[i] != NULL; i++) |
| 948 | if (strncmp(name, env[i], len) == 0 && env[i][len] == '=') |
| 949 | return(env[i] + len + 1); |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Read /etc/default/login. |
| 955 | * We pick up the PATH (or SUPATH for root) and UMASK. |
| 956 | */ |
| 957 | static void |
| 958 | read_etc_default_login(char ***env, u_int *envsize, uid_t uid) |
| 959 | { |
| 960 | char **tmpenv = NULL, *var; |
Darren Tucker | c11b1e8 | 2003-09-19 20:56:51 +1000 | [diff] [blame] | 961 | u_int i, tmpenvsize = 0; |
Darren Tucker | f391ba6 | 2003-10-02 20:07:09 +1000 | [diff] [blame] | 962 | u_long mask; |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 963 | |
| 964 | /* |
| 965 | * We don't want to copy the whole file to the child's environment, |
| 966 | * so we use a temporary environment and copy the variables we're |
| 967 | * interested in. |
| 968 | */ |
| 969 | read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login"); |
| 970 | |
Darren Tucker | c11b1e8 | 2003-09-19 20:56:51 +1000 | [diff] [blame] | 971 | if (tmpenv == NULL) |
| 972 | return; |
| 973 | |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 974 | if (uid == 0) |
| 975 | var = child_get_env(tmpenv, "SUPATH"); |
| 976 | else |
| 977 | var = child_get_env(tmpenv, "PATH"); |
| 978 | if (var != NULL) |
| 979 | child_set_env(env, envsize, "PATH", var); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 980 | |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 981 | if ((var = child_get_env(tmpenv, "UMASK")) != NULL) |
| 982 | if (sscanf(var, "%5lo", &mask) == 1) |
Darren Tucker | f391ba6 | 2003-10-02 20:07:09 +1000 | [diff] [blame] | 983 | umask((mode_t)mask); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 984 | |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 985 | for (i = 0; tmpenv[i] != NULL; i++) |
| 986 | xfree(tmpenv[i]); |
| 987 | xfree(tmpenv); |
| 988 | } |
| 989 | #endif /* HAVE_ETC_DEFAULT_LOGIN */ |
| 990 | |
Damien Miller | 7dff869 | 2005-05-26 11:34:51 +1000 | [diff] [blame] | 991 | void |
| 992 | copy_environment(char **source, char ***env, u_int *envsize) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 993 | { |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 994 | char *var_name, *var_val; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 995 | int i; |
| 996 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 997 | if (source == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 998 | return; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 999 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 1000 | for(i = 0; source[i] != NULL; i++) { |
| 1001 | var_name = xstrdup(source[i]); |
| 1002 | if ((var_val = strstr(var_name, "=")) == NULL) { |
| 1003 | xfree(var_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1004 | continue; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1005 | } |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 1006 | *var_val++ = '\0'; |
| 1007 | |
| 1008 | debug3("Copy environment: %s=%s", var_name, var_val); |
| 1009 | child_set_env(env, envsize, var_name, var_val); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1010 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 1011 | xfree(var_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1012 | } |
| 1013 | } |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1014 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1015 | static char ** |
| 1016 | do_setup_env(Session *s, const char *shell) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1017 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1018 | char buf[256]; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1019 | u_int i, envsize; |
Damien Miller | ad5ecbf | 2006-07-24 15:03:06 +1000 | [diff] [blame] | 1020 | char **env, *laddr; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1021 | struct passwd *pw = s->pw; |
Damien Miller | ad5ecbf | 2006-07-24 15:03:06 +1000 | [diff] [blame] | 1022 | #ifndef HAVE_LOGIN_CAP |
| 1023 | char *path = NULL; |
| 1024 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1025 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1026 | /* Initialize the environment. */ |
| 1027 | envsize = 100; |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 1028 | env = xcalloc(envsize, sizeof(char *)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1029 | env[0] = NULL; |
| 1030 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1031 | #ifdef HAVE_CYGWIN |
| 1032 | /* |
| 1033 | * The Windows environment contains some setting which are |
| 1034 | * important for a running system. They must not be dropped. |
| 1035 | */ |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 1036 | { |
| 1037 | char **p; |
| 1038 | |
| 1039 | p = fetch_windows_environment(); |
| 1040 | copy_environment(p, &env, &envsize); |
| 1041 | free_windows_environment(p); |
| 1042 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1043 | #endif |
| 1044 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1045 | #ifdef GSSAPI |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1046 | /* Allow any GSSAPI methods that we've used to alter |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1047 | * the childs environment as they see fit |
| 1048 | */ |
| 1049 | ssh_gssapi_do_child(&env, &envsize); |
| 1050 | #endif |
| 1051 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1052 | if (!options.use_login) { |
| 1053 | /* Set basic environment. */ |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1054 | for (i = 0; i < s->num_env; i++) |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1055 | child_set_env(&env, &envsize, s->env[i].name, |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 1056 | s->env[i].val); |
| 1057 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1058 | child_set_env(&env, &envsize, "USER", pw->pw_name); |
| 1059 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); |
Damien Miller | dfedbf8 | 2003-01-03 14:52:53 +1100 | [diff] [blame] | 1060 | #ifdef _AIX |
| 1061 | child_set_env(&env, &envsize, "LOGIN", pw->pw_name); |
| 1062 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1063 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1064 | #ifdef HAVE_LOGIN_CAP |
Ben Lindstrom | b9051ec | 2002-07-23 21:11:09 +0000 | [diff] [blame] | 1065 | if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0) |
| 1066 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); |
| 1067 | else |
| 1068 | child_set_env(&env, &envsize, "PATH", getenv("PATH")); |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1069 | #else /* HAVE_LOGIN_CAP */ |
| 1070 | # ifndef HAVE_CYGWIN |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1071 | /* |
| 1072 | * There's no standard path on Windows. The path contains |
| 1073 | * important components pointing to the system directories, |
| 1074 | * needed for loading shared libraries. So the path better |
| 1075 | * remains intact here. |
| 1076 | */ |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 1077 | # ifdef HAVE_ETC_DEFAULT_LOGIN |
| 1078 | read_etc_default_login(&env, &envsize, pw->pw_uid); |
| 1079 | path = child_get_env(env, "PATH"); |
| 1080 | # endif /* HAVE_ETC_DEFAULT_LOGIN */ |
| 1081 | if (path == NULL || *path == '\0') { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1082 | child_set_env(&env, &envsize, "PATH", |
Darren Tucker | e1a790d | 2003-09-16 11:52:19 +1000 | [diff] [blame] | 1083 | s->pw->pw_uid == 0 ? |
| 1084 | SUPERUSER_PATH : _PATH_STDPATH); |
| 1085 | } |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1086 | # endif /* HAVE_CYGWIN */ |
| 1087 | #endif /* HAVE_LOGIN_CAP */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1088 | |
| 1089 | snprintf(buf, sizeof buf, "%.200s/%.50s", |
| 1090 | _PATH_MAILDIR, pw->pw_name); |
| 1091 | child_set_env(&env, &envsize, "MAIL", buf); |
| 1092 | |
| 1093 | /* Normal systems set SHELL by default. */ |
| 1094 | child_set_env(&env, &envsize, "SHELL", shell); |
| 1095 | } |
| 1096 | if (getenv("TZ")) |
| 1097 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); |
| 1098 | |
| 1099 | /* Set custom environment options from RSA authentication. */ |
Ben Lindstrom | 38b951c | 2001-12-06 17:47:47 +0000 | [diff] [blame] | 1100 | if (!options.use_login) { |
| 1101 | while (custom_environment) { |
| 1102 | struct envstring *ce = custom_environment; |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 1103 | char *str = ce->s; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1104 | |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 1105 | for (i = 0; str[i] != '=' && str[i]; i++) |
Ben Lindstrom | 38b951c | 2001-12-06 17:47:47 +0000 | [diff] [blame] | 1106 | ; |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 1107 | if (str[i] == '=') { |
| 1108 | str[i] = 0; |
| 1109 | child_set_env(&env, &envsize, str, str + i + 1); |
Ben Lindstrom | 38b951c | 2001-12-06 17:47:47 +0000 | [diff] [blame] | 1110 | } |
| 1111 | custom_environment = ce->next; |
| 1112 | xfree(ce->s); |
| 1113 | xfree(ce); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1114 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1115 | } |
| 1116 | |
Damien Miller | f37e246 | 2002-09-19 11:47:55 +1000 | [diff] [blame] | 1117 | /* SSH_CLIENT deprecated */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1118 | snprintf(buf, sizeof buf, "%.50s %d %d", |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1119 | get_remote_ipaddr(), get_remote_port(), get_local_port()); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1120 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); |
| 1121 | |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1122 | laddr = get_local_ipaddr(packet_get_connection_in()); |
Damien Miller | f37e246 | 2002-09-19 11:47:55 +1000 | [diff] [blame] | 1123 | snprintf(buf, sizeof buf, "%.50s %d %.50s %d", |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1124 | get_remote_ipaddr(), get_remote_port(), laddr, get_local_port()); |
| 1125 | xfree(laddr); |
Damien Miller | f37e246 | 2002-09-19 11:47:55 +1000 | [diff] [blame] | 1126 | child_set_env(&env, &envsize, "SSH_CONNECTION", buf); |
| 1127 | |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1128 | if (s->ttyfd != -1) |
| 1129 | child_set_env(&env, &envsize, "SSH_TTY", s->tty); |
| 1130 | if (s->term) |
| 1131 | child_set_env(&env, &envsize, "TERM", s->term); |
| 1132 | if (s->display) |
| 1133 | child_set_env(&env, &envsize, "DISPLAY", s->display); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1134 | if (original_command) |
| 1135 | child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", |
| 1136 | original_command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1137 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 1138 | #ifdef _UNICOS |
| 1139 | if (cray_tmpdir[0] != '\0') |
| 1140 | child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir); |
| 1141 | #endif /* _UNICOS */ |
| 1142 | |
Darren Tucker | 9dc6c7d | 2005-02-02 18:30:33 +1100 | [diff] [blame] | 1143 | /* |
| 1144 | * Since we clear KRB5CCNAME at startup, if it's set now then it |
| 1145 | * must have been set by a native authentication method (eg AIX or |
| 1146 | * SIA), so copy it to the child. |
| 1147 | */ |
| 1148 | { |
| 1149 | char *cp; |
| 1150 | |
| 1151 | if ((cp = getenv("KRB5CCNAME")) != NULL) |
| 1152 | child_set_env(&env, &envsize, "KRB5CCNAME", cp); |
| 1153 | } |
| 1154 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1155 | #ifdef _AIX |
Ben Lindstrom | 839ac4f | 2002-02-24 20:42:46 +0000 | [diff] [blame] | 1156 | { |
| 1157 | char *cp; |
| 1158 | |
| 1159 | if ((cp = getenv("AUTHSTATE")) != NULL) |
| 1160 | child_set_env(&env, &envsize, "AUTHSTATE", cp); |
Ben Lindstrom | 839ac4f | 2002-02-24 20:42:46 +0000 | [diff] [blame] | 1161 | read_environment_file(&env, &envsize, "/etc/environment"); |
| 1162 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1163 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1164 | #ifdef KRB5 |
Damien Miller | 9c870f9 | 2004-04-16 22:47:55 +1000 | [diff] [blame] | 1165 | if (s->authctxt->krb5_ccname) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1166 | child_set_env(&env, &envsize, "KRB5CCNAME", |
Damien Miller | 9c870f9 | 2004-04-16 22:47:55 +1000 | [diff] [blame] | 1167 | s->authctxt->krb5_ccname); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1168 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1169 | #ifdef USE_PAM |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 1170 | /* |
| 1171 | * Pull in any environment variables that may have |
| 1172 | * been set by PAM. |
| 1173 | */ |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 1174 | if (options.use_pam) { |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 1175 | char **p; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1176 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 1177 | p = fetch_pam_child_environment(); |
| 1178 | copy_environment(p, &env, &envsize); |
| 1179 | free_pam_environment(p); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 1180 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 1181 | p = fetch_pam_environment(); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 1182 | copy_environment(p, &env, &envsize); |
| 1183 | free_pam_environment(p); |
| 1184 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1185 | #endif /* USE_PAM */ |
| 1186 | |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 1187 | if (auth_sock_name != NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1188 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, |
Ben Lindstrom | 8bb6f36 | 2002-06-11 15:59:02 +0000 | [diff] [blame] | 1189 | auth_sock_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1190 | |
| 1191 | /* read $HOME/.ssh/environment. */ |
Ben Lindstrom | 5d860f0 | 2002-08-01 01:28:38 +0000 | [diff] [blame] | 1192 | if (options.permit_user_env && !options.use_login) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1193 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", |
Damien Miller | e9994cb | 2002-09-10 21:43:53 +1000 | [diff] [blame] | 1194 | strcmp(pw->pw_dir, "/") ? pw->pw_dir : ""); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1195 | read_environment_file(&env, &envsize, buf); |
| 1196 | } |
| 1197 | if (debug_flag) { |
| 1198 | /* dump the environment */ |
| 1199 | fprintf(stderr, "Environment:\n"); |
| 1200 | for (i = 0; env[i]; i++) |
| 1201 | fprintf(stderr, " %.200s\n", env[i]); |
| 1202 | } |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1203 | return env; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found |
| 1208 | * first in this order). |
| 1209 | */ |
| 1210 | static void |
| 1211 | do_rc_files(Session *s, const char *shell) |
| 1212 | { |
| 1213 | FILE *f = NULL; |
| 1214 | char cmd[1024]; |
| 1215 | int do_xauth; |
| 1216 | struct stat st; |
| 1217 | |
| 1218 | do_xauth = |
| 1219 | s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; |
| 1220 | |
| 1221 | /* ignore _PATH_SSH_USER_RC for subsystems */ |
| 1222 | if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) { |
| 1223 | snprintf(cmd, sizeof cmd, "%s -c '%s %s'", |
| 1224 | shell, _PATH_BSHELL, _PATH_SSH_USER_RC); |
| 1225 | if (debug_flag) |
| 1226 | fprintf(stderr, "Running %s\n", cmd); |
| 1227 | f = popen(cmd, "w"); |
| 1228 | if (f) { |
| 1229 | if (do_xauth) |
| 1230 | fprintf(f, "%s %s\n", s->auth_proto, |
| 1231 | s->auth_data); |
| 1232 | pclose(f); |
| 1233 | } else |
| 1234 | fprintf(stderr, "Could not run %s\n", |
| 1235 | _PATH_SSH_USER_RC); |
| 1236 | } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { |
| 1237 | if (debug_flag) |
| 1238 | fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, |
| 1239 | _PATH_SSH_SYSTEM_RC); |
| 1240 | f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); |
| 1241 | if (f) { |
| 1242 | if (do_xauth) |
| 1243 | fprintf(f, "%s %s\n", s->auth_proto, |
| 1244 | s->auth_data); |
| 1245 | pclose(f); |
| 1246 | } else |
| 1247 | fprintf(stderr, "Could not run %s\n", |
| 1248 | _PATH_SSH_SYSTEM_RC); |
| 1249 | } else if (do_xauth && options.xauth_location != NULL) { |
| 1250 | /* Add authority data to .Xauthority if appropriate. */ |
| 1251 | if (debug_flag) { |
| 1252 | fprintf(stderr, |
Ben Lindstrom | 611797e | 2002-12-23 02:15:57 +0000 | [diff] [blame] | 1253 | "Running %.500s remove %.100s\n", |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1254 | options.xauth_location, s->auth_display); |
Ben Lindstrom | 611797e | 2002-12-23 02:15:57 +0000 | [diff] [blame] | 1255 | fprintf(stderr, |
| 1256 | "%.500s add %.100s %.100s %.100s\n", |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1257 | options.xauth_location, s->auth_display, |
| 1258 | s->auth_proto, s->auth_data); |
| 1259 | } |
| 1260 | snprintf(cmd, sizeof cmd, "%s -q -", |
| 1261 | options.xauth_location); |
| 1262 | f = popen(cmd, "w"); |
| 1263 | if (f) { |
Ben Lindstrom | 611797e | 2002-12-23 02:15:57 +0000 | [diff] [blame] | 1264 | fprintf(f, "remove %s\n", |
| 1265 | s->auth_display); |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1266 | fprintf(f, "add %s %s %s\n", |
| 1267 | s->auth_display, s->auth_proto, |
| 1268 | s->auth_data); |
| 1269 | pclose(f); |
| 1270 | } else { |
| 1271 | fprintf(stderr, "Could not run %s\n", |
| 1272 | cmd); |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | static void |
| 1278 | do_nologin(struct passwd *pw) |
| 1279 | { |
| 1280 | FILE *f = NULL; |
| 1281 | char buf[1024]; |
| 1282 | |
| 1283 | #ifdef HAVE_LOGIN_CAP |
| 1284 | if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid) |
| 1285 | f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN, |
| 1286 | _PATH_NOLOGIN), "r"); |
| 1287 | #else |
| 1288 | if (pw->pw_uid) |
| 1289 | f = fopen(_PATH_NOLOGIN, "r"); |
| 1290 | #endif |
| 1291 | if (f) { |
| 1292 | /* /etc/nologin exists. Print its contents and exit. */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1293 | logit("User %.100s not allowed because %s exists", |
Damien Miller | a6eb2b7 | 2002-09-19 11:50:48 +1000 | [diff] [blame] | 1294 | pw->pw_name, _PATH_NOLOGIN); |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1295 | while (fgets(buf, sizeof(buf), f)) |
| 1296 | fputs(buf, stderr); |
| 1297 | fclose(f); |
Damien Miller | f25c18d | 2003-01-07 17:38:58 +1100 | [diff] [blame] | 1298 | fflush(NULL); |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1299 | exit(254); |
| 1300 | } |
| 1301 | } |
| 1302 | |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1303 | /* |
| 1304 | * Chroot into a directory after checking it for safety: all path components |
| 1305 | * must be root-owned directories with strict permissions. |
| 1306 | */ |
| 1307 | static void |
| 1308 | safely_chroot(const char *path, uid_t uid) |
| 1309 | { |
| 1310 | const char *cp; |
| 1311 | char component[MAXPATHLEN]; |
| 1312 | struct stat st; |
| 1313 | |
| 1314 | if (*path != '/') |
| 1315 | fatal("chroot path does not begin at root"); |
| 1316 | if (strlen(path) >= sizeof(component)) |
| 1317 | fatal("chroot path too long"); |
| 1318 | |
| 1319 | /* |
| 1320 | * Descend the path, checking that each component is a |
| 1321 | * root-owned directory with strict permissions. |
| 1322 | */ |
| 1323 | for (cp = path; cp != NULL;) { |
| 1324 | if ((cp = strchr(cp, '/')) == NULL) |
| 1325 | strlcpy(component, path, sizeof(component)); |
| 1326 | else { |
| 1327 | cp++; |
| 1328 | memcpy(component, path, cp - path); |
| 1329 | component[cp - path] = '\0'; |
| 1330 | } |
| 1331 | |
| 1332 | debug3("%s: checking '%s'", __func__, component); |
| 1333 | |
| 1334 | if (stat(component, &st) != 0) |
| 1335 | fatal("%s: stat(\"%s\"): %s", __func__, |
| 1336 | component, strerror(errno)); |
| 1337 | if (st.st_uid != 0 || (st.st_mode & 022) != 0) |
| 1338 | fatal("bad ownership or modes for chroot " |
| 1339 | "directory %s\"%s\"", |
| 1340 | cp == NULL ? "" : "component ", component); |
| 1341 | if (!S_ISDIR(st.st_mode)) |
| 1342 | fatal("chroot path %s\"%s\" is not a directory", |
| 1343 | cp == NULL ? "" : "component ", component); |
| 1344 | |
| 1345 | } |
| 1346 | |
| 1347 | if (chdir(path) == -1) |
| 1348 | fatal("Unable to chdir to chroot path \"%s\": " |
| 1349 | "%s", path, strerror(errno)); |
| 1350 | if (chroot(path) == -1) |
| 1351 | fatal("chroot(\"%s\"): %s", path, strerror(errno)); |
| 1352 | if (chdir("/") == -1) |
| 1353 | fatal("%s: chdir(/) after chroot: %s", |
| 1354 | __func__, strerror(errno)); |
| 1355 | verbose("Changed root directory to \"%s\"", path); |
| 1356 | } |
| 1357 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1358 | /* Set login name, uid, gid, and groups. */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1359 | void |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1360 | do_setusercontext(struct passwd *pw) |
| 1361 | { |
Damien Miller | 1a3ccb0 | 2003-02-24 13:04:01 +1100 | [diff] [blame] | 1362 | #ifndef HAVE_CYGWIN |
| 1363 | if (getuid() == 0 || geteuid() == 0) |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1364 | #endif /* HAVE_CYGWIN */ |
Damien Miller | 1a3ccb0 | 2003-02-24 13:04:01 +1100 | [diff] [blame] | 1365 | { |
| 1366 | |
Ben Lindstrom | f0bfa83 | 2002-06-21 00:01:18 +0000 | [diff] [blame] | 1367 | #ifdef HAVE_SETPCRED |
Darren Tucker | 793e817 | 2003-07-08 21:01:04 +1000 | [diff] [blame] | 1368 | if (setpcred(pw->pw_name, (char **)NULL) == -1) |
| 1369 | fatal("Failed to set process credentials"); |
Ben Lindstrom | f0bfa83 | 2002-06-21 00:01:18 +0000 | [diff] [blame] | 1370 | #endif /* HAVE_SETPCRED */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1371 | #ifdef HAVE_LOGIN_CAP |
Ben Lindstrom | 938b828 | 2002-07-15 17:58:34 +0000 | [diff] [blame] | 1372 | # ifdef __bsdi__ |
Damien Miller | f18cd16 | 2002-06-26 19:12:59 +1000 | [diff] [blame] | 1373 | setpgid(0, 0); |
Ben Lindstrom | 938b828 | 2002-07-15 17:58:34 +0000 | [diff] [blame] | 1374 | # endif |
Darren Tucker | c97b01a | 2005-02-16 16:47:37 +1100 | [diff] [blame] | 1375 | #ifdef GSSAPI |
| 1376 | if (options.gss_authentication) { |
| 1377 | temporarily_use_uid(pw); |
| 1378 | ssh_gssapi_storecreds(); |
| 1379 | restore_uid(); |
| 1380 | } |
| 1381 | #endif |
Damien Miller | d352636 | 2004-01-23 14:16:26 +1100 | [diff] [blame] | 1382 | # ifdef USE_PAM |
| 1383 | if (options.use_pam) { |
| 1384 | do_pam_session(); |
Darren Tucker | 2d96364 | 2007-08-13 23:11:56 +1000 | [diff] [blame] | 1385 | do_pam_setcred(use_privsep); |
Damien Miller | d352636 | 2004-01-23 14:16:26 +1100 | [diff] [blame] | 1386 | } |
| 1387 | # endif /* USE_PAM */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1388 | if (setusercontext(lc, pw, pw->pw_uid, |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1389 | (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) { |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1390 | perror("unable to set user context"); |
| 1391 | exit(1); |
| 1392 | } |
| 1393 | #else |
| 1394 | # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID) |
| 1395 | /* Sets login uid for accounting */ |
| 1396 | if (getluid() == -1 && setluid(pw->pw_uid) == -1) |
| 1397 | error("setluid: %s", strerror(errno)); |
| 1398 | # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */ |
| 1399 | |
| 1400 | if (setlogin(pw->pw_name) < 0) |
| 1401 | error("setlogin failed: %s", strerror(errno)); |
| 1402 | if (setgid(pw->pw_gid) < 0) { |
| 1403 | perror("setgid"); |
| 1404 | exit(1); |
| 1405 | } |
| 1406 | /* Initialize the group list. */ |
| 1407 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { |
| 1408 | perror("initgroups"); |
| 1409 | exit(1); |
| 1410 | } |
| 1411 | endgrent(); |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1412 | # ifdef GSSAPI |
Darren Tucker | c97b01a | 2005-02-16 16:47:37 +1100 | [diff] [blame] | 1413 | if (options.gss_authentication) { |
| 1414 | temporarily_use_uid(pw); |
| 1415 | ssh_gssapi_storecreds(); |
| 1416 | restore_uid(); |
| 1417 | } |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1418 | # endif |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1419 | # ifdef USE_PAM |
| 1420 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1421 | * PAM credentials may take the form of supplementary groups. |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1422 | * These will have been wiped by the above initgroups() call. |
| 1423 | * Reestablish them here. |
| 1424 | */ |
Damien Miller | 341c6e6 | 2003-09-02 23:18:52 +1000 | [diff] [blame] | 1425 | if (options.use_pam) { |
| 1426 | do_pam_session(); |
Darren Tucker | 2d96364 | 2007-08-13 23:11:56 +1000 | [diff] [blame] | 1427 | do_pam_setcred(use_privsep); |
Damien Miller | 341c6e6 | 2003-09-02 23:18:52 +1000 | [diff] [blame] | 1428 | } |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1429 | # endif /* USE_PAM */ |
| 1430 | # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) |
| 1431 | irix_setusercontext(pw); |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1432 | # endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */ |
Ben Lindstrom | b129be6 | 2002-06-25 17:12:26 +0000 | [diff] [blame] | 1433 | # ifdef _AIX |
Ben Lindstrom | 51b2488 | 2002-07-04 03:08:40 +0000 | [diff] [blame] | 1434 | aix_usrinfo(pw); |
Ben Lindstrom | b129be6 | 2002-06-25 17:12:26 +0000 | [diff] [blame] | 1435 | # endif /* _AIX */ |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1436 | # ifdef USE_LIBIAF |
Tim Rice | 2291c00 | 2005-08-26 13:15:19 -0700 | [diff] [blame] | 1437 | if (set_id(pw->pw_name) != 0) { |
| 1438 | exit(1); |
| 1439 | } |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1440 | # endif /* USE_LIBIAF */ |
| 1441 | #endif |
| 1442 | |
| 1443 | if (options.chroot_directory != NULL && |
| 1444 | strcasecmp(options.chroot_directory, "none") != 0) { |
| 1445 | char *chroot_path; |
| 1446 | |
| 1447 | chroot_path = percent_expand(options.chroot_directory, |
| 1448 | "h", pw->pw_dir, "u", pw->pw_name, (char *)NULL); |
| 1449 | safely_chroot(chroot_path, pw->pw_uid); |
| 1450 | free(chroot_path); |
| 1451 | } |
| 1452 | |
| 1453 | #ifdef HAVE_LOGIN_CAP |
| 1454 | if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) { |
| 1455 | perror("unable to set user context (setuser)"); |
| 1456 | exit(1); |
| 1457 | } |
| 1458 | #else |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1459 | /* Permanently switch to the desired uid. */ |
| 1460 | permanently_set_uid(pw); |
| 1461 | #endif |
| 1462 | } |
Damien Miller | 1a3ccb0 | 2003-02-24 13:04:01 +1100 | [diff] [blame] | 1463 | |
| 1464 | #ifdef HAVE_CYGWIN |
| 1465 | if (is_winnt) |
| 1466 | #endif |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1467 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) |
| 1468 | fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); |
Damien Miller | 73b42d2 | 2006-04-22 21:26:08 +1000 | [diff] [blame] | 1469 | |
| 1470 | #ifdef WITH_SELINUX |
| 1471 | ssh_selinux_setup_exec_context(pw->pw_name); |
| 1472 | #endif |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Ben Lindstrom | 0810519 | 2002-03-22 02:50:06 +0000 | [diff] [blame] | 1475 | static void |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1476 | do_pwchange(Session *s) |
| 1477 | { |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 1478 | fflush(NULL); |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1479 | fprintf(stderr, "WARNING: Your password has expired.\n"); |
| 1480 | if (s->ttyfd != -1) { |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1481 | fprintf(stderr, |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1482 | "You must change your password now and login again!\n"); |
Darren Tucker | 33370e0 | 2005-02-09 22:17:28 +1100 | [diff] [blame] | 1483 | #ifdef PASSWD_NEEDS_USERNAME |
| 1484 | execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name, |
| 1485 | (char *)NULL); |
| 1486 | #else |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1487 | execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL); |
Darren Tucker | 33370e0 | 2005-02-09 22:17:28 +1100 | [diff] [blame] | 1488 | #endif |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1489 | perror("passwd"); |
| 1490 | } else { |
| 1491 | fprintf(stderr, |
| 1492 | "Password change required but no TTY available.\n"); |
| 1493 | } |
| 1494 | exit(1); |
| 1495 | } |
| 1496 | |
| 1497 | static void |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1498 | launch_login(struct passwd *pw, const char *hostname) |
| 1499 | { |
| 1500 | /* Launch login(1). */ |
| 1501 | |
Ben Lindstrom | 378a417 | 2002-06-07 14:49:56 +0000 | [diff] [blame] | 1502 | execl(LOGIN_PROGRAM, "login", "-h", hostname, |
Kevin Steves | b4799a3 | 2002-03-24 23:19:54 +0000 | [diff] [blame] | 1503 | #ifdef xxxLOGIN_NEEDS_TERM |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 1504 | (s->term ? s->term : "unknown"), |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1505 | #endif /* LOGIN_NEEDS_TERM */ |
Kevin Steves | 5feaaef | 2002-04-23 20:45:55 +0000 | [diff] [blame] | 1506 | #ifdef LOGIN_NO_ENDOPT |
| 1507 | "-p", "-f", pw->pw_name, (char *)NULL); |
| 1508 | #else |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1509 | "-p", "-f", "--", pw->pw_name, (char *)NULL); |
Kevin Steves | 5feaaef | 2002-04-23 20:45:55 +0000 | [diff] [blame] | 1510 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1511 | |
| 1512 | /* Login couldn't be executed, die. */ |
| 1513 | |
| 1514 | perror("login"); |
| 1515 | exit(1); |
| 1516 | } |
| 1517 | |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1518 | static void |
| 1519 | child_close_fds(void) |
| 1520 | { |
| 1521 | int i; |
| 1522 | |
| 1523 | if (packet_get_connection_in() == packet_get_connection_out()) |
| 1524 | close(packet_get_connection_in()); |
| 1525 | else { |
| 1526 | close(packet_get_connection_in()); |
| 1527 | close(packet_get_connection_out()); |
| 1528 | } |
| 1529 | /* |
| 1530 | * Close all descriptors related to channels. They will still remain |
| 1531 | * open in the parent. |
| 1532 | */ |
| 1533 | /* XXX better use close-on-exec? -markus */ |
| 1534 | channel_close_all(); |
| 1535 | |
| 1536 | /* |
| 1537 | * Close any extra file descriptors. Note that there may still be |
| 1538 | * descriptors left by system functions. They will be closed later. |
| 1539 | */ |
| 1540 | endpwent(); |
| 1541 | |
| 1542 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 1543 | * Close any extra open file descriptors so that we don't have them |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1544 | * hanging around in clients. Note that we want to do this after |
| 1545 | * initgroups, because at least on Solaris 2.3 it leaves file |
| 1546 | * descriptors open. |
| 1547 | */ |
| 1548 | for (i = 3; i < 64; i++) |
| 1549 | close(i); |
| 1550 | } |
| 1551 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1552 | /* |
| 1553 | * Performs common processing for the child, such as setting up the |
| 1554 | * environment, closing extra file descriptors, setting the user and group |
| 1555 | * ids, and executing the command or shell. |
| 1556 | */ |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1557 | #define ARGV_MAX 10 |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1558 | void |
| 1559 | do_child(Session *s, const char *command) |
| 1560 | { |
| 1561 | extern char **environ; |
| 1562 | char **env; |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1563 | char *argv[ARGV_MAX]; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1564 | const char *shell, *shell0, *hostname = NULL; |
| 1565 | struct passwd *pw = s->pw; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* remove hostkey from the child's memory */ |
| 1568 | destroy_sensitive_data(); |
| 1569 | |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1570 | /* Force a password change */ |
| 1571 | if (s->authctxt->force_pwchange) { |
| 1572 | do_setusercontext(pw); |
| 1573 | child_close_fds(); |
| 1574 | do_pwchange(s); |
| 1575 | exit(1); |
| 1576 | } |
| 1577 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1578 | /* login(1) is only called if we execute the login shell */ |
| 1579 | if (options.use_login && command != NULL) |
| 1580 | options.use_login = 0; |
| 1581 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 1582 | #ifdef _UNICOS |
| 1583 | cray_setup(pw->pw_uid, pw->pw_name, command); |
| 1584 | #endif /* _UNICOS */ |
| 1585 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1586 | /* |
| 1587 | * Login(1) does this as well, and it needs uid 0 for the "-h" |
| 1588 | * switch, so we let login(1) to this for us. |
| 1589 | */ |
| 1590 | if (!options.use_login) { |
| 1591 | #ifdef HAVE_OSF_SIA |
Ben Lindstrom | c8c548d | 2003-03-21 01:18:09 +0000 | [diff] [blame] | 1592 | session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty); |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1593 | if (!check_quietlogin(s, command)) |
| 1594 | do_motd(); |
| 1595 | #else /* HAVE_OSF_SIA */ |
Darren Tucker | 42308a4 | 2005-10-30 15:31:55 +1100 | [diff] [blame] | 1596 | /* When PAM is enabled we rely on it to do the nologin check */ |
| 1597 | if (!options.use_pam) |
| 1598 | do_nologin(pw); |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1599 | do_setusercontext(pw); |
Darren Tucker | 0a44d1e | 2004-07-01 09:48:29 +1000 | [diff] [blame] | 1600 | /* |
| 1601 | * PAM session modules in do_setusercontext may have |
| 1602 | * generated messages, so if this in an interactive |
| 1603 | * login then display them too. |
| 1604 | */ |
Darren Tucker | a2a3ed0 | 2004-09-11 23:09:53 +1000 | [diff] [blame] | 1605 | if (!check_quietlogin(s, command)) |
Darren Tucker | 0a44d1e | 2004-07-01 09:48:29 +1000 | [diff] [blame] | 1606 | display_loginmsg(); |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1607 | #endif /* HAVE_OSF_SIA */ |
| 1608 | } |
| 1609 | |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1610 | #ifdef USE_PAM |
Darren Tucker | 4855415 | 2005-04-21 19:50:55 +1000 | [diff] [blame] | 1611 | if (options.use_pam && !options.use_login && !is_pam_session_open()) { |
| 1612 | debug3("PAM session not opened, exiting"); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1613 | display_loginmsg(); |
| 1614 | exit(254); |
| 1615 | } |
| 1616 | #endif |
| 1617 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1618 | /* |
| 1619 | * Get the shell from the password data. An empty shell field is |
| 1620 | * legal, and means /bin/sh. |
| 1621 | */ |
| 1622 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
Ben Lindstrom | 4676760 | 2002-12-23 02:26:08 +0000 | [diff] [blame] | 1623 | |
| 1624 | /* |
| 1625 | * Make sure $SHELL points to the shell from the password file, |
| 1626 | * even if shell is overridden from login.conf |
| 1627 | */ |
| 1628 | env = do_setup_env(s, shell); |
| 1629 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1630 | #ifdef HAVE_LOGIN_CAP |
| 1631 | shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); |
| 1632 | #endif |
| 1633 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1634 | /* we have to stash the hostname before we close our socket. */ |
| 1635 | if (options.use_login) |
Ben Lindstrom | f15a386 | 2001-04-05 23:32:17 +0000 | [diff] [blame] | 1636 | hostname = get_remote_name_or_ip(utmp_len, |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 1637 | options.use_dns); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1638 | /* |
| 1639 | * Close the connection descriptors; note that this is the child, and |
| 1640 | * the server will still have the socket open, and it is important |
| 1641 | * that we do not shutdown it. Note that the descriptors cannot be |
| 1642 | * closed before building the environment, as we call |
| 1643 | * get_remote_ipaddr there. |
| 1644 | */ |
Darren Tucker | 23bc8d0 | 2004-02-06 16:24:31 +1100 | [diff] [blame] | 1645 | child_close_fds(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1646 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1647 | /* |
Damien Miller | 05eda43 | 2002-02-10 18:32:28 +1100 | [diff] [blame] | 1648 | * Must take new environment into use so that .ssh/rc, |
| 1649 | * /etc/ssh/sshrc and xauth are run in the proper environment. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1650 | */ |
| 1651 | environ = env; |
| 1652 | |
Darren Tucker | 3c78c5e | 2004-01-23 22:03:10 +1100 | [diff] [blame] | 1653 | #if defined(KRB5) && defined(USE_AFS) |
Darren Tucker | 22ef508 | 2003-12-31 11:37:34 +1100 | [diff] [blame] | 1654 | /* |
| 1655 | * At this point, we check to see if AFS is active and if we have |
| 1656 | * a valid Kerberos 5 TGT. If so, it seems like a good idea to see |
| 1657 | * if we can (and need to) extend the ticket into an AFS token. If |
| 1658 | * we don't do this, we run into potential problems if the user's |
| 1659 | * home directory is in AFS and it's not world-readable. |
| 1660 | */ |
| 1661 | |
| 1662 | if (options.kerberos_get_afs_token && k_hasafs() && |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 1663 | (s->authctxt->krb5_ctx != NULL)) { |
Darren Tucker | 22ef508 | 2003-12-31 11:37:34 +1100 | [diff] [blame] | 1664 | char cell[64]; |
| 1665 | |
| 1666 | debug("Getting AFS token"); |
| 1667 | |
| 1668 | k_setpag(); |
| 1669 | |
| 1670 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) |
| 1671 | krb5_afslog(s->authctxt->krb5_ctx, |
| 1672 | s->authctxt->krb5_fwd_ccache, cell, NULL); |
| 1673 | |
| 1674 | krb5_afslog_home(s->authctxt->krb5_ctx, |
| 1675 | s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir); |
| 1676 | } |
| 1677 | #endif |
| 1678 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 1679 | /* Change current directory to the user's home directory. */ |
Damien Miller | 139d4cd | 2001-10-10 15:07:44 +1000 | [diff] [blame] | 1680 | if (chdir(pw->pw_dir) < 0) { |
| 1681 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", |
| 1682 | pw->pw_dir, strerror(errno)); |
| 1683 | #ifdef HAVE_LOGIN_CAP |
| 1684 | if (login_getcapbool(lc, "requirehome", 0)) |
| 1685 | exit(1); |
| 1686 | #endif |
| 1687 | } |
| 1688 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1689 | if (!options.use_login) |
| 1690 | do_rc_files(s, shell); |
Ben Lindstrom | de71cda | 2001-03-24 00:43:26 +0000 | [diff] [blame] | 1691 | |
| 1692 | /* restore SIGPIPE for child */ |
Damien Miller | 9ab00b4 | 2006-08-05 12:40:11 +1000 | [diff] [blame] | 1693 | signal(SIGPIPE, SIG_DFL); |
Ben Lindstrom | de71cda | 2001-03-24 00:43:26 +0000 | [diff] [blame] | 1694 | |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1695 | if (s->is_subsystem == SUBSYSTEM_INT_SFTP) { |
| 1696 | extern int optind, optreset; |
| 1697 | int i; |
| 1698 | char *p, *args; |
| 1699 | |
| 1700 | setproctitle("%s@internal-sftp-server", s->pw->pw_name); |
| 1701 | args = strdup(command ? command : "sftp-server"); |
| 1702 | for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " "))) |
| 1703 | if (i < ARGV_MAX - 1) |
| 1704 | argv[i++] = p; |
| 1705 | argv[i] = NULL; |
| 1706 | optind = optreset = 1; |
| 1707 | __progname = argv[0]; |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1708 | exit(sftp_server_main(i, argv, s->pw)); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1709 | } |
| 1710 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1711 | if (options.use_login) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1712 | launch_login(pw, hostname); |
| 1713 | /* NEVERREACHED */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /* Get the last component of the shell name. */ |
| 1717 | if ((shell0 = strrchr(shell, '/')) != NULL) |
| 1718 | shell0++; |
| 1719 | else |
| 1720 | shell0 = shell; |
| 1721 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1722 | /* |
| 1723 | * If we have no command, execute the shell. In this case, the shell |
| 1724 | * name to be passed in argv[0] is preceded by '-' to indicate that |
| 1725 | * this is a login shell. |
| 1726 | */ |
| 1727 | if (!command) { |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1728 | char argv0[256]; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1729 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1730 | /* Start the shell. Set initial character to '-'. */ |
| 1731 | argv0[0] = '-'; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1732 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1733 | if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1) |
| 1734 | >= sizeof(argv0) - 1) { |
| 1735 | errno = EINVAL; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1736 | perror(shell); |
| 1737 | exit(1); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1738 | } |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1739 | |
| 1740 | /* Execute the shell. */ |
| 1741 | argv[0] = argv0; |
| 1742 | argv[1] = NULL; |
| 1743 | execve(shell, argv, env); |
| 1744 | |
| 1745 | /* Executing the shell failed. */ |
| 1746 | perror(shell); |
| 1747 | exit(1); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1748 | } |
| 1749 | /* |
| 1750 | * Execute the command using the user's shell. This uses the -c |
| 1751 | * option to execute the command. |
| 1752 | */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1753 | argv[0] = (char *) shell0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1754 | argv[1] = "-c"; |
| 1755 | argv[2] = (char *) command; |
| 1756 | argv[3] = NULL; |
| 1757 | execve(shell, argv, env); |
| 1758 | perror(shell); |
| 1759 | exit(1); |
| 1760 | } |
| 1761 | |
| 1762 | Session * |
| 1763 | session_new(void) |
| 1764 | { |
| 1765 | int i; |
| 1766 | static int did_init = 0; |
| 1767 | if (!did_init) { |
| 1768 | debug("session_new: init"); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1769 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1770 | sessions[i].used = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1771 | } |
| 1772 | did_init = 1; |
| 1773 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1774 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1775 | Session *s = &sessions[i]; |
| 1776 | if (! s->used) { |
Ben Lindstrom | 6029432 | 2001-03-26 05:38:25 +0000 | [diff] [blame] | 1777 | memset(s, 0, sizeof(*s)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1778 | s->chanid = -1; |
| 1779 | s->ptyfd = -1; |
| 1780 | s->ttyfd = -1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1781 | s->used = 1; |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1782 | s->self = i; |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 1783 | s->x11_chanids = NULL; |
Damien Miller | 15b2952 | 2000-10-14 12:33:48 +1100 | [diff] [blame] | 1784 | debug("session_new: session %d", i); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1785 | return s; |
| 1786 | } |
| 1787 | } |
| 1788 | return NULL; |
| 1789 | } |
| 1790 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1791 | static void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1792 | session_dump(void) |
| 1793 | { |
| 1794 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1795 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1796 | Session *s = &sessions[i]; |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1797 | debug("dump: used %d session %d %p channel %d pid %ld", |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1798 | s->used, |
| 1799 | s->self, |
| 1800 | s, |
| 1801 | s->chanid, |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1802 | (long)s->pid); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1806 | int |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 1807 | session_open(Authctxt *authctxt, int chanid) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1808 | { |
| 1809 | Session *s = session_new(); |
| 1810 | debug("session_open: channel %d", chanid); |
| 1811 | if (s == NULL) { |
| 1812 | error("no more sessions"); |
| 1813 | return 0; |
| 1814 | } |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 1815 | s->authctxt = authctxt; |
| 1816 | s->pw = authctxt->pw; |
Damien Miller | 3e3b514 | 2003-11-17 21:13:40 +1100 | [diff] [blame] | 1817 | if (s->pw == NULL || !authctxt->valid) |
Kevin Steves | 43cdef3 | 2001-02-11 14:12:08 +0000 | [diff] [blame] | 1818 | fatal("no user for session %d", s->self); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1819 | debug("session_open: session %d: link with channel %d", s->self, chanid); |
| 1820 | s->chanid = chanid; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1821 | return 1; |
| 1822 | } |
| 1823 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1824 | Session * |
| 1825 | session_by_tty(char *tty) |
| 1826 | { |
| 1827 | int i; |
| 1828 | for (i = 0; i < MAX_SESSIONS; i++) { |
| 1829 | Session *s = &sessions[i]; |
| 1830 | if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) { |
| 1831 | debug("session_by_tty: session %d tty %s", i, tty); |
| 1832 | return s; |
| 1833 | } |
| 1834 | } |
| 1835 | debug("session_by_tty: unknown tty %.100s", tty); |
| 1836 | session_dump(); |
| 1837 | return NULL; |
| 1838 | } |
| 1839 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1840 | static Session * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1841 | session_by_channel(int id) |
| 1842 | { |
| 1843 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1844 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1845 | Session *s = &sessions[i]; |
| 1846 | if (s->used && s->chanid == id) { |
| 1847 | debug("session_by_channel: session %d channel %d", i, id); |
| 1848 | return s; |
| 1849 | } |
| 1850 | } |
| 1851 | debug("session_by_channel: unknown channel %d", id); |
| 1852 | session_dump(); |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1856 | static Session * |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 1857 | session_by_x11_channel(int id) |
| 1858 | { |
| 1859 | int i, j; |
| 1860 | |
| 1861 | for (i = 0; i < MAX_SESSIONS; i++) { |
| 1862 | Session *s = &sessions[i]; |
| 1863 | |
| 1864 | if (s->x11_chanids == NULL || !s->used) |
| 1865 | continue; |
| 1866 | for (j = 0; s->x11_chanids[j] != -1; j++) { |
| 1867 | if (s->x11_chanids[j] == id) { |
| 1868 | debug("session_by_x11_channel: session %d " |
| 1869 | "channel %d", s->self, id); |
| 1870 | return s; |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | debug("session_by_x11_channel: unknown channel %d", id); |
| 1875 | session_dump(); |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | |
| 1879 | static Session * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1880 | session_by_pid(pid_t pid) |
| 1881 | { |
| 1882 | int i; |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1883 | debug("session_by_pid: pid %ld", (long)pid); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1884 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1885 | Session *s = &sessions[i]; |
| 1886 | if (s->used && s->pid == pid) |
| 1887 | return s; |
| 1888 | } |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 1889 | error("session_by_pid: unknown pid %ld", (long)pid); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1890 | session_dump(); |
| 1891 | return NULL; |
| 1892 | } |
| 1893 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1894 | static int |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1895 | session_window_change_req(Session *s) |
| 1896 | { |
| 1897 | s->col = packet_get_int(); |
| 1898 | s->row = packet_get_int(); |
| 1899 | s->xpixel = packet_get_int(); |
| 1900 | s->ypixel = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1901 | packet_check_eom(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1902 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1903 | return 1; |
| 1904 | } |
| 1905 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1906 | static int |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1907 | session_pty_req(Session *s) |
| 1908 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1909 | u_int len; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 1910 | int n_bytes; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1911 | |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1912 | if (no_pty_flag) { |
| 1913 | debug("Allocating a pty not permitted for this authentication."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1914 | return 0; |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1915 | } |
| 1916 | if (s->ttyfd != -1) { |
| 1917 | packet_disconnect("Protocol error: you already have a pty."); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1918 | return 0; |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1921 | s->term = packet_get_string(&len); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1922 | |
| 1923 | if (compat20) { |
| 1924 | s->col = packet_get_int(); |
| 1925 | s->row = packet_get_int(); |
| 1926 | } else { |
| 1927 | s->row = packet_get_int(); |
| 1928 | s->col = packet_get_int(); |
| 1929 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1930 | s->xpixel = packet_get_int(); |
| 1931 | s->ypixel = packet_get_int(); |
| 1932 | |
| 1933 | if (strcmp(s->term, "") == 0) { |
| 1934 | xfree(s->term); |
| 1935 | s->term = NULL; |
| 1936 | } |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1937 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1938 | /* Allocate a pty and open it. */ |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1939 | debug("Allocating pty."); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1940 | if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) { |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1941 | if (s->term) |
| 1942 | xfree(s->term); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1943 | s->term = NULL; |
| 1944 | s->ptyfd = -1; |
| 1945 | s->ttyfd = -1; |
| 1946 | error("session_pty_req: session %d alloc failed", s->self); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1947 | return 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1948 | } |
| 1949 | debug("session_pty_req: session %d alloc %s", s->self, s->tty); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1950 | |
| 1951 | /* for SSH1 the tty modes length is not given */ |
| 1952 | if (!compat20) |
| 1953 | n_bytes = packet_remaining(); |
| 1954 | tty_parse_modes(s->ttyfd, &n_bytes); |
| 1955 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1956 | if (!use_privsep) |
| 1957 | pty_setowner(s->pw, s->tty); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1958 | |
| 1959 | /* Set window size from the packet. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1960 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1961 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1962 | packet_check_eom(); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1963 | session_proctitle(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1964 | return 1; |
| 1965 | } |
| 1966 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1967 | static int |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1968 | session_subsystem_req(Session *s) |
| 1969 | { |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1970 | struct stat st; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1971 | u_int len; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1972 | int success = 0; |
Damien Miller | 917f9b6 | 2006-07-10 20:36:47 +1000 | [diff] [blame] | 1973 | char *prog, *cmd, *subsys = packet_get_string(&len); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1974 | u_int i; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1975 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1976 | packet_check_eom(); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1977 | logit("subsystem request for %.100s", subsys); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1978 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1979 | for (i = 0; i < options.num_subsystems; i++) { |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1980 | if (strcmp(subsys, options.subsystem_name[i]) == 0) { |
Damien Miller | 917f9b6 | 2006-07-10 20:36:47 +1000 | [diff] [blame] | 1981 | prog = options.subsystem_command[i]; |
| 1982 | cmd = options.subsystem_args[i]; |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame^] | 1983 | if (!strcmp(INTERNAL_SFTP_NAME, prog)) { |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1984 | s->is_subsystem = SUBSYSTEM_INT_SFTP; |
| 1985 | } else if (stat(prog, &st) < 0) { |
Damien Miller | 917f9b6 | 2006-07-10 20:36:47 +1000 | [diff] [blame] | 1986 | error("subsystem: cannot stat %s: %s", prog, |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1987 | strerror(errno)); |
| 1988 | break; |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1989 | } else { |
| 1990 | s->is_subsystem = SUBSYSTEM_EXT; |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1991 | } |
| 1992 | debug("subsystem: exec() %s", cmd); |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1993 | do_exec(s, cmd); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1994 | success = 1; |
Damien Miller | 5fab4b9 | 2002-02-05 12:15:07 +1100 | [diff] [blame] | 1995 | break; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | if (!success) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2000 | logit("subsystem request for %.100s failed, subsystem not found", |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 2001 | subsys); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2002 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2003 | xfree(subsys); |
| 2004 | return success; |
| 2005 | } |
| 2006 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2007 | static int |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2008 | session_x11_req(Session *s) |
| 2009 | { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2010 | int success; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2011 | |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2012 | if (s->auth_proto != NULL || s->auth_data != NULL) { |
| 2013 | error("session_x11_req: session %d: " |
Darren Tucker | 6355187 | 2005-12-20 16:14:15 +1100 | [diff] [blame] | 2014 | "x11 forwarding already active", s->self); |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2015 | return 0; |
| 2016 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2017 | s->single_connection = packet_get_char(); |
| 2018 | s->auth_proto = packet_get_string(NULL); |
| 2019 | s->auth_data = packet_get_string(NULL); |
| 2020 | s->screen = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2021 | packet_check_eom(); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2022 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2023 | success = session_setup_x11fwd(s); |
| 2024 | if (!success) { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2025 | xfree(s->auth_proto); |
| 2026 | xfree(s->auth_data); |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 2027 | s->auth_proto = NULL; |
| 2028 | s->auth_data = NULL; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2029 | } |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2030 | return success; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2031 | } |
| 2032 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2033 | static int |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2034 | session_shell_req(Session *s) |
| 2035 | { |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2036 | packet_check_eom(); |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 2037 | do_exec(s, NULL); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2038 | return 1; |
| 2039 | } |
| 2040 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2041 | static int |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2042 | session_exec_req(Session *s) |
| 2043 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 2044 | u_int len; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2045 | char *command = packet_get_string(&len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2046 | packet_check_eom(); |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 2047 | do_exec(s, command); |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 2048 | xfree(command); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2049 | return 1; |
| 2050 | } |
| 2051 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2052 | static int |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 2053 | session_break_req(Session *s) |
| 2054 | { |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 2055 | |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 2056 | packet_get_int(); /* ignored */ |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 2057 | packet_check_eom(); |
| 2058 | |
Darren Tucker | 3bdbd84 | 2003-08-13 20:31:05 +1000 | [diff] [blame] | 2059 | if (s->ttyfd == -1 || |
| 2060 | tcsendbreak(s->ttyfd, 0) < 0) |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 2061 | return 0; |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 2062 | return 1; |
| 2063 | } |
| 2064 | |
| 2065 | static int |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 2066 | session_env_req(Session *s) |
| 2067 | { |
| 2068 | char *name, *val; |
| 2069 | u_int name_len, val_len, i; |
| 2070 | |
| 2071 | name = packet_get_string(&name_len); |
| 2072 | val = packet_get_string(&val_len); |
| 2073 | packet_check_eom(); |
| 2074 | |
| 2075 | /* Don't set too many environment variables */ |
| 2076 | if (s->num_env > 128) { |
| 2077 | debug2("Ignoring env request %s: too many env vars", name); |
| 2078 | goto fail; |
| 2079 | } |
| 2080 | |
| 2081 | for (i = 0; i < options.num_accept_env; i++) { |
| 2082 | if (match_pattern(name, options.accept_env[i])) { |
| 2083 | debug2("Setting env %d: %s=%s", s->num_env, name, val); |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2084 | s->env = xrealloc(s->env, s->num_env + 1, |
| 2085 | sizeof(*s->env)); |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 2086 | s->env[s->num_env].name = name; |
| 2087 | s->env[s->num_env].val = val; |
| 2088 | s->num_env++; |
| 2089 | return (1); |
| 2090 | } |
| 2091 | } |
| 2092 | debug2("Ignoring env request %s: disallowed name", name); |
| 2093 | |
| 2094 | fail: |
| 2095 | xfree(name); |
| 2096 | xfree(val); |
| 2097 | return (0); |
| 2098 | } |
| 2099 | |
| 2100 | static int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2101 | session_auth_agent_req(Session *s) |
| 2102 | { |
| 2103 | static int called = 0; |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2104 | packet_check_eom(); |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 2105 | if (no_agent_forwarding_flag) { |
| 2106 | debug("session_auth_agent_req: no_agent_forwarding_flag"); |
| 2107 | return 0; |
| 2108 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2109 | if (called) { |
| 2110 | return 0; |
| 2111 | } else { |
| 2112 | called = 1; |
| 2113 | return auth_input_request_forwarding(s->pw); |
| 2114 | } |
| 2115 | } |
| 2116 | |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 2117 | int |
| 2118 | session_input_channel_req(Channel *c, const char *rtype) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2119 | { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2120 | int success = 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2121 | Session *s; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2122 | |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 2123 | if ((s = session_by_channel(c->self)) == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2124 | logit("session_input_channel_req: no session %d req %.100s", |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 2125 | c->self, rtype); |
| 2126 | return 0; |
| 2127 | } |
| 2128 | debug("session_input_channel_req: session %d req %s", s->self, rtype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2129 | |
| 2130 | /* |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 2131 | * a session is in LARVAL state until a shell, a command |
| 2132 | * or a subsystem is executed |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2133 | */ |
| 2134 | if (c->type == SSH_CHANNEL_LARVAL) { |
| 2135 | if (strcmp(rtype, "shell") == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2136 | success = session_shell_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2137 | } else if (strcmp(rtype, "exec") == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 2138 | success = session_exec_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2139 | } else if (strcmp(rtype, "pty-req") == 0) { |
Darren Tucker | 82a3d2b | 2007-02-19 22:10:25 +1100 | [diff] [blame] | 2140 | success = session_pty_req(s); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2141 | } else if (strcmp(rtype, "x11-req") == 0) { |
| 2142 | success = session_x11_req(s); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2143 | } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { |
| 2144 | success = session_auth_agent_req(s); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2145 | } else if (strcmp(rtype, "subsystem") == 0) { |
| 2146 | success = session_subsystem_req(s); |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 2147 | } else if (strcmp(rtype, "env") == 0) { |
| 2148 | success = session_env_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | if (strcmp(rtype, "window-change") == 0) { |
| 2152 | success = session_window_change_req(s); |
Damien Miller | a6b1d16 | 2004-06-30 22:41:07 +1000 | [diff] [blame] | 2153 | } else if (strcmp(rtype, "break") == 0) { |
| 2154 | success = session_break_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2155 | } |
Damien Miller | a6b1d16 | 2004-06-30 22:41:07 +1000 | [diff] [blame] | 2156 | |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 2157 | return success; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | void |
| 2161 | session_set_fds(Session *s, int fdin, int fdout, int fderr) |
| 2162 | { |
| 2163 | if (!compat20) |
| 2164 | fatal("session_set_fds: called for proto != 2.0"); |
| 2165 | /* |
| 2166 | * now that have a child and a pipe to the child, |
| 2167 | * we can activate our channel and register the fd's |
| 2168 | */ |
| 2169 | if (s->chanid == -1) |
| 2170 | fatal("no channel for session %d", s->self); |
| 2171 | channel_set_fds(s->chanid, |
| 2172 | fdout, fdin, fderr, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2173 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, |
Damien Miller | 19a5945 | 2002-02-19 15:20:57 +1100 | [diff] [blame] | 2174 | 1, |
| 2175 | CHAN_SES_WINDOW_DEFAULT); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2176 | } |
| 2177 | |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 2178 | /* |
| 2179 | * Function to perform pty cleanup. Also called if we get aborted abnormally |
| 2180 | * (e.g., due to a dropped connection). |
| 2181 | */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2182 | void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2183 | session_pty_cleanup2(Session *s) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2184 | { |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 2185 | if (s == NULL) { |
| 2186 | error("session_pty_cleanup: no session"); |
| 2187 | return; |
| 2188 | } |
| 2189 | if (s->ttyfd == -1) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2190 | return; |
| 2191 | |
Kevin Steves | 43cdef3 | 2001-02-11 14:12:08 +0000 | [diff] [blame] | 2192 | debug("session_pty_cleanup: session %d release %s", s->self, s->tty); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2193 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2194 | /* Record that the user has logged out. */ |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 2195 | if (s->pid != 0) |
Tim Rice | e06ae4a | 2002-02-24 17:56:46 -0800 | [diff] [blame] | 2196 | record_logout(s->pid, s->tty, s->pw->pw_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2197 | |
| 2198 | /* Release the pseudo-tty. */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2199 | if (getuid() == 0) |
| 2200 | pty_release(s->tty); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2201 | |
| 2202 | /* |
| 2203 | * Close the server side of the socket pairs. We must do this after |
| 2204 | * the pty cleanup, so that another process doesn't get this pty |
| 2205 | * while we're still cleaning up. |
| 2206 | */ |
| 2207 | if (close(s->ptymaster) < 0) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2208 | error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno)); |
Damien Miller | 0585d51 | 2001-10-12 11:35:50 +1000 | [diff] [blame] | 2209 | |
| 2210 | /* unlink pty from session */ |
| 2211 | s->ttyfd = -1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2212 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2213 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2214 | void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2215 | session_pty_cleanup(Session *s) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2216 | { |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2217 | PRIVSEP(session_pty_cleanup2(s)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Damien Miller | 5a80bba | 2002-09-04 16:39:02 +1000 | [diff] [blame] | 2220 | static char * |
| 2221 | sig2name(int sig) |
| 2222 | { |
| 2223 | #define SSH_SIG(x) if (sig == SIG ## x) return #x |
| 2224 | SSH_SIG(ABRT); |
| 2225 | SSH_SIG(ALRM); |
| 2226 | SSH_SIG(FPE); |
| 2227 | SSH_SIG(HUP); |
| 2228 | SSH_SIG(ILL); |
| 2229 | SSH_SIG(INT); |
| 2230 | SSH_SIG(KILL); |
| 2231 | SSH_SIG(PIPE); |
| 2232 | SSH_SIG(QUIT); |
| 2233 | SSH_SIG(SEGV); |
| 2234 | SSH_SIG(TERM); |
| 2235 | SSH_SIG(USR1); |
| 2236 | SSH_SIG(USR2); |
| 2237 | #undef SSH_SIG |
| 2238 | return "SIG@openssh.com"; |
| 2239 | } |
| 2240 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2241 | static void |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2242 | session_close_x11(int id) |
| 2243 | { |
| 2244 | Channel *c; |
| 2245 | |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 2246 | if ((c = channel_by_id(id)) == NULL) { |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2247 | debug("session_close_x11: x11 channel %d missing", id); |
| 2248 | } else { |
| 2249 | /* Detach X11 listener */ |
| 2250 | debug("session_close_x11: detach x11 channel %d", id); |
| 2251 | channel_cancel_cleanup(id); |
| 2252 | if (c->ostate != CHAN_OUTPUT_CLOSED) |
| 2253 | chan_mark_dead(c); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | static void |
| 2258 | session_close_single_x11(int id, void *arg) |
| 2259 | { |
| 2260 | Session *s; |
| 2261 | u_int i; |
| 2262 | |
| 2263 | debug3("session_close_single_x11: channel %d", id); |
| 2264 | channel_cancel_cleanup(id); |
Darren Tucker | 82a3d2b | 2007-02-19 22:10:25 +1100 | [diff] [blame] | 2265 | if ((s = session_by_x11_channel(id)) == NULL) |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2266 | fatal("session_close_single_x11: no x11 channel %d", id); |
| 2267 | for (i = 0; s->x11_chanids[i] != -1; i++) { |
| 2268 | debug("session_close_single_x11: session %d: " |
| 2269 | "closing channel %d", s->self, s->x11_chanids[i]); |
| 2270 | /* |
| 2271 | * The channel "id" is already closing, but make sure we |
| 2272 | * close all of its siblings. |
| 2273 | */ |
| 2274 | if (s->x11_chanids[i] != id) |
| 2275 | session_close_x11(s->x11_chanids[i]); |
| 2276 | } |
| 2277 | xfree(s->x11_chanids); |
| 2278 | s->x11_chanids = NULL; |
| 2279 | if (s->display) { |
| 2280 | xfree(s->display); |
| 2281 | s->display = NULL; |
| 2282 | } |
| 2283 | if (s->auth_proto) { |
| 2284 | xfree(s->auth_proto); |
| 2285 | s->auth_proto = NULL; |
| 2286 | } |
| 2287 | if (s->auth_data) { |
| 2288 | xfree(s->auth_data); |
| 2289 | s->auth_data = NULL; |
| 2290 | } |
| 2291 | if (s->auth_display) { |
| 2292 | xfree(s->auth_display); |
| 2293 | s->auth_display = NULL; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | static void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2298 | session_exit_message(Session *s, int status) |
| 2299 | { |
| 2300 | Channel *c; |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 2301 | |
| 2302 | if ((c = channel_lookup(s->chanid)) == NULL) |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 2303 | fatal("session_exit_message: session %d: no channel %d", |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2304 | s->self, s->chanid); |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 2305 | debug("session_exit_message: session %d channel %d pid %ld", |
| 2306 | s->self, s->chanid, (long)s->pid); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2307 | |
| 2308 | if (WIFEXITED(status)) { |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 2309 | channel_request_start(s->chanid, "exit-status", 0); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2310 | packet_put_int(WEXITSTATUS(status)); |
| 2311 | packet_send(); |
| 2312 | } else if (WIFSIGNALED(status)) { |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 2313 | channel_request_start(s->chanid, "exit-signal", 0); |
Damien Miller | 5a80bba | 2002-09-04 16:39:02 +1000 | [diff] [blame] | 2314 | packet_put_cstring(sig2name(WTERMSIG(status))); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 2315 | #ifdef WCOREDUMP |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2316 | packet_put_char(WCOREDUMP(status)); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 2317 | #else /* WCOREDUMP */ |
| 2318 | packet_put_char(0); |
| 2319 | #endif /* WCOREDUMP */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2320 | packet_put_cstring(""); |
| 2321 | packet_put_cstring(""); |
| 2322 | packet_send(); |
| 2323 | } else { |
| 2324 | /* Some weird exit cause. Just exit. */ |
| 2325 | packet_disconnect("wait returned status %04x.", status); |
| 2326 | } |
| 2327 | |
| 2328 | /* disconnect channel */ |
| 2329 | debug("session_exit_message: release channel %d", s->chanid); |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 2330 | |
| 2331 | /* |
| 2332 | * Adjust cleanup callback attachment to send close messages when |
Damien Miller | c91e556 | 2006-03-26 13:58:55 +1100 | [diff] [blame] | 2333 | * the channel gets EOF. The session will be then be closed |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 2334 | * by session_close_by_channel when the childs close their fds. |
| 2335 | */ |
| 2336 | channel_register_cleanup(c->self, session_close_by_channel, 1); |
| 2337 | |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 2338 | /* |
| 2339 | * emulate a write failure with 'chan_write_failed', nobody will be |
| 2340 | * interested in data we write. |
| 2341 | * Note that we must not call 'chan_read_failed', since there could |
| 2342 | * be some more data waiting in the pipe. |
| 2343 | */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2344 | if (c->ostate != CHAN_OUTPUT_CLOSED) |
| 2345 | chan_write_failed(c); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2346 | } |
| 2347 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2348 | void |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 2349 | session_close(Session *s) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2350 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 2351 | u_int i; |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 2352 | |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 2353 | debug("session_close: session %d pid %ld", s->self, (long)s->pid); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2354 | if (s->ttyfd != -1) |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 2355 | session_pty_cleanup(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2356 | if (s->term) |
| 2357 | xfree(s->term); |
| 2358 | if (s->display) |
| 2359 | xfree(s->display); |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2360 | if (s->x11_chanids) |
| 2361 | xfree(s->x11_chanids); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 2362 | if (s->auth_display) |
| 2363 | xfree(s->auth_display); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2364 | if (s->auth_data) |
| 2365 | xfree(s->auth_data); |
| 2366 | if (s->auth_proto) |
| 2367 | xfree(s->auth_proto); |
| 2368 | s->used = 0; |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 2369 | if (s->env != NULL) { |
| 2370 | for (i = 0; i < s->num_env; i++) { |
| 2371 | xfree(s->env[i].name); |
| 2372 | xfree(s->env[i].val); |
| 2373 | } |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 2374 | xfree(s->env); |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 2375 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2376 | session_proctitle(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | void |
| 2380 | session_close_by_pid(pid_t pid, int status) |
| 2381 | { |
| 2382 | Session *s = session_by_pid(pid); |
| 2383 | if (s == NULL) { |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 2384 | debug("session_close_by_pid: no session for pid %ld", |
| 2385 | (long)pid); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2386 | return; |
| 2387 | } |
| 2388 | if (s->chanid != -1) |
| 2389 | session_exit_message(s, status); |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 2390 | if (s->ttyfd != -1) |
| 2391 | session_pty_cleanup(s); |
Tim Rice | 83d2f5f | 2006-02-07 15:17:44 -0800 | [diff] [blame] | 2392 | s->pid = 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | /* |
| 2396 | * this is called when a channel dies before |
| 2397 | * the session 'child' itself dies |
| 2398 | */ |
| 2399 | void |
| 2400 | session_close_by_channel(int id, void *arg) |
| 2401 | { |
| 2402 | Session *s = session_by_channel(id); |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 2403 | u_int i; |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2404 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2405 | if (s == NULL) { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2406 | debug("session_close_by_channel: no session for id %d", id); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2407 | return; |
| 2408 | } |
Ben Lindstrom | ce0f634 | 2002-06-11 16:42:49 +0000 | [diff] [blame] | 2409 | debug("session_close_by_channel: channel %d child %ld", |
| 2410 | id, (long)s->pid); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2411 | if (s->pid != 0) { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2412 | debug("session_close_by_channel: channel %d: has child", id); |
Damien Miller | 0585d51 | 2001-10-12 11:35:50 +1000 | [diff] [blame] | 2413 | /* |
| 2414 | * delay detach of session, but release pty, since |
| 2415 | * the fd's to the child are already closed |
| 2416 | */ |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2417 | if (s->ttyfd != -1) |
Damien Miller | 0585d51 | 2001-10-12 11:35:50 +1000 | [diff] [blame] | 2418 | session_pty_cleanup(s); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2419 | return; |
| 2420 | } |
| 2421 | /* detach by removing callback */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2422 | channel_cancel_cleanup(s->chanid); |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 2423 | |
| 2424 | /* Close any X11 listeners associated with this session */ |
| 2425 | if (s->x11_chanids != NULL) { |
| 2426 | for (i = 0; s->x11_chanids[i] != -1; i++) { |
| 2427 | session_close_x11(s->x11_chanids[i]); |
| 2428 | s->x11_chanids[i] = -1; |
| 2429 | } |
| 2430 | } |
| 2431 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2432 | s->chanid = -1; |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 2433 | session_close(s); |
| 2434 | } |
| 2435 | |
| 2436 | void |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2437 | session_destroy_all(void (*closefunc)(Session *)) |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 2438 | { |
| 2439 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 2440 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 2441 | Session *s = &sessions[i]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2442 | if (s->used) { |
| 2443 | if (closefunc != NULL) |
| 2444 | closefunc(s); |
| 2445 | else |
| 2446 | session_close(s); |
| 2447 | } |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 2448 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2449 | } |
| 2450 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2451 | static char * |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2452 | session_tty_list(void) |
| 2453 | { |
| 2454 | static char buf[1024]; |
| 2455 | int i; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 2456 | char *cp; |
| 2457 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2458 | buf[0] = '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 2459 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2460 | Session *s = &sessions[i]; |
| 2461 | if (s->used && s->ttyfd != -1) { |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2462 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 2463 | if (strncmp(s->tty, "/dev/", 5) != 0) { |
| 2464 | cp = strrchr(s->tty, '/'); |
| 2465 | cp = (cp == NULL) ? s->tty : cp + 1; |
| 2466 | } else |
| 2467 | cp = s->tty + 5; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2468 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2469 | if (buf[0] != '\0') |
| 2470 | strlcat(buf, ",", sizeof buf); |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 2471 | strlcat(buf, cp, sizeof buf); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2472 | } |
| 2473 | } |
| 2474 | if (buf[0] == '\0') |
| 2475 | strlcpy(buf, "notty", sizeof buf); |
| 2476 | return buf; |
| 2477 | } |
| 2478 | |
| 2479 | void |
| 2480 | session_proctitle(Session *s) |
| 2481 | { |
| 2482 | if (s->pw == NULL) |
| 2483 | error("no user for session %d", s->self); |
| 2484 | else |
| 2485 | setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); |
| 2486 | } |
| 2487 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2488 | int |
| 2489 | session_setup_x11fwd(Session *s) |
| 2490 | { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2491 | struct stat st; |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2492 | char display[512], auth_display[512]; |
| 2493 | char hostname[MAXHOSTNAMELEN]; |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2494 | u_int i; |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2495 | |
| 2496 | if (no_x11_forwarding_flag) { |
| 2497 | packet_send_debug("X11 forwarding disabled in user configuration file."); |
| 2498 | return 0; |
| 2499 | } |
| 2500 | if (!options.x11_forwarding) { |
| 2501 | debug("X11 forwarding disabled in server configuration file."); |
| 2502 | return 0; |
| 2503 | } |
| 2504 | if (!options.xauth_location || |
| 2505 | (stat(options.xauth_location, &st) == -1)) { |
| 2506 | packet_send_debug("No xauth program; cannot forward with spoofing."); |
| 2507 | return 0; |
| 2508 | } |
Ben Lindstrom | 699776e | 2001-06-21 03:14:49 +0000 | [diff] [blame] | 2509 | if (options.use_login) { |
| 2510 | packet_send_debug("X11 forwarding disabled; " |
| 2511 | "not compatible with UseLogin=yes."); |
| 2512 | return 0; |
| 2513 | } |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 2514 | if (s->display != NULL) { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2515 | debug("X11 display already set."); |
| 2516 | return 0; |
| 2517 | } |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 2518 | if (x11_create_display_inet(options.x11_display_offset, |
| 2519 | options.x11_use_localhost, s->single_connection, |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2520 | &s->display_number, &s->x11_chanids) == -1) { |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 2521 | debug("x11_create_display_inet failed."); |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2522 | return 0; |
| 2523 | } |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2524 | for (i = 0; s->x11_chanids[i] != -1; i++) { |
| 2525 | channel_register_cleanup(s->x11_chanids[i], |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 2526 | session_close_single_x11, 0); |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2527 | } |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2528 | |
| 2529 | /* Set up a suitable value for the DISPLAY variable. */ |
| 2530 | if (gethostname(hostname, sizeof(hostname)) < 0) |
| 2531 | fatal("gethostname: %.100s", strerror(errno)); |
| 2532 | /* |
| 2533 | * auth_display must be used as the displayname when the |
| 2534 | * authorization entry is added with xauth(1). This will be |
| 2535 | * different than the DISPLAY string for localhost displays. |
| 2536 | */ |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 2537 | if (options.x11_use_localhost) { |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 2538 | snprintf(display, sizeof display, "localhost:%u.%u", |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2539 | s->display_number, s->screen); |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 2540 | snprintf(auth_display, sizeof auth_display, "unix:%u.%u", |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 2541 | s->display_number, s->screen); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2542 | s->display = xstrdup(display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 2543 | s->auth_display = xstrdup(auth_display); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2544 | } else { |
| 2545 | #ifdef IPADDR_IN_DISPLAY |
| 2546 | struct hostent *he; |
| 2547 | struct in_addr my_addr; |
| 2548 | |
| 2549 | he = gethostbyname(hostname); |
| 2550 | if (he == NULL) { |
| 2551 | error("Can't get IP address for X11 DISPLAY."); |
| 2552 | packet_send_debug("Can't get IP address for X11 DISPLAY."); |
| 2553 | return 0; |
| 2554 | } |
| 2555 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 2556 | snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr), |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2557 | s->display_number, s->screen); |
| 2558 | #else |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 2559 | snprintf(display, sizeof display, "%.400s:%u.%u", hostname, |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2560 | s->display_number, s->screen); |
| 2561 | #endif |
| 2562 | s->display = xstrdup(display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 2563 | s->auth_display = xstrdup(display); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2566 | return 1; |
| 2567 | } |
| 2568 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2569 | static void |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 2570 | do_authenticated2(Authctxt *authctxt) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2571 | { |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 2572 | server_loop2(authctxt); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | void |
| 2576 | do_cleanup(Authctxt *authctxt) |
| 2577 | { |
| 2578 | static int called = 0; |
| 2579 | |
| 2580 | debug("do_cleanup"); |
| 2581 | |
| 2582 | /* no cleanup if we're in the child for login shell */ |
| 2583 | if (is_child) |
| 2584 | return; |
| 2585 | |
| 2586 | /* avoid double cleanup */ |
| 2587 | if (called) |
| 2588 | return; |
| 2589 | called = 1; |
| 2590 | |
Darren Tucker | 9142e1c | 2007-08-16 23:28:04 +1000 | [diff] [blame] | 2591 | if (authctxt == NULL) |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2592 | return; |
Darren Tucker | 9142e1c | 2007-08-16 23:28:04 +1000 | [diff] [blame] | 2593 | |
| 2594 | #ifdef USE_PAM |
| 2595 | if (options.use_pam) { |
| 2596 | sshpam_cleanup(); |
| 2597 | sshpam_thread_cleanup(); |
| 2598 | } |
| 2599 | #endif |
| 2600 | |
| 2601 | if (!authctxt->authenticated) |
| 2602 | return; |
| 2603 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2604 | #ifdef KRB5 |
| 2605 | if (options.kerberos_ticket_cleanup && |
| 2606 | authctxt->krb5_ctx) |
| 2607 | krb5_cleanup_proc(authctxt); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2608 | #endif |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2609 | |
| 2610 | #ifdef GSSAPI |
| 2611 | if (compat20 && options.gss_cleanup_creds) |
| 2612 | ssh_gssapi_cleanup_creds(); |
| 2613 | #endif |
| 2614 | |
| 2615 | /* remove agent socket */ |
| 2616 | auth_sock_cleanup_proc(authctxt->pw); |
| 2617 | |
| 2618 | /* |
| 2619 | * Cleanup ptys/utmp only if privsep is disabled, |
| 2620 | * or if running in monitor. |
| 2621 | */ |
| 2622 | if (!use_privsep || mm_is_monitor()) |
| 2623 | session_destroy_all(session_pty_cleanup2); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2624 | } |