blob: f2cf52006e4305ced72d99f1f92796c3608355ac [file] [log] [blame]
deraadt@openbsd.org21fd4772018-07-25 13:56:23 +00001/* $OpenBSD: session.c,v 1.305 2018/07/25 13:56:23 deraadt Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +000049#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100051#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100052#include <grp.h>
Damien Millere5c0d522014-07-03 21:24:19 +100053#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110055#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100057#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110058#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100059#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100060#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100061#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100062#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100063#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000064#include <limits.h>
Damien Millerb38eff82000-04-01 11:09:21 +100065
Damien Millerb84886b2008-05-19 15:05:07 +100066#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100068#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000070#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "packet.h"
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 *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000126#ifdef LOGIN_NEEDS_UTMPX
127static void do_pre_login(Session *s);
128#endif
Damien Millercf205e82001-04-16 18:29:15 +1000129void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000130int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000131
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000132static void do_authenticated2(struct ssh *, Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000133
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000134static int session_pty_req(struct ssh *, Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000135
Damien Millerb38eff82000-04-01 11:09:21 +1000136/* import */
137extern ServerOptions options;
138extern char *__progname;
Damien Millerb38eff82000-04-01 11:09:21 +1000139extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000140extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000141extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000142extern void destroy_sensitive_data(void);
markus@openbsd.org2808d182018-07-09 21:26:02 +0000143extern struct sshbuf *loginmsg;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000144extern struct sshauthopt *auth_opts;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000145char *tun_fwd_ifnames; /* serverloop.c */
Damien Miller37023962000-07-11 17:31:38 +1000146
Damien Miller7b28dc52000-09-05 13:34:53 +1100147/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000148const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100149
Damien Millerb38eff82000-04-01 11:09:21 +1000150/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000151static int sessions_first_unused = -1;
152static int sessions_nalloc = 0;
153static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100154
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100155#define SUBSYSTEM_NONE 0
156#define SUBSYSTEM_EXT 1
157#define SUBSYSTEM_INT_SFTP 2
158#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100159
Damien Millerad833b32000-08-23 10:46:23 +1000160#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000161login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000162#endif
163
Darren Tucker3e33cec2003-10-02 16:12:36 +1000164static int is_child = 0;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +0000165static int in_chroot = 0;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000166
djm@openbsd.org8f574952017-06-24 06:34:38 +0000167/* File containing userauth info, if ExposeAuthInfo set */
168static char *auth_info_file = NULL;
169
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000170/* Name and directory of socket for authentication agent forwarding. */
171static char *auth_sock_name = NULL;
172static char *auth_sock_dir = NULL;
173
174/* removes the agent forwarding socket */
175
176static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000177auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000178{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000179 if (auth_sock_name != NULL) {
180 temporarily_use_uid(pw);
181 unlink(auth_sock_name);
182 rmdir(auth_sock_dir);
183 auth_sock_name = NULL;
184 restore_uid();
185 }
186}
187
188static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000189auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000190{
191 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000192 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000193
194 if (auth_sock_name != NULL) {
195 error("authentication forwarding requested twice.");
196 return 0;
197 }
198
199 /* Temporarily drop privileged uid for mkdir/bind. */
200 temporarily_use_uid(pw);
201
202 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000203 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000204
205 /* Create private directory for socket */
206 if (mkdtemp(auth_sock_dir) == NULL) {
207 packet_send_debug("Agent forwarding disabled: "
208 "mkdtemp() failed: %.100s", strerror(errno));
209 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000210 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000211 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000212 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000213 }
Damien Miller7207f642008-05-19 15:34:50 +1000214
215 xasprintf(&auth_sock_name, "%s/agent.%ld",
216 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000217
Damien Miller7acefbb2014-07-18 14:11:24 +1000218 /* Start a Unix listener on auth_sock_name. */
219 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000220
221 /* Restore the privileged uid. */
222 restore_uid();
223
Damien Miller7acefbb2014-07-18 14:11:24 +1000224 /* Check for socket/bind/listen failure. */
225 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000226 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000227
228 /* Allocate a channel for the authentication agent socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000229 nc = channel_new(ssh, "auth socket",
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000230 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
231 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000232 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100233 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000234 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000235
236 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000237 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000238 if (auth_sock_dir != NULL) {
239 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000240 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000241 }
242 if (sock != -1)
243 close(sock);
244 auth_sock_name = NULL;
245 auth_sock_dir = NULL;
246 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000247}
248
Darren Tucker1921ed92004-02-10 13:23:28 +1100249static void
250display_loginmsg(void)
251{
markus@openbsd.org2808d182018-07-09 21:26:02 +0000252 int r;
253
254 if (sshbuf_len(loginmsg) == 0)
255 return;
256 if ((r = sshbuf_put_u8(loginmsg, 0)) != 0)
257 fatal("%s: buffer error: %s", __func__, ssh_err(r));
258 printf("%s", (char *)sshbuf_ptr(loginmsg));
259 sshbuf_reset(loginmsg);
Darren Tucker1921ed92004-02-10 13:23:28 +1100260}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000261
djm@openbsd.org8f574952017-06-24 06:34:38 +0000262static void
263prepare_auth_info_file(struct passwd *pw, struct sshbuf *info)
264{
265 int fd = -1, success = 0;
266
267 if (!options.expose_userauth_info || info == NULL)
268 return;
269
270 temporarily_use_uid(pw);
271 auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX");
272 if ((fd = mkstemp(auth_info_file)) == -1) {
273 error("%s: mkstemp: %s", __func__, strerror(errno));
274 goto out;
275 }
276 if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info),
277 sshbuf_len(info)) != sshbuf_len(info)) {
278 error("%s: write: %s", __func__, strerror(errno));
279 goto out;
280 }
281 if (close(fd) != 0) {
282 error("%s: close: %s", __func__, strerror(errno));
283 goto out;
284 }
285 success = 1;
286 out:
287 if (!success) {
288 if (fd != -1)
289 close(fd);
290 free(auth_info_file);
291 auth_info_file = NULL;
292 }
293 restore_uid();
294}
295
djm@openbsd.org7c856852018-03-03 03:15:51 +0000296static void
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000297set_fwdpermit_from_authopts(struct ssh *ssh, const struct sshauthopt *opts)
djm@openbsd.org7c856852018-03-03 03:15:51 +0000298{
299 char *tmp, *cp, *host;
300 int port;
301 size_t i;
302
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000303 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0) {
304 channel_clear_permission(ssh, FORWARD_USER, FORWARD_LOCAL);
305 for (i = 0; i < auth_opts->npermitopen; i++) {
306 tmp = cp = xstrdup(auth_opts->permitopen[i]);
307 /* This shouldn't fail as it has already been checked */
308 if ((host = hpdelim(&cp)) == NULL)
309 fatal("%s: internal error: hpdelim", __func__);
310 host = cleanhostname(host);
311 if (cp == NULL || (port = permitopen_port(cp)) < 0)
312 fatal("%s: internal error: permitopen port",
313 __func__);
314 channel_add_permission(ssh,
315 FORWARD_USER, FORWARD_LOCAL, host, port);
316 free(tmp);
317 }
318 }
319 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) != 0) {
320 channel_clear_permission(ssh, FORWARD_USER, FORWARD_REMOTE);
321 for (i = 0; i < auth_opts->npermitlisten; i++) {
322 tmp = cp = xstrdup(auth_opts->permitlisten[i]);
323 /* This shouldn't fail as it has already been checked */
324 if ((host = hpdelim(&cp)) == NULL)
325 fatal("%s: internal error: hpdelim", __func__);
326 host = cleanhostname(host);
327 if (cp == NULL || (port = permitopen_port(cp)) < 0)
328 fatal("%s: internal error: permitlisten port",
329 __func__);
330 channel_add_permission(ssh,
331 FORWARD_USER, FORWARD_REMOTE, host, port);
332 free(tmp);
333 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000334 }
335}
336
Ben Lindstromb31783d2001-03-22 02:02:12 +0000337void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000338do_authenticated(struct ssh *ssh, Authctxt *authctxt)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000339{
Damien Miller97f39ae2003-02-24 11:57:01 +1100340 setproctitle("%s", authctxt->pw->pw_name);
341
djm@openbsd.org7c856852018-03-03 03:15:51 +0000342 auth_log_authopts("active", auth_opts, 0);
343
Ben Lindstromb31783d2001-03-22 02:02:12 +0000344 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000345 /* XXX - streamlocal? */
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000346 set_fwdpermit_from_authopts(ssh, auth_opts);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000347
djm@openbsd.org115063a2018-06-06 18:22:41 +0000348 if (!auth_opts->permit_port_forwarding_flag ||
349 options.disable_forwarding) {
350 channel_disable_admin(ssh, FORWARD_LOCAL);
351 channel_disable_admin(ssh, FORWARD_REMOTE);
352 } else {
353 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
354 channel_disable_admin(ssh, FORWARD_LOCAL);
355 else
356 channel_permit_all(ssh, FORWARD_LOCAL);
357 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0)
358 channel_disable_admin(ssh, FORWARD_REMOTE);
359 else
360 channel_permit_all(ssh, FORWARD_REMOTE);
361 }
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100362 auth_debug_send();
363
djm@openbsd.org8f574952017-06-24 06:34:38 +0000364 prepare_auth_info_file(authctxt->pw, authctxt->session_info);
365
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000366 do_authenticated2(ssh, authctxt);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000367
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000368 do_cleanup(ssh, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000369}
370
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000371/* Check untrusted xauth strings for metacharacters */
372static int
373xauth_valid_string(const char *s)
374{
375 size_t i;
376
377 for (i = 0; s[i] != '\0'; i++) {
378 if (!isalnum((u_char)s[i]) &&
379 s[i] != '.' && s[i] != ':' && s[i] != '/' &&
380 s[i] != '-' && s[i] != '_')
deraadt@openbsd.org21fd4772018-07-25 13:56:23 +0000381 return 0;
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000382 }
383 return 1;
384}
385
Darren Tucker293ee3c2014-01-19 15:28:01 +1100386#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000387/*
388 * This is called to fork and execute a command when we have no tty. This
389 * will call do_child from the child, and server_loop from the parent after
390 * setting up file descriptors and such.
391 */
Damien Miller7207f642008-05-19 15:34:50 +1000392int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000393do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000394{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000395 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000396#ifdef USE_PIPES
397 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000398
Damien Miller22a29882010-05-10 11:53:54 +1000399 if (s == NULL)
400 fatal("do_exec_no_pty: no session");
401
Damien Millerb38eff82000-04-01 11:09:21 +1000402 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000403 if (pipe(pin) < 0) {
404 error("%s: pipe in: %.100s", __func__, strerror(errno));
405 return -1;
406 }
407 if (pipe(pout) < 0) {
408 error("%s: pipe out: %.100s", __func__, strerror(errno));
409 close(pin[0]);
410 close(pin[1]);
411 return -1;
412 }
Damien Miller8853ca52010-06-26 10:00:14 +1000413 if (pipe(perr) < 0) {
414 error("%s: pipe err: %.100s", __func__,
415 strerror(errno));
416 close(pin[0]);
417 close(pin[1]);
418 close(pout[0]);
419 close(pout[1]);
420 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000421 }
422#else
Damien Millerb38eff82000-04-01 11:09:21 +1000423 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000424
Damien Miller22a29882010-05-10 11:53:54 +1000425 if (s == NULL)
426 fatal("do_exec_no_pty: no session");
427
Damien Millerb38eff82000-04-01 11:09:21 +1000428 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000429 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
430 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
431 return -1;
432 }
Damien Miller8853ca52010-06-26 10:00:14 +1000433 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
434 error("%s: socketpair #2: %.100s", __func__,
435 strerror(errno));
436 close(inout[0]);
437 close(inout[1]);
438 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000439 }
440#endif
441
Damien Millere247cc42000-05-07 12:03:14 +1000442 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000443
Damien Millerb38eff82000-04-01 11:09:21 +1000444 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000445 switch ((pid = fork())) {
446 case -1:
447 error("%s: fork: %.100s", __func__, strerror(errno));
448#ifdef USE_PIPES
449 close(pin[0]);
450 close(pin[1]);
451 close(pout[0]);
452 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000453 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000454 close(perr[1]);
455#else
456 close(inout[0]);
457 close(inout[1]);
458 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000459 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000460#endif
461 return -1;
462 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000463 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000464
Damien Millerb38eff82000-04-01 11:09:21 +1000465 /*
466 * Create a new session and process group since the 4.4BSD
467 * setlogin() affects the entire process group.
468 */
469 if (setsid() < 0)
470 error("setsid failed: %.100s", strerror(errno));
471
472#ifdef USE_PIPES
473 /*
474 * Redirect stdin. We close the parent side of the socket
475 * pair, and make the child side the standard input.
476 */
477 close(pin[1]);
478 if (dup2(pin[0], 0) < 0)
479 perror("dup2 stdin");
480 close(pin[0]);
481
482 /* Redirect stdout. */
483 close(pout[0]);
484 if (dup2(pout[1], 1) < 0)
485 perror("dup2 stdout");
486 close(pout[1]);
487
488 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000489 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000490 if (dup2(perr[1], 2) < 0)
491 perror("dup2 stderr");
492 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000493#else
Damien Millerb38eff82000-04-01 11:09:21 +1000494 /*
495 * Redirect stdin, stdout, and stderr. Stdin and stdout will
496 * use the same socket, as some programs (particularly rdist)
497 * seem to depend on it.
498 */
499 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000500 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000501 if (dup2(inout[0], 0) < 0) /* stdin */
502 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000503 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000504 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000505 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000506 if (dup2(err[0], 2) < 0) /* stderr */
507 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000508 close(err[0]);
509#endif
510
Damien Millerb38eff82000-04-01 11:09:21 +1000511 /* Do processing for the child (exec command etc). */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000512 do_child(ssh, s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000513 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000514 default:
515 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000516 }
Damien Miller7207f642008-05-19 15:34:50 +1000517
Damien Millerbac2d8a2000-09-05 16:13:06 +1100518#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100519 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100520#endif
Damien Miller7207f642008-05-19 15:34:50 +1000521
Damien Millerb38eff82000-04-01 11:09:21 +1000522 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000523 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100524 packet_set_interactive(s->display != NULL,
525 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000526
527 /*
528 * Clear loginmsg, since it's the child's responsibility to display
529 * it to the user, otherwise multiple sessions may accumulate
530 * multiple copies of the login messages.
531 */
Damien Miller120a1ec2018-07-10 19:39:52 +1000532 sshbuf_reset(loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000533
Damien Millerb38eff82000-04-01 11:09:21 +1000534#ifdef USE_PIPES
535 /* We are the parent. Close the child sides of the pipes. */
536 close(pin[0]);
537 close(pout[1]);
538 close(perr[1]);
539
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000540 session_set_fds(ssh, s, pin[1], pout[0], perr[0],
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000541 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000542#else
Damien Millerb38eff82000-04-01 11:09:21 +1000543 /* We are the parent. Close the child sides of the socket pairs. */
544 close(inout[0]);
545 close(err[0]);
546
547 /*
548 * Enter the interactive session. Note: server_loop must be able to
549 * handle the case that fdin and fdout are the same.
550 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000551 session_set_fds(s, inout[1], inout[1], err[1],
552 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000553#endif
554 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000555}
556
557/*
558 * This is called to fork and execute a command when we have a tty. This
559 * will call do_child from the child, and server_loop from the parent after
560 * setting up file descriptors, controlling tty, updating wtmp, utmp,
561 * lastlog, and other such operations.
562 */
Damien Miller7207f642008-05-19 15:34:50 +1000563int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000564do_exec_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000565{
Damien Millerb38eff82000-04-01 11:09:21 +1000566 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000567 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000568
569 if (s == NULL)
570 fatal("do_exec_pty: no session");
571 ptyfd = s->ptyfd;
572 ttyfd = s->ttyfd;
573
Damien Miller7207f642008-05-19 15:34:50 +1000574 /*
575 * Create another descriptor of the pty master side for use as the
576 * standard input. We could use the original descriptor, but this
577 * simplifies code in server_loop. The descriptor is bidirectional.
578 * Do this before forking (and cleanup in the child) so as to
579 * detect and gracefully fail out-of-fd conditions.
580 */
581 if ((fdout = dup(ptyfd)) < 0) {
582 error("%s: dup #1: %s", __func__, strerror(errno));
583 close(ttyfd);
584 close(ptyfd);
585 return -1;
586 }
587 /* we keep a reference to the pty master */
588 if ((ptymaster = dup(ptyfd)) < 0) {
589 error("%s: dup #2: %s", __func__, strerror(errno));
590 close(ttyfd);
591 close(ptyfd);
592 close(fdout);
593 return -1;
594 }
595
Damien Millerb38eff82000-04-01 11:09:21 +1000596 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000597 switch ((pid = fork())) {
598 case -1:
599 error("%s: fork: %.100s", __func__, strerror(errno));
600 close(fdout);
601 close(ptymaster);
602 close(ttyfd);
603 close(ptyfd);
604 return -1;
605 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000606 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000607
Damien Miller7207f642008-05-19 15:34:50 +1000608 close(fdout);
609 close(ptymaster);
610
Damien Millerb38eff82000-04-01 11:09:21 +1000611 /* Close the master side of the pseudo tty. */
612 close(ptyfd);
613
614 /* Make the pseudo tty our controlling tty. */
615 pty_make_controlling_tty(&ttyfd, s->tty);
616
Damien Miller9c751422001-10-10 15:02:46 +1000617 /* Redirect stdin/stdout/stderr from the pseudo tty. */
618 if (dup2(ttyfd, 0) < 0)
619 error("dup2 stdin: %s", strerror(errno));
620 if (dup2(ttyfd, 1) < 0)
621 error("dup2 stdout: %s", strerror(errno));
622 if (dup2(ttyfd, 2) < 0)
623 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000624
625 /* Close the extra descriptor for the pseudo tty. */
626 close(ttyfd);
627
Damien Miller942da032000-08-18 13:59:06 +1000628 /* record login, etc. similar to login(1) */
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000629#ifndef HAVE_OSF_SIA
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000630 do_login(ssh, s, command);
Damien Miller364a9bd2001-04-16 18:37:05 +1000631#endif
Damien Miller7207f642008-05-19 15:34:50 +1000632 /*
633 * Do common processing for the child, such as execing
634 * the command.
635 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000636 do_child(ssh, s, command);
Darren Tucker43e7a352009-06-21 19:50:08 +1000637 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000638 default:
639 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000640 }
Damien Miller7207f642008-05-19 15:34:50 +1000641
Damien Millerbac2d8a2000-09-05 16:13:06 +1100642#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100643 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100644#endif
Damien Miller7207f642008-05-19 15:34:50 +1000645
Damien Millerb38eff82000-04-01 11:09:21 +1000646 s->pid = pid;
647
648 /* Parent. Close the slave side of the pseudo tty. */
649 close(ttyfd);
650
Damien Millerb38eff82000-04-01 11:09:21 +1000651 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000652 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100653 packet_set_interactive(1,
654 options.ip_qos_interactive, options.ip_qos_bulk);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000655 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
Damien Miller7207f642008-05-19 15:34:50 +1000656 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000657}
658
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000659#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000660static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000661do_pre_login(Session *s)
662{
Darren Tuckeraa377682016-06-20 15:55:34 +1000663 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000664 socklen_t fromlen;
665 struct sockaddr_storage from;
666 pid_t pid = getpid();
667
668 /*
669 * Get IP address of client. If the connection is not a socket, let
670 * the address be 0.0.0.0.
671 */
672 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000673 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000674 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000675 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100676 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000677 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000678 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000679 }
680 }
681
682 record_utmp_only(pid, s->tty, s->pw->pw_name,
Darren Tuckeraa377682016-06-20 15:55:34 +1000683 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000684 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000685}
686#endif
687
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000688/*
689 * This is called to fork and execute a command. If another command is
690 * to be forced, execute that instead.
691 */
Damien Miller7207f642008-05-19 15:34:50 +1000692int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000693do_exec(struct ssh *ssh, Session *s, const char *command)
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000694{
Damien Miller7207f642008-05-19 15:34:50 +1000695 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000696 const char *forced = NULL, *tty = NULL;
697 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000698
Damien Millere2754432006-07-24 14:06:47 +1000699 if (options.adm_forced_command) {
700 original_command = command;
701 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100702 forced = "(config)";
djm@openbsd.org7c856852018-03-03 03:15:51 +0000703 } else if (auth_opts->force_command != NULL) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000704 original_command = command;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000705 command = auth_opts->force_command;
Damien Miller71df7522013-10-15 12:12:02 +1100706 forced = "(key-option)";
707 }
708 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100709 if (IS_INTERNAL_SFTP(command)) {
710 s->is_subsystem = s->is_subsystem ?
711 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
712 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100713 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100714 snprintf(session_type, sizeof(session_type),
715 "forced-command %s '%.900s'", forced, command);
716 } else if (s->is_subsystem) {
717 snprintf(session_type, sizeof(session_type),
718 "subsystem '%.900s'", s->subsys);
719 } else if (command == NULL) {
720 snprintf(session_type, sizeof(session_type), "shell");
721 } else {
722 /* NB. we don't log unforced commands to preserve privacy */
723 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000724 }
725
Damien Miller71df7522013-10-15 12:12:02 +1100726 if (s->ttyfd != -1) {
727 tty = s->tty;
728 if (strncmp(tty, "/dev/", 5) == 0)
729 tty += 5;
730 }
731
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000732 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100733 session_type,
734 tty == NULL ? "" : " on ",
735 tty == NULL ? "" : tty,
736 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000737 ssh_remote_ipaddr(ssh),
738 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000739 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100740
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100741#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100742 if (command != NULL)
743 PRIVSEP(audit_run_command(command));
744 else if (s->ttyfd == -1) {
745 char *shell = s->pw->pw_shell;
746
747 if (shell[0] == '\0') /* empty shell means /bin/sh */
748 shell =_PATH_BSHELL;
749 PRIVSEP(audit_run_command(shell));
750 }
751#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000752 if (s->ttyfd != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000753 ret = do_exec_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000754 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000755 ret = do_exec_no_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000756
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000757 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000758
Darren Tucker09991742004-07-17 17:05:14 +1000759 /*
760 * Clear loginmsg: it's the child's responsibility to display
761 * it to the user, otherwise multiple sessions may accumulate
762 * multiple copies of the login messages.
763 */
markus@openbsd.org2808d182018-07-09 21:26:02 +0000764 sshbuf_reset(loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000765
766 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000767}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000768
Damien Miller942da032000-08-18 13:59:06 +1000769/* administrative, login(1)-like work */
770void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000771do_login(struct ssh *ssh, Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000772{
Damien Miller942da032000-08-18 13:59:06 +1000773 socklen_t fromlen;
774 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000775 struct passwd * pw = s->pw;
776 pid_t pid = getpid();
777
778 /*
779 * Get IP address of client. If the connection is not a socket, let
780 * the address be 0.0.0.0.
781 */
782 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000783 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000784 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000785 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000786 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000787 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000788 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000789 }
790 }
791
792 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000793 if (!use_privsep)
794 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000795 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000796 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000797 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000798
Kevin Steves092f2ef2000-10-14 13:36:13 +0000799#ifdef USE_PAM
800 /*
801 * If password change is needed, do it now.
802 * This needs to occur before the ~/.hushlogin check.
803 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100804 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
805 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000806 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100807 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000808 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000809 }
810#endif
811
Damien Millercf205e82001-04-16 18:29:15 +1000812 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000813 return;
814
Darren Tucker1921ed92004-02-10 13:23:28 +1100815 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000816
Damien Millercf205e82001-04-16 18:29:15 +1000817 do_motd();
818}
819
820/*
821 * Display the message of the day.
822 */
823void
824do_motd(void)
825{
826 FILE *f;
827 char buf[256];
828
Damien Miller942da032000-08-18 13:59:06 +1000829 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000830#ifdef HAVE_LOGIN_CAP
831 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
832 "/etc/motd"), "r");
833#else
Damien Miller942da032000-08-18 13:59:06 +1000834 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000835#endif
Damien Miller942da032000-08-18 13:59:06 +1000836 if (f) {
837 while (fgets(buf, sizeof(buf), f))
838 fputs(buf, stdout);
839 fclose(f);
840 }
841 }
842}
843
Kevin Steves8f63caa2001-07-04 18:23:02 +0000844
845/*
846 * Check for quiet login, either .hushlogin or command given.
847 */
848int
849check_quietlogin(Session *s, const char *command)
850{
851 char buf[256];
852 struct passwd *pw = s->pw;
853 struct stat st;
854
855 /* Return 1 if .hushlogin exists or a command given. */
856 if (command != NULL)
857 return 1;
858 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
859#ifdef HAVE_LOGIN_CAP
860 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
861 return 1;
862#else
863 if (stat(buf, &st) >= 0)
864 return 1;
865#endif
866 return 0;
867}
868
Damien Millerb38eff82000-04-01 11:09:21 +1000869/*
Damien Millerb38eff82000-04-01 11:09:21 +1000870 * Reads environment variables from the given file and adds/overrides them
871 * into the environment. If the file does not exist, this does nothing.
872 * Otherwise, it must consist of empty lines, comments (line starts with '#')
873 * and assignments of the form name=value. No other forms are allowed.
djm@openbsd.org95344c22018-07-03 10:59:35 +0000874 * If whitelist is not NULL, then it is interpreted as a pattern list and
875 * only variable names that match it will be accepted.
Damien Millerb38eff82000-04-01 11:09:21 +1000876 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000877static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000878read_environment_file(char ***env, u_int *envsize,
djm@openbsd.org95344c22018-07-03 10:59:35 +0000879 const char *filename, const char *whitelist)
Damien Millerb38eff82000-04-01 11:09:21 +1000880{
881 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000882 char *line = NULL, *cp, *value;
883 size_t linesize = 0;
Damien Miller990070a2002-06-26 23:51:06 +1000884 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000885
886 f = fopen(filename, "r");
887 if (!f)
888 return;
889
markus@openbsd.org7f906352018-06-06 18:29:18 +0000890 while (getline(&line, &linesize, f) != -1) {
Damien Miller990070a2002-06-26 23:51:06 +1000891 if (++lineno > 1000)
892 fatal("Too many lines in environment file %s", filename);
markus@openbsd.org7f906352018-06-06 18:29:18 +0000893 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
Damien Millerb38eff82000-04-01 11:09:21 +1000894 ;
895 if (!*cp || *cp == '#' || *cp == '\n')
896 continue;
Damien Miller14b017d2007-09-17 16:09:15 +1000897
898 cp[strcspn(cp, "\n")] = '\0';
899
Damien Millerb38eff82000-04-01 11:09:21 +1000900 value = strchr(cp, '=');
901 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000902 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
903 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000904 continue;
905 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000906 /*
907 * Replace the equals sign by nul, and advance value to
908 * the value string.
909 */
Damien Millerb38eff82000-04-01 11:09:21 +1000910 *value = '\0';
911 value++;
djm@openbsd.org95344c22018-07-03 10:59:35 +0000912 if (whitelist != NULL &&
913 match_pattern_list(cp, whitelist, 0) != 1)
914 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000915 child_set_env(env, envsize, cp, value);
916 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000917 free(line);
Damien Millerb38eff82000-04-01 11:09:21 +1000918 fclose(f);
919}
920
Darren Tuckere1a790d2003-09-16 11:52:19 +1000921#ifdef HAVE_ETC_DEFAULT_LOGIN
922/*
923 * Return named variable from specified environment, or NULL if not present.
924 */
925static char *
926child_get_env(char **env, const char *name)
927{
928 int i;
929 size_t len;
930
931 len = strlen(name);
932 for (i=0; env[i] != NULL; i++)
933 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
934 return(env[i] + len + 1);
935 return NULL;
936}
937
938/*
939 * Read /etc/default/login.
940 * We pick up the PATH (or SUPATH for root) and UMASK.
941 */
942static void
943read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
944{
945 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000946 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000947 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000948
949 /*
950 * We don't want to copy the whole file to the child's environment,
951 * so we use a temporary environment and copy the variables we're
952 * interested in.
953 */
Darren Tucker3deb56f2018-07-05 13:32:01 +1000954 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login",
955 options.permit_user_env_whitelist);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000956
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000957 if (tmpenv == NULL)
958 return;
959
Darren Tuckere1a790d2003-09-16 11:52:19 +1000960 if (uid == 0)
961 var = child_get_env(tmpenv, "SUPATH");
962 else
963 var = child_get_env(tmpenv, "PATH");
964 if (var != NULL)
965 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100966
Darren Tuckere1a790d2003-09-16 11:52:19 +1000967 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
968 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000969 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100970
Darren Tuckere1a790d2003-09-16 11:52:19 +1000971 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +1000972 free(tmpenv[i]);
973 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000974}
975#endif /* HAVE_ETC_DEFAULT_LOGIN */
976
Damien Miller94bc1e72017-07-28 14:50:59 +1000977static void
978copy_environment_blacklist(char **source, char ***env, u_int *envsize,
979 const char *blacklist)
Damien Millerb38eff82000-04-01 11:09:21 +1000980{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100981 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000982 int i;
983
Damien Millerbb9ffc12002-01-08 10:59:32 +1100984 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000985 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000986
Damien Millerbb9ffc12002-01-08 10:59:32 +1100987 for(i = 0; source[i] != NULL; i++) {
988 var_name = xstrdup(source[i]);
989 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000990 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000991 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000992 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100993 *var_val++ = '\0';
994
Damien Miller94bc1e72017-07-28 14:50:59 +1000995 if (blacklist == NULL ||
996 match_pattern_list(var_name, blacklist, 0) != 1) {
997 debug3("Copy environment: %s=%s", var_name, var_val);
998 child_set_env(env, envsize, var_name, var_val);
999 }
Damien Miller787b2ec2003-11-21 23:56:47 +11001000
Darren Tuckerf60845f2013-06-02 08:07:31 +10001001 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001002 }
1003}
Damien Millercb5e44a2000-09-29 12:12:36 +11001004
Damien Miller94bc1e72017-07-28 14:50:59 +10001005void
1006copy_environment(char **source, char ***env, u_int *envsize)
1007{
1008 copy_environment_blacklist(source, env, envsize, NULL);
1009}
1010
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001011static char **
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001012do_setup_env(struct ssh *ssh, Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001013{
Damien Millerb38eff82000-04-01 11:09:21 +10001014 char buf[256];
djm@openbsd.org7c856852018-03-03 03:15:51 +00001015 size_t n;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001016 u_int i, envsize;
djm@openbsd.org28013752018-06-09 03:03:10 +00001017 char *ocp, *cp, *value, **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001018 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001019#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001020 char *path = NULL;
1021#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001022
Damien Millerb38eff82000-04-01 11:09:21 +10001023 /* Initialize the environment. */
1024 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001025 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001026 env[0] = NULL;
1027
Damien Millerbac2d8a2000-09-05 16:13:06 +11001028#ifdef HAVE_CYGWIN
1029 /*
1030 * The Windows environment contains some setting which are
1031 * important for a running system. They must not be dropped.
1032 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001033 {
1034 char **p;
1035
1036 p = fetch_windows_environment();
1037 copy_environment(p, &env, &envsize);
1038 free_windows_environment(p);
1039 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001040#endif
1041
Darren Tucker0efd1552003-08-26 11:49:55 +10001042#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001043 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001044 * the childs environment as they see fit
1045 */
1046 ssh_gssapi_do_child(&env, &envsize);
1047#endif
1048
djm@openbsd.org83b58182016-08-19 03:18:06 +00001049 /* Set basic environment. */
1050 for (i = 0; i < s->num_env; i++)
1051 child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
Darren Tucker46bc0752004-05-02 22:11:30 +10001052
djm@openbsd.org83b58182016-08-19 03:18:06 +00001053 child_set_env(&env, &envsize, "USER", pw->pw_name);
1054 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001055#ifdef _AIX
djm@openbsd.org83b58182016-08-19 03:18:06 +00001056 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001057#endif
djm@openbsd.org83b58182016-08-19 03:18:06 +00001058 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001059#ifdef HAVE_LOGIN_CAP
djm@openbsd.org83b58182016-08-19 03:18:06 +00001060 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1061 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1062 else
1063 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001064#else /* HAVE_LOGIN_CAP */
1065# ifndef HAVE_CYGWIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001066 /*
1067 * There's no standard path on Windows. The path contains
1068 * important components pointing to the system directories,
1069 * needed for loading shared libraries. So the path better
1070 * remains intact here.
1071 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001072# ifdef HAVE_ETC_DEFAULT_LOGIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001073 read_etc_default_login(&env, &envsize, pw->pw_uid);
1074 path = child_get_env(env, "PATH");
Darren Tuckere1a790d2003-09-16 11:52:19 +10001075# endif /* HAVE_ETC_DEFAULT_LOGIN */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001076 if (path == NULL || *path == '\0') {
1077 child_set_env(&env, &envsize, "PATH",
1078 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1079 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001080# endif /* HAVE_CYGWIN */
1081#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001082
djm@openbsd.org83b58182016-08-19 03:18:06 +00001083 snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
1084 child_set_env(&env, &envsize, "MAIL", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001085
djm@openbsd.org83b58182016-08-19 03:18:06 +00001086 /* Normal systems set SHELL by default. */
1087 child_set_env(&env, &envsize, "SHELL", shell);
1088
Damien Millerb38eff82000-04-01 11:09:21 +10001089 if (getenv("TZ"))
1090 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001091 if (s->term)
1092 child_set_env(&env, &envsize, "TERM", s->term);
1093 if (s->display)
1094 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Millerb38eff82000-04-01 11:09:21 +10001095
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001096 /*
1097 * Since we clear KRB5CCNAME at startup, if it's set now then it
1098 * must have been set by a native authentication method (eg AIX or
1099 * SIA), so copy it to the child.
1100 */
1101 {
1102 char *cp;
1103
1104 if ((cp = getenv("KRB5CCNAME")) != NULL)
1105 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1106 }
1107
Damien Millerb38eff82000-04-01 11:09:21 +10001108#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001109 {
1110 char *cp;
1111
1112 if ((cp = getenv("AUTHSTATE")) != NULL)
1113 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Darren Tucker3deb56f2018-07-05 13:32:01 +10001114 read_environment_file(&env, &envsize, "/etc/environment",
1115 options.permit_user_env_whitelist);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001116 }
Damien Millerb38eff82000-04-01 11:09:21 +10001117#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001118#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001119 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001120 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001121 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001122#endif
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001123 if (auth_sock_name != NULL)
1124 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1125 auth_sock_name);
1126
1127
1128 /* Set custom environment options from pubkey authentication. */
1129 if (options.permit_user_env) {
1130 for (n = 0 ; n < auth_opts->nenv; n++) {
1131 ocp = xstrdup(auth_opts->env[n]);
1132 cp = strchr(ocp, '=');
1133 if (*cp == '=') {
1134 *cp = '\0';
djm@openbsd.org95344c22018-07-03 10:59:35 +00001135 /* Apply PermitUserEnvironment whitelist */
1136 if (options.permit_user_env_whitelist == NULL ||
1137 match_pattern_list(ocp,
1138 options.permit_user_env_whitelist, 0) == 1)
1139 child_set_env(&env, &envsize,
1140 ocp, cp + 1);
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001141 }
1142 free(ocp);
1143 }
1144 }
1145
1146 /* read $HOME/.ssh/environment. */
1147 if (options.permit_user_env) {
1148 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1149 pw->pw_dir);
djm@openbsd.org95344c22018-07-03 10:59:35 +00001150 read_environment_file(&env, &envsize, buf,
1151 options.permit_user_env_whitelist);
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001152 }
1153
Damien Millerb38eff82000-04-01 11:09:21 +10001154#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001155 /*
1156 * Pull in any environment variables that may have
1157 * been set by PAM.
1158 */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001159 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001160 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001161
Damien Miller94bc1e72017-07-28 14:50:59 +10001162 /*
1163 * Don't allow SSH_AUTH_INFO variables posted to PAM to leak
1164 * back into the environment.
1165 */
Damien Millerc756e9b2003-11-17 21:41:42 +11001166 p = fetch_pam_child_environment();
Damien Miller94bc1e72017-07-28 14:50:59 +10001167 copy_environment_blacklist(p, &env, &envsize, "SSH_AUTH_INFO*");
Damien Millerc756e9b2003-11-17 21:41:42 +11001168 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001169
Damien Millerc756e9b2003-11-17 21:41:42 +11001170 p = fetch_pam_environment();
Damien Miller94bc1e72017-07-28 14:50:59 +10001171 copy_environment_blacklist(p, &env, &envsize, "SSH_AUTH_INFO*");
Kevin Steves38b050a2002-07-23 00:44:07 +00001172 free_pam_environment(p);
1173 }
Damien Millerb38eff82000-04-01 11:09:21 +10001174#endif /* USE_PAM */
1175
djm@openbsd.org28013752018-06-09 03:03:10 +00001176 /* Environment specified by admin */
1177 for (i = 0; i < options.num_setenv; i++) {
1178 cp = xstrdup(options.setenv[i]);
1179 if ((value = strchr(cp, '=')) == NULL) {
1180 /* shouldn't happen; vars are checked in servconf.c */
1181 fatal("Invalid config SetEnv: %s", options.setenv[i]);
1182 }
1183 *value++ = '\0';
1184 child_set_env(&env, &envsize, cp, value);
1185 }
1186
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001187 /* SSH_CLIENT deprecated */
1188 snprintf(buf, sizeof buf, "%.50s %d %d",
1189 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1190 ssh_local_port(ssh));
1191 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001192
djm@openbsd.org3b9798b2018-06-09 02:58:02 +00001193 laddr = get_local_ipaddr(packet_get_connection_in());
1194 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1195 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1196 laddr, ssh_local_port(ssh));
1197 free(laddr);
1198 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1199
1200 if (tun_fwd_ifnames != NULL)
1201 child_set_env(&env, &envsize, "SSH_TUNNEL", tun_fwd_ifnames);
1202 if (auth_info_file != NULL)
1203 child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file);
1204 if (s->ttyfd != -1)
1205 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1206 if (original_command)
1207 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1208 original_command);
1209
Damien Millerb38eff82000-04-01 11:09:21 +10001210 if (debug_flag) {
1211 /* dump the environment */
1212 fprintf(stderr, "Environment:\n");
1213 for (i = 0; env[i]; i++)
1214 fprintf(stderr, " %.200s\n", env[i]);
1215 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001216 return env;
1217}
1218
1219/*
1220 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1221 * first in this order).
1222 */
1223static void
djm@openbsd.org7c856852018-03-03 03:15:51 +00001224do_rc_files(struct ssh *ssh, Session *s, const char *shell)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001225{
1226 FILE *f = NULL;
1227 char cmd[1024];
1228 int do_xauth;
1229 struct stat st;
1230
1231 do_xauth =
1232 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1233
Damien Millera1b48cc2008-03-27 11:02:02 +11001234 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001235 if (!s->is_subsystem && options.adm_forced_command == NULL &&
djm@openbsd.org7c856852018-03-03 03:15:51 +00001236 auth_opts->permit_user_rc && options.permit_user_rc &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001237 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001238 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1239 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1240 if (debug_flag)
1241 fprintf(stderr, "Running %s\n", cmd);
1242 f = popen(cmd, "w");
1243 if (f) {
1244 if (do_xauth)
1245 fprintf(f, "%s %s\n", s->auth_proto,
1246 s->auth_data);
1247 pclose(f);
1248 } else
1249 fprintf(stderr, "Could not run %s\n",
1250 _PATH_SSH_USER_RC);
1251 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1252 if (debug_flag)
1253 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1254 _PATH_SSH_SYSTEM_RC);
1255 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1256 if (f) {
1257 if (do_xauth)
1258 fprintf(f, "%s %s\n", s->auth_proto,
1259 s->auth_data);
1260 pclose(f);
1261 } else
1262 fprintf(stderr, "Could not run %s\n",
1263 _PATH_SSH_SYSTEM_RC);
1264 } else if (do_xauth && options.xauth_location != NULL) {
1265 /* Add authority data to .Xauthority if appropriate. */
1266 if (debug_flag) {
1267 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001268 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001269 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001270 fprintf(stderr,
1271 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001272 options.xauth_location, s->auth_display,
1273 s->auth_proto, s->auth_data);
1274 }
1275 snprintf(cmd, sizeof cmd, "%s -q -",
1276 options.xauth_location);
1277 f = popen(cmd, "w");
1278 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001279 fprintf(f, "remove %s\n",
1280 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001281 fprintf(f, "add %s %s %s\n",
1282 s->auth_display, s->auth_proto,
1283 s->auth_data);
1284 pclose(f);
1285 } else {
1286 fprintf(stderr, "Could not run %s\n",
1287 cmd);
1288 }
1289 }
1290}
1291
1292static void
1293do_nologin(struct passwd *pw)
1294{
1295 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001296 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1297 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001298
1299#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001300 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001301 return;
1302 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001303#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001304 if (pw->pw_uid == 0)
1305 return;
1306 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001307#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001308 if (stat(nl, &sb) == -1) {
1309 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001310 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001311 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001312 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001313
1314 /* /etc/nologin exists. Print its contents if we can and exit. */
1315 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1316 if ((f = fopen(nl, "r")) != NULL) {
Darren Tucker19edfd42018-02-13 08:25:46 +11001317 while (fgets(buf, sizeof(buf), f))
1318 fputs(buf, stderr);
1319 fclose(f);
1320 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001321 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001322}
1323
Damien Millerd8cb1f12008-02-10 22:40:12 +11001324/*
1325 * Chroot into a directory after checking it for safety: all path components
1326 * must be root-owned directories with strict permissions.
1327 */
1328static void
1329safely_chroot(const char *path, uid_t uid)
1330{
1331 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001332 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001333 struct stat st;
1334
1335 if (*path != '/')
1336 fatal("chroot path does not begin at root");
1337 if (strlen(path) >= sizeof(component))
1338 fatal("chroot path too long");
1339
1340 /*
1341 * Descend the path, checking that each component is a
1342 * root-owned directory with strict permissions.
1343 */
1344 for (cp = path; cp != NULL;) {
1345 if ((cp = strchr(cp, '/')) == NULL)
1346 strlcpy(component, path, sizeof(component));
1347 else {
1348 cp++;
1349 memcpy(component, path, cp - path);
1350 component[cp - path] = '\0';
1351 }
1352
1353 debug3("%s: checking '%s'", __func__, component);
1354
1355 if (stat(component, &st) != 0)
1356 fatal("%s: stat(\"%s\"): %s", __func__,
1357 component, strerror(errno));
1358 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1359 fatal("bad ownership or modes for chroot "
1360 "directory %s\"%s\"",
1361 cp == NULL ? "" : "component ", component);
1362 if (!S_ISDIR(st.st_mode))
1363 fatal("chroot path %s\"%s\" is not a directory",
1364 cp == NULL ? "" : "component ", component);
1365
1366 }
1367
1368 if (chdir(path) == -1)
1369 fatal("Unable to chdir to chroot path \"%s\": "
1370 "%s", path, strerror(errno));
1371 if (chroot(path) == -1)
1372 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1373 if (chdir("/") == -1)
1374 fatal("%s: chdir(/) after chroot: %s",
1375 __func__, strerror(errno));
1376 verbose("Changed root directory to \"%s\"", path);
1377}
1378
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001379/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001381do_setusercontext(struct passwd *pw)
1382{
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001383 char uidstr[32], *chroot_path, *tmp;
Damien Miller54e37732008-02-10 22:48:55 +11001384
Darren Tucker97528352010-11-05 12:03:05 +11001385 platform_setusercontext(pw);
1386
Darren Tuckerb12fe272010-11-05 14:47:01 +11001387 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001388#ifdef HAVE_LOGIN_CAP
1389 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001390 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001391 perror("unable to set user context");
1392 exit(1);
1393 }
1394#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001395 if (setlogin(pw->pw_name) < 0)
1396 error("setlogin failed: %s", strerror(errno));
1397 if (setgid(pw->pw_gid) < 0) {
1398 perror("setgid");
1399 exit(1);
1400 }
1401 /* Initialize the group list. */
1402 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1403 perror("initgroups");
1404 exit(1);
1405 }
1406 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001407#endif
1408
Darren Tucker920612e2010-11-05 12:36:15 +11001409 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001410
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001411 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001412 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001413 tmp = tilde_expand_filename(options.chroot_directory,
1414 pw->pw_uid);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001415 snprintf(uidstr, sizeof(uidstr), "%llu",
1416 (unsigned long long)pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001417 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001418 "u", pw->pw_name, "U", uidstr, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001419 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001420 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001421 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001422 /* Make sure we don't attempt to chroot again */
1423 free(options.chroot_directory);
1424 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001425 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001426 }
1427
1428#ifdef HAVE_LOGIN_CAP
1429 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1430 perror("unable to set user context (setuser)");
1431 exit(1);
1432 }
Damien Miller58528402013-03-15 11:22:37 +11001433 /*
1434 * FreeBSD's setusercontext() will not apply the user's
1435 * own umask setting unless running with the user's UID.
1436 */
1437 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001438#else
Tim Rice9464ba62014-01-20 17:59:28 -08001439# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001440 /*
1441 * In a chroot environment, the set_id() will always fail;
1442 * typically because of the lack of necessary authentication
1443 * services and runtime such as ./usr/lib/libiaf.so,
1444 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1445 * internal sftp chroot case. We'll lose auditing and ACLs but
1446 * permanently_set_uid will take care of the rest.
1447 */
1448 if (!in_chroot && set_id(pw->pw_name) != 0)
1449 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001450# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001451 /* Permanently switch to the desired uid. */
1452 permanently_set_uid(pw);
1453#endif
Damien Millera56086b2013-04-23 15:24:18 +10001454 } else if (options.chroot_directory != NULL &&
1455 strcasecmp(options.chroot_directory, "none") != 0) {
1456 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001457 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001458
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001459 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1460 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1461}
1462
Ben Lindstrom08105192002-03-22 02:50:06 +00001463static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001464do_pwchange(Session *s)
1465{
Darren Tucker09991742004-07-17 17:05:14 +10001466 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001467 fprintf(stderr, "WARNING: Your password has expired.\n");
1468 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001469 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001470 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001471#ifdef WITH_SELINUX
1472 setexeccon(NULL);
1473#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001474#ifdef PASSWD_NEEDS_USERNAME
1475 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1476 (char *)NULL);
1477#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001478 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001479#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001480 perror("passwd");
1481 } else {
1482 fprintf(stderr,
1483 "Password change required but no TTY available.\n");
1484 }
1485 exit(1);
1486}
1487
1488static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001489child_close_fds(struct ssh *ssh)
Darren Tucker23bc8d02004-02-06 16:24:31 +11001490{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001491 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001492
djm@openbsd.org141efe42015-01-14 20:05:27 +00001493 if (auth_sock != -1) {
1494 close(auth_sock);
1495 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001496 }
1497
Darren Tucker23bc8d02004-02-06 16:24:31 +11001498 if (packet_get_connection_in() == packet_get_connection_out())
1499 close(packet_get_connection_in());
1500 else {
1501 close(packet_get_connection_in());
1502 close(packet_get_connection_out());
1503 }
1504 /*
1505 * Close all descriptors related to channels. They will still remain
1506 * open in the parent.
1507 */
1508 /* XXX better use close-on-exec? -markus */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001509 channel_close_all(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001510
1511 /*
1512 * Close any extra file descriptors. Note that there may still be
1513 * descriptors left by system functions. They will be closed later.
1514 */
1515 endpwent();
1516
1517 /*
Damien Miller788f2122005-11-05 15:14:59 +11001518 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001519 * hanging around in clients. Note that we want to do this after
1520 * initgroups, because at least on Solaris 2.3 it leaves file
1521 * descriptors open.
1522 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001523 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001524}
1525
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001526/*
1527 * Performs common processing for the child, such as setting up the
1528 * environment, closing extra file descriptors, setting the user and group
1529 * ids, and executing the command or shell.
1530 */
Damien Millerdfc24252008-02-10 22:29:40 +11001531#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001532void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001533do_child(struct ssh *ssh, Session *s, const char *command)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001534{
1535 extern char **environ;
1536 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001537 char *argv[ARGV_MAX];
djm@openbsd.org83b58182016-08-19 03:18:06 +00001538 const char *shell, *shell0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001539 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001540 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001541
1542 /* remove hostkey from the child's memory */
1543 destroy_sensitive_data();
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001544 packet_clear_keys();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001545
Darren Tucker23bc8d02004-02-06 16:24:31 +11001546 /* Force a password change */
1547 if (s->authctxt->force_pwchange) {
1548 do_setusercontext(pw);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001549 child_close_fds(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001550 do_pwchange(s);
1551 exit(1);
1552 }
1553
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001554 /*
1555 * Login(1) does this as well, and it needs uid 0 for the "-h"
1556 * switch, so we let login(1) to this for us.
1557 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001558#ifdef HAVE_OSF_SIA
djm@openbsd.org83b58182016-08-19 03:18:06 +00001559 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1560 if (!check_quietlogin(s, command))
1561 do_motd();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001562#else /* HAVE_OSF_SIA */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001563 /* When PAM is enabled we rely on it to do the nologin check */
1564 if (!options.use_pam)
1565 do_nologin(pw);
1566 do_setusercontext(pw);
1567 /*
1568 * PAM session modules in do_setusercontext may have
1569 * generated messages, so if this in an interactive
1570 * login then display them too.
1571 */
1572 if (!check_quietlogin(s, command))
1573 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001574#endif /* HAVE_OSF_SIA */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001575
Darren Tucker69687f42004-09-11 22:17:26 +10001576#ifdef USE_PAM
djm@openbsd.org83b58182016-08-19 03:18:06 +00001577 if (options.use_pam && !is_pam_session_open()) {
Darren Tucker48554152005-04-21 19:50:55 +10001578 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001579 display_loginmsg();
1580 exit(254);
1581 }
1582#endif
1583
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001584 /*
1585 * Get the shell from the password data. An empty shell field is
1586 * legal, and means /bin/sh.
1587 */
1588 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001589
1590 /*
1591 * Make sure $SHELL points to the shell from the password file,
1592 * even if shell is overridden from login.conf
1593 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001594 env = do_setup_env(ssh, s, shell);
Ben Lindstrom46767602002-12-23 02:26:08 +00001595
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001596#ifdef HAVE_LOGIN_CAP
1597 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1598#endif
1599
Damien Millerb38eff82000-04-01 11:09:21 +10001600 /*
1601 * Close the connection descriptors; note that this is the child, and
1602 * the server will still have the socket open, and it is important
1603 * that we do not shutdown it. Note that the descriptors cannot be
1604 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001605 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001606 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001607 child_close_fds(ssh);
Damien Millerb38eff82000-04-01 11:09:21 +10001608
Damien Millerb38eff82000-04-01 11:09:21 +10001609 /*
Damien Miller05eda432002-02-10 18:32:28 +11001610 * Must take new environment into use so that .ssh/rc,
1611 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001612 */
1613 environ = env;
1614
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001615#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001616 /*
1617 * At this point, we check to see if AFS is active and if we have
1618 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1619 * if we can (and need to) extend the ticket into an AFS token. If
1620 * we don't do this, we run into potential problems if the user's
1621 * home directory is in AFS and it's not world-readable.
1622 */
1623
1624 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001625 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001626 char cell[64];
1627
1628 debug("Getting AFS token");
1629
1630 k_setpag();
1631
1632 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1633 krb5_afslog(s->authctxt->krb5_ctx,
1634 s->authctxt->krb5_fwd_ccache, cell, NULL);
1635
1636 krb5_afslog_home(s->authctxt->krb5_ctx,
1637 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1638 }
1639#endif
1640
Damien Miller788f2122005-11-05 15:14:59 +11001641 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001642 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001643 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001644#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001645 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001646#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001647 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001648 fprintf(stderr, "Could not chdir to home "
1649 "directory %s: %s\n", pw->pw_dir,
1650 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001651 }
Damien Miller6051c942008-06-16 07:53:16 +10001652 if (r)
1653 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001654 }
1655
Damien Millera1939002008-03-15 17:27:58 +11001656 closefrom(STDERR_FILENO + 1);
1657
djm@openbsd.org7c856852018-03-03 03:15:51 +00001658 do_rc_files(ssh, s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001659
1660 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001661 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001662
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001663 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1664 printf("This service allows sftp connections only.\n");
1665 fflush(NULL);
1666 exit(1);
1667 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001668 extern int optind, optreset;
1669 int i;
1670 char *p, *args;
1671
Darren Tuckerac46a912009-06-21 17:55:23 +10001672 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001673 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001674 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1675 if (i < ARGV_MAX - 1)
1676 argv[i++] = p;
1677 argv[i] = NULL;
1678 optind = optreset = 1;
1679 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001680#ifdef WITH_SELINUX
1681 ssh_selinux_change_context("sftpd_t");
1682#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001683 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001684 }
1685
Darren Tucker695ed392009-10-07 09:02:18 +11001686 fflush(NULL);
1687
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001688 /* Get the last component of the shell name. */
1689 if ((shell0 = strrchr(shell, '/')) != NULL)
1690 shell0++;
1691 else
1692 shell0 = shell;
1693
Damien Millerb38eff82000-04-01 11:09:21 +10001694 /*
1695 * If we have no command, execute the shell. In this case, the shell
1696 * name to be passed in argv[0] is preceded by '-' to indicate that
1697 * this is a login shell.
1698 */
1699 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001700 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001701
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001702 /* Start the shell. Set initial character to '-'. */
1703 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001704
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001705 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1706 >= sizeof(argv0) - 1) {
1707 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001708 perror(shell);
1709 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001710 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001711
1712 /* Execute the shell. */
1713 argv[0] = argv0;
1714 argv[1] = NULL;
1715 execve(shell, argv, env);
1716
1717 /* Executing the shell failed. */
1718 perror(shell);
1719 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001720 }
1721 /*
1722 * Execute the command using the user's shell. This uses the -c
1723 * option to execute the command.
1724 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001725 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001726 argv[1] = "-c";
1727 argv[2] = (char *) command;
1728 argv[3] = NULL;
1729 execve(shell, argv, env);
1730 perror(shell);
1731 exit(1);
1732}
1733
Damien Miller7207f642008-05-19 15:34:50 +10001734void
1735session_unused(int id)
1736{
1737 debug3("%s: session id %d unused", __func__, id);
1738 if (id >= options.max_sessions ||
1739 id >= sessions_nalloc) {
1740 fatal("%s: insane session id %d (max %d nalloc %d)",
1741 __func__, id, options.max_sessions, sessions_nalloc);
1742 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001743 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001744 sessions[id].self = id;
1745 sessions[id].used = 0;
1746 sessions[id].chanid = -1;
1747 sessions[id].ptyfd = -1;
1748 sessions[id].ttyfd = -1;
1749 sessions[id].ptymaster = -1;
1750 sessions[id].x11_chanids = NULL;
1751 sessions[id].next_unused = sessions_first_unused;
1752 sessions_first_unused = id;
1753}
1754
Damien Millerb38eff82000-04-01 11:09:21 +10001755Session *
1756session_new(void)
1757{
Damien Miller7207f642008-05-19 15:34:50 +10001758 Session *s, *tmp;
1759
1760 if (sessions_first_unused == -1) {
1761 if (sessions_nalloc >= options.max_sessions)
1762 return NULL;
1763 debug2("%s: allocate (allocated %d max %d)",
1764 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001765 tmp = xrecallocarray(sessions, sessions_nalloc,
1766 sessions_nalloc + 1, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001767 if (tmp == NULL) {
1768 error("%s: cannot allocate %d sessions",
1769 __func__, sessions_nalloc + 1);
1770 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001771 }
Damien Miller7207f642008-05-19 15:34:50 +10001772 sessions = tmp;
1773 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001774 }
Damien Miller7207f642008-05-19 15:34:50 +10001775
1776 if (sessions_first_unused >= sessions_nalloc ||
1777 sessions_first_unused < 0) {
1778 fatal("%s: insane first_unused %d max %d nalloc %d",
1779 __func__, sessions_first_unused, options.max_sessions,
1780 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001781 }
Damien Miller7207f642008-05-19 15:34:50 +10001782
1783 s = &sessions[sessions_first_unused];
1784 if (s->used) {
1785 fatal("%s: session %d already used",
1786 __func__, sessions_first_unused);
1787 }
1788 sessions_first_unused = s->next_unused;
1789 s->used = 1;
1790 s->next_unused = -1;
1791 debug("session_new: session %d", s->self);
1792
1793 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001794}
1795
Ben Lindstrombba81212001-06-25 05:01:22 +00001796static void
Damien Millerb38eff82000-04-01 11:09:21 +10001797session_dump(void)
1798{
1799 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001800 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001801 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001802
1803 debug("dump: used %d next_unused %d session %d %p "
1804 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001805 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001806 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001807 s->self,
1808 s,
1809 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001810 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001811 }
1812}
1813
Damien Millerefb4afe2000-04-12 18:45:05 +10001814int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001815session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001816{
1817 Session *s = session_new();
1818 debug("session_open: channel %d", chanid);
1819 if (s == NULL) {
1820 error("no more sessions");
1821 return 0;
1822 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001823 s->authctxt = authctxt;
1824 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001825 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001826 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001827 debug("session_open: session %d: link with channel %d", s->self, chanid);
1828 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001829 return 1;
1830}
1831
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001832Session *
1833session_by_tty(char *tty)
1834{
1835 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001836 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001837 Session *s = &sessions[i];
1838 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1839 debug("session_by_tty: session %d tty %s", i, tty);
1840 return s;
1841 }
1842 }
1843 debug("session_by_tty: unknown tty %.100s", tty);
1844 session_dump();
1845 return NULL;
1846}
1847
Ben Lindstrombba81212001-06-25 05:01:22 +00001848static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001849session_by_channel(int id)
1850{
1851 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001852 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001853 Session *s = &sessions[i];
1854 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001855 debug("session_by_channel: session %d channel %d",
1856 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001857 return s;
1858 }
1859 }
1860 debug("session_by_channel: unknown channel %d", id);
1861 session_dump();
1862 return NULL;
1863}
1864
Ben Lindstrombba81212001-06-25 05:01:22 +00001865static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001866session_by_x11_channel(int id)
1867{
1868 int i, j;
1869
Damien Miller7207f642008-05-19 15:34:50 +10001870 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001871 Session *s = &sessions[i];
1872
1873 if (s->x11_chanids == NULL || !s->used)
1874 continue;
1875 for (j = 0; s->x11_chanids[j] != -1; j++) {
1876 if (s->x11_chanids[j] == id) {
1877 debug("session_by_x11_channel: session %d "
1878 "channel %d", s->self, id);
1879 return s;
1880 }
1881 }
1882 }
1883 debug("session_by_x11_channel: unknown channel %d", id);
1884 session_dump();
1885 return NULL;
1886}
1887
1888static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001889session_by_pid(pid_t pid)
1890{
1891 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001892 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001893 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001894 Session *s = &sessions[i];
1895 if (s->used && s->pid == pid)
1896 return s;
1897 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001898 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001899 session_dump();
1900 return NULL;
1901}
1902
Ben Lindstrombba81212001-06-25 05:01:22 +00001903static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001904session_window_change_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001905{
1906 s->col = packet_get_int();
1907 s->row = packet_get_int();
1908 s->xpixel = packet_get_int();
1909 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001910 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001911 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1912 return 1;
1913}
1914
Ben Lindstrombba81212001-06-25 05:01:22 +00001915static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001916session_pty_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001917{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001918 u_int len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001919
djm@openbsd.org7c856852018-03-03 03:15:51 +00001920 if (!auth_opts->permit_pty_flag || !options.permit_tty) {
1921 debug("Allocating a pty not permitted for this connection.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001922 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001923 }
1924 if (s->ttyfd != -1) {
1925 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001926 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001927 }
1928
Damien Millerefb4afe2000-04-12 18:45:05 +10001929 s->term = packet_get_string(&len);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001930 s->col = packet_get_int();
1931 s->row = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +10001932 s->xpixel = packet_get_int();
1933 s->ypixel = packet_get_int();
1934
1935 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001936 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001937 s->term = NULL;
1938 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001939
Damien Millerefb4afe2000-04-12 18:45:05 +10001940 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001941 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10001942 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1943 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10001944 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001945 s->term = NULL;
1946 s->ptyfd = -1;
1947 s->ttyfd = -1;
1948 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001949 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001950 }
1951 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001952
markus@openbsd.org89dd6152018-07-09 21:20:26 +00001953 ssh_tty_parse_modes(ssh, s->ttyfd);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001954
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001955 if (!use_privsep)
1956 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001957
1958 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001959 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1960
Damien Miller48b03fc2002-01-22 23:11:40 +11001961 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001962 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001963 return 1;
1964}
1965
Ben Lindstrombba81212001-06-25 05:01:22 +00001966static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001967session_subsystem_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001968{
Damien Millerae452462001-10-10 15:08:06 +10001969 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001970 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001971 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11001972 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10001973 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001974
Damien Miller71df7522013-10-15 12:12:02 +11001975 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001976 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11001977 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10001978 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10001979
Damien Millerf6d9e222000-06-18 14:50:44 +10001980 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11001981 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001982 prog = options.subsystem_command[i];
1983 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001984 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11001985 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001986 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11001987 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001988 if (stat(prog, &st) < 0)
1989 debug("subsystem: cannot stat %s: %s",
1990 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001991 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001992 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001993 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001994 success = do_exec(ssh, s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11001995 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001996 }
1997 }
1998
1999 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11002000 logit("subsystem request for %.100s by user %s failed, "
2001 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10002002
Damien Millerbd483e72000-04-30 10:00:53 +10002003 return success;
2004}
2005
Ben Lindstrombba81212001-06-25 05:01:22 +00002006static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002007session_x11_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10002008{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002009 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002010
Damien Miller2b9b0452005-07-17 17:19:24 +10002011 if (s->auth_proto != NULL || s->auth_data != NULL) {
2012 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002013 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002014 return 0;
2015 }
Damien Millerbd483e72000-04-30 10:00:53 +10002016 s->single_connection = packet_get_char();
2017 s->auth_proto = packet_get_string(NULL);
2018 s->auth_data = packet_get_string(NULL);
2019 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002020 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002021
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00002022 if (xauth_valid_string(s->auth_proto) &&
2023 xauth_valid_string(s->auth_data))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002024 success = session_setup_x11fwd(ssh, s);
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00002025 else {
2026 success = 0;
2027 error("Invalid X11 forwarding data");
2028 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002029 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10002030 free(s->auth_proto);
2031 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002032 s->auth_proto = NULL;
2033 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002034 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002035 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002036}
2037
Ben Lindstrombba81212001-06-25 05:01:22 +00002038static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002039session_shell_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10002040{
Damien Miller48b03fc2002-01-22 23:11:40 +11002041 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002042 return do_exec(ssh, s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002043}
2044
Ben Lindstrombba81212001-06-25 05:01:22 +00002045static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002046session_exec_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10002047{
Damien Miller7207f642008-05-19 15:34:50 +10002048 u_int len, success;
2049
Damien Millerf6d9e222000-06-18 14:50:44 +10002050 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002051 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002052 success = do_exec(ssh, s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10002053 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10002054 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002055}
2056
Ben Lindstrombba81212001-06-25 05:01:22 +00002057static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002058session_break_req(struct ssh *ssh, Session *s)
Damien Miller54c45982003-05-15 10:20:13 +10002059{
Damien Miller54c45982003-05-15 10:20:13 +10002060
Darren Tucker1f8311c2004-05-13 16:39:33 +10002061 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002062 packet_check_eom();
2063
Darren Tucker9c5d5532011-11-04 10:55:24 +11002064 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002065 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002066 return 1;
2067}
2068
2069static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002070session_env_req(struct ssh *ssh, Session *s)
Darren Tucker46bc0752004-05-02 22:11:30 +10002071{
2072 char *name, *val;
2073 u_int name_len, val_len, i;
2074
Damien Miller8569eba2014-03-04 09:35:17 +11002075 name = packet_get_cstring(&name_len);
2076 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002077 packet_check_eom();
2078
2079 /* Don't set too many environment variables */
2080 if (s->num_env > 128) {
2081 debug2("Ignoring env request %s: too many env vars", name);
2082 goto fail;
2083 }
2084
2085 for (i = 0; i < options.num_accept_env; i++) {
2086 if (match_pattern(name, options.accept_env[i])) {
2087 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002088 s->env = xrecallocarray(s->env, s->num_env,
2089 s->num_env + 1, sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002090 s->env[s->num_env].name = name;
2091 s->env[s->num_env].val = val;
2092 s->num_env++;
2093 return (1);
2094 }
2095 }
2096 debug2("Ignoring env request %s: disallowed name", name);
2097
2098 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002099 free(name);
2100 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002101 return (0);
2102}
2103
2104static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002105session_auth_agent_req(struct ssh *ssh, Session *s)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002106{
2107 static int called = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00002108
Damien Miller48b03fc2002-01-22 23:11:40 +11002109 packet_check_eom();
djm@openbsd.org7c856852018-03-03 03:15:51 +00002110 if (!auth_opts->permit_agent_forwarding_flag ||
2111 !options.allow_agent_forwarding) {
2112 debug("%s: agent forwarding disabled", __func__);
Ben Lindstrom14920292000-11-21 21:24:55 +00002113 return 0;
2114 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002115 if (called) {
2116 return 0;
2117 } else {
2118 called = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002119 return auth_input_request_forwarding(ssh, s->pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002120 }
2121}
2122
Damien Millerc7ef63d2002-02-05 12:21:42 +11002123int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002124session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002125{
Damien Millerefb4afe2000-04-12 18:45:05 +10002126 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002127 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002128
Damien Millerc7ef63d2002-02-05 12:21:42 +11002129 if ((s = session_by_channel(c->self)) == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002130 logit("%s: no session %d req %.100s", __func__, c->self, rtype);
Damien Millerc7ef63d2002-02-05 12:21:42 +11002131 return 0;
2132 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002133 debug("%s: session %d req %s", __func__, s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002134
2135 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002136 * a session is in LARVAL state until a shell, a command
2137 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002138 */
2139 if (c->type == SSH_CHANNEL_LARVAL) {
2140 if (strcmp(rtype, "shell") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002141 success = session_shell_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002142 } else if (strcmp(rtype, "exec") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002143 success = session_exec_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002144 } else if (strcmp(rtype, "pty-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002145 success = session_pty_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002146 } else if (strcmp(rtype, "x11-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002147 success = session_x11_req(ssh, s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002148 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002149 success = session_auth_agent_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002150 } else if (strcmp(rtype, "subsystem") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002151 success = session_subsystem_req(ssh, s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002152 } else if (strcmp(rtype, "env") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002153 success = session_env_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002154 }
2155 }
2156 if (strcmp(rtype, "window-change") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002157 success = session_window_change_req(ssh, s);
Damien Millera6b1d162004-06-30 22:41:07 +10002158 } else if (strcmp(rtype, "break") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002159 success = session_break_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002160 }
Damien Millera6b1d162004-06-30 22:41:07 +10002161
Damien Millerc7ef63d2002-02-05 12:21:42 +11002162 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002163}
2164
2165void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002166session_set_fds(struct ssh *ssh, Session *s,
2167 int fdin, int fdout, int fderr, int ignore_fderr, int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002168{
Damien Millerefb4afe2000-04-12 18:45:05 +10002169 /*
2170 * now that have a child and a pipe to the child,
2171 * we can activate our channel and register the fd's
2172 */
2173 if (s->chanid == -1)
2174 fatal("no channel for session %d", s->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002175 channel_set_fds(ssh, s->chanid,
Damien Millerefb4afe2000-04-12 18:45:05 +10002176 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002177 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002178 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002179}
2180
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002181/*
2182 * Function to perform pty cleanup. Also called if we get aborted abnormally
2183 * (e.g., due to a dropped connection).
2184 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002185void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002186session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002187{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002188 if (s == NULL) {
2189 error("session_pty_cleanup: no session");
2190 return;
2191 }
2192 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002193 return;
2194
Kevin Steves43cdef32001-02-11 14:12:08 +00002195 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002196
Damien Millerb38eff82000-04-01 11:09:21 +10002197 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002198 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002199 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002200
2201 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002202 if (getuid() == 0)
2203 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002204
2205 /*
2206 * Close the server side of the socket pairs. We must do this after
2207 * the pty cleanup, so that another process doesn't get this pty
2208 * while we're still cleaning up.
2209 */
Damien Miller7207f642008-05-19 15:34:50 +10002210 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2211 error("close(s->ptymaster/%d): %s",
2212 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002213
2214 /* unlink pty from session */
2215 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002216}
Damien Millerefb4afe2000-04-12 18:45:05 +10002217
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002218void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002219session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002220{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002221 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002222}
2223
Damien Miller5a80bba2002-09-04 16:39:02 +10002224static char *
2225sig2name(int sig)
2226{
2227#define SSH_SIG(x) if (sig == SIG ## x) return #x
2228 SSH_SIG(ABRT);
2229 SSH_SIG(ALRM);
2230 SSH_SIG(FPE);
2231 SSH_SIG(HUP);
2232 SSH_SIG(ILL);
2233 SSH_SIG(INT);
2234 SSH_SIG(KILL);
2235 SSH_SIG(PIPE);
2236 SSH_SIG(QUIT);
2237 SSH_SIG(SEGV);
2238 SSH_SIG(TERM);
2239 SSH_SIG(USR1);
2240 SSH_SIG(USR2);
2241#undef SSH_SIG
2242 return "SIG@openssh.com";
2243}
2244
Ben Lindstrombba81212001-06-25 05:01:22 +00002245static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002246session_close_x11(struct ssh *ssh, int id)
Damien Miller2b9b0452005-07-17 17:19:24 +10002247{
2248 Channel *c;
2249
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002250 if ((c = channel_by_id(ssh, id)) == NULL) {
2251 debug("%s: x11 channel %d missing", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002252 } else {
2253 /* Detach X11 listener */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002254 debug("%s: detach x11 channel %d", __func__, id);
2255 channel_cancel_cleanup(ssh, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002256 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002257 chan_mark_dead(ssh, c);
Damien Miller2b9b0452005-07-17 17:19:24 +10002258 }
2259}
2260
2261static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002262session_close_single_x11(struct ssh *ssh, int id, void *arg)
Damien Miller2b9b0452005-07-17 17:19:24 +10002263{
2264 Session *s;
2265 u_int i;
2266
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002267 debug3("%s: channel %d", __func__, id);
2268 channel_cancel_cleanup(ssh, id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002269 if ((s = session_by_x11_channel(id)) == NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002270 fatal("%s: no x11 channel %d", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002271 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002272 debug("%s: session %d: closing channel %d",
2273 __func__, s->self, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002274 /*
2275 * The channel "id" is already closing, but make sure we
2276 * close all of its siblings.
2277 */
2278 if (s->x11_chanids[i] != id)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002279 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002280 }
Darren Tuckera627d422013-06-02 07:31:17 +10002281 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002282 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002283 free(s->display);
2284 s->display = NULL;
2285 free(s->auth_proto);
2286 s->auth_proto = NULL;
2287 free(s->auth_data);
2288 s->auth_data = NULL;
2289 free(s->auth_display);
2290 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002291}
2292
2293static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002294session_exit_message(struct ssh *ssh, Session *s, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002295{
2296 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002297
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002298 if ((c = channel_lookup(ssh, s->chanid)) == NULL)
2299 fatal("%s: session %d: no channel %d",
2300 __func__, s->self, s->chanid);
2301 debug("%s: session %d channel %d pid %ld",
2302 __func__, s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002303
2304 if (WIFEXITED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002305 channel_request_start(ssh, s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002306 packet_put_int(WEXITSTATUS(status));
2307 packet_send();
2308 } else if (WIFSIGNALED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002309 channel_request_start(ssh, s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002310 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002311#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002312 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002313#else /* WCOREDUMP */
2314 packet_put_char(0);
2315#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002316 packet_put_cstring("");
2317 packet_put_cstring("");
2318 packet_send();
2319 } else {
2320 /* Some weird exit cause. Just exit. */
2321 packet_disconnect("wait returned status %04x.", status);
2322 }
2323
2324 /* disconnect channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002325 debug("%s: release channel %d", __func__, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002326
2327 /*
2328 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002329 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002330 * by session_close_by_channel when the childs close their fds.
2331 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002332 channel_register_cleanup(ssh, c->self, session_close_by_channel, 1);
Damien Miller39eda6e2005-11-05 14:52:50 +11002333
Damien Miller166fca82000-04-20 07:42:21 +10002334 /*
2335 * emulate a write failure with 'chan_write_failed', nobody will be
2336 * interested in data we write.
2337 * Note that we must not call 'chan_read_failed', since there could
2338 * be some more data waiting in the pipe.
2339 */
Damien Millerbd483e72000-04-30 10:00:53 +10002340 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002341 chan_write_failed(ssh, c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002342}
2343
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002344void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002345session_close(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002346{
Damien Millereccb9de2005-06-17 12:59:34 +10002347 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002348
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002349 verbose("Close session: user %s from %.200s port %d id %d",
2350 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002351 ssh_remote_ipaddr(ssh),
2352 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002353 s->self);
2354
Darren Tucker3e33cec2003-10-02 16:12:36 +10002355 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002356 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002357 free(s->term);
2358 free(s->display);
2359 free(s->x11_chanids);
2360 free(s->auth_display);
2361 free(s->auth_data);
2362 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002363 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002364 if (s->env != NULL) {
2365 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002366 free(s->env[i].name);
2367 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002368 }
Darren Tuckera627d422013-06-02 07:31:17 +10002369 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002370 }
Damien Millere247cc42000-05-07 12:03:14 +10002371 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002372 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002373}
2374
2375void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002376session_close_by_pid(struct ssh *ssh, pid_t pid, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002377{
2378 Session *s = session_by_pid(pid);
2379 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002380 debug("%s: no session for pid %ld", __func__, (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002381 return;
2382 }
2383 if (s->chanid != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002384 session_exit_message(ssh, s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002385 if (s->ttyfd != -1)
2386 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002387 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002388}
2389
2390/*
2391 * this is called when a channel dies before
2392 * the session 'child' itself dies
2393 */
2394void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002395session_close_by_channel(struct ssh *ssh, int id, void *arg)
Damien Millerefb4afe2000-04-12 18:45:05 +10002396{
2397 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002398 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002399
Damien Millerefb4afe2000-04-12 18:45:05 +10002400 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002401 debug("%s: no session for id %d", __func__, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002402 return;
2403 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002404 debug("%s: channel %d child %ld", __func__, id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002405 if (s->pid != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002406 debug("%s: channel %d: has child", __func__, id);
Damien Miller0585d512001-10-12 11:35:50 +10002407 /*
2408 * delay detach of session, but release pty, since
2409 * the fd's to the child are already closed
2410 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002411 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002412 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002413 return;
2414 }
2415 /* detach by removing callback */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002416 channel_cancel_cleanup(ssh, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002417
2418 /* Close any X11 listeners associated with this session */
2419 if (s->x11_chanids != NULL) {
2420 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002421 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller39eda6e2005-11-05 14:52:50 +11002422 s->x11_chanids[i] = -1;
2423 }
2424 }
2425
Damien Millerefb4afe2000-04-12 18:45:05 +10002426 s->chanid = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002427 session_close(ssh, s);
Damien Miller52b77be2001-10-10 15:14:37 +10002428}
2429
2430void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002431session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002432{
2433 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002434 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002435 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002436 if (s->used) {
2437 if (closefunc != NULL)
2438 closefunc(s);
2439 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002440 session_close(ssh, s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002441 }
Damien Miller52b77be2001-10-10 15:14:37 +10002442 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002443}
2444
Ben Lindstrombba81212001-06-25 05:01:22 +00002445static char *
Damien Millere247cc42000-05-07 12:03:14 +10002446session_tty_list(void)
2447{
2448 static char buf[1024];
2449 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002450 char *cp;
2451
Damien Millere247cc42000-05-07 12:03:14 +10002452 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002453 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002454 Session *s = &sessions[i];
2455 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002456
Damien Millera8ed44b2003-01-10 09:53:12 +11002457 if (strncmp(s->tty, "/dev/", 5) != 0) {
2458 cp = strrchr(s->tty, '/');
2459 cp = (cp == NULL) ? s->tty : cp + 1;
2460 } else
2461 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002462
Damien Millere247cc42000-05-07 12:03:14 +10002463 if (buf[0] != '\0')
2464 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002465 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002466 }
2467 }
2468 if (buf[0] == '\0')
2469 strlcpy(buf, "notty", sizeof buf);
2470 return buf;
2471}
2472
2473void
2474session_proctitle(Session *s)
2475{
2476 if (s->pw == NULL)
2477 error("no user for session %d", s->self);
2478 else
2479 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2480}
2481
Ben Lindstrom768176b2001-06-09 01:29:12 +00002482int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002483session_setup_x11fwd(struct ssh *ssh, Session *s)
Ben Lindstrom768176b2001-06-09 01:29:12 +00002484{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002485 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002486 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002487 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002488 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002489
djm@openbsd.org7c856852018-03-03 03:15:51 +00002490 if (!auth_opts->permit_x11_forwarding_flag) {
2491 packet_send_debug("X11 forwarding disabled by key options.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002492 return 0;
2493 }
2494 if (!options.x11_forwarding) {
2495 debug("X11 forwarding disabled in server configuration file.");
2496 return 0;
2497 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002498 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002499 (stat(options.xauth_location, &st) == -1)) {
djm@openbsd.org7c856852018-03-03 03:15:51 +00002500 packet_send_debug("No xauth program; cannot forward X11.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002501 return 0;
2502 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002503 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002504 debug("X11 display already set.");
2505 return 0;
2506 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002507 if (x11_create_display_inet(ssh, options.x11_display_offset,
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002508 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002509 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002510 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002511 return 0;
2512 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002513 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002514 channel_register_cleanup(ssh, s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002515 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002516 }
Kevin Steves366298c2001-12-19 17:58:01 +00002517
2518 /* Set up a suitable value for the DISPLAY variable. */
2519 if (gethostname(hostname, sizeof(hostname)) < 0)
2520 fatal("gethostname: %.100s", strerror(errno));
2521 /*
2522 * auth_display must be used as the displayname when the
2523 * authorization entry is added with xauth(1). This will be
2524 * different than the DISPLAY string for localhost displays.
2525 */
Damien Miller95c249f2002-02-05 12:11:34 +11002526 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002527 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002528 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002529 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002530 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002531 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002532 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002533 } else {
2534#ifdef IPADDR_IN_DISPLAY
2535 struct hostent *he;
2536 struct in_addr my_addr;
2537
2538 he = gethostbyname(hostname);
2539 if (he == NULL) {
2540 error("Can't get IP address for X11 DISPLAY.");
2541 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2542 return 0;
2543 }
2544 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002545 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002546 s->display_number, s->screen);
2547#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002548 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002549 s->display_number, s->screen);
2550#endif
2551 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002552 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002553 }
2554
Ben Lindstrom768176b2001-06-09 01:29:12 +00002555 return 1;
2556}
2557
Ben Lindstrombba81212001-06-25 05:01:22 +00002558static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002559do_authenticated2(struct ssh *ssh, Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002560{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002561 server_loop2(ssh, authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002562}
2563
2564void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002565do_cleanup(struct ssh *ssh, Authctxt *authctxt)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002566{
2567 static int called = 0;
2568
2569 debug("do_cleanup");
2570
2571 /* no cleanup if we're in the child for login shell */
2572 if (is_child)
2573 return;
2574
2575 /* avoid double cleanup */
2576 if (called)
2577 return;
2578 called = 1;
2579
Darren Tucker9142e1c2007-08-16 23:28:04 +10002580 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002581 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002582
2583#ifdef USE_PAM
2584 if (options.use_pam) {
2585 sshpam_cleanup();
2586 sshpam_thread_cleanup();
2587 }
2588#endif
2589
2590 if (!authctxt->authenticated)
2591 return;
2592
Darren Tucker3e33cec2003-10-02 16:12:36 +10002593#ifdef KRB5
2594 if (options.kerberos_ticket_cleanup &&
2595 authctxt->krb5_ctx)
2596 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002597#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002598
2599#ifdef GSSAPI
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002600 if (options.gss_cleanup_creds)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002601 ssh_gssapi_cleanup_creds();
2602#endif
2603
2604 /* remove agent socket */
2605 auth_sock_cleanup_proc(authctxt->pw);
2606
djm@openbsd.org8f574952017-06-24 06:34:38 +00002607 /* remove userauth info */
2608 if (auth_info_file != NULL) {
2609 temporarily_use_uid(authctxt->pw);
2610 unlink(auth_info_file);
2611 restore_uid();
2612 free(auth_info_file);
2613 auth_info_file = NULL;
2614 }
2615
Darren Tucker3e33cec2003-10-02 16:12:36 +10002616 /*
2617 * Cleanup ptys/utmp only if privsep is disabled,
2618 * or if running in monitor.
2619 */
2620 if (!use_privsep || mm_is_monitor())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002621 session_destroy_all(ssh, session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002622}
djm@openbsd.org95767262016-03-07 19:02:43 +00002623
2624/* Return a name for the remote host that fits inside utmp_size */
2625
2626const char *
2627session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2628{
2629 const char *remote = "";
2630
2631 if (utmp_size > 0)
2632 remote = auth_get_canonical_hostname(ssh, use_dns);
2633 if (utmp_size == 0 || strlen(remote) > utmp_size)
2634 remote = ssh_remote_ipaddr(ssh);
2635 return remote;
2636}
2637