blob: c0b0a942e2f8bca724fb6f6110287e136da7bd22 [file] [log] [blame]
Damien Millerc10bf4d2014-04-20 12:58:04 +10001/* $OpenBSD: session.c,v 1.271 2014/03/03 22:22:30 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 Miller6645e7a2006-03-15 14:42:54 +110052#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110053#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100055#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110056#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100057#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100058#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100059#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100060#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100061#include <unistd.h>
Damien Millerb38eff82000-04-01 11:09:21 +100062
Damien Millerb84886b2008-05-19 15:05:07 +100063#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100064#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100065#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "ssh1.h"
67#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000068#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100069#include "packet.h"
70#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100071#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100072#include "uidswap.h"
73#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000074#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100075#include "key.h"
76#include "cipher.h"
77#ifdef GSSAPI
78#include "ssh-gss.h"
79#endif
80#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100081#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100082#include "auth-options.h"
Damien Miller85b45e02013-07-20 13:21:52 +100083#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084#include "pathnames.h"
85#include "log.h"
86#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000087#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000088#include "serverloop.h"
89#include "canohost.h"
Damien Millerd8cb1f12008-02-10 22:40:12 +110090#include "misc.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000091#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100092#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110094#include "sftp.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100095
Darren Tucker3c78c5e2004-01-23 22:03:10 +110096#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110097#include <kafs.h>
98#endif
99
Damien Miller14684a12011-05-20 11:23:07 +1000100#ifdef WITH_SELINUX
101#include <selinux/selinux.h>
102#endif
103
Damien Millerad793d52008-11-03 19:17:57 +1100104#define IS_INTERNAL_SFTP(c) \
105 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
106 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
107 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
108 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
109
Damien Millerb38eff82000-04-01 11:09:21 +1000110/* func */
111
112Session *session_new(void);
Damien Miller8853ca52010-06-26 10:00:14 +1000113void session_set_fds(Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000114void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000115void session_proctitle(Session *);
116int session_setup_x11fwd(Session *);
Damien Miller7207f642008-05-19 15:34:50 +1000117int do_exec_pty(Session *, const char *);
118int do_exec_no_pty(Session *, const char *);
119int do_exec(Session *, const char *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000120void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000121#ifdef LOGIN_NEEDS_UTMPX
122static void do_pre_login(Session *s);
123#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000124void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000125void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000126int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000127
Ben Lindstrombba81212001-06-25 05:01:22 +0000128static void do_authenticated1(Authctxt *);
129static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000130
Ben Lindstrombba81212001-06-25 05:01:22 +0000131static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000132
Damien Millerb38eff82000-04-01 11:09:21 +1000133/* import */
134extern ServerOptions options;
135extern char *__progname;
136extern int log_stderr;
137extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000138extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000139extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000140extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000141extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000142
Damien Miller7b28dc52000-09-05 13:34:53 +1100143/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000144const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100145
Damien Millerb38eff82000-04-01 11:09:21 +1000146/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000147static int sessions_first_unused = -1;
148static int sessions_nalloc = 0;
149static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100150
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100151#define SUBSYSTEM_NONE 0
152#define SUBSYSTEM_EXT 1
153#define SUBSYSTEM_INT_SFTP 2
154#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100155
Damien Millerad833b32000-08-23 10:46:23 +1000156#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000157login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000158#endif
159
Darren Tucker3e33cec2003-10-02 16:12:36 +1000160static int is_child = 0;
161
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000162/* Name and directory of socket for authentication agent forwarding. */
163static char *auth_sock_name = NULL;
164static char *auth_sock_dir = NULL;
165
166/* removes the agent forwarding socket */
167
168static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000169auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000170{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000171 if (auth_sock_name != NULL) {
172 temporarily_use_uid(pw);
173 unlink(auth_sock_name);
174 rmdir(auth_sock_dir);
175 auth_sock_name = NULL;
176 restore_uid();
177 }
178}
179
180static int
181auth_input_request_forwarding(struct passwd * pw)
182{
183 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000184 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000185 struct sockaddr_un sunaddr;
186
187 if (auth_sock_name != NULL) {
188 error("authentication forwarding requested twice.");
189 return 0;
190 }
191
192 /* Temporarily drop privileged uid for mkdir/bind. */
193 temporarily_use_uid(pw);
194
195 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000196 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000197
198 /* Create private directory for socket */
199 if (mkdtemp(auth_sock_dir) == NULL) {
200 packet_send_debug("Agent forwarding disabled: "
201 "mkdtemp() failed: %.100s", strerror(errno));
202 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000203 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000204 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000205 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000206 }
Damien Miller7207f642008-05-19 15:34:50 +1000207
208 xasprintf(&auth_sock_name, "%s/agent.%ld",
209 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000210
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000211 /* Create the socket. */
212 sock = socket(AF_UNIX, SOCK_STREAM, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000213 if (sock < 0) {
214 error("socket: %.100s", strerror(errno));
215 restore_uid();
216 goto authsock_err;
217 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000218
219 /* Bind it to the name. */
220 memset(&sunaddr, 0, sizeof(sunaddr));
221 sunaddr.sun_family = AF_UNIX;
222 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
223
Damien Miller7207f642008-05-19 15:34:50 +1000224 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
225 error("bind: %.100s", strerror(errno));
226 restore_uid();
227 goto authsock_err;
228 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000229
230 /* Restore the privileged uid. */
231 restore_uid();
232
233 /* Start listening on the socket. */
Damien Miller7207f642008-05-19 15:34:50 +1000234 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
235 error("listen: %.100s", strerror(errno));
236 goto authsock_err;
237 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000238
239 /* Allocate a channel for the authentication agent socket. */
240 nc = channel_new("auth socket",
241 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
242 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000243 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100244 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000245 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000246
247 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000248 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000249 if (auth_sock_dir != NULL) {
250 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000251 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000252 }
253 if (sock != -1)
254 close(sock);
255 auth_sock_name = NULL;
256 auth_sock_dir = NULL;
257 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000258}
259
Darren Tucker1921ed92004-02-10 13:23:28 +1100260static void
261display_loginmsg(void)
262{
Damien Miller46d38de2005-07-17 17:02:09 +1000263 if (buffer_len(&loginmsg) > 0) {
264 buffer_append(&loginmsg, "\0", 1);
265 printf("%s", (char *)buffer_ptr(&loginmsg));
266 buffer_clear(&loginmsg);
267 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100268}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000269
Ben Lindstromb31783d2001-03-22 02:02:12 +0000270void
271do_authenticated(Authctxt *authctxt)
272{
Damien Miller97f39ae2003-02-24 11:57:01 +1100273 setproctitle("%s", authctxt->pw->pw_name);
274
Ben Lindstromb31783d2001-03-22 02:02:12 +0000275 /* setup the channel layer */
Damien Milleraa5b3f82012-12-03 09:50:54 +1100276 if (no_port_forwarding_flag ||
277 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
278 channel_disable_adm_local_opens();
279 else
Ben Lindstromb31783d2001-03-22 02:02:12 +0000280 channel_permit_all_opens();
281
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100282 auth_debug_send();
283
Ben Lindstromb31783d2001-03-22 02:02:12 +0000284 if (compat20)
285 do_authenticated2(authctxt);
286 else
287 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000288
Darren Tucker3e33cec2003-10-02 16:12:36 +1000289 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000290}
291
Damien Millerb38eff82000-04-01 11:09:21 +1000292/*
Damien Millerb38eff82000-04-01 11:09:21 +1000293 * Prepares for an interactive session. This is called after the user has
294 * been successfully authenticated. During this message exchange, pseudo
295 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
296 * are requested, etc.
297 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000298static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000299do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000300{
301 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000302 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100303 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000304 int enable_compression_after_reply = 0;
305 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000306
307 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100308 if (s == NULL) {
309 error("no more sessions");
310 return;
311 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000312 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000313 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000314
Damien Millerb38eff82000-04-01 11:09:21 +1000315 /*
316 * We stay in this loop until the client requests to execute a shell
317 * or a command.
318 */
319 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000320 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000321
322 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100323 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000324
325 /* Process the packet. */
326 switch (type) {
327 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000328 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100329 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000330 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000331 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100332 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000333 break;
334 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000335 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000336 debug2("compression disabled");
337 break;
338 }
Damien Millerb38eff82000-04-01 11:09:21 +1000339 /* Enable compression after we have responded with SUCCESS. */
340 enable_compression_after_reply = 1;
341 success = 1;
342 break;
343
344 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000345 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000346 break;
347
348 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000349 s->auth_proto = packet_get_string(&proto_len);
350 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000351
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000352 screen_flag = packet_get_protocol_flags() &
353 SSH_PROTOFLAG_SCREEN_NUMBER;
354 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
355
356 if (packet_remaining() == 4) {
357 if (!screen_flag)
358 debug2("Buggy client: "
359 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000360 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000361 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000362 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000363 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100364 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000365 success = session_setup_x11fwd(s);
366 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000367 free(s->auth_proto);
368 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000369 s->auth_proto = NULL;
370 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000371 }
Damien Millerb38eff82000-04-01 11:09:21 +1000372 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000373
374 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
Damien Miller4f755cd2008-05-19 14:57:41 +1000375 if (!options.allow_agent_forwarding ||
376 no_agent_forwarding_flag || compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000377 debug("Authentication agent forwarding not permitted for this authentication.");
378 break;
379 }
380 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000381 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000382 break;
383
384 case SSH_CMSG_PORT_FORWARD_REQUEST:
385 if (no_port_forwarding_flag) {
386 debug("Port forwarding not permitted for this authentication.");
387 break;
388 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100389 if (!(options.allow_tcp_forwarding & FORWARD_REMOTE)) {
Damien Miller50a41ed2000-10-16 12:14:42 +1100390 debug("Port forwarding not permitted.");
391 break;
392 }
Damien Millerb38eff82000-04-01 11:09:21 +1000393 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000394 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
395 options.gateway_ports) < 0) {
396 debug("Port forwarding failed.");
397 break;
398 }
Damien Millerb38eff82000-04-01 11:09:21 +1000399 success = 1;
400 break;
401
402 case SSH_CMSG_MAX_PACKET_SIZE:
403 if (packet_set_maxsize(packet_get_int()) > 0)
404 success = 1;
405 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100406
Damien Millerb38eff82000-04-01 11:09:21 +1000407 case SSH_CMSG_EXEC_SHELL:
408 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000409 if (type == SSH_CMSG_EXEC_CMD) {
410 command = packet_get_string(&dlen);
411 debug("Exec command '%.500s'", command);
Damien Miller7207f642008-05-19 15:34:50 +1000412 if (do_exec(s, command) != 0)
413 packet_disconnect(
414 "command execution failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000415 free(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000416 } else {
Damien Miller7207f642008-05-19 15:34:50 +1000417 if (do_exec(s, NULL) != 0)
418 packet_disconnect(
419 "shell execution failed");
Damien Millerb38eff82000-04-01 11:09:21 +1000420 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100421 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000422 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000423 return;
424
425 default:
426 /*
427 * Any unknown messages in this phase are ignored,
428 * and a failure message is returned.
429 */
Damien Miller996acd22003-04-09 20:59:48 +1000430 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000431 }
432 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
433 packet_send();
434 packet_write_wait();
435
436 /* Enable compression now that we have replied if appropriate. */
437 if (enable_compression_after_reply) {
438 enable_compression_after_reply = 0;
439 packet_start_compression(compression_level);
440 }
441 }
442}
443
Darren Tucker293ee3c2014-01-19 15:28:01 +1100444#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000445/*
446 * This is called to fork and execute a command when we have no tty. This
447 * will call do_child from the child, and server_loop from the parent after
448 * setting up file descriptors and such.
449 */
Damien Miller7207f642008-05-19 15:34:50 +1000450int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000451do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000452{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000453 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000454
455#ifdef USE_PIPES
456 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000457
Damien Miller22a29882010-05-10 11:53:54 +1000458 if (s == NULL)
459 fatal("do_exec_no_pty: no session");
460
Damien Millerb38eff82000-04-01 11:09:21 +1000461 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000462 if (pipe(pin) < 0) {
463 error("%s: pipe in: %.100s", __func__, strerror(errno));
464 return -1;
465 }
466 if (pipe(pout) < 0) {
467 error("%s: pipe out: %.100s", __func__, strerror(errno));
468 close(pin[0]);
469 close(pin[1]);
470 return -1;
471 }
Damien Miller8853ca52010-06-26 10:00:14 +1000472 if (pipe(perr) < 0) {
473 error("%s: pipe err: %.100s", __func__,
474 strerror(errno));
475 close(pin[0]);
476 close(pin[1]);
477 close(pout[0]);
478 close(pout[1]);
479 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000480 }
481#else
Damien Millerb38eff82000-04-01 11:09:21 +1000482 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000483
Damien Miller22a29882010-05-10 11:53:54 +1000484 if (s == NULL)
485 fatal("do_exec_no_pty: no session");
486
Damien Millerb38eff82000-04-01 11:09:21 +1000487 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000488 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
489 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
490 return -1;
491 }
Damien Miller8853ca52010-06-26 10:00:14 +1000492 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
493 error("%s: socketpair #2: %.100s", __func__,
494 strerror(errno));
495 close(inout[0]);
496 close(inout[1]);
497 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000498 }
499#endif
500
Damien Millere247cc42000-05-07 12:03:14 +1000501 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000502
Damien Millerb38eff82000-04-01 11:09:21 +1000503 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000504 switch ((pid = fork())) {
505 case -1:
506 error("%s: fork: %.100s", __func__, strerror(errno));
507#ifdef USE_PIPES
508 close(pin[0]);
509 close(pin[1]);
510 close(pout[0]);
511 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000512 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000513 close(perr[1]);
514#else
515 close(inout[0]);
516 close(inout[1]);
517 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000518 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000519#endif
520 return -1;
521 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000522 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000523
Damien Millerb38eff82000-04-01 11:09:21 +1000524 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000525 log_init(__progname, options.log_level,
526 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000527
528 /*
529 * Create a new session and process group since the 4.4BSD
530 * setlogin() affects the entire process group.
531 */
532 if (setsid() < 0)
533 error("setsid failed: %.100s", strerror(errno));
534
535#ifdef USE_PIPES
536 /*
537 * Redirect stdin. We close the parent side of the socket
538 * pair, and make the child side the standard input.
539 */
540 close(pin[1]);
541 if (dup2(pin[0], 0) < 0)
542 perror("dup2 stdin");
543 close(pin[0]);
544
545 /* Redirect stdout. */
546 close(pout[0]);
547 if (dup2(pout[1], 1) < 0)
548 perror("dup2 stdout");
549 close(pout[1]);
550
551 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000552 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000553 if (dup2(perr[1], 2) < 0)
554 perror("dup2 stderr");
555 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000556#else
Damien Millerb38eff82000-04-01 11:09:21 +1000557 /*
558 * Redirect stdin, stdout, and stderr. Stdin and stdout will
559 * use the same socket, as some programs (particularly rdist)
560 * seem to depend on it.
561 */
562 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000563 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000564 if (dup2(inout[0], 0) < 0) /* stdin */
565 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000566 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000567 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000568 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000569 if (dup2(err[0], 2) < 0) /* stderr */
570 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000571 close(err[0]);
572#endif
573
Damien Millerb38eff82000-04-01 11:09:21 +1000574
Tim Rice81ed5182002-09-25 17:38:46 -0700575#ifdef _UNICOS
576 cray_init_job(s->pw); /* set up cray jid and tmpdir */
577#endif
578
Damien Millerb38eff82000-04-01 11:09:21 +1000579 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000580 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000581 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000582 default:
583 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000584 }
Damien Miller7207f642008-05-19 15:34:50 +1000585
Tim Rice81ed5182002-09-25 17:38:46 -0700586#ifdef _UNICOS
587 signal(WJSIGNAL, cray_job_termination_handler);
588#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100589#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100590 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100591#endif
Damien Miller7207f642008-05-19 15:34:50 +1000592
Damien Millerb38eff82000-04-01 11:09:21 +1000593 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000594 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100595 packet_set_interactive(s->display != NULL,
596 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000597
598 /*
599 * Clear loginmsg, since it's the child's responsibility to display
600 * it to the user, otherwise multiple sessions may accumulate
601 * multiple copies of the login messages.
602 */
603 buffer_clear(&loginmsg);
604
Damien Millerb38eff82000-04-01 11:09:21 +1000605#ifdef USE_PIPES
606 /* We are the parent. Close the child sides of the pipes. */
607 close(pin[0]);
608 close(pout[1]);
609 close(perr[1]);
610
Damien Millerefb4afe2000-04-12 18:45:05 +1000611 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000612 session_set_fds(s, pin[1], pout[0], perr[0],
613 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000614 } else {
615 /* Enter the interactive session. */
616 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000617 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000618 }
Damien Miller7207f642008-05-19 15:34:50 +1000619#else
Damien Millerb38eff82000-04-01 11:09:21 +1000620 /* We are the parent. Close the child sides of the socket pairs. */
621 close(inout[0]);
622 close(err[0]);
623
624 /*
625 * Enter the interactive session. Note: server_loop must be able to
626 * handle the case that fdin and fdout are the same.
627 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000628 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000629 session_set_fds(s, inout[1], inout[1], err[1],
630 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000631 } else {
632 server_loop(pid, inout[1], inout[1], err[1]);
633 /* server_loop has closed inout[1] and err[1]. */
634 }
Damien Miller7207f642008-05-19 15:34:50 +1000635#endif
636 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000637}
638
639/*
640 * This is called to fork and execute a command when we have a tty. This
641 * will call do_child from the child, and server_loop from the parent after
642 * setting up file descriptors, controlling tty, updating wtmp, utmp,
643 * lastlog, and other such operations.
644 */
Damien Miller7207f642008-05-19 15:34:50 +1000645int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000646do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000647{
Damien Millerb38eff82000-04-01 11:09:21 +1000648 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000649 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000650
651 if (s == NULL)
652 fatal("do_exec_pty: no session");
653 ptyfd = s->ptyfd;
654 ttyfd = s->ttyfd;
655
Damien Miller7207f642008-05-19 15:34:50 +1000656 /*
657 * Create another descriptor of the pty master side for use as the
658 * standard input. We could use the original descriptor, but this
659 * simplifies code in server_loop. The descriptor is bidirectional.
660 * Do this before forking (and cleanup in the child) so as to
661 * detect and gracefully fail out-of-fd conditions.
662 */
663 if ((fdout = dup(ptyfd)) < 0) {
664 error("%s: dup #1: %s", __func__, strerror(errno));
665 close(ttyfd);
666 close(ptyfd);
667 return -1;
668 }
669 /* we keep a reference to the pty master */
670 if ((ptymaster = dup(ptyfd)) < 0) {
671 error("%s: dup #2: %s", __func__, strerror(errno));
672 close(ttyfd);
673 close(ptyfd);
674 close(fdout);
675 return -1;
676 }
677
Damien Millerb38eff82000-04-01 11:09:21 +1000678 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000679 switch ((pid = fork())) {
680 case -1:
681 error("%s: fork: %.100s", __func__, strerror(errno));
682 close(fdout);
683 close(ptymaster);
684 close(ttyfd);
685 close(ptyfd);
686 return -1;
687 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000688 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000689
Damien Miller7207f642008-05-19 15:34:50 +1000690 close(fdout);
691 close(ptymaster);
692
Damien Miller942da032000-08-18 13:59:06 +1000693 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000694 log_init(__progname, options.log_level,
695 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000696 /* Close the master side of the pseudo tty. */
697 close(ptyfd);
698
699 /* Make the pseudo tty our controlling tty. */
700 pty_make_controlling_tty(&ttyfd, s->tty);
701
Damien Miller9c751422001-10-10 15:02:46 +1000702 /* Redirect stdin/stdout/stderr from the pseudo tty. */
703 if (dup2(ttyfd, 0) < 0)
704 error("dup2 stdin: %s", strerror(errno));
705 if (dup2(ttyfd, 1) < 0)
706 error("dup2 stdout: %s", strerror(errno));
707 if (dup2(ttyfd, 2) < 0)
708 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000709
710 /* Close the extra descriptor for the pseudo tty. */
711 close(ttyfd);
712
Damien Miller942da032000-08-18 13:59:06 +1000713 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000714#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700715 if (!(options.use_login && command == NULL)) {
716#ifdef _UNICOS
717 cray_init_job(s->pw); /* set up cray jid and tmpdir */
718#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100719 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700720 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000721# ifdef LOGIN_NEEDS_UTMPX
722 else
723 do_pre_login(s);
724# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000725#endif
Damien Miller7207f642008-05-19 15:34:50 +1000726 /*
727 * Do common processing for the child, such as execing
728 * the command.
729 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000730 do_child(s, command);
731 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000732 default:
733 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000734 }
Damien Miller7207f642008-05-19 15:34:50 +1000735
Tim Rice81ed5182002-09-25 17:38:46 -0700736#ifdef _UNICOS
737 signal(WJSIGNAL, cray_job_termination_handler);
738#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100739#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100740 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100741#endif
Damien Miller7207f642008-05-19 15:34:50 +1000742
Damien Millerb38eff82000-04-01 11:09:21 +1000743 s->pid = pid;
744
745 /* Parent. Close the slave side of the pseudo tty. */
746 close(ttyfd);
747
Damien Millerb38eff82000-04-01 11:09:21 +1000748 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000749 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100750 packet_set_interactive(1,
751 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Millerefb4afe2000-04-12 18:45:05 +1000752 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000753 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000754 } else {
755 server_loop(pid, ptyfd, fdout, -1);
756 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000757 }
Damien Miller7207f642008-05-19 15:34:50 +1000758 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000759}
760
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000761#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000762static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000763do_pre_login(Session *s)
764{
765 socklen_t fromlen;
766 struct sockaddr_storage from;
767 pid_t pid = getpid();
768
769 /*
770 * Get IP address of client. If the connection is not a socket, let
771 * the address be 0.0.0.0.
772 */
773 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000774 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000775 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000776 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100777 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000778 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000779 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000780 }
781 }
782
783 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000784 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000785 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000786}
787#endif
788
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000789/*
790 * This is called to fork and execute a command. If another command is
791 * to be forced, execute that instead.
792 */
Damien Miller7207f642008-05-19 15:34:50 +1000793int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000794do_exec(Session *s, const char *command)
795{
Damien Miller7207f642008-05-19 15:34:50 +1000796 int ret;
Damien Miller71df7522013-10-15 12:12:02 +1100797 const char *forced = NULL;
798 char session_type[1024], *tty = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000799
Damien Millere2754432006-07-24 14:06:47 +1000800 if (options.adm_forced_command) {
801 original_command = command;
802 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100803 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000804 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000805 original_command = command;
806 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100807 forced = "(key-option)";
808 }
809 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100810 if (IS_INTERNAL_SFTP(command)) {
811 s->is_subsystem = s->is_subsystem ?
812 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
813 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100814 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100815 snprintf(session_type, sizeof(session_type),
816 "forced-command %s '%.900s'", forced, command);
817 } else if (s->is_subsystem) {
818 snprintf(session_type, sizeof(session_type),
819 "subsystem '%.900s'", s->subsys);
820 } else if (command == NULL) {
821 snprintf(session_type, sizeof(session_type), "shell");
822 } else {
823 /* NB. we don't log unforced commands to preserve privacy */
824 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000825 }
826
Damien Miller71df7522013-10-15 12:12:02 +1100827 if (s->ttyfd != -1) {
828 tty = s->tty;
829 if (strncmp(tty, "/dev/", 5) == 0)
830 tty += 5;
831 }
832
833 verbose("Starting session: %s%s%s for %s from %.200s port %d",
834 session_type,
835 tty == NULL ? "" : " on ",
836 tty == NULL ? "" : tty,
837 s->pw->pw_name,
838 get_remote_ipaddr(),
839 get_remote_port());
840
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100841#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100842 if (command != NULL)
843 PRIVSEP(audit_run_command(command));
844 else if (s->ttyfd == -1) {
845 char *shell = s->pw->pw_shell;
846
847 if (shell[0] == '\0') /* empty shell means /bin/sh */
848 shell =_PATH_BSHELL;
849 PRIVSEP(audit_run_command(shell));
850 }
851#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000852 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000853 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000854 else
Damien Miller7207f642008-05-19 15:34:50 +1000855 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000856
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000857 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000858
Darren Tucker09991742004-07-17 17:05:14 +1000859 /*
860 * Clear loginmsg: it's the child's responsibility to display
861 * it to the user, otherwise multiple sessions may accumulate
862 * multiple copies of the login messages.
863 */
864 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000865
866 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000867}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000868
Damien Miller942da032000-08-18 13:59:06 +1000869/* administrative, login(1)-like work */
870void
Damien Miller69b69aa2000-10-28 14:19:58 +1100871do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000872{
Damien Miller942da032000-08-18 13:59:06 +1000873 socklen_t fromlen;
874 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000875 struct passwd * pw = s->pw;
876 pid_t pid = getpid();
877
878 /*
879 * Get IP address of client. If the connection is not a socket, let
880 * the address be 0.0.0.0.
881 */
882 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000883 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000884 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000885 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000886 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000887 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000888 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000889 }
890 }
891
892 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000893 if (!use_privsep)
894 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
895 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000896 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000897 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000898
Kevin Steves092f2ef2000-10-14 13:36:13 +0000899#ifdef USE_PAM
900 /*
901 * If password change is needed, do it now.
902 * This needs to occur before the ~/.hushlogin check.
903 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100904 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
905 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000906 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100907 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000908 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000909 }
910#endif
911
Damien Millercf205e82001-04-16 18:29:15 +1000912 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000913 return;
914
Darren Tucker1921ed92004-02-10 13:23:28 +1100915 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000916
Damien Millercf205e82001-04-16 18:29:15 +1000917 do_motd();
918}
919
920/*
921 * Display the message of the day.
922 */
923void
924do_motd(void)
925{
926 FILE *f;
927 char buf[256];
928
Damien Miller942da032000-08-18 13:59:06 +1000929 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000930#ifdef HAVE_LOGIN_CAP
931 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
932 "/etc/motd"), "r");
933#else
Damien Miller942da032000-08-18 13:59:06 +1000934 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000935#endif
Damien Miller942da032000-08-18 13:59:06 +1000936 if (f) {
937 while (fgets(buf, sizeof(buf), f))
938 fputs(buf, stdout);
939 fclose(f);
940 }
941 }
942}
943
Kevin Steves8f63caa2001-07-04 18:23:02 +0000944
945/*
946 * Check for quiet login, either .hushlogin or command given.
947 */
948int
949check_quietlogin(Session *s, const char *command)
950{
951 char buf[256];
952 struct passwd *pw = s->pw;
953 struct stat st;
954
955 /* Return 1 if .hushlogin exists or a command given. */
956 if (command != NULL)
957 return 1;
958 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
959#ifdef HAVE_LOGIN_CAP
960 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
961 return 1;
962#else
963 if (stat(buf, &st) >= 0)
964 return 1;
965#endif
966 return 0;
967}
968
Damien Millerb38eff82000-04-01 11:09:21 +1000969/*
970 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100971 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000972 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000973void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000974child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100975 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000976{
Damien Millerb38eff82000-04-01 11:09:21 +1000977 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000978 u_int envsize;
979 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000980
Damien Miller8569eba2014-03-04 09:35:17 +1100981 if (strchr(name, '=') != NULL) {
982 error("Invalid environment variable \"%.100s\"", name);
983 return;
984 }
985
Damien Millerb38eff82000-04-01 11:09:21 +1000986 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000987 * If we're passed an uninitialized list, allocate a single null
988 * entry before continuing.
989 */
990 if (*envp == NULL && *envsizep == 0) {
991 *envp = xmalloc(sizeof(char *));
992 *envp[0] = NULL;
993 *envsizep = 1;
994 }
995
996 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000997 * Find the slot where the value should be stored. If the variable
998 * already exists, we reuse the slot; otherwise we append a new slot
999 * at the end of the array, expanding if necessary.
1000 */
1001 env = *envp;
1002 namelen = strlen(name);
1003 for (i = 0; env[i]; i++)
1004 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
1005 break;
1006 if (env[i]) {
1007 /* Reuse the slot. */
Darren Tuckera627d422013-06-02 07:31:17 +10001008 free(env[i]);
Damien Millerb38eff82000-04-01 11:09:21 +10001009 } else {
1010 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +10001011 envsize = *envsizep;
1012 if (i >= envsize - 1) {
1013 if (envsize >= 1000)
1014 fatal("child_set_env: too many env vars");
1015 envsize += 50;
Damien Miller36812092006-03-26 14:22:47 +11001016 env = (*envp) = xrealloc(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +10001017 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +10001018 }
1019 /* Need to set the NULL pointer at end of array beyond the new slot. */
1020 env[i + 1] = NULL;
1021 }
1022
1023 /* Allocate space and format the variable in the appropriate slot. */
1024 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
1025 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
1026}
1027
1028/*
1029 * Reads environment variables from the given file and adds/overrides them
1030 * into the environment. If the file does not exist, this does nothing.
1031 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1032 * and assignments of the form name=value. No other forms are allowed.
1033 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001034static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001035read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001036 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +10001037{
1038 FILE *f;
1039 char buf[4096];
1040 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +10001041 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001042
1043 f = fopen(filename, "r");
1044 if (!f)
1045 return;
1046
1047 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +10001048 if (++lineno > 1000)
1049 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001050 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1051 ;
1052 if (!*cp || *cp == '#' || *cp == '\n')
1053 continue;
Damien Miller14b017d2007-09-17 16:09:15 +10001054
1055 cp[strcspn(cp, "\n")] = '\0';
1056
Damien Millerb38eff82000-04-01 11:09:21 +10001057 value = strchr(cp, '=');
1058 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +10001059 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1060 filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001061 continue;
1062 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001063 /*
1064 * Replace the equals sign by nul, and advance value to
1065 * the value string.
1066 */
Damien Millerb38eff82000-04-01 11:09:21 +10001067 *value = '\0';
1068 value++;
1069 child_set_env(env, envsize, cp, value);
1070 }
1071 fclose(f);
1072}
1073
Darren Tuckere1a790d2003-09-16 11:52:19 +10001074#ifdef HAVE_ETC_DEFAULT_LOGIN
1075/*
1076 * Return named variable from specified environment, or NULL if not present.
1077 */
1078static char *
1079child_get_env(char **env, const char *name)
1080{
1081 int i;
1082 size_t len;
1083
1084 len = strlen(name);
1085 for (i=0; env[i] != NULL; i++)
1086 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1087 return(env[i] + len + 1);
1088 return NULL;
1089}
1090
1091/*
1092 * Read /etc/default/login.
1093 * We pick up the PATH (or SUPATH for root) and UMASK.
1094 */
1095static void
1096read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1097{
1098 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001099 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +10001100 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +10001101
1102 /*
1103 * We don't want to copy the whole file to the child's environment,
1104 * so we use a temporary environment and copy the variables we're
1105 * interested in.
1106 */
1107 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1108
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001109 if (tmpenv == NULL)
1110 return;
1111
Darren Tuckere1a790d2003-09-16 11:52:19 +10001112 if (uid == 0)
1113 var = child_get_env(tmpenv, "SUPATH");
1114 else
1115 var = child_get_env(tmpenv, "PATH");
1116 if (var != NULL)
1117 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +11001118
Darren Tuckere1a790d2003-09-16 11:52:19 +10001119 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1120 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +10001121 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +11001122
Darren Tuckere1a790d2003-09-16 11:52:19 +10001123 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001124 free(tmpenv[i]);
1125 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +10001126}
1127#endif /* HAVE_ETC_DEFAULT_LOGIN */
1128
Damien Miller7dff8692005-05-26 11:34:51 +10001129void
1130copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +10001131{
Damien Millerbb9ffc12002-01-08 10:59:32 +11001132 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +10001133 int i;
1134
Damien Millerbb9ffc12002-01-08 10:59:32 +11001135 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001136 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001137
Damien Millerbb9ffc12002-01-08 10:59:32 +11001138 for(i = 0; source[i] != NULL; i++) {
1139 var_name = xstrdup(source[i]);
1140 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001141 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001142 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001143 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001144 *var_val++ = '\0';
1145
1146 debug3("Copy environment: %s=%s", var_name, var_val);
1147 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001148
Darren Tuckerf60845f2013-06-02 08:07:31 +10001149 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001150 }
1151}
Damien Millercb5e44a2000-09-29 12:12:36 +11001152
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001153static char **
1154do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001155{
Damien Millerb38eff82000-04-01 11:09:21 +10001156 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001157 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001158 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001159 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001160#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001161 char *path = NULL;
1162#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001163
Damien Millerb38eff82000-04-01 11:09:21 +10001164 /* Initialize the environment. */
1165 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001166 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001167 env[0] = NULL;
1168
Damien Millerbac2d8a2000-09-05 16:13:06 +11001169#ifdef HAVE_CYGWIN
1170 /*
1171 * The Windows environment contains some setting which are
1172 * important for a running system. They must not be dropped.
1173 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001174 {
1175 char **p;
1176
1177 p = fetch_windows_environment();
1178 copy_environment(p, &env, &envsize);
1179 free_windows_environment(p);
1180 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001181#endif
1182
Darren Tucker0efd1552003-08-26 11:49:55 +10001183#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001184 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001185 * the childs environment as they see fit
1186 */
1187 ssh_gssapi_do_child(&env, &envsize);
1188#endif
1189
Damien Millerb38eff82000-04-01 11:09:21 +10001190 if (!options.use_login) {
1191 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001192 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001193 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001194 s->env[i].val);
1195
Damien Millerb38eff82000-04-01 11:09:21 +10001196 child_set_env(&env, &envsize, "USER", pw->pw_name);
1197 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001198#ifdef _AIX
1199 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1200#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001201 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001202#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001203 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1204 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1205 else
1206 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001207#else /* HAVE_LOGIN_CAP */
1208# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001209 /*
1210 * There's no standard path on Windows. The path contains
1211 * important components pointing to the system directories,
1212 * needed for loading shared libraries. So the path better
1213 * remains intact here.
1214 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001215# ifdef HAVE_ETC_DEFAULT_LOGIN
1216 read_etc_default_login(&env, &envsize, pw->pw_uid);
1217 path = child_get_env(env, "PATH");
1218# endif /* HAVE_ETC_DEFAULT_LOGIN */
1219 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001220 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001221 s->pw->pw_uid == 0 ?
1222 SUPERUSER_PATH : _PATH_STDPATH);
1223 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001224# endif /* HAVE_CYGWIN */
1225#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001226
1227 snprintf(buf, sizeof buf, "%.200s/%.50s",
1228 _PATH_MAILDIR, pw->pw_name);
1229 child_set_env(&env, &envsize, "MAIL", buf);
1230
1231 /* Normal systems set SHELL by default. */
1232 child_set_env(&env, &envsize, "SHELL", shell);
1233 }
1234 if (getenv("TZ"))
1235 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1236
1237 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001238 if (!options.use_login) {
1239 while (custom_environment) {
1240 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001241 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001242
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001243 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001244 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001245 if (str[i] == '=') {
1246 str[i] = 0;
1247 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001248 }
1249 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +10001250 free(ce->s);
1251 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001252 }
Damien Millerb38eff82000-04-01 11:09:21 +10001253 }
1254
Damien Millerf37e2462002-09-19 11:47:55 +10001255 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001256 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001257 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001258 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1259
Damien Miller00111382003-03-10 11:21:17 +11001260 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001261 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001262 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
Darren Tuckera627d422013-06-02 07:31:17 +10001263 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001264 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1265
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001266 if (s->ttyfd != -1)
1267 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1268 if (s->term)
1269 child_set_env(&env, &envsize, "TERM", s->term);
1270 if (s->display)
1271 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001272 if (original_command)
1273 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1274 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001275
Tim Rice81ed5182002-09-25 17:38:46 -07001276#ifdef _UNICOS
1277 if (cray_tmpdir[0] != '\0')
1278 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1279#endif /* _UNICOS */
1280
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001281 /*
1282 * Since we clear KRB5CCNAME at startup, if it's set now then it
1283 * must have been set by a native authentication method (eg AIX or
1284 * SIA), so copy it to the child.
1285 */
1286 {
1287 char *cp;
1288
1289 if ((cp = getenv("KRB5CCNAME")) != NULL)
1290 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1291 }
1292
Damien Millerb38eff82000-04-01 11:09:21 +10001293#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001294 {
1295 char *cp;
1296
1297 if ((cp = getenv("AUTHSTATE")) != NULL)
1298 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001299 read_environment_file(&env, &envsize, "/etc/environment");
1300 }
Damien Millerb38eff82000-04-01 11:09:21 +10001301#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001302#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001303 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001304 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001305 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001306#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001307#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001308 /*
1309 * Pull in any environment variables that may have
1310 * been set by PAM.
1311 */
Damien Miller4e448a32003-05-14 15:11:48 +10001312 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001313 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001314
Damien Millerc756e9b2003-11-17 21:41:42 +11001315 p = fetch_pam_child_environment();
1316 copy_environment(p, &env, &envsize);
1317 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001318
Damien Millerc756e9b2003-11-17 21:41:42 +11001319 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001320 copy_environment(p, &env, &envsize);
1321 free_pam_environment(p);
1322 }
Damien Millerb38eff82000-04-01 11:09:21 +10001323#endif /* USE_PAM */
1324
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001325 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001326 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001327 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001328
1329 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001330 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001331 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001332 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001333 read_environment_file(&env, &envsize, buf);
1334 }
1335 if (debug_flag) {
1336 /* dump the environment */
1337 fprintf(stderr, "Environment:\n");
1338 for (i = 0; env[i]; i++)
1339 fprintf(stderr, " %.200s\n", env[i]);
1340 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001341 return env;
1342}
1343
1344/*
1345 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1346 * first in this order).
1347 */
1348static void
1349do_rc_files(Session *s, const char *shell)
1350{
1351 FILE *f = NULL;
1352 char cmd[1024];
1353 int do_xauth;
1354 struct stat st;
1355
1356 do_xauth =
1357 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1358
Damien Millera1b48cc2008-03-27 11:02:02 +11001359 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001360 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Millerff0dd882008-05-19 14:55:02 +10001361 !no_user_rc && stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001362 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1363 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1364 if (debug_flag)
1365 fprintf(stderr, "Running %s\n", cmd);
1366 f = popen(cmd, "w");
1367 if (f) {
1368 if (do_xauth)
1369 fprintf(f, "%s %s\n", s->auth_proto,
1370 s->auth_data);
1371 pclose(f);
1372 } else
1373 fprintf(stderr, "Could not run %s\n",
1374 _PATH_SSH_USER_RC);
1375 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1376 if (debug_flag)
1377 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1378 _PATH_SSH_SYSTEM_RC);
1379 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1380 if (f) {
1381 if (do_xauth)
1382 fprintf(f, "%s %s\n", s->auth_proto,
1383 s->auth_data);
1384 pclose(f);
1385 } else
1386 fprintf(stderr, "Could not run %s\n",
1387 _PATH_SSH_SYSTEM_RC);
1388 } else if (do_xauth && options.xauth_location != NULL) {
1389 /* Add authority data to .Xauthority if appropriate. */
1390 if (debug_flag) {
1391 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001392 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001393 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001394 fprintf(stderr,
1395 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001396 options.xauth_location, s->auth_display,
1397 s->auth_proto, s->auth_data);
1398 }
1399 snprintf(cmd, sizeof cmd, "%s -q -",
1400 options.xauth_location);
1401 f = popen(cmd, "w");
1402 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001403 fprintf(f, "remove %s\n",
1404 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001405 fprintf(f, "add %s %s %s\n",
1406 s->auth_display, s->auth_proto,
1407 s->auth_data);
1408 pclose(f);
1409 } else {
1410 fprintf(stderr, "Could not run %s\n",
1411 cmd);
1412 }
1413 }
1414}
1415
1416static void
1417do_nologin(struct passwd *pw)
1418{
1419 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001420 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1421 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001422
1423#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001424 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001425 return;
1426 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001427#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001428 if (pw->pw_uid == 0)
1429 return;
1430 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001431#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001432 if (stat(nl, &sb) == -1) {
1433 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001434 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001435 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001436 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001437
1438 /* /etc/nologin exists. Print its contents if we can and exit. */
1439 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1440 if ((f = fopen(nl, "r")) != NULL) {
1441 while (fgets(buf, sizeof(buf), f))
1442 fputs(buf, stderr);
1443 fclose(f);
1444 }
1445 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001446}
1447
Damien Millerd8cb1f12008-02-10 22:40:12 +11001448/*
1449 * Chroot into a directory after checking it for safety: all path components
1450 * must be root-owned directories with strict permissions.
1451 */
1452static void
1453safely_chroot(const char *path, uid_t uid)
1454{
1455 const char *cp;
1456 char component[MAXPATHLEN];
1457 struct stat st;
1458
1459 if (*path != '/')
1460 fatal("chroot path does not begin at root");
1461 if (strlen(path) >= sizeof(component))
1462 fatal("chroot path too long");
1463
1464 /*
1465 * Descend the path, checking that each component is a
1466 * root-owned directory with strict permissions.
1467 */
1468 for (cp = path; cp != NULL;) {
1469 if ((cp = strchr(cp, '/')) == NULL)
1470 strlcpy(component, path, sizeof(component));
1471 else {
1472 cp++;
1473 memcpy(component, path, cp - path);
1474 component[cp - path] = '\0';
1475 }
1476
1477 debug3("%s: checking '%s'", __func__, component);
1478
1479 if (stat(component, &st) != 0)
1480 fatal("%s: stat(\"%s\"): %s", __func__,
1481 component, strerror(errno));
1482 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1483 fatal("bad ownership or modes for chroot "
1484 "directory %s\"%s\"",
1485 cp == NULL ? "" : "component ", component);
1486 if (!S_ISDIR(st.st_mode))
1487 fatal("chroot path %s\"%s\" is not a directory",
1488 cp == NULL ? "" : "component ", component);
1489
1490 }
1491
1492 if (chdir(path) == -1)
1493 fatal("Unable to chdir to chroot path \"%s\": "
1494 "%s", path, strerror(errno));
1495 if (chroot(path) == -1)
1496 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1497 if (chdir("/") == -1)
1498 fatal("%s: chdir(/) after chroot: %s",
1499 __func__, strerror(errno));
1500 verbose("Changed root directory to \"%s\"", path);
1501}
1502
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001503/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001505do_setusercontext(struct passwd *pw)
1506{
Damien Miller54e37732008-02-10 22:48:55 +11001507 char *chroot_path, *tmp;
1508
Darren Tucker97528352010-11-05 12:03:05 +11001509 platform_setusercontext(pw);
1510
Darren Tuckerb12fe272010-11-05 14:47:01 +11001511 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001512#ifdef HAVE_LOGIN_CAP
1513 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001514 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001515 perror("unable to set user context");
1516 exit(1);
1517 }
1518#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001519 if (setlogin(pw->pw_name) < 0)
1520 error("setlogin failed: %s", strerror(errno));
1521 if (setgid(pw->pw_gid) < 0) {
1522 perror("setgid");
1523 exit(1);
1524 }
1525 /* Initialize the group list. */
1526 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1527 perror("initgroups");
1528 exit(1);
1529 }
1530 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001531#endif
1532
Darren Tucker920612e2010-11-05 12:36:15 +11001533 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001534
Damien Millerd8cb1f12008-02-10 22:40:12 +11001535 if (options.chroot_directory != NULL &&
1536 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001537 tmp = tilde_expand_filename(options.chroot_directory,
1538 pw->pw_uid);
1539 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1540 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001541 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001542 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001543 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001544 /* Make sure we don't attempt to chroot again */
1545 free(options.chroot_directory);
1546 options.chroot_directory = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001547 }
1548
1549#ifdef HAVE_LOGIN_CAP
1550 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1551 perror("unable to set user context (setuser)");
1552 exit(1);
1553 }
Damien Miller58528402013-03-15 11:22:37 +11001554 /*
1555 * FreeBSD's setusercontext() will not apply the user's
1556 * own umask setting unless running with the user's UID.
1557 */
1558 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001559#else
Tim Rice9464ba62014-01-20 17:59:28 -08001560# ifdef USE_LIBIAF
1561 if (set_id(pw->pw_name) != 0) {
Tim Rice617da332014-01-22 19:16:10 -08001562 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001563 }
1564# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001565 /* Permanently switch to the desired uid. */
1566 permanently_set_uid(pw);
1567#endif
Damien Millera56086b2013-04-23 15:24:18 +10001568 } else if (options.chroot_directory != NULL &&
1569 strcasecmp(options.chroot_directory, "none") != 0) {
1570 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001571 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001572
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001573 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1574 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1575}
1576
Ben Lindstrom08105192002-03-22 02:50:06 +00001577static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001578do_pwchange(Session *s)
1579{
Darren Tucker09991742004-07-17 17:05:14 +10001580 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001581 fprintf(stderr, "WARNING: Your password has expired.\n");
1582 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001583 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001584 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001585#ifdef WITH_SELINUX
1586 setexeccon(NULL);
1587#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001588#ifdef PASSWD_NEEDS_USERNAME
1589 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1590 (char *)NULL);
1591#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001592 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001593#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001594 perror("passwd");
1595 } else {
1596 fprintf(stderr,
1597 "Password change required but no TTY available.\n");
1598 }
1599 exit(1);
1600}
1601
1602static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603launch_login(struct passwd *pw, const char *hostname)
1604{
1605 /* Launch login(1). */
1606
Ben Lindstrom378a4172002-06-07 14:49:56 +00001607 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001608#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001609 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001610#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001611#ifdef LOGIN_NO_ENDOPT
1612 "-p", "-f", pw->pw_name, (char *)NULL);
1613#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001615#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001616
1617 /* Login couldn't be executed, die. */
1618
1619 perror("login");
1620 exit(1);
1621}
1622
Darren Tucker23bc8d02004-02-06 16:24:31 +11001623static void
1624child_close_fds(void)
1625{
Damien Miller85b45e02013-07-20 13:21:52 +10001626 extern AuthenticationConnection *auth_conn;
1627
1628 if (auth_conn) {
1629 ssh_close_authentication_connection(auth_conn);
1630 auth_conn = NULL;
1631 }
1632
Darren Tucker23bc8d02004-02-06 16:24:31 +11001633 if (packet_get_connection_in() == packet_get_connection_out())
1634 close(packet_get_connection_in());
1635 else {
1636 close(packet_get_connection_in());
1637 close(packet_get_connection_out());
1638 }
1639 /*
1640 * Close all descriptors related to channels. They will still remain
1641 * open in the parent.
1642 */
1643 /* XXX better use close-on-exec? -markus */
1644 channel_close_all();
1645
1646 /*
1647 * Close any extra file descriptors. Note that there may still be
1648 * descriptors left by system functions. They will be closed later.
1649 */
1650 endpwent();
1651
1652 /*
Damien Miller788f2122005-11-05 15:14:59 +11001653 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001654 * hanging around in clients. Note that we want to do this after
1655 * initgroups, because at least on Solaris 2.3 it leaves file
1656 * descriptors open.
1657 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001658 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001659}
1660
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001661/*
1662 * Performs common processing for the child, such as setting up the
1663 * environment, closing extra file descriptors, setting the user and group
1664 * ids, and executing the command or shell.
1665 */
Damien Millerdfc24252008-02-10 22:29:40 +11001666#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001667void
1668do_child(Session *s, const char *command)
1669{
1670 extern char **environ;
1671 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001672 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001673 const char *shell, *shell0, *hostname = NULL;
1674 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001675 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001676
1677 /* remove hostkey from the child's memory */
1678 destroy_sensitive_data();
1679
Darren Tucker23bc8d02004-02-06 16:24:31 +11001680 /* Force a password change */
1681 if (s->authctxt->force_pwchange) {
1682 do_setusercontext(pw);
1683 child_close_fds();
1684 do_pwchange(s);
1685 exit(1);
1686 }
1687
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001688 /* login(1) is only called if we execute the login shell */
1689 if (options.use_login && command != NULL)
1690 options.use_login = 0;
1691
Tim Rice81ed5182002-09-25 17:38:46 -07001692#ifdef _UNICOS
1693 cray_setup(pw->pw_uid, pw->pw_name, command);
1694#endif /* _UNICOS */
1695
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001696 /*
1697 * Login(1) does this as well, and it needs uid 0 for the "-h"
1698 * switch, so we let login(1) to this for us.
1699 */
1700 if (!options.use_login) {
1701#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001702 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001703 if (!check_quietlogin(s, command))
1704 do_motd();
1705#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001706 /* When PAM is enabled we rely on it to do the nologin check */
1707 if (!options.use_pam)
1708 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001709 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001710 /*
1711 * PAM session modules in do_setusercontext may have
1712 * generated messages, so if this in an interactive
1713 * login then display them too.
1714 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001715 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001716 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001717#endif /* HAVE_OSF_SIA */
1718 }
1719
Darren Tucker69687f42004-09-11 22:17:26 +10001720#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001721 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1722 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001723 display_loginmsg();
1724 exit(254);
1725 }
1726#endif
1727
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001728 /*
1729 * Get the shell from the password data. An empty shell field is
1730 * legal, and means /bin/sh.
1731 */
1732 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001733
1734 /*
1735 * Make sure $SHELL points to the shell from the password file,
1736 * even if shell is overridden from login.conf
1737 */
1738 env = do_setup_env(s, shell);
1739
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001740#ifdef HAVE_LOGIN_CAP
1741 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1742#endif
1743
Damien Millerad833b32000-08-23 10:46:23 +10001744 /* we have to stash the hostname before we close our socket. */
1745 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001746 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001747 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001748 /*
1749 * Close the connection descriptors; note that this is the child, and
1750 * the server will still have the socket open, and it is important
1751 * that we do not shutdown it. Note that the descriptors cannot be
1752 * closed before building the environment, as we call
1753 * get_remote_ipaddr there.
1754 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001755 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001756
Damien Millerb38eff82000-04-01 11:09:21 +10001757 /*
Damien Miller05eda432002-02-10 18:32:28 +11001758 * Must take new environment into use so that .ssh/rc,
1759 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001760 */
1761 environ = env;
1762
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001763#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001764 /*
1765 * At this point, we check to see if AFS is active and if we have
1766 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1767 * if we can (and need to) extend the ticket into an AFS token. If
1768 * we don't do this, we run into potential problems if the user's
1769 * home directory is in AFS and it's not world-readable.
1770 */
1771
1772 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001773 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001774 char cell[64];
1775
1776 debug("Getting AFS token");
1777
1778 k_setpag();
1779
1780 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1781 krb5_afslog(s->authctxt->krb5_ctx,
1782 s->authctxt->krb5_fwd_ccache, cell, NULL);
1783
1784 krb5_afslog_home(s->authctxt->krb5_ctx,
1785 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1786 }
1787#endif
1788
Damien Miller788f2122005-11-05 15:14:59 +11001789 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001790 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001791 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001792#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001793 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001794#endif
Damien Miller7aa46ec2010-06-26 09:37:57 +10001795 if (r || options.chroot_directory == NULL ||
1796 strcasecmp(options.chroot_directory, "none") == 0)
Damien Miller6051c942008-06-16 07:53:16 +10001797 fprintf(stderr, "Could not chdir to home "
1798 "directory %s: %s\n", pw->pw_dir,
1799 strerror(errno));
1800 if (r)
1801 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001802 }
1803
Damien Millera1939002008-03-15 17:27:58 +11001804 closefrom(STDERR_FILENO + 1);
1805
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001806 if (!options.use_login)
1807 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001808
1809 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001810 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001811
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001812 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1813 printf("This service allows sftp connections only.\n");
1814 fflush(NULL);
1815 exit(1);
1816 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001817 extern int optind, optreset;
1818 int i;
1819 char *p, *args;
1820
Darren Tuckerac46a912009-06-21 17:55:23 +10001821 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001822 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001823 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1824 if (i < ARGV_MAX - 1)
1825 argv[i++] = p;
1826 argv[i] = NULL;
1827 optind = optreset = 1;
1828 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001829#ifdef WITH_SELINUX
1830 ssh_selinux_change_context("sftpd_t");
1831#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001832 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001833 }
1834
Darren Tucker695ed392009-10-07 09:02:18 +11001835 fflush(NULL);
1836
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001837 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001838 launch_login(pw, hostname);
1839 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001840 }
1841
1842 /* Get the last component of the shell name. */
1843 if ((shell0 = strrchr(shell, '/')) != NULL)
1844 shell0++;
1845 else
1846 shell0 = shell;
1847
Damien Millerb38eff82000-04-01 11:09:21 +10001848 /*
1849 * If we have no command, execute the shell. In this case, the shell
1850 * name to be passed in argv[0] is preceded by '-' to indicate that
1851 * this is a login shell.
1852 */
1853 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001854 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001855
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001856 /* Start the shell. Set initial character to '-'. */
1857 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001858
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001859 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1860 >= sizeof(argv0) - 1) {
1861 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001862 perror(shell);
1863 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001864 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001865
1866 /* Execute the shell. */
1867 argv[0] = argv0;
1868 argv[1] = NULL;
1869 execve(shell, argv, env);
1870
1871 /* Executing the shell failed. */
1872 perror(shell);
1873 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001874 }
1875 /*
1876 * Execute the command using the user's shell. This uses the -c
1877 * option to execute the command.
1878 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001879 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001880 argv[1] = "-c";
1881 argv[2] = (char *) command;
1882 argv[3] = NULL;
1883 execve(shell, argv, env);
1884 perror(shell);
1885 exit(1);
1886}
1887
Damien Miller7207f642008-05-19 15:34:50 +10001888void
1889session_unused(int id)
1890{
1891 debug3("%s: session id %d unused", __func__, id);
1892 if (id >= options.max_sessions ||
1893 id >= sessions_nalloc) {
1894 fatal("%s: insane session id %d (max %d nalloc %d)",
1895 __func__, id, options.max_sessions, sessions_nalloc);
1896 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001897 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001898 sessions[id].self = id;
1899 sessions[id].used = 0;
1900 sessions[id].chanid = -1;
1901 sessions[id].ptyfd = -1;
1902 sessions[id].ttyfd = -1;
1903 sessions[id].ptymaster = -1;
1904 sessions[id].x11_chanids = NULL;
1905 sessions[id].next_unused = sessions_first_unused;
1906 sessions_first_unused = id;
1907}
1908
Damien Millerb38eff82000-04-01 11:09:21 +10001909Session *
1910session_new(void)
1911{
Damien Miller7207f642008-05-19 15:34:50 +10001912 Session *s, *tmp;
1913
1914 if (sessions_first_unused == -1) {
1915 if (sessions_nalloc >= options.max_sessions)
1916 return NULL;
1917 debug2("%s: allocate (allocated %d max %d)",
1918 __func__, sessions_nalloc, options.max_sessions);
1919 tmp = xrealloc(sessions, sessions_nalloc + 1,
1920 sizeof(*sessions));
1921 if (tmp == NULL) {
1922 error("%s: cannot allocate %d sessions",
1923 __func__, sessions_nalloc + 1);
1924 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001925 }
Damien Miller7207f642008-05-19 15:34:50 +10001926 sessions = tmp;
1927 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001928 }
Damien Miller7207f642008-05-19 15:34:50 +10001929
1930 if (sessions_first_unused >= sessions_nalloc ||
1931 sessions_first_unused < 0) {
1932 fatal("%s: insane first_unused %d max %d nalloc %d",
1933 __func__, sessions_first_unused, options.max_sessions,
1934 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001935 }
Damien Miller7207f642008-05-19 15:34:50 +10001936
1937 s = &sessions[sessions_first_unused];
1938 if (s->used) {
1939 fatal("%s: session %d already used",
1940 __func__, sessions_first_unused);
1941 }
1942 sessions_first_unused = s->next_unused;
1943 s->used = 1;
1944 s->next_unused = -1;
1945 debug("session_new: session %d", s->self);
1946
1947 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001948}
1949
Ben Lindstrombba81212001-06-25 05:01:22 +00001950static void
Damien Millerb38eff82000-04-01 11:09:21 +10001951session_dump(void)
1952{
1953 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001954 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001955 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001956
1957 debug("dump: used %d next_unused %d session %d %p "
1958 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001959 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001960 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001961 s->self,
1962 s,
1963 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001964 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001965 }
1966}
1967
Damien Millerefb4afe2000-04-12 18:45:05 +10001968int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001969session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001970{
1971 Session *s = session_new();
1972 debug("session_open: channel %d", chanid);
1973 if (s == NULL) {
1974 error("no more sessions");
1975 return 0;
1976 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001977 s->authctxt = authctxt;
1978 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001979 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001980 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001981 debug("session_open: session %d: link with channel %d", s->self, chanid);
1982 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001983 return 1;
1984}
1985
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001986Session *
1987session_by_tty(char *tty)
1988{
1989 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001990 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001991 Session *s = &sessions[i];
1992 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1993 debug("session_by_tty: session %d tty %s", i, tty);
1994 return s;
1995 }
1996 }
1997 debug("session_by_tty: unknown tty %.100s", tty);
1998 session_dump();
1999 return NULL;
2000}
2001
Ben Lindstrombba81212001-06-25 05:01:22 +00002002static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002003session_by_channel(int id)
2004{
2005 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002006 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002007 Session *s = &sessions[i];
2008 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10002009 debug("session_by_channel: session %d channel %d",
2010 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002011 return s;
2012 }
2013 }
2014 debug("session_by_channel: unknown channel %d", id);
2015 session_dump();
2016 return NULL;
2017}
2018
Ben Lindstrombba81212001-06-25 05:01:22 +00002019static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10002020session_by_x11_channel(int id)
2021{
2022 int i, j;
2023
Damien Miller7207f642008-05-19 15:34:50 +10002024 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002025 Session *s = &sessions[i];
2026
2027 if (s->x11_chanids == NULL || !s->used)
2028 continue;
2029 for (j = 0; s->x11_chanids[j] != -1; j++) {
2030 if (s->x11_chanids[j] == id) {
2031 debug("session_by_x11_channel: session %d "
2032 "channel %d", s->self, id);
2033 return s;
2034 }
2035 }
2036 }
2037 debug("session_by_x11_channel: unknown channel %d", id);
2038 session_dump();
2039 return NULL;
2040}
2041
2042static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10002043session_by_pid(pid_t pid)
2044{
2045 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00002046 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10002047 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10002048 Session *s = &sessions[i];
2049 if (s->used && s->pid == pid)
2050 return s;
2051 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002052 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002053 session_dump();
2054 return NULL;
2055}
2056
Ben Lindstrombba81212001-06-25 05:01:22 +00002057static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002058session_window_change_req(Session *s)
2059{
2060 s->col = packet_get_int();
2061 s->row = packet_get_int();
2062 s->xpixel = packet_get_int();
2063 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002064 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10002065 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2066 return 1;
2067}
2068
Ben Lindstrombba81212001-06-25 05:01:22 +00002069static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002070session_pty_req(Session *s)
2071{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002072 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00002073 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002074
Damien Miller5ff30c62013-10-30 22:21:50 +11002075 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00002076 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10002077 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002078 }
2079 if (s->ttyfd != -1) {
2080 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10002081 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002082 }
2083
Damien Millerefb4afe2000-04-12 18:45:05 +10002084 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002085
2086 if (compat20) {
2087 s->col = packet_get_int();
2088 s->row = packet_get_int();
2089 } else {
2090 s->row = packet_get_int();
2091 s->col = packet_get_int();
2092 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002093 s->xpixel = packet_get_int();
2094 s->ypixel = packet_get_int();
2095
2096 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002097 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002098 s->term = NULL;
2099 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00002100
Damien Millerefb4afe2000-04-12 18:45:05 +10002101 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00002102 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10002103 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2104 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10002105 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002106 s->term = NULL;
2107 s->ptyfd = -1;
2108 s->ttyfd = -1;
2109 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10002110 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002111 }
2112 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002113
2114 /* for SSH1 the tty modes length is not given */
2115 if (!compat20)
2116 n_bytes = packet_remaining();
2117 tty_parse_modes(s->ttyfd, &n_bytes);
2118
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002119 if (!use_privsep)
2120 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002121
2122 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10002123 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2124
Damien Miller48b03fc2002-01-22 23:11:40 +11002125 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10002126 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002127 return 1;
2128}
2129
Ben Lindstrombba81212001-06-25 05:01:22 +00002130static int
Damien Millerbd483e72000-04-30 10:00:53 +10002131session_subsystem_req(Session *s)
2132{
Damien Millerae452462001-10-10 15:08:06 +10002133 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002134 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10002135 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11002136 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10002137 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10002138
Damien Miller71df7522013-10-15 12:12:02 +11002139 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002140 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11002141 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10002142 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10002143
Damien Millerf6d9e222000-06-18 14:50:44 +10002144 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11002145 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10002146 prog = options.subsystem_command[i];
2147 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002148 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11002149 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002150 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11002151 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002152 if (stat(prog, &st) < 0)
2153 debug("subsystem: cannot stat %s: %s",
2154 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11002155 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002156 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10002157 }
Damien Miller7207f642008-05-19 15:34:50 +10002158 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11002159 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10002160 }
2161 }
2162
2163 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11002164 logit("subsystem request for %.100s by user %s failed, "
2165 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10002166
Damien Millerbd483e72000-04-30 10:00:53 +10002167 return success;
2168}
2169
Ben Lindstrombba81212001-06-25 05:01:22 +00002170static int
Damien Millerbd483e72000-04-30 10:00:53 +10002171session_x11_req(Session *s)
2172{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002173 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002174
Damien Miller2b9b0452005-07-17 17:19:24 +10002175 if (s->auth_proto != NULL || s->auth_data != NULL) {
2176 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002177 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002178 return 0;
2179 }
Damien Millerbd483e72000-04-30 10:00:53 +10002180 s->single_connection = packet_get_char();
2181 s->auth_proto = packet_get_string(NULL);
2182 s->auth_data = packet_get_string(NULL);
2183 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002184 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002185
Ben Lindstrom768176b2001-06-09 01:29:12 +00002186 success = session_setup_x11fwd(s);
2187 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002188 free(s->auth_proto);
2189 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002190 s->auth_proto = NULL;
2191 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002192 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002193 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002194}
2195
Ben Lindstrombba81212001-06-25 05:01:22 +00002196static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002197session_shell_req(Session *s)
2198{
Damien Miller48b03fc2002-01-22 23:11:40 +11002199 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002200 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002201}
2202
Ben Lindstrombba81212001-06-25 05:01:22 +00002203static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002204session_exec_req(Session *s)
2205{
Damien Miller7207f642008-05-19 15:34:50 +10002206 u_int len, success;
2207
Damien Millerf6d9e222000-06-18 14:50:44 +10002208 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002209 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002210 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002211 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002212 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002213}
2214
Ben Lindstrombba81212001-06-25 05:01:22 +00002215static int
Damien Miller54c45982003-05-15 10:20:13 +10002216session_break_req(Session *s)
2217{
Damien Miller54c45982003-05-15 10:20:13 +10002218
Darren Tucker1f8311c2004-05-13 16:39:33 +10002219 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002220 packet_check_eom();
2221
Darren Tucker9c5d5532011-11-04 10:55:24 +11002222 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002223 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002224 return 1;
2225}
2226
2227static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002228session_env_req(Session *s)
2229{
2230 char *name, *val;
2231 u_int name_len, val_len, i;
2232
Damien Miller8569eba2014-03-04 09:35:17 +11002233 name = packet_get_cstring(&name_len);
2234 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002235 packet_check_eom();
2236
2237 /* Don't set too many environment variables */
2238 if (s->num_env > 128) {
2239 debug2("Ignoring env request %s: too many env vars", name);
2240 goto fail;
2241 }
2242
2243 for (i = 0; i < options.num_accept_env; i++) {
2244 if (match_pattern(name, options.accept_env[i])) {
2245 debug2("Setting env %d: %s=%s", s->num_env, name, val);
Damien Miller36812092006-03-26 14:22:47 +11002246 s->env = xrealloc(s->env, s->num_env + 1,
2247 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002248 s->env[s->num_env].name = name;
2249 s->env[s->num_env].val = val;
2250 s->num_env++;
2251 return (1);
2252 }
2253 }
2254 debug2("Ignoring env request %s: disallowed name", name);
2255
2256 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002257 free(name);
2258 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002259 return (0);
2260}
2261
2262static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002263session_auth_agent_req(Session *s)
2264{
2265 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002266 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002267 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002268 debug("session_auth_agent_req: no_agent_forwarding_flag");
2269 return 0;
2270 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002271 if (called) {
2272 return 0;
2273 } else {
2274 called = 1;
2275 return auth_input_request_forwarding(s->pw);
2276 }
2277}
2278
Damien Millerc7ef63d2002-02-05 12:21:42 +11002279int
2280session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002281{
Damien Millerefb4afe2000-04-12 18:45:05 +10002282 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002283 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002284
Damien Millerc7ef63d2002-02-05 12:21:42 +11002285 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002286 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002287 c->self, rtype);
2288 return 0;
2289 }
2290 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002291
2292 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002293 * a session is in LARVAL state until a shell, a command
2294 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002295 */
2296 if (c->type == SSH_CHANNEL_LARVAL) {
2297 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002298 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002299 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002300 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002301 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002302 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002303 } else if (strcmp(rtype, "x11-req") == 0) {
2304 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002305 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2306 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002307 } else if (strcmp(rtype, "subsystem") == 0) {
2308 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002309 } else if (strcmp(rtype, "env") == 0) {
2310 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002311 }
2312 }
2313 if (strcmp(rtype, "window-change") == 0) {
2314 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002315 } else if (strcmp(rtype, "break") == 0) {
2316 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002317 }
Damien Millera6b1d162004-06-30 22:41:07 +10002318
Damien Millerc7ef63d2002-02-05 12:21:42 +11002319 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002320}
2321
2322void
Damien Miller8853ca52010-06-26 10:00:14 +10002323session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2324 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002325{
2326 if (!compat20)
2327 fatal("session_set_fds: called for proto != 2.0");
2328 /*
2329 * now that have a child and a pipe to the child,
2330 * we can activate our channel and register the fd's
2331 */
2332 if (s->chanid == -1)
2333 fatal("no channel for session %d", s->self);
2334 channel_set_fds(s->chanid,
2335 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002336 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002337 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002338}
2339
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002340/*
2341 * Function to perform pty cleanup. Also called if we get aborted abnormally
2342 * (e.g., due to a dropped connection).
2343 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002344void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002345session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002346{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002347 if (s == NULL) {
2348 error("session_pty_cleanup: no session");
2349 return;
2350 }
2351 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002352 return;
2353
Kevin Steves43cdef32001-02-11 14:12:08 +00002354 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002355
Damien Millerb38eff82000-04-01 11:09:21 +10002356 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002357 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002358 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002359
2360 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002361 if (getuid() == 0)
2362 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002363
2364 /*
2365 * Close the server side of the socket pairs. We must do this after
2366 * the pty cleanup, so that another process doesn't get this pty
2367 * while we're still cleaning up.
2368 */
Damien Miller7207f642008-05-19 15:34:50 +10002369 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2370 error("close(s->ptymaster/%d): %s",
2371 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002372
2373 /* unlink pty from session */
2374 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002375}
Damien Millerefb4afe2000-04-12 18:45:05 +10002376
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002377void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002378session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002379{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002380 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002381}
2382
Damien Miller5a80bba2002-09-04 16:39:02 +10002383static char *
2384sig2name(int sig)
2385{
2386#define SSH_SIG(x) if (sig == SIG ## x) return #x
2387 SSH_SIG(ABRT);
2388 SSH_SIG(ALRM);
2389 SSH_SIG(FPE);
2390 SSH_SIG(HUP);
2391 SSH_SIG(ILL);
2392 SSH_SIG(INT);
2393 SSH_SIG(KILL);
2394 SSH_SIG(PIPE);
2395 SSH_SIG(QUIT);
2396 SSH_SIG(SEGV);
2397 SSH_SIG(TERM);
2398 SSH_SIG(USR1);
2399 SSH_SIG(USR2);
2400#undef SSH_SIG
2401 return "SIG@openssh.com";
2402}
2403
Ben Lindstrombba81212001-06-25 05:01:22 +00002404static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002405session_close_x11(int id)
2406{
2407 Channel *c;
2408
Damien Millerd47c62a2005-12-13 19:33:57 +11002409 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002410 debug("session_close_x11: x11 channel %d missing", id);
2411 } else {
2412 /* Detach X11 listener */
2413 debug("session_close_x11: detach x11 channel %d", id);
2414 channel_cancel_cleanup(id);
2415 if (c->ostate != CHAN_OUTPUT_CLOSED)
2416 chan_mark_dead(c);
2417 }
2418}
2419
2420static void
2421session_close_single_x11(int id, void *arg)
2422{
2423 Session *s;
2424 u_int i;
2425
2426 debug3("session_close_single_x11: channel %d", id);
2427 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002428 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002429 fatal("session_close_single_x11: no x11 channel %d", id);
2430 for (i = 0; s->x11_chanids[i] != -1; i++) {
2431 debug("session_close_single_x11: session %d: "
2432 "closing channel %d", s->self, s->x11_chanids[i]);
2433 /*
2434 * The channel "id" is already closing, but make sure we
2435 * close all of its siblings.
2436 */
2437 if (s->x11_chanids[i] != id)
2438 session_close_x11(s->x11_chanids[i]);
2439 }
Darren Tuckera627d422013-06-02 07:31:17 +10002440 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002441 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002442 free(s->display);
2443 s->display = NULL;
2444 free(s->auth_proto);
2445 s->auth_proto = NULL;
2446 free(s->auth_data);
2447 s->auth_data = NULL;
2448 free(s->auth_display);
2449 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002450}
2451
2452static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002453session_exit_message(Session *s, int status)
2454{
2455 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002456
2457 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002458 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002459 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002460 debug("session_exit_message: session %d channel %d pid %ld",
2461 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002462
2463 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002464 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002465 packet_put_int(WEXITSTATUS(status));
2466 packet_send();
2467 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002468 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002469 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002470#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002471 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002472#else /* WCOREDUMP */
2473 packet_put_char(0);
2474#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002475 packet_put_cstring("");
2476 packet_put_cstring("");
2477 packet_send();
2478 } else {
2479 /* Some weird exit cause. Just exit. */
2480 packet_disconnect("wait returned status %04x.", status);
2481 }
2482
2483 /* disconnect channel */
2484 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002485
2486 /*
2487 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002488 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002489 * by session_close_by_channel when the childs close their fds.
2490 */
2491 channel_register_cleanup(c->self, session_close_by_channel, 1);
2492
Damien Miller166fca82000-04-20 07:42:21 +10002493 /*
2494 * emulate a write failure with 'chan_write_failed', nobody will be
2495 * interested in data we write.
2496 * Note that we must not call 'chan_read_failed', since there could
2497 * be some more data waiting in the pipe.
2498 */
Damien Millerbd483e72000-04-30 10:00:53 +10002499 if (c->ostate != CHAN_OUTPUT_CLOSED)
2500 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002501}
2502
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002503void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002504session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002505{
Damien Millereccb9de2005-06-17 12:59:34 +10002506 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002507
Ben Lindstromce0f6342002-06-11 16:42:49 +00002508 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002509 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002510 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002511 free(s->term);
2512 free(s->display);
2513 free(s->x11_chanids);
2514 free(s->auth_display);
2515 free(s->auth_data);
2516 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002517 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002518 if (s->env != NULL) {
2519 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002520 free(s->env[i].name);
2521 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002522 }
Darren Tuckera627d422013-06-02 07:31:17 +10002523 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002524 }
Damien Millere247cc42000-05-07 12:03:14 +10002525 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002526 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002527}
2528
2529void
2530session_close_by_pid(pid_t pid, int status)
2531{
2532 Session *s = session_by_pid(pid);
2533 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002534 debug("session_close_by_pid: no session for pid %ld",
2535 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002536 return;
2537 }
2538 if (s->chanid != -1)
2539 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002540 if (s->ttyfd != -1)
2541 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002542 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002543}
2544
2545/*
2546 * this is called when a channel dies before
2547 * the session 'child' itself dies
2548 */
2549void
2550session_close_by_channel(int id, void *arg)
2551{
2552 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002553 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002554
Damien Millerefb4afe2000-04-12 18:45:05 +10002555 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002556 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002557 return;
2558 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002559 debug("session_close_by_channel: channel %d child %ld",
2560 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002561 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002562 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002563 /*
2564 * delay detach of session, but release pty, since
2565 * the fd's to the child are already closed
2566 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002567 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002568 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002569 return;
2570 }
2571 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002572 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002573
2574 /* Close any X11 listeners associated with this session */
2575 if (s->x11_chanids != NULL) {
2576 for (i = 0; s->x11_chanids[i] != -1; i++) {
2577 session_close_x11(s->x11_chanids[i]);
2578 s->x11_chanids[i] = -1;
2579 }
2580 }
2581
Damien Millerefb4afe2000-04-12 18:45:05 +10002582 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002583 session_close(s);
2584}
2585
2586void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002587session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002588{
2589 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002590 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002591 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002592 if (s->used) {
2593 if (closefunc != NULL)
2594 closefunc(s);
2595 else
2596 session_close(s);
2597 }
Damien Miller52b77be2001-10-10 15:14:37 +10002598 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002599}
2600
Ben Lindstrombba81212001-06-25 05:01:22 +00002601static char *
Damien Millere247cc42000-05-07 12:03:14 +10002602session_tty_list(void)
2603{
2604 static char buf[1024];
2605 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002606 char *cp;
2607
Damien Millere247cc42000-05-07 12:03:14 +10002608 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002609 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002610 Session *s = &sessions[i];
2611 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002612
Damien Millera8ed44b2003-01-10 09:53:12 +11002613 if (strncmp(s->tty, "/dev/", 5) != 0) {
2614 cp = strrchr(s->tty, '/');
2615 cp = (cp == NULL) ? s->tty : cp + 1;
2616 } else
2617 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002618
Damien Millere247cc42000-05-07 12:03:14 +10002619 if (buf[0] != '\0')
2620 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002621 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002622 }
2623 }
2624 if (buf[0] == '\0')
2625 strlcpy(buf, "notty", sizeof buf);
2626 return buf;
2627}
2628
2629void
2630session_proctitle(Session *s)
2631{
2632 if (s->pw == NULL)
2633 error("no user for session %d", s->self);
2634 else
2635 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2636}
2637
Ben Lindstrom768176b2001-06-09 01:29:12 +00002638int
2639session_setup_x11fwd(Session *s)
2640{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002641 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002642 char display[512], auth_display[512];
2643 char hostname[MAXHOSTNAMELEN];
Damien Miller2b9b0452005-07-17 17:19:24 +10002644 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002645
2646 if (no_x11_forwarding_flag) {
2647 packet_send_debug("X11 forwarding disabled in user configuration file.");
2648 return 0;
2649 }
2650 if (!options.x11_forwarding) {
2651 debug("X11 forwarding disabled in server configuration file.");
2652 return 0;
2653 }
2654 if (!options.xauth_location ||
2655 (stat(options.xauth_location, &st) == -1)) {
2656 packet_send_debug("No xauth program; cannot forward with spoofing.");
2657 return 0;
2658 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002659 if (options.use_login) {
2660 packet_send_debug("X11 forwarding disabled; "
2661 "not compatible with UseLogin=yes.");
2662 return 0;
2663 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002664 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002665 debug("X11 display already set.");
2666 return 0;
2667 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002668 if (x11_create_display_inet(options.x11_display_offset,
2669 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002670 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002671 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002672 return 0;
2673 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002674 for (i = 0; s->x11_chanids[i] != -1; i++) {
2675 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002676 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002677 }
Kevin Steves366298c2001-12-19 17:58:01 +00002678
2679 /* Set up a suitable value for the DISPLAY variable. */
2680 if (gethostname(hostname, sizeof(hostname)) < 0)
2681 fatal("gethostname: %.100s", strerror(errno));
2682 /*
2683 * auth_display must be used as the displayname when the
2684 * authorization entry is added with xauth(1). This will be
2685 * different than the DISPLAY string for localhost displays.
2686 */
Damien Miller95c249f2002-02-05 12:11:34 +11002687 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002688 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002689 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002690 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002691 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002692 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002693 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002694 } else {
2695#ifdef IPADDR_IN_DISPLAY
2696 struct hostent *he;
2697 struct in_addr my_addr;
2698
2699 he = gethostbyname(hostname);
2700 if (he == NULL) {
2701 error("Can't get IP address for X11 DISPLAY.");
2702 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2703 return 0;
2704 }
2705 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002706 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002707 s->display_number, s->screen);
2708#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002709 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002710 s->display_number, s->screen);
2711#endif
2712 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002713 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002714 }
2715
Ben Lindstrom768176b2001-06-09 01:29:12 +00002716 return 1;
2717}
2718
Ben Lindstrombba81212001-06-25 05:01:22 +00002719static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002720do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002721{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002722 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002723}
2724
2725void
2726do_cleanup(Authctxt *authctxt)
2727{
2728 static int called = 0;
2729
2730 debug("do_cleanup");
2731
2732 /* no cleanup if we're in the child for login shell */
2733 if (is_child)
2734 return;
2735
2736 /* avoid double cleanup */
2737 if (called)
2738 return;
2739 called = 1;
2740
Darren Tucker9142e1c2007-08-16 23:28:04 +10002741 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002742 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002743
2744#ifdef USE_PAM
2745 if (options.use_pam) {
2746 sshpam_cleanup();
2747 sshpam_thread_cleanup();
2748 }
2749#endif
2750
2751 if (!authctxt->authenticated)
2752 return;
2753
Darren Tucker3e33cec2003-10-02 16:12:36 +10002754#ifdef KRB5
2755 if (options.kerberos_ticket_cleanup &&
2756 authctxt->krb5_ctx)
2757 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002758#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002759
2760#ifdef GSSAPI
2761 if (compat20 && options.gss_cleanup_creds)
2762 ssh_gssapi_cleanup_creds();
2763#endif
2764
2765 /* remove agent socket */
2766 auth_sock_cleanup_proc(authctxt->pw);
2767
2768 /*
2769 * Cleanup ptys/utmp only if privsep is disabled,
2770 * or if running in monitor.
2771 */
2772 if (!use_privsep || mm_is_monitor())
2773 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002774}