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