blob: 6fb63d3ad90b53c8f314b93996bf3181d2cca296 [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
Damien Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000012 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Damien Miller1cf76d92006-03-15 12:01:14 +110036RCSID("$OpenBSD: session.c,v 1.197 2006/02/28 01:10:21 djm Exp $");
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Millerf17883e2006-03-15 11:45:54 +110039#ifdef HAVE_SYS_STAT_H
40# include <sys/stat.h>
41#endif
Damien Miller9cf6d072006-03-15 11:29:24 +110042#include <sys/wait.h>
Damien Miller574c41f2006-03-15 11:40:10 +110043#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110044
45#include <paths.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110046#include <signal.h>
Damien Millerb38eff82000-04-01 11:09:21 +100047
Damien Millerb38eff82000-04-01 11:09:21 +100048#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "ssh1.h"
50#include "ssh2.h"
51#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000052#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100053#include "packet.h"
54#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100055#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100056#include "uidswap.h"
57#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000058#include "channels.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100059#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100061#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "pathnames.h"
63#include "log.h"
64#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000065#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "serverloop.h"
67#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000068#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100069#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "monitor_wrap.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100071
Darren Tucker3c78c5e2004-01-23 22:03:10 +110072#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110073#include <kafs.h>
74#endif
75
Darren Tucker0efd1552003-08-26 11:49:55 +100076#ifdef GSSAPI
77#include "ssh-gss.h"
78#endif
79
Damien Millerb38eff82000-04-01 11:09:21 +100080/* func */
81
82Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000083void session_set_fds(Session *, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +100084void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000085void session_proctitle(Session *);
86int session_setup_x11fwd(Session *);
87void do_exec_pty(Session *, const char *);
88void do_exec_no_pty(Session *, const char *);
89void do_exec(Session *, const char *);
90void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +000091#ifdef LOGIN_NEEDS_UTMPX
92static void do_pre_login(Session *s);
93#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +000094void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +100095void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000096int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +100097
Ben Lindstrombba81212001-06-25 05:01:22 +000098static void do_authenticated1(Authctxt *);
99static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000100
Ben Lindstrombba81212001-06-25 05:01:22 +0000101static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000102
Damien Millerb38eff82000-04-01 11:09:21 +1000103/* import */
104extern ServerOptions options;
105extern char *__progname;
106extern int log_stderr;
107extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000108extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000109extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000110extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000111extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000112
Damien Miller7b28dc52000-09-05 13:34:53 +1100113/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000114const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100115
Damien Millerb38eff82000-04-01 11:09:21 +1000116/* data */
117#define MAX_SESSIONS 10
118Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100119
Damien Millerad833b32000-08-23 10:46:23 +1000120#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000121login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000122#endif
123
Darren Tucker3e33cec2003-10-02 16:12:36 +1000124static int is_child = 0;
125
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000126/* Name and directory of socket for authentication agent forwarding. */
127static char *auth_sock_name = NULL;
128static char *auth_sock_dir = NULL;
129
130/* removes the agent forwarding socket */
131
132static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000133auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000134{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000135 if (auth_sock_name != NULL) {
136 temporarily_use_uid(pw);
137 unlink(auth_sock_name);
138 rmdir(auth_sock_dir);
139 auth_sock_name = NULL;
140 restore_uid();
141 }
142}
143
144static int
145auth_input_request_forwarding(struct passwd * pw)
146{
147 Channel *nc;
148 int sock;
149 struct sockaddr_un sunaddr;
150
151 if (auth_sock_name != NULL) {
152 error("authentication forwarding requested twice.");
153 return 0;
154 }
155
156 /* Temporarily drop privileged uid for mkdir/bind. */
157 temporarily_use_uid(pw);
158
159 /* Allocate a buffer for the socket name, and format the name. */
160 auth_sock_name = xmalloc(MAXPATHLEN);
161 auth_sock_dir = xmalloc(MAXPATHLEN);
Darren Tucker072a7b12003-10-15 16:10:25 +1000162 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000163
164 /* Create private directory for socket */
165 if (mkdtemp(auth_sock_dir) == NULL) {
166 packet_send_debug("Agent forwarding disabled: "
167 "mkdtemp() failed: %.100s", strerror(errno));
168 restore_uid();
169 xfree(auth_sock_name);
170 xfree(auth_sock_dir);
171 auth_sock_name = NULL;
172 auth_sock_dir = NULL;
173 return 0;
174 }
Ben Lindstromce0f6342002-06-11 16:42:49 +0000175 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
176 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000177
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000178 /* Create the socket. */
179 sock = socket(AF_UNIX, SOCK_STREAM, 0);
180 if (sock < 0)
181 packet_disconnect("socket: %.100s", strerror(errno));
182
183 /* Bind it to the name. */
184 memset(&sunaddr, 0, sizeof(sunaddr));
185 sunaddr.sun_family = AF_UNIX;
186 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
187
188 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
189 packet_disconnect("bind: %.100s", strerror(errno));
190
191 /* Restore the privileged uid. */
192 restore_uid();
193
194 /* Start listening on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +1100195 if (listen(sock, SSH_LISTEN_BACKLOG) < 0)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000196 packet_disconnect("listen: %.100s", strerror(errno));
197
198 /* Allocate a channel for the authentication agent socket. */
199 nc = channel_new("auth socket",
200 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
201 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000202 0, "auth socket", 1);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000203 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
204 return 1;
205}
206
Darren Tucker1921ed92004-02-10 13:23:28 +1100207static void
208display_loginmsg(void)
209{
Damien Miller46d38de2005-07-17 17:02:09 +1000210 if (buffer_len(&loginmsg) > 0) {
211 buffer_append(&loginmsg, "\0", 1);
212 printf("%s", (char *)buffer_ptr(&loginmsg));
213 buffer_clear(&loginmsg);
214 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100215}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000216
Ben Lindstromb31783d2001-03-22 02:02:12 +0000217void
218do_authenticated(Authctxt *authctxt)
219{
Damien Miller97f39ae2003-02-24 11:57:01 +1100220 setproctitle("%s", authctxt->pw->pw_name);
221
Ben Lindstromb31783d2001-03-22 02:02:12 +0000222 /* setup the channel layer */
223 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
224 channel_permit_all_opens();
225
226 if (compat20)
227 do_authenticated2(authctxt);
228 else
229 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000230
Darren Tucker3e33cec2003-10-02 16:12:36 +1000231 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000232}
233
Damien Millerb38eff82000-04-01 11:09:21 +1000234/*
Damien Millerb38eff82000-04-01 11:09:21 +1000235 * Prepares for an interactive session. This is called after the user has
236 * been successfully authenticated. During this message exchange, pseudo
237 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
238 * are requested, etc.
239 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000241do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000242{
243 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000244 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100245 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000246 int enable_compression_after_reply = 0;
247 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000248
249 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100250 if (s == NULL) {
251 error("no more sessions");
252 return;
253 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000254 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000255 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000256
Damien Millerb38eff82000-04-01 11:09:21 +1000257 /*
258 * We stay in this loop until the client requests to execute a shell
259 * or a command.
260 */
261 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000262 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000263
264 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100265 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000266
267 /* Process the packet. */
268 switch (type) {
269 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000270 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100271 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000272 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000273 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100274 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000275 break;
276 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000277 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000278 debug2("compression disabled");
279 break;
280 }
Damien Millerb38eff82000-04-01 11:09:21 +1000281 /* Enable compression after we have responded with SUCCESS. */
282 enable_compression_after_reply = 1;
283 success = 1;
284 break;
285
286 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000287 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000288 break;
289
290 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000291 s->auth_proto = packet_get_string(&proto_len);
292 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000293
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000294 screen_flag = packet_get_protocol_flags() &
295 SSH_PROTOFLAG_SCREEN_NUMBER;
296 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
297
298 if (packet_remaining() == 4) {
299 if (!screen_flag)
300 debug2("Buggy client: "
301 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000302 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000303 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000304 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000305 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100306 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000307 success = session_setup_x11fwd(s);
308 if (!success) {
309 xfree(s->auth_proto);
310 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000311 s->auth_proto = NULL;
312 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000313 }
Damien Millerb38eff82000-04-01 11:09:21 +1000314 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000315
316 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
317 if (no_agent_forwarding_flag || compat13) {
318 debug("Authentication agent forwarding not permitted for this authentication.");
319 break;
320 }
321 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000322 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000323 break;
324
325 case SSH_CMSG_PORT_FORWARD_REQUEST:
326 if (no_port_forwarding_flag) {
327 debug("Port forwarding not permitted for this authentication.");
328 break;
329 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100330 if (!options.allow_tcp_forwarding) {
331 debug("Port forwarding not permitted.");
332 break;
333 }
Damien Millerb38eff82000-04-01 11:09:21 +1000334 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000335 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000336 success = 1;
337 break;
338
339 case SSH_CMSG_MAX_PACKET_SIZE:
340 if (packet_set_maxsize(packet_get_int()) > 0)
341 success = 1;
342 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100343
Damien Millerb38eff82000-04-01 11:09:21 +1000344 case SSH_CMSG_EXEC_SHELL:
345 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000346 if (type == SSH_CMSG_EXEC_CMD) {
347 command = packet_get_string(&dlen);
348 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000349 do_exec(s, command);
350 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000351 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000352 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000353 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100354 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000355 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000356 return;
357
358 default:
359 /*
360 * Any unknown messages in this phase are ignored,
361 * and a failure message is returned.
362 */
Damien Miller996acd22003-04-09 20:59:48 +1000363 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000364 }
365 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
366 packet_send();
367 packet_write_wait();
368
369 /* Enable compression now that we have replied if appropriate. */
370 if (enable_compression_after_reply) {
371 enable_compression_after_reply = 0;
372 packet_start_compression(compression_level);
373 }
374 }
375}
376
377/*
378 * This is called to fork and execute a command when we have no tty. This
379 * will call do_child from the child, and server_loop from the parent after
380 * setting up file descriptors and such.
381 */
Damien Miller4af51302000-04-16 11:18:38 +1000382void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000383do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000384{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000385 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000386
387#ifdef USE_PIPES
388 int pin[2], pout[2], perr[2];
389 /* Allocate pipes for communicating with the program. */
390 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
391 packet_disconnect("Could not create pipes: %.100s",
392 strerror(errno));
393#else /* USE_PIPES */
394 int inout[2], err[2];
395 /* Uses socket pairs to communicate with the program. */
396 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
397 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
398 packet_disconnect("Could not create socket pairs: %.100s",
399 strerror(errno));
400#endif /* USE_PIPES */
401 if (s == NULL)
402 fatal("do_exec_no_pty: no session");
403
Damien Millere247cc42000-05-07 12:03:14 +1000404 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000405
Damien Millerc5946332001-02-28 11:46:11 +1100406#if defined(USE_PAM)
Darren Tucker1825f262004-02-24 00:01:27 +1100407 if (options.use_pam && !use_privsep)
Damien Miller4e448a32003-05-14 15:11:48 +1000408 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000409#endif /* USE_PAM */
410
Damien Millerb38eff82000-04-01 11:09:21 +1000411 /* Fork the child. */
412 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000413 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000414
Damien Millerb38eff82000-04-01 11:09:21 +1000415 /* Child. Reinitialize the log since the pid has changed. */
416 log_init(__progname, options.log_level, options.log_facility, log_stderr);
417
418 /*
419 * Create a new session and process group since the 4.4BSD
420 * setlogin() affects the entire process group.
421 */
422 if (setsid() < 0)
423 error("setsid failed: %.100s", strerror(errno));
424
425#ifdef USE_PIPES
426 /*
427 * Redirect stdin. We close the parent side of the socket
428 * pair, and make the child side the standard input.
429 */
430 close(pin[1]);
431 if (dup2(pin[0], 0) < 0)
432 perror("dup2 stdin");
433 close(pin[0]);
434
435 /* Redirect stdout. */
436 close(pout[0]);
437 if (dup2(pout[1], 1) < 0)
438 perror("dup2 stdout");
439 close(pout[1]);
440
441 /* Redirect stderr. */
442 close(perr[0]);
443 if (dup2(perr[1], 2) < 0)
444 perror("dup2 stderr");
445 close(perr[1]);
446#else /* USE_PIPES */
447 /*
448 * Redirect stdin, stdout, and stderr. Stdin and stdout will
449 * use the same socket, as some programs (particularly rdist)
450 * seem to depend on it.
451 */
452 close(inout[1]);
453 close(err[1]);
454 if (dup2(inout[0], 0) < 0) /* stdin */
455 perror("dup2 stdin");
456 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
457 perror("dup2 stdout");
458 if (dup2(err[0], 2) < 0) /* stderr */
459 perror("dup2 stderr");
460#endif /* USE_PIPES */
461
Tim Rice81ed5182002-09-25 17:38:46 -0700462#ifdef _UNICOS
463 cray_init_job(s->pw); /* set up cray jid and tmpdir */
464#endif
465
Damien Millerb38eff82000-04-01 11:09:21 +1000466 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000467 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000468 /* NOTREACHED */
469 }
Tim Rice81ed5182002-09-25 17:38:46 -0700470#ifdef _UNICOS
471 signal(WJSIGNAL, cray_job_termination_handler);
472#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100473#ifdef HAVE_CYGWIN
474 if (is_winnt)
475 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
476#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000477 if (pid < 0)
478 packet_disconnect("fork failed: %.100s", strerror(errno));
479 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000480 /* Set interactive/non-interactive mode. */
481 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000482#ifdef USE_PIPES
483 /* We are the parent. Close the child sides of the pipes. */
484 close(pin[0]);
485 close(pout[1]);
486 close(perr[1]);
487
Damien Millerefb4afe2000-04-12 18:45:05 +1000488 if (compat20) {
Darren Tucker723e9452004-06-22 12:57:08 +1000489 if (s->is_subsystem) {
490 close(perr[0]);
491 perr[0] = -1;
492 }
493 session_set_fds(s, pin[1], pout[0], perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000494 } else {
495 /* Enter the interactive session. */
496 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000497 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000498 }
Damien Millerb38eff82000-04-01 11:09:21 +1000499#else /* USE_PIPES */
500 /* We are the parent. Close the child sides of the socket pairs. */
501 close(inout[0]);
502 close(err[0]);
503
504 /*
Darren Tuckerb3850592004-03-27 16:44:21 +1100505 * Clear loginmsg, since it's the child's responsibility to display
506 * it to the user, otherwise multiple sessions may accumulate
507 * multiple copies of the login messages.
508 */
509 buffer_clear(&loginmsg);
510
511 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000512 * Enter the interactive session. Note: server_loop must be able to
513 * handle the case that fdin and fdout are the same.
514 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000515 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000516 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000517 } else {
518 server_loop(pid, inout[1], inout[1], err[1]);
519 /* server_loop has closed inout[1] and err[1]. */
520 }
Damien Millerb38eff82000-04-01 11:09:21 +1000521#endif /* USE_PIPES */
522}
523
524/*
525 * This is called to fork and execute a command when we have a tty. This
526 * will call do_child from the child, and server_loop from the parent after
527 * setting up file descriptors, controlling tty, updating wtmp, utmp,
528 * lastlog, and other such operations.
529 */
Damien Miller4af51302000-04-16 11:18:38 +1000530void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000531do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000532{
Damien Millerb38eff82000-04-01 11:09:21 +1000533 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000534 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000535
536 if (s == NULL)
537 fatal("do_exec_pty: no session");
538 ptyfd = s->ptyfd;
539 ttyfd = s->ttyfd;
540
Damien Millerc5946332001-02-28 11:46:11 +1100541#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000542 if (options.use_pam) {
Damien Miller341c6e62003-09-02 23:18:52 +1000543 do_pam_set_tty(s->tty);
Darren Tuckeref3a4a22004-02-06 15:30:50 +1100544 if (!use_privsep)
545 do_pam_setcred(1);
Damien Miller4e448a32003-05-14 15:11:48 +1000546 }
Damien Miller5a761312001-02-27 09:28:23 +1100547#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000548
Damien Millerb38eff82000-04-01 11:09:21 +1000549 /* Fork the child. */
550 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000551 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000552
Damien Miller942da032000-08-18 13:59:06 +1000553 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000554 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000555 /* Close the master side of the pseudo tty. */
556 close(ptyfd);
557
558 /* Make the pseudo tty our controlling tty. */
559 pty_make_controlling_tty(&ttyfd, s->tty);
560
Damien Miller9c751422001-10-10 15:02:46 +1000561 /* Redirect stdin/stdout/stderr from the pseudo tty. */
562 if (dup2(ttyfd, 0) < 0)
563 error("dup2 stdin: %s", strerror(errno));
564 if (dup2(ttyfd, 1) < 0)
565 error("dup2 stdout: %s", strerror(errno));
566 if (dup2(ttyfd, 2) < 0)
567 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000568
569 /* Close the extra descriptor for the pseudo tty. */
570 close(ttyfd);
571
Damien Miller942da032000-08-18 13:59:06 +1000572 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000573#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700574 if (!(options.use_login && command == NULL)) {
575#ifdef _UNICOS
576 cray_init_job(s->pw); /* set up cray jid and tmpdir */
577#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100578 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700579 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000580# ifdef LOGIN_NEEDS_UTMPX
581 else
582 do_pre_login(s);
583# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000584#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000585
Damien Millerb38eff82000-04-01 11:09:21 +1000586 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000587 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000588 /* NOTREACHED */
589 }
Tim Rice81ed5182002-09-25 17:38:46 -0700590#ifdef _UNICOS
591 signal(WJSIGNAL, cray_job_termination_handler);
592#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100593#ifdef HAVE_CYGWIN
594 if (is_winnt)
595 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
596#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000597 if (pid < 0)
598 packet_disconnect("fork failed: %.100s", strerror(errno));
599 s->pid = pid;
600
601 /* Parent. Close the slave side of the pseudo tty. */
602 close(ttyfd);
603
604 /*
605 * Create another descriptor of the pty master side for use as the
606 * standard input. We could use the original descriptor, but this
607 * simplifies code in server_loop. The descriptor is bidirectional.
608 */
609 fdout = dup(ptyfd);
610 if (fdout < 0)
611 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
612
613 /* we keep a reference to the pty master */
614 ptymaster = dup(ptyfd);
615 if (ptymaster < 0)
616 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
617 s->ptymaster = ptymaster;
618
619 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000620 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000621 if (compat20) {
622 session_set_fds(s, ptyfd, fdout, -1);
623 } else {
624 server_loop(pid, ptyfd, fdout, -1);
625 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000626 }
Damien Millerb38eff82000-04-01 11:09:21 +1000627}
628
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000629#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000630static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000631do_pre_login(Session *s)
632{
633 socklen_t fromlen;
634 struct sockaddr_storage from;
635 pid_t pid = getpid();
636
637 /*
638 * Get IP address of client. If the connection is not a socket, let
639 * the address be 0.0.0.0.
640 */
641 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000642 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000643 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000644 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100645 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000646 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000647 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000648 }
649 }
650
651 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000652 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000653 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000654}
655#endif
656
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000657/*
658 * This is called to fork and execute a command. If another command is
659 * to be forced, execute that instead.
660 */
661void
662do_exec(Session *s, const char *command)
663{
664 if (forced_command) {
665 original_command = command;
666 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000667 debug("Forced command '%.900s'", command);
668 }
669
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100670#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100671 if (command != NULL)
672 PRIVSEP(audit_run_command(command));
673 else if (s->ttyfd == -1) {
674 char *shell = s->pw->pw_shell;
675
676 if (shell[0] == '\0') /* empty shell means /bin/sh */
677 shell =_PATH_BSHELL;
678 PRIVSEP(audit_run_command(shell));
679 }
680#endif
681
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000682 if (s->ttyfd != -1)
683 do_exec_pty(s, command);
684 else
685 do_exec_no_pty(s, command);
686
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000687 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000688
Darren Tucker09991742004-07-17 17:05:14 +1000689 /*
690 * Clear loginmsg: it's the child's responsibility to display
691 * it to the user, otherwise multiple sessions may accumulate
692 * multiple copies of the login messages.
693 */
694 buffer_clear(&loginmsg);
695}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000696
Damien Miller942da032000-08-18 13:59:06 +1000697/* administrative, login(1)-like work */
698void
Damien Miller69b69aa2000-10-28 14:19:58 +1100699do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000700{
Damien Miller942da032000-08-18 13:59:06 +1000701 socklen_t fromlen;
702 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000703 struct passwd * pw = s->pw;
704 pid_t pid = getpid();
705
706 /*
707 * Get IP address of client. If the connection is not a socket, let
708 * the address be 0.0.0.0.
709 */
710 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000711 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000712 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000713 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000714 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000715 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000716 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000717 }
718 }
719
720 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000721 if (!use_privsep)
722 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
723 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000724 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000725 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000726
Kevin Steves092f2ef2000-10-14 13:36:13 +0000727#ifdef USE_PAM
728 /*
729 * If password change is needed, do it now.
730 * This needs to occur before the ~/.hushlogin check.
731 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100732 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
733 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000734 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100735 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000736 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000737 }
738#endif
739
Damien Millercf205e82001-04-16 18:29:15 +1000740 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000741 return;
742
Darren Tucker1921ed92004-02-10 13:23:28 +1100743 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000744
Damien Millercf205e82001-04-16 18:29:15 +1000745 do_motd();
746}
747
748/*
749 * Display the message of the day.
750 */
751void
752do_motd(void)
753{
754 FILE *f;
755 char buf[256];
756
Damien Miller942da032000-08-18 13:59:06 +1000757 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000758#ifdef HAVE_LOGIN_CAP
759 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
760 "/etc/motd"), "r");
761#else
Damien Miller942da032000-08-18 13:59:06 +1000762 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000763#endif
Damien Miller942da032000-08-18 13:59:06 +1000764 if (f) {
765 while (fgets(buf, sizeof(buf), f))
766 fputs(buf, stdout);
767 fclose(f);
768 }
769 }
770}
771
Kevin Steves8f63caa2001-07-04 18:23:02 +0000772
773/*
774 * Check for quiet login, either .hushlogin or command given.
775 */
776int
777check_quietlogin(Session *s, const char *command)
778{
779 char buf[256];
780 struct passwd *pw = s->pw;
781 struct stat st;
782
783 /* Return 1 if .hushlogin exists or a command given. */
784 if (command != NULL)
785 return 1;
786 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
787#ifdef HAVE_LOGIN_CAP
788 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
789 return 1;
790#else
791 if (stat(buf, &st) >= 0)
792 return 1;
793#endif
794 return 0;
795}
796
Damien Millerb38eff82000-04-01 11:09:21 +1000797/*
798 * Sets the value of the given variable in the environment. If the variable
799 * already exists, its value is overriden.
800 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000801void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000802child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100803 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000804{
Damien Millerb38eff82000-04-01 11:09:21 +1000805 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000806 u_int envsize;
807 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000808
809 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000810 * If we're passed an uninitialized list, allocate a single null
811 * entry before continuing.
812 */
813 if (*envp == NULL && *envsizep == 0) {
814 *envp = xmalloc(sizeof(char *));
815 *envp[0] = NULL;
816 *envsizep = 1;
817 }
818
819 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000820 * Find the slot where the value should be stored. If the variable
821 * already exists, we reuse the slot; otherwise we append a new slot
822 * at the end of the array, expanding if necessary.
823 */
824 env = *envp;
825 namelen = strlen(name);
826 for (i = 0; env[i]; i++)
827 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
828 break;
829 if (env[i]) {
830 /* Reuse the slot. */
831 xfree(env[i]);
832 } else {
833 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000834 envsize = *envsizep;
835 if (i >= envsize - 1) {
836 if (envsize >= 1000)
837 fatal("child_set_env: too many env vars");
838 envsize += 50;
839 env = (*envp) = xrealloc(env, envsize * sizeof(char *));
840 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000841 }
842 /* Need to set the NULL pointer at end of array beyond the new slot. */
843 env[i + 1] = NULL;
844 }
845
846 /* Allocate space and format the variable in the appropriate slot. */
847 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
848 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
849}
850
851/*
852 * Reads environment variables from the given file and adds/overrides them
853 * into the environment. If the file does not exist, this does nothing.
854 * Otherwise, it must consist of empty lines, comments (line starts with '#')
855 * and assignments of the form name=value. No other forms are allowed.
856 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000857static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000858read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100859 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000860{
861 FILE *f;
862 char buf[4096];
863 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000864 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000865
866 f = fopen(filename, "r");
867 if (!f)
868 return;
869
870 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000871 if (++lineno > 1000)
872 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000873 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
874 ;
875 if (!*cp || *cp == '#' || *cp == '\n')
876 continue;
877 if (strchr(cp, '\n'))
878 *strchr(cp, '\n') = '\0';
879 value = strchr(cp, '=');
880 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000881 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
882 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000883 continue;
884 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000885 /*
886 * Replace the equals sign by nul, and advance value to
887 * the value string.
888 */
Damien Millerb38eff82000-04-01 11:09:21 +1000889 *value = '\0';
890 value++;
891 child_set_env(env, envsize, cp, value);
892 }
893 fclose(f);
894}
895
Darren Tuckere1a790d2003-09-16 11:52:19 +1000896#ifdef HAVE_ETC_DEFAULT_LOGIN
897/*
898 * Return named variable from specified environment, or NULL if not present.
899 */
900static char *
901child_get_env(char **env, const char *name)
902{
903 int i;
904 size_t len;
905
906 len = strlen(name);
907 for (i=0; env[i] != NULL; i++)
908 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
909 return(env[i] + len + 1);
910 return NULL;
911}
912
913/*
914 * Read /etc/default/login.
915 * We pick up the PATH (or SUPATH for root) and UMASK.
916 */
917static void
918read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
919{
920 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000921 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000922 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000923
924 /*
925 * We don't want to copy the whole file to the child's environment,
926 * so we use a temporary environment and copy the variables we're
927 * interested in.
928 */
929 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
930
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000931 if (tmpenv == NULL)
932 return;
933
Darren Tuckere1a790d2003-09-16 11:52:19 +1000934 if (uid == 0)
935 var = child_get_env(tmpenv, "SUPATH");
936 else
937 var = child_get_env(tmpenv, "PATH");
938 if (var != NULL)
939 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100940
Darren Tuckere1a790d2003-09-16 11:52:19 +1000941 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
942 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000943 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100944
Darren Tuckere1a790d2003-09-16 11:52:19 +1000945 for (i = 0; tmpenv[i] != NULL; i++)
946 xfree(tmpenv[i]);
947 xfree(tmpenv);
948}
949#endif /* HAVE_ETC_DEFAULT_LOGIN */
950
Damien Miller7dff8692005-05-26 11:34:51 +1000951void
952copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000953{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100954 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000955 int i;
956
Damien Millerbb9ffc12002-01-08 10:59:32 +1100957 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000958 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000959
Damien Millerbb9ffc12002-01-08 10:59:32 +1100960 for(i = 0; source[i] != NULL; i++) {
961 var_name = xstrdup(source[i]);
962 if ((var_val = strstr(var_name, "=")) == NULL) {
963 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000964 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000965 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100966 *var_val++ = '\0';
967
968 debug3("Copy environment: %s=%s", var_name, var_val);
969 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +1100970
Damien Millerbb9ffc12002-01-08 10:59:32 +1100971 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000972 }
973}
Damien Millercb5e44a2000-09-29 12:12:36 +1100974
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000975static char **
976do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000977{
Damien Millerb38eff82000-04-01 11:09:21 +1000978 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000979 u_int i, envsize;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000980 char **env, *laddr, *path = NULL;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000981 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000982
Damien Millerb38eff82000-04-01 11:09:21 +1000983 /* Initialize the environment. */
984 envsize = 100;
985 env = xmalloc(envsize * sizeof(char *));
986 env[0] = NULL;
987
Damien Millerbac2d8a2000-09-05 16:13:06 +1100988#ifdef HAVE_CYGWIN
989 /*
990 * The Windows environment contains some setting which are
991 * important for a running system. They must not be dropped.
992 */
Darren Tucker14c372d2004-08-30 20:42:08 +1000993 {
994 char **p;
995
996 p = fetch_windows_environment();
997 copy_environment(p, &env, &envsize);
998 free_windows_environment(p);
999 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001000#endif
1001
Darren Tucker0efd1552003-08-26 11:49:55 +10001002#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001003 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001004 * the childs environment as they see fit
1005 */
1006 ssh_gssapi_do_child(&env, &envsize);
1007#endif
1008
Damien Millerb38eff82000-04-01 11:09:21 +10001009 if (!options.use_login) {
1010 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001011 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001012 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001013 s->env[i].val);
1014
Damien Millerb38eff82000-04-01 11:09:21 +10001015 child_set_env(&env, &envsize, "USER", pw->pw_name);
1016 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001017#ifdef _AIX
1018 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1019#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001020 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001021#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001022 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1023 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1024 else
1025 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001026#else /* HAVE_LOGIN_CAP */
1027# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001028 /*
1029 * There's no standard path on Windows. The path contains
1030 * important components pointing to the system directories,
1031 * needed for loading shared libraries. So the path better
1032 * remains intact here.
1033 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001034# ifdef HAVE_ETC_DEFAULT_LOGIN
1035 read_etc_default_login(&env, &envsize, pw->pw_uid);
1036 path = child_get_env(env, "PATH");
1037# endif /* HAVE_ETC_DEFAULT_LOGIN */
1038 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001039 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001040 s->pw->pw_uid == 0 ?
1041 SUPERUSER_PATH : _PATH_STDPATH);
1042 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001043# endif /* HAVE_CYGWIN */
1044#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001045
1046 snprintf(buf, sizeof buf, "%.200s/%.50s",
1047 _PATH_MAILDIR, pw->pw_name);
1048 child_set_env(&env, &envsize, "MAIL", buf);
1049
1050 /* Normal systems set SHELL by default. */
1051 child_set_env(&env, &envsize, "SHELL", shell);
1052 }
1053 if (getenv("TZ"))
1054 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1055
1056 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001057 if (!options.use_login) {
1058 while (custom_environment) {
1059 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001060 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001061
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001062 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001063 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001064 if (str[i] == '=') {
1065 str[i] = 0;
1066 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001067 }
1068 custom_environment = ce->next;
1069 xfree(ce->s);
1070 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001071 }
Damien Millerb38eff82000-04-01 11:09:21 +10001072 }
1073
Damien Millerf37e2462002-09-19 11:47:55 +10001074 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001075 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001076 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001077 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1078
Damien Miller00111382003-03-10 11:21:17 +11001079 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001080 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001081 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1082 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001083 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1084
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001085 if (s->ttyfd != -1)
1086 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1087 if (s->term)
1088 child_set_env(&env, &envsize, "TERM", s->term);
1089 if (s->display)
1090 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001091 if (original_command)
1092 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1093 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001094
Tim Rice81ed5182002-09-25 17:38:46 -07001095#ifdef _UNICOS
1096 if (cray_tmpdir[0] != '\0')
1097 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1098#endif /* _UNICOS */
1099
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001100 /*
1101 * Since we clear KRB5CCNAME at startup, if it's set now then it
1102 * must have been set by a native authentication method (eg AIX or
1103 * SIA), so copy it to the child.
1104 */
1105 {
1106 char *cp;
1107
1108 if ((cp = getenv("KRB5CCNAME")) != NULL)
1109 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1110 }
1111
Damien Millerb38eff82000-04-01 11:09:21 +10001112#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001113 {
1114 char *cp;
1115
1116 if ((cp = getenv("AUTHSTATE")) != NULL)
1117 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001118 read_environment_file(&env, &envsize, "/etc/environment");
1119 }
Damien Millerb38eff82000-04-01 11:09:21 +10001120#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001121#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001122 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001123 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001124 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001125#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001126#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001127 /*
1128 * Pull in any environment variables that may have
1129 * been set by PAM.
1130 */
Damien Miller4e448a32003-05-14 15:11:48 +10001131 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001132 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001133
Damien Millerc756e9b2003-11-17 21:41:42 +11001134 p = fetch_pam_child_environment();
1135 copy_environment(p, &env, &envsize);
1136 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001137
Damien Millerc756e9b2003-11-17 21:41:42 +11001138 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001139 copy_environment(p, &env, &envsize);
1140 free_pam_environment(p);
1141 }
Damien Millerb38eff82000-04-01 11:09:21 +10001142#endif /* USE_PAM */
1143
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001144 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001145 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001146 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001147
1148 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001149 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001150 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001151 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001152 read_environment_file(&env, &envsize, buf);
1153 }
1154 if (debug_flag) {
1155 /* dump the environment */
1156 fprintf(stderr, "Environment:\n");
1157 for (i = 0; env[i]; i++)
1158 fprintf(stderr, " %.200s\n", env[i]);
1159 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001160 return env;
1161}
1162
1163/*
1164 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1165 * first in this order).
1166 */
1167static void
1168do_rc_files(Session *s, const char *shell)
1169{
1170 FILE *f = NULL;
1171 char cmd[1024];
1172 int do_xauth;
1173 struct stat st;
1174
1175 do_xauth =
1176 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1177
1178 /* ignore _PATH_SSH_USER_RC for subsystems */
1179 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1180 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1181 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1182 if (debug_flag)
1183 fprintf(stderr, "Running %s\n", cmd);
1184 f = popen(cmd, "w");
1185 if (f) {
1186 if (do_xauth)
1187 fprintf(f, "%s %s\n", s->auth_proto,
1188 s->auth_data);
1189 pclose(f);
1190 } else
1191 fprintf(stderr, "Could not run %s\n",
1192 _PATH_SSH_USER_RC);
1193 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1194 if (debug_flag)
1195 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1196 _PATH_SSH_SYSTEM_RC);
1197 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1198 if (f) {
1199 if (do_xauth)
1200 fprintf(f, "%s %s\n", s->auth_proto,
1201 s->auth_data);
1202 pclose(f);
1203 } else
1204 fprintf(stderr, "Could not run %s\n",
1205 _PATH_SSH_SYSTEM_RC);
1206 } else if (do_xauth && options.xauth_location != NULL) {
1207 /* Add authority data to .Xauthority if appropriate. */
1208 if (debug_flag) {
1209 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001210 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001211 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001212 fprintf(stderr,
1213 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001214 options.xauth_location, s->auth_display,
1215 s->auth_proto, s->auth_data);
1216 }
1217 snprintf(cmd, sizeof cmd, "%s -q -",
1218 options.xauth_location);
1219 f = popen(cmd, "w");
1220 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001221 fprintf(f, "remove %s\n",
1222 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001223 fprintf(f, "add %s %s %s\n",
1224 s->auth_display, s->auth_proto,
1225 s->auth_data);
1226 pclose(f);
1227 } else {
1228 fprintf(stderr, "Could not run %s\n",
1229 cmd);
1230 }
1231 }
1232}
1233
1234static void
1235do_nologin(struct passwd *pw)
1236{
1237 FILE *f = NULL;
1238 char buf[1024];
1239
1240#ifdef HAVE_LOGIN_CAP
1241 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1242 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1243 _PATH_NOLOGIN), "r");
1244#else
1245 if (pw->pw_uid)
1246 f = fopen(_PATH_NOLOGIN, "r");
1247#endif
1248 if (f) {
1249 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001250 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001251 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001252 while (fgets(buf, sizeof(buf), f))
1253 fputs(buf, stderr);
1254 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001255 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001256 exit(254);
1257 }
1258}
1259
1260/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001262do_setusercontext(struct passwd *pw)
1263{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001264#ifndef HAVE_CYGWIN
1265 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001266#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001267 {
1268
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001269#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001270 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1271 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001272#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001273#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001274# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001275 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001276# endif
Darren Tuckerc97b01a2005-02-16 16:47:37 +11001277#ifdef GSSAPI
1278 if (options.gss_authentication) {
1279 temporarily_use_uid(pw);
1280 ssh_gssapi_storecreds();
1281 restore_uid();
1282 }
1283#endif
Damien Millerd3526362004-01-23 14:16:26 +11001284# ifdef USE_PAM
1285 if (options.use_pam) {
1286 do_pam_session();
1287 do_pam_setcred(0);
1288 }
1289# endif /* USE_PAM */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001290 if (setusercontext(lc, pw, pw->pw_uid,
1291 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1292 perror("unable to set user context");
1293 exit(1);
1294 }
1295#else
1296# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1297 /* Sets login uid for accounting */
1298 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1299 error("setluid: %s", strerror(errno));
1300# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1301
1302 if (setlogin(pw->pw_name) < 0)
1303 error("setlogin failed: %s", strerror(errno));
1304 if (setgid(pw->pw_gid) < 0) {
1305 perror("setgid");
1306 exit(1);
1307 }
1308 /* Initialize the group list. */
1309 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1310 perror("initgroups");
1311 exit(1);
1312 }
1313 endgrent();
Darren Tuckerc97b01a2005-02-16 16:47:37 +11001314#ifdef GSSAPI
1315 if (options.gss_authentication) {
1316 temporarily_use_uid(pw);
1317 ssh_gssapi_storecreds();
1318 restore_uid();
1319 }
1320#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001321# ifdef USE_PAM
1322 /*
Damien Millera8e06ce2003-11-21 23:48:55 +11001323 * PAM credentials may take the form of supplementary groups.
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001324 * These will have been wiped by the above initgroups() call.
1325 * Reestablish them here.
1326 */
Damien Miller341c6e62003-09-02 23:18:52 +10001327 if (options.use_pam) {
1328 do_pam_session();
Damien Miller4e448a32003-05-14 15:11:48 +10001329 do_pam_setcred(0);
Damien Miller341c6e62003-09-02 23:18:52 +10001330 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001331# endif /* USE_PAM */
1332# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1333 irix_setusercontext(pw);
1334# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001335# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001336 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001337# endif /* _AIX */
Tim Rice66fd2172005-08-31 09:59:49 -07001338#if defined(HAVE_LIBIAF) && !defined(BROKEN_LIBIAF)
Tim Rice2291c002005-08-26 13:15:19 -07001339 if (set_id(pw->pw_name) != 0) {
1340 exit(1);
1341 }
Tim Rice66fd2172005-08-31 09:59:49 -07001342#endif /* HAVE_LIBIAF && !BROKEN_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001343 /* Permanently switch to the desired uid. */
1344 permanently_set_uid(pw);
1345#endif
1346 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001347
1348#ifdef HAVE_CYGWIN
1349 if (is_winnt)
1350#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001351 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1352 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1353}
1354
Ben Lindstrom08105192002-03-22 02:50:06 +00001355static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001356do_pwchange(Session *s)
1357{
Darren Tucker09991742004-07-17 17:05:14 +10001358 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001359 fprintf(stderr, "WARNING: Your password has expired.\n");
1360 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001361 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001362 "You must change your password now and login again!\n");
Darren Tucker33370e02005-02-09 22:17:28 +11001363#ifdef PASSWD_NEEDS_USERNAME
1364 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1365 (char *)NULL);
1366#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001367 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001368#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001369 perror("passwd");
1370 } else {
1371 fprintf(stderr,
1372 "Password change required but no TTY available.\n");
1373 }
1374 exit(1);
1375}
1376
1377static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001378launch_login(struct passwd *pw, const char *hostname)
1379{
1380 /* Launch login(1). */
1381
Ben Lindstrom378a4172002-06-07 14:49:56 +00001382 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001383#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001384 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001386#ifdef LOGIN_NO_ENDOPT
1387 "-p", "-f", pw->pw_name, (char *)NULL);
1388#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001390#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391
1392 /* Login couldn't be executed, die. */
1393
1394 perror("login");
1395 exit(1);
1396}
1397
Darren Tucker23bc8d02004-02-06 16:24:31 +11001398static void
1399child_close_fds(void)
1400{
1401 int i;
1402
1403 if (packet_get_connection_in() == packet_get_connection_out())
1404 close(packet_get_connection_in());
1405 else {
1406 close(packet_get_connection_in());
1407 close(packet_get_connection_out());
1408 }
1409 /*
1410 * Close all descriptors related to channels. They will still remain
1411 * open in the parent.
1412 */
1413 /* XXX better use close-on-exec? -markus */
1414 channel_close_all();
1415
1416 /*
1417 * Close any extra file descriptors. Note that there may still be
1418 * descriptors left by system functions. They will be closed later.
1419 */
1420 endpwent();
1421
1422 /*
Damien Miller788f2122005-11-05 15:14:59 +11001423 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001424 * hanging around in clients. Note that we want to do this after
1425 * initgroups, because at least on Solaris 2.3 it leaves file
1426 * descriptors open.
1427 */
1428 for (i = 3; i < 64; i++)
1429 close(i);
1430}
1431
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001432/*
1433 * Performs common processing for the child, such as setting up the
1434 * environment, closing extra file descriptors, setting the user and group
1435 * ids, and executing the command or shell.
1436 */
1437void
1438do_child(Session *s, const char *command)
1439{
1440 extern char **environ;
1441 char **env;
1442 char *argv[10];
1443 const char *shell, *shell0, *hostname = NULL;
1444 struct passwd *pw = s->pw;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001445
1446 /* remove hostkey from the child's memory */
1447 destroy_sensitive_data();
1448
Darren Tucker23bc8d02004-02-06 16:24:31 +11001449 /* Force a password change */
1450 if (s->authctxt->force_pwchange) {
1451 do_setusercontext(pw);
1452 child_close_fds();
1453 do_pwchange(s);
1454 exit(1);
1455 }
1456
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001457 /* login(1) is only called if we execute the login shell */
1458 if (options.use_login && command != NULL)
1459 options.use_login = 0;
1460
Tim Rice81ed5182002-09-25 17:38:46 -07001461#ifdef _UNICOS
1462 cray_setup(pw->pw_uid, pw->pw_name, command);
1463#endif /* _UNICOS */
1464
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001465 /*
1466 * Login(1) does this as well, and it needs uid 0 for the "-h"
1467 * switch, so we let login(1) to this for us.
1468 */
1469 if (!options.use_login) {
1470#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001471 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001472 if (!check_quietlogin(s, command))
1473 do_motd();
1474#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001475 /* When PAM is enabled we rely on it to do the nologin check */
1476 if (!options.use_pam)
1477 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001478 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001479 /*
1480 * PAM session modules in do_setusercontext may have
1481 * generated messages, so if this in an interactive
1482 * login then display them too.
1483 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001484 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001485 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001486#endif /* HAVE_OSF_SIA */
1487 }
1488
Darren Tucker69687f42004-09-11 22:17:26 +10001489#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001490 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1491 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001492 display_loginmsg();
1493 exit(254);
1494 }
1495#endif
1496
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001497 /*
1498 * Get the shell from the password data. An empty shell field is
1499 * legal, and means /bin/sh.
1500 */
1501 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001502
1503 /*
1504 * Make sure $SHELL points to the shell from the password file,
1505 * even if shell is overridden from login.conf
1506 */
1507 env = do_setup_env(s, shell);
1508
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001509#ifdef HAVE_LOGIN_CAP
1510 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1511#endif
1512
Damien Millerad833b32000-08-23 10:46:23 +10001513 /* we have to stash the hostname before we close our socket. */
1514 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001515 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001516 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001517 /*
1518 * Close the connection descriptors; note that this is the child, and
1519 * the server will still have the socket open, and it is important
1520 * that we do not shutdown it. Note that the descriptors cannot be
1521 * closed before building the environment, as we call
1522 * get_remote_ipaddr there.
1523 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001524 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001525
Damien Millerb38eff82000-04-01 11:09:21 +10001526 /*
Damien Miller05eda432002-02-10 18:32:28 +11001527 * Must take new environment into use so that .ssh/rc,
1528 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001529 */
1530 environ = env;
1531
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001532#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001533 /*
1534 * At this point, we check to see if AFS is active and if we have
1535 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1536 * if we can (and need to) extend the ticket into an AFS token. If
1537 * we don't do this, we run into potential problems if the user's
1538 * home directory is in AFS and it's not world-readable.
1539 */
1540
1541 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001542 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001543 char cell[64];
1544
1545 debug("Getting AFS token");
1546
1547 k_setpag();
1548
1549 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1550 krb5_afslog(s->authctxt->krb5_ctx,
1551 s->authctxt->krb5_fwd_ccache, cell, NULL);
1552
1553 krb5_afslog_home(s->authctxt->krb5_ctx,
1554 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1555 }
1556#endif
1557
Damien Miller788f2122005-11-05 15:14:59 +11001558 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001559 if (chdir(pw->pw_dir) < 0) {
1560 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1561 pw->pw_dir, strerror(errno));
1562#ifdef HAVE_LOGIN_CAP
1563 if (login_getcapbool(lc, "requirehome", 0))
1564 exit(1);
1565#endif
1566 }
1567
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001568 if (!options.use_login)
1569 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001570
1571 /* restore SIGPIPE for child */
1572 signal(SIGPIPE, SIG_DFL);
1573
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001574 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575 launch_login(pw, hostname);
1576 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001577 }
1578
1579 /* Get the last component of the shell name. */
1580 if ((shell0 = strrchr(shell, '/')) != NULL)
1581 shell0++;
1582 else
1583 shell0 = shell;
1584
Damien Millerb38eff82000-04-01 11:09:21 +10001585 /*
1586 * If we have no command, execute the shell. In this case, the shell
1587 * name to be passed in argv[0] is preceded by '-' to indicate that
1588 * this is a login shell.
1589 */
1590 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001591 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001592
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001593 /* Start the shell. Set initial character to '-'. */
1594 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001595
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001596 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1597 >= sizeof(argv0) - 1) {
1598 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001599 perror(shell);
1600 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001601 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001602
1603 /* Execute the shell. */
1604 argv[0] = argv0;
1605 argv[1] = NULL;
1606 execve(shell, argv, env);
1607
1608 /* Executing the shell failed. */
1609 perror(shell);
1610 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001611 }
1612 /*
1613 * Execute the command using the user's shell. This uses the -c
1614 * option to execute the command.
1615 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001616 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001617 argv[1] = "-c";
1618 argv[2] = (char *) command;
1619 argv[3] = NULL;
1620 execve(shell, argv, env);
1621 perror(shell);
1622 exit(1);
1623}
1624
1625Session *
1626session_new(void)
1627{
1628 int i;
1629 static int did_init = 0;
1630 if (!did_init) {
1631 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001632 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001633 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001634 }
1635 did_init = 1;
1636 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001637 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001638 Session *s = &sessions[i];
1639 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001640 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001641 s->chanid = -1;
1642 s->ptyfd = -1;
1643 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001644 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001645 s->self = i;
Damien Miller2b9b0452005-07-17 17:19:24 +10001646 s->x11_chanids = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001647 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001648 return s;
1649 }
1650 }
1651 return NULL;
1652}
1653
Ben Lindstrombba81212001-06-25 05:01:22 +00001654static void
Damien Millerb38eff82000-04-01 11:09:21 +10001655session_dump(void)
1656{
1657 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001658 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001659 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001660 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001661 s->used,
1662 s->self,
1663 s,
1664 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001665 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001666 }
1667}
1668
Damien Millerefb4afe2000-04-12 18:45:05 +10001669int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001670session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001671{
1672 Session *s = session_new();
1673 debug("session_open: channel %d", chanid);
1674 if (s == NULL) {
1675 error("no more sessions");
1676 return 0;
1677 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001678 s->authctxt = authctxt;
1679 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001680 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001681 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001682 debug("session_open: session %d: link with channel %d", s->self, chanid);
1683 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001684 return 1;
1685}
1686
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687Session *
1688session_by_tty(char *tty)
1689{
1690 int i;
1691 for (i = 0; i < MAX_SESSIONS; i++) {
1692 Session *s = &sessions[i];
1693 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1694 debug("session_by_tty: session %d tty %s", i, tty);
1695 return s;
1696 }
1697 }
1698 debug("session_by_tty: unknown tty %.100s", tty);
1699 session_dump();
1700 return NULL;
1701}
1702
Ben Lindstrombba81212001-06-25 05:01:22 +00001703static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001704session_by_channel(int id)
1705{
1706 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001707 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001708 Session *s = &sessions[i];
1709 if (s->used && s->chanid == id) {
1710 debug("session_by_channel: session %d channel %d", i, id);
1711 return s;
1712 }
1713 }
1714 debug("session_by_channel: unknown channel %d", id);
1715 session_dump();
1716 return NULL;
1717}
1718
Ben Lindstrombba81212001-06-25 05:01:22 +00001719static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001720session_by_x11_channel(int id)
1721{
1722 int i, j;
1723
1724 for (i = 0; i < MAX_SESSIONS; i++) {
1725 Session *s = &sessions[i];
1726
1727 if (s->x11_chanids == NULL || !s->used)
1728 continue;
1729 for (j = 0; s->x11_chanids[j] != -1; j++) {
1730 if (s->x11_chanids[j] == id) {
1731 debug("session_by_x11_channel: session %d "
1732 "channel %d", s->self, id);
1733 return s;
1734 }
1735 }
1736 }
1737 debug("session_by_x11_channel: unknown channel %d", id);
1738 session_dump();
1739 return NULL;
1740}
1741
1742static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001743session_by_pid(pid_t pid)
1744{
1745 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001746 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001747 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001748 Session *s = &sessions[i];
1749 if (s->used && s->pid == pid)
1750 return s;
1751 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001752 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001753 session_dump();
1754 return NULL;
1755}
1756
Ben Lindstrombba81212001-06-25 05:01:22 +00001757static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001758session_window_change_req(Session *s)
1759{
1760 s->col = packet_get_int();
1761 s->row = packet_get_int();
1762 s->xpixel = packet_get_int();
1763 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001764 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001765 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1766 return 1;
1767}
1768
Ben Lindstrombba81212001-06-25 05:01:22 +00001769static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001770session_pty_req(Session *s)
1771{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001772 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001773 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001774
Ben Lindstrom49c12602001-06-13 04:37:36 +00001775 if (no_pty_flag) {
1776 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001777 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001778 }
1779 if (s->ttyfd != -1) {
1780 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001781 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001782 }
1783
Damien Millerefb4afe2000-04-12 18:45:05 +10001784 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001785
1786 if (compat20) {
1787 s->col = packet_get_int();
1788 s->row = packet_get_int();
1789 } else {
1790 s->row = packet_get_int();
1791 s->col = packet_get_int();
1792 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001793 s->xpixel = packet_get_int();
1794 s->ypixel = packet_get_int();
1795
1796 if (strcmp(s->term, "") == 0) {
1797 xfree(s->term);
1798 s->term = NULL;
1799 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001800
Damien Millerefb4afe2000-04-12 18:45:05 +10001801 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001802 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001804 if (s->term)
1805 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001806 s->term = NULL;
1807 s->ptyfd = -1;
1808 s->ttyfd = -1;
1809 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001810 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001811 }
1812 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001813
1814 /* for SSH1 the tty modes length is not given */
1815 if (!compat20)
1816 n_bytes = packet_remaining();
1817 tty_parse_modes(s->ttyfd, &n_bytes);
1818
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001819 if (!use_privsep)
1820 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001821
1822 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001823 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1824
Damien Miller48b03fc2002-01-22 23:11:40 +11001825 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001826 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001827 return 1;
1828}
1829
Ben Lindstrombba81212001-06-25 05:01:22 +00001830static int
Damien Millerbd483e72000-04-30 10:00:53 +10001831session_subsystem_req(Session *s)
1832{
Damien Millerae452462001-10-10 15:08:06 +10001833 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001834 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001835 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001836 char *cmd, *subsys = packet_get_string(&len);
Damien Millereccb9de2005-06-17 12:59:34 +10001837 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001838
Damien Miller48b03fc2002-01-22 23:11:40 +11001839 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001840 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001841
Damien Millerf6d9e222000-06-18 14:50:44 +10001842 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001843 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1844 cmd = options.subsystem_command[i];
1845 if (stat(cmd, &st) < 0) {
1846 error("subsystem: cannot stat %s: %s", cmd,
1847 strerror(errno));
1848 break;
1849 }
1850 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001851 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001852 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001853 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001854 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001855 }
1856 }
1857
1858 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10001859 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001860 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001861
Damien Millerbd483e72000-04-30 10:00:53 +10001862 xfree(subsys);
1863 return success;
1864}
1865
Ben Lindstrombba81212001-06-25 05:01:22 +00001866static int
Damien Millerbd483e72000-04-30 10:00:53 +10001867session_x11_req(Session *s)
1868{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001869 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001870
Damien Miller2b9b0452005-07-17 17:19:24 +10001871 if (s->auth_proto != NULL || s->auth_data != NULL) {
1872 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11001873 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10001874 return 0;
1875 }
Damien Millerbd483e72000-04-30 10:00:53 +10001876 s->single_connection = packet_get_char();
1877 s->auth_proto = packet_get_string(NULL);
1878 s->auth_data = packet_get_string(NULL);
1879 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001880 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001881
Ben Lindstrom768176b2001-06-09 01:29:12 +00001882 success = session_setup_x11fwd(s);
1883 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001884 xfree(s->auth_proto);
1885 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001886 s->auth_proto = NULL;
1887 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001888 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001889 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001890}
1891
Ben Lindstrombba81212001-06-25 05:01:22 +00001892static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001893session_shell_req(Session *s)
1894{
Damien Miller48b03fc2002-01-22 23:11:40 +11001895 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001896 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001897 return 1;
1898}
1899
Ben Lindstrombba81212001-06-25 05:01:22 +00001900static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001901session_exec_req(Session *s)
1902{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001903 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001904 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001905 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001906 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001907 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001908 return 1;
1909}
1910
Ben Lindstrombba81212001-06-25 05:01:22 +00001911static int
Damien Miller54c45982003-05-15 10:20:13 +10001912session_break_req(Session *s)
1913{
Damien Miller54c45982003-05-15 10:20:13 +10001914
Darren Tucker1f8311c2004-05-13 16:39:33 +10001915 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10001916 packet_check_eom();
1917
Darren Tucker3bdbd842003-08-13 20:31:05 +10001918 if (s->ttyfd == -1 ||
1919 tcsendbreak(s->ttyfd, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10001920 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10001921 return 1;
1922}
1923
1924static int
Darren Tucker46bc0752004-05-02 22:11:30 +10001925session_env_req(Session *s)
1926{
1927 char *name, *val;
1928 u_int name_len, val_len, i;
1929
1930 name = packet_get_string(&name_len);
1931 val = packet_get_string(&val_len);
1932 packet_check_eom();
1933
1934 /* Don't set too many environment variables */
1935 if (s->num_env > 128) {
1936 debug2("Ignoring env request %s: too many env vars", name);
1937 goto fail;
1938 }
1939
1940 for (i = 0; i < options.num_accept_env; i++) {
1941 if (match_pattern(name, options.accept_env[i])) {
1942 debug2("Setting env %d: %s=%s", s->num_env, name, val);
1943 s->env = xrealloc(s->env, sizeof(*s->env) *
1944 (s->num_env + 1));
1945 s->env[s->num_env].name = name;
1946 s->env[s->num_env].val = val;
1947 s->num_env++;
1948 return (1);
1949 }
1950 }
1951 debug2("Ignoring env request %s: disallowed name", name);
1952
1953 fail:
1954 xfree(name);
1955 xfree(val);
1956 return (0);
1957}
1958
1959static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001960session_auth_agent_req(Session *s)
1961{
1962 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001963 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001964 if (no_agent_forwarding_flag) {
1965 debug("session_auth_agent_req: no_agent_forwarding_flag");
1966 return 0;
1967 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001968 if (called) {
1969 return 0;
1970 } else {
1971 called = 1;
1972 return auth_input_request_forwarding(s->pw);
1973 }
1974}
1975
Damien Millerc7ef63d2002-02-05 12:21:42 +11001976int
1977session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001978{
Damien Millerefb4afe2000-04-12 18:45:05 +10001979 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001980 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001981
Damien Millerc7ef63d2002-02-05 12:21:42 +11001982 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10001983 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11001984 c->self, rtype);
1985 return 0;
1986 }
1987 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001988
1989 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001990 * a session is in LARVAL state until a shell, a command
1991 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001992 */
1993 if (c->type == SSH_CHANNEL_LARVAL) {
1994 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001995 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001996 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001997 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001998 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001999 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002000 } else if (strcmp(rtype, "x11-req") == 0) {
2001 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002002 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2003 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002004 } else if (strcmp(rtype, "subsystem") == 0) {
2005 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002006 } else if (strcmp(rtype, "env") == 0) {
2007 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002008 }
2009 }
2010 if (strcmp(rtype, "window-change") == 0) {
2011 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002012 } else if (strcmp(rtype, "break") == 0) {
2013 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002014 }
Damien Millera6b1d162004-06-30 22:41:07 +10002015
Damien Millerc7ef63d2002-02-05 12:21:42 +11002016 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002017}
2018
2019void
2020session_set_fds(Session *s, int fdin, int fdout, int fderr)
2021{
2022 if (!compat20)
2023 fatal("session_set_fds: called for proto != 2.0");
2024 /*
2025 * now that have a child and a pipe to the child,
2026 * we can activate our channel and register the fd's
2027 */
2028 if (s->chanid == -1)
2029 fatal("no channel for session %d", s->self);
2030 channel_set_fds(s->chanid,
2031 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11002032 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11002033 1,
2034 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002035}
2036
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002037/*
2038 * Function to perform pty cleanup. Also called if we get aborted abnormally
2039 * (e.g., due to a dropped connection).
2040 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002041void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002042session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002043{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002044 if (s == NULL) {
2045 error("session_pty_cleanup: no session");
2046 return;
2047 }
2048 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002049 return;
2050
Kevin Steves43cdef32001-02-11 14:12:08 +00002051 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002052
Damien Millerb38eff82000-04-01 11:09:21 +10002053 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002054 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002055 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002056
2057 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002058 if (getuid() == 0)
2059 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002060
2061 /*
2062 * Close the server side of the socket pairs. We must do this after
2063 * the pty cleanup, so that another process doesn't get this pty
2064 * while we're still cleaning up.
2065 */
2066 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002067 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002068
2069 /* unlink pty from session */
2070 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002071}
Damien Millerefb4afe2000-04-12 18:45:05 +10002072
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002073void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002074session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002075{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002076 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002077}
2078
Damien Miller5a80bba2002-09-04 16:39:02 +10002079static char *
2080sig2name(int sig)
2081{
2082#define SSH_SIG(x) if (sig == SIG ## x) return #x
2083 SSH_SIG(ABRT);
2084 SSH_SIG(ALRM);
2085 SSH_SIG(FPE);
2086 SSH_SIG(HUP);
2087 SSH_SIG(ILL);
2088 SSH_SIG(INT);
2089 SSH_SIG(KILL);
2090 SSH_SIG(PIPE);
2091 SSH_SIG(QUIT);
2092 SSH_SIG(SEGV);
2093 SSH_SIG(TERM);
2094 SSH_SIG(USR1);
2095 SSH_SIG(USR2);
2096#undef SSH_SIG
2097 return "SIG@openssh.com";
2098}
2099
Ben Lindstrombba81212001-06-25 05:01:22 +00002100static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002101session_close_x11(int id)
2102{
2103 Channel *c;
2104
Damien Millerd47c62a2005-12-13 19:33:57 +11002105 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002106 debug("session_close_x11: x11 channel %d missing", id);
2107 } else {
2108 /* Detach X11 listener */
2109 debug("session_close_x11: detach x11 channel %d", id);
2110 channel_cancel_cleanup(id);
2111 if (c->ostate != CHAN_OUTPUT_CLOSED)
2112 chan_mark_dead(c);
2113 }
2114}
2115
2116static void
2117session_close_single_x11(int id, void *arg)
2118{
2119 Session *s;
2120 u_int i;
2121
2122 debug3("session_close_single_x11: channel %d", id);
2123 channel_cancel_cleanup(id);
2124 if ((s = session_by_x11_channel(id)) == NULL)
2125 fatal("session_close_single_x11: no x11 channel %d", id);
2126 for (i = 0; s->x11_chanids[i] != -1; i++) {
2127 debug("session_close_single_x11: session %d: "
2128 "closing channel %d", s->self, s->x11_chanids[i]);
2129 /*
2130 * The channel "id" is already closing, but make sure we
2131 * close all of its siblings.
2132 */
2133 if (s->x11_chanids[i] != id)
2134 session_close_x11(s->x11_chanids[i]);
2135 }
2136 xfree(s->x11_chanids);
2137 s->x11_chanids = NULL;
2138 if (s->display) {
2139 xfree(s->display);
2140 s->display = NULL;
2141 }
2142 if (s->auth_proto) {
2143 xfree(s->auth_proto);
2144 s->auth_proto = NULL;
2145 }
2146 if (s->auth_data) {
2147 xfree(s->auth_data);
2148 s->auth_data = NULL;
2149 }
2150 if (s->auth_display) {
2151 xfree(s->auth_display);
2152 s->auth_display = NULL;
2153 }
2154}
2155
2156static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002157session_exit_message(Session *s, int status)
2158{
2159 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002160
2161 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002162 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002163 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002164 debug("session_exit_message: session %d channel %d pid %ld",
2165 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002166
2167 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002168 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002169 packet_put_int(WEXITSTATUS(status));
2170 packet_send();
2171 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002172 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002173 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002174#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10002175 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002176#else /* WCOREDUMP */
2177 packet_put_char(0);
2178#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002179 packet_put_cstring("");
2180 packet_put_cstring("");
2181 packet_send();
2182 } else {
2183 /* Some weird exit cause. Just exit. */
2184 packet_disconnect("wait returned status %04x.", status);
2185 }
2186
2187 /* disconnect channel */
2188 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002189
2190 /*
2191 * Adjust cleanup callback attachment to send close messages when
2192 * the channel gets EOF. The session will be then be closed
2193 * by session_close_by_channel when the childs close their fds.
2194 */
2195 channel_register_cleanup(c->self, session_close_by_channel, 1);
2196
Damien Miller166fca82000-04-20 07:42:21 +10002197 /*
2198 * emulate a write failure with 'chan_write_failed', nobody will be
2199 * interested in data we write.
2200 * Note that we must not call 'chan_read_failed', since there could
2201 * be some more data waiting in the pipe.
2202 */
Damien Millerbd483e72000-04-30 10:00:53 +10002203 if (c->ostate != CHAN_OUTPUT_CLOSED)
2204 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002205}
2206
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002207void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002208session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002209{
Damien Millereccb9de2005-06-17 12:59:34 +10002210 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002211
Ben Lindstromce0f6342002-06-11 16:42:49 +00002212 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002213 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002214 session_pty_cleanup(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002215 if (s->term)
2216 xfree(s->term);
2217 if (s->display)
2218 xfree(s->display);
Damien Miller2b9b0452005-07-17 17:19:24 +10002219 if (s->x11_chanids)
2220 xfree(s->x11_chanids);
Damien Miller512bccb2002-02-05 12:11:02 +11002221 if (s->auth_display)
2222 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10002223 if (s->auth_data)
2224 xfree(s->auth_data);
2225 if (s->auth_proto)
2226 xfree(s->auth_proto);
2227 s->used = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +10002228 for (i = 0; i < s->num_env; i++) {
2229 xfree(s->env[i].name);
2230 xfree(s->env[i].val);
2231 }
2232 if (s->env != NULL)
2233 xfree(s->env);
Damien Millere247cc42000-05-07 12:03:14 +10002234 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002235}
2236
2237void
2238session_close_by_pid(pid_t pid, int status)
2239{
2240 Session *s = session_by_pid(pid);
2241 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002242 debug("session_close_by_pid: no session for pid %ld",
2243 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002244 return;
2245 }
2246 if (s->chanid != -1)
2247 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002248 if (s->ttyfd != -1)
2249 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002250 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002251}
2252
2253/*
2254 * this is called when a channel dies before
2255 * the session 'child' itself dies
2256 */
2257void
2258session_close_by_channel(int id, void *arg)
2259{
2260 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002261 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002262
Damien Millerefb4afe2000-04-12 18:45:05 +10002263 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002264 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002265 return;
2266 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002267 debug("session_close_by_channel: channel %d child %ld",
2268 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002269 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002270 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002271 /*
2272 * delay detach of session, but release pty, since
2273 * the fd's to the child are already closed
2274 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002275 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002276 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002277 return;
2278 }
2279 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002280 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002281
2282 /* Close any X11 listeners associated with this session */
2283 if (s->x11_chanids != NULL) {
2284 for (i = 0; s->x11_chanids[i] != -1; i++) {
2285 session_close_x11(s->x11_chanids[i]);
2286 s->x11_chanids[i] = -1;
2287 }
2288 }
2289
Damien Millerefb4afe2000-04-12 18:45:05 +10002290 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002291 session_close(s);
2292}
2293
2294void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002295session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002296{
2297 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002298 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002299 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002300 if (s->used) {
2301 if (closefunc != NULL)
2302 closefunc(s);
2303 else
2304 session_close(s);
2305 }
Damien Miller52b77be2001-10-10 15:14:37 +10002306 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002307}
2308
Ben Lindstrombba81212001-06-25 05:01:22 +00002309static char *
Damien Millere247cc42000-05-07 12:03:14 +10002310session_tty_list(void)
2311{
2312 static char buf[1024];
2313 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002314 char *cp;
2315
Damien Millere247cc42000-05-07 12:03:14 +10002316 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002317 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002318 Session *s = &sessions[i];
2319 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002320
Damien Millera8ed44b2003-01-10 09:53:12 +11002321 if (strncmp(s->tty, "/dev/", 5) != 0) {
2322 cp = strrchr(s->tty, '/');
2323 cp = (cp == NULL) ? s->tty : cp + 1;
2324 } else
2325 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002326
Damien Millere247cc42000-05-07 12:03:14 +10002327 if (buf[0] != '\0')
2328 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002329 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002330 }
2331 }
2332 if (buf[0] == '\0')
2333 strlcpy(buf, "notty", sizeof buf);
2334 return buf;
2335}
2336
2337void
2338session_proctitle(Session *s)
2339{
2340 if (s->pw == NULL)
2341 error("no user for session %d", s->self);
2342 else
2343 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2344}
2345
Ben Lindstrom768176b2001-06-09 01:29:12 +00002346int
2347session_setup_x11fwd(Session *s)
2348{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002349 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002350 char display[512], auth_display[512];
2351 char hostname[MAXHOSTNAMELEN];
Damien Miller2b9b0452005-07-17 17:19:24 +10002352 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002353
2354 if (no_x11_forwarding_flag) {
2355 packet_send_debug("X11 forwarding disabled in user configuration file.");
2356 return 0;
2357 }
2358 if (!options.x11_forwarding) {
2359 debug("X11 forwarding disabled in server configuration file.");
2360 return 0;
2361 }
2362 if (!options.xauth_location ||
2363 (stat(options.xauth_location, &st) == -1)) {
2364 packet_send_debug("No xauth program; cannot forward with spoofing.");
2365 return 0;
2366 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002367 if (options.use_login) {
2368 packet_send_debug("X11 forwarding disabled; "
2369 "not compatible with UseLogin=yes.");
2370 return 0;
2371 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002372 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002373 debug("X11 display already set.");
2374 return 0;
2375 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002376 if (x11_create_display_inet(options.x11_display_offset,
2377 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002378 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002379 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002380 return 0;
2381 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002382 for (i = 0; s->x11_chanids[i] != -1; i++) {
2383 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002384 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002385 }
Kevin Steves366298c2001-12-19 17:58:01 +00002386
2387 /* Set up a suitable value for the DISPLAY variable. */
2388 if (gethostname(hostname, sizeof(hostname)) < 0)
2389 fatal("gethostname: %.100s", strerror(errno));
2390 /*
2391 * auth_display must be used as the displayname when the
2392 * authorization entry is added with xauth(1). This will be
2393 * different than the DISPLAY string for localhost displays.
2394 */
Damien Miller95c249f2002-02-05 12:11:34 +11002395 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002396 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002397 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002398 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002399 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002400 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002401 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002402 } else {
2403#ifdef IPADDR_IN_DISPLAY
2404 struct hostent *he;
2405 struct in_addr my_addr;
2406
2407 he = gethostbyname(hostname);
2408 if (he == NULL) {
2409 error("Can't get IP address for X11 DISPLAY.");
2410 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2411 return 0;
2412 }
2413 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002414 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002415 s->display_number, s->screen);
2416#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002417 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002418 s->display_number, s->screen);
2419#endif
2420 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002421 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002422 }
2423
Ben Lindstrom768176b2001-06-09 01:29:12 +00002424 return 1;
2425}
2426
Ben Lindstrombba81212001-06-25 05:01:22 +00002427static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002428do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002429{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002430 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002431}
2432
2433void
2434do_cleanup(Authctxt *authctxt)
2435{
2436 static int called = 0;
2437
2438 debug("do_cleanup");
2439
2440 /* no cleanup if we're in the child for login shell */
2441 if (is_child)
2442 return;
2443
2444 /* avoid double cleanup */
2445 if (called)
2446 return;
2447 called = 1;
2448
2449 if (authctxt == NULL)
2450 return;
2451#ifdef KRB5
2452 if (options.kerberos_ticket_cleanup &&
2453 authctxt->krb5_ctx)
2454 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002455#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002456
2457#ifdef GSSAPI
2458 if (compat20 && options.gss_cleanup_creds)
2459 ssh_gssapi_cleanup_creds();
2460#endif
2461
Darren Tucker8846a072003-10-07 11:30:15 +10002462#ifdef USE_PAM
2463 if (options.use_pam) {
2464 sshpam_cleanup();
2465 sshpam_thread_cleanup();
2466 }
2467#endif
2468
Darren Tucker3e33cec2003-10-02 16:12:36 +10002469 /* remove agent socket */
2470 auth_sock_cleanup_proc(authctxt->pw);
2471
2472 /*
2473 * Cleanup ptys/utmp only if privsep is disabled,
2474 * or if running in monitor.
2475 */
2476 if (!use_privsep || mm_is_monitor())
2477 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002478}