blob: 1768c8c2f00c3581fef3d29db3d9fead11d9ec71 [file] [log] [blame]
Damien Millerd8cb1f12008-02-10 22:40:12 +11001/* $OpenBSD: session.c,v 1.226 2008/02/08 23:24:07 djm Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
Darren Tucker39972492006-07-12 22:22:46 +100049#include <errno.h>
Damien Miller427a1d52006-07-10 20:20:33 +100050#include <grp.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110051#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110052#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110053#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100054#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110055#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100056#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100057#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100058#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100059#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100060#include <unistd.h>
Damien Millerb38eff82000-04-01 11:09:21 +100061
Damien Millerd7834352006-08-05 12:39:39 +100062#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100063#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "ssh1.h"
65#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000066#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100067#include "packet.h"
68#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100069#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100070#include "uidswap.h"
71#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000072#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "key.h"
74#include "cipher.h"
75#ifdef GSSAPI
76#include "ssh-gss.h"
77#endif
78#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100079#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100080#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "pathnames.h"
82#include "log.h"
83#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000084#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "serverloop.h"
86#include "canohost.h"
Damien Millerd8cb1f12008-02-10 22:40:12 +110087#include "misc.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000088#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100089#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090#include "monitor_wrap.h"
Damien Millerdfc24252008-02-10 22:29:40 +110091#include "sftp.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100092
Darren Tucker3c78c5e2004-01-23 22:03:10 +110093#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110094#include <kafs.h>
95#endif
96
Damien Millerd8cb1f12008-02-10 22:40:12 +110097/* Magic name for internal sftp-server */
98#define INTERNAL_SFTP_NAME "internal-sftp"
99
Damien Millerb38eff82000-04-01 11:09:21 +1000100/* func */
101
102Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000103void session_set_fds(Session *, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000104void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000105void session_proctitle(Session *);
106int session_setup_x11fwd(Session *);
107void do_exec_pty(Session *, const char *);
108void do_exec_no_pty(Session *, const char *);
109void do_exec(Session *, const char *);
110void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000111#ifdef LOGIN_NEEDS_UTMPX
112static void do_pre_login(Session *s);
113#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000114void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000115void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000116int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000117
Ben Lindstrombba81212001-06-25 05:01:22 +0000118static void do_authenticated1(Authctxt *);
119static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000120
Ben Lindstrombba81212001-06-25 05:01:22 +0000121static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000122
Damien Millerb38eff82000-04-01 11:09:21 +1000123/* import */
124extern ServerOptions options;
125extern char *__progname;
126extern int log_stderr;
127extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000128extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000129extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000130extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000131extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000132
Damien Miller7b28dc52000-09-05 13:34:53 +1100133/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000134const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100135
Damien Millerb38eff82000-04-01 11:09:21 +1000136/* data */
Damien Millerd8cb1f12008-02-10 22:40:12 +1100137#define MAX_SESSIONS 20
Damien Millerb38eff82000-04-01 11:09:21 +1000138Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100139
Damien Millerdfc24252008-02-10 22:29:40 +1100140#define SUBSYSTEM_NONE 0
141#define SUBSYSTEM_EXT 1
142#define SUBSYSTEM_INT_SFTP 2
143
Damien Millerad833b32000-08-23 10:46:23 +1000144#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000145login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000146#endif
147
Darren Tucker3e33cec2003-10-02 16:12:36 +1000148static int is_child = 0;
149
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000150/* Name and directory of socket for authentication agent forwarding. */
151static char *auth_sock_name = NULL;
152static char *auth_sock_dir = NULL;
153
154/* removes the agent forwarding socket */
155
156static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000157auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000158{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000159 if (auth_sock_name != NULL) {
160 temporarily_use_uid(pw);
161 unlink(auth_sock_name);
162 rmdir(auth_sock_dir);
163 auth_sock_name = NULL;
164 restore_uid();
165 }
166}
167
168static int
169auth_input_request_forwarding(struct passwd * pw)
170{
171 Channel *nc;
172 int sock;
173 struct sockaddr_un sunaddr;
174
175 if (auth_sock_name != NULL) {
176 error("authentication forwarding requested twice.");
177 return 0;
178 }
179
180 /* Temporarily drop privileged uid for mkdir/bind. */
181 temporarily_use_uid(pw);
182
183 /* Allocate a buffer for the socket name, and format the name. */
184 auth_sock_name = xmalloc(MAXPATHLEN);
185 auth_sock_dir = xmalloc(MAXPATHLEN);
Darren Tucker072a7b12003-10-15 16:10:25 +1000186 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000187
188 /* Create private directory for socket */
189 if (mkdtemp(auth_sock_dir) == NULL) {
190 packet_send_debug("Agent forwarding disabled: "
191 "mkdtemp() failed: %.100s", strerror(errno));
192 restore_uid();
193 xfree(auth_sock_name);
194 xfree(auth_sock_dir);
195 auth_sock_name = NULL;
196 auth_sock_dir = NULL;
197 return 0;
198 }
Ben Lindstromce0f6342002-06-11 16:42:49 +0000199 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
200 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000201
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000202 /* Create the socket. */
203 sock = socket(AF_UNIX, SOCK_STREAM, 0);
204 if (sock < 0)
205 packet_disconnect("socket: %.100s", strerror(errno));
206
207 /* Bind it to the name. */
208 memset(&sunaddr, 0, sizeof(sunaddr));
209 sunaddr.sun_family = AF_UNIX;
210 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
211
Damien Miller90967402006-03-26 14:07:26 +1100212 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000213 packet_disconnect("bind: %.100s", strerror(errno));
214
215 /* Restore the privileged uid. */
216 restore_uid();
217
218 /* Start listening on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +1100219 if (listen(sock, SSH_LISTEN_BACKLOG) < 0)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000220 packet_disconnect("listen: %.100s", strerror(errno));
221
222 /* Allocate a channel for the authentication agent socket. */
223 nc = channel_new("auth socket",
224 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
225 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000226 0, "auth socket", 1);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000227 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
228 return 1;
229}
230
Darren Tucker1921ed92004-02-10 13:23:28 +1100231static void
232display_loginmsg(void)
233{
Damien Miller46d38de2005-07-17 17:02:09 +1000234 if (buffer_len(&loginmsg) > 0) {
235 buffer_append(&loginmsg, "\0", 1);
236 printf("%s", (char *)buffer_ptr(&loginmsg));
237 buffer_clear(&loginmsg);
238 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100239}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000240
Ben Lindstromb31783d2001-03-22 02:02:12 +0000241void
242do_authenticated(Authctxt *authctxt)
243{
Damien Miller97f39ae2003-02-24 11:57:01 +1100244 setproctitle("%s", authctxt->pw->pw_name);
245
Ben Lindstromb31783d2001-03-22 02:02:12 +0000246 /* setup the channel layer */
247 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
248 channel_permit_all_opens();
249
250 if (compat20)
251 do_authenticated2(authctxt);
252 else
253 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000254
Darren Tucker3e33cec2003-10-02 16:12:36 +1000255 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000256}
257
Damien Millerb38eff82000-04-01 11:09:21 +1000258/*
Damien Millerb38eff82000-04-01 11:09:21 +1000259 * Prepares for an interactive session. This is called after the user has
260 * been successfully authenticated. During this message exchange, pseudo
261 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
262 * are requested, etc.
263 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000264static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000265do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000266{
267 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000268 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100269 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000270 int enable_compression_after_reply = 0;
271 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000272
273 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100274 if (s == NULL) {
275 error("no more sessions");
276 return;
277 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000278 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000279 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000280
Damien Millerb38eff82000-04-01 11:09:21 +1000281 /*
282 * We stay in this loop until the client requests to execute a shell
283 * or a command.
284 */
285 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000286 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000287
288 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100289 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000290
291 /* Process the packet. */
292 switch (type) {
293 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000294 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100295 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000296 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000297 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100298 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000299 break;
300 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000301 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000302 debug2("compression disabled");
303 break;
304 }
Damien Millerb38eff82000-04-01 11:09:21 +1000305 /* Enable compression after we have responded with SUCCESS. */
306 enable_compression_after_reply = 1;
307 success = 1;
308 break;
309
310 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000311 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000312 break;
313
314 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000315 s->auth_proto = packet_get_string(&proto_len);
316 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000317
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000318 screen_flag = packet_get_protocol_flags() &
319 SSH_PROTOFLAG_SCREEN_NUMBER;
320 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
321
322 if (packet_remaining() == 4) {
323 if (!screen_flag)
324 debug2("Buggy client: "
325 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000326 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000327 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000328 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000329 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100330 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000331 success = session_setup_x11fwd(s);
332 if (!success) {
333 xfree(s->auth_proto);
334 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000335 s->auth_proto = NULL;
336 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000337 }
Damien Millerb38eff82000-04-01 11:09:21 +1000338 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000339
340 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
341 if (no_agent_forwarding_flag || compat13) {
342 debug("Authentication agent forwarding not permitted for this authentication.");
343 break;
344 }
345 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000346 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000347 break;
348
349 case SSH_CMSG_PORT_FORWARD_REQUEST:
350 if (no_port_forwarding_flag) {
351 debug("Port forwarding not permitted for this authentication.");
352 break;
353 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100354 if (!options.allow_tcp_forwarding) {
355 debug("Port forwarding not permitted.");
356 break;
357 }
Damien Millerb38eff82000-04-01 11:09:21 +1000358 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000359 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
360 options.gateway_ports) < 0) {
361 debug("Port forwarding failed.");
362 break;
363 }
Damien Millerb38eff82000-04-01 11:09:21 +1000364 success = 1;
365 break;
366
367 case SSH_CMSG_MAX_PACKET_SIZE:
368 if (packet_set_maxsize(packet_get_int()) > 0)
369 success = 1;
370 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100371
Damien Millerb38eff82000-04-01 11:09:21 +1000372 case SSH_CMSG_EXEC_SHELL:
373 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000374 if (type == SSH_CMSG_EXEC_CMD) {
375 command = packet_get_string(&dlen);
376 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000377 do_exec(s, command);
378 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000379 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000380 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000381 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100382 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000383 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000384 return;
385
386 default:
387 /*
388 * Any unknown messages in this phase are ignored,
389 * and a failure message is returned.
390 */
Damien Miller996acd22003-04-09 20:59:48 +1000391 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000392 }
393 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
394 packet_send();
395 packet_write_wait();
396
397 /* Enable compression now that we have replied if appropriate. */
398 if (enable_compression_after_reply) {
399 enable_compression_after_reply = 0;
400 packet_start_compression(compression_level);
401 }
402 }
403}
404
405/*
406 * This is called to fork and execute a command when we have no tty. This
407 * will call do_child from the child, and server_loop from the parent after
408 * setting up file descriptors and such.
409 */
Damien Miller4af51302000-04-16 11:18:38 +1000410void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000411do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000412{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000413 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000414
415#ifdef USE_PIPES
416 int pin[2], pout[2], perr[2];
417 /* Allocate pipes for communicating with the program. */
418 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
419 packet_disconnect("Could not create pipes: %.100s",
420 strerror(errno));
421#else /* USE_PIPES */
422 int inout[2], err[2];
423 /* Uses socket pairs to communicate with the program. */
424 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
425 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
426 packet_disconnect("Could not create socket pairs: %.100s",
427 strerror(errno));
428#endif /* USE_PIPES */
429 if (s == NULL)
430 fatal("do_exec_no_pty: no session");
431
Damien Millere247cc42000-05-07 12:03:14 +1000432 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000433
Damien Millerc5946332001-02-28 11:46:11 +1100434#if defined(USE_PAM)
Darren Tucker1825f262004-02-24 00:01:27 +1100435 if (options.use_pam && !use_privsep)
Damien Miller4e448a32003-05-14 15:11:48 +1000436 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000437#endif /* USE_PAM */
438
Damien Millerb38eff82000-04-01 11:09:21 +1000439 /* Fork the child. */
440 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000441 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000442
Damien Millerb38eff82000-04-01 11:09:21 +1000443 /* Child. Reinitialize the log since the pid has changed. */
444 log_init(__progname, options.log_level, options.log_facility, log_stderr);
445
446 /*
447 * Create a new session and process group since the 4.4BSD
448 * setlogin() affects the entire process group.
449 */
450 if (setsid() < 0)
451 error("setsid failed: %.100s", strerror(errno));
452
453#ifdef USE_PIPES
454 /*
455 * Redirect stdin. We close the parent side of the socket
456 * pair, and make the child side the standard input.
457 */
458 close(pin[1]);
459 if (dup2(pin[0], 0) < 0)
460 perror("dup2 stdin");
461 close(pin[0]);
462
463 /* Redirect stdout. */
464 close(pout[0]);
465 if (dup2(pout[1], 1) < 0)
466 perror("dup2 stdout");
467 close(pout[1]);
468
469 /* Redirect stderr. */
470 close(perr[0]);
471 if (dup2(perr[1], 2) < 0)
472 perror("dup2 stderr");
473 close(perr[1]);
474#else /* USE_PIPES */
475 /*
476 * Redirect stdin, stdout, and stderr. Stdin and stdout will
477 * use the same socket, as some programs (particularly rdist)
478 * seem to depend on it.
479 */
480 close(inout[1]);
481 close(err[1]);
482 if (dup2(inout[0], 0) < 0) /* stdin */
483 perror("dup2 stdin");
484 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
485 perror("dup2 stdout");
486 if (dup2(err[0], 2) < 0) /* stderr */
487 perror("dup2 stderr");
488#endif /* USE_PIPES */
489
Tim Rice81ed5182002-09-25 17:38:46 -0700490#ifdef _UNICOS
491 cray_init_job(s->pw); /* set up cray jid and tmpdir */
492#endif
493
Damien Millerb38eff82000-04-01 11:09:21 +1000494 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000495 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000496 /* NOTREACHED */
497 }
Tim Rice81ed5182002-09-25 17:38:46 -0700498#ifdef _UNICOS
499 signal(WJSIGNAL, cray_job_termination_handler);
500#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100501#ifdef HAVE_CYGWIN
502 if (is_winnt)
503 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
504#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000505 if (pid < 0)
506 packet_disconnect("fork failed: %.100s", strerror(errno));
507 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000508 /* Set interactive/non-interactive mode. */
509 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000510#ifdef USE_PIPES
511 /* We are the parent. Close the child sides of the pipes. */
512 close(pin[0]);
513 close(pout[1]);
514 close(perr[1]);
515
Damien Millerefb4afe2000-04-12 18:45:05 +1000516 if (compat20) {
Darren Tucker723e9452004-06-22 12:57:08 +1000517 if (s->is_subsystem) {
518 close(perr[0]);
519 perr[0] = -1;
520 }
521 session_set_fds(s, pin[1], pout[0], perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000522 } else {
523 /* Enter the interactive session. */
524 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000525 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000526 }
Damien Millerb38eff82000-04-01 11:09:21 +1000527#else /* USE_PIPES */
528 /* We are the parent. Close the child sides of the socket pairs. */
529 close(inout[0]);
530 close(err[0]);
531
532 /*
Darren Tuckerb3850592004-03-27 16:44:21 +1100533 * Clear loginmsg, since it's the child's responsibility to display
534 * it to the user, otherwise multiple sessions may accumulate
535 * multiple copies of the login messages.
536 */
537 buffer_clear(&loginmsg);
538
539 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000540 * Enter the interactive session. Note: server_loop must be able to
541 * handle the case that fdin and fdout are the same.
542 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000543 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000544 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000545 } else {
546 server_loop(pid, inout[1], inout[1], err[1]);
547 /* server_loop has closed inout[1] and err[1]. */
548 }
Damien Millerb38eff82000-04-01 11:09:21 +1000549#endif /* USE_PIPES */
550}
551
552/*
553 * This is called to fork and execute a command when we have a tty. This
554 * will call do_child from the child, and server_loop from the parent after
555 * setting up file descriptors, controlling tty, updating wtmp, utmp,
556 * lastlog, and other such operations.
557 */
Damien Miller4af51302000-04-16 11:18:38 +1000558void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000559do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000560{
Damien Millerb38eff82000-04-01 11:09:21 +1000561 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000562 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000563
564 if (s == NULL)
565 fatal("do_exec_pty: no session");
566 ptyfd = s->ptyfd;
567 ttyfd = s->ttyfd;
568
Damien Millerc5946332001-02-28 11:46:11 +1100569#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000570 if (options.use_pam) {
Damien Miller341c6e62003-09-02 23:18:52 +1000571 do_pam_set_tty(s->tty);
Darren Tuckeref3a4a22004-02-06 15:30:50 +1100572 if (!use_privsep)
573 do_pam_setcred(1);
Damien Miller4e448a32003-05-14 15:11:48 +1000574 }
Damien Miller5a761312001-02-27 09:28:23 +1100575#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000576
Damien Millerb38eff82000-04-01 11:09:21 +1000577 /* Fork the child. */
578 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000579 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000580
Damien Miller942da032000-08-18 13:59:06 +1000581 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000582 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000583 /* Close the master side of the pseudo tty. */
584 close(ptyfd);
585
586 /* Make the pseudo tty our controlling tty. */
587 pty_make_controlling_tty(&ttyfd, s->tty);
588
Damien Miller9c751422001-10-10 15:02:46 +1000589 /* Redirect stdin/stdout/stderr from the pseudo tty. */
590 if (dup2(ttyfd, 0) < 0)
591 error("dup2 stdin: %s", strerror(errno));
592 if (dup2(ttyfd, 1) < 0)
593 error("dup2 stdout: %s", strerror(errno));
594 if (dup2(ttyfd, 2) < 0)
595 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000596
597 /* Close the extra descriptor for the pseudo tty. */
598 close(ttyfd);
599
Damien Miller942da032000-08-18 13:59:06 +1000600 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000601#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700602 if (!(options.use_login && command == NULL)) {
603#ifdef _UNICOS
604 cray_init_job(s->pw); /* set up cray jid and tmpdir */
605#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100606 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700607 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000608# ifdef LOGIN_NEEDS_UTMPX
609 else
610 do_pre_login(s);
611# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000612#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000613
Damien Millerb38eff82000-04-01 11:09:21 +1000614 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000615 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000616 /* NOTREACHED */
617 }
Tim Rice81ed5182002-09-25 17:38:46 -0700618#ifdef _UNICOS
619 signal(WJSIGNAL, cray_job_termination_handler);
620#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100621#ifdef HAVE_CYGWIN
622 if (is_winnt)
623 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
624#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000625 if (pid < 0)
626 packet_disconnect("fork failed: %.100s", strerror(errno));
627 s->pid = pid;
628
629 /* Parent. Close the slave side of the pseudo tty. */
630 close(ttyfd);
631
632 /*
633 * Create another descriptor of the pty master side for use as the
634 * standard input. We could use the original descriptor, but this
635 * simplifies code in server_loop. The descriptor is bidirectional.
636 */
637 fdout = dup(ptyfd);
638 if (fdout < 0)
639 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
640
641 /* we keep a reference to the pty master */
642 ptymaster = dup(ptyfd);
643 if (ptymaster < 0)
644 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
645 s->ptymaster = ptymaster;
646
647 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000648 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000649 if (compat20) {
650 session_set_fds(s, ptyfd, fdout, -1);
651 } else {
652 server_loop(pid, ptyfd, fdout, -1);
653 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000654 }
Damien Millerb38eff82000-04-01 11:09:21 +1000655}
656
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000657#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000658static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000659do_pre_login(Session *s)
660{
661 socklen_t fromlen;
662 struct sockaddr_storage from;
663 pid_t pid = getpid();
664
665 /*
666 * Get IP address of client. If the connection is not a socket, let
667 * the address be 0.0.0.0.
668 */
669 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000670 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000671 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000672 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100673 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000674 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000675 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000676 }
677 }
678
679 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000680 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000681 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000682}
683#endif
684
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000685/*
686 * This is called to fork and execute a command. If another command is
687 * to be forced, execute that instead.
688 */
689void
690do_exec(Session *s, const char *command)
691{
Damien Millere2754432006-07-24 14:06:47 +1000692 if (options.adm_forced_command) {
693 original_command = command;
694 command = options.adm_forced_command;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100695 if (strcmp(INTERNAL_SFTP_NAME, command) == 0)
696 s->is_subsystem = SUBSYSTEM_INT_SFTP;
697 else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100698 s->is_subsystem = SUBSYSTEM_EXT;
Damien Millere2754432006-07-24 14:06:47 +1000699 debug("Forced command (config) '%.900s'", command);
700 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000701 original_command = command;
702 command = forced_command;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100703 if (strcmp(INTERNAL_SFTP_NAME, command) == 0)
704 s->is_subsystem = SUBSYSTEM_INT_SFTP;
705 else if (s->is_subsystem)
Damien Millerdfc24252008-02-10 22:29:40 +1100706 s->is_subsystem = SUBSYSTEM_EXT;
Damien Millere2754432006-07-24 14:06:47 +1000707 debug("Forced command (key option) '%.900s'", command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000708 }
709
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100710#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100711 if (command != NULL)
712 PRIVSEP(audit_run_command(command));
713 else if (s->ttyfd == -1) {
714 char *shell = s->pw->pw_shell;
715
716 if (shell[0] == '\0') /* empty shell means /bin/sh */
717 shell =_PATH_BSHELL;
718 PRIVSEP(audit_run_command(shell));
719 }
720#endif
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000721 if (s->ttyfd != -1)
722 do_exec_pty(s, command);
723 else
724 do_exec_no_pty(s, command);
725
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000726 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000727
Darren Tucker09991742004-07-17 17:05:14 +1000728 /*
729 * Clear loginmsg: it's the child's responsibility to display
730 * it to the user, otherwise multiple sessions may accumulate
731 * multiple copies of the login messages.
732 */
733 buffer_clear(&loginmsg);
734}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000735
Damien Miller942da032000-08-18 13:59:06 +1000736/* administrative, login(1)-like work */
737void
Damien Miller69b69aa2000-10-28 14:19:58 +1100738do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000739{
Damien Miller942da032000-08-18 13:59:06 +1000740 socklen_t fromlen;
741 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000742 struct passwd * pw = s->pw;
743 pid_t pid = getpid();
744
745 /*
746 * Get IP address of client. If the connection is not a socket, let
747 * the address be 0.0.0.0.
748 */
749 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000750 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000751 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000752 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000753 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000754 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000755 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000756 }
757 }
758
759 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000760 if (!use_privsep)
761 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
762 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000763 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000764 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000765
Kevin Steves092f2ef2000-10-14 13:36:13 +0000766#ifdef USE_PAM
767 /*
768 * If password change is needed, do it now.
769 * This needs to occur before the ~/.hushlogin check.
770 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100771 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
772 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000773 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100774 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000775 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000776 }
777#endif
778
Damien Millercf205e82001-04-16 18:29:15 +1000779 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000780 return;
781
Darren Tucker1921ed92004-02-10 13:23:28 +1100782 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000783
Damien Millercf205e82001-04-16 18:29:15 +1000784 do_motd();
785}
786
787/*
788 * Display the message of the day.
789 */
790void
791do_motd(void)
792{
793 FILE *f;
794 char buf[256];
795
Damien Miller942da032000-08-18 13:59:06 +1000796 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000797#ifdef HAVE_LOGIN_CAP
798 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
799 "/etc/motd"), "r");
800#else
Damien Miller942da032000-08-18 13:59:06 +1000801 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000802#endif
Damien Miller942da032000-08-18 13:59:06 +1000803 if (f) {
804 while (fgets(buf, sizeof(buf), f))
805 fputs(buf, stdout);
806 fclose(f);
807 }
808 }
809}
810
Kevin Steves8f63caa2001-07-04 18:23:02 +0000811
812/*
813 * Check for quiet login, either .hushlogin or command given.
814 */
815int
816check_quietlogin(Session *s, const char *command)
817{
818 char buf[256];
819 struct passwd *pw = s->pw;
820 struct stat st;
821
822 /* Return 1 if .hushlogin exists or a command given. */
823 if (command != NULL)
824 return 1;
825 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
826#ifdef HAVE_LOGIN_CAP
827 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
828 return 1;
829#else
830 if (stat(buf, &st) >= 0)
831 return 1;
832#endif
833 return 0;
834}
835
Damien Millerb38eff82000-04-01 11:09:21 +1000836/*
837 * Sets the value of the given variable in the environment. If the variable
838 * already exists, its value is overriden.
839 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000840void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000841child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100842 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000843{
Damien Millerb38eff82000-04-01 11:09:21 +1000844 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000845 u_int envsize;
846 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000847
848 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000849 * If we're passed an uninitialized list, allocate a single null
850 * entry before continuing.
851 */
852 if (*envp == NULL && *envsizep == 0) {
853 *envp = xmalloc(sizeof(char *));
854 *envp[0] = NULL;
855 *envsizep = 1;
856 }
857
858 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000859 * Find the slot where the value should be stored. If the variable
860 * already exists, we reuse the slot; otherwise we append a new slot
861 * at the end of the array, expanding if necessary.
862 */
863 env = *envp;
864 namelen = strlen(name);
865 for (i = 0; env[i]; i++)
866 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
867 break;
868 if (env[i]) {
869 /* Reuse the slot. */
870 xfree(env[i]);
871 } else {
872 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000873 envsize = *envsizep;
874 if (i >= envsize - 1) {
875 if (envsize >= 1000)
876 fatal("child_set_env: too many env vars");
877 envsize += 50;
Damien Miller36812092006-03-26 14:22:47 +1100878 env = (*envp) = xrealloc(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000879 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000880 }
881 /* Need to set the NULL pointer at end of array beyond the new slot. */
882 env[i + 1] = NULL;
883 }
884
885 /* Allocate space and format the variable in the appropriate slot. */
886 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
887 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
888}
889
890/*
891 * Reads environment variables from the given file and adds/overrides them
892 * into the environment. If the file does not exist, this does nothing.
893 * Otherwise, it must consist of empty lines, comments (line starts with '#')
894 * and assignments of the form name=value. No other forms are allowed.
895 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000896static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000897read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100898 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000899{
900 FILE *f;
901 char buf[4096];
902 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000903 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000904
905 f = fopen(filename, "r");
906 if (!f)
907 return;
908
909 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000910 if (++lineno > 1000)
911 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000912 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
913 ;
914 if (!*cp || *cp == '#' || *cp == '\n')
915 continue;
Damien Miller14b017d2007-09-17 16:09:15 +1000916
917 cp[strcspn(cp, "\n")] = '\0';
918
Damien Millerb38eff82000-04-01 11:09:21 +1000919 value = strchr(cp, '=');
920 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000921 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
922 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000923 continue;
924 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000925 /*
926 * Replace the equals sign by nul, and advance value to
927 * the value string.
928 */
Damien Millerb38eff82000-04-01 11:09:21 +1000929 *value = '\0';
930 value++;
931 child_set_env(env, envsize, cp, value);
932 }
933 fclose(f);
934}
935
Darren Tuckere1a790d2003-09-16 11:52:19 +1000936#ifdef HAVE_ETC_DEFAULT_LOGIN
937/*
938 * Return named variable from specified environment, or NULL if not present.
939 */
940static char *
941child_get_env(char **env, const char *name)
942{
943 int i;
944 size_t len;
945
946 len = strlen(name);
947 for (i=0; env[i] != NULL; i++)
948 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
949 return(env[i] + len + 1);
950 return NULL;
951}
952
953/*
954 * Read /etc/default/login.
955 * We pick up the PATH (or SUPATH for root) and UMASK.
956 */
957static void
958read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
959{
960 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000961 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000962 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000963
964 /*
965 * We don't want to copy the whole file to the child's environment,
966 * so we use a temporary environment and copy the variables we're
967 * interested in.
968 */
969 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
970
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000971 if (tmpenv == NULL)
972 return;
973
Darren Tuckere1a790d2003-09-16 11:52:19 +1000974 if (uid == 0)
975 var = child_get_env(tmpenv, "SUPATH");
976 else
977 var = child_get_env(tmpenv, "PATH");
978 if (var != NULL)
979 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100980
Darren Tuckere1a790d2003-09-16 11:52:19 +1000981 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
982 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000983 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100984
Darren Tuckere1a790d2003-09-16 11:52:19 +1000985 for (i = 0; tmpenv[i] != NULL; i++)
986 xfree(tmpenv[i]);
987 xfree(tmpenv);
988}
989#endif /* HAVE_ETC_DEFAULT_LOGIN */
990
Damien Miller7dff8692005-05-26 11:34:51 +1000991void
992copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000993{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100994 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000995 int i;
996
Damien Millerbb9ffc12002-01-08 10:59:32 +1100997 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000998 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000999
Damien Millerbb9ffc12002-01-08 10:59:32 +11001000 for(i = 0; source[i] != NULL; i++) {
1001 var_name = xstrdup(source[i]);
1002 if ((var_val = strstr(var_name, "=")) == NULL) {
1003 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001004 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001005 }
Damien Millerbb9ffc12002-01-08 10:59:32 +11001006 *var_val++ = '\0';
1007
1008 debug3("Copy environment: %s=%s", var_name, var_val);
1009 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +11001010
Damien Millerbb9ffc12002-01-08 10:59:32 +11001011 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001012 }
1013}
Damien Millercb5e44a2000-09-29 12:12:36 +11001014
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001015static char **
1016do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +10001017{
Damien Millerb38eff82000-04-01 11:09:21 +10001018 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001019 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001020 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001021 struct passwd *pw = s->pw;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001022#ifndef HAVE_LOGIN_CAP
1023 char *path = NULL;
1024#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001025
Damien Millerb38eff82000-04-01 11:09:21 +10001026 /* Initialize the environment. */
1027 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001028 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001029 env[0] = NULL;
1030
Damien Millerbac2d8a2000-09-05 16:13:06 +11001031#ifdef HAVE_CYGWIN
1032 /*
1033 * The Windows environment contains some setting which are
1034 * important for a running system. They must not be dropped.
1035 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001036 {
1037 char **p;
1038
1039 p = fetch_windows_environment();
1040 copy_environment(p, &env, &envsize);
1041 free_windows_environment(p);
1042 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001043#endif
1044
Darren Tucker0efd1552003-08-26 11:49:55 +10001045#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001046 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001047 * the childs environment as they see fit
1048 */
1049 ssh_gssapi_do_child(&env, &envsize);
1050#endif
1051
Damien Millerb38eff82000-04-01 11:09:21 +10001052 if (!options.use_login) {
1053 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001054 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001055 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001056 s->env[i].val);
1057
Damien Millerb38eff82000-04-01 11:09:21 +10001058 child_set_env(&env, &envsize, "USER", pw->pw_name);
1059 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001060#ifdef _AIX
1061 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1062#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001063 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001064#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001065 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1066 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1067 else
1068 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001069#else /* HAVE_LOGIN_CAP */
1070# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001071 /*
1072 * There's no standard path on Windows. The path contains
1073 * important components pointing to the system directories,
1074 * needed for loading shared libraries. So the path better
1075 * remains intact here.
1076 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001077# ifdef HAVE_ETC_DEFAULT_LOGIN
1078 read_etc_default_login(&env, &envsize, pw->pw_uid);
1079 path = child_get_env(env, "PATH");
1080# endif /* HAVE_ETC_DEFAULT_LOGIN */
1081 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001082 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001083 s->pw->pw_uid == 0 ?
1084 SUPERUSER_PATH : _PATH_STDPATH);
1085 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001086# endif /* HAVE_CYGWIN */
1087#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001088
1089 snprintf(buf, sizeof buf, "%.200s/%.50s",
1090 _PATH_MAILDIR, pw->pw_name);
1091 child_set_env(&env, &envsize, "MAIL", buf);
1092
1093 /* Normal systems set SHELL by default. */
1094 child_set_env(&env, &envsize, "SHELL", shell);
1095 }
1096 if (getenv("TZ"))
1097 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1098
1099 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001100 if (!options.use_login) {
1101 while (custom_environment) {
1102 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001103 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001104
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001105 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001106 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001107 if (str[i] == '=') {
1108 str[i] = 0;
1109 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001110 }
1111 custom_environment = ce->next;
1112 xfree(ce->s);
1113 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001114 }
Damien Millerb38eff82000-04-01 11:09:21 +10001115 }
1116
Damien Millerf37e2462002-09-19 11:47:55 +10001117 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001118 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001119 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001120 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1121
Damien Miller00111382003-03-10 11:21:17 +11001122 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001123 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001124 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1125 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001126 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1127
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001128 if (s->ttyfd != -1)
1129 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1130 if (s->term)
1131 child_set_env(&env, &envsize, "TERM", s->term);
1132 if (s->display)
1133 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001134 if (original_command)
1135 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1136 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001137
Tim Rice81ed5182002-09-25 17:38:46 -07001138#ifdef _UNICOS
1139 if (cray_tmpdir[0] != '\0')
1140 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1141#endif /* _UNICOS */
1142
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001143 /*
1144 * Since we clear KRB5CCNAME at startup, if it's set now then it
1145 * must have been set by a native authentication method (eg AIX or
1146 * SIA), so copy it to the child.
1147 */
1148 {
1149 char *cp;
1150
1151 if ((cp = getenv("KRB5CCNAME")) != NULL)
1152 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1153 }
1154
Damien Millerb38eff82000-04-01 11:09:21 +10001155#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001156 {
1157 char *cp;
1158
1159 if ((cp = getenv("AUTHSTATE")) != NULL)
1160 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001161 read_environment_file(&env, &envsize, "/etc/environment");
1162 }
Damien Millerb38eff82000-04-01 11:09:21 +10001163#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001164#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001165 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001166 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001167 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001168#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001169#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001170 /*
1171 * Pull in any environment variables that may have
1172 * been set by PAM.
1173 */
Damien Miller4e448a32003-05-14 15:11:48 +10001174 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001175 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001176
Damien Millerc756e9b2003-11-17 21:41:42 +11001177 p = fetch_pam_child_environment();
1178 copy_environment(p, &env, &envsize);
1179 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001180
Damien Millerc756e9b2003-11-17 21:41:42 +11001181 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001182 copy_environment(p, &env, &envsize);
1183 free_pam_environment(p);
1184 }
Damien Millerb38eff82000-04-01 11:09:21 +10001185#endif /* USE_PAM */
1186
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001187 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001188 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001189 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001190
1191 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001192 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001193 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001194 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001195 read_environment_file(&env, &envsize, buf);
1196 }
1197 if (debug_flag) {
1198 /* dump the environment */
1199 fprintf(stderr, "Environment:\n");
1200 for (i = 0; env[i]; i++)
1201 fprintf(stderr, " %.200s\n", env[i]);
1202 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001203 return env;
1204}
1205
1206/*
1207 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1208 * first in this order).
1209 */
1210static void
1211do_rc_files(Session *s, const char *shell)
1212{
1213 FILE *f = NULL;
1214 char cmd[1024];
1215 int do_xauth;
1216 struct stat st;
1217
1218 do_xauth =
1219 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1220
1221 /* ignore _PATH_SSH_USER_RC for subsystems */
1222 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1223 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1224 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1225 if (debug_flag)
1226 fprintf(stderr, "Running %s\n", cmd);
1227 f = popen(cmd, "w");
1228 if (f) {
1229 if (do_xauth)
1230 fprintf(f, "%s %s\n", s->auth_proto,
1231 s->auth_data);
1232 pclose(f);
1233 } else
1234 fprintf(stderr, "Could not run %s\n",
1235 _PATH_SSH_USER_RC);
1236 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1237 if (debug_flag)
1238 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1239 _PATH_SSH_SYSTEM_RC);
1240 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1241 if (f) {
1242 if (do_xauth)
1243 fprintf(f, "%s %s\n", s->auth_proto,
1244 s->auth_data);
1245 pclose(f);
1246 } else
1247 fprintf(stderr, "Could not run %s\n",
1248 _PATH_SSH_SYSTEM_RC);
1249 } else if (do_xauth && options.xauth_location != NULL) {
1250 /* Add authority data to .Xauthority if appropriate. */
1251 if (debug_flag) {
1252 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001253 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001254 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001255 fprintf(stderr,
1256 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001257 options.xauth_location, s->auth_display,
1258 s->auth_proto, s->auth_data);
1259 }
1260 snprintf(cmd, sizeof cmd, "%s -q -",
1261 options.xauth_location);
1262 f = popen(cmd, "w");
1263 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001264 fprintf(f, "remove %s\n",
1265 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001266 fprintf(f, "add %s %s %s\n",
1267 s->auth_display, s->auth_proto,
1268 s->auth_data);
1269 pclose(f);
1270 } else {
1271 fprintf(stderr, "Could not run %s\n",
1272 cmd);
1273 }
1274 }
1275}
1276
1277static void
1278do_nologin(struct passwd *pw)
1279{
1280 FILE *f = NULL;
1281 char buf[1024];
1282
1283#ifdef HAVE_LOGIN_CAP
1284 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1285 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1286 _PATH_NOLOGIN), "r");
1287#else
1288 if (pw->pw_uid)
1289 f = fopen(_PATH_NOLOGIN, "r");
1290#endif
1291 if (f) {
1292 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001293 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001294 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001295 while (fgets(buf, sizeof(buf), f))
1296 fputs(buf, stderr);
1297 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001298 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001299 exit(254);
1300 }
1301}
1302
Damien Millerd8cb1f12008-02-10 22:40:12 +11001303/*
1304 * Chroot into a directory after checking it for safety: all path components
1305 * must be root-owned directories with strict permissions.
1306 */
1307static void
1308safely_chroot(const char *path, uid_t uid)
1309{
1310 const char *cp;
1311 char component[MAXPATHLEN];
1312 struct stat st;
1313
1314 if (*path != '/')
1315 fatal("chroot path does not begin at root");
1316 if (strlen(path) >= sizeof(component))
1317 fatal("chroot path too long");
1318
1319 /*
1320 * Descend the path, checking that each component is a
1321 * root-owned directory with strict permissions.
1322 */
1323 for (cp = path; cp != NULL;) {
1324 if ((cp = strchr(cp, '/')) == NULL)
1325 strlcpy(component, path, sizeof(component));
1326 else {
1327 cp++;
1328 memcpy(component, path, cp - path);
1329 component[cp - path] = '\0';
1330 }
1331
1332 debug3("%s: checking '%s'", __func__, component);
1333
1334 if (stat(component, &st) != 0)
1335 fatal("%s: stat(\"%s\"): %s", __func__,
1336 component, strerror(errno));
1337 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1338 fatal("bad ownership or modes for chroot "
1339 "directory %s\"%s\"",
1340 cp == NULL ? "" : "component ", component);
1341 if (!S_ISDIR(st.st_mode))
1342 fatal("chroot path %s\"%s\" is not a directory",
1343 cp == NULL ? "" : "component ", component);
1344
1345 }
1346
1347 if (chdir(path) == -1)
1348 fatal("Unable to chdir to chroot path \"%s\": "
1349 "%s", path, strerror(errno));
1350 if (chroot(path) == -1)
1351 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1352 if (chdir("/") == -1)
1353 fatal("%s: chdir(/) after chroot: %s",
1354 __func__, strerror(errno));
1355 verbose("Changed root directory to \"%s\"", path);
1356}
1357
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001358/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001360do_setusercontext(struct passwd *pw)
1361{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001362#ifndef HAVE_CYGWIN
1363 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001364#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001365 {
1366
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001367#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001368 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1369 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001370#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001371#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001372# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001373 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001374# endif
Darren Tuckerc97b01a2005-02-16 16:47:37 +11001375#ifdef GSSAPI
1376 if (options.gss_authentication) {
1377 temporarily_use_uid(pw);
1378 ssh_gssapi_storecreds();
1379 restore_uid();
1380 }
1381#endif
Damien Millerd3526362004-01-23 14:16:26 +11001382# ifdef USE_PAM
1383 if (options.use_pam) {
1384 do_pam_session();
Darren Tucker2d963642007-08-13 23:11:56 +10001385 do_pam_setcred(use_privsep);
Damien Millerd3526362004-01-23 14:16:26 +11001386 }
1387# endif /* USE_PAM */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001388 if (setusercontext(lc, pw, pw->pw_uid,
Damien Millerd8cb1f12008-02-10 22:40:12 +11001389 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001390 perror("unable to set user context");
1391 exit(1);
1392 }
1393#else
1394# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1395 /* Sets login uid for accounting */
1396 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1397 error("setluid: %s", strerror(errno));
1398# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1399
1400 if (setlogin(pw->pw_name) < 0)
1401 error("setlogin failed: %s", strerror(errno));
1402 if (setgid(pw->pw_gid) < 0) {
1403 perror("setgid");
1404 exit(1);
1405 }
1406 /* Initialize the group list. */
1407 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1408 perror("initgroups");
1409 exit(1);
1410 }
1411 endgrent();
Damien Millerd8cb1f12008-02-10 22:40:12 +11001412# ifdef GSSAPI
Darren Tuckerc97b01a2005-02-16 16:47:37 +11001413 if (options.gss_authentication) {
1414 temporarily_use_uid(pw);
1415 ssh_gssapi_storecreds();
1416 restore_uid();
1417 }
Damien Millerd8cb1f12008-02-10 22:40:12 +11001418# endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001419# ifdef USE_PAM
1420 /*
Damien Millera8e06ce2003-11-21 23:48:55 +11001421 * PAM credentials may take the form of supplementary groups.
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001422 * These will have been wiped by the above initgroups() call.
1423 * Reestablish them here.
1424 */
Damien Miller341c6e62003-09-02 23:18:52 +10001425 if (options.use_pam) {
1426 do_pam_session();
Darren Tucker2d963642007-08-13 23:11:56 +10001427 do_pam_setcred(use_privsep);
Damien Miller341c6e62003-09-02 23:18:52 +10001428 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001429# endif /* USE_PAM */
1430# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1431 irix_setusercontext(pw);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001432# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001433# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001434 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001435# endif /* _AIX */
Damien Millerd8cb1f12008-02-10 22:40:12 +11001436# ifdef USE_LIBIAF
Tim Rice2291c002005-08-26 13:15:19 -07001437 if (set_id(pw->pw_name) != 0) {
1438 exit(1);
1439 }
Damien Millerd8cb1f12008-02-10 22:40:12 +11001440# endif /* USE_LIBIAF */
1441#endif
1442
1443 if (options.chroot_directory != NULL &&
1444 strcasecmp(options.chroot_directory, "none") != 0) {
1445 char *chroot_path;
1446
1447 chroot_path = percent_expand(options.chroot_directory,
1448 "h", pw->pw_dir, "u", pw->pw_name, (char *)NULL);
1449 safely_chroot(chroot_path, pw->pw_uid);
1450 free(chroot_path);
1451 }
1452
1453#ifdef HAVE_LOGIN_CAP
1454 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1455 perror("unable to set user context (setuser)");
1456 exit(1);
1457 }
1458#else
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001459 /* Permanently switch to the desired uid. */
1460 permanently_set_uid(pw);
1461#endif
1462 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001463
1464#ifdef HAVE_CYGWIN
1465 if (is_winnt)
1466#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001467 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1468 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Miller73b42d22006-04-22 21:26:08 +10001469
1470#ifdef WITH_SELINUX
1471 ssh_selinux_setup_exec_context(pw->pw_name);
1472#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001473}
1474
Ben Lindstrom08105192002-03-22 02:50:06 +00001475static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001476do_pwchange(Session *s)
1477{
Darren Tucker09991742004-07-17 17:05:14 +10001478 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001479 fprintf(stderr, "WARNING: Your password has expired.\n");
1480 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001481 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001482 "You must change your password now and login again!\n");
Darren Tucker33370e02005-02-09 22:17:28 +11001483#ifdef PASSWD_NEEDS_USERNAME
1484 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1485 (char *)NULL);
1486#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001487 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001488#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001489 perror("passwd");
1490 } else {
1491 fprintf(stderr,
1492 "Password change required but no TTY available.\n");
1493 }
1494 exit(1);
1495}
1496
1497static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498launch_login(struct passwd *pw, const char *hostname)
1499{
1500 /* Launch login(1). */
1501
Ben Lindstrom378a4172002-06-07 14:49:56 +00001502 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001503#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001504 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001506#ifdef LOGIN_NO_ENDOPT
1507 "-p", "-f", pw->pw_name, (char *)NULL);
1508#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001510#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511
1512 /* Login couldn't be executed, die. */
1513
1514 perror("login");
1515 exit(1);
1516}
1517
Darren Tucker23bc8d02004-02-06 16:24:31 +11001518static void
1519child_close_fds(void)
1520{
1521 int i;
1522
1523 if (packet_get_connection_in() == packet_get_connection_out())
1524 close(packet_get_connection_in());
1525 else {
1526 close(packet_get_connection_in());
1527 close(packet_get_connection_out());
1528 }
1529 /*
1530 * Close all descriptors related to channels. They will still remain
1531 * open in the parent.
1532 */
1533 /* XXX better use close-on-exec? -markus */
1534 channel_close_all();
1535
1536 /*
1537 * Close any extra file descriptors. Note that there may still be
1538 * descriptors left by system functions. They will be closed later.
1539 */
1540 endpwent();
1541
1542 /*
Damien Miller788f2122005-11-05 15:14:59 +11001543 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001544 * hanging around in clients. Note that we want to do this after
1545 * initgroups, because at least on Solaris 2.3 it leaves file
1546 * descriptors open.
1547 */
1548 for (i = 3; i < 64; i++)
1549 close(i);
1550}
1551
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001552/*
1553 * Performs common processing for the child, such as setting up the
1554 * environment, closing extra file descriptors, setting the user and group
1555 * ids, and executing the command or shell.
1556 */
Damien Millerdfc24252008-02-10 22:29:40 +11001557#define ARGV_MAX 10
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001558void
1559do_child(Session *s, const char *command)
1560{
1561 extern char **environ;
1562 char **env;
Damien Millerdfc24252008-02-10 22:29:40 +11001563 char *argv[ARGV_MAX];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001564 const char *shell, *shell0, *hostname = NULL;
1565 struct passwd *pw = s->pw;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001566
1567 /* remove hostkey from the child's memory */
1568 destroy_sensitive_data();
1569
Darren Tucker23bc8d02004-02-06 16:24:31 +11001570 /* Force a password change */
1571 if (s->authctxt->force_pwchange) {
1572 do_setusercontext(pw);
1573 child_close_fds();
1574 do_pwchange(s);
1575 exit(1);
1576 }
1577
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001578 /* login(1) is only called if we execute the login shell */
1579 if (options.use_login && command != NULL)
1580 options.use_login = 0;
1581
Tim Rice81ed5182002-09-25 17:38:46 -07001582#ifdef _UNICOS
1583 cray_setup(pw->pw_uid, pw->pw_name, command);
1584#endif /* _UNICOS */
1585
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001586 /*
1587 * Login(1) does this as well, and it needs uid 0 for the "-h"
1588 * switch, so we let login(1) to this for us.
1589 */
1590 if (!options.use_login) {
1591#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001592 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001593 if (!check_quietlogin(s, command))
1594 do_motd();
1595#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001596 /* When PAM is enabled we rely on it to do the nologin check */
1597 if (!options.use_pam)
1598 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001599 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001600 /*
1601 * PAM session modules in do_setusercontext may have
1602 * generated messages, so if this in an interactive
1603 * login then display them too.
1604 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001605 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001606 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001607#endif /* HAVE_OSF_SIA */
1608 }
1609
Darren Tucker69687f42004-09-11 22:17:26 +10001610#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001611 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1612 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001613 display_loginmsg();
1614 exit(254);
1615 }
1616#endif
1617
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001618 /*
1619 * Get the shell from the password data. An empty shell field is
1620 * legal, and means /bin/sh.
1621 */
1622 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001623
1624 /*
1625 * Make sure $SHELL points to the shell from the password file,
1626 * even if shell is overridden from login.conf
1627 */
1628 env = do_setup_env(s, shell);
1629
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001630#ifdef HAVE_LOGIN_CAP
1631 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1632#endif
1633
Damien Millerad833b32000-08-23 10:46:23 +10001634 /* we have to stash the hostname before we close our socket. */
1635 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001636 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001637 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001638 /*
1639 * Close the connection descriptors; note that this is the child, and
1640 * the server will still have the socket open, and it is important
1641 * that we do not shutdown it. Note that the descriptors cannot be
1642 * closed before building the environment, as we call
1643 * get_remote_ipaddr there.
1644 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001645 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001646
Damien Millerb38eff82000-04-01 11:09:21 +10001647 /*
Damien Miller05eda432002-02-10 18:32:28 +11001648 * Must take new environment into use so that .ssh/rc,
1649 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001650 */
1651 environ = env;
1652
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001653#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001654 /*
1655 * At this point, we check to see if AFS is active and if we have
1656 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1657 * if we can (and need to) extend the ticket into an AFS token. If
1658 * we don't do this, we run into potential problems if the user's
1659 * home directory is in AFS and it's not world-readable.
1660 */
1661
1662 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001663 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001664 char cell[64];
1665
1666 debug("Getting AFS token");
1667
1668 k_setpag();
1669
1670 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1671 krb5_afslog(s->authctxt->krb5_ctx,
1672 s->authctxt->krb5_fwd_ccache, cell, NULL);
1673
1674 krb5_afslog_home(s->authctxt->krb5_ctx,
1675 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1676 }
1677#endif
1678
Damien Miller788f2122005-11-05 15:14:59 +11001679 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001680 if (chdir(pw->pw_dir) < 0) {
1681 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1682 pw->pw_dir, strerror(errno));
1683#ifdef HAVE_LOGIN_CAP
1684 if (login_getcapbool(lc, "requirehome", 0))
1685 exit(1);
1686#endif
1687 }
1688
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001689 if (!options.use_login)
1690 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001691
1692 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001693 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001694
Damien Millerdfc24252008-02-10 22:29:40 +11001695 if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
1696 extern int optind, optreset;
1697 int i;
1698 char *p, *args;
1699
1700 setproctitle("%s@internal-sftp-server", s->pw->pw_name);
1701 args = strdup(command ? command : "sftp-server");
1702 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1703 if (i < ARGV_MAX - 1)
1704 argv[i++] = p;
1705 argv[i] = NULL;
1706 optind = optreset = 1;
1707 __progname = argv[0];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001708 exit(sftp_server_main(i, argv, s->pw));
Damien Millerdfc24252008-02-10 22:29:40 +11001709 }
1710
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001711 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712 launch_login(pw, hostname);
1713 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001714 }
1715
1716 /* Get the last component of the shell name. */
1717 if ((shell0 = strrchr(shell, '/')) != NULL)
1718 shell0++;
1719 else
1720 shell0 = shell;
1721
Damien Millerb38eff82000-04-01 11:09:21 +10001722 /*
1723 * If we have no command, execute the shell. In this case, the shell
1724 * name to be passed in argv[0] is preceded by '-' to indicate that
1725 * this is a login shell.
1726 */
1727 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001728 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001729
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001730 /* Start the shell. Set initial character to '-'. */
1731 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001732
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001733 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1734 >= sizeof(argv0) - 1) {
1735 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001736 perror(shell);
1737 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001738 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001739
1740 /* Execute the shell. */
1741 argv[0] = argv0;
1742 argv[1] = NULL;
1743 execve(shell, argv, env);
1744
1745 /* Executing the shell failed. */
1746 perror(shell);
1747 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001748 }
1749 /*
1750 * Execute the command using the user's shell. This uses the -c
1751 * option to execute the command.
1752 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001753 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001754 argv[1] = "-c";
1755 argv[2] = (char *) command;
1756 argv[3] = NULL;
1757 execve(shell, argv, env);
1758 perror(shell);
1759 exit(1);
1760}
1761
1762Session *
1763session_new(void)
1764{
1765 int i;
1766 static int did_init = 0;
1767 if (!did_init) {
1768 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001769 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001770 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001771 }
1772 did_init = 1;
1773 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001774 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001775 Session *s = &sessions[i];
1776 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001777 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001778 s->chanid = -1;
1779 s->ptyfd = -1;
1780 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001781 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001782 s->self = i;
Damien Miller2b9b0452005-07-17 17:19:24 +10001783 s->x11_chanids = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001784 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001785 return s;
1786 }
1787 }
1788 return NULL;
1789}
1790
Ben Lindstrombba81212001-06-25 05:01:22 +00001791static void
Damien Millerb38eff82000-04-01 11:09:21 +10001792session_dump(void)
1793{
1794 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001795 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001796 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001797 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001798 s->used,
1799 s->self,
1800 s,
1801 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001802 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001803 }
1804}
1805
Damien Millerefb4afe2000-04-12 18:45:05 +10001806int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001807session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001808{
1809 Session *s = session_new();
1810 debug("session_open: channel %d", chanid);
1811 if (s == NULL) {
1812 error("no more sessions");
1813 return 0;
1814 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001815 s->authctxt = authctxt;
1816 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001817 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001818 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001819 debug("session_open: session %d: link with channel %d", s->self, chanid);
1820 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001821 return 1;
1822}
1823
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001824Session *
1825session_by_tty(char *tty)
1826{
1827 int i;
1828 for (i = 0; i < MAX_SESSIONS; i++) {
1829 Session *s = &sessions[i];
1830 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1831 debug("session_by_tty: session %d tty %s", i, tty);
1832 return s;
1833 }
1834 }
1835 debug("session_by_tty: unknown tty %.100s", tty);
1836 session_dump();
1837 return NULL;
1838}
1839
Ben Lindstrombba81212001-06-25 05:01:22 +00001840static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001841session_by_channel(int id)
1842{
1843 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001844 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001845 Session *s = &sessions[i];
1846 if (s->used && s->chanid == id) {
1847 debug("session_by_channel: session %d channel %d", i, id);
1848 return s;
1849 }
1850 }
1851 debug("session_by_channel: unknown channel %d", id);
1852 session_dump();
1853 return NULL;
1854}
1855
Ben Lindstrombba81212001-06-25 05:01:22 +00001856static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001857session_by_x11_channel(int id)
1858{
1859 int i, j;
1860
1861 for (i = 0; i < MAX_SESSIONS; i++) {
1862 Session *s = &sessions[i];
1863
1864 if (s->x11_chanids == NULL || !s->used)
1865 continue;
1866 for (j = 0; s->x11_chanids[j] != -1; j++) {
1867 if (s->x11_chanids[j] == id) {
1868 debug("session_by_x11_channel: session %d "
1869 "channel %d", s->self, id);
1870 return s;
1871 }
1872 }
1873 }
1874 debug("session_by_x11_channel: unknown channel %d", id);
1875 session_dump();
1876 return NULL;
1877}
1878
1879static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001880session_by_pid(pid_t pid)
1881{
1882 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001883 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001884 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001885 Session *s = &sessions[i];
1886 if (s->used && s->pid == pid)
1887 return s;
1888 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001889 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001890 session_dump();
1891 return NULL;
1892}
1893
Ben Lindstrombba81212001-06-25 05:01:22 +00001894static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001895session_window_change_req(Session *s)
1896{
1897 s->col = packet_get_int();
1898 s->row = packet_get_int();
1899 s->xpixel = packet_get_int();
1900 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001901 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001902 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1903 return 1;
1904}
1905
Ben Lindstrombba81212001-06-25 05:01:22 +00001906static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001907session_pty_req(Session *s)
1908{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001909 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001910 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001911
Ben Lindstrom49c12602001-06-13 04:37:36 +00001912 if (no_pty_flag) {
1913 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001914 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001915 }
1916 if (s->ttyfd != -1) {
1917 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001918 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001919 }
1920
Damien Millerefb4afe2000-04-12 18:45:05 +10001921 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001922
1923 if (compat20) {
1924 s->col = packet_get_int();
1925 s->row = packet_get_int();
1926 } else {
1927 s->row = packet_get_int();
1928 s->col = packet_get_int();
1929 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001930 s->xpixel = packet_get_int();
1931 s->ypixel = packet_get_int();
1932
1933 if (strcmp(s->term, "") == 0) {
1934 xfree(s->term);
1935 s->term = NULL;
1936 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001937
Damien Millerefb4afe2000-04-12 18:45:05 +10001938 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001939 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001940 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001941 if (s->term)
1942 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001943 s->term = NULL;
1944 s->ptyfd = -1;
1945 s->ttyfd = -1;
1946 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001947 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001948 }
1949 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001950
1951 /* for SSH1 the tty modes length is not given */
1952 if (!compat20)
1953 n_bytes = packet_remaining();
1954 tty_parse_modes(s->ttyfd, &n_bytes);
1955
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001956 if (!use_privsep)
1957 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001958
1959 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001960 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1961
Damien Miller48b03fc2002-01-22 23:11:40 +11001962 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001963 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001964 return 1;
1965}
1966
Ben Lindstrombba81212001-06-25 05:01:22 +00001967static int
Damien Millerbd483e72000-04-30 10:00:53 +10001968session_subsystem_req(Session *s)
1969{
Damien Millerae452462001-10-10 15:08:06 +10001970 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001971 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001972 int success = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001973 char *prog, *cmd, *subsys = packet_get_string(&len);
Damien Millereccb9de2005-06-17 12:59:34 +10001974 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001975
Damien Miller48b03fc2002-01-22 23:11:40 +11001976 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001977 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001978
Damien Millerf6d9e222000-06-18 14:50:44 +10001979 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001980 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001981 prog = options.subsystem_command[i];
1982 cmd = options.subsystem_args[i];
Damien Millerd8cb1f12008-02-10 22:40:12 +11001983 if (!strcmp(INTERNAL_SFTP_NAME, prog)) {
Damien Millerdfc24252008-02-10 22:29:40 +11001984 s->is_subsystem = SUBSYSTEM_INT_SFTP;
1985 } else if (stat(prog, &st) < 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001986 error("subsystem: cannot stat %s: %s", prog,
Damien Millerae452462001-10-10 15:08:06 +10001987 strerror(errno));
1988 break;
Damien Millerdfc24252008-02-10 22:29:40 +11001989 } else {
1990 s->is_subsystem = SUBSYSTEM_EXT;
Damien Millerae452462001-10-10 15:08:06 +10001991 }
1992 debug("subsystem: exec() %s", cmd);
Damien Millerae452462001-10-10 15:08:06 +10001993 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001994 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001995 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001996 }
1997 }
1998
1999 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10002000 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10002001 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10002002
Damien Millerbd483e72000-04-30 10:00:53 +10002003 xfree(subsys);
2004 return success;
2005}
2006
Ben Lindstrombba81212001-06-25 05:01:22 +00002007static int
Damien Millerbd483e72000-04-30 10:00:53 +10002008session_x11_req(Session *s)
2009{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002010 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10002011
Damien Miller2b9b0452005-07-17 17:19:24 +10002012 if (s->auth_proto != NULL || s->auth_data != NULL) {
2013 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11002014 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10002015 return 0;
2016 }
Damien Millerbd483e72000-04-30 10:00:53 +10002017 s->single_connection = packet_get_char();
2018 s->auth_proto = packet_get_string(NULL);
2019 s->auth_data = packet_get_string(NULL);
2020 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002021 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002022
Ben Lindstrom768176b2001-06-09 01:29:12 +00002023 success = session_setup_x11fwd(s);
2024 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10002025 xfree(s->auth_proto);
2026 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00002027 s->auth_proto = NULL;
2028 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10002029 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002030 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10002031}
2032
Ben Lindstrombba81212001-06-25 05:01:22 +00002033static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002034session_shell_req(Session *s)
2035{
Damien Miller48b03fc2002-01-22 23:11:40 +11002036 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00002037 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10002038 return 1;
2039}
2040
Ben Lindstrombba81212001-06-25 05:01:22 +00002041static int
Damien Millerf6d9e222000-06-18 14:50:44 +10002042session_exec_req(Session *s)
2043{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002044 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10002045 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002046 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00002047 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00002048 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10002049 return 1;
2050}
2051
Ben Lindstrombba81212001-06-25 05:01:22 +00002052static int
Damien Miller54c45982003-05-15 10:20:13 +10002053session_break_req(Session *s)
2054{
Damien Miller54c45982003-05-15 10:20:13 +10002055
Darren Tucker1f8311c2004-05-13 16:39:33 +10002056 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10002057 packet_check_eom();
2058
Darren Tucker3bdbd842003-08-13 20:31:05 +10002059 if (s->ttyfd == -1 ||
2060 tcsendbreak(s->ttyfd, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10002061 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10002062 return 1;
2063}
2064
2065static int
Darren Tucker46bc0752004-05-02 22:11:30 +10002066session_env_req(Session *s)
2067{
2068 char *name, *val;
2069 u_int name_len, val_len, i;
2070
2071 name = packet_get_string(&name_len);
2072 val = packet_get_string(&val_len);
2073 packet_check_eom();
2074
2075 /* Don't set too many environment variables */
2076 if (s->num_env > 128) {
2077 debug2("Ignoring env request %s: too many env vars", name);
2078 goto fail;
2079 }
2080
2081 for (i = 0; i < options.num_accept_env; i++) {
2082 if (match_pattern(name, options.accept_env[i])) {
2083 debug2("Setting env %d: %s=%s", s->num_env, name, val);
Damien Miller36812092006-03-26 14:22:47 +11002084 s->env = xrealloc(s->env, s->num_env + 1,
2085 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10002086 s->env[s->num_env].name = name;
2087 s->env[s->num_env].val = val;
2088 s->num_env++;
2089 return (1);
2090 }
2091 }
2092 debug2("Ignoring env request %s: disallowed name", name);
2093
2094 fail:
2095 xfree(name);
2096 xfree(val);
2097 return (0);
2098}
2099
2100static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002101session_auth_agent_req(Session *s)
2102{
2103 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11002104 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00002105 if (no_agent_forwarding_flag) {
2106 debug("session_auth_agent_req: no_agent_forwarding_flag");
2107 return 0;
2108 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11002109 if (called) {
2110 return 0;
2111 } else {
2112 called = 1;
2113 return auth_input_request_forwarding(s->pw);
2114 }
2115}
2116
Damien Millerc7ef63d2002-02-05 12:21:42 +11002117int
2118session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002119{
Damien Millerefb4afe2000-04-12 18:45:05 +10002120 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002121 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002122
Damien Millerc7ef63d2002-02-05 12:21:42 +11002123 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002124 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002125 c->self, rtype);
2126 return 0;
2127 }
2128 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002129
2130 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002131 * a session is in LARVAL state until a shell, a command
2132 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002133 */
2134 if (c->type == SSH_CHANNEL_LARVAL) {
2135 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002136 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002137 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002138 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002139 } else if (strcmp(rtype, "pty-req") == 0) {
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002140 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002141 } else if (strcmp(rtype, "x11-req") == 0) {
2142 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002143 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2144 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002145 } else if (strcmp(rtype, "subsystem") == 0) {
2146 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002147 } else if (strcmp(rtype, "env") == 0) {
2148 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002149 }
2150 }
2151 if (strcmp(rtype, "window-change") == 0) {
2152 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002153 } else if (strcmp(rtype, "break") == 0) {
2154 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002155 }
Damien Millera6b1d162004-06-30 22:41:07 +10002156
Damien Millerc7ef63d2002-02-05 12:21:42 +11002157 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002158}
2159
2160void
2161session_set_fds(Session *s, int fdin, int fdout, int fderr)
2162{
2163 if (!compat20)
2164 fatal("session_set_fds: called for proto != 2.0");
2165 /*
2166 * now that have a child and a pipe to the child,
2167 * we can activate our channel and register the fd's
2168 */
2169 if (s->chanid == -1)
2170 fatal("no channel for session %d", s->self);
2171 channel_set_fds(s->chanid,
2172 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11002173 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11002174 1,
2175 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002176}
2177
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002178/*
2179 * Function to perform pty cleanup. Also called if we get aborted abnormally
2180 * (e.g., due to a dropped connection).
2181 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002182void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002183session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002184{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002185 if (s == NULL) {
2186 error("session_pty_cleanup: no session");
2187 return;
2188 }
2189 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002190 return;
2191
Kevin Steves43cdef32001-02-11 14:12:08 +00002192 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002193
Damien Millerb38eff82000-04-01 11:09:21 +10002194 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002195 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002196 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002197
2198 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002199 if (getuid() == 0)
2200 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002201
2202 /*
2203 * Close the server side of the socket pairs. We must do this after
2204 * the pty cleanup, so that another process doesn't get this pty
2205 * while we're still cleaning up.
2206 */
2207 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002208 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002209
2210 /* unlink pty from session */
2211 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002212}
Damien Millerefb4afe2000-04-12 18:45:05 +10002213
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002214void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002215session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002216{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002217 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002218}
2219
Damien Miller5a80bba2002-09-04 16:39:02 +10002220static char *
2221sig2name(int sig)
2222{
2223#define SSH_SIG(x) if (sig == SIG ## x) return #x
2224 SSH_SIG(ABRT);
2225 SSH_SIG(ALRM);
2226 SSH_SIG(FPE);
2227 SSH_SIG(HUP);
2228 SSH_SIG(ILL);
2229 SSH_SIG(INT);
2230 SSH_SIG(KILL);
2231 SSH_SIG(PIPE);
2232 SSH_SIG(QUIT);
2233 SSH_SIG(SEGV);
2234 SSH_SIG(TERM);
2235 SSH_SIG(USR1);
2236 SSH_SIG(USR2);
2237#undef SSH_SIG
2238 return "SIG@openssh.com";
2239}
2240
Ben Lindstrombba81212001-06-25 05:01:22 +00002241static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002242session_close_x11(int id)
2243{
2244 Channel *c;
2245
Damien Millerd47c62a2005-12-13 19:33:57 +11002246 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002247 debug("session_close_x11: x11 channel %d missing", id);
2248 } else {
2249 /* Detach X11 listener */
2250 debug("session_close_x11: detach x11 channel %d", id);
2251 channel_cancel_cleanup(id);
2252 if (c->ostate != CHAN_OUTPUT_CLOSED)
2253 chan_mark_dead(c);
2254 }
2255}
2256
2257static void
2258session_close_single_x11(int id, void *arg)
2259{
2260 Session *s;
2261 u_int i;
2262
2263 debug3("session_close_single_x11: channel %d", id);
2264 channel_cancel_cleanup(id);
Darren Tucker82a3d2b2007-02-19 22:10:25 +11002265 if ((s = session_by_x11_channel(id)) == NULL)
Damien Miller2b9b0452005-07-17 17:19:24 +10002266 fatal("session_close_single_x11: no x11 channel %d", id);
2267 for (i = 0; s->x11_chanids[i] != -1; i++) {
2268 debug("session_close_single_x11: session %d: "
2269 "closing channel %d", s->self, s->x11_chanids[i]);
2270 /*
2271 * The channel "id" is already closing, but make sure we
2272 * close all of its siblings.
2273 */
2274 if (s->x11_chanids[i] != id)
2275 session_close_x11(s->x11_chanids[i]);
2276 }
2277 xfree(s->x11_chanids);
2278 s->x11_chanids = NULL;
2279 if (s->display) {
2280 xfree(s->display);
2281 s->display = NULL;
2282 }
2283 if (s->auth_proto) {
2284 xfree(s->auth_proto);
2285 s->auth_proto = NULL;
2286 }
2287 if (s->auth_data) {
2288 xfree(s->auth_data);
2289 s->auth_data = NULL;
2290 }
2291 if (s->auth_display) {
2292 xfree(s->auth_display);
2293 s->auth_display = NULL;
2294 }
2295}
2296
2297static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002298session_exit_message(Session *s, int status)
2299{
2300 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002301
2302 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002303 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002304 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002305 debug("session_exit_message: session %d channel %d pid %ld",
2306 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002307
2308 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002309 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002310 packet_put_int(WEXITSTATUS(status));
2311 packet_send();
2312 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002313 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002314 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002315#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10002316 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002317#else /* WCOREDUMP */
2318 packet_put_char(0);
2319#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002320 packet_put_cstring("");
2321 packet_put_cstring("");
2322 packet_send();
2323 } else {
2324 /* Some weird exit cause. Just exit. */
2325 packet_disconnect("wait returned status %04x.", status);
2326 }
2327
2328 /* disconnect channel */
2329 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002330
2331 /*
2332 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002333 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002334 * by session_close_by_channel when the childs close their fds.
2335 */
2336 channel_register_cleanup(c->self, session_close_by_channel, 1);
2337
Damien Miller166fca82000-04-20 07:42:21 +10002338 /*
2339 * emulate a write failure with 'chan_write_failed', nobody will be
2340 * interested in data we write.
2341 * Note that we must not call 'chan_read_failed', since there could
2342 * be some more data waiting in the pipe.
2343 */
Damien Millerbd483e72000-04-30 10:00:53 +10002344 if (c->ostate != CHAN_OUTPUT_CLOSED)
2345 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002346}
2347
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002348void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002349session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002350{
Damien Millereccb9de2005-06-17 12:59:34 +10002351 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002352
Ben Lindstromce0f6342002-06-11 16:42:49 +00002353 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002354 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002355 session_pty_cleanup(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002356 if (s->term)
2357 xfree(s->term);
2358 if (s->display)
2359 xfree(s->display);
Damien Miller2b9b0452005-07-17 17:19:24 +10002360 if (s->x11_chanids)
2361 xfree(s->x11_chanids);
Damien Miller512bccb2002-02-05 12:11:02 +11002362 if (s->auth_display)
2363 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10002364 if (s->auth_data)
2365 xfree(s->auth_data);
2366 if (s->auth_proto)
2367 xfree(s->auth_proto);
2368 s->used = 0;
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002369 if (s->env != NULL) {
2370 for (i = 0; i < s->num_env; i++) {
2371 xfree(s->env[i].name);
2372 xfree(s->env[i].val);
2373 }
Darren Tucker46bc0752004-05-02 22:11:30 +10002374 xfree(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002375 }
Damien Millere247cc42000-05-07 12:03:14 +10002376 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002377}
2378
2379void
2380session_close_by_pid(pid_t pid, int status)
2381{
2382 Session *s = session_by_pid(pid);
2383 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002384 debug("session_close_by_pid: no session for pid %ld",
2385 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002386 return;
2387 }
2388 if (s->chanid != -1)
2389 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002390 if (s->ttyfd != -1)
2391 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002392 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002393}
2394
2395/*
2396 * this is called when a channel dies before
2397 * the session 'child' itself dies
2398 */
2399void
2400session_close_by_channel(int id, void *arg)
2401{
2402 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002403 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002404
Damien Millerefb4afe2000-04-12 18:45:05 +10002405 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002406 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002407 return;
2408 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002409 debug("session_close_by_channel: channel %d child %ld",
2410 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002411 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002412 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002413 /*
2414 * delay detach of session, but release pty, since
2415 * the fd's to the child are already closed
2416 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002417 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002418 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002419 return;
2420 }
2421 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002422 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002423
2424 /* Close any X11 listeners associated with this session */
2425 if (s->x11_chanids != NULL) {
2426 for (i = 0; s->x11_chanids[i] != -1; i++) {
2427 session_close_x11(s->x11_chanids[i]);
2428 s->x11_chanids[i] = -1;
2429 }
2430 }
2431
Damien Millerefb4afe2000-04-12 18:45:05 +10002432 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002433 session_close(s);
2434}
2435
2436void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002437session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002438{
2439 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002440 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002441 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002442 if (s->used) {
2443 if (closefunc != NULL)
2444 closefunc(s);
2445 else
2446 session_close(s);
2447 }
Damien Miller52b77be2001-10-10 15:14:37 +10002448 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002449}
2450
Ben Lindstrombba81212001-06-25 05:01:22 +00002451static char *
Damien Millere247cc42000-05-07 12:03:14 +10002452session_tty_list(void)
2453{
2454 static char buf[1024];
2455 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002456 char *cp;
2457
Damien Millere247cc42000-05-07 12:03:14 +10002458 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002459 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002460 Session *s = &sessions[i];
2461 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002462
Damien Millera8ed44b2003-01-10 09:53:12 +11002463 if (strncmp(s->tty, "/dev/", 5) != 0) {
2464 cp = strrchr(s->tty, '/');
2465 cp = (cp == NULL) ? s->tty : cp + 1;
2466 } else
2467 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002468
Damien Millere247cc42000-05-07 12:03:14 +10002469 if (buf[0] != '\0')
2470 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002471 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002472 }
2473 }
2474 if (buf[0] == '\0')
2475 strlcpy(buf, "notty", sizeof buf);
2476 return buf;
2477}
2478
2479void
2480session_proctitle(Session *s)
2481{
2482 if (s->pw == NULL)
2483 error("no user for session %d", s->self);
2484 else
2485 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2486}
2487
Ben Lindstrom768176b2001-06-09 01:29:12 +00002488int
2489session_setup_x11fwd(Session *s)
2490{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002491 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002492 char display[512], auth_display[512];
2493 char hostname[MAXHOSTNAMELEN];
Damien Miller2b9b0452005-07-17 17:19:24 +10002494 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002495
2496 if (no_x11_forwarding_flag) {
2497 packet_send_debug("X11 forwarding disabled in user configuration file.");
2498 return 0;
2499 }
2500 if (!options.x11_forwarding) {
2501 debug("X11 forwarding disabled in server configuration file.");
2502 return 0;
2503 }
2504 if (!options.xauth_location ||
2505 (stat(options.xauth_location, &st) == -1)) {
2506 packet_send_debug("No xauth program; cannot forward with spoofing.");
2507 return 0;
2508 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002509 if (options.use_login) {
2510 packet_send_debug("X11 forwarding disabled; "
2511 "not compatible with UseLogin=yes.");
2512 return 0;
2513 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002514 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002515 debug("X11 display already set.");
2516 return 0;
2517 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002518 if (x11_create_display_inet(options.x11_display_offset,
2519 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002520 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002521 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002522 return 0;
2523 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002524 for (i = 0; s->x11_chanids[i] != -1; i++) {
2525 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002526 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002527 }
Kevin Steves366298c2001-12-19 17:58:01 +00002528
2529 /* Set up a suitable value for the DISPLAY variable. */
2530 if (gethostname(hostname, sizeof(hostname)) < 0)
2531 fatal("gethostname: %.100s", strerror(errno));
2532 /*
2533 * auth_display must be used as the displayname when the
2534 * authorization entry is added with xauth(1). This will be
2535 * different than the DISPLAY string for localhost displays.
2536 */
Damien Miller95c249f2002-02-05 12:11:34 +11002537 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002538 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002539 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002540 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002541 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002542 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002543 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002544 } else {
2545#ifdef IPADDR_IN_DISPLAY
2546 struct hostent *he;
2547 struct in_addr my_addr;
2548
2549 he = gethostbyname(hostname);
2550 if (he == NULL) {
2551 error("Can't get IP address for X11 DISPLAY.");
2552 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2553 return 0;
2554 }
2555 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002556 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002557 s->display_number, s->screen);
2558#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002559 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002560 s->display_number, s->screen);
2561#endif
2562 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002563 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002564 }
2565
Ben Lindstrom768176b2001-06-09 01:29:12 +00002566 return 1;
2567}
2568
Ben Lindstrombba81212001-06-25 05:01:22 +00002569static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002570do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002571{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002572 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002573}
2574
2575void
2576do_cleanup(Authctxt *authctxt)
2577{
2578 static int called = 0;
2579
2580 debug("do_cleanup");
2581
2582 /* no cleanup if we're in the child for login shell */
2583 if (is_child)
2584 return;
2585
2586 /* avoid double cleanup */
2587 if (called)
2588 return;
2589 called = 1;
2590
Darren Tucker9142e1c2007-08-16 23:28:04 +10002591 if (authctxt == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002592 return;
Darren Tucker9142e1c2007-08-16 23:28:04 +10002593
2594#ifdef USE_PAM
2595 if (options.use_pam) {
2596 sshpam_cleanup();
2597 sshpam_thread_cleanup();
2598 }
2599#endif
2600
2601 if (!authctxt->authenticated)
2602 return;
2603
Darren Tucker3e33cec2003-10-02 16:12:36 +10002604#ifdef KRB5
2605 if (options.kerberos_ticket_cleanup &&
2606 authctxt->krb5_ctx)
2607 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002608#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002609
2610#ifdef GSSAPI
2611 if (compat20 && options.gss_cleanup_creds)
2612 ssh_gssapi_cleanup_creds();
2613#endif
2614
2615 /* remove agent socket */
2616 auth_sock_cleanup_proc(authctxt->pw);
2617
2618 /*
2619 * Cleanup ptys/utmp only if privsep is disabled,
2620 * or if running in monitor.
2621 */
2622 if (!use_privsep || mm_is_monitor())
2623 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002624}