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