blob: 65bf2877618cf25f62ec958ba6301b7a1ca49ec9 [file] [log] [blame]
Damien Miller29cd1882012-04-22 11:08:10 +10001/* $OpenBSD: session.c,v 1.260 2012/03/15 03:10:27 guenther 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
Darren Tucker39972492006-07-12 22:22:46 +100049#include <errno.h>
Damien Miller22a29882010-05-10 11:53:54 +100050#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100051#include <grp.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110052#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110053#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100055#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110056#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100057#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100058#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100059#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100060#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100061#include <unistd.h>
Damien Millerb38eff82000-04-01 11:09:21 +100062
Damien Millerb84886b2008-05-19 15:05:07 +100063#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100064#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100065#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "ssh1.h"
67#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000068#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100069#include "packet.h"
70#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100071#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100072#include "uidswap.h"
73#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000074#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100075#include "key.h"
76#include "cipher.h"
77#ifdef GSSAPI
78#include "ssh-gss.h"
79#endif
80#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100081#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100082#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000083#include "pathnames.h"
84#include "log.h"
85#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000086#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087#include "serverloop.h"
88#include "canohost.h"
Damien Millerd8cb1f12008-02-10 22:40:12 +110089#include "misc.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000090#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100091#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110093#include "sftp.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100094
Darren Tucker3c78c5e2004-01-23 22:03:10 +110095#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110096#include <kafs.h>
97#endif
98
Damien Miller14684a12011-05-20 11:23:07 +100099#ifdef WITH_SELINUX
100#include <selinux/selinux.h>
101#endif
102
Damien Millerad793d52008-11-03 19:17:57 +1100103#define IS_INTERNAL_SFTP(c) \
104 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
105 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
106 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
107 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
108
Damien Millerb38eff82000-04-01 11:09:21 +1000109/* func */
110
111Session *session_new(void);
Damien Miller8853ca52010-06-26 10:00:14 +1000112void session_set_fds(Session *, int, int, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000113void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000114void session_proctitle(Session *);
115int session_setup_x11fwd(Session *);
Damien Miller7207f642008-05-19 15:34:50 +1000116int do_exec_pty(Session *, const char *);
117int do_exec_no_pty(Session *, const char *);
118int do_exec(Session *, const char *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000119void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000120#ifdef LOGIN_NEEDS_UTMPX
121static void do_pre_login(Session *s);
122#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000123void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000124void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000125int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000126
Ben Lindstrombba81212001-06-25 05:01:22 +0000127static void do_authenticated1(Authctxt *);
128static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000129
Ben Lindstrombba81212001-06-25 05:01:22 +0000130static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000131
Damien Millerb38eff82000-04-01 11:09:21 +1000132/* import */
133extern ServerOptions options;
134extern char *__progname;
135extern int log_stderr;
136extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000137extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000138extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000139extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000140extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000141
Damien Miller7b28dc52000-09-05 13:34:53 +1100142/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000143const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100144
Damien Millerb38eff82000-04-01 11:09:21 +1000145/* data */
Damien Miller7207f642008-05-19 15:34:50 +1000146static int sessions_first_unused = -1;
147static int sessions_nalloc = 0;
148static Session *sessions = NULL;
Damien Miller15e7d4b2000-09-29 10:57:35 +1100149
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100150#define SUBSYSTEM_NONE 0
151#define SUBSYSTEM_EXT 1
152#define SUBSYSTEM_INT_SFTP 2
153#define SUBSYSTEM_INT_SFTP_ERROR 3
Damien Millerdfc24252008-02-10 22:29:40 +1100154
Damien Millerad833b32000-08-23 10:46:23 +1000155#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000156login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000157#endif
158
Darren Tucker3e33cec2003-10-02 16:12:36 +1000159static int is_child = 0;
160
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000161/* Name and directory of socket for authentication agent forwarding. */
162static char *auth_sock_name = NULL;
163static char *auth_sock_dir = NULL;
164
165/* removes the agent forwarding socket */
166
167static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000168auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000169{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000170 if (auth_sock_name != NULL) {
171 temporarily_use_uid(pw);
172 unlink(auth_sock_name);
173 rmdir(auth_sock_dir);
174 auth_sock_name = NULL;
175 restore_uid();
176 }
177}
178
179static int
180auth_input_request_forwarding(struct passwd * pw)
181{
182 Channel *nc;
Damien Miller7207f642008-05-19 15:34:50 +1000183 int sock = -1;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000184 struct sockaddr_un sunaddr;
185
186 if (auth_sock_name != NULL) {
187 error("authentication forwarding requested twice.");
188 return 0;
189 }
190
191 /* Temporarily drop privileged uid for mkdir/bind. */
192 temporarily_use_uid(pw);
193
194 /* Allocate a buffer for the socket name, and format the name. */
Damien Miller7207f642008-05-19 15:34:50 +1000195 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000196
197 /* Create private directory for socket */
198 if (mkdtemp(auth_sock_dir) == NULL) {
199 packet_send_debug("Agent forwarding disabled: "
200 "mkdtemp() failed: %.100s", strerror(errno));
201 restore_uid();
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000202 xfree(auth_sock_dir);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000203 auth_sock_dir = NULL;
Damien Miller7207f642008-05-19 15:34:50 +1000204 goto authsock_err;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000205 }
Damien Miller7207f642008-05-19 15:34:50 +1000206
207 xasprintf(&auth_sock_name, "%s/agent.%ld",
208 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000209
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000210 /* Create the socket. */
211 sock = socket(AF_UNIX, SOCK_STREAM, 0);
Damien Miller7207f642008-05-19 15:34:50 +1000212 if (sock < 0) {
213 error("socket: %.100s", strerror(errno));
214 restore_uid();
215 goto authsock_err;
216 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000217
218 /* Bind it to the name. */
219 memset(&sunaddr, 0, sizeof(sunaddr));
220 sunaddr.sun_family = AF_UNIX;
221 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
222
Damien Miller7207f642008-05-19 15:34:50 +1000223 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
224 error("bind: %.100s", strerror(errno));
225 restore_uid();
226 goto authsock_err;
227 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000228
229 /* Restore the privileged uid. */
230 restore_uid();
231
232 /* Start listening on the socket. */
Damien Miller7207f642008-05-19 15:34:50 +1000233 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
234 error("listen: %.100s", strerror(errno));
235 goto authsock_err;
236 }
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000237
238 /* Allocate a channel for the authentication agent socket. */
239 nc = channel_new("auth socket",
240 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
241 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000242 0, "auth socket", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100243 nc->path = xstrdup(auth_sock_name);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000244 return 1;
Damien Miller7207f642008-05-19 15:34:50 +1000245
246 authsock_err:
247 if (auth_sock_name != NULL)
248 xfree(auth_sock_name);
249 if (auth_sock_dir != NULL) {
250 rmdir(auth_sock_dir);
251 xfree(auth_sock_dir);
252 }
253 if (sock != -1)
254 close(sock);
255 auth_sock_name = NULL;
256 auth_sock_dir = NULL;
257 return 0;
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000258}
259
Darren Tucker1921ed92004-02-10 13:23:28 +1100260static void
261display_loginmsg(void)
262{
Damien Miller46d38de2005-07-17 17:02:09 +1000263 if (buffer_len(&loginmsg) > 0) {
264 buffer_append(&loginmsg, "\0", 1);
265 printf("%s", (char *)buffer_ptr(&loginmsg));
266 buffer_clear(&loginmsg);
267 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100268}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000269
Ben Lindstromb31783d2001-03-22 02:02:12 +0000270void
271do_authenticated(Authctxt *authctxt)
272{
Damien Miller97f39ae2003-02-24 11:57:01 +1100273 setproctitle("%s", authctxt->pw->pw_name);
274
Ben Lindstromb31783d2001-03-22 02:02:12 +0000275 /* setup the channel layer */
276 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
277 channel_permit_all_opens();
278
Darren Tuckercd70e1b2010-03-07 23:05:17 +1100279 auth_debug_send();
280
Ben Lindstromb31783d2001-03-22 02:02:12 +0000281 if (compat20)
282 do_authenticated2(authctxt);
283 else
284 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000285
Darren Tucker3e33cec2003-10-02 16:12:36 +1000286 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000287}
288
Damien Millerb38eff82000-04-01 11:09:21 +1000289/*
Damien Millerb38eff82000-04-01 11:09:21 +1000290 * Prepares for an interactive session. This is called after the user has
291 * been successfully authenticated. During this message exchange, pseudo
292 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
293 * are requested, etc.
294 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000295static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000296do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000297{
298 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000299 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100300 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000301 int enable_compression_after_reply = 0;
302 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000303
304 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100305 if (s == NULL) {
306 error("no more sessions");
307 return;
308 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000309 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000310 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000311
Damien Millerb38eff82000-04-01 11:09:21 +1000312 /*
313 * We stay in this loop until the client requests to execute a shell
314 * or a command.
315 */
316 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000317 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000318
319 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100320 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000321
322 /* Process the packet. */
323 switch (type) {
324 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000325 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100326 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000327 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000328 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100329 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000330 break;
331 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000332 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000333 debug2("compression disabled");
334 break;
335 }
Damien Millerb38eff82000-04-01 11:09:21 +1000336 /* Enable compression after we have responded with SUCCESS. */
337 enable_compression_after_reply = 1;
338 success = 1;
339 break;
340
341 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000342 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000343 break;
344
345 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000346 s->auth_proto = packet_get_string(&proto_len);
347 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000348
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000349 screen_flag = packet_get_protocol_flags() &
350 SSH_PROTOFLAG_SCREEN_NUMBER;
351 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
352
353 if (packet_remaining() == 4) {
354 if (!screen_flag)
355 debug2("Buggy client: "
356 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000357 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000358 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000359 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000360 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100361 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000362 success = session_setup_x11fwd(s);
363 if (!success) {
364 xfree(s->auth_proto);
365 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000366 s->auth_proto = NULL;
367 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000368 }
Damien Millerb38eff82000-04-01 11:09:21 +1000369 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000370
371 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
Damien Miller4f755cd2008-05-19 14:57:41 +1000372 if (!options.allow_agent_forwarding ||
373 no_agent_forwarding_flag || compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000374 debug("Authentication agent forwarding not permitted for this authentication.");
375 break;
376 }
377 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000378 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000379 break;
380
381 case SSH_CMSG_PORT_FORWARD_REQUEST:
382 if (no_port_forwarding_flag) {
383 debug("Port forwarding not permitted for this authentication.");
384 break;
385 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100386 if (!options.allow_tcp_forwarding) {
387 debug("Port forwarding not permitted.");
388 break;
389 }
Damien Millerb38eff82000-04-01 11:09:21 +1000390 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000391 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
392 options.gateway_ports) < 0) {
393 debug("Port forwarding failed.");
394 break;
395 }
Damien Millerb38eff82000-04-01 11:09:21 +1000396 success = 1;
397 break;
398
399 case SSH_CMSG_MAX_PACKET_SIZE:
400 if (packet_set_maxsize(packet_get_int()) > 0)
401 success = 1;
402 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100403
Damien Millerb38eff82000-04-01 11:09:21 +1000404 case SSH_CMSG_EXEC_SHELL:
405 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000406 if (type == SSH_CMSG_EXEC_CMD) {
407 command = packet_get_string(&dlen);
408 debug("Exec command '%.500s'", command);
Damien Miller7207f642008-05-19 15:34:50 +1000409 if (do_exec(s, command) != 0)
410 packet_disconnect(
411 "command execution failed");
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000412 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000413 } else {
Damien Miller7207f642008-05-19 15:34:50 +1000414 if (do_exec(s, NULL) != 0)
415 packet_disconnect(
416 "shell execution failed");
Damien Millerb38eff82000-04-01 11:09:21 +1000417 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100418 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000419 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000420 return;
421
422 default:
423 /*
424 * Any unknown messages in this phase are ignored,
425 * and a failure message is returned.
426 */
Damien Miller996acd22003-04-09 20:59:48 +1000427 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000428 }
429 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
430 packet_send();
431 packet_write_wait();
432
433 /* Enable compression now that we have replied if appropriate. */
434 if (enable_compression_after_reply) {
435 enable_compression_after_reply = 0;
436 packet_start_compression(compression_level);
437 }
438 }
439}
440
Damien Miller7207f642008-05-19 15:34:50 +1000441#define USE_PIPES
Damien Millerb38eff82000-04-01 11:09:21 +1000442/*
443 * This is called to fork and execute a command when we have no tty. This
444 * will call do_child from the child, and server_loop from the parent after
445 * setting up file descriptors and such.
446 */
Damien Miller7207f642008-05-19 15:34:50 +1000447int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000448do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000449{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000450 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000451
452#ifdef USE_PIPES
453 int pin[2], pout[2], perr[2];
Damien Miller7207f642008-05-19 15:34:50 +1000454
Damien Miller22a29882010-05-10 11:53:54 +1000455 if (s == NULL)
456 fatal("do_exec_no_pty: no session");
457
Damien Millerb38eff82000-04-01 11:09:21 +1000458 /* Allocate pipes for communicating with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000459 if (pipe(pin) < 0) {
460 error("%s: pipe in: %.100s", __func__, strerror(errno));
461 return -1;
462 }
463 if (pipe(pout) < 0) {
464 error("%s: pipe out: %.100s", __func__, strerror(errno));
465 close(pin[0]);
466 close(pin[1]);
467 return -1;
468 }
Damien Miller8853ca52010-06-26 10:00:14 +1000469 if (pipe(perr) < 0) {
470 error("%s: pipe err: %.100s", __func__,
471 strerror(errno));
472 close(pin[0]);
473 close(pin[1]);
474 close(pout[0]);
475 close(pout[1]);
476 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000477 }
478#else
Damien Millerb38eff82000-04-01 11:09:21 +1000479 int inout[2], err[2];
Damien Miller7207f642008-05-19 15:34:50 +1000480
Damien Miller22a29882010-05-10 11:53:54 +1000481 if (s == NULL)
482 fatal("do_exec_no_pty: no session");
483
Damien Millerb38eff82000-04-01 11:09:21 +1000484 /* Uses socket pairs to communicate with the program. */
Damien Miller7207f642008-05-19 15:34:50 +1000485 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
486 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
487 return -1;
488 }
Damien Miller8853ca52010-06-26 10:00:14 +1000489 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
490 error("%s: socketpair #2: %.100s", __func__,
491 strerror(errno));
492 close(inout[0]);
493 close(inout[1]);
494 return -1;
Damien Miller7207f642008-05-19 15:34:50 +1000495 }
496#endif
497
Damien Millere247cc42000-05-07 12:03:14 +1000498 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000499
Damien Millerb38eff82000-04-01 11:09:21 +1000500 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000501 switch ((pid = fork())) {
502 case -1:
503 error("%s: fork: %.100s", __func__, strerror(errno));
504#ifdef USE_PIPES
505 close(pin[0]);
506 close(pin[1]);
507 close(pout[0]);
508 close(pout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000509 close(perr[0]);
Damien Miller7207f642008-05-19 15:34:50 +1000510 close(perr[1]);
511#else
512 close(inout[0]);
513 close(inout[1]);
514 close(err[0]);
Damien Miller8853ca52010-06-26 10:00:14 +1000515 close(err[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000516#endif
517 return -1;
518 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000519 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000520
Damien Millerb38eff82000-04-01 11:09:21 +1000521 /* Child. Reinitialize the log since the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000522 log_init(__progname, options.log_level,
523 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000524
525 /*
526 * Create a new session and process group since the 4.4BSD
527 * setlogin() affects the entire process group.
528 */
529 if (setsid() < 0)
530 error("setsid failed: %.100s", strerror(errno));
531
532#ifdef USE_PIPES
533 /*
534 * Redirect stdin. We close the parent side of the socket
535 * pair, and make the child side the standard input.
536 */
537 close(pin[1]);
538 if (dup2(pin[0], 0) < 0)
539 perror("dup2 stdin");
540 close(pin[0]);
541
542 /* Redirect stdout. */
543 close(pout[0]);
544 if (dup2(pout[1], 1) < 0)
545 perror("dup2 stdout");
546 close(pout[1]);
547
548 /* Redirect stderr. */
Damien Miller8853ca52010-06-26 10:00:14 +1000549 close(perr[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000550 if (dup2(perr[1], 2) < 0)
551 perror("dup2 stderr");
552 close(perr[1]);
Damien Miller7207f642008-05-19 15:34:50 +1000553#else
Damien Millerb38eff82000-04-01 11:09:21 +1000554 /*
555 * Redirect stdin, stdout, and stderr. Stdin and stdout will
556 * use the same socket, as some programs (particularly rdist)
557 * seem to depend on it.
558 */
559 close(inout[1]);
Damien Miller8853ca52010-06-26 10:00:14 +1000560 close(err[1]);
Damien Millerb38eff82000-04-01 11:09:21 +1000561 if (dup2(inout[0], 0) < 0) /* stdin */
562 perror("dup2 stdin");
Damien Miller7207f642008-05-19 15:34:50 +1000563 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
Damien Millerb38eff82000-04-01 11:09:21 +1000564 perror("dup2 stdout");
Damien Miller7207f642008-05-19 15:34:50 +1000565 close(inout[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000566 if (dup2(err[0], 2) < 0) /* stderr */
567 perror("dup2 stderr");
Damien Miller7207f642008-05-19 15:34:50 +1000568 close(err[0]);
569#endif
570
Damien Millerb38eff82000-04-01 11:09:21 +1000571
Tim Rice81ed5182002-09-25 17:38:46 -0700572#ifdef _UNICOS
573 cray_init_job(s->pw); /* set up cray jid and tmpdir */
574#endif
575
Damien Millerb38eff82000-04-01 11:09:21 +1000576 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000577 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000578 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000579 default:
580 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000581 }
Damien Miller7207f642008-05-19 15:34:50 +1000582
Tim Rice81ed5182002-09-25 17:38:46 -0700583#ifdef _UNICOS
584 signal(WJSIGNAL, cray_job_termination_handler);
585#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100586#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100587 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100588#endif
Damien Miller7207f642008-05-19 15:34:50 +1000589
Damien Millerb38eff82000-04-01 11:09:21 +1000590 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000591 /* Set interactive/non-interactive mode. */
Damien Miller0dac6fb2010-11-20 15:19:38 +1100592 packet_set_interactive(s->display != NULL,
593 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller7207f642008-05-19 15:34:50 +1000594
595 /*
596 * Clear loginmsg, since it's the child's responsibility to display
597 * it to the user, otherwise multiple sessions may accumulate
598 * multiple copies of the login messages.
599 */
600 buffer_clear(&loginmsg);
601
Damien Millerb38eff82000-04-01 11:09:21 +1000602#ifdef USE_PIPES
603 /* We are the parent. Close the child sides of the pipes. */
604 close(pin[0]);
605 close(pout[1]);
606 close(perr[1]);
607
Damien Millerefb4afe2000-04-12 18:45:05 +1000608 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000609 session_set_fds(s, pin[1], pout[0], perr[0],
610 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000611 } else {
612 /* Enter the interactive session. */
613 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000614 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000615 }
Damien Miller7207f642008-05-19 15:34:50 +1000616#else
Damien Millerb38eff82000-04-01 11:09:21 +1000617 /* We are the parent. Close the child sides of the socket pairs. */
618 close(inout[0]);
619 close(err[0]);
620
621 /*
622 * Enter the interactive session. Note: server_loop must be able to
623 * handle the case that fdin and fdout are the same.
624 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000625 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000626 session_set_fds(s, inout[1], inout[1], err[1],
627 s->is_subsystem, 0);
Damien Millerefb4afe2000-04-12 18:45:05 +1000628 } else {
629 server_loop(pid, inout[1], inout[1], err[1]);
630 /* server_loop has closed inout[1] and err[1]. */
631 }
Damien Miller7207f642008-05-19 15:34:50 +1000632#endif
633 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000634}
635
636/*
637 * This is called to fork and execute a command when we have a tty. This
638 * will call do_child from the child, and server_loop from the parent after
639 * setting up file descriptors, controlling tty, updating wtmp, utmp,
640 * lastlog, and other such operations.
641 */
Damien Miller7207f642008-05-19 15:34:50 +1000642int
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000643do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000644{
Damien Millerb38eff82000-04-01 11:09:21 +1000645 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000646 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000647
648 if (s == NULL)
649 fatal("do_exec_pty: no session");
650 ptyfd = s->ptyfd;
651 ttyfd = s->ttyfd;
652
Damien Miller7207f642008-05-19 15:34:50 +1000653 /*
654 * Create another descriptor of the pty master side for use as the
655 * standard input. We could use the original descriptor, but this
656 * simplifies code in server_loop. The descriptor is bidirectional.
657 * Do this before forking (and cleanup in the child) so as to
658 * detect and gracefully fail out-of-fd conditions.
659 */
660 if ((fdout = dup(ptyfd)) < 0) {
661 error("%s: dup #1: %s", __func__, strerror(errno));
662 close(ttyfd);
663 close(ptyfd);
664 return -1;
665 }
666 /* we keep a reference to the pty master */
667 if ((ptymaster = dup(ptyfd)) < 0) {
668 error("%s: dup #2: %s", __func__, strerror(errno));
669 close(ttyfd);
670 close(ptyfd);
671 close(fdout);
672 return -1;
673 }
674
Damien Millerb38eff82000-04-01 11:09:21 +1000675 /* Fork the child. */
Damien Miller7207f642008-05-19 15:34:50 +1000676 switch ((pid = fork())) {
677 case -1:
678 error("%s: fork: %.100s", __func__, strerror(errno));
679 close(fdout);
680 close(ptymaster);
681 close(ttyfd);
682 close(ptyfd);
683 return -1;
684 case 0:
Darren Tucker3e33cec2003-10-02 16:12:36 +1000685 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000686
Damien Miller7207f642008-05-19 15:34:50 +1000687 close(fdout);
688 close(ptymaster);
689
Damien Miller942da032000-08-18 13:59:06 +1000690 /* Child. Reinitialize the log because the pid has changed. */
Damien Miller7207f642008-05-19 15:34:50 +1000691 log_init(__progname, options.log_level,
692 options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000693 /* Close the master side of the pseudo tty. */
694 close(ptyfd);
695
696 /* Make the pseudo tty our controlling tty. */
697 pty_make_controlling_tty(&ttyfd, s->tty);
698
Damien Miller9c751422001-10-10 15:02:46 +1000699 /* Redirect stdin/stdout/stderr from the pseudo tty. */
700 if (dup2(ttyfd, 0) < 0)
701 error("dup2 stdin: %s", strerror(errno));
702 if (dup2(ttyfd, 1) < 0)
703 error("dup2 stdout: %s", strerror(errno));
704 if (dup2(ttyfd, 2) < 0)
705 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000706
707 /* Close the extra descriptor for the pseudo tty. */
708 close(ttyfd);
709
Damien Miller942da032000-08-18 13:59:06 +1000710 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000711#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700712 if (!(options.use_login && command == NULL)) {
713#ifdef _UNICOS
714 cray_init_job(s->pw); /* set up cray jid and tmpdir */
715#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100716 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700717 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000718# ifdef LOGIN_NEEDS_UTMPX
719 else
720 do_pre_login(s);
721# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000722#endif
Damien Miller7207f642008-05-19 15:34:50 +1000723 /*
724 * Do common processing for the child, such as execing
725 * the command.
726 */
Darren Tucker43e7a352009-06-21 19:50:08 +1000727 do_child(s, command);
728 /* NOTREACHED */
Damien Miller7207f642008-05-19 15:34:50 +1000729 default:
730 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000731 }
Damien Miller7207f642008-05-19 15:34:50 +1000732
Tim Rice81ed5182002-09-25 17:38:46 -0700733#ifdef _UNICOS
734 signal(WJSIGNAL, cray_job_termination_handler);
735#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100736#ifdef HAVE_CYGWIN
Darren Tucker9d86e5d2009-03-08 11:40:27 +1100737 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100738#endif
Damien Miller7207f642008-05-19 15:34:50 +1000739
Damien Millerb38eff82000-04-01 11:09:21 +1000740 s->pid = pid;
741
742 /* Parent. Close the slave side of the pseudo tty. */
743 close(ttyfd);
744
Damien Millerb38eff82000-04-01 11:09:21 +1000745 /* Enter interactive session. */
Damien Miller7207f642008-05-19 15:34:50 +1000746 s->ptymaster = ptymaster;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100747 packet_set_interactive(1,
748 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Millerefb4afe2000-04-12 18:45:05 +1000749 if (compat20) {
Damien Miller8853ca52010-06-26 10:00:14 +1000750 session_set_fds(s, ptyfd, fdout, -1, 1, 1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000751 } else {
752 server_loop(pid, ptyfd, fdout, -1);
753 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000754 }
Damien Miller7207f642008-05-19 15:34:50 +1000755 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000756}
757
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000758#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000759static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000760do_pre_login(Session *s)
761{
762 socklen_t fromlen;
763 struct sockaddr_storage from;
764 pid_t pid = getpid();
765
766 /*
767 * Get IP address of client. If the connection is not a socket, let
768 * the address be 0.0.0.0.
769 */
770 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000771 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000772 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000773 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100774 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000775 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000776 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000777 }
778 }
779
780 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000781 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000782 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000783}
784#endif
785
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000786/*
787 * This is called to fork and execute a command. If another command is
788 * to be forced, execute that instead.
789 */
Damien Miller7207f642008-05-19 15:34:50 +1000790int
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000791do_exec(Session *s, const char *command)
792{
Damien Miller7207f642008-05-19 15:34:50 +1000793 int ret;
794
Damien Millere2754432006-07-24 14:06:47 +1000795 if (options.adm_forced_command) {
796 original_command = command;
797 command = options.adm_forced_command;
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100798 if (IS_INTERNAL_SFTP(command)) {
799 s->is_subsystem = s->is_subsystem ?
800 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
801 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100802 s->is_subsystem = SUBSYSTEM_EXT;
Damien Millere2754432006-07-24 14:06:47 +1000803 debug("Forced command (config) '%.900s'", command);
804 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000805 original_command = command;
806 command = forced_command;
Darren Tuckerd6b06a92010-01-08 17:09:11 +1100807 if (IS_INTERNAL_SFTP(command)) {
808 s->is_subsystem = s->is_subsystem ?
809 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
810 } else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100811 s->is_subsystem = SUBSYSTEM_EXT;
Damien Millere2754432006-07-24 14:06:47 +1000812 debug("Forced command (key option) '%.900s'", command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000813 }
814
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100815#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100816 if (command != NULL)
817 PRIVSEP(audit_run_command(command));
818 else if (s->ttyfd == -1) {
819 char *shell = s->pw->pw_shell;
820
821 if (shell[0] == '\0') /* empty shell means /bin/sh */
822 shell =_PATH_BSHELL;
823 PRIVSEP(audit_run_command(shell));
824 }
825#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000826 if (s->ttyfd != -1)
Damien Miller7207f642008-05-19 15:34:50 +1000827 ret = do_exec_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000828 else
Damien Miller7207f642008-05-19 15:34:50 +1000829 ret = do_exec_no_pty(s, command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000830
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000831 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000832
Darren Tucker09991742004-07-17 17:05:14 +1000833 /*
834 * Clear loginmsg: it's the child's responsibility to display
835 * it to the user, otherwise multiple sessions may accumulate
836 * multiple copies of the login messages.
837 */
838 buffer_clear(&loginmsg);
Damien Miller7207f642008-05-19 15:34:50 +1000839
840 return ret;
Darren Tucker09991742004-07-17 17:05:14 +1000841}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000842
Damien Miller942da032000-08-18 13:59:06 +1000843/* administrative, login(1)-like work */
844void
Damien Miller69b69aa2000-10-28 14:19:58 +1100845do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000846{
Damien Miller942da032000-08-18 13:59:06 +1000847 socklen_t fromlen;
848 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000849 struct passwd * pw = s->pw;
850 pid_t pid = getpid();
851
852 /*
853 * Get IP address of client. If the connection is not a socket, let
854 * the address be 0.0.0.0.
855 */
856 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000857 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000858 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000859 if (getpeername(packet_get_connection_in(),
Darren Tucker43e7a352009-06-21 19:50:08 +1000860 (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000861 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000862 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000863 }
864 }
865
866 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000867 if (!use_privsep)
868 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
869 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000870 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000871 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000872
Kevin Steves092f2ef2000-10-14 13:36:13 +0000873#ifdef USE_PAM
874 /*
875 * If password change is needed, do it now.
876 * This needs to occur before the ~/.hushlogin check.
877 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100878 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
879 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000880 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100881 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000882 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000883 }
884#endif
885
Damien Millercf205e82001-04-16 18:29:15 +1000886 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000887 return;
888
Darren Tucker1921ed92004-02-10 13:23:28 +1100889 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000890
Damien Millercf205e82001-04-16 18:29:15 +1000891 do_motd();
892}
893
894/*
895 * Display the message of the day.
896 */
897void
898do_motd(void)
899{
900 FILE *f;
901 char buf[256];
902
Damien Miller942da032000-08-18 13:59:06 +1000903 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000904#ifdef HAVE_LOGIN_CAP
905 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
906 "/etc/motd"), "r");
907#else
Damien Miller942da032000-08-18 13:59:06 +1000908 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000909#endif
Damien Miller942da032000-08-18 13:59:06 +1000910 if (f) {
911 while (fgets(buf, sizeof(buf), f))
912 fputs(buf, stdout);
913 fclose(f);
914 }
915 }
916}
917
Kevin Steves8f63caa2001-07-04 18:23:02 +0000918
919/*
920 * Check for quiet login, either .hushlogin or command given.
921 */
922int
923check_quietlogin(Session *s, const char *command)
924{
925 char buf[256];
926 struct passwd *pw = s->pw;
927 struct stat st;
928
929 /* Return 1 if .hushlogin exists or a command given. */
930 if (command != NULL)
931 return 1;
932 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
933#ifdef HAVE_LOGIN_CAP
934 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
935 return 1;
936#else
937 if (stat(buf, &st) >= 0)
938 return 1;
939#endif
940 return 0;
941}
942
Damien Millerb38eff82000-04-01 11:09:21 +1000943/*
944 * Sets the value of the given variable in the environment. If the variable
Darren Tucker63917bd2008-11-11 16:33:48 +1100945 * already exists, its value is overridden.
Damien Millerb38eff82000-04-01 11:09:21 +1000946 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000947void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000948child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100949 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000950{
Damien Millerb38eff82000-04-01 11:09:21 +1000951 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000952 u_int envsize;
953 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000954
955 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000956 * If we're passed an uninitialized list, allocate a single null
957 * entry before continuing.
958 */
959 if (*envp == NULL && *envsizep == 0) {
960 *envp = xmalloc(sizeof(char *));
961 *envp[0] = NULL;
962 *envsizep = 1;
963 }
964
965 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000966 * Find the slot where the value should be stored. If the variable
967 * already exists, we reuse the slot; otherwise we append a new slot
968 * at the end of the array, expanding if necessary.
969 */
970 env = *envp;
971 namelen = strlen(name);
972 for (i = 0; env[i]; i++)
973 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
974 break;
975 if (env[i]) {
976 /* Reuse the slot. */
977 xfree(env[i]);
978 } else {
979 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000980 envsize = *envsizep;
981 if (i >= envsize - 1) {
982 if (envsize >= 1000)
983 fatal("child_set_env: too many env vars");
984 envsize += 50;
Damien Miller36812092006-03-26 14:22:47 +1100985 env = (*envp) = xrealloc(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000986 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000987 }
988 /* Need to set the NULL pointer at end of array beyond the new slot. */
989 env[i + 1] = NULL;
990 }
991
992 /* Allocate space and format the variable in the appropriate slot. */
993 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
994 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
995}
996
997/*
998 * Reads environment variables from the given file and adds/overrides them
999 * into the environment. If the file does not exist, this does nothing.
1000 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1001 * and assignments of the form name=value. No other forms are allowed.
1002 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001003static void
Ben Lindstrom46c16222000-12-22 01:43:59 +00001004read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001005 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +10001006{
1007 FILE *f;
1008 char buf[4096];
1009 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +10001010 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001011
1012 f = fopen(filename, "r");
1013 if (!f)
1014 return;
1015
1016 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +10001017 if (++lineno > 1000)
1018 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001019 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1020 ;
1021 if (!*cp || *cp == '#' || *cp == '\n')
1022 continue;
Damien Miller14b017d2007-09-17 16:09:15 +10001023
1024 cp[strcspn(cp, "\n")] = '\0';
1025
Damien Millerb38eff82000-04-01 11:09:21 +10001026 value = strchr(cp, '=');
1027 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +10001028 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1029 filename);
Damien Millerb38eff82000-04-01 11:09:21 +10001030 continue;
1031 }
Damien Millerb1715dc2000-05-30 13:44:51 +10001032 /*
1033 * Replace the equals sign by nul, and advance value to
1034 * the value string.
1035 */
Damien Millerb38eff82000-04-01 11:09:21 +10001036 *value = '\0';
1037 value++;
1038 child_set_env(env, envsize, cp, value);
1039 }
1040 fclose(f);
1041}
1042
Darren Tuckere1a790d2003-09-16 11:52:19 +10001043#ifdef HAVE_ETC_DEFAULT_LOGIN
1044/*
1045 * Return named variable from specified environment, or NULL if not present.
1046 */
1047static char *
1048child_get_env(char **env, const char *name)
1049{
1050 int i;
1051 size_t len;
1052
1053 len = strlen(name);
1054 for (i=0; env[i] != NULL; i++)
1055 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1056 return(env[i] + len + 1);
1057 return NULL;
1058}
1059
1060/*
1061 * Read /etc/default/login.
1062 * We pick up the PATH (or SUPATH for root) and UMASK.
1063 */
1064static void
1065read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1066{
1067 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001068 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +10001069 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +10001070
1071 /*
1072 * We don't want to copy the whole file to the child's environment,
1073 * so we use a temporary environment and copy the variables we're
1074 * interested in.
1075 */
1076 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1077
Darren Tuckerc11b1e82003-09-19 20:56:51 +10001078 if (tmpenv == NULL)
1079 return;
1080
Darren Tuckere1a790d2003-09-16 11:52:19 +10001081 if (uid == 0)
1082 var = child_get_env(tmpenv, "SUPATH");
1083 else
1084 var = child_get_env(tmpenv, "PATH");
1085 if (var != NULL)
1086 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +11001087
Darren Tuckere1a790d2003-09-16 11:52:19 +10001088 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1089 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +10001090 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +11001091
Darren Tuckere1a790d2003-09-16 11:52:19 +10001092 for (i = 0; tmpenv[i] != NULL; i++)
1093 xfree(tmpenv[i]);
1094 xfree(tmpenv);
1095}
1096#endif /* HAVE_ETC_DEFAULT_LOGIN */
1097
Damien Miller7dff8692005-05-26 11:34:51 +10001098void
1099copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +10001100{
Damien Millerbb9ffc12002-01-08 10:59:32 +11001101 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +10001102 int i;
1103
Damien Millerbb9ffc12002-01-08 10:59:32 +11001104 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001105 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001106
Damien Millerbb9ffc12002-01-08 10:59:32 +11001107 for(i = 0; source[i] != NULL; i++) {
1108 var_name = xstrdup(source[i]);
1109 if ((var_val = strstr(var_name, "=")) == NULL) {
1110 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001111 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001112 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001113 *var_val++ = '\0';
1114
1115 debug3("Copy environment: %s=%s", var_name, var_val);
1116 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001117
Damien Millerbb9ffc12002-01-08 10:59:32 +11001118 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001119 }
1120}
Damien Millercb5e44a2000-09-29 12:12:36 +11001121
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001122static char **
1123do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001124{
Damien Millerb38eff82000-04-01 11:09:21 +10001125 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001126 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001127 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001128 struct passwd *pw = s->pw;
Darren Tucker9d86e5d2009-03-08 11:40:27 +11001129#if !defined (HAVE_LOGIN_CAP) && !defined (HAVE_CYGWIN)
Damien Millerad5ecbf2006-07-24 15:03:06 +10001130 char *path = NULL;
1131#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001132
Damien Millerb38eff82000-04-01 11:09:21 +10001133 /* Initialize the environment. */
1134 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001135 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001136 env[0] = NULL;
1137
Damien Millerbac2d8a2000-09-05 16:13:06 +11001138#ifdef HAVE_CYGWIN
1139 /*
1140 * The Windows environment contains some setting which are
1141 * important for a running system. They must not be dropped.
1142 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001143 {
1144 char **p;
1145
1146 p = fetch_windows_environment();
1147 copy_environment(p, &env, &envsize);
1148 free_windows_environment(p);
1149 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001150#endif
1151
Darren Tucker0efd1552003-08-26 11:49:55 +10001152#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001153 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001154 * the childs environment as they see fit
1155 */
1156 ssh_gssapi_do_child(&env, &envsize);
1157#endif
1158
Damien Millerb38eff82000-04-01 11:09:21 +10001159 if (!options.use_login) {
1160 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001161 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001162 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001163 s->env[i].val);
1164
Damien Millerb38eff82000-04-01 11:09:21 +10001165 child_set_env(&env, &envsize, "USER", pw->pw_name);
1166 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001167#ifdef _AIX
1168 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1169#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001170 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001171#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001172 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1173 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1174 else
1175 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001176#else /* HAVE_LOGIN_CAP */
1177# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001178 /*
1179 * There's no standard path on Windows. The path contains
1180 * important components pointing to the system directories,
1181 * needed for loading shared libraries. So the path better
1182 * remains intact here.
1183 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001184# ifdef HAVE_ETC_DEFAULT_LOGIN
1185 read_etc_default_login(&env, &envsize, pw->pw_uid);
1186 path = child_get_env(env, "PATH");
1187# endif /* HAVE_ETC_DEFAULT_LOGIN */
1188 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001189 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001190 s->pw->pw_uid == 0 ?
1191 SUPERUSER_PATH : _PATH_STDPATH);
1192 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001193# endif /* HAVE_CYGWIN */
1194#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001195
1196 snprintf(buf, sizeof buf, "%.200s/%.50s",
1197 _PATH_MAILDIR, pw->pw_name);
1198 child_set_env(&env, &envsize, "MAIL", buf);
1199
1200 /* Normal systems set SHELL by default. */
1201 child_set_env(&env, &envsize, "SHELL", shell);
1202 }
1203 if (getenv("TZ"))
1204 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1205
1206 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001207 if (!options.use_login) {
1208 while (custom_environment) {
1209 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001210 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001211
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001212 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001213 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001214 if (str[i] == '=') {
1215 str[i] = 0;
1216 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001217 }
1218 custom_environment = ce->next;
1219 xfree(ce->s);
1220 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001221 }
Damien Millerb38eff82000-04-01 11:09:21 +10001222 }
1223
Damien Millerf37e2462002-09-19 11:47:55 +10001224 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001225 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001226 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001227 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1228
Damien Miller00111382003-03-10 11:21:17 +11001229 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001230 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001231 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1232 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001233 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1234
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001235 if (s->ttyfd != -1)
1236 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1237 if (s->term)
1238 child_set_env(&env, &envsize, "TERM", s->term);
1239 if (s->display)
1240 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001241 if (original_command)
1242 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1243 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001244
Tim Rice81ed5182002-09-25 17:38:46 -07001245#ifdef _UNICOS
1246 if (cray_tmpdir[0] != '\0')
1247 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1248#endif /* _UNICOS */
1249
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001250 /*
1251 * Since we clear KRB5CCNAME at startup, if it's set now then it
1252 * must have been set by a native authentication method (eg AIX or
1253 * SIA), so copy it to the child.
1254 */
1255 {
1256 char *cp;
1257
1258 if ((cp = getenv("KRB5CCNAME")) != NULL)
1259 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1260 }
1261
Damien Millerb38eff82000-04-01 11:09:21 +10001262#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001263 {
1264 char *cp;
1265
1266 if ((cp = getenv("AUTHSTATE")) != NULL)
1267 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001268 read_environment_file(&env, &envsize, "/etc/environment");
1269 }
Damien Millerb38eff82000-04-01 11:09:21 +10001270#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001271#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001272 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001273 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001274 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001275#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001276#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001277 /*
1278 * Pull in any environment variables that may have
1279 * been set by PAM.
1280 */
Damien Miller4e448a32003-05-14 15:11:48 +10001281 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001282 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001283
Damien Millerc756e9b2003-11-17 21:41:42 +11001284 p = fetch_pam_child_environment();
1285 copy_environment(p, &env, &envsize);
1286 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001287
Damien Millerc756e9b2003-11-17 21:41:42 +11001288 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001289 copy_environment(p, &env, &envsize);
1290 free_pam_environment(p);
1291 }
Damien Millerb38eff82000-04-01 11:09:21 +10001292#endif /* USE_PAM */
1293
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001294 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001295 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001296 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001297
1298 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001299 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001300 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001301 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001302 read_environment_file(&env, &envsize, buf);
1303 }
1304 if (debug_flag) {
1305 /* dump the environment */
1306 fprintf(stderr, "Environment:\n");
1307 for (i = 0; env[i]; i++)
1308 fprintf(stderr, " %.200s\n", env[i]);
1309 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001310 return env;
1311}
1312
1313/*
1314 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1315 * first in this order).
1316 */
1317static void
1318do_rc_files(Session *s, const char *shell)
1319{
1320 FILE *f = NULL;
1321 char cmd[1024];
1322 int do_xauth;
1323 struct stat st;
1324
1325 do_xauth =
1326 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1327
Damien Millera1b48cc2008-03-27 11:02:02 +11001328 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
Damien Miller55360e12008-03-27 11:02:27 +11001329 if (!s->is_subsystem && options.adm_forced_command == NULL &&
Damien Millerff0dd882008-05-19 14:55:02 +10001330 !no_user_rc && stat(_PATH_SSH_USER_RC, &st) >= 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001331 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1332 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1333 if (debug_flag)
1334 fprintf(stderr, "Running %s\n", cmd);
1335 f = popen(cmd, "w");
1336 if (f) {
1337 if (do_xauth)
1338 fprintf(f, "%s %s\n", s->auth_proto,
1339 s->auth_data);
1340 pclose(f);
1341 } else
1342 fprintf(stderr, "Could not run %s\n",
1343 _PATH_SSH_USER_RC);
1344 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1345 if (debug_flag)
1346 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1347 _PATH_SSH_SYSTEM_RC);
1348 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1349 if (f) {
1350 if (do_xauth)
1351 fprintf(f, "%s %s\n", s->auth_proto,
1352 s->auth_data);
1353 pclose(f);
1354 } else
1355 fprintf(stderr, "Could not run %s\n",
1356 _PATH_SSH_SYSTEM_RC);
1357 } else if (do_xauth && options.xauth_location != NULL) {
1358 /* Add authority data to .Xauthority if appropriate. */
1359 if (debug_flag) {
1360 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001361 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001362 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001363 fprintf(stderr,
1364 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001365 options.xauth_location, s->auth_display,
1366 s->auth_proto, s->auth_data);
1367 }
1368 snprintf(cmd, sizeof cmd, "%s -q -",
1369 options.xauth_location);
1370 f = popen(cmd, "w");
1371 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001372 fprintf(f, "remove %s\n",
1373 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001374 fprintf(f, "add %s %s %s\n",
1375 s->auth_display, s->auth_proto,
1376 s->auth_data);
1377 pclose(f);
1378 } else {
1379 fprintf(stderr, "Could not run %s\n",
1380 cmd);
1381 }
1382 }
1383}
1384
1385static void
1386do_nologin(struct passwd *pw)
1387{
1388 FILE *f = NULL;
Darren Tucker09aa4c02010-01-12 19:51:48 +11001389 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
1390 struct stat sb;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001391
1392#ifdef HAVE_LOGIN_CAP
Damien Miller29cd1882012-04-22 11:08:10 +10001393 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
Darren Tucker09aa4c02010-01-12 19:51:48 +11001394 return;
1395 nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001396#else
Darren Tucker09aa4c02010-01-12 19:51:48 +11001397 if (pw->pw_uid == 0)
1398 return;
1399 nl = def_nl;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001400#endif
Darren Tucker09aa4c02010-01-12 19:51:48 +11001401 if (stat(nl, &sb) == -1) {
1402 if (nl != def_nl)
1403 xfree(nl);
1404 return;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001405 }
Darren Tucker09aa4c02010-01-12 19:51:48 +11001406
1407 /* /etc/nologin exists. Print its contents if we can and exit. */
1408 logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
1409 if ((f = fopen(nl, "r")) != NULL) {
1410 while (fgets(buf, sizeof(buf), f))
1411 fputs(buf, stderr);
1412 fclose(f);
1413 }
1414 exit(254);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001415}
1416
Damien Millerd8cb1f12008-02-10 22:40:12 +11001417/*
1418 * Chroot into a directory after checking it for safety: all path components
1419 * must be root-owned directories with strict permissions.
1420 */
1421static void
1422safely_chroot(const char *path, uid_t uid)
1423{
1424 const char *cp;
1425 char component[MAXPATHLEN];
1426 struct stat st;
1427
1428 if (*path != '/')
1429 fatal("chroot path does not begin at root");
1430 if (strlen(path) >= sizeof(component))
1431 fatal("chroot path too long");
1432
1433 /*
1434 * Descend the path, checking that each component is a
1435 * root-owned directory with strict permissions.
1436 */
1437 for (cp = path; cp != NULL;) {
1438 if ((cp = strchr(cp, '/')) == NULL)
1439 strlcpy(component, path, sizeof(component));
1440 else {
1441 cp++;
1442 memcpy(component, path, cp - path);
1443 component[cp - path] = '\0';
1444 }
1445
1446 debug3("%s: checking '%s'", __func__, component);
1447
1448 if (stat(component, &st) != 0)
1449 fatal("%s: stat(\"%s\"): %s", __func__,
1450 component, strerror(errno));
1451 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1452 fatal("bad ownership or modes for chroot "
1453 "directory %s\"%s\"",
1454 cp == NULL ? "" : "component ", component);
1455 if (!S_ISDIR(st.st_mode))
1456 fatal("chroot path %s\"%s\" is not a directory",
1457 cp == NULL ? "" : "component ", component);
1458
1459 }
1460
1461 if (chdir(path) == -1)
1462 fatal("Unable to chdir to chroot path \"%s\": "
1463 "%s", path, strerror(errno));
1464 if (chroot(path) == -1)
1465 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1466 if (chdir("/") == -1)
1467 fatal("%s: chdir(/) after chroot: %s",
1468 __func__, strerror(errno));
1469 verbose("Changed root directory to \"%s\"", path);
1470}
1471
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001472/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001474do_setusercontext(struct passwd *pw)
1475{
Damien Miller54e37732008-02-10 22:48:55 +11001476 char *chroot_path, *tmp;
1477
Darren Tucker97528352010-11-05 12:03:05 +11001478 platform_setusercontext(pw);
1479
Darren Tuckerb12fe272010-11-05 14:47:01 +11001480 if (platform_privileged_uidswap()) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001481#ifdef HAVE_LOGIN_CAP
1482 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001483 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001484 perror("unable to set user context");
1485 exit(1);
1486 }
1487#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001488 if (setlogin(pw->pw_name) < 0)
1489 error("setlogin failed: %s", strerror(errno));
1490 if (setgid(pw->pw_gid) < 0) {
1491 perror("setgid");
1492 exit(1);
1493 }
1494 /* Initialize the group list. */
1495 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1496 perror("initgroups");
1497 exit(1);
1498 }
1499 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001500#endif
1501
Darren Tucker920612e2010-11-05 12:36:15 +11001502 platform_setusercontext_post_groups(pw);
Damien Miller8b906422010-03-26 11:04:09 +11001503
Damien Millerd8cb1f12008-02-10 22:40:12 +11001504 if (options.chroot_directory != NULL &&
1505 strcasecmp(options.chroot_directory, "none") != 0) {
Damien Miller54e37732008-02-10 22:48:55 +11001506 tmp = tilde_expand_filename(options.chroot_directory,
1507 pw->pw_uid);
1508 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1509 "u", pw->pw_name, (char *)NULL);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001510 safely_chroot(chroot_path, pw->pw_uid);
Damien Miller54e37732008-02-10 22:48:55 +11001511 free(tmp);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001512 free(chroot_path);
1513 }
1514
1515#ifdef HAVE_LOGIN_CAP
1516 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1517 perror("unable to set user context (setuser)");
1518 exit(1);
1519 }
1520#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001521 /* Permanently switch to the desired uid. */
1522 permanently_set_uid(pw);
1523#endif
1524 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001525
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001526 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1527 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1528}
1529
Ben Lindstrom08105192002-03-22 02:50:06 +00001530static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001531do_pwchange(Session *s)
1532{
Darren Tucker09991742004-07-17 17:05:14 +10001533 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001534 fprintf(stderr, "WARNING: Your password has expired.\n");
1535 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001536 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001537 "You must change your password now and login again!\n");
Damien Miller14684a12011-05-20 11:23:07 +10001538#ifdef WITH_SELINUX
1539 setexeccon(NULL);
1540#endif
Darren Tucker33370e02005-02-09 22:17:28 +11001541#ifdef PASSWD_NEEDS_USERNAME
1542 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1543 (char *)NULL);
1544#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001545 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001546#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001547 perror("passwd");
1548 } else {
1549 fprintf(stderr,
1550 "Password change required but no TTY available.\n");
1551 }
1552 exit(1);
1553}
1554
1555static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001556launch_login(struct passwd *pw, const char *hostname)
1557{
1558 /* Launch login(1). */
1559
Ben Lindstrom378a4172002-06-07 14:49:56 +00001560 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001561#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001562 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001563#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001564#ifdef LOGIN_NO_ENDOPT
1565 "-p", "-f", pw->pw_name, (char *)NULL);
1566#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001567 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001568#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001569
1570 /* Login couldn't be executed, die. */
1571
1572 perror("login");
1573 exit(1);
1574}
1575
Darren Tucker23bc8d02004-02-06 16:24:31 +11001576static void
1577child_close_fds(void)
1578{
Darren Tucker23bc8d02004-02-06 16:24:31 +11001579 if (packet_get_connection_in() == packet_get_connection_out())
1580 close(packet_get_connection_in());
1581 else {
1582 close(packet_get_connection_in());
1583 close(packet_get_connection_out());
1584 }
1585 /*
1586 * Close all descriptors related to channels. They will still remain
1587 * open in the parent.
1588 */
1589 /* XXX better use close-on-exec? -markus */
1590 channel_close_all();
1591
1592 /*
1593 * Close any extra file descriptors. Note that there may still be
1594 * descriptors left by system functions. They will be closed later.
1595 */
1596 endpwent();
1597
1598 /*
Damien Miller788f2122005-11-05 15:14:59 +11001599 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001600 * hanging around in clients. Note that we want to do this after
1601 * initgroups, because at least on Solaris 2.3 it leaves file
1602 * descriptors open.
1603 */
Damien Millerf80c3de2010-12-01 12:02:59 +11001604 closefrom(STDERR_FILENO + 1);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001605}
1606
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001607/*
1608 * Performs common processing for the child, such as setting up the
1609 * environment, closing extra file descriptors, setting the user and group
1610 * ids, and executing the command or shell.
1611 */
Damien Millerdfc24252008-02-10 22:29:40 +11001612#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001613void
1614do_child(Session *s, const char *command)
1615{
1616 extern char **environ;
1617 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001618 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001619 const char *shell, *shell0, *hostname = NULL;
1620 struct passwd *pw = s->pw;
Damien Miller6051c942008-06-16 07:53:16 +10001621 int r = 0;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001622
1623 /* remove hostkey from the child's memory */
1624 destroy_sensitive_data();
1625
Darren Tucker23bc8d02004-02-06 16:24:31 +11001626 /* Force a password change */
1627 if (s->authctxt->force_pwchange) {
1628 do_setusercontext(pw);
1629 child_close_fds();
1630 do_pwchange(s);
1631 exit(1);
1632 }
1633
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001634 /* login(1) is only called if we execute the login shell */
1635 if (options.use_login && command != NULL)
1636 options.use_login = 0;
1637
Tim Rice81ed5182002-09-25 17:38:46 -07001638#ifdef _UNICOS
1639 cray_setup(pw->pw_uid, pw->pw_name, command);
1640#endif /* _UNICOS */
1641
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001642 /*
1643 * Login(1) does this as well, and it needs uid 0 for the "-h"
1644 * switch, so we let login(1) to this for us.
1645 */
1646 if (!options.use_login) {
1647#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001648 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001649 if (!check_quietlogin(s, command))
1650 do_motd();
1651#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001652 /* When PAM is enabled we rely on it to do the nologin check */
1653 if (!options.use_pam)
1654 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001655 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001656 /*
1657 * PAM session modules in do_setusercontext may have
1658 * generated messages, so if this in an interactive
1659 * login then display them too.
1660 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001661 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001662 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001663#endif /* HAVE_OSF_SIA */
1664 }
1665
Darren Tucker69687f42004-09-11 22:17:26 +10001666#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001667 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1668 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001669 display_loginmsg();
1670 exit(254);
1671 }
1672#endif
1673
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001674 /*
1675 * Get the shell from the password data. An empty shell field is
1676 * legal, and means /bin/sh.
1677 */
1678 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001679
1680 /*
1681 * Make sure $SHELL points to the shell from the password file,
1682 * even if shell is overridden from login.conf
1683 */
1684 env = do_setup_env(s, shell);
1685
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001686#ifdef HAVE_LOGIN_CAP
1687 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1688#endif
1689
Damien Millerad833b32000-08-23 10:46:23 +10001690 /* we have to stash the hostname before we close our socket. */
1691 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001692 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001693 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001694 /*
1695 * Close the connection descriptors; note that this is the child, and
1696 * the server will still have the socket open, and it is important
1697 * that we do not shutdown it. Note that the descriptors cannot be
1698 * closed before building the environment, as we call
1699 * get_remote_ipaddr there.
1700 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001701 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001702
Damien Millerb38eff82000-04-01 11:09:21 +10001703 /*
Damien Miller05eda432002-02-10 18:32:28 +11001704 * Must take new environment into use so that .ssh/rc,
1705 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001706 */
1707 environ = env;
1708
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001709#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001710 /*
1711 * At this point, we check to see if AFS is active and if we have
1712 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1713 * if we can (and need to) extend the ticket into an AFS token. If
1714 * we don't do this, we run into potential problems if the user's
1715 * home directory is in AFS and it's not world-readable.
1716 */
1717
1718 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001719 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001720 char cell[64];
1721
1722 debug("Getting AFS token");
1723
1724 k_setpag();
1725
1726 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1727 krb5_afslog(s->authctxt->krb5_ctx,
1728 s->authctxt->krb5_fwd_ccache, cell, NULL);
1729
1730 krb5_afslog_home(s->authctxt->krb5_ctx,
1731 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1732 }
1733#endif
1734
Damien Miller788f2122005-11-05 15:14:59 +11001735 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001736 if (chdir(pw->pw_dir) < 0) {
Damien Miller6051c942008-06-16 07:53:16 +10001737 /* Suppress missing homedir warning for chroot case */
Damien Miller139d4cd2001-10-10 15:07:44 +10001738#ifdef HAVE_LOGIN_CAP
Damien Miller6051c942008-06-16 07:53:16 +10001739 r = login_getcapbool(lc, "requirehome", 0);
Damien Miller139d4cd2001-10-10 15:07:44 +10001740#endif
Damien Miller7aa46ec2010-06-26 09:37:57 +10001741 if (r || options.chroot_directory == NULL ||
1742 strcasecmp(options.chroot_directory, "none") == 0)
Damien Miller6051c942008-06-16 07:53:16 +10001743 fprintf(stderr, "Could not chdir to home "
1744 "directory %s: %s\n", pw->pw_dir,
1745 strerror(errno));
1746 if (r)
1747 exit(1);
Damien Miller139d4cd2001-10-10 15:07:44 +10001748 }
1749
Damien Millera1939002008-03-15 17:27:58 +11001750 closefrom(STDERR_FILENO + 1);
1751
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001752 if (!options.use_login)
1753 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001754
1755 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001756 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001757
Darren Tuckerd6b06a92010-01-08 17:09:11 +11001758 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1759 printf("This service allows sftp connections only.\n");
1760 fflush(NULL);
1761 exit(1);
1762 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
Damien Millerdfc24252008-02-10 22:29:40 +11001763 extern int optind, optreset;
1764 int i;
1765 char *p, *args;
1766
Darren Tuckerac46a912009-06-21 17:55:23 +10001767 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
Damien Millerd58f5602008-11-03 19:20:49 +11001768 args = xstrdup(command ? command : "sftp-server");
Damien Millerdfc24252008-02-10 22:29:40 +11001769 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1770 if (i < ARGV_MAX - 1)
1771 argv[i++] = p;
1772 argv[i] = NULL;
1773 optind = optreset = 1;
1774 __progname = argv[0];
Darren Tucker4d6656b2009-10-24 15:04:12 +11001775#ifdef WITH_SELINUX
1776 ssh_selinux_change_context("sftpd_t");
1777#endif
Damien Millerd8cb1f12008-02-10 22:40:12 +11001778 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001779 }
1780
Darren Tucker695ed392009-10-07 09:02:18 +11001781 fflush(NULL);
1782
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001783 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784 launch_login(pw, hostname);
1785 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001786 }
1787
1788 /* Get the last component of the shell name. */
1789 if ((shell0 = strrchr(shell, '/')) != NULL)
1790 shell0++;
1791 else
1792 shell0 = shell;
1793
Damien Millerb38eff82000-04-01 11:09:21 +10001794 /*
1795 * If we have no command, execute the shell. In this case, the shell
1796 * name to be passed in argv[0] is preceded by '-' to indicate that
1797 * this is a login shell.
1798 */
1799 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001800 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001801
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001802 /* Start the shell. Set initial character to '-'. */
1803 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001804
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001805 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1806 >= sizeof(argv0) - 1) {
1807 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001808 perror(shell);
1809 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001810 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001811
1812 /* Execute the shell. */
1813 argv[0] = argv0;
1814 argv[1] = NULL;
1815 execve(shell, argv, env);
1816
1817 /* Executing the shell failed. */
1818 perror(shell);
1819 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001820 }
1821 /*
1822 * Execute the command using the user's shell. This uses the -c
1823 * option to execute the command.
1824 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001825 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001826 argv[1] = "-c";
1827 argv[2] = (char *) command;
1828 argv[3] = NULL;
1829 execve(shell, argv, env);
1830 perror(shell);
1831 exit(1);
1832}
1833
Damien Miller7207f642008-05-19 15:34:50 +10001834void
1835session_unused(int id)
1836{
1837 debug3("%s: session id %d unused", __func__, id);
1838 if (id >= options.max_sessions ||
1839 id >= sessions_nalloc) {
1840 fatal("%s: insane session id %d (max %d nalloc %d)",
1841 __func__, id, options.max_sessions, sessions_nalloc);
1842 }
1843 bzero(&sessions[id], sizeof(*sessions));
1844 sessions[id].self = id;
1845 sessions[id].used = 0;
1846 sessions[id].chanid = -1;
1847 sessions[id].ptyfd = -1;
1848 sessions[id].ttyfd = -1;
1849 sessions[id].ptymaster = -1;
1850 sessions[id].x11_chanids = NULL;
1851 sessions[id].next_unused = sessions_first_unused;
1852 sessions_first_unused = id;
1853}
1854
Damien Millerb38eff82000-04-01 11:09:21 +10001855Session *
1856session_new(void)
1857{
Damien Miller7207f642008-05-19 15:34:50 +10001858 Session *s, *tmp;
1859
1860 if (sessions_first_unused == -1) {
1861 if (sessions_nalloc >= options.max_sessions)
1862 return NULL;
1863 debug2("%s: allocate (allocated %d max %d)",
1864 __func__, sessions_nalloc, options.max_sessions);
1865 tmp = xrealloc(sessions, sessions_nalloc + 1,
1866 sizeof(*sessions));
1867 if (tmp == NULL) {
1868 error("%s: cannot allocate %d sessions",
1869 __func__, sessions_nalloc + 1);
1870 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001871 }
Damien Miller7207f642008-05-19 15:34:50 +10001872 sessions = tmp;
1873 session_unused(sessions_nalloc++);
Damien Millerb38eff82000-04-01 11:09:21 +10001874 }
Damien Miller7207f642008-05-19 15:34:50 +10001875
1876 if (sessions_first_unused >= sessions_nalloc ||
1877 sessions_first_unused < 0) {
1878 fatal("%s: insane first_unused %d max %d nalloc %d",
1879 __func__, sessions_first_unused, options.max_sessions,
1880 sessions_nalloc);
Damien Millerb38eff82000-04-01 11:09:21 +10001881 }
Damien Miller7207f642008-05-19 15:34:50 +10001882
1883 s = &sessions[sessions_first_unused];
1884 if (s->used) {
1885 fatal("%s: session %d already used",
1886 __func__, sessions_first_unused);
1887 }
1888 sessions_first_unused = s->next_unused;
1889 s->used = 1;
1890 s->next_unused = -1;
1891 debug("session_new: session %d", s->self);
1892
1893 return s;
Damien Millerb38eff82000-04-01 11:09:21 +10001894}
1895
Ben Lindstrombba81212001-06-25 05:01:22 +00001896static void
Damien Millerb38eff82000-04-01 11:09:21 +10001897session_dump(void)
1898{
1899 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001900 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001901 Session *s = &sessions[i];
Damien Miller7207f642008-05-19 15:34:50 +10001902
1903 debug("dump: used %d next_unused %d session %d %p "
1904 "channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001905 s->used,
Damien Miller7207f642008-05-19 15:34:50 +10001906 s->next_unused,
Damien Millerb38eff82000-04-01 11:09:21 +10001907 s->self,
1908 s,
1909 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001910 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001911 }
1912}
1913
Damien Millerefb4afe2000-04-12 18:45:05 +10001914int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001915session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001916{
1917 Session *s = session_new();
1918 debug("session_open: channel %d", chanid);
1919 if (s == NULL) {
1920 error("no more sessions");
1921 return 0;
1922 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001923 s->authctxt = authctxt;
1924 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001925 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001926 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001927 debug("session_open: session %d: link with channel %d", s->self, chanid);
1928 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001929 return 1;
1930}
1931
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001932Session *
1933session_by_tty(char *tty)
1934{
1935 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001936 for (i = 0; i < sessions_nalloc; i++) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001937 Session *s = &sessions[i];
1938 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1939 debug("session_by_tty: session %d tty %s", i, tty);
1940 return s;
1941 }
1942 }
1943 debug("session_by_tty: unknown tty %.100s", tty);
1944 session_dump();
1945 return NULL;
1946}
1947
Ben Lindstrombba81212001-06-25 05:01:22 +00001948static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001949session_by_channel(int id)
1950{
1951 int i;
Damien Miller7207f642008-05-19 15:34:50 +10001952 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001953 Session *s = &sessions[i];
1954 if (s->used && s->chanid == id) {
Damien Miller7207f642008-05-19 15:34:50 +10001955 debug("session_by_channel: session %d channel %d",
1956 i, id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001957 return s;
1958 }
1959 }
1960 debug("session_by_channel: unknown channel %d", id);
1961 session_dump();
1962 return NULL;
1963}
1964
Ben Lindstrombba81212001-06-25 05:01:22 +00001965static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001966session_by_x11_channel(int id)
1967{
1968 int i, j;
1969
Damien Miller7207f642008-05-19 15:34:50 +10001970 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller2b9b0452005-07-17 17:19:24 +10001971 Session *s = &sessions[i];
1972
1973 if (s->x11_chanids == NULL || !s->used)
1974 continue;
1975 for (j = 0; s->x11_chanids[j] != -1; j++) {
1976 if (s->x11_chanids[j] == id) {
1977 debug("session_by_x11_channel: session %d "
1978 "channel %d", s->self, id);
1979 return s;
1980 }
1981 }
1982 }
1983 debug("session_by_x11_channel: unknown channel %d", id);
1984 session_dump();
1985 return NULL;
1986}
1987
1988static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001989session_by_pid(pid_t pid)
1990{
1991 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001992 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller7207f642008-05-19 15:34:50 +10001993 for (i = 0; i < sessions_nalloc; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001994 Session *s = &sessions[i];
1995 if (s->used && s->pid == pid)
1996 return s;
1997 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001998 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001999 session_dump();
2000 return NULL;
2001}
2002
Ben Lindstrombba81212001-06-25 05:01:22 +00002003static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002004session_window_change_req(Session *s)
2005{
2006 s->col = packet_get_int();
2007 s->row = packet_get_int();
2008 s->xpixel = packet_get_int();
2009 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002010 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10002011 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2012 return 1;
2013}
2014
Ben Lindstrombba81212001-06-25 05:01:22 +00002015static int
Damien Millerefb4afe2000-04-12 18:45:05 +10002016session_pty_req(Session *s)
2017{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002018 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00002019 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002020
Ben Lindstrom49c12602001-06-13 04:37:36 +00002021 if (no_pty_flag) {
2022 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10002023 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002024 }
2025 if (s->ttyfd != -1) {
2026 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10002027 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00002028 }
2029
Damien Millerefb4afe2000-04-12 18:45:05 +10002030 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002031
2032 if (compat20) {
2033 s->col = packet_get_int();
2034 s->row = packet_get_int();
2035 } else {
2036 s->row = packet_get_int();
2037 s->col = packet_get_int();
2038 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002039 s->xpixel = packet_get_int();
2040 s->ypixel = packet_get_int();
2041
2042 if (strcmp(s->term, "") == 0) {
2043 xfree(s->term);
2044 s->term = NULL;
2045 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00002046
Damien Millerefb4afe2000-04-12 18:45:05 +10002047 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00002048 debug("Allocating pty.");
Damien Miller7207f642008-05-19 15:34:50 +10002049 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2050 sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00002051 if (s->term)
2052 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10002053 s->term = NULL;
2054 s->ptyfd = -1;
2055 s->ttyfd = -1;
2056 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10002057 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002058 }
2059 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002060
2061 /* for SSH1 the tty modes length is not given */
2062 if (!compat20)
2063 n_bytes = packet_remaining();
2064 tty_parse_modes(s->ttyfd, &n_bytes);
2065
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002066 if (!use_privsep)
2067 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00002068
2069 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10002070 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2071
Damien Miller48b03fc2002-01-22 23:11:40 +11002072 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10002073 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002074 return 1;
2075}
2076
Ben Lindstrombba81212001-06-25 05:01:22 +00002077static int
Damien Millerbd483e72000-04-30 10:00:53 +10002078session_subsystem_req(Session *s)
2079{
Damien Millerae452462001-10-10 15:08:06 +10002080 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002081 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10002082 int success = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10002083 char *prog, *cmd, *subsys = packet_get_string(&len);
Damien Millereccb9de2005-06-17 12:59:34 +10002084 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10002085
Damien Miller48b03fc2002-01-22 23:11:40 +11002086 packet_check_eom();
Damien Miller1b2b61e2010-06-26 09:47:43 +10002087 logit("subsystem request for %.100s by user %s", subsys,
2088 s->pw->pw_name);
Damien Millerbd483e72000-04-30 10:00:53 +10002089
Damien Millerf6d9e222000-06-18 14:50:44 +10002090 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10002091 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10002092 prog = options.subsystem_command[i];
2093 cmd = options.subsystem_args[i];
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002094 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
Damien Millerdfc24252008-02-10 22:29:40 +11002095 s->is_subsystem = SUBSYSTEM_INT_SFTP;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002096 debug("subsystem: %s", prog);
Damien Millerdfc24252008-02-10 22:29:40 +11002097 } else {
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002098 if (stat(prog, &st) < 0)
2099 debug("subsystem: cannot stat %s: %s",
2100 prog, strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11002101 s->is_subsystem = SUBSYSTEM_EXT;
Darren Tuckerc3dc4042010-01-08 17:09:50 +11002102 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10002103 }
Damien Miller7207f642008-05-19 15:34:50 +10002104 success = do_exec(s, cmd) == 0;
Damien Miller5fab4b92002-02-05 12:15:07 +11002105 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10002106 }
2107 }
2108
2109 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10002110 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10002111 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10002112
Damien Millerbd483e72000-04-30 10:00:53 +10002113 xfree(subsys);
2114 return success;
2115}
2116
Ben Lindstrombba81212001-06-25 05:01:22 +00002117static int
Damien Millerbd483e72000-04-30 10:00:53 +10002118session_x11_req(Session *s)
2119{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002120 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002121
Damien Miller2b9b0452005-07-17 17:19:24 +10002122 if (s->auth_proto != NULL || s->auth_data != NULL) {
2123 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002124 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002125 return 0;
2126 }
Damien Millerbd483e72000-04-30 10:00:53 +10002127 s->single_connection = packet_get_char();
2128 s->auth_proto = packet_get_string(NULL);
2129 s->auth_data = packet_get_string(NULL);
2130 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002131 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002132
Ben Lindstrom768176b2001-06-09 01:29:12 +00002133 success = session_setup_x11fwd(s);
2134 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10002135 xfree(s->auth_proto);
2136 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002137 s->auth_proto = NULL;
2138 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002139 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002140 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002141}
2142
Ben Lindstrombba81212001-06-25 05:01:22 +00002143static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002144session_shell_req(Session *s)
2145{
Damien Miller48b03fc2002-01-22 23:11:40 +11002146 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002147 return do_exec(s, NULL) == 0;
Damien Millerf6d9e222000-06-18 14:50:44 +10002148}
2149
Ben Lindstrombba81212001-06-25 05:01:22 +00002150static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002151session_exec_req(Session *s)
2152{
Damien Miller7207f642008-05-19 15:34:50 +10002153 u_int len, success;
2154
Damien Millerf6d9e222000-06-18 14:50:44 +10002155 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002156 packet_check_eom();
Damien Miller7207f642008-05-19 15:34:50 +10002157 success = do_exec(s, command) == 0;
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00002158 xfree(command);
Damien Miller7207f642008-05-19 15:34:50 +10002159 return success;
Damien Millerf6d9e222000-06-18 14:50:44 +10002160}
2161
Ben Lindstrombba81212001-06-25 05:01:22 +00002162static int
Damien Miller54c45982003-05-15 10:20:13 +10002163session_break_req(Session *s)
2164{
Damien Miller54c45982003-05-15 10:20:13 +10002165
Darren Tucker1f8311c2004-05-13 16:39:33 +10002166 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002167 packet_check_eom();
2168
Darren Tucker9c5d5532011-11-04 10:55:24 +11002169 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002170 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002171 return 1;
2172}
2173
2174static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002175session_env_req(Session *s)
2176{
2177 char *name, *val;
2178 u_int name_len, val_len, i;
2179
2180 name = packet_get_string(&name_len);
2181 val = packet_get_string(&val_len);
2182 packet_check_eom();
2183
2184 /* Don't set too many environment variables */
2185 if (s->num_env > 128) {
2186 debug2("Ignoring env request %s: too many env vars", name);
2187 goto fail;
2188 }
2189
2190 for (i = 0; i < options.num_accept_env; i++) {
2191 if (match_pattern(name, options.accept_env[i])) {
2192 debug2("Setting env %d: %s=%s", s->num_env, name, val);
Damien Miller36812092006-03-26 14:22:47 +11002193 s->env = xrealloc(s->env, s->num_env + 1,
2194 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002195 s->env[s->num_env].name = name;
2196 s->env[s->num_env].val = val;
2197 s->num_env++;
2198 return (1);
2199 }
2200 }
2201 debug2("Ignoring env request %s: disallowed name", name);
2202
2203 fail:
2204 xfree(name);
2205 xfree(val);
2206 return (0);
2207}
2208
2209static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002210session_auth_agent_req(Session *s)
2211{
2212 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002213 packet_check_eom();
Damien Miller4f755cd2008-05-19 14:57:41 +10002214 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
Ben Lindstrom14920292000-11-21 21:24:55 +00002215 debug("session_auth_agent_req: no_agent_forwarding_flag");
2216 return 0;
2217 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002218 if (called) {
2219 return 0;
2220 } else {
2221 called = 1;
2222 return auth_input_request_forwarding(s->pw);
2223 }
2224}
2225
Damien Millerc7ef63d2002-02-05 12:21:42 +11002226int
2227session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002228{
Damien Millerefb4afe2000-04-12 18:45:05 +10002229 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002230 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002231
Damien Millerc7ef63d2002-02-05 12:21:42 +11002232 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002233 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002234 c->self, rtype);
2235 return 0;
2236 }
2237 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002238
2239 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002240 * a session is in LARVAL state until a shell, a command
2241 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002242 */
2243 if (c->type == SSH_CHANNEL_LARVAL) {
2244 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002245 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002246 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002247 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002248 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002249 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002250 } else if (strcmp(rtype, "x11-req") == 0) {
2251 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002252 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2253 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002254 } else if (strcmp(rtype, "subsystem") == 0) {
2255 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002256 } else if (strcmp(rtype, "env") == 0) {
2257 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002258 }
2259 }
2260 if (strcmp(rtype, "window-change") == 0) {
2261 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002262 } else if (strcmp(rtype, "break") == 0) {
2263 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002264 }
Damien Millera6b1d162004-06-30 22:41:07 +10002265
Damien Millerc7ef63d2002-02-05 12:21:42 +11002266 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002267}
2268
2269void
Damien Miller8853ca52010-06-26 10:00:14 +10002270session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
2271 int is_tty)
Damien Millerefb4afe2000-04-12 18:45:05 +10002272{
2273 if (!compat20)
2274 fatal("session_set_fds: called for proto != 2.0");
2275 /*
2276 * now that have a child and a pipe to the child,
2277 * we can activate our channel and register the fd's
2278 */
2279 if (s->chanid == -1)
2280 fatal("no channel for session %d", s->self);
2281 channel_set_fds(s->chanid,
2282 fdout, fdin, fderr,
Damien Miller8853ca52010-06-26 10:00:14 +10002283 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Darren Tuckered3cdc02008-06-16 23:29:18 +10002284 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002285}
2286
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002287/*
2288 * Function to perform pty cleanup. Also called if we get aborted abnormally
2289 * (e.g., due to a dropped connection).
2290 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002291void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002292session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002293{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002294 if (s == NULL) {
2295 error("session_pty_cleanup: no session");
2296 return;
2297 }
2298 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002299 return;
2300
Kevin Steves43cdef32001-02-11 14:12:08 +00002301 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002302
Damien Millerb38eff82000-04-01 11:09:21 +10002303 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002304 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002305 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002306
2307 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002308 if (getuid() == 0)
2309 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002310
2311 /*
2312 * Close the server side of the socket pairs. We must do this after
2313 * the pty cleanup, so that another process doesn't get this pty
2314 * while we're still cleaning up.
2315 */
Damien Miller7207f642008-05-19 15:34:50 +10002316 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2317 error("close(s->ptymaster/%d): %s",
2318 s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002319
2320 /* unlink pty from session */
2321 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002322}
Damien Millerefb4afe2000-04-12 18:45:05 +10002323
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002324void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002325session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002326{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002327 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002328}
2329
Damien Miller5a80bba2002-09-04 16:39:02 +10002330static char *
2331sig2name(int sig)
2332{
2333#define SSH_SIG(x) if (sig == SIG ## x) return #x
2334 SSH_SIG(ABRT);
2335 SSH_SIG(ALRM);
2336 SSH_SIG(FPE);
2337 SSH_SIG(HUP);
2338 SSH_SIG(ILL);
2339 SSH_SIG(INT);
2340 SSH_SIG(KILL);
2341 SSH_SIG(PIPE);
2342 SSH_SIG(QUIT);
2343 SSH_SIG(SEGV);
2344 SSH_SIG(TERM);
2345 SSH_SIG(USR1);
2346 SSH_SIG(USR2);
2347#undef SSH_SIG
2348 return "SIG@openssh.com";
2349}
2350
Ben Lindstrombba81212001-06-25 05:01:22 +00002351static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002352session_close_x11(int id)
2353{
2354 Channel *c;
2355
Damien Millerd47c62a2005-12-13 19:33:57 +11002356 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002357 debug("session_close_x11: x11 channel %d missing", id);
2358 } else {
2359 /* Detach X11 listener */
2360 debug("session_close_x11: detach x11 channel %d", id);
2361 channel_cancel_cleanup(id);
2362 if (c->ostate != CHAN_OUTPUT_CLOSED)
2363 chan_mark_dead(c);
2364 }
2365}
2366
2367static void
2368session_close_single_x11(int id, void *arg)
2369{
2370 Session *s;
2371 u_int i;
2372
2373 debug3("session_close_single_x11: channel %d", id);
2374 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002375 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002376 fatal("session_close_single_x11: no x11 channel %d", id);
2377 for (i = 0; s->x11_chanids[i] != -1; i++) {
2378 debug("session_close_single_x11: session %d: "
2379 "closing channel %d", s->self, s->x11_chanids[i]);
2380 /*
2381 * The channel "id" is already closing, but make sure we
2382 * close all of its siblings.
2383 */
2384 if (s->x11_chanids[i] != id)
2385 session_close_x11(s->x11_chanids[i]);
2386 }
2387 xfree(s->x11_chanids);
2388 s->x11_chanids = NULL;
2389 if (s->display) {
2390 xfree(s->display);
2391 s->display = NULL;
2392 }
2393 if (s->auth_proto) {
2394 xfree(s->auth_proto);
2395 s->auth_proto = NULL;
2396 }
2397 if (s->auth_data) {
2398 xfree(s->auth_data);
2399 s->auth_data = NULL;
2400 }
2401 if (s->auth_display) {
2402 xfree(s->auth_display);
2403 s->auth_display = NULL;
2404 }
2405}
2406
2407static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002408session_exit_message(Session *s, int status)
2409{
2410 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002411
2412 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002413 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002414 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002415 debug("session_exit_message: session %d channel %d pid %ld",
2416 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002417
2418 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002419 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002420 packet_put_int(WEXITSTATUS(status));
2421 packet_send();
2422 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002423 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002424 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002425#ifdef WCOREDUMP
Damien Miller767087b2008-03-07 18:32:42 +11002426 packet_put_char(WCOREDUMP(status)? 1 : 0);
Damien Millerf3c6cf12000-05-17 22:08:29 +10002427#else /* WCOREDUMP */
2428 packet_put_char(0);
2429#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002430 packet_put_cstring("");
2431 packet_put_cstring("");
2432 packet_send();
2433 } else {
2434 /* Some weird exit cause. Just exit. */
2435 packet_disconnect("wait returned status %04x.", status);
2436 }
2437
2438 /* disconnect channel */
2439 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002440
2441 /*
2442 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002443 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002444 * by session_close_by_channel when the childs close their fds.
2445 */
2446 channel_register_cleanup(c->self, session_close_by_channel, 1);
2447
Damien Miller166fca82000-04-20 07:42:21 +10002448 /*
2449 * emulate a write failure with 'chan_write_failed', nobody will be
2450 * interested in data we write.
2451 * Note that we must not call 'chan_read_failed', since there could
2452 * be some more data waiting in the pipe.
2453 */
Damien Millerbd483e72000-04-30 10:00:53 +10002454 if (c->ostate != CHAN_OUTPUT_CLOSED)
2455 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002456}
2457
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002458void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002459session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002460{
Damien Millereccb9de2005-06-17 12:59:34 +10002461 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002462
Ben Lindstromce0f6342002-06-11 16:42:49 +00002463 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002464 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002465 session_pty_cleanup(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002466 if (s->term)
2467 xfree(s->term);
2468 if (s->display)
2469 xfree(s->display);
Damien Miller2b9b0452005-07-17 17:19:24 +10002470 if (s->x11_chanids)
2471 xfree(s->x11_chanids);
Damien Miller512bccb2002-02-05 12:11:02 +11002472 if (s->auth_display)
2473 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10002474 if (s->auth_data)
2475 xfree(s->auth_data);
2476 if (s->auth_proto)
2477 xfree(s->auth_proto);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002478 if (s->env != NULL) {
2479 for (i = 0; i < s->num_env; i++) {
2480 xfree(s->env[i].name);
2481 xfree(s->env[i].val);
2482 }
Darren Tucker46bc0752004-05-02 22:11:30 +10002483 xfree(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002484 }
Damien Millere247cc42000-05-07 12:03:14 +10002485 session_proctitle(s);
Damien Miller7207f642008-05-19 15:34:50 +10002486 session_unused(s->self);
Damien Millerefb4afe2000-04-12 18:45:05 +10002487}
2488
2489void
2490session_close_by_pid(pid_t pid, int status)
2491{
2492 Session *s = session_by_pid(pid);
2493 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002494 debug("session_close_by_pid: no session for pid %ld",
2495 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002496 return;
2497 }
2498 if (s->chanid != -1)
2499 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002500 if (s->ttyfd != -1)
2501 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002502 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002503}
2504
2505/*
2506 * this is called when a channel dies before
2507 * the session 'child' itself dies
2508 */
2509void
2510session_close_by_channel(int id, void *arg)
2511{
2512 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002513 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002514
Damien Millerefb4afe2000-04-12 18:45:05 +10002515 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002516 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002517 return;
2518 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002519 debug("session_close_by_channel: channel %d child %ld",
2520 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002521 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002522 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002523 /*
2524 * delay detach of session, but release pty, since
2525 * the fd's to the child are already closed
2526 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002527 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002528 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002529 return;
2530 }
2531 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002532 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002533
2534 /* Close any X11 listeners associated with this session */
2535 if (s->x11_chanids != NULL) {
2536 for (i = 0; s->x11_chanids[i] != -1; i++) {
2537 session_close_x11(s->x11_chanids[i]);
2538 s->x11_chanids[i] = -1;
2539 }
2540 }
2541
Damien Millerefb4afe2000-04-12 18:45:05 +10002542 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002543 session_close(s);
2544}
2545
2546void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002547session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002548{
2549 int i;
Damien Miller7207f642008-05-19 15:34:50 +10002550 for (i = 0; i < sessions_nalloc; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002551 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002552 if (s->used) {
2553 if (closefunc != NULL)
2554 closefunc(s);
2555 else
2556 session_close(s);
2557 }
Damien Miller52b77be2001-10-10 15:14:37 +10002558 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002559}
2560
Ben Lindstrombba81212001-06-25 05:01:22 +00002561static char *
Damien Millere247cc42000-05-07 12:03:14 +10002562session_tty_list(void)
2563{
2564 static char buf[1024];
2565 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002566 char *cp;
2567
Damien Millere247cc42000-05-07 12:03:14 +10002568 buf[0] = '\0';
Damien Miller7207f642008-05-19 15:34:50 +10002569 for (i = 0; i < sessions_nalloc; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002570 Session *s = &sessions[i];
2571 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002572
Damien Millera8ed44b2003-01-10 09:53:12 +11002573 if (strncmp(s->tty, "/dev/", 5) != 0) {
2574 cp = strrchr(s->tty, '/');
2575 cp = (cp == NULL) ? s->tty : cp + 1;
2576 } else
2577 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002578
Damien Millere247cc42000-05-07 12:03:14 +10002579 if (buf[0] != '\0')
2580 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002581 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002582 }
2583 }
2584 if (buf[0] == '\0')
2585 strlcpy(buf, "notty", sizeof buf);
2586 return buf;
2587}
2588
2589void
2590session_proctitle(Session *s)
2591{
2592 if (s->pw == NULL)
2593 error("no user for session %d", s->self);
2594 else
2595 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2596}
2597
Ben Lindstrom768176b2001-06-09 01:29:12 +00002598int
2599session_setup_x11fwd(Session *s)
2600{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002601 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002602 char display[512], auth_display[512];
2603 char hostname[MAXHOSTNAMELEN];
Damien Miller2b9b0452005-07-17 17:19:24 +10002604 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002605
2606 if (no_x11_forwarding_flag) {
2607 packet_send_debug("X11 forwarding disabled in user configuration file.");
2608 return 0;
2609 }
2610 if (!options.x11_forwarding) {
2611 debug("X11 forwarding disabled in server configuration file.");
2612 return 0;
2613 }
2614 if (!options.xauth_location ||
2615 (stat(options.xauth_location, &st) == -1)) {
2616 packet_send_debug("No xauth program; cannot forward with spoofing.");
2617 return 0;
2618 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002619 if (options.use_login) {
2620 packet_send_debug("X11 forwarding disabled; "
2621 "not compatible with UseLogin=yes.");
2622 return 0;
2623 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002624 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002625 debug("X11 display already set.");
2626 return 0;
2627 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002628 if (x11_create_display_inet(options.x11_display_offset,
2629 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002630 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002631 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002632 return 0;
2633 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002634 for (i = 0; s->x11_chanids[i] != -1; i++) {
2635 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002636 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002637 }
Kevin Steves366298c2001-12-19 17:58:01 +00002638
2639 /* Set up a suitable value for the DISPLAY variable. */
2640 if (gethostname(hostname, sizeof(hostname)) < 0)
2641 fatal("gethostname: %.100s", strerror(errno));
2642 /*
2643 * auth_display must be used as the displayname when the
2644 * authorization entry is added with xauth(1). This will be
2645 * different than the DISPLAY string for localhost displays.
2646 */
Damien Miller95c249f2002-02-05 12:11:34 +11002647 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002648 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002649 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002650 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002651 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002652 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002653 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002654 } else {
2655#ifdef IPADDR_IN_DISPLAY
2656 struct hostent *he;
2657 struct in_addr my_addr;
2658
2659 he = gethostbyname(hostname);
2660 if (he == NULL) {
2661 error("Can't get IP address for X11 DISPLAY.");
2662 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2663 return 0;
2664 }
2665 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002666 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002667 s->display_number, s->screen);
2668#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002669 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002670 s->display_number, s->screen);
2671#endif
2672 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002673 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002674 }
2675
Ben Lindstrom768176b2001-06-09 01:29:12 +00002676 return 1;
2677}
2678
Ben Lindstrombba81212001-06-25 05:01:22 +00002679static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002680do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002681{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002682 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002683}
2684
2685void
2686do_cleanup(Authctxt *authctxt)
2687{
2688 static int called = 0;
2689
2690 debug("do_cleanup");
2691
2692 /* no cleanup if we're in the child for login shell */
2693 if (is_child)
2694 return;
2695
2696 /* avoid double cleanup */
2697 if (called)
2698 return;
2699 called = 1;
2700
Darren Tucker9142e1c2007-08-16 23:28:04 +10002701 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002702 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002703
2704#ifdef USE_PAM
2705 if (options.use_pam) {
2706 sshpam_cleanup();
2707 sshpam_thread_cleanup();
2708 }
2709#endif
2710
2711 if (!authctxt->authenticated)
2712 return;
2713
Darren Tucker3e33cec2003-10-02 16:12:36 +10002714#ifdef KRB5
2715 if (options.kerberos_ticket_cleanup &&
2716 authctxt->krb5_ctx)
2717 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002718#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002719
2720#ifdef GSSAPI
2721 if (compat20 && options.gss_cleanup_creds)
2722 ssh_gssapi_cleanup_creds();
2723#endif
2724
2725 /* remove agent socket */
2726 auth_sock_cleanup_proc(authctxt->pw);
2727
2728 /*
2729 * Cleanup ptys/utmp only if privsep is disabled,
2730 * or if running in monitor.
2731 */
2732 if (!use_privsep || mm_is_monitor())
2733 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002734}