blob: a08aa69d1679bf5b8278a055ac870da8897fb888 [file] [log] [blame]
djm@openbsd.org7844f352016-11-30 03:00:05 +00001/* $OpenBSD: session.c,v 1.286 2016/11/30 03:00:05 djm Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
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();
1489
Darren Tucker23bc8d02004-02-06 16:24:31 +11001490 /* Force a password change */
1491 if (s->authctxt->force_pwchange) {
1492 do_setusercontext(pw);
1493 child_close_fds();
1494 do_pwchange(s);
1495 exit(1);
1496 }
1497
Tim Rice81ed5182002-09-25 17:38:46 -07001498#ifdef _UNICOS
1499 cray_setup(pw->pw_uid, pw->pw_name, command);
1500#endif /* _UNICOS */
1501
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001502 /*
1503 * Login(1) does this as well, and it needs uid 0 for the "-h"
1504 * switch, so we let login(1) to this for us.
1505 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001506#ifdef HAVE_OSF_SIA
djm@openbsd.org83b58182016-08-19 03:18:06 +00001507 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1508 if (!check_quietlogin(s, command))
1509 do_motd();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001510#else /* HAVE_OSF_SIA */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001511 /* When PAM is enabled we rely on it to do the nologin check */
1512 if (!options.use_pam)
1513 do_nologin(pw);
1514 do_setusercontext(pw);
1515 /*
1516 * PAM session modules in do_setusercontext may have
1517 * generated messages, so if this in an interactive
1518 * login then display them too.
1519 */
1520 if (!check_quietlogin(s, command))
1521 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001522#endif /* HAVE_OSF_SIA */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001523
Darren Tucker69687f42004-09-11 22:17:26 +10001524#ifdef USE_PAM
djm@openbsd.org83b58182016-08-19 03:18:06 +00001525 if (options.use_pam && !is_pam_session_open()) {
Darren Tucker48554152005-04-21 19:50:55 +10001526 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001527 display_loginmsg();
1528 exit(254);
1529 }
1530#endif
1531
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001532 /*
1533 * Get the shell from the password data. An empty shell field is
1534 * legal, and means /bin/sh.
1535 */
1536 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001537
1538 /*
1539 * Make sure $SHELL points to the shell from the password file,
1540 * even if shell is overridden from login.conf
1541 */
1542 env = do_setup_env(s, shell);
1543
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001544#ifdef HAVE_LOGIN_CAP
1545 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1546#endif
1547
Damien Millerb38eff82000-04-01 11:09:21 +10001548 /*
1549 * Close the connection descriptors; note that this is the child, and
1550 * the server will still have the socket open, and it is important
1551 * that we do not shutdown it. Note that the descriptors cannot be
1552 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001553 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001554 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001555 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001556
Damien Millerb38eff82000-04-01 11:09:21 +10001557 /*
Damien Miller05eda432002-02-10 18:32:28 +11001558 * Must take new environment into use so that .ssh/rc,
1559 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001560 */
1561 environ = env;
1562
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001563#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001564 /*
1565 * At this point, we check to see if AFS is active and if we have
1566 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1567 * if we can (and need to) extend the ticket into an AFS token. If
1568 * we don't do this, we run into potential problems if the user's
1569 * home directory is in AFS and it's not world-readable.
1570 */
1571
1572 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001573 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001574 char cell[64];
1575
1576 debug("Getting AFS token");
1577
1578 k_setpag();
1579
1580 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1581 krb5_afslog(s->authctxt->krb5_ctx,
1582 s->authctxt->krb5_fwd_ccache, cell, NULL);
1583
1584 krb5_afslog_home(s->authctxt->krb5_ctx,
1585 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1586 }
1587#endif
1588
Damien Miller788f2122005-11-05 15:14:59 +11001589 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001590 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001591 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001592#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001593 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001594#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001595 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001596 fprintf(stderr, "Could not chdir to home "
1597 "directory %s: %s\n", pw->pw_dir,
1598 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001599 }
Damien Miller6051c942008-06-16 07:53:16 +10001600 if (r)
1601 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001602 }
1603
Damien Millera1939002008-03-15 17:27:58 +11001604 closefrom(STDERR_FILENO + 1);
1605
djm@openbsd.org83b58182016-08-19 03:18:06 +00001606 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001607
1608 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001609 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001610
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001611 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1612 printf("This service allows sftp connections only.\n");
1613 fflush(NULL);
1614 exit(1);
1615 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001616 extern int optind, optreset;
1617 int i;
1618 char *p, *args;
1619
Darren Tuckerac46a912009-06-21 17:55:23 +10001620 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001621 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001622 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1623 if (i < ARGV_MAX - 1)
1624 argv[i++] = p;
1625 argv[i] = NULL;
1626 optind = optreset = 1;
1627 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001628#ifdef WITH_SELINUX
1629 ssh_selinux_change_context("sftpd_t");
1630#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001631 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001632 }
1633
Darren Tucker695ed392009-10-07 09:02:18 +11001634 fflush(NULL);
1635
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001636 /* Get the last component of the shell name. */
1637 if ((shell0 = strrchr(shell, '/')) != NULL)
1638 shell0++;
1639 else
1640 shell0 = shell;
1641
Damien Millerb38eff82000-04-01 11:09:21 +10001642 /*
1643 * If we have no command, execute the shell. In this case, the shell
1644 * name to be passed in argv[0] is preceded by '-' to indicate that
1645 * this is a login shell.
1646 */
1647 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001648 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001649
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001650 /* Start the shell. Set initial character to '-'. */
1651 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001652
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001653 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1654 >= sizeof(argv0) - 1) {
1655 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001656 perror(shell);
1657 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001658 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001659
1660 /* Execute the shell. */
1661 argv[0] = argv0;
1662 argv[1] = NULL;
1663 execve(shell, argv, env);
1664
1665 /* Executing the shell failed. */
1666 perror(shell);
1667 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001668 }
1669 /*
1670 * Execute the command using the user's shell. This uses the -c
1671 * option to execute the command.
1672 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001673 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001674 argv[1] = "-c";
1675 argv[2] = (char *) command;
1676 argv[3] = NULL;
1677 execve(shell, argv, env);
1678 perror(shell);
1679 exit(1);
1680}
1681
Damien Miller7207f642008-05-19 15:34:50 +10001682void
1683session_unused(int id)
1684{
1685 debug3("%s: session id %d unused", __func__, id);
1686 if (id >= options.max_sessions ||
1687 id >= sessions_nalloc) {
1688 fatal("%s: insane session id %d (max %d nalloc %d)",
1689 __func__, id, options.max_sessions, sessions_nalloc);
1690 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001691 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001692 sessions[id].self = id;
1693 sessions[id].used = 0;
1694 sessions[id].chanid = -1;
1695 sessions[id].ptyfd = -1;
1696 sessions[id].ttyfd = -1;
1697 sessions[id].ptymaster = -1;
1698 sessions[id].x11_chanids = NULL;
1699 sessions[id].next_unused = sessions_first_unused;
1700 sessions_first_unused = id;
1701}
1702
Damien Millerb38eff82000-04-01 11:09:21 +10001703Session *
1704session_new(void)
1705{
Damien Miller7207f642008-05-19 15:34:50 +10001706 Session *s, *tmp;
1707
1708 if (sessions_first_unused == -1) {
1709 if (sessions_nalloc >= options.max_sessions)
1710 return NULL;
1711 debug2("%s: allocate (allocated %d max %d)",
1712 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001713 tmp = xreallocarray(sessions, sessions_nalloc + 1,
Damien Miller7207f642008-05-19 15:34:50 +10001714 sizeof(*sessions));
1715 if (tmp == NULL) {
1716 error("%s: cannot allocate %d sessions",
1717 __func__, sessions_nalloc + 1);
1718 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001719 }
Damien Miller7207f642008-05-19 15:34:50 +10001720 sessions = tmp;
1721 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001722 }
Damien Miller7207f642008-05-19 15:34:50 +10001723
1724 if (sessions_first_unused >= sessions_nalloc ||
1725 sessions_first_unused < 0) {
1726 fatal("%s: insane first_unused %d max %d nalloc %d",
1727 __func__, sessions_first_unused, options.max_sessions,
1728 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001729 }
Damien Miller7207f642008-05-19 15:34:50 +10001730
1731 s = &sessions[sessions_first_unused];
1732 if (s->used) {
1733 fatal("%s: session %d already used",
1734 __func__, sessions_first_unused);
1735 }
1736 sessions_first_unused = s->next_unused;
1737 s->used = 1;
1738 s->next_unused = -1;
1739 debug("session_new: session %d", s->self);
1740
1741 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001742}
1743
Ben Lindstrombba81212001-06-25 05:01:22 +00001744static void
Damien Millerb38eff82000-04-01 11:09:21 +10001745session_dump(void)
1746{
1747 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001748 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001749 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001750
1751 debug("dump: used %d next_unused %d session %d %p "
1752 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001753 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001754 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001755 s->self,
1756 s,
1757 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001758 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001759 }
1760}
1761
Damien Millerefb4afe2000-04-12 18:45:05 +10001762int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001763session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001764{
1765 Session *s = session_new();
1766 debug("session_open: channel %d", chanid);
1767 if (s == NULL) {
1768 error("no more sessions");
1769 return 0;
1770 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001771 s->authctxt = authctxt;
1772 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001773 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001774 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001775 debug("session_open: session %d: link with channel %d", s->self, chanid);
1776 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001777 return 1;
1778}
1779
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001780Session *
1781session_by_tty(char *tty)
1782{
1783 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001784 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001785 Session *s = &sessions[i];
1786 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1787 debug("session_by_tty: session %d tty %s", i, tty);
1788 return s;
1789 }
1790 }
1791 debug("session_by_tty: unknown tty %.100s", tty);
1792 session_dump();
1793 return NULL;
1794}
1795
Ben Lindstrombba81212001-06-25 05:01:22 +00001796static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001797session_by_channel(int id)
1798{
1799 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001800 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001801 Session *s = &sessions[i];
1802 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001803 debug("session_by_channel: session %d channel %d",
1804 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001805 return s;
1806 }
1807 }
1808 debug("session_by_channel: unknown channel %d", id);
1809 session_dump();
1810 return NULL;
1811}
1812
Ben Lindstrombba81212001-06-25 05:01:22 +00001813static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001814session_by_x11_channel(int id)
1815{
1816 int i, j;
1817
Damien Miller7207f642008-05-19 15:34:50 +10001818 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001819 Session *s = &sessions[i];
1820
1821 if (s->x11_chanids == NULL || !s->used)
1822 continue;
1823 for (j = 0; s->x11_chanids[j] != -1; j++) {
1824 if (s->x11_chanids[j] == id) {
1825 debug("session_by_x11_channel: session %d "
1826 "channel %d", s->self, id);
1827 return s;
1828 }
1829 }
1830 }
1831 debug("session_by_x11_channel: unknown channel %d", id);
1832 session_dump();
1833 return NULL;
1834}
1835
1836static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001837session_by_pid(pid_t pid)
1838{
1839 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001840 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001841 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001842 Session *s = &sessions[i];
1843 if (s->used && s->pid == pid)
1844 return s;
1845 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001846 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001847 session_dump();
1848 return NULL;
1849}
1850
Ben Lindstrombba81212001-06-25 05:01:22 +00001851static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001852session_window_change_req(Session *s)
1853{
1854 s->col = packet_get_int();
1855 s->row = packet_get_int();
1856 s->xpixel = packet_get_int();
1857 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001858 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001859 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1860 return 1;
1861}
1862
Ben Lindstrombba81212001-06-25 05:01:22 +00001863static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001864session_pty_req(Session *s)
1865{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001866 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001867 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001868
Damien Miller5ff30c62013-10-30 22:21:50 +11001869 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001870 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001871 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001872 }
1873 if (s->ttyfd != -1) {
1874 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001875 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001876 }
1877
Damien Millerefb4afe2000-04-12 18:45:05 +10001878 s->term = packet_get_string(&len);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001879 s->col = packet_get_int();
1880 s->row = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +10001881 s->xpixel = packet_get_int();
1882 s->ypixel = packet_get_int();
1883
1884 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001885 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001886 s->term = NULL;
1887 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001888
Damien Millerefb4afe2000-04-12 18:45:05 +10001889 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001890 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10001891 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1892 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10001893 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001894 s->term = NULL;
1895 s->ptyfd = -1;
1896 s->ttyfd = -1;
1897 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001898 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001899 }
1900 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001901
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001902 n_bytes = packet_remaining();
Ben Lindstrom49c12602001-06-13 04:37:36 +00001903 tty_parse_modes(s->ttyfd, &n_bytes);
1904
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001905 if (!use_privsep)
1906 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001907
1908 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001909 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1910
Damien Miller48b03fc2002-01-22 23:11:40 +11001911 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001912 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001913 return 1;
1914}
1915
Ben Lindstrombba81212001-06-25 05:01:22 +00001916static int
Damien Millerbd483e72000-04-30 10:00:53 +10001917session_subsystem_req(Session *s)
1918{
Damien Millerae452462001-10-10 15:08:06 +10001919 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001920 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001921 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11001922 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10001923 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001924
Damien Miller71df7522013-10-15 12:12:02 +11001925 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001926 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11001927 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10001928 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10001929
Damien Millerf6d9e222000-06-18 14:50:44 +10001930 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11001931 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001932 prog = options.subsystem_command[i];
1933 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001934 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11001935 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001936 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11001937 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001938 if (stat(prog, &st) < 0)
1939 debug("subsystem: cannot stat %s: %s",
1940 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001941 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001942 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001943 }
Damien Miller7207f642008-05-19 15:34:50 +10001944 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11001945 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001946 }
1947 }
1948
1949 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11001950 logit("subsystem request for %.100s by user %s failed, "
1951 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10001952
Damien Millerbd483e72000-04-30 10:00:53 +10001953 return success;
1954}
1955
Ben Lindstrombba81212001-06-25 05:01:22 +00001956static int
Damien Millerbd483e72000-04-30 10:00:53 +10001957session_x11_req(Session *s)
1958{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001959 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001960
Damien Miller2b9b0452005-07-17 17:19:24 +10001961 if (s->auth_proto != NULL || s->auth_data != NULL) {
1962 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11001963 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10001964 return 0;
1965 }
Damien Millerbd483e72000-04-30 10:00:53 +10001966 s->single_connection = packet_get_char();
1967 s->auth_proto = packet_get_string(NULL);
1968 s->auth_data = packet_get_string(NULL);
1969 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001970 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001971
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00001972 if (xauth_valid_string(s->auth_proto) &&
1973 xauth_valid_string(s->auth_data))
1974 success = session_setup_x11fwd(s);
1975 else {
1976 success = 0;
1977 error("Invalid X11 forwarding data");
1978 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001979 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10001980 free(s->auth_proto);
1981 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001982 s->auth_proto = NULL;
1983 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001984 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001985 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001986}
1987
Ben Lindstrombba81212001-06-25 05:01:22 +00001988static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001989session_shell_req(Session *s)
1990{
Damien Miller48b03fc2002-01-22 23:11:40 +11001991 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10001992 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10001993}
1994
Ben Lindstrombba81212001-06-25 05:01:22 +00001995static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001996session_exec_req(Session *s)
1997{
Damien Miller7207f642008-05-19 15:34:50 +10001998 u_int len, success;
1999
Damien Millerf6d9e222000-06-18 14:50:44 +10002000 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002001 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002002 success = do_exec(s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002003 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002004 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002005}
2006
Ben Lindstrombba81212001-06-25 05:01:22 +00002007static int
Damien Miller54c45982003-05-15 10:20:13 +10002008session_break_req(Session *s)
2009{
Damien Miller54c45982003-05-15 10:20:13 +10002010
Darren Tucker1f8311c2004-05-13 16:39:33 +10002011 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002012 packet_check_eom();
2013
Darren Tucker9c5d5532011-11-04 10:55:24 +11002014 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002015 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002016 return 1;
2017}
2018
2019static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002020session_env_req(Session *s)
2021{
2022 char *name, *val;
2023 u_int name_len, val_len, i;
2024
Damien Miller8569eba2014-03-04 09:35:17 +11002025 name = packet_get_cstring(&name_len);
2026 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002027 packet_check_eom();
2028
2029 /* Don't set too many environment variables */
2030 if (s->num_env > 128) {
2031 debug2("Ignoring env request %s: too many env vars", name);
2032 goto fail;
2033 }
2034
2035 for (i = 0; i < options.num_accept_env; i++) {
2036 if (match_pattern(name, options.accept_env[i])) {
2037 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002038 s->env = xreallocarray(s->env, s->num_env + 1,
Damien Miller36812092006-03-26 14:22:47 +11002039 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002040 s->env[s->num_env].name = name;
2041 s->env[s->num_env].val = val;
2042 s->num_env++;
2043 return (1);
2044 }
2045 }
2046 debug2("Ignoring env request %s: disallowed name", name);
2047
2048 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002049 free(name);
2050 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002051 return (0);
2052}
2053
2054static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002055session_auth_agent_req(Session *s)
2056{
2057 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002058 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002059 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002060 debug("session_auth_agent_req: no_agent_forwarding_flag");
2061 return 0;
2062 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002063 if (called) {
2064 return 0;
2065 } else {
2066 called = 1;
2067 return auth_input_request_forwarding(s->pw);
2068 }
2069}
2070
Damien Millerc7ef63d2002-02-05 12:21:42 +11002071int
2072session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002073{
Damien Millerefb4afe2000-04-12 18:45:05 +10002074 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002075 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002076
Damien Millerc7ef63d2002-02-05 12:21:42 +11002077 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002078 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002079 c->self, rtype);
2080 return 0;
2081 }
2082 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002083
2084 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002085 * a session is in LARVAL state until a shell, a command
2086 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002087 */
2088 if (c->type == SSH_CHANNEL_LARVAL) {
2089 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002090 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002091 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002092 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002093 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002094 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002095 } else if (strcmp(rtype, "x11-req") == 0) {
2096 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002097 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2098 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002099 } else if (strcmp(rtype, "subsystem") == 0) {
2100 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002101 } else if (strcmp(rtype, "env") == 0) {
2102 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002103 }
2104 }
2105 if (strcmp(rtype, "window-change") == 0) {
2106 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002107 } else if (strcmp(rtype, "break") == 0) {
2108 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002109 }
Damien Millera6b1d162004-06-30 22:41:07 +10002110
Damien Millerc7ef63d2002-02-05 12:21:42 +11002111 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002112}
2113
2114void
Damien Miller8853ca52010-06-26 10:00:14 +10002115session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2116 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002117{
Damien Millerefb4afe2000-04-12 18:45:05 +10002118 /*
2119 * now that have a child and a pipe to the child,
2120 * we can activate our channel and register the fd's
2121 */
2122 if (s->chanid == -1)
2123 fatal("no channel for session %d", s->self);
2124 channel_set_fds(s->chanid,
2125 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002126 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002127 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002128}
2129
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002130/*
2131 * Function to perform pty cleanup. Also called if we get aborted abnormally
2132 * (e.g., due to a dropped connection).
2133 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002134void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002135session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002136{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002137 if (s == NULL) {
2138 error("session_pty_cleanup: no session");
2139 return;
2140 }
2141 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002142 return;
2143
Kevin Steves43cdef32001-02-11 14:12:08 +00002144 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002145
Damien Millerb38eff82000-04-01 11:09:21 +10002146 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002147 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002148 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002149
2150 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002151 if (getuid() == 0)
2152 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002153
2154 /*
2155 * Close the server side of the socket pairs. We must do this after
2156 * the pty cleanup, so that another process doesn't get this pty
2157 * while we're still cleaning up.
2158 */
Damien Miller7207f642008-05-19 15:34:50 +10002159 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2160 error("close(s->ptymaster/%d): %s",
2161 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002162
2163 /* unlink pty from session */
2164 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002165}
Damien Millerefb4afe2000-04-12 18:45:05 +10002166
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002167void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002168session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002169{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002170 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002171}
2172
Damien Miller5a80bba2002-09-04 16:39:02 +10002173static char *
2174sig2name(int sig)
2175{
2176#define SSH_SIG(x) if (sig == SIG ## x) return #x
2177 SSH_SIG(ABRT);
2178 SSH_SIG(ALRM);
2179 SSH_SIG(FPE);
2180 SSH_SIG(HUP);
2181 SSH_SIG(ILL);
2182 SSH_SIG(INT);
2183 SSH_SIG(KILL);
2184 SSH_SIG(PIPE);
2185 SSH_SIG(QUIT);
2186 SSH_SIG(SEGV);
2187 SSH_SIG(TERM);
2188 SSH_SIG(USR1);
2189 SSH_SIG(USR2);
2190#undef SSH_SIG
2191 return "SIG@openssh.com";
2192}
2193
Ben Lindstrombba81212001-06-25 05:01:22 +00002194static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002195session_close_x11(int id)
2196{
2197 Channel *c;
2198
Damien Millerd47c62a2005-12-13 19:33:57 +11002199 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002200 debug("session_close_x11: x11 channel %d missing", id);
2201 } else {
2202 /* Detach X11 listener */
2203 debug("session_close_x11: detach x11 channel %d", id);
2204 channel_cancel_cleanup(id);
2205 if (c->ostate != CHAN_OUTPUT_CLOSED)
2206 chan_mark_dead(c);
2207 }
2208}
2209
2210static void
2211session_close_single_x11(int id, void *arg)
2212{
2213 Session *s;
2214 u_int i;
2215
2216 debug3("session_close_single_x11: channel %d", id);
2217 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002218 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002219 fatal("session_close_single_x11: no x11 channel %d", id);
2220 for (i = 0; s->x11_chanids[i] != -1; i++) {
2221 debug("session_close_single_x11: session %d: "
2222 "closing channel %d", s->self, s->x11_chanids[i]);
2223 /*
2224 * The channel "id" is already closing, but make sure we
2225 * close all of its siblings.
2226 */
2227 if (s->x11_chanids[i] != id)
2228 session_close_x11(s->x11_chanids[i]);
2229 }
Darren Tuckera627d422013-06-02 07:31:17 +10002230 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002231 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002232 free(s->display);
2233 s->display = NULL;
2234 free(s->auth_proto);
2235 s->auth_proto = NULL;
2236 free(s->auth_data);
2237 s->auth_data = NULL;
2238 free(s->auth_display);
2239 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002240}
2241
2242static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002243session_exit_message(Session *s, int status)
2244{
2245 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002246
2247 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002248 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002249 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002250 debug("session_exit_message: session %d channel %d pid %ld",
2251 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002252
2253 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002254 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002255 packet_put_int(WEXITSTATUS(status));
2256 packet_send();
2257 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002258 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002259 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002260#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002261 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002262#else /* WCOREDUMP */
2263 packet_put_char(0);
2264#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002265 packet_put_cstring("");
2266 packet_put_cstring("");
2267 packet_send();
2268 } else {
2269 /* Some weird exit cause. Just exit. */
2270 packet_disconnect("wait returned status %04x.", status);
2271 }
2272
2273 /* disconnect channel */
2274 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002275
2276 /*
2277 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002278 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002279 * by session_close_by_channel when the childs close their fds.
2280 */
2281 channel_register_cleanup(c->self, session_close_by_channel, 1);
2282
Damien Miller166fca82000-04-20 07:42:21 +10002283 /*
2284 * emulate a write failure with 'chan_write_failed', nobody will be
2285 * interested in data we write.
2286 * Note that we must not call 'chan_read_failed', since there could
2287 * be some more data waiting in the pipe.
2288 */
Damien Millerbd483e72000-04-30 10:00:53 +10002289 if (c->ostate != CHAN_OUTPUT_CLOSED)
2290 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002291}
2292
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002293void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002294session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002295{
djm@openbsd.org95767262016-03-07 19:02:43 +00002296 struct ssh *ssh = active_state; /* XXX */
Damien Millereccb9de2005-06-17 12:59:34 +10002297 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002298
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002299 verbose("Close session: user %s from %.200s port %d id %d",
2300 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002301 ssh_remote_ipaddr(ssh),
2302 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002303 s->self);
2304
Darren Tucker3e33cec2003-10-02 16:12:36 +10002305 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002306 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002307 free(s->term);
2308 free(s->display);
2309 free(s->x11_chanids);
2310 free(s->auth_display);
2311 free(s->auth_data);
2312 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002313 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002314 if (s->env != NULL) {
2315 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002316 free(s->env[i].name);
2317 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002318 }
Darren Tuckera627d422013-06-02 07:31:17 +10002319 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002320 }
Damien Millere247cc42000-05-07 12:03:14 +10002321 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002322 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002323}
2324
2325void
2326session_close_by_pid(pid_t pid, int status)
2327{
2328 Session *s = session_by_pid(pid);
2329 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002330 debug("session_close_by_pid: no session for pid %ld",
2331 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002332 return;
2333 }
2334 if (s->chanid != -1)
2335 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002336 if (s->ttyfd != -1)
2337 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002338 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002339}
2340
2341/*
2342 * this is called when a channel dies before
2343 * the session 'child' itself dies
2344 */
2345void
2346session_close_by_channel(int id, void *arg)
2347{
2348 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002349 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002350
Damien Millerefb4afe2000-04-12 18:45:05 +10002351 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002352 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002353 return;
2354 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002355 debug("session_close_by_channel: channel %d child %ld",
2356 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002357 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002358 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002359 /*
2360 * delay detach of session, but release pty, since
2361 * the fd's to the child are already closed
2362 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002363 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002364 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002365 return;
2366 }
2367 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002368 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002369
2370 /* Close any X11 listeners associated with this session */
2371 if (s->x11_chanids != NULL) {
2372 for (i = 0; s->x11_chanids[i] != -1; i++) {
2373 session_close_x11(s->x11_chanids[i]);
2374 s->x11_chanids[i] = -1;
2375 }
2376 }
2377
Damien Millerefb4afe2000-04-12 18:45:05 +10002378 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002379 session_close(s);
2380}
2381
2382void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002383session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002384{
2385 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002386 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002387 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002388 if (s->used) {
2389 if (closefunc != NULL)
2390 closefunc(s);
2391 else
2392 session_close(s);
2393 }
Damien Miller52b77be2001-10-10 15:14:37 +10002394 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002395}
2396
Ben Lindstrombba81212001-06-25 05:01:22 +00002397static char *
Damien Millere247cc42000-05-07 12:03:14 +10002398session_tty_list(void)
2399{
2400 static char buf[1024];
2401 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002402 char *cp;
2403
Damien Millere247cc42000-05-07 12:03:14 +10002404 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002405 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002406 Session *s = &sessions[i];
2407 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002408
Damien Millera8ed44b2003-01-10 09:53:12 +11002409 if (strncmp(s->tty, "/dev/", 5) != 0) {
2410 cp = strrchr(s->tty, '/');
2411 cp = (cp == NULL) ? s->tty : cp + 1;
2412 } else
2413 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002414
Damien Millere247cc42000-05-07 12:03:14 +10002415 if (buf[0] != '\0')
2416 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002417 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002418 }
2419 }
2420 if (buf[0] == '\0')
2421 strlcpy(buf, "notty", sizeof buf);
2422 return buf;
2423}
2424
2425void
2426session_proctitle(Session *s)
2427{
2428 if (s->pw == NULL)
2429 error("no user for session %d", s->self);
2430 else
2431 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2432}
2433
Ben Lindstrom768176b2001-06-09 01:29:12 +00002434int
2435session_setup_x11fwd(Session *s)
2436{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002437 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002438 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002439 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002440 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002441
2442 if (no_x11_forwarding_flag) {
2443 packet_send_debug("X11 forwarding disabled in user configuration file.");
2444 return 0;
2445 }
2446 if (!options.x11_forwarding) {
2447 debug("X11 forwarding disabled in server configuration file.");
2448 return 0;
2449 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002450 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002451 (stat(options.xauth_location, &st) == -1)) {
2452 packet_send_debug("No xauth program; cannot forward with spoofing.");
2453 return 0;
2454 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002455 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002456 debug("X11 display already set.");
2457 return 0;
2458 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002459 if (x11_create_display_inet(options.x11_display_offset,
2460 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002461 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002462 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002463 return 0;
2464 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002465 for (i = 0; s->x11_chanids[i] != -1; i++) {
2466 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002467 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002468 }
Kevin Steves366298c2001-12-19 17:58:01 +00002469
2470 /* Set up a suitable value for the DISPLAY variable. */
2471 if (gethostname(hostname, sizeof(hostname)) < 0)
2472 fatal("gethostname: %.100s", strerror(errno));
2473 /*
2474 * auth_display must be used as the displayname when the
2475 * authorization entry is added with xauth(1). This will be
2476 * different than the DISPLAY string for localhost displays.
2477 */
Damien Miller95c249f2002-02-05 12:11:34 +11002478 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002479 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002480 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002481 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002482 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002483 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002484 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002485 } else {
2486#ifdef IPADDR_IN_DISPLAY
2487 struct hostent *he;
2488 struct in_addr my_addr;
2489
2490 he = gethostbyname(hostname);
2491 if (he == NULL) {
2492 error("Can't get IP address for X11 DISPLAY.");
2493 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2494 return 0;
2495 }
2496 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002497 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002498 s->display_number, s->screen);
2499#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002500 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002501 s->display_number, s->screen);
2502#endif
2503 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002504 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002505 }
2506
Ben Lindstrom768176b2001-06-09 01:29:12 +00002507 return 1;
2508}
2509
Ben Lindstrombba81212001-06-25 05:01:22 +00002510static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002511do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002512{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002513 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002514}
2515
2516void
2517do_cleanup(Authctxt *authctxt)
2518{
2519 static int called = 0;
2520
2521 debug("do_cleanup");
2522
2523 /* no cleanup if we're in the child for login shell */
2524 if (is_child)
2525 return;
2526
2527 /* avoid double cleanup */
2528 if (called)
2529 return;
2530 called = 1;
2531
Darren Tucker9142e1c2007-08-16 23:28:04 +10002532 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002533 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002534
2535#ifdef USE_PAM
2536 if (options.use_pam) {
2537 sshpam_cleanup();
2538 sshpam_thread_cleanup();
2539 }
2540#endif
2541
2542 if (!authctxt->authenticated)
2543 return;
2544
Darren Tucker3e33cec2003-10-02 16:12:36 +10002545#ifdef KRB5
2546 if (options.kerberos_ticket_cleanup &&
2547 authctxt->krb5_ctx)
2548 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002549#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002550
2551#ifdef GSSAPI
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002552 if (options.gss_cleanup_creds)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002553 ssh_gssapi_cleanup_creds();
2554#endif
2555
2556 /* remove agent socket */
2557 auth_sock_cleanup_proc(authctxt->pw);
2558
2559 /*
2560 * Cleanup ptys/utmp only if privsep is disabled,
2561 * or if running in monitor.
2562 */
2563 if (!use_privsep || mm_is_monitor())
2564 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002565}
djm@openbsd.org95767262016-03-07 19:02:43 +00002566
2567/* Return a name for the remote host that fits inside utmp_size */
2568
2569const char *
2570session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2571{
2572 const char *remote = "";
2573
2574 if (utmp_size > 0)
2575 remote = auth_get_canonical_hostname(ssh, use_dns);
2576 if (utmp_size == 0 || strlen(remote) > utmp_size)
2577 remote = ssh_remote_ipaddr(ssh);
2578 return remote;
2579}
2580