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