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