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" |
Ben Lindstrom | c447fee | 2002-04-02 20:35:35 +0000 | [diff] [blame] | 36 | RCSID("$OpenBSD: session.c,v 1.134 2002/03/29 18:59:31 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 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 61 | #ifdef HAVE_CYGWIN |
| 62 | #include <windows.h> |
| 63 | #include <sys/cygwin.h> |
| 64 | #define is_winnt (GetVersion() < 0x80000000) |
| 65 | #endif |
| 66 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 67 | /* func */ |
| 68 | |
| 69 | Session *session_new(void); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 70 | void session_set_fds(Session *, int, int, int); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 71 | void session_pty_cleanup(void *); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 72 | void session_proctitle(Session *); |
| 73 | int session_setup_x11fwd(Session *); |
| 74 | void do_exec_pty(Session *, const char *); |
| 75 | void do_exec_no_pty(Session *, const char *); |
| 76 | void do_exec(Session *, const char *); |
| 77 | void do_login(Session *, const char *); |
Kevin Steves | a0957d6 | 2001-09-27 19:50:26 +0000 | [diff] [blame] | 78 | #ifdef LOGIN_NEEDS_UTMPX |
| 79 | static void do_pre_login(Session *s); |
| 80 | #endif |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 81 | void do_child(Session *, const char *); |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 82 | void do_motd(void); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 83 | int check_quietlogin(Session *, const char *); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 84 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 85 | static void do_authenticated1(Authctxt *); |
| 86 | static void do_authenticated2(Authctxt *); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 87 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 88 | static int session_pty_req(Session *); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 89 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 90 | /* import */ |
| 91 | extern ServerOptions options; |
| 92 | extern char *__progname; |
| 93 | extern int log_stderr; |
| 94 | extern int debug_flag; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 95 | extern u_int utmp_len; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 96 | extern int startup_pipe; |
Ben Lindstrom | 005dd22 | 2001-04-18 15:29:33 +0000 | [diff] [blame] | 97 | extern void destroy_sensitive_data(void); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 98 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 99 | /* original command from peer. */ |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 100 | const char *original_command = NULL; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 101 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 102 | /* data */ |
| 103 | #define MAX_SESSIONS 10 |
| 104 | Session sessions[MAX_SESSIONS]; |
Damien Miller | 15e7d4b | 2000-09-29 10:57:35 +1100 | [diff] [blame] | 105 | |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 106 | #ifdef WITH_AIXAUTHENTICATE |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 107 | char *aixloginmsg; |
| 108 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 109 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 110 | #ifdef HAVE_LOGIN_CAP |
Ben Lindstrom | b481e13 | 2002-03-22 01:35:47 +0000 | [diff] [blame] | 111 | login_cap_t *lc; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 112 | #endif |
| 113 | |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 114 | void |
| 115 | do_authenticated(Authctxt *authctxt) |
| 116 | { |
| 117 | /* |
| 118 | * Cancel the alarm we set to limit the time taken for |
| 119 | * authentication. |
| 120 | */ |
| 121 | alarm(0); |
| 122 | if (startup_pipe != -1) { |
| 123 | close(startup_pipe); |
| 124 | startup_pipe = -1; |
| 125 | } |
Damien Miller | e49d096 | 2001-11-13 23:46:18 +1100 | [diff] [blame] | 126 | #ifdef WITH_AIXAUTHENTICATE |
| 127 | /* We don't have a pty yet, so just label the line as "ssh" */ |
| 128 | if (loginsuccess(authctxt->user, |
Damien Miller | f3451a2 | 2002-02-05 12:40:46 +1100 | [diff] [blame] | 129 | get_canonical_hostname(options.verify_reverse_mapping), |
Damien Miller | e49d096 | 2001-11-13 23:46:18 +1100 | [diff] [blame] | 130 | "ssh", &aixloginmsg) < 0) |
| 131 | aixloginmsg = NULL; |
| 132 | #endif /* WITH_AIXAUTHENTICATE */ |
| 133 | |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 134 | /* setup the channel layer */ |
| 135 | if (!no_port_forwarding_flag && options.allow_tcp_forwarding) |
| 136 | channel_permit_all_opens(); |
| 137 | |
| 138 | if (compat20) |
| 139 | do_authenticated2(authctxt); |
| 140 | else |
| 141 | do_authenticated1(authctxt); |
Ben Lindstrom | 838394c | 2001-06-09 01:11:59 +0000 | [diff] [blame] | 142 | |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 143 | /* remove agent socket */ |
Ben Lindstrom | 838394c | 2001-06-09 01:11:59 +0000 | [diff] [blame] | 144 | if (auth_get_socket_name()) |
Ben Lindstrom | 983c098 | 2001-06-09 01:20:06 +0000 | [diff] [blame] | 145 | auth_sock_cleanup_proc(authctxt->pw); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 146 | #ifdef KRB4 |
| 147 | if (options.kerberos_ticket_cleanup) |
| 148 | krb4_cleanup_proc(authctxt); |
| 149 | #endif |
| 150 | #ifdef KRB5 |
| 151 | if (options.kerberos_ticket_cleanup) |
| 152 | krb5_cleanup_proc(authctxt); |
| 153 | #endif |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 156 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 157 | * Prepares for an interactive session. This is called after the user has |
| 158 | * been successfully authenticated. During this message exchange, pseudo |
| 159 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings |
| 160 | * are requested, etc. |
| 161 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 162 | static void |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 163 | do_authenticated1(Authctxt *authctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 164 | { |
| 165 | Session *s; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 166 | char *command; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 167 | int success, type, screen_flag; |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 168 | int compression_level = 0, enable_compression_after_reply = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 169 | u_int proto_len, data_len, dlen; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 170 | |
| 171 | s = session_new(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 172 | s->authctxt = authctxt; |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 173 | s->pw = authctxt->pw; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 174 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 175 | /* |
| 176 | * We stay in this loop until the client requests to execute a shell |
| 177 | * or a command. |
| 178 | */ |
| 179 | for (;;) { |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 180 | success = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 181 | |
| 182 | /* Get a packet from the client. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 183 | type = packet_read(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 184 | |
| 185 | /* Process the packet. */ |
| 186 | switch (type) { |
| 187 | case SSH_CMSG_REQUEST_COMPRESSION: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 188 | compression_level = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 189 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 190 | if (compression_level < 1 || compression_level > 9) { |
| 191 | packet_send_debug("Received illegal compression level %d.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 192 | compression_level); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | /* Enable compression after we have responded with SUCCESS. */ |
| 196 | enable_compression_after_reply = 1; |
| 197 | success = 1; |
| 198 | break; |
| 199 | |
| 200 | case SSH_CMSG_REQUEST_PTY: |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 201 | success = session_pty_req(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 202 | break; |
| 203 | |
| 204 | case SSH_CMSG_X11_REQUEST_FORWARDING: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 205 | s->auth_proto = packet_get_string(&proto_len); |
| 206 | s->auth_data = packet_get_string(&data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 207 | |
Ben Lindstrom | 7603b2d | 2001-02-26 20:13:32 +0000 | [diff] [blame] | 208 | screen_flag = packet_get_protocol_flags() & |
| 209 | SSH_PROTOFLAG_SCREEN_NUMBER; |
| 210 | debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag); |
| 211 | |
| 212 | if (packet_remaining() == 4) { |
| 213 | if (!screen_flag) |
| 214 | debug2("Buggy client: " |
| 215 | "X11 screen flag missing"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 216 | s->screen = packet_get_int(); |
Ben Lindstrom | 8dcdeb8 | 2001-02-16 16:02:14 +0000 | [diff] [blame] | 217 | } else { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 218 | s->screen = 0; |
Ben Lindstrom | 8dcdeb8 | 2001-02-16 16:02:14 +0000 | [diff] [blame] | 219 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 220 | packet_check_eom(); |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 221 | success = session_setup_x11fwd(s); |
| 222 | if (!success) { |
| 223 | xfree(s->auth_proto); |
| 224 | xfree(s->auth_data); |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 225 | s->auth_proto = NULL; |
| 226 | s->auth_data = NULL; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 227 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 228 | break; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 229 | |
| 230 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: |
| 231 | if (no_agent_forwarding_flag || compat13) { |
| 232 | debug("Authentication agent forwarding not permitted for this authentication."); |
| 233 | break; |
| 234 | } |
| 235 | debug("Received authentication agent forwarding request."); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 236 | success = auth_input_request_forwarding(s->pw); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | case SSH_CMSG_PORT_FORWARD_REQUEST: |
| 240 | if (no_port_forwarding_flag) { |
| 241 | debug("Port forwarding not permitted for this authentication."); |
| 242 | break; |
| 243 | } |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 244 | if (!options.allow_tcp_forwarding) { |
| 245 | debug("Port forwarding not permitted."); |
| 246 | break; |
| 247 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 248 | debug("Received TCP/IP port forwarding request."); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 249 | 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] | 250 | success = 1; |
| 251 | break; |
| 252 | |
| 253 | case SSH_CMSG_MAX_PACKET_SIZE: |
| 254 | if (packet_set_maxsize(packet_get_int()) > 0) |
| 255 | success = 1; |
| 256 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 257 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 258 | #if defined(AFS) || defined(KRB5) |
| 259 | case SSH_CMSG_HAVE_KERBEROS_TGT: |
| 260 | if (!options.kerberos_tgt_passing) { |
| 261 | verbose("Kerberos TGT passing disabled."); |
| 262 | } else { |
| 263 | char *kdata = packet_get_string(&dlen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 264 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 265 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 266 | /* XXX - 0x41, see creds_to_radix version */ |
| 267 | if (kdata[0] != 0x41) { |
| 268 | #ifdef KRB5 |
| 269 | krb5_data tgt; |
| 270 | tgt.data = kdata; |
| 271 | tgt.length = dlen; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 272 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 273 | if (auth_krb5_tgt(s->authctxt, &tgt)) |
| 274 | success = 1; |
| 275 | else |
| 276 | verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user); |
| 277 | #endif /* KRB5 */ |
| 278 | } else { |
| 279 | #ifdef AFS |
| 280 | if (auth_krb4_tgt(s->authctxt, kdata)) |
| 281 | success = 1; |
| 282 | else |
| 283 | verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user); |
| 284 | #endif /* AFS */ |
| 285 | } |
| 286 | xfree(kdata); |
| 287 | } |
| 288 | break; |
| 289 | #endif /* AFS || KRB5 */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 290 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 291 | #ifdef AFS |
| 292 | case SSH_CMSG_HAVE_AFS_TOKEN: |
| 293 | if (!options.afs_token_passing || !k_hasafs()) { |
| 294 | verbose("AFS token passing disabled."); |
| 295 | } else { |
| 296 | /* Accept AFS token. */ |
| 297 | char *token = packet_get_string(&dlen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 298 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 299 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 300 | if (auth_afs_token(s->authctxt, token)) |
| 301 | success = 1; |
| 302 | else |
| 303 | verbose("AFS token refused for %.100s", |
| 304 | s->authctxt->user); |
| 305 | xfree(token); |
| 306 | } |
| 307 | break; |
| 308 | #endif /* AFS */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 309 | |
| 310 | case SSH_CMSG_EXEC_SHELL: |
| 311 | case SSH_CMSG_EXEC_CMD: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 312 | if (type == SSH_CMSG_EXEC_CMD) { |
| 313 | command = packet_get_string(&dlen); |
| 314 | debug("Exec command '%.500s'", command); |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 315 | do_exec(s, command); |
| 316 | xfree(command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 317 | } else { |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 318 | do_exec(s, NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 319 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 320 | packet_check_eom(); |
Ben Lindstrom | cb3929d | 2001-06-09 01:34:15 +0000 | [diff] [blame] | 321 | session_close(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 322 | return; |
| 323 | |
| 324 | default: |
| 325 | /* |
| 326 | * Any unknown messages in this phase are ignored, |
| 327 | * and a failure message is returned. |
| 328 | */ |
| 329 | log("Unknown packet type received after authentication: %d", type); |
| 330 | } |
| 331 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); |
| 332 | packet_send(); |
| 333 | packet_write_wait(); |
| 334 | |
| 335 | /* Enable compression now that we have replied if appropriate. */ |
| 336 | if (enable_compression_after_reply) { |
| 337 | enable_compression_after_reply = 0; |
| 338 | packet_start_compression(compression_level); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * This is called to fork and execute a command when we have no tty. This |
| 345 | * will call do_child from the child, and server_loop from the parent after |
| 346 | * setting up file descriptors and such. |
| 347 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 348 | void |
Ben Lindstrom | b4c961d | 2001-03-22 01:25:37 +0000 | [diff] [blame] | 349 | do_exec_no_pty(Session *s, const char *command) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 350 | { |
| 351 | int pid; |
| 352 | |
| 353 | #ifdef USE_PIPES |
| 354 | int pin[2], pout[2], perr[2]; |
| 355 | /* Allocate pipes for communicating with the program. */ |
| 356 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) |
| 357 | packet_disconnect("Could not create pipes: %.100s", |
| 358 | strerror(errno)); |
| 359 | #else /* USE_PIPES */ |
| 360 | int inout[2], err[2]; |
| 361 | /* Uses socket pairs to communicate with the program. */ |
| 362 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || |
| 363 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) |
| 364 | packet_disconnect("Could not create socket pairs: %.100s", |
| 365 | strerror(errno)); |
| 366 | #endif /* USE_PIPES */ |
| 367 | if (s == NULL) |
| 368 | fatal("do_exec_no_pty: no session"); |
| 369 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 370 | session_proctitle(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 371 | |
Damien Miller | c594633 | 2001-02-28 11:46:11 +1100 | [diff] [blame] | 372 | #if defined(USE_PAM) |
Damien Miller | 665af9c | 2001-06-27 09:34:15 +1000 | [diff] [blame] | 373 | do_pam_session(s->pw->pw_name, NULL); |
Damien Miller | f9e9300 | 2001-03-27 16:12:24 +1000 | [diff] [blame] | 374 | do_pam_setcred(1); |
Damien Miller | c3aa3dd | 2001-10-28 22:34:52 +1100 | [diff] [blame] | 375 | if (is_pam_password_change_required()) |
| 376 | packet_disconnect("Password change required but no " |
| 377 | "TTY available"); |
Kevin Steves | ff793a2 | 2001-02-21 16:36:51 +0000 | [diff] [blame] | 378 | #endif /* USE_PAM */ |
| 379 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 380 | /* Fork the child. */ |
| 381 | if ((pid = fork()) == 0) { |
| 382 | /* Child. Reinitialize the log since the pid has changed. */ |
| 383 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
| 384 | |
| 385 | /* |
| 386 | * Create a new session and process group since the 4.4BSD |
| 387 | * setlogin() affects the entire process group. |
| 388 | */ |
| 389 | if (setsid() < 0) |
| 390 | error("setsid failed: %.100s", strerror(errno)); |
| 391 | |
| 392 | #ifdef USE_PIPES |
| 393 | /* |
| 394 | * Redirect stdin. We close the parent side of the socket |
| 395 | * pair, and make the child side the standard input. |
| 396 | */ |
| 397 | close(pin[1]); |
| 398 | if (dup2(pin[0], 0) < 0) |
| 399 | perror("dup2 stdin"); |
| 400 | close(pin[0]); |
| 401 | |
| 402 | /* Redirect stdout. */ |
| 403 | close(pout[0]); |
| 404 | if (dup2(pout[1], 1) < 0) |
| 405 | perror("dup2 stdout"); |
| 406 | close(pout[1]); |
| 407 | |
| 408 | /* Redirect stderr. */ |
| 409 | close(perr[0]); |
| 410 | if (dup2(perr[1], 2) < 0) |
| 411 | perror("dup2 stderr"); |
| 412 | close(perr[1]); |
| 413 | #else /* USE_PIPES */ |
| 414 | /* |
| 415 | * Redirect stdin, stdout, and stderr. Stdin and stdout will |
| 416 | * use the same socket, as some programs (particularly rdist) |
| 417 | * seem to depend on it. |
| 418 | */ |
| 419 | close(inout[1]); |
| 420 | close(err[1]); |
| 421 | if (dup2(inout[0], 0) < 0) /* stdin */ |
| 422 | perror("dup2 stdin"); |
| 423 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ |
| 424 | perror("dup2 stdout"); |
| 425 | if (dup2(err[0], 2) < 0) /* stderr */ |
| 426 | perror("dup2 stderr"); |
| 427 | #endif /* USE_PIPES */ |
| 428 | |
| 429 | /* Do processing for the child (exec command etc). */ |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 430 | do_child(s, command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 431 | /* NOTREACHED */ |
| 432 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 433 | #ifdef HAVE_CYGWIN |
| 434 | if (is_winnt) |
| 435 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); |
| 436 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 437 | if (pid < 0) |
| 438 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 439 | s->pid = pid; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 440 | /* Set interactive/non-interactive mode. */ |
| 441 | packet_set_interactive(s->display != NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 442 | #ifdef USE_PIPES |
| 443 | /* We are the parent. Close the child sides of the pipes. */ |
| 444 | close(pin[0]); |
| 445 | close(pout[1]); |
| 446 | close(perr[1]); |
| 447 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 448 | if (compat20) { |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 449 | 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] | 450 | } else { |
| 451 | /* Enter the interactive session. */ |
| 452 | server_loop(pid, pin[1], pout[0], perr[0]); |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 453 | /* server_loop has closed pin[1], pout[0], and perr[0]. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 454 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 455 | #else /* USE_PIPES */ |
| 456 | /* We are the parent. Close the child sides of the socket pairs. */ |
| 457 | close(inout[0]); |
| 458 | close(err[0]); |
| 459 | |
| 460 | /* |
| 461 | * Enter the interactive session. Note: server_loop must be able to |
| 462 | * handle the case that fdin and fdout are the same. |
| 463 | */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 464 | if (compat20) { |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 465 | 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] | 466 | } else { |
| 467 | server_loop(pid, inout[1], inout[1], err[1]); |
| 468 | /* server_loop has closed inout[1] and err[1]. */ |
| 469 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 470 | #endif /* USE_PIPES */ |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * This is called to fork and execute a command when we have a tty. This |
| 475 | * will call do_child from the child, and server_loop from the parent after |
| 476 | * setting up file descriptors, controlling tty, updating wtmp, utmp, |
| 477 | * lastlog, and other such operations. |
| 478 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 479 | void |
Ben Lindstrom | b4c961d | 2001-03-22 01:25:37 +0000 | [diff] [blame] | 480 | do_exec_pty(Session *s, const char *command) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 481 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 482 | int fdout, ptyfd, ttyfd, ptymaster; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 483 | pid_t pid; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 484 | |
| 485 | if (s == NULL) |
| 486 | fatal("do_exec_pty: no session"); |
| 487 | ptyfd = s->ptyfd; |
| 488 | ttyfd = s->ttyfd; |
| 489 | |
Damien Miller | c594633 | 2001-02-28 11:46:11 +1100 | [diff] [blame] | 490 | #if defined(USE_PAM) |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 491 | do_pam_session(s->pw->pw_name, s->tty); |
Damien Miller | f9e9300 | 2001-03-27 16:12:24 +1000 | [diff] [blame] | 492 | do_pam_setcred(1); |
Damien Miller | 5a76131 | 2001-02-27 09:28:23 +1100 | [diff] [blame] | 493 | #endif |
Kevin Steves | ff793a2 | 2001-02-21 16:36:51 +0000 | [diff] [blame] | 494 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 495 | /* Fork the child. */ |
| 496 | if ((pid = fork()) == 0) { |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 497 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 498 | /* Child. Reinitialize the log because the pid has changed. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 499 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 500 | /* Close the master side of the pseudo tty. */ |
| 501 | close(ptyfd); |
| 502 | |
| 503 | /* Make the pseudo tty our controlling tty. */ |
| 504 | pty_make_controlling_tty(&ttyfd, s->tty); |
| 505 | |
Damien Miller | 9c75142 | 2001-10-10 15:02:46 +1000 | [diff] [blame] | 506 | /* Redirect stdin/stdout/stderr from the pseudo tty. */ |
| 507 | if (dup2(ttyfd, 0) < 0) |
| 508 | error("dup2 stdin: %s", strerror(errno)); |
| 509 | if (dup2(ttyfd, 1) < 0) |
| 510 | error("dup2 stdout: %s", strerror(errno)); |
| 511 | if (dup2(ttyfd, 2) < 0) |
| 512 | error("dup2 stderr: %s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 513 | |
| 514 | /* Close the extra descriptor for the pseudo tty. */ |
| 515 | close(ttyfd); |
| 516 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 517 | /* record login, etc. similar to login(1) */ |
Damien Miller | 364a9bd | 2001-04-16 18:37:05 +1000 | [diff] [blame] | 518 | #ifndef HAVE_OSF_SIA |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 519 | if (!(options.use_login && command == NULL)) |
| 520 | do_login(s, command); |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 521 | # ifdef LOGIN_NEEDS_UTMPX |
| 522 | else |
| 523 | do_pre_login(s); |
| 524 | # endif |
Damien Miller | 364a9bd | 2001-04-16 18:37:05 +1000 | [diff] [blame] | 525 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 526 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 527 | /* Do common processing for the child, such as execing the command. */ |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 528 | do_child(s, command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 529 | /* NOTREACHED */ |
| 530 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 531 | #ifdef HAVE_CYGWIN |
| 532 | if (is_winnt) |
| 533 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); |
| 534 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 535 | if (pid < 0) |
| 536 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 537 | s->pid = pid; |
| 538 | |
| 539 | /* Parent. Close the slave side of the pseudo tty. */ |
| 540 | close(ttyfd); |
| 541 | |
| 542 | /* |
| 543 | * Create another descriptor of the pty master side for use as the |
| 544 | * standard input. We could use the original descriptor, but this |
| 545 | * simplifies code in server_loop. The descriptor is bidirectional. |
| 546 | */ |
| 547 | fdout = dup(ptyfd); |
| 548 | if (fdout < 0) |
| 549 | packet_disconnect("dup #1 failed: %.100s", strerror(errno)); |
| 550 | |
| 551 | /* we keep a reference to the pty master */ |
| 552 | ptymaster = dup(ptyfd); |
| 553 | if (ptymaster < 0) |
| 554 | packet_disconnect("dup #2 failed: %.100s", strerror(errno)); |
| 555 | s->ptymaster = ptymaster; |
| 556 | |
| 557 | /* Enter interactive session. */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 558 | packet_set_interactive(1); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 559 | if (compat20) { |
| 560 | session_set_fds(s, ptyfd, fdout, -1); |
| 561 | } else { |
| 562 | server_loop(pid, ptyfd, fdout, -1); |
| 563 | /* server_loop _has_ closed ptyfd and fdout. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 564 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 565 | } |
| 566 | |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 567 | #ifdef LOGIN_NEEDS_UTMPX |
Damien Miller | 599d8eb | 2001-09-15 12:25:53 +1000 | [diff] [blame] | 568 | static void |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 569 | do_pre_login(Session *s) |
| 570 | { |
| 571 | socklen_t fromlen; |
| 572 | struct sockaddr_storage from; |
| 573 | pid_t pid = getpid(); |
| 574 | |
| 575 | /* |
| 576 | * Get IP address of client. If the connection is not a socket, let |
| 577 | * the address be 0.0.0.0. |
| 578 | */ |
| 579 | memset(&from, 0, sizeof(from)); |
| 580 | if (packet_connection_is_on_socket()) { |
| 581 | fromlen = sizeof(from); |
| 582 | if (getpeername(packet_get_connection_in(), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 583 | (struct sockaddr *) & from, &fromlen) < 0) { |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 584 | debug("getpeername: %.100s", strerror(errno)); |
| 585 | fatal_cleanup(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | record_utmp_only(pid, s->tty, s->pw->pw_name, |
Damien Miller | f3451a2 | 2002-02-05 12:40:46 +1100 | [diff] [blame] | 590 | get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping), |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 591 | (struct sockaddr *)&from); |
| 592 | } |
| 593 | #endif |
| 594 | |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 595 | /* |
| 596 | * This is called to fork and execute a command. If another command is |
| 597 | * to be forced, execute that instead. |
| 598 | */ |
| 599 | void |
| 600 | do_exec(Session *s, const char *command) |
| 601 | { |
| 602 | if (forced_command) { |
| 603 | original_command = command; |
| 604 | command = forced_command; |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 605 | debug("Forced command '%.900s'", command); |
| 606 | } |
| 607 | |
| 608 | if (s->ttyfd != -1) |
| 609 | do_exec_pty(s, command); |
| 610 | else |
| 611 | do_exec_no_pty(s, command); |
| 612 | |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 613 | original_command = NULL; |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 616 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 617 | /* administrative, login(1)-like work */ |
| 618 | void |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 619 | do_login(Session *s, const char *command) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 620 | { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 621 | char *time_string; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 622 | socklen_t fromlen; |
| 623 | struct sockaddr_storage from; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 624 | struct passwd * pw = s->pw; |
| 625 | pid_t pid = getpid(); |
| 626 | |
| 627 | /* |
| 628 | * Get IP address of client. If the connection is not a socket, let |
| 629 | * the address be 0.0.0.0. |
| 630 | */ |
| 631 | memset(&from, 0, sizeof(from)); |
| 632 | if (packet_connection_is_on_socket()) { |
| 633 | fromlen = sizeof(from); |
| 634 | if (getpeername(packet_get_connection_in(), |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 635 | (struct sockaddr *) & from, &fromlen) < 0) { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 636 | debug("getpeername: %.100s", strerror(errno)); |
| 637 | fatal_cleanup(); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | /* 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] | 642 | if (!use_privsep) |
| 643 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, |
| 644 | get_remote_name_or_ip(utmp_len, |
| 645 | options.verify_reverse_mapping), |
| 646 | (struct sockaddr *)&from); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 647 | |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 648 | #ifdef USE_PAM |
| 649 | /* |
| 650 | * If password change is needed, do it now. |
| 651 | * This needs to occur before the ~/.hushlogin check. |
| 652 | */ |
Damien Miller | 646aa60 | 2001-02-15 11:51:32 +1100 | [diff] [blame] | 653 | if (is_pam_password_change_required()) { |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 654 | print_pam_messages(); |
| 655 | do_pam_chauthtok(); |
| 656 | } |
| 657 | #endif |
| 658 | |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 659 | if (check_quietlogin(s, command)) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 660 | return; |
| 661 | |
| 662 | #ifdef USE_PAM |
Damien Miller | 646aa60 | 2001-02-15 11:51:32 +1100 | [diff] [blame] | 663 | if (!is_pam_password_change_required()) |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 664 | print_pam_messages(); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 665 | #endif /* USE_PAM */ |
| 666 | #ifdef WITH_AIXAUTHENTICATE |
| 667 | if (aixloginmsg && *aixloginmsg) |
| 668 | printf("%s\n", aixloginmsg); |
| 669 | #endif /* WITH_AIXAUTHENTICATE */ |
| 670 | |
Ben Lindstrom | c447fee | 2002-04-02 20:35:35 +0000 | [diff] [blame] | 671 | if (options.print_lastlog && s->last_login_time != 0) { |
| 672 | time_string = ctime(&s->last_login_time); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 673 | if (strchr(time_string, '\n')) |
| 674 | *strchr(time_string, '\n') = 0; |
Ben Lindstrom | c447fee | 2002-04-02 20:35:35 +0000 | [diff] [blame] | 675 | if (strcmp(s->hostname, "") == 0) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 676 | printf("Last login: %s\r\n", time_string); |
| 677 | else |
Ben Lindstrom | c447fee | 2002-04-02 20:35:35 +0000 | [diff] [blame] | 678 | printf("Last login: %s from %s\r\n", time_string, |
| 679 | s->hostname); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 680 | } |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 681 | |
| 682 | do_motd(); |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Display the message of the day. |
| 687 | */ |
| 688 | void |
| 689 | do_motd(void) |
| 690 | { |
| 691 | FILE *f; |
| 692 | char buf[256]; |
| 693 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 694 | if (options.print_motd) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 695 | #ifdef HAVE_LOGIN_CAP |
| 696 | f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", |
| 697 | "/etc/motd"), "r"); |
| 698 | #else |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 699 | f = fopen("/etc/motd", "r"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 700 | #endif |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 701 | if (f) { |
| 702 | while (fgets(buf, sizeof(buf), f)) |
| 703 | fputs(buf, stdout); |
| 704 | fclose(f); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 709 | |
| 710 | /* |
| 711 | * Check for quiet login, either .hushlogin or command given. |
| 712 | */ |
| 713 | int |
| 714 | check_quietlogin(Session *s, const char *command) |
| 715 | { |
| 716 | char buf[256]; |
| 717 | struct passwd *pw = s->pw; |
| 718 | struct stat st; |
| 719 | |
| 720 | /* Return 1 if .hushlogin exists or a command given. */ |
| 721 | if (command != NULL) |
| 722 | return 1; |
| 723 | snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); |
| 724 | #ifdef HAVE_LOGIN_CAP |
| 725 | if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) |
| 726 | return 1; |
| 727 | #else |
| 728 | if (stat(buf, &st) >= 0) |
| 729 | return 1; |
| 730 | #endif |
| 731 | return 0; |
| 732 | } |
| 733 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 734 | /* |
| 735 | * Sets the value of the given variable in the environment. If the variable |
| 736 | * already exists, its value is overriden. |
| 737 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 738 | static void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 739 | child_set_env(char ***envp, u_int *envsizep, const char *name, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 740 | const char *value) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 741 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 742 | u_int i, namelen; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 743 | char **env; |
| 744 | |
| 745 | /* |
| 746 | * Find the slot where the value should be stored. If the variable |
| 747 | * already exists, we reuse the slot; otherwise we append a new slot |
| 748 | * at the end of the array, expanding if necessary. |
| 749 | */ |
| 750 | env = *envp; |
| 751 | namelen = strlen(name); |
| 752 | for (i = 0; env[i]; i++) |
| 753 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') |
| 754 | break; |
| 755 | if (env[i]) { |
| 756 | /* Reuse the slot. */ |
| 757 | xfree(env[i]); |
| 758 | } else { |
| 759 | /* New variable. Expand if necessary. */ |
| 760 | if (i >= (*envsizep) - 1) { |
| 761 | (*envsizep) += 50; |
| 762 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); |
| 763 | } |
| 764 | /* Need to set the NULL pointer at end of array beyond the new slot. */ |
| 765 | env[i + 1] = NULL; |
| 766 | } |
| 767 | |
| 768 | /* Allocate space and format the variable in the appropriate slot. */ |
| 769 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); |
| 770 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Reads environment variables from the given file and adds/overrides them |
| 775 | * into the environment. If the file does not exist, this does nothing. |
| 776 | * Otherwise, it must consist of empty lines, comments (line starts with '#') |
| 777 | * and assignments of the form name=value. No other forms are allowed. |
| 778 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 779 | static void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 780 | read_environment_file(char ***env, u_int *envsize, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 781 | const char *filename) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 782 | { |
| 783 | FILE *f; |
| 784 | char buf[4096]; |
| 785 | char *cp, *value; |
| 786 | |
| 787 | f = fopen(filename, "r"); |
| 788 | if (!f) |
| 789 | return; |
| 790 | |
| 791 | while (fgets(buf, sizeof(buf), f)) { |
| 792 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
| 793 | ; |
| 794 | if (!*cp || *cp == '#' || *cp == '\n') |
| 795 | continue; |
| 796 | if (strchr(cp, '\n')) |
| 797 | *strchr(cp, '\n') = '\0'; |
| 798 | value = strchr(cp, '='); |
| 799 | if (value == NULL) { |
| 800 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); |
| 801 | continue; |
| 802 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 803 | /* |
| 804 | * Replace the equals sign by nul, and advance value to |
| 805 | * the value string. |
| 806 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 807 | *value = '\0'; |
| 808 | value++; |
| 809 | child_set_env(env, envsize, cp, value); |
| 810 | } |
| 811 | fclose(f); |
| 812 | } |
| 813 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 814 | void copy_environment(char **source, char ***env, u_int *envsize) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 815 | { |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 816 | char *var_name, *var_val; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 817 | int i; |
| 818 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 819 | if (source == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 820 | return; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 821 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 822 | for(i = 0; source[i] != NULL; i++) { |
| 823 | var_name = xstrdup(source[i]); |
| 824 | if ((var_val = strstr(var_name, "=")) == NULL) { |
| 825 | xfree(var_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 826 | continue; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 827 | } |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 828 | *var_val++ = '\0'; |
| 829 | |
| 830 | debug3("Copy environment: %s=%s", var_name, var_val); |
| 831 | child_set_env(env, envsize, var_name, var_val); |
| 832 | |
| 833 | xfree(var_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 834 | } |
| 835 | } |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 836 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 837 | static char ** |
| 838 | do_setup_env(Session *s, const char *shell) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 839 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 840 | char buf[256]; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 841 | u_int i, envsize; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 842 | char **env; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 843 | struct passwd *pw = s->pw; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 844 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 845 | /* Initialize the environment. */ |
| 846 | envsize = 100; |
| 847 | env = xmalloc(envsize * sizeof(char *)); |
| 848 | env[0] = NULL; |
| 849 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 850 | #ifdef HAVE_CYGWIN |
| 851 | /* |
| 852 | * The Windows environment contains some setting which are |
| 853 | * important for a running system. They must not be dropped. |
| 854 | */ |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 855 | copy_environment(environ, &env, &envsize); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 856 | #endif |
| 857 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 858 | if (!options.use_login) { |
| 859 | /* Set basic environment. */ |
| 860 | child_set_env(&env, &envsize, "USER", pw->pw_name); |
| 861 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); |
| 862 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 863 | #ifdef HAVE_LOGIN_CAP |
| 864 | (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH); |
| 865 | child_set_env(&env, &envsize, "PATH", getenv("PATH")); |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 866 | #else /* HAVE_LOGIN_CAP */ |
| 867 | # ifndef HAVE_CYGWIN |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 868 | /* |
| 869 | * There's no standard path on Windows. The path contains |
| 870 | * important components pointing to the system directories, |
| 871 | * needed for loading shared libraries. So the path better |
| 872 | * remains intact here. |
| 873 | */ |
Damien Miller | a18bbd3 | 2002-05-13 10:48:57 +1000 | [diff] [blame^] | 874 | # ifdef SUPERUSER_PATH |
| 875 | child_set_env(&env, &envsize, "PATH", |
| 876 | s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH); |
| 877 | # else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 878 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); |
Damien Miller | a18bbd3 | 2002-05-13 10:48:57 +1000 | [diff] [blame^] | 879 | # endif /* SUPERUSER_PATH */ |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 880 | # endif /* HAVE_CYGWIN */ |
| 881 | #endif /* HAVE_LOGIN_CAP */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 882 | |
| 883 | snprintf(buf, sizeof buf, "%.200s/%.50s", |
| 884 | _PATH_MAILDIR, pw->pw_name); |
| 885 | child_set_env(&env, &envsize, "MAIL", buf); |
| 886 | |
| 887 | /* Normal systems set SHELL by default. */ |
| 888 | child_set_env(&env, &envsize, "SHELL", shell); |
| 889 | } |
| 890 | if (getenv("TZ")) |
| 891 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); |
| 892 | |
| 893 | /* Set custom environment options from RSA authentication. */ |
Ben Lindstrom | 38b951c | 2001-12-06 17:47:47 +0000 | [diff] [blame] | 894 | if (!options.use_login) { |
| 895 | while (custom_environment) { |
| 896 | struct envstring *ce = custom_environment; |
| 897 | char *s = ce->s; |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 898 | |
Ben Lindstrom | 38b951c | 2001-12-06 17:47:47 +0000 | [diff] [blame] | 899 | for (i = 0; s[i] != '=' && s[i]; i++) |
| 900 | ; |
| 901 | if (s[i] == '=') { |
| 902 | s[i] = 0; |
| 903 | child_set_env(&env, &envsize, s, s + i + 1); |
| 904 | } |
| 905 | custom_environment = ce->next; |
| 906 | xfree(ce->s); |
| 907 | xfree(ce); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 908 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | snprintf(buf, sizeof buf, "%.50s %d %d", |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 912 | get_remote_ipaddr(), get_remote_port(), get_local_port()); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 913 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); |
| 914 | |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 915 | if (s->ttyfd != -1) |
| 916 | child_set_env(&env, &envsize, "SSH_TTY", s->tty); |
| 917 | if (s->term) |
| 918 | child_set_env(&env, &envsize, "TERM", s->term); |
| 919 | if (s->display) |
| 920 | child_set_env(&env, &envsize, "DISPLAY", s->display); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 921 | if (original_command) |
| 922 | child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", |
| 923 | original_command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 924 | |
| 925 | #ifdef _AIX |
Ben Lindstrom | 839ac4f | 2002-02-24 20:42:46 +0000 | [diff] [blame] | 926 | { |
| 927 | char *cp; |
| 928 | |
| 929 | if ((cp = getenv("AUTHSTATE")) != NULL) |
| 930 | child_set_env(&env, &envsize, "AUTHSTATE", cp); |
| 931 | if ((cp = getenv("KRB5CCNAME")) != NULL) |
| 932 | child_set_env(&env, &envsize, "KRB5CCNAME", cp); |
| 933 | read_environment_file(&env, &envsize, "/etc/environment"); |
| 934 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 935 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 936 | #ifdef KRB4 |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 937 | if (s->authctxt->krb4_ticket_file) |
| 938 | child_set_env(&env, &envsize, "KRBTKFILE", |
| 939 | s->authctxt->krb4_ticket_file); |
| 940 | #endif |
| 941 | #ifdef KRB5 |
| 942 | if (s->authctxt->krb5_ticket_file) |
| 943 | child_set_env(&env, &envsize, "KRB5CCNAME", |
| 944 | s->authctxt->krb5_ticket_file); |
| 945 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 946 | #ifdef USE_PAM |
| 947 | /* Pull in any environment variables that may have been set by PAM. */ |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 948 | copy_environment(fetch_pam_environment(), &env, &envsize); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 949 | #endif /* USE_PAM */ |
| 950 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 951 | if (auth_get_socket_name() != NULL) |
| 952 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 953 | auth_get_socket_name()); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 954 | |
| 955 | /* read $HOME/.ssh/environment. */ |
| 956 | if (!options.use_login) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 957 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", |
| 958 | pw->pw_dir); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 959 | read_environment_file(&env, &envsize, buf); |
| 960 | } |
| 961 | if (debug_flag) { |
| 962 | /* dump the environment */ |
| 963 | fprintf(stderr, "Environment:\n"); |
| 964 | for (i = 0; env[i]; i++) |
| 965 | fprintf(stderr, " %.200s\n", env[i]); |
| 966 | } |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 967 | return env; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found |
| 972 | * first in this order). |
| 973 | */ |
| 974 | static void |
| 975 | do_rc_files(Session *s, const char *shell) |
| 976 | { |
| 977 | FILE *f = NULL; |
| 978 | char cmd[1024]; |
| 979 | int do_xauth; |
| 980 | struct stat st; |
| 981 | |
| 982 | do_xauth = |
| 983 | s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; |
| 984 | |
| 985 | /* ignore _PATH_SSH_USER_RC for subsystems */ |
| 986 | if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) { |
| 987 | snprintf(cmd, sizeof cmd, "%s -c '%s %s'", |
| 988 | shell, _PATH_BSHELL, _PATH_SSH_USER_RC); |
| 989 | if (debug_flag) |
| 990 | fprintf(stderr, "Running %s\n", cmd); |
| 991 | f = popen(cmd, "w"); |
| 992 | if (f) { |
| 993 | if (do_xauth) |
| 994 | fprintf(f, "%s %s\n", s->auth_proto, |
| 995 | s->auth_data); |
| 996 | pclose(f); |
| 997 | } else |
| 998 | fprintf(stderr, "Could not run %s\n", |
| 999 | _PATH_SSH_USER_RC); |
| 1000 | } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { |
| 1001 | if (debug_flag) |
| 1002 | fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, |
| 1003 | _PATH_SSH_SYSTEM_RC); |
| 1004 | f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); |
| 1005 | if (f) { |
| 1006 | if (do_xauth) |
| 1007 | fprintf(f, "%s %s\n", s->auth_proto, |
| 1008 | s->auth_data); |
| 1009 | pclose(f); |
| 1010 | } else |
| 1011 | fprintf(stderr, "Could not run %s\n", |
| 1012 | _PATH_SSH_SYSTEM_RC); |
| 1013 | } else if (do_xauth && options.xauth_location != NULL) { |
| 1014 | /* Add authority data to .Xauthority if appropriate. */ |
| 1015 | if (debug_flag) { |
| 1016 | fprintf(stderr, |
| 1017 | "Running %.100s add " |
| 1018 | "%.100s %.100s %.100s\n", |
| 1019 | options.xauth_location, s->auth_display, |
| 1020 | s->auth_proto, s->auth_data); |
| 1021 | } |
| 1022 | snprintf(cmd, sizeof cmd, "%s -q -", |
| 1023 | options.xauth_location); |
| 1024 | f = popen(cmd, "w"); |
| 1025 | if (f) { |
| 1026 | fprintf(f, "add %s %s %s\n", |
| 1027 | s->auth_display, s->auth_proto, |
| 1028 | s->auth_data); |
| 1029 | pclose(f); |
| 1030 | } else { |
| 1031 | fprintf(stderr, "Could not run %s\n", |
| 1032 | cmd); |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | static void |
| 1038 | do_nologin(struct passwd *pw) |
| 1039 | { |
| 1040 | FILE *f = NULL; |
| 1041 | char buf[1024]; |
| 1042 | |
| 1043 | #ifdef HAVE_LOGIN_CAP |
| 1044 | if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid) |
| 1045 | f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN, |
| 1046 | _PATH_NOLOGIN), "r"); |
| 1047 | #else |
| 1048 | if (pw->pw_uid) |
| 1049 | f = fopen(_PATH_NOLOGIN, "r"); |
| 1050 | #endif |
| 1051 | if (f) { |
| 1052 | /* /etc/nologin exists. Print its contents and exit. */ |
| 1053 | while (fgets(buf, sizeof(buf), f)) |
| 1054 | fputs(buf, stderr); |
| 1055 | fclose(f); |
| 1056 | exit(254); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /* Set login name, uid, gid, and groups. */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1061 | void |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1062 | do_setusercontext(struct passwd *pw) |
| 1063 | { |
| 1064 | #ifdef HAVE_CYGWIN |
Ben Lindstrom | 3107efc | 2002-02-21 15:37:02 +0000 | [diff] [blame] | 1065 | if (is_winnt) { |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1066 | #else /* HAVE_CYGWIN */ |
| 1067 | if (getuid() == 0 || geteuid() == 0) { |
| 1068 | #endif /* HAVE_CYGWIN */ |
| 1069 | #ifdef HAVE_GETUSERATTR |
| 1070 | set_limits_from_userattr(pw->pw_name); |
| 1071 | #endif /* HAVE_GETUSERATTR */ |
| 1072 | #ifdef HAVE_LOGIN_CAP |
| 1073 | if (setusercontext(lc, pw, pw->pw_uid, |
| 1074 | (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) { |
| 1075 | perror("unable to set user context"); |
| 1076 | exit(1); |
| 1077 | } |
| 1078 | #else |
| 1079 | # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID) |
| 1080 | /* Sets login uid for accounting */ |
| 1081 | if (getluid() == -1 && setluid(pw->pw_uid) == -1) |
| 1082 | error("setluid: %s", strerror(errno)); |
| 1083 | # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */ |
| 1084 | |
| 1085 | if (setlogin(pw->pw_name) < 0) |
| 1086 | error("setlogin failed: %s", strerror(errno)); |
| 1087 | if (setgid(pw->pw_gid) < 0) { |
| 1088 | perror("setgid"); |
| 1089 | exit(1); |
| 1090 | } |
| 1091 | /* Initialize the group list. */ |
| 1092 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { |
| 1093 | perror("initgroups"); |
| 1094 | exit(1); |
| 1095 | } |
| 1096 | endgrent(); |
| 1097 | # ifdef USE_PAM |
| 1098 | /* |
| 1099 | * PAM credentials may take the form of supplementary groups. |
| 1100 | * These will have been wiped by the above initgroups() call. |
| 1101 | * Reestablish them here. |
| 1102 | */ |
| 1103 | do_pam_setcred(0); |
| 1104 | # endif /* USE_PAM */ |
| 1105 | # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) |
| 1106 | irix_setusercontext(pw); |
| 1107 | # endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1108 | /* Permanently switch to the desired uid. */ |
| 1109 | permanently_set_uid(pw); |
| 1110 | #endif |
| 1111 | } |
| 1112 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) |
| 1113 | fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); |
| 1114 | } |
| 1115 | |
Ben Lindstrom | 0810519 | 2002-03-22 02:50:06 +0000 | [diff] [blame] | 1116 | static void |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1117 | launch_login(struct passwd *pw, const char *hostname) |
| 1118 | { |
| 1119 | /* Launch login(1). */ |
| 1120 | |
| 1121 | execl("/usr/bin/login", "login", "-h", hostname, |
Kevin Steves | b4799a3 | 2002-03-24 23:19:54 +0000 | [diff] [blame] | 1122 | #ifdef xxxLOGIN_NEEDS_TERM |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1123 | (s->term ? s->term : "unknown"), |
| 1124 | #endif /* LOGIN_NEEDS_TERM */ |
Kevin Steves | 5feaaef | 2002-04-23 20:45:55 +0000 | [diff] [blame] | 1125 | #ifdef LOGIN_NO_ENDOPT |
| 1126 | "-p", "-f", pw->pw_name, (char *)NULL); |
| 1127 | #else |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1128 | "-p", "-f", "--", pw->pw_name, (char *)NULL); |
Kevin Steves | 5feaaef | 2002-04-23 20:45:55 +0000 | [diff] [blame] | 1129 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* Login couldn't be executed, die. */ |
| 1132 | |
| 1133 | perror("login"); |
| 1134 | exit(1); |
| 1135 | } |
| 1136 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1137 | /* |
| 1138 | * Performs common processing for the child, such as setting up the |
| 1139 | * environment, closing extra file descriptors, setting the user and group |
| 1140 | * ids, and executing the command or shell. |
| 1141 | */ |
| 1142 | void |
| 1143 | do_child(Session *s, const char *command) |
| 1144 | { |
| 1145 | extern char **environ; |
| 1146 | char **env; |
| 1147 | char *argv[10]; |
| 1148 | const char *shell, *shell0, *hostname = NULL; |
| 1149 | struct passwd *pw = s->pw; |
| 1150 | u_int i; |
| 1151 | |
| 1152 | /* remove hostkey from the child's memory */ |
| 1153 | destroy_sensitive_data(); |
| 1154 | |
| 1155 | /* login(1) is only called if we execute the login shell */ |
| 1156 | if (options.use_login && command != NULL) |
| 1157 | options.use_login = 0; |
| 1158 | |
| 1159 | /* |
| 1160 | * Login(1) does this as well, and it needs uid 0 for the "-h" |
| 1161 | * switch, so we let login(1) to this for us. |
| 1162 | */ |
| 1163 | if (!options.use_login) { |
| 1164 | #ifdef HAVE_OSF_SIA |
| 1165 | session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty); |
| 1166 | if (!check_quietlogin(s, command)) |
| 1167 | do_motd(); |
| 1168 | #else /* HAVE_OSF_SIA */ |
| 1169 | do_nologin(pw); |
Ben Lindstrom | c004135 | 2002-02-25 15:48:02 +0000 | [diff] [blame] | 1170 | # ifdef _AIX |
| 1171 | aix_usrinfo(pw, s->tty, s->ttyfd); |
| 1172 | # endif /* _AIX */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1173 | do_setusercontext(pw); |
| 1174 | #endif /* HAVE_OSF_SIA */ |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Get the shell from the password data. An empty shell field is |
| 1179 | * legal, and means /bin/sh. |
| 1180 | */ |
| 1181 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
| 1182 | #ifdef HAVE_LOGIN_CAP |
| 1183 | shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); |
| 1184 | #endif |
| 1185 | |
| 1186 | env = do_setup_env(s, shell); |
| 1187 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1188 | /* we have to stash the hostname before we close our socket. */ |
| 1189 | if (options.use_login) |
Ben Lindstrom | f15a386 | 2001-04-05 23:32:17 +0000 | [diff] [blame] | 1190 | hostname = get_remote_name_or_ip(utmp_len, |
Damien Miller | c5d8635 | 2002-02-05 12:13:41 +1100 | [diff] [blame] | 1191 | options.verify_reverse_mapping); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1192 | /* |
| 1193 | * Close the connection descriptors; note that this is the child, and |
| 1194 | * the server will still have the socket open, and it is important |
| 1195 | * that we do not shutdown it. Note that the descriptors cannot be |
| 1196 | * closed before building the environment, as we call |
| 1197 | * get_remote_ipaddr there. |
| 1198 | */ |
| 1199 | if (packet_get_connection_in() == packet_get_connection_out()) |
| 1200 | close(packet_get_connection_in()); |
| 1201 | else { |
| 1202 | close(packet_get_connection_in()); |
| 1203 | close(packet_get_connection_out()); |
| 1204 | } |
| 1205 | /* |
| 1206 | * Close all descriptors related to channels. They will still remain |
| 1207 | * open in the parent. |
| 1208 | */ |
| 1209 | /* XXX better use close-on-exec? -markus */ |
| 1210 | channel_close_all(); |
| 1211 | |
| 1212 | /* |
| 1213 | * Close any extra file descriptors. Note that there may still be |
| 1214 | * descriptors left by system functions. They will be closed later. |
| 1215 | */ |
| 1216 | endpwent(); |
| 1217 | |
| 1218 | /* |
| 1219 | * Close any extra open file descriptors so that we don\'t have them |
| 1220 | * hanging around in clients. Note that we want to do this after |
| 1221 | * initgroups, because at least on Solaris 2.3 it leaves file |
| 1222 | * descriptors open. |
| 1223 | */ |
| 1224 | for (i = 3; i < 64; i++) |
| 1225 | close(i); |
| 1226 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1227 | /* |
Damien Miller | 05eda43 | 2002-02-10 18:32:28 +1100 | [diff] [blame] | 1228 | * Must take new environment into use so that .ssh/rc, |
| 1229 | * /etc/ssh/sshrc and xauth are run in the proper environment. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1230 | */ |
| 1231 | environ = env; |
| 1232 | |
Ben Lindstrom | 37e41c9 | 2001-09-16 22:17:15 +0000 | [diff] [blame] | 1233 | #ifdef AFS |
| 1234 | /* Try to get AFS tokens for the local cell. */ |
| 1235 | if (k_hasafs()) { |
| 1236 | char cell[64]; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1237 | |
Ben Lindstrom | 37e41c9 | 2001-09-16 22:17:15 +0000 | [diff] [blame] | 1238 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) |
| 1239 | krb_afslog(cell, 0); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1240 | |
Ben Lindstrom | 37e41c9 | 2001-09-16 22:17:15 +0000 | [diff] [blame] | 1241 | krb_afslog(0, 0); |
| 1242 | } |
| 1243 | #endif /* AFS */ |
| 1244 | |
Damien Miller | 139d4cd | 2001-10-10 15:07:44 +1000 | [diff] [blame] | 1245 | /* Change current directory to the user\'s home directory. */ |
| 1246 | if (chdir(pw->pw_dir) < 0) { |
| 1247 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", |
| 1248 | pw->pw_dir, strerror(errno)); |
| 1249 | #ifdef HAVE_LOGIN_CAP |
| 1250 | if (login_getcapbool(lc, "requirehome", 0)) |
| 1251 | exit(1); |
| 1252 | #endif |
| 1253 | } |
| 1254 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1255 | if (!options.use_login) |
| 1256 | do_rc_files(s, shell); |
Ben Lindstrom | de71cda | 2001-03-24 00:43:26 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* restore SIGPIPE for child */ |
| 1259 | signal(SIGPIPE, SIG_DFL); |
| 1260 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1261 | if (options.use_login) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1262 | launch_login(pw, hostname); |
| 1263 | /* NEVERREACHED */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | /* Get the last component of the shell name. */ |
| 1267 | if ((shell0 = strrchr(shell, '/')) != NULL) |
| 1268 | shell0++; |
| 1269 | else |
| 1270 | shell0 = shell; |
| 1271 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1272 | /* |
| 1273 | * If we have no command, execute the shell. In this case, the shell |
| 1274 | * name to be passed in argv[0] is preceded by '-' to indicate that |
| 1275 | * this is a login shell. |
| 1276 | */ |
| 1277 | if (!command) { |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1278 | char argv0[256]; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1279 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1280 | /* Start the shell. Set initial character to '-'. */ |
| 1281 | argv0[0] = '-'; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1282 | |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1283 | if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1) |
| 1284 | >= sizeof(argv0) - 1) { |
| 1285 | errno = EINVAL; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1286 | perror(shell); |
| 1287 | exit(1); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1288 | } |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1289 | |
| 1290 | /* Execute the shell. */ |
| 1291 | argv[0] = argv0; |
| 1292 | argv[1] = NULL; |
| 1293 | execve(shell, argv, env); |
| 1294 | |
| 1295 | /* Executing the shell failed. */ |
| 1296 | perror(shell); |
| 1297 | exit(1); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1298 | } |
| 1299 | /* |
| 1300 | * Execute the command using the user's shell. This uses the -c |
| 1301 | * option to execute the command. |
| 1302 | */ |
Ben Lindstrom | 4e97e85 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1303 | argv[0] = (char *) shell0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1304 | argv[1] = "-c"; |
| 1305 | argv[2] = (char *) command; |
| 1306 | argv[3] = NULL; |
| 1307 | execve(shell, argv, env); |
| 1308 | perror(shell); |
| 1309 | exit(1); |
| 1310 | } |
| 1311 | |
| 1312 | Session * |
| 1313 | session_new(void) |
| 1314 | { |
| 1315 | int i; |
| 1316 | static int did_init = 0; |
| 1317 | if (!did_init) { |
| 1318 | debug("session_new: init"); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1319 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1320 | sessions[i].used = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1321 | } |
| 1322 | did_init = 1; |
| 1323 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1324 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1325 | Session *s = &sessions[i]; |
| 1326 | if (! s->used) { |
Ben Lindstrom | 6029432 | 2001-03-26 05:38:25 +0000 | [diff] [blame] | 1327 | memset(s, 0, sizeof(*s)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1328 | s->chanid = -1; |
| 1329 | s->ptyfd = -1; |
| 1330 | s->ttyfd = -1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1331 | s->used = 1; |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1332 | s->self = i; |
Damien Miller | 15b2952 | 2000-10-14 12:33:48 +1100 | [diff] [blame] | 1333 | debug("session_new: session %d", i); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1334 | return s; |
| 1335 | } |
| 1336 | } |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1340 | static void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1341 | session_dump(void) |
| 1342 | { |
| 1343 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1344 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1345 | Session *s = &sessions[i]; |
| 1346 | debug("dump: used %d session %d %p channel %d pid %d", |
| 1347 | s->used, |
| 1348 | s->self, |
| 1349 | s, |
| 1350 | s->chanid, |
| 1351 | s->pid); |
| 1352 | } |
| 1353 | } |
| 1354 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1355 | int |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 1356 | session_open(Authctxt *authctxt, int chanid) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1357 | { |
| 1358 | Session *s = session_new(); |
| 1359 | debug("session_open: channel %d", chanid); |
| 1360 | if (s == NULL) { |
| 1361 | error("no more sessions"); |
| 1362 | return 0; |
| 1363 | } |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 1364 | s->authctxt = authctxt; |
| 1365 | s->pw = authctxt->pw; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1366 | if (s->pw == NULL) |
Kevin Steves | 43cdef3 | 2001-02-11 14:12:08 +0000 | [diff] [blame] | 1367 | fatal("no user for session %d", s->self); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1368 | debug("session_open: session %d: link with channel %d", s->self, chanid); |
| 1369 | s->chanid = chanid; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1370 | return 1; |
| 1371 | } |
| 1372 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1373 | Session * |
| 1374 | session_by_tty(char *tty) |
| 1375 | { |
| 1376 | int i; |
| 1377 | for (i = 0; i < MAX_SESSIONS; i++) { |
| 1378 | Session *s = &sessions[i]; |
| 1379 | if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) { |
| 1380 | debug("session_by_tty: session %d tty %s", i, tty); |
| 1381 | return s; |
| 1382 | } |
| 1383 | } |
| 1384 | debug("session_by_tty: unknown tty %.100s", tty); |
| 1385 | session_dump(); |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1389 | static Session * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1390 | session_by_channel(int id) |
| 1391 | { |
| 1392 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1393 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1394 | Session *s = &sessions[i]; |
| 1395 | if (s->used && s->chanid == id) { |
| 1396 | debug("session_by_channel: session %d channel %d", i, id); |
| 1397 | return s; |
| 1398 | } |
| 1399 | } |
| 1400 | debug("session_by_channel: unknown channel %d", id); |
| 1401 | session_dump(); |
| 1402 | return NULL; |
| 1403 | } |
| 1404 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1405 | static Session * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1406 | session_by_pid(pid_t pid) |
| 1407 | { |
| 1408 | int i; |
| 1409 | debug("session_by_pid: pid %d", pid); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1410 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1411 | Session *s = &sessions[i]; |
| 1412 | if (s->used && s->pid == pid) |
| 1413 | return s; |
| 1414 | } |
| 1415 | error("session_by_pid: unknown pid %d", pid); |
| 1416 | session_dump(); |
| 1417 | return NULL; |
| 1418 | } |
| 1419 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1420 | static int |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1421 | session_window_change_req(Session *s) |
| 1422 | { |
| 1423 | s->col = packet_get_int(); |
| 1424 | s->row = packet_get_int(); |
| 1425 | s->xpixel = packet_get_int(); |
| 1426 | s->ypixel = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1427 | packet_check_eom(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1428 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1432 | static int |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1433 | session_pty_req(Session *s) |
| 1434 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1435 | u_int len; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 1436 | int n_bytes; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1437 | |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1438 | if (no_pty_flag) { |
| 1439 | debug("Allocating a pty not permitted for this authentication."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1440 | return 0; |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1441 | } |
| 1442 | if (s->ttyfd != -1) { |
| 1443 | packet_disconnect("Protocol error: you already have a pty."); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1444 | return 0; |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1445 | } |
Ben Lindstrom | c447fee | 2002-04-02 20:35:35 +0000 | [diff] [blame] | 1446 | /* Get the time and hostname when the user last logged in. */ |
| 1447 | if (options.print_lastlog) { |
| 1448 | s->hostname[0] = '\0'; |
| 1449 | s->last_login_time = get_last_login_time(s->pw->pw_uid, |
| 1450 | s->pw->pw_name, s->hostname, sizeof(s->hostname)); |
| 1451 | } |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1452 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1453 | s->term = packet_get_string(&len); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (compat20) { |
| 1456 | s->col = packet_get_int(); |
| 1457 | s->row = packet_get_int(); |
| 1458 | } else { |
| 1459 | s->row = packet_get_int(); |
| 1460 | s->col = packet_get_int(); |
| 1461 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1462 | s->xpixel = packet_get_int(); |
| 1463 | s->ypixel = packet_get_int(); |
| 1464 | |
| 1465 | if (strcmp(s->term, "") == 0) { |
| 1466 | xfree(s->term); |
| 1467 | s->term = NULL; |
| 1468 | } |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1469 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1470 | /* Allocate a pty and open it. */ |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1471 | debug("Allocating pty."); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1472 | 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] | 1473 | if (s->term) |
| 1474 | xfree(s->term); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1475 | s->term = NULL; |
| 1476 | s->ptyfd = -1; |
| 1477 | s->ttyfd = -1; |
| 1478 | error("session_pty_req: session %d alloc failed", s->self); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1479 | return 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1480 | } |
| 1481 | debug("session_pty_req: session %d alloc %s", s->self, s->tty); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1482 | |
| 1483 | /* for SSH1 the tty modes length is not given */ |
| 1484 | if (!compat20) |
| 1485 | n_bytes = packet_remaining(); |
| 1486 | tty_parse_modes(s->ttyfd, &n_bytes); |
| 1487 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1488 | /* |
| 1489 | * Add a cleanup function to clear the utmp entry and record logout |
| 1490 | * time in case we call fatal() (e.g., the connection gets closed). |
| 1491 | */ |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1492 | fatal_add_cleanup(session_pty_cleanup, (void *)s); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1493 | if (!use_privsep) |
| 1494 | pty_setowner(s->pw, s->tty); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1495 | |
| 1496 | /* Set window size from the packet. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1497 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1498 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1499 | packet_check_eom(); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1500 | session_proctitle(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1501 | return 1; |
| 1502 | } |
| 1503 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1504 | static int |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1505 | session_subsystem_req(Session *s) |
| 1506 | { |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1507 | struct stat st; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1508 | u_int len; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1509 | int success = 0; |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1510 | char *cmd, *subsys = packet_get_string(&len); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1511 | int i; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1512 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1513 | packet_check_eom(); |
Damien Miller | baa0870 | 2002-02-05 12:14:10 +1100 | [diff] [blame] | 1514 | log("subsystem request for %.100s", subsys); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1515 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1516 | for (i = 0; i < options.num_subsystems; i++) { |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1517 | if (strcmp(subsys, options.subsystem_name[i]) == 0) { |
| 1518 | cmd = options.subsystem_command[i]; |
| 1519 | if (stat(cmd, &st) < 0) { |
| 1520 | error("subsystem: cannot stat %s: %s", cmd, |
| 1521 | strerror(errno)); |
| 1522 | break; |
| 1523 | } |
| 1524 | debug("subsystem: exec() %s", cmd); |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 1525 | s->is_subsystem = 1; |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1526 | do_exec(s, cmd); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1527 | success = 1; |
Damien Miller | 5fab4b9 | 2002-02-05 12:15:07 +1100 | [diff] [blame] | 1528 | break; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | if (!success) |
Damien Miller | baa0870 | 2002-02-05 12:14:10 +1100 | [diff] [blame] | 1533 | log("subsystem request for %.100s failed, subsystem not found", |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1534 | subsys); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1535 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1536 | xfree(subsys); |
| 1537 | return success; |
| 1538 | } |
| 1539 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1540 | static int |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1541 | session_x11_req(Session *s) |
| 1542 | { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1543 | int success; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1544 | |
| 1545 | s->single_connection = packet_get_char(); |
| 1546 | s->auth_proto = packet_get_string(NULL); |
| 1547 | s->auth_data = packet_get_string(NULL); |
| 1548 | s->screen = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1549 | packet_check_eom(); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1550 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1551 | success = session_setup_x11fwd(s); |
| 1552 | if (!success) { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1553 | xfree(s->auth_proto); |
| 1554 | xfree(s->auth_data); |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 1555 | s->auth_proto = NULL; |
| 1556 | s->auth_data = NULL; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1557 | } |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1558 | return success; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1559 | } |
| 1560 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1561 | static int |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1562 | session_shell_req(Session *s) |
| 1563 | { |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1564 | packet_check_eom(); |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 1565 | do_exec(s, NULL); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1566 | return 1; |
| 1567 | } |
| 1568 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1569 | static int |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1570 | session_exec_req(Session *s) |
| 1571 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1572 | u_int len; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1573 | char *command = packet_get_string(&len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1574 | packet_check_eom(); |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 1575 | do_exec(s, command); |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 1576 | xfree(command); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1577 | return 1; |
| 1578 | } |
| 1579 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1580 | static int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1581 | session_auth_agent_req(Session *s) |
| 1582 | { |
| 1583 | static int called = 0; |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1584 | packet_check_eom(); |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 1585 | if (no_agent_forwarding_flag) { |
| 1586 | debug("session_auth_agent_req: no_agent_forwarding_flag"); |
| 1587 | return 0; |
| 1588 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1589 | if (called) { |
| 1590 | return 0; |
| 1591 | } else { |
| 1592 | called = 1; |
| 1593 | return auth_input_request_forwarding(s->pw); |
| 1594 | } |
| 1595 | } |
| 1596 | |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 1597 | int |
| 1598 | session_input_channel_req(Channel *c, const char *rtype) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1599 | { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1600 | int success = 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1601 | Session *s; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1602 | |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 1603 | if ((s = session_by_channel(c->self)) == NULL) { |
| 1604 | log("session_input_channel_req: no session %d req %.100s", |
| 1605 | c->self, rtype); |
| 1606 | return 0; |
| 1607 | } |
| 1608 | debug("session_input_channel_req: session %d req %s", s->self, rtype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1609 | |
| 1610 | /* |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 1611 | * a session is in LARVAL state until a shell, a command |
| 1612 | * or a subsystem is executed |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1613 | */ |
| 1614 | if (c->type == SSH_CHANNEL_LARVAL) { |
| 1615 | if (strcmp(rtype, "shell") == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1616 | success = session_shell_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1617 | } else if (strcmp(rtype, "exec") == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1618 | success = session_exec_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1619 | } else if (strcmp(rtype, "pty-req") == 0) { |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 1620 | success = session_pty_req(s); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1621 | } else if (strcmp(rtype, "x11-req") == 0) { |
| 1622 | success = session_x11_req(s); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1623 | } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { |
| 1624 | success = session_auth_agent_req(s); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1625 | } else if (strcmp(rtype, "subsystem") == 0) { |
| 1626 | success = session_subsystem_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | if (strcmp(rtype, "window-change") == 0) { |
| 1630 | success = session_window_change_req(s); |
| 1631 | } |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 1632 | return success; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | void |
| 1636 | session_set_fds(Session *s, int fdin, int fdout, int fderr) |
| 1637 | { |
| 1638 | if (!compat20) |
| 1639 | fatal("session_set_fds: called for proto != 2.0"); |
| 1640 | /* |
| 1641 | * now that have a child and a pipe to the child, |
| 1642 | * we can activate our channel and register the fd's |
| 1643 | */ |
| 1644 | if (s->chanid == -1) |
| 1645 | fatal("no channel for session %d", s->self); |
| 1646 | channel_set_fds(s->chanid, |
| 1647 | fdout, fdin, fderr, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1648 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, |
Damien Miller | 19a5945 | 2002-02-19 15:20:57 +1100 | [diff] [blame] | 1649 | 1, |
| 1650 | CHAN_SES_WINDOW_DEFAULT); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1651 | } |
| 1652 | |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1653 | /* |
| 1654 | * Function to perform pty cleanup. Also called if we get aborted abnormally |
| 1655 | * (e.g., due to a dropped connection). |
| 1656 | */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1657 | void |
| 1658 | session_pty_cleanup2(void *session) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1659 | { |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1660 | Session *s = session; |
| 1661 | |
| 1662 | if (s == NULL) { |
| 1663 | error("session_pty_cleanup: no session"); |
| 1664 | return; |
| 1665 | } |
| 1666 | if (s->ttyfd == -1) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1667 | return; |
| 1668 | |
Kevin Steves | 43cdef3 | 2001-02-11 14:12:08 +0000 | [diff] [blame] | 1669 | debug("session_pty_cleanup: session %d release %s", s->self, s->tty); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1670 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1671 | /* Record that the user has logged out. */ |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1672 | if (s->pid != 0) |
Tim Rice | e06ae4a | 2002-02-24 17:56:46 -0800 | [diff] [blame] | 1673 | record_logout(s->pid, s->tty, s->pw->pw_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1674 | |
| 1675 | /* Release the pseudo-tty. */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1676 | if (getuid() == 0) |
| 1677 | pty_release(s->tty); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1678 | |
| 1679 | /* |
| 1680 | * Close the server side of the socket pairs. We must do this after |
| 1681 | * the pty cleanup, so that another process doesn't get this pty |
| 1682 | * while we're still cleaning up. |
| 1683 | */ |
| 1684 | if (close(s->ptymaster) < 0) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1685 | error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno)); |
Damien Miller | 0585d51 | 2001-10-12 11:35:50 +1000 | [diff] [blame] | 1686 | |
| 1687 | /* unlink pty from session */ |
| 1688 | s->ttyfd = -1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1689 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1690 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1691 | void |
| 1692 | session_pty_cleanup(void *session) |
| 1693 | { |
| 1694 | PRIVSEP(session_pty_cleanup2(session)); |
| 1695 | } |
| 1696 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1697 | static void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1698 | session_exit_message(Session *s, int status) |
| 1699 | { |
| 1700 | Channel *c; |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 1701 | |
| 1702 | if ((c = channel_lookup(s->chanid)) == NULL) |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1703 | fatal("session_exit_message: session %d: no channel %d", |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1704 | s->self, s->chanid); |
| 1705 | debug("session_exit_message: session %d channel %d pid %d", |
| 1706 | s->self, s->chanid, s->pid); |
| 1707 | |
| 1708 | if (WIFEXITED(status)) { |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 1709 | channel_request_start(s->chanid, "exit-status", 0); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1710 | packet_put_int(WEXITSTATUS(status)); |
| 1711 | packet_send(); |
| 1712 | } else if (WIFSIGNALED(status)) { |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 1713 | channel_request_start(s->chanid, "exit-signal", 0); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1714 | packet_put_int(WTERMSIG(status)); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 1715 | #ifdef WCOREDUMP |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1716 | packet_put_char(WCOREDUMP(status)); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 1717 | #else /* WCOREDUMP */ |
| 1718 | packet_put_char(0); |
| 1719 | #endif /* WCOREDUMP */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1720 | packet_put_cstring(""); |
| 1721 | packet_put_cstring(""); |
| 1722 | packet_send(); |
| 1723 | } else { |
| 1724 | /* Some weird exit cause. Just exit. */ |
| 1725 | packet_disconnect("wait returned status %04x.", status); |
| 1726 | } |
| 1727 | |
| 1728 | /* disconnect channel */ |
| 1729 | debug("session_exit_message: release channel %d", s->chanid); |
| 1730 | channel_cancel_cleanup(s->chanid); |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 1731 | /* |
| 1732 | * emulate a write failure with 'chan_write_failed', nobody will be |
| 1733 | * interested in data we write. |
| 1734 | * Note that we must not call 'chan_read_failed', since there could |
| 1735 | * be some more data waiting in the pipe. |
| 1736 | */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1737 | if (c->ostate != CHAN_OUTPUT_CLOSED) |
| 1738 | chan_write_failed(c); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1739 | s->chanid = -1; |
| 1740 | } |
| 1741 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1742 | void |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1743 | session_close(Session *s) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1744 | { |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1745 | debug("session_close: session %d pid %d", s->self, s->pid); |
| 1746 | if (s->ttyfd != -1) { |
| 1747 | fatal_remove_cleanup(session_pty_cleanup, (void *)s); |
| 1748 | session_pty_cleanup(s); |
| 1749 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1750 | if (s->term) |
| 1751 | xfree(s->term); |
| 1752 | if (s->display) |
| 1753 | xfree(s->display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 1754 | if (s->auth_display) |
| 1755 | xfree(s->auth_display); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1756 | if (s->auth_data) |
| 1757 | xfree(s->auth_data); |
| 1758 | if (s->auth_proto) |
| 1759 | xfree(s->auth_proto); |
| 1760 | s->used = 0; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1761 | session_proctitle(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | void |
| 1765 | session_close_by_pid(pid_t pid, int status) |
| 1766 | { |
| 1767 | Session *s = session_by_pid(pid); |
| 1768 | if (s == NULL) { |
Ben Lindstrom | 7a83722 | 2001-06-13 19:23:32 +0000 | [diff] [blame] | 1769 | debug("session_close_by_pid: no session for pid %d", pid); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1770 | return; |
| 1771 | } |
| 1772 | if (s->chanid != -1) |
| 1773 | session_exit_message(s, status); |
| 1774 | session_close(s); |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * this is called when a channel dies before |
| 1779 | * the session 'child' itself dies |
| 1780 | */ |
| 1781 | void |
| 1782 | session_close_by_channel(int id, void *arg) |
| 1783 | { |
| 1784 | Session *s = session_by_channel(id); |
| 1785 | if (s == NULL) { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1786 | debug("session_close_by_channel: no session for id %d", id); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1787 | return; |
| 1788 | } |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1789 | debug("session_close_by_channel: channel %d child %d", id, s->pid); |
| 1790 | if (s->pid != 0) { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1791 | debug("session_close_by_channel: channel %d: has child", id); |
Damien Miller | 0585d51 | 2001-10-12 11:35:50 +1000 | [diff] [blame] | 1792 | /* |
| 1793 | * delay detach of session, but release pty, since |
| 1794 | * the fd's to the child are already closed |
| 1795 | */ |
| 1796 | if (s->ttyfd != -1) { |
| 1797 | fatal_remove_cleanup(session_pty_cleanup, (void *)s); |
| 1798 | session_pty_cleanup(s); |
| 1799 | } |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1800 | return; |
| 1801 | } |
| 1802 | /* detach by removing callback */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1803 | channel_cancel_cleanup(s->chanid); |
| 1804 | s->chanid = -1; |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1805 | session_close(s); |
| 1806 | } |
| 1807 | |
| 1808 | void |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1809 | session_destroy_all(void (*closefunc)(Session *)) |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1810 | { |
| 1811 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1812 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1813 | Session *s = &sessions[i]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1814 | if (s->used) { |
| 1815 | if (closefunc != NULL) |
| 1816 | closefunc(s); |
| 1817 | else |
| 1818 | session_close(s); |
| 1819 | } |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1820 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1821 | } |
| 1822 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1823 | static char * |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1824 | session_tty_list(void) |
| 1825 | { |
| 1826 | static char buf[1024]; |
| 1827 | int i; |
| 1828 | buf[0] = '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1829 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1830 | Session *s = &sessions[i]; |
| 1831 | if (s->used && s->ttyfd != -1) { |
| 1832 | if (buf[0] != '\0') |
| 1833 | strlcat(buf, ",", sizeof buf); |
| 1834 | strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf); |
| 1835 | } |
| 1836 | } |
| 1837 | if (buf[0] == '\0') |
| 1838 | strlcpy(buf, "notty", sizeof buf); |
| 1839 | return buf; |
| 1840 | } |
| 1841 | |
| 1842 | void |
| 1843 | session_proctitle(Session *s) |
| 1844 | { |
| 1845 | if (s->pw == NULL) |
| 1846 | error("no user for session %d", s->self); |
| 1847 | else |
| 1848 | setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); |
| 1849 | } |
| 1850 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1851 | int |
| 1852 | session_setup_x11fwd(Session *s) |
| 1853 | { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1854 | struct stat st; |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1855 | char display[512], auth_display[512]; |
| 1856 | char hostname[MAXHOSTNAMELEN]; |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1857 | |
| 1858 | if (no_x11_forwarding_flag) { |
| 1859 | packet_send_debug("X11 forwarding disabled in user configuration file."); |
| 1860 | return 0; |
| 1861 | } |
| 1862 | if (!options.x11_forwarding) { |
| 1863 | debug("X11 forwarding disabled in server configuration file."); |
| 1864 | return 0; |
| 1865 | } |
| 1866 | if (!options.xauth_location || |
| 1867 | (stat(options.xauth_location, &st) == -1)) { |
| 1868 | packet_send_debug("No xauth program; cannot forward with spoofing."); |
| 1869 | return 0; |
| 1870 | } |
Ben Lindstrom | 699776e | 2001-06-21 03:14:49 +0000 | [diff] [blame] | 1871 | if (options.use_login) { |
| 1872 | packet_send_debug("X11 forwarding disabled; " |
| 1873 | "not compatible with UseLogin=yes."); |
| 1874 | return 0; |
| 1875 | } |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 1876 | if (s->display != NULL) { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1877 | debug("X11 display already set."); |
| 1878 | return 0; |
| 1879 | } |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1880 | s->display_number = x11_create_display_inet(options.x11_display_offset, |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 1881 | options.x11_use_localhost, s->single_connection); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1882 | if (s->display_number == -1) { |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 1883 | debug("x11_create_display_inet failed."); |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1884 | return 0; |
| 1885 | } |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1886 | |
| 1887 | /* Set up a suitable value for the DISPLAY variable. */ |
| 1888 | if (gethostname(hostname, sizeof(hostname)) < 0) |
| 1889 | fatal("gethostname: %.100s", strerror(errno)); |
| 1890 | /* |
| 1891 | * auth_display must be used as the displayname when the |
| 1892 | * authorization entry is added with xauth(1). This will be |
| 1893 | * different than the DISPLAY string for localhost displays. |
| 1894 | */ |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 1895 | if (options.x11_use_localhost) { |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1896 | snprintf(display, sizeof display, "localhost:%d.%d", |
| 1897 | s->display_number, s->screen); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 1898 | snprintf(auth_display, sizeof auth_display, "unix:%d.%d", |
| 1899 | s->display_number, s->screen); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1900 | s->display = xstrdup(display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 1901 | s->auth_display = xstrdup(auth_display); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1902 | } else { |
| 1903 | #ifdef IPADDR_IN_DISPLAY |
| 1904 | struct hostent *he; |
| 1905 | struct in_addr my_addr; |
| 1906 | |
| 1907 | he = gethostbyname(hostname); |
| 1908 | if (he == NULL) { |
| 1909 | error("Can't get IP address for X11 DISPLAY."); |
| 1910 | packet_send_debug("Can't get IP address for X11 DISPLAY."); |
| 1911 | return 0; |
| 1912 | } |
| 1913 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); |
| 1914 | snprintf(display, sizeof display, "%.50s:%d.%d", inet_ntoa(my_addr), |
| 1915 | s->display_number, s->screen); |
| 1916 | #else |
| 1917 | snprintf(display, sizeof display, "%.400s:%d.%d", hostname, |
| 1918 | s->display_number, s->screen); |
| 1919 | #endif |
| 1920 | s->display = xstrdup(display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 1921 | s->auth_display = xstrdup(display); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1924 | return 1; |
| 1925 | } |
| 1926 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1927 | static void |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 1928 | do_authenticated2(Authctxt *authctxt) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1929 | { |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 1930 | server_loop2(authctxt); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1931 | } |