blob: 4653b09ff8174dc6ad6d4020dc67d809e8c097ec [file] [log] [blame]
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00001/* $OpenBSD: session.c,v 1.282 2016/03/10 11:47:57 djm Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
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 Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
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 Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +000049#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100051#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100052#include <grp.h>
Damien Millere5c0d522014-07-03 21:24:19 +100053#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110055#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100057#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110058#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100059#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100060#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100061#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100062#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100063#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000064#include <limits.h>
Damien Millerb38eff82000-04-01 11:09:21 +100065
Damien Millerb84886b2008-05-19 15:05:07 +100066#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100068#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "ssh1.h"
70#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000071#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100072#include "packet.h"
73#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100074#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100075#include "uidswap.h"
76#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000077#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100078#include "key.h"
79#include "cipher.h"
80#ifdef GSSAPI
81#include "ssh-gss.h"
82#endif
83#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100084#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100085#include "auth-options.h"
Damien Miller85b45e02013-07-20 13:21:52 +100086#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087#include "pathnames.h"
88#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100089#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000091#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "serverloop.h"
93#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000094#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100095#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110097#include "sftp.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100098
Darren Tucker3c78c5e2004-01-23 22:03:10 +110099#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +1100100#include <kafs.h>
101#endif
102
Damien Miller14684a12011-05-20 11:23:07 +1000103#ifdef WITH_SELINUX
104#include <selinux/selinux.h>
105#endif
106
Damien Millerad793d52008-11-03 19:17:57 +1100107#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 Millerb38eff82000-04-01 11:09:21 +1000113/* func */
114
115Session *session_new(void);
Damien Miller8853ca52010-06-26 10:00:14 +1000116void session_set_fds(Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000117void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000118void session_proctitle(Session *);
119int session_setup_x11fwd(Session *);
Damien Miller7207f642008-05-19 15:34:50 +1000120int do_exec_pty(Session *, const char *);
121int do_exec_no_pty(Session *, const char *);
122int do_exec(Session *, const char *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000123void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000124#ifdef LOGIN_NEEDS_UTMPX
125static void do_pre_login(Session *s);
126#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000127void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000128void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000129int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000130
Ben Lindstrombba81212001-06-25 05:01:22 +0000131static void do_authenticated1(Authctxt *);
132static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000133
Ben Lindstrombba81212001-06-25 05:01:22 +0000134static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000135
Damien Millerb38eff82000-04-01 11:09:21 +1000136/* import */
137extern ServerOptions options;
138extern char *__progname;
139extern int log_stderr;
140extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000141extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000142extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000143extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000144extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000145
Damien Miller7b28dc52000-09-05 13:34:53 +1100146/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000147const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100148
Damien Millerb38eff82000-04-01 11:09:21 +1000149/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000150static int sessions_first_unused = -1;
151static int sessions_nalloc = 0;
152static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100153
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100154#define SUBSYSTEM_NONE 0
155#define SUBSYSTEM_EXT 1
156#define SUBSYSTEM_INT_SFTP 2
157#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100158
Damien Millerad833b32000-08-23 10:46:23 +1000159#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000160login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000161#endif
162
Darren Tucker3e33cec2003-10-02 16:12:36 +1000163static int is_child = 0;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +0000164static int in_chroot = 0;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000165
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000166/* Name and directory of socket for authentication agent forwarding. */
167static char *auth_sock_name = NULL;
168static char *auth_sock_dir = NULL;
169
170/* removes the agent forwarding socket */
171
172static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000173auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000174{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000175 if (auth_sock_name != NULL) {
176 temporarily_use_uid(pw);
177 unlink(auth_sock_name);
178 rmdir(auth_sock_dir);
179 auth_sock_name = NULL;
180 restore_uid();
181 }
182}
183
184static int
185auth_input_request_forwarding(struct passwd * pw)
186{
187 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000188 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000189
190 if (auth_sock_name != NULL) {
191 error("authentication forwarding requested twice.");
192 return 0;
193 }
194
195 /* Temporarily drop privileged uid for mkdir/bind. */
196 temporarily_use_uid(pw);
197
198 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000199 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000200
201 /* Create private directory for socket */
202 if (mkdtemp(auth_sock_dir) == NULL) {
203 packet_send_debug("Agent forwarding disabled: "
204 "mkdtemp() failed: %.100s", strerror(errno));
205 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000206 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000207 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000208 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000209 }
Damien Miller7207f642008-05-19 15:34:50 +1000210
211 xasprintf(&auth_sock_name, "%s/agent.%ld",
212 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000213
Damien Miller7acefbb2014-07-18 14:11:24 +1000214 /* Start a Unix listener on auth_sock_name. */
215 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000216
217 /* Restore the privileged uid. */
218 restore_uid();
219
Damien Miller7acefbb2014-07-18 14:11:24 +1000220 /* Check for socket/bind/listen failure. */
221 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000222 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000223
224 /* Allocate a channel for the authentication agent socket. */
225 nc = channel_new("auth socket",
226 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
227 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000228 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100229 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000230 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000231
232 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000233 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000234 if (auth_sock_dir != NULL) {
235 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000236 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000237 }
238 if (sock != -1)
239 close(sock);
240 auth_sock_name = NULL;
241 auth_sock_dir = NULL;
242 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000243}
244
Darren Tucker1921ed92004-02-10 13:23:28 +1100245static void
246display_loginmsg(void)
247{
Damien Miller46d38de2005-07-17 17:02:09 +1000248 if (buffer_len(&loginmsg) > 0) {
249 buffer_append(&loginmsg, "\0", 1);
250 printf("%s", (char *)buffer_ptr(&loginmsg));
251 buffer_clear(&loginmsg);
252 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100253}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000254
Ben Lindstromb31783d2001-03-22 02:02:12 +0000255void
256do_authenticated(Authctxt *authctxt)
257{
Damien Miller97f39ae2003-02-24 11:57:01 +1100258 setproctitle("%s", authctxt->pw->pw_name);
259
Ben Lindstromb31783d2001-03-22 02:02:12 +0000260 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000261 /* XXX - streamlocal? */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100262 if (no_port_forwarding_flag ||
263 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
264 channel_disable_adm_local_opens();
265 else
Ben Lindstromb31783d2001-03-22 02:02:12 +0000266 channel_permit_all_opens();
267
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100268 auth_debug_send();
269
Ben Lindstromb31783d2001-03-22 02:02:12 +0000270 if (compat20)
271 do_authenticated2(authctxt);
272 else
273 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000274
Darren Tucker3e33cec2003-10-02 16:12:36 +1000275 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000276}
277
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000278/* Check untrusted xauth strings for metacharacters */
279static int
280xauth_valid_string(const char *s)
281{
282 size_t i;
283
284 for (i = 0; s[i] != '\0'; i++) {
285 if (!isalnum((u_char)s[i]) &&
286 s[i] != '.' && s[i] != ':' && s[i] != '/' &&
287 s[i] != '-' && s[i] != '_')
288 return 0;
289 }
290 return 1;
291}
292
Damien Millerb38eff82000-04-01 11:09:21 +1000293/*
Damien Millerb38eff82000-04-01 11:09:21 +1000294 * Prepares for an interactive session. This is called after the user has
295 * been successfully authenticated. During this message exchange, pseudo
296 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
297 * are requested, etc.
298 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000299static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000300do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000301{
302 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000303 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100304 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000305 int enable_compression_after_reply = 0;
306 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000307
308 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100309 if (s == NULL) {
310 error("no more sessions");
311 return;
312 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000313 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000314 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000315
Damien Millerb38eff82000-04-01 11:09:21 +1000316 /*
317 * We stay in this loop until the client requests to execute a shell
318 * or a command.
319 */
320 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000321 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000322
323 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100324 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000325
326 /* Process the packet. */
327 switch (type) {
328 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000329 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100330 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000331 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000332 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100333 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000334 break;
335 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000336 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000337 debug2("compression disabled");
338 break;
339 }
Damien Millerb38eff82000-04-01 11:09:21 +1000340 /* Enable compression after we have responded with SUCCESS. */
341 enable_compression_after_reply = 1;
342 success = 1;
343 break;
344
345 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000346 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000347 break;
348
349 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000350 s->auth_proto = packet_get_string(&proto_len);
351 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000352
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000353 screen_flag = packet_get_protocol_flags() &
354 SSH_PROTOFLAG_SCREEN_NUMBER;
355 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
356
357 if (packet_remaining() == 4) {
358 if (!screen_flag)
359 debug2("Buggy client: "
360 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000361 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000362 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000363 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000364 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100365 packet_check_eom();
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000366 if (xauth_valid_string(s->auth_proto) &&
367 xauth_valid_string(s->auth_data))
368 success = session_setup_x11fwd(s);
369 else {
370 success = 0;
371 error("Invalid X11 forwarding data");
372 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000373 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000374 free(s->auth_proto);
375 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000376 s->auth_proto = NULL;
377 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000378 }
Damien Millerb38eff82000-04-01 11:09:21 +1000379 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000380
381 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
Damien Miller4f755cd2008-05-19 14:57:41 +1000382 if (!options.allow_agent_forwarding ||
383 no_agent_forwarding_flag || compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000384 debug("Authentication agent forwarding not permitted for this authentication.");
385 break;
386 }
387 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000388 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000389 break;
390
391 case SSH_CMSG_PORT_FORWARD_REQUEST:
392 if (no_port_forwarding_flag) {
393 debug("Port forwarding not permitted for this authentication.");
394 break;
395 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100396 if (!(options.allow_tcp_forwarding & FORWARD_REMOTE)) {
Damien Miller50a41ed2000-10-16 12:14:42 +1100397 debug("Port forwarding not permitted.");
398 break;
399 }
Damien Millerb38eff82000-04-01 11:09:21 +1000400 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000401 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
Damien Miller7acefbb2014-07-18 14:11:24 +1000402 &options.fwd_opts) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000403 debug("Port forwarding failed.");
404 break;
405 }
Damien Millerb38eff82000-04-01 11:09:21 +1000406 success = 1;
407 break;
408
409 case SSH_CMSG_MAX_PACKET_SIZE:
410 if (packet_set_maxsize(packet_get_int()) > 0)
411 success = 1;
412 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100413
Damien Millerb38eff82000-04-01 11:09:21 +1000414 case SSH_CMSG_EXEC_SHELL:
415 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000416 if (type == SSH_CMSG_EXEC_CMD) {
417 command = packet_get_string(&dlen);
418 debug("Exec command '%.500s'", command);
Damien Miller7207f642008-05-19 15:34:50 +1000419 if (do_exec(s, command) != 0)
420 packet_disconnect(
421 "command execution failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000422 free(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000423 } else {
Damien Miller7207f642008-05-19 15:34:50 +1000424 if (do_exec(s, NULL) != 0)
425 packet_disconnect(
426 "shell execution failed");
Damien Millerb38eff82000-04-01 11:09:21 +1000427 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100428 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000429 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000430 return;
431
432 default:
433 /*
434 * Any unknown messages in this phase are ignored,
435 * and a failure message is returned.
436 */
Damien Miller996acd22003-04-09 20:59:48 +1000437 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000438 }
439 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
440 packet_send();
441 packet_write_wait();
442
443 /* Enable compression now that we have replied if appropriate. */
444 if (enable_compression_after_reply) {
445 enable_compression_after_reply = 0;
446 packet_start_compression(compression_level);
447 }
448 }
449}
450
Darren Tucker293ee3c2014-01-19 15:28:01 +1100451#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000452/*
453 * This is called to fork and execute a command when we have no tty. This
454 * will call do_child from the child, and server_loop from the parent after
455 * setting up file descriptors and such.
456 */
Damien Miller7207f642008-05-19 15:34:50 +1000457int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000458do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000459{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000460 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000461
462#ifdef USE_PIPES
463 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000464
Damien Miller22a29882010-05-10 11:53:54 +1000465 if (s == NULL)
466 fatal("do_exec_no_pty: no session");
467
Damien Millerb38eff82000-04-01 11:09:21 +1000468 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000469 if (pipe(pin) < 0) {
470 error("%s: pipe in: %.100s", __func__, strerror(errno));
471 return -1;
472 }
473 if (pipe(pout) < 0) {
474 error("%s: pipe out: %.100s", __func__, strerror(errno));
475 close(pin[0]);
476 close(pin[1]);
477 return -1;
478 }
Damien Miller8853ca52010-06-26 10:00:14 +1000479 if (pipe(perr) < 0) {
480 error("%s: pipe err: %.100s", __func__,
481 strerror(errno));
482 close(pin[0]);
483 close(pin[1]);
484 close(pout[0]);
485 close(pout[1]);
486 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000487 }
488#else
Damien Millerb38eff82000-04-01 11:09:21 +1000489 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000490
Damien Miller22a29882010-05-10 11:53:54 +1000491 if (s == NULL)
492 fatal("do_exec_no_pty: no session");
493
Damien Millerb38eff82000-04-01 11:09:21 +1000494 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000495 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
496 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
497 return -1;
498 }
Damien Miller8853ca52010-06-26 10:00:14 +1000499 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
500 error("%s: socketpair #2: %.100s", __func__,
501 strerror(errno));
502 close(inout[0]);
503 close(inout[1]);
504 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000505 }
506#endif
507
Damien Millere247cc42000-05-07 12:03:14 +1000508 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000509
Damien Millerb38eff82000-04-01 11:09:21 +1000510 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000511 switch ((pid = fork())) {
512 case -1:
513 error("%s: fork: %.100s", __func__, strerror(errno));
514#ifdef USE_PIPES
515 close(pin[0]);
516 close(pin[1]);
517 close(pout[0]);
518 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000519 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000520 close(perr[1]);
521#else
522 close(inout[0]);
523 close(inout[1]);
524 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000525 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000526#endif
527 return -1;
528 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000529 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000530
Damien Millerb38eff82000-04-01 11:09:21 +1000531 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000532 log_init(__progname, options.log_level,
533 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000534
535 /*
536 * Create a new session and process group since the 4.4BSD
537 * setlogin() affects the entire process group.
538 */
539 if (setsid() < 0)
540 error("setsid failed: %.100s", strerror(errno));
541
542#ifdef USE_PIPES
543 /*
544 * Redirect stdin. We close the parent side of the socket
545 * pair, and make the child side the standard input.
546 */
547 close(pin[1]);
548 if (dup2(pin[0], 0) < 0)
549 perror("dup2 stdin");
550 close(pin[0]);
551
552 /* Redirect stdout. */
553 close(pout[0]);
554 if (dup2(pout[1], 1) < 0)
555 perror("dup2 stdout");
556 close(pout[1]);
557
558 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000559 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000560 if (dup2(perr[1], 2) < 0)
561 perror("dup2 stderr");
562 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000563#else
Damien Millerb38eff82000-04-01 11:09:21 +1000564 /*
565 * Redirect stdin, stdout, and stderr. Stdin and stdout will
566 * use the same socket, as some programs (particularly rdist)
567 * seem to depend on it.
568 */
569 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000570 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000571 if (dup2(inout[0], 0) < 0) /* stdin */
572 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000573 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000574 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000575 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000576 if (dup2(err[0], 2) < 0) /* stderr */
577 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000578 close(err[0]);
579#endif
580
Damien Millerb38eff82000-04-01 11:09:21 +1000581
Tim Rice81ed5182002-09-25 17:38:46 -0700582#ifdef _UNICOS
583 cray_init_job(s->pw); /* set up cray jid and tmpdir */
584#endif
585
Damien Millerb38eff82000-04-01 11:09:21 +1000586 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000587 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000588 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000589 default:
590 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000591 }
Damien Miller7207f642008-05-19 15:34:50 +1000592
Tim Rice81ed5182002-09-25 17:38:46 -0700593#ifdef _UNICOS
594 signal(WJSIGNAL, cray_job_termination_handler);
595#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100596#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100597 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100598#endif
Damien Miller7207f642008-05-19 15:34:50 +1000599
Damien Millerb38eff82000-04-01 11:09:21 +1000600 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000601 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100602 packet_set_interactive(s->display != NULL,
603 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000604
605 /*
606 * Clear loginmsg, since it's the child's responsibility to display
607 * it to the user, otherwise multiple sessions may accumulate
608 * multiple copies of the login messages.
609 */
610 buffer_clear(&loginmsg);
611
Damien Millerb38eff82000-04-01 11:09:21 +1000612#ifdef USE_PIPES
613 /* We are the parent. Close the child sides of the pipes. */
614 close(pin[0]);
615 close(pout[1]);
616 close(perr[1]);
617
Damien Millerefb4afe2000-04-12 18:45:05 +1000618 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000619 session_set_fds(s, pin[1], pout[0], perr[0],
620 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000621 } else {
622 /* Enter the interactive session. */
623 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000624 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000625 }
Damien Miller7207f642008-05-19 15:34:50 +1000626#else
Damien Millerb38eff82000-04-01 11:09:21 +1000627 /* We are the parent. Close the child sides of the socket pairs. */
628 close(inout[0]);
629 close(err[0]);
630
631 /*
632 * Enter the interactive session. Note: server_loop must be able to
633 * handle the case that fdin and fdout are the same.
634 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000635 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000636 session_set_fds(s, inout[1], inout[1], err[1],
637 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000638 } else {
639 server_loop(pid, inout[1], inout[1], err[1]);
640 /* server_loop has closed inout[1] and err[1]. */
641 }
Damien Miller7207f642008-05-19 15:34:50 +1000642#endif
643 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000644}
645
646/*
647 * This is called to fork and execute a command when we have a tty. This
648 * will call do_child from the child, and server_loop from the parent after
649 * setting up file descriptors, controlling tty, updating wtmp, utmp,
650 * lastlog, and other such operations.
651 */
Damien Miller7207f642008-05-19 15:34:50 +1000652int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000653do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000654{
Damien Millerb38eff82000-04-01 11:09:21 +1000655 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000656 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000657
658 if (s == NULL)
659 fatal("do_exec_pty: no session");
660 ptyfd = s->ptyfd;
661 ttyfd = s->ttyfd;
662
Damien Miller7207f642008-05-19 15:34:50 +1000663 /*
664 * Create another descriptor of the pty master side for use as the
665 * standard input. We could use the original descriptor, but this
666 * simplifies code in server_loop. The descriptor is bidirectional.
667 * Do this before forking (and cleanup in the child) so as to
668 * detect and gracefully fail out-of-fd conditions.
669 */
670 if ((fdout = dup(ptyfd)) < 0) {
671 error("%s: dup #1: %s", __func__, strerror(errno));
672 close(ttyfd);
673 close(ptyfd);
674 return -1;
675 }
676 /* we keep a reference to the pty master */
677 if ((ptymaster = dup(ptyfd)) < 0) {
678 error("%s: dup #2: %s", __func__, strerror(errno));
679 close(ttyfd);
680 close(ptyfd);
681 close(fdout);
682 return -1;
683 }
684
Damien Millerb38eff82000-04-01 11:09:21 +1000685 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000686 switch ((pid = fork())) {
687 case -1:
688 error("%s: fork: %.100s", __func__, strerror(errno));
689 close(fdout);
690 close(ptymaster);
691 close(ttyfd);
692 close(ptyfd);
693 return -1;
694 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000695 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000696
Damien Miller7207f642008-05-19 15:34:50 +1000697 close(fdout);
698 close(ptymaster);
699
Damien Miller942da032000-08-18 13:59:06 +1000700 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000701 log_init(__progname, options.log_level,
702 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000703 /* Close the master side of the pseudo tty. */
704 close(ptyfd);
705
706 /* Make the pseudo tty our controlling tty. */
707 pty_make_controlling_tty(&ttyfd, s->tty);
708
Damien Miller9c751422001-10-10 15:02:46 +1000709 /* Redirect stdin/stdout/stderr from the pseudo tty. */
710 if (dup2(ttyfd, 0) < 0)
711 error("dup2 stdin: %s", strerror(errno));
712 if (dup2(ttyfd, 1) < 0)
713 error("dup2 stdout: %s", strerror(errno));
714 if (dup2(ttyfd, 2) < 0)
715 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000716
717 /* Close the extra descriptor for the pseudo tty. */
718 close(ttyfd);
719
Damien Miller942da032000-08-18 13:59:06 +1000720 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000721#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700722 if (!(options.use_login && command == NULL)) {
723#ifdef _UNICOS
724 cray_init_job(s->pw); /* set up cray jid and tmpdir */
725#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100726 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700727 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000728# ifdef LOGIN_NEEDS_UTMPX
729 else
730 do_pre_login(s);
731# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000732#endif
Damien Miller7207f642008-05-19 15:34:50 +1000733 /*
734 * Do common processing for the child, such as execing
735 * the command.
736 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000737 do_child(s, command);
738 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000739 default:
740 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000741 }
Damien Miller7207f642008-05-19 15:34:50 +1000742
Tim Rice81ed5182002-09-25 17:38:46 -0700743#ifdef _UNICOS
744 signal(WJSIGNAL, cray_job_termination_handler);
745#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100746#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100747 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100748#endif
Damien Miller7207f642008-05-19 15:34:50 +1000749
Damien Millerb38eff82000-04-01 11:09:21 +1000750 s->pid = pid;
751
752 /* Parent. Close the slave side of the pseudo tty. */
753 close(ttyfd);
754
Damien Millerb38eff82000-04-01 11:09:21 +1000755 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000756 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100757 packet_set_interactive(1,
758 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Millerefb4afe2000-04-12 18:45:05 +1000759 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000760 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000761 } else {
762 server_loop(pid, ptyfd, fdout, -1);
763 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000764 }
Damien Miller7207f642008-05-19 15:34:50 +1000765 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000766}
767
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000768#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000769static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000770do_pre_login(Session *s)
771{
772 socklen_t fromlen;
773 struct sockaddr_storage from;
774 pid_t pid = getpid();
775
776 /*
777 * Get IP address of client. If the connection is not a socket, let
778 * the address be 0.0.0.0.
779 */
780 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000781 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000782 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000783 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100784 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000785 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000786 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000787 }
788 }
789
790 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000791 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000792 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000793}
794#endif
795
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000796/*
797 * This is called to fork and execute a command. If another command is
798 * to be forced, execute that instead.
799 */
Damien Miller7207f642008-05-19 15:34:50 +1000800int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000801do_exec(Session *s, const char *command)
802{
djm@openbsd.org95767262016-03-07 19:02:43 +0000803 struct ssh *ssh = active_state; /* XXX */
Damien Miller7207f642008-05-19 15:34:50 +1000804 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000805 const char *forced = NULL, *tty = NULL;
806 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000807
Damien Millere2754432006-07-24 14:06:47 +1000808 if (options.adm_forced_command) {
809 original_command = command;
810 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100811 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000812 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000813 original_command = command;
814 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100815 forced = "(key-option)";
816 }
817 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100818 if (IS_INTERNAL_SFTP(command)) {
819 s->is_subsystem = s->is_subsystem ?
820 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
821 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100822 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100823 snprintf(session_type, sizeof(session_type),
824 "forced-command %s '%.900s'", forced, command);
825 } else if (s->is_subsystem) {
826 snprintf(session_type, sizeof(session_type),
827 "subsystem '%.900s'", s->subsys);
828 } else if (command == NULL) {
829 snprintf(session_type, sizeof(session_type), "shell");
830 } else {
831 /* NB. we don't log unforced commands to preserve privacy */
832 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000833 }
834
Damien Miller71df7522013-10-15 12:12:02 +1100835 if (s->ttyfd != -1) {
836 tty = s->tty;
837 if (strncmp(tty, "/dev/", 5) == 0)
838 tty += 5;
839 }
840
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000841 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100842 session_type,
843 tty == NULL ? "" : " on ",
844 tty == NULL ? "" : tty,
845 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000846 ssh_remote_ipaddr(ssh),
847 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000848 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100849
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100850#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100851 if (command != NULL)
852 PRIVSEP(audit_run_command(command));
853 else if (s->ttyfd == -1) {
854 char *shell = s->pw->pw_shell;
855
856 if (shell[0] == '\0') /* empty shell means /bin/sh */
857 shell =_PATH_BSHELL;
858 PRIVSEP(audit_run_command(shell));
859 }
860#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000861 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000862 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000863 else
Damien Miller7207f642008-05-19 15:34:50 +1000864 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000865
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000866 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000867
Darren Tucker09991742004-07-17 17:05:14 +1000868 /*
869 * Clear loginmsg: it's the child's responsibility to display
870 * it to the user, otherwise multiple sessions may accumulate
871 * multiple copies of the login messages.
872 */
873 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000874
875 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000876}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000877
Damien Miller942da032000-08-18 13:59:06 +1000878/* administrative, login(1)-like work */
879void
Damien Miller69b69aa2000-10-28 14:19:58 +1100880do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000881{
djm@openbsd.org95767262016-03-07 19:02:43 +0000882 struct ssh *ssh = active_state; /* XXX */
Damien Miller942da032000-08-18 13:59:06 +1000883 socklen_t fromlen;
884 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000885 struct passwd * pw = s->pw;
886 pid_t pid = getpid();
887
888 /*
889 * Get IP address of client. If the connection is not a socket, let
890 * the address be 0.0.0.0.
891 */
892 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000893 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000894 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000895 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000896 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000897 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000898 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000899 }
900 }
901
902 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000903 if (!use_privsep)
904 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000905 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000906 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000907 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000908
Kevin Steves092f2ef2000-10-14 13:36:13 +0000909#ifdef USE_PAM
910 /*
911 * If password change is needed, do it now.
912 * This needs to occur before the ~/.hushlogin check.
913 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100914 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
915 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000916 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100917 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000918 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000919 }
920#endif
921
Damien Millercf205e82001-04-16 18:29:15 +1000922 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000923 return;
924
Darren Tucker1921ed92004-02-10 13:23:28 +1100925 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000926
Damien Millercf205e82001-04-16 18:29:15 +1000927 do_motd();
928}
929
930/*
931 * Display the message of the day.
932 */
933void
934do_motd(void)
935{
936 FILE *f;
937 char buf[256];
938
Damien Miller942da032000-08-18 13:59:06 +1000939 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000940#ifdef HAVE_LOGIN_CAP
941 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
942 "/etc/motd"), "r");
943#else
Damien Miller942da032000-08-18 13:59:06 +1000944 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000945#endif
Damien Miller942da032000-08-18 13:59:06 +1000946 if (f) {
947 while (fgets(buf, sizeof(buf), f))
948 fputs(buf, stdout);
949 fclose(f);
950 }
951 }
952}
953
Kevin Steves8f63caa2001-07-04 18:23:02 +0000954
955/*
956 * Check for quiet login, either .hushlogin or command given.
957 */
958int
959check_quietlogin(Session *s, const char *command)
960{
961 char buf[256];
962 struct passwd *pw = s->pw;
963 struct stat st;
964
965 /* Return 1 if .hushlogin exists or a command given. */
966 if (command != NULL)
967 return 1;
968 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
969#ifdef HAVE_LOGIN_CAP
970 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
971 return 1;
972#else
973 if (stat(buf, &st) >= 0)
974 return 1;
975#endif
976 return 0;
977}
978
Damien Millerb38eff82000-04-01 11:09:21 +1000979/*
980 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100981 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000982 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000983void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000984child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100985 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000986{
Damien Millerb38eff82000-04-01 11:09:21 +1000987 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000988 u_int envsize;
989 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000990
Damien Miller8569eba2014-03-04 09:35:17 +1100991 if (strchr(name, '=') != NULL) {
992 error("Invalid environment variable \"%.100s\"", name);
993 return;
994 }
995
Damien Millerb38eff82000-04-01 11:09:21 +1000996 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000997 * If we're passed an uninitialized list, allocate a single null
998 * entry before continuing.
999 */
1000 if (*envp == NULL && *envsizep == 0) {
1001 *envp = xmalloc(sizeof(char *));
1002 *envp[0] = NULL;
1003 *envsizep = 1;
1004 }
1005
1006 /*
Damien Millerb38eff82000-04-01 11:09:21 +10001007 * Find the slot where the value should be stored. If the variable
1008 * already exists, we reuse the slot; otherwise we append a new slot
1009 * at the end of the array, expanding if necessary.
1010 */
1011 env = *envp;
1012 namelen = strlen(name);
1013 for (i = 0; env[i]; i++)
1014 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
1015 break;
1016 if (env[i]) {
1017 /* Reuse the slot. */
Darren Tuckera627d422013-06-02 07:31:17 +10001018 free(env[i]);
Damien Millerb38eff82000-04-01 11:09:21 +10001019 } else {
1020 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +10001021 envsize = *envsizep;
1022 if (i >= envsize - 1) {
1023 if (envsize >= 1000)
1024 fatal("child_set_env: too many env vars");
1025 envsize += 50;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001026 env = (*envp) = xreallocarray(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +10001027 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +10001028 }
1029 /* Need to set the NULL pointer at end of array beyond the new slot. */
1030 env[i + 1] = NULL;
1031 }
1032
1033 /* Allocate space and format the variable in the appropriate slot. */
1034 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
1035 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
1036}
1037
1038/*
1039 * Reads environment variables from the given file and adds/overrides them
1040 * into the environment. If the file does not exist, this does nothing.
1041 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1042 * and assignments of the form name=value. No other forms are allowed.
1043 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001044static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001045read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001046 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +10001047{
1048 FILE *f;
1049 char buf[4096];
1050 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +10001051 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001052
1053 f = fopen(filename, "r");
1054 if (!f)
1055 return;
1056
1057 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +10001058 if (++lineno > 1000)
1059 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001060 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1061 ;
1062 if (!*cp || *cp == '#' || *cp == '\n')
1063 continue;
Damien Miller14b017d2007-09-17 16:09:15 +10001064
1065 cp[strcspn(cp, "\n")] = '\0';
1066
Damien Millerb38eff82000-04-01 11:09:21 +10001067 value = strchr(cp, '=');
1068 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +10001069 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1070 filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001071 continue;
1072 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001073 /*
1074 * Replace the equals sign by nul, and advance value to
1075 * the value string.
1076 */
Damien Millerb38eff82000-04-01 11:09:21 +10001077 *value = '\0';
1078 value++;
1079 child_set_env(env, envsize, cp, value);
1080 }
1081 fclose(f);
1082}
1083
Darren Tuckere1a790d2003-09-16 11:52:19 +10001084#ifdef HAVE_ETC_DEFAULT_LOGIN
1085/*
1086 * Return named variable from specified environment, or NULL if not present.
1087 */
1088static char *
1089child_get_env(char **env, const char *name)
1090{
1091 int i;
1092 size_t len;
1093
1094 len = strlen(name);
1095 for (i=0; env[i] != NULL; i++)
1096 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1097 return(env[i] + len + 1);
1098 return NULL;
1099}
1100
1101/*
1102 * Read /etc/default/login.
1103 * We pick up the PATH (or SUPATH for root) and UMASK.
1104 */
1105static void
1106read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1107{
1108 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001109 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +10001110 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +10001111
1112 /*
1113 * We don't want to copy the whole file to the child's environment,
1114 * so we use a temporary environment and copy the variables we're
1115 * interested in.
1116 */
1117 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1118
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001119 if (tmpenv == NULL)
1120 return;
1121
Darren Tuckere1a790d2003-09-16 11:52:19 +10001122 if (uid == 0)
1123 var = child_get_env(tmpenv, "SUPATH");
1124 else
1125 var = child_get_env(tmpenv, "PATH");
1126 if (var != NULL)
1127 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +11001128
Darren Tuckere1a790d2003-09-16 11:52:19 +10001129 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1130 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +10001131 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +11001132
Darren Tuckere1a790d2003-09-16 11:52:19 +10001133 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001134 free(tmpenv[i]);
1135 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +10001136}
1137#endif /* HAVE_ETC_DEFAULT_LOGIN */
1138
Damien Miller7dff8692005-05-26 11:34:51 +10001139void
1140copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +10001141{
Damien Millerbb9ffc12002-01-08 10:59:32 +11001142 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +10001143 int i;
1144
Damien Millerbb9ffc12002-01-08 10:59:32 +11001145 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001146 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001147
Damien Millerbb9ffc12002-01-08 10:59:32 +11001148 for(i = 0; source[i] != NULL; i++) {
1149 var_name = xstrdup(source[i]);
1150 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001151 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001152 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001153 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001154 *var_val++ = '\0';
1155
1156 debug3("Copy environment: %s=%s", var_name, var_val);
1157 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001158
Darren Tuckerf60845f2013-06-02 08:07:31 +10001159 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001160 }
1161}
Damien Millercb5e44a2000-09-29 12:12:36 +11001162
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001163static char **
1164do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001165{
djm@openbsd.org95767262016-03-07 19:02:43 +00001166 struct ssh *ssh = active_state; /* XXX */
Damien Millerb38eff82000-04-01 11:09:21 +10001167 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001168 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001169 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001170 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001171#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001172 char *path = NULL;
1173#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001174
Damien Millerb38eff82000-04-01 11:09:21 +10001175 /* Initialize the environment. */
1176 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001177 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001178 env[0] = NULL;
1179
Damien Millerbac2d8a2000-09-05 16:13:06 +11001180#ifdef HAVE_CYGWIN
1181 /*
1182 * The Windows environment contains some setting which are
1183 * important for a running system. They must not be dropped.
1184 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001185 {
1186 char **p;
1187
1188 p = fetch_windows_environment();
1189 copy_environment(p, &env, &envsize);
1190 free_windows_environment(p);
1191 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001192#endif
1193
Darren Tucker0efd1552003-08-26 11:49:55 +10001194#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001195 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001196 * the childs environment as they see fit
1197 */
1198 ssh_gssapi_do_child(&env, &envsize);
1199#endif
1200
Damien Millerb38eff82000-04-01 11:09:21 +10001201 if (!options.use_login) {
1202 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001203 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001204 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001205 s->env[i].val);
1206
Damien Millerb38eff82000-04-01 11:09:21 +10001207 child_set_env(&env, &envsize, "USER", pw->pw_name);
1208 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001209#ifdef _AIX
1210 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1211#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001212 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001213#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001214 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1215 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1216 else
1217 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001218#else /* HAVE_LOGIN_CAP */
1219# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001220 /*
1221 * There's no standard path on Windows. The path contains
1222 * important components pointing to the system directories,
1223 * needed for loading shared libraries. So the path better
1224 * remains intact here.
1225 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001226# ifdef HAVE_ETC_DEFAULT_LOGIN
1227 read_etc_default_login(&env, &envsize, pw->pw_uid);
1228 path = child_get_env(env, "PATH");
1229# endif /* HAVE_ETC_DEFAULT_LOGIN */
1230 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001231 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001232 s->pw->pw_uid == 0 ?
1233 SUPERUSER_PATH : _PATH_STDPATH);
1234 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001235# endif /* HAVE_CYGWIN */
1236#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001237
1238 snprintf(buf, sizeof buf, "%.200s/%.50s",
1239 _PATH_MAILDIR, pw->pw_name);
1240 child_set_env(&env, &envsize, "MAIL", buf);
1241
1242 /* Normal systems set SHELL by default. */
1243 child_set_env(&env, &envsize, "SHELL", shell);
1244 }
1245 if (getenv("TZ"))
1246 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1247
1248 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001249 if (!options.use_login) {
1250 while (custom_environment) {
1251 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001252 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001253
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001254 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001255 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001256 if (str[i] == '=') {
1257 str[i] = 0;
1258 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001259 }
1260 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +10001261 free(ce->s);
1262 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001263 }
Damien Millerb38eff82000-04-01 11:09:21 +10001264 }
1265
Damien Millerf37e2462002-09-19 11:47:55 +10001266 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001267 snprintf(buf, sizeof buf, "%.50s %d %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001268 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1269 ssh_local_port(ssh));
Damien Millerb38eff82000-04-01 11:09:21 +10001270 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1271
Damien Miller00111382003-03-10 11:21:17 +11001272 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001273 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001274 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1275 laddr, ssh_local_port(ssh));
Darren Tuckera627d422013-06-02 07:31:17 +10001276 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001277 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1278
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001279 if (s->ttyfd != -1)
1280 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1281 if (s->term)
1282 child_set_env(&env, &envsize, "TERM", s->term);
1283 if (s->display)
1284 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001285 if (original_command)
1286 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1287 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001288
Tim Rice81ed5182002-09-25 17:38:46 -07001289#ifdef _UNICOS
1290 if (cray_tmpdir[0] != '\0')
1291 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1292#endif /* _UNICOS */
1293
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001294 /*
1295 * Since we clear KRB5CCNAME at startup, if it's set now then it
1296 * must have been set by a native authentication method (eg AIX or
1297 * SIA), so copy it to the child.
1298 */
1299 {
1300 char *cp;
1301
1302 if ((cp = getenv("KRB5CCNAME")) != NULL)
1303 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1304 }
1305
Damien Millerb38eff82000-04-01 11:09:21 +10001306#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001307 {
1308 char *cp;
1309
1310 if ((cp = getenv("AUTHSTATE")) != NULL)
1311 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001312 read_environment_file(&env, &envsize, "/etc/environment");
1313 }
Damien Millerb38eff82000-04-01 11:09:21 +10001314#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001315#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001316 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001317 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001318 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001319#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001320#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001321 /*
1322 * Pull in any environment variables that may have
1323 * been set by PAM.
1324 */
Damien Miller85bdcd72016-04-13 10:39:57 +10001325 if (options.use_pam && !options.use_login) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001326 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001327
Damien Millerc756e9b2003-11-17 21:41:42 +11001328 p = fetch_pam_child_environment();
1329 copy_environment(p, &env, &envsize);
1330 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001331
Damien Millerc756e9b2003-11-17 21:41:42 +11001332 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001333 copy_environment(p, &env, &envsize);
1334 free_pam_environment(p);
1335 }
Damien Millerb38eff82000-04-01 11:09:21 +10001336#endif /* USE_PAM */
1337
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001338 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001339 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001340 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001341
1342 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001343 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001344 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001345 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001346 read_environment_file(&env, &envsize, buf);
1347 }
1348 if (debug_flag) {
1349 /* dump the environment */
1350 fprintf(stderr, "Environment:\n");
1351 for (i = 0; env[i]; i++)
1352 fprintf(stderr, " %.200s\n", env[i]);
1353 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001354 return env;
1355}
1356
1357/*
1358 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1359 * first in this order).
1360 */
1361static void
1362do_rc_files(Session *s, const char *shell)
1363{
1364 FILE *f = NULL;
1365 char cmd[1024];
1366 int do_xauth;
1367 struct stat st;
1368
1369 do_xauth =
1370 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1371
Damien Millera1b48cc2008-03-27 11:02:02 +11001372 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001373 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001374 !no_user_rc && options.permit_user_rc &&
1375 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001376 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1377 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1378 if (debug_flag)
1379 fprintf(stderr, "Running %s\n", cmd);
1380 f = popen(cmd, "w");
1381 if (f) {
1382 if (do_xauth)
1383 fprintf(f, "%s %s\n", s->auth_proto,
1384 s->auth_data);
1385 pclose(f);
1386 } else
1387 fprintf(stderr, "Could not run %s\n",
1388 _PATH_SSH_USER_RC);
1389 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1390 if (debug_flag)
1391 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1392 _PATH_SSH_SYSTEM_RC);
1393 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1394 if (f) {
1395 if (do_xauth)
1396 fprintf(f, "%s %s\n", s->auth_proto,
1397 s->auth_data);
1398 pclose(f);
1399 } else
1400 fprintf(stderr, "Could not run %s\n",
1401 _PATH_SSH_SYSTEM_RC);
1402 } else if (do_xauth && options.xauth_location != NULL) {
1403 /* Add authority data to .Xauthority if appropriate. */
1404 if (debug_flag) {
1405 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001406 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001407 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001408 fprintf(stderr,
1409 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001410 options.xauth_location, s->auth_display,
1411 s->auth_proto, s->auth_data);
1412 }
1413 snprintf(cmd, sizeof cmd, "%s -q -",
1414 options.xauth_location);
1415 f = popen(cmd, "w");
1416 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001417 fprintf(f, "remove %s\n",
1418 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001419 fprintf(f, "add %s %s %s\n",
1420 s->auth_display, s->auth_proto,
1421 s->auth_data);
1422 pclose(f);
1423 } else {
1424 fprintf(stderr, "Could not run %s\n",
1425 cmd);
1426 }
1427 }
1428}
1429
1430static void
1431do_nologin(struct passwd *pw)
1432{
1433 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001434 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1435 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001436
1437#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001438 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001439 return;
1440 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001441#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001442 if (pw->pw_uid == 0)
1443 return;
1444 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001445#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001446 if (stat(nl, &sb) == -1) {
1447 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001448 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001449 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001450 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001451
1452 /* /etc/nologin exists. Print its contents if we can and exit. */
1453 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1454 if ((f = fopen(nl, "r")) != NULL) {
1455 while (fgets(buf, sizeof(buf), f))
1456 fputs(buf, stderr);
1457 fclose(f);
1458 }
1459 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001460}
1461
Damien Millerd8cb1f12008-02-10 22:40:12 +11001462/*
1463 * Chroot into a directory after checking it for safety: all path components
1464 * must be root-owned directories with strict permissions.
1465 */
1466static void
1467safely_chroot(const char *path, uid_t uid)
1468{
1469 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001470 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001471 struct stat st;
1472
1473 if (*path != '/')
1474 fatal("chroot path does not begin at root");
1475 if (strlen(path) >= sizeof(component))
1476 fatal("chroot path too long");
1477
1478 /*
1479 * Descend the path, checking that each component is a
1480 * root-owned directory with strict permissions.
1481 */
1482 for (cp = path; cp != NULL;) {
1483 if ((cp = strchr(cp, '/')) == NULL)
1484 strlcpy(component, path, sizeof(component));
1485 else {
1486 cp++;
1487 memcpy(component, path, cp - path);
1488 component[cp - path] = '\0';
1489 }
1490
1491 debug3("%s: checking '%s'", __func__, component);
1492
1493 if (stat(component, &st) != 0)
1494 fatal("%s: stat(\"%s\"): %s", __func__,
1495 component, strerror(errno));
1496 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1497 fatal("bad ownership or modes for chroot "
1498 "directory %s\"%s\"",
1499 cp == NULL ? "" : "component ", component);
1500 if (!S_ISDIR(st.st_mode))
1501 fatal("chroot path %s\"%s\" is not a directory",
1502 cp == NULL ? "" : "component ", component);
1503
1504 }
1505
1506 if (chdir(path) == -1)
1507 fatal("Unable to chdir to chroot path \"%s\": "
1508 "%s", path, strerror(errno));
1509 if (chroot(path) == -1)
1510 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1511 if (chdir("/") == -1)
1512 fatal("%s: chdir(/) after chroot: %s",
1513 __func__, strerror(errno));
1514 verbose("Changed root directory to \"%s\"", path);
1515}
1516
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001517/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001519do_setusercontext(struct passwd *pw)
1520{
Damien Miller54e37732008-02-10 22:48:55 +11001521 char *chroot_path, *tmp;
1522
Darren Tucker97528352010-11-05 12:03:05 +11001523 platform_setusercontext(pw);
1524
Darren Tuckerb12fe272010-11-05 14:47:01 +11001525 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001526#ifdef HAVE_LOGIN_CAP
1527 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001528 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001529 perror("unable to set user context");
1530 exit(1);
1531 }
1532#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001533 if (setlogin(pw->pw_name) < 0)
1534 error("setlogin failed: %s", strerror(errno));
1535 if (setgid(pw->pw_gid) < 0) {
1536 perror("setgid");
1537 exit(1);
1538 }
1539 /* Initialize the group list. */
1540 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1541 perror("initgroups");
1542 exit(1);
1543 }
1544 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001545#endif
1546
Darren Tucker920612e2010-11-05 12:36:15 +11001547 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001548
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001549 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001550 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001551 tmp = tilde_expand_filename(options.chroot_directory,
1552 pw->pw_uid);
1553 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1554 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001555 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001556 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001557 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001558 /* Make sure we don't attempt to chroot again */
1559 free(options.chroot_directory);
1560 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001561 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001562 }
1563
1564#ifdef HAVE_LOGIN_CAP
1565 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1566 perror("unable to set user context (setuser)");
1567 exit(1);
1568 }
Damien Miller58528402013-03-15 11:22:37 +11001569 /*
1570 * FreeBSD's setusercontext() will not apply the user's
1571 * own umask setting unless running with the user's UID.
1572 */
1573 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001574#else
Tim Rice9464ba62014-01-20 17:59:28 -08001575# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001576 /*
1577 * In a chroot environment, the set_id() will always fail;
1578 * typically because of the lack of necessary authentication
1579 * services and runtime such as ./usr/lib/libiaf.so,
1580 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1581 * internal sftp chroot case. We'll lose auditing and ACLs but
1582 * permanently_set_uid will take care of the rest.
1583 */
1584 if (!in_chroot && set_id(pw->pw_name) != 0)
1585 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001586# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001587 /* Permanently switch to the desired uid. */
1588 permanently_set_uid(pw);
1589#endif
Damien Millera56086b2013-04-23 15:24:18 +10001590 } else if (options.chroot_directory != NULL &&
1591 strcasecmp(options.chroot_directory, "none") != 0) {
1592 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001593 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001594
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001595 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1596 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1597}
1598
Ben Lindstrom08105192002-03-22 02:50:06 +00001599static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001600do_pwchange(Session *s)
1601{
Darren Tucker09991742004-07-17 17:05:14 +10001602 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001603 fprintf(stderr, "WARNING: Your password has expired.\n");
1604 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001605 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001606 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001607#ifdef WITH_SELINUX
1608 setexeccon(NULL);
1609#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001610#ifdef PASSWD_NEEDS_USERNAME
1611 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1612 (char *)NULL);
1613#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001614 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001615#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001616 perror("passwd");
1617 } else {
1618 fprintf(stderr,
1619 "Password change required but no TTY available.\n");
1620 }
1621 exit(1);
1622}
1623
1624static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625launch_login(struct passwd *pw, const char *hostname)
1626{
1627 /* Launch login(1). */
1628
Ben Lindstrom378a4172002-06-07 14:49:56 +00001629 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001630#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001631 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001633#ifdef LOGIN_NO_ENDOPT
1634 "-p", "-f", pw->pw_name, (char *)NULL);
1635#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001636 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001637#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638
1639 /* Login couldn't be executed, die. */
1640
1641 perror("login");
1642 exit(1);
1643}
1644
Darren Tucker23bc8d02004-02-06 16:24:31 +11001645static void
1646child_close_fds(void)
1647{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001648 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001649
djm@openbsd.org141efe42015-01-14 20:05:27 +00001650 if (auth_sock != -1) {
1651 close(auth_sock);
1652 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001653 }
1654
Darren Tucker23bc8d02004-02-06 16:24:31 +11001655 if (packet_get_connection_in() == packet_get_connection_out())
1656 close(packet_get_connection_in());
1657 else {
1658 close(packet_get_connection_in());
1659 close(packet_get_connection_out());
1660 }
1661 /*
1662 * Close all descriptors related to channels. They will still remain
1663 * open in the parent.
1664 */
1665 /* XXX better use close-on-exec? -markus */
1666 channel_close_all();
1667
1668 /*
1669 * Close any extra file descriptors. Note that there may still be
1670 * descriptors left by system functions. They will be closed later.
1671 */
1672 endpwent();
1673
1674 /*
Damien Miller788f2122005-11-05 15:14:59 +11001675 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001676 * hanging around in clients. Note that we want to do this after
1677 * initgroups, because at least on Solaris 2.3 it leaves file
1678 * descriptors open.
1679 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001680 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001681}
1682
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001683/*
1684 * Performs common processing for the child, such as setting up the
1685 * environment, closing extra file descriptors, setting the user and group
1686 * ids, and executing the command or shell.
1687 */
Damien Millerdfc24252008-02-10 22:29:40 +11001688#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001689void
1690do_child(Session *s, const char *command)
1691{
djm@openbsd.org95767262016-03-07 19:02:43 +00001692 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001693 extern char **environ;
1694 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001695 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001696 const char *shell, *shell0, *hostname = NULL;
1697 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001698 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001699
1700 /* remove hostkey from the child's memory */
1701 destroy_sensitive_data();
1702
Darren Tucker23bc8d02004-02-06 16:24:31 +11001703 /* Force a password change */
1704 if (s->authctxt->force_pwchange) {
1705 do_setusercontext(pw);
1706 child_close_fds();
1707 do_pwchange(s);
1708 exit(1);
1709 }
1710
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001711 /* login(1) is only called if we execute the login shell */
1712 if (options.use_login && command != NULL)
1713 options.use_login = 0;
1714
Tim Rice81ed5182002-09-25 17:38:46 -07001715#ifdef _UNICOS
1716 cray_setup(pw->pw_uid, pw->pw_name, command);
1717#endif /* _UNICOS */
1718
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001719 /*
1720 * Login(1) does this as well, and it needs uid 0 for the "-h"
1721 * switch, so we let login(1) to this for us.
1722 */
1723 if (!options.use_login) {
1724#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001725 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001726 if (!check_quietlogin(s, command))
1727 do_motd();
1728#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001729 /* When PAM is enabled we rely on it to do the nologin check */
1730 if (!options.use_pam)
1731 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001732 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001733 /*
1734 * PAM session modules in do_setusercontext may have
1735 * generated messages, so if this in an interactive
1736 * login then display them too.
1737 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001738 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001739 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001740#endif /* HAVE_OSF_SIA */
1741 }
1742
Darren Tucker69687f42004-09-11 22:17:26 +10001743#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001744 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1745 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001746 display_loginmsg();
1747 exit(254);
1748 }
1749#endif
1750
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001751 /*
1752 * Get the shell from the password data. An empty shell field is
1753 * legal, and means /bin/sh.
1754 */
1755 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001756
1757 /*
1758 * Make sure $SHELL points to the shell from the password file,
1759 * even if shell is overridden from login.conf
1760 */
1761 env = do_setup_env(s, shell);
1762
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001763#ifdef HAVE_LOGIN_CAP
1764 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1765#endif
1766
Damien Millerad833b32000-08-23 10:46:23 +10001767 /* we have to stash the hostname before we close our socket. */
1768 if (options.use_login)
djm@openbsd.org95767262016-03-07 19:02:43 +00001769 hostname = session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001770 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001771 /*
1772 * Close the connection descriptors; note that this is the child, and
1773 * the server will still have the socket open, and it is important
1774 * that we do not shutdown it. Note that the descriptors cannot be
1775 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001776 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001777 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001778 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001779
Damien Millerb38eff82000-04-01 11:09:21 +10001780 /*
Damien Miller05eda432002-02-10 18:32:28 +11001781 * Must take new environment into use so that .ssh/rc,
1782 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001783 */
1784 environ = env;
1785
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001786#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001787 /*
1788 * At this point, we check to see if AFS is active and if we have
1789 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1790 * if we can (and need to) extend the ticket into an AFS token. If
1791 * we don't do this, we run into potential problems if the user's
1792 * home directory is in AFS and it's not world-readable.
1793 */
1794
1795 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001796 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001797 char cell[64];
1798
1799 debug("Getting AFS token");
1800
1801 k_setpag();
1802
1803 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1804 krb5_afslog(s->authctxt->krb5_ctx,
1805 s->authctxt->krb5_fwd_ccache, cell, NULL);
1806
1807 krb5_afslog_home(s->authctxt->krb5_ctx,
1808 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1809 }
1810#endif
1811
Damien Miller788f2122005-11-05 15:14:59 +11001812 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001813 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001814 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001815#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001816 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001817#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001818 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001819 fprintf(stderr, "Could not chdir to home "
1820 "directory %s: %s\n", pw->pw_dir,
1821 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001822 }
Damien Miller6051c942008-06-16 07:53:16 +10001823 if (r)
1824 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001825 }
1826
Damien Millera1939002008-03-15 17:27:58 +11001827 closefrom(STDERR_FILENO + 1);
1828
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001829 if (!options.use_login)
1830 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001831
1832 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001833 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001834
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001835 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1836 printf("This service allows sftp connections only.\n");
1837 fflush(NULL);
1838 exit(1);
1839 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001840 extern int optind, optreset;
1841 int i;
1842 char *p, *args;
1843
Darren Tuckerac46a912009-06-21 17:55:23 +10001844 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001845 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001846 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1847 if (i < ARGV_MAX - 1)
1848 argv[i++] = p;
1849 argv[i] = NULL;
1850 optind = optreset = 1;
1851 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001852#ifdef WITH_SELINUX
1853 ssh_selinux_change_context("sftpd_t");
1854#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001855 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001856 }
1857
Darren Tucker695ed392009-10-07 09:02:18 +11001858 fflush(NULL);
1859
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001860 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001861 launch_login(pw, hostname);
1862 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001863 }
1864
1865 /* Get the last component of the shell name. */
1866 if ((shell0 = strrchr(shell, '/')) != NULL)
1867 shell0++;
1868 else
1869 shell0 = shell;
1870
Damien Millerb38eff82000-04-01 11:09:21 +10001871 /*
1872 * If we have no command, execute the shell. In this case, the shell
1873 * name to be passed in argv[0] is preceded by '-' to indicate that
1874 * this is a login shell.
1875 */
1876 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001877 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001878
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001879 /* Start the shell. Set initial character to '-'. */
1880 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001881
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001882 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1883 >= sizeof(argv0) - 1) {
1884 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001885 perror(shell);
1886 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001887 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001888
1889 /* Execute the shell. */
1890 argv[0] = argv0;
1891 argv[1] = NULL;
1892 execve(shell, argv, env);
1893
1894 /* Executing the shell failed. */
1895 perror(shell);
1896 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001897 }
1898 /*
1899 * Execute the command using the user's shell. This uses the -c
1900 * option to execute the command.
1901 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001902 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001903 argv[1] = "-c";
1904 argv[2] = (char *) command;
1905 argv[3] = NULL;
1906 execve(shell, argv, env);
1907 perror(shell);
1908 exit(1);
1909}
1910
Damien Miller7207f642008-05-19 15:34:50 +10001911void
1912session_unused(int id)
1913{
1914 debug3("%s: session id %d unused", __func__, id);
1915 if (id >= options.max_sessions ||
1916 id >= sessions_nalloc) {
1917 fatal("%s: insane session id %d (max %d nalloc %d)",
1918 __func__, id, options.max_sessions, sessions_nalloc);
1919 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001920 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001921 sessions[id].self = id;
1922 sessions[id].used = 0;
1923 sessions[id].chanid = -1;
1924 sessions[id].ptyfd = -1;
1925 sessions[id].ttyfd = -1;
1926 sessions[id].ptymaster = -1;
1927 sessions[id].x11_chanids = NULL;
1928 sessions[id].next_unused = sessions_first_unused;
1929 sessions_first_unused = id;
1930}
1931
Damien Millerb38eff82000-04-01 11:09:21 +10001932Session *
1933session_new(void)
1934{
Damien Miller7207f642008-05-19 15:34:50 +10001935 Session *s, *tmp;
1936
1937 if (sessions_first_unused == -1) {
1938 if (sessions_nalloc >= options.max_sessions)
1939 return NULL;
1940 debug2("%s: allocate (allocated %d max %d)",
1941 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001942 tmp = xreallocarray(sessions, sessions_nalloc + 1,
Damien Miller7207f642008-05-19 15:34:50 +10001943 sizeof(*sessions));
1944 if (tmp == NULL) {
1945 error("%s: cannot allocate %d sessions",
1946 __func__, sessions_nalloc + 1);
1947 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001948 }
Damien Miller7207f642008-05-19 15:34:50 +10001949 sessions = tmp;
1950 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001951 }
Damien Miller7207f642008-05-19 15:34:50 +10001952
1953 if (sessions_first_unused >= sessions_nalloc ||
1954 sessions_first_unused < 0) {
1955 fatal("%s: insane first_unused %d max %d nalloc %d",
1956 __func__, sessions_first_unused, options.max_sessions,
1957 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001958 }
Damien Miller7207f642008-05-19 15:34:50 +10001959
1960 s = &sessions[sessions_first_unused];
1961 if (s->used) {
1962 fatal("%s: session %d already used",
1963 __func__, sessions_first_unused);
1964 }
1965 sessions_first_unused = s->next_unused;
1966 s->used = 1;
1967 s->next_unused = -1;
1968 debug("session_new: session %d", s->self);
1969
1970 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001971}
1972
Ben Lindstrombba81212001-06-25 05:01:22 +00001973static void
Damien Millerb38eff82000-04-01 11:09:21 +10001974session_dump(void)
1975{
1976 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001977 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001978 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001979
1980 debug("dump: used %d next_unused %d session %d %p "
1981 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001982 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001983 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001984 s->self,
1985 s,
1986 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001987 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001988 }
1989}
1990
Damien Millerefb4afe2000-04-12 18:45:05 +10001991int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001992session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001993{
1994 Session *s = session_new();
1995 debug("session_open: channel %d", chanid);
1996 if (s == NULL) {
1997 error("no more sessions");
1998 return 0;
1999 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00002000 s->authctxt = authctxt;
2001 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11002002 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00002003 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002004 debug("session_open: session %d: link with channel %d", s->self, chanid);
2005 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10002006 return 1;
2007}
2008
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002009Session *
2010session_by_tty(char *tty)
2011{
2012 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002013 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002014 Session *s = &sessions[i];
2015 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
2016 debug("session_by_tty: session %d tty %s", i, tty);
2017 return s;
2018 }
2019 }
2020 debug("session_by_tty: unknown tty %.100s", tty);
2021 session_dump();
2022 return NULL;
2023}
2024
Ben Lindstrombba81212001-06-25 05:01:22 +00002025static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002026session_by_channel(int id)
2027{
2028 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002029 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002030 Session *s = &sessions[i];
2031 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10002032 debug("session_by_channel: session %d channel %d",
2033 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002034 return s;
2035 }
2036 }
2037 debug("session_by_channel: unknown channel %d", id);
2038 session_dump();
2039 return NULL;
2040}
2041
Ben Lindstrombba81212001-06-25 05:01:22 +00002042static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10002043session_by_x11_channel(int id)
2044{
2045 int i, j;
2046
Damien Miller7207f642008-05-19 15:34:50 +10002047 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002048 Session *s = &sessions[i];
2049
2050 if (s->x11_chanids == NULL || !s->used)
2051 continue;
2052 for (j = 0; s->x11_chanids[j] != -1; j++) {
2053 if (s->x11_chanids[j] == id) {
2054 debug("session_by_x11_channel: session %d "
2055 "channel %d", s->self, id);
2056 return s;
2057 }
2058 }
2059 }
2060 debug("session_by_x11_channel: unknown channel %d", id);
2061 session_dump();
2062 return NULL;
2063}
2064
2065static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002066session_by_pid(pid_t pid)
2067{
2068 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00002069 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10002070 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002071 Session *s = &sessions[i];
2072 if (s->used && s->pid == pid)
2073 return s;
2074 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002075 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002076 session_dump();
2077 return NULL;
2078}
2079
Ben Lindstrombba81212001-06-25 05:01:22 +00002080static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002081session_window_change_req(Session *s)
2082{
2083 s->col = packet_get_int();
2084 s->row = packet_get_int();
2085 s->xpixel = packet_get_int();
2086 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002087 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10002088 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2089 return 1;
2090}
2091
Ben Lindstrombba81212001-06-25 05:01:22 +00002092static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002093session_pty_req(Session *s)
2094{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002095 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00002096 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002097
Damien Miller5ff30c62013-10-30 22:21:50 +11002098 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00002099 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10002100 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002101 }
2102 if (s->ttyfd != -1) {
2103 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10002104 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002105 }
2106
Damien Millerefb4afe2000-04-12 18:45:05 +10002107 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002108
2109 if (compat20) {
2110 s->col = packet_get_int();
2111 s->row = packet_get_int();
2112 } else {
2113 s->row = packet_get_int();
2114 s->col = packet_get_int();
2115 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002116 s->xpixel = packet_get_int();
2117 s->ypixel = packet_get_int();
2118
2119 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002120 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002121 s->term = NULL;
2122 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00002123
Damien Millerefb4afe2000-04-12 18:45:05 +10002124 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00002125 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10002126 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2127 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10002128 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002129 s->term = NULL;
2130 s->ptyfd = -1;
2131 s->ttyfd = -1;
2132 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10002133 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002134 }
2135 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002136
2137 /* for SSH1 the tty modes length is not given */
2138 if (!compat20)
2139 n_bytes = packet_remaining();
2140 tty_parse_modes(s->ttyfd, &n_bytes);
2141
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002142 if (!use_privsep)
2143 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002144
2145 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10002146 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2147
Damien Miller48b03fc2002-01-22 23:11:40 +11002148 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10002149 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002150 return 1;
2151}
2152
Ben Lindstrombba81212001-06-25 05:01:22 +00002153static int
Damien Millerbd483e72000-04-30 10:00:53 +10002154session_subsystem_req(Session *s)
2155{
Damien Millerae452462001-10-10 15:08:06 +10002156 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002157 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10002158 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11002159 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10002160 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10002161
Damien Miller71df7522013-10-15 12:12:02 +11002162 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002163 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11002164 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10002165 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10002166
Damien Millerf6d9e222000-06-18 14:50:44 +10002167 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11002168 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10002169 prog = options.subsystem_command[i];
2170 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002171 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11002172 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002173 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11002174 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002175 if (stat(prog, &st) < 0)
2176 debug("subsystem: cannot stat %s: %s",
2177 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11002178 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002179 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10002180 }
Damien Miller7207f642008-05-19 15:34:50 +10002181 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11002182 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10002183 }
2184 }
2185
2186 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11002187 logit("subsystem request for %.100s by user %s failed, "
2188 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10002189
Damien Millerbd483e72000-04-30 10:00:53 +10002190 return success;
2191}
2192
Ben Lindstrombba81212001-06-25 05:01:22 +00002193static int
Damien Millerbd483e72000-04-30 10:00:53 +10002194session_x11_req(Session *s)
2195{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002196 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002197
Damien Miller2b9b0452005-07-17 17:19:24 +10002198 if (s->auth_proto != NULL || s->auth_data != NULL) {
2199 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002200 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002201 return 0;
2202 }
Damien Millerbd483e72000-04-30 10:00:53 +10002203 s->single_connection = packet_get_char();
2204 s->auth_proto = packet_get_string(NULL);
2205 s->auth_data = packet_get_string(NULL);
2206 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002207 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002208
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00002209 if (xauth_valid_string(s->auth_proto) &&
2210 xauth_valid_string(s->auth_data))
2211 success = session_setup_x11fwd(s);
2212 else {
2213 success = 0;
2214 error("Invalid X11 forwarding data");
2215 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002216 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002217 free(s->auth_proto);
2218 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002219 s->auth_proto = NULL;
2220 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002221 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002222 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002223}
2224
Ben Lindstrombba81212001-06-25 05:01:22 +00002225static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002226session_shell_req(Session *s)
2227{
Damien Miller48b03fc2002-01-22 23:11:40 +11002228 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002229 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002230}
2231
Ben Lindstrombba81212001-06-25 05:01:22 +00002232static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002233session_exec_req(Session *s)
2234{
Damien Miller7207f642008-05-19 15:34:50 +10002235 u_int len, success;
2236
Damien Millerf6d9e222000-06-18 14:50:44 +10002237 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002238 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002239 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002240 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002241 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002242}
2243
Ben Lindstrombba81212001-06-25 05:01:22 +00002244static int
Damien Miller54c45982003-05-15 10:20:13 +10002245session_break_req(Session *s)
2246{
Damien Miller54c45982003-05-15 10:20:13 +10002247
Darren Tucker1f8311c2004-05-13 16:39:33 +10002248 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002249 packet_check_eom();
2250
Darren Tucker9c5d5532011-11-04 10:55:24 +11002251 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002252 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002253 return 1;
2254}
2255
2256static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002257session_env_req(Session *s)
2258{
2259 char *name, *val;
2260 u_int name_len, val_len, i;
2261
Damien Miller8569eba2014-03-04 09:35:17 +11002262 name = packet_get_cstring(&name_len);
2263 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002264 packet_check_eom();
2265
2266 /* Don't set too many environment variables */
2267 if (s->num_env > 128) {
2268 debug2("Ignoring env request %s: too many env vars", name);
2269 goto fail;
2270 }
2271
2272 for (i = 0; i < options.num_accept_env; i++) {
2273 if (match_pattern(name, options.accept_env[i])) {
2274 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002275 s->env = xreallocarray(s->env, s->num_env + 1,
Damien Miller36812092006-03-26 14:22:47 +11002276 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002277 s->env[s->num_env].name = name;
2278 s->env[s->num_env].val = val;
2279 s->num_env++;
2280 return (1);
2281 }
2282 }
2283 debug2("Ignoring env request %s: disallowed name", name);
2284
2285 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002286 free(name);
2287 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002288 return (0);
2289}
2290
2291static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002292session_auth_agent_req(Session *s)
2293{
2294 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002295 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002296 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002297 debug("session_auth_agent_req: no_agent_forwarding_flag");
2298 return 0;
2299 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002300 if (called) {
2301 return 0;
2302 } else {
2303 called = 1;
2304 return auth_input_request_forwarding(s->pw);
2305 }
2306}
2307
Damien Millerc7ef63d2002-02-05 12:21:42 +11002308int
2309session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002310{
Damien Millerefb4afe2000-04-12 18:45:05 +10002311 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002312 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002313
Damien Millerc7ef63d2002-02-05 12:21:42 +11002314 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002315 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002316 c->self, rtype);
2317 return 0;
2318 }
2319 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002320
2321 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002322 * a session is in LARVAL state until a shell, a command
2323 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002324 */
2325 if (c->type == SSH_CHANNEL_LARVAL) {
2326 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002327 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002328 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002329 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002330 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002331 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002332 } else if (strcmp(rtype, "x11-req") == 0) {
2333 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002334 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2335 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002336 } else if (strcmp(rtype, "subsystem") == 0) {
2337 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002338 } else if (strcmp(rtype, "env") == 0) {
2339 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002340 }
2341 }
2342 if (strcmp(rtype, "window-change") == 0) {
2343 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002344 } else if (strcmp(rtype, "break") == 0) {
2345 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002346 }
Damien Millera6b1d162004-06-30 22:41:07 +10002347
Damien Millerc7ef63d2002-02-05 12:21:42 +11002348 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002349}
2350
2351void
Damien Miller8853ca52010-06-26 10:00:14 +10002352session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2353 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002354{
2355 if (!compat20)
2356 fatal("session_set_fds: called for proto != 2.0");
2357 /*
2358 * now that have a child and a pipe to the child,
2359 * we can activate our channel and register the fd's
2360 */
2361 if (s->chanid == -1)
2362 fatal("no channel for session %d", s->self);
2363 channel_set_fds(s->chanid,
2364 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002365 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002366 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002367}
2368
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002369/*
2370 * Function to perform pty cleanup. Also called if we get aborted abnormally
2371 * (e.g., due to a dropped connection).
2372 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002373void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002374session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002375{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002376 if (s == NULL) {
2377 error("session_pty_cleanup: no session");
2378 return;
2379 }
2380 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002381 return;
2382
Kevin Steves43cdef32001-02-11 14:12:08 +00002383 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002384
Damien Millerb38eff82000-04-01 11:09:21 +10002385 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002386 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002387 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002388
2389 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002390 if (getuid() == 0)
2391 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002392
2393 /*
2394 * Close the server side of the socket pairs. We must do this after
2395 * the pty cleanup, so that another process doesn't get this pty
2396 * while we're still cleaning up.
2397 */
Damien Miller7207f642008-05-19 15:34:50 +10002398 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2399 error("close(s->ptymaster/%d): %s",
2400 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002401
2402 /* unlink pty from session */
2403 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002404}
Damien Millerefb4afe2000-04-12 18:45:05 +10002405
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002406void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002407session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002408{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002409 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002410}
2411
Damien Miller5a80bba2002-09-04 16:39:02 +10002412static char *
2413sig2name(int sig)
2414{
2415#define SSH_SIG(x) if (sig == SIG ## x) return #x
2416 SSH_SIG(ABRT);
2417 SSH_SIG(ALRM);
2418 SSH_SIG(FPE);
2419 SSH_SIG(HUP);
2420 SSH_SIG(ILL);
2421 SSH_SIG(INT);
2422 SSH_SIG(KILL);
2423 SSH_SIG(PIPE);
2424 SSH_SIG(QUIT);
2425 SSH_SIG(SEGV);
2426 SSH_SIG(TERM);
2427 SSH_SIG(USR1);
2428 SSH_SIG(USR2);
2429#undef SSH_SIG
2430 return "SIG@openssh.com";
2431}
2432
Ben Lindstrombba81212001-06-25 05:01:22 +00002433static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002434session_close_x11(int id)
2435{
2436 Channel *c;
2437
Damien Millerd47c62a2005-12-13 19:33:57 +11002438 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002439 debug("session_close_x11: x11 channel %d missing", id);
2440 } else {
2441 /* Detach X11 listener */
2442 debug("session_close_x11: detach x11 channel %d", id);
2443 channel_cancel_cleanup(id);
2444 if (c->ostate != CHAN_OUTPUT_CLOSED)
2445 chan_mark_dead(c);
2446 }
2447}
2448
2449static void
2450session_close_single_x11(int id, void *arg)
2451{
2452 Session *s;
2453 u_int i;
2454
2455 debug3("session_close_single_x11: channel %d", id);
2456 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002457 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002458 fatal("session_close_single_x11: no x11 channel %d", id);
2459 for (i = 0; s->x11_chanids[i] != -1; i++) {
2460 debug("session_close_single_x11: session %d: "
2461 "closing channel %d", s->self, s->x11_chanids[i]);
2462 /*
2463 * The channel "id" is already closing, but make sure we
2464 * close all of its siblings.
2465 */
2466 if (s->x11_chanids[i] != id)
2467 session_close_x11(s->x11_chanids[i]);
2468 }
Darren Tuckera627d422013-06-02 07:31:17 +10002469 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002470 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002471 free(s->display);
2472 s->display = NULL;
2473 free(s->auth_proto);
2474 s->auth_proto = NULL;
2475 free(s->auth_data);
2476 s->auth_data = NULL;
2477 free(s->auth_display);
2478 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002479}
2480
2481static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002482session_exit_message(Session *s, int status)
2483{
2484 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002485
2486 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002487 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002488 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002489 debug("session_exit_message: session %d channel %d pid %ld",
2490 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002491
2492 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002493 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002494 packet_put_int(WEXITSTATUS(status));
2495 packet_send();
2496 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002497 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002498 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002499#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002500 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002501#else /* WCOREDUMP */
2502 packet_put_char(0);
2503#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002504 packet_put_cstring("");
2505 packet_put_cstring("");
2506 packet_send();
2507 } else {
2508 /* Some weird exit cause. Just exit. */
2509 packet_disconnect("wait returned status %04x.", status);
2510 }
2511
2512 /* disconnect channel */
2513 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002514
2515 /*
2516 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002517 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002518 * by session_close_by_channel when the childs close their fds.
2519 */
2520 channel_register_cleanup(c->self, session_close_by_channel, 1);
2521
Damien Miller166fca82000-04-20 07:42:21 +10002522 /*
2523 * emulate a write failure with 'chan_write_failed', nobody will be
2524 * interested in data we write.
2525 * Note that we must not call 'chan_read_failed', since there could
2526 * be some more data waiting in the pipe.
2527 */
Damien Millerbd483e72000-04-30 10:00:53 +10002528 if (c->ostate != CHAN_OUTPUT_CLOSED)
2529 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002530}
2531
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002532void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002533session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002534{
djm@openbsd.org95767262016-03-07 19:02:43 +00002535 struct ssh *ssh = active_state; /* XXX */
Damien Millereccb9de2005-06-17 12:59:34 +10002536 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002537
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002538 verbose("Close session: user %s from %.200s port %d id %d",
2539 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002540 ssh_remote_ipaddr(ssh),
2541 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002542 s->self);
2543
Darren Tucker3e33cec2003-10-02 16:12:36 +10002544 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002545 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002546 free(s->term);
2547 free(s->display);
2548 free(s->x11_chanids);
2549 free(s->auth_display);
2550 free(s->auth_data);
2551 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002552 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002553 if (s->env != NULL) {
2554 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002555 free(s->env[i].name);
2556 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002557 }
Darren Tuckera627d422013-06-02 07:31:17 +10002558 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002559 }
Damien Millere247cc42000-05-07 12:03:14 +10002560 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002561 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002562}
2563
2564void
2565session_close_by_pid(pid_t pid, int status)
2566{
2567 Session *s = session_by_pid(pid);
2568 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002569 debug("session_close_by_pid: no session for pid %ld",
2570 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002571 return;
2572 }
2573 if (s->chanid != -1)
2574 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002575 if (s->ttyfd != -1)
2576 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002577 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002578}
2579
2580/*
2581 * this is called when a channel dies before
2582 * the session 'child' itself dies
2583 */
2584void
2585session_close_by_channel(int id, void *arg)
2586{
2587 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002588 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002589
Damien Millerefb4afe2000-04-12 18:45:05 +10002590 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002591 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002592 return;
2593 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002594 debug("session_close_by_channel: channel %d child %ld",
2595 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002596 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002597 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002598 /*
2599 * delay detach of session, but release pty, since
2600 * the fd's to the child are already closed
2601 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002602 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002603 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002604 return;
2605 }
2606 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002607 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002608
2609 /* Close any X11 listeners associated with this session */
2610 if (s->x11_chanids != NULL) {
2611 for (i = 0; s->x11_chanids[i] != -1; i++) {
2612 session_close_x11(s->x11_chanids[i]);
2613 s->x11_chanids[i] = -1;
2614 }
2615 }
2616
Damien Millerefb4afe2000-04-12 18:45:05 +10002617 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002618 session_close(s);
2619}
2620
2621void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002622session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002623{
2624 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002625 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002626 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002627 if (s->used) {
2628 if (closefunc != NULL)
2629 closefunc(s);
2630 else
2631 session_close(s);
2632 }
Damien Miller52b77be2001-10-10 15:14:37 +10002633 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002634}
2635
Ben Lindstrombba81212001-06-25 05:01:22 +00002636static char *
Damien Millere247cc42000-05-07 12:03:14 +10002637session_tty_list(void)
2638{
2639 static char buf[1024];
2640 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002641 char *cp;
2642
Damien Millere247cc42000-05-07 12:03:14 +10002643 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002644 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002645 Session *s = &sessions[i];
2646 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002647
Damien Millera8ed44b2003-01-10 09:53:12 +11002648 if (strncmp(s->tty, "/dev/", 5) != 0) {
2649 cp = strrchr(s->tty, '/');
2650 cp = (cp == NULL) ? s->tty : cp + 1;
2651 } else
2652 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002653
Damien Millere247cc42000-05-07 12:03:14 +10002654 if (buf[0] != '\0')
2655 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002656 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002657 }
2658 }
2659 if (buf[0] == '\0')
2660 strlcpy(buf, "notty", sizeof buf);
2661 return buf;
2662}
2663
2664void
2665session_proctitle(Session *s)
2666{
2667 if (s->pw == NULL)
2668 error("no user for session %d", s->self);
2669 else
2670 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2671}
2672
Ben Lindstrom768176b2001-06-09 01:29:12 +00002673int
2674session_setup_x11fwd(Session *s)
2675{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002676 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002677 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002678 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002679 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002680
2681 if (no_x11_forwarding_flag) {
2682 packet_send_debug("X11 forwarding disabled in user configuration file.");
2683 return 0;
2684 }
2685 if (!options.x11_forwarding) {
2686 debug("X11 forwarding disabled in server configuration file.");
2687 return 0;
2688 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002689 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002690 (stat(options.xauth_location, &st) == -1)) {
2691 packet_send_debug("No xauth program; cannot forward with spoofing.");
2692 return 0;
2693 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002694 if (options.use_login) {
2695 packet_send_debug("X11 forwarding disabled; "
2696 "not compatible with UseLogin=yes.");
2697 return 0;
2698 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002699 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002700 debug("X11 display already set.");
2701 return 0;
2702 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002703 if (x11_create_display_inet(options.x11_display_offset,
2704 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002705 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002706 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002707 return 0;
2708 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002709 for (i = 0; s->x11_chanids[i] != -1; i++) {
2710 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002711 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002712 }
Kevin Steves366298c2001-12-19 17:58:01 +00002713
2714 /* Set up a suitable value for the DISPLAY variable. */
2715 if (gethostname(hostname, sizeof(hostname)) < 0)
2716 fatal("gethostname: %.100s", strerror(errno));
2717 /*
2718 * auth_display must be used as the displayname when the
2719 * authorization entry is added with xauth(1). This will be
2720 * different than the DISPLAY string for localhost displays.
2721 */
Damien Miller95c249f2002-02-05 12:11:34 +11002722 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002723 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002724 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002725 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002726 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002727 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002728 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002729 } else {
2730#ifdef IPADDR_IN_DISPLAY
2731 struct hostent *he;
2732 struct in_addr my_addr;
2733
2734 he = gethostbyname(hostname);
2735 if (he == NULL) {
2736 error("Can't get IP address for X11 DISPLAY.");
2737 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2738 return 0;
2739 }
2740 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002741 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002742 s->display_number, s->screen);
2743#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002744 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002745 s->display_number, s->screen);
2746#endif
2747 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002748 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002749 }
2750
Ben Lindstrom768176b2001-06-09 01:29:12 +00002751 return 1;
2752}
2753
Ben Lindstrombba81212001-06-25 05:01:22 +00002754static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002755do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002756{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002757 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002758}
2759
2760void
2761do_cleanup(Authctxt *authctxt)
2762{
2763 static int called = 0;
2764
2765 debug("do_cleanup");
2766
2767 /* no cleanup if we're in the child for login shell */
2768 if (is_child)
2769 return;
2770
2771 /* avoid double cleanup */
2772 if (called)
2773 return;
2774 called = 1;
2775
Darren Tucker9142e1c2007-08-16 23:28:04 +10002776 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002777 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002778
2779#ifdef USE_PAM
2780 if (options.use_pam) {
2781 sshpam_cleanup();
2782 sshpam_thread_cleanup();
2783 }
2784#endif
2785
2786 if (!authctxt->authenticated)
2787 return;
2788
Darren Tucker3e33cec2003-10-02 16:12:36 +10002789#ifdef KRB5
2790 if (options.kerberos_ticket_cleanup &&
2791 authctxt->krb5_ctx)
2792 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002793#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002794
2795#ifdef GSSAPI
2796 if (compat20 && options.gss_cleanup_creds)
2797 ssh_gssapi_cleanup_creds();
2798#endif
2799
2800 /* remove agent socket */
2801 auth_sock_cleanup_proc(authctxt->pw);
2802
2803 /*
2804 * Cleanup ptys/utmp only if privsep is disabled,
2805 * or if running in monitor.
2806 */
2807 if (!use_privsep || mm_is_monitor())
2808 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002809}
djm@openbsd.org95767262016-03-07 19:02:43 +00002810
2811/* Return a name for the remote host that fits inside utmp_size */
2812
2813const char *
2814session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2815{
2816 const char *remote = "";
2817
2818 if (utmp_size > 0)
2819 remote = auth_get_canonical_hostname(ssh, use_dns);
2820 if (utmp_size == 0 || strlen(remote) > utmp_size)
2821 remote = ssh_remote_ipaddr(ssh);
2822 return remote;
2823}
2824