blob: 18cdfa8cf450c1826d4cb290720e274c480b8a80 [file] [log] [blame]
djm@openbsd.orgd081f012020-03-13 03:17:07 +00001/* $OpenBSD: session.c,v 1.319 2020/03/13 03:17:07 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 Millera7a73ee2006-08-05 11:37:59 +100059#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100060#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100061#include <string.h>
deraadt@openbsd.org72687c82019-11-13 04:47:52 +000062#include <stdarg.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"
markus@openbsd.org2808d182018-07-09 21:26:02 +000072#include "sshbuf.h"
73#include "ssherr.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100074#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100075#include "uidswap.h"
76#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000077#include "channels.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000078#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100079#include "cipher.h"
80#ifdef GSSAPI
81#include "ssh-gss.h"
82#endif
83#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100084#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100085#include "auth-options.h"
Damien Miller85b45e02013-07-20 13:21:52 +100086#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087#include "pathnames.h"
88#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100089#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000091#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "serverloop.h"
93#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000094#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100095#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110097#include "sftp.h"
djm@openbsd.org8f574952017-06-24 06:34:38 +000098#include "atomicio.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100099
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100100#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +1100101#include <kafs.h>
102#endif
103
Damien Miller14684a12011-05-20 11:23:07 +1000104#ifdef WITH_SELINUX
105#include <selinux/selinux.h>
106#endif
107
Damien Millerad793d52008-11-03 19:17:57 +1100108#define IS_INTERNAL_SFTP(c) \
109 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
110 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
111 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
112 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
113
Damien Millerb38eff82000-04-01 11:09:21 +1000114/* func */
115
116Session *session_new(void);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000117void session_set_fds(struct ssh *, Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000118void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000119void session_proctitle(Session *);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000120int session_setup_x11fwd(struct ssh *, Session *);
121int do_exec_pty(struct ssh *, Session *, const char *);
122int do_exec_no_pty(struct ssh *, Session *, const char *);
123int do_exec(struct ssh *, Session *, const char *);
124void do_login(struct ssh *, Session *, const char *);
125void do_child(struct ssh *, Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000126void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000127int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000128
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000129static void do_authenticated2(struct ssh *, Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000130
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000131static int session_pty_req(struct ssh *, Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000132
Damien Millerb38eff82000-04-01 11:09:21 +1000133/* import */
134extern ServerOptions options;
135extern char *__progname;
Damien Millerb38eff82000-04-01 11:09:21 +1000136extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000137extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000138extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000139extern void destroy_sensitive_data(void);
markus@openbsd.org2808d182018-07-09 21:26:02 +0000140extern struct sshbuf *loginmsg;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000141extern struct sshauthopt *auth_opts;
djm@openbsd.orgf47d72d2019-01-17 04:45:09 +0000142extern char *tun_fwd_ifnames; /* serverloop.c */
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
djm@openbsd.org8f574952017-06-24 06:34:38 +0000164/* File containing userauth info, if ExposeAuthInfo set */
165static char *auth_info_file = NULL;
166
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000167/* Name and directory of socket for authentication agent forwarding. */
168static char *auth_sock_name = NULL;
169static char *auth_sock_dir = NULL;
170
171/* removes the agent forwarding socket */
172
173static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000174auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000175{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000176 if (auth_sock_name != NULL) {
177 temporarily_use_uid(pw);
178 unlink(auth_sock_name);
179 rmdir(auth_sock_dir);
180 auth_sock_name = NULL;
181 restore_uid();
182 }
183}
184
185static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000186auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000187{
188 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000189 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000190
191 if (auth_sock_name != NULL) {
192 error("authentication forwarding requested twice.");
193 return 0;
194 }
195
196 /* Temporarily drop privileged uid for mkdir/bind. */
197 temporarily_use_uid(pw);
198
199 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000200 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000201
202 /* Create private directory for socket */
203 if (mkdtemp(auth_sock_dir) == NULL) {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +0000204 ssh_packet_send_debug(ssh, "Agent forwarding disabled: "
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000205 "mkdtemp() failed: %.100s", strerror(errno));
206 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000207 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000208 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000209 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000210 }
Damien Miller7207f642008-05-19 15:34:50 +1000211
212 xasprintf(&auth_sock_name, "%s/agent.%ld",
213 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000214
Damien Miller7acefbb2014-07-18 14:11:24 +1000215 /* Start a Unix listener on auth_sock_name. */
216 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000217
218 /* Restore the privileged uid. */
219 restore_uid();
220
Damien Miller7acefbb2014-07-18 14:11:24 +1000221 /* Check for socket/bind/listen failure. */
222 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000223 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000224
225 /* Allocate a channel for the authentication agent socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000226 nc = channel_new(ssh, "auth socket",
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000227 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
228 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000229 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100230 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000231 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000232
233 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000234 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000235 if (auth_sock_dir != NULL) {
djm@openbsd.org625b6262019-02-22 03:37:11 +0000236 temporarily_use_uid(pw);
Damien Miller7207f642008-05-19 15:34:50 +1000237 rmdir(auth_sock_dir);
djm@openbsd.org625b6262019-02-22 03:37:11 +0000238 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000239 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000240 }
241 if (sock != -1)
242 close(sock);
243 auth_sock_name = NULL;
244 auth_sock_dir = NULL;
245 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000246}
247
Darren Tucker1921ed92004-02-10 13:23:28 +1100248static void
249display_loginmsg(void)
250{
markus@openbsd.org2808d182018-07-09 21:26:02 +0000251 int r;
252
253 if (sshbuf_len(loginmsg) == 0)
254 return;
255 if ((r = sshbuf_put_u8(loginmsg, 0)) != 0)
256 fatal("%s: buffer error: %s", __func__, ssh_err(r));
257 printf("%s", (char *)sshbuf_ptr(loginmsg));
258 sshbuf_reset(loginmsg);
Darren Tucker1921ed92004-02-10 13:23:28 +1100259}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000260
djm@openbsd.org8f574952017-06-24 06:34:38 +0000261static void
262prepare_auth_info_file(struct passwd *pw, struct sshbuf *info)
263{
264 int fd = -1, success = 0;
265
266 if (!options.expose_userauth_info || info == NULL)
267 return;
268
269 temporarily_use_uid(pw);
270 auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX");
271 if ((fd = mkstemp(auth_info_file)) == -1) {
272 error("%s: mkstemp: %s", __func__, strerror(errno));
273 goto out;
274 }
275 if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info),
276 sshbuf_len(info)) != sshbuf_len(info)) {
277 error("%s: write: %s", __func__, strerror(errno));
278 goto out;
279 }
280 if (close(fd) != 0) {
281 error("%s: close: %s", __func__, strerror(errno));
282 goto out;
283 }
284 success = 1;
285 out:
286 if (!success) {
287 if (fd != -1)
288 close(fd);
289 free(auth_info_file);
290 auth_info_file = NULL;
291 }
292 restore_uid();
293}
294
djm@openbsd.org7c856852018-03-03 03:15:51 +0000295static void
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000296set_fwdpermit_from_authopts(struct ssh *ssh, const struct sshauthopt *opts)
djm@openbsd.org7c856852018-03-03 03:15:51 +0000297{
298 char *tmp, *cp, *host;
299 int port;
300 size_t i;
301
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000302 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0) {
303 channel_clear_permission(ssh, FORWARD_USER, FORWARD_LOCAL);
304 for (i = 0; i < auth_opts->npermitopen; i++) {
305 tmp = cp = xstrdup(auth_opts->permitopen[i]);
306 /* This shouldn't fail as it has already been checked */
307 if ((host = hpdelim(&cp)) == NULL)
308 fatal("%s: internal error: hpdelim", __func__);
309 host = cleanhostname(host);
310 if (cp == NULL || (port = permitopen_port(cp)) < 0)
311 fatal("%s: internal error: permitopen port",
312 __func__);
313 channel_add_permission(ssh,
314 FORWARD_USER, FORWARD_LOCAL, host, port);
315 free(tmp);
316 }
317 }
318 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) != 0) {
319 channel_clear_permission(ssh, FORWARD_USER, FORWARD_REMOTE);
320 for (i = 0; i < auth_opts->npermitlisten; i++) {
321 tmp = cp = xstrdup(auth_opts->permitlisten[i]);
322 /* This shouldn't fail as it has already been checked */
323 if ((host = hpdelim(&cp)) == NULL)
324 fatal("%s: internal error: hpdelim", __func__);
325 host = cleanhostname(host);
326 if (cp == NULL || (port = permitopen_port(cp)) < 0)
327 fatal("%s: internal error: permitlisten port",
328 __func__);
329 channel_add_permission(ssh,
330 FORWARD_USER, FORWARD_REMOTE, host, port);
331 free(tmp);
332 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000333 }
334}
335
Ben Lindstromb31783d2001-03-22 02:02:12 +0000336void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000337do_authenticated(struct ssh *ssh, Authctxt *authctxt)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000338{
Damien Miller97f39ae2003-02-24 11:57:01 +1100339 setproctitle("%s", authctxt->pw->pw_name);
340
djm@openbsd.org7c856852018-03-03 03:15:51 +0000341 auth_log_authopts("active", auth_opts, 0);
342
Ben Lindstromb31783d2001-03-22 02:02:12 +0000343 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000344 /* XXX - streamlocal? */
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000345 set_fwdpermit_from_authopts(ssh, auth_opts);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000346
djm@openbsd.org115063a2018-06-06 18:22:41 +0000347 if (!auth_opts->permit_port_forwarding_flag ||
348 options.disable_forwarding) {
349 channel_disable_admin(ssh, FORWARD_LOCAL);
350 channel_disable_admin(ssh, FORWARD_REMOTE);
351 } else {
352 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
353 channel_disable_admin(ssh, FORWARD_LOCAL);
354 else
355 channel_permit_all(ssh, FORWARD_LOCAL);
356 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0)
357 channel_disable_admin(ssh, FORWARD_REMOTE);
358 else
359 channel_permit_all(ssh, FORWARD_REMOTE);
360 }
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000361 auth_debug_send(ssh);
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100362
djm@openbsd.org8f574952017-06-24 06:34:38 +0000363 prepare_auth_info_file(authctxt->pw, authctxt->session_info);
364
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000365 do_authenticated2(ssh, authctxt);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000366
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000367 do_cleanup(ssh, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000368}
369
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000370/* Check untrusted xauth strings for metacharacters */
371static int
372xauth_valid_string(const char *s)
373{
374 size_t i;
375
376 for (i = 0; s[i] != '\0'; i++) {
377 if (!isalnum((u_char)s[i]) &&
378 s[i] != '.' && s[i] != ':' && s[i] != '/' &&
379 s[i] != '-' && s[i] != '_')
deraadt@openbsd.org21fd4772018-07-25 13:56:23 +0000380 return 0;
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000381 }
382 return 1;
383}
384
Darren Tucker293ee3c2014-01-19 15:28:01 +1100385#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000386/*
387 * This is called to fork and execute a command when we have no tty. This
388 * will call do_child from the child, and server_loop from the parent after
389 * setting up file descriptors and such.
390 */
Damien Miller7207f642008-05-19 15:34:50 +1000391int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000392do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000393{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000394 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000395#ifdef USE_PIPES
396 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000397
Damien Miller22a29882010-05-10 11:53:54 +1000398 if (s == NULL)
399 fatal("do_exec_no_pty: no session");
400
Damien Millerb38eff82000-04-01 11:09:21 +1000401 /* Allocate pipes for communicating with the program. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000402 if (pipe(pin) == -1) {
Damien Miller7207f642008-05-19 15:34:50 +1000403 error("%s: pipe in: %.100s", __func__, strerror(errno));
404 return -1;
405 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000406 if (pipe(pout) == -1) {
Damien Miller7207f642008-05-19 15:34:50 +1000407 error("%s: pipe out: %.100s", __func__, strerror(errno));
408 close(pin[0]);
409 close(pin[1]);
410 return -1;
411 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000412 if (pipe(perr) == -1) {
Damien Miller8853ca52010-06-26 10:00:14 +1000413 error("%s: pipe err: %.100s", __func__,
414 strerror(errno));
415 close(pin[0]);
416 close(pin[1]);
417 close(pout[0]);
418 close(pout[1]);
419 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000420 }
421#else
Damien Millerb38eff82000-04-01 11:09:21 +1000422 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000423
Damien Miller22a29882010-05-10 11:53:54 +1000424 if (s == NULL)
425 fatal("do_exec_no_pty: no session");
426
Damien Millerb38eff82000-04-01 11:09:21 +1000427 /* Uses socket pairs to communicate with the program. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000428 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) {
Damien Miller7207f642008-05-19 15:34:50 +1000429 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
430 return -1;
431 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000432 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) == -1) {
Damien Miller8853ca52010-06-26 10:00:14 +1000433 error("%s: socketpair #2: %.100s", __func__,
434 strerror(errno));
435 close(inout[0]);
436 close(inout[1]);
437 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000438 }
439#endif
440
Damien Millere247cc42000-05-07 12:03:14 +1000441 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000442
Damien Millerb38eff82000-04-01 11:09:21 +1000443 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000444 switch ((pid = fork())) {
445 case -1:
446 error("%s: fork: %.100s", __func__, strerror(errno));
447#ifdef USE_PIPES
448 close(pin[0]);
449 close(pin[1]);
450 close(pout[0]);
451 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000452 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000453 close(perr[1]);
454#else
455 close(inout[0]);
456 close(inout[1]);
457 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000458 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000459#endif
460 return -1;
461 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000462 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000463
Damien Millerb38eff82000-04-01 11:09:21 +1000464 /*
465 * Create a new session and process group since the 4.4BSD
466 * setlogin() affects the entire process group.
467 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000468 if (setsid() == -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000469 error("setsid failed: %.100s", strerror(errno));
470
471#ifdef USE_PIPES
472 /*
473 * Redirect stdin. We close the parent side of the socket
474 * pair, and make the child side the standard input.
475 */
476 close(pin[1]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000477 if (dup2(pin[0], 0) == -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000478 perror("dup2 stdin");
479 close(pin[0]);
480
481 /* Redirect stdout. */
482 close(pout[0]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000483 if (dup2(pout[1], 1) == -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000484 perror("dup2 stdout");
485 close(pout[1]);
486
487 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000488 close(perr[0]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000489 if (dup2(perr[1], 2) == -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000490 perror("dup2 stderr");
491 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000492#else
Damien Millerb38eff82000-04-01 11:09:21 +1000493 /*
494 * Redirect stdin, stdout, and stderr. Stdin and stdout will
495 * use the same socket, as some programs (particularly rdist)
496 * seem to depend on it.
497 */
498 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000499 close(err[1]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000500 if (dup2(inout[0], 0) == -1) /* stdin */
Damien Millerb38eff82000-04-01 11:09:21 +1000501 perror("dup2 stdin");
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000502 if (dup2(inout[0], 1) == -1) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000503 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000504 close(inout[0]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000505 if (dup2(err[0], 2) == -1) /* stderr */
Damien Millerb38eff82000-04-01 11:09:21 +1000506 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000507 close(err[0]);
508#endif
509
Damien Millerb38eff82000-04-01 11:09:21 +1000510 /* Do processing for the child (exec command etc). */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000511 do_child(ssh, s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000512 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000513 default:
514 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000515 }
Damien Miller7207f642008-05-19 15:34:50 +1000516
Damien Millerbac2d8a2000-09-05 16:13:06 +1100517#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100518 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100519#endif
Damien Miller7207f642008-05-19 15:34:50 +1000520
Damien Millerb38eff82000-04-01 11:09:21 +1000521 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000522 /* Set interactive/non-interactive mode. */
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +0000523 ssh_packet_set_interactive(ssh, s->display != NULL,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100524 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000525
526 /*
527 * Clear loginmsg, since it's the child's responsibility to display
528 * it to the user, otherwise multiple sessions may accumulate
529 * multiple copies of the login messages.
530 */
Damien Miller120a1ec2018-07-10 19:39:52 +1000531 sshbuf_reset(loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000532
Damien Millerb38eff82000-04-01 11:09:21 +1000533#ifdef USE_PIPES
534 /* We are the parent. Close the child sides of the pipes. */
535 close(pin[0]);
536 close(pout[1]);
537 close(perr[1]);
538
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000539 session_set_fds(ssh, s, pin[1], pout[0], perr[0],
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000540 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000541#else
Damien Millerb38eff82000-04-01 11:09:21 +1000542 /* We are the parent. Close the child sides of the socket pairs. */
543 close(inout[0]);
544 close(err[0]);
545
546 /*
547 * Enter the interactive session. Note: server_loop must be able to
548 * handle the case that fdin and fdout are the same.
549 */
dtucker@openbsd.org8c53d402019-02-05 11:35:56 +0000550 session_set_fds(ssh, s, inout[1], inout[1], err[1],
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000551 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000552#endif
553 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000554}
555
556/*
557 * This is called to fork and execute a command when we have a tty. This
558 * will call do_child from the child, and server_loop from the parent after
559 * setting up file descriptors, controlling tty, updating wtmp, utmp,
560 * lastlog, and other such operations.
561 */
Damien Miller7207f642008-05-19 15:34:50 +1000562int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000563do_exec_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000564{
Damien Millerb38eff82000-04-01 11:09:21 +1000565 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000566 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000567
568 if (s == NULL)
569 fatal("do_exec_pty: no session");
570 ptyfd = s->ptyfd;
571 ttyfd = s->ttyfd;
572
Damien Miller7207f642008-05-19 15:34:50 +1000573 /*
574 * Create another descriptor of the pty master side for use as the
575 * standard input. We could use the original descriptor, but this
576 * simplifies code in server_loop. The descriptor is bidirectional.
577 * Do this before forking (and cleanup in the child) so as to
578 * detect and gracefully fail out-of-fd conditions.
579 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000580 if ((fdout = dup(ptyfd)) == -1) {
Damien Miller7207f642008-05-19 15:34:50 +1000581 error("%s: dup #1: %s", __func__, strerror(errno));
582 close(ttyfd);
583 close(ptyfd);
584 return -1;
585 }
586 /* we keep a reference to the pty master */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000587 if ((ptymaster = dup(ptyfd)) == -1) {
Damien Miller7207f642008-05-19 15:34:50 +1000588 error("%s: dup #2: %s", __func__, strerror(errno));
589 close(ttyfd);
590 close(ptyfd);
591 close(fdout);
592 return -1;
593 }
594
Damien Millerb38eff82000-04-01 11:09:21 +1000595 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000596 switch ((pid = fork())) {
597 case -1:
598 error("%s: fork: %.100s", __func__, strerror(errno));
599 close(fdout);
600 close(ptymaster);
601 close(ttyfd);
602 close(ptyfd);
603 return -1;
604 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000605 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000606
Damien Miller7207f642008-05-19 15:34:50 +1000607 close(fdout);
608 close(ptymaster);
609
Damien Millerb38eff82000-04-01 11:09:21 +1000610 /* Close the master side of the pseudo tty. */
611 close(ptyfd);
612
613 /* Make the pseudo tty our controlling tty. */
614 pty_make_controlling_tty(&ttyfd, s->tty);
615
Damien Miller9c751422001-10-10 15:02:46 +1000616 /* Redirect stdin/stdout/stderr from the pseudo tty. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000617 if (dup2(ttyfd, 0) == -1)
Damien Miller9c751422001-10-10 15:02:46 +1000618 error("dup2 stdin: %s", strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000619 if (dup2(ttyfd, 1) == -1)
Damien Miller9c751422001-10-10 15:02:46 +1000620 error("dup2 stdout: %s", strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000621 if (dup2(ttyfd, 2) == -1)
Damien Miller9c751422001-10-10 15:02:46 +1000622 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000623
624 /* Close the extra descriptor for the pseudo tty. */
625 close(ttyfd);
626
Damien Miller942da032000-08-18 13:59:06 +1000627 /* record login, etc. similar to login(1) */
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000628#ifndef HAVE_OSF_SIA
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000629 do_login(ssh, s, command);
Damien Miller364a9bd2001-04-16 18:37:05 +1000630#endif
Damien Miller7207f642008-05-19 15:34:50 +1000631 /*
632 * Do common processing for the child, such as execing
633 * the command.
634 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000635 do_child(ssh, s, command);
Darren Tucker43e7a352009-06-21 19:50:08 +1000636 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000637 default:
638 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000639 }
Damien Miller7207f642008-05-19 15:34:50 +1000640
Damien Millerbac2d8a2000-09-05 16:13:06 +1100641#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100642 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100643#endif
Damien Miller7207f642008-05-19 15:34:50 +1000644
Damien Millerb38eff82000-04-01 11:09:21 +1000645 s->pid = pid;
646
647 /* Parent. Close the slave side of the pseudo tty. */
648 close(ttyfd);
649
Damien Millerb38eff82000-04-01 11:09:21 +1000650 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000651 s->ptymaster = ptymaster;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +0000652 ssh_packet_set_interactive(ssh, 1,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100653 options.ip_qos_interactive, options.ip_qos_bulk);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000654 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
Damien Miller7207f642008-05-19 15:34:50 +1000655 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000656}
657
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000658/*
659 * This is called to fork and execute a command. If another command is
660 * to be forced, execute that instead.
661 */
Damien Miller7207f642008-05-19 15:34:50 +1000662int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000663do_exec(struct ssh *ssh, Session *s, const char *command)
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000664{
Damien Miller7207f642008-05-19 15:34:50 +1000665 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000666 const char *forced = NULL, *tty = NULL;
667 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000668
Damien Millere2754432006-07-24 14:06:47 +1000669 if (options.adm_forced_command) {
670 original_command = command;
671 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100672 forced = "(config)";
djm@openbsd.org7c856852018-03-03 03:15:51 +0000673 } else if (auth_opts->force_command != NULL) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000674 original_command = command;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000675 command = auth_opts->force_command;
Damien Miller71df7522013-10-15 12:12:02 +1100676 forced = "(key-option)";
677 }
djm@openbsd.orgcd989252018-10-02 12:40:07 +0000678 s->forced = 0;
Damien Miller71df7522013-10-15 12:12:02 +1100679 if (forced != NULL) {
djm@openbsd.orgcd989252018-10-02 12:40:07 +0000680 s->forced = 1;
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100681 if (IS_INTERNAL_SFTP(command)) {
682 s->is_subsystem = s->is_subsystem ?
683 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
684 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100685 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100686 snprintf(session_type, sizeof(session_type),
687 "forced-command %s '%.900s'", forced, command);
688 } else if (s->is_subsystem) {
689 snprintf(session_type, sizeof(session_type),
690 "subsystem '%.900s'", s->subsys);
691 } else if (command == NULL) {
692 snprintf(session_type, sizeof(session_type), "shell");
693 } else {
694 /* NB. we don't log unforced commands to preserve privacy */
695 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000696 }
697
Damien Miller71df7522013-10-15 12:12:02 +1100698 if (s->ttyfd != -1) {
699 tty = s->tty;
700 if (strncmp(tty, "/dev/", 5) == 0)
701 tty += 5;
702 }
703
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000704 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100705 session_type,
706 tty == NULL ? "" : " on ",
707 tty == NULL ? "" : tty,
708 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000709 ssh_remote_ipaddr(ssh),
710 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000711 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100712
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100713#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100714 if (command != NULL)
715 PRIVSEP(audit_run_command(command));
716 else if (s->ttyfd == -1) {
717 char *shell = s->pw->pw_shell;
718
719 if (shell[0] == '\0') /* empty shell means /bin/sh */
720 shell =_PATH_BSHELL;
721 PRIVSEP(audit_run_command(shell));
722 }
723#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000724 if (s->ttyfd != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000725 ret = do_exec_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000726 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000727 ret = do_exec_no_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000728
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000729 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000730
Darren Tucker09991742004-07-17 17:05:14 +1000731 /*
732 * Clear loginmsg: it's the child's responsibility to display
733 * it to the user, otherwise multiple sessions may accumulate
734 * multiple copies of the login messages.
735 */
markus@openbsd.org2808d182018-07-09 21:26:02 +0000736 sshbuf_reset(loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000737
738 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000739}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000740
Damien Miller942da032000-08-18 13:59:06 +1000741/* administrative, login(1)-like work */
742void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000743do_login(struct ssh *ssh, Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000744{
Damien Miller942da032000-08-18 13:59:06 +1000745 socklen_t fromlen;
746 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000747 struct passwd * pw = s->pw;
748 pid_t pid = getpid();
749
750 /*
751 * Get IP address of client. If the connection is not a socket, let
752 * the address be 0.0.0.0.
753 */
754 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000755 fromlen = sizeof(from);
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +0000756 if (ssh_packet_connection_is_on_socket(ssh)) {
757 if (getpeername(ssh_packet_get_connection_in(ssh),
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000758 (struct sockaddr *)&from, &fromlen) == -1) {
Damien Miller942da032000-08-18 13:59:06 +1000759 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000760 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000761 }
762 }
763
764 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000765 if (!use_privsep)
766 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000767 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000768 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000769 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000770
Kevin Steves092f2ef2000-10-14 13:36:13 +0000771#ifdef USE_PAM
772 /*
773 * If password change is needed, do it now.
774 * This needs to occur before the ~/.hushlogin check.
775 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100776 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
777 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000778 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100779 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000780 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000781 }
782#endif
783
Damien Millercf205e82001-04-16 18:29:15 +1000784 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000785 return;
786
Darren Tucker1921ed92004-02-10 13:23:28 +1100787 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000788
Damien Millercf205e82001-04-16 18:29:15 +1000789 do_motd();
790}
791
792/*
793 * Display the message of the day.
794 */
795void
796do_motd(void)
797{
798 FILE *f;
799 char buf[256];
800
Damien Miller942da032000-08-18 13:59:06 +1000801 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000802#ifdef HAVE_LOGIN_CAP
803 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
804 "/etc/motd"), "r");
805#else
Damien Miller942da032000-08-18 13:59:06 +1000806 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000807#endif
Damien Miller942da032000-08-18 13:59:06 +1000808 if (f) {
809 while (fgets(buf, sizeof(buf), f))
810 fputs(buf, stdout);
811 fclose(f);
812 }
813 }
814}
815
Kevin Steves8f63caa2001-07-04 18:23:02 +0000816
817/*
818 * Check for quiet login, either .hushlogin or command given.
819 */
820int
821check_quietlogin(Session *s, const char *command)
822{
823 char buf[256];
824 struct passwd *pw = s->pw;
825 struct stat st;
826
827 /* Return 1 if .hushlogin exists or a command given. */
828 if (command != NULL)
829 return 1;
830 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
831#ifdef HAVE_LOGIN_CAP
832 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
833 return 1;
834#else
835 if (stat(buf, &st) >= 0)
836 return 1;
837#endif
838 return 0;
839}
840
Damien Millerb38eff82000-04-01 11:09:21 +1000841/*
Damien Millerb38eff82000-04-01 11:09:21 +1000842 * Reads environment variables from the given file and adds/overrides them
843 * into the environment. If the file does not exist, this does nothing.
844 * Otherwise, it must consist of empty lines, comments (line starts with '#')
845 * and assignments of the form name=value. No other forms are allowed.
djm@openbsd.org95344c22018-07-03 10:59:35 +0000846 * If whitelist is not NULL, then it is interpreted as a pattern list and
847 * only variable names that match it will be accepted.
Damien Millerb38eff82000-04-01 11:09:21 +1000848 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000849static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000850read_environment_file(char ***env, u_int *envsize,
djm@openbsd.org95344c22018-07-03 10:59:35 +0000851 const char *filename, const char *whitelist)
Damien Millerb38eff82000-04-01 11:09:21 +1000852{
853 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000854 char *line = NULL, *cp, *value;
855 size_t linesize = 0;
Damien Miller990070a2002-06-26 23:51:06 +1000856 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000857
858 f = fopen(filename, "r");
859 if (!f)
860 return;
861
markus@openbsd.org7f906352018-06-06 18:29:18 +0000862 while (getline(&line, &linesize, f) != -1) {
Damien Miller990070a2002-06-26 23:51:06 +1000863 if (++lineno > 1000)
864 fatal("Too many lines in environment file %s", filename);
markus@openbsd.org7f906352018-06-06 18:29:18 +0000865 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
Damien Millerb38eff82000-04-01 11:09:21 +1000866 ;
867 if (!*cp || *cp == '#' || *cp == '\n')
868 continue;
Damien Miller14b017d2007-09-17 16:09:15 +1000869
870 cp[strcspn(cp, "\n")] = '\0';
871
Damien Millerb38eff82000-04-01 11:09:21 +1000872 value = strchr(cp, '=');
873 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000874 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
875 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000876 continue;
877 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000878 /*
879 * Replace the equals sign by nul, and advance value to
880 * the value string.
881 */
Damien Millerb38eff82000-04-01 11:09:21 +1000882 *value = '\0';
883 value++;
djm@openbsd.org95344c22018-07-03 10:59:35 +0000884 if (whitelist != NULL &&
885 match_pattern_list(cp, whitelist, 0) != 1)
886 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000887 child_set_env(env, envsize, cp, value);
888 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000889 free(line);
Damien Millerb38eff82000-04-01 11:09:21 +1000890 fclose(f);
891}
892
Darren Tuckere1a790d2003-09-16 11:52:19 +1000893#ifdef HAVE_ETC_DEFAULT_LOGIN
894/*
895 * Return named variable from specified environment, or NULL if not present.
896 */
897static char *
898child_get_env(char **env, const char *name)
899{
900 int i;
901 size_t len;
902
903 len = strlen(name);
904 for (i=0; env[i] != NULL; i++)
905 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
906 return(env[i] + len + 1);
907 return NULL;
908}
909
910/*
911 * Read /etc/default/login.
912 * We pick up the PATH (or SUPATH for root) and UMASK.
913 */
914static void
915read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
916{
917 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000918 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000919 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000920
921 /*
922 * We don't want to copy the whole file to the child's environment,
923 * so we use a temporary environment and copy the variables we're
924 * interested in.
925 */
Darren Tucker3deb56f2018-07-05 13:32:01 +1000926 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login",
927 options.permit_user_env_whitelist);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000928
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000929 if (tmpenv == NULL)
930 return;
931
Darren Tuckere1a790d2003-09-16 11:52:19 +1000932 if (uid == 0)
933 var = child_get_env(tmpenv, "SUPATH");
934 else
935 var = child_get_env(tmpenv, "PATH");
936 if (var != NULL)
937 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100938
Darren Tuckere1a790d2003-09-16 11:52:19 +1000939 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
940 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000941 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100942
Darren Tuckere1a790d2003-09-16 11:52:19 +1000943 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +1000944 free(tmpenv[i]);
945 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000946}
947#endif /* HAVE_ETC_DEFAULT_LOGIN */
948
Damien Millera8bd5fd2020-01-21 12:32:16 +1100949#if defined(USE_PAM) || defined(HAVE_CYGWIN)
Damien Miller94bc1e72017-07-28 14:50:59 +1000950static void
951copy_environment_blacklist(char **source, char ***env, u_int *envsize,
952 const char *blacklist)
Damien Millerb38eff82000-04-01 11:09:21 +1000953{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100954 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000955 int i;
956
Damien Millerbb9ffc12002-01-08 10:59:32 +1100957 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000958 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000959
Damien Millerbb9ffc12002-01-08 10:59:32 +1100960 for(i = 0; source[i] != NULL; i++) {
961 var_name = xstrdup(source[i]);
962 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000963 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000964 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000965 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100966 *var_val++ = '\0';
967
Damien Miller94bc1e72017-07-28 14:50:59 +1000968 if (blacklist == NULL ||
969 match_pattern_list(var_name, blacklist, 0) != 1) {
970 debug3("Copy environment: %s=%s", var_name, var_val);
971 child_set_env(env, envsize, var_name, var_val);
972 }
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 Millera8bd5fd2020-01-21 12:32:16 +1100977#endif /* defined(USE_PAM) || defined(HAVE_CYGWIN) */
Damien Millercb5e44a2000-09-29 12:12:36 +1100978
Ruben Kerkhofb3f70092020-01-20 11:56:48 +0100979#ifdef HAVE_CYGWIN
980static void
Damien Miller94bc1e72017-07-28 14:50:59 +1000981copy_environment(char **source, char ***env, u_int *envsize)
982{
983 copy_environment_blacklist(source, env, envsize, NULL);
984}
Ruben Kerkhofb3f70092020-01-20 11:56:48 +0100985#endif
Damien Miller94bc1e72017-07-28 14:50:59 +1000986
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000987static char **
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000988do_setup_env(struct ssh *ssh, Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000989{
Damien Millerb38eff82000-04-01 11:09:21 +1000990 char buf[256];
djm@openbsd.org7c856852018-03-03 03:15:51 +0000991 size_t n;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000992 u_int i, envsize;
djm@openbsd.org28013752018-06-09 03:03:10 +0000993 char *ocp, *cp, *value, **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000994 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100995#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +1000996 char *path = NULL;
997#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000998
Damien Millerb38eff82000-04-01 11:09:21 +1000999 /* Initialize the environment. */
1000 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001001 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001002 env[0] = NULL;
1003
Damien Millerbac2d8a2000-09-05 16:13:06 +11001004#ifdef HAVE_CYGWIN
1005 /*
1006 * The Windows environment contains some setting which are
1007 * important for a running system. They must not be dropped.
1008 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001009 {
1010 char **p;
1011
1012 p = fetch_windows_environment();
1013 copy_environment(p, &env, &envsize);
1014 free_windows_environment(p);
1015 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001016#endif
1017
Darren Tucker0efd1552003-08-26 11:49:55 +10001018#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001019 /* Allow any GSSAPI methods that we've used to alter
djm@openbsd.orgd081f012020-03-13 03:17:07 +00001020 * the child's environment as they see fit
Darren Tucker0efd1552003-08-26 11:49:55 +10001021 */
1022 ssh_gssapi_do_child(&env, &envsize);
1023#endif
1024
djm@openbsd.org83b58182016-08-19 03:18:06 +00001025 /* Set basic environment. */
1026 for (i = 0; i < s->num_env; i++)
1027 child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
Darren Tucker46bc0752004-05-02 22:11:30 +10001028
djm@openbsd.org83b58182016-08-19 03:18:06 +00001029 child_set_env(&env, &envsize, "USER", pw->pw_name);
1030 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001031#ifdef _AIX
djm@openbsd.org83b58182016-08-19 03:18:06 +00001032 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001033#endif
djm@openbsd.org83b58182016-08-19 03:18:06 +00001034 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001035#ifdef HAVE_LOGIN_CAP
djm@openbsd.org83b58182016-08-19 03:18:06 +00001036 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1037 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1038 else
1039 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001040#else /* HAVE_LOGIN_CAP */
1041# ifndef HAVE_CYGWIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001042 /*
1043 * There's no standard path on Windows. The path contains
1044 * important components pointing to the system directories,
1045 * needed for loading shared libraries. So the path better
1046 * remains intact here.
1047 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001048# ifdef HAVE_ETC_DEFAULT_LOGIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001049 read_etc_default_login(&env, &envsize, pw->pw_uid);
1050 path = child_get_env(env, "PATH");
Darren Tuckere1a790d2003-09-16 11:52:19 +10001051# endif /* HAVE_ETC_DEFAULT_LOGIN */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001052 if (path == NULL || *path == '\0') {
1053 child_set_env(&env, &envsize, "PATH",
1054 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1055 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001056# endif /* HAVE_CYGWIN */
1057#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001058
Damien Miller2ff2e192019-02-08 14:53:35 +11001059 if (!options.use_pam) {
1060 snprintf(buf, sizeof buf, "%.200s/%.50s",
1061 _PATH_MAILDIR, pw->pw_name);
1062 child_set_env(&env, &envsize, "MAIL", buf);
1063 }
Damien Millerb38eff82000-04-01 11:09:21 +10001064
djm@openbsd.org83b58182016-08-19 03:18:06 +00001065 /* Normal systems set SHELL by default. */
1066 child_set_env(&env, &envsize, "SHELL", shell);
1067
Damien Millerb38eff82000-04-01 11:09:21 +10001068 if (getenv("TZ"))
1069 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001070 if (s->term)
1071 child_set_env(&env, &envsize, "TERM", s->term);
1072 if (s->display)
1073 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Millerb38eff82000-04-01 11:09:21 +10001074
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001075 /*
1076 * Since we clear KRB5CCNAME at startup, if it's set now then it
1077 * must have been set by a native authentication method (eg AIX or
1078 * SIA), so copy it to the child.
1079 */
1080 {
1081 char *cp;
1082
1083 if ((cp = getenv("KRB5CCNAME")) != NULL)
1084 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1085 }
1086
Damien Millerb38eff82000-04-01 11:09:21 +10001087#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001088 {
1089 char *cp;
1090
1091 if ((cp = getenv("AUTHSTATE")) != NULL)
1092 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Darren Tucker3deb56f2018-07-05 13:32:01 +10001093 read_environment_file(&env, &envsize, "/etc/environment",
1094 options.permit_user_env_whitelist);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001095 }
Damien Millerb38eff82000-04-01 11:09:21 +10001096#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001097#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001098 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001099 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001100 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001101#endif
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001102 if (auth_sock_name != NULL)
1103 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1104 auth_sock_name);
1105
1106
1107 /* Set custom environment options from pubkey authentication. */
1108 if (options.permit_user_env) {
1109 for (n = 0 ; n < auth_opts->nenv; n++) {
1110 ocp = xstrdup(auth_opts->env[n]);
1111 cp = strchr(ocp, '=');
1112 if (*cp == '=') {
1113 *cp = '\0';
djm@openbsd.org95344c22018-07-03 10:59:35 +00001114 /* Apply PermitUserEnvironment whitelist */
1115 if (options.permit_user_env_whitelist == NULL ||
1116 match_pattern_list(ocp,
1117 options.permit_user_env_whitelist, 0) == 1)
1118 child_set_env(&env, &envsize,
1119 ocp, cp + 1);
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001120 }
1121 free(ocp);
1122 }
1123 }
1124
1125 /* read $HOME/.ssh/environment. */
1126 if (options.permit_user_env) {
1127 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1128 pw->pw_dir);
djm@openbsd.org95344c22018-07-03 10:59:35 +00001129 read_environment_file(&env, &envsize, buf,
1130 options.permit_user_env_whitelist);
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001131 }
1132
Damien Millerb38eff82000-04-01 11:09:21 +10001133#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001134 /*
1135 * Pull in any environment variables that may have
1136 * been set by PAM.
1137 */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001138 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001139 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001140
Damien Miller94bc1e72017-07-28 14:50:59 +10001141 /*
Damien Miller8a22ffa2018-12-07 15:41:16 +11001142 * Don't allow PAM-internal env vars to leak
1143 * back into the session environment.
Damien Miller94bc1e72017-07-28 14:50:59 +10001144 */
Damien Miller8a22ffa2018-12-07 15:41:16 +11001145#define PAM_ENV_BLACKLIST "SSH_AUTH_INFO*,SSH_CONNECTION*"
Damien Millerc756e9b2003-11-17 21:41:42 +11001146 p = fetch_pam_child_environment();
Damien Miller8a22ffa2018-12-07 15:41:16 +11001147 copy_environment_blacklist(p, &env, &envsize,
1148 PAM_ENV_BLACKLIST);
Damien Millerc756e9b2003-11-17 21:41:42 +11001149 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001150
Damien Millerc756e9b2003-11-17 21:41:42 +11001151 p = fetch_pam_environment();
Damien Miller8a22ffa2018-12-07 15:41:16 +11001152 copy_environment_blacklist(p, &env, &envsize,
1153 PAM_ENV_BLACKLIST);
Kevin Steves38b050a2002-07-23 00:44:07 +00001154 free_pam_environment(p);
1155 }
Damien Millerb38eff82000-04-01 11:09:21 +10001156#endif /* USE_PAM */
1157
djm@openbsd.org28013752018-06-09 03:03:10 +00001158 /* Environment specified by admin */
1159 for (i = 0; i < options.num_setenv; i++) {
1160 cp = xstrdup(options.setenv[i]);
1161 if ((value = strchr(cp, '=')) == NULL) {
1162 /* shouldn't happen; vars are checked in servconf.c */
1163 fatal("Invalid config SetEnv: %s", options.setenv[i]);
1164 }
1165 *value++ = '\0';
1166 child_set_env(&env, &envsize, cp, value);
1167 }
1168
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001169 /* SSH_CLIENT deprecated */
1170 snprintf(buf, sizeof buf, "%.50s %d %d",
1171 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1172 ssh_local_port(ssh));
1173 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001174
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001175 laddr = get_local_ipaddr(ssh_packet_get_connection_in(ssh));
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001176 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1177 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1178 laddr, ssh_local_port(ssh));
1179 free(laddr);
1180 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1181
1182 if (tun_fwd_ifnames != NULL)
1183 child_set_env(&env, &envsize, "SSH_TUNNEL", tun_fwd_ifnames);
1184 if (auth_info_file != NULL)
1185 child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file);
1186 if (s->ttyfd != -1)
1187 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1188 if (original_command)
1189 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1190 original_command);
1191
Damien Millerb38eff82000-04-01 11:09:21 +10001192 if (debug_flag) {
1193 /* dump the environment */
1194 fprintf(stderr, "Environment:\n");
1195 for (i = 0; env[i]; i++)
1196 fprintf(stderr, " %.200s\n", env[i]);
1197 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001198 return env;
1199}
1200
1201/*
1202 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1203 * first in this order).
1204 */
1205static void
djm@openbsd.org7c856852018-03-03 03:15:51 +00001206do_rc_files(struct ssh *ssh, Session *s, const char *shell)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001207{
1208 FILE *f = NULL;
1209 char cmd[1024];
1210 int do_xauth;
1211 struct stat st;
1212
1213 do_xauth =
1214 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1215
Damien Millera1b48cc2008-03-27 11:02:02 +11001216 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001217 if (!s->is_subsystem && options.adm_forced_command == NULL &&
djm@openbsd.org7c856852018-03-03 03:15:51 +00001218 auth_opts->permit_user_rc && options.permit_user_rc &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001219 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001220 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1221 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1222 if (debug_flag)
1223 fprintf(stderr, "Running %s\n", cmd);
1224 f = popen(cmd, "w");
1225 if (f) {
1226 if (do_xauth)
1227 fprintf(f, "%s %s\n", s->auth_proto,
1228 s->auth_data);
1229 pclose(f);
1230 } else
1231 fprintf(stderr, "Could not run %s\n",
1232 _PATH_SSH_USER_RC);
1233 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1234 if (debug_flag)
1235 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1236 _PATH_SSH_SYSTEM_RC);
1237 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1238 if (f) {
1239 if (do_xauth)
1240 fprintf(f, "%s %s\n", s->auth_proto,
1241 s->auth_data);
1242 pclose(f);
1243 } else
1244 fprintf(stderr, "Could not run %s\n",
1245 _PATH_SSH_SYSTEM_RC);
1246 } else if (do_xauth && options.xauth_location != NULL) {
1247 /* Add authority data to .Xauthority if appropriate. */
1248 if (debug_flag) {
1249 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001250 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001251 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001252 fprintf(stderr,
1253 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001254 options.xauth_location, s->auth_display,
1255 s->auth_proto, s->auth_data);
1256 }
1257 snprintf(cmd, sizeof cmd, "%s -q -",
1258 options.xauth_location);
1259 f = popen(cmd, "w");
1260 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001261 fprintf(f, "remove %s\n",
1262 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001263 fprintf(f, "add %s %s %s\n",
1264 s->auth_display, s->auth_proto,
1265 s->auth_data);
1266 pclose(f);
1267 } else {
1268 fprintf(stderr, "Could not run %s\n",
1269 cmd);
1270 }
1271 }
1272}
1273
1274static void
1275do_nologin(struct passwd *pw)
1276{
1277 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001278 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1279 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001280
1281#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001282 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001283 return;
1284 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001285#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001286 if (pw->pw_uid == 0)
1287 return;
1288 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001289#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001290 if (stat(nl, &sb) == -1) {
1291 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001292 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001293 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001294 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001295
1296 /* /etc/nologin exists. Print its contents if we can and exit. */
1297 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1298 if ((f = fopen(nl, "r")) != NULL) {
Darren Tucker19edfd42018-02-13 08:25:46 +11001299 while (fgets(buf, sizeof(buf), f))
1300 fputs(buf, stderr);
1301 fclose(f);
1302 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001303 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001304}
1305
Damien Millerd8cb1f12008-02-10 22:40:12 +11001306/*
1307 * Chroot into a directory after checking it for safety: all path components
1308 * must be root-owned directories with strict permissions.
1309 */
1310static void
1311safely_chroot(const char *path, uid_t uid)
1312{
1313 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001314 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001315 struct stat st;
1316
djm@openbsd.org2a358622018-11-16 03:26:01 +00001317 if (!path_absolute(path))
Damien Millerd8cb1f12008-02-10 22:40:12 +11001318 fatal("chroot path does not begin at root");
1319 if (strlen(path) >= sizeof(component))
1320 fatal("chroot path too long");
1321
1322 /*
1323 * Descend the path, checking that each component is a
1324 * root-owned directory with strict permissions.
1325 */
1326 for (cp = path; cp != NULL;) {
1327 if ((cp = strchr(cp, '/')) == NULL)
1328 strlcpy(component, path, sizeof(component));
1329 else {
1330 cp++;
1331 memcpy(component, path, cp - path);
1332 component[cp - path] = '\0';
1333 }
1334
1335 debug3("%s: checking '%s'", __func__, component);
1336
1337 if (stat(component, &st) != 0)
1338 fatal("%s: stat(\"%s\"): %s", __func__,
1339 component, strerror(errno));
1340 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1341 fatal("bad ownership or modes for chroot "
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001342 "directory %s\"%s\"",
Damien Millerd8cb1f12008-02-10 22:40:12 +11001343 cp == NULL ? "" : "component ", component);
1344 if (!S_ISDIR(st.st_mode))
1345 fatal("chroot path %s\"%s\" is not a directory",
1346 cp == NULL ? "" : "component ", component);
1347
1348 }
1349
1350 if (chdir(path) == -1)
1351 fatal("Unable to chdir to chroot path \"%s\": "
1352 "%s", path, strerror(errno));
1353 if (chroot(path) == -1)
1354 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1355 if (chdir("/") == -1)
1356 fatal("%s: chdir(/) after chroot: %s",
1357 __func__, strerror(errno));
1358 verbose("Changed root directory to \"%s\"", path);
1359}
1360
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001361/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001363do_setusercontext(struct passwd *pw)
1364{
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001365 char uidstr[32], *chroot_path, *tmp;
Damien Miller54e37732008-02-10 22:48:55 +11001366
Darren Tucker97528352010-11-05 12:03:05 +11001367 platform_setusercontext(pw);
1368
Darren Tuckerb12fe272010-11-05 14:47:01 +11001369 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001370#ifdef HAVE_LOGIN_CAP
1371 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001372 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001373 perror("unable to set user context");
1374 exit(1);
1375 }
1376#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001377 if (setlogin(pw->pw_name) < 0)
1378 error("setlogin failed: %s", strerror(errno));
1379 if (setgid(pw->pw_gid) < 0) {
1380 perror("setgid");
1381 exit(1);
1382 }
1383 /* Initialize the group list. */
1384 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1385 perror("initgroups");
1386 exit(1);
1387 }
1388 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001389#endif
1390
Darren Tucker920612e2010-11-05 12:36:15 +11001391 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001392
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001393 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001394 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001395 tmp = tilde_expand_filename(options.chroot_directory,
1396 pw->pw_uid);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001397 snprintf(uidstr, sizeof(uidstr), "%llu",
1398 (unsigned long long)pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001399 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001400 "u", pw->pw_name, "U", uidstr, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001401 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001402 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001403 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001404 /* Make sure we don't attempt to chroot again */
1405 free(options.chroot_directory);
1406 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001407 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001408 }
1409
1410#ifdef HAVE_LOGIN_CAP
1411 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1412 perror("unable to set user context (setuser)");
1413 exit(1);
1414 }
Damien Miller58528402013-03-15 11:22:37 +11001415 /*
1416 * FreeBSD's setusercontext() will not apply the user's
1417 * own umask setting unless running with the user's UID.
1418 */
1419 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001420#else
Tim Rice9464ba62014-01-20 17:59:28 -08001421# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001422 /*
1423 * In a chroot environment, the set_id() will always fail;
1424 * typically because of the lack of necessary authentication
1425 * services and runtime such as ./usr/lib/libiaf.so,
1426 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1427 * internal sftp chroot case. We'll lose auditing and ACLs but
1428 * permanently_set_uid will take care of the rest.
1429 */
1430 if (!in_chroot && set_id(pw->pw_name) != 0)
1431 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001432# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001433 /* Permanently switch to the desired uid. */
1434 permanently_set_uid(pw);
1435#endif
Damien Millera56086b2013-04-23 15:24:18 +10001436 } else if (options.chroot_directory != NULL &&
1437 strcasecmp(options.chroot_directory, "none") != 0) {
1438 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001439 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001440
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001441 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1442 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1443}
1444
Ben Lindstrom08105192002-03-22 02:50:06 +00001445static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001446do_pwchange(Session *s)
1447{
Darren Tucker09991742004-07-17 17:05:14 +10001448 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001449 fprintf(stderr, "WARNING: Your password has expired.\n");
1450 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001451 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001452 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001453#ifdef WITH_SELINUX
1454 setexeccon(NULL);
1455#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001456#ifdef PASSWD_NEEDS_USERNAME
1457 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1458 (char *)NULL);
1459#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001460 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001461#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001462 perror("passwd");
1463 } else {
1464 fprintf(stderr,
1465 "Password change required but no TTY available.\n");
1466 }
1467 exit(1);
1468}
1469
1470static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001471child_close_fds(struct ssh *ssh)
Darren Tucker23bc8d02004-02-06 16:24:31 +11001472{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001473 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001474
djm@openbsd.org141efe42015-01-14 20:05:27 +00001475 if (auth_sock != -1) {
1476 close(auth_sock);
1477 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001478 }
1479
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001480 if (ssh_packet_get_connection_in(ssh) ==
1481 ssh_packet_get_connection_out(ssh))
1482 close(ssh_packet_get_connection_in(ssh));
Darren Tucker23bc8d02004-02-06 16:24:31 +11001483 else {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001484 close(ssh_packet_get_connection_in(ssh));
1485 close(ssh_packet_get_connection_out(ssh));
Darren Tucker23bc8d02004-02-06 16:24:31 +11001486 }
1487 /*
1488 * Close all descriptors related to channels. They will still remain
1489 * open in the parent.
1490 */
1491 /* XXX better use close-on-exec? -markus */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001492 channel_close_all(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001493
1494 /*
1495 * Close any extra file descriptors. Note that there may still be
1496 * descriptors left by system functions. They will be closed later.
1497 */
1498 endpwent();
1499
1500 /*
Damien Miller788f2122005-11-05 15:14:59 +11001501 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001502 * hanging around in clients. Note that we want to do this after
1503 * initgroups, because at least on Solaris 2.3 it leaves file
1504 * descriptors open.
1505 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001506 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001507}
1508
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001509/*
1510 * Performs common processing for the child, such as setting up the
1511 * environment, closing extra file descriptors, setting the user and group
1512 * ids, and executing the command or shell.
1513 */
Damien Millerdfc24252008-02-10 22:29:40 +11001514#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001515void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001516do_child(struct ssh *ssh, Session *s, const char *command)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001517{
1518 extern char **environ;
djm@openbsd.org318e4f82019-02-10 11:10:57 +00001519 char **env, *argv[ARGV_MAX], remote_id[512];
djm@openbsd.org83b58182016-08-19 03:18:06 +00001520 const char *shell, *shell0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001521 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001522 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001523
djm@openbsd.org318e4f82019-02-10 11:10:57 +00001524 sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
1525
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001526 /* remove hostkey from the child's memory */
1527 destroy_sensitive_data();
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001528 ssh_packet_clear_keys(ssh);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001529
Darren Tucker23bc8d02004-02-06 16:24:31 +11001530 /* Force a password change */
1531 if (s->authctxt->force_pwchange) {
1532 do_setusercontext(pw);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001533 child_close_fds(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001534 do_pwchange(s);
1535 exit(1);
1536 }
1537
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001538 /*
1539 * Login(1) does this as well, and it needs uid 0 for the "-h"
1540 * switch, so we let login(1) to this for us.
1541 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001542#ifdef HAVE_OSF_SIA
djm@openbsd.org83b58182016-08-19 03:18:06 +00001543 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1544 if (!check_quietlogin(s, command))
1545 do_motd();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001546#else /* HAVE_OSF_SIA */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001547 /* When PAM is enabled we rely on it to do the nologin check */
1548 if (!options.use_pam)
1549 do_nologin(pw);
1550 do_setusercontext(pw);
1551 /*
1552 * PAM session modules in do_setusercontext may have
1553 * generated messages, so if this in an interactive
1554 * login then display them too.
1555 */
1556 if (!check_quietlogin(s, command))
1557 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001558#endif /* HAVE_OSF_SIA */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001559
Darren Tucker69687f42004-09-11 22:17:26 +10001560#ifdef USE_PAM
djm@openbsd.org83b58182016-08-19 03:18:06 +00001561 if (options.use_pam && !is_pam_session_open()) {
Darren Tucker48554152005-04-21 19:50:55 +10001562 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001563 display_loginmsg();
1564 exit(254);
1565 }
1566#endif
1567
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001568 /*
1569 * Get the shell from the password data. An empty shell field is
1570 * legal, and means /bin/sh.
1571 */
1572 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001573
1574 /*
1575 * Make sure $SHELL points to the shell from the password file,
1576 * even if shell is overridden from login.conf
1577 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001578 env = do_setup_env(ssh, s, shell);
Ben Lindstrom46767602002-12-23 02:26:08 +00001579
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001580#ifdef HAVE_LOGIN_CAP
1581 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1582#endif
1583
Damien Millerb38eff82000-04-01 11:09:21 +10001584 /*
1585 * Close the connection descriptors; note that this is the child, and
1586 * the server will still have the socket open, and it is important
1587 * that we do not shutdown it. Note that the descriptors cannot be
1588 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001589 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001590 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001591 child_close_fds(ssh);
Damien Millerb38eff82000-04-01 11:09:21 +10001592
Damien Millerb38eff82000-04-01 11:09:21 +10001593 /*
Damien Miller05eda432002-02-10 18:32:28 +11001594 * Must take new environment into use so that .ssh/rc,
1595 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001596 */
1597 environ = env;
1598
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001599#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001600 /*
1601 * At this point, we check to see if AFS is active and if we have
1602 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1603 * if we can (and need to) extend the ticket into an AFS token. If
1604 * we don't do this, we run into potential problems if the user's
1605 * home directory is in AFS and it's not world-readable.
1606 */
1607
1608 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001609 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001610 char cell[64];
1611
1612 debug("Getting AFS token");
1613
1614 k_setpag();
1615
1616 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1617 krb5_afslog(s->authctxt->krb5_ctx,
1618 s->authctxt->krb5_fwd_ccache, cell, NULL);
1619
1620 krb5_afslog_home(s->authctxt->krb5_ctx,
1621 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1622 }
1623#endif
1624
Damien Miller788f2122005-11-05 15:14:59 +11001625 /* Change current directory to the user's home directory. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001626 if (chdir(pw->pw_dir) == -1) {
Damien Miller6051c942008-06-16 07:53:16 +10001627 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001628#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001629 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001630#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001631 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001632 fprintf(stderr, "Could not chdir to home "
1633 "directory %s: %s\n", pw->pw_dir,
1634 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001635 }
Damien Miller6051c942008-06-16 07:53:16 +10001636 if (r)
1637 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001638 }
1639
Damien Millera1939002008-03-15 17:27:58 +11001640 closefrom(STDERR_FILENO + 1);
1641
djm@openbsd.org7c856852018-03-03 03:15:51 +00001642 do_rc_files(ssh, s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001643
1644 /* restore SIGPIPE for child */
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001645 ssh_signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001646
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001647 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
djm@openbsd.org318e4f82019-02-10 11:10:57 +00001648 error("Connection from %s: refusing non-sftp session",
1649 remote_id);
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001650 printf("This service allows sftp connections only.\n");
1651 fflush(NULL);
1652 exit(1);
1653 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001654 extern int optind, optreset;
1655 int i;
1656 char *p, *args;
1657
Darren Tuckerac46a912009-06-21 17:55:23 +10001658 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001659 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001660 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1661 if (i < ARGV_MAX - 1)
1662 argv[i++] = p;
1663 argv[i] = NULL;
1664 optind = optreset = 1;
1665 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001666#ifdef WITH_SELINUX
1667 ssh_selinux_change_context("sftpd_t");
1668#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001669 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001670 }
1671
Darren Tucker695ed392009-10-07 09:02:18 +11001672 fflush(NULL);
1673
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001674 /* Get the last component of the shell name. */
1675 if ((shell0 = strrchr(shell, '/')) != NULL)
1676 shell0++;
1677 else
1678 shell0 = shell;
1679
Damien Millerb38eff82000-04-01 11:09:21 +10001680 /*
1681 * If we have no command, execute the shell. In this case, the shell
1682 * name to be passed in argv[0] is preceded by '-' to indicate that
1683 * this is a login shell.
1684 */
1685 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001686 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001687
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001688 /* Start the shell. Set initial character to '-'. */
1689 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001690
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001691 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1692 >= sizeof(argv0) - 1) {
1693 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001694 perror(shell);
1695 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001696 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001697
1698 /* Execute the shell. */
1699 argv[0] = argv0;
1700 argv[1] = NULL;
1701 execve(shell, argv, env);
1702
1703 /* Executing the shell failed. */
1704 perror(shell);
1705 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001706 }
1707 /*
1708 * Execute the command using the user's shell. This uses the -c
1709 * option to execute the command.
1710 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001711 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001712 argv[1] = "-c";
1713 argv[2] = (char *) command;
1714 argv[3] = NULL;
1715 execve(shell, argv, env);
1716 perror(shell);
1717 exit(1);
1718}
1719
Damien Miller7207f642008-05-19 15:34:50 +10001720void
1721session_unused(int id)
1722{
1723 debug3("%s: session id %d unused", __func__, id);
1724 if (id >= options.max_sessions ||
1725 id >= sessions_nalloc) {
1726 fatal("%s: insane session id %d (max %d nalloc %d)",
1727 __func__, id, options.max_sessions, sessions_nalloc);
1728 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001729 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001730 sessions[id].self = id;
1731 sessions[id].used = 0;
1732 sessions[id].chanid = -1;
1733 sessions[id].ptyfd = -1;
1734 sessions[id].ttyfd = -1;
1735 sessions[id].ptymaster = -1;
1736 sessions[id].x11_chanids = NULL;
1737 sessions[id].next_unused = sessions_first_unused;
1738 sessions_first_unused = id;
1739}
1740
Damien Millerb38eff82000-04-01 11:09:21 +10001741Session *
1742session_new(void)
1743{
Damien Miller7207f642008-05-19 15:34:50 +10001744 Session *s, *tmp;
1745
1746 if (sessions_first_unused == -1) {
1747 if (sessions_nalloc >= options.max_sessions)
1748 return NULL;
1749 debug2("%s: allocate (allocated %d max %d)",
1750 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001751 tmp = xrecallocarray(sessions, sessions_nalloc,
1752 sessions_nalloc + 1, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001753 if (tmp == NULL) {
1754 error("%s: cannot allocate %d sessions",
1755 __func__, sessions_nalloc + 1);
1756 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001757 }
Damien Miller7207f642008-05-19 15:34:50 +10001758 sessions = tmp;
1759 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001760 }
Damien Miller7207f642008-05-19 15:34:50 +10001761
1762 if (sessions_first_unused >= sessions_nalloc ||
1763 sessions_first_unused < 0) {
1764 fatal("%s: insane first_unused %d max %d nalloc %d",
1765 __func__, sessions_first_unused, options.max_sessions,
1766 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001767 }
Damien Miller7207f642008-05-19 15:34:50 +10001768
1769 s = &sessions[sessions_first_unused];
1770 if (s->used) {
1771 fatal("%s: session %d already used",
1772 __func__, sessions_first_unused);
1773 }
1774 sessions_first_unused = s->next_unused;
1775 s->used = 1;
1776 s->next_unused = -1;
1777 debug("session_new: session %d", s->self);
1778
1779 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001780}
1781
Ben Lindstrombba81212001-06-25 05:01:22 +00001782static void
Damien Millerb38eff82000-04-01 11:09:21 +10001783session_dump(void)
1784{
1785 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001786 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001787 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001788
1789 debug("dump: used %d next_unused %d session %d %p "
1790 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001791 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001792 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001793 s->self,
1794 s,
1795 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001796 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001797 }
1798}
1799
Damien Millerefb4afe2000-04-12 18:45:05 +10001800int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001801session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001802{
1803 Session *s = session_new();
1804 debug("session_open: channel %d", chanid);
1805 if (s == NULL) {
1806 error("no more sessions");
1807 return 0;
1808 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001809 s->authctxt = authctxt;
1810 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001811 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001812 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001813 debug("session_open: session %d: link with channel %d", s->self, chanid);
1814 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001815 return 1;
1816}
1817
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001818Session *
1819session_by_tty(char *tty)
1820{
1821 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001822 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001823 Session *s = &sessions[i];
1824 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1825 debug("session_by_tty: session %d tty %s", i, tty);
1826 return s;
1827 }
1828 }
1829 debug("session_by_tty: unknown tty %.100s", tty);
1830 session_dump();
1831 return NULL;
1832}
1833
Ben Lindstrombba81212001-06-25 05:01:22 +00001834static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001835session_by_channel(int id)
1836{
1837 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001838 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001839 Session *s = &sessions[i];
1840 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001841 debug("session_by_channel: session %d channel %d",
1842 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001843 return s;
1844 }
1845 }
1846 debug("session_by_channel: unknown channel %d", id);
1847 session_dump();
1848 return NULL;
1849}
1850
Ben Lindstrombba81212001-06-25 05:01:22 +00001851static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001852session_by_x11_channel(int id)
1853{
1854 int i, j;
1855
Damien Miller7207f642008-05-19 15:34:50 +10001856 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001857 Session *s = &sessions[i];
1858
1859 if (s->x11_chanids == NULL || !s->used)
1860 continue;
1861 for (j = 0; s->x11_chanids[j] != -1; j++) {
1862 if (s->x11_chanids[j] == id) {
1863 debug("session_by_x11_channel: session %d "
1864 "channel %d", s->self, id);
1865 return s;
1866 }
1867 }
1868 }
1869 debug("session_by_x11_channel: unknown channel %d", id);
1870 session_dump();
1871 return NULL;
1872}
1873
1874static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001875session_by_pid(pid_t pid)
1876{
1877 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001878 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001879 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001880 Session *s = &sessions[i];
1881 if (s->used && s->pid == pid)
1882 return s;
1883 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001884 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001885 session_dump();
1886 return NULL;
1887}
1888
Ben Lindstrombba81212001-06-25 05:01:22 +00001889static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001890session_window_change_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001891{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001892 int r;
1893
1894 if ((r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
1895 (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
1896 (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
1897 (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0 ||
1898 (r = sshpkt_get_end(ssh)) != 0)
1899 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10001900 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1901 return 1;
1902}
1903
Ben Lindstrombba81212001-06-25 05:01:22 +00001904static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001905session_pty_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001906{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001907 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001908
djm@openbsd.org7c856852018-03-03 03:15:51 +00001909 if (!auth_opts->permit_pty_flag || !options.permit_tty) {
1910 debug("Allocating a pty not permitted for this connection.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001911 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001912 }
1913 if (s->ttyfd != -1) {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001914 ssh_packet_disconnect(ssh, "Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001915 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001916 }
1917
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001918 if ((r = sshpkt_get_cstring(ssh, &s->term, NULL)) != 0 ||
1919 (r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
1920 (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
1921 (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
1922 (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0)
1923 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10001924
1925 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001926 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001927 s->term = NULL;
1928 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001929
Damien Millerefb4afe2000-04-12 18:45:05 +10001930 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001931 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10001932 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1933 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10001934 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001935 s->term = NULL;
1936 s->ptyfd = -1;
1937 s->ttyfd = -1;
1938 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001939 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001940 }
1941 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001942
markus@openbsd.org89dd6152018-07-09 21:20:26 +00001943 ssh_tty_parse_modes(ssh, s->ttyfd);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001944
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001945 if ((r = sshpkt_get_end(ssh)) != 0)
1946 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
1947
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001948 if (!use_privsep)
1949 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001950
1951 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001952 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1953
Damien Millere247cc42000-05-07 12:03:14 +10001954 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001955 return 1;
1956}
1957
Ben Lindstrombba81212001-06-25 05:01:22 +00001958static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001959session_subsystem_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001960{
Damien Millerae452462001-10-10 15:08:06 +10001961 struct stat st;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001962 int r, success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11001963 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10001964 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001965
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001966 if ((r = sshpkt_get_cstring(ssh, &s->subsys, NULL)) != 0 ||
1967 (r = sshpkt_get_end(ssh)) != 0)
1968 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Miller71df7522013-10-15 12:12:02 +11001969 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10001970 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10001971
Damien Millerf6d9e222000-06-18 14:50:44 +10001972 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11001973 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001974 prog = options.subsystem_command[i];
1975 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001976 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11001977 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001978 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11001979 } else {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001980 if (stat(prog, &st) == -1)
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001981 debug("subsystem: cannot stat %s: %s",
1982 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001983 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001984 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001985 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001986 success = do_exec(ssh, s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11001987 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001988 }
1989 }
1990
1991 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11001992 logit("subsystem request for %.100s by user %s failed, "
1993 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10001994
Damien Millerbd483e72000-04-30 10:00:53 +10001995 return success;
1996}
1997
Ben Lindstrombba81212001-06-25 05:01:22 +00001998static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001999session_x11_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10002000{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002001 int r, success;
2002 u_char single_connection = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002003
Damien Miller2b9b0452005-07-17 17:19:24 +10002004 if (s->auth_proto != NULL || s->auth_data != NULL) {
2005 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002006 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002007 return 0;
2008 }
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002009 if ((r = sshpkt_get_u8(ssh, &single_connection)) != 0 ||
2010 (r = sshpkt_get_cstring(ssh, &s->auth_proto, NULL)) != 0 ||
2011 (r = sshpkt_get_cstring(ssh, &s->auth_data, NULL)) != 0 ||
2012 (r = sshpkt_get_u32(ssh, &s->screen)) != 0 ||
2013 (r = sshpkt_get_end(ssh)) != 0)
2014 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2015
2016 s->single_connection = single_connection;
Damien Millerbd483e72000-04-30 10:00:53 +10002017
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00002018 if (xauth_valid_string(s->auth_proto) &&
2019 xauth_valid_string(s->auth_data))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002020 success = session_setup_x11fwd(ssh, s);
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00002021 else {
2022 success = 0;
2023 error("Invalid X11 forwarding data");
2024 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002025 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002026 free(s->auth_proto);
2027 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002028 s->auth_proto = NULL;
2029 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002030 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002031 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002032}
2033
Ben Lindstrombba81212001-06-25 05:01:22 +00002034static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002035session_shell_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10002036{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002037 int r;
2038
2039 if ((r = sshpkt_get_end(ssh)) != 0)
2040 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002041 return do_exec(ssh, s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002042}
2043
Ben Lindstrombba81212001-06-25 05:01:22 +00002044static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002045session_exec_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10002046{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002047 u_int success;
2048 int r;
2049 char *command = NULL;
Damien Miller7207f642008-05-19 15:34:50 +10002050
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002051 if ((r = sshpkt_get_cstring(ssh, &command, NULL)) != 0 ||
2052 (r = sshpkt_get_end(ssh)) != 0)
2053 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2054
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002055 success = do_exec(ssh, s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002056 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002057 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002058}
2059
Ben Lindstrombba81212001-06-25 05:01:22 +00002060static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002061session_break_req(struct ssh *ssh, Session *s)
Damien Miller54c45982003-05-15 10:20:13 +10002062{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002063 int r;
Damien Miller54c45982003-05-15 10:20:13 +10002064
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002065 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* ignore */
2066 (r = sshpkt_get_end(ssh)) != 0)
2067 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Miller54c45982003-05-15 10:20:13 +10002068
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002069 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) == -1)
Damien Miller54c45982003-05-15 10:20:13 +10002070 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002071 return 1;
2072}
2073
2074static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002075session_env_req(struct ssh *ssh, Session *s)
Darren Tucker46bc0752004-05-02 22:11:30 +10002076{
2077 char *name, *val;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002078 u_int i;
2079 int r;
Darren Tucker46bc0752004-05-02 22:11:30 +10002080
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002081 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
2082 (r = sshpkt_get_cstring(ssh, &val, NULL)) != 0 ||
2083 (r = sshpkt_get_end(ssh)) != 0)
2084 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Darren Tucker46bc0752004-05-02 22:11:30 +10002085
2086 /* Don't set too many environment variables */
2087 if (s->num_env > 128) {
2088 debug2("Ignoring env request %s: too many env vars", name);
2089 goto fail;
2090 }
2091
2092 for (i = 0; i < options.num_accept_env; i++) {
2093 if (match_pattern(name, options.accept_env[i])) {
2094 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002095 s->env = xrecallocarray(s->env, s->num_env,
2096 s->num_env + 1, sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002097 s->env[s->num_env].name = name;
2098 s->env[s->num_env].val = val;
2099 s->num_env++;
2100 return (1);
2101 }
2102 }
2103 debug2("Ignoring env request %s: disallowed name", name);
2104
2105 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002106 free(name);
2107 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002108 return (0);
2109}
2110
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002111/*
2112 * Conversion of signals from ssh channel request names.
2113 * Subset of signals from RFC 4254 section 6.10C, with SIGINFO as
2114 * local extension.
2115 */
2116static int
2117name2sig(char *name)
2118{
2119#define SSH_SIG(x) if (strcmp(name, #x) == 0) return SIG ## x
2120 SSH_SIG(HUP);
2121 SSH_SIG(INT);
2122 SSH_SIG(KILL);
2123 SSH_SIG(QUIT);
2124 SSH_SIG(TERM);
2125 SSH_SIG(USR1);
2126 SSH_SIG(USR2);
2127#undef SSH_SIG
Damien Millerff3a4112018-10-02 22:49:40 +10002128#ifdef SIGINFO
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002129 if (strcmp(name, "INFO@openssh.com") == 0)
2130 return SIGINFO;
Damien Millerff3a4112018-10-02 22:49:40 +10002131#endif
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002132 return -1;
2133}
2134
2135static int
2136session_signal_req(struct ssh *ssh, Session *s)
2137{
2138 char *signame = NULL;
2139 int r, sig, success = 0;
2140
2141 if ((r = sshpkt_get_cstring(ssh, &signame, NULL)) != 0 ||
2142 (r = sshpkt_get_end(ssh)) != 0) {
2143 error("%s: parse packet: %s", __func__, ssh_err(r));
2144 goto out;
2145 }
2146 if ((sig = name2sig(signame)) == -1) {
2147 error("%s: unsupported signal \"%s\"", __func__, signame);
2148 goto out;
2149 }
2150 if (s->pid <= 0) {
2151 error("%s: no pid for session %d", __func__, s->self);
2152 goto out;
2153 }
2154 if (s->forced || s->is_subsystem) {
2155 error("%s: refusing to send signal %s to %s session", __func__,
2156 signame, s->forced ? "forced-command" : "subsystem");
2157 goto out;
2158 }
2159 if (!use_privsep || mm_is_monitor()) {
2160 error("%s: session signalling requires privilege separation",
2161 __func__);
2162 goto out;
2163 }
2164
2165 debug("%s: signal %s, killpg(%ld, %d)", __func__, signame,
2166 (long)s->pid, sig);
2167 temporarily_use_uid(s->pw);
2168 r = killpg(s->pid, sig);
2169 restore_uid();
2170 if (r != 0) {
2171 error("%s: killpg(%ld, %d): %s", __func__, (long)s->pid,
2172 sig, strerror(errno));
2173 goto out;
2174 }
2175
2176 /* success */
2177 success = 1;
2178 out:
2179 free(signame);
2180 return success;
2181}
2182
Darren Tucker46bc0752004-05-02 22:11:30 +10002183static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002184session_auth_agent_req(struct ssh *ssh, Session *s)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002185{
2186 static int called = 0;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002187 int r;
djm@openbsd.org7c856852018-03-03 03:15:51 +00002188
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002189 if ((r = sshpkt_get_end(ssh)) != 0)
2190 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
djm@openbsd.org7c856852018-03-03 03:15:51 +00002191 if (!auth_opts->permit_agent_forwarding_flag ||
2192 !options.allow_agent_forwarding) {
2193 debug("%s: agent forwarding disabled", __func__);
Ben Lindstrom14920292000-11-21 21:24:55 +00002194 return 0;
2195 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002196 if (called) {
2197 return 0;
2198 } else {
2199 called = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002200 return auth_input_request_forwarding(ssh, s->pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002201 }
2202}
2203
Damien Millerc7ef63d2002-02-05 12:21:42 +11002204int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002205session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002206{
Damien Millerefb4afe2000-04-12 18:45:05 +10002207 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002208 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002209
Damien Millerc7ef63d2002-02-05 12:21:42 +11002210 if ((s = session_by_channel(c->self)) == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002211 logit("%s: no session %d req %.100s", __func__, c->self, rtype);
Damien Millerc7ef63d2002-02-05 12:21:42 +11002212 return 0;
2213 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002214 debug("%s: session %d req %s", __func__, s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002215
2216 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002217 * a session is in LARVAL state until a shell, a command
2218 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002219 */
2220 if (c->type == SSH_CHANNEL_LARVAL) {
2221 if (strcmp(rtype, "shell") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002222 success = session_shell_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002223 } else if (strcmp(rtype, "exec") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002224 success = session_exec_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002225 } else if (strcmp(rtype, "pty-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002226 success = session_pty_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002227 } else if (strcmp(rtype, "x11-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002228 success = session_x11_req(ssh, s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002229 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002230 success = session_auth_agent_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002231 } else if (strcmp(rtype, "subsystem") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002232 success = session_subsystem_req(ssh, s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002233 } else if (strcmp(rtype, "env") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002234 success = session_env_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002235 }
2236 }
2237 if (strcmp(rtype, "window-change") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002238 success = session_window_change_req(ssh, s);
Damien Millera6b1d162004-06-30 22:41:07 +10002239 } else if (strcmp(rtype, "break") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002240 success = session_break_req(ssh, s);
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002241 } else if (strcmp(rtype, "signal") == 0) {
2242 success = session_signal_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002243 }
Damien Millera6b1d162004-06-30 22:41:07 +10002244
Damien Millerc7ef63d2002-02-05 12:21:42 +11002245 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002246}
2247
2248void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002249session_set_fds(struct ssh *ssh, Session *s,
2250 int fdin, int fdout, int fderr, int ignore_fderr, int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002251{
Damien Millerefb4afe2000-04-12 18:45:05 +10002252 /*
2253 * now that have a child and a pipe to the child,
2254 * we can activate our channel and register the fd's
2255 */
2256 if (s->chanid == -1)
2257 fatal("no channel for session %d", s->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002258 channel_set_fds(ssh, s->chanid,
Damien Millerefb4afe2000-04-12 18:45:05 +10002259 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002260 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002261 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002262}
2263
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002264/*
2265 * Function to perform pty cleanup. Also called if we get aborted abnormally
2266 * (e.g., due to a dropped connection).
2267 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002268void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002269session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002270{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002271 if (s == NULL) {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002272 error("%s: no session", __func__);
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002273 return;
2274 }
2275 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002276 return;
2277
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002278 debug("%s: session %d release %s", __func__, s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002279
Damien Millerb38eff82000-04-01 11:09:21 +10002280 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002281 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002282 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002283
2284 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002285 if (getuid() == 0)
2286 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002287
2288 /*
2289 * Close the server side of the socket pairs. We must do this after
2290 * the pty cleanup, so that another process doesn't get this pty
2291 * while we're still cleaning up.
2292 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002293 if (s->ptymaster != -1 && close(s->ptymaster) == -1)
Damien Miller7207f642008-05-19 15:34:50 +10002294 error("close(s->ptymaster/%d): %s",
2295 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002296
2297 /* unlink pty from session */
2298 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002299}
Damien Millerefb4afe2000-04-12 18:45:05 +10002300
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002301void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002302session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002303{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002304 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002305}
2306
Damien Miller5a80bba2002-09-04 16:39:02 +10002307static char *
2308sig2name(int sig)
2309{
2310#define SSH_SIG(x) if (sig == SIG ## x) return #x
2311 SSH_SIG(ABRT);
2312 SSH_SIG(ALRM);
2313 SSH_SIG(FPE);
2314 SSH_SIG(HUP);
2315 SSH_SIG(ILL);
2316 SSH_SIG(INT);
2317 SSH_SIG(KILL);
2318 SSH_SIG(PIPE);
2319 SSH_SIG(QUIT);
2320 SSH_SIG(SEGV);
2321 SSH_SIG(TERM);
2322 SSH_SIG(USR1);
2323 SSH_SIG(USR2);
2324#undef SSH_SIG
2325 return "SIG@openssh.com";
2326}
2327
Ben Lindstrombba81212001-06-25 05:01:22 +00002328static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002329session_close_x11(struct ssh *ssh, int id)
Damien Miller2b9b0452005-07-17 17:19:24 +10002330{
2331 Channel *c;
2332
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002333 if ((c = channel_by_id(ssh, id)) == NULL) {
2334 debug("%s: x11 channel %d missing", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002335 } else {
2336 /* Detach X11 listener */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002337 debug("%s: detach x11 channel %d", __func__, id);
2338 channel_cancel_cleanup(ssh, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002339 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002340 chan_mark_dead(ssh, c);
Damien Miller2b9b0452005-07-17 17:19:24 +10002341 }
2342}
2343
2344static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002345session_close_single_x11(struct ssh *ssh, int id, void *arg)
Damien Miller2b9b0452005-07-17 17:19:24 +10002346{
2347 Session *s;
2348 u_int i;
2349
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002350 debug3("%s: channel %d", __func__, id);
2351 channel_cancel_cleanup(ssh, id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002352 if ((s = session_by_x11_channel(id)) == NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002353 fatal("%s: no x11 channel %d", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002354 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002355 debug("%s: session %d: closing channel %d",
2356 __func__, s->self, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002357 /*
2358 * The channel "id" is already closing, but make sure we
2359 * close all of its siblings.
2360 */
2361 if (s->x11_chanids[i] != id)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002362 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002363 }
Darren Tuckera627d422013-06-02 07:31:17 +10002364 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002365 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002366 free(s->display);
2367 s->display = NULL;
2368 free(s->auth_proto);
2369 s->auth_proto = NULL;
2370 free(s->auth_data);
2371 s->auth_data = NULL;
2372 free(s->auth_display);
2373 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002374}
2375
2376static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002377session_exit_message(struct ssh *ssh, Session *s, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002378{
2379 Channel *c;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002380 int r;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002381
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002382 if ((c = channel_lookup(ssh, s->chanid)) == NULL)
2383 fatal("%s: session %d: no channel %d",
2384 __func__, s->self, s->chanid);
2385 debug("%s: session %d channel %d pid %ld",
2386 __func__, s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002387
2388 if (WIFEXITED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002389 channel_request_start(ssh, s->chanid, "exit-status", 0);
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002390 if ((r = sshpkt_put_u32(ssh, WEXITSTATUS(status))) != 0 ||
2391 (r = sshpkt_send(ssh)) != 0)
2392 sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10002393 } else if (WIFSIGNALED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002394 channel_request_start(ssh, s->chanid, "exit-signal", 0);
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002395#ifndef WCOREDUMP
2396# define WCOREDUMP(x) (0)
2397#endif
2398 if ((r = sshpkt_put_cstring(ssh, sig2name(WTERMSIG(status)))) != 0 ||
2399 (r = sshpkt_put_u8(ssh, WCOREDUMP(status)? 1 : 0)) != 0 ||
2400 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2401 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2402 (r = sshpkt_send(ssh)) != 0)
2403 sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10002404 } else {
2405 /* Some weird exit cause. Just exit. */
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002406 ssh_packet_disconnect(ssh, "wait returned status %04x.", status);
Damien Millerefb4afe2000-04-12 18:45:05 +10002407 }
2408
2409 /* disconnect channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002410 debug("%s: release channel %d", __func__, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002411
2412 /*
2413 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002414 * the channel gets EOF. The session will be then be closed
djm@openbsd.orgd081f012020-03-13 03:17:07 +00002415 * by session_close_by_channel when the child sessions close their fds.
Damien Miller39eda6e2005-11-05 14:52:50 +11002416 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002417 channel_register_cleanup(ssh, c->self, session_close_by_channel, 1);
Damien Miller39eda6e2005-11-05 14:52:50 +11002418
Damien Miller166fca82000-04-20 07:42:21 +10002419 /*
2420 * emulate a write failure with 'chan_write_failed', nobody will be
2421 * interested in data we write.
2422 * Note that we must not call 'chan_read_failed', since there could
2423 * be some more data waiting in the pipe.
2424 */
Damien Millerbd483e72000-04-30 10:00:53 +10002425 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002426 chan_write_failed(ssh, c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002427}
2428
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002429void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002430session_close(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002431{
Damien Millereccb9de2005-06-17 12:59:34 +10002432 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002433
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002434 verbose("Close session: user %s from %.200s port %d id %d",
2435 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002436 ssh_remote_ipaddr(ssh),
2437 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002438 s->self);
2439
Darren Tucker3e33cec2003-10-02 16:12:36 +10002440 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002441 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002442 free(s->term);
2443 free(s->display);
2444 free(s->x11_chanids);
2445 free(s->auth_display);
2446 free(s->auth_data);
2447 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002448 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002449 if (s->env != NULL) {
2450 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002451 free(s->env[i].name);
2452 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002453 }
Darren Tuckera627d422013-06-02 07:31:17 +10002454 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002455 }
Damien Millere247cc42000-05-07 12:03:14 +10002456 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002457 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002458}
2459
2460void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002461session_close_by_pid(struct ssh *ssh, pid_t pid, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002462{
2463 Session *s = session_by_pid(pid);
2464 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002465 debug("%s: no session for pid %ld", __func__, (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002466 return;
2467 }
2468 if (s->chanid != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002469 session_exit_message(ssh, s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002470 if (s->ttyfd != -1)
2471 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002472 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002473}
2474
2475/*
2476 * this is called when a channel dies before
2477 * the session 'child' itself dies
2478 */
2479void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002480session_close_by_channel(struct ssh *ssh, int id, void *arg)
Damien Millerefb4afe2000-04-12 18:45:05 +10002481{
2482 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002483 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002484
Damien Millerefb4afe2000-04-12 18:45:05 +10002485 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002486 debug("%s: no session for id %d", __func__, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002487 return;
2488 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002489 debug("%s: channel %d child %ld", __func__, id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002490 if (s->pid != 0) {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002491 debug("%s: channel %d: has child, ttyfd %d",
2492 __func__, id, s->ttyfd);
Damien Miller0585d512001-10-12 11:35:50 +10002493 /*
2494 * delay detach of session, but release pty, since
2495 * the fd's to the child are already closed
2496 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002497 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002498 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002499 return;
2500 }
2501 /* detach by removing callback */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002502 channel_cancel_cleanup(ssh, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002503
2504 /* Close any X11 listeners associated with this session */
2505 if (s->x11_chanids != NULL) {
2506 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002507 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller39eda6e2005-11-05 14:52:50 +11002508 s->x11_chanids[i] = -1;
2509 }
2510 }
2511
Damien Millerefb4afe2000-04-12 18:45:05 +10002512 s->chanid = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002513 session_close(ssh, s);
Damien Miller52b77be2001-10-10 15:14:37 +10002514}
2515
2516void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002517session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002518{
2519 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002520 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002521 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002522 if (s->used) {
2523 if (closefunc != NULL)
2524 closefunc(s);
2525 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002526 session_close(ssh, s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002527 }
Damien Miller52b77be2001-10-10 15:14:37 +10002528 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002529}
2530
Ben Lindstrombba81212001-06-25 05:01:22 +00002531static char *
Damien Millere247cc42000-05-07 12:03:14 +10002532session_tty_list(void)
2533{
2534 static char buf[1024];
2535 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002536 char *cp;
2537
Damien Millere247cc42000-05-07 12:03:14 +10002538 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002539 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002540 Session *s = &sessions[i];
2541 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002542
Damien Millera8ed44b2003-01-10 09:53:12 +11002543 if (strncmp(s->tty, "/dev/", 5) != 0) {
2544 cp = strrchr(s->tty, '/');
2545 cp = (cp == NULL) ? s->tty : cp + 1;
2546 } else
2547 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002548
Damien Millere247cc42000-05-07 12:03:14 +10002549 if (buf[0] != '\0')
2550 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002551 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002552 }
2553 }
2554 if (buf[0] == '\0')
2555 strlcpy(buf, "notty", sizeof buf);
2556 return buf;
2557}
2558
2559void
2560session_proctitle(Session *s)
2561{
2562 if (s->pw == NULL)
2563 error("no user for session %d", s->self);
2564 else
2565 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2566}
2567
Ben Lindstrom768176b2001-06-09 01:29:12 +00002568int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002569session_setup_x11fwd(struct ssh *ssh, Session *s)
Ben Lindstrom768176b2001-06-09 01:29:12 +00002570{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002571 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002572 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002573 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002574 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002575
djm@openbsd.org7c856852018-03-03 03:15:51 +00002576 if (!auth_opts->permit_x11_forwarding_flag) {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002577 ssh_packet_send_debug(ssh, "X11 forwarding disabled by key options.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002578 return 0;
2579 }
2580 if (!options.x11_forwarding) {
2581 debug("X11 forwarding disabled in server configuration file.");
2582 return 0;
2583 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002584 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002585 (stat(options.xauth_location, &st) == -1)) {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002586 ssh_packet_send_debug(ssh, "No xauth program; cannot forward X11.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002587 return 0;
2588 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002589 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002590 debug("X11 display already set.");
2591 return 0;
2592 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002593 if (x11_create_display_inet(ssh, options.x11_display_offset,
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002594 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002595 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002596 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002597 return 0;
2598 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002599 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002600 channel_register_cleanup(ssh, s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002601 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002602 }
Kevin Steves366298c2001-12-19 17:58:01 +00002603
2604 /* Set up a suitable value for the DISPLAY variable. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002605 if (gethostname(hostname, sizeof(hostname)) == -1)
Kevin Steves366298c2001-12-19 17:58:01 +00002606 fatal("gethostname: %.100s", strerror(errno));
2607 /*
2608 * auth_display must be used as the displayname when the
2609 * authorization entry is added with xauth(1). This will be
2610 * different than the DISPLAY string for localhost displays.
2611 */
Damien Miller95c249f2002-02-05 12:11:34 +11002612 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002613 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002614 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002615 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002616 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002617 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002618 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002619 } else {
2620#ifdef IPADDR_IN_DISPLAY
2621 struct hostent *he;
2622 struct in_addr my_addr;
2623
2624 he = gethostbyname(hostname);
2625 if (he == NULL) {
2626 error("Can't get IP address for X11 DISPLAY.");
Damien Miller885bc112019-04-04 02:47:40 +11002627 ssh_packet_send_debug(ssh, "Can't get IP address for X11 DISPLAY.");
Kevin Steves366298c2001-12-19 17:58:01 +00002628 return 0;
2629 }
2630 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002631 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002632 s->display_number, s->screen);
2633#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002634 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002635 s->display_number, s->screen);
2636#endif
2637 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002638 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002639 }
2640
Ben Lindstrom768176b2001-06-09 01:29:12 +00002641 return 1;
2642}
2643
Ben Lindstrombba81212001-06-25 05:01:22 +00002644static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002645do_authenticated2(struct ssh *ssh, Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002646{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002647 server_loop2(ssh, authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002648}
2649
2650void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002651do_cleanup(struct ssh *ssh, Authctxt *authctxt)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002652{
2653 static int called = 0;
2654
2655 debug("do_cleanup");
2656
2657 /* no cleanup if we're in the child for login shell */
2658 if (is_child)
2659 return;
2660
2661 /* avoid double cleanup */
2662 if (called)
2663 return;
2664 called = 1;
2665
Darren Tucker9142e1c2007-08-16 23:28:04 +10002666 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002667 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002668
2669#ifdef USE_PAM
2670 if (options.use_pam) {
2671 sshpam_cleanup();
2672 sshpam_thread_cleanup();
2673 }
2674#endif
2675
2676 if (!authctxt->authenticated)
2677 return;
2678
Darren Tucker3e33cec2003-10-02 16:12:36 +10002679#ifdef KRB5
2680 if (options.kerberos_ticket_cleanup &&
2681 authctxt->krb5_ctx)
2682 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002683#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002684
2685#ifdef GSSAPI
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002686 if (options.gss_cleanup_creds)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002687 ssh_gssapi_cleanup_creds();
2688#endif
2689
2690 /* remove agent socket */
2691 auth_sock_cleanup_proc(authctxt->pw);
2692
djm@openbsd.org8f574952017-06-24 06:34:38 +00002693 /* remove userauth info */
2694 if (auth_info_file != NULL) {
2695 temporarily_use_uid(authctxt->pw);
2696 unlink(auth_info_file);
2697 restore_uid();
2698 free(auth_info_file);
2699 auth_info_file = NULL;
2700 }
2701
Darren Tucker3e33cec2003-10-02 16:12:36 +10002702 /*
2703 * Cleanup ptys/utmp only if privsep is disabled,
2704 * or if running in monitor.
2705 */
2706 if (!use_privsep || mm_is_monitor())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002707 session_destroy_all(ssh, session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002708}
djm@openbsd.org95767262016-03-07 19:02:43 +00002709
2710/* Return a name for the remote host that fits inside utmp_size */
2711
2712const char *
2713session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2714{
2715 const char *remote = "";
2716
2717 if (utmp_size > 0)
2718 remote = auth_get_canonical_hostname(ssh, use_dns);
2719 if (utmp_size == 0 || strlen(remote) > utmp_size)
2720 remote = ssh_remote_ipaddr(ssh);
2721 return remote;
2722}
2723