blob: 5a64715e2cfdb36aa709a0081321bbc17625fe65 [file] [log] [blame]
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001/* $OpenBSD: session.c,v 1.278 2015/04/24 01:36:00 deraadt 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;
163
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000164/* Name and directory of socket for authentication agent forwarding. */
165static char *auth_sock_name = NULL;
166static char *auth_sock_dir = NULL;
167
168/* removes the agent forwarding socket */
169
170static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000171auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000172{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000173 if (auth_sock_name != NULL) {
174 temporarily_use_uid(pw);
175 unlink(auth_sock_name);
176 rmdir(auth_sock_dir);
177 auth_sock_name = NULL;
178 restore_uid();
179 }
180}
181
182static int
183auth_input_request_forwarding(struct passwd * pw)
184{
185 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000186 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000187
188 if (auth_sock_name != NULL) {
189 error("authentication forwarding requested twice.");
190 return 0;
191 }
192
193 /* Temporarily drop privileged uid for mkdir/bind. */
194 temporarily_use_uid(pw);
195
196 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000197 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000198
199 /* Create private directory for socket */
200 if (mkdtemp(auth_sock_dir) == NULL) {
201 packet_send_debug("Agent forwarding disabled: "
202 "mkdtemp() failed: %.100s", strerror(errno));
203 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000204 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000205 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000206 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000207 }
Damien Miller7207f642008-05-19 15:34:50 +1000208
209 xasprintf(&auth_sock_name, "%s/agent.%ld",
210 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000211
Damien Miller7acefbb2014-07-18 14:11:24 +1000212 /* Start a Unix listener on auth_sock_name. */
213 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000214
215 /* Restore the privileged uid. */
216 restore_uid();
217
Damien Miller7acefbb2014-07-18 14:11:24 +1000218 /* Check for socket/bind/listen failure. */
219 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000220 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000221
222 /* Allocate a channel for the authentication agent socket. */
223 nc = channel_new("auth socket",
224 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
225 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000226 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100227 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000228 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000229
230 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000231 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000232 if (auth_sock_dir != NULL) {
233 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000234 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000235 }
236 if (sock != -1)
237 close(sock);
238 auth_sock_name = NULL;
239 auth_sock_dir = NULL;
240 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000241}
242
Darren Tucker1921ed92004-02-10 13:23:28 +1100243static void
244display_loginmsg(void)
245{
Damien Miller46d38de2005-07-17 17:02:09 +1000246 if (buffer_len(&loginmsg) > 0) {
247 buffer_append(&loginmsg, "\0", 1);
248 printf("%s", (char *)buffer_ptr(&loginmsg));
249 buffer_clear(&loginmsg);
250 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100251}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000252
Ben Lindstromb31783d2001-03-22 02:02:12 +0000253void
254do_authenticated(Authctxt *authctxt)
255{
Damien Miller97f39ae2003-02-24 11:57:01 +1100256 setproctitle("%s", authctxt->pw->pw_name);
257
Ben Lindstromb31783d2001-03-22 02:02:12 +0000258 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000259 /* XXX - streamlocal? */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100260 if (no_port_forwarding_flag ||
261 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
262 channel_disable_adm_local_opens();
263 else
Ben Lindstromb31783d2001-03-22 02:02:12 +0000264 channel_permit_all_opens();
265
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100266 auth_debug_send();
267
Ben Lindstromb31783d2001-03-22 02:02:12 +0000268 if (compat20)
269 do_authenticated2(authctxt);
270 else
271 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000272
Darren Tucker3e33cec2003-10-02 16:12:36 +1000273 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000274}
275
Damien Millerb38eff82000-04-01 11:09:21 +1000276/*
Damien Millerb38eff82000-04-01 11:09:21 +1000277 * Prepares for an interactive session. This is called after the user has
278 * been successfully authenticated. During this message exchange, pseudo
279 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
280 * are requested, etc.
281 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000282static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000283do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000284{
285 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000286 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100287 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000288 int enable_compression_after_reply = 0;
289 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000290
291 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100292 if (s == NULL) {
293 error("no more sessions");
294 return;
295 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000296 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000297 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000298
Damien Millerb38eff82000-04-01 11:09:21 +1000299 /*
300 * We stay in this loop until the client requests to execute a shell
301 * or a command.
302 */
303 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000304 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000305
306 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100307 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000308
309 /* Process the packet. */
310 switch (type) {
311 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000312 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100313 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000314 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000315 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100316 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000317 break;
318 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000319 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000320 debug2("compression disabled");
321 break;
322 }
Damien Millerb38eff82000-04-01 11:09:21 +1000323 /* Enable compression after we have responded with SUCCESS. */
324 enable_compression_after_reply = 1;
325 success = 1;
326 break;
327
328 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000329 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000330 break;
331
332 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000333 s->auth_proto = packet_get_string(&proto_len);
334 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000335
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000336 screen_flag = packet_get_protocol_flags() &
337 SSH_PROTOFLAG_SCREEN_NUMBER;
338 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
339
340 if (packet_remaining() == 4) {
341 if (!screen_flag)
342 debug2("Buggy client: "
343 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000344 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000345 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000346 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000347 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100348 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000349 success = session_setup_x11fwd(s);
350 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000351 free(s->auth_proto);
352 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000353 s->auth_proto = NULL;
354 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000355 }
Damien Millerb38eff82000-04-01 11:09:21 +1000356 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000357
358 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
Damien Miller4f755cd2008-05-19 14:57:41 +1000359 if (!options.allow_agent_forwarding ||
360 no_agent_forwarding_flag || compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000361 debug("Authentication agent forwarding not permitted for this authentication.");
362 break;
363 }
364 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000365 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000366 break;
367
368 case SSH_CMSG_PORT_FORWARD_REQUEST:
369 if (no_port_forwarding_flag) {
370 debug("Port forwarding not permitted for this authentication.");
371 break;
372 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100373 if (!(options.allow_tcp_forwarding & FORWARD_REMOTE)) {
Damien Miller50a41ed2000-10-16 12:14:42 +1100374 debug("Port forwarding not permitted.");
375 break;
376 }
Damien Millerb38eff82000-04-01 11:09:21 +1000377 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000378 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
Damien Miller7acefbb2014-07-18 14:11:24 +1000379 &options.fwd_opts) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000380 debug("Port forwarding failed.");
381 break;
382 }
Damien Millerb38eff82000-04-01 11:09:21 +1000383 success = 1;
384 break;
385
386 case SSH_CMSG_MAX_PACKET_SIZE:
387 if (packet_set_maxsize(packet_get_int()) > 0)
388 success = 1;
389 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100390
Damien Millerb38eff82000-04-01 11:09:21 +1000391 case SSH_CMSG_EXEC_SHELL:
392 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000393 if (type == SSH_CMSG_EXEC_CMD) {
394 command = packet_get_string(&dlen);
395 debug("Exec command '%.500s'", command);
Damien Miller7207f642008-05-19 15:34:50 +1000396 if (do_exec(s, command) != 0)
397 packet_disconnect(
398 "command execution failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000399 free(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000400 } else {
Damien Miller7207f642008-05-19 15:34:50 +1000401 if (do_exec(s, NULL) != 0)
402 packet_disconnect(
403 "shell execution failed");
Damien Millerb38eff82000-04-01 11:09:21 +1000404 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100405 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000406 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000407 return;
408
409 default:
410 /*
411 * Any unknown messages in this phase are ignored,
412 * and a failure message is returned.
413 */
Damien Miller996acd22003-04-09 20:59:48 +1000414 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000415 }
416 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
417 packet_send();
418 packet_write_wait();
419
420 /* Enable compression now that we have replied if appropriate. */
421 if (enable_compression_after_reply) {
422 enable_compression_after_reply = 0;
423 packet_start_compression(compression_level);
424 }
425 }
426}
427
Darren Tucker293ee3c2014-01-19 15:28:01 +1100428#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000429/*
430 * This is called to fork and execute a command when we have no tty. This
431 * will call do_child from the child, and server_loop from the parent after
432 * setting up file descriptors and such.
433 */
Damien Miller7207f642008-05-19 15:34:50 +1000434int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000435do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000436{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000437 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000438
439#ifdef USE_PIPES
440 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000441
Damien Miller22a29882010-05-10 11:53:54 +1000442 if (s == NULL)
443 fatal("do_exec_no_pty: no session");
444
Damien Millerb38eff82000-04-01 11:09:21 +1000445 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000446 if (pipe(pin) < 0) {
447 error("%s: pipe in: %.100s", __func__, strerror(errno));
448 return -1;
449 }
450 if (pipe(pout) < 0) {
451 error("%s: pipe out: %.100s", __func__, strerror(errno));
452 close(pin[0]);
453 close(pin[1]);
454 return -1;
455 }
Damien Miller8853ca52010-06-26 10:00:14 +1000456 if (pipe(perr) < 0) {
457 error("%s: pipe err: %.100s", __func__,
458 strerror(errno));
459 close(pin[0]);
460 close(pin[1]);
461 close(pout[0]);
462 close(pout[1]);
463 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000464 }
465#else
Damien Millerb38eff82000-04-01 11:09:21 +1000466 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000467
Damien Miller22a29882010-05-10 11:53:54 +1000468 if (s == NULL)
469 fatal("do_exec_no_pty: no session");
470
Damien Millerb38eff82000-04-01 11:09:21 +1000471 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000472 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
473 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
474 return -1;
475 }
Damien Miller8853ca52010-06-26 10:00:14 +1000476 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
477 error("%s: socketpair #2: %.100s", __func__,
478 strerror(errno));
479 close(inout[0]);
480 close(inout[1]);
481 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000482 }
483#endif
484
Damien Millere247cc42000-05-07 12:03:14 +1000485 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000486
Damien Millerb38eff82000-04-01 11:09:21 +1000487 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000488 switch ((pid = fork())) {
489 case -1:
490 error("%s: fork: %.100s", __func__, strerror(errno));
491#ifdef USE_PIPES
492 close(pin[0]);
493 close(pin[1]);
494 close(pout[0]);
495 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000496 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000497 close(perr[1]);
498#else
499 close(inout[0]);
500 close(inout[1]);
501 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000502 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000503#endif
504 return -1;
505 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000506 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000507
Damien Millerb38eff82000-04-01 11:09:21 +1000508 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000509 log_init(__progname, options.log_level,
510 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000511
512 /*
513 * Create a new session and process group since the 4.4BSD
514 * setlogin() affects the entire process group.
515 */
516 if (setsid() < 0)
517 error("setsid failed: %.100s", strerror(errno));
518
519#ifdef USE_PIPES
520 /*
521 * Redirect stdin. We close the parent side of the socket
522 * pair, and make the child side the standard input.
523 */
524 close(pin[1]);
525 if (dup2(pin[0], 0) < 0)
526 perror("dup2 stdin");
527 close(pin[0]);
528
529 /* Redirect stdout. */
530 close(pout[0]);
531 if (dup2(pout[1], 1) < 0)
532 perror("dup2 stdout");
533 close(pout[1]);
534
535 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000536 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000537 if (dup2(perr[1], 2) < 0)
538 perror("dup2 stderr");
539 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000540#else
Damien Millerb38eff82000-04-01 11:09:21 +1000541 /*
542 * Redirect stdin, stdout, and stderr. Stdin and stdout will
543 * use the same socket, as some programs (particularly rdist)
544 * seem to depend on it.
545 */
546 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000547 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000548 if (dup2(inout[0], 0) < 0) /* stdin */
549 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000550 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000551 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000552 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000553 if (dup2(err[0], 2) < 0) /* stderr */
554 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000555 close(err[0]);
556#endif
557
Damien Millerb38eff82000-04-01 11:09:21 +1000558
Tim Rice81ed5182002-09-25 17:38:46 -0700559#ifdef _UNICOS
560 cray_init_job(s->pw); /* set up cray jid and tmpdir */
561#endif
562
Damien Millerb38eff82000-04-01 11:09:21 +1000563 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000564 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000565 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000566 default:
567 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000568 }
Damien Miller7207f642008-05-19 15:34:50 +1000569
Tim Rice81ed5182002-09-25 17:38:46 -0700570#ifdef _UNICOS
571 signal(WJSIGNAL, cray_job_termination_handler);
572#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100573#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100574 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100575#endif
Damien Miller7207f642008-05-19 15:34:50 +1000576
Damien Millerb38eff82000-04-01 11:09:21 +1000577 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000578 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100579 packet_set_interactive(s->display != NULL,
580 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000581
582 /*
583 * Clear loginmsg, since it's the child's responsibility to display
584 * it to the user, otherwise multiple sessions may accumulate
585 * multiple copies of the login messages.
586 */
587 buffer_clear(&loginmsg);
588
Damien Millerb38eff82000-04-01 11:09:21 +1000589#ifdef USE_PIPES
590 /* We are the parent. Close the child sides of the pipes. */
591 close(pin[0]);
592 close(pout[1]);
593 close(perr[1]);
594
Damien Millerefb4afe2000-04-12 18:45:05 +1000595 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000596 session_set_fds(s, pin[1], pout[0], perr[0],
597 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000598 } else {
599 /* Enter the interactive session. */
600 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000601 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000602 }
Damien Miller7207f642008-05-19 15:34:50 +1000603#else
Damien Millerb38eff82000-04-01 11:09:21 +1000604 /* We are the parent. Close the child sides of the socket pairs. */
605 close(inout[0]);
606 close(err[0]);
607
608 /*
609 * Enter the interactive session. Note: server_loop must be able to
610 * handle the case that fdin and fdout are the same.
611 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000612 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000613 session_set_fds(s, inout[1], inout[1], err[1],
614 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000615 } else {
616 server_loop(pid, inout[1], inout[1], err[1]);
617 /* server_loop has closed inout[1] and err[1]. */
618 }
Damien Miller7207f642008-05-19 15:34:50 +1000619#endif
620 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000621}
622
623/*
624 * This is called to fork and execute a command when we have a tty. This
625 * will call do_child from the child, and server_loop from the parent after
626 * setting up file descriptors, controlling tty, updating wtmp, utmp,
627 * lastlog, and other such operations.
628 */
Damien Miller7207f642008-05-19 15:34:50 +1000629int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000630do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000631{
Damien Millerb38eff82000-04-01 11:09:21 +1000632 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000633 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000634
635 if (s == NULL)
636 fatal("do_exec_pty: no session");
637 ptyfd = s->ptyfd;
638 ttyfd = s->ttyfd;
639
Damien Miller7207f642008-05-19 15:34:50 +1000640 /*
641 * Create another descriptor of the pty master side for use as the
642 * standard input. We could use the original descriptor, but this
643 * simplifies code in server_loop. The descriptor is bidirectional.
644 * Do this before forking (and cleanup in the child) so as to
645 * detect and gracefully fail out-of-fd conditions.
646 */
647 if ((fdout = dup(ptyfd)) < 0) {
648 error("%s: dup #1: %s", __func__, strerror(errno));
649 close(ttyfd);
650 close(ptyfd);
651 return -1;
652 }
653 /* we keep a reference to the pty master */
654 if ((ptymaster = dup(ptyfd)) < 0) {
655 error("%s: dup #2: %s", __func__, strerror(errno));
656 close(ttyfd);
657 close(ptyfd);
658 close(fdout);
659 return -1;
660 }
661
Damien Millerb38eff82000-04-01 11:09:21 +1000662 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000663 switch ((pid = fork())) {
664 case -1:
665 error("%s: fork: %.100s", __func__, strerror(errno));
666 close(fdout);
667 close(ptymaster);
668 close(ttyfd);
669 close(ptyfd);
670 return -1;
671 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000672 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000673
Damien Miller7207f642008-05-19 15:34:50 +1000674 close(fdout);
675 close(ptymaster);
676
Damien Miller942da032000-08-18 13:59:06 +1000677 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000678 log_init(__progname, options.log_level,
679 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000680 /* Close the master side of the pseudo tty. */
681 close(ptyfd);
682
683 /* Make the pseudo tty our controlling tty. */
684 pty_make_controlling_tty(&ttyfd, s->tty);
685
Damien Miller9c751422001-10-10 15:02:46 +1000686 /* Redirect stdin/stdout/stderr from the pseudo tty. */
687 if (dup2(ttyfd, 0) < 0)
688 error("dup2 stdin: %s", strerror(errno));
689 if (dup2(ttyfd, 1) < 0)
690 error("dup2 stdout: %s", strerror(errno));
691 if (dup2(ttyfd, 2) < 0)
692 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000693
694 /* Close the extra descriptor for the pseudo tty. */
695 close(ttyfd);
696
Damien Miller942da032000-08-18 13:59:06 +1000697 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000698#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700699 if (!(options.use_login && command == NULL)) {
700#ifdef _UNICOS
701 cray_init_job(s->pw); /* set up cray jid and tmpdir */
702#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100703 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700704 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000705# ifdef LOGIN_NEEDS_UTMPX
706 else
707 do_pre_login(s);
708# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000709#endif
Damien Miller7207f642008-05-19 15:34:50 +1000710 /*
711 * Do common processing for the child, such as execing
712 * the command.
713 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000714 do_child(s, command);
715 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000716 default:
717 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000718 }
Damien Miller7207f642008-05-19 15:34:50 +1000719
Tim Rice81ed5182002-09-25 17:38:46 -0700720#ifdef _UNICOS
721 signal(WJSIGNAL, cray_job_termination_handler);
722#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100723#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100724 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100725#endif
Damien Miller7207f642008-05-19 15:34:50 +1000726
Damien Millerb38eff82000-04-01 11:09:21 +1000727 s->pid = pid;
728
729 /* Parent. Close the slave side of the pseudo tty. */
730 close(ttyfd);
731
Damien Millerb38eff82000-04-01 11:09:21 +1000732 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000733 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100734 packet_set_interactive(1,
735 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Millerefb4afe2000-04-12 18:45:05 +1000736 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000737 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000738 } else {
739 server_loop(pid, ptyfd, fdout, -1);
740 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000741 }
Damien Miller7207f642008-05-19 15:34:50 +1000742 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000743}
744
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000745#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000746static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000747do_pre_login(Session *s)
748{
749 socklen_t fromlen;
750 struct sockaddr_storage from;
751 pid_t pid = getpid();
752
753 /*
754 * Get IP address of client. If the connection is not a socket, let
755 * the address be 0.0.0.0.
756 */
757 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000758 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000759 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000760 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100761 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000762 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000763 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000764 }
765 }
766
767 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000768 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000769 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000770}
771#endif
772
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000773/*
774 * This is called to fork and execute a command. If another command is
775 * to be forced, execute that instead.
776 */
Damien Miller7207f642008-05-19 15:34:50 +1000777int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000778do_exec(Session *s, const char *command)
779{
Damien Miller7207f642008-05-19 15:34:50 +1000780 int ret;
Damien Miller71df7522013-10-15 12:12:02 +1100781 const char *forced = NULL;
782 char session_type[1024], *tty = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000783
Damien Millere2754432006-07-24 14:06:47 +1000784 if (options.adm_forced_command) {
785 original_command = command;
786 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100787 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000788 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000789 original_command = command;
790 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100791 forced = "(key-option)";
792 }
793 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100794 if (IS_INTERNAL_SFTP(command)) {
795 s->is_subsystem = s->is_subsystem ?
796 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
797 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100798 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100799 snprintf(session_type, sizeof(session_type),
800 "forced-command %s '%.900s'", forced, command);
801 } else if (s->is_subsystem) {
802 snprintf(session_type, sizeof(session_type),
803 "subsystem '%.900s'", s->subsys);
804 } else if (command == NULL) {
805 snprintf(session_type, sizeof(session_type), "shell");
806 } else {
807 /* NB. we don't log unforced commands to preserve privacy */
808 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000809 }
810
Damien Miller71df7522013-10-15 12:12:02 +1100811 if (s->ttyfd != -1) {
812 tty = s->tty;
813 if (strncmp(tty, "/dev/", 5) == 0)
814 tty += 5;
815 }
816
817 verbose("Starting session: %s%s%s for %s from %.200s port %d",
818 session_type,
819 tty == NULL ? "" : " on ",
820 tty == NULL ? "" : tty,
821 s->pw->pw_name,
822 get_remote_ipaddr(),
823 get_remote_port());
824
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100825#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100826 if (command != NULL)
827 PRIVSEP(audit_run_command(command));
828 else if (s->ttyfd == -1) {
829 char *shell = s->pw->pw_shell;
830
831 if (shell[0] == '\0') /* empty shell means /bin/sh */
832 shell =_PATH_BSHELL;
833 PRIVSEP(audit_run_command(shell));
834 }
835#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000836 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000837 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000838 else
Damien Miller7207f642008-05-19 15:34:50 +1000839 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000840
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000841 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000842
Darren Tucker09991742004-07-17 17:05:14 +1000843 /*
844 * Clear loginmsg: it's the child's responsibility to display
845 * it to the user, otherwise multiple sessions may accumulate
846 * multiple copies of the login messages.
847 */
848 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000849
850 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000851}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000852
Damien Miller942da032000-08-18 13:59:06 +1000853/* administrative, login(1)-like work */
854void
Damien Miller69b69aa2000-10-28 14:19:58 +1100855do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000856{
Damien Miller942da032000-08-18 13:59:06 +1000857 socklen_t fromlen;
858 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000859 struct passwd * pw = s->pw;
860 pid_t pid = getpid();
861
862 /*
863 * Get IP address of client. If the connection is not a socket, let
864 * the address be 0.0.0.0.
865 */
866 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000867 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000868 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000869 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000870 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000871 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000872 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000873 }
874 }
875
876 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000877 if (!use_privsep)
878 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
879 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000880 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000881 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000882
Kevin Steves092f2ef2000-10-14 13:36:13 +0000883#ifdef USE_PAM
884 /*
885 * If password change is needed, do it now.
886 * This needs to occur before the ~/.hushlogin check.
887 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100888 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
889 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000890 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100891 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000892 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000893 }
894#endif
895
Damien Millercf205e82001-04-16 18:29:15 +1000896 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000897 return;
898
Darren Tucker1921ed92004-02-10 13:23:28 +1100899 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000900
Damien Millercf205e82001-04-16 18:29:15 +1000901 do_motd();
902}
903
904/*
905 * Display the message of the day.
906 */
907void
908do_motd(void)
909{
910 FILE *f;
911 char buf[256];
912
Damien Miller942da032000-08-18 13:59:06 +1000913 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000914#ifdef HAVE_LOGIN_CAP
915 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
916 "/etc/motd"), "r");
917#else
Damien Miller942da032000-08-18 13:59:06 +1000918 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000919#endif
Damien Miller942da032000-08-18 13:59:06 +1000920 if (f) {
921 while (fgets(buf, sizeof(buf), f))
922 fputs(buf, stdout);
923 fclose(f);
924 }
925 }
926}
927
Kevin Steves8f63caa2001-07-04 18:23:02 +0000928
929/*
930 * Check for quiet login, either .hushlogin or command given.
931 */
932int
933check_quietlogin(Session *s, const char *command)
934{
935 char buf[256];
936 struct passwd *pw = s->pw;
937 struct stat st;
938
939 /* Return 1 if .hushlogin exists or a command given. */
940 if (command != NULL)
941 return 1;
942 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
943#ifdef HAVE_LOGIN_CAP
944 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
945 return 1;
946#else
947 if (stat(buf, &st) >= 0)
948 return 1;
949#endif
950 return 0;
951}
952
Damien Millerb38eff82000-04-01 11:09:21 +1000953/*
954 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100955 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000956 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000957void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000958child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100959 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000960{
Damien Millerb38eff82000-04-01 11:09:21 +1000961 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000962 u_int envsize;
963 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000964
Damien Miller8569eba2014-03-04 09:35:17 +1100965 if (strchr(name, '=') != NULL) {
966 error("Invalid environment variable \"%.100s\"", name);
967 return;
968 }
969
Damien Millerb38eff82000-04-01 11:09:21 +1000970 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000971 * If we're passed an uninitialized list, allocate a single null
972 * entry before continuing.
973 */
974 if (*envp == NULL && *envsizep == 0) {
975 *envp = xmalloc(sizeof(char *));
976 *envp[0] = NULL;
977 *envsizep = 1;
978 }
979
980 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000981 * Find the slot where the value should be stored. If the variable
982 * already exists, we reuse the slot; otherwise we append a new slot
983 * at the end of the array, expanding if necessary.
984 */
985 env = *envp;
986 namelen = strlen(name);
987 for (i = 0; env[i]; i++)
988 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
989 break;
990 if (env[i]) {
991 /* Reuse the slot. */
Darren Tuckera627d422013-06-02 07:31:17 +1000992 free(env[i]);
Damien Millerb38eff82000-04-01 11:09:21 +1000993 } else {
994 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000995 envsize = *envsizep;
996 if (i >= envsize - 1) {
997 if (envsize >= 1000)
998 fatal("child_set_env: too many env vars");
999 envsize += 50;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001000 env = (*envp) = xreallocarray(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +10001001 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +10001002 }
1003 /* Need to set the NULL pointer at end of array beyond the new slot. */
1004 env[i + 1] = NULL;
1005 }
1006
1007 /* Allocate space and format the variable in the appropriate slot. */
1008 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
1009 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
1010}
1011
1012/*
1013 * Reads environment variables from the given file and adds/overrides them
1014 * into the environment. If the file does not exist, this does nothing.
1015 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1016 * and assignments of the form name=value. No other forms are allowed.
1017 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001018static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001019read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001020 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +10001021{
1022 FILE *f;
1023 char buf[4096];
1024 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +10001025 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001026
1027 f = fopen(filename, "r");
1028 if (!f)
1029 return;
1030
1031 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +10001032 if (++lineno > 1000)
1033 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001034 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1035 ;
1036 if (!*cp || *cp == '#' || *cp == '\n')
1037 continue;
Damien Miller14b017d2007-09-17 16:09:15 +10001038
1039 cp[strcspn(cp, "\n")] = '\0';
1040
Damien Millerb38eff82000-04-01 11:09:21 +10001041 value = strchr(cp, '=');
1042 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +10001043 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1044 filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001045 continue;
1046 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001047 /*
1048 * Replace the equals sign by nul, and advance value to
1049 * the value string.
1050 */
Damien Millerb38eff82000-04-01 11:09:21 +10001051 *value = '\0';
1052 value++;
1053 child_set_env(env, envsize, cp, value);
1054 }
1055 fclose(f);
1056}
1057
Darren Tuckere1a790d2003-09-16 11:52:19 +10001058#ifdef HAVE_ETC_DEFAULT_LOGIN
1059/*
1060 * Return named variable from specified environment, or NULL if not present.
1061 */
1062static char *
1063child_get_env(char **env, const char *name)
1064{
1065 int i;
1066 size_t len;
1067
1068 len = strlen(name);
1069 for (i=0; env[i] != NULL; i++)
1070 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1071 return(env[i] + len + 1);
1072 return NULL;
1073}
1074
1075/*
1076 * Read /etc/default/login.
1077 * We pick up the PATH (or SUPATH for root) and UMASK.
1078 */
1079static void
1080read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1081{
1082 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001083 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +10001084 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +10001085
1086 /*
1087 * We don't want to copy the whole file to the child's environment,
1088 * so we use a temporary environment and copy the variables we're
1089 * interested in.
1090 */
1091 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1092
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001093 if (tmpenv == NULL)
1094 return;
1095
Darren Tuckere1a790d2003-09-16 11:52:19 +10001096 if (uid == 0)
1097 var = child_get_env(tmpenv, "SUPATH");
1098 else
1099 var = child_get_env(tmpenv, "PATH");
1100 if (var != NULL)
1101 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +11001102
Darren Tuckere1a790d2003-09-16 11:52:19 +10001103 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1104 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +10001105 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +11001106
Darren Tuckere1a790d2003-09-16 11:52:19 +10001107 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001108 free(tmpenv[i]);
1109 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +10001110}
1111#endif /* HAVE_ETC_DEFAULT_LOGIN */
1112
Damien Miller7dff8692005-05-26 11:34:51 +10001113void
1114copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +10001115{
Damien Millerbb9ffc12002-01-08 10:59:32 +11001116 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +10001117 int i;
1118
Damien Millerbb9ffc12002-01-08 10:59:32 +11001119 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001120 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001121
Damien Millerbb9ffc12002-01-08 10:59:32 +11001122 for(i = 0; source[i] != NULL; i++) {
1123 var_name = xstrdup(source[i]);
1124 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001125 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001126 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001127 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001128 *var_val++ = '\0';
1129
1130 debug3("Copy environment: %s=%s", var_name, var_val);
1131 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001132
Darren Tuckerf60845f2013-06-02 08:07:31 +10001133 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001134 }
1135}
Damien Millercb5e44a2000-09-29 12:12:36 +11001136
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001137static char **
1138do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001139{
Damien Millerb38eff82000-04-01 11:09:21 +10001140 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001141 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001142 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001143 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001144#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001145 char *path = NULL;
1146#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001147
Damien Millerb38eff82000-04-01 11:09:21 +10001148 /* Initialize the environment. */
1149 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001150 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001151 env[0] = NULL;
1152
Damien Millerbac2d8a2000-09-05 16:13:06 +11001153#ifdef HAVE_CYGWIN
1154 /*
1155 * The Windows environment contains some setting which are
1156 * important for a running system. They must not be dropped.
1157 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001158 {
1159 char **p;
1160
1161 p = fetch_windows_environment();
1162 copy_environment(p, &env, &envsize);
1163 free_windows_environment(p);
1164 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001165#endif
1166
Darren Tucker0efd1552003-08-26 11:49:55 +10001167#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001168 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001169 * the childs environment as they see fit
1170 */
1171 ssh_gssapi_do_child(&env, &envsize);
1172#endif
1173
Damien Millerb38eff82000-04-01 11:09:21 +10001174 if (!options.use_login) {
1175 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001176 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001177 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001178 s->env[i].val);
1179
Damien Millerb38eff82000-04-01 11:09:21 +10001180 child_set_env(&env, &envsize, "USER", pw->pw_name);
1181 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001182#ifdef _AIX
1183 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1184#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001185 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001186#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001187 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1188 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1189 else
1190 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001191#else /* HAVE_LOGIN_CAP */
1192# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001193 /*
1194 * There's no standard path on Windows. The path contains
1195 * important components pointing to the system directories,
1196 * needed for loading shared libraries. So the path better
1197 * remains intact here.
1198 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001199# ifdef HAVE_ETC_DEFAULT_LOGIN
1200 read_etc_default_login(&env, &envsize, pw->pw_uid);
1201 path = child_get_env(env, "PATH");
1202# endif /* HAVE_ETC_DEFAULT_LOGIN */
1203 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001204 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001205 s->pw->pw_uid == 0 ?
1206 SUPERUSER_PATH : _PATH_STDPATH);
1207 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001208# endif /* HAVE_CYGWIN */
1209#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001210
1211 snprintf(buf, sizeof buf, "%.200s/%.50s",
1212 _PATH_MAILDIR, pw->pw_name);
1213 child_set_env(&env, &envsize, "MAIL", buf);
1214
1215 /* Normal systems set SHELL by default. */
1216 child_set_env(&env, &envsize, "SHELL", shell);
1217 }
1218 if (getenv("TZ"))
1219 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1220
1221 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001222 if (!options.use_login) {
1223 while (custom_environment) {
1224 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001225 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001226
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001227 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001228 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001229 if (str[i] == '=') {
1230 str[i] = 0;
1231 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001232 }
1233 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +10001234 free(ce->s);
1235 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001236 }
Damien Millerb38eff82000-04-01 11:09:21 +10001237 }
1238
Damien Millerf37e2462002-09-19 11:47:55 +10001239 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001240 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001241 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001242 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1243
Damien Miller00111382003-03-10 11:21:17 +11001244 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001245 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001246 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
Darren Tuckera627d422013-06-02 07:31:17 +10001247 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001248 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1249
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001250 if (s->ttyfd != -1)
1251 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1252 if (s->term)
1253 child_set_env(&env, &envsize, "TERM", s->term);
1254 if (s->display)
1255 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001256 if (original_command)
1257 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1258 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001259
Tim Rice81ed5182002-09-25 17:38:46 -07001260#ifdef _UNICOS
1261 if (cray_tmpdir[0] != '\0')
1262 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1263#endif /* _UNICOS */
1264
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001265 /*
1266 * Since we clear KRB5CCNAME at startup, if it's set now then it
1267 * must have been set by a native authentication method (eg AIX or
1268 * SIA), so copy it to the child.
1269 */
1270 {
1271 char *cp;
1272
1273 if ((cp = getenv("KRB5CCNAME")) != NULL)
1274 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1275 }
1276
Damien Millerb38eff82000-04-01 11:09:21 +10001277#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001278 {
1279 char *cp;
1280
1281 if ((cp = getenv("AUTHSTATE")) != NULL)
1282 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001283 read_environment_file(&env, &envsize, "/etc/environment");
1284 }
Damien Millerb38eff82000-04-01 11:09:21 +10001285#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001286#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001287 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001288 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001289 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001290#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001291#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001292 /*
1293 * Pull in any environment variables that may have
1294 * been set by PAM.
1295 */
Damien Miller4e448a32003-05-14 15:11:48 +10001296 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001297 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001298
Damien Millerc756e9b2003-11-17 21:41:42 +11001299 p = fetch_pam_child_environment();
1300 copy_environment(p, &env, &envsize);
1301 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001302
Damien Millerc756e9b2003-11-17 21:41:42 +11001303 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001304 copy_environment(p, &env, &envsize);
1305 free_pam_environment(p);
1306 }
Damien Millerb38eff82000-04-01 11:09:21 +10001307#endif /* USE_PAM */
1308
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001309 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001310 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001311 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001312
1313 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001314 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001315 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001316 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001317 read_environment_file(&env, &envsize, buf);
1318 }
1319 if (debug_flag) {
1320 /* dump the environment */
1321 fprintf(stderr, "Environment:\n");
1322 for (i = 0; env[i]; i++)
1323 fprintf(stderr, " %.200s\n", env[i]);
1324 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001325 return env;
1326}
1327
1328/*
1329 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1330 * first in this order).
1331 */
1332static void
1333do_rc_files(Session *s, const char *shell)
1334{
1335 FILE *f = NULL;
1336 char cmd[1024];
1337 int do_xauth;
1338 struct stat st;
1339
1340 do_xauth =
1341 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1342
Damien Millera1b48cc2008-03-27 11:02:02 +11001343 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001344 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001345 !no_user_rc && options.permit_user_rc &&
1346 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001347 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1348 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1349 if (debug_flag)
1350 fprintf(stderr, "Running %s\n", cmd);
1351 f = popen(cmd, "w");
1352 if (f) {
1353 if (do_xauth)
1354 fprintf(f, "%s %s\n", s->auth_proto,
1355 s->auth_data);
1356 pclose(f);
1357 } else
1358 fprintf(stderr, "Could not run %s\n",
1359 _PATH_SSH_USER_RC);
1360 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1361 if (debug_flag)
1362 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1363 _PATH_SSH_SYSTEM_RC);
1364 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1365 if (f) {
1366 if (do_xauth)
1367 fprintf(f, "%s %s\n", s->auth_proto,
1368 s->auth_data);
1369 pclose(f);
1370 } else
1371 fprintf(stderr, "Could not run %s\n",
1372 _PATH_SSH_SYSTEM_RC);
1373 } else if (do_xauth && options.xauth_location != NULL) {
1374 /* Add authority data to .Xauthority if appropriate. */
1375 if (debug_flag) {
1376 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001377 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001378 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001379 fprintf(stderr,
1380 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001381 options.xauth_location, s->auth_display,
1382 s->auth_proto, s->auth_data);
1383 }
1384 snprintf(cmd, sizeof cmd, "%s -q -",
1385 options.xauth_location);
1386 f = popen(cmd, "w");
1387 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001388 fprintf(f, "remove %s\n",
1389 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001390 fprintf(f, "add %s %s %s\n",
1391 s->auth_display, s->auth_proto,
1392 s->auth_data);
1393 pclose(f);
1394 } else {
1395 fprintf(stderr, "Could not run %s\n",
1396 cmd);
1397 }
1398 }
1399}
1400
1401static void
1402do_nologin(struct passwd *pw)
1403{
1404 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001405 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1406 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001407
1408#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001409 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001410 return;
1411 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001412#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001413 if (pw->pw_uid == 0)
1414 return;
1415 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001416#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001417 if (stat(nl, &sb) == -1) {
1418 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001419 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001420 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001421 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001422
1423 /* /etc/nologin exists. Print its contents if we can and exit. */
1424 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1425 if ((f = fopen(nl, "r")) != NULL) {
1426 while (fgets(buf, sizeof(buf), f))
1427 fputs(buf, stderr);
1428 fclose(f);
1429 }
1430 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001431}
1432
Damien Millerd8cb1f12008-02-10 22:40:12 +11001433/*
1434 * Chroot into a directory after checking it for safety: all path components
1435 * must be root-owned directories with strict permissions.
1436 */
1437static void
1438safely_chroot(const char *path, uid_t uid)
1439{
1440 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001441 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001442 struct stat st;
1443
1444 if (*path != '/')
1445 fatal("chroot path does not begin at root");
1446 if (strlen(path) >= sizeof(component))
1447 fatal("chroot path too long");
1448
1449 /*
1450 * Descend the path, checking that each component is a
1451 * root-owned directory with strict permissions.
1452 */
1453 for (cp = path; cp != NULL;) {
1454 if ((cp = strchr(cp, '/')) == NULL)
1455 strlcpy(component, path, sizeof(component));
1456 else {
1457 cp++;
1458 memcpy(component, path, cp - path);
1459 component[cp - path] = '\0';
1460 }
1461
1462 debug3("%s: checking '%s'", __func__, component);
1463
1464 if (stat(component, &st) != 0)
1465 fatal("%s: stat(\"%s\"): %s", __func__,
1466 component, strerror(errno));
1467 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1468 fatal("bad ownership or modes for chroot "
1469 "directory %s\"%s\"",
1470 cp == NULL ? "" : "component ", component);
1471 if (!S_ISDIR(st.st_mode))
1472 fatal("chroot path %s\"%s\" is not a directory",
1473 cp == NULL ? "" : "component ", component);
1474
1475 }
1476
1477 if (chdir(path) == -1)
1478 fatal("Unable to chdir to chroot path \"%s\": "
1479 "%s", path, strerror(errno));
1480 if (chroot(path) == -1)
1481 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1482 if (chdir("/") == -1)
1483 fatal("%s: chdir(/) after chroot: %s",
1484 __func__, strerror(errno));
1485 verbose("Changed root directory to \"%s\"", path);
1486}
1487
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001488/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001489void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001490do_setusercontext(struct passwd *pw)
1491{
Damien Miller54e37732008-02-10 22:48:55 +11001492 char *chroot_path, *tmp;
Tim Ricea261b8d2014-06-18 16:17:28 -07001493#ifdef USE_LIBIAF
1494 int doing_chroot = 0;
1495#endif
Damien Miller54e37732008-02-10 22:48:55 +11001496
Darren Tucker97528352010-11-05 12:03:05 +11001497 platform_setusercontext(pw);
1498
Darren Tuckerb12fe272010-11-05 14:47:01 +11001499 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001500#ifdef HAVE_LOGIN_CAP
1501 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001502 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001503 perror("unable to set user context");
1504 exit(1);
1505 }
1506#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001507 if (setlogin(pw->pw_name) < 0)
1508 error("setlogin failed: %s", strerror(errno));
1509 if (setgid(pw->pw_gid) < 0) {
1510 perror("setgid");
1511 exit(1);
1512 }
1513 /* Initialize the group list. */
1514 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1515 perror("initgroups");
1516 exit(1);
1517 }
1518 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001519#endif
1520
Darren Tucker920612e2010-11-05 12:36:15 +11001521 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001522
Damien Millerd8cb1f12008-02-10 22:40:12 +11001523 if (options.chroot_directory != NULL &&
1524 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001525 tmp = tilde_expand_filename(options.chroot_directory,
1526 pw->pw_uid);
1527 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1528 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001529 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001530 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001531 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001532 /* Make sure we don't attempt to chroot again */
1533 free(options.chroot_directory);
1534 options.chroot_directory = NULL;
Tim Ricea261b8d2014-06-18 16:17:28 -07001535#ifdef USE_LIBIAF
1536 doing_chroot = 1;
1537#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001538 }
1539
1540#ifdef HAVE_LOGIN_CAP
1541 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1542 perror("unable to set user context (setuser)");
1543 exit(1);
1544 }
Damien Miller58528402013-03-15 11:22:37 +11001545 /*
1546 * FreeBSD's setusercontext() will not apply the user's
1547 * own umask setting unless running with the user's UID.
1548 */
1549 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001550#else
Tim Rice9464ba62014-01-20 17:59:28 -08001551# ifdef USE_LIBIAF
Tim Ricea261b8d2014-06-18 16:17:28 -07001552/* In a chroot environment, the set_id() will always fail; typically
1553 * because of the lack of necessary authentication services and runtime
1554 * such as ./usr/lib/libiaf.so, ./usr/lib/libpam.so.1, and ./etc/passwd
1555 * We skip it in the internal sftp chroot case.
1556 * We'll lose auditing and ACLs but permanently_set_uid will
1557 * take care of the rest.
1558 */
1559 if ((doing_chroot == 0) && set_id(pw->pw_name) != 0) {
Tim Rice617da332014-01-22 19:16:10 -08001560 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001561 }
1562# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001563 /* Permanently switch to the desired uid. */
1564 permanently_set_uid(pw);
1565#endif
Damien Millera56086b2013-04-23 15:24:18 +10001566 } else if (options.chroot_directory != NULL &&
1567 strcasecmp(options.chroot_directory, "none") != 0) {
1568 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001569 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001570
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001571 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1572 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1573}
1574
Ben Lindstrom08105192002-03-22 02:50:06 +00001575static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001576do_pwchange(Session *s)
1577{
Darren Tucker09991742004-07-17 17:05:14 +10001578 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001579 fprintf(stderr, "WARNING: Your password has expired.\n");
1580 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001581 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001582 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001583#ifdef WITH_SELINUX
1584 setexeccon(NULL);
1585#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001586#ifdef PASSWD_NEEDS_USERNAME
1587 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1588 (char *)NULL);
1589#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001590 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001591#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001592 perror("passwd");
1593 } else {
1594 fprintf(stderr,
1595 "Password change required but no TTY available.\n");
1596 }
1597 exit(1);
1598}
1599
1600static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001601launch_login(struct passwd *pw, const char *hostname)
1602{
1603 /* Launch login(1). */
1604
Ben Lindstrom378a4172002-06-07 14:49:56 +00001605 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001606#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001607 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001608#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001609#ifdef LOGIN_NO_ENDOPT
1610 "-p", "-f", pw->pw_name, (char *)NULL);
1611#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001612 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001613#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614
1615 /* Login couldn't be executed, die. */
1616
1617 perror("login");
1618 exit(1);
1619}
1620
Darren Tucker23bc8d02004-02-06 16:24:31 +11001621static void
1622child_close_fds(void)
1623{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001624 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001625
djm@openbsd.org141efe42015-01-14 20:05:27 +00001626 if (auth_sock != -1) {
1627 close(auth_sock);
1628 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001629 }
1630
Darren Tucker23bc8d02004-02-06 16:24:31 +11001631 if (packet_get_connection_in() == packet_get_connection_out())
1632 close(packet_get_connection_in());
1633 else {
1634 close(packet_get_connection_in());
1635 close(packet_get_connection_out());
1636 }
1637 /*
1638 * Close all descriptors related to channels. They will still remain
1639 * open in the parent.
1640 */
1641 /* XXX better use close-on-exec? -markus */
1642 channel_close_all();
1643
1644 /*
1645 * Close any extra file descriptors. Note that there may still be
1646 * descriptors left by system functions. They will be closed later.
1647 */
1648 endpwent();
1649
1650 /*
Damien Miller788f2122005-11-05 15:14:59 +11001651 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001652 * hanging around in clients. Note that we want to do this after
1653 * initgroups, because at least on Solaris 2.3 it leaves file
1654 * descriptors open.
1655 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001656 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001657}
1658
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001659/*
1660 * Performs common processing for the child, such as setting up the
1661 * environment, closing extra file descriptors, setting the user and group
1662 * ids, and executing the command or shell.
1663 */
Damien Millerdfc24252008-02-10 22:29:40 +11001664#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001665void
1666do_child(Session *s, const char *command)
1667{
1668 extern char **environ;
1669 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001670 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001671 const char *shell, *shell0, *hostname = NULL;
1672 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001673 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001674
1675 /* remove hostkey from the child's memory */
1676 destroy_sensitive_data();
1677
Darren Tucker23bc8d02004-02-06 16:24:31 +11001678 /* Force a password change */
1679 if (s->authctxt->force_pwchange) {
1680 do_setusercontext(pw);
1681 child_close_fds();
1682 do_pwchange(s);
1683 exit(1);
1684 }
1685
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001686 /* login(1) is only called if we execute the login shell */
1687 if (options.use_login && command != NULL)
1688 options.use_login = 0;
1689
Tim Rice81ed5182002-09-25 17:38:46 -07001690#ifdef _UNICOS
1691 cray_setup(pw->pw_uid, pw->pw_name, command);
1692#endif /* _UNICOS */
1693
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001694 /*
1695 * Login(1) does this as well, and it needs uid 0 for the "-h"
1696 * switch, so we let login(1) to this for us.
1697 */
1698 if (!options.use_login) {
1699#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001700 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001701 if (!check_quietlogin(s, command))
1702 do_motd();
1703#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001704 /* When PAM is enabled we rely on it to do the nologin check */
1705 if (!options.use_pam)
1706 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001707 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001708 /*
1709 * PAM session modules in do_setusercontext may have
1710 * generated messages, so if this in an interactive
1711 * login then display them too.
1712 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001713 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001714 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001715#endif /* HAVE_OSF_SIA */
1716 }
1717
Darren Tucker69687f42004-09-11 22:17:26 +10001718#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001719 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1720 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001721 display_loginmsg();
1722 exit(254);
1723 }
1724#endif
1725
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001726 /*
1727 * Get the shell from the password data. An empty shell field is
1728 * legal, and means /bin/sh.
1729 */
1730 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001731
1732 /*
1733 * Make sure $SHELL points to the shell from the password file,
1734 * even if shell is overridden from login.conf
1735 */
1736 env = do_setup_env(s, shell);
1737
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001738#ifdef HAVE_LOGIN_CAP
1739 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1740#endif
1741
Damien Millerad833b32000-08-23 10:46:23 +10001742 /* we have to stash the hostname before we close our socket. */
1743 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001744 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001745 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001746 /*
1747 * Close the connection descriptors; note that this is the child, and
1748 * the server will still have the socket open, and it is important
1749 * that we do not shutdown it. Note that the descriptors cannot be
1750 * closed before building the environment, as we call
1751 * get_remote_ipaddr there.
1752 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001753 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001754
Damien Millerb38eff82000-04-01 11:09:21 +10001755 /*
Damien Miller05eda432002-02-10 18:32:28 +11001756 * Must take new environment into use so that .ssh/rc,
1757 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001758 */
1759 environ = env;
1760
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001761#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001762 /*
1763 * At this point, we check to see if AFS is active and if we have
1764 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1765 * if we can (and need to) extend the ticket into an AFS token. If
1766 * we don't do this, we run into potential problems if the user's
1767 * home directory is in AFS and it's not world-readable.
1768 */
1769
1770 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001771 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001772 char cell[64];
1773
1774 debug("Getting AFS token");
1775
1776 k_setpag();
1777
1778 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1779 krb5_afslog(s->authctxt->krb5_ctx,
1780 s->authctxt->krb5_fwd_ccache, cell, NULL);
1781
1782 krb5_afslog_home(s->authctxt->krb5_ctx,
1783 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1784 }
1785#endif
1786
Damien Miller788f2122005-11-05 15:14:59 +11001787 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001788 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001789 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001790#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001791 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001792#endif
Damien Miller7aa46ec2010-06-26 09:37:57 +10001793 if (r || options.chroot_directory == NULL ||
1794 strcasecmp(options.chroot_directory, "none") == 0)
Damien Miller6051c942008-06-16 07:53:16 +10001795 fprintf(stderr, "Could not chdir to home "
1796 "directory %s: %s\n", pw->pw_dir,
1797 strerror(errno));
1798 if (r)
1799 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001800 }
1801
Damien Millera1939002008-03-15 17:27:58 +11001802 closefrom(STDERR_FILENO + 1);
1803
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001804 if (!options.use_login)
1805 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001806
1807 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001808 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001809
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001810 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1811 printf("This service allows sftp connections only.\n");
1812 fflush(NULL);
1813 exit(1);
1814 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001815 extern int optind, optreset;
1816 int i;
1817 char *p, *args;
1818
Darren Tuckerac46a912009-06-21 17:55:23 +10001819 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001820 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001821 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1822 if (i < ARGV_MAX - 1)
1823 argv[i++] = p;
1824 argv[i] = NULL;
1825 optind = optreset = 1;
1826 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001827#ifdef WITH_SELINUX
1828 ssh_selinux_change_context("sftpd_t");
1829#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001830 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001831 }
1832
Darren Tucker695ed392009-10-07 09:02:18 +11001833 fflush(NULL);
1834
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001835 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001836 launch_login(pw, hostname);
1837 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001838 }
1839
1840 /* Get the last component of the shell name. */
1841 if ((shell0 = strrchr(shell, '/')) != NULL)
1842 shell0++;
1843 else
1844 shell0 = shell;
1845
Damien Millerb38eff82000-04-01 11:09:21 +10001846 /*
1847 * If we have no command, execute the shell. In this case, the shell
1848 * name to be passed in argv[0] is preceded by '-' to indicate that
1849 * this is a login shell.
1850 */
1851 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001852 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001853
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001854 /* Start the shell. Set initial character to '-'. */
1855 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001856
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001857 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1858 >= sizeof(argv0) - 1) {
1859 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001860 perror(shell);
1861 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001862 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001863
1864 /* Execute the shell. */
1865 argv[0] = argv0;
1866 argv[1] = NULL;
1867 execve(shell, argv, env);
1868
1869 /* Executing the shell failed. */
1870 perror(shell);
1871 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001872 }
1873 /*
1874 * Execute the command using the user's shell. This uses the -c
1875 * option to execute the command.
1876 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001877 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001878 argv[1] = "-c";
1879 argv[2] = (char *) command;
1880 argv[3] = NULL;
1881 execve(shell, argv, env);
1882 perror(shell);
1883 exit(1);
1884}
1885
Damien Miller7207f642008-05-19 15:34:50 +10001886void
1887session_unused(int id)
1888{
1889 debug3("%s: session id %d unused", __func__, id);
1890 if (id >= options.max_sessions ||
1891 id >= sessions_nalloc) {
1892 fatal("%s: insane session id %d (max %d nalloc %d)",
1893 __func__, id, options.max_sessions, sessions_nalloc);
1894 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001895 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001896 sessions[id].self = id;
1897 sessions[id].used = 0;
1898 sessions[id].chanid = -1;
1899 sessions[id].ptyfd = -1;
1900 sessions[id].ttyfd = -1;
1901 sessions[id].ptymaster = -1;
1902 sessions[id].x11_chanids = NULL;
1903 sessions[id].next_unused = sessions_first_unused;
1904 sessions_first_unused = id;
1905}
1906
Damien Millerb38eff82000-04-01 11:09:21 +10001907Session *
1908session_new(void)
1909{
Damien Miller7207f642008-05-19 15:34:50 +10001910 Session *s, *tmp;
1911
1912 if (sessions_first_unused == -1) {
1913 if (sessions_nalloc >= options.max_sessions)
1914 return NULL;
1915 debug2("%s: allocate (allocated %d max %d)",
1916 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001917 tmp = xreallocarray(sessions, sessions_nalloc + 1,
Damien Miller7207f642008-05-19 15:34:50 +10001918 sizeof(*sessions));
1919 if (tmp == NULL) {
1920 error("%s: cannot allocate %d sessions",
1921 __func__, sessions_nalloc + 1);
1922 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001923 }
Damien Miller7207f642008-05-19 15:34:50 +10001924 sessions = tmp;
1925 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001926 }
Damien Miller7207f642008-05-19 15:34:50 +10001927
1928 if (sessions_first_unused >= sessions_nalloc ||
1929 sessions_first_unused < 0) {
1930 fatal("%s: insane first_unused %d max %d nalloc %d",
1931 __func__, sessions_first_unused, options.max_sessions,
1932 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001933 }
Damien Miller7207f642008-05-19 15:34:50 +10001934
1935 s = &sessions[sessions_first_unused];
1936 if (s->used) {
1937 fatal("%s: session %d already used",
1938 __func__, sessions_first_unused);
1939 }
1940 sessions_first_unused = s->next_unused;
1941 s->used = 1;
1942 s->next_unused = -1;
1943 debug("session_new: session %d", s->self);
1944
1945 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001946}
1947
Ben Lindstrombba81212001-06-25 05:01:22 +00001948static void
Damien Millerb38eff82000-04-01 11:09:21 +10001949session_dump(void)
1950{
1951 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001952 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001953 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001954
1955 debug("dump: used %d next_unused %d session %d %p "
1956 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001957 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001958 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001959 s->self,
1960 s,
1961 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001962 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001963 }
1964}
1965
Damien Millerefb4afe2000-04-12 18:45:05 +10001966int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001967session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001968{
1969 Session *s = session_new();
1970 debug("session_open: channel %d", chanid);
1971 if (s == NULL) {
1972 error("no more sessions");
1973 return 0;
1974 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001975 s->authctxt = authctxt;
1976 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001977 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001978 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001979 debug("session_open: session %d: link with channel %d", s->self, chanid);
1980 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001981 return 1;
1982}
1983
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001984Session *
1985session_by_tty(char *tty)
1986{
1987 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001988 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001989 Session *s = &sessions[i];
1990 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1991 debug("session_by_tty: session %d tty %s", i, tty);
1992 return s;
1993 }
1994 }
1995 debug("session_by_tty: unknown tty %.100s", tty);
1996 session_dump();
1997 return NULL;
1998}
1999
Ben Lindstrombba81212001-06-25 05:01:22 +00002000static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002001session_by_channel(int id)
2002{
2003 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002004 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002005 Session *s = &sessions[i];
2006 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10002007 debug("session_by_channel: session %d channel %d",
2008 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002009 return s;
2010 }
2011 }
2012 debug("session_by_channel: unknown channel %d", id);
2013 session_dump();
2014 return NULL;
2015}
2016
Ben Lindstrombba81212001-06-25 05:01:22 +00002017static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10002018session_by_x11_channel(int id)
2019{
2020 int i, j;
2021
Damien Miller7207f642008-05-19 15:34:50 +10002022 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002023 Session *s = &sessions[i];
2024
2025 if (s->x11_chanids == NULL || !s->used)
2026 continue;
2027 for (j = 0; s->x11_chanids[j] != -1; j++) {
2028 if (s->x11_chanids[j] == id) {
2029 debug("session_by_x11_channel: session %d "
2030 "channel %d", s->self, id);
2031 return s;
2032 }
2033 }
2034 }
2035 debug("session_by_x11_channel: unknown channel %d", id);
2036 session_dump();
2037 return NULL;
2038}
2039
2040static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002041session_by_pid(pid_t pid)
2042{
2043 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00002044 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10002045 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002046 Session *s = &sessions[i];
2047 if (s->used && s->pid == pid)
2048 return s;
2049 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002050 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002051 session_dump();
2052 return NULL;
2053}
2054
Ben Lindstrombba81212001-06-25 05:01:22 +00002055static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002056session_window_change_req(Session *s)
2057{
2058 s->col = packet_get_int();
2059 s->row = packet_get_int();
2060 s->xpixel = packet_get_int();
2061 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002062 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10002063 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2064 return 1;
2065}
2066
Ben Lindstrombba81212001-06-25 05:01:22 +00002067static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002068session_pty_req(Session *s)
2069{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002070 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00002071 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002072
Damien Miller5ff30c62013-10-30 22:21:50 +11002073 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00002074 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10002075 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002076 }
2077 if (s->ttyfd != -1) {
2078 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10002079 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002080 }
2081
Damien Millerefb4afe2000-04-12 18:45:05 +10002082 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002083
2084 if (compat20) {
2085 s->col = packet_get_int();
2086 s->row = packet_get_int();
2087 } else {
2088 s->row = packet_get_int();
2089 s->col = packet_get_int();
2090 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002091 s->xpixel = packet_get_int();
2092 s->ypixel = packet_get_int();
2093
2094 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002095 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002096 s->term = NULL;
2097 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00002098
Damien Millerefb4afe2000-04-12 18:45:05 +10002099 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00002100 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10002101 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2102 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10002103 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002104 s->term = NULL;
2105 s->ptyfd = -1;
2106 s->ttyfd = -1;
2107 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10002108 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002109 }
2110 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002111
2112 /* for SSH1 the tty modes length is not given */
2113 if (!compat20)
2114 n_bytes = packet_remaining();
2115 tty_parse_modes(s->ttyfd, &n_bytes);
2116
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002117 if (!use_privsep)
2118 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002119
2120 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10002121 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2122
Damien Miller48b03fc2002-01-22 23:11:40 +11002123 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10002124 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002125 return 1;
2126}
2127
Ben Lindstrombba81212001-06-25 05:01:22 +00002128static int
Damien Millerbd483e72000-04-30 10:00:53 +10002129session_subsystem_req(Session *s)
2130{
Damien Millerae452462001-10-10 15:08:06 +10002131 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002132 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10002133 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11002134 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10002135 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10002136
Damien Miller71df7522013-10-15 12:12:02 +11002137 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002138 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11002139 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10002140 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10002141
Damien Millerf6d9e222000-06-18 14:50:44 +10002142 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11002143 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10002144 prog = options.subsystem_command[i];
2145 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002146 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11002147 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002148 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11002149 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002150 if (stat(prog, &st) < 0)
2151 debug("subsystem: cannot stat %s: %s",
2152 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11002153 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002154 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10002155 }
Damien Miller7207f642008-05-19 15:34:50 +10002156 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11002157 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10002158 }
2159 }
2160
2161 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11002162 logit("subsystem request for %.100s by user %s failed, "
2163 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10002164
Damien Millerbd483e72000-04-30 10:00:53 +10002165 return success;
2166}
2167
Ben Lindstrombba81212001-06-25 05:01:22 +00002168static int
Damien Millerbd483e72000-04-30 10:00:53 +10002169session_x11_req(Session *s)
2170{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002171 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002172
Damien Miller2b9b0452005-07-17 17:19:24 +10002173 if (s->auth_proto != NULL || s->auth_data != NULL) {
2174 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002175 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002176 return 0;
2177 }
Damien Millerbd483e72000-04-30 10:00:53 +10002178 s->single_connection = packet_get_char();
2179 s->auth_proto = packet_get_string(NULL);
2180 s->auth_data = packet_get_string(NULL);
2181 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002182 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002183
Ben Lindstrom768176b2001-06-09 01:29:12 +00002184 success = session_setup_x11fwd(s);
2185 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002186 free(s->auth_proto);
2187 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002188 s->auth_proto = NULL;
2189 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002190 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002191 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002192}
2193
Ben Lindstrombba81212001-06-25 05:01:22 +00002194static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002195session_shell_req(Session *s)
2196{
Damien Miller48b03fc2002-01-22 23:11:40 +11002197 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002198 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002199}
2200
Ben Lindstrombba81212001-06-25 05:01:22 +00002201static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002202session_exec_req(Session *s)
2203{
Damien Miller7207f642008-05-19 15:34:50 +10002204 u_int len, success;
2205
Damien Millerf6d9e222000-06-18 14:50:44 +10002206 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002207 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002208 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002209 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002210 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002211}
2212
Ben Lindstrombba81212001-06-25 05:01:22 +00002213static int
Damien Miller54c45982003-05-15 10:20:13 +10002214session_break_req(Session *s)
2215{
Damien Miller54c45982003-05-15 10:20:13 +10002216
Darren Tucker1f8311c2004-05-13 16:39:33 +10002217 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002218 packet_check_eom();
2219
Darren Tucker9c5d5532011-11-04 10:55:24 +11002220 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002221 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002222 return 1;
2223}
2224
2225static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002226session_env_req(Session *s)
2227{
2228 char *name, *val;
2229 u_int name_len, val_len, i;
2230
Damien Miller8569eba2014-03-04 09:35:17 +11002231 name = packet_get_cstring(&name_len);
2232 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002233 packet_check_eom();
2234
2235 /* Don't set too many environment variables */
2236 if (s->num_env > 128) {
2237 debug2("Ignoring env request %s: too many env vars", name);
2238 goto fail;
2239 }
2240
2241 for (i = 0; i < options.num_accept_env; i++) {
2242 if (match_pattern(name, options.accept_env[i])) {
2243 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002244 s->env = xreallocarray(s->env, s->num_env + 1,
Damien Miller36812092006-03-26 14:22:47 +11002245 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002246 s->env[s->num_env].name = name;
2247 s->env[s->num_env].val = val;
2248 s->num_env++;
2249 return (1);
2250 }
2251 }
2252 debug2("Ignoring env request %s: disallowed name", name);
2253
2254 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002255 free(name);
2256 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002257 return (0);
2258}
2259
2260static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002261session_auth_agent_req(Session *s)
2262{
2263 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002264 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002265 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002266 debug("session_auth_agent_req: no_agent_forwarding_flag");
2267 return 0;
2268 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002269 if (called) {
2270 return 0;
2271 } else {
2272 called = 1;
2273 return auth_input_request_forwarding(s->pw);
2274 }
2275}
2276
Damien Millerc7ef63d2002-02-05 12:21:42 +11002277int
2278session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002279{
Damien Millerefb4afe2000-04-12 18:45:05 +10002280 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002281 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002282
Damien Millerc7ef63d2002-02-05 12:21:42 +11002283 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002284 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002285 c->self, rtype);
2286 return 0;
2287 }
2288 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002289
2290 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002291 * a session is in LARVAL state until a shell, a command
2292 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002293 */
2294 if (c->type == SSH_CHANNEL_LARVAL) {
2295 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002296 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002297 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002298 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002299 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002300 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002301 } else if (strcmp(rtype, "x11-req") == 0) {
2302 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002303 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2304 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002305 } else if (strcmp(rtype, "subsystem") == 0) {
2306 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002307 } else if (strcmp(rtype, "env") == 0) {
2308 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002309 }
2310 }
2311 if (strcmp(rtype, "window-change") == 0) {
2312 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002313 } else if (strcmp(rtype, "break") == 0) {
2314 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002315 }
Damien Millera6b1d162004-06-30 22:41:07 +10002316
Damien Millerc7ef63d2002-02-05 12:21:42 +11002317 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002318}
2319
2320void
Damien Miller8853ca52010-06-26 10:00:14 +10002321session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2322 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002323{
2324 if (!compat20)
2325 fatal("session_set_fds: called for proto != 2.0");
2326 /*
2327 * now that have a child and a pipe to the child,
2328 * we can activate our channel and register the fd's
2329 */
2330 if (s->chanid == -1)
2331 fatal("no channel for session %d", s->self);
2332 channel_set_fds(s->chanid,
2333 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002334 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002335 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002336}
2337
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002338/*
2339 * Function to perform pty cleanup. Also called if we get aborted abnormally
2340 * (e.g., due to a dropped connection).
2341 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002342void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002343session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002344{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002345 if (s == NULL) {
2346 error("session_pty_cleanup: no session");
2347 return;
2348 }
2349 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002350 return;
2351
Kevin Steves43cdef32001-02-11 14:12:08 +00002352 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002353
Damien Millerb38eff82000-04-01 11:09:21 +10002354 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002355 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002356 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002357
2358 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002359 if (getuid() == 0)
2360 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002361
2362 /*
2363 * Close the server side of the socket pairs. We must do this after
2364 * the pty cleanup, so that another process doesn't get this pty
2365 * while we're still cleaning up.
2366 */
Damien Miller7207f642008-05-19 15:34:50 +10002367 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2368 error("close(s->ptymaster/%d): %s",
2369 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002370
2371 /* unlink pty from session */
2372 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002373}
Damien Millerefb4afe2000-04-12 18:45:05 +10002374
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002375void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002376session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002377{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002378 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002379}
2380
Damien Miller5a80bba2002-09-04 16:39:02 +10002381static char *
2382sig2name(int sig)
2383{
2384#define SSH_SIG(x) if (sig == SIG ## x) return #x
2385 SSH_SIG(ABRT);
2386 SSH_SIG(ALRM);
2387 SSH_SIG(FPE);
2388 SSH_SIG(HUP);
2389 SSH_SIG(ILL);
2390 SSH_SIG(INT);
2391 SSH_SIG(KILL);
2392 SSH_SIG(PIPE);
2393 SSH_SIG(QUIT);
2394 SSH_SIG(SEGV);
2395 SSH_SIG(TERM);
2396 SSH_SIG(USR1);
2397 SSH_SIG(USR2);
2398#undef SSH_SIG
2399 return "SIG@openssh.com";
2400}
2401
Ben Lindstrombba81212001-06-25 05:01:22 +00002402static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002403session_close_x11(int id)
2404{
2405 Channel *c;
2406
Damien Millerd47c62a2005-12-13 19:33:57 +11002407 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002408 debug("session_close_x11: x11 channel %d missing", id);
2409 } else {
2410 /* Detach X11 listener */
2411 debug("session_close_x11: detach x11 channel %d", id);
2412 channel_cancel_cleanup(id);
2413 if (c->ostate != CHAN_OUTPUT_CLOSED)
2414 chan_mark_dead(c);
2415 }
2416}
2417
2418static void
2419session_close_single_x11(int id, void *arg)
2420{
2421 Session *s;
2422 u_int i;
2423
2424 debug3("session_close_single_x11: channel %d", id);
2425 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002426 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002427 fatal("session_close_single_x11: no x11 channel %d", id);
2428 for (i = 0; s->x11_chanids[i] != -1; i++) {
2429 debug("session_close_single_x11: session %d: "
2430 "closing channel %d", s->self, s->x11_chanids[i]);
2431 /*
2432 * The channel "id" is already closing, but make sure we
2433 * close all of its siblings.
2434 */
2435 if (s->x11_chanids[i] != id)
2436 session_close_x11(s->x11_chanids[i]);
2437 }
Darren Tuckera627d422013-06-02 07:31:17 +10002438 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002439 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002440 free(s->display);
2441 s->display = NULL;
2442 free(s->auth_proto);
2443 s->auth_proto = NULL;
2444 free(s->auth_data);
2445 s->auth_data = NULL;
2446 free(s->auth_display);
2447 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002448}
2449
2450static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002451session_exit_message(Session *s, int status)
2452{
2453 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002454
2455 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002456 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002457 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002458 debug("session_exit_message: session %d channel %d pid %ld",
2459 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002460
2461 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002462 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002463 packet_put_int(WEXITSTATUS(status));
2464 packet_send();
2465 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002466 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002467 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002468#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002469 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002470#else /* WCOREDUMP */
2471 packet_put_char(0);
2472#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002473 packet_put_cstring("");
2474 packet_put_cstring("");
2475 packet_send();
2476 } else {
2477 /* Some weird exit cause. Just exit. */
2478 packet_disconnect("wait returned status %04x.", status);
2479 }
2480
2481 /* disconnect channel */
2482 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002483
2484 /*
2485 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002486 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002487 * by session_close_by_channel when the childs close their fds.
2488 */
2489 channel_register_cleanup(c->self, session_close_by_channel, 1);
2490
Damien Miller166fca82000-04-20 07:42:21 +10002491 /*
2492 * emulate a write failure with 'chan_write_failed', nobody will be
2493 * interested in data we write.
2494 * Note that we must not call 'chan_read_failed', since there could
2495 * be some more data waiting in the pipe.
2496 */
Damien Millerbd483e72000-04-30 10:00:53 +10002497 if (c->ostate != CHAN_OUTPUT_CLOSED)
2498 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002499}
2500
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002501void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002502session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002503{
Damien Millereccb9de2005-06-17 12:59:34 +10002504 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002505
Ben Lindstromce0f6342002-06-11 16:42:49 +00002506 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002507 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002508 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002509 free(s->term);
2510 free(s->display);
2511 free(s->x11_chanids);
2512 free(s->auth_display);
2513 free(s->auth_data);
2514 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002515 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002516 if (s->env != NULL) {
2517 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002518 free(s->env[i].name);
2519 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002520 }
Darren Tuckera627d422013-06-02 07:31:17 +10002521 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002522 }
Damien Millere247cc42000-05-07 12:03:14 +10002523 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002524 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002525}
2526
2527void
2528session_close_by_pid(pid_t pid, int status)
2529{
2530 Session *s = session_by_pid(pid);
2531 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002532 debug("session_close_by_pid: no session for pid %ld",
2533 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002534 return;
2535 }
2536 if (s->chanid != -1)
2537 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002538 if (s->ttyfd != -1)
2539 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002540 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002541}
2542
2543/*
2544 * this is called when a channel dies before
2545 * the session 'child' itself dies
2546 */
2547void
2548session_close_by_channel(int id, void *arg)
2549{
2550 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002551 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002552
Damien Millerefb4afe2000-04-12 18:45:05 +10002553 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002554 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002555 return;
2556 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002557 debug("session_close_by_channel: channel %d child %ld",
2558 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002559 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002560 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002561 /*
2562 * delay detach of session, but release pty, since
2563 * the fd's to the child are already closed
2564 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002565 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002566 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002567 return;
2568 }
2569 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002570 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002571
2572 /* Close any X11 listeners associated with this session */
2573 if (s->x11_chanids != NULL) {
2574 for (i = 0; s->x11_chanids[i] != -1; i++) {
2575 session_close_x11(s->x11_chanids[i]);
2576 s->x11_chanids[i] = -1;
2577 }
2578 }
2579
Damien Millerefb4afe2000-04-12 18:45:05 +10002580 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002581 session_close(s);
2582}
2583
2584void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002585session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002586{
2587 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002588 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002589 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002590 if (s->used) {
2591 if (closefunc != NULL)
2592 closefunc(s);
2593 else
2594 session_close(s);
2595 }
Damien Miller52b77be2001-10-10 15:14:37 +10002596 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002597}
2598
Ben Lindstrombba81212001-06-25 05:01:22 +00002599static char *
Damien Millere247cc42000-05-07 12:03:14 +10002600session_tty_list(void)
2601{
2602 static char buf[1024];
2603 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002604 char *cp;
2605
Damien Millere247cc42000-05-07 12:03:14 +10002606 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002607 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002608 Session *s = &sessions[i];
2609 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002610
Damien Millera8ed44b2003-01-10 09:53:12 +11002611 if (strncmp(s->tty, "/dev/", 5) != 0) {
2612 cp = strrchr(s->tty, '/');
2613 cp = (cp == NULL) ? s->tty : cp + 1;
2614 } else
2615 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002616
Damien Millere247cc42000-05-07 12:03:14 +10002617 if (buf[0] != '\0')
2618 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002619 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002620 }
2621 }
2622 if (buf[0] == '\0')
2623 strlcpy(buf, "notty", sizeof buf);
2624 return buf;
2625}
2626
2627void
2628session_proctitle(Session *s)
2629{
2630 if (s->pw == NULL)
2631 error("no user for session %d", s->self);
2632 else
2633 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2634}
2635
Ben Lindstrom768176b2001-06-09 01:29:12 +00002636int
2637session_setup_x11fwd(Session *s)
2638{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002639 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002640 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002641 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002642 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002643
2644 if (no_x11_forwarding_flag) {
2645 packet_send_debug("X11 forwarding disabled in user configuration file.");
2646 return 0;
2647 }
2648 if (!options.x11_forwarding) {
2649 debug("X11 forwarding disabled in server configuration file.");
2650 return 0;
2651 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002652 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002653 (stat(options.xauth_location, &st) == -1)) {
2654 packet_send_debug("No xauth program; cannot forward with spoofing.");
2655 return 0;
2656 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002657 if (options.use_login) {
2658 packet_send_debug("X11 forwarding disabled; "
2659 "not compatible with UseLogin=yes.");
2660 return 0;
2661 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002662 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002663 debug("X11 display already set.");
2664 return 0;
2665 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002666 if (x11_create_display_inet(options.x11_display_offset,
2667 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002668 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002669 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002670 return 0;
2671 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002672 for (i = 0; s->x11_chanids[i] != -1; i++) {
2673 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002674 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002675 }
Kevin Steves366298c2001-12-19 17:58:01 +00002676
2677 /* Set up a suitable value for the DISPLAY variable. */
2678 if (gethostname(hostname, sizeof(hostname)) < 0)
2679 fatal("gethostname: %.100s", strerror(errno));
2680 /*
2681 * auth_display must be used as the displayname when the
2682 * authorization entry is added with xauth(1). This will be
2683 * different than the DISPLAY string for localhost displays.
2684 */
Damien Miller95c249f2002-02-05 12:11:34 +11002685 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002686 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002687 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002688 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002689 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002690 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002691 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002692 } else {
2693#ifdef IPADDR_IN_DISPLAY
2694 struct hostent *he;
2695 struct in_addr my_addr;
2696
2697 he = gethostbyname(hostname);
2698 if (he == NULL) {
2699 error("Can't get IP address for X11 DISPLAY.");
2700 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2701 return 0;
2702 }
2703 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002704 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002705 s->display_number, s->screen);
2706#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002707 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002708 s->display_number, s->screen);
2709#endif
2710 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002711 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002712 }
2713
Ben Lindstrom768176b2001-06-09 01:29:12 +00002714 return 1;
2715}
2716
Ben Lindstrombba81212001-06-25 05:01:22 +00002717static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002718do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002719{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002720 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002721}
2722
2723void
2724do_cleanup(Authctxt *authctxt)
2725{
2726 static int called = 0;
2727
2728 debug("do_cleanup");
2729
2730 /* no cleanup if we're in the child for login shell */
2731 if (is_child)
2732 return;
2733
2734 /* avoid double cleanup */
2735 if (called)
2736 return;
2737 called = 1;
2738
Darren Tucker9142e1c2007-08-16 23:28:04 +10002739 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002740 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002741
2742#ifdef USE_PAM
2743 if (options.use_pam) {
2744 sshpam_cleanup();
2745 sshpam_thread_cleanup();
2746 }
2747#endif
2748
2749 if (!authctxt->authenticated)
2750 return;
2751
Darren Tucker3e33cec2003-10-02 16:12:36 +10002752#ifdef KRB5
2753 if (options.kerberos_ticket_cleanup &&
2754 authctxt->krb5_ctx)
2755 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002756#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002757
2758#ifdef GSSAPI
2759 if (compat20 && options.gss_cleanup_creds)
2760 ssh_gssapi_cleanup_creds();
2761#endif
2762
2763 /* remove agent socket */
2764 auth_sock_cleanup_proc(authctxt->pw);
2765
2766 /*
2767 * Cleanup ptys/utmp only if privsep is disabled,
2768 * or if running in monitor.
2769 */
2770 if (!use_privsep || mm_is_monitor())
2771 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002772}