blob: 4ef48ecd6c85f929f456245852e9afed1efdf32c [file] [log] [blame]
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001/* $OpenBSD: session.c,v 1.288 2017/05/31 09:15:42 deraadt Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +000049#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100051#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100052#include <grp.h>
Damien Millere5c0d522014-07-03 21:24:19 +100053#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110055#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100057#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110058#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100059#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100060#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100061#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100062#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100063#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000064#include <limits.h>
Damien Millerb38eff82000-04-01 11:09:21 +100065
Damien Millerb84886b2008-05-19 15:05:07 +100066#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100068#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000070#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "packet.h"
72#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100073#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100074#include "uidswap.h"
75#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000076#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100077#include "key.h"
78#include "cipher.h"
79#ifdef GSSAPI
80#include "ssh-gss.h"
81#endif
82#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100083#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100084#include "auth-options.h"
Damien Miller85b45e02013-07-20 13:21:52 +100085#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "pathnames.h"
87#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100088#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000090#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000091#include "serverloop.h"
92#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000093#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100094#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110096#include "sftp.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100097
Darren Tucker3c78c5e2004-01-23 22:03:10 +110098#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110099#include <kafs.h>
100#endif
101
Damien Miller14684a12011-05-20 11:23:07 +1000102#ifdef WITH_SELINUX
103#include <selinux/selinux.h>
104#endif
105
Damien Millerad793d52008-11-03 19:17:57 +1100106#define IS_INTERNAL_SFTP(c) \
107 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
108 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
109 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
110 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
111
Damien Millerb38eff82000-04-01 11:09:21 +1000112/* func */
113
114Session *session_new(void);
Damien Miller8853ca52010-06-26 10:00:14 +1000115void session_set_fds(Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000116void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000117void session_proctitle(Session *);
118int session_setup_x11fwd(Session *);
Damien Miller7207f642008-05-19 15:34:50 +1000119int do_exec_pty(Session *, const char *);
120int do_exec_no_pty(Session *, const char *);
121int do_exec(Session *, const char *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000122void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000123#ifdef LOGIN_NEEDS_UTMPX
124static void do_pre_login(Session *s);
125#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000126void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000127void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000128int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000129
Ben Lindstrombba81212001-06-25 05:01:22 +0000130static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000131
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000133
Damien Millerb38eff82000-04-01 11:09:21 +1000134/* import */
135extern ServerOptions options;
136extern char *__progname;
137extern int log_stderr;
138extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000139extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000140extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000141extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000142extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000143
Damien Miller7b28dc52000-09-05 13:34:53 +1100144/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000145const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000148static int sessions_first_unused = -1;
149static int sessions_nalloc = 0;
150static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100151
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100152#define SUBSYSTEM_NONE 0
153#define SUBSYSTEM_EXT 1
154#define SUBSYSTEM_INT_SFTP 2
155#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100156
Damien Millerad833b32000-08-23 10:46:23 +1000157#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000158login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000159#endif
160
Darren Tucker3e33cec2003-10-02 16:12:36 +1000161static int is_child = 0;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +0000162static int in_chroot = 0;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000163
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000164/* Name and directory of socket for authentication agent forwarding. */
165static char *auth_sock_name = NULL;
166static char *auth_sock_dir = NULL;
167
168/* removes the agent forwarding socket */
169
170static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000171auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000172{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000173 if (auth_sock_name != NULL) {
174 temporarily_use_uid(pw);
175 unlink(auth_sock_name);
176 rmdir(auth_sock_dir);
177 auth_sock_name = NULL;
178 restore_uid();
179 }
180}
181
182static int
183auth_input_request_forwarding(struct passwd * pw)
184{
185 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000186 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000187
188 if (auth_sock_name != NULL) {
189 error("authentication forwarding requested twice.");
190 return 0;
191 }
192
193 /* Temporarily drop privileged uid for mkdir/bind. */
194 temporarily_use_uid(pw);
195
196 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000197 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000198
199 /* Create private directory for socket */
200 if (mkdtemp(auth_sock_dir) == NULL) {
201 packet_send_debug("Agent forwarding disabled: "
202 "mkdtemp() failed: %.100s", strerror(errno));
203 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000204 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000205 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000206 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000207 }
Damien Miller7207f642008-05-19 15:34:50 +1000208
209 xasprintf(&auth_sock_name, "%s/agent.%ld",
210 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000211
Damien Miller7acefbb2014-07-18 14:11:24 +1000212 /* Start a Unix listener on auth_sock_name. */
213 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000214
215 /* Restore the privileged uid. */
216 restore_uid();
217
Damien Miller7acefbb2014-07-18 14:11:24 +1000218 /* Check for socket/bind/listen failure. */
219 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000220 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000221
222 /* Allocate a channel for the authentication agent socket. */
223 nc = channel_new("auth socket",
224 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
225 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000226 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100227 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000228 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000229
230 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000231 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000232 if (auth_sock_dir != NULL) {
233 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000234 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000235 }
236 if (sock != -1)
237 close(sock);
238 auth_sock_name = NULL;
239 auth_sock_dir = NULL;
240 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000241}
242
Darren Tucker1921ed92004-02-10 13:23:28 +1100243static void
244display_loginmsg(void)
245{
Damien Miller46d38de2005-07-17 17:02:09 +1000246 if (buffer_len(&loginmsg) > 0) {
247 buffer_append(&loginmsg, "\0", 1);
248 printf("%s", (char *)buffer_ptr(&loginmsg));
249 buffer_clear(&loginmsg);
250 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100251}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000252
Ben Lindstromb31783d2001-03-22 02:02:12 +0000253void
254do_authenticated(Authctxt *authctxt)
255{
Damien Miller97f39ae2003-02-24 11:57:01 +1100256 setproctitle("%s", authctxt->pw->pw_name);
257
Ben Lindstromb31783d2001-03-22 02:02:12 +0000258 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000259 /* XXX - streamlocal? */
djm@openbsd.org7844f352016-11-30 03:00:05 +0000260 if (no_port_forwarding_flag || options.disable_forwarding ||
Damien Milleraa5b3f82012-12-03 09:50:54 +1100261 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
262 channel_disable_adm_local_opens();
263 else
Ben Lindstromb31783d2001-03-22 02:02:12 +0000264 channel_permit_all_opens();
265
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100266 auth_debug_send();
267
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000268 do_authenticated2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000269 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000270}
271
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000272/* Check untrusted xauth strings for metacharacters */
273static int
274xauth_valid_string(const char *s)
275{
276 size_t i;
277
278 for (i = 0; s[i] != '\0'; i++) {
279 if (!isalnum((u_char)s[i]) &&
280 s[i] != '.' && s[i] != ':' && s[i] != '/' &&
281 s[i] != '-' && s[i] != '_')
282 return 0;
283 }
284 return 1;
285}
286
Darren Tucker293ee3c2014-01-19 15:28:01 +1100287#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000288/*
289 * This is called to fork and execute a command when we have no tty. This
290 * will call do_child from the child, and server_loop from the parent after
291 * setting up file descriptors and such.
292 */
Damien Miller7207f642008-05-19 15:34:50 +1000293int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000294do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000295{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000296 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000297
298#ifdef USE_PIPES
299 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000300
Damien Miller22a29882010-05-10 11:53:54 +1000301 if (s == NULL)
302 fatal("do_exec_no_pty: no session");
303
Damien Millerb38eff82000-04-01 11:09:21 +1000304 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000305 if (pipe(pin) < 0) {
306 error("%s: pipe in: %.100s", __func__, strerror(errno));
307 return -1;
308 }
309 if (pipe(pout) < 0) {
310 error("%s: pipe out: %.100s", __func__, strerror(errno));
311 close(pin[0]);
312 close(pin[1]);
313 return -1;
314 }
Damien Miller8853ca52010-06-26 10:00:14 +1000315 if (pipe(perr) < 0) {
316 error("%s: pipe err: %.100s", __func__,
317 strerror(errno));
318 close(pin[0]);
319 close(pin[1]);
320 close(pout[0]);
321 close(pout[1]);
322 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000323 }
324#else
Damien Millerb38eff82000-04-01 11:09:21 +1000325 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000326
Damien Miller22a29882010-05-10 11:53:54 +1000327 if (s == NULL)
328 fatal("do_exec_no_pty: no session");
329
Damien Millerb38eff82000-04-01 11:09:21 +1000330 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000331 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
332 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
333 return -1;
334 }
Damien Miller8853ca52010-06-26 10:00:14 +1000335 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
336 error("%s: socketpair #2: %.100s", __func__,
337 strerror(errno));
338 close(inout[0]);
339 close(inout[1]);
340 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000341 }
342#endif
343
Damien Millere247cc42000-05-07 12:03:14 +1000344 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000345
Damien Millerb38eff82000-04-01 11:09:21 +1000346 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000347 switch ((pid = fork())) {
348 case -1:
349 error("%s: fork: %.100s", __func__, strerror(errno));
350#ifdef USE_PIPES
351 close(pin[0]);
352 close(pin[1]);
353 close(pout[0]);
354 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000355 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000356 close(perr[1]);
357#else
358 close(inout[0]);
359 close(inout[1]);
360 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000361 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000362#endif
363 return -1;
364 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000365 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000366
Damien Millerb38eff82000-04-01 11:09:21 +1000367 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000368 log_init(__progname, options.log_level,
369 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000370
371 /*
372 * Create a new session and process group since the 4.4BSD
373 * setlogin() affects the entire process group.
374 */
375 if (setsid() < 0)
376 error("setsid failed: %.100s", strerror(errno));
377
378#ifdef USE_PIPES
379 /*
380 * Redirect stdin. We close the parent side of the socket
381 * pair, and make the child side the standard input.
382 */
383 close(pin[1]);
384 if (dup2(pin[0], 0) < 0)
385 perror("dup2 stdin");
386 close(pin[0]);
387
388 /* Redirect stdout. */
389 close(pout[0]);
390 if (dup2(pout[1], 1) < 0)
391 perror("dup2 stdout");
392 close(pout[1]);
393
394 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000395 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000396 if (dup2(perr[1], 2) < 0)
397 perror("dup2 stderr");
398 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000399#else
Damien Millerb38eff82000-04-01 11:09:21 +1000400 /*
401 * Redirect stdin, stdout, and stderr. Stdin and stdout will
402 * use the same socket, as some programs (particularly rdist)
403 * seem to depend on it.
404 */
405 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000406 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000407 if (dup2(inout[0], 0) < 0) /* stdin */
408 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000409 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000410 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000411 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000412 if (dup2(err[0], 2) < 0) /* stderr */
413 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000414 close(err[0]);
415#endif
416
Damien Millerb38eff82000-04-01 11:09:21 +1000417
Tim Rice81ed5182002-09-25 17:38:46 -0700418#ifdef _UNICOS
419 cray_init_job(s->pw); /* set up cray jid and tmpdir */
420#endif
421
Damien Millerb38eff82000-04-01 11:09:21 +1000422 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000423 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000424 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000425 default:
426 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000427 }
Damien Miller7207f642008-05-19 15:34:50 +1000428
Tim Rice81ed5182002-09-25 17:38:46 -0700429#ifdef _UNICOS
430 signal(WJSIGNAL, cray_job_termination_handler);
431#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100432#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100433 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100434#endif
Damien Miller7207f642008-05-19 15:34:50 +1000435
Damien Millerb38eff82000-04-01 11:09:21 +1000436 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000437 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100438 packet_set_interactive(s->display != NULL,
439 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000440
441 /*
442 * Clear loginmsg, since it's the child's responsibility to display
443 * it to the user, otherwise multiple sessions may accumulate
444 * multiple copies of the login messages.
445 */
446 buffer_clear(&loginmsg);
447
Damien Millerb38eff82000-04-01 11:09:21 +1000448#ifdef USE_PIPES
449 /* We are the parent. Close the child sides of the pipes. */
450 close(pin[0]);
451 close(pout[1]);
452 close(perr[1]);
453
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000454 session_set_fds(s, pin[1], pout[0], perr[0],
455 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000456#else
Damien Millerb38eff82000-04-01 11:09:21 +1000457 /* We are the parent. Close the child sides of the socket pairs. */
458 close(inout[0]);
459 close(err[0]);
460
461 /*
462 * Enter the interactive session. Note: server_loop must be able to
463 * handle the case that fdin and fdout are the same.
464 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000465 session_set_fds(s, inout[1], inout[1], err[1],
466 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000467#endif
468 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000469}
470
471/*
472 * This is called to fork and execute a command when we have a tty. This
473 * will call do_child from the child, and server_loop from the parent after
474 * setting up file descriptors, controlling tty, updating wtmp, utmp,
475 * lastlog, and other such operations.
476 */
Damien Miller7207f642008-05-19 15:34:50 +1000477int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000478do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000479{
Damien Millerb38eff82000-04-01 11:09:21 +1000480 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000481 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000482
483 if (s == NULL)
484 fatal("do_exec_pty: no session");
485 ptyfd = s->ptyfd;
486 ttyfd = s->ttyfd;
487
Damien Miller7207f642008-05-19 15:34:50 +1000488 /*
489 * Create another descriptor of the pty master side for use as the
490 * standard input. We could use the original descriptor, but this
491 * simplifies code in server_loop. The descriptor is bidirectional.
492 * Do this before forking (and cleanup in the child) so as to
493 * detect and gracefully fail out-of-fd conditions.
494 */
495 if ((fdout = dup(ptyfd)) < 0) {
496 error("%s: dup #1: %s", __func__, strerror(errno));
497 close(ttyfd);
498 close(ptyfd);
499 return -1;
500 }
501 /* we keep a reference to the pty master */
502 if ((ptymaster = dup(ptyfd)) < 0) {
503 error("%s: dup #2: %s", __func__, strerror(errno));
504 close(ttyfd);
505 close(ptyfd);
506 close(fdout);
507 return -1;
508 }
509
Damien Millerb38eff82000-04-01 11:09:21 +1000510 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000511 switch ((pid = fork())) {
512 case -1:
513 error("%s: fork: %.100s", __func__, strerror(errno));
514 close(fdout);
515 close(ptymaster);
516 close(ttyfd);
517 close(ptyfd);
518 return -1;
519 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000520 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000521
Damien Miller7207f642008-05-19 15:34:50 +1000522 close(fdout);
523 close(ptymaster);
524
Damien Miller942da032000-08-18 13:59:06 +1000525 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000526 log_init(__progname, options.log_level,
527 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000528 /* Close the master side of the pseudo tty. */
529 close(ptyfd);
530
531 /* Make the pseudo tty our controlling tty. */
532 pty_make_controlling_tty(&ttyfd, s->tty);
533
Damien Miller9c751422001-10-10 15:02:46 +1000534 /* Redirect stdin/stdout/stderr from the pseudo tty. */
535 if (dup2(ttyfd, 0) < 0)
536 error("dup2 stdin: %s", strerror(errno));
537 if (dup2(ttyfd, 1) < 0)
538 error("dup2 stdout: %s", strerror(errno));
539 if (dup2(ttyfd, 2) < 0)
540 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000541
542 /* Close the extra descriptor for the pseudo tty. */
543 close(ttyfd);
544
Damien Miller942da032000-08-18 13:59:06 +1000545 /* record login, etc. similar to login(1) */
Tim Rice81ed5182002-09-25 17:38:46 -0700546#ifdef _UNICOS
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000547 cray_init_job(s->pw); /* set up cray jid and tmpdir */
Tim Rice81ed5182002-09-25 17:38:46 -0700548#endif /* _UNICOS */
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000549#ifndef HAVE_OSF_SIA
550 do_login(s, command);
Damien Miller364a9bd2001-04-16 18:37:05 +1000551#endif
Damien Miller7207f642008-05-19 15:34:50 +1000552 /*
553 * Do common processing for the child, such as execing
554 * the command.
555 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000556 do_child(s, command);
557 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000558 default:
559 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000560 }
Damien Miller7207f642008-05-19 15:34:50 +1000561
Tim Rice81ed5182002-09-25 17:38:46 -0700562#ifdef _UNICOS
563 signal(WJSIGNAL, cray_job_termination_handler);
564#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100565#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100566 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100567#endif
Damien Miller7207f642008-05-19 15:34:50 +1000568
Damien Millerb38eff82000-04-01 11:09:21 +1000569 s->pid = pid;
570
571 /* Parent. Close the slave side of the pseudo tty. */
572 close(ttyfd);
573
Damien Millerb38eff82000-04-01 11:09:21 +1000574 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000575 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100576 packet_set_interactive(1,
577 options.ip_qos_interactive, options.ip_qos_bulk);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000578 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Miller7207f642008-05-19 15:34:50 +1000579 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000580}
581
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000582#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000583static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000584do_pre_login(Session *s)
585{
Darren Tuckeraa377682016-06-20 15:55:34 +1000586 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000587 socklen_t fromlen;
588 struct sockaddr_storage from;
589 pid_t pid = getpid();
590
591 /*
592 * Get IP address of client. If the connection is not a socket, let
593 * the address be 0.0.0.0.
594 */
595 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000596 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000597 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000598 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100599 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000600 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000601 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000602 }
603 }
604
605 record_utmp_only(pid, s->tty, s->pw->pw_name,
Darren Tuckeraa377682016-06-20 15:55:34 +1000606 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000607 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000608}
609#endif
610
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000611/*
612 * This is called to fork and execute a command. If another command is
613 * to be forced, execute that instead.
614 */
Damien Miller7207f642008-05-19 15:34:50 +1000615int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000616do_exec(Session *s, const char *command)
617{
djm@openbsd.org95767262016-03-07 19:02:43 +0000618 struct ssh *ssh = active_state; /* XXX */
Damien Miller7207f642008-05-19 15:34:50 +1000619 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000620 const char *forced = NULL, *tty = NULL;
621 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000622
Damien Millere2754432006-07-24 14:06:47 +1000623 if (options.adm_forced_command) {
624 original_command = command;
625 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100626 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000627 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000628 original_command = command;
629 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100630 forced = "(key-option)";
631 }
632 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100633 if (IS_INTERNAL_SFTP(command)) {
634 s->is_subsystem = s->is_subsystem ?
635 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
636 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100637 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100638 snprintf(session_type, sizeof(session_type),
639 "forced-command %s '%.900s'", forced, command);
640 } else if (s->is_subsystem) {
641 snprintf(session_type, sizeof(session_type),
642 "subsystem '%.900s'", s->subsys);
643 } else if (command == NULL) {
644 snprintf(session_type, sizeof(session_type), "shell");
645 } else {
646 /* NB. we don't log unforced commands to preserve privacy */
647 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000648 }
649
Damien Miller71df7522013-10-15 12:12:02 +1100650 if (s->ttyfd != -1) {
651 tty = s->tty;
652 if (strncmp(tty, "/dev/", 5) == 0)
653 tty += 5;
654 }
655
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000656 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100657 session_type,
658 tty == NULL ? "" : " on ",
659 tty == NULL ? "" : tty,
660 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000661 ssh_remote_ipaddr(ssh),
662 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000663 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100664
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100665#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100666 if (command != NULL)
667 PRIVSEP(audit_run_command(command));
668 else if (s->ttyfd == -1) {
669 char *shell = s->pw->pw_shell;
670
671 if (shell[0] == '\0') /* empty shell means /bin/sh */
672 shell =_PATH_BSHELL;
673 PRIVSEP(audit_run_command(shell));
674 }
675#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000676 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000677 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000678 else
Damien Miller7207f642008-05-19 15:34:50 +1000679 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000680
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000681 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000682
Darren Tucker09991742004-07-17 17:05:14 +1000683 /*
684 * Clear loginmsg: it's the child's responsibility to display
685 * it to the user, otherwise multiple sessions may accumulate
686 * multiple copies of the login messages.
687 */
688 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000689
690 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000691}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000692
Damien Miller942da032000-08-18 13:59:06 +1000693/* administrative, login(1)-like work */
694void
Damien Miller69b69aa2000-10-28 14:19:58 +1100695do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000696{
djm@openbsd.org95767262016-03-07 19:02:43 +0000697 struct ssh *ssh = active_state; /* XXX */
Damien Miller942da032000-08-18 13:59:06 +1000698 socklen_t fromlen;
699 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000700 struct passwd * pw = s->pw;
701 pid_t pid = getpid();
702
703 /*
704 * Get IP address of client. If the connection is not a socket, let
705 * the address be 0.0.0.0.
706 */
707 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000708 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000709 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000710 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000711 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000712 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000713 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000714 }
715 }
716
717 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000718 if (!use_privsep)
719 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000720 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000721 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000722 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000723
Kevin Steves092f2ef2000-10-14 13:36:13 +0000724#ifdef USE_PAM
725 /*
726 * If password change is needed, do it now.
727 * This needs to occur before the ~/.hushlogin check.
728 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100729 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
730 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000731 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100732 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000733 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000734 }
735#endif
736
Damien Millercf205e82001-04-16 18:29:15 +1000737 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000738 return;
739
Darren Tucker1921ed92004-02-10 13:23:28 +1100740 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000741
Damien Millercf205e82001-04-16 18:29:15 +1000742 do_motd();
743}
744
745/*
746 * Display the message of the day.
747 */
748void
749do_motd(void)
750{
751 FILE *f;
752 char buf[256];
753
Damien Miller942da032000-08-18 13:59:06 +1000754 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000755#ifdef HAVE_LOGIN_CAP
756 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
757 "/etc/motd"), "r");
758#else
Damien Miller942da032000-08-18 13:59:06 +1000759 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000760#endif
Damien Miller942da032000-08-18 13:59:06 +1000761 if (f) {
762 while (fgets(buf, sizeof(buf), f))
763 fputs(buf, stdout);
764 fclose(f);
765 }
766 }
767}
768
Kevin Steves8f63caa2001-07-04 18:23:02 +0000769
770/*
771 * Check for quiet login, either .hushlogin or command given.
772 */
773int
774check_quietlogin(Session *s, const char *command)
775{
776 char buf[256];
777 struct passwd *pw = s->pw;
778 struct stat st;
779
780 /* Return 1 if .hushlogin exists or a command given. */
781 if (command != NULL)
782 return 1;
783 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
784#ifdef HAVE_LOGIN_CAP
785 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
786 return 1;
787#else
788 if (stat(buf, &st) >= 0)
789 return 1;
790#endif
791 return 0;
792}
793
Damien Millerb38eff82000-04-01 11:09:21 +1000794/*
795 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100796 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000797 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000798void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000799child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100800 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000801{
Damien Millerb38eff82000-04-01 11:09:21 +1000802 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000803 u_int envsize;
804 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000805
Damien Miller8569eba2014-03-04 09:35:17 +1100806 if (strchr(name, '=') != NULL) {
807 error("Invalid environment variable \"%.100s\"", name);
808 return;
809 }
810
Damien Millerb38eff82000-04-01 11:09:21 +1000811 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000812 * If we're passed an uninitialized list, allocate a single null
813 * entry before continuing.
814 */
815 if (*envp == NULL && *envsizep == 0) {
816 *envp = xmalloc(sizeof(char *));
817 *envp[0] = NULL;
818 *envsizep = 1;
819 }
820
821 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000822 * Find the slot where the value should be stored. If the variable
823 * already exists, we reuse the slot; otherwise we append a new slot
824 * at the end of the array, expanding if necessary.
825 */
826 env = *envp;
827 namelen = strlen(name);
828 for (i = 0; env[i]; i++)
829 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
830 break;
831 if (env[i]) {
832 /* Reuse the slot. */
Darren Tuckera627d422013-06-02 07:31:17 +1000833 free(env[i]);
Damien Millerb38eff82000-04-01 11:09:21 +1000834 } else {
835 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000836 envsize = *envsizep;
837 if (i >= envsize - 1) {
838 if (envsize >= 1000)
839 fatal("child_set_env: too many env vars");
840 envsize += 50;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000841 env = (*envp) = xreallocarray(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000842 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000843 }
844 /* Need to set the NULL pointer at end of array beyond the new slot. */
845 env[i + 1] = NULL;
846 }
847
848 /* Allocate space and format the variable in the appropriate slot. */
849 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
850 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
851}
852
853/*
854 * Reads environment variables from the given file and adds/overrides them
855 * into the environment. If the file does not exist, this does nothing.
856 * Otherwise, it must consist of empty lines, comments (line starts with '#')
857 * and assignments of the form name=value. No other forms are allowed.
858 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000860read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100861 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000862{
863 FILE *f;
864 char buf[4096];
865 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000866 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000867
868 f = fopen(filename, "r");
869 if (!f)
870 return;
871
872 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000873 if (++lineno > 1000)
874 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000875 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
876 ;
877 if (!*cp || *cp == '#' || *cp == '\n')
878 continue;
Damien Miller14b017d2007-09-17 16:09:15 +1000879
880 cp[strcspn(cp, "\n")] = '\0';
881
Damien Millerb38eff82000-04-01 11:09:21 +1000882 value = strchr(cp, '=');
883 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000884 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
885 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000886 continue;
887 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000888 /*
889 * Replace the equals sign by nul, and advance value to
890 * the value string.
891 */
Damien Millerb38eff82000-04-01 11:09:21 +1000892 *value = '\0';
893 value++;
894 child_set_env(env, envsize, cp, value);
895 }
896 fclose(f);
897}
898
Darren Tuckere1a790d2003-09-16 11:52:19 +1000899#ifdef HAVE_ETC_DEFAULT_LOGIN
900/*
901 * Return named variable from specified environment, or NULL if not present.
902 */
903static char *
904child_get_env(char **env, const char *name)
905{
906 int i;
907 size_t len;
908
909 len = strlen(name);
910 for (i=0; env[i] != NULL; i++)
911 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
912 return(env[i] + len + 1);
913 return NULL;
914}
915
916/*
917 * Read /etc/default/login.
918 * We pick up the PATH (or SUPATH for root) and UMASK.
919 */
920static void
921read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
922{
923 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000924 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000925 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000926
927 /*
928 * We don't want to copy the whole file to the child's environment,
929 * so we use a temporary environment and copy the variables we're
930 * interested in.
931 */
932 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
933
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000934 if (tmpenv == NULL)
935 return;
936
Darren Tuckere1a790d2003-09-16 11:52:19 +1000937 if (uid == 0)
938 var = child_get_env(tmpenv, "SUPATH");
939 else
940 var = child_get_env(tmpenv, "PATH");
941 if (var != NULL)
942 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100943
Darren Tuckere1a790d2003-09-16 11:52:19 +1000944 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
945 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000946 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100947
Darren Tuckere1a790d2003-09-16 11:52:19 +1000948 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +1000949 free(tmpenv[i]);
950 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000951}
952#endif /* HAVE_ETC_DEFAULT_LOGIN */
953
Damien Miller7dff8692005-05-26 11:34:51 +1000954void
955copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000956{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100957 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000958 int i;
959
Damien Millerbb9ffc12002-01-08 10:59:32 +1100960 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000961 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000962
Damien Millerbb9ffc12002-01-08 10:59:32 +1100963 for(i = 0; source[i] != NULL; i++) {
964 var_name = xstrdup(source[i]);
965 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000966 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000967 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000968 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100969 *var_val++ = '\0';
970
971 debug3("Copy environment: %s=%s", var_name, var_val);
972 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +1100973
Darren Tuckerf60845f2013-06-02 08:07:31 +1000974 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000975 }
976}
Damien Millercb5e44a2000-09-29 12:12:36 +1100977
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000978static char **
979do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000980{
djm@openbsd.org95767262016-03-07 19:02:43 +0000981 struct ssh *ssh = active_state; /* XXX */
Damien Millerb38eff82000-04-01 11:09:21 +1000982 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000983 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +1000984 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000985 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100986#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +1000987 char *path = NULL;
988#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000989
Damien Millerb38eff82000-04-01 11:09:21 +1000990 /* Initialize the environment. */
991 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000992 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +1000993 env[0] = NULL;
994
Damien Millerbac2d8a2000-09-05 16:13:06 +1100995#ifdef HAVE_CYGWIN
996 /*
997 * The Windows environment contains some setting which are
998 * important for a running system. They must not be dropped.
999 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001000 {
1001 char **p;
1002
1003 p = fetch_windows_environment();
1004 copy_environment(p, &env, &envsize);
1005 free_windows_environment(p);
1006 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001007#endif
1008
Darren Tucker0efd1552003-08-26 11:49:55 +10001009#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001010 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001011 * the childs environment as they see fit
1012 */
1013 ssh_gssapi_do_child(&env, &envsize);
1014#endif
1015
djm@openbsd.org83b58182016-08-19 03:18:06 +00001016 /* Set basic environment. */
1017 for (i = 0; i < s->num_env; i++)
1018 child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
Darren Tucker46bc0752004-05-02 22:11:30 +10001019
djm@openbsd.org83b58182016-08-19 03:18:06 +00001020 child_set_env(&env, &envsize, "USER", pw->pw_name);
1021 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001022#ifdef _AIX
djm@openbsd.org83b58182016-08-19 03:18:06 +00001023 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001024#endif
djm@openbsd.org83b58182016-08-19 03:18:06 +00001025 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001026#ifdef HAVE_LOGIN_CAP
djm@openbsd.org83b58182016-08-19 03:18:06 +00001027 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1028 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1029 else
1030 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001031#else /* HAVE_LOGIN_CAP */
1032# ifndef HAVE_CYGWIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001033 /*
1034 * There's no standard path on Windows. The path contains
1035 * important components pointing to the system directories,
1036 * needed for loading shared libraries. So the path better
1037 * remains intact here.
1038 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001039# ifdef HAVE_ETC_DEFAULT_LOGIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001040 read_etc_default_login(&env, &envsize, pw->pw_uid);
1041 path = child_get_env(env, "PATH");
Darren Tuckere1a790d2003-09-16 11:52:19 +10001042# endif /* HAVE_ETC_DEFAULT_LOGIN */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001043 if (path == NULL || *path == '\0') {
1044 child_set_env(&env, &envsize, "PATH",
1045 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1046 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001047# endif /* HAVE_CYGWIN */
1048#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001049
djm@openbsd.org83b58182016-08-19 03:18:06 +00001050 snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
1051 child_set_env(&env, &envsize, "MAIL", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001052
djm@openbsd.org83b58182016-08-19 03:18:06 +00001053 /* Normal systems set SHELL by default. */
1054 child_set_env(&env, &envsize, "SHELL", shell);
1055
Damien Millerb38eff82000-04-01 11:09:21 +10001056 if (getenv("TZ"))
1057 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1058
1059 /* Set custom environment options from RSA authentication. */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001060 while (custom_environment) {
1061 struct envstring *ce = custom_environment;
1062 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001063
djm@openbsd.org83b58182016-08-19 03:18:06 +00001064 for (i = 0; str[i] != '=' && str[i]; i++)
1065 ;
1066 if (str[i] == '=') {
1067 str[i] = 0;
1068 child_set_env(&env, &envsize, str, str + i + 1);
Damien Millerb38eff82000-04-01 11:09:21 +10001069 }
djm@openbsd.org83b58182016-08-19 03:18:06 +00001070 custom_environment = ce->next;
1071 free(ce->s);
1072 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001073 }
1074
Damien Millerf37e2462002-09-19 11:47:55 +10001075 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001076 snprintf(buf, sizeof buf, "%.50s %d %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001077 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1078 ssh_local_port(ssh));
Damien Millerb38eff82000-04-01 11:09:21 +10001079 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1080
Damien Miller00111382003-03-10 11:21:17 +11001081 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001082 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001083 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1084 laddr, ssh_local_port(ssh));
Darren Tuckera627d422013-06-02 07:31:17 +10001085 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001086 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1087
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001088 if (s->ttyfd != -1)
1089 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1090 if (s->term)
1091 child_set_env(&env, &envsize, "TERM", s->term);
1092 if (s->display)
1093 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001094 if (original_command)
1095 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1096 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001097
Tim Rice81ed5182002-09-25 17:38:46 -07001098#ifdef _UNICOS
1099 if (cray_tmpdir[0] != '\0')
1100 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1101#endif /* _UNICOS */
1102
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001103 /*
1104 * Since we clear KRB5CCNAME at startup, if it's set now then it
1105 * must have been set by a native authentication method (eg AIX or
1106 * SIA), so copy it to the child.
1107 */
1108 {
1109 char *cp;
1110
1111 if ((cp = getenv("KRB5CCNAME")) != NULL)
1112 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1113 }
1114
Damien Millerb38eff82000-04-01 11:09:21 +10001115#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001116 {
1117 char *cp;
1118
1119 if ((cp = getenv("AUTHSTATE")) != NULL)
1120 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001121 read_environment_file(&env, &envsize, "/etc/environment");
1122 }
Damien Millerb38eff82000-04-01 11:09:21 +10001123#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001124#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001125 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001126 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001127 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001128#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001129#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001130 /*
1131 * Pull in any environment variables that may have
1132 * been set by PAM.
1133 */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001134 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001135 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001136
Damien Millerc756e9b2003-11-17 21:41:42 +11001137 p = fetch_pam_child_environment();
1138 copy_environment(p, &env, &envsize);
1139 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001140
Damien Millerc756e9b2003-11-17 21:41:42 +11001141 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001142 copy_environment(p, &env, &envsize);
1143 free_pam_environment(p);
1144 }
Damien Millerb38eff82000-04-01 11:09:21 +10001145#endif /* USE_PAM */
1146
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001147 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001148 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001149 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001150
1151 /* read $HOME/.ssh/environment. */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001152 if (options.permit_user_env) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001153 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001154 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001155 read_environment_file(&env, &envsize, buf);
1156 }
1157 if (debug_flag) {
1158 /* dump the environment */
1159 fprintf(stderr, "Environment:\n");
1160 for (i = 0; env[i]; i++)
1161 fprintf(stderr, " %.200s\n", env[i]);
1162 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001163 return env;
1164}
1165
1166/*
1167 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1168 * first in this order).
1169 */
1170static void
1171do_rc_files(Session *s, const char *shell)
1172{
1173 FILE *f = NULL;
1174 char cmd[1024];
1175 int do_xauth;
1176 struct stat st;
1177
1178 do_xauth =
1179 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1180
Damien Millera1b48cc2008-03-27 11:02:02 +11001181 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001182 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001183 !no_user_rc && options.permit_user_rc &&
1184 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001185 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1186 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1187 if (debug_flag)
1188 fprintf(stderr, "Running %s\n", cmd);
1189 f = popen(cmd, "w");
1190 if (f) {
1191 if (do_xauth)
1192 fprintf(f, "%s %s\n", s->auth_proto,
1193 s->auth_data);
1194 pclose(f);
1195 } else
1196 fprintf(stderr, "Could not run %s\n",
1197 _PATH_SSH_USER_RC);
1198 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1199 if (debug_flag)
1200 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1201 _PATH_SSH_SYSTEM_RC);
1202 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1203 if (f) {
1204 if (do_xauth)
1205 fprintf(f, "%s %s\n", s->auth_proto,
1206 s->auth_data);
1207 pclose(f);
1208 } else
1209 fprintf(stderr, "Could not run %s\n",
1210 _PATH_SSH_SYSTEM_RC);
1211 } else if (do_xauth && options.xauth_location != NULL) {
1212 /* Add authority data to .Xauthority if appropriate. */
1213 if (debug_flag) {
1214 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001215 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001216 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001217 fprintf(stderr,
1218 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001219 options.xauth_location, s->auth_display,
1220 s->auth_proto, s->auth_data);
1221 }
1222 snprintf(cmd, sizeof cmd, "%s -q -",
1223 options.xauth_location);
1224 f = popen(cmd, "w");
1225 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001226 fprintf(f, "remove %s\n",
1227 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001228 fprintf(f, "add %s %s %s\n",
1229 s->auth_display, s->auth_proto,
1230 s->auth_data);
1231 pclose(f);
1232 } else {
1233 fprintf(stderr, "Could not run %s\n",
1234 cmd);
1235 }
1236 }
1237}
1238
1239static void
1240do_nologin(struct passwd *pw)
1241{
1242 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001243 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1244 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001245
1246#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001247 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001248 return;
1249 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001250#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001251 if (pw->pw_uid == 0)
1252 return;
1253 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001254#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001255 if (stat(nl, &sb) == -1) {
1256 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001257 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001258 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001259 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001260
1261 /* /etc/nologin exists. Print its contents if we can and exit. */
1262 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1263 if ((f = fopen(nl, "r")) != NULL) {
1264 while (fgets(buf, sizeof(buf), f))
1265 fputs(buf, stderr);
1266 fclose(f);
1267 }
1268 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001269}
1270
Damien Millerd8cb1f12008-02-10 22:40:12 +11001271/*
1272 * Chroot into a directory after checking it for safety: all path components
1273 * must be root-owned directories with strict permissions.
1274 */
1275static void
1276safely_chroot(const char *path, uid_t uid)
1277{
1278 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001279 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001280 struct stat st;
1281
1282 if (*path != '/')
1283 fatal("chroot path does not begin at root");
1284 if (strlen(path) >= sizeof(component))
1285 fatal("chroot path too long");
1286
1287 /*
1288 * Descend the path, checking that each component is a
1289 * root-owned directory with strict permissions.
1290 */
1291 for (cp = path; cp != NULL;) {
1292 if ((cp = strchr(cp, '/')) == NULL)
1293 strlcpy(component, path, sizeof(component));
1294 else {
1295 cp++;
1296 memcpy(component, path, cp - path);
1297 component[cp - path] = '\0';
1298 }
1299
1300 debug3("%s: checking '%s'", __func__, component);
1301
1302 if (stat(component, &st) != 0)
1303 fatal("%s: stat(\"%s\"): %s", __func__,
1304 component, strerror(errno));
1305 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1306 fatal("bad ownership or modes for chroot "
1307 "directory %s\"%s\"",
1308 cp == NULL ? "" : "component ", component);
1309 if (!S_ISDIR(st.st_mode))
1310 fatal("chroot path %s\"%s\" is not a directory",
1311 cp == NULL ? "" : "component ", component);
1312
1313 }
1314
1315 if (chdir(path) == -1)
1316 fatal("Unable to chdir to chroot path \"%s\": "
1317 "%s", path, strerror(errno));
1318 if (chroot(path) == -1)
1319 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1320 if (chdir("/") == -1)
1321 fatal("%s: chdir(/) after chroot: %s",
1322 __func__, strerror(errno));
1323 verbose("Changed root directory to \"%s\"", path);
1324}
1325
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001326/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001327void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001328do_setusercontext(struct passwd *pw)
1329{
Damien Miller54e37732008-02-10 22:48:55 +11001330 char *chroot_path, *tmp;
1331
Darren Tucker97528352010-11-05 12:03:05 +11001332 platform_setusercontext(pw);
1333
Darren Tuckerb12fe272010-11-05 14:47:01 +11001334 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001335#ifdef HAVE_LOGIN_CAP
1336 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001337 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001338 perror("unable to set user context");
1339 exit(1);
1340 }
1341#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001342 if (setlogin(pw->pw_name) < 0)
1343 error("setlogin failed: %s", strerror(errno));
1344 if (setgid(pw->pw_gid) < 0) {
1345 perror("setgid");
1346 exit(1);
1347 }
1348 /* Initialize the group list. */
1349 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1350 perror("initgroups");
1351 exit(1);
1352 }
1353 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001354#endif
1355
Darren Tucker920612e2010-11-05 12:36:15 +11001356 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001357
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001358 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001359 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001360 tmp = tilde_expand_filename(options.chroot_directory,
1361 pw->pw_uid);
1362 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1363 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001364 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001365 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001366 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001367 /* Make sure we don't attempt to chroot again */
1368 free(options.chroot_directory);
1369 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001370 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001371 }
1372
1373#ifdef HAVE_LOGIN_CAP
1374 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1375 perror("unable to set user context (setuser)");
1376 exit(1);
1377 }
Damien Miller58528402013-03-15 11:22:37 +11001378 /*
1379 * FreeBSD's setusercontext() will not apply the user's
1380 * own umask setting unless running with the user's UID.
1381 */
1382 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001383#else
Tim Rice9464ba62014-01-20 17:59:28 -08001384# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001385 /*
1386 * In a chroot environment, the set_id() will always fail;
1387 * typically because of the lack of necessary authentication
1388 * services and runtime such as ./usr/lib/libiaf.so,
1389 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1390 * internal sftp chroot case. We'll lose auditing and ACLs but
1391 * permanently_set_uid will take care of the rest.
1392 */
1393 if (!in_chroot && set_id(pw->pw_name) != 0)
1394 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001395# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001396 /* Permanently switch to the desired uid. */
1397 permanently_set_uid(pw);
1398#endif
Damien Millera56086b2013-04-23 15:24:18 +10001399 } else if (options.chroot_directory != NULL &&
1400 strcasecmp(options.chroot_directory, "none") != 0) {
1401 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001402 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001403
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001404 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1405 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1406}
1407
Ben Lindstrom08105192002-03-22 02:50:06 +00001408static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001409do_pwchange(Session *s)
1410{
Darren Tucker09991742004-07-17 17:05:14 +10001411 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001412 fprintf(stderr, "WARNING: Your password has expired.\n");
1413 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001414 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001415 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001416#ifdef WITH_SELINUX
1417 setexeccon(NULL);
1418#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001419#ifdef PASSWD_NEEDS_USERNAME
1420 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1421 (char *)NULL);
1422#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001423 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001424#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001425 perror("passwd");
1426 } else {
1427 fprintf(stderr,
1428 "Password change required but no TTY available.\n");
1429 }
1430 exit(1);
1431}
1432
1433static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001434child_close_fds(void)
1435{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001436 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001437
djm@openbsd.org141efe42015-01-14 20:05:27 +00001438 if (auth_sock != -1) {
1439 close(auth_sock);
1440 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001441 }
1442
Darren Tucker23bc8d02004-02-06 16:24:31 +11001443 if (packet_get_connection_in() == packet_get_connection_out())
1444 close(packet_get_connection_in());
1445 else {
1446 close(packet_get_connection_in());
1447 close(packet_get_connection_out());
1448 }
1449 /*
1450 * Close all descriptors related to channels. They will still remain
1451 * open in the parent.
1452 */
1453 /* XXX better use close-on-exec? -markus */
1454 channel_close_all();
1455
1456 /*
1457 * Close any extra file descriptors. Note that there may still be
1458 * descriptors left by system functions. They will be closed later.
1459 */
1460 endpwent();
1461
1462 /*
Damien Miller788f2122005-11-05 15:14:59 +11001463 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001464 * hanging around in clients. Note that we want to do this after
1465 * initgroups, because at least on Solaris 2.3 it leaves file
1466 * descriptors open.
1467 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001468 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001469}
1470
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001471/*
1472 * Performs common processing for the child, such as setting up the
1473 * environment, closing extra file descriptors, setting the user and group
1474 * ids, and executing the command or shell.
1475 */
Damien Millerdfc24252008-02-10 22:29:40 +11001476#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001477void
1478do_child(Session *s, const char *command)
1479{
1480 extern char **environ;
1481 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001482 char *argv[ARGV_MAX];
djm@openbsd.org83b58182016-08-19 03:18:06 +00001483 const char *shell, *shell0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001484 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001485 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001486
1487 /* remove hostkey from the child's memory */
1488 destroy_sensitive_data();
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001489 packet_clear_keys();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001490
Darren Tucker23bc8d02004-02-06 16:24:31 +11001491 /* Force a password change */
1492 if (s->authctxt->force_pwchange) {
1493 do_setusercontext(pw);
1494 child_close_fds();
1495 do_pwchange(s);
1496 exit(1);
1497 }
1498
Tim Rice81ed5182002-09-25 17:38:46 -07001499#ifdef _UNICOS
1500 cray_setup(pw->pw_uid, pw->pw_name, command);
1501#endif /* _UNICOS */
1502
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001503 /*
1504 * Login(1) does this as well, and it needs uid 0 for the "-h"
1505 * switch, so we let login(1) to this for us.
1506 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001507#ifdef HAVE_OSF_SIA
djm@openbsd.org83b58182016-08-19 03:18:06 +00001508 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1509 if (!check_quietlogin(s, command))
1510 do_motd();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001511#else /* HAVE_OSF_SIA */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001512 /* When PAM is enabled we rely on it to do the nologin check */
1513 if (!options.use_pam)
1514 do_nologin(pw);
1515 do_setusercontext(pw);
1516 /*
1517 * PAM session modules in do_setusercontext may have
1518 * generated messages, so if this in an interactive
1519 * login then display them too.
1520 */
1521 if (!check_quietlogin(s, command))
1522 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001523#endif /* HAVE_OSF_SIA */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001524
Darren Tucker69687f42004-09-11 22:17:26 +10001525#ifdef USE_PAM
djm@openbsd.org83b58182016-08-19 03:18:06 +00001526 if (options.use_pam && !is_pam_session_open()) {
Darren Tucker48554152005-04-21 19:50:55 +10001527 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001528 display_loginmsg();
1529 exit(254);
1530 }
1531#endif
1532
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001533 /*
1534 * Get the shell from the password data. An empty shell field is
1535 * legal, and means /bin/sh.
1536 */
1537 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001538
1539 /*
1540 * Make sure $SHELL points to the shell from the password file,
1541 * even if shell is overridden from login.conf
1542 */
1543 env = do_setup_env(s, shell);
1544
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001545#ifdef HAVE_LOGIN_CAP
1546 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1547#endif
1548
Damien Millerb38eff82000-04-01 11:09:21 +10001549 /*
1550 * Close the connection descriptors; note that this is the child, and
1551 * the server will still have the socket open, and it is important
1552 * that we do not shutdown it. Note that the descriptors cannot be
1553 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001554 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001555 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001556 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001557
Damien Millerb38eff82000-04-01 11:09:21 +10001558 /*
Damien Miller05eda432002-02-10 18:32:28 +11001559 * Must take new environment into use so that .ssh/rc,
1560 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001561 */
1562 environ = env;
1563
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001564#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001565 /*
1566 * At this point, we check to see if AFS is active and if we have
1567 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1568 * if we can (and need to) extend the ticket into an AFS token. If
1569 * we don't do this, we run into potential problems if the user's
1570 * home directory is in AFS and it's not world-readable.
1571 */
1572
1573 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001574 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001575 char cell[64];
1576
1577 debug("Getting AFS token");
1578
1579 k_setpag();
1580
1581 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1582 krb5_afslog(s->authctxt->krb5_ctx,
1583 s->authctxt->krb5_fwd_ccache, cell, NULL);
1584
1585 krb5_afslog_home(s->authctxt->krb5_ctx,
1586 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1587 }
1588#endif
1589
Damien Miller788f2122005-11-05 15:14:59 +11001590 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001591 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001592 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001593#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001594 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001595#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001596 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001597 fprintf(stderr, "Could not chdir to home "
1598 "directory %s: %s\n", pw->pw_dir,
1599 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001600 }
Damien Miller6051c942008-06-16 07:53:16 +10001601 if (r)
1602 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001603 }
1604
Damien Millera1939002008-03-15 17:27:58 +11001605 closefrom(STDERR_FILENO + 1);
1606
djm@openbsd.org83b58182016-08-19 03:18:06 +00001607 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001608
1609 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001610 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001611
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001612 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1613 printf("This service allows sftp connections only.\n");
1614 fflush(NULL);
1615 exit(1);
1616 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001617 extern int optind, optreset;
1618 int i;
1619 char *p, *args;
1620
Darren Tuckerac46a912009-06-21 17:55:23 +10001621 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001622 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001623 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1624 if (i < ARGV_MAX - 1)
1625 argv[i++] = p;
1626 argv[i] = NULL;
1627 optind = optreset = 1;
1628 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001629#ifdef WITH_SELINUX
1630 ssh_selinux_change_context("sftpd_t");
1631#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001632 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001633 }
1634
Darren Tucker695ed392009-10-07 09:02:18 +11001635 fflush(NULL);
1636
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001637 /* Get the last component of the shell name. */
1638 if ((shell0 = strrchr(shell, '/')) != NULL)
1639 shell0++;
1640 else
1641 shell0 = shell;
1642
Damien Millerb38eff82000-04-01 11:09:21 +10001643 /*
1644 * If we have no command, execute the shell. In this case, the shell
1645 * name to be passed in argv[0] is preceded by '-' to indicate that
1646 * this is a login shell.
1647 */
1648 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001649 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001650
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001651 /* Start the shell. Set initial character to '-'. */
1652 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001653
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001654 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1655 >= sizeof(argv0) - 1) {
1656 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001657 perror(shell);
1658 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001659 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001660
1661 /* Execute the shell. */
1662 argv[0] = argv0;
1663 argv[1] = NULL;
1664 execve(shell, argv, env);
1665
1666 /* Executing the shell failed. */
1667 perror(shell);
1668 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001669 }
1670 /*
1671 * Execute the command using the user's shell. This uses the -c
1672 * option to execute the command.
1673 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001674 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001675 argv[1] = "-c";
1676 argv[2] = (char *) command;
1677 argv[3] = NULL;
1678 execve(shell, argv, env);
1679 perror(shell);
1680 exit(1);
1681}
1682
Damien Miller7207f642008-05-19 15:34:50 +10001683void
1684session_unused(int id)
1685{
1686 debug3("%s: session id %d unused", __func__, id);
1687 if (id >= options.max_sessions ||
1688 id >= sessions_nalloc) {
1689 fatal("%s: insane session id %d (max %d nalloc %d)",
1690 __func__, id, options.max_sessions, sessions_nalloc);
1691 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001692 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001693 sessions[id].self = id;
1694 sessions[id].used = 0;
1695 sessions[id].chanid = -1;
1696 sessions[id].ptyfd = -1;
1697 sessions[id].ttyfd = -1;
1698 sessions[id].ptymaster = -1;
1699 sessions[id].x11_chanids = NULL;
1700 sessions[id].next_unused = sessions_first_unused;
1701 sessions_first_unused = id;
1702}
1703
Damien Millerb38eff82000-04-01 11:09:21 +10001704Session *
1705session_new(void)
1706{
Damien Miller7207f642008-05-19 15:34:50 +10001707 Session *s, *tmp;
1708
1709 if (sessions_first_unused == -1) {
1710 if (sessions_nalloc >= options.max_sessions)
1711 return NULL;
1712 debug2("%s: allocate (allocated %d max %d)",
1713 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001714 tmp = xrecallocarray(sessions, sessions_nalloc,
1715 sessions_nalloc + 1, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001716 if (tmp == NULL) {
1717 error("%s: cannot allocate %d sessions",
1718 __func__, sessions_nalloc + 1);
1719 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001720 }
Damien Miller7207f642008-05-19 15:34:50 +10001721 sessions = tmp;
1722 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001723 }
Damien Miller7207f642008-05-19 15:34:50 +10001724
1725 if (sessions_first_unused >= sessions_nalloc ||
1726 sessions_first_unused < 0) {
1727 fatal("%s: insane first_unused %d max %d nalloc %d",
1728 __func__, sessions_first_unused, options.max_sessions,
1729 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001730 }
Damien Miller7207f642008-05-19 15:34:50 +10001731
1732 s = &sessions[sessions_first_unused];
1733 if (s->used) {
1734 fatal("%s: session %d already used",
1735 __func__, sessions_first_unused);
1736 }
1737 sessions_first_unused = s->next_unused;
1738 s->used = 1;
1739 s->next_unused = -1;
1740 debug("session_new: session %d", s->self);
1741
1742 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001743}
1744
Ben Lindstrombba81212001-06-25 05:01:22 +00001745static void
Damien Millerb38eff82000-04-01 11:09:21 +10001746session_dump(void)
1747{
1748 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001749 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001750 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001751
1752 debug("dump: used %d next_unused %d session %d %p "
1753 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001754 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001755 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001756 s->self,
1757 s,
1758 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001759 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001760 }
1761}
1762
Damien Millerefb4afe2000-04-12 18:45:05 +10001763int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001764session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001765{
1766 Session *s = session_new();
1767 debug("session_open: channel %d", chanid);
1768 if (s == NULL) {
1769 error("no more sessions");
1770 return 0;
1771 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001772 s->authctxt = authctxt;
1773 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001774 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001775 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001776 debug("session_open: session %d: link with channel %d", s->self, chanid);
1777 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001778 return 1;
1779}
1780
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781Session *
1782session_by_tty(char *tty)
1783{
1784 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001785 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001786 Session *s = &sessions[i];
1787 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1788 debug("session_by_tty: session %d tty %s", i, tty);
1789 return s;
1790 }
1791 }
1792 debug("session_by_tty: unknown tty %.100s", tty);
1793 session_dump();
1794 return NULL;
1795}
1796
Ben Lindstrombba81212001-06-25 05:01:22 +00001797static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001798session_by_channel(int id)
1799{
1800 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001801 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001802 Session *s = &sessions[i];
1803 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001804 debug("session_by_channel: session %d channel %d",
1805 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001806 return s;
1807 }
1808 }
1809 debug("session_by_channel: unknown channel %d", id);
1810 session_dump();
1811 return NULL;
1812}
1813
Ben Lindstrombba81212001-06-25 05:01:22 +00001814static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001815session_by_x11_channel(int id)
1816{
1817 int i, j;
1818
Damien Miller7207f642008-05-19 15:34:50 +10001819 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001820 Session *s = &sessions[i];
1821
1822 if (s->x11_chanids == NULL || !s->used)
1823 continue;
1824 for (j = 0; s->x11_chanids[j] != -1; j++) {
1825 if (s->x11_chanids[j] == id) {
1826 debug("session_by_x11_channel: session %d "
1827 "channel %d", s->self, id);
1828 return s;
1829 }
1830 }
1831 }
1832 debug("session_by_x11_channel: unknown channel %d", id);
1833 session_dump();
1834 return NULL;
1835}
1836
1837static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001838session_by_pid(pid_t pid)
1839{
1840 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001841 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001842 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001843 Session *s = &sessions[i];
1844 if (s->used && s->pid == pid)
1845 return s;
1846 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001847 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001848 session_dump();
1849 return NULL;
1850}
1851
Ben Lindstrombba81212001-06-25 05:01:22 +00001852static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001853session_window_change_req(Session *s)
1854{
1855 s->col = packet_get_int();
1856 s->row = packet_get_int();
1857 s->xpixel = packet_get_int();
1858 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001859 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001860 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1861 return 1;
1862}
1863
Ben Lindstrombba81212001-06-25 05:01:22 +00001864static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001865session_pty_req(Session *s)
1866{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001867 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001868 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001869
Damien Miller5ff30c62013-10-30 22:21:50 +11001870 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001871 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001872 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001873 }
1874 if (s->ttyfd != -1) {
1875 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001876 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001877 }
1878
Damien Millerefb4afe2000-04-12 18:45:05 +10001879 s->term = packet_get_string(&len);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001880 s->col = packet_get_int();
1881 s->row = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +10001882 s->xpixel = packet_get_int();
1883 s->ypixel = packet_get_int();
1884
1885 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001886 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001887 s->term = NULL;
1888 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001889
Damien Millerefb4afe2000-04-12 18:45:05 +10001890 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001891 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10001892 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1893 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10001894 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001895 s->term = NULL;
1896 s->ptyfd = -1;
1897 s->ttyfd = -1;
1898 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001899 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001900 }
1901 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001902
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001903 n_bytes = packet_remaining();
Ben Lindstrom49c12602001-06-13 04:37:36 +00001904 tty_parse_modes(s->ttyfd, &n_bytes);
1905
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001906 if (!use_privsep)
1907 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001908
1909 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001910 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1911
Damien Miller48b03fc2002-01-22 23:11:40 +11001912 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001913 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001914 return 1;
1915}
1916
Ben Lindstrombba81212001-06-25 05:01:22 +00001917static int
Damien Millerbd483e72000-04-30 10:00:53 +10001918session_subsystem_req(Session *s)
1919{
Damien Millerae452462001-10-10 15:08:06 +10001920 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001921 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001922 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11001923 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10001924 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001925
Damien Miller71df7522013-10-15 12:12:02 +11001926 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001927 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11001928 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10001929 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10001930
Damien Millerf6d9e222000-06-18 14:50:44 +10001931 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11001932 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001933 prog = options.subsystem_command[i];
1934 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001935 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11001936 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001937 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11001938 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001939 if (stat(prog, &st) < 0)
1940 debug("subsystem: cannot stat %s: %s",
1941 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001942 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001943 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001944 }
Damien Miller7207f642008-05-19 15:34:50 +10001945 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11001946 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001947 }
1948 }
1949
1950 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11001951 logit("subsystem request for %.100s by user %s failed, "
1952 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10001953
Damien Millerbd483e72000-04-30 10:00:53 +10001954 return success;
1955}
1956
Ben Lindstrombba81212001-06-25 05:01:22 +00001957static int
Damien Millerbd483e72000-04-30 10:00:53 +10001958session_x11_req(Session *s)
1959{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001960 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001961
Damien Miller2b9b0452005-07-17 17:19:24 +10001962 if (s->auth_proto != NULL || s->auth_data != NULL) {
1963 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11001964 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10001965 return 0;
1966 }
Damien Millerbd483e72000-04-30 10:00:53 +10001967 s->single_connection = packet_get_char();
1968 s->auth_proto = packet_get_string(NULL);
1969 s->auth_data = packet_get_string(NULL);
1970 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001971 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001972
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00001973 if (xauth_valid_string(s->auth_proto) &&
1974 xauth_valid_string(s->auth_data))
1975 success = session_setup_x11fwd(s);
1976 else {
1977 success = 0;
1978 error("Invalid X11 forwarding data");
1979 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001980 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10001981 free(s->auth_proto);
1982 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001983 s->auth_proto = NULL;
1984 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001985 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001986 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001987}
1988
Ben Lindstrombba81212001-06-25 05:01:22 +00001989static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001990session_shell_req(Session *s)
1991{
Damien Miller48b03fc2002-01-22 23:11:40 +11001992 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10001993 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10001994}
1995
Ben Lindstrombba81212001-06-25 05:01:22 +00001996static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001997session_exec_req(Session *s)
1998{
Damien Miller7207f642008-05-19 15:34:50 +10001999 u_int len, success;
2000
Damien Millerf6d9e222000-06-18 14:50:44 +10002001 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002002 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002003 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002004 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002005 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002006}
2007
Ben Lindstrombba81212001-06-25 05:01:22 +00002008static int
Damien Miller54c45982003-05-15 10:20:13 +10002009session_break_req(Session *s)
2010{
Damien Miller54c45982003-05-15 10:20:13 +10002011
Darren Tucker1f8311c2004-05-13 16:39:33 +10002012 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002013 packet_check_eom();
2014
Darren Tucker9c5d5532011-11-04 10:55:24 +11002015 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002016 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002017 return 1;
2018}
2019
2020static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002021session_env_req(Session *s)
2022{
2023 char *name, *val;
2024 u_int name_len, val_len, i;
2025
Damien Miller8569eba2014-03-04 09:35:17 +11002026 name = packet_get_cstring(&name_len);
2027 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002028 packet_check_eom();
2029
2030 /* Don't set too many environment variables */
2031 if (s->num_env > 128) {
2032 debug2("Ignoring env request %s: too many env vars", name);
2033 goto fail;
2034 }
2035
2036 for (i = 0; i < options.num_accept_env; i++) {
2037 if (match_pattern(name, options.accept_env[i])) {
2038 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002039 s->env = xrecallocarray(s->env, s->num_env,
2040 s->num_env + 1, sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002041 s->env[s->num_env].name = name;
2042 s->env[s->num_env].val = val;
2043 s->num_env++;
2044 return (1);
2045 }
2046 }
2047 debug2("Ignoring env request %s: disallowed name", name);
2048
2049 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002050 free(name);
2051 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002052 return (0);
2053}
2054
2055static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002056session_auth_agent_req(Session *s)
2057{
2058 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002059 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002060 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002061 debug("session_auth_agent_req: no_agent_forwarding_flag");
2062 return 0;
2063 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002064 if (called) {
2065 return 0;
2066 } else {
2067 called = 1;
2068 return auth_input_request_forwarding(s->pw);
2069 }
2070}
2071
Damien Millerc7ef63d2002-02-05 12:21:42 +11002072int
2073session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002074{
Damien Millerefb4afe2000-04-12 18:45:05 +10002075 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002076 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002077
Damien Millerc7ef63d2002-02-05 12:21:42 +11002078 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002079 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002080 c->self, rtype);
2081 return 0;
2082 }
2083 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002084
2085 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002086 * a session is in LARVAL state until a shell, a command
2087 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002088 */
2089 if (c->type == SSH_CHANNEL_LARVAL) {
2090 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002091 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002092 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002093 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002094 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002095 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002096 } else if (strcmp(rtype, "x11-req") == 0) {
2097 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002098 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2099 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002100 } else if (strcmp(rtype, "subsystem") == 0) {
2101 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002102 } else if (strcmp(rtype, "env") == 0) {
2103 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002104 }
2105 }
2106 if (strcmp(rtype, "window-change") == 0) {
2107 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002108 } else if (strcmp(rtype, "break") == 0) {
2109 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002110 }
Damien Millera6b1d162004-06-30 22:41:07 +10002111
Damien Millerc7ef63d2002-02-05 12:21:42 +11002112 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002113}
2114
2115void
Damien Miller8853ca52010-06-26 10:00:14 +10002116session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2117 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002118{
Damien Millerefb4afe2000-04-12 18:45:05 +10002119 /*
2120 * now that have a child and a pipe to the child,
2121 * we can activate our channel and register the fd's
2122 */
2123 if (s->chanid == -1)
2124 fatal("no channel for session %d", s->self);
2125 channel_set_fds(s->chanid,
2126 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002127 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002128 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002129}
2130
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002131/*
2132 * Function to perform pty cleanup. Also called if we get aborted abnormally
2133 * (e.g., due to a dropped connection).
2134 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002135void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002136session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002137{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002138 if (s == NULL) {
2139 error("session_pty_cleanup: no session");
2140 return;
2141 }
2142 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002143 return;
2144
Kevin Steves43cdef32001-02-11 14:12:08 +00002145 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002146
Damien Millerb38eff82000-04-01 11:09:21 +10002147 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002148 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002149 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002150
2151 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002152 if (getuid() == 0)
2153 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002154
2155 /*
2156 * Close the server side of the socket pairs. We must do this after
2157 * the pty cleanup, so that another process doesn't get this pty
2158 * while we're still cleaning up.
2159 */
Damien Miller7207f642008-05-19 15:34:50 +10002160 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2161 error("close(s->ptymaster/%d): %s",
2162 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002163
2164 /* unlink pty from session */
2165 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002166}
Damien Millerefb4afe2000-04-12 18:45:05 +10002167
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002168void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002169session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002170{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002171 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002172}
2173
Damien Miller5a80bba2002-09-04 16:39:02 +10002174static char *
2175sig2name(int sig)
2176{
2177#define SSH_SIG(x) if (sig == SIG ## x) return #x
2178 SSH_SIG(ABRT);
2179 SSH_SIG(ALRM);
2180 SSH_SIG(FPE);
2181 SSH_SIG(HUP);
2182 SSH_SIG(ILL);
2183 SSH_SIG(INT);
2184 SSH_SIG(KILL);
2185 SSH_SIG(PIPE);
2186 SSH_SIG(QUIT);
2187 SSH_SIG(SEGV);
2188 SSH_SIG(TERM);
2189 SSH_SIG(USR1);
2190 SSH_SIG(USR2);
2191#undef SSH_SIG
2192 return "SIG@openssh.com";
2193}
2194
Ben Lindstrombba81212001-06-25 05:01:22 +00002195static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002196session_close_x11(int id)
2197{
2198 Channel *c;
2199
Damien Millerd47c62a2005-12-13 19:33:57 +11002200 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002201 debug("session_close_x11: x11 channel %d missing", id);
2202 } else {
2203 /* Detach X11 listener */
2204 debug("session_close_x11: detach x11 channel %d", id);
2205 channel_cancel_cleanup(id);
2206 if (c->ostate != CHAN_OUTPUT_CLOSED)
2207 chan_mark_dead(c);
2208 }
2209}
2210
2211static void
2212session_close_single_x11(int id, void *arg)
2213{
2214 Session *s;
2215 u_int i;
2216
2217 debug3("session_close_single_x11: channel %d", id);
2218 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002219 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002220 fatal("session_close_single_x11: no x11 channel %d", id);
2221 for (i = 0; s->x11_chanids[i] != -1; i++) {
2222 debug("session_close_single_x11: session %d: "
2223 "closing channel %d", s->self, s->x11_chanids[i]);
2224 /*
2225 * The channel "id" is already closing, but make sure we
2226 * close all of its siblings.
2227 */
2228 if (s->x11_chanids[i] != id)
2229 session_close_x11(s->x11_chanids[i]);
2230 }
Darren Tuckera627d422013-06-02 07:31:17 +10002231 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002232 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002233 free(s->display);
2234 s->display = NULL;
2235 free(s->auth_proto);
2236 s->auth_proto = NULL;
2237 free(s->auth_data);
2238 s->auth_data = NULL;
2239 free(s->auth_display);
2240 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002241}
2242
2243static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002244session_exit_message(Session *s, int status)
2245{
2246 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002247
2248 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002249 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002250 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002251 debug("session_exit_message: session %d channel %d pid %ld",
2252 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002253
2254 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002255 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002256 packet_put_int(WEXITSTATUS(status));
2257 packet_send();
2258 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002259 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002260 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002261#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002262 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002263#else /* WCOREDUMP */
2264 packet_put_char(0);
2265#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002266 packet_put_cstring("");
2267 packet_put_cstring("");
2268 packet_send();
2269 } else {
2270 /* Some weird exit cause. Just exit. */
2271 packet_disconnect("wait returned status %04x.", status);
2272 }
2273
2274 /* disconnect channel */
2275 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002276
2277 /*
2278 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002279 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002280 * by session_close_by_channel when the childs close their fds.
2281 */
2282 channel_register_cleanup(c->self, session_close_by_channel, 1);
2283
Damien Miller166fca82000-04-20 07:42:21 +10002284 /*
2285 * emulate a write failure with 'chan_write_failed', nobody will be
2286 * interested in data we write.
2287 * Note that we must not call 'chan_read_failed', since there could
2288 * be some more data waiting in the pipe.
2289 */
Damien Millerbd483e72000-04-30 10:00:53 +10002290 if (c->ostate != CHAN_OUTPUT_CLOSED)
2291 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002292}
2293
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002294void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002295session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002296{
djm@openbsd.org95767262016-03-07 19:02:43 +00002297 struct ssh *ssh = active_state; /* XXX */
Damien Millereccb9de2005-06-17 12:59:34 +10002298 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002299
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002300 verbose("Close session: user %s from %.200s port %d id %d",
2301 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002302 ssh_remote_ipaddr(ssh),
2303 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002304 s->self);
2305
Darren Tucker3e33cec2003-10-02 16:12:36 +10002306 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002307 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002308 free(s->term);
2309 free(s->display);
2310 free(s->x11_chanids);
2311 free(s->auth_display);
2312 free(s->auth_data);
2313 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002314 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002315 if (s->env != NULL) {
2316 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002317 free(s->env[i].name);
2318 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002319 }
Darren Tuckera627d422013-06-02 07:31:17 +10002320 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002321 }
Damien Millere247cc42000-05-07 12:03:14 +10002322 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002323 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002324}
2325
2326void
2327session_close_by_pid(pid_t pid, int status)
2328{
2329 Session *s = session_by_pid(pid);
2330 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002331 debug("session_close_by_pid: no session for pid %ld",
2332 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002333 return;
2334 }
2335 if (s->chanid != -1)
2336 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002337 if (s->ttyfd != -1)
2338 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002339 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002340}
2341
2342/*
2343 * this is called when a channel dies before
2344 * the session 'child' itself dies
2345 */
2346void
2347session_close_by_channel(int id, void *arg)
2348{
2349 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002350 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002351
Damien Millerefb4afe2000-04-12 18:45:05 +10002352 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002353 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002354 return;
2355 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002356 debug("session_close_by_channel: channel %d child %ld",
2357 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002358 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002359 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002360 /*
2361 * delay detach of session, but release pty, since
2362 * the fd's to the child are already closed
2363 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002364 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002365 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002366 return;
2367 }
2368 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002369 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002370
2371 /* Close any X11 listeners associated with this session */
2372 if (s->x11_chanids != NULL) {
2373 for (i = 0; s->x11_chanids[i] != -1; i++) {
2374 session_close_x11(s->x11_chanids[i]);
2375 s->x11_chanids[i] = -1;
2376 }
2377 }
2378
Damien Millerefb4afe2000-04-12 18:45:05 +10002379 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002380 session_close(s);
2381}
2382
2383void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002384session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002385{
2386 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002387 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002388 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002389 if (s->used) {
2390 if (closefunc != NULL)
2391 closefunc(s);
2392 else
2393 session_close(s);
2394 }
Damien Miller52b77be2001-10-10 15:14:37 +10002395 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002396}
2397
Ben Lindstrombba81212001-06-25 05:01:22 +00002398static char *
Damien Millere247cc42000-05-07 12:03:14 +10002399session_tty_list(void)
2400{
2401 static char buf[1024];
2402 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002403 char *cp;
2404
Damien Millere247cc42000-05-07 12:03:14 +10002405 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002406 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002407 Session *s = &sessions[i];
2408 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002409
Damien Millera8ed44b2003-01-10 09:53:12 +11002410 if (strncmp(s->tty, "/dev/", 5) != 0) {
2411 cp = strrchr(s->tty, '/');
2412 cp = (cp == NULL) ? s->tty : cp + 1;
2413 } else
2414 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002415
Damien Millere247cc42000-05-07 12:03:14 +10002416 if (buf[0] != '\0')
2417 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002418 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002419 }
2420 }
2421 if (buf[0] == '\0')
2422 strlcpy(buf, "notty", sizeof buf);
2423 return buf;
2424}
2425
2426void
2427session_proctitle(Session *s)
2428{
2429 if (s->pw == NULL)
2430 error("no user for session %d", s->self);
2431 else
2432 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2433}
2434
Ben Lindstrom768176b2001-06-09 01:29:12 +00002435int
2436session_setup_x11fwd(Session *s)
2437{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002438 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002439 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002440 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002441 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002442
2443 if (no_x11_forwarding_flag) {
2444 packet_send_debug("X11 forwarding disabled in user configuration file.");
2445 return 0;
2446 }
2447 if (!options.x11_forwarding) {
2448 debug("X11 forwarding disabled in server configuration file.");
2449 return 0;
2450 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002451 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002452 (stat(options.xauth_location, &st) == -1)) {
2453 packet_send_debug("No xauth program; cannot forward with spoofing.");
2454 return 0;
2455 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002456 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002457 debug("X11 display already set.");
2458 return 0;
2459 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002460 if (x11_create_display_inet(options.x11_display_offset,
2461 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002462 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002463 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002464 return 0;
2465 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002466 for (i = 0; s->x11_chanids[i] != -1; i++) {
2467 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002468 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002469 }
Kevin Steves366298c2001-12-19 17:58:01 +00002470
2471 /* Set up a suitable value for the DISPLAY variable. */
2472 if (gethostname(hostname, sizeof(hostname)) < 0)
2473 fatal("gethostname: %.100s", strerror(errno));
2474 /*
2475 * auth_display must be used as the displayname when the
2476 * authorization entry is added with xauth(1). This will be
2477 * different than the DISPLAY string for localhost displays.
2478 */
Damien Miller95c249f2002-02-05 12:11:34 +11002479 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002480 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002481 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002482 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002483 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002484 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002485 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002486 } else {
2487#ifdef IPADDR_IN_DISPLAY
2488 struct hostent *he;
2489 struct in_addr my_addr;
2490
2491 he = gethostbyname(hostname);
2492 if (he == NULL) {
2493 error("Can't get IP address for X11 DISPLAY.");
2494 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2495 return 0;
2496 }
2497 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002498 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002499 s->display_number, s->screen);
2500#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002501 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002502 s->display_number, s->screen);
2503#endif
2504 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002505 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002506 }
2507
Ben Lindstrom768176b2001-06-09 01:29:12 +00002508 return 1;
2509}
2510
Ben Lindstrombba81212001-06-25 05:01:22 +00002511static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002512do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002513{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002514 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002515}
2516
2517void
2518do_cleanup(Authctxt *authctxt)
2519{
2520 static int called = 0;
2521
2522 debug("do_cleanup");
2523
2524 /* no cleanup if we're in the child for login shell */
2525 if (is_child)
2526 return;
2527
2528 /* avoid double cleanup */
2529 if (called)
2530 return;
2531 called = 1;
2532
Darren Tucker9142e1c2007-08-16 23:28:04 +10002533 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002534 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002535
2536#ifdef USE_PAM
2537 if (options.use_pam) {
2538 sshpam_cleanup();
2539 sshpam_thread_cleanup();
2540 }
2541#endif
2542
2543 if (!authctxt->authenticated)
2544 return;
2545
Darren Tucker3e33cec2003-10-02 16:12:36 +10002546#ifdef KRB5
2547 if (options.kerberos_ticket_cleanup &&
2548 authctxt->krb5_ctx)
2549 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002550#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002551
2552#ifdef GSSAPI
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002553 if (options.gss_cleanup_creds)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002554 ssh_gssapi_cleanup_creds();
2555#endif
2556
2557 /* remove agent socket */
2558 auth_sock_cleanup_proc(authctxt->pw);
2559
2560 /*
2561 * Cleanup ptys/utmp only if privsep is disabled,
2562 * or if running in monitor.
2563 */
2564 if (!use_privsep || mm_is_monitor())
2565 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002566}
djm@openbsd.org95767262016-03-07 19:02:43 +00002567
2568/* Return a name for the remote host that fits inside utmp_size */
2569
2570const char *
2571session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2572{
2573 const char *remote = "";
2574
2575 if (utmp_size > 0)
2576 remote = auth_get_canonical_hostname(ssh, use_dns);
2577 if (utmp_size == 0 || strlen(remote) > utmp_size)
2578 remote = ssh_remote_ipaddr(ssh);
2579 return remote;
2580}
2581