blob: 9a75c622ecf0e1251bc29d40513a9bdbee97644d [file] [log] [blame]
djm@openbsd.org95767262016-03-07 19:02:43 +00001/* $OpenBSD: session.c,v 1.281 2016/03/07 19:02:43 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
Darren Tucker39972492006-07-12 22:22:46 +100049#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100050#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100051#include <grp.h>
Damien Millere5c0d522014-07-03 21:24:19 +100052#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110053#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110054#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110055#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100056#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110057#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100058#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100059#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100060#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100061#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100062#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000063#include <limits.h>
Damien Millerb38eff82000-04-01 11:09:21 +100064
Damien Millerb84886b2008-05-19 15:05:07 +100065#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100066#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100067#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "ssh1.h"
69#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000070#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "packet.h"
72#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100073#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100074#include "uidswap.h"
75#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000076#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100077#include "key.h"
78#include "cipher.h"
79#ifdef GSSAPI
80#include "ssh-gss.h"
81#endif
82#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100083#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100084#include "auth-options.h"
Damien Miller85b45e02013-07-20 13:21:52 +100085#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "pathnames.h"
87#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100088#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000090#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000091#include "serverloop.h"
92#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000093#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100094#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110096#include "sftp.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100097
Darren Tucker3c78c5e2004-01-23 22:03:10 +110098#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110099#include <kafs.h>
100#endif
101
Damien Miller14684a12011-05-20 11:23:07 +1000102#ifdef WITH_SELINUX
103#include <selinux/selinux.h>
104#endif
105
Damien Millerad793d52008-11-03 19:17:57 +1100106#define IS_INTERNAL_SFTP(c) \
107 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
108 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
109 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
110 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
111
Damien Millerb38eff82000-04-01 11:09:21 +1000112/* func */
113
114Session *session_new(void);
Damien Miller8853ca52010-06-26 10:00:14 +1000115void session_set_fds(Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000116void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000117void session_proctitle(Session *);
118int session_setup_x11fwd(Session *);
Damien Miller7207f642008-05-19 15:34:50 +1000119int do_exec_pty(Session *, const char *);
120int do_exec_no_pty(Session *, const char *);
121int do_exec(Session *, const char *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000122void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000123#ifdef LOGIN_NEEDS_UTMPX
124static void do_pre_login(Session *s);
125#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000126void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000127void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000128int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000129
Ben Lindstrombba81212001-06-25 05:01:22 +0000130static void do_authenticated1(Authctxt *);
131static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000132
Ben Lindstrombba81212001-06-25 05:01:22 +0000133static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000134
Damien Millerb38eff82000-04-01 11:09:21 +1000135/* import */
136extern ServerOptions options;
137extern char *__progname;
138extern int log_stderr;
139extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000140extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000141extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000142extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000143extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000144
Damien Miller7b28dc52000-09-05 13:34:53 +1100145/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000146const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100147
Damien Millerb38eff82000-04-01 11:09:21 +1000148/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000149static int sessions_first_unused = -1;
150static int sessions_nalloc = 0;
151static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100152
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100153#define SUBSYSTEM_NONE 0
154#define SUBSYSTEM_EXT 1
155#define SUBSYSTEM_INT_SFTP 2
156#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100157
Damien Millerad833b32000-08-23 10:46:23 +1000158#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000159login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000160#endif
161
Darren Tucker3e33cec2003-10-02 16:12:36 +1000162static int is_child = 0;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +0000163static int in_chroot = 0;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000164
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000165/* Name and directory of socket for authentication agent forwarding. */
166static char *auth_sock_name = NULL;
167static char *auth_sock_dir = NULL;
168
169/* removes the agent forwarding socket */
170
171static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000172auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000173{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000174 if (auth_sock_name != NULL) {
175 temporarily_use_uid(pw);
176 unlink(auth_sock_name);
177 rmdir(auth_sock_dir);
178 auth_sock_name = NULL;
179 restore_uid();
180 }
181}
182
183static int
184auth_input_request_forwarding(struct passwd * pw)
185{
186 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000187 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000188
189 if (auth_sock_name != NULL) {
190 error("authentication forwarding requested twice.");
191 return 0;
192 }
193
194 /* Temporarily drop privileged uid for mkdir/bind. */
195 temporarily_use_uid(pw);
196
197 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000198 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000199
200 /* Create private directory for socket */
201 if (mkdtemp(auth_sock_dir) == NULL) {
202 packet_send_debug("Agent forwarding disabled: "
203 "mkdtemp() failed: %.100s", strerror(errno));
204 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000205 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000206 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000207 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000208 }
Damien Miller7207f642008-05-19 15:34:50 +1000209
210 xasprintf(&auth_sock_name, "%s/agent.%ld",
211 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000212
Damien Miller7acefbb2014-07-18 14:11:24 +1000213 /* Start a Unix listener on auth_sock_name. */
214 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000215
216 /* Restore the privileged uid. */
217 restore_uid();
218
Damien Miller7acefbb2014-07-18 14:11:24 +1000219 /* Check for socket/bind/listen failure. */
220 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000221 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000222
223 /* Allocate a channel for the authentication agent socket. */
224 nc = channel_new("auth socket",
225 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
226 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000227 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100228 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000229 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000230
231 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000232 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000233 if (auth_sock_dir != NULL) {
234 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000235 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000236 }
237 if (sock != -1)
238 close(sock);
239 auth_sock_name = NULL;
240 auth_sock_dir = NULL;
241 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000242}
243
Darren Tucker1921ed92004-02-10 13:23:28 +1100244static void
245display_loginmsg(void)
246{
Damien Miller46d38de2005-07-17 17:02:09 +1000247 if (buffer_len(&loginmsg) > 0) {
248 buffer_append(&loginmsg, "\0", 1);
249 printf("%s", (char *)buffer_ptr(&loginmsg));
250 buffer_clear(&loginmsg);
251 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100252}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000253
Ben Lindstromb31783d2001-03-22 02:02:12 +0000254void
255do_authenticated(Authctxt *authctxt)
256{
Damien Miller97f39ae2003-02-24 11:57:01 +1100257 setproctitle("%s", authctxt->pw->pw_name);
258
Ben Lindstromb31783d2001-03-22 02:02:12 +0000259 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000260 /* XXX - streamlocal? */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100261 if (no_port_forwarding_flag ||
262 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
263 channel_disable_adm_local_opens();
264 else
Ben Lindstromb31783d2001-03-22 02:02:12 +0000265 channel_permit_all_opens();
266
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100267 auth_debug_send();
268
Ben Lindstromb31783d2001-03-22 02:02:12 +0000269 if (compat20)
270 do_authenticated2(authctxt);
271 else
272 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000273
Darren Tucker3e33cec2003-10-02 16:12:36 +1000274 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000275}
276
Damien Millerb38eff82000-04-01 11:09:21 +1000277/*
Damien Millerb38eff82000-04-01 11:09:21 +1000278 * Prepares for an interactive session. This is called after the user has
279 * been successfully authenticated. During this message exchange, pseudo
280 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
281 * are requested, etc.
282 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000284do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000285{
286 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000287 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100288 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000289 int enable_compression_after_reply = 0;
290 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000291
292 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100293 if (s == NULL) {
294 error("no more sessions");
295 return;
296 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000297 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000298 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000299
Damien Millerb38eff82000-04-01 11:09:21 +1000300 /*
301 * We stay in this loop until the client requests to execute a shell
302 * or a command.
303 */
304 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000305 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000306
307 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100308 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000309
310 /* Process the packet. */
311 switch (type) {
312 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000313 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100314 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000315 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000316 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100317 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000318 break;
319 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000320 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000321 debug2("compression disabled");
322 break;
323 }
Damien Millerb38eff82000-04-01 11:09:21 +1000324 /* Enable compression after we have responded with SUCCESS. */
325 enable_compression_after_reply = 1;
326 success = 1;
327 break;
328
329 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000330 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000331 break;
332
333 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000334 s->auth_proto = packet_get_string(&proto_len);
335 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000336
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000337 screen_flag = packet_get_protocol_flags() &
338 SSH_PROTOFLAG_SCREEN_NUMBER;
339 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
340
341 if (packet_remaining() == 4) {
342 if (!screen_flag)
343 debug2("Buggy client: "
344 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000345 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000346 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000347 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000348 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100349 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000350 success = session_setup_x11fwd(s);
351 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000352 free(s->auth_proto);
353 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000354 s->auth_proto = NULL;
355 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000356 }
Damien Millerb38eff82000-04-01 11:09:21 +1000357 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000358
359 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
Damien Miller4f755cd2008-05-19 14:57:41 +1000360 if (!options.allow_agent_forwarding ||
361 no_agent_forwarding_flag || compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000362 debug("Authentication agent forwarding not permitted for this authentication.");
363 break;
364 }
365 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000366 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000367 break;
368
369 case SSH_CMSG_PORT_FORWARD_REQUEST:
370 if (no_port_forwarding_flag) {
371 debug("Port forwarding not permitted for this authentication.");
372 break;
373 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100374 if (!(options.allow_tcp_forwarding & FORWARD_REMOTE)) {
Damien Miller50a41ed2000-10-16 12:14:42 +1100375 debug("Port forwarding not permitted.");
376 break;
377 }
Damien Millerb38eff82000-04-01 11:09:21 +1000378 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000379 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
Damien Miller7acefbb2014-07-18 14:11:24 +1000380 &options.fwd_opts) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000381 debug("Port forwarding failed.");
382 break;
383 }
Damien Millerb38eff82000-04-01 11:09:21 +1000384 success = 1;
385 break;
386
387 case SSH_CMSG_MAX_PACKET_SIZE:
388 if (packet_set_maxsize(packet_get_int()) > 0)
389 success = 1;
390 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100391
Damien Millerb38eff82000-04-01 11:09:21 +1000392 case SSH_CMSG_EXEC_SHELL:
393 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000394 if (type == SSH_CMSG_EXEC_CMD) {
395 command = packet_get_string(&dlen);
396 debug("Exec command '%.500s'", command);
Damien Miller7207f642008-05-19 15:34:50 +1000397 if (do_exec(s, command) != 0)
398 packet_disconnect(
399 "command execution failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000400 free(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000401 } else {
Damien Miller7207f642008-05-19 15:34:50 +1000402 if (do_exec(s, NULL) != 0)
403 packet_disconnect(
404 "shell execution failed");
Damien Millerb38eff82000-04-01 11:09:21 +1000405 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100406 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000407 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000408 return;
409
410 default:
411 /*
412 * Any unknown messages in this phase are ignored,
413 * and a failure message is returned.
414 */
Damien Miller996acd22003-04-09 20:59:48 +1000415 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000416 }
417 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
418 packet_send();
419 packet_write_wait();
420
421 /* Enable compression now that we have replied if appropriate. */
422 if (enable_compression_after_reply) {
423 enable_compression_after_reply = 0;
424 packet_start_compression(compression_level);
425 }
426 }
427}
428
Darren Tucker293ee3c2014-01-19 15:28:01 +1100429#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000430/*
431 * This is called to fork and execute a command when we have no tty. This
432 * will call do_child from the child, and server_loop from the parent after
433 * setting up file descriptors and such.
434 */
Damien Miller7207f642008-05-19 15:34:50 +1000435int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000436do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000437{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000438 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000439
440#ifdef USE_PIPES
441 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000442
Damien Miller22a29882010-05-10 11:53:54 +1000443 if (s == NULL)
444 fatal("do_exec_no_pty: no session");
445
Damien Millerb38eff82000-04-01 11:09:21 +1000446 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000447 if (pipe(pin) < 0) {
448 error("%s: pipe in: %.100s", __func__, strerror(errno));
449 return -1;
450 }
451 if (pipe(pout) < 0) {
452 error("%s: pipe out: %.100s", __func__, strerror(errno));
453 close(pin[0]);
454 close(pin[1]);
455 return -1;
456 }
Damien Miller8853ca52010-06-26 10:00:14 +1000457 if (pipe(perr) < 0) {
458 error("%s: pipe err: %.100s", __func__,
459 strerror(errno));
460 close(pin[0]);
461 close(pin[1]);
462 close(pout[0]);
463 close(pout[1]);
464 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000465 }
466#else
Damien Millerb38eff82000-04-01 11:09:21 +1000467 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000468
Damien Miller22a29882010-05-10 11:53:54 +1000469 if (s == NULL)
470 fatal("do_exec_no_pty: no session");
471
Damien Millerb38eff82000-04-01 11:09:21 +1000472 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000473 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
474 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
475 return -1;
476 }
Damien Miller8853ca52010-06-26 10:00:14 +1000477 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
478 error("%s: socketpair #2: %.100s", __func__,
479 strerror(errno));
480 close(inout[0]);
481 close(inout[1]);
482 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000483 }
484#endif
485
Damien Millere247cc42000-05-07 12:03:14 +1000486 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000487
Damien Millerb38eff82000-04-01 11:09:21 +1000488 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000489 switch ((pid = fork())) {
490 case -1:
491 error("%s: fork: %.100s", __func__, strerror(errno));
492#ifdef USE_PIPES
493 close(pin[0]);
494 close(pin[1]);
495 close(pout[0]);
496 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000497 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000498 close(perr[1]);
499#else
500 close(inout[0]);
501 close(inout[1]);
502 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000503 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000504#endif
505 return -1;
506 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000507 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000508
Damien Millerb38eff82000-04-01 11:09:21 +1000509 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000510 log_init(__progname, options.log_level,
511 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000512
513 /*
514 * Create a new session and process group since the 4.4BSD
515 * setlogin() affects the entire process group.
516 */
517 if (setsid() < 0)
518 error("setsid failed: %.100s", strerror(errno));
519
520#ifdef USE_PIPES
521 /*
522 * Redirect stdin. We close the parent side of the socket
523 * pair, and make the child side the standard input.
524 */
525 close(pin[1]);
526 if (dup2(pin[0], 0) < 0)
527 perror("dup2 stdin");
528 close(pin[0]);
529
530 /* Redirect stdout. */
531 close(pout[0]);
532 if (dup2(pout[1], 1) < 0)
533 perror("dup2 stdout");
534 close(pout[1]);
535
536 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000537 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000538 if (dup2(perr[1], 2) < 0)
539 perror("dup2 stderr");
540 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000541#else
Damien Millerb38eff82000-04-01 11:09:21 +1000542 /*
543 * Redirect stdin, stdout, and stderr. Stdin and stdout will
544 * use the same socket, as some programs (particularly rdist)
545 * seem to depend on it.
546 */
547 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000548 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000549 if (dup2(inout[0], 0) < 0) /* stdin */
550 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000551 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000552 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000553 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000554 if (dup2(err[0], 2) < 0) /* stderr */
555 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000556 close(err[0]);
557#endif
558
Damien Millerb38eff82000-04-01 11:09:21 +1000559
Tim Rice81ed5182002-09-25 17:38:46 -0700560#ifdef _UNICOS
561 cray_init_job(s->pw); /* set up cray jid and tmpdir */
562#endif
563
Damien Millerb38eff82000-04-01 11:09:21 +1000564 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000565 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000566 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000567 default:
568 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000569 }
Damien Miller7207f642008-05-19 15:34:50 +1000570
Tim Rice81ed5182002-09-25 17:38:46 -0700571#ifdef _UNICOS
572 signal(WJSIGNAL, cray_job_termination_handler);
573#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100574#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100575 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100576#endif
Damien Miller7207f642008-05-19 15:34:50 +1000577
Damien Millerb38eff82000-04-01 11:09:21 +1000578 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000579 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100580 packet_set_interactive(s->display != NULL,
581 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000582
583 /*
584 * Clear loginmsg, since it's the child's responsibility to display
585 * it to the user, otherwise multiple sessions may accumulate
586 * multiple copies of the login messages.
587 */
588 buffer_clear(&loginmsg);
589
Damien Millerb38eff82000-04-01 11:09:21 +1000590#ifdef USE_PIPES
591 /* We are the parent. Close the child sides of the pipes. */
592 close(pin[0]);
593 close(pout[1]);
594 close(perr[1]);
595
Damien Millerefb4afe2000-04-12 18:45:05 +1000596 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000597 session_set_fds(s, pin[1], pout[0], perr[0],
598 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000599 } else {
600 /* Enter the interactive session. */
601 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000602 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000603 }
Damien Miller7207f642008-05-19 15:34:50 +1000604#else
Damien Millerb38eff82000-04-01 11:09:21 +1000605 /* We are the parent. Close the child sides of the socket pairs. */
606 close(inout[0]);
607 close(err[0]);
608
609 /*
610 * Enter the interactive session. Note: server_loop must be able to
611 * handle the case that fdin and fdout are the same.
612 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000613 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000614 session_set_fds(s, inout[1], inout[1], err[1],
615 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000616 } else {
617 server_loop(pid, inout[1], inout[1], err[1]);
618 /* server_loop has closed inout[1] and err[1]. */
619 }
Damien Miller7207f642008-05-19 15:34:50 +1000620#endif
621 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000622}
623
624/*
625 * This is called to fork and execute a command when we have a tty. This
626 * will call do_child from the child, and server_loop from the parent after
627 * setting up file descriptors, controlling tty, updating wtmp, utmp,
628 * lastlog, and other such operations.
629 */
Damien Miller7207f642008-05-19 15:34:50 +1000630int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000631do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000632{
Damien Millerb38eff82000-04-01 11:09:21 +1000633 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000634 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000635
636 if (s == NULL)
637 fatal("do_exec_pty: no session");
638 ptyfd = s->ptyfd;
639 ttyfd = s->ttyfd;
640
Damien Miller7207f642008-05-19 15:34:50 +1000641 /*
642 * Create another descriptor of the pty master side for use as the
643 * standard input. We could use the original descriptor, but this
644 * simplifies code in server_loop. The descriptor is bidirectional.
645 * Do this before forking (and cleanup in the child) so as to
646 * detect and gracefully fail out-of-fd conditions.
647 */
648 if ((fdout = dup(ptyfd)) < 0) {
649 error("%s: dup #1: %s", __func__, strerror(errno));
650 close(ttyfd);
651 close(ptyfd);
652 return -1;
653 }
654 /* we keep a reference to the pty master */
655 if ((ptymaster = dup(ptyfd)) < 0) {
656 error("%s: dup #2: %s", __func__, strerror(errno));
657 close(ttyfd);
658 close(ptyfd);
659 close(fdout);
660 return -1;
661 }
662
Damien Millerb38eff82000-04-01 11:09:21 +1000663 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000664 switch ((pid = fork())) {
665 case -1:
666 error("%s: fork: %.100s", __func__, strerror(errno));
667 close(fdout);
668 close(ptymaster);
669 close(ttyfd);
670 close(ptyfd);
671 return -1;
672 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000673 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000674
Damien Miller7207f642008-05-19 15:34:50 +1000675 close(fdout);
676 close(ptymaster);
677
Damien Miller942da032000-08-18 13:59:06 +1000678 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000679 log_init(__progname, options.log_level,
680 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000681 /* Close the master side of the pseudo tty. */
682 close(ptyfd);
683
684 /* Make the pseudo tty our controlling tty. */
685 pty_make_controlling_tty(&ttyfd, s->tty);
686
Damien Miller9c751422001-10-10 15:02:46 +1000687 /* Redirect stdin/stdout/stderr from the pseudo tty. */
688 if (dup2(ttyfd, 0) < 0)
689 error("dup2 stdin: %s", strerror(errno));
690 if (dup2(ttyfd, 1) < 0)
691 error("dup2 stdout: %s", strerror(errno));
692 if (dup2(ttyfd, 2) < 0)
693 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000694
695 /* Close the extra descriptor for the pseudo tty. */
696 close(ttyfd);
697
Damien Miller942da032000-08-18 13:59:06 +1000698 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000699#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700700 if (!(options.use_login && command == NULL)) {
701#ifdef _UNICOS
702 cray_init_job(s->pw); /* set up cray jid and tmpdir */
703#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100704 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700705 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000706# ifdef LOGIN_NEEDS_UTMPX
707 else
708 do_pre_login(s);
709# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000710#endif
Damien Miller7207f642008-05-19 15:34:50 +1000711 /*
712 * Do common processing for the child, such as execing
713 * the command.
714 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000715 do_child(s, command);
716 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000717 default:
718 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000719 }
Damien Miller7207f642008-05-19 15:34:50 +1000720
Tim Rice81ed5182002-09-25 17:38:46 -0700721#ifdef _UNICOS
722 signal(WJSIGNAL, cray_job_termination_handler);
723#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100724#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100725 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100726#endif
Damien Miller7207f642008-05-19 15:34:50 +1000727
Damien Millerb38eff82000-04-01 11:09:21 +1000728 s->pid = pid;
729
730 /* Parent. Close the slave side of the pseudo tty. */
731 close(ttyfd);
732
Damien Millerb38eff82000-04-01 11:09:21 +1000733 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000734 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100735 packet_set_interactive(1,
736 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Millerefb4afe2000-04-12 18:45:05 +1000737 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000738 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000739 } else {
740 server_loop(pid, ptyfd, fdout, -1);
741 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000742 }
Damien Miller7207f642008-05-19 15:34:50 +1000743 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000744}
745
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000746#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000747static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000748do_pre_login(Session *s)
749{
750 socklen_t fromlen;
751 struct sockaddr_storage from;
752 pid_t pid = getpid();
753
754 /*
755 * Get IP address of client. If the connection is not a socket, let
756 * the address be 0.0.0.0.
757 */
758 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000759 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000760 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000761 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100762 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000763 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000764 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000765 }
766 }
767
768 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000769 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000770 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000771}
772#endif
773
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000774/*
775 * This is called to fork and execute a command. If another command is
776 * to be forced, execute that instead.
777 */
Damien Miller7207f642008-05-19 15:34:50 +1000778int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000779do_exec(Session *s, const char *command)
780{
djm@openbsd.org95767262016-03-07 19:02:43 +0000781 struct ssh *ssh = active_state; /* XXX */
Damien Miller7207f642008-05-19 15:34:50 +1000782 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000783 const char *forced = NULL, *tty = NULL;
784 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000785
Damien Millere2754432006-07-24 14:06:47 +1000786 if (options.adm_forced_command) {
787 original_command = command;
788 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100789 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000790 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000791 original_command = command;
792 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100793 forced = "(key-option)";
794 }
795 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100796 if (IS_INTERNAL_SFTP(command)) {
797 s->is_subsystem = s->is_subsystem ?
798 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
799 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100800 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100801 snprintf(session_type, sizeof(session_type),
802 "forced-command %s '%.900s'", forced, command);
803 } else if (s->is_subsystem) {
804 snprintf(session_type, sizeof(session_type),
805 "subsystem '%.900s'", s->subsys);
806 } else if (command == NULL) {
807 snprintf(session_type, sizeof(session_type), "shell");
808 } else {
809 /* NB. we don't log unforced commands to preserve privacy */
810 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000811 }
812
Damien Miller71df7522013-10-15 12:12:02 +1100813 if (s->ttyfd != -1) {
814 tty = s->tty;
815 if (strncmp(tty, "/dev/", 5) == 0)
816 tty += 5;
817 }
818
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000819 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100820 session_type,
821 tty == NULL ? "" : " on ",
822 tty == NULL ? "" : tty,
823 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000824 ssh_remote_ipaddr(ssh),
825 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000826 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100827
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100828#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100829 if (command != NULL)
830 PRIVSEP(audit_run_command(command));
831 else if (s->ttyfd == -1) {
832 char *shell = s->pw->pw_shell;
833
834 if (shell[0] == '\0') /* empty shell means /bin/sh */
835 shell =_PATH_BSHELL;
836 PRIVSEP(audit_run_command(shell));
837 }
838#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000839 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000840 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000841 else
Damien Miller7207f642008-05-19 15:34:50 +1000842 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000843
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000844 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000845
Darren Tucker09991742004-07-17 17:05:14 +1000846 /*
847 * Clear loginmsg: it's the child's responsibility to display
848 * it to the user, otherwise multiple sessions may accumulate
849 * multiple copies of the login messages.
850 */
851 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000852
853 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000854}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000855
Damien Miller942da032000-08-18 13:59:06 +1000856/* administrative, login(1)-like work */
857void
Damien Miller69b69aa2000-10-28 14:19:58 +1100858do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000859{
djm@openbsd.org95767262016-03-07 19:02:43 +0000860 struct ssh *ssh = active_state; /* XXX */
Damien Miller942da032000-08-18 13:59:06 +1000861 socklen_t fromlen;
862 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000863 struct passwd * pw = s->pw;
864 pid_t pid = getpid();
865
866 /*
867 * Get IP address of client. If the connection is not a socket, let
868 * the address be 0.0.0.0.
869 */
870 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000871 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000872 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000873 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000874 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000875 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000876 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000877 }
878 }
879
880 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000881 if (!use_privsep)
882 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000883 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000884 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000885 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000886
Kevin Steves092f2ef2000-10-14 13:36:13 +0000887#ifdef USE_PAM
888 /*
889 * If password change is needed, do it now.
890 * This needs to occur before the ~/.hushlogin check.
891 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100892 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
893 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000894 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100895 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000896 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000897 }
898#endif
899
Damien Millercf205e82001-04-16 18:29:15 +1000900 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000901 return;
902
Darren Tucker1921ed92004-02-10 13:23:28 +1100903 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000904
Damien Millercf205e82001-04-16 18:29:15 +1000905 do_motd();
906}
907
908/*
909 * Display the message of the day.
910 */
911void
912do_motd(void)
913{
914 FILE *f;
915 char buf[256];
916
Damien Miller942da032000-08-18 13:59:06 +1000917 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000918#ifdef HAVE_LOGIN_CAP
919 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
920 "/etc/motd"), "r");
921#else
Damien Miller942da032000-08-18 13:59:06 +1000922 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000923#endif
Damien Miller942da032000-08-18 13:59:06 +1000924 if (f) {
925 while (fgets(buf, sizeof(buf), f))
926 fputs(buf, stdout);
927 fclose(f);
928 }
929 }
930}
931
Kevin Steves8f63caa2001-07-04 18:23:02 +0000932
933/*
934 * Check for quiet login, either .hushlogin or command given.
935 */
936int
937check_quietlogin(Session *s, const char *command)
938{
939 char buf[256];
940 struct passwd *pw = s->pw;
941 struct stat st;
942
943 /* Return 1 if .hushlogin exists or a command given. */
944 if (command != NULL)
945 return 1;
946 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
947#ifdef HAVE_LOGIN_CAP
948 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
949 return 1;
950#else
951 if (stat(buf, &st) >= 0)
952 return 1;
953#endif
954 return 0;
955}
956
Damien Millerb38eff82000-04-01 11:09:21 +1000957/*
958 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100959 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000960 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000961void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000962child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100963 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000964{
Damien Millerb38eff82000-04-01 11:09:21 +1000965 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000966 u_int envsize;
967 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000968
Damien Miller8569eba2014-03-04 09:35:17 +1100969 if (strchr(name, '=') != NULL) {
970 error("Invalid environment variable \"%.100s\"", name);
971 return;
972 }
973
Damien Millerb38eff82000-04-01 11:09:21 +1000974 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000975 * If we're passed an uninitialized list, allocate a single null
976 * entry before continuing.
977 */
978 if (*envp == NULL && *envsizep == 0) {
979 *envp = xmalloc(sizeof(char *));
980 *envp[0] = NULL;
981 *envsizep = 1;
982 }
983
984 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000985 * Find the slot where the value should be stored. If the variable
986 * already exists, we reuse the slot; otherwise we append a new slot
987 * at the end of the array, expanding if necessary.
988 */
989 env = *envp;
990 namelen = strlen(name);
991 for (i = 0; env[i]; i++)
992 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
993 break;
994 if (env[i]) {
995 /* Reuse the slot. */
Darren Tuckera627d422013-06-02 07:31:17 +1000996 free(env[i]);
Damien Millerb38eff82000-04-01 11:09:21 +1000997 } else {
998 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000999 envsize = *envsizep;
1000 if (i >= envsize - 1) {
1001 if (envsize >= 1000)
1002 fatal("child_set_env: too many env vars");
1003 envsize += 50;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001004 env = (*envp) = xreallocarray(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +10001005 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +10001006 }
1007 /* Need to set the NULL pointer at end of array beyond the new slot. */
1008 env[i + 1] = NULL;
1009 }
1010
1011 /* Allocate space and format the variable in the appropriate slot. */
1012 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
1013 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
1014}
1015
1016/*
1017 * Reads environment variables from the given file and adds/overrides them
1018 * into the environment. If the file does not exist, this does nothing.
1019 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1020 * and assignments of the form name=value. No other forms are allowed.
1021 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001022static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001023read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001024 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +10001025{
1026 FILE *f;
1027 char buf[4096];
1028 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +10001029 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001030
1031 f = fopen(filename, "r");
1032 if (!f)
1033 return;
1034
1035 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +10001036 if (++lineno > 1000)
1037 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001038 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1039 ;
1040 if (!*cp || *cp == '#' || *cp == '\n')
1041 continue;
Damien Miller14b017d2007-09-17 16:09:15 +10001042
1043 cp[strcspn(cp, "\n")] = '\0';
1044
Damien Millerb38eff82000-04-01 11:09:21 +10001045 value = strchr(cp, '=');
1046 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +10001047 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1048 filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001049 continue;
1050 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001051 /*
1052 * Replace the equals sign by nul, and advance value to
1053 * the value string.
1054 */
Damien Millerb38eff82000-04-01 11:09:21 +10001055 *value = '\0';
1056 value++;
1057 child_set_env(env, envsize, cp, value);
1058 }
1059 fclose(f);
1060}
1061
Darren Tuckere1a790d2003-09-16 11:52:19 +10001062#ifdef HAVE_ETC_DEFAULT_LOGIN
1063/*
1064 * Return named variable from specified environment, or NULL if not present.
1065 */
1066static char *
1067child_get_env(char **env, const char *name)
1068{
1069 int i;
1070 size_t len;
1071
1072 len = strlen(name);
1073 for (i=0; env[i] != NULL; i++)
1074 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1075 return(env[i] + len + 1);
1076 return NULL;
1077}
1078
1079/*
1080 * Read /etc/default/login.
1081 * We pick up the PATH (or SUPATH for root) and UMASK.
1082 */
1083static void
1084read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1085{
1086 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001087 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +10001088 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +10001089
1090 /*
1091 * We don't want to copy the whole file to the child's environment,
1092 * so we use a temporary environment and copy the variables we're
1093 * interested in.
1094 */
1095 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1096
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001097 if (tmpenv == NULL)
1098 return;
1099
Darren Tuckere1a790d2003-09-16 11:52:19 +10001100 if (uid == 0)
1101 var = child_get_env(tmpenv, "SUPATH");
1102 else
1103 var = child_get_env(tmpenv, "PATH");
1104 if (var != NULL)
1105 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +11001106
Darren Tuckere1a790d2003-09-16 11:52:19 +10001107 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1108 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +10001109 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +11001110
Darren Tuckere1a790d2003-09-16 11:52:19 +10001111 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001112 free(tmpenv[i]);
1113 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +10001114}
1115#endif /* HAVE_ETC_DEFAULT_LOGIN */
1116
Damien Miller7dff8692005-05-26 11:34:51 +10001117void
1118copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +10001119{
Damien Millerbb9ffc12002-01-08 10:59:32 +11001120 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +10001121 int i;
1122
Damien Millerbb9ffc12002-01-08 10:59:32 +11001123 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001124 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001125
Damien Millerbb9ffc12002-01-08 10:59:32 +11001126 for(i = 0; source[i] != NULL; i++) {
1127 var_name = xstrdup(source[i]);
1128 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001129 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001130 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001131 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001132 *var_val++ = '\0';
1133
1134 debug3("Copy environment: %s=%s", var_name, var_val);
1135 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001136
Darren Tuckerf60845f2013-06-02 08:07:31 +10001137 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001138 }
1139}
Damien Millercb5e44a2000-09-29 12:12:36 +11001140
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001141static char **
1142do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001143{
djm@openbsd.org95767262016-03-07 19:02:43 +00001144 struct ssh *ssh = active_state; /* XXX */
Damien Millerb38eff82000-04-01 11:09:21 +10001145 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001146 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001147 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001148 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001149#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001150 char *path = NULL;
1151#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001152
Damien Millerb38eff82000-04-01 11:09:21 +10001153 /* Initialize the environment. */
1154 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001155 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001156 env[0] = NULL;
1157
Damien Millerbac2d8a2000-09-05 16:13:06 +11001158#ifdef HAVE_CYGWIN
1159 /*
1160 * The Windows environment contains some setting which are
1161 * important for a running system. They must not be dropped.
1162 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001163 {
1164 char **p;
1165
1166 p = fetch_windows_environment();
1167 copy_environment(p, &env, &envsize);
1168 free_windows_environment(p);
1169 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001170#endif
1171
Darren Tucker0efd1552003-08-26 11:49:55 +10001172#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001173 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001174 * the childs environment as they see fit
1175 */
1176 ssh_gssapi_do_child(&env, &envsize);
1177#endif
1178
Damien Millerb38eff82000-04-01 11:09:21 +10001179 if (!options.use_login) {
1180 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001181 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001182 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001183 s->env[i].val);
1184
Damien Millerb38eff82000-04-01 11:09:21 +10001185 child_set_env(&env, &envsize, "USER", pw->pw_name);
1186 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001187#ifdef _AIX
1188 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1189#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001190 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001191#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001192 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1193 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1194 else
1195 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001196#else /* HAVE_LOGIN_CAP */
1197# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001198 /*
1199 * There's no standard path on Windows. The path contains
1200 * important components pointing to the system directories,
1201 * needed for loading shared libraries. So the path better
1202 * remains intact here.
1203 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001204# ifdef HAVE_ETC_DEFAULT_LOGIN
1205 read_etc_default_login(&env, &envsize, pw->pw_uid);
1206 path = child_get_env(env, "PATH");
1207# endif /* HAVE_ETC_DEFAULT_LOGIN */
1208 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001209 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001210 s->pw->pw_uid == 0 ?
1211 SUPERUSER_PATH : _PATH_STDPATH);
1212 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001213# endif /* HAVE_CYGWIN */
1214#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001215
1216 snprintf(buf, sizeof buf, "%.200s/%.50s",
1217 _PATH_MAILDIR, pw->pw_name);
1218 child_set_env(&env, &envsize, "MAIL", buf);
1219
1220 /* Normal systems set SHELL by default. */
1221 child_set_env(&env, &envsize, "SHELL", shell);
1222 }
1223 if (getenv("TZ"))
1224 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1225
1226 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001227 if (!options.use_login) {
1228 while (custom_environment) {
1229 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001230 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001231
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001232 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001233 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001234 if (str[i] == '=') {
1235 str[i] = 0;
1236 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001237 }
1238 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +10001239 free(ce->s);
1240 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001241 }
Damien Millerb38eff82000-04-01 11:09:21 +10001242 }
1243
Damien Millerf37e2462002-09-19 11:47:55 +10001244 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001245 snprintf(buf, sizeof buf, "%.50s %d %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001246 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1247 ssh_local_port(ssh));
Damien Millerb38eff82000-04-01 11:09:21 +10001248 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1249
Damien Miller00111382003-03-10 11:21:17 +11001250 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001251 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001252 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1253 laddr, ssh_local_port(ssh));
Darren Tuckera627d422013-06-02 07:31:17 +10001254 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001255 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1256
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001257 if (s->ttyfd != -1)
1258 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1259 if (s->term)
1260 child_set_env(&env, &envsize, "TERM", s->term);
1261 if (s->display)
1262 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001263 if (original_command)
1264 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1265 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001266
Tim Rice81ed5182002-09-25 17:38:46 -07001267#ifdef _UNICOS
1268 if (cray_tmpdir[0] != '\0')
1269 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1270#endif /* _UNICOS */
1271
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001272 /*
1273 * Since we clear KRB5CCNAME at startup, if it's set now then it
1274 * must have been set by a native authentication method (eg AIX or
1275 * SIA), so copy it to the child.
1276 */
1277 {
1278 char *cp;
1279
1280 if ((cp = getenv("KRB5CCNAME")) != NULL)
1281 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1282 }
1283
Damien Millerb38eff82000-04-01 11:09:21 +10001284#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001285 {
1286 char *cp;
1287
1288 if ((cp = getenv("AUTHSTATE")) != NULL)
1289 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001290 read_environment_file(&env, &envsize, "/etc/environment");
1291 }
Damien Millerb38eff82000-04-01 11:09:21 +10001292#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001293#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001294 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001295 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001296 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001297#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001298#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001299 /*
1300 * Pull in any environment variables that may have
1301 * been set by PAM.
1302 */
Damien Miller4e448a32003-05-14 15:11:48 +10001303 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001304 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001305
Damien Millerc756e9b2003-11-17 21:41:42 +11001306 p = fetch_pam_child_environment();
1307 copy_environment(p, &env, &envsize);
1308 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001309
Damien Millerc756e9b2003-11-17 21:41:42 +11001310 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001311 copy_environment(p, &env, &envsize);
1312 free_pam_environment(p);
1313 }
Damien Millerb38eff82000-04-01 11:09:21 +10001314#endif /* USE_PAM */
1315
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001316 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001317 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001318 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001319
1320 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001321 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001322 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001323 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001324 read_environment_file(&env, &envsize, buf);
1325 }
1326 if (debug_flag) {
1327 /* dump the environment */
1328 fprintf(stderr, "Environment:\n");
1329 for (i = 0; env[i]; i++)
1330 fprintf(stderr, " %.200s\n", env[i]);
1331 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001332 return env;
1333}
1334
1335/*
1336 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1337 * first in this order).
1338 */
1339static void
1340do_rc_files(Session *s, const char *shell)
1341{
1342 FILE *f = NULL;
1343 char cmd[1024];
1344 int do_xauth;
1345 struct stat st;
1346
1347 do_xauth =
1348 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1349
Damien Millera1b48cc2008-03-27 11:02:02 +11001350 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001351 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001352 !no_user_rc && options.permit_user_rc &&
1353 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001354 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1355 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1356 if (debug_flag)
1357 fprintf(stderr, "Running %s\n", cmd);
1358 f = popen(cmd, "w");
1359 if (f) {
1360 if (do_xauth)
1361 fprintf(f, "%s %s\n", s->auth_proto,
1362 s->auth_data);
1363 pclose(f);
1364 } else
1365 fprintf(stderr, "Could not run %s\n",
1366 _PATH_SSH_USER_RC);
1367 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1368 if (debug_flag)
1369 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1370 _PATH_SSH_SYSTEM_RC);
1371 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1372 if (f) {
1373 if (do_xauth)
1374 fprintf(f, "%s %s\n", s->auth_proto,
1375 s->auth_data);
1376 pclose(f);
1377 } else
1378 fprintf(stderr, "Could not run %s\n",
1379 _PATH_SSH_SYSTEM_RC);
1380 } else if (do_xauth && options.xauth_location != NULL) {
1381 /* Add authority data to .Xauthority if appropriate. */
1382 if (debug_flag) {
1383 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001384 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001385 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001386 fprintf(stderr,
1387 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001388 options.xauth_location, s->auth_display,
1389 s->auth_proto, s->auth_data);
1390 }
1391 snprintf(cmd, sizeof cmd, "%s -q -",
1392 options.xauth_location);
1393 f = popen(cmd, "w");
1394 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001395 fprintf(f, "remove %s\n",
1396 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001397 fprintf(f, "add %s %s %s\n",
1398 s->auth_display, s->auth_proto,
1399 s->auth_data);
1400 pclose(f);
1401 } else {
1402 fprintf(stderr, "Could not run %s\n",
1403 cmd);
1404 }
1405 }
1406}
1407
1408static void
1409do_nologin(struct passwd *pw)
1410{
1411 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001412 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1413 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001414
1415#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001416 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001417 return;
1418 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001419#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001420 if (pw->pw_uid == 0)
1421 return;
1422 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001423#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001424 if (stat(nl, &sb) == -1) {
1425 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001426 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001427 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001428 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001429
1430 /* /etc/nologin exists. Print its contents if we can and exit. */
1431 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1432 if ((f = fopen(nl, "r")) != NULL) {
1433 while (fgets(buf, sizeof(buf), f))
1434 fputs(buf, stderr);
1435 fclose(f);
1436 }
1437 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001438}
1439
Damien Millerd8cb1f12008-02-10 22:40:12 +11001440/*
1441 * Chroot into a directory after checking it for safety: all path components
1442 * must be root-owned directories with strict permissions.
1443 */
1444static void
1445safely_chroot(const char *path, uid_t uid)
1446{
1447 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001448 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001449 struct stat st;
1450
1451 if (*path != '/')
1452 fatal("chroot path does not begin at root");
1453 if (strlen(path) >= sizeof(component))
1454 fatal("chroot path too long");
1455
1456 /*
1457 * Descend the path, checking that each component is a
1458 * root-owned directory with strict permissions.
1459 */
1460 for (cp = path; cp != NULL;) {
1461 if ((cp = strchr(cp, '/')) == NULL)
1462 strlcpy(component, path, sizeof(component));
1463 else {
1464 cp++;
1465 memcpy(component, path, cp - path);
1466 component[cp - path] = '\0';
1467 }
1468
1469 debug3("%s: checking '%s'", __func__, component);
1470
1471 if (stat(component, &st) != 0)
1472 fatal("%s: stat(\"%s\"): %s", __func__,
1473 component, strerror(errno));
1474 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1475 fatal("bad ownership or modes for chroot "
1476 "directory %s\"%s\"",
1477 cp == NULL ? "" : "component ", component);
1478 if (!S_ISDIR(st.st_mode))
1479 fatal("chroot path %s\"%s\" is not a directory",
1480 cp == NULL ? "" : "component ", component);
1481
1482 }
1483
1484 if (chdir(path) == -1)
1485 fatal("Unable to chdir to chroot path \"%s\": "
1486 "%s", path, strerror(errno));
1487 if (chroot(path) == -1)
1488 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1489 if (chdir("/") == -1)
1490 fatal("%s: chdir(/) after chroot: %s",
1491 __func__, strerror(errno));
1492 verbose("Changed root directory to \"%s\"", path);
1493}
1494
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001495/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001497do_setusercontext(struct passwd *pw)
1498{
Damien Miller54e37732008-02-10 22:48:55 +11001499 char *chroot_path, *tmp;
1500
Darren Tucker97528352010-11-05 12:03:05 +11001501 platform_setusercontext(pw);
1502
Darren Tuckerb12fe272010-11-05 14:47:01 +11001503 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001504#ifdef HAVE_LOGIN_CAP
1505 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001506 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001507 perror("unable to set user context");
1508 exit(1);
1509 }
1510#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001511 if (setlogin(pw->pw_name) < 0)
1512 error("setlogin failed: %s", strerror(errno));
1513 if (setgid(pw->pw_gid) < 0) {
1514 perror("setgid");
1515 exit(1);
1516 }
1517 /* Initialize the group list. */
1518 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1519 perror("initgroups");
1520 exit(1);
1521 }
1522 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001523#endif
1524
Darren Tucker920612e2010-11-05 12:36:15 +11001525 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001526
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001527 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001528 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001529 tmp = tilde_expand_filename(options.chroot_directory,
1530 pw->pw_uid);
1531 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1532 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001533 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001534 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001535 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001536 /* Make sure we don't attempt to chroot again */
1537 free(options.chroot_directory);
1538 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001539 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001540 }
1541
1542#ifdef HAVE_LOGIN_CAP
1543 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1544 perror("unable to set user context (setuser)");
1545 exit(1);
1546 }
Damien Miller58528402013-03-15 11:22:37 +11001547 /*
1548 * FreeBSD's setusercontext() will not apply the user's
1549 * own umask setting unless running with the user's UID.
1550 */
1551 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001552#else
Tim Rice9464ba62014-01-20 17:59:28 -08001553# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001554 /*
1555 * In a chroot environment, the set_id() will always fail;
1556 * typically because of the lack of necessary authentication
1557 * services and runtime such as ./usr/lib/libiaf.so,
1558 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1559 * internal sftp chroot case. We'll lose auditing and ACLs but
1560 * permanently_set_uid will take care of the rest.
1561 */
1562 if (!in_chroot && set_id(pw->pw_name) != 0)
1563 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001564# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001565 /* Permanently switch to the desired uid. */
1566 permanently_set_uid(pw);
1567#endif
Damien Millera56086b2013-04-23 15:24:18 +10001568 } else if (options.chroot_directory != NULL &&
1569 strcasecmp(options.chroot_directory, "none") != 0) {
1570 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001571 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001572
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001573 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1574 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1575}
1576
Ben Lindstrom08105192002-03-22 02:50:06 +00001577static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001578do_pwchange(Session *s)
1579{
Darren Tucker09991742004-07-17 17:05:14 +10001580 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001581 fprintf(stderr, "WARNING: Your password has expired.\n");
1582 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001583 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001584 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001585#ifdef WITH_SELINUX
1586 setexeccon(NULL);
1587#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001588#ifdef PASSWD_NEEDS_USERNAME
1589 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1590 (char *)NULL);
1591#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001592 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001593#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001594 perror("passwd");
1595 } else {
1596 fprintf(stderr,
1597 "Password change required but no TTY available.\n");
1598 }
1599 exit(1);
1600}
1601
1602static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603launch_login(struct passwd *pw, const char *hostname)
1604{
1605 /* Launch login(1). */
1606
Ben Lindstrom378a4172002-06-07 14:49:56 +00001607 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001608#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001609 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001610#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001611#ifdef LOGIN_NO_ENDOPT
1612 "-p", "-f", pw->pw_name, (char *)NULL);
1613#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001615#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001616
1617 /* Login couldn't be executed, die. */
1618
1619 perror("login");
1620 exit(1);
1621}
1622
Darren Tucker23bc8d02004-02-06 16:24:31 +11001623static void
1624child_close_fds(void)
1625{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001626 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001627
djm@openbsd.org141efe42015-01-14 20:05:27 +00001628 if (auth_sock != -1) {
1629 close(auth_sock);
1630 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001631 }
1632
Darren Tucker23bc8d02004-02-06 16:24:31 +11001633 if (packet_get_connection_in() == packet_get_connection_out())
1634 close(packet_get_connection_in());
1635 else {
1636 close(packet_get_connection_in());
1637 close(packet_get_connection_out());
1638 }
1639 /*
1640 * Close all descriptors related to channels. They will still remain
1641 * open in the parent.
1642 */
1643 /* XXX better use close-on-exec? -markus */
1644 channel_close_all();
1645
1646 /*
1647 * Close any extra file descriptors. Note that there may still be
1648 * descriptors left by system functions. They will be closed later.
1649 */
1650 endpwent();
1651
1652 /*
Damien Miller788f2122005-11-05 15:14:59 +11001653 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001654 * hanging around in clients. Note that we want to do this after
1655 * initgroups, because at least on Solaris 2.3 it leaves file
1656 * descriptors open.
1657 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001658 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001659}
1660
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001661/*
1662 * Performs common processing for the child, such as setting up the
1663 * environment, closing extra file descriptors, setting the user and group
1664 * ids, and executing the command or shell.
1665 */
Damien Millerdfc24252008-02-10 22:29:40 +11001666#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001667void
1668do_child(Session *s, const char *command)
1669{
djm@openbsd.org95767262016-03-07 19:02:43 +00001670 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001671 extern char **environ;
1672 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001673 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001674 const char *shell, *shell0, *hostname = NULL;
1675 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001676 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001677
1678 /* remove hostkey from the child's memory */
1679 destroy_sensitive_data();
1680
Darren Tucker23bc8d02004-02-06 16:24:31 +11001681 /* Force a password change */
1682 if (s->authctxt->force_pwchange) {
1683 do_setusercontext(pw);
1684 child_close_fds();
1685 do_pwchange(s);
1686 exit(1);
1687 }
1688
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001689 /* login(1) is only called if we execute the login shell */
1690 if (options.use_login && command != NULL)
1691 options.use_login = 0;
1692
Tim Rice81ed5182002-09-25 17:38:46 -07001693#ifdef _UNICOS
1694 cray_setup(pw->pw_uid, pw->pw_name, command);
1695#endif /* _UNICOS */
1696
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001697 /*
1698 * Login(1) does this as well, and it needs uid 0 for the "-h"
1699 * switch, so we let login(1) to this for us.
1700 */
1701 if (!options.use_login) {
1702#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001703 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001704 if (!check_quietlogin(s, command))
1705 do_motd();
1706#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001707 /* When PAM is enabled we rely on it to do the nologin check */
1708 if (!options.use_pam)
1709 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001710 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001711 /*
1712 * PAM session modules in do_setusercontext may have
1713 * generated messages, so if this in an interactive
1714 * login then display them too.
1715 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001716 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001717 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001718#endif /* HAVE_OSF_SIA */
1719 }
1720
Darren Tucker69687f42004-09-11 22:17:26 +10001721#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001722 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1723 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001724 display_loginmsg();
1725 exit(254);
1726 }
1727#endif
1728
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001729 /*
1730 * Get the shell from the password data. An empty shell field is
1731 * legal, and means /bin/sh.
1732 */
1733 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001734
1735 /*
1736 * Make sure $SHELL points to the shell from the password file,
1737 * even if shell is overridden from login.conf
1738 */
1739 env = do_setup_env(s, shell);
1740
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001741#ifdef HAVE_LOGIN_CAP
1742 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1743#endif
1744
Damien Millerad833b32000-08-23 10:46:23 +10001745 /* we have to stash the hostname before we close our socket. */
1746 if (options.use_login)
djm@openbsd.org95767262016-03-07 19:02:43 +00001747 hostname = session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001748 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001749 /*
1750 * Close the connection descriptors; note that this is the child, and
1751 * the server will still have the socket open, and it is important
1752 * that we do not shutdown it. Note that the descriptors cannot be
1753 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001754 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001755 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001756 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001757
Damien Millerb38eff82000-04-01 11:09:21 +10001758 /*
Damien Miller05eda432002-02-10 18:32:28 +11001759 * Must take new environment into use so that .ssh/rc,
1760 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001761 */
1762 environ = env;
1763
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001764#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001765 /*
1766 * At this point, we check to see if AFS is active and if we have
1767 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1768 * if we can (and need to) extend the ticket into an AFS token. If
1769 * we don't do this, we run into potential problems if the user's
1770 * home directory is in AFS and it's not world-readable.
1771 */
1772
1773 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001774 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001775 char cell[64];
1776
1777 debug("Getting AFS token");
1778
1779 k_setpag();
1780
1781 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1782 krb5_afslog(s->authctxt->krb5_ctx,
1783 s->authctxt->krb5_fwd_ccache, cell, NULL);
1784
1785 krb5_afslog_home(s->authctxt->krb5_ctx,
1786 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1787 }
1788#endif
1789
Damien Miller788f2122005-11-05 15:14:59 +11001790 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001791 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001792 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001793#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001794 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001795#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001796 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001797 fprintf(stderr, "Could not chdir to home "
1798 "directory %s: %s\n", pw->pw_dir,
1799 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001800 }
Damien Miller6051c942008-06-16 07:53:16 +10001801 if (r)
1802 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001803 }
1804
Damien Millera1939002008-03-15 17:27:58 +11001805 closefrom(STDERR_FILENO + 1);
1806
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001807 if (!options.use_login)
1808 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001809
1810 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001811 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001812
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001813 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1814 printf("This service allows sftp connections only.\n");
1815 fflush(NULL);
1816 exit(1);
1817 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001818 extern int optind, optreset;
1819 int i;
1820 char *p, *args;
1821
Darren Tuckerac46a912009-06-21 17:55:23 +10001822 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001823 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001824 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1825 if (i < ARGV_MAX - 1)
1826 argv[i++] = p;
1827 argv[i] = NULL;
1828 optind = optreset = 1;
1829 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001830#ifdef WITH_SELINUX
1831 ssh_selinux_change_context("sftpd_t");
1832#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001833 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001834 }
1835
Darren Tucker695ed392009-10-07 09:02:18 +11001836 fflush(NULL);
1837
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001838 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001839 launch_login(pw, hostname);
1840 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001841 }
1842
1843 /* Get the last component of the shell name. */
1844 if ((shell0 = strrchr(shell, '/')) != NULL)
1845 shell0++;
1846 else
1847 shell0 = shell;
1848
Damien Millerb38eff82000-04-01 11:09:21 +10001849 /*
1850 * If we have no command, execute the shell. In this case, the shell
1851 * name to be passed in argv[0] is preceded by '-' to indicate that
1852 * this is a login shell.
1853 */
1854 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001855 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001856
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001857 /* Start the shell. Set initial character to '-'. */
1858 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001859
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001860 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1861 >= sizeof(argv0) - 1) {
1862 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001863 perror(shell);
1864 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001865 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001866
1867 /* Execute the shell. */
1868 argv[0] = argv0;
1869 argv[1] = NULL;
1870 execve(shell, argv, env);
1871
1872 /* Executing the shell failed. */
1873 perror(shell);
1874 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001875 }
1876 /*
1877 * Execute the command using the user's shell. This uses the -c
1878 * option to execute the command.
1879 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001880 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001881 argv[1] = "-c";
1882 argv[2] = (char *) command;
1883 argv[3] = NULL;
1884 execve(shell, argv, env);
1885 perror(shell);
1886 exit(1);
1887}
1888
Damien Miller7207f642008-05-19 15:34:50 +10001889void
1890session_unused(int id)
1891{
1892 debug3("%s: session id %d unused", __func__, id);
1893 if (id >= options.max_sessions ||
1894 id >= sessions_nalloc) {
1895 fatal("%s: insane session id %d (max %d nalloc %d)",
1896 __func__, id, options.max_sessions, sessions_nalloc);
1897 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001898 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001899 sessions[id].self = id;
1900 sessions[id].used = 0;
1901 sessions[id].chanid = -1;
1902 sessions[id].ptyfd = -1;
1903 sessions[id].ttyfd = -1;
1904 sessions[id].ptymaster = -1;
1905 sessions[id].x11_chanids = NULL;
1906 sessions[id].next_unused = sessions_first_unused;
1907 sessions_first_unused = id;
1908}
1909
Damien Millerb38eff82000-04-01 11:09:21 +10001910Session *
1911session_new(void)
1912{
Damien Miller7207f642008-05-19 15:34:50 +10001913 Session *s, *tmp;
1914
1915 if (sessions_first_unused == -1) {
1916 if (sessions_nalloc >= options.max_sessions)
1917 return NULL;
1918 debug2("%s: allocate (allocated %d max %d)",
1919 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001920 tmp = xreallocarray(sessions, sessions_nalloc + 1,
Damien Miller7207f642008-05-19 15:34:50 +10001921 sizeof(*sessions));
1922 if (tmp == NULL) {
1923 error("%s: cannot allocate %d sessions",
1924 __func__, sessions_nalloc + 1);
1925 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001926 }
Damien Miller7207f642008-05-19 15:34:50 +10001927 sessions = tmp;
1928 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001929 }
Damien Miller7207f642008-05-19 15:34:50 +10001930
1931 if (sessions_first_unused >= sessions_nalloc ||
1932 sessions_first_unused < 0) {
1933 fatal("%s: insane first_unused %d max %d nalloc %d",
1934 __func__, sessions_first_unused, options.max_sessions,
1935 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001936 }
Damien Miller7207f642008-05-19 15:34:50 +10001937
1938 s = &sessions[sessions_first_unused];
1939 if (s->used) {
1940 fatal("%s: session %d already used",
1941 __func__, sessions_first_unused);
1942 }
1943 sessions_first_unused = s->next_unused;
1944 s->used = 1;
1945 s->next_unused = -1;
1946 debug("session_new: session %d", s->self);
1947
1948 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001949}
1950
Ben Lindstrombba81212001-06-25 05:01:22 +00001951static void
Damien Millerb38eff82000-04-01 11:09:21 +10001952session_dump(void)
1953{
1954 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001955 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001956 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001957
1958 debug("dump: used %d next_unused %d session %d %p "
1959 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001960 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001961 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001962 s->self,
1963 s,
1964 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001965 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001966 }
1967}
1968
Damien Millerefb4afe2000-04-12 18:45:05 +10001969int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001970session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001971{
1972 Session *s = session_new();
1973 debug("session_open: channel %d", chanid);
1974 if (s == NULL) {
1975 error("no more sessions");
1976 return 0;
1977 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001978 s->authctxt = authctxt;
1979 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001980 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001981 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001982 debug("session_open: session %d: link with channel %d", s->self, chanid);
1983 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001984 return 1;
1985}
1986
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001987Session *
1988session_by_tty(char *tty)
1989{
1990 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001991 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001992 Session *s = &sessions[i];
1993 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1994 debug("session_by_tty: session %d tty %s", i, tty);
1995 return s;
1996 }
1997 }
1998 debug("session_by_tty: unknown tty %.100s", tty);
1999 session_dump();
2000 return NULL;
2001}
2002
Ben Lindstrombba81212001-06-25 05:01:22 +00002003static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002004session_by_channel(int id)
2005{
2006 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002007 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002008 Session *s = &sessions[i];
2009 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10002010 debug("session_by_channel: session %d channel %d",
2011 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002012 return s;
2013 }
2014 }
2015 debug("session_by_channel: unknown channel %d", id);
2016 session_dump();
2017 return NULL;
2018}
2019
Ben Lindstrombba81212001-06-25 05:01:22 +00002020static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10002021session_by_x11_channel(int id)
2022{
2023 int i, j;
2024
Damien Miller7207f642008-05-19 15:34:50 +10002025 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002026 Session *s = &sessions[i];
2027
2028 if (s->x11_chanids == NULL || !s->used)
2029 continue;
2030 for (j = 0; s->x11_chanids[j] != -1; j++) {
2031 if (s->x11_chanids[j] == id) {
2032 debug("session_by_x11_channel: session %d "
2033 "channel %d", s->self, id);
2034 return s;
2035 }
2036 }
2037 }
2038 debug("session_by_x11_channel: unknown channel %d", id);
2039 session_dump();
2040 return NULL;
2041}
2042
2043static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002044session_by_pid(pid_t pid)
2045{
2046 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00002047 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10002048 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002049 Session *s = &sessions[i];
2050 if (s->used && s->pid == pid)
2051 return s;
2052 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002053 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002054 session_dump();
2055 return NULL;
2056}
2057
Ben Lindstrombba81212001-06-25 05:01:22 +00002058static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002059session_window_change_req(Session *s)
2060{
2061 s->col = packet_get_int();
2062 s->row = packet_get_int();
2063 s->xpixel = packet_get_int();
2064 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002065 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10002066 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2067 return 1;
2068}
2069
Ben Lindstrombba81212001-06-25 05:01:22 +00002070static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002071session_pty_req(Session *s)
2072{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002073 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00002074 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002075
Damien Miller5ff30c62013-10-30 22:21:50 +11002076 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00002077 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10002078 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002079 }
2080 if (s->ttyfd != -1) {
2081 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10002082 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002083 }
2084
Damien Millerefb4afe2000-04-12 18:45:05 +10002085 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002086
2087 if (compat20) {
2088 s->col = packet_get_int();
2089 s->row = packet_get_int();
2090 } else {
2091 s->row = packet_get_int();
2092 s->col = packet_get_int();
2093 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002094 s->xpixel = packet_get_int();
2095 s->ypixel = packet_get_int();
2096
2097 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002098 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002099 s->term = NULL;
2100 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00002101
Damien Millerefb4afe2000-04-12 18:45:05 +10002102 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00002103 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10002104 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2105 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10002106 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002107 s->term = NULL;
2108 s->ptyfd = -1;
2109 s->ttyfd = -1;
2110 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10002111 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002112 }
2113 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002114
2115 /* for SSH1 the tty modes length is not given */
2116 if (!compat20)
2117 n_bytes = packet_remaining();
2118 tty_parse_modes(s->ttyfd, &n_bytes);
2119
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002120 if (!use_privsep)
2121 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002122
2123 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10002124 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2125
Damien Miller48b03fc2002-01-22 23:11:40 +11002126 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10002127 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002128 return 1;
2129}
2130
Ben Lindstrombba81212001-06-25 05:01:22 +00002131static int
Damien Millerbd483e72000-04-30 10:00:53 +10002132session_subsystem_req(Session *s)
2133{
Damien Millerae452462001-10-10 15:08:06 +10002134 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002135 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10002136 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11002137 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10002138 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10002139
Damien Miller71df7522013-10-15 12:12:02 +11002140 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002141 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11002142 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10002143 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10002144
Damien Millerf6d9e222000-06-18 14:50:44 +10002145 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11002146 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10002147 prog = options.subsystem_command[i];
2148 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002149 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11002150 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002151 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11002152 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002153 if (stat(prog, &st) < 0)
2154 debug("subsystem: cannot stat %s: %s",
2155 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11002156 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002157 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10002158 }
Damien Miller7207f642008-05-19 15:34:50 +10002159 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11002160 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10002161 }
2162 }
2163
2164 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11002165 logit("subsystem request for %.100s by user %s failed, "
2166 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10002167
Damien Millerbd483e72000-04-30 10:00:53 +10002168 return success;
2169}
2170
Ben Lindstrombba81212001-06-25 05:01:22 +00002171static int
Damien Millerbd483e72000-04-30 10:00:53 +10002172session_x11_req(Session *s)
2173{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002174 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002175
Damien Miller2b9b0452005-07-17 17:19:24 +10002176 if (s->auth_proto != NULL || s->auth_data != NULL) {
2177 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002178 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002179 return 0;
2180 }
Damien Millerbd483e72000-04-30 10:00:53 +10002181 s->single_connection = packet_get_char();
2182 s->auth_proto = packet_get_string(NULL);
2183 s->auth_data = packet_get_string(NULL);
2184 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002185 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002186
Ben Lindstrom768176b2001-06-09 01:29:12 +00002187 success = session_setup_x11fwd(s);
2188 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002189 free(s->auth_proto);
2190 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002191 s->auth_proto = NULL;
2192 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002193 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002194 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002195}
2196
Ben Lindstrombba81212001-06-25 05:01:22 +00002197static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002198session_shell_req(Session *s)
2199{
Damien Miller48b03fc2002-01-22 23:11:40 +11002200 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002201 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002202}
2203
Ben Lindstrombba81212001-06-25 05:01:22 +00002204static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002205session_exec_req(Session *s)
2206{
Damien Miller7207f642008-05-19 15:34:50 +10002207 u_int len, success;
2208
Damien Millerf6d9e222000-06-18 14:50:44 +10002209 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002210 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002211 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002212 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002213 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002214}
2215
Ben Lindstrombba81212001-06-25 05:01:22 +00002216static int
Damien Miller54c45982003-05-15 10:20:13 +10002217session_break_req(Session *s)
2218{
Damien Miller54c45982003-05-15 10:20:13 +10002219
Darren Tucker1f8311c2004-05-13 16:39:33 +10002220 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002221 packet_check_eom();
2222
Darren Tucker9c5d5532011-11-04 10:55:24 +11002223 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002224 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002225 return 1;
2226}
2227
2228static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002229session_env_req(Session *s)
2230{
2231 char *name, *val;
2232 u_int name_len, val_len, i;
2233
Damien Miller8569eba2014-03-04 09:35:17 +11002234 name = packet_get_cstring(&name_len);
2235 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002236 packet_check_eom();
2237
2238 /* Don't set too many environment variables */
2239 if (s->num_env > 128) {
2240 debug2("Ignoring env request %s: too many env vars", name);
2241 goto fail;
2242 }
2243
2244 for (i = 0; i < options.num_accept_env; i++) {
2245 if (match_pattern(name, options.accept_env[i])) {
2246 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002247 s->env = xreallocarray(s->env, s->num_env + 1,
Damien Miller36812092006-03-26 14:22:47 +11002248 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002249 s->env[s->num_env].name = name;
2250 s->env[s->num_env].val = val;
2251 s->num_env++;
2252 return (1);
2253 }
2254 }
2255 debug2("Ignoring env request %s: disallowed name", name);
2256
2257 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002258 free(name);
2259 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002260 return (0);
2261}
2262
2263static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002264session_auth_agent_req(Session *s)
2265{
2266 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002267 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002268 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002269 debug("session_auth_agent_req: no_agent_forwarding_flag");
2270 return 0;
2271 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002272 if (called) {
2273 return 0;
2274 } else {
2275 called = 1;
2276 return auth_input_request_forwarding(s->pw);
2277 }
2278}
2279
Damien Millerc7ef63d2002-02-05 12:21:42 +11002280int
2281session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002282{
Damien Millerefb4afe2000-04-12 18:45:05 +10002283 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002284 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002285
Damien Millerc7ef63d2002-02-05 12:21:42 +11002286 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002287 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002288 c->self, rtype);
2289 return 0;
2290 }
2291 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002292
2293 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002294 * a session is in LARVAL state until a shell, a command
2295 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002296 */
2297 if (c->type == SSH_CHANNEL_LARVAL) {
2298 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002299 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002300 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002301 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002302 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002303 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002304 } else if (strcmp(rtype, "x11-req") == 0) {
2305 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002306 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2307 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002308 } else if (strcmp(rtype, "subsystem") == 0) {
2309 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002310 } else if (strcmp(rtype, "env") == 0) {
2311 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002312 }
2313 }
2314 if (strcmp(rtype, "window-change") == 0) {
2315 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002316 } else if (strcmp(rtype, "break") == 0) {
2317 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002318 }
Damien Millera6b1d162004-06-30 22:41:07 +10002319
Damien Millerc7ef63d2002-02-05 12:21:42 +11002320 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002321}
2322
2323void
Damien Miller8853ca52010-06-26 10:00:14 +10002324session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2325 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002326{
2327 if (!compat20)
2328 fatal("session_set_fds: called for proto != 2.0");
2329 /*
2330 * now that have a child and a pipe to the child,
2331 * we can activate our channel and register the fd's
2332 */
2333 if (s->chanid == -1)
2334 fatal("no channel for session %d", s->self);
2335 channel_set_fds(s->chanid,
2336 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002337 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002338 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002339}
2340
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002341/*
2342 * Function to perform pty cleanup. Also called if we get aborted abnormally
2343 * (e.g., due to a dropped connection).
2344 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002345void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002346session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002347{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002348 if (s == NULL) {
2349 error("session_pty_cleanup: no session");
2350 return;
2351 }
2352 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002353 return;
2354
Kevin Steves43cdef32001-02-11 14:12:08 +00002355 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002356
Damien Millerb38eff82000-04-01 11:09:21 +10002357 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002358 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002359 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002360
2361 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002362 if (getuid() == 0)
2363 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002364
2365 /*
2366 * Close the server side of the socket pairs. We must do this after
2367 * the pty cleanup, so that another process doesn't get this pty
2368 * while we're still cleaning up.
2369 */
Damien Miller7207f642008-05-19 15:34:50 +10002370 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2371 error("close(s->ptymaster/%d): %s",
2372 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002373
2374 /* unlink pty from session */
2375 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002376}
Damien Millerefb4afe2000-04-12 18:45:05 +10002377
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002378void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002379session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002380{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002381 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002382}
2383
Damien Miller5a80bba2002-09-04 16:39:02 +10002384static char *
2385sig2name(int sig)
2386{
2387#define SSH_SIG(x) if (sig == SIG ## x) return #x
2388 SSH_SIG(ABRT);
2389 SSH_SIG(ALRM);
2390 SSH_SIG(FPE);
2391 SSH_SIG(HUP);
2392 SSH_SIG(ILL);
2393 SSH_SIG(INT);
2394 SSH_SIG(KILL);
2395 SSH_SIG(PIPE);
2396 SSH_SIG(QUIT);
2397 SSH_SIG(SEGV);
2398 SSH_SIG(TERM);
2399 SSH_SIG(USR1);
2400 SSH_SIG(USR2);
2401#undef SSH_SIG
2402 return "SIG@openssh.com";
2403}
2404
Ben Lindstrombba81212001-06-25 05:01:22 +00002405static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002406session_close_x11(int id)
2407{
2408 Channel *c;
2409
Damien Millerd47c62a2005-12-13 19:33:57 +11002410 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002411 debug("session_close_x11: x11 channel %d missing", id);
2412 } else {
2413 /* Detach X11 listener */
2414 debug("session_close_x11: detach x11 channel %d", id);
2415 channel_cancel_cleanup(id);
2416 if (c->ostate != CHAN_OUTPUT_CLOSED)
2417 chan_mark_dead(c);
2418 }
2419}
2420
2421static void
2422session_close_single_x11(int id, void *arg)
2423{
2424 Session *s;
2425 u_int i;
2426
2427 debug3("session_close_single_x11: channel %d", id);
2428 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002429 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002430 fatal("session_close_single_x11: no x11 channel %d", id);
2431 for (i = 0; s->x11_chanids[i] != -1; i++) {
2432 debug("session_close_single_x11: session %d: "
2433 "closing channel %d", s->self, s->x11_chanids[i]);
2434 /*
2435 * The channel "id" is already closing, but make sure we
2436 * close all of its siblings.
2437 */
2438 if (s->x11_chanids[i] != id)
2439 session_close_x11(s->x11_chanids[i]);
2440 }
Darren Tuckera627d422013-06-02 07:31:17 +10002441 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002442 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002443 free(s->display);
2444 s->display = NULL;
2445 free(s->auth_proto);
2446 s->auth_proto = NULL;
2447 free(s->auth_data);
2448 s->auth_data = NULL;
2449 free(s->auth_display);
2450 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002451}
2452
2453static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002454session_exit_message(Session *s, int status)
2455{
2456 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002457
2458 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002459 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002460 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002461 debug("session_exit_message: session %d channel %d pid %ld",
2462 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002463
2464 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002465 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002466 packet_put_int(WEXITSTATUS(status));
2467 packet_send();
2468 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002469 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002470 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002471#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002472 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002473#else /* WCOREDUMP */
2474 packet_put_char(0);
2475#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002476 packet_put_cstring("");
2477 packet_put_cstring("");
2478 packet_send();
2479 } else {
2480 /* Some weird exit cause. Just exit. */
2481 packet_disconnect("wait returned status %04x.", status);
2482 }
2483
2484 /* disconnect channel */
2485 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002486
2487 /*
2488 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002489 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002490 * by session_close_by_channel when the childs close their fds.
2491 */
2492 channel_register_cleanup(c->self, session_close_by_channel, 1);
2493
Damien Miller166fca82000-04-20 07:42:21 +10002494 /*
2495 * emulate a write failure with 'chan_write_failed', nobody will be
2496 * interested in data we write.
2497 * Note that we must not call 'chan_read_failed', since there could
2498 * be some more data waiting in the pipe.
2499 */
Damien Millerbd483e72000-04-30 10:00:53 +10002500 if (c->ostate != CHAN_OUTPUT_CLOSED)
2501 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002502}
2503
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002504void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002505session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002506{
djm@openbsd.org95767262016-03-07 19:02:43 +00002507 struct ssh *ssh = active_state; /* XXX */
Damien Millereccb9de2005-06-17 12:59:34 +10002508 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002509
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002510 verbose("Close session: user %s from %.200s port %d id %d",
2511 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002512 ssh_remote_ipaddr(ssh),
2513 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002514 s->self);
2515
Darren Tucker3e33cec2003-10-02 16:12:36 +10002516 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002517 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002518 free(s->term);
2519 free(s->display);
2520 free(s->x11_chanids);
2521 free(s->auth_display);
2522 free(s->auth_data);
2523 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002524 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002525 if (s->env != NULL) {
2526 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002527 free(s->env[i].name);
2528 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002529 }
Darren Tuckera627d422013-06-02 07:31:17 +10002530 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002531 }
Damien Millere247cc42000-05-07 12:03:14 +10002532 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002533 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002534}
2535
2536void
2537session_close_by_pid(pid_t pid, int status)
2538{
2539 Session *s = session_by_pid(pid);
2540 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002541 debug("session_close_by_pid: no session for pid %ld",
2542 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002543 return;
2544 }
2545 if (s->chanid != -1)
2546 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002547 if (s->ttyfd != -1)
2548 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002549 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002550}
2551
2552/*
2553 * this is called when a channel dies before
2554 * the session 'child' itself dies
2555 */
2556void
2557session_close_by_channel(int id, void *arg)
2558{
2559 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002560 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002561
Damien Millerefb4afe2000-04-12 18:45:05 +10002562 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002563 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002564 return;
2565 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002566 debug("session_close_by_channel: channel %d child %ld",
2567 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002568 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002569 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002570 /*
2571 * delay detach of session, but release pty, since
2572 * the fd's to the child are already closed
2573 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002574 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002575 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002576 return;
2577 }
2578 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002579 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002580
2581 /* Close any X11 listeners associated with this session */
2582 if (s->x11_chanids != NULL) {
2583 for (i = 0; s->x11_chanids[i] != -1; i++) {
2584 session_close_x11(s->x11_chanids[i]);
2585 s->x11_chanids[i] = -1;
2586 }
2587 }
2588
Damien Millerefb4afe2000-04-12 18:45:05 +10002589 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002590 session_close(s);
2591}
2592
2593void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002594session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002595{
2596 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002597 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002598 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002599 if (s->used) {
2600 if (closefunc != NULL)
2601 closefunc(s);
2602 else
2603 session_close(s);
2604 }
Damien Miller52b77be2001-10-10 15:14:37 +10002605 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002606}
2607
Ben Lindstrombba81212001-06-25 05:01:22 +00002608static char *
Damien Millere247cc42000-05-07 12:03:14 +10002609session_tty_list(void)
2610{
2611 static char buf[1024];
2612 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002613 char *cp;
2614
Damien Millere247cc42000-05-07 12:03:14 +10002615 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002616 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002617 Session *s = &sessions[i];
2618 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002619
Damien Millera8ed44b2003-01-10 09:53:12 +11002620 if (strncmp(s->tty, "/dev/", 5) != 0) {
2621 cp = strrchr(s->tty, '/');
2622 cp = (cp == NULL) ? s->tty : cp + 1;
2623 } else
2624 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002625
Damien Millere247cc42000-05-07 12:03:14 +10002626 if (buf[0] != '\0')
2627 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002628 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002629 }
2630 }
2631 if (buf[0] == '\0')
2632 strlcpy(buf, "notty", sizeof buf);
2633 return buf;
2634}
2635
2636void
2637session_proctitle(Session *s)
2638{
2639 if (s->pw == NULL)
2640 error("no user for session %d", s->self);
2641 else
2642 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2643}
2644
Ben Lindstrom768176b2001-06-09 01:29:12 +00002645int
2646session_setup_x11fwd(Session *s)
2647{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002648 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002649 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002650 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002651 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002652
2653 if (no_x11_forwarding_flag) {
2654 packet_send_debug("X11 forwarding disabled in user configuration file.");
2655 return 0;
2656 }
2657 if (!options.x11_forwarding) {
2658 debug("X11 forwarding disabled in server configuration file.");
2659 return 0;
2660 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002661 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002662 (stat(options.xauth_location, &st) == -1)) {
2663 packet_send_debug("No xauth program; cannot forward with spoofing.");
2664 return 0;
2665 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002666 if (options.use_login) {
2667 packet_send_debug("X11 forwarding disabled; "
2668 "not compatible with UseLogin=yes.");
2669 return 0;
2670 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002671 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002672 debug("X11 display already set.");
2673 return 0;
2674 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002675 if (x11_create_display_inet(options.x11_display_offset,
2676 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002677 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002678 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002679 return 0;
2680 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002681 for (i = 0; s->x11_chanids[i] != -1; i++) {
2682 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002683 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002684 }
Kevin Steves366298c2001-12-19 17:58:01 +00002685
2686 /* Set up a suitable value for the DISPLAY variable. */
2687 if (gethostname(hostname, sizeof(hostname)) < 0)
2688 fatal("gethostname: %.100s", strerror(errno));
2689 /*
2690 * auth_display must be used as the displayname when the
2691 * authorization entry is added with xauth(1). This will be
2692 * different than the DISPLAY string for localhost displays.
2693 */
Damien Miller95c249f2002-02-05 12:11:34 +11002694 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002695 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002696 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002697 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002698 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002699 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002700 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002701 } else {
2702#ifdef IPADDR_IN_DISPLAY
2703 struct hostent *he;
2704 struct in_addr my_addr;
2705
2706 he = gethostbyname(hostname);
2707 if (he == NULL) {
2708 error("Can't get IP address for X11 DISPLAY.");
2709 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2710 return 0;
2711 }
2712 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002713 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002714 s->display_number, s->screen);
2715#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002716 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002717 s->display_number, s->screen);
2718#endif
2719 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002720 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002721 }
2722
Ben Lindstrom768176b2001-06-09 01:29:12 +00002723 return 1;
2724}
2725
Ben Lindstrombba81212001-06-25 05:01:22 +00002726static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002727do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002728{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002729 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002730}
2731
2732void
2733do_cleanup(Authctxt *authctxt)
2734{
2735 static int called = 0;
2736
2737 debug("do_cleanup");
2738
2739 /* no cleanup if we're in the child for login shell */
2740 if (is_child)
2741 return;
2742
2743 /* avoid double cleanup */
2744 if (called)
2745 return;
2746 called = 1;
2747
Darren Tucker9142e1c2007-08-16 23:28:04 +10002748 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002749 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002750
2751#ifdef USE_PAM
2752 if (options.use_pam) {
2753 sshpam_cleanup();
2754 sshpam_thread_cleanup();
2755 }
2756#endif
2757
2758 if (!authctxt->authenticated)
2759 return;
2760
Darren Tucker3e33cec2003-10-02 16:12:36 +10002761#ifdef KRB5
2762 if (options.kerberos_ticket_cleanup &&
2763 authctxt->krb5_ctx)
2764 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002765#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002766
2767#ifdef GSSAPI
2768 if (compat20 && options.gss_cleanup_creds)
2769 ssh_gssapi_cleanup_creds();
2770#endif
2771
2772 /* remove agent socket */
2773 auth_sock_cleanup_proc(authctxt->pw);
2774
2775 /*
2776 * Cleanup ptys/utmp only if privsep is disabled,
2777 * or if running in monitor.
2778 */
2779 if (!use_privsep || mm_is_monitor())
2780 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002781}
djm@openbsd.org95767262016-03-07 19:02:43 +00002782
2783/* Return a name for the remote host that fits inside utmp_size */
2784
2785const char *
2786session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2787{
2788 const char *remote = "";
2789
2790 if (utmp_size > 0)
2791 remote = auth_get_canonical_hostname(ssh, use_dns);
2792 if (utmp_size == 0 || strlen(remote) > utmp_size)
2793 remote = ssh_remote_ipaddr(ssh);
2794 return remote;
2795}
2796