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