blob: e12f6677e16b8715f221a097b8c4f47085850cb2 [file] [log] [blame]
djm@openbsd.org161cf412014-12-22 07:55:51 +00001/* $OpenBSD: session.c,v 1.275 2014/12/22 07:55:51 djm Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
Darren Tucker39972492006-07-12 22:22:46 +100049#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100050#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100051#include <grp.h>
Damien Millere5c0d522014-07-03 21:24:19 +100052#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110053#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110054#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110055#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100056#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110057#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100058#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100059#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100060#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100061#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100062#include <unistd.h>
Damien Millerb38eff82000-04-01 11:09:21 +100063
Damien Millerb84886b2008-05-19 15:05:07 +100064#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100066#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "ssh1.h"
68#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000069#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100070#include "packet.h"
71#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100072#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100073#include "uidswap.h"
74#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000075#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100076#include "key.h"
77#include "cipher.h"
78#ifdef GSSAPI
79#include "ssh-gss.h"
80#endif
81#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100082#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100083#include "auth-options.h"
Damien Miller85b45e02013-07-20 13:21:52 +100084#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "pathnames.h"
86#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100087#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000088#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000089#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090#include "serverloop.h"
91#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000092#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100093#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000094#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110095#include "sftp.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100096
Darren Tucker3c78c5e2004-01-23 22:03:10 +110097#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110098#include <kafs.h>
99#endif
100
Damien Miller14684a12011-05-20 11:23:07 +1000101#ifdef WITH_SELINUX
102#include <selinux/selinux.h>
103#endif
104
Damien Millerad793d52008-11-03 19:17:57 +1100105#define IS_INTERNAL_SFTP(c) \
106 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
107 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
108 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
109 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
110
Damien Millerb38eff82000-04-01 11:09:21 +1000111/* func */
112
113Session *session_new(void);
Damien Miller8853ca52010-06-26 10:00:14 +1000114void session_set_fds(Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000115void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000116void session_proctitle(Session *);
117int session_setup_x11fwd(Session *);
Damien Miller7207f642008-05-19 15:34:50 +1000118int do_exec_pty(Session *, const char *);
119int do_exec_no_pty(Session *, const char *);
120int do_exec(Session *, const char *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000121void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000122#ifdef LOGIN_NEEDS_UTMPX
123static void do_pre_login(Session *s);
124#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000125void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000126void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000127int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000128
Ben Lindstrombba81212001-06-25 05:01:22 +0000129static void do_authenticated1(Authctxt *);
130static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000131
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000133
Damien Millerb38eff82000-04-01 11:09:21 +1000134/* import */
135extern ServerOptions options;
136extern char *__progname;
137extern int log_stderr;
138extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000139extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000140extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000141extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000142extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000143
Damien Miller7b28dc52000-09-05 13:34:53 +1100144/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000145const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000148static int sessions_first_unused = -1;
149static int sessions_nalloc = 0;
150static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100151
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100152#define SUBSYSTEM_NONE 0
153#define SUBSYSTEM_EXT 1
154#define SUBSYSTEM_INT_SFTP 2
155#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100156
Damien Millerad833b32000-08-23 10:46:23 +1000157#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000158login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000159#endif
160
Darren Tucker3e33cec2003-10-02 16:12:36 +1000161static int is_child = 0;
162
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000163/* Name and directory of socket for authentication agent forwarding. */
164static char *auth_sock_name = NULL;
165static char *auth_sock_dir = NULL;
166
167/* removes the agent forwarding socket */
168
169static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000170auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000171{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000172 if (auth_sock_name != NULL) {
173 temporarily_use_uid(pw);
174 unlink(auth_sock_name);
175 rmdir(auth_sock_dir);
176 auth_sock_name = NULL;
177 restore_uid();
178 }
179}
180
181static int
182auth_input_request_forwarding(struct passwd * pw)
183{
184 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000185 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000186
187 if (auth_sock_name != NULL) {
188 error("authentication forwarding requested twice.");
189 return 0;
190 }
191
192 /* Temporarily drop privileged uid for mkdir/bind. */
193 temporarily_use_uid(pw);
194
195 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000196 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000197
198 /* Create private directory for socket */
199 if (mkdtemp(auth_sock_dir) == NULL) {
200 packet_send_debug("Agent forwarding disabled: "
201 "mkdtemp() failed: %.100s", strerror(errno));
202 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000203 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000204 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000205 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000206 }
Damien Miller7207f642008-05-19 15:34:50 +1000207
208 xasprintf(&auth_sock_name, "%s/agent.%ld",
209 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000210
Damien Miller7acefbb2014-07-18 14:11:24 +1000211 /* Start a Unix listener on auth_sock_name. */
212 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000213
214 /* Restore the privileged uid. */
215 restore_uid();
216
Damien Miller7acefbb2014-07-18 14:11:24 +1000217 /* Check for socket/bind/listen failure. */
218 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000219 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000220
221 /* Allocate a channel for the authentication agent socket. */
222 nc = channel_new("auth socket",
223 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
224 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000225 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100226 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000227 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000228
229 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000230 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000231 if (auth_sock_dir != NULL) {
232 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000233 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000234 }
235 if (sock != -1)
236 close(sock);
237 auth_sock_name = NULL;
238 auth_sock_dir = NULL;
239 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000240}
241
Darren Tucker1921ed92004-02-10 13:23:28 +1100242static void
243display_loginmsg(void)
244{
Damien Miller46d38de2005-07-17 17:02:09 +1000245 if (buffer_len(&loginmsg) > 0) {
246 buffer_append(&loginmsg, "\0", 1);
247 printf("%s", (char *)buffer_ptr(&loginmsg));
248 buffer_clear(&loginmsg);
249 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100250}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000251
Ben Lindstromb31783d2001-03-22 02:02:12 +0000252void
253do_authenticated(Authctxt *authctxt)
254{
Damien Miller97f39ae2003-02-24 11:57:01 +1100255 setproctitle("%s", authctxt->pw->pw_name);
256
Ben Lindstromb31783d2001-03-22 02:02:12 +0000257 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000258 /* XXX - streamlocal? */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100259 if (no_port_forwarding_flag ||
260 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
261 channel_disable_adm_local_opens();
262 else
Ben Lindstromb31783d2001-03-22 02:02:12 +0000263 channel_permit_all_opens();
264
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100265 auth_debug_send();
266
Ben Lindstromb31783d2001-03-22 02:02:12 +0000267 if (compat20)
268 do_authenticated2(authctxt);
269 else
270 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000271
Darren Tucker3e33cec2003-10-02 16:12:36 +1000272 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000273}
274
Damien Millerb38eff82000-04-01 11:09:21 +1000275/*
Damien Millerb38eff82000-04-01 11:09:21 +1000276 * Prepares for an interactive session. This is called after the user has
277 * been successfully authenticated. During this message exchange, pseudo
278 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
279 * are requested, etc.
280 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000281static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000282do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000283{
284 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000285 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100286 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000287 int enable_compression_after_reply = 0;
288 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000289
290 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100291 if (s == NULL) {
292 error("no more sessions");
293 return;
294 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000295 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000296 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000297
Damien Millerb38eff82000-04-01 11:09:21 +1000298 /*
299 * We stay in this loop until the client requests to execute a shell
300 * or a command.
301 */
302 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000303 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000304
305 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100306 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000307
308 /* Process the packet. */
309 switch (type) {
310 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000311 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100312 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000313 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000314 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100315 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000316 break;
317 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000318 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000319 debug2("compression disabled");
320 break;
321 }
Damien Millerb38eff82000-04-01 11:09:21 +1000322 /* Enable compression after we have responded with SUCCESS. */
323 enable_compression_after_reply = 1;
324 success = 1;
325 break;
326
327 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000328 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000329 break;
330
331 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000332 s->auth_proto = packet_get_string(&proto_len);
333 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000334
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000335 screen_flag = packet_get_protocol_flags() &
336 SSH_PROTOFLAG_SCREEN_NUMBER;
337 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
338
339 if (packet_remaining() == 4) {
340 if (!screen_flag)
341 debug2("Buggy client: "
342 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000343 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000344 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000345 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000346 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100347 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000348 success = session_setup_x11fwd(s);
349 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000350 free(s->auth_proto);
351 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000352 s->auth_proto = NULL;
353 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000354 }
Damien Millerb38eff82000-04-01 11:09:21 +1000355 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000356
357 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
Damien Miller4f755cd2008-05-19 14:57:41 +1000358 if (!options.allow_agent_forwarding ||
359 no_agent_forwarding_flag || compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000360 debug("Authentication agent forwarding not permitted for this authentication.");
361 break;
362 }
363 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000364 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000365 break;
366
367 case SSH_CMSG_PORT_FORWARD_REQUEST:
368 if (no_port_forwarding_flag) {
369 debug("Port forwarding not permitted for this authentication.");
370 break;
371 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100372 if (!(options.allow_tcp_forwarding & FORWARD_REMOTE)) {
Damien Miller50a41ed2000-10-16 12:14:42 +1100373 debug("Port forwarding not permitted.");
374 break;
375 }
Damien Millerb38eff82000-04-01 11:09:21 +1000376 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000377 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
Damien Miller7acefbb2014-07-18 14:11:24 +1000378 &options.fwd_opts) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000379 debug("Port forwarding failed.");
380 break;
381 }
Damien Millerb38eff82000-04-01 11:09:21 +1000382 success = 1;
383 break;
384
385 case SSH_CMSG_MAX_PACKET_SIZE:
386 if (packet_set_maxsize(packet_get_int()) > 0)
387 success = 1;
388 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100389
Damien Millerb38eff82000-04-01 11:09:21 +1000390 case SSH_CMSG_EXEC_SHELL:
391 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000392 if (type == SSH_CMSG_EXEC_CMD) {
393 command = packet_get_string(&dlen);
394 debug("Exec command '%.500s'", command);
Damien Miller7207f642008-05-19 15:34:50 +1000395 if (do_exec(s, command) != 0)
396 packet_disconnect(
397 "command execution failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000398 free(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000399 } else {
Damien Miller7207f642008-05-19 15:34:50 +1000400 if (do_exec(s, NULL) != 0)
401 packet_disconnect(
402 "shell execution failed");
Damien Millerb38eff82000-04-01 11:09:21 +1000403 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100404 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000405 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000406 return;
407
408 default:
409 /*
410 * Any unknown messages in this phase are ignored,
411 * and a failure message is returned.
412 */
Damien Miller996acd22003-04-09 20:59:48 +1000413 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000414 }
415 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
416 packet_send();
417 packet_write_wait();
418
419 /* Enable compression now that we have replied if appropriate. */
420 if (enable_compression_after_reply) {
421 enable_compression_after_reply = 0;
422 packet_start_compression(compression_level);
423 }
424 }
425}
426
Darren Tucker293ee3c2014-01-19 15:28:01 +1100427#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000428/*
429 * This is called to fork and execute a command when we have no tty. This
430 * will call do_child from the child, and server_loop from the parent after
431 * setting up file descriptors and such.
432 */
Damien Miller7207f642008-05-19 15:34:50 +1000433int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000434do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000435{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000436 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000437
438#ifdef USE_PIPES
439 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000440
Damien Miller22a29882010-05-10 11:53:54 +1000441 if (s == NULL)
442 fatal("do_exec_no_pty: no session");
443
Damien Millerb38eff82000-04-01 11:09:21 +1000444 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000445 if (pipe(pin) < 0) {
446 error("%s: pipe in: %.100s", __func__, strerror(errno));
447 return -1;
448 }
449 if (pipe(pout) < 0) {
450 error("%s: pipe out: %.100s", __func__, strerror(errno));
451 close(pin[0]);
452 close(pin[1]);
453 return -1;
454 }
Damien Miller8853ca52010-06-26 10:00:14 +1000455 if (pipe(perr) < 0) {
456 error("%s: pipe err: %.100s", __func__,
457 strerror(errno));
458 close(pin[0]);
459 close(pin[1]);
460 close(pout[0]);
461 close(pout[1]);
462 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000463 }
464#else
Damien Millerb38eff82000-04-01 11:09:21 +1000465 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000466
Damien Miller22a29882010-05-10 11:53:54 +1000467 if (s == NULL)
468 fatal("do_exec_no_pty: no session");
469
Damien Millerb38eff82000-04-01 11:09:21 +1000470 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000471 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
472 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
473 return -1;
474 }
Damien Miller8853ca52010-06-26 10:00:14 +1000475 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
476 error("%s: socketpair #2: %.100s", __func__,
477 strerror(errno));
478 close(inout[0]);
479 close(inout[1]);
480 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000481 }
482#endif
483
Damien Millere247cc42000-05-07 12:03:14 +1000484 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000485
Damien Millerb38eff82000-04-01 11:09:21 +1000486 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000487 switch ((pid = fork())) {
488 case -1:
489 error("%s: fork: %.100s", __func__, strerror(errno));
490#ifdef USE_PIPES
491 close(pin[0]);
492 close(pin[1]);
493 close(pout[0]);
494 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000495 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000496 close(perr[1]);
497#else
498 close(inout[0]);
499 close(inout[1]);
500 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000501 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000502#endif
503 return -1;
504 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000505 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000506
Damien Millerb38eff82000-04-01 11:09:21 +1000507 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000508 log_init(__progname, options.log_level,
509 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000510
511 /*
512 * Create a new session and process group since the 4.4BSD
513 * setlogin() affects the entire process group.
514 */
515 if (setsid() < 0)
516 error("setsid failed: %.100s", strerror(errno));
517
518#ifdef USE_PIPES
519 /*
520 * Redirect stdin. We close the parent side of the socket
521 * pair, and make the child side the standard input.
522 */
523 close(pin[1]);
524 if (dup2(pin[0], 0) < 0)
525 perror("dup2 stdin");
526 close(pin[0]);
527
528 /* Redirect stdout. */
529 close(pout[0]);
530 if (dup2(pout[1], 1) < 0)
531 perror("dup2 stdout");
532 close(pout[1]);
533
534 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000535 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000536 if (dup2(perr[1], 2) < 0)
537 perror("dup2 stderr");
538 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000539#else
Damien Millerb38eff82000-04-01 11:09:21 +1000540 /*
541 * Redirect stdin, stdout, and stderr. Stdin and stdout will
542 * use the same socket, as some programs (particularly rdist)
543 * seem to depend on it.
544 */
545 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000546 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000547 if (dup2(inout[0], 0) < 0) /* stdin */
548 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000549 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000550 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000551 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000552 if (dup2(err[0], 2) < 0) /* stderr */
553 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000554 close(err[0]);
555#endif
556
Damien Millerb38eff82000-04-01 11:09:21 +1000557
Tim Rice81ed5182002-09-25 17:38:46 -0700558#ifdef _UNICOS
559 cray_init_job(s->pw); /* set up cray jid and tmpdir */
560#endif
561
Damien Millerb38eff82000-04-01 11:09:21 +1000562 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000563 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000564 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000565 default:
566 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000567 }
Damien Miller7207f642008-05-19 15:34:50 +1000568
Tim Rice81ed5182002-09-25 17:38:46 -0700569#ifdef _UNICOS
570 signal(WJSIGNAL, cray_job_termination_handler);
571#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100572#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100573 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100574#endif
Damien Miller7207f642008-05-19 15:34:50 +1000575
Damien Millerb38eff82000-04-01 11:09:21 +1000576 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000577 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100578 packet_set_interactive(s->display != NULL,
579 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000580
581 /*
582 * Clear loginmsg, since it's the child's responsibility to display
583 * it to the user, otherwise multiple sessions may accumulate
584 * multiple copies of the login messages.
585 */
586 buffer_clear(&loginmsg);
587
Damien Millerb38eff82000-04-01 11:09:21 +1000588#ifdef USE_PIPES
589 /* We are the parent. Close the child sides of the pipes. */
590 close(pin[0]);
591 close(pout[1]);
592 close(perr[1]);
593
Damien Millerefb4afe2000-04-12 18:45:05 +1000594 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000595 session_set_fds(s, pin[1], pout[0], perr[0],
596 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000597 } else {
598 /* Enter the interactive session. */
599 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000600 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000601 }
Damien Miller7207f642008-05-19 15:34:50 +1000602#else
Damien Millerb38eff82000-04-01 11:09:21 +1000603 /* We are the parent. Close the child sides of the socket pairs. */
604 close(inout[0]);
605 close(err[0]);
606
607 /*
608 * Enter the interactive session. Note: server_loop must be able to
609 * handle the case that fdin and fdout are the same.
610 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000611 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000612 session_set_fds(s, inout[1], inout[1], err[1],
613 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000614 } else {
615 server_loop(pid, inout[1], inout[1], err[1]);
616 /* server_loop has closed inout[1] and err[1]. */
617 }
Damien Miller7207f642008-05-19 15:34:50 +1000618#endif
619 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000620}
621
622/*
623 * This is called to fork and execute a command when we have a tty. This
624 * will call do_child from the child, and server_loop from the parent after
625 * setting up file descriptors, controlling tty, updating wtmp, utmp,
626 * lastlog, and other such operations.
627 */
Damien Miller7207f642008-05-19 15:34:50 +1000628int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000629do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000630{
Damien Millerb38eff82000-04-01 11:09:21 +1000631 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000632 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000633
634 if (s == NULL)
635 fatal("do_exec_pty: no session");
636 ptyfd = s->ptyfd;
637 ttyfd = s->ttyfd;
638
Damien Miller7207f642008-05-19 15:34:50 +1000639 /*
640 * Create another descriptor of the pty master side for use as the
641 * standard input. We could use the original descriptor, but this
642 * simplifies code in server_loop. The descriptor is bidirectional.
643 * Do this before forking (and cleanup in the child) so as to
644 * detect and gracefully fail out-of-fd conditions.
645 */
646 if ((fdout = dup(ptyfd)) < 0) {
647 error("%s: dup #1: %s", __func__, strerror(errno));
648 close(ttyfd);
649 close(ptyfd);
650 return -1;
651 }
652 /* we keep a reference to the pty master */
653 if ((ptymaster = dup(ptyfd)) < 0) {
654 error("%s: dup #2: %s", __func__, strerror(errno));
655 close(ttyfd);
656 close(ptyfd);
657 close(fdout);
658 return -1;
659 }
660
Damien Millerb38eff82000-04-01 11:09:21 +1000661 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000662 switch ((pid = fork())) {
663 case -1:
664 error("%s: fork: %.100s", __func__, strerror(errno));
665 close(fdout);
666 close(ptymaster);
667 close(ttyfd);
668 close(ptyfd);
669 return -1;
670 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000671 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000672
Damien Miller7207f642008-05-19 15:34:50 +1000673 close(fdout);
674 close(ptymaster);
675
Damien Miller942da032000-08-18 13:59:06 +1000676 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000677 log_init(__progname, options.log_level,
678 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000679 /* Close the master side of the pseudo tty. */
680 close(ptyfd);
681
682 /* Make the pseudo tty our controlling tty. */
683 pty_make_controlling_tty(&ttyfd, s->tty);
684
Damien Miller9c751422001-10-10 15:02:46 +1000685 /* Redirect stdin/stdout/stderr from the pseudo tty. */
686 if (dup2(ttyfd, 0) < 0)
687 error("dup2 stdin: %s", strerror(errno));
688 if (dup2(ttyfd, 1) < 0)
689 error("dup2 stdout: %s", strerror(errno));
690 if (dup2(ttyfd, 2) < 0)
691 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000692
693 /* Close the extra descriptor for the pseudo tty. */
694 close(ttyfd);
695
Damien Miller942da032000-08-18 13:59:06 +1000696 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000697#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700698 if (!(options.use_login && command == NULL)) {
699#ifdef _UNICOS
700 cray_init_job(s->pw); /* set up cray jid and tmpdir */
701#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100702 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700703 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000704# ifdef LOGIN_NEEDS_UTMPX
705 else
706 do_pre_login(s);
707# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000708#endif
Damien Miller7207f642008-05-19 15:34:50 +1000709 /*
710 * Do common processing for the child, such as execing
711 * the command.
712 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000713 do_child(s, command);
714 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000715 default:
716 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000717 }
Damien Miller7207f642008-05-19 15:34:50 +1000718
Tim Rice81ed5182002-09-25 17:38:46 -0700719#ifdef _UNICOS
720 signal(WJSIGNAL, cray_job_termination_handler);
721#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100722#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100723 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100724#endif
Damien Miller7207f642008-05-19 15:34:50 +1000725
Damien Millerb38eff82000-04-01 11:09:21 +1000726 s->pid = pid;
727
728 /* Parent. Close the slave side of the pseudo tty. */
729 close(ttyfd);
730
Damien Millerb38eff82000-04-01 11:09:21 +1000731 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000732 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100733 packet_set_interactive(1,
734 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Millerefb4afe2000-04-12 18:45:05 +1000735 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000736 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000737 } else {
738 server_loop(pid, ptyfd, fdout, -1);
739 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000740 }
Damien Miller7207f642008-05-19 15:34:50 +1000741 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000742}
743
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000744#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000745static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000746do_pre_login(Session *s)
747{
748 socklen_t fromlen;
749 struct sockaddr_storage from;
750 pid_t pid = getpid();
751
752 /*
753 * Get IP address of client. If the connection is not a socket, let
754 * the address be 0.0.0.0.
755 */
756 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000757 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000758 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000759 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100760 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000761 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000762 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000763 }
764 }
765
766 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000767 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000768 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000769}
770#endif
771
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000772/*
773 * This is called to fork and execute a command. If another command is
774 * to be forced, execute that instead.
775 */
Damien Miller7207f642008-05-19 15:34:50 +1000776int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000777do_exec(Session *s, const char *command)
778{
Damien Miller7207f642008-05-19 15:34:50 +1000779 int ret;
Damien Miller71df7522013-10-15 12:12:02 +1100780 const char *forced = NULL;
781 char session_type[1024], *tty = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000782
Damien Millere2754432006-07-24 14:06:47 +1000783 if (options.adm_forced_command) {
784 original_command = command;
785 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100786 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000787 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000788 original_command = command;
789 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100790 forced = "(key-option)";
791 }
792 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100793 if (IS_INTERNAL_SFTP(command)) {
794 s->is_subsystem = s->is_subsystem ?
795 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
796 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100797 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100798 snprintf(session_type, sizeof(session_type),
799 "forced-command %s '%.900s'", forced, command);
800 } else if (s->is_subsystem) {
801 snprintf(session_type, sizeof(session_type),
802 "subsystem '%.900s'", s->subsys);
803 } else if (command == NULL) {
804 snprintf(session_type, sizeof(session_type), "shell");
805 } else {
806 /* NB. we don't log unforced commands to preserve privacy */
807 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000808 }
809
Damien Miller71df7522013-10-15 12:12:02 +1100810 if (s->ttyfd != -1) {
811 tty = s->tty;
812 if (strncmp(tty, "/dev/", 5) == 0)
813 tty += 5;
814 }
815
816 verbose("Starting session: %s%s%s for %s from %.200s port %d",
817 session_type,
818 tty == NULL ? "" : " on ",
819 tty == NULL ? "" : tty,
820 s->pw->pw_name,
821 get_remote_ipaddr(),
822 get_remote_port());
823
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100824#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100825 if (command != NULL)
826 PRIVSEP(audit_run_command(command));
827 else if (s->ttyfd == -1) {
828 char *shell = s->pw->pw_shell;
829
830 if (shell[0] == '\0') /* empty shell means /bin/sh */
831 shell =_PATH_BSHELL;
832 PRIVSEP(audit_run_command(shell));
833 }
834#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000835 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000836 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000837 else
Damien Miller7207f642008-05-19 15:34:50 +1000838 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000839
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000840 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000841
Darren Tucker09991742004-07-17 17:05:14 +1000842 /*
843 * Clear loginmsg: it's the child's responsibility to display
844 * it to the user, otherwise multiple sessions may accumulate
845 * multiple copies of the login messages.
846 */
847 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000848
849 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000850}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000851
Damien Miller942da032000-08-18 13:59:06 +1000852/* administrative, login(1)-like work */
853void
Damien Miller69b69aa2000-10-28 14:19:58 +1100854do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000855{
Damien Miller942da032000-08-18 13:59:06 +1000856 socklen_t fromlen;
857 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000858 struct passwd * pw = s->pw;
859 pid_t pid = getpid();
860
861 /*
862 * Get IP address of client. If the connection is not a socket, let
863 * the address be 0.0.0.0.
864 */
865 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000866 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000867 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000868 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000869 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000870 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000871 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000872 }
873 }
874
875 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000876 if (!use_privsep)
877 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
878 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000879 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000880 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000881
Kevin Steves092f2ef2000-10-14 13:36:13 +0000882#ifdef USE_PAM
883 /*
884 * If password change is needed, do it now.
885 * This needs to occur before the ~/.hushlogin check.
886 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100887 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
888 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000889 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100890 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000891 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000892 }
893#endif
894
Damien Millercf205e82001-04-16 18:29:15 +1000895 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000896 return;
897
Darren Tucker1921ed92004-02-10 13:23:28 +1100898 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000899
Damien Millercf205e82001-04-16 18:29:15 +1000900 do_motd();
901}
902
903/*
904 * Display the message of the day.
905 */
906void
907do_motd(void)
908{
909 FILE *f;
910 char buf[256];
911
Damien Miller942da032000-08-18 13:59:06 +1000912 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000913#ifdef HAVE_LOGIN_CAP
914 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
915 "/etc/motd"), "r");
916#else
Damien Miller942da032000-08-18 13:59:06 +1000917 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000918#endif
Damien Miller942da032000-08-18 13:59:06 +1000919 if (f) {
920 while (fgets(buf, sizeof(buf), f))
921 fputs(buf, stdout);
922 fclose(f);
923 }
924 }
925}
926
Kevin Steves8f63caa2001-07-04 18:23:02 +0000927
928/*
929 * Check for quiet login, either .hushlogin or command given.
930 */
931int
932check_quietlogin(Session *s, const char *command)
933{
934 char buf[256];
935 struct passwd *pw = s->pw;
936 struct stat st;
937
938 /* Return 1 if .hushlogin exists or a command given. */
939 if (command != NULL)
940 return 1;
941 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
942#ifdef HAVE_LOGIN_CAP
943 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
944 return 1;
945#else
946 if (stat(buf, &st) >= 0)
947 return 1;
948#endif
949 return 0;
950}
951
Damien Millerb38eff82000-04-01 11:09:21 +1000952/*
953 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100954 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000955 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000956void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000957child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100958 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000959{
Damien Millerb38eff82000-04-01 11:09:21 +1000960 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000961 u_int envsize;
962 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000963
Damien Miller8569eba2014-03-04 09:35:17 +1100964 if (strchr(name, '=') != NULL) {
965 error("Invalid environment variable \"%.100s\"", name);
966 return;
967 }
968
Damien Millerb38eff82000-04-01 11:09:21 +1000969 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000970 * If we're passed an uninitialized list, allocate a single null
971 * entry before continuing.
972 */
973 if (*envp == NULL && *envsizep == 0) {
974 *envp = xmalloc(sizeof(char *));
975 *envp[0] = NULL;
976 *envsizep = 1;
977 }
978
979 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000980 * Find the slot where the value should be stored. If the variable
981 * already exists, we reuse the slot; otherwise we append a new slot
982 * at the end of the array, expanding if necessary.
983 */
984 env = *envp;
985 namelen = strlen(name);
986 for (i = 0; env[i]; i++)
987 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
988 break;
989 if (env[i]) {
990 /* Reuse the slot. */
Darren Tuckera627d422013-06-02 07:31:17 +1000991 free(env[i]);
Damien Millerb38eff82000-04-01 11:09:21 +1000992 } else {
993 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000994 envsize = *envsizep;
995 if (i >= envsize - 1) {
996 if (envsize >= 1000)
997 fatal("child_set_env: too many env vars");
998 envsize += 50;
Damien Miller36812092006-03-26 14:22:47 +1100999 env = (*envp) = xrealloc(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +10001000 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +10001001 }
1002 /* Need to set the NULL pointer at end of array beyond the new slot. */
1003 env[i + 1] = NULL;
1004 }
1005
1006 /* Allocate space and format the variable in the appropriate slot. */
1007 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
1008 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
1009}
1010
1011/*
1012 * Reads environment variables from the given file and adds/overrides them
1013 * into the environment. If the file does not exist, this does nothing.
1014 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1015 * and assignments of the form name=value. No other forms are allowed.
1016 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001017static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001018read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001019 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +10001020{
1021 FILE *f;
1022 char buf[4096];
1023 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +10001024 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001025
1026 f = fopen(filename, "r");
1027 if (!f)
1028 return;
1029
1030 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +10001031 if (++lineno > 1000)
1032 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001033 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1034 ;
1035 if (!*cp || *cp == '#' || *cp == '\n')
1036 continue;
Damien Miller14b017d2007-09-17 16:09:15 +10001037
1038 cp[strcspn(cp, "\n")] = '\0';
1039
Damien Millerb38eff82000-04-01 11:09:21 +10001040 value = strchr(cp, '=');
1041 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +10001042 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1043 filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001044 continue;
1045 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001046 /*
1047 * Replace the equals sign by nul, and advance value to
1048 * the value string.
1049 */
Damien Millerb38eff82000-04-01 11:09:21 +10001050 *value = '\0';
1051 value++;
1052 child_set_env(env, envsize, cp, value);
1053 }
1054 fclose(f);
1055}
1056
Darren Tuckere1a790d2003-09-16 11:52:19 +10001057#ifdef HAVE_ETC_DEFAULT_LOGIN
1058/*
1059 * Return named variable from specified environment, or NULL if not present.
1060 */
1061static char *
1062child_get_env(char **env, const char *name)
1063{
1064 int i;
1065 size_t len;
1066
1067 len = strlen(name);
1068 for (i=0; env[i] != NULL; i++)
1069 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1070 return(env[i] + len + 1);
1071 return NULL;
1072}
1073
1074/*
1075 * Read /etc/default/login.
1076 * We pick up the PATH (or SUPATH for root) and UMASK.
1077 */
1078static void
1079read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1080{
1081 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001082 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +10001083 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +10001084
1085 /*
1086 * We don't want to copy the whole file to the child's environment,
1087 * so we use a temporary environment and copy the variables we're
1088 * interested in.
1089 */
1090 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1091
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001092 if (tmpenv == NULL)
1093 return;
1094
Darren Tuckere1a790d2003-09-16 11:52:19 +10001095 if (uid == 0)
1096 var = child_get_env(tmpenv, "SUPATH");
1097 else
1098 var = child_get_env(tmpenv, "PATH");
1099 if (var != NULL)
1100 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +11001101
Darren Tuckere1a790d2003-09-16 11:52:19 +10001102 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1103 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +10001104 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +11001105
Darren Tuckere1a790d2003-09-16 11:52:19 +10001106 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001107 free(tmpenv[i]);
1108 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +10001109}
1110#endif /* HAVE_ETC_DEFAULT_LOGIN */
1111
Damien Miller7dff8692005-05-26 11:34:51 +10001112void
1113copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +10001114{
Damien Millerbb9ffc12002-01-08 10:59:32 +11001115 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +10001116 int i;
1117
Damien Millerbb9ffc12002-01-08 10:59:32 +11001118 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001119 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001120
Damien Millerbb9ffc12002-01-08 10:59:32 +11001121 for(i = 0; source[i] != NULL; i++) {
1122 var_name = xstrdup(source[i]);
1123 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001124 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001125 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001126 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001127 *var_val++ = '\0';
1128
1129 debug3("Copy environment: %s=%s", var_name, var_val);
1130 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001131
Darren Tuckerf60845f2013-06-02 08:07:31 +10001132 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001133 }
1134}
Damien Millercb5e44a2000-09-29 12:12:36 +11001135
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001136static char **
1137do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001138{
Damien Millerb38eff82000-04-01 11:09:21 +10001139 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001140 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001141 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001142 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001143#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001144 char *path = NULL;
1145#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001146
Damien Millerb38eff82000-04-01 11:09:21 +10001147 /* Initialize the environment. */
1148 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001149 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001150 env[0] = NULL;
1151
Damien Millerbac2d8a2000-09-05 16:13:06 +11001152#ifdef HAVE_CYGWIN
1153 /*
1154 * The Windows environment contains some setting which are
1155 * important for a running system. They must not be dropped.
1156 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001157 {
1158 char **p;
1159
1160 p = fetch_windows_environment();
1161 copy_environment(p, &env, &envsize);
1162 free_windows_environment(p);
1163 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001164#endif
1165
Darren Tucker0efd1552003-08-26 11:49:55 +10001166#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001167 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001168 * the childs environment as they see fit
1169 */
1170 ssh_gssapi_do_child(&env, &envsize);
1171#endif
1172
Damien Millerb38eff82000-04-01 11:09:21 +10001173 if (!options.use_login) {
1174 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001175 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001176 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001177 s->env[i].val);
1178
Damien Millerb38eff82000-04-01 11:09:21 +10001179 child_set_env(&env, &envsize, "USER", pw->pw_name);
1180 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001181#ifdef _AIX
1182 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1183#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001184 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001185#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001186 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1187 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1188 else
1189 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001190#else /* HAVE_LOGIN_CAP */
1191# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001192 /*
1193 * There's no standard path on Windows. The path contains
1194 * important components pointing to the system directories,
1195 * needed for loading shared libraries. So the path better
1196 * remains intact here.
1197 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001198# ifdef HAVE_ETC_DEFAULT_LOGIN
1199 read_etc_default_login(&env, &envsize, pw->pw_uid);
1200 path = child_get_env(env, "PATH");
1201# endif /* HAVE_ETC_DEFAULT_LOGIN */
1202 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001203 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001204 s->pw->pw_uid == 0 ?
1205 SUPERUSER_PATH : _PATH_STDPATH);
1206 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001207# endif /* HAVE_CYGWIN */
1208#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001209
1210 snprintf(buf, sizeof buf, "%.200s/%.50s",
1211 _PATH_MAILDIR, pw->pw_name);
1212 child_set_env(&env, &envsize, "MAIL", buf);
1213
1214 /* Normal systems set SHELL by default. */
1215 child_set_env(&env, &envsize, "SHELL", shell);
1216 }
1217 if (getenv("TZ"))
1218 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1219
1220 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001221 if (!options.use_login) {
1222 while (custom_environment) {
1223 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001224 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001225
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001226 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001227 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001228 if (str[i] == '=') {
1229 str[i] = 0;
1230 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001231 }
1232 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +10001233 free(ce->s);
1234 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001235 }
Damien Millerb38eff82000-04-01 11:09:21 +10001236 }
1237
Damien Millerf37e2462002-09-19 11:47:55 +10001238 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001239 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001240 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001241 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1242
Damien Miller00111382003-03-10 11:21:17 +11001243 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001244 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001245 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
Darren Tuckera627d422013-06-02 07:31:17 +10001246 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001247 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1248
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001249 if (s->ttyfd != -1)
1250 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1251 if (s->term)
1252 child_set_env(&env, &envsize, "TERM", s->term);
1253 if (s->display)
1254 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001255 if (original_command)
1256 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1257 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001258
Tim Rice81ed5182002-09-25 17:38:46 -07001259#ifdef _UNICOS
1260 if (cray_tmpdir[0] != '\0')
1261 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1262#endif /* _UNICOS */
1263
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001264 /*
1265 * Since we clear KRB5CCNAME at startup, if it's set now then it
1266 * must have been set by a native authentication method (eg AIX or
1267 * SIA), so copy it to the child.
1268 */
1269 {
1270 char *cp;
1271
1272 if ((cp = getenv("KRB5CCNAME")) != NULL)
1273 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1274 }
1275
Damien Millerb38eff82000-04-01 11:09:21 +10001276#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001277 {
1278 char *cp;
1279
1280 if ((cp = getenv("AUTHSTATE")) != NULL)
1281 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001282 read_environment_file(&env, &envsize, "/etc/environment");
1283 }
Damien Millerb38eff82000-04-01 11:09:21 +10001284#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001285#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001286 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001287 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001288 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001289#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001290#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001291 /*
1292 * Pull in any environment variables that may have
1293 * been set by PAM.
1294 */
Damien Miller4e448a32003-05-14 15:11:48 +10001295 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001296 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001297
Damien Millerc756e9b2003-11-17 21:41:42 +11001298 p = fetch_pam_child_environment();
1299 copy_environment(p, &env, &envsize);
1300 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001301
Damien Millerc756e9b2003-11-17 21:41:42 +11001302 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001303 copy_environment(p, &env, &envsize);
1304 free_pam_environment(p);
1305 }
Damien Millerb38eff82000-04-01 11:09:21 +10001306#endif /* USE_PAM */
1307
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001308 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001309 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001310 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001311
1312 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001313 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001314 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001315 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001316 read_environment_file(&env, &envsize, buf);
1317 }
1318 if (debug_flag) {
1319 /* dump the environment */
1320 fprintf(stderr, "Environment:\n");
1321 for (i = 0; env[i]; i++)
1322 fprintf(stderr, " %.200s\n", env[i]);
1323 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001324 return env;
1325}
1326
1327/*
1328 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1329 * first in this order).
1330 */
1331static void
1332do_rc_files(Session *s, const char *shell)
1333{
1334 FILE *f = NULL;
1335 char cmd[1024];
1336 int do_xauth;
1337 struct stat st;
1338
1339 do_xauth =
1340 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1341
Damien Millera1b48cc2008-03-27 11:02:02 +11001342 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001343 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001344 !no_user_rc && options.permit_user_rc &&
1345 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001346 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1347 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1348 if (debug_flag)
1349 fprintf(stderr, "Running %s\n", cmd);
1350 f = popen(cmd, "w");
1351 if (f) {
1352 if (do_xauth)
1353 fprintf(f, "%s %s\n", s->auth_proto,
1354 s->auth_data);
1355 pclose(f);
1356 } else
1357 fprintf(stderr, "Could not run %s\n",
1358 _PATH_SSH_USER_RC);
1359 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1360 if (debug_flag)
1361 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1362 _PATH_SSH_SYSTEM_RC);
1363 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1364 if (f) {
1365 if (do_xauth)
1366 fprintf(f, "%s %s\n", s->auth_proto,
1367 s->auth_data);
1368 pclose(f);
1369 } else
1370 fprintf(stderr, "Could not run %s\n",
1371 _PATH_SSH_SYSTEM_RC);
1372 } else if (do_xauth && options.xauth_location != NULL) {
1373 /* Add authority data to .Xauthority if appropriate. */
1374 if (debug_flag) {
1375 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001376 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001377 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001378 fprintf(stderr,
1379 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001380 options.xauth_location, s->auth_display,
1381 s->auth_proto, s->auth_data);
1382 }
1383 snprintf(cmd, sizeof cmd, "%s -q -",
1384 options.xauth_location);
1385 f = popen(cmd, "w");
1386 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001387 fprintf(f, "remove %s\n",
1388 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001389 fprintf(f, "add %s %s %s\n",
1390 s->auth_display, s->auth_proto,
1391 s->auth_data);
1392 pclose(f);
1393 } else {
1394 fprintf(stderr, "Could not run %s\n",
1395 cmd);
1396 }
1397 }
1398}
1399
1400static void
1401do_nologin(struct passwd *pw)
1402{
1403 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001404 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1405 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001406
1407#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001408 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001409 return;
1410 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001411#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001412 if (pw->pw_uid == 0)
1413 return;
1414 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001415#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001416 if (stat(nl, &sb) == -1) {
1417 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001418 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001419 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001420 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001421
1422 /* /etc/nologin exists. Print its contents if we can and exit. */
1423 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1424 if ((f = fopen(nl, "r")) != NULL) {
1425 while (fgets(buf, sizeof(buf), f))
1426 fputs(buf, stderr);
1427 fclose(f);
1428 }
1429 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001430}
1431
Damien Millerd8cb1f12008-02-10 22:40:12 +11001432/*
1433 * Chroot into a directory after checking it for safety: all path components
1434 * must be root-owned directories with strict permissions.
1435 */
1436static void
1437safely_chroot(const char *path, uid_t uid)
1438{
1439 const char *cp;
1440 char component[MAXPATHLEN];
1441 struct stat st;
1442
1443 if (*path != '/')
1444 fatal("chroot path does not begin at root");
1445 if (strlen(path) >= sizeof(component))
1446 fatal("chroot path too long");
1447
1448 /*
1449 * Descend the path, checking that each component is a
1450 * root-owned directory with strict permissions.
1451 */
1452 for (cp = path; cp != NULL;) {
1453 if ((cp = strchr(cp, '/')) == NULL)
1454 strlcpy(component, path, sizeof(component));
1455 else {
1456 cp++;
1457 memcpy(component, path, cp - path);
1458 component[cp - path] = '\0';
1459 }
1460
1461 debug3("%s: checking '%s'", __func__, component);
1462
1463 if (stat(component, &st) != 0)
1464 fatal("%s: stat(\"%s\"): %s", __func__,
1465 component, strerror(errno));
1466 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1467 fatal("bad ownership or modes for chroot "
1468 "directory %s\"%s\"",
1469 cp == NULL ? "" : "component ", component);
1470 if (!S_ISDIR(st.st_mode))
1471 fatal("chroot path %s\"%s\" is not a directory",
1472 cp == NULL ? "" : "component ", component);
1473
1474 }
1475
1476 if (chdir(path) == -1)
1477 fatal("Unable to chdir to chroot path \"%s\": "
1478 "%s", path, strerror(errno));
1479 if (chroot(path) == -1)
1480 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1481 if (chdir("/") == -1)
1482 fatal("%s: chdir(/) after chroot: %s",
1483 __func__, strerror(errno));
1484 verbose("Changed root directory to \"%s\"", path);
1485}
1486
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001487/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001488void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001489do_setusercontext(struct passwd *pw)
1490{
Damien Miller54e37732008-02-10 22:48:55 +11001491 char *chroot_path, *tmp;
Tim Ricea261b8d2014-06-18 16:17:28 -07001492#ifdef USE_LIBIAF
1493 int doing_chroot = 0;
1494#endif
Damien Miller54e37732008-02-10 22:48:55 +11001495
Darren Tucker97528352010-11-05 12:03:05 +11001496 platform_setusercontext(pw);
1497
Darren Tuckerb12fe272010-11-05 14:47:01 +11001498 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001499#ifdef HAVE_LOGIN_CAP
1500 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001501 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001502 perror("unable to set user context");
1503 exit(1);
1504 }
1505#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001506 if (setlogin(pw->pw_name) < 0)
1507 error("setlogin failed: %s", strerror(errno));
1508 if (setgid(pw->pw_gid) < 0) {
1509 perror("setgid");
1510 exit(1);
1511 }
1512 /* Initialize the group list. */
1513 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1514 perror("initgroups");
1515 exit(1);
1516 }
1517 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001518#endif
1519
Darren Tucker920612e2010-11-05 12:36:15 +11001520 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001521
Damien Millerd8cb1f12008-02-10 22:40:12 +11001522 if (options.chroot_directory != NULL &&
1523 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001524 tmp = tilde_expand_filename(options.chroot_directory,
1525 pw->pw_uid);
1526 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1527 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001528 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001529 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001530 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001531 /* Make sure we don't attempt to chroot again */
1532 free(options.chroot_directory);
1533 options.chroot_directory = NULL;
Tim Ricea261b8d2014-06-18 16:17:28 -07001534#ifdef USE_LIBIAF
1535 doing_chroot = 1;
1536#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001537 }
1538
1539#ifdef HAVE_LOGIN_CAP
1540 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1541 perror("unable to set user context (setuser)");
1542 exit(1);
1543 }
Damien Miller58528402013-03-15 11:22:37 +11001544 /*
1545 * FreeBSD's setusercontext() will not apply the user's
1546 * own umask setting unless running with the user's UID.
1547 */
1548 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001549#else
Tim Rice9464ba62014-01-20 17:59:28 -08001550# ifdef USE_LIBIAF
Tim Ricea261b8d2014-06-18 16:17:28 -07001551/* In a chroot environment, the set_id() will always fail; typically
1552 * because of the lack of necessary authentication services and runtime
1553 * such as ./usr/lib/libiaf.so, ./usr/lib/libpam.so.1, and ./etc/passwd
1554 * We skip it in the internal sftp chroot case.
1555 * We'll lose auditing and ACLs but permanently_set_uid will
1556 * take care of the rest.
1557 */
1558 if ((doing_chroot == 0) && set_id(pw->pw_name) != 0) {
Tim Rice617da332014-01-22 19:16:10 -08001559 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001560 }
1561# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001562 /* Permanently switch to the desired uid. */
1563 permanently_set_uid(pw);
1564#endif
Damien Millera56086b2013-04-23 15:24:18 +10001565 } else if (options.chroot_directory != NULL &&
1566 strcasecmp(options.chroot_directory, "none") != 0) {
1567 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001568 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001569
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001570 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1571 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1572}
1573
Ben Lindstrom08105192002-03-22 02:50:06 +00001574static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001575do_pwchange(Session *s)
1576{
Darren Tucker09991742004-07-17 17:05:14 +10001577 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001578 fprintf(stderr, "WARNING: Your password has expired.\n");
1579 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001580 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001581 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001582#ifdef WITH_SELINUX
1583 setexeccon(NULL);
1584#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001585#ifdef PASSWD_NEEDS_USERNAME
1586 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1587 (char *)NULL);
1588#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001589 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001590#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001591 perror("passwd");
1592 } else {
1593 fprintf(stderr,
1594 "Password change required but no TTY available.\n");
1595 }
1596 exit(1);
1597}
1598
1599static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600launch_login(struct passwd *pw, const char *hostname)
1601{
1602 /* Launch login(1). */
1603
Ben Lindstrom378a4172002-06-07 14:49:56 +00001604 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001605#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001606 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001607#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001608#ifdef LOGIN_NO_ENDOPT
1609 "-p", "-f", pw->pw_name, (char *)NULL);
1610#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001611 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001612#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001613
1614 /* Login couldn't be executed, die. */
1615
1616 perror("login");
1617 exit(1);
1618}
1619
Darren Tucker23bc8d02004-02-06 16:24:31 +11001620static void
1621child_close_fds(void)
1622{
Damien Miller85b45e02013-07-20 13:21:52 +10001623 extern AuthenticationConnection *auth_conn;
1624
1625 if (auth_conn) {
1626 ssh_close_authentication_connection(auth_conn);
1627 auth_conn = NULL;
1628 }
1629
Darren Tucker23bc8d02004-02-06 16:24:31 +11001630 if (packet_get_connection_in() == packet_get_connection_out())
1631 close(packet_get_connection_in());
1632 else {
1633 close(packet_get_connection_in());
1634 close(packet_get_connection_out());
1635 }
1636 /*
1637 * Close all descriptors related to channels. They will still remain
1638 * open in the parent.
1639 */
1640 /* XXX better use close-on-exec? -markus */
1641 channel_close_all();
1642
1643 /*
1644 * Close any extra file descriptors. Note that there may still be
1645 * descriptors left by system functions. They will be closed later.
1646 */
1647 endpwent();
1648
1649 /*
Damien Miller788f2122005-11-05 15:14:59 +11001650 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001651 * hanging around in clients. Note that we want to do this after
1652 * initgroups, because at least on Solaris 2.3 it leaves file
1653 * descriptors open.
1654 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001655 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001656}
1657
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001658/*
1659 * Performs common processing for the child, such as setting up the
1660 * environment, closing extra file descriptors, setting the user and group
1661 * ids, and executing the command or shell.
1662 */
Damien Millerdfc24252008-02-10 22:29:40 +11001663#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001664void
1665do_child(Session *s, const char *command)
1666{
1667 extern char **environ;
1668 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001669 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001670 const char *shell, *shell0, *hostname = NULL;
1671 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001672 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001673
1674 /* remove hostkey from the child's memory */
1675 destroy_sensitive_data();
1676
Darren Tucker23bc8d02004-02-06 16:24:31 +11001677 /* Force a password change */
1678 if (s->authctxt->force_pwchange) {
1679 do_setusercontext(pw);
1680 child_close_fds();
1681 do_pwchange(s);
1682 exit(1);
1683 }
1684
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001685 /* login(1) is only called if we execute the login shell */
1686 if (options.use_login && command != NULL)
1687 options.use_login = 0;
1688
Tim Rice81ed5182002-09-25 17:38:46 -07001689#ifdef _UNICOS
1690 cray_setup(pw->pw_uid, pw->pw_name, command);
1691#endif /* _UNICOS */
1692
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001693 /*
1694 * Login(1) does this as well, and it needs uid 0 for the "-h"
1695 * switch, so we let login(1) to this for us.
1696 */
1697 if (!options.use_login) {
1698#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001699 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001700 if (!check_quietlogin(s, command))
1701 do_motd();
1702#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001703 /* When PAM is enabled we rely on it to do the nologin check */
1704 if (!options.use_pam)
1705 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001706 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001707 /*
1708 * PAM session modules in do_setusercontext may have
1709 * generated messages, so if this in an interactive
1710 * login then display them too.
1711 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001712 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001713 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001714#endif /* HAVE_OSF_SIA */
1715 }
1716
Darren Tucker69687f42004-09-11 22:17:26 +10001717#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001718 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1719 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001720 display_loginmsg();
1721 exit(254);
1722 }
1723#endif
1724
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001725 /*
1726 * Get the shell from the password data. An empty shell field is
1727 * legal, and means /bin/sh.
1728 */
1729 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001730
1731 /*
1732 * Make sure $SHELL points to the shell from the password file,
1733 * even if shell is overridden from login.conf
1734 */
1735 env = do_setup_env(s, shell);
1736
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001737#ifdef HAVE_LOGIN_CAP
1738 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1739#endif
1740
Damien Millerad833b32000-08-23 10:46:23 +10001741 /* we have to stash the hostname before we close our socket. */
1742 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001743 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001744 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001745 /*
1746 * Close the connection descriptors; note that this is the child, and
1747 * the server will still have the socket open, and it is important
1748 * that we do not shutdown it. Note that the descriptors cannot be
1749 * closed before building the environment, as we call
1750 * get_remote_ipaddr there.
1751 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001752 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001753
Damien Millerb38eff82000-04-01 11:09:21 +10001754 /*
Damien Miller05eda432002-02-10 18:32:28 +11001755 * Must take new environment into use so that .ssh/rc,
1756 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001757 */
1758 environ = env;
1759
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001760#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001761 /*
1762 * At this point, we check to see if AFS is active and if we have
1763 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1764 * if we can (and need to) extend the ticket into an AFS token. If
1765 * we don't do this, we run into potential problems if the user's
1766 * home directory is in AFS and it's not world-readable.
1767 */
1768
1769 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001770 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001771 char cell[64];
1772
1773 debug("Getting AFS token");
1774
1775 k_setpag();
1776
1777 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1778 krb5_afslog(s->authctxt->krb5_ctx,
1779 s->authctxt->krb5_fwd_ccache, cell, NULL);
1780
1781 krb5_afslog_home(s->authctxt->krb5_ctx,
1782 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1783 }
1784#endif
1785
Damien Miller788f2122005-11-05 15:14:59 +11001786 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001787 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001788 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001789#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001790 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001791#endif
Damien Miller7aa46ec2010-06-26 09:37:57 +10001792 if (r || options.chroot_directory == NULL ||
1793 strcasecmp(options.chroot_directory, "none") == 0)
Damien Miller6051c942008-06-16 07:53:16 +10001794 fprintf(stderr, "Could not chdir to home "
1795 "directory %s: %s\n", pw->pw_dir,
1796 strerror(errno));
1797 if (r)
1798 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001799 }
1800
Damien Millera1939002008-03-15 17:27:58 +11001801 closefrom(STDERR_FILENO + 1);
1802
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001803 if (!options.use_login)
1804 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001805
1806 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001807 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001808
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001809 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1810 printf("This service allows sftp connections only.\n");
1811 fflush(NULL);
1812 exit(1);
1813 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001814 extern int optind, optreset;
1815 int i;
1816 char *p, *args;
1817
Darren Tuckerac46a912009-06-21 17:55:23 +10001818 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001819 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001820 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1821 if (i < ARGV_MAX - 1)
1822 argv[i++] = p;
1823 argv[i] = NULL;
1824 optind = optreset = 1;
1825 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001826#ifdef WITH_SELINUX
1827 ssh_selinux_change_context("sftpd_t");
1828#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001829 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001830 }
1831
Darren Tucker695ed392009-10-07 09:02:18 +11001832 fflush(NULL);
1833
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001834 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001835 launch_login(pw, hostname);
1836 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001837 }
1838
1839 /* Get the last component of the shell name. */
1840 if ((shell0 = strrchr(shell, '/')) != NULL)
1841 shell0++;
1842 else
1843 shell0 = shell;
1844
Damien Millerb38eff82000-04-01 11:09:21 +10001845 /*
1846 * If we have no command, execute the shell. In this case, the shell
1847 * name to be passed in argv[0] is preceded by '-' to indicate that
1848 * this is a login shell.
1849 */
1850 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001851 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001852
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001853 /* Start the shell. Set initial character to '-'. */
1854 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001855
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001856 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1857 >= sizeof(argv0) - 1) {
1858 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001859 perror(shell);
1860 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001861 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001862
1863 /* Execute the shell. */
1864 argv[0] = argv0;
1865 argv[1] = NULL;
1866 execve(shell, argv, env);
1867
1868 /* Executing the shell failed. */
1869 perror(shell);
1870 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001871 }
1872 /*
1873 * Execute the command using the user's shell. This uses the -c
1874 * option to execute the command.
1875 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001876 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001877 argv[1] = "-c";
1878 argv[2] = (char *) command;
1879 argv[3] = NULL;
1880 execve(shell, argv, env);
1881 perror(shell);
1882 exit(1);
1883}
1884
Damien Miller7207f642008-05-19 15:34:50 +10001885void
1886session_unused(int id)
1887{
1888 debug3("%s: session id %d unused", __func__, id);
1889 if (id >= options.max_sessions ||
1890 id >= sessions_nalloc) {
1891 fatal("%s: insane session id %d (max %d nalloc %d)",
1892 __func__, id, options.max_sessions, sessions_nalloc);
1893 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001894 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001895 sessions[id].self = id;
1896 sessions[id].used = 0;
1897 sessions[id].chanid = -1;
1898 sessions[id].ptyfd = -1;
1899 sessions[id].ttyfd = -1;
1900 sessions[id].ptymaster = -1;
1901 sessions[id].x11_chanids = NULL;
1902 sessions[id].next_unused = sessions_first_unused;
1903 sessions_first_unused = id;
1904}
1905
Damien Millerb38eff82000-04-01 11:09:21 +10001906Session *
1907session_new(void)
1908{
Damien Miller7207f642008-05-19 15:34:50 +10001909 Session *s, *tmp;
1910
1911 if (sessions_first_unused == -1) {
1912 if (sessions_nalloc >= options.max_sessions)
1913 return NULL;
1914 debug2("%s: allocate (allocated %d max %d)",
1915 __func__, sessions_nalloc, options.max_sessions);
1916 tmp = xrealloc(sessions, sessions_nalloc + 1,
1917 sizeof(*sessions));
1918 if (tmp == NULL) {
1919 error("%s: cannot allocate %d sessions",
1920 __func__, sessions_nalloc + 1);
1921 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001922 }
Damien Miller7207f642008-05-19 15:34:50 +10001923 sessions = tmp;
1924 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001925 }
Damien Miller7207f642008-05-19 15:34:50 +10001926
1927 if (sessions_first_unused >= sessions_nalloc ||
1928 sessions_first_unused < 0) {
1929 fatal("%s: insane first_unused %d max %d nalloc %d",
1930 __func__, sessions_first_unused, options.max_sessions,
1931 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001932 }
Damien Miller7207f642008-05-19 15:34:50 +10001933
1934 s = &sessions[sessions_first_unused];
1935 if (s->used) {
1936 fatal("%s: session %d already used",
1937 __func__, sessions_first_unused);
1938 }
1939 sessions_first_unused = s->next_unused;
1940 s->used = 1;
1941 s->next_unused = -1;
1942 debug("session_new: session %d", s->self);
1943
1944 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001945}
1946
Ben Lindstrombba81212001-06-25 05:01:22 +00001947static void
Damien Millerb38eff82000-04-01 11:09:21 +10001948session_dump(void)
1949{
1950 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001951 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001952 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001953
1954 debug("dump: used %d next_unused %d session %d %p "
1955 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001956 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001957 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001958 s->self,
1959 s,
1960 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001961 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001962 }
1963}
1964
Damien Millerefb4afe2000-04-12 18:45:05 +10001965int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001966session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001967{
1968 Session *s = session_new();
1969 debug("session_open: channel %d", chanid);
1970 if (s == NULL) {
1971 error("no more sessions");
1972 return 0;
1973 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001974 s->authctxt = authctxt;
1975 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001976 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001977 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001978 debug("session_open: session %d: link with channel %d", s->self, chanid);
1979 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001980 return 1;
1981}
1982
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001983Session *
1984session_by_tty(char *tty)
1985{
1986 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001987 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001988 Session *s = &sessions[i];
1989 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1990 debug("session_by_tty: session %d tty %s", i, tty);
1991 return s;
1992 }
1993 }
1994 debug("session_by_tty: unknown tty %.100s", tty);
1995 session_dump();
1996 return NULL;
1997}
1998
Ben Lindstrombba81212001-06-25 05:01:22 +00001999static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002000session_by_channel(int id)
2001{
2002 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002003 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002004 Session *s = &sessions[i];
2005 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10002006 debug("session_by_channel: session %d channel %d",
2007 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002008 return s;
2009 }
2010 }
2011 debug("session_by_channel: unknown channel %d", id);
2012 session_dump();
2013 return NULL;
2014}
2015
Ben Lindstrombba81212001-06-25 05:01:22 +00002016static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10002017session_by_x11_channel(int id)
2018{
2019 int i, j;
2020
Damien Miller7207f642008-05-19 15:34:50 +10002021 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002022 Session *s = &sessions[i];
2023
2024 if (s->x11_chanids == NULL || !s->used)
2025 continue;
2026 for (j = 0; s->x11_chanids[j] != -1; j++) {
2027 if (s->x11_chanids[j] == id) {
2028 debug("session_by_x11_channel: session %d "
2029 "channel %d", s->self, id);
2030 return s;
2031 }
2032 }
2033 }
2034 debug("session_by_x11_channel: unknown channel %d", id);
2035 session_dump();
2036 return NULL;
2037}
2038
2039static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002040session_by_pid(pid_t pid)
2041{
2042 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00002043 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10002044 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002045 Session *s = &sessions[i];
2046 if (s->used && s->pid == pid)
2047 return s;
2048 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002049 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002050 session_dump();
2051 return NULL;
2052}
2053
Ben Lindstrombba81212001-06-25 05:01:22 +00002054static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002055session_window_change_req(Session *s)
2056{
2057 s->col = packet_get_int();
2058 s->row = packet_get_int();
2059 s->xpixel = packet_get_int();
2060 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002061 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10002062 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2063 return 1;
2064}
2065
Ben Lindstrombba81212001-06-25 05:01:22 +00002066static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002067session_pty_req(Session *s)
2068{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002069 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00002070 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002071
Damien Miller5ff30c62013-10-30 22:21:50 +11002072 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00002073 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10002074 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002075 }
2076 if (s->ttyfd != -1) {
2077 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10002078 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002079 }
2080
Damien Millerefb4afe2000-04-12 18:45:05 +10002081 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002082
2083 if (compat20) {
2084 s->col = packet_get_int();
2085 s->row = packet_get_int();
2086 } else {
2087 s->row = packet_get_int();
2088 s->col = packet_get_int();
2089 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002090 s->xpixel = packet_get_int();
2091 s->ypixel = packet_get_int();
2092
2093 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002094 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002095 s->term = NULL;
2096 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00002097
Damien Millerefb4afe2000-04-12 18:45:05 +10002098 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00002099 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10002100 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2101 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10002102 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002103 s->term = NULL;
2104 s->ptyfd = -1;
2105 s->ttyfd = -1;
2106 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10002107 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002108 }
2109 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002110
2111 /* for SSH1 the tty modes length is not given */
2112 if (!compat20)
2113 n_bytes = packet_remaining();
2114 tty_parse_modes(s->ttyfd, &n_bytes);
2115
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002116 if (!use_privsep)
2117 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002118
2119 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10002120 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2121
Damien Miller48b03fc2002-01-22 23:11:40 +11002122 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10002123 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002124 return 1;
2125}
2126
Ben Lindstrombba81212001-06-25 05:01:22 +00002127static int
Damien Millerbd483e72000-04-30 10:00:53 +10002128session_subsystem_req(Session *s)
2129{
Damien Millerae452462001-10-10 15:08:06 +10002130 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002131 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10002132 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11002133 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10002134 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10002135
Damien Miller71df7522013-10-15 12:12:02 +11002136 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002137 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11002138 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10002139 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10002140
Damien Millerf6d9e222000-06-18 14:50:44 +10002141 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11002142 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10002143 prog = options.subsystem_command[i];
2144 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002145 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11002146 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002147 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11002148 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002149 if (stat(prog, &st) < 0)
2150 debug("subsystem: cannot stat %s: %s",
2151 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11002152 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002153 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10002154 }
Damien Miller7207f642008-05-19 15:34:50 +10002155 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11002156 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10002157 }
2158 }
2159
2160 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11002161 logit("subsystem request for %.100s by user %s failed, "
2162 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10002163
Damien Millerbd483e72000-04-30 10:00:53 +10002164 return success;
2165}
2166
Ben Lindstrombba81212001-06-25 05:01:22 +00002167static int
Damien Millerbd483e72000-04-30 10:00:53 +10002168session_x11_req(Session *s)
2169{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002170 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002171
Damien Miller2b9b0452005-07-17 17:19:24 +10002172 if (s->auth_proto != NULL || s->auth_data != NULL) {
2173 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002174 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002175 return 0;
2176 }
Damien Millerbd483e72000-04-30 10:00:53 +10002177 s->single_connection = packet_get_char();
2178 s->auth_proto = packet_get_string(NULL);
2179 s->auth_data = packet_get_string(NULL);
2180 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002181 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002182
Ben Lindstrom768176b2001-06-09 01:29:12 +00002183 success = session_setup_x11fwd(s);
2184 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002185 free(s->auth_proto);
2186 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002187 s->auth_proto = NULL;
2188 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002189 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002190 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002191}
2192
Ben Lindstrombba81212001-06-25 05:01:22 +00002193static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002194session_shell_req(Session *s)
2195{
Damien Miller48b03fc2002-01-22 23:11:40 +11002196 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002197 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002198}
2199
Ben Lindstrombba81212001-06-25 05:01:22 +00002200static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002201session_exec_req(Session *s)
2202{
Damien Miller7207f642008-05-19 15:34:50 +10002203 u_int len, success;
2204
Damien Millerf6d9e222000-06-18 14:50:44 +10002205 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002206 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002207 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002208 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002209 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002210}
2211
Ben Lindstrombba81212001-06-25 05:01:22 +00002212static int
Damien Miller54c45982003-05-15 10:20:13 +10002213session_break_req(Session *s)
2214{
Damien Miller54c45982003-05-15 10:20:13 +10002215
Darren Tucker1f8311c2004-05-13 16:39:33 +10002216 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002217 packet_check_eom();
2218
Darren Tucker9c5d5532011-11-04 10:55:24 +11002219 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002220 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002221 return 1;
2222}
2223
2224static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002225session_env_req(Session *s)
2226{
2227 char *name, *val;
2228 u_int name_len, val_len, i;
2229
Damien Miller8569eba2014-03-04 09:35:17 +11002230 name = packet_get_cstring(&name_len);
2231 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002232 packet_check_eom();
2233
2234 /* Don't set too many environment variables */
2235 if (s->num_env > 128) {
2236 debug2("Ignoring env request %s: too many env vars", name);
2237 goto fail;
2238 }
2239
2240 for (i = 0; i < options.num_accept_env; i++) {
2241 if (match_pattern(name, options.accept_env[i])) {
2242 debug2("Setting env %d: %s=%s", s->num_env, name, val);
Damien Miller36812092006-03-26 14:22:47 +11002243 s->env = xrealloc(s->env, s->num_env + 1,
2244 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002245 s->env[s->num_env].name = name;
2246 s->env[s->num_env].val = val;
2247 s->num_env++;
2248 return (1);
2249 }
2250 }
2251 debug2("Ignoring env request %s: disallowed name", name);
2252
2253 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002254 free(name);
2255 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002256 return (0);
2257}
2258
2259static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002260session_auth_agent_req(Session *s)
2261{
2262 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002263 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002264 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002265 debug("session_auth_agent_req: no_agent_forwarding_flag");
2266 return 0;
2267 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002268 if (called) {
2269 return 0;
2270 } else {
2271 called = 1;
2272 return auth_input_request_forwarding(s->pw);
2273 }
2274}
2275
Damien Millerc7ef63d2002-02-05 12:21:42 +11002276int
2277session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002278{
Damien Millerefb4afe2000-04-12 18:45:05 +10002279 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002280 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002281
Damien Millerc7ef63d2002-02-05 12:21:42 +11002282 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002283 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002284 c->self, rtype);
2285 return 0;
2286 }
2287 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002288
2289 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002290 * a session is in LARVAL state until a shell, a command
2291 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002292 */
2293 if (c->type == SSH_CHANNEL_LARVAL) {
2294 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002295 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002296 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002297 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002298 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002299 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002300 } else if (strcmp(rtype, "x11-req") == 0) {
2301 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002302 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2303 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002304 } else if (strcmp(rtype, "subsystem") == 0) {
2305 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002306 } else if (strcmp(rtype, "env") == 0) {
2307 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002308 }
2309 }
2310 if (strcmp(rtype, "window-change") == 0) {
2311 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002312 } else if (strcmp(rtype, "break") == 0) {
2313 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002314 }
Damien Millera6b1d162004-06-30 22:41:07 +10002315
Damien Millerc7ef63d2002-02-05 12:21:42 +11002316 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002317}
2318
2319void
Damien Miller8853ca52010-06-26 10:00:14 +10002320session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2321 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002322{
2323 if (!compat20)
2324 fatal("session_set_fds: called for proto != 2.0");
2325 /*
2326 * now that have a child and a pipe to the child,
2327 * we can activate our channel and register the fd's
2328 */
2329 if (s->chanid == -1)
2330 fatal("no channel for session %d", s->self);
2331 channel_set_fds(s->chanid,
2332 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002333 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002334 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002335}
2336
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002337/*
2338 * Function to perform pty cleanup. Also called if we get aborted abnormally
2339 * (e.g., due to a dropped connection).
2340 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002341void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002342session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002343{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002344 if (s == NULL) {
2345 error("session_pty_cleanup: no session");
2346 return;
2347 }
2348 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002349 return;
2350
Kevin Steves43cdef32001-02-11 14:12:08 +00002351 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002352
Damien Millerb38eff82000-04-01 11:09:21 +10002353 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002354 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002355 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002356
2357 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002358 if (getuid() == 0)
2359 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002360
2361 /*
2362 * Close the server side of the socket pairs. We must do this after
2363 * the pty cleanup, so that another process doesn't get this pty
2364 * while we're still cleaning up.
2365 */
Damien Miller7207f642008-05-19 15:34:50 +10002366 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2367 error("close(s->ptymaster/%d): %s",
2368 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002369
2370 /* unlink pty from session */
2371 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002372}
Damien Millerefb4afe2000-04-12 18:45:05 +10002373
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002374void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002375session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002376{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002377 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002378}
2379
Damien Miller5a80bba2002-09-04 16:39:02 +10002380static char *
2381sig2name(int sig)
2382{
2383#define SSH_SIG(x) if (sig == SIG ## x) return #x
2384 SSH_SIG(ABRT);
2385 SSH_SIG(ALRM);
2386 SSH_SIG(FPE);
2387 SSH_SIG(HUP);
2388 SSH_SIG(ILL);
2389 SSH_SIG(INT);
2390 SSH_SIG(KILL);
2391 SSH_SIG(PIPE);
2392 SSH_SIG(QUIT);
2393 SSH_SIG(SEGV);
2394 SSH_SIG(TERM);
2395 SSH_SIG(USR1);
2396 SSH_SIG(USR2);
2397#undef SSH_SIG
2398 return "SIG@openssh.com";
2399}
2400
Ben Lindstrombba81212001-06-25 05:01:22 +00002401static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002402session_close_x11(int id)
2403{
2404 Channel *c;
2405
Damien Millerd47c62a2005-12-13 19:33:57 +11002406 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002407 debug("session_close_x11: x11 channel %d missing", id);
2408 } else {
2409 /* Detach X11 listener */
2410 debug("session_close_x11: detach x11 channel %d", id);
2411 channel_cancel_cleanup(id);
2412 if (c->ostate != CHAN_OUTPUT_CLOSED)
2413 chan_mark_dead(c);
2414 }
2415}
2416
2417static void
2418session_close_single_x11(int id, void *arg)
2419{
2420 Session *s;
2421 u_int i;
2422
2423 debug3("session_close_single_x11: channel %d", id);
2424 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002425 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002426 fatal("session_close_single_x11: no x11 channel %d", id);
2427 for (i = 0; s->x11_chanids[i] != -1; i++) {
2428 debug("session_close_single_x11: session %d: "
2429 "closing channel %d", s->self, s->x11_chanids[i]);
2430 /*
2431 * The channel "id" is already closing, but make sure we
2432 * close all of its siblings.
2433 */
2434 if (s->x11_chanids[i] != id)
2435 session_close_x11(s->x11_chanids[i]);
2436 }
Darren Tuckera627d422013-06-02 07:31:17 +10002437 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002438 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002439 free(s->display);
2440 s->display = NULL;
2441 free(s->auth_proto);
2442 s->auth_proto = NULL;
2443 free(s->auth_data);
2444 s->auth_data = NULL;
2445 free(s->auth_display);
2446 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002447}
2448
2449static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002450session_exit_message(Session *s, int status)
2451{
2452 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002453
2454 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002455 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002456 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002457 debug("session_exit_message: session %d channel %d pid %ld",
2458 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002459
2460 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002461 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002462 packet_put_int(WEXITSTATUS(status));
2463 packet_send();
2464 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002465 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002466 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002467#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002468 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002469#else /* WCOREDUMP */
2470 packet_put_char(0);
2471#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002472 packet_put_cstring("");
2473 packet_put_cstring("");
2474 packet_send();
2475 } else {
2476 /* Some weird exit cause. Just exit. */
2477 packet_disconnect("wait returned status %04x.", status);
2478 }
2479
2480 /* disconnect channel */
2481 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002482
2483 /*
2484 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002485 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002486 * by session_close_by_channel when the childs close their fds.
2487 */
2488 channel_register_cleanup(c->self, session_close_by_channel, 1);
2489
Damien Miller166fca82000-04-20 07:42:21 +10002490 /*
2491 * emulate a write failure with 'chan_write_failed', nobody will be
2492 * interested in data we write.
2493 * Note that we must not call 'chan_read_failed', since there could
2494 * be some more data waiting in the pipe.
2495 */
Damien Millerbd483e72000-04-30 10:00:53 +10002496 if (c->ostate != CHAN_OUTPUT_CLOSED)
2497 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002498}
2499
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002500void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002501session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002502{
Damien Millereccb9de2005-06-17 12:59:34 +10002503 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002504
Ben Lindstromce0f6342002-06-11 16:42:49 +00002505 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002506 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002507 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002508 free(s->term);
2509 free(s->display);
2510 free(s->x11_chanids);
2511 free(s->auth_display);
2512 free(s->auth_data);
2513 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002514 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002515 if (s->env != NULL) {
2516 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002517 free(s->env[i].name);
2518 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002519 }
Darren Tuckera627d422013-06-02 07:31:17 +10002520 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002521 }
Damien Millere247cc42000-05-07 12:03:14 +10002522 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002523 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002524}
2525
2526void
2527session_close_by_pid(pid_t pid, int status)
2528{
2529 Session *s = session_by_pid(pid);
2530 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002531 debug("session_close_by_pid: no session for pid %ld",
2532 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002533 return;
2534 }
2535 if (s->chanid != -1)
2536 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002537 if (s->ttyfd != -1)
2538 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002539 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002540}
2541
2542/*
2543 * this is called when a channel dies before
2544 * the session 'child' itself dies
2545 */
2546void
2547session_close_by_channel(int id, void *arg)
2548{
2549 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002550 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002551
Damien Millerefb4afe2000-04-12 18:45:05 +10002552 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002553 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002554 return;
2555 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002556 debug("session_close_by_channel: channel %d child %ld",
2557 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002558 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002559 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002560 /*
2561 * delay detach of session, but release pty, since
2562 * the fd's to the child are already closed
2563 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002564 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002565 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002566 return;
2567 }
2568 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002569 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002570
2571 /* Close any X11 listeners associated with this session */
2572 if (s->x11_chanids != NULL) {
2573 for (i = 0; s->x11_chanids[i] != -1; i++) {
2574 session_close_x11(s->x11_chanids[i]);
2575 s->x11_chanids[i] = -1;
2576 }
2577 }
2578
Damien Millerefb4afe2000-04-12 18:45:05 +10002579 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002580 session_close(s);
2581}
2582
2583void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002584session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002585{
2586 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002587 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002588 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002589 if (s->used) {
2590 if (closefunc != NULL)
2591 closefunc(s);
2592 else
2593 session_close(s);
2594 }
Damien Miller52b77be2001-10-10 15:14:37 +10002595 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002596}
2597
Ben Lindstrombba81212001-06-25 05:01:22 +00002598static char *
Damien Millere247cc42000-05-07 12:03:14 +10002599session_tty_list(void)
2600{
2601 static char buf[1024];
2602 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002603 char *cp;
2604
Damien Millere247cc42000-05-07 12:03:14 +10002605 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002606 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002607 Session *s = &sessions[i];
2608 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002609
Damien Millera8ed44b2003-01-10 09:53:12 +11002610 if (strncmp(s->tty, "/dev/", 5) != 0) {
2611 cp = strrchr(s->tty, '/');
2612 cp = (cp == NULL) ? s->tty : cp + 1;
2613 } else
2614 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002615
Damien Millere247cc42000-05-07 12:03:14 +10002616 if (buf[0] != '\0')
2617 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002618 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002619 }
2620 }
2621 if (buf[0] == '\0')
2622 strlcpy(buf, "notty", sizeof buf);
2623 return buf;
2624}
2625
2626void
2627session_proctitle(Session *s)
2628{
2629 if (s->pw == NULL)
2630 error("no user for session %d", s->self);
2631 else
2632 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2633}
2634
Ben Lindstrom768176b2001-06-09 01:29:12 +00002635int
2636session_setup_x11fwd(Session *s)
2637{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002638 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002639 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002640 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002641 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002642
2643 if (no_x11_forwarding_flag) {
2644 packet_send_debug("X11 forwarding disabled in user configuration file.");
2645 return 0;
2646 }
2647 if (!options.x11_forwarding) {
2648 debug("X11 forwarding disabled in server configuration file.");
2649 return 0;
2650 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002651 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002652 (stat(options.xauth_location, &st) == -1)) {
2653 packet_send_debug("No xauth program; cannot forward with spoofing.");
2654 return 0;
2655 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002656 if (options.use_login) {
2657 packet_send_debug("X11 forwarding disabled; "
2658 "not compatible with UseLogin=yes.");
2659 return 0;
2660 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002661 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002662 debug("X11 display already set.");
2663 return 0;
2664 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002665 if (x11_create_display_inet(options.x11_display_offset,
2666 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002667 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002668 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002669 return 0;
2670 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002671 for (i = 0; s->x11_chanids[i] != -1; i++) {
2672 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002673 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002674 }
Kevin Steves366298c2001-12-19 17:58:01 +00002675
2676 /* Set up a suitable value for the DISPLAY variable. */
2677 if (gethostname(hostname, sizeof(hostname)) < 0)
2678 fatal("gethostname: %.100s", strerror(errno));
2679 /*
2680 * auth_display must be used as the displayname when the
2681 * authorization entry is added with xauth(1). This will be
2682 * different than the DISPLAY string for localhost displays.
2683 */
Damien Miller95c249f2002-02-05 12:11:34 +11002684 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002685 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002686 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002687 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002688 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002689 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002690 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002691 } else {
2692#ifdef IPADDR_IN_DISPLAY
2693 struct hostent *he;
2694 struct in_addr my_addr;
2695
2696 he = gethostbyname(hostname);
2697 if (he == NULL) {
2698 error("Can't get IP address for X11 DISPLAY.");
2699 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2700 return 0;
2701 }
2702 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002703 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002704 s->display_number, s->screen);
2705#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002706 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002707 s->display_number, s->screen);
2708#endif
2709 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002710 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002711 }
2712
Ben Lindstrom768176b2001-06-09 01:29:12 +00002713 return 1;
2714}
2715
Ben Lindstrombba81212001-06-25 05:01:22 +00002716static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002717do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002718{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002719 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002720}
2721
2722void
2723do_cleanup(Authctxt *authctxt)
2724{
2725 static int called = 0;
2726
2727 debug("do_cleanup");
2728
2729 /* no cleanup if we're in the child for login shell */
2730 if (is_child)
2731 return;
2732
2733 /* avoid double cleanup */
2734 if (called)
2735 return;
2736 called = 1;
2737
Darren Tucker9142e1c2007-08-16 23:28:04 +10002738 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002739 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002740
2741#ifdef USE_PAM
2742 if (options.use_pam) {
2743 sshpam_cleanup();
2744 sshpam_thread_cleanup();
2745 }
2746#endif
2747
2748 if (!authctxt->authenticated)
2749 return;
2750
Darren Tucker3e33cec2003-10-02 16:12:36 +10002751#ifdef KRB5
2752 if (options.kerberos_ticket_cleanup &&
2753 authctxt->krb5_ctx)
2754 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002755#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002756
2757#ifdef GSSAPI
2758 if (compat20 && options.gss_cleanup_creds)
2759 ssh_gssapi_cleanup_creds();
2760#endif
2761
2762 /* remove agent socket */
2763 auth_sock_cleanup_proc(authctxt->pw);
2764
2765 /*
2766 * Cleanup ptys/utmp only if privsep is disabled,
2767 * or if running in monitor.
2768 */
2769 if (!use_privsep || mm_is_monitor())
2770 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002771}