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