blob: 4862e5d63f5e0b96689deb40f81f5dc326bfa903 [file] [log] [blame]
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001/* $OpenBSD: session.c,v 1.312 2019/01/19 21:41:53 djm Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +000049#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100051#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100052#include <grp.h>
Damien Millere5c0d522014-07-03 21:24:19 +100053#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110055#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100057#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110058#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100059#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100060#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100061#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100062#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100063#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000064#include <limits.h>
Damien Millerb38eff82000-04-01 11:09:21 +100065
Damien Millerb84886b2008-05-19 15:05:07 +100066#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100068#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000070#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "packet.h"
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) {
236 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000237 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000238 }
239 if (sock != -1)
240 close(sock);
241 auth_sock_name = NULL;
242 auth_sock_dir = NULL;
243 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000244}
245
Darren Tucker1921ed92004-02-10 13:23:28 +1100246static void
247display_loginmsg(void)
248{
markus@openbsd.org2808d182018-07-09 21:26:02 +0000249 int r;
250
251 if (sshbuf_len(loginmsg) == 0)
252 return;
253 if ((r = sshbuf_put_u8(loginmsg, 0)) != 0)
254 fatal("%s: buffer error: %s", __func__, ssh_err(r));
255 printf("%s", (char *)sshbuf_ptr(loginmsg));
256 sshbuf_reset(loginmsg);
Darren Tucker1921ed92004-02-10 13:23:28 +1100257}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000258
djm@openbsd.org8f574952017-06-24 06:34:38 +0000259static void
260prepare_auth_info_file(struct passwd *pw, struct sshbuf *info)
261{
262 int fd = -1, success = 0;
263
264 if (!options.expose_userauth_info || info == NULL)
265 return;
266
267 temporarily_use_uid(pw);
268 auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX");
269 if ((fd = mkstemp(auth_info_file)) == -1) {
270 error("%s: mkstemp: %s", __func__, strerror(errno));
271 goto out;
272 }
273 if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info),
274 sshbuf_len(info)) != sshbuf_len(info)) {
275 error("%s: write: %s", __func__, strerror(errno));
276 goto out;
277 }
278 if (close(fd) != 0) {
279 error("%s: close: %s", __func__, strerror(errno));
280 goto out;
281 }
282 success = 1;
283 out:
284 if (!success) {
285 if (fd != -1)
286 close(fd);
287 free(auth_info_file);
288 auth_info_file = NULL;
289 }
290 restore_uid();
291}
292
djm@openbsd.org7c856852018-03-03 03:15:51 +0000293static void
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000294set_fwdpermit_from_authopts(struct ssh *ssh, const struct sshauthopt *opts)
djm@openbsd.org7c856852018-03-03 03:15:51 +0000295{
296 char *tmp, *cp, *host;
297 int port;
298 size_t i;
299
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000300 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0) {
301 channel_clear_permission(ssh, FORWARD_USER, FORWARD_LOCAL);
302 for (i = 0; i < auth_opts->npermitopen; i++) {
303 tmp = cp = xstrdup(auth_opts->permitopen[i]);
304 /* This shouldn't fail as it has already been checked */
305 if ((host = hpdelim(&cp)) == NULL)
306 fatal("%s: internal error: hpdelim", __func__);
307 host = cleanhostname(host);
308 if (cp == NULL || (port = permitopen_port(cp)) < 0)
309 fatal("%s: internal error: permitopen port",
310 __func__);
311 channel_add_permission(ssh,
312 FORWARD_USER, FORWARD_LOCAL, host, port);
313 free(tmp);
314 }
315 }
316 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) != 0) {
317 channel_clear_permission(ssh, FORWARD_USER, FORWARD_REMOTE);
318 for (i = 0; i < auth_opts->npermitlisten; i++) {
319 tmp = cp = xstrdup(auth_opts->permitlisten[i]);
320 /* This shouldn't fail as it has already been checked */
321 if ((host = hpdelim(&cp)) == NULL)
322 fatal("%s: internal error: hpdelim", __func__);
323 host = cleanhostname(host);
324 if (cp == NULL || (port = permitopen_port(cp)) < 0)
325 fatal("%s: internal error: permitlisten port",
326 __func__);
327 channel_add_permission(ssh,
328 FORWARD_USER, FORWARD_REMOTE, host, port);
329 free(tmp);
330 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000331 }
332}
333
Ben Lindstromb31783d2001-03-22 02:02:12 +0000334void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000335do_authenticated(struct ssh *ssh, Authctxt *authctxt)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000336{
Damien Miller97f39ae2003-02-24 11:57:01 +1100337 setproctitle("%s", authctxt->pw->pw_name);
338
djm@openbsd.org7c856852018-03-03 03:15:51 +0000339 auth_log_authopts("active", auth_opts, 0);
340
Ben Lindstromb31783d2001-03-22 02:02:12 +0000341 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 /* XXX - streamlocal? */
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000343 set_fwdpermit_from_authopts(ssh, auth_opts);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000344
djm@openbsd.org115063a2018-06-06 18:22:41 +0000345 if (!auth_opts->permit_port_forwarding_flag ||
346 options.disable_forwarding) {
347 channel_disable_admin(ssh, FORWARD_LOCAL);
348 channel_disable_admin(ssh, FORWARD_REMOTE);
349 } else {
350 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
351 channel_disable_admin(ssh, FORWARD_LOCAL);
352 else
353 channel_permit_all(ssh, FORWARD_LOCAL);
354 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0)
355 channel_disable_admin(ssh, FORWARD_REMOTE);
356 else
357 channel_permit_all(ssh, FORWARD_REMOTE);
358 }
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000359 auth_debug_send(ssh);
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100360
djm@openbsd.org8f574952017-06-24 06:34:38 +0000361 prepare_auth_info_file(authctxt->pw, authctxt->session_info);
362
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000363 do_authenticated2(ssh, authctxt);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000364
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000365 do_cleanup(ssh, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000366}
367
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000368/* Check untrusted xauth strings for metacharacters */
369static int
370xauth_valid_string(const char *s)
371{
372 size_t i;
373
374 for (i = 0; s[i] != '\0'; i++) {
375 if (!isalnum((u_char)s[i]) &&
376 s[i] != '.' && s[i] != ':' && s[i] != '/' &&
377 s[i] != '-' && s[i] != '_')
deraadt@openbsd.org21fd4772018-07-25 13:56:23 +0000378 return 0;
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000379 }
380 return 1;
381}
382
Darren Tucker293ee3c2014-01-19 15:28:01 +1100383#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000384/*
385 * This is called to fork and execute a command when we have no tty. This
386 * will call do_child from the child, and server_loop from the parent after
387 * setting up file descriptors and such.
388 */
Damien Miller7207f642008-05-19 15:34:50 +1000389int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000390do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000391{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000392 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000393#ifdef USE_PIPES
394 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000395
Damien Miller22a29882010-05-10 11:53:54 +1000396 if (s == NULL)
397 fatal("do_exec_no_pty: no session");
398
Damien Millerb38eff82000-04-01 11:09:21 +1000399 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000400 if (pipe(pin) < 0) {
401 error("%s: pipe in: %.100s", __func__, strerror(errno));
402 return -1;
403 }
404 if (pipe(pout) < 0) {
405 error("%s: pipe out: %.100s", __func__, strerror(errno));
406 close(pin[0]);
407 close(pin[1]);
408 return -1;
409 }
Damien Miller8853ca52010-06-26 10:00:14 +1000410 if (pipe(perr) < 0) {
411 error("%s: pipe err: %.100s", __func__,
412 strerror(errno));
413 close(pin[0]);
414 close(pin[1]);
415 close(pout[0]);
416 close(pout[1]);
417 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000418 }
419#else
Damien Millerb38eff82000-04-01 11:09:21 +1000420 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000421
Damien Miller22a29882010-05-10 11:53:54 +1000422 if (s == NULL)
423 fatal("do_exec_no_pty: no session");
424
Damien Millerb38eff82000-04-01 11:09:21 +1000425 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000426 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
427 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
428 return -1;
429 }
Damien Miller8853ca52010-06-26 10:00:14 +1000430 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
431 error("%s: socketpair #2: %.100s", __func__,
432 strerror(errno));
433 close(inout[0]);
434 close(inout[1]);
435 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000436 }
437#endif
438
Damien Millere247cc42000-05-07 12:03:14 +1000439 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000440
Damien Millerb38eff82000-04-01 11:09:21 +1000441 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000442 switch ((pid = fork())) {
443 case -1:
444 error("%s: fork: %.100s", __func__, strerror(errno));
445#ifdef USE_PIPES
446 close(pin[0]);
447 close(pin[1]);
448 close(pout[0]);
449 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000450 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000451 close(perr[1]);
452#else
453 close(inout[0]);
454 close(inout[1]);
455 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000456 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000457#endif
458 return -1;
459 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000460 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000461
Damien Millerb38eff82000-04-01 11:09:21 +1000462 /*
463 * Create a new session and process group since the 4.4BSD
464 * setlogin() affects the entire process group.
465 */
466 if (setsid() < 0)
467 error("setsid failed: %.100s", strerror(errno));
468
469#ifdef USE_PIPES
470 /*
471 * Redirect stdin. We close the parent side of the socket
472 * pair, and make the child side the standard input.
473 */
474 close(pin[1]);
475 if (dup2(pin[0], 0) < 0)
476 perror("dup2 stdin");
477 close(pin[0]);
478
479 /* Redirect stdout. */
480 close(pout[0]);
481 if (dup2(pout[1], 1) < 0)
482 perror("dup2 stdout");
483 close(pout[1]);
484
485 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000486 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000487 if (dup2(perr[1], 2) < 0)
488 perror("dup2 stderr");
489 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000490#else
Damien Millerb38eff82000-04-01 11:09:21 +1000491 /*
492 * Redirect stdin, stdout, and stderr. Stdin and stdout will
493 * use the same socket, as some programs (particularly rdist)
494 * seem to depend on it.
495 */
496 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000497 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000498 if (dup2(inout[0], 0) < 0) /* stdin */
499 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000500 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000501 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000502 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000503 if (dup2(err[0], 2) < 0) /* stderr */
504 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000505 close(err[0]);
506#endif
507
Damien Millerb38eff82000-04-01 11:09:21 +1000508 /* Do processing for the child (exec command etc). */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000509 do_child(ssh, s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000510 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000511 default:
512 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000513 }
Damien Miller7207f642008-05-19 15:34:50 +1000514
Damien Millerbac2d8a2000-09-05 16:13:06 +1100515#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100516 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100517#endif
Damien Miller7207f642008-05-19 15:34:50 +1000518
Damien Millerb38eff82000-04-01 11:09:21 +1000519 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000520 /* Set interactive/non-interactive mode. */
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +0000521 ssh_packet_set_interactive(ssh, s->display != NULL,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100522 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000523
524 /*
525 * Clear loginmsg, since it's the child's responsibility to display
526 * it to the user, otherwise multiple sessions may accumulate
527 * multiple copies of the login messages.
528 */
Damien Miller120a1ec2018-07-10 19:39:52 +1000529 sshbuf_reset(loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000530
Damien Millerb38eff82000-04-01 11:09:21 +1000531#ifdef USE_PIPES
532 /* We are the parent. Close the child sides of the pipes. */
533 close(pin[0]);
534 close(pout[1]);
535 close(perr[1]);
536
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000537 session_set_fds(ssh, s, pin[1], pout[0], perr[0],
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000538 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000539#else
Damien Millerb38eff82000-04-01 11:09:21 +1000540 /* We are the parent. Close the child sides of the socket pairs. */
541 close(inout[0]);
542 close(err[0]);
543
544 /*
545 * Enter the interactive session. Note: server_loop must be able to
546 * handle the case that fdin and fdout are the same.
547 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000548 session_set_fds(s, inout[1], inout[1], err[1],
549 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000550#endif
551 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000552}
553
554/*
555 * This is called to fork and execute a command when we have a tty. This
556 * will call do_child from the child, and server_loop from the parent after
557 * setting up file descriptors, controlling tty, updating wtmp, utmp,
558 * lastlog, and other such operations.
559 */
Damien Miller7207f642008-05-19 15:34:50 +1000560int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000561do_exec_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000562{
Damien Millerb38eff82000-04-01 11:09:21 +1000563 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000564 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000565
566 if (s == NULL)
567 fatal("do_exec_pty: no session");
568 ptyfd = s->ptyfd;
569 ttyfd = s->ttyfd;
570
Damien Miller7207f642008-05-19 15:34:50 +1000571 /*
572 * Create another descriptor of the pty master side for use as the
573 * standard input. We could use the original descriptor, but this
574 * simplifies code in server_loop. The descriptor is bidirectional.
575 * Do this before forking (and cleanup in the child) so as to
576 * detect and gracefully fail out-of-fd conditions.
577 */
578 if ((fdout = dup(ptyfd)) < 0) {
579 error("%s: dup #1: %s", __func__, strerror(errno));
580 close(ttyfd);
581 close(ptyfd);
582 return -1;
583 }
584 /* we keep a reference to the pty master */
585 if ((ptymaster = dup(ptyfd)) < 0) {
586 error("%s: dup #2: %s", __func__, strerror(errno));
587 close(ttyfd);
588 close(ptyfd);
589 close(fdout);
590 return -1;
591 }
592
Damien Millerb38eff82000-04-01 11:09:21 +1000593 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000594 switch ((pid = fork())) {
595 case -1:
596 error("%s: fork: %.100s", __func__, strerror(errno));
597 close(fdout);
598 close(ptymaster);
599 close(ttyfd);
600 close(ptyfd);
601 return -1;
602 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000603 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000604
Damien Miller7207f642008-05-19 15:34:50 +1000605 close(fdout);
606 close(ptymaster);
607
Damien Millerb38eff82000-04-01 11:09:21 +1000608 /* Close the master side of the pseudo tty. */
609 close(ptyfd);
610
611 /* Make the pseudo tty our controlling tty. */
612 pty_make_controlling_tty(&ttyfd, s->tty);
613
Damien Miller9c751422001-10-10 15:02:46 +1000614 /* Redirect stdin/stdout/stderr from the pseudo tty. */
615 if (dup2(ttyfd, 0) < 0)
616 error("dup2 stdin: %s", strerror(errno));
617 if (dup2(ttyfd, 1) < 0)
618 error("dup2 stdout: %s", strerror(errno));
619 if (dup2(ttyfd, 2) < 0)
620 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000621
622 /* Close the extra descriptor for the pseudo tty. */
623 close(ttyfd);
624
Damien Miller942da032000-08-18 13:59:06 +1000625 /* record login, etc. similar to login(1) */
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000626#ifndef HAVE_OSF_SIA
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000627 do_login(ssh, s, command);
Damien Miller364a9bd2001-04-16 18:37:05 +1000628#endif
Damien Miller7207f642008-05-19 15:34:50 +1000629 /*
630 * Do common processing for the child, such as execing
631 * the command.
632 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000633 do_child(ssh, s, command);
Darren Tucker43e7a352009-06-21 19:50:08 +1000634 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000635 default:
636 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000637 }
Damien Miller7207f642008-05-19 15:34:50 +1000638
Damien Millerbac2d8a2000-09-05 16:13:06 +1100639#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100640 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100641#endif
Damien Miller7207f642008-05-19 15:34:50 +1000642
Damien Millerb38eff82000-04-01 11:09:21 +1000643 s->pid = pid;
644
645 /* Parent. Close the slave side of the pseudo tty. */
646 close(ttyfd);
647
Damien Millerb38eff82000-04-01 11:09:21 +1000648 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000649 s->ptymaster = ptymaster;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +0000650 ssh_packet_set_interactive(ssh, 1,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100651 options.ip_qos_interactive, options.ip_qos_bulk);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000652 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
Damien Miller7207f642008-05-19 15:34:50 +1000653 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000654}
655
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000656/*
657 * This is called to fork and execute a command. If another command is
658 * to be forced, execute that instead.
659 */
Damien Miller7207f642008-05-19 15:34:50 +1000660int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000661do_exec(struct ssh *ssh, Session *s, const char *command)
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000662{
Damien Miller7207f642008-05-19 15:34:50 +1000663 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000664 const char *forced = NULL, *tty = NULL;
665 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000666
Damien Millere2754432006-07-24 14:06:47 +1000667 if (options.adm_forced_command) {
668 original_command = command;
669 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100670 forced = "(config)";
djm@openbsd.org7c856852018-03-03 03:15:51 +0000671 } else if (auth_opts->force_command != NULL) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000672 original_command = command;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000673 command = auth_opts->force_command;
Damien Miller71df7522013-10-15 12:12:02 +1100674 forced = "(key-option)";
675 }
djm@openbsd.orgcd989252018-10-02 12:40:07 +0000676 s->forced = 0;
Damien Miller71df7522013-10-15 12:12:02 +1100677 if (forced != NULL) {
djm@openbsd.orgcd989252018-10-02 12:40:07 +0000678 s->forced = 1;
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100679 if (IS_INTERNAL_SFTP(command)) {
680 s->is_subsystem = s->is_subsystem ?
681 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
682 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100683 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100684 snprintf(session_type, sizeof(session_type),
685 "forced-command %s '%.900s'", forced, command);
686 } else if (s->is_subsystem) {
687 snprintf(session_type, sizeof(session_type),
688 "subsystem '%.900s'", s->subsys);
689 } else if (command == NULL) {
690 snprintf(session_type, sizeof(session_type), "shell");
691 } else {
692 /* NB. we don't log unforced commands to preserve privacy */
693 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000694 }
695
Damien Miller71df7522013-10-15 12:12:02 +1100696 if (s->ttyfd != -1) {
697 tty = s->tty;
698 if (strncmp(tty, "/dev/", 5) == 0)
699 tty += 5;
700 }
701
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000702 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100703 session_type,
704 tty == NULL ? "" : " on ",
705 tty == NULL ? "" : tty,
706 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000707 ssh_remote_ipaddr(ssh),
708 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000709 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100710
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100711#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100712 if (command != NULL)
713 PRIVSEP(audit_run_command(command));
714 else if (s->ttyfd == -1) {
715 char *shell = s->pw->pw_shell;
716
717 if (shell[0] == '\0') /* empty shell means /bin/sh */
718 shell =_PATH_BSHELL;
719 PRIVSEP(audit_run_command(shell));
720 }
721#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000722 if (s->ttyfd != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000723 ret = do_exec_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000724 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000725 ret = do_exec_no_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000726
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000727 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000728
Darren Tucker09991742004-07-17 17:05:14 +1000729 /*
730 * Clear loginmsg: it's the child's responsibility to display
731 * it to the user, otherwise multiple sessions may accumulate
732 * multiple copies of the login messages.
733 */
markus@openbsd.org2808d182018-07-09 21:26:02 +0000734 sshbuf_reset(loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000735
736 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000737}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000738
Damien Miller942da032000-08-18 13:59:06 +1000739/* administrative, login(1)-like work */
740void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000741do_login(struct ssh *ssh, Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000742{
Damien Miller942da032000-08-18 13:59:06 +1000743 socklen_t fromlen;
744 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000745 struct passwd * pw = s->pw;
746 pid_t pid = getpid();
747
748 /*
749 * Get IP address of client. If the connection is not a socket, let
750 * the address be 0.0.0.0.
751 */
752 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000753 fromlen = sizeof(from);
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +0000754 if (ssh_packet_connection_is_on_socket(ssh)) {
755 if (getpeername(ssh_packet_get_connection_in(ssh),
Darren Tucker43e7a352009-06-21 19:50:08 +1000756 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000757 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000758 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000759 }
760 }
761
762 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000763 if (!use_privsep)
764 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000765 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000766 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000767 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000768
Kevin Steves092f2ef2000-10-14 13:36:13 +0000769#ifdef USE_PAM
770 /*
771 * If password change is needed, do it now.
772 * This needs to occur before the ~/.hushlogin check.
773 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100774 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
775 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000776 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100777 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000778 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000779 }
780#endif
781
Damien Millercf205e82001-04-16 18:29:15 +1000782 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000783 return;
784
Darren Tucker1921ed92004-02-10 13:23:28 +1100785 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000786
Damien Millercf205e82001-04-16 18:29:15 +1000787 do_motd();
788}
789
790/*
791 * Display the message of the day.
792 */
793void
794do_motd(void)
795{
796 FILE *f;
797 char buf[256];
798
Damien Miller942da032000-08-18 13:59:06 +1000799 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000800#ifdef HAVE_LOGIN_CAP
801 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
802 "/etc/motd"), "r");
803#else
Damien Miller942da032000-08-18 13:59:06 +1000804 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000805#endif
Damien Miller942da032000-08-18 13:59:06 +1000806 if (f) {
807 while (fgets(buf, sizeof(buf), f))
808 fputs(buf, stdout);
809 fclose(f);
810 }
811 }
812}
813
Kevin Steves8f63caa2001-07-04 18:23:02 +0000814
815/*
816 * Check for quiet login, either .hushlogin or command given.
817 */
818int
819check_quietlogin(Session *s, const char *command)
820{
821 char buf[256];
822 struct passwd *pw = s->pw;
823 struct stat st;
824
825 /* Return 1 if .hushlogin exists or a command given. */
826 if (command != NULL)
827 return 1;
828 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
829#ifdef HAVE_LOGIN_CAP
830 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
831 return 1;
832#else
833 if (stat(buf, &st) >= 0)
834 return 1;
835#endif
836 return 0;
837}
838
Damien Millerb38eff82000-04-01 11:09:21 +1000839/*
Damien Millerb38eff82000-04-01 11:09:21 +1000840 * Reads environment variables from the given file and adds/overrides them
841 * into the environment. If the file does not exist, this does nothing.
842 * Otherwise, it must consist of empty lines, comments (line starts with '#')
843 * and assignments of the form name=value. No other forms are allowed.
djm@openbsd.org95344c22018-07-03 10:59:35 +0000844 * If whitelist is not NULL, then it is interpreted as a pattern list and
845 * only variable names that match it will be accepted.
Damien Millerb38eff82000-04-01 11:09:21 +1000846 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000847static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000848read_environment_file(char ***env, u_int *envsize,
djm@openbsd.org95344c22018-07-03 10:59:35 +0000849 const char *filename, const char *whitelist)
Damien Millerb38eff82000-04-01 11:09:21 +1000850{
851 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000852 char *line = NULL, *cp, *value;
853 size_t linesize = 0;
Damien Miller990070a2002-06-26 23:51:06 +1000854 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000855
856 f = fopen(filename, "r");
857 if (!f)
858 return;
859
markus@openbsd.org7f906352018-06-06 18:29:18 +0000860 while (getline(&line, &linesize, f) != -1) {
Damien Miller990070a2002-06-26 23:51:06 +1000861 if (++lineno > 1000)
862 fatal("Too many lines in environment file %s", filename);
markus@openbsd.org7f906352018-06-06 18:29:18 +0000863 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
Damien Millerb38eff82000-04-01 11:09:21 +1000864 ;
865 if (!*cp || *cp == '#' || *cp == '\n')
866 continue;
Damien Miller14b017d2007-09-17 16:09:15 +1000867
868 cp[strcspn(cp, "\n")] = '\0';
869
Damien Millerb38eff82000-04-01 11:09:21 +1000870 value = strchr(cp, '=');
871 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000872 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
873 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000874 continue;
875 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000876 /*
877 * Replace the equals sign by nul, and advance value to
878 * the value string.
879 */
Damien Millerb38eff82000-04-01 11:09:21 +1000880 *value = '\0';
881 value++;
djm@openbsd.org95344c22018-07-03 10:59:35 +0000882 if (whitelist != NULL &&
883 match_pattern_list(cp, whitelist, 0) != 1)
884 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000885 child_set_env(env, envsize, cp, value);
886 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000887 free(line);
Damien Millerb38eff82000-04-01 11:09:21 +1000888 fclose(f);
889}
890
Darren Tuckere1a790d2003-09-16 11:52:19 +1000891#ifdef HAVE_ETC_DEFAULT_LOGIN
892/*
893 * Return named variable from specified environment, or NULL if not present.
894 */
895static char *
896child_get_env(char **env, const char *name)
897{
898 int i;
899 size_t len;
900
901 len = strlen(name);
902 for (i=0; env[i] != NULL; i++)
903 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
904 return(env[i] + len + 1);
905 return NULL;
906}
907
908/*
909 * Read /etc/default/login.
910 * We pick up the PATH (or SUPATH for root) and UMASK.
911 */
912static void
913read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
914{
915 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000916 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000917 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000918
919 /*
920 * We don't want to copy the whole file to the child's environment,
921 * so we use a temporary environment and copy the variables we're
922 * interested in.
923 */
Darren Tucker3deb56f2018-07-05 13:32:01 +1000924 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login",
925 options.permit_user_env_whitelist);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000926
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000927 if (tmpenv == NULL)
928 return;
929
Darren Tuckere1a790d2003-09-16 11:52:19 +1000930 if (uid == 0)
931 var = child_get_env(tmpenv, "SUPATH");
932 else
933 var = child_get_env(tmpenv, "PATH");
934 if (var != NULL)
935 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100936
Darren Tuckere1a790d2003-09-16 11:52:19 +1000937 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
938 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000939 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100940
Darren Tuckere1a790d2003-09-16 11:52:19 +1000941 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +1000942 free(tmpenv[i]);
943 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000944}
945#endif /* HAVE_ETC_DEFAULT_LOGIN */
946
Damien Miller94bc1e72017-07-28 14:50:59 +1000947static void
948copy_environment_blacklist(char **source, char ***env, u_int *envsize,
949 const char *blacklist)
Damien Millerb38eff82000-04-01 11:09:21 +1000950{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100951 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000952 int i;
953
Damien Millerbb9ffc12002-01-08 10:59:32 +1100954 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000955 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000956
Damien Millerbb9ffc12002-01-08 10:59:32 +1100957 for(i = 0; source[i] != NULL; i++) {
958 var_name = xstrdup(source[i]);
959 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000960 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000961 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000962 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100963 *var_val++ = '\0';
964
Damien Miller94bc1e72017-07-28 14:50:59 +1000965 if (blacklist == NULL ||
966 match_pattern_list(var_name, blacklist, 0) != 1) {
967 debug3("Copy environment: %s=%s", var_name, var_val);
968 child_set_env(env, envsize, var_name, var_val);
969 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100970
Darren Tuckerf60845f2013-06-02 08:07:31 +1000971 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000972 }
973}
Damien Millercb5e44a2000-09-29 12:12:36 +1100974
Damien Miller94bc1e72017-07-28 14:50:59 +1000975void
976copy_environment(char **source, char ***env, u_int *envsize)
977{
978 copy_environment_blacklist(source, env, envsize, NULL);
979}
980
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000981static char **
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000982do_setup_env(struct ssh *ssh, Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000983{
Damien Millerb38eff82000-04-01 11:09:21 +1000984 char buf[256];
djm@openbsd.org7c856852018-03-03 03:15:51 +0000985 size_t n;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000986 u_int i, envsize;
djm@openbsd.org28013752018-06-09 03:03:10 +0000987 char *ocp, *cp, *value, **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000988 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100989#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +1000990 char *path = NULL;
991#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000992
Damien Millerb38eff82000-04-01 11:09:21 +1000993 /* Initialize the environment. */
994 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000995 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +1000996 env[0] = NULL;
997
Damien Millerbac2d8a2000-09-05 16:13:06 +1100998#ifdef HAVE_CYGWIN
999 /*
1000 * The Windows environment contains some setting which are
1001 * important for a running system. They must not be dropped.
1002 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001003 {
1004 char **p;
1005
1006 p = fetch_windows_environment();
1007 copy_environment(p, &env, &envsize);
1008 free_windows_environment(p);
1009 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001010#endif
1011
Darren Tucker0efd1552003-08-26 11:49:55 +10001012#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001013 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001014 * the childs environment as they see fit
1015 */
1016 ssh_gssapi_do_child(&env, &envsize);
1017#endif
1018
djm@openbsd.org83b58182016-08-19 03:18:06 +00001019 /* Set basic environment. */
1020 for (i = 0; i < s->num_env; i++)
1021 child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
Darren Tucker46bc0752004-05-02 22:11:30 +10001022
djm@openbsd.org83b58182016-08-19 03:18:06 +00001023 child_set_env(&env, &envsize, "USER", pw->pw_name);
1024 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001025#ifdef _AIX
djm@openbsd.org83b58182016-08-19 03:18:06 +00001026 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001027#endif
djm@openbsd.org83b58182016-08-19 03:18:06 +00001028 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001029#ifdef HAVE_LOGIN_CAP
djm@openbsd.org83b58182016-08-19 03:18:06 +00001030 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1031 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1032 else
1033 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001034#else /* HAVE_LOGIN_CAP */
1035# ifndef HAVE_CYGWIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001036 /*
1037 * There's no standard path on Windows. The path contains
1038 * important components pointing to the system directories,
1039 * needed for loading shared libraries. So the path better
1040 * remains intact here.
1041 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001042# ifdef HAVE_ETC_DEFAULT_LOGIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001043 read_etc_default_login(&env, &envsize, pw->pw_uid);
1044 path = child_get_env(env, "PATH");
Darren Tuckere1a790d2003-09-16 11:52:19 +10001045# endif /* HAVE_ETC_DEFAULT_LOGIN */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001046 if (path == NULL || *path == '\0') {
1047 child_set_env(&env, &envsize, "PATH",
1048 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1049 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001050# endif /* HAVE_CYGWIN */
1051#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001052
djm@openbsd.org83b58182016-08-19 03:18:06 +00001053 snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
1054 child_set_env(&env, &envsize, "MAIL", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001055
djm@openbsd.org83b58182016-08-19 03:18:06 +00001056 /* Normal systems set SHELL by default. */
1057 child_set_env(&env, &envsize, "SHELL", shell);
1058
Damien Millerb38eff82000-04-01 11:09:21 +10001059 if (getenv("TZ"))
1060 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001061 if (s->term)
1062 child_set_env(&env, &envsize, "TERM", s->term);
1063 if (s->display)
1064 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Millerb38eff82000-04-01 11:09:21 +10001065
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001066 /*
1067 * Since we clear KRB5CCNAME at startup, if it's set now then it
1068 * must have been set by a native authentication method (eg AIX or
1069 * SIA), so copy it to the child.
1070 */
1071 {
1072 char *cp;
1073
1074 if ((cp = getenv("KRB5CCNAME")) != NULL)
1075 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1076 }
1077
Damien Millerb38eff82000-04-01 11:09:21 +10001078#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001079 {
1080 char *cp;
1081
1082 if ((cp = getenv("AUTHSTATE")) != NULL)
1083 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Darren Tucker3deb56f2018-07-05 13:32:01 +10001084 read_environment_file(&env, &envsize, "/etc/environment",
1085 options.permit_user_env_whitelist);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001086 }
Damien Millerb38eff82000-04-01 11:09:21 +10001087#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001088#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001089 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001090 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001091 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001092#endif
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001093 if (auth_sock_name != NULL)
1094 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1095 auth_sock_name);
1096
1097
1098 /* Set custom environment options from pubkey authentication. */
1099 if (options.permit_user_env) {
1100 for (n = 0 ; n < auth_opts->nenv; n++) {
1101 ocp = xstrdup(auth_opts->env[n]);
1102 cp = strchr(ocp, '=');
1103 if (*cp == '=') {
1104 *cp = '\0';
djm@openbsd.org95344c22018-07-03 10:59:35 +00001105 /* Apply PermitUserEnvironment whitelist */
1106 if (options.permit_user_env_whitelist == NULL ||
1107 match_pattern_list(ocp,
1108 options.permit_user_env_whitelist, 0) == 1)
1109 child_set_env(&env, &envsize,
1110 ocp, cp + 1);
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001111 }
1112 free(ocp);
1113 }
1114 }
1115
1116 /* read $HOME/.ssh/environment. */
1117 if (options.permit_user_env) {
1118 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1119 pw->pw_dir);
djm@openbsd.org95344c22018-07-03 10:59:35 +00001120 read_environment_file(&env, &envsize, buf,
1121 options.permit_user_env_whitelist);
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001122 }
1123
Damien Millerb38eff82000-04-01 11:09:21 +10001124#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001125 /*
1126 * Pull in any environment variables that may have
1127 * been set by PAM.
1128 */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001129 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001130 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001131
Damien Miller94bc1e72017-07-28 14:50:59 +10001132 /*
Damien Miller8a22ffa2018-12-07 15:41:16 +11001133 * Don't allow PAM-internal env vars to leak
1134 * back into the session environment.
Damien Miller94bc1e72017-07-28 14:50:59 +10001135 */
Damien Miller8a22ffa2018-12-07 15:41:16 +11001136#define PAM_ENV_BLACKLIST "SSH_AUTH_INFO*,SSH_CONNECTION*"
Damien Millerc756e9b2003-11-17 21:41:42 +11001137 p = fetch_pam_child_environment();
Damien Miller8a22ffa2018-12-07 15:41:16 +11001138 copy_environment_blacklist(p, &env, &envsize,
1139 PAM_ENV_BLACKLIST);
Damien Millerc756e9b2003-11-17 21:41:42 +11001140 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001141
Damien Millerc756e9b2003-11-17 21:41:42 +11001142 p = fetch_pam_environment();
Damien Miller8a22ffa2018-12-07 15:41:16 +11001143 copy_environment_blacklist(p, &env, &envsize,
1144 PAM_ENV_BLACKLIST);
Kevin Steves38b050a2002-07-23 00:44:07 +00001145 free_pam_environment(p);
1146 }
Damien Millerb38eff82000-04-01 11:09:21 +10001147#endif /* USE_PAM */
1148
djm@openbsd.org28013752018-06-09 03:03:10 +00001149 /* Environment specified by admin */
1150 for (i = 0; i < options.num_setenv; i++) {
1151 cp = xstrdup(options.setenv[i]);
1152 if ((value = strchr(cp, '=')) == NULL) {
1153 /* shouldn't happen; vars are checked in servconf.c */
1154 fatal("Invalid config SetEnv: %s", options.setenv[i]);
1155 }
1156 *value++ = '\0';
1157 child_set_env(&env, &envsize, cp, value);
1158 }
1159
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001160 /* SSH_CLIENT deprecated */
1161 snprintf(buf, sizeof buf, "%.50s %d %d",
1162 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1163 ssh_local_port(ssh));
1164 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001165
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001166 laddr = get_local_ipaddr(ssh_packet_get_connection_in(ssh));
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001167 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1168 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1169 laddr, ssh_local_port(ssh));
1170 free(laddr);
1171 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1172
1173 if (tun_fwd_ifnames != NULL)
1174 child_set_env(&env, &envsize, "SSH_TUNNEL", tun_fwd_ifnames);
1175 if (auth_info_file != NULL)
1176 child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file);
1177 if (s->ttyfd != -1)
1178 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1179 if (original_command)
1180 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1181 original_command);
1182
Damien Millerb38eff82000-04-01 11:09:21 +10001183 if (debug_flag) {
1184 /* dump the environment */
1185 fprintf(stderr, "Environment:\n");
1186 for (i = 0; env[i]; i++)
1187 fprintf(stderr, " %.200s\n", env[i]);
1188 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001189 return env;
1190}
1191
1192/*
1193 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1194 * first in this order).
1195 */
1196static void
djm@openbsd.org7c856852018-03-03 03:15:51 +00001197do_rc_files(struct ssh *ssh, Session *s, const char *shell)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001198{
1199 FILE *f = NULL;
1200 char cmd[1024];
1201 int do_xauth;
1202 struct stat st;
1203
1204 do_xauth =
1205 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1206
Damien Millera1b48cc2008-03-27 11:02:02 +11001207 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001208 if (!s->is_subsystem && options.adm_forced_command == NULL &&
djm@openbsd.org7c856852018-03-03 03:15:51 +00001209 auth_opts->permit_user_rc && options.permit_user_rc &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001210 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001211 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1212 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1213 if (debug_flag)
1214 fprintf(stderr, "Running %s\n", cmd);
1215 f = popen(cmd, "w");
1216 if (f) {
1217 if (do_xauth)
1218 fprintf(f, "%s %s\n", s->auth_proto,
1219 s->auth_data);
1220 pclose(f);
1221 } else
1222 fprintf(stderr, "Could not run %s\n",
1223 _PATH_SSH_USER_RC);
1224 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1225 if (debug_flag)
1226 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1227 _PATH_SSH_SYSTEM_RC);
1228 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1229 if (f) {
1230 if (do_xauth)
1231 fprintf(f, "%s %s\n", s->auth_proto,
1232 s->auth_data);
1233 pclose(f);
1234 } else
1235 fprintf(stderr, "Could not run %s\n",
1236 _PATH_SSH_SYSTEM_RC);
1237 } else if (do_xauth && options.xauth_location != NULL) {
1238 /* Add authority data to .Xauthority if appropriate. */
1239 if (debug_flag) {
1240 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001241 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001242 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001243 fprintf(stderr,
1244 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001245 options.xauth_location, s->auth_display,
1246 s->auth_proto, s->auth_data);
1247 }
1248 snprintf(cmd, sizeof cmd, "%s -q -",
1249 options.xauth_location);
1250 f = popen(cmd, "w");
1251 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001252 fprintf(f, "remove %s\n",
1253 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001254 fprintf(f, "add %s %s %s\n",
1255 s->auth_display, s->auth_proto,
1256 s->auth_data);
1257 pclose(f);
1258 } else {
1259 fprintf(stderr, "Could not run %s\n",
1260 cmd);
1261 }
1262 }
1263}
1264
1265static void
1266do_nologin(struct passwd *pw)
1267{
1268 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001269 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1270 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001271
1272#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001273 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001274 return;
1275 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001276#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001277 if (pw->pw_uid == 0)
1278 return;
1279 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001280#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001281 if (stat(nl, &sb) == -1) {
1282 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001283 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001284 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001285 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001286
1287 /* /etc/nologin exists. Print its contents if we can and exit. */
1288 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1289 if ((f = fopen(nl, "r")) != NULL) {
Darren Tucker19edfd42018-02-13 08:25:46 +11001290 while (fgets(buf, sizeof(buf), f))
1291 fputs(buf, stderr);
1292 fclose(f);
1293 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001294 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001295}
1296
Damien Millerd8cb1f12008-02-10 22:40:12 +11001297/*
1298 * Chroot into a directory after checking it for safety: all path components
1299 * must be root-owned directories with strict permissions.
1300 */
1301static void
1302safely_chroot(const char *path, uid_t uid)
1303{
1304 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001305 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001306 struct stat st;
1307
djm@openbsd.org2a358622018-11-16 03:26:01 +00001308 if (!path_absolute(path))
Damien Millerd8cb1f12008-02-10 22:40:12 +11001309 fatal("chroot path does not begin at root");
1310 if (strlen(path) >= sizeof(component))
1311 fatal("chroot path too long");
1312
1313 /*
1314 * Descend the path, checking that each component is a
1315 * root-owned directory with strict permissions.
1316 */
1317 for (cp = path; cp != NULL;) {
1318 if ((cp = strchr(cp, '/')) == NULL)
1319 strlcpy(component, path, sizeof(component));
1320 else {
1321 cp++;
1322 memcpy(component, path, cp - path);
1323 component[cp - path] = '\0';
1324 }
1325
1326 debug3("%s: checking '%s'", __func__, component);
1327
1328 if (stat(component, &st) != 0)
1329 fatal("%s: stat(\"%s\"): %s", __func__,
1330 component, strerror(errno));
1331 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1332 fatal("bad ownership or modes for chroot "
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001333 "directory %s\"%s\"",
Damien Millerd8cb1f12008-02-10 22:40:12 +11001334 cp == NULL ? "" : "component ", component);
1335 if (!S_ISDIR(st.st_mode))
1336 fatal("chroot path %s\"%s\" is not a directory",
1337 cp == NULL ? "" : "component ", component);
1338
1339 }
1340
1341 if (chdir(path) == -1)
1342 fatal("Unable to chdir to chroot path \"%s\": "
1343 "%s", path, strerror(errno));
1344 if (chroot(path) == -1)
1345 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1346 if (chdir("/") == -1)
1347 fatal("%s: chdir(/) after chroot: %s",
1348 __func__, strerror(errno));
1349 verbose("Changed root directory to \"%s\"", path);
1350}
1351
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001352/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001354do_setusercontext(struct passwd *pw)
1355{
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001356 char uidstr[32], *chroot_path, *tmp;
Damien Miller54e37732008-02-10 22:48:55 +11001357
Darren Tucker97528352010-11-05 12:03:05 +11001358 platform_setusercontext(pw);
1359
Darren Tuckerb12fe272010-11-05 14:47:01 +11001360 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001361#ifdef HAVE_LOGIN_CAP
1362 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001363 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001364 perror("unable to set user context");
1365 exit(1);
1366 }
1367#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001368 if (setlogin(pw->pw_name) < 0)
1369 error("setlogin failed: %s", strerror(errno));
1370 if (setgid(pw->pw_gid) < 0) {
1371 perror("setgid");
1372 exit(1);
1373 }
1374 /* Initialize the group list. */
1375 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1376 perror("initgroups");
1377 exit(1);
1378 }
1379 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001380#endif
1381
Darren Tucker920612e2010-11-05 12:36:15 +11001382 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001383
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001384 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001385 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001386 tmp = tilde_expand_filename(options.chroot_directory,
1387 pw->pw_uid);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001388 snprintf(uidstr, sizeof(uidstr), "%llu",
1389 (unsigned long long)pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001390 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001391 "u", pw->pw_name, "U", uidstr, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001392 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001393 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001394 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001395 /* Make sure we don't attempt to chroot again */
1396 free(options.chroot_directory);
1397 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001398 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001399 }
1400
1401#ifdef HAVE_LOGIN_CAP
1402 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1403 perror("unable to set user context (setuser)");
1404 exit(1);
1405 }
Damien Miller58528402013-03-15 11:22:37 +11001406 /*
1407 * FreeBSD's setusercontext() will not apply the user's
1408 * own umask setting unless running with the user's UID.
1409 */
1410 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001411#else
Tim Rice9464ba62014-01-20 17:59:28 -08001412# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001413 /*
1414 * In a chroot environment, the set_id() will always fail;
1415 * typically because of the lack of necessary authentication
1416 * services and runtime such as ./usr/lib/libiaf.so,
1417 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1418 * internal sftp chroot case. We'll lose auditing and ACLs but
1419 * permanently_set_uid will take care of the rest.
1420 */
1421 if (!in_chroot && set_id(pw->pw_name) != 0)
1422 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001423# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001424 /* Permanently switch to the desired uid. */
1425 permanently_set_uid(pw);
1426#endif
Damien Millera56086b2013-04-23 15:24:18 +10001427 } else if (options.chroot_directory != NULL &&
1428 strcasecmp(options.chroot_directory, "none") != 0) {
1429 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001430 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001431
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001432 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1433 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1434}
1435
Ben Lindstrom08105192002-03-22 02:50:06 +00001436static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001437do_pwchange(Session *s)
1438{
Darren Tucker09991742004-07-17 17:05:14 +10001439 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001440 fprintf(stderr, "WARNING: Your password has expired.\n");
1441 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001442 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001443 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001444#ifdef WITH_SELINUX
1445 setexeccon(NULL);
1446#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001447#ifdef PASSWD_NEEDS_USERNAME
1448 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1449 (char *)NULL);
1450#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001451 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001452#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001453 perror("passwd");
1454 } else {
1455 fprintf(stderr,
1456 "Password change required but no TTY available.\n");
1457 }
1458 exit(1);
1459}
1460
1461static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001462child_close_fds(struct ssh *ssh)
Darren Tucker23bc8d02004-02-06 16:24:31 +11001463{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001464 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001465
djm@openbsd.org141efe42015-01-14 20:05:27 +00001466 if (auth_sock != -1) {
1467 close(auth_sock);
1468 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001469 }
1470
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001471 if (ssh_packet_get_connection_in(ssh) ==
1472 ssh_packet_get_connection_out(ssh))
1473 close(ssh_packet_get_connection_in(ssh));
Darren Tucker23bc8d02004-02-06 16:24:31 +11001474 else {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001475 close(ssh_packet_get_connection_in(ssh));
1476 close(ssh_packet_get_connection_out(ssh));
Darren Tucker23bc8d02004-02-06 16:24:31 +11001477 }
1478 /*
1479 * Close all descriptors related to channels. They will still remain
1480 * open in the parent.
1481 */
1482 /* XXX better use close-on-exec? -markus */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001483 channel_close_all(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001484
1485 /*
1486 * Close any extra file descriptors. Note that there may still be
1487 * descriptors left by system functions. They will be closed later.
1488 */
1489 endpwent();
1490
1491 /*
Damien Miller788f2122005-11-05 15:14:59 +11001492 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001493 * hanging around in clients. Note that we want to do this after
1494 * initgroups, because at least on Solaris 2.3 it leaves file
1495 * descriptors open.
1496 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001497 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001498}
1499
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001500/*
1501 * Performs common processing for the child, such as setting up the
1502 * environment, closing extra file descriptors, setting the user and group
1503 * ids, and executing the command or shell.
1504 */
Damien Millerdfc24252008-02-10 22:29:40 +11001505#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001506void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001507do_child(struct ssh *ssh, Session *s, const char *command)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001508{
1509 extern char **environ;
1510 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001511 char *argv[ARGV_MAX];
djm@openbsd.org83b58182016-08-19 03:18:06 +00001512 const char *shell, *shell0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001513 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001514 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001515
1516 /* remove hostkey from the child's memory */
1517 destroy_sensitive_data();
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001518 ssh_packet_clear_keys(ssh);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001519
Darren Tucker23bc8d02004-02-06 16:24:31 +11001520 /* Force a password change */
1521 if (s->authctxt->force_pwchange) {
1522 do_setusercontext(pw);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001523 child_close_fds(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001524 do_pwchange(s);
1525 exit(1);
1526 }
1527
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001528 /*
1529 * Login(1) does this as well, and it needs uid 0 for the "-h"
1530 * switch, so we let login(1) to this for us.
1531 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001532#ifdef HAVE_OSF_SIA
djm@openbsd.org83b58182016-08-19 03:18:06 +00001533 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1534 if (!check_quietlogin(s, command))
1535 do_motd();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001536#else /* HAVE_OSF_SIA */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001537 /* When PAM is enabled we rely on it to do the nologin check */
1538 if (!options.use_pam)
1539 do_nologin(pw);
1540 do_setusercontext(pw);
1541 /*
1542 * PAM session modules in do_setusercontext may have
1543 * generated messages, so if this in an interactive
1544 * login then display them too.
1545 */
1546 if (!check_quietlogin(s, command))
1547 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001548#endif /* HAVE_OSF_SIA */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001549
Darren Tucker69687f42004-09-11 22:17:26 +10001550#ifdef USE_PAM
djm@openbsd.org83b58182016-08-19 03:18:06 +00001551 if (options.use_pam && !is_pam_session_open()) {
Darren Tucker48554152005-04-21 19:50:55 +10001552 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001553 display_loginmsg();
1554 exit(254);
1555 }
1556#endif
1557
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001558 /*
1559 * Get the shell from the password data. An empty shell field is
1560 * legal, and means /bin/sh.
1561 */
1562 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001563
1564 /*
1565 * Make sure $SHELL points to the shell from the password file,
1566 * even if shell is overridden from login.conf
1567 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001568 env = do_setup_env(ssh, s, shell);
Ben Lindstrom46767602002-12-23 02:26:08 +00001569
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001570#ifdef HAVE_LOGIN_CAP
1571 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1572#endif
1573
Damien Millerb38eff82000-04-01 11:09:21 +10001574 /*
1575 * Close the connection descriptors; note that this is the child, and
1576 * the server will still have the socket open, and it is important
1577 * that we do not shutdown it. Note that the descriptors cannot be
1578 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001579 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001580 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001581 child_close_fds(ssh);
Damien Millerb38eff82000-04-01 11:09:21 +10001582
Damien Millerb38eff82000-04-01 11:09:21 +10001583 /*
Damien Miller05eda432002-02-10 18:32:28 +11001584 * Must take new environment into use so that .ssh/rc,
1585 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001586 */
1587 environ = env;
1588
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001589#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001590 /*
1591 * At this point, we check to see if AFS is active and if we have
1592 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1593 * if we can (and need to) extend the ticket into an AFS token. If
1594 * we don't do this, we run into potential problems if the user's
1595 * home directory is in AFS and it's not world-readable.
1596 */
1597
1598 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001599 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001600 char cell[64];
1601
1602 debug("Getting AFS token");
1603
1604 k_setpag();
1605
1606 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1607 krb5_afslog(s->authctxt->krb5_ctx,
1608 s->authctxt->krb5_fwd_ccache, cell, NULL);
1609
1610 krb5_afslog_home(s->authctxt->krb5_ctx,
1611 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1612 }
1613#endif
1614
Damien Miller788f2122005-11-05 15:14:59 +11001615 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001616 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001617 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001618#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001619 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001620#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001621 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001622 fprintf(stderr, "Could not chdir to home "
1623 "directory %s: %s\n", pw->pw_dir,
1624 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001625 }
Damien Miller6051c942008-06-16 07:53:16 +10001626 if (r)
1627 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001628 }
1629
Damien Millera1939002008-03-15 17:27:58 +11001630 closefrom(STDERR_FILENO + 1);
1631
djm@openbsd.org7c856852018-03-03 03:15:51 +00001632 do_rc_files(ssh, s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001633
1634 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001635 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001636
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001637 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1638 printf("This service allows sftp connections only.\n");
1639 fflush(NULL);
1640 exit(1);
1641 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001642 extern int optind, optreset;
1643 int i;
1644 char *p, *args;
1645
Darren Tuckerac46a912009-06-21 17:55:23 +10001646 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001647 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001648 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1649 if (i < ARGV_MAX - 1)
1650 argv[i++] = p;
1651 argv[i] = NULL;
1652 optind = optreset = 1;
1653 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001654#ifdef WITH_SELINUX
1655 ssh_selinux_change_context("sftpd_t");
1656#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001657 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001658 }
1659
Darren Tucker695ed392009-10-07 09:02:18 +11001660 fflush(NULL);
1661
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001662 /* Get the last component of the shell name. */
1663 if ((shell0 = strrchr(shell, '/')) != NULL)
1664 shell0++;
1665 else
1666 shell0 = shell;
1667
Damien Millerb38eff82000-04-01 11:09:21 +10001668 /*
1669 * If we have no command, execute the shell. In this case, the shell
1670 * name to be passed in argv[0] is preceded by '-' to indicate that
1671 * this is a login shell.
1672 */
1673 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001674 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001675
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001676 /* Start the shell. Set initial character to '-'. */
1677 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001678
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001679 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1680 >= sizeof(argv0) - 1) {
1681 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001682 perror(shell);
1683 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001684 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001685
1686 /* Execute the shell. */
1687 argv[0] = argv0;
1688 argv[1] = NULL;
1689 execve(shell, argv, env);
1690
1691 /* Executing the shell failed. */
1692 perror(shell);
1693 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001694 }
1695 /*
1696 * Execute the command using the user's shell. This uses the -c
1697 * option to execute the command.
1698 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001699 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001700 argv[1] = "-c";
1701 argv[2] = (char *) command;
1702 argv[3] = NULL;
1703 execve(shell, argv, env);
1704 perror(shell);
1705 exit(1);
1706}
1707
Damien Miller7207f642008-05-19 15:34:50 +10001708void
1709session_unused(int id)
1710{
1711 debug3("%s: session id %d unused", __func__, id);
1712 if (id >= options.max_sessions ||
1713 id >= sessions_nalloc) {
1714 fatal("%s: insane session id %d (max %d nalloc %d)",
1715 __func__, id, options.max_sessions, sessions_nalloc);
1716 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001717 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001718 sessions[id].self = id;
1719 sessions[id].used = 0;
1720 sessions[id].chanid = -1;
1721 sessions[id].ptyfd = -1;
1722 sessions[id].ttyfd = -1;
1723 sessions[id].ptymaster = -1;
1724 sessions[id].x11_chanids = NULL;
1725 sessions[id].next_unused = sessions_first_unused;
1726 sessions_first_unused = id;
1727}
1728
Damien Millerb38eff82000-04-01 11:09:21 +10001729Session *
1730session_new(void)
1731{
Damien Miller7207f642008-05-19 15:34:50 +10001732 Session *s, *tmp;
1733
1734 if (sessions_first_unused == -1) {
1735 if (sessions_nalloc >= options.max_sessions)
1736 return NULL;
1737 debug2("%s: allocate (allocated %d max %d)",
1738 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001739 tmp = xrecallocarray(sessions, sessions_nalloc,
1740 sessions_nalloc + 1, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001741 if (tmp == NULL) {
1742 error("%s: cannot allocate %d sessions",
1743 __func__, sessions_nalloc + 1);
1744 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001745 }
Damien Miller7207f642008-05-19 15:34:50 +10001746 sessions = tmp;
1747 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001748 }
Damien Miller7207f642008-05-19 15:34:50 +10001749
1750 if (sessions_first_unused >= sessions_nalloc ||
1751 sessions_first_unused < 0) {
1752 fatal("%s: insane first_unused %d max %d nalloc %d",
1753 __func__, sessions_first_unused, options.max_sessions,
1754 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001755 }
Damien Miller7207f642008-05-19 15:34:50 +10001756
1757 s = &sessions[sessions_first_unused];
1758 if (s->used) {
1759 fatal("%s: session %d already used",
1760 __func__, sessions_first_unused);
1761 }
1762 sessions_first_unused = s->next_unused;
1763 s->used = 1;
1764 s->next_unused = -1;
1765 debug("session_new: session %d", s->self);
1766
1767 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001768}
1769
Ben Lindstrombba81212001-06-25 05:01:22 +00001770static void
Damien Millerb38eff82000-04-01 11:09:21 +10001771session_dump(void)
1772{
1773 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001774 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001775 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001776
1777 debug("dump: used %d next_unused %d session %d %p "
1778 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001779 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001780 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001781 s->self,
1782 s,
1783 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001784 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001785 }
1786}
1787
Damien Millerefb4afe2000-04-12 18:45:05 +10001788int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001789session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001790{
1791 Session *s = session_new();
1792 debug("session_open: channel %d", chanid);
1793 if (s == NULL) {
1794 error("no more sessions");
1795 return 0;
1796 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001797 s->authctxt = authctxt;
1798 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001799 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001800 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001801 debug("session_open: session %d: link with channel %d", s->self, chanid);
1802 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001803 return 1;
1804}
1805
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001806Session *
1807session_by_tty(char *tty)
1808{
1809 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001810 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001811 Session *s = &sessions[i];
1812 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1813 debug("session_by_tty: session %d tty %s", i, tty);
1814 return s;
1815 }
1816 }
1817 debug("session_by_tty: unknown tty %.100s", tty);
1818 session_dump();
1819 return NULL;
1820}
1821
Ben Lindstrombba81212001-06-25 05:01:22 +00001822static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001823session_by_channel(int id)
1824{
1825 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001826 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001827 Session *s = &sessions[i];
1828 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001829 debug("session_by_channel: session %d channel %d",
1830 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001831 return s;
1832 }
1833 }
1834 debug("session_by_channel: unknown channel %d", id);
1835 session_dump();
1836 return NULL;
1837}
1838
Ben Lindstrombba81212001-06-25 05:01:22 +00001839static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001840session_by_x11_channel(int id)
1841{
1842 int i, j;
1843
Damien Miller7207f642008-05-19 15:34:50 +10001844 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001845 Session *s = &sessions[i];
1846
1847 if (s->x11_chanids == NULL || !s->used)
1848 continue;
1849 for (j = 0; s->x11_chanids[j] != -1; j++) {
1850 if (s->x11_chanids[j] == id) {
1851 debug("session_by_x11_channel: session %d "
1852 "channel %d", s->self, id);
1853 return s;
1854 }
1855 }
1856 }
1857 debug("session_by_x11_channel: unknown channel %d", id);
1858 session_dump();
1859 return NULL;
1860}
1861
1862static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001863session_by_pid(pid_t pid)
1864{
1865 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001866 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001867 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001868 Session *s = &sessions[i];
1869 if (s->used && s->pid == pid)
1870 return s;
1871 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001872 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001873 session_dump();
1874 return NULL;
1875}
1876
Ben Lindstrombba81212001-06-25 05:01:22 +00001877static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001878session_window_change_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001879{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001880 int r;
1881
1882 if ((r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
1883 (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
1884 (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
1885 (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0 ||
1886 (r = sshpkt_get_end(ssh)) != 0)
1887 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10001888 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1889 return 1;
1890}
1891
Ben Lindstrombba81212001-06-25 05:01:22 +00001892static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001893session_pty_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001894{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001895 int r;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001896
djm@openbsd.org7c856852018-03-03 03:15:51 +00001897 if (!auth_opts->permit_pty_flag || !options.permit_tty) {
1898 debug("Allocating a pty not permitted for this connection.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001899 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001900 }
1901 if (s->ttyfd != -1) {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001902 ssh_packet_disconnect(ssh, "Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001903 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001904 }
1905
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001906 if ((r = sshpkt_get_cstring(ssh, &s->term, NULL)) != 0 ||
1907 (r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
1908 (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
1909 (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
1910 (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0)
1911 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10001912
1913 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001914 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001915 s->term = NULL;
1916 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001917
Damien Millerefb4afe2000-04-12 18:45:05 +10001918 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001919 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10001920 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1921 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10001922 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001923 s->term = NULL;
1924 s->ptyfd = -1;
1925 s->ttyfd = -1;
1926 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001927 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001928 }
1929 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001930
markus@openbsd.org89dd6152018-07-09 21:20:26 +00001931 ssh_tty_parse_modes(ssh, s->ttyfd);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001932
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001933 if ((r = sshpkt_get_end(ssh)) != 0)
1934 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
1935
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001936 if (!use_privsep)
1937 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001938
1939 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001940 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1941
Damien Millere247cc42000-05-07 12:03:14 +10001942 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001943 return 1;
1944}
1945
Ben Lindstrombba81212001-06-25 05:01:22 +00001946static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001947session_subsystem_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001948{
Damien Millerae452462001-10-10 15:08:06 +10001949 struct stat st;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001950 int r, success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11001951 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10001952 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001953
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001954 if ((r = sshpkt_get_cstring(ssh, &s->subsys, NULL)) != 0 ||
1955 (r = sshpkt_get_end(ssh)) != 0)
1956 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Miller71df7522013-10-15 12:12:02 +11001957 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10001958 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10001959
Damien Millerf6d9e222000-06-18 14:50:44 +10001960 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11001961 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001962 prog = options.subsystem_command[i];
1963 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001964 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11001965 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001966 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11001967 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001968 if (stat(prog, &st) < 0)
1969 debug("subsystem: cannot stat %s: %s",
1970 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001971 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001972 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001973 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001974 success = do_exec(ssh, s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11001975 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001976 }
1977 }
1978
1979 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11001980 logit("subsystem request for %.100s by user %s failed, "
1981 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10001982
Damien Millerbd483e72000-04-30 10:00:53 +10001983 return success;
1984}
1985
Ben Lindstrombba81212001-06-25 05:01:22 +00001986static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001987session_x11_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001988{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001989 int r, success;
1990 u_char single_connection = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001991
Damien Miller2b9b0452005-07-17 17:19:24 +10001992 if (s->auth_proto != NULL || s->auth_data != NULL) {
1993 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11001994 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10001995 return 0;
1996 }
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00001997 if ((r = sshpkt_get_u8(ssh, &single_connection)) != 0 ||
1998 (r = sshpkt_get_cstring(ssh, &s->auth_proto, NULL)) != 0 ||
1999 (r = sshpkt_get_cstring(ssh, &s->auth_data, NULL)) != 0 ||
2000 (r = sshpkt_get_u32(ssh, &s->screen)) != 0 ||
2001 (r = sshpkt_get_end(ssh)) != 0)
2002 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2003
2004 s->single_connection = single_connection;
Damien Millerbd483e72000-04-30 10:00:53 +10002005
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00002006 if (xauth_valid_string(s->auth_proto) &&
2007 xauth_valid_string(s->auth_data))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002008 success = session_setup_x11fwd(ssh, s);
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00002009 else {
2010 success = 0;
2011 error("Invalid X11 forwarding data");
2012 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002013 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002014 free(s->auth_proto);
2015 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002016 s->auth_proto = NULL;
2017 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002018 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002019 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002020}
2021
Ben Lindstrombba81212001-06-25 05:01:22 +00002022static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002023session_shell_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10002024{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002025 int r;
2026
2027 if ((r = sshpkt_get_end(ssh)) != 0)
2028 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002029 return do_exec(ssh, s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002030}
2031
Ben Lindstrombba81212001-06-25 05:01:22 +00002032static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002033session_exec_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10002034{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002035 u_int success;
2036 int r;
2037 char *command = NULL;
Damien Miller7207f642008-05-19 15:34:50 +10002038
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002039 if ((r = sshpkt_get_cstring(ssh, &command, NULL)) != 0 ||
2040 (r = sshpkt_get_end(ssh)) != 0)
2041 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2042
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002043 success = do_exec(ssh, s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002044 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002045 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002046}
2047
Ben Lindstrombba81212001-06-25 05:01:22 +00002048static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002049session_break_req(struct ssh *ssh, Session *s)
Damien Miller54c45982003-05-15 10:20:13 +10002050{
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002051 int r;
Damien Miller54c45982003-05-15 10:20:13 +10002052
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002053 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* ignore */
2054 (r = sshpkt_get_end(ssh)) != 0)
2055 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Damien Miller54c45982003-05-15 10:20:13 +10002056
Darren Tucker9c5d5532011-11-04 10:55:24 +11002057 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002058 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002059 return 1;
2060}
2061
2062static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002063session_env_req(struct ssh *ssh, Session *s)
Darren Tucker46bc0752004-05-02 22:11:30 +10002064{
2065 char *name, *val;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002066 u_int i;
2067 int r;
Darren Tucker46bc0752004-05-02 22:11:30 +10002068
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002069 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
2070 (r = sshpkt_get_cstring(ssh, &val, NULL)) != 0 ||
2071 (r = sshpkt_get_end(ssh)) != 0)
2072 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
Darren Tucker46bc0752004-05-02 22:11:30 +10002073
2074 /* Don't set too many environment variables */
2075 if (s->num_env > 128) {
2076 debug2("Ignoring env request %s: too many env vars", name);
2077 goto fail;
2078 }
2079
2080 for (i = 0; i < options.num_accept_env; i++) {
2081 if (match_pattern(name, options.accept_env[i])) {
2082 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002083 s->env = xrecallocarray(s->env, s->num_env,
2084 s->num_env + 1, sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002085 s->env[s->num_env].name = name;
2086 s->env[s->num_env].val = val;
2087 s->num_env++;
2088 return (1);
2089 }
2090 }
2091 debug2("Ignoring env request %s: disallowed name", name);
2092
2093 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002094 free(name);
2095 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002096 return (0);
2097}
2098
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002099/*
2100 * Conversion of signals from ssh channel request names.
2101 * Subset of signals from RFC 4254 section 6.10C, with SIGINFO as
2102 * local extension.
2103 */
2104static int
2105name2sig(char *name)
2106{
2107#define SSH_SIG(x) if (strcmp(name, #x) == 0) return SIG ## x
2108 SSH_SIG(HUP);
2109 SSH_SIG(INT);
2110 SSH_SIG(KILL);
2111 SSH_SIG(QUIT);
2112 SSH_SIG(TERM);
2113 SSH_SIG(USR1);
2114 SSH_SIG(USR2);
2115#undef SSH_SIG
Damien Millerff3a4112018-10-02 22:49:40 +10002116#ifdef SIGINFO
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002117 if (strcmp(name, "INFO@openssh.com") == 0)
2118 return SIGINFO;
Damien Millerff3a4112018-10-02 22:49:40 +10002119#endif
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002120 return -1;
2121}
2122
2123static int
2124session_signal_req(struct ssh *ssh, Session *s)
2125{
2126 char *signame = NULL;
2127 int r, sig, success = 0;
2128
2129 if ((r = sshpkt_get_cstring(ssh, &signame, NULL)) != 0 ||
2130 (r = sshpkt_get_end(ssh)) != 0) {
2131 error("%s: parse packet: %s", __func__, ssh_err(r));
2132 goto out;
2133 }
2134 if ((sig = name2sig(signame)) == -1) {
2135 error("%s: unsupported signal \"%s\"", __func__, signame);
2136 goto out;
2137 }
2138 if (s->pid <= 0) {
2139 error("%s: no pid for session %d", __func__, s->self);
2140 goto out;
2141 }
2142 if (s->forced || s->is_subsystem) {
2143 error("%s: refusing to send signal %s to %s session", __func__,
2144 signame, s->forced ? "forced-command" : "subsystem");
2145 goto out;
2146 }
2147 if (!use_privsep || mm_is_monitor()) {
2148 error("%s: session signalling requires privilege separation",
2149 __func__);
2150 goto out;
2151 }
2152
2153 debug("%s: signal %s, killpg(%ld, %d)", __func__, signame,
2154 (long)s->pid, sig);
2155 temporarily_use_uid(s->pw);
2156 r = killpg(s->pid, sig);
2157 restore_uid();
2158 if (r != 0) {
2159 error("%s: killpg(%ld, %d): %s", __func__, (long)s->pid,
2160 sig, strerror(errno));
2161 goto out;
2162 }
2163
2164 /* success */
2165 success = 1;
2166 out:
2167 free(signame);
2168 return success;
2169}
2170
Darren Tucker46bc0752004-05-02 22:11:30 +10002171static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002172session_auth_agent_req(struct ssh *ssh, Session *s)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002173{
2174 static int called = 0;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002175 int r;
djm@openbsd.org7c856852018-03-03 03:15:51 +00002176
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002177 if ((r = sshpkt_get_end(ssh)) != 0)
2178 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
djm@openbsd.org7c856852018-03-03 03:15:51 +00002179 if (!auth_opts->permit_agent_forwarding_flag ||
2180 !options.allow_agent_forwarding) {
2181 debug("%s: agent forwarding disabled", __func__);
Ben Lindstrom14920292000-11-21 21:24:55 +00002182 return 0;
2183 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002184 if (called) {
2185 return 0;
2186 } else {
2187 called = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002188 return auth_input_request_forwarding(ssh, s->pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002189 }
2190}
2191
Damien Millerc7ef63d2002-02-05 12:21:42 +11002192int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002193session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002194{
Damien Millerefb4afe2000-04-12 18:45:05 +10002195 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002196 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002197
Damien Millerc7ef63d2002-02-05 12:21:42 +11002198 if ((s = session_by_channel(c->self)) == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002199 logit("%s: no session %d req %.100s", __func__, c->self, rtype);
Damien Millerc7ef63d2002-02-05 12:21:42 +11002200 return 0;
2201 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002202 debug("%s: session %d req %s", __func__, s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002203
2204 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002205 * a session is in LARVAL state until a shell, a command
2206 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002207 */
2208 if (c->type == SSH_CHANNEL_LARVAL) {
2209 if (strcmp(rtype, "shell") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002210 success = session_shell_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002211 } else if (strcmp(rtype, "exec") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002212 success = session_exec_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002213 } else if (strcmp(rtype, "pty-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002214 success = session_pty_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002215 } else if (strcmp(rtype, "x11-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002216 success = session_x11_req(ssh, s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002217 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002218 success = session_auth_agent_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002219 } else if (strcmp(rtype, "subsystem") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002220 success = session_subsystem_req(ssh, s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002221 } else if (strcmp(rtype, "env") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002222 success = session_env_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002223 }
2224 }
2225 if (strcmp(rtype, "window-change") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002226 success = session_window_change_req(ssh, s);
Damien Millera6b1d162004-06-30 22:41:07 +10002227 } else if (strcmp(rtype, "break") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002228 success = session_break_req(ssh, s);
djm@openbsd.orgcd989252018-10-02 12:40:07 +00002229 } else if (strcmp(rtype, "signal") == 0) {
2230 success = session_signal_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002231 }
Damien Millera6b1d162004-06-30 22:41:07 +10002232
Damien Millerc7ef63d2002-02-05 12:21:42 +11002233 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002234}
2235
2236void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002237session_set_fds(struct ssh *ssh, Session *s,
2238 int fdin, int fdout, int fderr, int ignore_fderr, int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002239{
Damien Millerefb4afe2000-04-12 18:45:05 +10002240 /*
2241 * now that have a child and a pipe to the child,
2242 * we can activate our channel and register the fd's
2243 */
2244 if (s->chanid == -1)
2245 fatal("no channel for session %d", s->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002246 channel_set_fds(ssh, s->chanid,
Damien Millerefb4afe2000-04-12 18:45:05 +10002247 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002248 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002249 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002250}
2251
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002252/*
2253 * Function to perform pty cleanup. Also called if we get aborted abnormally
2254 * (e.g., due to a dropped connection).
2255 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002256void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002257session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002258{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002259 if (s == NULL) {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002260 error("%s: no session", __func__);
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002261 return;
2262 }
2263 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002264 return;
2265
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002266 debug("%s: session %d release %s", __func__, s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002267
Damien Millerb38eff82000-04-01 11:09:21 +10002268 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002269 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002270 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002271
2272 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002273 if (getuid() == 0)
2274 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002275
2276 /*
2277 * Close the server side of the socket pairs. We must do this after
2278 * the pty cleanup, so that another process doesn't get this pty
2279 * while we're still cleaning up.
2280 */
Damien Miller7207f642008-05-19 15:34:50 +10002281 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2282 error("close(s->ptymaster/%d): %s",
2283 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002284
2285 /* unlink pty from session */
2286 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002287}
Damien Millerefb4afe2000-04-12 18:45:05 +10002288
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002289void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002290session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002291{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002292 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002293}
2294
Damien Miller5a80bba2002-09-04 16:39:02 +10002295static char *
2296sig2name(int sig)
2297{
2298#define SSH_SIG(x) if (sig == SIG ## x) return #x
2299 SSH_SIG(ABRT);
2300 SSH_SIG(ALRM);
2301 SSH_SIG(FPE);
2302 SSH_SIG(HUP);
2303 SSH_SIG(ILL);
2304 SSH_SIG(INT);
2305 SSH_SIG(KILL);
2306 SSH_SIG(PIPE);
2307 SSH_SIG(QUIT);
2308 SSH_SIG(SEGV);
2309 SSH_SIG(TERM);
2310 SSH_SIG(USR1);
2311 SSH_SIG(USR2);
2312#undef SSH_SIG
2313 return "SIG@openssh.com";
2314}
2315
Ben Lindstrombba81212001-06-25 05:01:22 +00002316static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002317session_close_x11(struct ssh *ssh, int id)
Damien Miller2b9b0452005-07-17 17:19:24 +10002318{
2319 Channel *c;
2320
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002321 if ((c = channel_by_id(ssh, id)) == NULL) {
2322 debug("%s: x11 channel %d missing", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002323 } else {
2324 /* Detach X11 listener */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002325 debug("%s: detach x11 channel %d", __func__, id);
2326 channel_cancel_cleanup(ssh, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002327 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002328 chan_mark_dead(ssh, c);
Damien Miller2b9b0452005-07-17 17:19:24 +10002329 }
2330}
2331
2332static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002333session_close_single_x11(struct ssh *ssh, int id, void *arg)
Damien Miller2b9b0452005-07-17 17:19:24 +10002334{
2335 Session *s;
2336 u_int i;
2337
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002338 debug3("%s: channel %d", __func__, id);
2339 channel_cancel_cleanup(ssh, id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002340 if ((s = session_by_x11_channel(id)) == NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002341 fatal("%s: no x11 channel %d", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002342 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002343 debug("%s: session %d: closing channel %d",
2344 __func__, s->self, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002345 /*
2346 * The channel "id" is already closing, but make sure we
2347 * close all of its siblings.
2348 */
2349 if (s->x11_chanids[i] != id)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002350 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002351 }
Darren Tuckera627d422013-06-02 07:31:17 +10002352 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002353 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002354 free(s->display);
2355 s->display = NULL;
2356 free(s->auth_proto);
2357 s->auth_proto = NULL;
2358 free(s->auth_data);
2359 s->auth_data = NULL;
2360 free(s->auth_display);
2361 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002362}
2363
2364static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002365session_exit_message(struct ssh *ssh, Session *s, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002366{
2367 Channel *c;
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002368 int r;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002369
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002370 if ((c = channel_lookup(ssh, s->chanid)) == NULL)
2371 fatal("%s: session %d: no channel %d",
2372 __func__, s->self, s->chanid);
2373 debug("%s: session %d channel %d pid %ld",
2374 __func__, s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002375
2376 if (WIFEXITED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002377 channel_request_start(ssh, s->chanid, "exit-status", 0);
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002378 if ((r = sshpkt_put_u32(ssh, WEXITSTATUS(status))) != 0 ||
2379 (r = sshpkt_send(ssh)) != 0)
2380 sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10002381 } else if (WIFSIGNALED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002382 channel_request_start(ssh, s->chanid, "exit-signal", 0);
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002383#ifndef WCOREDUMP
2384# define WCOREDUMP(x) (0)
2385#endif
2386 if ((r = sshpkt_put_cstring(ssh, sig2name(WTERMSIG(status)))) != 0 ||
2387 (r = sshpkt_put_u8(ssh, WCOREDUMP(status)? 1 : 0)) != 0 ||
2388 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2389 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2390 (r = sshpkt_send(ssh)) != 0)
2391 sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
Damien Millerefb4afe2000-04-12 18:45:05 +10002392 } else {
2393 /* Some weird exit cause. Just exit. */
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002394 ssh_packet_disconnect(ssh, "wait returned status %04x.", status);
Damien Millerefb4afe2000-04-12 18:45:05 +10002395 }
2396
2397 /* disconnect channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002398 debug("%s: release channel %d", __func__, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002399
2400 /*
2401 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002402 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002403 * by session_close_by_channel when the childs close their fds.
2404 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002405 channel_register_cleanup(ssh, c->self, session_close_by_channel, 1);
Damien Miller39eda6e2005-11-05 14:52:50 +11002406
Damien Miller166fca82000-04-20 07:42:21 +10002407 /*
2408 * emulate a write failure with 'chan_write_failed', nobody will be
2409 * interested in data we write.
2410 * Note that we must not call 'chan_read_failed', since there could
2411 * be some more data waiting in the pipe.
2412 */
Damien Millerbd483e72000-04-30 10:00:53 +10002413 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002414 chan_write_failed(ssh, c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002415}
2416
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002417void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002418session_close(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002419{
Damien Millereccb9de2005-06-17 12:59:34 +10002420 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002421
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002422 verbose("Close session: user %s from %.200s port %d id %d",
2423 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002424 ssh_remote_ipaddr(ssh),
2425 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002426 s->self);
2427
Darren Tucker3e33cec2003-10-02 16:12:36 +10002428 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002429 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002430 free(s->term);
2431 free(s->display);
2432 free(s->x11_chanids);
2433 free(s->auth_display);
2434 free(s->auth_data);
2435 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002436 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002437 if (s->env != NULL) {
2438 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002439 free(s->env[i].name);
2440 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002441 }
Darren Tuckera627d422013-06-02 07:31:17 +10002442 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002443 }
Damien Millere247cc42000-05-07 12:03:14 +10002444 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002445 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002446}
2447
2448void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002449session_close_by_pid(struct ssh *ssh, pid_t pid, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002450{
2451 Session *s = session_by_pid(pid);
2452 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002453 debug("%s: no session for pid %ld", __func__, (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002454 return;
2455 }
2456 if (s->chanid != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002457 session_exit_message(ssh, s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002458 if (s->ttyfd != -1)
2459 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002460 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002461}
2462
2463/*
2464 * this is called when a channel dies before
2465 * the session 'child' itself dies
2466 */
2467void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002468session_close_by_channel(struct ssh *ssh, int id, void *arg)
Damien Millerefb4afe2000-04-12 18:45:05 +10002469{
2470 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002471 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002472
Damien Millerefb4afe2000-04-12 18:45:05 +10002473 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002474 debug("%s: no session for id %d", __func__, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002475 return;
2476 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002477 debug("%s: channel %d child %ld", __func__, id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002478 if (s->pid != 0) {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002479 debug("%s: channel %d: has child, ttyfd %d",
2480 __func__, id, s->ttyfd);
Damien Miller0585d512001-10-12 11:35:50 +10002481 /*
2482 * delay detach of session, but release pty, since
2483 * the fd's to the child are already closed
2484 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002485 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002486 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002487 return;
2488 }
2489 /* detach by removing callback */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002490 channel_cancel_cleanup(ssh, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002491
2492 /* Close any X11 listeners associated with this session */
2493 if (s->x11_chanids != NULL) {
2494 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002495 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller39eda6e2005-11-05 14:52:50 +11002496 s->x11_chanids[i] = -1;
2497 }
2498 }
2499
Damien Millerefb4afe2000-04-12 18:45:05 +10002500 s->chanid = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002501 session_close(ssh, s);
Damien Miller52b77be2001-10-10 15:14:37 +10002502}
2503
2504void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002505session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002506{
2507 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002508 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002509 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002510 if (s->used) {
2511 if (closefunc != NULL)
2512 closefunc(s);
2513 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002514 session_close(ssh, s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002515 }
Damien Miller52b77be2001-10-10 15:14:37 +10002516 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002517}
2518
Ben Lindstrombba81212001-06-25 05:01:22 +00002519static char *
Damien Millere247cc42000-05-07 12:03:14 +10002520session_tty_list(void)
2521{
2522 static char buf[1024];
2523 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002524 char *cp;
2525
Damien Millere247cc42000-05-07 12:03:14 +10002526 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002527 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002528 Session *s = &sessions[i];
2529 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002530
Damien Millera8ed44b2003-01-10 09:53:12 +11002531 if (strncmp(s->tty, "/dev/", 5) != 0) {
2532 cp = strrchr(s->tty, '/');
2533 cp = (cp == NULL) ? s->tty : cp + 1;
2534 } else
2535 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002536
Damien Millere247cc42000-05-07 12:03:14 +10002537 if (buf[0] != '\0')
2538 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002539 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002540 }
2541 }
2542 if (buf[0] == '\0')
2543 strlcpy(buf, "notty", sizeof buf);
2544 return buf;
2545}
2546
2547void
2548session_proctitle(Session *s)
2549{
2550 if (s->pw == NULL)
2551 error("no user for session %d", s->self);
2552 else
2553 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2554}
2555
Ben Lindstrom768176b2001-06-09 01:29:12 +00002556int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002557session_setup_x11fwd(struct ssh *ssh, Session *s)
Ben Lindstrom768176b2001-06-09 01:29:12 +00002558{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002559 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002560 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002561 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002562 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002563
djm@openbsd.org7c856852018-03-03 03:15:51 +00002564 if (!auth_opts->permit_x11_forwarding_flag) {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002565 ssh_packet_send_debug(ssh, "X11 forwarding disabled by key options.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002566 return 0;
2567 }
2568 if (!options.x11_forwarding) {
2569 debug("X11 forwarding disabled in server configuration file.");
2570 return 0;
2571 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002572 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002573 (stat(options.xauth_location, &st) == -1)) {
djm@openbsd.orga5e2ad82019-01-19 21:41:53 +00002574 ssh_packet_send_debug(ssh, "No xauth program; cannot forward X11.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002575 return 0;
2576 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002577 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002578 debug("X11 display already set.");
2579 return 0;
2580 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002581 if (x11_create_display_inet(ssh, options.x11_display_offset,
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002582 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002583 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002584 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002585 return 0;
2586 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002587 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002588 channel_register_cleanup(ssh, s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002589 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002590 }
Kevin Steves366298c2001-12-19 17:58:01 +00002591
2592 /* Set up a suitable value for the DISPLAY variable. */
2593 if (gethostname(hostname, sizeof(hostname)) < 0)
2594 fatal("gethostname: %.100s", strerror(errno));
2595 /*
2596 * auth_display must be used as the displayname when the
2597 * authorization entry is added with xauth(1). This will be
2598 * different than the DISPLAY string for localhost displays.
2599 */
Damien Miller95c249f2002-02-05 12:11:34 +11002600 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002601 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002602 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002603 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002604 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002605 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002606 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002607 } else {
2608#ifdef IPADDR_IN_DISPLAY
2609 struct hostent *he;
2610 struct in_addr my_addr;
2611
2612 he = gethostbyname(hostname);
2613 if (he == NULL) {
2614 error("Can't get IP address for X11 DISPLAY.");
2615 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2616 return 0;
2617 }
2618 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002619 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002620 s->display_number, s->screen);
2621#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002622 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002623 s->display_number, s->screen);
2624#endif
2625 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002626 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002627 }
2628
Ben Lindstrom768176b2001-06-09 01:29:12 +00002629 return 1;
2630}
2631
Ben Lindstrombba81212001-06-25 05:01:22 +00002632static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002633do_authenticated2(struct ssh *ssh, Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002634{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002635 server_loop2(ssh, authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002636}
2637
2638void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002639do_cleanup(struct ssh *ssh, Authctxt *authctxt)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002640{
2641 static int called = 0;
2642
2643 debug("do_cleanup");
2644
2645 /* no cleanup if we're in the child for login shell */
2646 if (is_child)
2647 return;
2648
2649 /* avoid double cleanup */
2650 if (called)
2651 return;
2652 called = 1;
2653
Darren Tucker9142e1c2007-08-16 23:28:04 +10002654 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002655 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002656
2657#ifdef USE_PAM
2658 if (options.use_pam) {
2659 sshpam_cleanup();
2660 sshpam_thread_cleanup();
2661 }
2662#endif
2663
2664 if (!authctxt->authenticated)
2665 return;
2666
Darren Tucker3e33cec2003-10-02 16:12:36 +10002667#ifdef KRB5
2668 if (options.kerberos_ticket_cleanup &&
2669 authctxt->krb5_ctx)
2670 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002671#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002672
2673#ifdef GSSAPI
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002674 if (options.gss_cleanup_creds)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002675 ssh_gssapi_cleanup_creds();
2676#endif
2677
2678 /* remove agent socket */
2679 auth_sock_cleanup_proc(authctxt->pw);
2680
djm@openbsd.org8f574952017-06-24 06:34:38 +00002681 /* remove userauth info */
2682 if (auth_info_file != NULL) {
2683 temporarily_use_uid(authctxt->pw);
2684 unlink(auth_info_file);
2685 restore_uid();
2686 free(auth_info_file);
2687 auth_info_file = NULL;
2688 }
2689
Darren Tucker3e33cec2003-10-02 16:12:36 +10002690 /*
2691 * Cleanup ptys/utmp only if privsep is disabled,
2692 * or if running in monitor.
2693 */
2694 if (!use_privsep || mm_is_monitor())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002695 session_destroy_all(ssh, session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002696}
djm@openbsd.org95767262016-03-07 19:02:43 +00002697
2698/* Return a name for the remote host that fits inside utmp_size */
2699
2700const char *
2701session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2702{
2703 const char *remote = "";
2704
2705 if (utmp_size > 0)
2706 remote = auth_get_canonical_hostname(ssh, use_dns);
2707 if (utmp_size == 0 || strlen(remote) > utmp_size)
2708 remote = ssh_remote_ipaddr(ssh);
2709 return remote;
2710}
2711