blob: 821867d5967b0c7f2a03f896a011da5ebdd0f629 [file] [log] [blame]
Damien Millere5c0d522014-07-03 21:24:19 +10001/* $OpenBSD: session.c,v 1.272 2014/07/03 03:34:09 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"
87#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000088#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "serverloop.h"
90#include "canohost.h"
Damien Millerd8cb1f12008-02-10 22:40:12 +110091#include "misc.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 struct sockaddr_un sunaddr;
187
188 if (auth_sock_name != NULL) {
189 error("authentication forwarding requested twice.");
190 return 0;
191 }
192
193 /* Temporarily drop privileged uid for mkdir/bind. */
194 temporarily_use_uid(pw);
195
196 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000197 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000198
199 /* Create private directory for socket */
200 if (mkdtemp(auth_sock_dir) == NULL) {
201 packet_send_debug("Agent forwarding disabled: "
202 "mkdtemp() failed: %.100s", strerror(errno));
203 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000204 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000205 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000206 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000207 }
Damien Miller7207f642008-05-19 15:34:50 +1000208
209 xasprintf(&auth_sock_name, "%s/agent.%ld",
210 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000211
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000212 /* Create the socket. */
213 sock = socket(AF_UNIX, SOCK_STREAM, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000214 if (sock < 0) {
215 error("socket: %.100s", strerror(errno));
216 restore_uid();
217 goto authsock_err;
218 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000219
220 /* Bind it to the name. */
221 memset(&sunaddr, 0, sizeof(sunaddr));
222 sunaddr.sun_family = AF_UNIX;
223 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
224
Damien Miller7207f642008-05-19 15:34:50 +1000225 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
226 error("bind: %.100s", strerror(errno));
227 restore_uid();
228 goto authsock_err;
229 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000230
231 /* Restore the privileged uid. */
232 restore_uid();
233
234 /* Start listening on the socket. */
Damien Miller7207f642008-05-19 15:34:50 +1000235 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
236 error("listen: %.100s", strerror(errno));
237 goto authsock_err;
238 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000239
240 /* Allocate a channel for the authentication agent socket. */
241 nc = channel_new("auth socket",
242 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
243 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000244 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100245 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000246 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000247
248 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000249 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000250 if (auth_sock_dir != NULL) {
251 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000252 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000253 }
254 if (sock != -1)
255 close(sock);
256 auth_sock_name = NULL;
257 auth_sock_dir = NULL;
258 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000259}
260
Darren Tucker1921ed92004-02-10 13:23:28 +1100261static void
262display_loginmsg(void)
263{
Damien Miller46d38de2005-07-17 17:02:09 +1000264 if (buffer_len(&loginmsg) > 0) {
265 buffer_append(&loginmsg, "\0", 1);
266 printf("%s", (char *)buffer_ptr(&loginmsg));
267 buffer_clear(&loginmsg);
268 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100269}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000270
Ben Lindstromb31783d2001-03-22 02:02:12 +0000271void
272do_authenticated(Authctxt *authctxt)
273{
Damien Miller97f39ae2003-02-24 11:57:01 +1100274 setproctitle("%s", authctxt->pw->pw_name);
275
Ben Lindstromb31783d2001-03-22 02:02:12 +0000276 /* setup the channel layer */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100277 if (no_port_forwarding_flag ||
278 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
279 channel_disable_adm_local_opens();
280 else
Ben Lindstromb31783d2001-03-22 02:02:12 +0000281 channel_permit_all_opens();
282
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100283 auth_debug_send();
284
Ben Lindstromb31783d2001-03-22 02:02:12 +0000285 if (compat20)
286 do_authenticated2(authctxt);
287 else
288 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000289
Darren Tucker3e33cec2003-10-02 16:12:36 +1000290 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000291}
292
Damien Millerb38eff82000-04-01 11:09:21 +1000293/*
Damien Millerb38eff82000-04-01 11:09:21 +1000294 * Prepares for an interactive session. This is called after the user has
295 * been successfully authenticated. During this message exchange, pseudo
296 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
297 * are requested, etc.
298 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000299static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000300do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000301{
302 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000303 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100304 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000305 int enable_compression_after_reply = 0;
306 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000307
308 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100309 if (s == NULL) {
310 error("no more sessions");
311 return;
312 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000313 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000314 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000315
Damien Millerb38eff82000-04-01 11:09:21 +1000316 /*
317 * We stay in this loop until the client requests to execute a shell
318 * or a command.
319 */
320 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000321 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000322
323 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100324 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000325
326 /* Process the packet. */
327 switch (type) {
328 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000329 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100330 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000331 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000332 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100333 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000334 break;
335 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000336 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000337 debug2("compression disabled");
338 break;
339 }
Damien Millerb38eff82000-04-01 11:09:21 +1000340 /* Enable compression after we have responded with SUCCESS. */
341 enable_compression_after_reply = 1;
342 success = 1;
343 break;
344
345 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000346 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000347 break;
348
349 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000350 s->auth_proto = packet_get_string(&proto_len);
351 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000352
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000353 screen_flag = packet_get_protocol_flags() &
354 SSH_PROTOFLAG_SCREEN_NUMBER;
355 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
356
357 if (packet_remaining() == 4) {
358 if (!screen_flag)
359 debug2("Buggy client: "
360 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000361 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000362 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000363 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000364 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100365 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000366 success = session_setup_x11fwd(s);
367 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000368 free(s->auth_proto);
369 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000370 s->auth_proto = NULL;
371 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000372 }
Damien Millerb38eff82000-04-01 11:09:21 +1000373 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000374
375 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
Damien Miller4f755cd2008-05-19 14:57:41 +1000376 if (!options.allow_agent_forwarding ||
377 no_agent_forwarding_flag || compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000378 debug("Authentication agent forwarding not permitted for this authentication.");
379 break;
380 }
381 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000382 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000383 break;
384
385 case SSH_CMSG_PORT_FORWARD_REQUEST:
386 if (no_port_forwarding_flag) {
387 debug("Port forwarding not permitted for this authentication.");
388 break;
389 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100390 if (!(options.allow_tcp_forwarding & FORWARD_REMOTE)) {
Damien Miller50a41ed2000-10-16 12:14:42 +1100391 debug("Port forwarding not permitted.");
392 break;
393 }
Damien Millerb38eff82000-04-01 11:09:21 +1000394 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000395 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
396 options.gateway_ports) < 0) {
397 debug("Port forwarding failed.");
398 break;
399 }
Damien Millerb38eff82000-04-01 11:09:21 +1000400 success = 1;
401 break;
402
403 case SSH_CMSG_MAX_PACKET_SIZE:
404 if (packet_set_maxsize(packet_get_int()) > 0)
405 success = 1;
406 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100407
Damien Millerb38eff82000-04-01 11:09:21 +1000408 case SSH_CMSG_EXEC_SHELL:
409 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000410 if (type == SSH_CMSG_EXEC_CMD) {
411 command = packet_get_string(&dlen);
412 debug("Exec command '%.500s'", command);
Damien Miller7207f642008-05-19 15:34:50 +1000413 if (do_exec(s, command) != 0)
414 packet_disconnect(
415 "command execution failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000416 free(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000417 } else {
Damien Miller7207f642008-05-19 15:34:50 +1000418 if (do_exec(s, NULL) != 0)
419 packet_disconnect(
420 "shell execution failed");
Damien Millerb38eff82000-04-01 11:09:21 +1000421 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100422 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000423 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000424 return;
425
426 default:
427 /*
428 * Any unknown messages in this phase are ignored,
429 * and a failure message is returned.
430 */
Damien Miller996acd22003-04-09 20:59:48 +1000431 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000432 }
433 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
434 packet_send();
435 packet_write_wait();
436
437 /* Enable compression now that we have replied if appropriate. */
438 if (enable_compression_after_reply) {
439 enable_compression_after_reply = 0;
440 packet_start_compression(compression_level);
441 }
442 }
443}
444
Darren Tucker293ee3c2014-01-19 15:28:01 +1100445#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000446/*
447 * This is called to fork and execute a command when we have no tty. This
448 * will call do_child from the child, and server_loop from the parent after
449 * setting up file descriptors and such.
450 */
Damien Miller7207f642008-05-19 15:34:50 +1000451int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000452do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000453{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000454 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000455
456#ifdef USE_PIPES
457 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000458
Damien Miller22a29882010-05-10 11:53:54 +1000459 if (s == NULL)
460 fatal("do_exec_no_pty: no session");
461
Damien Millerb38eff82000-04-01 11:09:21 +1000462 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000463 if (pipe(pin) < 0) {
464 error("%s: pipe in: %.100s", __func__, strerror(errno));
465 return -1;
466 }
467 if (pipe(pout) < 0) {
468 error("%s: pipe out: %.100s", __func__, strerror(errno));
469 close(pin[0]);
470 close(pin[1]);
471 return -1;
472 }
Damien Miller8853ca52010-06-26 10:00:14 +1000473 if (pipe(perr) < 0) {
474 error("%s: pipe err: %.100s", __func__,
475 strerror(errno));
476 close(pin[0]);
477 close(pin[1]);
478 close(pout[0]);
479 close(pout[1]);
480 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000481 }
482#else
Damien Millerb38eff82000-04-01 11:09:21 +1000483 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000484
Damien Miller22a29882010-05-10 11:53:54 +1000485 if (s == NULL)
486 fatal("do_exec_no_pty: no session");
487
Damien Millerb38eff82000-04-01 11:09:21 +1000488 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000489 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
490 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
491 return -1;
492 }
Damien Miller8853ca52010-06-26 10:00:14 +1000493 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
494 error("%s: socketpair #2: %.100s", __func__,
495 strerror(errno));
496 close(inout[0]);
497 close(inout[1]);
498 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000499 }
500#endif
501
Damien Millere247cc42000-05-07 12:03:14 +1000502 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000503
Damien Millerb38eff82000-04-01 11:09:21 +1000504 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000505 switch ((pid = fork())) {
506 case -1:
507 error("%s: fork: %.100s", __func__, strerror(errno));
508#ifdef USE_PIPES
509 close(pin[0]);
510 close(pin[1]);
511 close(pout[0]);
512 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000513 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000514 close(perr[1]);
515#else
516 close(inout[0]);
517 close(inout[1]);
518 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000519 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000520#endif
521 return -1;
522 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000523 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000524
Damien Millerb38eff82000-04-01 11:09:21 +1000525 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000526 log_init(__progname, options.log_level,
527 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000528
529 /*
530 * Create a new session and process group since the 4.4BSD
531 * setlogin() affects the entire process group.
532 */
533 if (setsid() < 0)
534 error("setsid failed: %.100s", strerror(errno));
535
536#ifdef USE_PIPES
537 /*
538 * Redirect stdin. We close the parent side of the socket
539 * pair, and make the child side the standard input.
540 */
541 close(pin[1]);
542 if (dup2(pin[0], 0) < 0)
543 perror("dup2 stdin");
544 close(pin[0]);
545
546 /* Redirect stdout. */
547 close(pout[0]);
548 if (dup2(pout[1], 1) < 0)
549 perror("dup2 stdout");
550 close(pout[1]);
551
552 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000553 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000554 if (dup2(perr[1], 2) < 0)
555 perror("dup2 stderr");
556 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000557#else
Damien Millerb38eff82000-04-01 11:09:21 +1000558 /*
559 * Redirect stdin, stdout, and stderr. Stdin and stdout will
560 * use the same socket, as some programs (particularly rdist)
561 * seem to depend on it.
562 */
563 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000564 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000565 if (dup2(inout[0], 0) < 0) /* stdin */
566 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000567 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000568 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000569 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000570 if (dup2(err[0], 2) < 0) /* stderr */
571 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000572 close(err[0]);
573#endif
574
Damien Millerb38eff82000-04-01 11:09:21 +1000575
Tim Rice81ed5182002-09-25 17:38:46 -0700576#ifdef _UNICOS
577 cray_init_job(s->pw); /* set up cray jid and tmpdir */
578#endif
579
Damien Millerb38eff82000-04-01 11:09:21 +1000580 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000581 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000582 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000583 default:
584 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000585 }
Damien Miller7207f642008-05-19 15:34:50 +1000586
Tim Rice81ed5182002-09-25 17:38:46 -0700587#ifdef _UNICOS
588 signal(WJSIGNAL, cray_job_termination_handler);
589#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100590#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100591 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100592#endif
Damien Miller7207f642008-05-19 15:34:50 +1000593
Damien Millerb38eff82000-04-01 11:09:21 +1000594 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000595 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100596 packet_set_interactive(s->display != NULL,
597 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000598
599 /*
600 * Clear loginmsg, since it's the child's responsibility to display
601 * it to the user, otherwise multiple sessions may accumulate
602 * multiple copies of the login messages.
603 */
604 buffer_clear(&loginmsg);
605
Damien Millerb38eff82000-04-01 11:09:21 +1000606#ifdef USE_PIPES
607 /* We are the parent. Close the child sides of the pipes. */
608 close(pin[0]);
609 close(pout[1]);
610 close(perr[1]);
611
Damien Millerefb4afe2000-04-12 18:45:05 +1000612 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000613 session_set_fds(s, pin[1], pout[0], perr[0],
614 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000615 } else {
616 /* Enter the interactive session. */
617 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000618 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000619 }
Damien Miller7207f642008-05-19 15:34:50 +1000620#else
Damien Millerb38eff82000-04-01 11:09:21 +1000621 /* We are the parent. Close the child sides of the socket pairs. */
622 close(inout[0]);
623 close(err[0]);
624
625 /*
626 * Enter the interactive session. Note: server_loop must be able to
627 * handle the case that fdin and fdout are the same.
628 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000629 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000630 session_set_fds(s, inout[1], inout[1], err[1],
631 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000632 } else {
633 server_loop(pid, inout[1], inout[1], err[1]);
634 /* server_loop has closed inout[1] and err[1]. */
635 }
Damien Miller7207f642008-05-19 15:34:50 +1000636#endif
637 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000638}
639
640/*
641 * This is called to fork and execute a command when we have a tty. This
642 * will call do_child from the child, and server_loop from the parent after
643 * setting up file descriptors, controlling tty, updating wtmp, utmp,
644 * lastlog, and other such operations.
645 */
Damien Miller7207f642008-05-19 15:34:50 +1000646int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000647do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000648{
Damien Millerb38eff82000-04-01 11:09:21 +1000649 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000650 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000651
652 if (s == NULL)
653 fatal("do_exec_pty: no session");
654 ptyfd = s->ptyfd;
655 ttyfd = s->ttyfd;
656
Damien Miller7207f642008-05-19 15:34:50 +1000657 /*
658 * Create another descriptor of the pty master side for use as the
659 * standard input. We could use the original descriptor, but this
660 * simplifies code in server_loop. The descriptor is bidirectional.
661 * Do this before forking (and cleanup in the child) so as to
662 * detect and gracefully fail out-of-fd conditions.
663 */
664 if ((fdout = dup(ptyfd)) < 0) {
665 error("%s: dup #1: %s", __func__, strerror(errno));
666 close(ttyfd);
667 close(ptyfd);
668 return -1;
669 }
670 /* we keep a reference to the pty master */
671 if ((ptymaster = dup(ptyfd)) < 0) {
672 error("%s: dup #2: %s", __func__, strerror(errno));
673 close(ttyfd);
674 close(ptyfd);
675 close(fdout);
676 return -1;
677 }
678
Damien Millerb38eff82000-04-01 11:09:21 +1000679 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000680 switch ((pid = fork())) {
681 case -1:
682 error("%s: fork: %.100s", __func__, strerror(errno));
683 close(fdout);
684 close(ptymaster);
685 close(ttyfd);
686 close(ptyfd);
687 return -1;
688 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000689 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000690
Damien Miller7207f642008-05-19 15:34:50 +1000691 close(fdout);
692 close(ptymaster);
693
Damien Miller942da032000-08-18 13:59:06 +1000694 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000695 log_init(__progname, options.log_level,
696 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000697 /* Close the master side of the pseudo tty. */
698 close(ptyfd);
699
700 /* Make the pseudo tty our controlling tty. */
701 pty_make_controlling_tty(&ttyfd, s->tty);
702
Damien Miller9c751422001-10-10 15:02:46 +1000703 /* Redirect stdin/stdout/stderr from the pseudo tty. */
704 if (dup2(ttyfd, 0) < 0)
705 error("dup2 stdin: %s", strerror(errno));
706 if (dup2(ttyfd, 1) < 0)
707 error("dup2 stdout: %s", strerror(errno));
708 if (dup2(ttyfd, 2) < 0)
709 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000710
711 /* Close the extra descriptor for the pseudo tty. */
712 close(ttyfd);
713
Damien Miller942da032000-08-18 13:59:06 +1000714 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000715#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700716 if (!(options.use_login && command == NULL)) {
717#ifdef _UNICOS
718 cray_init_job(s->pw); /* set up cray jid and tmpdir */
719#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100720 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700721 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000722# ifdef LOGIN_NEEDS_UTMPX
723 else
724 do_pre_login(s);
725# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000726#endif
Damien Miller7207f642008-05-19 15:34:50 +1000727 /*
728 * Do common processing for the child, such as execing
729 * the command.
730 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000731 do_child(s, command);
732 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000733 default:
734 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000735 }
Damien Miller7207f642008-05-19 15:34:50 +1000736
Tim Rice81ed5182002-09-25 17:38:46 -0700737#ifdef _UNICOS
738 signal(WJSIGNAL, cray_job_termination_handler);
739#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100740#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100741 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100742#endif
Damien Miller7207f642008-05-19 15:34:50 +1000743
Damien Millerb38eff82000-04-01 11:09:21 +1000744 s->pid = pid;
745
746 /* Parent. Close the slave side of the pseudo tty. */
747 close(ttyfd);
748
Damien Millerb38eff82000-04-01 11:09:21 +1000749 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000750 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100751 packet_set_interactive(1,
752 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Millerefb4afe2000-04-12 18:45:05 +1000753 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000754 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000755 } else {
756 server_loop(pid, ptyfd, fdout, -1);
757 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000758 }
Damien Miller7207f642008-05-19 15:34:50 +1000759 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000760}
761
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000762#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000763static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000764do_pre_login(Session *s)
765{
766 socklen_t fromlen;
767 struct sockaddr_storage from;
768 pid_t pid = getpid();
769
770 /*
771 * Get IP address of client. If the connection is not a socket, let
772 * the address be 0.0.0.0.
773 */
774 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000775 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000776 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000777 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100778 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000779 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000780 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000781 }
782 }
783
784 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000785 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000786 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000787}
788#endif
789
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000790/*
791 * This is called to fork and execute a command. If another command is
792 * to be forced, execute that instead.
793 */
Damien Miller7207f642008-05-19 15:34:50 +1000794int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000795do_exec(Session *s, const char *command)
796{
Damien Miller7207f642008-05-19 15:34:50 +1000797 int ret;
Damien Miller71df7522013-10-15 12:12:02 +1100798 const char *forced = NULL;
799 char session_type[1024], *tty = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000800
Damien Millere2754432006-07-24 14:06:47 +1000801 if (options.adm_forced_command) {
802 original_command = command;
803 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100804 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000805 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000806 original_command = command;
807 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100808 forced = "(key-option)";
809 }
810 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100811 if (IS_INTERNAL_SFTP(command)) {
812 s->is_subsystem = s->is_subsystem ?
813 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
814 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100815 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100816 snprintf(session_type, sizeof(session_type),
817 "forced-command %s '%.900s'", forced, command);
818 } else if (s->is_subsystem) {
819 snprintf(session_type, sizeof(session_type),
820 "subsystem '%.900s'", s->subsys);
821 } else if (command == NULL) {
822 snprintf(session_type, sizeof(session_type), "shell");
823 } else {
824 /* NB. we don't log unforced commands to preserve privacy */
825 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000826 }
827
Damien Miller71df7522013-10-15 12:12:02 +1100828 if (s->ttyfd != -1) {
829 tty = s->tty;
830 if (strncmp(tty, "/dev/", 5) == 0)
831 tty += 5;
832 }
833
834 verbose("Starting session: %s%s%s for %s from %.200s port %d",
835 session_type,
836 tty == NULL ? "" : " on ",
837 tty == NULL ? "" : tty,
838 s->pw->pw_name,
839 get_remote_ipaddr(),
840 get_remote_port());
841
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100842#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100843 if (command != NULL)
844 PRIVSEP(audit_run_command(command));
845 else if (s->ttyfd == -1) {
846 char *shell = s->pw->pw_shell;
847
848 if (shell[0] == '\0') /* empty shell means /bin/sh */
849 shell =_PATH_BSHELL;
850 PRIVSEP(audit_run_command(shell));
851 }
852#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000853 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000854 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000855 else
Damien Miller7207f642008-05-19 15:34:50 +1000856 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000857
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000858 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000859
Darren Tucker09991742004-07-17 17:05:14 +1000860 /*
861 * Clear loginmsg: it's the child's responsibility to display
862 * it to the user, otherwise multiple sessions may accumulate
863 * multiple copies of the login messages.
864 */
865 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000866
867 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000868}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000869
Damien Miller942da032000-08-18 13:59:06 +1000870/* administrative, login(1)-like work */
871void
Damien Miller69b69aa2000-10-28 14:19:58 +1100872do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000873{
Damien Miller942da032000-08-18 13:59:06 +1000874 socklen_t fromlen;
875 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000876 struct passwd * pw = s->pw;
877 pid_t pid = getpid();
878
879 /*
880 * Get IP address of client. If the connection is not a socket, let
881 * the address be 0.0.0.0.
882 */
883 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000884 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000885 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000886 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000887 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000888 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000889 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000890 }
891 }
892
893 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000894 if (!use_privsep)
895 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
896 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000897 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000898 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000899
Kevin Steves092f2ef2000-10-14 13:36:13 +0000900#ifdef USE_PAM
901 /*
902 * If password change is needed, do it now.
903 * This needs to occur before the ~/.hushlogin check.
904 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100905 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
906 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000907 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100908 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000909 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000910 }
911#endif
912
Damien Millercf205e82001-04-16 18:29:15 +1000913 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000914 return;
915
Darren Tucker1921ed92004-02-10 13:23:28 +1100916 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000917
Damien Millercf205e82001-04-16 18:29:15 +1000918 do_motd();
919}
920
921/*
922 * Display the message of the day.
923 */
924void
925do_motd(void)
926{
927 FILE *f;
928 char buf[256];
929
Damien Miller942da032000-08-18 13:59:06 +1000930 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000931#ifdef HAVE_LOGIN_CAP
932 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
933 "/etc/motd"), "r");
934#else
Damien Miller942da032000-08-18 13:59:06 +1000935 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000936#endif
Damien Miller942da032000-08-18 13:59:06 +1000937 if (f) {
938 while (fgets(buf, sizeof(buf), f))
939 fputs(buf, stdout);
940 fclose(f);
941 }
942 }
943}
944
Kevin Steves8f63caa2001-07-04 18:23:02 +0000945
946/*
947 * Check for quiet login, either .hushlogin or command given.
948 */
949int
950check_quietlogin(Session *s, const char *command)
951{
952 char buf[256];
953 struct passwd *pw = s->pw;
954 struct stat st;
955
956 /* Return 1 if .hushlogin exists or a command given. */
957 if (command != NULL)
958 return 1;
959 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
960#ifdef HAVE_LOGIN_CAP
961 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
962 return 1;
963#else
964 if (stat(buf, &st) >= 0)
965 return 1;
966#endif
967 return 0;
968}
969
Damien Millerb38eff82000-04-01 11:09:21 +1000970/*
971 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100972 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000973 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000974void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000975child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100976 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000977{
Damien Millerb38eff82000-04-01 11:09:21 +1000978 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000979 u_int envsize;
980 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000981
Damien Miller8569eba2014-03-04 09:35:17 +1100982 if (strchr(name, '=') != NULL) {
983 error("Invalid environment variable \"%.100s\"", name);
984 return;
985 }
986
Damien Millerb38eff82000-04-01 11:09:21 +1000987 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000988 * If we're passed an uninitialized list, allocate a single null
989 * entry before continuing.
990 */
991 if (*envp == NULL && *envsizep == 0) {
992 *envp = xmalloc(sizeof(char *));
993 *envp[0] = NULL;
994 *envsizep = 1;
995 }
996
997 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000998 * Find the slot where the value should be stored. If the variable
999 * already exists, we reuse the slot; otherwise we append a new slot
1000 * at the end of the array, expanding if necessary.
1001 */
1002 env = *envp;
1003 namelen = strlen(name);
1004 for (i = 0; env[i]; i++)
1005 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
1006 break;
1007 if (env[i]) {
1008 /* Reuse the slot. */
Darren Tuckera627d422013-06-02 07:31:17 +10001009 free(env[i]);
Damien Millerb38eff82000-04-01 11:09:21 +10001010 } else {
1011 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +10001012 envsize = *envsizep;
1013 if (i >= envsize - 1) {
1014 if (envsize >= 1000)
1015 fatal("child_set_env: too many env vars");
1016 envsize += 50;
Damien Miller36812092006-03-26 14:22:47 +11001017 env = (*envp) = xrealloc(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +10001018 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +10001019 }
1020 /* Need to set the NULL pointer at end of array beyond the new slot. */
1021 env[i + 1] = NULL;
1022 }
1023
1024 /* Allocate space and format the variable in the appropriate slot. */
1025 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
1026 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
1027}
1028
1029/*
1030 * Reads environment variables from the given file and adds/overrides them
1031 * into the environment. If the file does not exist, this does nothing.
1032 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1033 * and assignments of the form name=value. No other forms are allowed.
1034 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001035static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001036read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001037 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +10001038{
1039 FILE *f;
1040 char buf[4096];
1041 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +10001042 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001043
1044 f = fopen(filename, "r");
1045 if (!f)
1046 return;
1047
1048 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +10001049 if (++lineno > 1000)
1050 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001051 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1052 ;
1053 if (!*cp || *cp == '#' || *cp == '\n')
1054 continue;
Damien Miller14b017d2007-09-17 16:09:15 +10001055
1056 cp[strcspn(cp, "\n")] = '\0';
1057
Damien Millerb38eff82000-04-01 11:09:21 +10001058 value = strchr(cp, '=');
1059 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +10001060 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1061 filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001062 continue;
1063 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001064 /*
1065 * Replace the equals sign by nul, and advance value to
1066 * the value string.
1067 */
Damien Millerb38eff82000-04-01 11:09:21 +10001068 *value = '\0';
1069 value++;
1070 child_set_env(env, envsize, cp, value);
1071 }
1072 fclose(f);
1073}
1074
Darren Tuckere1a790d2003-09-16 11:52:19 +10001075#ifdef HAVE_ETC_DEFAULT_LOGIN
1076/*
1077 * Return named variable from specified environment, or NULL if not present.
1078 */
1079static char *
1080child_get_env(char **env, const char *name)
1081{
1082 int i;
1083 size_t len;
1084
1085 len = strlen(name);
1086 for (i=0; env[i] != NULL; i++)
1087 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1088 return(env[i] + len + 1);
1089 return NULL;
1090}
1091
1092/*
1093 * Read /etc/default/login.
1094 * We pick up the PATH (or SUPATH for root) and UMASK.
1095 */
1096static void
1097read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1098{
1099 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001100 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +10001101 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +10001102
1103 /*
1104 * We don't want to copy the whole file to the child's environment,
1105 * so we use a temporary environment and copy the variables we're
1106 * interested in.
1107 */
1108 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1109
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001110 if (tmpenv == NULL)
1111 return;
1112
Darren Tuckere1a790d2003-09-16 11:52:19 +10001113 if (uid == 0)
1114 var = child_get_env(tmpenv, "SUPATH");
1115 else
1116 var = child_get_env(tmpenv, "PATH");
1117 if (var != NULL)
1118 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +11001119
Darren Tuckere1a790d2003-09-16 11:52:19 +10001120 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1121 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +10001122 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +11001123
Darren Tuckere1a790d2003-09-16 11:52:19 +10001124 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001125 free(tmpenv[i]);
1126 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +10001127}
1128#endif /* HAVE_ETC_DEFAULT_LOGIN */
1129
Damien Miller7dff8692005-05-26 11:34:51 +10001130void
1131copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +10001132{
Damien Millerbb9ffc12002-01-08 10:59:32 +11001133 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +10001134 int i;
1135
Damien Millerbb9ffc12002-01-08 10:59:32 +11001136 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001137 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001138
Damien Millerbb9ffc12002-01-08 10:59:32 +11001139 for(i = 0; source[i] != NULL; i++) {
1140 var_name = xstrdup(source[i]);
1141 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001142 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001143 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001144 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001145 *var_val++ = '\0';
1146
1147 debug3("Copy environment: %s=%s", var_name, var_val);
1148 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001149
Darren Tuckerf60845f2013-06-02 08:07:31 +10001150 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001151 }
1152}
Damien Millercb5e44a2000-09-29 12:12:36 +11001153
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001154static char **
1155do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001156{
Damien Millerb38eff82000-04-01 11:09:21 +10001157 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001158 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001159 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001160 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001161#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001162 char *path = NULL;
1163#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001164
Damien Millerb38eff82000-04-01 11:09:21 +10001165 /* Initialize the environment. */
1166 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001167 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001168 env[0] = NULL;
1169
Damien Millerbac2d8a2000-09-05 16:13:06 +11001170#ifdef HAVE_CYGWIN
1171 /*
1172 * The Windows environment contains some setting which are
1173 * important for a running system. They must not be dropped.
1174 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001175 {
1176 char **p;
1177
1178 p = fetch_windows_environment();
1179 copy_environment(p, &env, &envsize);
1180 free_windows_environment(p);
1181 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001182#endif
1183
Darren Tucker0efd1552003-08-26 11:49:55 +10001184#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001185 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001186 * the childs environment as they see fit
1187 */
1188 ssh_gssapi_do_child(&env, &envsize);
1189#endif
1190
Damien Millerb38eff82000-04-01 11:09:21 +10001191 if (!options.use_login) {
1192 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001193 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001194 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001195 s->env[i].val);
1196
Damien Millerb38eff82000-04-01 11:09:21 +10001197 child_set_env(&env, &envsize, "USER", pw->pw_name);
1198 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001199#ifdef _AIX
1200 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1201#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001202 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001203#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001204 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1205 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1206 else
1207 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001208#else /* HAVE_LOGIN_CAP */
1209# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001210 /*
1211 * There's no standard path on Windows. The path contains
1212 * important components pointing to the system directories,
1213 * needed for loading shared libraries. So the path better
1214 * remains intact here.
1215 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001216# ifdef HAVE_ETC_DEFAULT_LOGIN
1217 read_etc_default_login(&env, &envsize, pw->pw_uid);
1218 path = child_get_env(env, "PATH");
1219# endif /* HAVE_ETC_DEFAULT_LOGIN */
1220 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001221 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001222 s->pw->pw_uid == 0 ?
1223 SUPERUSER_PATH : _PATH_STDPATH);
1224 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001225# endif /* HAVE_CYGWIN */
1226#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001227
1228 snprintf(buf, sizeof buf, "%.200s/%.50s",
1229 _PATH_MAILDIR, pw->pw_name);
1230 child_set_env(&env, &envsize, "MAIL", buf);
1231
1232 /* Normal systems set SHELL by default. */
1233 child_set_env(&env, &envsize, "SHELL", shell);
1234 }
1235 if (getenv("TZ"))
1236 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1237
1238 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001239 if (!options.use_login) {
1240 while (custom_environment) {
1241 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001242 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001243
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001244 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001245 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001246 if (str[i] == '=') {
1247 str[i] = 0;
1248 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001249 }
1250 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +10001251 free(ce->s);
1252 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001253 }
Damien Millerb38eff82000-04-01 11:09:21 +10001254 }
1255
Damien Millerf37e2462002-09-19 11:47:55 +10001256 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001257 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001258 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001259 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1260
Damien Miller00111382003-03-10 11:21:17 +11001261 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001262 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001263 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
Darren Tuckera627d422013-06-02 07:31:17 +10001264 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001265 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1266
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001267 if (s->ttyfd != -1)
1268 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1269 if (s->term)
1270 child_set_env(&env, &envsize, "TERM", s->term);
1271 if (s->display)
1272 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001273 if (original_command)
1274 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1275 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001276
Tim Rice81ed5182002-09-25 17:38:46 -07001277#ifdef _UNICOS
1278 if (cray_tmpdir[0] != '\0')
1279 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1280#endif /* _UNICOS */
1281
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001282 /*
1283 * Since we clear KRB5CCNAME at startup, if it's set now then it
1284 * must have been set by a native authentication method (eg AIX or
1285 * SIA), so copy it to the child.
1286 */
1287 {
1288 char *cp;
1289
1290 if ((cp = getenv("KRB5CCNAME")) != NULL)
1291 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1292 }
1293
Damien Millerb38eff82000-04-01 11:09:21 +10001294#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001295 {
1296 char *cp;
1297
1298 if ((cp = getenv("AUTHSTATE")) != NULL)
1299 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001300 read_environment_file(&env, &envsize, "/etc/environment");
1301 }
Damien Millerb38eff82000-04-01 11:09:21 +10001302#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001303#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001304 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001305 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001306 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001307#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001308#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001309 /*
1310 * Pull in any environment variables that may have
1311 * been set by PAM.
1312 */
Damien Miller4e448a32003-05-14 15:11:48 +10001313 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001314 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001315
Damien Millerc756e9b2003-11-17 21:41:42 +11001316 p = fetch_pam_child_environment();
1317 copy_environment(p, &env, &envsize);
1318 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001319
Damien Millerc756e9b2003-11-17 21:41:42 +11001320 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001321 copy_environment(p, &env, &envsize);
1322 free_pam_environment(p);
1323 }
Damien Millerb38eff82000-04-01 11:09:21 +10001324#endif /* USE_PAM */
1325
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001326 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001327 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001328 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001329
1330 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001331 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001332 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001333 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001334 read_environment_file(&env, &envsize, buf);
1335 }
1336 if (debug_flag) {
1337 /* dump the environment */
1338 fprintf(stderr, "Environment:\n");
1339 for (i = 0; env[i]; i++)
1340 fprintf(stderr, " %.200s\n", env[i]);
1341 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001342 return env;
1343}
1344
1345/*
1346 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1347 * first in this order).
1348 */
1349static void
1350do_rc_files(Session *s, const char *shell)
1351{
1352 FILE *f = NULL;
1353 char cmd[1024];
1354 int do_xauth;
1355 struct stat st;
1356
1357 do_xauth =
1358 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1359
Damien Millera1b48cc2008-03-27 11:02:02 +11001360 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001361 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Millerff0dd882008-05-19 14:55:02 +10001362 !no_user_rc && stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001363 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1364 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1365 if (debug_flag)
1366 fprintf(stderr, "Running %s\n", cmd);
1367 f = popen(cmd, "w");
1368 if (f) {
1369 if (do_xauth)
1370 fprintf(f, "%s %s\n", s->auth_proto,
1371 s->auth_data);
1372 pclose(f);
1373 } else
1374 fprintf(stderr, "Could not run %s\n",
1375 _PATH_SSH_USER_RC);
1376 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1377 if (debug_flag)
1378 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1379 _PATH_SSH_SYSTEM_RC);
1380 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1381 if (f) {
1382 if (do_xauth)
1383 fprintf(f, "%s %s\n", s->auth_proto,
1384 s->auth_data);
1385 pclose(f);
1386 } else
1387 fprintf(stderr, "Could not run %s\n",
1388 _PATH_SSH_SYSTEM_RC);
1389 } else if (do_xauth && options.xauth_location != NULL) {
1390 /* Add authority data to .Xauthority if appropriate. */
1391 if (debug_flag) {
1392 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001393 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001394 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001395 fprintf(stderr,
1396 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001397 options.xauth_location, s->auth_display,
1398 s->auth_proto, s->auth_data);
1399 }
1400 snprintf(cmd, sizeof cmd, "%s -q -",
1401 options.xauth_location);
1402 f = popen(cmd, "w");
1403 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001404 fprintf(f, "remove %s\n",
1405 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001406 fprintf(f, "add %s %s %s\n",
1407 s->auth_display, s->auth_proto,
1408 s->auth_data);
1409 pclose(f);
1410 } else {
1411 fprintf(stderr, "Could not run %s\n",
1412 cmd);
1413 }
1414 }
1415}
1416
1417static void
1418do_nologin(struct passwd *pw)
1419{
1420 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001421 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1422 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001423
1424#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001425 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001426 return;
1427 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001428#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001429 if (pw->pw_uid == 0)
1430 return;
1431 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001432#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001433 if (stat(nl, &sb) == -1) {
1434 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001435 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001436 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001437 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001438
1439 /* /etc/nologin exists. Print its contents if we can and exit. */
1440 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1441 if ((f = fopen(nl, "r")) != NULL) {
1442 while (fgets(buf, sizeof(buf), f))
1443 fputs(buf, stderr);
1444 fclose(f);
1445 }
1446 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001447}
1448
Damien Millerd8cb1f12008-02-10 22:40:12 +11001449/*
1450 * Chroot into a directory after checking it for safety: all path components
1451 * must be root-owned directories with strict permissions.
1452 */
1453static void
1454safely_chroot(const char *path, uid_t uid)
1455{
1456 const char *cp;
1457 char component[MAXPATHLEN];
1458 struct stat st;
1459
1460 if (*path != '/')
1461 fatal("chroot path does not begin at root");
1462 if (strlen(path) >= sizeof(component))
1463 fatal("chroot path too long");
1464
1465 /*
1466 * Descend the path, checking that each component is a
1467 * root-owned directory with strict permissions.
1468 */
1469 for (cp = path; cp != NULL;) {
1470 if ((cp = strchr(cp, '/')) == NULL)
1471 strlcpy(component, path, sizeof(component));
1472 else {
1473 cp++;
1474 memcpy(component, path, cp - path);
1475 component[cp - path] = '\0';
1476 }
1477
1478 debug3("%s: checking '%s'", __func__, component);
1479
1480 if (stat(component, &st) != 0)
1481 fatal("%s: stat(\"%s\"): %s", __func__,
1482 component, strerror(errno));
1483 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1484 fatal("bad ownership or modes for chroot "
1485 "directory %s\"%s\"",
1486 cp == NULL ? "" : "component ", component);
1487 if (!S_ISDIR(st.st_mode))
1488 fatal("chroot path %s\"%s\" is not a directory",
1489 cp == NULL ? "" : "component ", component);
1490
1491 }
1492
1493 if (chdir(path) == -1)
1494 fatal("Unable to chdir to chroot path \"%s\": "
1495 "%s", path, strerror(errno));
1496 if (chroot(path) == -1)
1497 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1498 if (chdir("/") == -1)
1499 fatal("%s: chdir(/) after chroot: %s",
1500 __func__, strerror(errno));
1501 verbose("Changed root directory to \"%s\"", path);
1502}
1503
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001504/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001506do_setusercontext(struct passwd *pw)
1507{
Damien Miller54e37732008-02-10 22:48:55 +11001508 char *chroot_path, *tmp;
Tim Ricea261b8d2014-06-18 16:17:28 -07001509#ifdef USE_LIBIAF
1510 int doing_chroot = 0;
1511#endif
Damien Miller54e37732008-02-10 22:48:55 +11001512
Darren Tucker97528352010-11-05 12:03:05 +11001513 platform_setusercontext(pw);
1514
Darren Tuckerb12fe272010-11-05 14:47:01 +11001515 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001516#ifdef HAVE_LOGIN_CAP
1517 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001518 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001519 perror("unable to set user context");
1520 exit(1);
1521 }
1522#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001523 if (setlogin(pw->pw_name) < 0)
1524 error("setlogin failed: %s", strerror(errno));
1525 if (setgid(pw->pw_gid) < 0) {
1526 perror("setgid");
1527 exit(1);
1528 }
1529 /* Initialize the group list. */
1530 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1531 perror("initgroups");
1532 exit(1);
1533 }
1534 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001535#endif
1536
Darren Tucker920612e2010-11-05 12:36:15 +11001537 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001538
Damien Millerd8cb1f12008-02-10 22:40:12 +11001539 if (options.chroot_directory != NULL &&
1540 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001541 tmp = tilde_expand_filename(options.chroot_directory,
1542 pw->pw_uid);
1543 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1544 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001545 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001546 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001547 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001548 /* Make sure we don't attempt to chroot again */
1549 free(options.chroot_directory);
1550 options.chroot_directory = NULL;
Tim Ricea261b8d2014-06-18 16:17:28 -07001551#ifdef USE_LIBIAF
1552 doing_chroot = 1;
1553#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001554 }
1555
1556#ifdef HAVE_LOGIN_CAP
1557 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1558 perror("unable to set user context (setuser)");
1559 exit(1);
1560 }
Damien Miller58528402013-03-15 11:22:37 +11001561 /*
1562 * FreeBSD's setusercontext() will not apply the user's
1563 * own umask setting unless running with the user's UID.
1564 */
1565 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001566#else
Tim Rice9464ba62014-01-20 17:59:28 -08001567# ifdef USE_LIBIAF
Tim Ricea261b8d2014-06-18 16:17:28 -07001568/* In a chroot environment, the set_id() will always fail; typically
1569 * because of the lack of necessary authentication services and runtime
1570 * such as ./usr/lib/libiaf.so, ./usr/lib/libpam.so.1, and ./etc/passwd
1571 * We skip it in the internal sftp chroot case.
1572 * We'll lose auditing and ACLs but permanently_set_uid will
1573 * take care of the rest.
1574 */
1575 if ((doing_chroot == 0) && set_id(pw->pw_name) != 0) {
Tim Rice617da332014-01-22 19:16:10 -08001576 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001577 }
1578# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001579 /* Permanently switch to the desired uid. */
1580 permanently_set_uid(pw);
1581#endif
Damien Millera56086b2013-04-23 15:24:18 +10001582 } else if (options.chroot_directory != NULL &&
1583 strcasecmp(options.chroot_directory, "none") != 0) {
1584 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001585 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001586
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001587 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1588 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1589}
1590
Ben Lindstrom08105192002-03-22 02:50:06 +00001591static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001592do_pwchange(Session *s)
1593{
Darren Tucker09991742004-07-17 17:05:14 +10001594 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001595 fprintf(stderr, "WARNING: Your password has expired.\n");
1596 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001597 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001598 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001599#ifdef WITH_SELINUX
1600 setexeccon(NULL);
1601#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001602#ifdef PASSWD_NEEDS_USERNAME
1603 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1604 (char *)NULL);
1605#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001606 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001607#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001608 perror("passwd");
1609 } else {
1610 fprintf(stderr,
1611 "Password change required but no TTY available.\n");
1612 }
1613 exit(1);
1614}
1615
1616static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001617launch_login(struct passwd *pw, const char *hostname)
1618{
1619 /* Launch login(1). */
1620
Ben Lindstrom378a4172002-06-07 14:49:56 +00001621 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001622#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001623 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001625#ifdef LOGIN_NO_ENDOPT
1626 "-p", "-f", pw->pw_name, (char *)NULL);
1627#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001628 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001629#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630
1631 /* Login couldn't be executed, die. */
1632
1633 perror("login");
1634 exit(1);
1635}
1636
Darren Tucker23bc8d02004-02-06 16:24:31 +11001637static void
1638child_close_fds(void)
1639{
Damien Miller85b45e02013-07-20 13:21:52 +10001640 extern AuthenticationConnection *auth_conn;
1641
1642 if (auth_conn) {
1643 ssh_close_authentication_connection(auth_conn);
1644 auth_conn = NULL;
1645 }
1646
Darren Tucker23bc8d02004-02-06 16:24:31 +11001647 if (packet_get_connection_in() == packet_get_connection_out())
1648 close(packet_get_connection_in());
1649 else {
1650 close(packet_get_connection_in());
1651 close(packet_get_connection_out());
1652 }
1653 /*
1654 * Close all descriptors related to channels. They will still remain
1655 * open in the parent.
1656 */
1657 /* XXX better use close-on-exec? -markus */
1658 channel_close_all();
1659
1660 /*
1661 * Close any extra file descriptors. Note that there may still be
1662 * descriptors left by system functions. They will be closed later.
1663 */
1664 endpwent();
1665
1666 /*
Damien Miller788f2122005-11-05 15:14:59 +11001667 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001668 * hanging around in clients. Note that we want to do this after
1669 * initgroups, because at least on Solaris 2.3 it leaves file
1670 * descriptors open.
1671 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001672 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001673}
1674
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001675/*
1676 * Performs common processing for the child, such as setting up the
1677 * environment, closing extra file descriptors, setting the user and group
1678 * ids, and executing the command or shell.
1679 */
Damien Millerdfc24252008-02-10 22:29:40 +11001680#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001681void
1682do_child(Session *s, const char *command)
1683{
1684 extern char **environ;
1685 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001686 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001687 const char *shell, *shell0, *hostname = NULL;
1688 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001689 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001690
1691 /* remove hostkey from the child's memory */
1692 destroy_sensitive_data();
1693
Darren Tucker23bc8d02004-02-06 16:24:31 +11001694 /* Force a password change */
1695 if (s->authctxt->force_pwchange) {
1696 do_setusercontext(pw);
1697 child_close_fds();
1698 do_pwchange(s);
1699 exit(1);
1700 }
1701
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001702 /* login(1) is only called if we execute the login shell */
1703 if (options.use_login && command != NULL)
1704 options.use_login = 0;
1705
Tim Rice81ed5182002-09-25 17:38:46 -07001706#ifdef _UNICOS
1707 cray_setup(pw->pw_uid, pw->pw_name, command);
1708#endif /* _UNICOS */
1709
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001710 /*
1711 * Login(1) does this as well, and it needs uid 0 for the "-h"
1712 * switch, so we let login(1) to this for us.
1713 */
1714 if (!options.use_login) {
1715#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001716 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001717 if (!check_quietlogin(s, command))
1718 do_motd();
1719#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001720 /* When PAM is enabled we rely on it to do the nologin check */
1721 if (!options.use_pam)
1722 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001723 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001724 /*
1725 * PAM session modules in do_setusercontext may have
1726 * generated messages, so if this in an interactive
1727 * login then display them too.
1728 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001729 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001730 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001731#endif /* HAVE_OSF_SIA */
1732 }
1733
Darren Tucker69687f42004-09-11 22:17:26 +10001734#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001735 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1736 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001737 display_loginmsg();
1738 exit(254);
1739 }
1740#endif
1741
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001742 /*
1743 * Get the shell from the password data. An empty shell field is
1744 * legal, and means /bin/sh.
1745 */
1746 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001747
1748 /*
1749 * Make sure $SHELL points to the shell from the password file,
1750 * even if shell is overridden from login.conf
1751 */
1752 env = do_setup_env(s, shell);
1753
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001754#ifdef HAVE_LOGIN_CAP
1755 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1756#endif
1757
Damien Millerad833b32000-08-23 10:46:23 +10001758 /* we have to stash the hostname before we close our socket. */
1759 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001760 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001761 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001762 /*
1763 * Close the connection descriptors; note that this is the child, and
1764 * the server will still have the socket open, and it is important
1765 * that we do not shutdown it. Note that the descriptors cannot be
1766 * closed before building the environment, as we call
1767 * get_remote_ipaddr there.
1768 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001769 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001770
Damien Millerb38eff82000-04-01 11:09:21 +10001771 /*
Damien Miller05eda432002-02-10 18:32:28 +11001772 * Must take new environment into use so that .ssh/rc,
1773 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001774 */
1775 environ = env;
1776
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001777#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001778 /*
1779 * At this point, we check to see if AFS is active and if we have
1780 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1781 * if we can (and need to) extend the ticket into an AFS token. If
1782 * we don't do this, we run into potential problems if the user's
1783 * home directory is in AFS and it's not world-readable.
1784 */
1785
1786 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001787 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001788 char cell[64];
1789
1790 debug("Getting AFS token");
1791
1792 k_setpag();
1793
1794 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1795 krb5_afslog(s->authctxt->krb5_ctx,
1796 s->authctxt->krb5_fwd_ccache, cell, NULL);
1797
1798 krb5_afslog_home(s->authctxt->krb5_ctx,
1799 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1800 }
1801#endif
1802
Damien Miller788f2122005-11-05 15:14:59 +11001803 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001804 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001805 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001806#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001807 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001808#endif
Damien Miller7aa46ec2010-06-26 09:37:57 +10001809 if (r || options.chroot_directory == NULL ||
1810 strcasecmp(options.chroot_directory, "none") == 0)
Damien Miller6051c942008-06-16 07:53:16 +10001811 fprintf(stderr, "Could not chdir to home "
1812 "directory %s: %s\n", pw->pw_dir,
1813 strerror(errno));
1814 if (r)
1815 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001816 }
1817
Damien Millera1939002008-03-15 17:27:58 +11001818 closefrom(STDERR_FILENO + 1);
1819
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001820 if (!options.use_login)
1821 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001822
1823 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001824 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001825
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001826 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1827 printf("This service allows sftp connections only.\n");
1828 fflush(NULL);
1829 exit(1);
1830 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001831 extern int optind, optreset;
1832 int i;
1833 char *p, *args;
1834
Darren Tuckerac46a912009-06-21 17:55:23 +10001835 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001836 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001837 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1838 if (i < ARGV_MAX - 1)
1839 argv[i++] = p;
1840 argv[i] = NULL;
1841 optind = optreset = 1;
1842 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001843#ifdef WITH_SELINUX
1844 ssh_selinux_change_context("sftpd_t");
1845#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001846 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001847 }
1848
Darren Tucker695ed392009-10-07 09:02:18 +11001849 fflush(NULL);
1850
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001851 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001852 launch_login(pw, hostname);
1853 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001854 }
1855
1856 /* Get the last component of the shell name. */
1857 if ((shell0 = strrchr(shell, '/')) != NULL)
1858 shell0++;
1859 else
1860 shell0 = shell;
1861
Damien Millerb38eff82000-04-01 11:09:21 +10001862 /*
1863 * If we have no command, execute the shell. In this case, the shell
1864 * name to be passed in argv[0] is preceded by '-' to indicate that
1865 * this is a login shell.
1866 */
1867 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001868 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001869
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001870 /* Start the shell. Set initial character to '-'. */
1871 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001872
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001873 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1874 >= sizeof(argv0) - 1) {
1875 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001876 perror(shell);
1877 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001878 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001879
1880 /* Execute the shell. */
1881 argv[0] = argv0;
1882 argv[1] = NULL;
1883 execve(shell, argv, env);
1884
1885 /* Executing the shell failed. */
1886 perror(shell);
1887 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001888 }
1889 /*
1890 * Execute the command using the user's shell. This uses the -c
1891 * option to execute the command.
1892 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001893 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001894 argv[1] = "-c";
1895 argv[2] = (char *) command;
1896 argv[3] = NULL;
1897 execve(shell, argv, env);
1898 perror(shell);
1899 exit(1);
1900}
1901
Damien Miller7207f642008-05-19 15:34:50 +10001902void
1903session_unused(int id)
1904{
1905 debug3("%s: session id %d unused", __func__, id);
1906 if (id >= options.max_sessions ||
1907 id >= sessions_nalloc) {
1908 fatal("%s: insane session id %d (max %d nalloc %d)",
1909 __func__, id, options.max_sessions, sessions_nalloc);
1910 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001911 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001912 sessions[id].self = id;
1913 sessions[id].used = 0;
1914 sessions[id].chanid = -1;
1915 sessions[id].ptyfd = -1;
1916 sessions[id].ttyfd = -1;
1917 sessions[id].ptymaster = -1;
1918 sessions[id].x11_chanids = NULL;
1919 sessions[id].next_unused = sessions_first_unused;
1920 sessions_first_unused = id;
1921}
1922
Damien Millerb38eff82000-04-01 11:09:21 +10001923Session *
1924session_new(void)
1925{
Damien Miller7207f642008-05-19 15:34:50 +10001926 Session *s, *tmp;
1927
1928 if (sessions_first_unused == -1) {
1929 if (sessions_nalloc >= options.max_sessions)
1930 return NULL;
1931 debug2("%s: allocate (allocated %d max %d)",
1932 __func__, sessions_nalloc, options.max_sessions);
1933 tmp = xrealloc(sessions, sessions_nalloc + 1,
1934 sizeof(*sessions));
1935 if (tmp == NULL) {
1936 error("%s: cannot allocate %d sessions",
1937 __func__, sessions_nalloc + 1);
1938 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001939 }
Damien Miller7207f642008-05-19 15:34:50 +10001940 sessions = tmp;
1941 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001942 }
Damien Miller7207f642008-05-19 15:34:50 +10001943
1944 if (sessions_first_unused >= sessions_nalloc ||
1945 sessions_first_unused < 0) {
1946 fatal("%s: insane first_unused %d max %d nalloc %d",
1947 __func__, sessions_first_unused, options.max_sessions,
1948 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001949 }
Damien Miller7207f642008-05-19 15:34:50 +10001950
1951 s = &sessions[sessions_first_unused];
1952 if (s->used) {
1953 fatal("%s: session %d already used",
1954 __func__, sessions_first_unused);
1955 }
1956 sessions_first_unused = s->next_unused;
1957 s->used = 1;
1958 s->next_unused = -1;
1959 debug("session_new: session %d", s->self);
1960
1961 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001962}
1963
Ben Lindstrombba81212001-06-25 05:01:22 +00001964static void
Damien Millerb38eff82000-04-01 11:09:21 +10001965session_dump(void)
1966{
1967 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001968 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001969 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001970
1971 debug("dump: used %d next_unused %d session %d %p "
1972 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001973 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001974 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001975 s->self,
1976 s,
1977 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001978 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001979 }
1980}
1981
Damien Millerefb4afe2000-04-12 18:45:05 +10001982int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001983session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001984{
1985 Session *s = session_new();
1986 debug("session_open: channel %d", chanid);
1987 if (s == NULL) {
1988 error("no more sessions");
1989 return 0;
1990 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001991 s->authctxt = authctxt;
1992 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001993 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001994 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001995 debug("session_open: session %d: link with channel %d", s->self, chanid);
1996 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001997 return 1;
1998}
1999
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002000Session *
2001session_by_tty(char *tty)
2002{
2003 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002004 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002005 Session *s = &sessions[i];
2006 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
2007 debug("session_by_tty: session %d tty %s", i, tty);
2008 return s;
2009 }
2010 }
2011 debug("session_by_tty: unknown tty %.100s", tty);
2012 session_dump();
2013 return NULL;
2014}
2015
Ben Lindstrombba81212001-06-25 05:01:22 +00002016static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002017session_by_channel(int id)
2018{
2019 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002020 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002021 Session *s = &sessions[i];
2022 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10002023 debug("session_by_channel: session %d channel %d",
2024 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002025 return s;
2026 }
2027 }
2028 debug("session_by_channel: unknown channel %d", id);
2029 session_dump();
2030 return NULL;
2031}
2032
Ben Lindstrombba81212001-06-25 05:01:22 +00002033static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10002034session_by_x11_channel(int id)
2035{
2036 int i, j;
2037
Damien Miller7207f642008-05-19 15:34:50 +10002038 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002039 Session *s = &sessions[i];
2040
2041 if (s->x11_chanids == NULL || !s->used)
2042 continue;
2043 for (j = 0; s->x11_chanids[j] != -1; j++) {
2044 if (s->x11_chanids[j] == id) {
2045 debug("session_by_x11_channel: session %d "
2046 "channel %d", s->self, id);
2047 return s;
2048 }
2049 }
2050 }
2051 debug("session_by_x11_channel: unknown channel %d", id);
2052 session_dump();
2053 return NULL;
2054}
2055
2056static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002057session_by_pid(pid_t pid)
2058{
2059 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00002060 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10002061 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002062 Session *s = &sessions[i];
2063 if (s->used && s->pid == pid)
2064 return s;
2065 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002066 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002067 session_dump();
2068 return NULL;
2069}
2070
Ben Lindstrombba81212001-06-25 05:01:22 +00002071static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002072session_window_change_req(Session *s)
2073{
2074 s->col = packet_get_int();
2075 s->row = packet_get_int();
2076 s->xpixel = packet_get_int();
2077 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002078 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10002079 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2080 return 1;
2081}
2082
Ben Lindstrombba81212001-06-25 05:01:22 +00002083static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002084session_pty_req(Session *s)
2085{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002086 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00002087 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002088
Damien Miller5ff30c62013-10-30 22:21:50 +11002089 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00002090 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10002091 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002092 }
2093 if (s->ttyfd != -1) {
2094 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10002095 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002096 }
2097
Damien Millerefb4afe2000-04-12 18:45:05 +10002098 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002099
2100 if (compat20) {
2101 s->col = packet_get_int();
2102 s->row = packet_get_int();
2103 } else {
2104 s->row = packet_get_int();
2105 s->col = packet_get_int();
2106 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002107 s->xpixel = packet_get_int();
2108 s->ypixel = packet_get_int();
2109
2110 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002111 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002112 s->term = NULL;
2113 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00002114
Damien Millerefb4afe2000-04-12 18:45:05 +10002115 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00002116 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10002117 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2118 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10002119 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002120 s->term = NULL;
2121 s->ptyfd = -1;
2122 s->ttyfd = -1;
2123 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10002124 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002125 }
2126 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002127
2128 /* for SSH1 the tty modes length is not given */
2129 if (!compat20)
2130 n_bytes = packet_remaining();
2131 tty_parse_modes(s->ttyfd, &n_bytes);
2132
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002133 if (!use_privsep)
2134 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002135
2136 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10002137 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2138
Damien Miller48b03fc2002-01-22 23:11:40 +11002139 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10002140 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002141 return 1;
2142}
2143
Ben Lindstrombba81212001-06-25 05:01:22 +00002144static int
Damien Millerbd483e72000-04-30 10:00:53 +10002145session_subsystem_req(Session *s)
2146{
Damien Millerae452462001-10-10 15:08:06 +10002147 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002148 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10002149 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11002150 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10002151 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10002152
Damien Miller71df7522013-10-15 12:12:02 +11002153 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002154 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11002155 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10002156 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10002157
Damien Millerf6d9e222000-06-18 14:50:44 +10002158 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11002159 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10002160 prog = options.subsystem_command[i];
2161 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002162 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11002163 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002164 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11002165 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002166 if (stat(prog, &st) < 0)
2167 debug("subsystem: cannot stat %s: %s",
2168 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11002169 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002170 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10002171 }
Damien Miller7207f642008-05-19 15:34:50 +10002172 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11002173 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10002174 }
2175 }
2176
2177 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11002178 logit("subsystem request for %.100s by user %s failed, "
2179 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10002180
Damien Millerbd483e72000-04-30 10:00:53 +10002181 return success;
2182}
2183
Ben Lindstrombba81212001-06-25 05:01:22 +00002184static int
Damien Millerbd483e72000-04-30 10:00:53 +10002185session_x11_req(Session *s)
2186{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002187 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002188
Damien Miller2b9b0452005-07-17 17:19:24 +10002189 if (s->auth_proto != NULL || s->auth_data != NULL) {
2190 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002191 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002192 return 0;
2193 }
Damien Millerbd483e72000-04-30 10:00:53 +10002194 s->single_connection = packet_get_char();
2195 s->auth_proto = packet_get_string(NULL);
2196 s->auth_data = packet_get_string(NULL);
2197 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002198 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002199
Ben Lindstrom768176b2001-06-09 01:29:12 +00002200 success = session_setup_x11fwd(s);
2201 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002202 free(s->auth_proto);
2203 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002204 s->auth_proto = NULL;
2205 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002206 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002207 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002208}
2209
Ben Lindstrombba81212001-06-25 05:01:22 +00002210static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002211session_shell_req(Session *s)
2212{
Damien Miller48b03fc2002-01-22 23:11:40 +11002213 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002214 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002215}
2216
Ben Lindstrombba81212001-06-25 05:01:22 +00002217static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002218session_exec_req(Session *s)
2219{
Damien Miller7207f642008-05-19 15:34:50 +10002220 u_int len, success;
2221
Damien Millerf6d9e222000-06-18 14:50:44 +10002222 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002223 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002224 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002225 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002226 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002227}
2228
Ben Lindstrombba81212001-06-25 05:01:22 +00002229static int
Damien Miller54c45982003-05-15 10:20:13 +10002230session_break_req(Session *s)
2231{
Damien Miller54c45982003-05-15 10:20:13 +10002232
Darren Tucker1f8311c2004-05-13 16:39:33 +10002233 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002234 packet_check_eom();
2235
Darren Tucker9c5d5532011-11-04 10:55:24 +11002236 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002237 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002238 return 1;
2239}
2240
2241static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002242session_env_req(Session *s)
2243{
2244 char *name, *val;
2245 u_int name_len, val_len, i;
2246
Damien Miller8569eba2014-03-04 09:35:17 +11002247 name = packet_get_cstring(&name_len);
2248 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002249 packet_check_eom();
2250
2251 /* Don't set too many environment variables */
2252 if (s->num_env > 128) {
2253 debug2("Ignoring env request %s: too many env vars", name);
2254 goto fail;
2255 }
2256
2257 for (i = 0; i < options.num_accept_env; i++) {
2258 if (match_pattern(name, options.accept_env[i])) {
2259 debug2("Setting env %d: %s=%s", s->num_env, name, val);
Damien Miller36812092006-03-26 14:22:47 +11002260 s->env = xrealloc(s->env, s->num_env + 1,
2261 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002262 s->env[s->num_env].name = name;
2263 s->env[s->num_env].val = val;
2264 s->num_env++;
2265 return (1);
2266 }
2267 }
2268 debug2("Ignoring env request %s: disallowed name", name);
2269
2270 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002271 free(name);
2272 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002273 return (0);
2274}
2275
2276static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002277session_auth_agent_req(Session *s)
2278{
2279 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002280 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002281 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002282 debug("session_auth_agent_req: no_agent_forwarding_flag");
2283 return 0;
2284 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002285 if (called) {
2286 return 0;
2287 } else {
2288 called = 1;
2289 return auth_input_request_forwarding(s->pw);
2290 }
2291}
2292
Damien Millerc7ef63d2002-02-05 12:21:42 +11002293int
2294session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002295{
Damien Millerefb4afe2000-04-12 18:45:05 +10002296 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002297 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002298
Damien Millerc7ef63d2002-02-05 12:21:42 +11002299 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002300 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002301 c->self, rtype);
2302 return 0;
2303 }
2304 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002305
2306 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002307 * a session is in LARVAL state until a shell, a command
2308 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002309 */
2310 if (c->type == SSH_CHANNEL_LARVAL) {
2311 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002312 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002313 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002314 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002315 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002316 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002317 } else if (strcmp(rtype, "x11-req") == 0) {
2318 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002319 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2320 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002321 } else if (strcmp(rtype, "subsystem") == 0) {
2322 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002323 } else if (strcmp(rtype, "env") == 0) {
2324 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002325 }
2326 }
2327 if (strcmp(rtype, "window-change") == 0) {
2328 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002329 } else if (strcmp(rtype, "break") == 0) {
2330 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002331 }
Damien Millera6b1d162004-06-30 22:41:07 +10002332
Damien Millerc7ef63d2002-02-05 12:21:42 +11002333 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002334}
2335
2336void
Damien Miller8853ca52010-06-26 10:00:14 +10002337session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2338 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002339{
2340 if (!compat20)
2341 fatal("session_set_fds: called for proto != 2.0");
2342 /*
2343 * now that have a child and a pipe to the child,
2344 * we can activate our channel and register the fd's
2345 */
2346 if (s->chanid == -1)
2347 fatal("no channel for session %d", s->self);
2348 channel_set_fds(s->chanid,
2349 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002350 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002351 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002352}
2353
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002354/*
2355 * Function to perform pty cleanup. Also called if we get aborted abnormally
2356 * (e.g., due to a dropped connection).
2357 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002358void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002359session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002360{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002361 if (s == NULL) {
2362 error("session_pty_cleanup: no session");
2363 return;
2364 }
2365 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002366 return;
2367
Kevin Steves43cdef32001-02-11 14:12:08 +00002368 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002369
Damien Millerb38eff82000-04-01 11:09:21 +10002370 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002371 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002372 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002373
2374 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002375 if (getuid() == 0)
2376 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002377
2378 /*
2379 * Close the server side of the socket pairs. We must do this after
2380 * the pty cleanup, so that another process doesn't get this pty
2381 * while we're still cleaning up.
2382 */
Damien Miller7207f642008-05-19 15:34:50 +10002383 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2384 error("close(s->ptymaster/%d): %s",
2385 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002386
2387 /* unlink pty from session */
2388 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002389}
Damien Millerefb4afe2000-04-12 18:45:05 +10002390
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002391void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002392session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002393{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002394 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002395}
2396
Damien Miller5a80bba2002-09-04 16:39:02 +10002397static char *
2398sig2name(int sig)
2399{
2400#define SSH_SIG(x) if (sig == SIG ## x) return #x
2401 SSH_SIG(ABRT);
2402 SSH_SIG(ALRM);
2403 SSH_SIG(FPE);
2404 SSH_SIG(HUP);
2405 SSH_SIG(ILL);
2406 SSH_SIG(INT);
2407 SSH_SIG(KILL);
2408 SSH_SIG(PIPE);
2409 SSH_SIG(QUIT);
2410 SSH_SIG(SEGV);
2411 SSH_SIG(TERM);
2412 SSH_SIG(USR1);
2413 SSH_SIG(USR2);
2414#undef SSH_SIG
2415 return "SIG@openssh.com";
2416}
2417
Ben Lindstrombba81212001-06-25 05:01:22 +00002418static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002419session_close_x11(int id)
2420{
2421 Channel *c;
2422
Damien Millerd47c62a2005-12-13 19:33:57 +11002423 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002424 debug("session_close_x11: x11 channel %d missing", id);
2425 } else {
2426 /* Detach X11 listener */
2427 debug("session_close_x11: detach x11 channel %d", id);
2428 channel_cancel_cleanup(id);
2429 if (c->ostate != CHAN_OUTPUT_CLOSED)
2430 chan_mark_dead(c);
2431 }
2432}
2433
2434static void
2435session_close_single_x11(int id, void *arg)
2436{
2437 Session *s;
2438 u_int i;
2439
2440 debug3("session_close_single_x11: channel %d", id);
2441 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002442 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002443 fatal("session_close_single_x11: no x11 channel %d", id);
2444 for (i = 0; s->x11_chanids[i] != -1; i++) {
2445 debug("session_close_single_x11: session %d: "
2446 "closing channel %d", s->self, s->x11_chanids[i]);
2447 /*
2448 * The channel "id" is already closing, but make sure we
2449 * close all of its siblings.
2450 */
2451 if (s->x11_chanids[i] != id)
2452 session_close_x11(s->x11_chanids[i]);
2453 }
Darren Tuckera627d422013-06-02 07:31:17 +10002454 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002455 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002456 free(s->display);
2457 s->display = NULL;
2458 free(s->auth_proto);
2459 s->auth_proto = NULL;
2460 free(s->auth_data);
2461 s->auth_data = NULL;
2462 free(s->auth_display);
2463 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002464}
2465
2466static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002467session_exit_message(Session *s, int status)
2468{
2469 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002470
2471 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002472 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002473 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002474 debug("session_exit_message: session %d channel %d pid %ld",
2475 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002476
2477 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002478 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002479 packet_put_int(WEXITSTATUS(status));
2480 packet_send();
2481 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002482 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002483 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002484#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002485 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002486#else /* WCOREDUMP */
2487 packet_put_char(0);
2488#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002489 packet_put_cstring("");
2490 packet_put_cstring("");
2491 packet_send();
2492 } else {
2493 /* Some weird exit cause. Just exit. */
2494 packet_disconnect("wait returned status %04x.", status);
2495 }
2496
2497 /* disconnect channel */
2498 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002499
2500 /*
2501 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002502 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002503 * by session_close_by_channel when the childs close their fds.
2504 */
2505 channel_register_cleanup(c->self, session_close_by_channel, 1);
2506
Damien Miller166fca82000-04-20 07:42:21 +10002507 /*
2508 * emulate a write failure with 'chan_write_failed', nobody will be
2509 * interested in data we write.
2510 * Note that we must not call 'chan_read_failed', since there could
2511 * be some more data waiting in the pipe.
2512 */
Damien Millerbd483e72000-04-30 10:00:53 +10002513 if (c->ostate != CHAN_OUTPUT_CLOSED)
2514 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002515}
2516
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002517void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002518session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002519{
Damien Millereccb9de2005-06-17 12:59:34 +10002520 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002521
Ben Lindstromce0f6342002-06-11 16:42:49 +00002522 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002523 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002524 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002525 free(s->term);
2526 free(s->display);
2527 free(s->x11_chanids);
2528 free(s->auth_display);
2529 free(s->auth_data);
2530 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002531 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002532 if (s->env != NULL) {
2533 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002534 free(s->env[i].name);
2535 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002536 }
Darren Tuckera627d422013-06-02 07:31:17 +10002537 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002538 }
Damien Millere247cc42000-05-07 12:03:14 +10002539 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002540 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002541}
2542
2543void
2544session_close_by_pid(pid_t pid, int status)
2545{
2546 Session *s = session_by_pid(pid);
2547 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002548 debug("session_close_by_pid: no session for pid %ld",
2549 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002550 return;
2551 }
2552 if (s->chanid != -1)
2553 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002554 if (s->ttyfd != -1)
2555 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002556 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002557}
2558
2559/*
2560 * this is called when a channel dies before
2561 * the session 'child' itself dies
2562 */
2563void
2564session_close_by_channel(int id, void *arg)
2565{
2566 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002567 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002568
Damien Millerefb4afe2000-04-12 18:45:05 +10002569 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002570 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002571 return;
2572 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002573 debug("session_close_by_channel: channel %d child %ld",
2574 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002575 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002576 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002577 /*
2578 * delay detach of session, but release pty, since
2579 * the fd's to the child are already closed
2580 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002581 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002582 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002583 return;
2584 }
2585 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002586 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002587
2588 /* Close any X11 listeners associated with this session */
2589 if (s->x11_chanids != NULL) {
2590 for (i = 0; s->x11_chanids[i] != -1; i++) {
2591 session_close_x11(s->x11_chanids[i]);
2592 s->x11_chanids[i] = -1;
2593 }
2594 }
2595
Damien Millerefb4afe2000-04-12 18:45:05 +10002596 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002597 session_close(s);
2598}
2599
2600void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002601session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002602{
2603 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002604 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002605 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002606 if (s->used) {
2607 if (closefunc != NULL)
2608 closefunc(s);
2609 else
2610 session_close(s);
2611 }
Damien Miller52b77be2001-10-10 15:14:37 +10002612 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002613}
2614
Ben Lindstrombba81212001-06-25 05:01:22 +00002615static char *
Damien Millere247cc42000-05-07 12:03:14 +10002616session_tty_list(void)
2617{
2618 static char buf[1024];
2619 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002620 char *cp;
2621
Damien Millere247cc42000-05-07 12:03:14 +10002622 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002623 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002624 Session *s = &sessions[i];
2625 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002626
Damien Millera8ed44b2003-01-10 09:53:12 +11002627 if (strncmp(s->tty, "/dev/", 5) != 0) {
2628 cp = strrchr(s->tty, '/');
2629 cp = (cp == NULL) ? s->tty : cp + 1;
2630 } else
2631 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002632
Damien Millere247cc42000-05-07 12:03:14 +10002633 if (buf[0] != '\0')
2634 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002635 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002636 }
2637 }
2638 if (buf[0] == '\0')
2639 strlcpy(buf, "notty", sizeof buf);
2640 return buf;
2641}
2642
2643void
2644session_proctitle(Session *s)
2645{
2646 if (s->pw == NULL)
2647 error("no user for session %d", s->self);
2648 else
2649 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2650}
2651
Ben Lindstrom768176b2001-06-09 01:29:12 +00002652int
2653session_setup_x11fwd(Session *s)
2654{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002655 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002656 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002657 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002658 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002659
2660 if (no_x11_forwarding_flag) {
2661 packet_send_debug("X11 forwarding disabled in user configuration file.");
2662 return 0;
2663 }
2664 if (!options.x11_forwarding) {
2665 debug("X11 forwarding disabled in server configuration file.");
2666 return 0;
2667 }
2668 if (!options.xauth_location ||
2669 (stat(options.xauth_location, &st) == -1)) {
2670 packet_send_debug("No xauth program; cannot forward with spoofing.");
2671 return 0;
2672 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002673 if (options.use_login) {
2674 packet_send_debug("X11 forwarding disabled; "
2675 "not compatible with UseLogin=yes.");
2676 return 0;
2677 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002678 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002679 debug("X11 display already set.");
2680 return 0;
2681 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002682 if (x11_create_display_inet(options.x11_display_offset,
2683 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002684 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002685 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002686 return 0;
2687 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002688 for (i = 0; s->x11_chanids[i] != -1; i++) {
2689 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002690 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002691 }
Kevin Steves366298c2001-12-19 17:58:01 +00002692
2693 /* Set up a suitable value for the DISPLAY variable. */
2694 if (gethostname(hostname, sizeof(hostname)) < 0)
2695 fatal("gethostname: %.100s", strerror(errno));
2696 /*
2697 * auth_display must be used as the displayname when the
2698 * authorization entry is added with xauth(1). This will be
2699 * different than the DISPLAY string for localhost displays.
2700 */
Damien Miller95c249f2002-02-05 12:11:34 +11002701 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002702 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002703 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002704 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002705 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002706 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002707 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002708 } else {
2709#ifdef IPADDR_IN_DISPLAY
2710 struct hostent *he;
2711 struct in_addr my_addr;
2712
2713 he = gethostbyname(hostname);
2714 if (he == NULL) {
2715 error("Can't get IP address for X11 DISPLAY.");
2716 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2717 return 0;
2718 }
2719 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002720 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002721 s->display_number, s->screen);
2722#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002723 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002724 s->display_number, s->screen);
2725#endif
2726 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002727 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002728 }
2729
Ben Lindstrom768176b2001-06-09 01:29:12 +00002730 return 1;
2731}
2732
Ben Lindstrombba81212001-06-25 05:01:22 +00002733static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002734do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002735{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002736 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002737}
2738
2739void
2740do_cleanup(Authctxt *authctxt)
2741{
2742 static int called = 0;
2743
2744 debug("do_cleanup");
2745
2746 /* no cleanup if we're in the child for login shell */
2747 if (is_child)
2748 return;
2749
2750 /* avoid double cleanup */
2751 if (called)
2752 return;
2753 called = 1;
2754
Darren Tucker9142e1c2007-08-16 23:28:04 +10002755 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002756 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002757
2758#ifdef USE_PAM
2759 if (options.use_pam) {
2760 sshpam_cleanup();
2761 sshpam_thread_cleanup();
2762 }
2763#endif
2764
2765 if (!authctxt->authenticated)
2766 return;
2767
Darren Tucker3e33cec2003-10-02 16:12:36 +10002768#ifdef KRB5
2769 if (options.kerberos_ticket_cleanup &&
2770 authctxt->krb5_ctx)
2771 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002772#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002773
2774#ifdef GSSAPI
2775 if (compat20 && options.gss_cleanup_creds)
2776 ssh_gssapi_cleanup_creds();
2777#endif
2778
2779 /* remove agent socket */
2780 auth_sock_cleanup_proc(authctxt->pw);
2781
2782 /*
2783 * Cleanup ptys/utmp only if privsep is disabled,
2784 * or if running in monitor.
2785 */
2786 if (!use_privsep || mm_is_monitor())
2787 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002788}