blob: a701fa0cdc6d1802c6fe04a744c9d55459a2fd30 [file] [log] [blame]
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001/* $OpenBSD: session.c,v 1.293 2017/10/23 05:08:00 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;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000143char *tun_fwd_ifnames; /* serverloop.c */
Damien Miller37023962000-07-11 17:31:38 +1000144
Damien Miller7b28dc52000-09-05 13:34:53 +1100145/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000146const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100147
Damien Millerb38eff82000-04-01 11:09:21 +1000148/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000149static int sessions_first_unused = -1;
150static int sessions_nalloc = 0;
151static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100152
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100153#define SUBSYSTEM_NONE 0
154#define SUBSYSTEM_EXT 1
155#define SUBSYSTEM_INT_SFTP 2
156#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100157
Damien Millerad833b32000-08-23 10:46:23 +1000158#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000159login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000160#endif
161
Darren Tucker3e33cec2003-10-02 16:12:36 +1000162static int is_child = 0;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +0000163static int in_chroot = 0;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000164
djm@openbsd.org8f574952017-06-24 06:34:38 +0000165/* File containing userauth info, if ExposeAuthInfo set */
166static char *auth_info_file = NULL;
167
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000168/* Name and directory of socket for authentication agent forwarding. */
169static char *auth_sock_name = NULL;
170static char *auth_sock_dir = NULL;
171
172/* removes the agent forwarding socket */
173
174static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000175auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000176{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000177 if (auth_sock_name != NULL) {
178 temporarily_use_uid(pw);
179 unlink(auth_sock_name);
180 rmdir(auth_sock_dir);
181 auth_sock_name = NULL;
182 restore_uid();
183 }
184}
185
186static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000187auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000188{
189 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000190 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000191
192 if (auth_sock_name != NULL) {
193 error("authentication forwarding requested twice.");
194 return 0;
195 }
196
197 /* Temporarily drop privileged uid for mkdir/bind. */
198 temporarily_use_uid(pw);
199
200 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000201 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000202
203 /* Create private directory for socket */
204 if (mkdtemp(auth_sock_dir) == NULL) {
205 packet_send_debug("Agent forwarding disabled: "
206 "mkdtemp() failed: %.100s", strerror(errno));
207 restore_uid();
Darren Tuckera627d422013-06-02 07:31:17 +1000208 free(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000209 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000210 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000211 }
Damien Miller7207f642008-05-19 15:34:50 +1000212
213 xasprintf(&auth_sock_name, "%s/agent.%ld",
214 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000215
Damien Miller7acefbb2014-07-18 14:11:24 +1000216 /* Start a Unix listener on auth_sock_name. */
217 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000218
219 /* Restore the privileged uid. */
220 restore_uid();
221
Damien Miller7acefbb2014-07-18 14:11:24 +1000222 /* Check for socket/bind/listen failure. */
223 if (sock < 0)
Damien Miller7207f642008-05-19 15:34:50 +1000224 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000225
226 /* Allocate a channel for the authentication agent socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000227 nc = channel_new(ssh, "auth socket",
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000228 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
229 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000230 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100231 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000232 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000233
234 authsock_err:
Darren Tuckera627d422013-06-02 07:31:17 +1000235 free(auth_sock_name);
Damien Miller7207f642008-05-19 15:34:50 +1000236 if (auth_sock_dir != NULL) {
237 rmdir(auth_sock_dir);
Darren Tuckera627d422013-06-02 07:31:17 +1000238 free(auth_sock_dir);
Damien Miller7207f642008-05-19 15:34:50 +1000239 }
240 if (sock != -1)
241 close(sock);
242 auth_sock_name = NULL;
243 auth_sock_dir = NULL;
244 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000245}
246
Darren Tucker1921ed92004-02-10 13:23:28 +1100247static void
248display_loginmsg(void)
249{
Damien Miller46d38de2005-07-17 17:02:09 +1000250 if (buffer_len(&loginmsg) > 0) {
251 buffer_append(&loginmsg, "\0", 1);
252 printf("%s", (char *)buffer_ptr(&loginmsg));
253 buffer_clear(&loginmsg);
254 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100255}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000256
djm@openbsd.org8f574952017-06-24 06:34:38 +0000257static void
258prepare_auth_info_file(struct passwd *pw, struct sshbuf *info)
259{
260 int fd = -1, success = 0;
261
262 if (!options.expose_userauth_info || info == NULL)
263 return;
264
265 temporarily_use_uid(pw);
266 auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX");
267 if ((fd = mkstemp(auth_info_file)) == -1) {
268 error("%s: mkstemp: %s", __func__, strerror(errno));
269 goto out;
270 }
271 if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info),
272 sshbuf_len(info)) != sshbuf_len(info)) {
273 error("%s: write: %s", __func__, strerror(errno));
274 goto out;
275 }
276 if (close(fd) != 0) {
277 error("%s: close: %s", __func__, strerror(errno));
278 goto out;
279 }
280 success = 1;
281 out:
282 if (!success) {
283 if (fd != -1)
284 close(fd);
285 free(auth_info_file);
286 auth_info_file = NULL;
287 }
288 restore_uid();
289}
290
Ben Lindstromb31783d2001-03-22 02:02:12 +0000291void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000292do_authenticated(struct ssh *ssh, Authctxt *authctxt)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000293{
Damien Miller97f39ae2003-02-24 11:57:01 +1100294 setproctitle("%s", authctxt->pw->pw_name);
295
Ben Lindstromb31783d2001-03-22 02:02:12 +0000296 /* setup the channel layer */
Damien Miller7acefbb2014-07-18 14:11:24 +1000297 /* XXX - streamlocal? */
djm@openbsd.org7844f352016-11-30 03:00:05 +0000298 if (no_port_forwarding_flag || options.disable_forwarding ||
Damien Milleraa5b3f82012-12-03 09:50:54 +1100299 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000300 channel_disable_adm_local_opens(ssh);
Damien Milleraa5b3f82012-12-03 09:50:54 +1100301 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000302 channel_permit_all_opens(ssh);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000303
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100304 auth_debug_send();
305
djm@openbsd.org8f574952017-06-24 06:34:38 +0000306 prepare_auth_info_file(authctxt->pw, authctxt->session_info);
307
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000308 do_authenticated2(ssh, authctxt);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000309
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000310 do_cleanup(ssh, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000311}
312
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +0000313/* Check untrusted xauth strings for metacharacters */
314static int
315xauth_valid_string(const char *s)
316{
317 size_t i;
318
319 for (i = 0; s[i] != '\0'; i++) {
320 if (!isalnum((u_char)s[i]) &&
321 s[i] != '.' && s[i] != ':' && s[i] != '/' &&
322 s[i] != '-' && s[i] != '_')
323 return 0;
324 }
325 return 1;
326}
327
Darren Tucker293ee3c2014-01-19 15:28:01 +1100328#define USE_PIPES 1
Damien Millerb38eff82000-04-01 11:09:21 +1000329/*
330 * This is called to fork and execute a command when we have no tty. This
331 * will call do_child from the child, and server_loop from the parent after
332 * setting up file descriptors and such.
333 */
Damien Miller7207f642008-05-19 15:34:50 +1000334int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000335do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000336{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000337 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000338
339#ifdef USE_PIPES
340 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000341
Damien Miller22a29882010-05-10 11:53:54 +1000342 if (s == NULL)
343 fatal("do_exec_no_pty: no session");
344
Damien Millerb38eff82000-04-01 11:09:21 +1000345 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000346 if (pipe(pin) < 0) {
347 error("%s: pipe in: %.100s", __func__, strerror(errno));
348 return -1;
349 }
350 if (pipe(pout) < 0) {
351 error("%s: pipe out: %.100s", __func__, strerror(errno));
352 close(pin[0]);
353 close(pin[1]);
354 return -1;
355 }
Damien Miller8853ca52010-06-26 10:00:14 +1000356 if (pipe(perr) < 0) {
357 error("%s: pipe err: %.100s", __func__,
358 strerror(errno));
359 close(pin[0]);
360 close(pin[1]);
361 close(pout[0]);
362 close(pout[1]);
363 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000364 }
365#else
Damien Millerb38eff82000-04-01 11:09:21 +1000366 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000367
Damien Miller22a29882010-05-10 11:53:54 +1000368 if (s == NULL)
369 fatal("do_exec_no_pty: no session");
370
Damien Millerb38eff82000-04-01 11:09:21 +1000371 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000372 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
373 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
374 return -1;
375 }
Damien Miller8853ca52010-06-26 10:00:14 +1000376 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
377 error("%s: socketpair #2: %.100s", __func__,
378 strerror(errno));
379 close(inout[0]);
380 close(inout[1]);
381 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000382 }
383#endif
384
Damien Millere247cc42000-05-07 12:03:14 +1000385 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000386
Damien Millerb38eff82000-04-01 11:09:21 +1000387 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000388 switch ((pid = fork())) {
389 case -1:
390 error("%s: fork: %.100s", __func__, strerror(errno));
391#ifdef USE_PIPES
392 close(pin[0]);
393 close(pin[1]);
394 close(pout[0]);
395 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000396 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000397 close(perr[1]);
398#else
399 close(inout[0]);
400 close(inout[1]);
401 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000402 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000403#endif
404 return -1;
405 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000406 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000407
Damien Millerb38eff82000-04-01 11:09:21 +1000408 /*
409 * Create a new session and process group since the 4.4BSD
410 * setlogin() affects the entire process group.
411 */
412 if (setsid() < 0)
413 error("setsid failed: %.100s", strerror(errno));
414
415#ifdef USE_PIPES
416 /*
417 * Redirect stdin. We close the parent side of the socket
418 * pair, and make the child side the standard input.
419 */
420 close(pin[1]);
421 if (dup2(pin[0], 0) < 0)
422 perror("dup2 stdin");
423 close(pin[0]);
424
425 /* Redirect stdout. */
426 close(pout[0]);
427 if (dup2(pout[1], 1) < 0)
428 perror("dup2 stdout");
429 close(pout[1]);
430
431 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000432 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000433 if (dup2(perr[1], 2) < 0)
434 perror("dup2 stderr");
435 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000436#else
Damien Millerb38eff82000-04-01 11:09:21 +1000437 /*
438 * Redirect stdin, stdout, and stderr. Stdin and stdout will
439 * use the same socket, as some programs (particularly rdist)
440 * seem to depend on it.
441 */
442 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000443 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000444 if (dup2(inout[0], 0) < 0) /* stdin */
445 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000446 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000447 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000448 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000449 if (dup2(err[0], 2) < 0) /* stderr */
450 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000451 close(err[0]);
452#endif
453
Damien Millerb38eff82000-04-01 11:09:21 +1000454
Tim Rice81ed5182002-09-25 17:38:46 -0700455#ifdef _UNICOS
456 cray_init_job(s->pw); /* set up cray jid and tmpdir */
457#endif
458
Damien Millerb38eff82000-04-01 11:09:21 +1000459 /* Do processing for the child (exec command etc). */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000460 do_child(ssh, s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000461 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000462 default:
463 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000464 }
Damien Miller7207f642008-05-19 15:34:50 +1000465
Tim Rice81ed5182002-09-25 17:38:46 -0700466#ifdef _UNICOS
467 signal(WJSIGNAL, cray_job_termination_handler);
468#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100469#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100470 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100471#endif
Damien Miller7207f642008-05-19 15:34:50 +1000472
Damien Millerb38eff82000-04-01 11:09:21 +1000473 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000474 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100475 packet_set_interactive(s->display != NULL,
476 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000477
478 /*
479 * Clear loginmsg, since it's the child's responsibility to display
480 * it to the user, otherwise multiple sessions may accumulate
481 * multiple copies of the login messages.
482 */
483 buffer_clear(&loginmsg);
484
Damien Millerb38eff82000-04-01 11:09:21 +1000485#ifdef USE_PIPES
486 /* We are the parent. Close the child sides of the pipes. */
487 close(pin[0]);
488 close(pout[1]);
489 close(perr[1]);
490
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000491 session_set_fds(ssh, s, pin[1], pout[0], perr[0],
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000492 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000493#else
Damien Millerb38eff82000-04-01 11:09:21 +1000494 /* We are the parent. Close the child sides of the socket pairs. */
495 close(inout[0]);
496 close(err[0]);
497
498 /*
499 * Enter the interactive session. Note: server_loop must be able to
500 * handle the case that fdin and fdout are the same.
501 */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000502 session_set_fds(s, inout[1], inout[1], err[1],
503 s->is_subsystem, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000504#endif
505 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000506}
507
508/*
509 * This is called to fork and execute a command when we have a tty. This
510 * will call do_child from the child, and server_loop from the parent after
511 * setting up file descriptors, controlling tty, updating wtmp, utmp,
512 * lastlog, and other such operations.
513 */
Damien Miller7207f642008-05-19 15:34:50 +1000514int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000515do_exec_pty(struct ssh *ssh, Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000516{
Damien Millerb38eff82000-04-01 11:09:21 +1000517 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000518 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000519
520 if (s == NULL)
521 fatal("do_exec_pty: no session");
522 ptyfd = s->ptyfd;
523 ttyfd = s->ttyfd;
524
Damien Miller7207f642008-05-19 15:34:50 +1000525 /*
526 * Create another descriptor of the pty master side for use as the
527 * standard input. We could use the original descriptor, but this
528 * simplifies code in server_loop. The descriptor is bidirectional.
529 * Do this before forking (and cleanup in the child) so as to
530 * detect and gracefully fail out-of-fd conditions.
531 */
532 if ((fdout = dup(ptyfd)) < 0) {
533 error("%s: dup #1: %s", __func__, strerror(errno));
534 close(ttyfd);
535 close(ptyfd);
536 return -1;
537 }
538 /* we keep a reference to the pty master */
539 if ((ptymaster = dup(ptyfd)) < 0) {
540 error("%s: dup #2: %s", __func__, strerror(errno));
541 close(ttyfd);
542 close(ptyfd);
543 close(fdout);
544 return -1;
545 }
546
Damien Millerb38eff82000-04-01 11:09:21 +1000547 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000548 switch ((pid = fork())) {
549 case -1:
550 error("%s: fork: %.100s", __func__, strerror(errno));
551 close(fdout);
552 close(ptymaster);
553 close(ttyfd);
554 close(ptyfd);
555 return -1;
556 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000557 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000558
Damien Miller7207f642008-05-19 15:34:50 +1000559 close(fdout);
560 close(ptymaster);
561
Damien Millerb38eff82000-04-01 11:09:21 +1000562 /* Close the master side of the pseudo tty. */
563 close(ptyfd);
564
565 /* Make the pseudo tty our controlling tty. */
566 pty_make_controlling_tty(&ttyfd, s->tty);
567
Damien Miller9c751422001-10-10 15:02:46 +1000568 /* Redirect stdin/stdout/stderr from the pseudo tty. */
569 if (dup2(ttyfd, 0) < 0)
570 error("dup2 stdin: %s", strerror(errno));
571 if (dup2(ttyfd, 1) < 0)
572 error("dup2 stdout: %s", strerror(errno));
573 if (dup2(ttyfd, 2) < 0)
574 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000575
576 /* Close the extra descriptor for the pseudo tty. */
577 close(ttyfd);
578
Damien Miller942da032000-08-18 13:59:06 +1000579 /* record login, etc. similar to login(1) */
Tim Rice81ed5182002-09-25 17:38:46 -0700580#ifdef _UNICOS
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000581 cray_init_job(s->pw); /* set up cray jid and tmpdir */
Tim Rice81ed5182002-09-25 17:38:46 -0700582#endif /* _UNICOS */
otto@openbsd.orgfc041c42016-08-23 16:21:45 +0000583#ifndef HAVE_OSF_SIA
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000584 do_login(ssh, s, command);
Damien Miller364a9bd2001-04-16 18:37:05 +1000585#endif
Damien Miller7207f642008-05-19 15:34:50 +1000586 /*
587 * Do common processing for the child, such as execing
588 * the command.
589 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000590 do_child(ssh, s, command);
Darren Tucker43e7a352009-06-21 19:50:08 +1000591 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000592 default:
593 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000594 }
Damien Miller7207f642008-05-19 15:34:50 +1000595
Tim Rice81ed5182002-09-25 17:38:46 -0700596#ifdef _UNICOS
597 signal(WJSIGNAL, cray_job_termination_handler);
598#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100599#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100600 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100601#endif
Damien Miller7207f642008-05-19 15:34:50 +1000602
Damien Millerb38eff82000-04-01 11:09:21 +1000603 s->pid = pid;
604
605 /* Parent. Close the slave side of the pseudo tty. */
606 close(ttyfd);
607
Damien Millerb38eff82000-04-01 11:09:21 +1000608 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000609 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100610 packet_set_interactive(1,
611 options.ip_qos_interactive, options.ip_qos_bulk);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000612 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
Damien Miller7207f642008-05-19 15:34:50 +1000613 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000614}
615
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000616#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000617static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000618do_pre_login(Session *s)
619{
Darren Tuckeraa377682016-06-20 15:55:34 +1000620 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000621 socklen_t fromlen;
622 struct sockaddr_storage from;
623 pid_t pid = getpid();
624
625 /*
626 * Get IP address of client. If the connection is not a socket, let
627 * the address be 0.0.0.0.
628 */
629 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000630 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000631 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000632 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100633 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000634 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000635 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000636 }
637 }
638
639 record_utmp_only(pid, s->tty, s->pw->pw_name,
Darren Tuckeraa377682016-06-20 15:55:34 +1000640 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000641 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000642}
643#endif
644
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000645/*
646 * This is called to fork and execute a command. If another command is
647 * to be forced, execute that instead.
648 */
Damien Miller7207f642008-05-19 15:34:50 +1000649int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000650do_exec(struct ssh *ssh, Session *s, const char *command)
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000651{
Damien Miller7207f642008-05-19 15:34:50 +1000652 int ret;
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000653 const char *forced = NULL, *tty = NULL;
654 char session_type[1024];
Damien Miller7207f642008-05-19 15:34:50 +1000655
Damien Millere2754432006-07-24 14:06:47 +1000656 if (options.adm_forced_command) {
657 original_command = command;
658 command = options.adm_forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100659 forced = "(config)";
Damien Millere2754432006-07-24 14:06:47 +1000660 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000661 original_command = command;
662 command = forced_command;
Damien Miller71df7522013-10-15 12:12:02 +1100663 forced = "(key-option)";
664 }
665 if (forced != NULL) {
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100666 if (IS_INTERNAL_SFTP(command)) {
667 s->is_subsystem = s->is_subsystem ?
668 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
669 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100670 s->is_subsystem = SUBSYSTEM_EXT;
Damien Miller71df7522013-10-15 12:12:02 +1100671 snprintf(session_type, sizeof(session_type),
672 "forced-command %s '%.900s'", forced, command);
673 } else if (s->is_subsystem) {
674 snprintf(session_type, sizeof(session_type),
675 "subsystem '%.900s'", s->subsys);
676 } else if (command == NULL) {
677 snprintf(session_type, sizeof(session_type), "shell");
678 } else {
679 /* NB. we don't log unforced commands to preserve privacy */
680 snprintf(session_type, sizeof(session_type), "command");
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000681 }
682
Damien Miller71df7522013-10-15 12:12:02 +1100683 if (s->ttyfd != -1) {
684 tty = s->tty;
685 if (strncmp(tty, "/dev/", 5) == 0)
686 tty += 5;
687 }
688
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000689 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
Damien Miller71df7522013-10-15 12:12:02 +1100690 session_type,
691 tty == NULL ? "" : " on ",
692 tty == NULL ? "" : tty,
693 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +0000694 ssh_remote_ipaddr(ssh),
695 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +0000696 s->self);
Damien Miller71df7522013-10-15 12:12:02 +1100697
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100698#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100699 if (command != NULL)
700 PRIVSEP(audit_run_command(command));
701 else if (s->ttyfd == -1) {
702 char *shell = s->pw->pw_shell;
703
704 if (shell[0] == '\0') /* empty shell means /bin/sh */
705 shell =_PATH_BSHELL;
706 PRIVSEP(audit_run_command(shell));
707 }
708#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000709 if (s->ttyfd != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000710 ret = do_exec_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000711 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000712 ret = do_exec_no_pty(ssh, s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000713
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000714 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000715
Darren Tucker09991742004-07-17 17:05:14 +1000716 /*
717 * Clear loginmsg: it's the child's responsibility to display
718 * it to the user, otherwise multiple sessions may accumulate
719 * multiple copies of the login messages.
720 */
721 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000722
723 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000724}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000725
Damien Miller942da032000-08-18 13:59:06 +1000726/* administrative, login(1)-like work */
727void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000728do_login(struct ssh *ssh, Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000729{
Damien Miller942da032000-08-18 13:59:06 +1000730 socklen_t fromlen;
731 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000732 struct passwd * pw = s->pw;
733 pid_t pid = getpid();
734
735 /*
736 * Get IP address of client. If the connection is not a socket, let
737 * the address be 0.0.0.0.
738 */
739 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000740 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000741 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000742 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000743 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000744 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000745 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000746 }
747 }
748
749 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000750 if (!use_privsep)
751 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +0000752 session_get_remote_name_or_ip(ssh, utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000753 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000754 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000755
Kevin Steves092f2ef2000-10-14 13:36:13 +0000756#ifdef USE_PAM
757 /*
758 * If password change is needed, do it now.
759 * This needs to occur before the ~/.hushlogin check.
760 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100761 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
762 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000763 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100764 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000765 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000766 }
767#endif
768
Damien Millercf205e82001-04-16 18:29:15 +1000769 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000770 return;
771
Darren Tucker1921ed92004-02-10 13:23:28 +1100772 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000773
Damien Millercf205e82001-04-16 18:29:15 +1000774 do_motd();
775}
776
777/*
778 * Display the message of the day.
779 */
780void
781do_motd(void)
782{
783 FILE *f;
784 char buf[256];
785
Damien Miller942da032000-08-18 13:59:06 +1000786 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000787#ifdef HAVE_LOGIN_CAP
788 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
789 "/etc/motd"), "r");
790#else
Damien Miller942da032000-08-18 13:59:06 +1000791 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000792#endif
Damien Miller942da032000-08-18 13:59:06 +1000793 if (f) {
794 while (fgets(buf, sizeof(buf), f))
795 fputs(buf, stdout);
796 fclose(f);
797 }
798 }
799}
800
Kevin Steves8f63caa2001-07-04 18:23:02 +0000801
802/*
803 * Check for quiet login, either .hushlogin or command given.
804 */
805int
806check_quietlogin(Session *s, const char *command)
807{
808 char buf[256];
809 struct passwd *pw = s->pw;
810 struct stat st;
811
812 /* Return 1 if .hushlogin exists or a command given. */
813 if (command != NULL)
814 return 1;
815 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
816#ifdef HAVE_LOGIN_CAP
817 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
818 return 1;
819#else
820 if (stat(buf, &st) >= 0)
821 return 1;
822#endif
823 return 0;
824}
825
Damien Millerb38eff82000-04-01 11:09:21 +1000826/*
Damien Millerb38eff82000-04-01 11:09:21 +1000827 * Reads environment variables from the given file and adds/overrides them
828 * into the environment. If the file does not exist, this does nothing.
829 * Otherwise, it must consist of empty lines, comments (line starts with '#')
830 * and assignments of the form name=value. No other forms are allowed.
831 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000833read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100834 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000835{
836 FILE *f;
837 char buf[4096];
838 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000839 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000840
841 f = fopen(filename, "r");
842 if (!f)
843 return;
844
845 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000846 if (++lineno > 1000)
847 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000848 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
849 ;
850 if (!*cp || *cp == '#' || *cp == '\n')
851 continue;
Damien Miller14b017d2007-09-17 16:09:15 +1000852
853 cp[strcspn(cp, "\n")] = '\0';
854
Damien Millerb38eff82000-04-01 11:09:21 +1000855 value = strchr(cp, '=');
856 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000857 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
858 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000859 continue;
860 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000861 /*
862 * Replace the equals sign by nul, and advance value to
863 * the value string.
864 */
Damien Millerb38eff82000-04-01 11:09:21 +1000865 *value = '\0';
866 value++;
867 child_set_env(env, envsize, cp, value);
868 }
869 fclose(f);
870}
871
Darren Tuckere1a790d2003-09-16 11:52:19 +1000872#ifdef HAVE_ETC_DEFAULT_LOGIN
873/*
874 * Return named variable from specified environment, or NULL if not present.
875 */
876static char *
877child_get_env(char **env, const char *name)
878{
879 int i;
880 size_t len;
881
882 len = strlen(name);
883 for (i=0; env[i] != NULL; i++)
884 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
885 return(env[i] + len + 1);
886 return NULL;
887}
888
889/*
890 * Read /etc/default/login.
891 * We pick up the PATH (or SUPATH for root) and UMASK.
892 */
893static void
894read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
895{
896 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000897 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000898 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000899
900 /*
901 * We don't want to copy the whole file to the child's environment,
902 * so we use a temporary environment and copy the variables we're
903 * interested in.
904 */
905 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
906
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000907 if (tmpenv == NULL)
908 return;
909
Darren Tuckere1a790d2003-09-16 11:52:19 +1000910 if (uid == 0)
911 var = child_get_env(tmpenv, "SUPATH");
912 else
913 var = child_get_env(tmpenv, "PATH");
914 if (var != NULL)
915 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100916
Darren Tuckere1a790d2003-09-16 11:52:19 +1000917 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
918 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000919 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100920
Darren Tuckere1a790d2003-09-16 11:52:19 +1000921 for (i = 0; tmpenv[i] != NULL; i++)
Darren Tuckerf60845f2013-06-02 08:07:31 +1000922 free(tmpenv[i]);
923 free(tmpenv);
Darren Tuckere1a790d2003-09-16 11:52:19 +1000924}
925#endif /* HAVE_ETC_DEFAULT_LOGIN */
926
Damien Miller94bc1e72017-07-28 14:50:59 +1000927static void
928copy_environment_blacklist(char **source, char ***env, u_int *envsize,
929 const char *blacklist)
Damien Millerb38eff82000-04-01 11:09:21 +1000930{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100931 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000932 int i;
933
Damien Millerbb9ffc12002-01-08 10:59:32 +1100934 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000935 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000936
Damien Millerbb9ffc12002-01-08 10:59:32 +1100937 for(i = 0; source[i] != NULL; i++) {
938 var_name = xstrdup(source[i]);
939 if ((var_val = strstr(var_name, "=")) == NULL) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000940 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000941 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000942 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100943 *var_val++ = '\0';
944
Damien Miller94bc1e72017-07-28 14:50:59 +1000945 if (blacklist == NULL ||
946 match_pattern_list(var_name, blacklist, 0) != 1) {
947 debug3("Copy environment: %s=%s", var_name, var_val);
948 child_set_env(env, envsize, var_name, var_val);
949 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100950
Darren Tuckerf60845f2013-06-02 08:07:31 +1000951 free(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000952 }
953}
Damien Millercb5e44a2000-09-29 12:12:36 +1100954
Damien Miller94bc1e72017-07-28 14:50:59 +1000955void
956copy_environment(char **source, char ***env, u_int *envsize)
957{
958 copy_environment_blacklist(source, env, envsize, NULL);
959}
960
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000961static char **
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000962do_setup_env(struct ssh *ssh, Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000963{
Damien Millerb38eff82000-04-01 11:09:21 +1000964 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000965 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +1000966 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000967 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100968#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +1000969 char *path = NULL;
970#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000971
Damien Millerb38eff82000-04-01 11:09:21 +1000972 /* Initialize the environment. */
973 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +1000974 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +1000975 env[0] = NULL;
976
Damien Millerbac2d8a2000-09-05 16:13:06 +1100977#ifdef HAVE_CYGWIN
978 /*
979 * The Windows environment contains some setting which are
980 * important for a running system. They must not be dropped.
981 */
Darren Tucker14c372d2004-08-30 20:42:08 +1000982 {
983 char **p;
984
985 p = fetch_windows_environment();
986 copy_environment(p, &env, &envsize);
987 free_windows_environment(p);
988 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100989#endif
990
Darren Tucker0efd1552003-08-26 11:49:55 +1000991#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100992 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +1000993 * the childs environment as they see fit
994 */
995 ssh_gssapi_do_child(&env, &envsize);
996#endif
997
djm@openbsd.org83b58182016-08-19 03:18:06 +0000998 /* Set basic environment. */
999 for (i = 0; i < s->num_env; i++)
1000 child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
Darren Tucker46bc0752004-05-02 22:11:30 +10001001
djm@openbsd.org83b58182016-08-19 03:18:06 +00001002 child_set_env(&env, &envsize, "USER", pw->pw_name);
1003 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001004#ifdef _AIX
djm@openbsd.org83b58182016-08-19 03:18:06 +00001005 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001006#endif
djm@openbsd.org83b58182016-08-19 03:18:06 +00001007 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001008#ifdef HAVE_LOGIN_CAP
djm@openbsd.org83b58182016-08-19 03:18:06 +00001009 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1010 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1011 else
1012 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001013#else /* HAVE_LOGIN_CAP */
1014# ifndef HAVE_CYGWIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001015 /*
1016 * There's no standard path on Windows. The path contains
1017 * important components pointing to the system directories,
1018 * needed for loading shared libraries. So the path better
1019 * remains intact here.
1020 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001021# ifdef HAVE_ETC_DEFAULT_LOGIN
djm@openbsd.org83b58182016-08-19 03:18:06 +00001022 read_etc_default_login(&env, &envsize, pw->pw_uid);
1023 path = child_get_env(env, "PATH");
Darren Tuckere1a790d2003-09-16 11:52:19 +10001024# endif /* HAVE_ETC_DEFAULT_LOGIN */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001025 if (path == NULL || *path == '\0') {
1026 child_set_env(&env, &envsize, "PATH",
1027 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1028 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001029# endif /* HAVE_CYGWIN */
1030#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001031
djm@openbsd.org83b58182016-08-19 03:18:06 +00001032 snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
1033 child_set_env(&env, &envsize, "MAIL", buf);
Damien Millerb38eff82000-04-01 11:09:21 +10001034
djm@openbsd.org83b58182016-08-19 03:18:06 +00001035 /* Normal systems set SHELL by default. */
1036 child_set_env(&env, &envsize, "SHELL", shell);
1037
Damien Millerb38eff82000-04-01 11:09:21 +10001038 if (getenv("TZ"))
1039 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1040
1041 /* Set custom environment options from RSA authentication. */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001042 while (custom_environment) {
1043 struct envstring *ce = custom_environment;
1044 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001045
djm@openbsd.org83b58182016-08-19 03:18:06 +00001046 for (i = 0; str[i] != '=' && str[i]; i++)
1047 ;
1048 if (str[i] == '=') {
1049 str[i] = 0;
1050 child_set_env(&env, &envsize, str, str + i + 1);
Damien Millerb38eff82000-04-01 11:09:21 +10001051 }
djm@openbsd.org83b58182016-08-19 03:18:06 +00001052 custom_environment = ce->next;
1053 free(ce->s);
1054 free(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001055 }
1056
Damien Millerf37e2462002-09-19 11:47:55 +10001057 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001058 snprintf(buf, sizeof buf, "%.50s %d %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001059 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1060 ssh_local_port(ssh));
Damien Millerb38eff82000-04-01 11:09:21 +10001061 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1062
Damien Miller00111382003-03-10 11:21:17 +11001063 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001064 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
djm@openbsd.org95767262016-03-07 19:02:43 +00001065 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1066 laddr, ssh_local_port(ssh));
Darren Tuckera627d422013-06-02 07:31:17 +10001067 free(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001068 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1069
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001070 if (tun_fwd_ifnames != NULL)
1071 child_set_env(&env, &envsize, "SSH_TUNNEL", tun_fwd_ifnames);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001072 if (auth_info_file != NULL)
1073 child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file);
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001074 if (s->ttyfd != -1)
1075 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1076 if (s->term)
1077 child_set_env(&env, &envsize, "TERM", s->term);
1078 if (s->display)
1079 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001080 if (original_command)
1081 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1082 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001083
Tim Rice81ed5182002-09-25 17:38:46 -07001084#ifdef _UNICOS
1085 if (cray_tmpdir[0] != '\0')
1086 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1087#endif /* _UNICOS */
1088
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001089 /*
1090 * Since we clear KRB5CCNAME at startup, if it's set now then it
1091 * must have been set by a native authentication method (eg AIX or
1092 * SIA), so copy it to the child.
1093 */
1094 {
1095 char *cp;
1096
1097 if ((cp = getenv("KRB5CCNAME")) != NULL)
1098 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1099 }
1100
Damien Millerb38eff82000-04-01 11:09:21 +10001101#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001102 {
1103 char *cp;
1104
1105 if ((cp = getenv("AUTHSTATE")) != NULL)
1106 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001107 read_environment_file(&env, &envsize, "/etc/environment");
1108 }
Damien Millerb38eff82000-04-01 11:09:21 +10001109#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001110#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001111 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001112 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001113 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001114#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001115#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001116 /*
1117 * Pull in any environment variables that may have
1118 * been set by PAM.
1119 */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001120 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001121 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001122
Damien Miller94bc1e72017-07-28 14:50:59 +10001123 /*
1124 * Don't allow SSH_AUTH_INFO variables posted to PAM to leak
1125 * back into the environment.
1126 */
Damien Millerc756e9b2003-11-17 21:41:42 +11001127 p = fetch_pam_child_environment();
Damien Miller94bc1e72017-07-28 14:50:59 +10001128 copy_environment_blacklist(p, &env, &envsize, "SSH_AUTH_INFO*");
Damien Millerc756e9b2003-11-17 21:41:42 +11001129 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001130
Damien Millerc756e9b2003-11-17 21:41:42 +11001131 p = fetch_pam_environment();
Damien Miller94bc1e72017-07-28 14:50:59 +10001132 copy_environment_blacklist(p, &env, &envsize, "SSH_AUTH_INFO*");
Kevin Steves38b050a2002-07-23 00:44:07 +00001133 free_pam_environment(p);
1134 }
Damien Millerb38eff82000-04-01 11:09:21 +10001135#endif /* USE_PAM */
1136
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001137 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001138 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001139 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001140
1141 /* read $HOME/.ssh/environment. */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001142 if (options.permit_user_env) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001143 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001144 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001145 read_environment_file(&env, &envsize, buf);
1146 }
1147 if (debug_flag) {
1148 /* dump the environment */
1149 fprintf(stderr, "Environment:\n");
1150 for (i = 0; env[i]; i++)
1151 fprintf(stderr, " %.200s\n", env[i]);
1152 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001153 return env;
1154}
1155
1156/*
1157 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1158 * first in this order).
1159 */
1160static void
1161do_rc_files(Session *s, const char *shell)
1162{
1163 FILE *f = NULL;
1164 char cmd[1024];
1165 int do_xauth;
1166 struct stat st;
1167
1168 do_xauth =
1169 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1170
Damien Millera1b48cc2008-03-27 11:02:02 +11001171 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001172 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Miller72e6b5c2014-07-04 09:00:04 +10001173 !no_user_rc && options.permit_user_rc &&
1174 stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001175 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1176 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1177 if (debug_flag)
1178 fprintf(stderr, "Running %s\n", cmd);
1179 f = popen(cmd, "w");
1180 if (f) {
1181 if (do_xauth)
1182 fprintf(f, "%s %s\n", s->auth_proto,
1183 s->auth_data);
1184 pclose(f);
1185 } else
1186 fprintf(stderr, "Could not run %s\n",
1187 _PATH_SSH_USER_RC);
1188 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1189 if (debug_flag)
1190 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1191 _PATH_SSH_SYSTEM_RC);
1192 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1193 if (f) {
1194 if (do_xauth)
1195 fprintf(f, "%s %s\n", s->auth_proto,
1196 s->auth_data);
1197 pclose(f);
1198 } else
1199 fprintf(stderr, "Could not run %s\n",
1200 _PATH_SSH_SYSTEM_RC);
1201 } else if (do_xauth && options.xauth_location != NULL) {
1202 /* Add authority data to .Xauthority if appropriate. */
1203 if (debug_flag) {
1204 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001205 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001206 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001207 fprintf(stderr,
1208 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001209 options.xauth_location, s->auth_display,
1210 s->auth_proto, s->auth_data);
1211 }
1212 snprintf(cmd, sizeof cmd, "%s -q -",
1213 options.xauth_location);
1214 f = popen(cmd, "w");
1215 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001216 fprintf(f, "remove %s\n",
1217 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001218 fprintf(f, "add %s %s %s\n",
1219 s->auth_display, s->auth_proto,
1220 s->auth_data);
1221 pclose(f);
1222 } else {
1223 fprintf(stderr, "Could not run %s\n",
1224 cmd);
1225 }
1226 }
1227}
1228
1229static void
1230do_nologin(struct passwd *pw)
1231{
1232 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001233 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1234 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001235
1236#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001237 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001238 return;
1239 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001240#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001241 if (pw->pw_uid == 0)
1242 return;
1243 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001244#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001245 if (stat(nl, &sb) == -1) {
1246 if (nl != def_nl)
Darren Tuckera627d422013-06-02 07:31:17 +10001247 free(nl);
Darren Tucker09aa4c02010-01-12 19:51:48 +11001248 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001249 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001250
1251 /* /etc/nologin exists. Print its contents if we can and exit. */
1252 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1253 if ((f = fopen(nl, "r")) != NULL) {
1254 while (fgets(buf, sizeof(buf), f))
1255 fputs(buf, stderr);
1256 fclose(f);
1257 }
1258 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001259}
1260
Damien Millerd8cb1f12008-02-10 22:40:12 +11001261/*
1262 * Chroot into a directory after checking it for safety: all path components
1263 * must be root-owned directories with strict permissions.
1264 */
1265static void
1266safely_chroot(const char *path, uid_t uid)
1267{
1268 const char *cp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001269 char component[PATH_MAX];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001270 struct stat st;
1271
1272 if (*path != '/')
1273 fatal("chroot path does not begin at root");
1274 if (strlen(path) >= sizeof(component))
1275 fatal("chroot path too long");
1276
1277 /*
1278 * Descend the path, checking that each component is a
1279 * root-owned directory with strict permissions.
1280 */
1281 for (cp = path; cp != NULL;) {
1282 if ((cp = strchr(cp, '/')) == NULL)
1283 strlcpy(component, path, sizeof(component));
1284 else {
1285 cp++;
1286 memcpy(component, path, cp - path);
1287 component[cp - path] = '\0';
1288 }
1289
1290 debug3("%s: checking '%s'", __func__, component);
1291
1292 if (stat(component, &st) != 0)
1293 fatal("%s: stat(\"%s\"): %s", __func__,
1294 component, strerror(errno));
1295 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1296 fatal("bad ownership or modes for chroot "
1297 "directory %s\"%s\"",
1298 cp == NULL ? "" : "component ", component);
1299 if (!S_ISDIR(st.st_mode))
1300 fatal("chroot path %s\"%s\" is not a directory",
1301 cp == NULL ? "" : "component ", component);
1302
1303 }
1304
1305 if (chdir(path) == -1)
1306 fatal("Unable to chdir to chroot path \"%s\": "
1307 "%s", path, strerror(errno));
1308 if (chroot(path) == -1)
1309 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1310 if (chdir("/") == -1)
1311 fatal("%s: chdir(/) after chroot: %s",
1312 __func__, strerror(errno));
1313 verbose("Changed root directory to \"%s\"", path);
1314}
1315
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001316/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001318do_setusercontext(struct passwd *pw)
1319{
Damien Miller54e37732008-02-10 22:48:55 +11001320 char *chroot_path, *tmp;
1321
Darren Tucker97528352010-11-05 12:03:05 +11001322 platform_setusercontext(pw);
1323
Darren Tuckerb12fe272010-11-05 14:47:01 +11001324 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001325#ifdef HAVE_LOGIN_CAP
1326 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001327 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001328 perror("unable to set user context");
1329 exit(1);
1330 }
1331#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001332 if (setlogin(pw->pw_name) < 0)
1333 error("setlogin failed: %s", strerror(errno));
1334 if (setgid(pw->pw_gid) < 0) {
1335 perror("setgid");
1336 exit(1);
1337 }
1338 /* Initialize the group list. */
1339 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1340 perror("initgroups");
1341 exit(1);
1342 }
1343 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001344#endif
1345
Darren Tucker920612e2010-11-05 12:36:15 +11001346 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001347
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001348 if (!in_chroot && options.chroot_directory != NULL &&
Damien Millerd8cb1f12008-02-10 22:40:12 +11001349 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001350 tmp = tilde_expand_filename(options.chroot_directory,
1351 pw->pw_uid);
1352 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1353 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001354 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001355 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001356 free(chroot_path);
Damien Millera56086b2013-04-23 15:24:18 +10001357 /* Make sure we don't attempt to chroot again */
1358 free(options.chroot_directory);
1359 options.chroot_directory = NULL;
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001360 in_chroot = 1;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001361 }
1362
1363#ifdef HAVE_LOGIN_CAP
1364 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1365 perror("unable to set user context (setuser)");
1366 exit(1);
1367 }
Damien Miller58528402013-03-15 11:22:37 +11001368 /*
1369 * FreeBSD's setusercontext() will not apply the user's
1370 * own umask setting unless running with the user's UID.
1371 */
1372 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUMASK);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001373#else
Tim Rice9464ba62014-01-20 17:59:28 -08001374# ifdef USE_LIBIAF
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001375 /*
1376 * In a chroot environment, the set_id() will always fail;
1377 * typically because of the lack of necessary authentication
1378 * services and runtime such as ./usr/lib/libiaf.so,
1379 * ./usr/lib/libpam.so.1, and ./etc/passwd We skip it in the
1380 * internal sftp chroot case. We'll lose auditing and ACLs but
1381 * permanently_set_uid will take care of the rest.
1382 */
1383 if (!in_chroot && set_id(pw->pw_name) != 0)
1384 fatal("set_id(%s) Failed", pw->pw_name);
Tim Rice9464ba62014-01-20 17:59:28 -08001385# endif /* USE_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001386 /* Permanently switch to the desired uid. */
1387 permanently_set_uid(pw);
1388#endif
Damien Millera56086b2013-04-23 15:24:18 +10001389 } else if (options.chroot_directory != NULL &&
1390 strcasecmp(options.chroot_directory, "none") != 0) {
1391 fatal("server lacks privileges to chroot to ChrootDirectory");
Damien Millerf1a02ae2013-04-23 15:22:13 +10001392 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001393
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001394 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1395 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1396}
1397
Ben Lindstrom08105192002-03-22 02:50:06 +00001398static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001399do_pwchange(Session *s)
1400{
Darren Tucker09991742004-07-17 17:05:14 +10001401 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001402 fprintf(stderr, "WARNING: Your password has expired.\n");
1403 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001404 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001405 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001406#ifdef WITH_SELINUX
1407 setexeccon(NULL);
1408#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001409#ifdef PASSWD_NEEDS_USERNAME
1410 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1411 (char *)NULL);
1412#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001413 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001414#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001415 perror("passwd");
1416 } else {
1417 fprintf(stderr,
1418 "Password change required but no TTY available.\n");
1419 }
1420 exit(1);
1421}
1422
1423static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001424child_close_fds(struct ssh *ssh)
Darren Tucker23bc8d02004-02-06 16:24:31 +11001425{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001426 extern int auth_sock;
Damien Miller85b45e02013-07-20 13:21:52 +10001427
djm@openbsd.org141efe42015-01-14 20:05:27 +00001428 if (auth_sock != -1) {
1429 close(auth_sock);
1430 auth_sock = -1;
Damien Miller85b45e02013-07-20 13:21:52 +10001431 }
1432
Darren Tucker23bc8d02004-02-06 16:24:31 +11001433 if (packet_get_connection_in() == packet_get_connection_out())
1434 close(packet_get_connection_in());
1435 else {
1436 close(packet_get_connection_in());
1437 close(packet_get_connection_out());
1438 }
1439 /*
1440 * Close all descriptors related to channels. They will still remain
1441 * open in the parent.
1442 */
1443 /* XXX better use close-on-exec? -markus */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001444 channel_close_all(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001445
1446 /*
1447 * Close any extra file descriptors. Note that there may still be
1448 * descriptors left by system functions. They will be closed later.
1449 */
1450 endpwent();
1451
1452 /*
Damien Miller788f2122005-11-05 15:14:59 +11001453 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001454 * hanging around in clients. Note that we want to do this after
1455 * initgroups, because at least on Solaris 2.3 it leaves file
1456 * descriptors open.
1457 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001458 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001459}
1460
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001461/*
1462 * Performs common processing for the child, such as setting up the
1463 * environment, closing extra file descriptors, setting the user and group
1464 * ids, and executing the command or shell.
1465 */
Damien Millerdfc24252008-02-10 22:29:40 +11001466#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001467void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001468do_child(struct ssh *ssh, Session *s, const char *command)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001469{
1470 extern char **environ;
1471 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001472 char *argv[ARGV_MAX];
djm@openbsd.org83b58182016-08-19 03:18:06 +00001473 const char *shell, *shell0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001474 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001475 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001476
1477 /* remove hostkey from the child's memory */
1478 destroy_sensitive_data();
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001479 packet_clear_keys();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001480
Darren Tucker23bc8d02004-02-06 16:24:31 +11001481 /* Force a password change */
1482 if (s->authctxt->force_pwchange) {
1483 do_setusercontext(pw);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001484 child_close_fds(ssh);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001485 do_pwchange(s);
1486 exit(1);
1487 }
1488
Tim Rice81ed5182002-09-25 17:38:46 -07001489#ifdef _UNICOS
1490 cray_setup(pw->pw_uid, pw->pw_name, command);
1491#endif /* _UNICOS */
1492
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001493 /*
1494 * Login(1) does this as well, and it needs uid 0 for the "-h"
1495 * switch, so we let login(1) to this for us.
1496 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001497#ifdef HAVE_OSF_SIA
djm@openbsd.org83b58182016-08-19 03:18:06 +00001498 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1499 if (!check_quietlogin(s, command))
1500 do_motd();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001501#else /* HAVE_OSF_SIA */
djm@openbsd.org83b58182016-08-19 03:18:06 +00001502 /* When PAM is enabled we rely on it to do the nologin check */
1503 if (!options.use_pam)
1504 do_nologin(pw);
1505 do_setusercontext(pw);
1506 /*
1507 * PAM session modules in do_setusercontext may have
1508 * generated messages, so if this in an interactive
1509 * login then display them too.
1510 */
1511 if (!check_quietlogin(s, command))
1512 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001513#endif /* HAVE_OSF_SIA */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001514
Darren Tucker69687f42004-09-11 22:17:26 +10001515#ifdef USE_PAM
djm@openbsd.org83b58182016-08-19 03:18:06 +00001516 if (options.use_pam && !is_pam_session_open()) {
Darren Tucker48554152005-04-21 19:50:55 +10001517 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001518 display_loginmsg();
1519 exit(254);
1520 }
1521#endif
1522
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001523 /*
1524 * Get the shell from the password data. An empty shell field is
1525 * legal, and means /bin/sh.
1526 */
1527 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001528
1529 /*
1530 * Make sure $SHELL points to the shell from the password file,
1531 * even if shell is overridden from login.conf
1532 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001533 env = do_setup_env(ssh, s, shell);
Ben Lindstrom46767602002-12-23 02:26:08 +00001534
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001535#ifdef HAVE_LOGIN_CAP
1536 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1537#endif
1538
Damien Millerb38eff82000-04-01 11:09:21 +10001539 /*
1540 * Close the connection descriptors; note that this is the child, and
1541 * the server will still have the socket open, and it is important
1542 * that we do not shutdown it. Note that the descriptors cannot be
1543 * closed before building the environment, as we call
djm@openbsd.org95767262016-03-07 19:02:43 +00001544 * ssh_remote_ipaddr there.
Damien Millerb38eff82000-04-01 11:09:21 +10001545 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001546 child_close_fds(ssh);
Damien Millerb38eff82000-04-01 11:09:21 +10001547
Damien Millerb38eff82000-04-01 11:09:21 +10001548 /*
Damien Miller05eda432002-02-10 18:32:28 +11001549 * Must take new environment into use so that .ssh/rc,
1550 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001551 */
1552 environ = env;
1553
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001554#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001555 /*
1556 * At this point, we check to see if AFS is active and if we have
1557 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1558 * if we can (and need to) extend the ticket into an AFS token. If
1559 * we don't do this, we run into potential problems if the user's
1560 * home directory is in AFS and it's not world-readable.
1561 */
1562
1563 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001564 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001565 char cell[64];
1566
1567 debug("Getting AFS token");
1568
1569 k_setpag();
1570
1571 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1572 krb5_afslog(s->authctxt->krb5_ctx,
1573 s->authctxt->krb5_fwd_ccache, cell, NULL);
1574
1575 krb5_afslog_home(s->authctxt->krb5_ctx,
1576 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1577 }
1578#endif
1579
Damien Miller788f2122005-11-05 15:14:59 +11001580 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001581 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001582 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001583#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001584 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001585#endif
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001586 if (r || !in_chroot) {
Damien Miller6051c942008-06-16 07:53:16 +10001587 fprintf(stderr, "Could not chdir to home "
1588 "directory %s: %s\n", pw->pw_dir,
1589 strerror(errno));
djm@openbsd.org44fc7cd2015-10-24 22:52:22 +00001590 }
Damien Miller6051c942008-06-16 07:53:16 +10001591 if (r)
1592 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001593 }
1594
Damien Millera1939002008-03-15 17:27:58 +11001595 closefrom(STDERR_FILENO + 1);
1596
djm@openbsd.org83b58182016-08-19 03:18:06 +00001597 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001598
1599 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001600 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001601
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001602 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1603 printf("This service allows sftp connections only.\n");
1604 fflush(NULL);
1605 exit(1);
1606 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001607 extern int optind, optreset;
1608 int i;
1609 char *p, *args;
1610
Darren Tuckerac46a912009-06-21 17:55:23 +10001611 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001612 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001613 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1614 if (i < ARGV_MAX - 1)
1615 argv[i++] = p;
1616 argv[i] = NULL;
1617 optind = optreset = 1;
1618 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001619#ifdef WITH_SELINUX
1620 ssh_selinux_change_context("sftpd_t");
1621#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001622 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001623 }
1624
Darren Tucker695ed392009-10-07 09:02:18 +11001625 fflush(NULL);
1626
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001627 /* Get the last component of the shell name. */
1628 if ((shell0 = strrchr(shell, '/')) != NULL)
1629 shell0++;
1630 else
1631 shell0 = shell;
1632
Damien Millerb38eff82000-04-01 11:09:21 +10001633 /*
1634 * If we have no command, execute the shell. In this case, the shell
1635 * name to be passed in argv[0] is preceded by '-' to indicate that
1636 * this is a login shell.
1637 */
1638 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001639 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001640
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001641 /* Start the shell. Set initial character to '-'. */
1642 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001643
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001644 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1645 >= sizeof(argv0) - 1) {
1646 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001647 perror(shell);
1648 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001649 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001650
1651 /* Execute the shell. */
1652 argv[0] = argv0;
1653 argv[1] = NULL;
1654 execve(shell, argv, env);
1655
1656 /* Executing the shell failed. */
1657 perror(shell);
1658 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001659 }
1660 /*
1661 * Execute the command using the user's shell. This uses the -c
1662 * option to execute the command.
1663 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001664 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001665 argv[1] = "-c";
1666 argv[2] = (char *) command;
1667 argv[3] = NULL;
1668 execve(shell, argv, env);
1669 perror(shell);
1670 exit(1);
1671}
1672
Damien Miller7207f642008-05-19 15:34:50 +10001673void
1674session_unused(int id)
1675{
1676 debug3("%s: session id %d unused", __func__, id);
1677 if (id >= options.max_sessions ||
1678 id >= sessions_nalloc) {
1679 fatal("%s: insane session id %d (max %d nalloc %d)",
1680 __func__, id, options.max_sessions, sessions_nalloc);
1681 }
Damien Miller1d2c4562014-02-04 11:18:20 +11001682 memset(&sessions[id], 0, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001683 sessions[id].self = id;
1684 sessions[id].used = 0;
1685 sessions[id].chanid = -1;
1686 sessions[id].ptyfd = -1;
1687 sessions[id].ttyfd = -1;
1688 sessions[id].ptymaster = -1;
1689 sessions[id].x11_chanids = NULL;
1690 sessions[id].next_unused = sessions_first_unused;
1691 sessions_first_unused = id;
1692}
1693
Damien Millerb38eff82000-04-01 11:09:21 +10001694Session *
1695session_new(void)
1696{
Damien Miller7207f642008-05-19 15:34:50 +10001697 Session *s, *tmp;
1698
1699 if (sessions_first_unused == -1) {
1700 if (sessions_nalloc >= options.max_sessions)
1701 return NULL;
1702 debug2("%s: allocate (allocated %d max %d)",
1703 __func__, sessions_nalloc, options.max_sessions);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001704 tmp = xrecallocarray(sessions, sessions_nalloc,
1705 sessions_nalloc + 1, sizeof(*sessions));
Damien Miller7207f642008-05-19 15:34:50 +10001706 if (tmp == NULL) {
1707 error("%s: cannot allocate %d sessions",
1708 __func__, sessions_nalloc + 1);
1709 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001710 }
Damien Miller7207f642008-05-19 15:34:50 +10001711 sessions = tmp;
1712 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001713 }
Damien Miller7207f642008-05-19 15:34:50 +10001714
1715 if (sessions_first_unused >= sessions_nalloc ||
1716 sessions_first_unused < 0) {
1717 fatal("%s: insane first_unused %d max %d nalloc %d",
1718 __func__, sessions_first_unused, options.max_sessions,
1719 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001720 }
Damien Miller7207f642008-05-19 15:34:50 +10001721
1722 s = &sessions[sessions_first_unused];
1723 if (s->used) {
1724 fatal("%s: session %d already used",
1725 __func__, sessions_first_unused);
1726 }
1727 sessions_first_unused = s->next_unused;
1728 s->used = 1;
1729 s->next_unused = -1;
1730 debug("session_new: session %d", s->self);
1731
1732 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001733}
1734
Ben Lindstrombba81212001-06-25 05:01:22 +00001735static void
Damien Millerb38eff82000-04-01 11:09:21 +10001736session_dump(void)
1737{
1738 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001739 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001740 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001741
1742 debug("dump: used %d next_unused %d session %d %p "
1743 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001744 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001745 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001746 s->self,
1747 s,
1748 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001749 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001750 }
1751}
1752
Damien Millerefb4afe2000-04-12 18:45:05 +10001753int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001754session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001755{
1756 Session *s = session_new();
1757 debug("session_open: channel %d", chanid);
1758 if (s == NULL) {
1759 error("no more sessions");
1760 return 0;
1761 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001762 s->authctxt = authctxt;
1763 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001764 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001765 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001766 debug("session_open: session %d: link with channel %d", s->self, chanid);
1767 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001768 return 1;
1769}
1770
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771Session *
1772session_by_tty(char *tty)
1773{
1774 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001775 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776 Session *s = &sessions[i];
1777 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1778 debug("session_by_tty: session %d tty %s", i, tty);
1779 return s;
1780 }
1781 }
1782 debug("session_by_tty: unknown tty %.100s", tty);
1783 session_dump();
1784 return NULL;
1785}
1786
Ben Lindstrombba81212001-06-25 05:01:22 +00001787static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001788session_by_channel(int id)
1789{
1790 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001791 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001792 Session *s = &sessions[i];
1793 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001794 debug("session_by_channel: session %d channel %d",
1795 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001796 return s;
1797 }
1798 }
1799 debug("session_by_channel: unknown channel %d", id);
1800 session_dump();
1801 return NULL;
1802}
1803
Ben Lindstrombba81212001-06-25 05:01:22 +00001804static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001805session_by_x11_channel(int id)
1806{
1807 int i, j;
1808
Damien Miller7207f642008-05-19 15:34:50 +10001809 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001810 Session *s = &sessions[i];
1811
1812 if (s->x11_chanids == NULL || !s->used)
1813 continue;
1814 for (j = 0; s->x11_chanids[j] != -1; j++) {
1815 if (s->x11_chanids[j] == id) {
1816 debug("session_by_x11_channel: session %d "
1817 "channel %d", s->self, id);
1818 return s;
1819 }
1820 }
1821 }
1822 debug("session_by_x11_channel: unknown channel %d", id);
1823 session_dump();
1824 return NULL;
1825}
1826
1827static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001828session_by_pid(pid_t pid)
1829{
1830 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001831 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001832 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001833 Session *s = &sessions[i];
1834 if (s->used && s->pid == pid)
1835 return s;
1836 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001837 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001838 session_dump();
1839 return NULL;
1840}
1841
Ben Lindstrombba81212001-06-25 05:01:22 +00001842static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001843session_window_change_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001844{
1845 s->col = packet_get_int();
1846 s->row = packet_get_int();
1847 s->xpixel = packet_get_int();
1848 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001849 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001850 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1851 return 1;
1852}
1853
Ben Lindstrombba81212001-06-25 05:01:22 +00001854static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001855session_pty_req(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001856{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001857 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001858 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001859
Damien Miller5ff30c62013-10-30 22:21:50 +11001860 if (no_pty_flag || !options.permit_tty) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001861 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001862 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001863 }
1864 if (s->ttyfd != -1) {
1865 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001866 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001867 }
1868
Damien Millerefb4afe2000-04-12 18:45:05 +10001869 s->term = packet_get_string(&len);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001870 s->col = packet_get_int();
1871 s->row = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +10001872 s->xpixel = packet_get_int();
1873 s->ypixel = packet_get_int();
1874
1875 if (strcmp(s->term, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001876 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001877 s->term = NULL;
1878 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001879
Damien Millerefb4afe2000-04-12 18:45:05 +10001880 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001881 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10001882 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1883 sizeof(s->tty)))) {
Darren Tuckera627d422013-06-02 07:31:17 +10001884 free(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001885 s->term = NULL;
1886 s->ptyfd = -1;
1887 s->ttyfd = -1;
1888 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001889 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001890 }
1891 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001892
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001893 n_bytes = packet_remaining();
Ben Lindstrom49c12602001-06-13 04:37:36 +00001894 tty_parse_modes(s->ttyfd, &n_bytes);
1895
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001896 if (!use_privsep)
1897 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001898
1899 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001900 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1901
Damien Miller48b03fc2002-01-22 23:11:40 +11001902 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001903 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001904 return 1;
1905}
1906
Ben Lindstrombba81212001-06-25 05:01:22 +00001907static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001908session_subsystem_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001909{
Damien Millerae452462001-10-10 15:08:06 +10001910 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001911 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001912 int success = 0;
Damien Miller71df7522013-10-15 12:12:02 +11001913 char *prog, *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +10001914 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001915
Damien Miller71df7522013-10-15 12:12:02 +11001916 s->subsys = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001917 packet_check_eom();
Damien Miller71df7522013-10-15 12:12:02 +11001918 debug2("subsystem request for %.100s by user %s", s->subsys,
Damien Miller1b2b61e2010-06-26 09:47:43 +10001919 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10001920
Damien Millerf6d9e222000-06-18 14:50:44 +10001921 for (i = 0; i < options.num_subsystems; i++) {
Damien Miller71df7522013-10-15 12:12:02 +11001922 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001923 prog = options.subsystem_command[i];
1924 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001925 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11001926 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001927 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11001928 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001929 if (stat(prog, &st) < 0)
1930 debug("subsystem: cannot stat %s: %s",
1931 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001932 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11001933 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001934 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001935 success = do_exec(ssh, s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11001936 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001937 }
1938 }
1939
1940 if (!success)
Damien Miller71df7522013-10-15 12:12:02 +11001941 logit("subsystem request for %.100s by user %s failed, "
1942 "subsystem not found", s->subsys, s->pw->pw_name);
Damien Millerf6d9e222000-06-18 14:50:44 +10001943
Damien Millerbd483e72000-04-30 10:00:53 +10001944 return success;
1945}
1946
Ben Lindstrombba81212001-06-25 05:01:22 +00001947static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001948session_x11_req(struct ssh *ssh, Session *s)
Damien Millerbd483e72000-04-30 10:00:53 +10001949{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001950 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001951
Damien Miller2b9b0452005-07-17 17:19:24 +10001952 if (s->auth_proto != NULL || s->auth_data != NULL) {
1953 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11001954 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10001955 return 0;
1956 }
Damien Millerbd483e72000-04-30 10:00:53 +10001957 s->single_connection = packet_get_char();
1958 s->auth_proto = packet_get_string(NULL);
1959 s->auth_data = packet_get_string(NULL);
1960 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001961 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001962
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00001963 if (xauth_valid_string(s->auth_proto) &&
1964 xauth_valid_string(s->auth_data))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001965 success = session_setup_x11fwd(ssh, s);
djm@openbsd.org4b4bfb02016-03-10 11:47:57 +00001966 else {
1967 success = 0;
1968 error("Invalid X11 forwarding data");
1969 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001970 if (!success) {
Darren Tuckera627d422013-06-02 07:31:17 +10001971 free(s->auth_proto);
1972 free(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001973 s->auth_proto = NULL;
1974 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001975 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001976 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001977}
1978
Ben Lindstrombba81212001-06-25 05:01:22 +00001979static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001980session_shell_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10001981{
Damien Miller48b03fc2002-01-22 23:11:40 +11001982 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001983 return do_exec(ssh, s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10001984}
1985
Ben Lindstrombba81212001-06-25 05:01:22 +00001986static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001987session_exec_req(struct ssh *ssh, Session *s)
Damien Millerf6d9e222000-06-18 14:50:44 +10001988{
Damien Miller7207f642008-05-19 15:34:50 +10001989 u_int len, success;
1990
Damien Millerf6d9e222000-06-18 14:50:44 +10001991 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001992 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001993 success = do_exec(ssh, s, command) == 0;
Darren Tuckera627d422013-06-02 07:31:17 +10001994 free(command);
Damien Miller7207f642008-05-19 15:34:50 +10001995 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10001996}
1997
Ben Lindstrombba81212001-06-25 05:01:22 +00001998static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001999session_break_req(struct ssh *ssh, Session *s)
Damien Miller54c45982003-05-15 10:20:13 +10002000{
Damien Miller54c45982003-05-15 10:20:13 +10002001
Darren Tucker1f8311c2004-05-13 16:39:33 +10002002 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002003 packet_check_eom();
2004
Darren Tucker9c5d5532011-11-04 10:55:24 +11002005 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002006 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002007 return 1;
2008}
2009
2010static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002011session_env_req(struct ssh *ssh, Session *s)
Darren Tucker46bc0752004-05-02 22:11:30 +10002012{
2013 char *name, *val;
2014 u_int name_len, val_len, i;
2015
Damien Miller8569eba2014-03-04 09:35:17 +11002016 name = packet_get_cstring(&name_len);
2017 val = packet_get_cstring(&val_len);
Darren Tucker46bc0752004-05-02 22:11:30 +10002018 packet_check_eom();
2019
2020 /* Don't set too many environment variables */
2021 if (s->num_env > 128) {
2022 debug2("Ignoring env request %s: too many env vars", name);
2023 goto fail;
2024 }
2025
2026 for (i = 0; i < options.num_accept_env; i++) {
2027 if (match_pattern(name, options.accept_env[i])) {
2028 debug2("Setting env %d: %s=%s", s->num_env, name, val);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002029 s->env = xrecallocarray(s->env, s->num_env,
2030 s->num_env + 1, sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002031 s->env[s->num_env].name = name;
2032 s->env[s->num_env].val = val;
2033 s->num_env++;
2034 return (1);
2035 }
2036 }
2037 debug2("Ignoring env request %s: disallowed name", name);
2038
2039 fail:
Darren Tuckera627d422013-06-02 07:31:17 +10002040 free(name);
2041 free(val);
Darren Tucker46bc0752004-05-02 22:11:30 +10002042 return (0);
2043}
2044
2045static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002046session_auth_agent_req(struct ssh *ssh, Session *s)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002047{
2048 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002049 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002050 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002051 debug("session_auth_agent_req: no_agent_forwarding_flag");
2052 return 0;
2053 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002054 if (called) {
2055 return 0;
2056 } else {
2057 called = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002058 return auth_input_request_forwarding(ssh, s->pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002059 }
2060}
2061
Damien Millerc7ef63d2002-02-05 12:21:42 +11002062int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002063session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002064{
Damien Millerefb4afe2000-04-12 18:45:05 +10002065 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002066 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002067
Damien Millerc7ef63d2002-02-05 12:21:42 +11002068 if ((s = session_by_channel(c->self)) == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002069 logit("%s: no session %d req %.100s", __func__, c->self, rtype);
Damien Millerc7ef63d2002-02-05 12:21:42 +11002070 return 0;
2071 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002072 debug("%s: session %d req %s", __func__, s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002073
2074 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002075 * a session is in LARVAL state until a shell, a command
2076 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002077 */
2078 if (c->type == SSH_CHANNEL_LARVAL) {
2079 if (strcmp(rtype, "shell") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002080 success = session_shell_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002081 } else if (strcmp(rtype, "exec") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002082 success = session_exec_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002083 } else if (strcmp(rtype, "pty-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002084 success = session_pty_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002085 } else if (strcmp(rtype, "x11-req") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002086 success = session_x11_req(ssh, s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002087 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002088 success = session_auth_agent_req(ssh, s);
Damien Millerbd483e72000-04-30 10:00:53 +10002089 } else if (strcmp(rtype, "subsystem") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002090 success = session_subsystem_req(ssh, s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002091 } else if (strcmp(rtype, "env") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002092 success = session_env_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002093 }
2094 }
2095 if (strcmp(rtype, "window-change") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002096 success = session_window_change_req(ssh, s);
Damien Millera6b1d162004-06-30 22:41:07 +10002097 } else if (strcmp(rtype, "break") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002098 success = session_break_req(ssh, s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002099 }
Damien Millera6b1d162004-06-30 22:41:07 +10002100
Damien Millerc7ef63d2002-02-05 12:21:42 +11002101 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002102}
2103
2104void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002105session_set_fds(struct ssh *ssh, Session *s,
2106 int fdin, int fdout, int fderr, int ignore_fderr, int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002107{
Damien Millerefb4afe2000-04-12 18:45:05 +10002108 /*
2109 * now that have a child and a pipe to the child,
2110 * we can activate our channel and register the fd's
2111 */
2112 if (s->chanid == -1)
2113 fatal("no channel for session %d", s->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002114 channel_set_fds(ssh, s->chanid,
Damien Millerefb4afe2000-04-12 18:45:05 +10002115 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002116 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002117 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002118}
2119
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002120/*
2121 * Function to perform pty cleanup. Also called if we get aborted abnormally
2122 * (e.g., due to a dropped connection).
2123 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002124void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002125session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002126{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002127 if (s == NULL) {
2128 error("session_pty_cleanup: no session");
2129 return;
2130 }
2131 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002132 return;
2133
Kevin Steves43cdef32001-02-11 14:12:08 +00002134 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002135
Damien Millerb38eff82000-04-01 11:09:21 +10002136 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002137 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002138 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002139
2140 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002141 if (getuid() == 0)
2142 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002143
2144 /*
2145 * Close the server side of the socket pairs. We must do this after
2146 * the pty cleanup, so that another process doesn't get this pty
2147 * while we're still cleaning up.
2148 */
Damien Miller7207f642008-05-19 15:34:50 +10002149 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2150 error("close(s->ptymaster/%d): %s",
2151 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002152
2153 /* unlink pty from session */
2154 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002155}
Damien Millerefb4afe2000-04-12 18:45:05 +10002156
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002157void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002158session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002159{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002160 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002161}
2162
Damien Miller5a80bba2002-09-04 16:39:02 +10002163static char *
2164sig2name(int sig)
2165{
2166#define SSH_SIG(x) if (sig == SIG ## x) return #x
2167 SSH_SIG(ABRT);
2168 SSH_SIG(ALRM);
2169 SSH_SIG(FPE);
2170 SSH_SIG(HUP);
2171 SSH_SIG(ILL);
2172 SSH_SIG(INT);
2173 SSH_SIG(KILL);
2174 SSH_SIG(PIPE);
2175 SSH_SIG(QUIT);
2176 SSH_SIG(SEGV);
2177 SSH_SIG(TERM);
2178 SSH_SIG(USR1);
2179 SSH_SIG(USR2);
2180#undef SSH_SIG
2181 return "SIG@openssh.com";
2182}
2183
Ben Lindstrombba81212001-06-25 05:01:22 +00002184static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002185session_close_x11(struct ssh *ssh, int id)
Damien Miller2b9b0452005-07-17 17:19:24 +10002186{
2187 Channel *c;
2188
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002189 if ((c = channel_by_id(ssh, id)) == NULL) {
2190 debug("%s: x11 channel %d missing", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002191 } else {
2192 /* Detach X11 listener */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002193 debug("%s: detach x11 channel %d", __func__, id);
2194 channel_cancel_cleanup(ssh, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002195 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002196 chan_mark_dead(ssh, c);
Damien Miller2b9b0452005-07-17 17:19:24 +10002197 }
2198}
2199
2200static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002201session_close_single_x11(struct ssh *ssh, int id, void *arg)
Damien Miller2b9b0452005-07-17 17:19:24 +10002202{
2203 Session *s;
2204 u_int i;
2205
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002206 debug3("%s: channel %d", __func__, id);
2207 channel_cancel_cleanup(ssh, id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002208 if ((s = session_by_x11_channel(id)) == NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002209 fatal("%s: no x11 channel %d", __func__, id);
Damien Miller2b9b0452005-07-17 17:19:24 +10002210 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002211 debug("%s: session %d: closing channel %d",
2212 __func__, s->self, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002213 /*
2214 * The channel "id" is already closing, but make sure we
2215 * close all of its siblings.
2216 */
2217 if (s->x11_chanids[i] != id)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002218 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller2b9b0452005-07-17 17:19:24 +10002219 }
Darren Tuckera627d422013-06-02 07:31:17 +10002220 free(s->x11_chanids);
Damien Miller2b9b0452005-07-17 17:19:24 +10002221 s->x11_chanids = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +10002222 free(s->display);
2223 s->display = NULL;
2224 free(s->auth_proto);
2225 s->auth_proto = NULL;
2226 free(s->auth_data);
2227 s->auth_data = NULL;
2228 free(s->auth_display);
2229 s->auth_display = NULL;
Damien Miller2b9b0452005-07-17 17:19:24 +10002230}
2231
2232static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002233session_exit_message(struct ssh *ssh, Session *s, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002234{
2235 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002236
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002237 if ((c = channel_lookup(ssh, s->chanid)) == NULL)
2238 fatal("%s: session %d: no channel %d",
2239 __func__, s->self, s->chanid);
2240 debug("%s: session %d channel %d pid %ld",
2241 __func__, s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002242
2243 if (WIFEXITED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002244 channel_request_start(ssh, s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002245 packet_put_int(WEXITSTATUS(status));
2246 packet_send();
2247 } else if (WIFSIGNALED(status)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002248 channel_request_start(ssh, s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002249 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002250#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002251 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002252#else /* WCOREDUMP */
2253 packet_put_char(0);
2254#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002255 packet_put_cstring("");
2256 packet_put_cstring("");
2257 packet_send();
2258 } else {
2259 /* Some weird exit cause. Just exit. */
2260 packet_disconnect("wait returned status %04x.", status);
2261 }
2262
2263 /* disconnect channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002264 debug("%s: release channel %d", __func__, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002265
2266 /*
2267 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002268 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002269 * by session_close_by_channel when the childs close their fds.
2270 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002271 channel_register_cleanup(ssh, c->self, session_close_by_channel, 1);
Damien Miller39eda6e2005-11-05 14:52:50 +11002272
Damien Miller166fca82000-04-20 07:42:21 +10002273 /*
2274 * emulate a write failure with 'chan_write_failed', nobody will be
2275 * interested in data we write.
2276 * Note that we must not call 'chan_read_failed', since there could
2277 * be some more data waiting in the pipe.
2278 */
Damien Millerbd483e72000-04-30 10:00:53 +10002279 if (c->ostate != CHAN_OUTPUT_CLOSED)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002280 chan_write_failed(ssh, c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002281}
2282
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002283void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002284session_close(struct ssh *ssh, Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002285{
Damien Millereccb9de2005-06-17 12:59:34 +10002286 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002287
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002288 verbose("Close session: user %s from %.200s port %d id %d",
2289 s->pw->pw_name,
djm@openbsd.org95767262016-03-07 19:02:43 +00002290 ssh_remote_ipaddr(ssh),
2291 ssh_remote_port(ssh),
djm@openbsd.org5a0fcb72016-02-16 03:37:48 +00002292 s->self);
2293
Darren Tucker3e33cec2003-10-02 16:12:36 +10002294 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002295 session_pty_cleanup(s);
Darren Tuckera627d422013-06-02 07:31:17 +10002296 free(s->term);
2297 free(s->display);
2298 free(s->x11_chanids);
2299 free(s->auth_display);
2300 free(s->auth_data);
2301 free(s->auth_proto);
Damien Miller71df7522013-10-15 12:12:02 +11002302 free(s->subsys);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002303 if (s->env != NULL) {
2304 for (i = 0; i < s->num_env; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10002305 free(s->env[i].name);
2306 free(s->env[i].val);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002307 }
Darren Tuckera627d422013-06-02 07:31:17 +10002308 free(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002309 }
Damien Millere247cc42000-05-07 12:03:14 +10002310 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002311 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002312}
2313
2314void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002315session_close_by_pid(struct ssh *ssh, pid_t pid, int status)
Damien Millerefb4afe2000-04-12 18:45:05 +10002316{
2317 Session *s = session_by_pid(pid);
2318 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002319 debug("%s: no session for pid %ld", __func__, (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002320 return;
2321 }
2322 if (s->chanid != -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002323 session_exit_message(ssh, s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002324 if (s->ttyfd != -1)
2325 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002326 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002327}
2328
2329/*
2330 * this is called when a channel dies before
2331 * the session 'child' itself dies
2332 */
2333void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002334session_close_by_channel(struct ssh *ssh, int id, void *arg)
Damien Millerefb4afe2000-04-12 18:45:05 +10002335{
2336 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002337 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002338
Damien Millerefb4afe2000-04-12 18:45:05 +10002339 if (s == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002340 debug("%s: no session for id %d", __func__, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002341 return;
2342 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002343 debug("%s: channel %d child %ld", __func__, id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002344 if (s->pid != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002345 debug("%s: channel %d: has child", __func__, id);
Damien Miller0585d512001-10-12 11:35:50 +10002346 /*
2347 * delay detach of session, but release pty, since
2348 * the fd's to the child are already closed
2349 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002350 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002351 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002352 return;
2353 }
2354 /* detach by removing callback */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002355 channel_cancel_cleanup(ssh, s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002356
2357 /* Close any X11 listeners associated with this session */
2358 if (s->x11_chanids != NULL) {
2359 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002360 session_close_x11(ssh, s->x11_chanids[i]);
Damien Miller39eda6e2005-11-05 14:52:50 +11002361 s->x11_chanids[i] = -1;
2362 }
2363 }
2364
Damien Millerefb4afe2000-04-12 18:45:05 +10002365 s->chanid = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002366 session_close(ssh, s);
Damien Miller52b77be2001-10-10 15:14:37 +10002367}
2368
2369void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002370session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002371{
2372 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002373 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002374 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002375 if (s->used) {
2376 if (closefunc != NULL)
2377 closefunc(s);
2378 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002379 session_close(ssh, s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002380 }
Damien Miller52b77be2001-10-10 15:14:37 +10002381 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002382}
2383
Ben Lindstrombba81212001-06-25 05:01:22 +00002384static char *
Damien Millere247cc42000-05-07 12:03:14 +10002385session_tty_list(void)
2386{
2387 static char buf[1024];
2388 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002389 char *cp;
2390
Damien Millere247cc42000-05-07 12:03:14 +10002391 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002392 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002393 Session *s = &sessions[i];
2394 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002395
Damien Millera8ed44b2003-01-10 09:53:12 +11002396 if (strncmp(s->tty, "/dev/", 5) != 0) {
2397 cp = strrchr(s->tty, '/');
2398 cp = (cp == NULL) ? s->tty : cp + 1;
2399 } else
2400 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002401
Damien Millere247cc42000-05-07 12:03:14 +10002402 if (buf[0] != '\0')
2403 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002404 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002405 }
2406 }
2407 if (buf[0] == '\0')
2408 strlcpy(buf, "notty", sizeof buf);
2409 return buf;
2410}
2411
2412void
2413session_proctitle(Session *s)
2414{
2415 if (s->pw == NULL)
2416 error("no user for session %d", s->self);
2417 else
2418 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2419}
2420
Ben Lindstrom768176b2001-06-09 01:29:12 +00002421int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002422session_setup_x11fwd(struct ssh *ssh, Session *s)
Ben Lindstrom768176b2001-06-09 01:29:12 +00002423{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002424 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002425 char display[512], auth_display[512];
Damien Millere5c0d522014-07-03 21:24:19 +10002426 char hostname[NI_MAXHOST];
Damien Miller2b9b0452005-07-17 17:19:24 +10002427 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002428
2429 if (no_x11_forwarding_flag) {
2430 packet_send_debug("X11 forwarding disabled in user configuration file.");
2431 return 0;
2432 }
2433 if (!options.x11_forwarding) {
2434 debug("X11 forwarding disabled in server configuration file.");
2435 return 0;
2436 }
djm@openbsd.org161cf412014-12-22 07:55:51 +00002437 if (options.xauth_location == NULL ||
Ben Lindstrom768176b2001-06-09 01:29:12 +00002438 (stat(options.xauth_location, &st) == -1)) {
2439 packet_send_debug("No xauth program; cannot forward with spoofing.");
2440 return 0;
2441 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002442 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002443 debug("X11 display already set.");
2444 return 0;
2445 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002446 if (x11_create_display_inet(ssh, options.x11_display_offset,
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002447 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002448 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002449 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002450 return 0;
2451 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002452 for (i = 0; s->x11_chanids[i] != -1; i++) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002453 channel_register_cleanup(ssh, s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002454 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002455 }
Kevin Steves366298c2001-12-19 17:58:01 +00002456
2457 /* Set up a suitable value for the DISPLAY variable. */
2458 if (gethostname(hostname, sizeof(hostname)) < 0)
2459 fatal("gethostname: %.100s", strerror(errno));
2460 /*
2461 * auth_display must be used as the displayname when the
2462 * authorization entry is added with xauth(1). This will be
2463 * different than the DISPLAY string for localhost displays.
2464 */
Damien Miller95c249f2002-02-05 12:11:34 +11002465 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002466 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002467 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002468 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002469 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002470 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002471 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002472 } else {
2473#ifdef IPADDR_IN_DISPLAY
2474 struct hostent *he;
2475 struct in_addr my_addr;
2476
2477 he = gethostbyname(hostname);
2478 if (he == NULL) {
2479 error("Can't get IP address for X11 DISPLAY.");
2480 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2481 return 0;
2482 }
2483 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002484 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002485 s->display_number, s->screen);
2486#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002487 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002488 s->display_number, s->screen);
2489#endif
2490 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002491 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002492 }
2493
Ben Lindstrom768176b2001-06-09 01:29:12 +00002494 return 1;
2495}
2496
Ben Lindstrombba81212001-06-25 05:01:22 +00002497static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002498do_authenticated2(struct ssh *ssh, Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002499{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002500 server_loop2(ssh, authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002501}
2502
2503void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002504do_cleanup(struct ssh *ssh, Authctxt *authctxt)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002505{
2506 static int called = 0;
2507
2508 debug("do_cleanup");
2509
2510 /* no cleanup if we're in the child for login shell */
2511 if (is_child)
2512 return;
2513
2514 /* avoid double cleanup */
2515 if (called)
2516 return;
2517 called = 1;
2518
Darren Tucker9142e1c2007-08-16 23:28:04 +10002519 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002520 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002521
2522#ifdef USE_PAM
2523 if (options.use_pam) {
2524 sshpam_cleanup();
2525 sshpam_thread_cleanup();
2526 }
2527#endif
2528
2529 if (!authctxt->authenticated)
2530 return;
2531
Darren Tucker3e33cec2003-10-02 16:12:36 +10002532#ifdef KRB5
2533 if (options.kerberos_ticket_cleanup &&
2534 authctxt->krb5_ctx)
2535 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002536#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002537
2538#ifdef GSSAPI
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002539 if (options.gss_cleanup_creds)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002540 ssh_gssapi_cleanup_creds();
2541#endif
2542
2543 /* remove agent socket */
2544 auth_sock_cleanup_proc(authctxt->pw);
2545
djm@openbsd.org8f574952017-06-24 06:34:38 +00002546 /* remove userauth info */
2547 if (auth_info_file != NULL) {
2548 temporarily_use_uid(authctxt->pw);
2549 unlink(auth_info_file);
2550 restore_uid();
2551 free(auth_info_file);
2552 auth_info_file = NULL;
2553 }
2554
Darren Tucker3e33cec2003-10-02 16:12:36 +10002555 /*
2556 * Cleanup ptys/utmp only if privsep is disabled,
2557 * or if running in monitor.
2558 */
2559 if (!use_privsep || mm_is_monitor())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002560 session_destroy_all(ssh, session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002561}
djm@openbsd.org95767262016-03-07 19:02:43 +00002562
2563/* Return a name for the remote host that fits inside utmp_size */
2564
2565const char *
2566session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2567{
2568 const char *remote = "";
2569
2570 if (utmp_size > 0)
2571 remote = auth_get_canonical_hostname(ssh, use_dns);
2572 if (utmp_size == 0 || strlen(remote) > utmp_size)
2573 remote = ssh_remote_ipaddr(ssh);
2574 return remote;
2575}
2576