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