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