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