blob: 4bccb62d1e4ddfd381df674f3fe2b3dc7ef873e2 [file] [log] [blame]
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001/* $OpenBSD: session.c,v 1.292 2017/09/12 06:32:07 djm Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +000049#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100050#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100051#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100052#include <grp.h>
Damien Millere5c0d522014-07-03 21:24:19 +100053#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110055#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100057#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110058#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100059#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100060#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100061#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100062#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100063#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000064#include <limits.h>
Damien Millerb38eff82000-04-01 11:09:21 +100065
Damien Millerb84886b2008-05-19 15:05:07 +100066#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100068#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000070#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "packet.h"
72#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100073#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100074#include "uidswap.h"
75#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000076#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100077#include "key.h"
78#include "cipher.h"
79#ifdef GSSAPI
80#include "ssh-gss.h"
81#endif
82#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100083#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100084#include "auth-options.h"
Damien Miller85b45e02013-07-20 13:21:52 +100085#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "pathnames.h"
87#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100088#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000090#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000091#include "serverloop.h"
92#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000093#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100094#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110096#include "sftp.h"
djm@openbsd.org8f574952017-06-24 06:34:38 +000097#include "atomicio.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100098
Darren Tucker3c78c5e2004-01-23 22:03:10 +110099#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +1100100#include <kafs.h>
101#endif
102
Damien Miller14684a12011-05-20 11:23:07 +1000103#ifdef WITH_SELINUX
104#include <selinux/selinux.h>
105#endif
106
Damien Millerad793d52008-11-03 19:17:57 +1100107#define IS_INTERNAL_SFTP(c) \
108 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
109 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
110 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
111 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
112
Damien Millerb38eff82000-04-01 11:09:21 +1000113/* func */
114
115Session *session_new(void);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000116void session_set_fds(struct ssh *, Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000117void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000118void session_proctitle(Session *);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000119int session_setup_x11fwd(struct ssh *, Session *);
120int do_exec_pty(struct ssh *, Session *, const char *);
121int do_exec_no_pty(struct ssh *, Session *, const char *);
122int do_exec(struct ssh *, Session *, const char *);
123void do_login(struct ssh *, Session *, const char *);
124void do_child(struct ssh *, Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000125#ifdef LOGIN_NEEDS_UTMPX
126static void do_pre_login(Session *s);
127#endif
Damien Millercf205e82001-04-16 18:29:15 +1000128void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000129int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000130
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000131static void do_authenticated2(struct ssh *, Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000132
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000133static int session_pty_req(struct ssh *, Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000134
Damien Millerb38eff82000-04-01 11:09:21 +1000135/* import */
136extern ServerOptions options;
137extern char *__progname;
Damien Millerb38eff82000-04-01 11:09:21 +1000138extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000139extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000140extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000141extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000142extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000143
Damien Miller7b28dc52000-09-05 13:34:53 +1100144/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000145const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000148static int sessions_first_unused = -1;
149static int sessions_nalloc = 0;
150static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100151
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100152#define SUBSYSTEM_NONE 0
153#define SUBSYSTEM_EXT 1
154#define SUBSYSTEM_INT_SFTP 2
155#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100156
Damien Millerad833b32000-08-23 10:46:23 +1000157#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000158login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000159#endif
160
Darren Tucker3e33cec2003-10-02 16:12:36 +1000161static int is_child = 0;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +0000162static int in_chroot = 0;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000163
djm@openbsd.org8f574952017-06-24 06:34:38 +0000164/* File containing userauth info, if ExposeAuthInfo set */
165static char *auth_info_file = NULL;
166
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000167/* Name and directory of socket for authentication agent forwarding. */
168static char *auth_sock_name = NULL;
169static char *auth_sock_dir = NULL;
170
171/* removes the agent forwarding socket */
172
173static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000174auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000175{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000176 if (auth_sock_name != NULL) {
177 temporarily_use_uid(pw);
178 unlink(auth_sock_name);
179 rmdir(auth_sock_dir);
180 auth_sock_name = NULL;
181 restore_uid();
182 }
183}
184
185static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000186auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000187{
188 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000189 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000190
191 if (auth_sock_name != NULL) {
192 error("authentication forwarding requested twice.");
193 return 0;
194 }
195
196 /* Temporarily drop privileged uid for mkdir/bind. */
197 temporarily_use_uid(pw);
198
199 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000200 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000201
202 /* Create private directory for socket */
203 if (mkdtemp(auth_sock_dir) == NULL) {
204 packet_send_debug("Agent forwarding disabled: "
205 "mkdtemp() failed: %.100s", strerror(errno));
206 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000207 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000208 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000209 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000210 }
Damien Miller7207f642008-05-19 15:34:50 +1000211
212 xasprintf(&auth_sock_name, "%s/agent.%ld",
213 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000214
Damien Miller7acefbb2014-07-18 14:11:24 +1000215 /* Start a Unix listener on auth_sock_name. */
216 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000217
218 /* Restore the privileged uid. */
219 restore_uid();
220
Damien Miller7acefbb2014-07-18 14:11:24 +1000221 /* Check for socket/bind/listen failure. */
222 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000223 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000224
225 /* Allocate a channel for the authentication agent socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000226 nc = channel_new(ssh, "auth socket",
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000227 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
228 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000229 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100230 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000231 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000232
233 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000234 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000235 if (auth_sock_dir != NULL) {
236 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000237 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000238 }
239 if (sock != -1)
240 close(sock);
241 auth_sock_name = NULL;
242 auth_sock_dir = NULL;
243 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000244}
245
Darren Tucker1921ed92004-02-10 13:23:28 +1100246static void
247display_loginmsg(void)
248{
Damien Miller46d38de2005-07-17 17:02:09 +1000249 if (buffer_len(&loginmsg) > 0) {
250 buffer_append(&loginmsg, "\0", 1);
251 printf("%s", (char *)buffer_ptr(&loginmsg));
252 buffer_clear(&loginmsg);
253 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100254}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000255
djm@openbsd.org8f574952017-06-24 06:34:38 +0000256static void
257prepare_auth_info_file(struct passwd *pw, struct sshbuf *info)
258{
259 int fd = -1, success = 0;
260
261 if (!options.expose_userauth_info || info == NULL)
262 return;
263
264 temporarily_use_uid(pw);
265 auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX");
266 if ((fd = mkstemp(auth_info_file)) == -1) {
267 error("%s: mkstemp: %s", __func__, strerror(errno));
268 goto out;
269 }
270 if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info),
271 sshbuf_len(info)) != sshbuf_len(info)) {
272 error("%s: write: %s", __func__, strerror(errno));
273 goto out;
274 }
275 if (close(fd) != 0) {
276 error("%s: close: %s", __func__, strerror(errno));
277 goto out;
278 }
279 success = 1;
280 out:
281 if (!success) {
282 if (fd != -1)
283 close(fd);
284 free(auth_info_file);
285 auth_info_file = NULL;
286 }
287 restore_uid();
288}
289
Ben Lindstromb31783d2001-03-22 02:02:12 +0000290void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000291do_authenticated(struct ssh *ssh, Authctxt *authctxt)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000292{
Damien Miller97f39ae2003-02-24 11:57:01 +1100293 setproctitle("%s", authctxt->pw->pw_name);
294
Ben Lindstromb31783d2001-03-22 02:02:12 +0000295 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000296 /* XXX - streamlocal? */
djm@openbsd.org7844f352016-11-30 03:00:05 +0000297 if (no_port_forwarding_flag || options.disable_forwarding ||
Damien Milleraa5b3f82012-12-03 09:50:54 +1100298 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000299 channel_disable_adm_local_opens(ssh);
Damien Milleraa5b3f82012-12-03 09:50:54 +1100300 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000301 channel_permit_all_opens(ssh);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000302
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100303 auth_debug_send();
304
djm@openbsd.org8f574952017-06-24 06:34:38 +0000305 prepare_auth_info_file(authctxt->pw, authctxt->session_info);
306
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000307 do_authenticated2(ssh, authctxt);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000308
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000309 do_cleanup(ssh, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000310}
311
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000312/* Check untrusted xauth strings for metacharacters */
313static int
314xauth_valid_string(const char *s)
315{
316 size_t i;
317
318 for (i = 0; s[i] != '\0'; i++) {
319 if (!isalnum((u_char)s[i]) &&
320 s[i] != '.' && s[i] != ':' && s[i] != '/' &&
321 s[i] != '-' && s[i] != '_')
322 return 0;
323 }
324 return 1;
325}
326
Darren Tucker293ee3c2014-01-19 15:28:01 +1100327#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000328/*
329 * This is called to fork and execute a command when we have no tty. This
330 * will call do_child from the child, and server_loop from the parent after
331 * setting up file descriptors and such.
332 */
Damien Miller7207f642008-05-19 15:34:50 +1000333int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000334do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000335{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000336 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000337
338#ifdef USE_PIPES
339 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000340
Damien Miller22a29882010-05-10 11:53:54 +1000341 if (s == NULL)
342 fatal("do_exec_no_pty: no session");
343
Damien Millerb38eff82000-04-01 11:09:21 +1000344 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000345 if (pipe(pin) < 0) {
346 error("%s: pipe in: %.100s", __func__, strerror(errno));
347 return -1;
348 }
349 if (pipe(pout) < 0) {
350 error("%s: pipe out: %.100s", __func__, strerror(errno));
351 close(pin[0]);
352 close(pin[1]);
353 return -1;
354 }
Damien Miller8853ca52010-06-26 10:00:14 +1000355 if (pipe(perr) < 0) {
356 error("%s: pipe err: %.100s", __func__,
357 strerror(errno));
358 close(pin[0]);
359 close(pin[1]);
360 close(pout[0]);
361 close(pout[1]);
362 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000363 }
364#else
Damien Millerb38eff82000-04-01 11:09:21 +1000365 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000366
Damien Miller22a29882010-05-10 11:53:54 +1000367 if (s == NULL)
368 fatal("do_exec_no_pty: no session");
369
Damien Millerb38eff82000-04-01 11:09:21 +1000370 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000371 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
372 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
373 return -1;
374 }
Damien Miller8853ca52010-06-26 10:00:14 +1000375 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
376 error("%s: socketpair #2: %.100s", __func__,
377 strerror(errno));
378 close(inout[0]);
379 close(inout[1]);
380 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000381 }
382#endif
383
Damien Millere247cc42000-05-07 12:03:14 +1000384 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000385
Damien Millerb38eff82000-04-01 11:09:21 +1000386 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000387 switch ((pid = fork())) {
388 case -1:
389 error("%s: fork: %.100s", __func__, strerror(errno));
390#ifdef USE_PIPES
391 close(pin[0]);
392 close(pin[1]);
393 close(pout[0]);
394 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000395 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000396 close(perr[1]);
397#else
398 close(inout[0]);
399 close(inout[1]);
400 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000401 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000402#endif
403 return -1;
404 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000405 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000406
Damien Millerb38eff82000-04-01 11:09:21 +1000407 /*
408 * Create a new session and process group since the 4.4BSD
409 * setlogin() affects the entire process group.
410 */
411 if (setsid() < 0)
412 error("setsid failed: %.100s", strerror(errno));
413
414#ifdef USE_PIPES
415 /*
416 * Redirect stdin. We close the parent side of the socket
417 * pair, and make the child side the standard input.
418 */
419 close(pin[1]);
420 if (dup2(pin[0], 0) < 0)
421 perror("dup2 stdin");
422 close(pin[0]);
423
424 /* Redirect stdout. */
425 close(pout[0]);
426 if (dup2(pout[1], 1) < 0)
427 perror("dup2 stdout");
428 close(pout[1]);
429
430 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000431 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000432 if (dup2(perr[1], 2) < 0)
433 perror("dup2 stderr");
434 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000435#else
Damien Millerb38eff82000-04-01 11:09:21 +1000436 /*
437 * Redirect stdin, stdout, and stderr. Stdin and stdout will
438 * use the same socket, as some programs (particularly rdist)
439 * seem to depend on it.
440 */
441 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000442 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000443 if (dup2(inout[0], 0) < 0) /* stdin */
444 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000445 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000446 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000447 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000448 if (dup2(err[0], 2) < 0) /* stderr */
449 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000450 close(err[0]);
451#endif
452
Damien Millerb38eff82000-04-01 11:09:21 +1000453
Tim Rice81ed5182002-09-25 17:38:46 -0700454#ifdef _UNICOS
455 cray_init_job(s->pw); /* set up cray jid and tmpdir */
456#endif
457
Damien Millerb38eff82000-04-01 11:09:21 +1000458 /* Do processing for the child (exec command etc). */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000459 do_child(ssh, s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000460 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000461 default:
462 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000463 }
Damien Miller7207f642008-05-19 15:34:50 +1000464
Tim Rice81ed5182002-09-25 17:38:46 -0700465#ifdef _UNICOS
466 signal(WJSIGNAL, cray_job_termination_handler);
467#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100468#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100469 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100470#endif
Damien Miller7207f642008-05-19 15:34:50 +1000471
Damien Millerb38eff82000-04-01 11:09:21 +1000472 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000473 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100474 packet_set_interactive(s->display != NULL,
475 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000476
477 /*
478 * Clear loginmsg, since it's the child's responsibility to display
479 * it to the user, otherwise multiple sessions may accumulate
480 * multiple copies of the login messages.
481 */
482 buffer_clear(&loginmsg);
483
Damien Millerb38eff82000-04-01 11:09:21 +1000484#ifdef USE_PIPES
485 /* We are the parent. Close the child sides of the pipes. */
486 close(pin[0]);
487 close(pout[1]);
488 close(perr[1]);
489
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000490 session_set_fds(ssh, s, pin[1], pout[0], perr[0],
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000491 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000492#else
Damien Millerb38eff82000-04-01 11:09:21 +1000493 /* We are the parent. Close the child sides of the socket pairs. */
494 close(inout[0]);
495 close(err[0]);
496
497 /*
498 * Enter the interactive session. Note: server_loop must be able to
499 * handle the case that fdin and fdout are the same.
500 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000501 session_set_fds(s, inout[1], inout[1], err[1],
502 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000503#endif
504 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000505}
506
507/*
508 * This is called to fork and execute a command when we have a tty. This
509 * will call do_child from the child, and server_loop from the parent after
510 * setting up file descriptors, controlling tty, updating wtmp, utmp,
511 * lastlog, and other such operations.
512 */
Damien Miller7207f642008-05-19 15:34:50 +1000513int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000514do_exec_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000515{
Damien Millerb38eff82000-04-01 11:09:21 +1000516 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000517 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000518
519 if (s == NULL)
520 fatal("do_exec_pty: no session");
521 ptyfd = s->ptyfd;
522 ttyfd = s->ttyfd;
523
Damien Miller7207f642008-05-19 15:34:50 +1000524 /*
525 * Create another descriptor of the pty master side for use as the
526 * standard input. We could use the original descriptor, but this
527 * simplifies code in server_loop. The descriptor is bidirectional.
528 * Do this before forking (and cleanup in the child) so as to
529 * detect and gracefully fail out-of-fd conditions.
530 */
531 if ((fdout = dup(ptyfd)) < 0) {
532 error("%s: dup #1: %s", __func__, strerror(errno));
533 close(ttyfd);
534 close(ptyfd);
535 return -1;
536 }
537 /* we keep a reference to the pty master */
538 if ((ptymaster = dup(ptyfd)) < 0) {
539 error("%s: dup #2: %s", __func__, strerror(errno));
540 close(ttyfd);
541 close(ptyfd);
542 close(fdout);
543 return -1;
544 }
545
Damien Millerb38eff82000-04-01 11:09:21 +1000546 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000547 switch ((pid = fork())) {
548 case -1:
549 error("%s: fork: %.100s", __func__, strerror(errno));
550 close(fdout);
551 close(ptymaster);
552 close(ttyfd);
553 close(ptyfd);
554 return -1;
555 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000556 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000557
Damien Miller7207f642008-05-19 15:34:50 +1000558 close(fdout);
559 close(ptymaster);
560
Damien Millerb38eff82000-04-01 11:09:21 +1000561 /* Close the master side of the pseudo tty. */
562 close(ptyfd);
563
564 /* Make the pseudo tty our controlling tty. */
565 pty_make_controlling_tty(&ttyfd, s->tty);
566
Damien Miller9c751422001-10-10 15:02:46 +1000567 /* Redirect stdin/stdout/stderr from the pseudo tty. */
568 if (dup2(ttyfd, 0) < 0)
569 error("dup2 stdin: %s", strerror(errno));
570 if (dup2(ttyfd, 1) < 0)
571 error("dup2 stdout: %s", strerror(errno));
572 if (dup2(ttyfd, 2) < 0)
573 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000574
575 /* Close the extra descriptor for the pseudo tty. */
576 close(ttyfd);
577
Damien Miller942da032000-08-18 13:59:06 +1000578 /* record login, etc. similar to login(1) */
Tim Rice81ed5182002-09-25 17:38:46 -0700579#ifdef _UNICOS
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000580 cray_init_job(s->pw); /* set up cray jid and tmpdir */
Tim Rice81ed5182002-09-25 17:38:46 -0700581#endif /* _UNICOS */
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000582#ifndef HAVE_OSF_SIA
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000583 do_login(ssh, s, command);
Damien Miller364a9bd2001-04-16 18:37:05 +1000584#endif
Damien Miller7207f642008-05-19 15:34:50 +1000585 /*
586 * Do common processing for the child, such as execing
587 * the command.
588 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000589 do_child(ssh, s, command);
Darren Tucker43e7a352009-06-21 19:50:08 +1000590 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000591 default:
592 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000593 }
Damien Miller7207f642008-05-19 15:34:50 +1000594
Tim Rice81ed5182002-09-25 17:38:46 -0700595#ifdef _UNICOS
596 signal(WJSIGNAL, cray_job_termination_handler);
597#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100598#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100599 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100600#endif
Damien Miller7207f642008-05-19 15:34:50 +1000601
Damien Millerb38eff82000-04-01 11:09:21 +1000602 s->pid = pid;
603
604 /* Parent. Close the slave side of the pseudo tty. */
605 close(ttyfd);
606
Damien Millerb38eff82000-04-01 11:09:21 +1000607 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000608 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100609 packet_set_interactive(1,
610 options.ip_qos_interactive, options.ip_qos_bulk);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000611 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
Damien Miller7207f642008-05-19 15:34:50 +1000612 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000613}
614
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000615#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000616static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000617do_pre_login(Session *s)
618{
Darren Tuckeraa377682016-06-20 15:55:34 +1000619 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000620 socklen_t fromlen;
621 struct sockaddr_storage from;
622 pid_t pid = getpid();
623
624 /*
625 * Get IP address of client. If the connection is not a socket, let
626 * the address be 0.0.0.0.
627 */
628 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000629 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000630 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000631 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100632 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000633 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000634 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000635 }
636 }
637
638 record_utmp_only(pid, s->tty, s->pw->pw_name,
Darren Tuckeraa377682016-06-20 15:55:34 +1000639 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000640 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000641}
642#endif
643
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000644/*
645 * This is called to fork and execute a command. If another command is
646 * to be forced, execute that instead.
647 */
Damien Miller7207f642008-05-19 15:34:50 +1000648int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000649do_exec(struct ssh *ssh, Session *s, const char *command)
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000650{
Damien Miller7207f642008-05-19 15:34:50 +1000651 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000652 const char *forced = NULL, *tty = NULL;
653 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000654
Damien Millere2754432006-07-24 14:06:47 +1000655 if (options.adm_forced_command) {
656 original_command = command;
657 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100658 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000659 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000660 original_command = command;
661 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100662 forced = "(key-option)";
663 }
664 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100665 if (IS_INTERNAL_SFTP(command)) {
666 s->is_subsystem = s->is_subsystem ?
667 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
668 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100669 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100670 snprintf(session_type, sizeof(session_type),
671 "forced-command %s '%.900s'", forced, command);
672 } else if (s->is_subsystem) {
673 snprintf(session_type, sizeof(session_type),
674 "subsystem '%.900s'", s->subsys);
675 } else if (command == NULL) {
676 snprintf(session_type, sizeof(session_type), "shell");
677 } else {
678 /* NB. we don't log unforced commands to preserve privacy */
679 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000680 }
681
Damien Miller71df7522013-10-15 12:12:02 +1100682 if (s->ttyfd != -1) {
683 tty = s->tty;
684 if (strncmp(tty, "/dev/", 5) == 0)
685 tty += 5;
686 }
687
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000688 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100689 session_type,
690 tty == NULL ? "" : " on ",
691 tty == NULL ? "" : tty,
692 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000693 ssh_remote_ipaddr(ssh),
694 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000695 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100696
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100697#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100698 if (command != NULL)
699 PRIVSEP(audit_run_command(command));
700 else if (s->ttyfd == -1) {
701 char *shell = s->pw->pw_shell;
702
703 if (shell[0] == '\0') /* empty shell means /bin/sh */
704 shell =_PATH_BSHELL;
705 PRIVSEP(audit_run_command(shell));
706 }
707#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000708 if (s->ttyfd != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000709 ret = do_exec_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000710 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000711 ret = do_exec_no_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000712
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000713 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000714
Darren Tucker09991742004-07-17 17:05:14 +1000715 /*
716 * Clear loginmsg: it's the child's responsibility to display
717 * it to the user, otherwise multiple sessions may accumulate
718 * multiple copies of the login messages.
719 */
720 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000721
722 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000723}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000724
Damien Miller942da032000-08-18 13:59:06 +1000725/* administrative, login(1)-like work */
726void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000727do_login(struct ssh *ssh, Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000728{
Damien Miller942da032000-08-18 13:59:06 +1000729 socklen_t fromlen;
730 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000731 struct passwd * pw = s->pw;
732 pid_t pid = getpid();
733
734 /*
735 * Get IP address of client. If the connection is not a socket, let
736 * the address be 0.0.0.0.
737 */
738 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000739 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000740 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000741 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000742 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000743 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000744 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000745 }
746 }
747
748 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000749 if (!use_privsep)
750 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000751 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000752 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000753 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000754
Kevin Steves092f2ef2000-10-14 13:36:13 +0000755#ifdef USE_PAM
756 /*
757 * If password change is needed, do it now.
758 * This needs to occur before the ~/.hushlogin check.
759 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100760 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
761 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000762 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100763 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000764 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000765 }
766#endif
767
Damien Millercf205e82001-04-16 18:29:15 +1000768 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000769 return;
770
Darren Tucker1921ed92004-02-10 13:23:28 +1100771 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000772
Damien Millercf205e82001-04-16 18:29:15 +1000773 do_motd();
774}
775
776/*
777 * Display the message of the day.
778 */
779void
780do_motd(void)
781{
782 FILE *f;
783 char buf[256];
784
Damien Miller942da032000-08-18 13:59:06 +1000785 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000786#ifdef HAVE_LOGIN_CAP
787 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
788 "/etc/motd"), "r");
789#else
Damien Miller942da032000-08-18 13:59:06 +1000790 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000791#endif
Damien Miller942da032000-08-18 13:59:06 +1000792 if (f) {
793 while (fgets(buf, sizeof(buf), f))
794 fputs(buf, stdout);
795 fclose(f);
796 }
797 }
798}
799
Kevin Steves8f63caa2001-07-04 18:23:02 +0000800
801/*
802 * Check for quiet login, either .hushlogin or command given.
803 */
804int
805check_quietlogin(Session *s, const char *command)
806{
807 char buf[256];
808 struct passwd *pw = s->pw;
809 struct stat st;
810
811 /* Return 1 if .hushlogin exists or a command given. */
812 if (command != NULL)
813 return 1;
814 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
815#ifdef HAVE_LOGIN_CAP
816 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
817 return 1;
818#else
819 if (stat(buf, &st) >= 0)
820 return 1;
821#endif
822 return 0;
823}
824
Damien Millerb38eff82000-04-01 11:09:21 +1000825/*
Damien Millerb38eff82000-04-01 11:09:21 +1000826 * Reads environment variables from the given file and adds/overrides them
827 * into the environment. If the file does not exist, this does nothing.
828 * Otherwise, it must consist of empty lines, comments (line starts with '#')
829 * and assignments of the form name=value. No other forms are allowed.
830 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000831static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000832read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100833 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000834{
835 FILE *f;
836 char buf[4096];
837 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000838 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000839
840 f = fopen(filename, "r");
841 if (!f)
842 return;
843
844 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000845 if (++lineno > 1000)
846 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000847 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
848 ;
849 if (!*cp || *cp == '#' || *cp == '\n')
850 continue;
Damien Miller14b017d2007-09-17 16:09:15 +1000851
852 cp[strcspn(cp, "\n")] = '\0';
853
Damien Millerb38eff82000-04-01 11:09:21 +1000854 value = strchr(cp, '=');
855 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000856 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
857 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000858 continue;
859 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000860 /*
861 * Replace the equals sign by nul, and advance value to
862 * the value string.
863 */
Damien Millerb38eff82000-04-01 11:09:21 +1000864 *value = '\0';
865 value++;
866 child_set_env(env, envsize, cp, value);
867 }
868 fclose(f);
869}
870
Darren Tuckere1a790d2003-09-16 11:52:19 +1000871#ifdef HAVE_ETC_DEFAULT_LOGIN
872/*
873 * Return named variable from specified environment, or NULL if not present.
874 */
875static char *
876child_get_env(char **env, const char *name)
877{
878 int i;
879 size_t len;
880
881 len = strlen(name);
882 for (i=0; env[i] != NULL; i++)
883 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
884 return(env[i] + len + 1);
885 return NULL;
886}
887
888/*
889 * Read /etc/default/login.
890 * We pick up the PATH (or SUPATH for root) and UMASK.
891 */
892static void
893read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
894{
895 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000896 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000897 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000898
899 /*
900 * We don't want to copy the whole file to the child's environment,
901 * so we use a temporary environment and copy the variables we're
902 * interested in.
903 */
904 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
905
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000906 if (tmpenv == NULL)
907 return;
908
Darren Tuckere1a790d2003-09-16 11:52:19 +1000909 if (uid == 0)
910 var = child_get_env(tmpenv, "SUPATH");
911 else
912 var = child_get_env(tmpenv, "PATH");
913 if (var != NULL)
914 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100915
Darren Tuckere1a790d2003-09-16 11:52:19 +1000916 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
917 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000918 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100919
Darren Tuckere1a790d2003-09-16 11:52:19 +1000920 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +1000921 free(tmpenv[i]);
922 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000923}
924#endif /* HAVE_ETC_DEFAULT_LOGIN */
925
Damien Miller94bc1e72017-07-28 14:50:59 +1000926static void
927copy_environment_blacklist(char **source, char ***env, u_int *envsize,
928 const char *blacklist)
Damien Millerb38eff82000-04-01 11:09:21 +1000929{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100930 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000931 int i;
932
Damien Millerbb9ffc12002-01-08 10:59:32 +1100933 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000934 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000935
Damien Millerbb9ffc12002-01-08 10:59:32 +1100936 for(i = 0; source[i] != NULL; i++) {
937 var_name = xstrdup(source[i]);
938 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000939 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000940 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000941 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100942 *var_val++ = '\0';
943
Damien Miller94bc1e72017-07-28 14:50:59 +1000944 if (blacklist == NULL ||
945 match_pattern_list(var_name, blacklist, 0) != 1) {
946 debug3("Copy environment: %s=%s", var_name, var_val);
947 child_set_env(env, envsize, var_name, var_val);
948 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100949
Darren Tuckerf60845f2013-06-02 08:07:31 +1000950 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000951 }
952}
Damien Millercb5e44a2000-09-29 12:12:36 +1100953
Damien Miller94bc1e72017-07-28 14:50:59 +1000954void
955copy_environment(char **source, char ***env, u_int *envsize)
956{
957 copy_environment_blacklist(source, env, envsize, NULL);
958}
959
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000960static char **
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000961do_setup_env(struct ssh *ssh, Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000962{
Damien Millerb38eff82000-04-01 11:09:21 +1000963 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000964 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +1000965 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000966 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100967#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +1000968 char *path = NULL;
969#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000970
Damien Millerb38eff82000-04-01 11:09:21 +1000971 /* Initialize the environment. */
972 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000973 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +1000974 env[0] = NULL;
975
Damien Millerbac2d8a2000-09-05 16:13:06 +1100976#ifdef HAVE_CYGWIN
977 /*
978 * The Windows environment contains some setting which are
979 * important for a running system. They must not be dropped.
980 */
Darren Tucker14c372d2004-08-30 20:42:08 +1000981 {
982 char **p;
983
984 p = fetch_windows_environment();
985 copy_environment(p, &env, &envsize);
986 free_windows_environment(p);
987 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100988#endif
989
Darren Tucker0efd1552003-08-26 11:49:55 +1000990#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100991 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +1000992 * the childs environment as they see fit
993 */
994 ssh_gssapi_do_child(&env, &envsize);
995#endif
996
djm@openbsd.org83b58182016-08-19 03:18:06 +0000997 /* Set basic environment. */
998 for (i = 0; i < s->num_env; i++)
999 child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
Darren Tucker46bc0752004-05-02 22:11:30 +10001000
djm@openbsd.org83b58182016-08-19 03:18:06 +00001001 child_set_env(&env, &envsize, "USER", pw->pw_name);
1002 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001003#ifdef _AIX
djm@openbsd.org83b58182016-08-19 03:18:06 +00001004 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001005#endif
djm@openbsd.org83b58182016-08-19 03:18:06 +00001006 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001007#ifdef HAVE_LOGIN_CAP
djm@openbsd.org83b58182016-08-19 03:18:06 +00001008 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1009 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1010 else
1011 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001012#else /* HAVE_LOGIN_CAP */
1013# ifndef HAVE_CYGWIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001014 /*
1015 * There's no standard path on Windows. The path contains
1016 * important components pointing to the system directories,
1017 * needed for loading shared libraries. So the path better
1018 * remains intact here.
1019 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001020# ifdef HAVE_ETC_DEFAULT_LOGIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001021 read_etc_default_login(&env, &envsize, pw->pw_uid);
1022 path = child_get_env(env, "PATH");
Darren Tuckere1a790d2003-09-16 11:52:19 +10001023# endif /* HAVE_ETC_DEFAULT_LOGIN */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001024 if (path == NULL || *path == '\0') {
1025 child_set_env(&env, &envsize, "PATH",
1026 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1027 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001028# endif /* HAVE_CYGWIN */
1029#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001030
djm@openbsd.org83b58182016-08-19 03:18:06 +00001031 snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
1032 child_set_env(&env, &envsize, "MAIL", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001033
djm@openbsd.org83b58182016-08-19 03:18:06 +00001034 /* Normal systems set SHELL by default. */
1035 child_set_env(&env, &envsize, "SHELL", shell);
1036
Damien Millerb38eff82000-04-01 11:09:21 +10001037 if (getenv("TZ"))
1038 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1039
1040 /* Set custom environment options from RSA authentication. */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001041 while (custom_environment) {
1042 struct envstring *ce = custom_environment;
1043 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001044
djm@openbsd.org83b58182016-08-19 03:18:06 +00001045 for (i = 0; str[i] != '=' && str[i]; i++)
1046 ;
1047 if (str[i] == '=') {
1048 str[i] = 0;
1049 child_set_env(&env, &envsize, str, str + i + 1);
Damien Millerb38eff82000-04-01 11:09:21 +10001050 }
djm@openbsd.org83b58182016-08-19 03:18:06 +00001051 custom_environment = ce->next;
1052 free(ce->s);
1053 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001054 }
1055
Damien Millerf37e2462002-09-19 11:47:55 +10001056 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001057 snprintf(buf, sizeof buf, "%.50s %d %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001058 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1059 ssh_local_port(ssh));
Damien Millerb38eff82000-04-01 11:09:21 +10001060 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1061
Damien Miller00111382003-03-10 11:21:17 +11001062 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001063 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001064 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1065 laddr, ssh_local_port(ssh));
Darren Tuckera627d422013-06-02 07:31:17 +10001066 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001067 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1068
djm@openbsd.org8f574952017-06-24 06:34:38 +00001069 if (auth_info_file != NULL)
1070 child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file);
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001071 if (s->ttyfd != -1)
1072 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1073 if (s->term)
1074 child_set_env(&env, &envsize, "TERM", s->term);
1075 if (s->display)
1076 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001077 if (original_command)
1078 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1079 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001080
Tim Rice81ed5182002-09-25 17:38:46 -07001081#ifdef _UNICOS
1082 if (cray_tmpdir[0] != '\0')
1083 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1084#endif /* _UNICOS */
1085
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001086 /*
1087 * Since we clear KRB5CCNAME at startup, if it's set now then it
1088 * must have been set by a native authentication method (eg AIX or
1089 * SIA), so copy it to the child.
1090 */
1091 {
1092 char *cp;
1093
1094 if ((cp = getenv("KRB5CCNAME")) != NULL)
1095 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1096 }
1097
Damien Millerb38eff82000-04-01 11:09:21 +10001098#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001099 {
1100 char *cp;
1101
1102 if ((cp = getenv("AUTHSTATE")) != NULL)
1103 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001104 read_environment_file(&env, &envsize, "/etc/environment");
1105 }
Damien Millerb38eff82000-04-01 11:09:21 +10001106#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001107#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001108 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001109 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001110 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001111#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001112#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001113 /*
1114 * Pull in any environment variables that may have
1115 * been set by PAM.
1116 */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001117 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001118 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001119
Damien Miller94bc1e72017-07-28 14:50:59 +10001120 /*
1121 * Don't allow SSH_AUTH_INFO variables posted to PAM to leak
1122 * back into the environment.
1123 */
Damien Millerc756e9b2003-11-17 21:41:42 +11001124 p = fetch_pam_child_environment();
Damien Miller94bc1e72017-07-28 14:50:59 +10001125 copy_environment_blacklist(p, &env, &envsize, "SSH_AUTH_INFO*");
Damien Millerc756e9b2003-11-17 21:41:42 +11001126 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001127
Damien Millerc756e9b2003-11-17 21:41:42 +11001128 p = fetch_pam_environment();
Damien Miller94bc1e72017-07-28 14:50:59 +10001129 copy_environment_blacklist(p, &env, &envsize, "SSH_AUTH_INFO*");
Kevin Steves38b050a2002-07-23 00:44:07 +00001130 free_pam_environment(p);
1131 }
Damien Millerb38eff82000-04-01 11:09:21 +10001132#endif /* USE_PAM */
1133
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001134 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001135 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001136 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001137
1138 /* read $HOME/.ssh/environment. */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001139 if (options.permit_user_env) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001140 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001141 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001142 read_environment_file(&env, &envsize, buf);
1143 }
1144 if (debug_flag) {
1145 /* dump the environment */
1146 fprintf(stderr, "Environment:\n");
1147 for (i = 0; env[i]; i++)
1148 fprintf(stderr, " %.200s\n", env[i]);
1149 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001150 return env;
1151}
1152
1153/*
1154 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1155 * first in this order).
1156 */
1157static void
1158do_rc_files(Session *s, const char *shell)
1159{
1160 FILE *f = NULL;
1161 char cmd[1024];
1162 int do_xauth;
1163 struct stat st;
1164
1165 do_xauth =
1166 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1167
Damien Millera1b48cc2008-03-27 11:02:02 +11001168 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001169 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001170 !no_user_rc && options.permit_user_rc &&
1171 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001172 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1173 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1174 if (debug_flag)
1175 fprintf(stderr, "Running %s\n", cmd);
1176 f = popen(cmd, "w");
1177 if (f) {
1178 if (do_xauth)
1179 fprintf(f, "%s %s\n", s->auth_proto,
1180 s->auth_data);
1181 pclose(f);
1182 } else
1183 fprintf(stderr, "Could not run %s\n",
1184 _PATH_SSH_USER_RC);
1185 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1186 if (debug_flag)
1187 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1188 _PATH_SSH_SYSTEM_RC);
1189 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1190 if (f) {
1191 if (do_xauth)
1192 fprintf(f, "%s %s\n", s->auth_proto,
1193 s->auth_data);
1194 pclose(f);
1195 } else
1196 fprintf(stderr, "Could not run %s\n",
1197 _PATH_SSH_SYSTEM_RC);
1198 } else if (do_xauth && options.xauth_location != NULL) {
1199 /* Add authority data to .Xauthority if appropriate. */
1200 if (debug_flag) {
1201 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001202 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001203 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001204 fprintf(stderr,
1205 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001206 options.xauth_location, s->auth_display,
1207 s->auth_proto, s->auth_data);
1208 }
1209 snprintf(cmd, sizeof cmd, "%s -q -",
1210 options.xauth_location);
1211 f = popen(cmd, "w");
1212 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001213 fprintf(f, "remove %s\n",
1214 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001215 fprintf(f, "add %s %s %s\n",
1216 s->auth_display, s->auth_proto,
1217 s->auth_data);
1218 pclose(f);
1219 } else {
1220 fprintf(stderr, "Could not run %s\n",
1221 cmd);
1222 }
1223 }
1224}
1225
1226static void
1227do_nologin(struct passwd *pw)
1228{
1229 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001230 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1231 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001232
1233#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001234 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001235 return;
1236 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001237#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001238 if (pw->pw_uid == 0)
1239 return;
1240 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001241#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001242 if (stat(nl, &sb) == -1) {
1243 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001244 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001245 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001246 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001247
1248 /* /etc/nologin exists. Print its contents if we can and exit. */
1249 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1250 if ((f = fopen(nl, "r")) != NULL) {
1251 while (fgets(buf, sizeof(buf), f))
1252 fputs(buf, stderr);
1253 fclose(f);
1254 }
1255 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001256}
1257
Damien Millerd8cb1f12008-02-10 22:40:12 +11001258/*
1259 * Chroot into a directory after checking it for safety: all path components
1260 * must be root-owned directories with strict permissions.
1261 */
1262static void
1263safely_chroot(const char *path, uid_t uid)
1264{
1265 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001266 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001267 struct stat st;
1268
1269 if (*path != '/')
1270 fatal("chroot path does not begin at root");
1271 if (strlen(path) >= sizeof(component))
1272 fatal("chroot path too long");
1273
1274 /*
1275 * Descend the path, checking that each component is a
1276 * root-owned directory with strict permissions.
1277 */
1278 for (cp = path; cp != NULL;) {
1279 if ((cp = strchr(cp, '/')) == NULL)
1280 strlcpy(component, path, sizeof(component));
1281 else {
1282 cp++;
1283 memcpy(component, path, cp - path);
1284 component[cp - path] = '\0';
1285 }
1286
1287 debug3("%s: checking '%s'", __func__, component);
1288
1289 if (stat(component, &st) != 0)
1290 fatal("%s: stat(\"%s\"): %s", __func__,
1291 component, strerror(errno));
1292 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1293 fatal("bad ownership or modes for chroot "
1294 "directory %s\"%s\"",
1295 cp == NULL ? "" : "component ", component);
1296 if (!S_ISDIR(st.st_mode))
1297 fatal("chroot path %s\"%s\" is not a directory",
1298 cp == NULL ? "" : "component ", component);
1299
1300 }
1301
1302 if (chdir(path) == -1)
1303 fatal("Unable to chdir to chroot path \"%s\": "
1304 "%s", path, strerror(errno));
1305 if (chroot(path) == -1)
1306 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1307 if (chdir("/") == -1)
1308 fatal("%s: chdir(/) after chroot: %s",
1309 __func__, strerror(errno));
1310 verbose("Changed root directory to \"%s\"", path);
1311}
1312
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001313/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001315do_setusercontext(struct passwd *pw)
1316{
Damien Miller54e37732008-02-10 22:48:55 +11001317 char *chroot_path, *tmp;
1318
Darren Tucker97528352010-11-05 12:03:05 +11001319 platform_setusercontext(pw);
1320
Darren Tuckerb12fe272010-11-05 14:47:01 +11001321 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001322#ifdef HAVE_LOGIN_CAP
1323 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001324 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001325 perror("unable to set user context");
1326 exit(1);
1327 }
1328#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001329 if (setlogin(pw->pw_name) < 0)
1330 error("setlogin failed: %s", strerror(errno));
1331 if (setgid(pw->pw_gid) < 0) {
1332 perror("setgid");
1333 exit(1);
1334 }
1335 /* Initialize the group list. */
1336 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1337 perror("initgroups");
1338 exit(1);
1339 }
1340 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001341#endif
1342
Darren Tucker920612e2010-11-05 12:36:15 +11001343 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001344
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001345 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001346 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001347 tmp = tilde_expand_filename(options.chroot_directory,
1348 pw->pw_uid);
1349 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1350 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001351 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001352 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001353 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001354 /* Make sure we don't attempt to chroot again */
1355 free(options.chroot_directory);
1356 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001357 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001358 }
1359
1360#ifdef HAVE_LOGIN_CAP
1361 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1362 perror("unable to set user context (setuser)");
1363 exit(1);
1364 }
Damien Miller58528402013-03-15 11:22:37 +11001365 /*
1366 * FreeBSD's setusercontext() will not apply the user's
1367 * own umask setting unless running with the user's UID.
1368 */
1369 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001370#else
Tim Rice9464ba62014-01-20 17:59:28 -08001371# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001372 /*
1373 * In a chroot environment, the set_id() will always fail;
1374 * typically because of the lack of necessary authentication
1375 * services and runtime such as ./usr/lib/libiaf.so,
1376 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1377 * internal sftp chroot case. We'll lose auditing and ACLs but
1378 * permanently_set_uid will take care of the rest.
1379 */
1380 if (!in_chroot && set_id(pw->pw_name) != 0)
1381 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001382# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001383 /* Permanently switch to the desired uid. */
1384 permanently_set_uid(pw);
1385#endif
Damien Millera56086b2013-04-23 15:24:18 +10001386 } else if (options.chroot_directory != NULL &&
1387 strcasecmp(options.chroot_directory, "none") != 0) {
1388 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001389 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001390
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001391 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1392 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1393}
1394
Ben Lindstrom08105192002-03-22 02:50:06 +00001395static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001396do_pwchange(Session *s)
1397{
Darren Tucker09991742004-07-17 17:05:14 +10001398 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001399 fprintf(stderr, "WARNING: Your password has expired.\n");
1400 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001401 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001402 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001403#ifdef WITH_SELINUX
1404 setexeccon(NULL);
1405#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001406#ifdef PASSWD_NEEDS_USERNAME
1407 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1408 (char *)NULL);
1409#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001410 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001411#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001412 perror("passwd");
1413 } else {
1414 fprintf(stderr,
1415 "Password change required but no TTY available.\n");
1416 }
1417 exit(1);
1418}
1419
1420static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001421child_close_fds(struct ssh *ssh)
Darren Tucker23bc8d02004-02-06 16:24:31 +11001422{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001423 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001424
djm@openbsd.org141efe42015-01-14 20:05:27 +00001425 if (auth_sock != -1) {
1426 close(auth_sock);
1427 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001428 }
1429
Darren Tucker23bc8d02004-02-06 16:24:31 +11001430 if (packet_get_connection_in() == packet_get_connection_out())
1431 close(packet_get_connection_in());
1432 else {
1433 close(packet_get_connection_in());
1434 close(packet_get_connection_out());
1435 }
1436 /*
1437 * Close all descriptors related to channels. They will still remain
1438 * open in the parent.
1439 */
1440 /* XXX better use close-on-exec? -markus */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001441 channel_close_all(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001442
1443 /*
1444 * Close any extra file descriptors. Note that there may still be
1445 * descriptors left by system functions. They will be closed later.
1446 */
1447 endpwent();
1448
1449 /*
Damien Miller788f2122005-11-05 15:14:59 +11001450 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001451 * hanging around in clients. Note that we want to do this after
1452 * initgroups, because at least on Solaris 2.3 it leaves file
1453 * descriptors open.
1454 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001455 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001456}
1457
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001458/*
1459 * Performs common processing for the child, such as setting up the
1460 * environment, closing extra file descriptors, setting the user and group
1461 * ids, and executing the command or shell.
1462 */
Damien Millerdfc24252008-02-10 22:29:40 +11001463#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001464void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001465do_child(struct ssh *ssh, Session *s, const char *command)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001466{
1467 extern char **environ;
1468 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001469 char *argv[ARGV_MAX];
djm@openbsd.org83b58182016-08-19 03:18:06 +00001470 const char *shell, *shell0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001471 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001472 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001473
1474 /* remove hostkey from the child's memory */
1475 destroy_sensitive_data();
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001476 packet_clear_keys();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001477
Darren Tucker23bc8d02004-02-06 16:24:31 +11001478 /* Force a password change */
1479 if (s->authctxt->force_pwchange) {
1480 do_setusercontext(pw);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001481 child_close_fds(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001482 do_pwchange(s);
1483 exit(1);
1484 }
1485
Tim Rice81ed5182002-09-25 17:38:46 -07001486#ifdef _UNICOS
1487 cray_setup(pw->pw_uid, pw->pw_name, command);
1488#endif /* _UNICOS */
1489
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001490 /*
1491 * Login(1) does this as well, and it needs uid 0 for the "-h"
1492 * switch, so we let login(1) to this for us.
1493 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001494#ifdef HAVE_OSF_SIA
djm@openbsd.org83b58182016-08-19 03:18:06 +00001495 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1496 if (!check_quietlogin(s, command))
1497 do_motd();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001498#else /* HAVE_OSF_SIA */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001499 /* When PAM is enabled we rely on it to do the nologin check */
1500 if (!options.use_pam)
1501 do_nologin(pw);
1502 do_setusercontext(pw);
1503 /*
1504 * PAM session modules in do_setusercontext may have
1505 * generated messages, so if this in an interactive
1506 * login then display them too.
1507 */
1508 if (!check_quietlogin(s, command))
1509 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001510#endif /* HAVE_OSF_SIA */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001511
Darren Tucker69687f42004-09-11 22:17:26 +10001512#ifdef USE_PAM
djm@openbsd.org83b58182016-08-19 03:18:06 +00001513 if (options.use_pam && !is_pam_session_open()) {
Darren Tucker48554152005-04-21 19:50:55 +10001514 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001515 display_loginmsg();
1516 exit(254);
1517 }
1518#endif
1519
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001520 /*
1521 * Get the shell from the password data. An empty shell field is
1522 * legal, and means /bin/sh.
1523 */
1524 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001525
1526 /*
1527 * Make sure $SHELL points to the shell from the password file,
1528 * even if shell is overridden from login.conf
1529 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001530 env = do_setup_env(ssh, s, shell);
Ben Lindstrom46767602002-12-23 02:26:08 +00001531
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001532#ifdef HAVE_LOGIN_CAP
1533 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1534#endif
1535
Damien Millerb38eff82000-04-01 11:09:21 +10001536 /*
1537 * Close the connection descriptors; note that this is the child, and
1538 * the server will still have the socket open, and it is important
1539 * that we do not shutdown it. Note that the descriptors cannot be
1540 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001541 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001542 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001543 child_close_fds(ssh);
Damien Millerb38eff82000-04-01 11:09:21 +10001544
Damien Millerb38eff82000-04-01 11:09:21 +10001545 /*
Damien Miller05eda432002-02-10 18:32:28 +11001546 * Must take new environment into use so that .ssh/rc,
1547 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001548 */
1549 environ = env;
1550
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001551#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001552 /*
1553 * At this point, we check to see if AFS is active and if we have
1554 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1555 * if we can (and need to) extend the ticket into an AFS token. If
1556 * we don't do this, we run into potential problems if the user's
1557 * home directory is in AFS and it's not world-readable.
1558 */
1559
1560 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001561 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001562 char cell[64];
1563
1564 debug("Getting AFS token");
1565
1566 k_setpag();
1567
1568 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1569 krb5_afslog(s->authctxt->krb5_ctx,
1570 s->authctxt->krb5_fwd_ccache, cell, NULL);
1571
1572 krb5_afslog_home(s->authctxt->krb5_ctx,
1573 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1574 }
1575#endif
1576
Damien Miller788f2122005-11-05 15:14:59 +11001577 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001578 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001579 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001580#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001581 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001582#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001583 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001584 fprintf(stderr, "Could not chdir to home "
1585 "directory %s: %s\n", pw->pw_dir,
1586 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001587 }
Damien Miller6051c942008-06-16 07:53:16 +10001588 if (r)
1589 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001590 }
1591
Damien Millera1939002008-03-15 17:27:58 +11001592 closefrom(STDERR_FILENO + 1);
1593
djm@openbsd.org83b58182016-08-19 03:18:06 +00001594 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001595
1596 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001597 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001598
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001599 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1600 printf("This service allows sftp connections only.\n");
1601 fflush(NULL);
1602 exit(1);
1603 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001604 extern int optind, optreset;
1605 int i;
1606 char *p, *args;
1607
Darren Tuckerac46a912009-06-21 17:55:23 +10001608 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001609 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001610 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1611 if (i < ARGV_MAX - 1)
1612 argv[i++] = p;
1613 argv[i] = NULL;
1614 optind = optreset = 1;
1615 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001616#ifdef WITH_SELINUX
1617 ssh_selinux_change_context("sftpd_t");
1618#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001619 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001620 }
1621
Darren Tucker695ed392009-10-07 09:02:18 +11001622 fflush(NULL);
1623
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001624 /* Get the last component of the shell name. */
1625 if ((shell0 = strrchr(shell, '/')) != NULL)
1626 shell0++;
1627 else
1628 shell0 = shell;
1629
Damien Millerb38eff82000-04-01 11:09:21 +10001630 /*
1631 * If we have no command, execute the shell. In this case, the shell
1632 * name to be passed in argv[0] is preceded by '-' to indicate that
1633 * this is a login shell.
1634 */
1635 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001636 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001637
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001638 /* Start the shell. Set initial character to '-'. */
1639 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001640
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001641 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1642 >= sizeof(argv0) - 1) {
1643 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001644 perror(shell);
1645 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001646 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001647
1648 /* Execute the shell. */
1649 argv[0] = argv0;
1650 argv[1] = NULL;
1651 execve(shell, argv, env);
1652
1653 /* Executing the shell failed. */
1654 perror(shell);
1655 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001656 }
1657 /*
1658 * Execute the command using the user's shell. This uses the -c
1659 * option to execute the command.
1660 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001661 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001662 argv[1] = "-c";
1663 argv[2] = (char *) command;
1664 argv[3] = NULL;
1665 execve(shell, argv, env);
1666 perror(shell);
1667 exit(1);
1668}
1669
Damien Miller7207f642008-05-19 15:34:50 +10001670void
1671session_unused(int id)
1672{
1673 debug3("%s: session id %d unused", __func__, id);
1674 if (id >= options.max_sessions ||
1675 id >= sessions_nalloc) {
1676 fatal("%s: insane session id %d (max %d nalloc %d)",
1677 __func__, id, options.max_sessions, sessions_nalloc);
1678 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001679 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001680 sessions[id].self = id;
1681 sessions[id].used = 0;
1682 sessions[id].chanid = -1;
1683 sessions[id].ptyfd = -1;
1684 sessions[id].ttyfd = -1;
1685 sessions[id].ptymaster = -1;
1686 sessions[id].x11_chanids = NULL;
1687 sessions[id].next_unused = sessions_first_unused;
1688 sessions_first_unused = id;
1689}
1690
Damien Millerb38eff82000-04-01 11:09:21 +10001691Session *
1692session_new(void)
1693{
Damien Miller7207f642008-05-19 15:34:50 +10001694 Session *s, *tmp;
1695
1696 if (sessions_first_unused == -1) {
1697 if (sessions_nalloc >= options.max_sessions)
1698 return NULL;
1699 debug2("%s: allocate (allocated %d max %d)",
1700 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001701 tmp = xrecallocarray(sessions, sessions_nalloc,
1702 sessions_nalloc + 1, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001703 if (tmp == NULL) {
1704 error("%s: cannot allocate %d sessions",
1705 __func__, sessions_nalloc + 1);
1706 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001707 }
Damien Miller7207f642008-05-19 15:34:50 +10001708 sessions = tmp;
1709 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001710 }
Damien Miller7207f642008-05-19 15:34:50 +10001711
1712 if (sessions_first_unused >= sessions_nalloc ||
1713 sessions_first_unused < 0) {
1714 fatal("%s: insane first_unused %d max %d nalloc %d",
1715 __func__, sessions_first_unused, options.max_sessions,
1716 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001717 }
Damien Miller7207f642008-05-19 15:34:50 +10001718
1719 s = &sessions[sessions_first_unused];
1720 if (s->used) {
1721 fatal("%s: session %d already used",
1722 __func__, sessions_first_unused);
1723 }
1724 sessions_first_unused = s->next_unused;
1725 s->used = 1;
1726 s->next_unused = -1;
1727 debug("session_new: session %d", s->self);
1728
1729 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001730}
1731
Ben Lindstrombba81212001-06-25 05:01:22 +00001732static void
Damien Millerb38eff82000-04-01 11:09:21 +10001733session_dump(void)
1734{
1735 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001736 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001737 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001738
1739 debug("dump: used %d next_unused %d session %d %p "
1740 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001741 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001742 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001743 s->self,
1744 s,
1745 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001746 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001747 }
1748}
1749
Damien Millerefb4afe2000-04-12 18:45:05 +10001750int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001751session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001752{
1753 Session *s = session_new();
1754 debug("session_open: channel %d", chanid);
1755 if (s == NULL) {
1756 error("no more sessions");
1757 return 0;
1758 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001759 s->authctxt = authctxt;
1760 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001761 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001762 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001763 debug("session_open: session %d: link with channel %d", s->self, chanid);
1764 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001765 return 1;
1766}
1767
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001768Session *
1769session_by_tty(char *tty)
1770{
1771 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001772 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001773 Session *s = &sessions[i];
1774 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1775 debug("session_by_tty: session %d tty %s", i, tty);
1776 return s;
1777 }
1778 }
1779 debug("session_by_tty: unknown tty %.100s", tty);
1780 session_dump();
1781 return NULL;
1782}
1783
Ben Lindstrombba81212001-06-25 05:01:22 +00001784static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001785session_by_channel(int id)
1786{
1787 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001788 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001789 Session *s = &sessions[i];
1790 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001791 debug("session_by_channel: session %d channel %d",
1792 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001793 return s;
1794 }
1795 }
1796 debug("session_by_channel: unknown channel %d", id);
1797 session_dump();
1798 return NULL;
1799}
1800
Ben Lindstrombba81212001-06-25 05:01:22 +00001801static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001802session_by_x11_channel(int id)
1803{
1804 int i, j;
1805
Damien Miller7207f642008-05-19 15:34:50 +10001806 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001807 Session *s = &sessions[i];
1808
1809 if (s->x11_chanids == NULL || !s->used)
1810 continue;
1811 for (j = 0; s->x11_chanids[j] != -1; j++) {
1812 if (s->x11_chanids[j] == id) {
1813 debug("session_by_x11_channel: session %d "
1814 "channel %d", s->self, id);
1815 return s;
1816 }
1817 }
1818 }
1819 debug("session_by_x11_channel: unknown channel %d", id);
1820 session_dump();
1821 return NULL;
1822}
1823
1824static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001825session_by_pid(pid_t pid)
1826{
1827 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001828 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001829 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001830 Session *s = &sessions[i];
1831 if (s->used && s->pid == pid)
1832 return s;
1833 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001834 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001835 session_dump();
1836 return NULL;
1837}
1838
Ben Lindstrombba81212001-06-25 05:01:22 +00001839static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001840session_window_change_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001841{
1842 s->col = packet_get_int();
1843 s->row = packet_get_int();
1844 s->xpixel = packet_get_int();
1845 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001846 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001847 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1848 return 1;
1849}
1850
Ben Lindstrombba81212001-06-25 05:01:22 +00001851static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001852session_pty_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001853{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001854 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001855 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001856
Damien Miller5ff30c62013-10-30 22:21:50 +11001857 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001858 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001859 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001860 }
1861 if (s->ttyfd != -1) {
1862 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001863 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001864 }
1865
Damien Millerefb4afe2000-04-12 18:45:05 +10001866 s->term = packet_get_string(&len);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001867 s->col = packet_get_int();
1868 s->row = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +10001869 s->xpixel = packet_get_int();
1870 s->ypixel = packet_get_int();
1871
1872 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001873 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001874 s->term = NULL;
1875 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001876
Damien Millerefb4afe2000-04-12 18:45:05 +10001877 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001878 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10001879 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1880 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10001881 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001882 s->term = NULL;
1883 s->ptyfd = -1;
1884 s->ttyfd = -1;
1885 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001886 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001887 }
1888 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001889
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001890 n_bytes = packet_remaining();
Ben Lindstrom49c12602001-06-13 04:37:36 +00001891 tty_parse_modes(s->ttyfd, &n_bytes);
1892
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001893 if (!use_privsep)
1894 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001895
1896 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001897 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1898
Damien Miller48b03fc2002-01-22 23:11:40 +11001899 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001900 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001901 return 1;
1902}
1903
Ben Lindstrombba81212001-06-25 05:01:22 +00001904static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001905session_subsystem_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001906{
Damien Millerae452462001-10-10 15:08:06 +10001907 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001908 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001909 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11001910 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10001911 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001912
Damien Miller71df7522013-10-15 12:12:02 +11001913 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001914 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11001915 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10001916 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10001917
Damien Millerf6d9e222000-06-18 14:50:44 +10001918 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11001919 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001920 prog = options.subsystem_command[i];
1921 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001922 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11001923 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001924 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11001925 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001926 if (stat(prog, &st) < 0)
1927 debug("subsystem: cannot stat %s: %s",
1928 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001929 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001930 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001931 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001932 success = do_exec(ssh, s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11001933 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001934 }
1935 }
1936
1937 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11001938 logit("subsystem request for %.100s by user %s failed, "
1939 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10001940
Damien Millerbd483e72000-04-30 10:00:53 +10001941 return success;
1942}
1943
Ben Lindstrombba81212001-06-25 05:01:22 +00001944static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001945session_x11_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001946{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001947 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001948
Damien Miller2b9b0452005-07-17 17:19:24 +10001949 if (s->auth_proto != NULL || s->auth_data != NULL) {
1950 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11001951 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10001952 return 0;
1953 }
Damien Millerbd483e72000-04-30 10:00:53 +10001954 s->single_connection = packet_get_char();
1955 s->auth_proto = packet_get_string(NULL);
1956 s->auth_data = packet_get_string(NULL);
1957 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001958 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001959
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00001960 if (xauth_valid_string(s->auth_proto) &&
1961 xauth_valid_string(s->auth_data))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001962 success = session_setup_x11fwd(ssh, s);
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00001963 else {
1964 success = 0;
1965 error("Invalid X11 forwarding data");
1966 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001967 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10001968 free(s->auth_proto);
1969 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001970 s->auth_proto = NULL;
1971 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001972 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001973 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001974}
1975
Ben Lindstrombba81212001-06-25 05:01:22 +00001976static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001977session_shell_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10001978{
Damien Miller48b03fc2002-01-22 23:11:40 +11001979 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001980 return do_exec(ssh, s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10001981}
1982
Ben Lindstrombba81212001-06-25 05:01:22 +00001983static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001984session_exec_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10001985{
Damien Miller7207f642008-05-19 15:34:50 +10001986 u_int len, success;
1987
Damien Millerf6d9e222000-06-18 14:50:44 +10001988 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001989 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001990 success = do_exec(ssh, s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10001991 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10001992 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10001993}
1994
Ben Lindstrombba81212001-06-25 05:01:22 +00001995static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001996session_break_req(struct ssh *ssh, Session *s)
Damien Miller54c45982003-05-15 10:20:13 +10001997{
Damien Miller54c45982003-05-15 10:20:13 +10001998
Darren Tucker1f8311c2004-05-13 16:39:33 +10001999 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002000 packet_check_eom();
2001
Darren Tucker9c5d5532011-11-04 10:55:24 +11002002 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002003 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002004 return 1;
2005}
2006
2007static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002008session_env_req(struct ssh *ssh, Session *s)
Darren Tucker46bc0752004-05-02 22:11:30 +10002009{
2010 char *name, *val;
2011 u_int name_len, val_len, i;
2012
Damien Miller8569eba2014-03-04 09:35:17 +11002013 name = packet_get_cstring(&name_len);
2014 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002015 packet_check_eom();
2016
2017 /* Don't set too many environment variables */
2018 if (s->num_env > 128) {
2019 debug2("Ignoring env request %s: too many env vars", name);
2020 goto fail;
2021 }
2022
2023 for (i = 0; i < options.num_accept_env; i++) {
2024 if (match_pattern(name, options.accept_env[i])) {
2025 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002026 s->env = xrecallocarray(s->env, s->num_env,
2027 s->num_env + 1, sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002028 s->env[s->num_env].name = name;
2029 s->env[s->num_env].val = val;
2030 s->num_env++;
2031 return (1);
2032 }
2033 }
2034 debug2("Ignoring env request %s: disallowed name", name);
2035
2036 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002037 free(name);
2038 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002039 return (0);
2040}
2041
2042static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002043session_auth_agent_req(struct ssh *ssh, Session *s)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002044{
2045 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002046 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002047 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002048 debug("session_auth_agent_req: no_agent_forwarding_flag");
2049 return 0;
2050 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002051 if (called) {
2052 return 0;
2053 } else {
2054 called = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002055 return auth_input_request_forwarding(ssh, s->pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002056 }
2057}
2058
Damien Millerc7ef63d2002-02-05 12:21:42 +11002059int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002060session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002061{
Damien Millerefb4afe2000-04-12 18:45:05 +10002062 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002063 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002064
Damien Millerc7ef63d2002-02-05 12:21:42 +11002065 if ((s = session_by_channel(c->self)) == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002066 logit("%s: no session %d req %.100s", __func__, c->self, rtype);
Damien Millerc7ef63d2002-02-05 12:21:42 +11002067 return 0;
2068 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002069 debug("%s: session %d req %s", __func__, s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002070
2071 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002072 * a session is in LARVAL state until a shell, a command
2073 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002074 */
2075 if (c->type == SSH_CHANNEL_LARVAL) {
2076 if (strcmp(rtype, "shell") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002077 success = session_shell_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002078 } else if (strcmp(rtype, "exec") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002079 success = session_exec_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002080 } else if (strcmp(rtype, "pty-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002081 success = session_pty_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002082 } else if (strcmp(rtype, "x11-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002083 success = session_x11_req(ssh, s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002084 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002085 success = session_auth_agent_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002086 } else if (strcmp(rtype, "subsystem") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002087 success = session_subsystem_req(ssh, s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002088 } else if (strcmp(rtype, "env") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002089 success = session_env_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002090 }
2091 }
2092 if (strcmp(rtype, "window-change") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002093 success = session_window_change_req(ssh, s);
Damien Millera6b1d162004-06-30 22:41:07 +10002094 } else if (strcmp(rtype, "break") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002095 success = session_break_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002096 }
Damien Millera6b1d162004-06-30 22:41:07 +10002097
Damien Millerc7ef63d2002-02-05 12:21:42 +11002098 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002099}
2100
2101void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002102session_set_fds(struct ssh *ssh, Session *s,
2103 int fdin, int fdout, int fderr, int ignore_fderr, int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002104{
Damien Millerefb4afe2000-04-12 18:45:05 +10002105 /*
2106 * now that have a child and a pipe to the child,
2107 * we can activate our channel and register the fd's
2108 */
2109 if (s->chanid == -1)
2110 fatal("no channel for session %d", s->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002111 channel_set_fds(ssh, s->chanid,
Damien Millerefb4afe2000-04-12 18:45:05 +10002112 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002113 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002114 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002115}
2116
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002117/*
2118 * Function to perform pty cleanup. Also called if we get aborted abnormally
2119 * (e.g., due to a dropped connection).
2120 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002121void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002122session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002123{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002124 if (s == NULL) {
2125 error("session_pty_cleanup: no session");
2126 return;
2127 }
2128 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002129 return;
2130
Kevin Steves43cdef32001-02-11 14:12:08 +00002131 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002132
Damien Millerb38eff82000-04-01 11:09:21 +10002133 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002134 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002135 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002136
2137 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002138 if (getuid() == 0)
2139 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002140
2141 /*
2142 * Close the server side of the socket pairs. We must do this after
2143 * the pty cleanup, so that another process doesn't get this pty
2144 * while we're still cleaning up.
2145 */
Damien Miller7207f642008-05-19 15:34:50 +10002146 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2147 error("close(s->ptymaster/%d): %s",
2148 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002149
2150 /* unlink pty from session */
2151 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002152}
Damien Millerefb4afe2000-04-12 18:45:05 +10002153
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002154void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002155session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002156{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002157 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002158}
2159
Damien Miller5a80bba2002-09-04 16:39:02 +10002160static char *
2161sig2name(int sig)
2162{
2163#define SSH_SIG(x) if (sig == SIG ## x) return #x
2164 SSH_SIG(ABRT);
2165 SSH_SIG(ALRM);
2166 SSH_SIG(FPE);
2167 SSH_SIG(HUP);
2168 SSH_SIG(ILL);
2169 SSH_SIG(INT);
2170 SSH_SIG(KILL);
2171 SSH_SIG(PIPE);
2172 SSH_SIG(QUIT);
2173 SSH_SIG(SEGV);
2174 SSH_SIG(TERM);
2175 SSH_SIG(USR1);
2176 SSH_SIG(USR2);
2177#undef SSH_SIG
2178 return "SIG@openssh.com";
2179}
2180
Ben Lindstrombba81212001-06-25 05:01:22 +00002181static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002182session_close_x11(struct ssh *ssh, int id)
Damien Miller2b9b0452005-07-17 17:19:24 +10002183{
2184 Channel *c;
2185
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002186 if ((c = channel_by_id(ssh, id)) == NULL) {
2187 debug("%s: x11 channel %d missing", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002188 } else {
2189 /* Detach X11 listener */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002190 debug("%s: detach x11 channel %d", __func__, id);
2191 channel_cancel_cleanup(ssh, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002192 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002193 chan_mark_dead(ssh, c);
Damien Miller2b9b0452005-07-17 17:19:24 +10002194 }
2195}
2196
2197static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002198session_close_single_x11(struct ssh *ssh, int id, void *arg)
Damien Miller2b9b0452005-07-17 17:19:24 +10002199{
2200 Session *s;
2201 u_int i;
2202
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002203 debug3("%s: channel %d", __func__, id);
2204 channel_cancel_cleanup(ssh, id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002205 if ((s = session_by_x11_channel(id)) == NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002206 fatal("%s: no x11 channel %d", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002207 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002208 debug("%s: session %d: closing channel %d",
2209 __func__, s->self, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002210 /*
2211 * The channel "id" is already closing, but make sure we
2212 * close all of its siblings.
2213 */
2214 if (s->x11_chanids[i] != id)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002215 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002216 }
Darren Tuckera627d422013-06-02 07:31:17 +10002217 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002218 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002219 free(s->display);
2220 s->display = NULL;
2221 free(s->auth_proto);
2222 s->auth_proto = NULL;
2223 free(s->auth_data);
2224 s->auth_data = NULL;
2225 free(s->auth_display);
2226 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002227}
2228
2229static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002230session_exit_message(struct ssh *ssh, Session *s, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002231{
2232 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002233
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002234 if ((c = channel_lookup(ssh, s->chanid)) == NULL)
2235 fatal("%s: session %d: no channel %d",
2236 __func__, s->self, s->chanid);
2237 debug("%s: session %d channel %d pid %ld",
2238 __func__, s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002239
2240 if (WIFEXITED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002241 channel_request_start(ssh, s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002242 packet_put_int(WEXITSTATUS(status));
2243 packet_send();
2244 } else if (WIFSIGNALED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002245 channel_request_start(ssh, s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002246 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002247#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002248 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002249#else /* WCOREDUMP */
2250 packet_put_char(0);
2251#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002252 packet_put_cstring("");
2253 packet_put_cstring("");
2254 packet_send();
2255 } else {
2256 /* Some weird exit cause. Just exit. */
2257 packet_disconnect("wait returned status %04x.", status);
2258 }
2259
2260 /* disconnect channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002261 debug("%s: release channel %d", __func__, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002262
2263 /*
2264 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002265 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002266 * by session_close_by_channel when the childs close their fds.
2267 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002268 channel_register_cleanup(ssh, c->self, session_close_by_channel, 1);
Damien Miller39eda6e2005-11-05 14:52:50 +11002269
Damien Miller166fca82000-04-20 07:42:21 +10002270 /*
2271 * emulate a write failure with 'chan_write_failed', nobody will be
2272 * interested in data we write.
2273 * Note that we must not call 'chan_read_failed', since there could
2274 * be some more data waiting in the pipe.
2275 */
Damien Millerbd483e72000-04-30 10:00:53 +10002276 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002277 chan_write_failed(ssh, c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002278}
2279
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002280void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002281session_close(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002282{
Damien Millereccb9de2005-06-17 12:59:34 +10002283 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002284
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002285 verbose("Close session: user %s from %.200s port %d id %d",
2286 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002287 ssh_remote_ipaddr(ssh),
2288 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002289 s->self);
2290
Darren Tucker3e33cec2003-10-02 16:12:36 +10002291 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002292 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002293 free(s->term);
2294 free(s->display);
2295 free(s->x11_chanids);
2296 free(s->auth_display);
2297 free(s->auth_data);
2298 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002299 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002300 if (s->env != NULL) {
2301 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002302 free(s->env[i].name);
2303 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002304 }
Darren Tuckera627d422013-06-02 07:31:17 +10002305 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002306 }
Damien Millere247cc42000-05-07 12:03:14 +10002307 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002308 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002309}
2310
2311void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002312session_close_by_pid(struct ssh *ssh, pid_t pid, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002313{
2314 Session *s = session_by_pid(pid);
2315 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002316 debug("%s: no session for pid %ld", __func__, (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002317 return;
2318 }
2319 if (s->chanid != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002320 session_exit_message(ssh, s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002321 if (s->ttyfd != -1)
2322 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002323 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002324}
2325
2326/*
2327 * this is called when a channel dies before
2328 * the session 'child' itself dies
2329 */
2330void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002331session_close_by_channel(struct ssh *ssh, int id, void *arg)
Damien Millerefb4afe2000-04-12 18:45:05 +10002332{
2333 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002334 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002335
Damien Millerefb4afe2000-04-12 18:45:05 +10002336 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002337 debug("%s: no session for id %d", __func__, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002338 return;
2339 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002340 debug("%s: channel %d child %ld", __func__, id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002341 if (s->pid != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002342 debug("%s: channel %d: has child", __func__, id);
Damien Miller0585d512001-10-12 11:35:50 +10002343 /*
2344 * delay detach of session, but release pty, since
2345 * the fd's to the child are already closed
2346 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002347 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002348 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002349 return;
2350 }
2351 /* detach by removing callback */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002352 channel_cancel_cleanup(ssh, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002353
2354 /* Close any X11 listeners associated with this session */
2355 if (s->x11_chanids != NULL) {
2356 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002357 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller39eda6e2005-11-05 14:52:50 +11002358 s->x11_chanids[i] = -1;
2359 }
2360 }
2361
Damien Millerefb4afe2000-04-12 18:45:05 +10002362 s->chanid = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002363 session_close(ssh, s);
Damien Miller52b77be2001-10-10 15:14:37 +10002364}
2365
2366void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002367session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002368{
2369 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002370 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002371 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002372 if (s->used) {
2373 if (closefunc != NULL)
2374 closefunc(s);
2375 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002376 session_close(ssh, s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002377 }
Damien Miller52b77be2001-10-10 15:14:37 +10002378 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002379}
2380
Ben Lindstrombba81212001-06-25 05:01:22 +00002381static char *
Damien Millere247cc42000-05-07 12:03:14 +10002382session_tty_list(void)
2383{
2384 static char buf[1024];
2385 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002386 char *cp;
2387
Damien Millere247cc42000-05-07 12:03:14 +10002388 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002389 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002390 Session *s = &sessions[i];
2391 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002392
Damien Millera8ed44b2003-01-10 09:53:12 +11002393 if (strncmp(s->tty, "/dev/", 5) != 0) {
2394 cp = strrchr(s->tty, '/');
2395 cp = (cp == NULL) ? s->tty : cp + 1;
2396 } else
2397 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002398
Damien Millere247cc42000-05-07 12:03:14 +10002399 if (buf[0] != '\0')
2400 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002401 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002402 }
2403 }
2404 if (buf[0] == '\0')
2405 strlcpy(buf, "notty", sizeof buf);
2406 return buf;
2407}
2408
2409void
2410session_proctitle(Session *s)
2411{
2412 if (s->pw == NULL)
2413 error("no user for session %d", s->self);
2414 else
2415 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2416}
2417
Ben Lindstrom768176b2001-06-09 01:29:12 +00002418int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002419session_setup_x11fwd(struct ssh *ssh, Session *s)
Ben Lindstrom768176b2001-06-09 01:29:12 +00002420{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002421 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002422 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002423 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002424 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002425
2426 if (no_x11_forwarding_flag) {
2427 packet_send_debug("X11 forwarding disabled in user configuration file.");
2428 return 0;
2429 }
2430 if (!options.x11_forwarding) {
2431 debug("X11 forwarding disabled in server configuration file.");
2432 return 0;
2433 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002434 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002435 (stat(options.xauth_location, &st) == -1)) {
2436 packet_send_debug("No xauth program; cannot forward with spoofing.");
2437 return 0;
2438 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002439 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002440 debug("X11 display already set.");
2441 return 0;
2442 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002443 if (x11_create_display_inet(ssh, options.x11_display_offset,
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002444 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002445 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002446 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002447 return 0;
2448 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002449 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002450 channel_register_cleanup(ssh, s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002451 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002452 }
Kevin Steves366298c2001-12-19 17:58:01 +00002453
2454 /* Set up a suitable value for the DISPLAY variable. */
2455 if (gethostname(hostname, sizeof(hostname)) < 0)
2456 fatal("gethostname: %.100s", strerror(errno));
2457 /*
2458 * auth_display must be used as the displayname when the
2459 * authorization entry is added with xauth(1). This will be
2460 * different than the DISPLAY string for localhost displays.
2461 */
Damien Miller95c249f2002-02-05 12:11:34 +11002462 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002463 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002464 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002465 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002466 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002467 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002468 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002469 } else {
2470#ifdef IPADDR_IN_DISPLAY
2471 struct hostent *he;
2472 struct in_addr my_addr;
2473
2474 he = gethostbyname(hostname);
2475 if (he == NULL) {
2476 error("Can't get IP address for X11 DISPLAY.");
2477 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2478 return 0;
2479 }
2480 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002481 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002482 s->display_number, s->screen);
2483#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002484 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002485 s->display_number, s->screen);
2486#endif
2487 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002488 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002489 }
2490
Ben Lindstrom768176b2001-06-09 01:29:12 +00002491 return 1;
2492}
2493
Ben Lindstrombba81212001-06-25 05:01:22 +00002494static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002495do_authenticated2(struct ssh *ssh, Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002496{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002497 server_loop2(ssh, authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002498}
2499
2500void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002501do_cleanup(struct ssh *ssh, Authctxt *authctxt)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002502{
2503 static int called = 0;
2504
2505 debug("do_cleanup");
2506
2507 /* no cleanup if we're in the child for login shell */
2508 if (is_child)
2509 return;
2510
2511 /* avoid double cleanup */
2512 if (called)
2513 return;
2514 called = 1;
2515
Darren Tucker9142e1c2007-08-16 23:28:04 +10002516 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002517 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002518
2519#ifdef USE_PAM
2520 if (options.use_pam) {
2521 sshpam_cleanup();
2522 sshpam_thread_cleanup();
2523 }
2524#endif
2525
2526 if (!authctxt->authenticated)
2527 return;
2528
Darren Tucker3e33cec2003-10-02 16:12:36 +10002529#ifdef KRB5
2530 if (options.kerberos_ticket_cleanup &&
2531 authctxt->krb5_ctx)
2532 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002533#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002534
2535#ifdef GSSAPI
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002536 if (options.gss_cleanup_creds)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002537 ssh_gssapi_cleanup_creds();
2538#endif
2539
2540 /* remove agent socket */
2541 auth_sock_cleanup_proc(authctxt->pw);
2542
djm@openbsd.org8f574952017-06-24 06:34:38 +00002543 /* remove userauth info */
2544 if (auth_info_file != NULL) {
2545 temporarily_use_uid(authctxt->pw);
2546 unlink(auth_info_file);
2547 restore_uid();
2548 free(auth_info_file);
2549 auth_info_file = NULL;
2550 }
2551
Darren Tucker3e33cec2003-10-02 16:12:36 +10002552 /*
2553 * Cleanup ptys/utmp only if privsep is disabled,
2554 * or if running in monitor.
2555 */
2556 if (!use_privsep || mm_is_monitor())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002557 session_destroy_all(ssh, session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002558}
djm@openbsd.org95767262016-03-07 19:02:43 +00002559
2560/* Return a name for the remote host that fits inside utmp_size */
2561
2562const char *
2563session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2564{
2565 const char *remote = "";
2566
2567 if (utmp_size > 0)
2568 remote = auth_get_canonical_hostname(ssh, use_dns);
2569 if (utmp_size == 0 || strlen(remote) > utmp_size)
2570 remote = ssh_remote_ipaddr(ssh);
2571 return remote;
2572}
2573