blob: 8720ae7be3cda447f9363985b6bb91427e89d2f8 [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"
Darren Tucker723e9452004-06-22 12:57:08 +100036RCSID("$OpenBSD: session.c,v 1.176 2004/06/21 17:53:03 djm Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000042#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "packet.h"
44#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100045#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "uidswap.h"
47#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000048#include "channels.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100051#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
53#include "log.h"
54#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000055#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "serverloop.h"
57#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000058#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000059#include "monitor_wrap.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060
Darren Tucker3c78c5e2004-01-23 22:03:10 +110061#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110062#include <kafs.h>
63#endif
64
Darren Tucker0efd1552003-08-26 11:49:55 +100065#ifdef GSSAPI
66#include "ssh-gss.h"
67#endif
68
Damien Millerb38eff82000-04-01 11:09:21 +100069/* func */
70
71Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000072void session_set_fds(Session *, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +100073void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000074void session_proctitle(Session *);
75int session_setup_x11fwd(Session *);
76void do_exec_pty(Session *, const char *);
77void do_exec_no_pty(Session *, const char *);
78void do_exec(Session *, const char *);
79void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +000080#ifdef LOGIN_NEEDS_UTMPX
81static void do_pre_login(Session *s);
82#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +000083void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +100084void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000085int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +100086
Ben Lindstrombba81212001-06-25 05:01:22 +000087static void do_authenticated1(Authctxt *);
88static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000089
Ben Lindstrombba81212001-06-25 05:01:22 +000090static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +000091
Damien Millerb38eff82000-04-01 11:09:21 +100092/* import */
93extern ServerOptions options;
94extern char *__progname;
95extern int log_stderr;
96extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +000097extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +100098extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +000099extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000100extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000101
Damien Miller7b28dc52000-09-05 13:34:53 +1100102/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000103const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100104
Damien Millerb38eff82000-04-01 11:09:21 +1000105/* data */
106#define MAX_SESSIONS 10
107Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100108
Damien Millerad833b32000-08-23 10:46:23 +1000109#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000110login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000111#endif
112
Darren Tucker3e33cec2003-10-02 16:12:36 +1000113static int is_child = 0;
114
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000115/* Name and directory of socket for authentication agent forwarding. */
116static char *auth_sock_name = NULL;
117static char *auth_sock_dir = NULL;
118
119/* removes the agent forwarding socket */
120
121static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000122auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000123{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000124 if (auth_sock_name != NULL) {
125 temporarily_use_uid(pw);
126 unlink(auth_sock_name);
127 rmdir(auth_sock_dir);
128 auth_sock_name = NULL;
129 restore_uid();
130 }
131}
132
133static int
134auth_input_request_forwarding(struct passwd * pw)
135{
136 Channel *nc;
137 int sock;
138 struct sockaddr_un sunaddr;
139
140 if (auth_sock_name != NULL) {
141 error("authentication forwarding requested twice.");
142 return 0;
143 }
144
145 /* Temporarily drop privileged uid for mkdir/bind. */
146 temporarily_use_uid(pw);
147
148 /* Allocate a buffer for the socket name, and format the name. */
149 auth_sock_name = xmalloc(MAXPATHLEN);
150 auth_sock_dir = xmalloc(MAXPATHLEN);
Darren Tucker072a7b12003-10-15 16:10:25 +1000151 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000152
153 /* Create private directory for socket */
154 if (mkdtemp(auth_sock_dir) == NULL) {
155 packet_send_debug("Agent forwarding disabled: "
156 "mkdtemp() failed: %.100s", strerror(errno));
157 restore_uid();
158 xfree(auth_sock_name);
159 xfree(auth_sock_dir);
160 auth_sock_name = NULL;
161 auth_sock_dir = NULL;
162 return 0;
163 }
Ben Lindstromce0f6342002-06-11 16:42:49 +0000164 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
165 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000166
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000167 /* Create the socket. */
168 sock = socket(AF_UNIX, SOCK_STREAM, 0);
169 if (sock < 0)
170 packet_disconnect("socket: %.100s", strerror(errno));
171
172 /* Bind it to the name. */
173 memset(&sunaddr, 0, sizeof(sunaddr));
174 sunaddr.sun_family = AF_UNIX;
175 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
176
177 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
178 packet_disconnect("bind: %.100s", strerror(errno));
179
180 /* Restore the privileged uid. */
181 restore_uid();
182
183 /* Start listening on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +1100184 if (listen(sock, SSH_LISTEN_BACKLOG) < 0)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000185 packet_disconnect("listen: %.100s", strerror(errno));
186
187 /* Allocate a channel for the authentication agent socket. */
188 nc = channel_new("auth socket",
189 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
190 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000191 0, "auth socket", 1);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000192 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
193 return 1;
194}
195
Darren Tucker1921ed92004-02-10 13:23:28 +1100196static void
197display_loginmsg(void)
198{
199 if (buffer_len(&loginmsg) > 0) {
200 buffer_append(&loginmsg, "\0", 1);
201 printf("%s\n", (char *)buffer_ptr(&loginmsg));
202 buffer_clear(&loginmsg);
203 }
Darren Tuckerac7c9982004-04-07 08:04:09 +1000204 fflush(stdout);
Darren Tucker1921ed92004-02-10 13:23:28 +1100205}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000206
Ben Lindstromb31783d2001-03-22 02:02:12 +0000207void
208do_authenticated(Authctxt *authctxt)
209{
Damien Miller97f39ae2003-02-24 11:57:01 +1100210 setproctitle("%s", authctxt->pw->pw_name);
211
Ben Lindstromb31783d2001-03-22 02:02:12 +0000212 /*
213 * Cancel the alarm we set to limit the time taken for
214 * authentication.
215 */
216 alarm(0);
217 if (startup_pipe != -1) {
218 close(startup_pipe);
219 startup_pipe = -1;
220 }
Ben Lindstromb31783d2001-03-22 02:02:12 +0000221 /* setup the channel layer */
222 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
223 channel_permit_all_opens();
224
225 if (compat20)
226 do_authenticated2(authctxt);
227 else
228 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000229
Darren Tucker3e33cec2003-10-02 16:12:36 +1000230 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000231}
232
Damien Millerb38eff82000-04-01 11:09:21 +1000233/*
Damien Millerb38eff82000-04-01 11:09:21 +1000234 * Prepares for an interactive session. This is called after the user has
235 * been successfully authenticated. During this message exchange, pseudo
236 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
237 * are requested, etc.
238 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000239static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000240do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000241{
242 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000243 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100244 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000245 int enable_compression_after_reply = 0;
246 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000247
248 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000249 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000250 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000251
Damien Millerb38eff82000-04-01 11:09:21 +1000252 /*
253 * We stay in this loop until the client requests to execute a shell
254 * or a command.
255 */
256 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000257 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000258
259 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100260 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000261
262 /* Process the packet. */
263 switch (type) {
264 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000265 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100266 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000267 if (compression_level < 1 || compression_level > 9) {
268 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100269 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000270 break;
271 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000272 if (!options.compression) {
273 debug2("compression disabled");
274 break;
275 }
Damien Millerb38eff82000-04-01 11:09:21 +1000276 /* Enable compression after we have responded with SUCCESS. */
277 enable_compression_after_reply = 1;
278 success = 1;
279 break;
280
281 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000282 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000283 break;
284
285 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000286 s->auth_proto = packet_get_string(&proto_len);
287 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000288
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000289 screen_flag = packet_get_protocol_flags() &
290 SSH_PROTOFLAG_SCREEN_NUMBER;
291 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
292
293 if (packet_remaining() == 4) {
294 if (!screen_flag)
295 debug2("Buggy client: "
296 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000297 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000298 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000299 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000300 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100301 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000302 success = session_setup_x11fwd(s);
303 if (!success) {
304 xfree(s->auth_proto);
305 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000306 s->auth_proto = NULL;
307 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000308 }
Damien Millerb38eff82000-04-01 11:09:21 +1000309 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000310
311 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
312 if (no_agent_forwarding_flag || compat13) {
313 debug("Authentication agent forwarding not permitted for this authentication.");
314 break;
315 }
316 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000317 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000318 break;
319
320 case SSH_CMSG_PORT_FORWARD_REQUEST:
321 if (no_port_forwarding_flag) {
322 debug("Port forwarding not permitted for this authentication.");
323 break;
324 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100325 if (!options.allow_tcp_forwarding) {
326 debug("Port forwarding not permitted.");
327 break;
328 }
Damien Millerb38eff82000-04-01 11:09:21 +1000329 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000330 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000331 success = 1;
332 break;
333
334 case SSH_CMSG_MAX_PACKET_SIZE:
335 if (packet_set_maxsize(packet_get_int()) > 0)
336 success = 1;
337 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100338
Damien Millerb38eff82000-04-01 11:09:21 +1000339 case SSH_CMSG_EXEC_SHELL:
340 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000341 if (type == SSH_CMSG_EXEC_CMD) {
342 command = packet_get_string(&dlen);
343 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000344 do_exec(s, command);
345 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000346 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000347 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000348 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100349 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000350 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000351 return;
352
353 default:
354 /*
355 * Any unknown messages in this phase are ignored,
356 * and a failure message is returned.
357 */
Damien Miller996acd22003-04-09 20:59:48 +1000358 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000359 }
360 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
361 packet_send();
362 packet_write_wait();
363
364 /* Enable compression now that we have replied if appropriate. */
365 if (enable_compression_after_reply) {
366 enable_compression_after_reply = 0;
367 packet_start_compression(compression_level);
368 }
369 }
370}
371
372/*
373 * This is called to fork and execute a command when we have no tty. This
374 * will call do_child from the child, and server_loop from the parent after
375 * setting up file descriptors and such.
376 */
Damien Miller4af51302000-04-16 11:18:38 +1000377void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000378do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000379{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000380 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000381
382#ifdef USE_PIPES
383 int pin[2], pout[2], perr[2];
384 /* Allocate pipes for communicating with the program. */
385 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
386 packet_disconnect("Could not create pipes: %.100s",
387 strerror(errno));
388#else /* USE_PIPES */
389 int inout[2], err[2];
390 /* Uses socket pairs to communicate with the program. */
391 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
392 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
393 packet_disconnect("Could not create socket pairs: %.100s",
394 strerror(errno));
395#endif /* USE_PIPES */
396 if (s == NULL)
397 fatal("do_exec_no_pty: no session");
398
Damien Millere247cc42000-05-07 12:03:14 +1000399 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000400
Damien Millerc5946332001-02-28 11:46:11 +1100401#if defined(USE_PAM)
Darren Tucker1825f262004-02-24 00:01:27 +1100402 if (options.use_pam && !use_privsep)
Damien Miller4e448a32003-05-14 15:11:48 +1000403 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000404#endif /* USE_PAM */
405
Damien Millerb38eff82000-04-01 11:09:21 +1000406 /* Fork the child. */
407 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000408 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000409
Damien Millerb38eff82000-04-01 11:09:21 +1000410 /* Child. Reinitialize the log since the pid has changed. */
411 log_init(__progname, options.log_level, options.log_facility, log_stderr);
412
413 /*
414 * Create a new session and process group since the 4.4BSD
415 * setlogin() affects the entire process group.
416 */
417 if (setsid() < 0)
418 error("setsid failed: %.100s", strerror(errno));
419
420#ifdef USE_PIPES
421 /*
422 * Redirect stdin. We close the parent side of the socket
423 * pair, and make the child side the standard input.
424 */
425 close(pin[1]);
426 if (dup2(pin[0], 0) < 0)
427 perror("dup2 stdin");
428 close(pin[0]);
429
430 /* Redirect stdout. */
431 close(pout[0]);
432 if (dup2(pout[1], 1) < 0)
433 perror("dup2 stdout");
434 close(pout[1]);
435
436 /* Redirect stderr. */
437 close(perr[0]);
438 if (dup2(perr[1], 2) < 0)
439 perror("dup2 stderr");
440 close(perr[1]);
441#else /* USE_PIPES */
442 /*
443 * Redirect stdin, stdout, and stderr. Stdin and stdout will
444 * use the same socket, as some programs (particularly rdist)
445 * seem to depend on it.
446 */
447 close(inout[1]);
448 close(err[1]);
449 if (dup2(inout[0], 0) < 0) /* stdin */
450 perror("dup2 stdin");
451 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
452 perror("dup2 stdout");
453 if (dup2(err[0], 2) < 0) /* stderr */
454 perror("dup2 stderr");
455#endif /* USE_PIPES */
456
Tim Rice81ed5182002-09-25 17:38:46 -0700457#ifdef _UNICOS
458 cray_init_job(s->pw); /* set up cray jid and tmpdir */
459#endif
460
Damien Millerb38eff82000-04-01 11:09:21 +1000461 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000462 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000463 /* NOTREACHED */
464 }
Tim Rice81ed5182002-09-25 17:38:46 -0700465#ifdef _UNICOS
466 signal(WJSIGNAL, cray_job_termination_handler);
467#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100468#ifdef HAVE_CYGWIN
469 if (is_winnt)
470 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
471#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000472 if (pid < 0)
473 packet_disconnect("fork failed: %.100s", strerror(errno));
474 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000475 /* Set interactive/non-interactive mode. */
476 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000477#ifdef USE_PIPES
478 /* We are the parent. Close the child sides of the pipes. */
479 close(pin[0]);
480 close(pout[1]);
481 close(perr[1]);
482
Damien Millerefb4afe2000-04-12 18:45:05 +1000483 if (compat20) {
Darren Tucker723e9452004-06-22 12:57:08 +1000484 if (s->is_subsystem) {
485 close(perr[0]);
486 perr[0] = -1;
487 }
488 session_set_fds(s, pin[1], pout[0], perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000489 } else {
490 /* Enter the interactive session. */
491 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000492 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000493 }
Damien Millerb38eff82000-04-01 11:09:21 +1000494#else /* USE_PIPES */
495 /* We are the parent. Close the child sides of the socket pairs. */
496 close(inout[0]);
497 close(err[0]);
498
499 /*
Darren Tuckerb3850592004-03-27 16:44:21 +1100500 * Clear loginmsg, since it's the child's responsibility to display
501 * it to the user, otherwise multiple sessions may accumulate
502 * multiple copies of the login messages.
503 */
504 buffer_clear(&loginmsg);
505
506 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000507 * Enter the interactive session. Note: server_loop must be able to
508 * handle the case that fdin and fdout are the same.
509 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000510 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000511 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000512 } else {
513 server_loop(pid, inout[1], inout[1], err[1]);
514 /* server_loop has closed inout[1] and err[1]. */
515 }
Damien Millerb38eff82000-04-01 11:09:21 +1000516#endif /* USE_PIPES */
517}
518
519/*
520 * This is called to fork and execute a command when we have a tty. This
521 * will call do_child from the child, and server_loop from the parent after
522 * setting up file descriptors, controlling tty, updating wtmp, utmp,
523 * lastlog, and other such operations.
524 */
Damien Miller4af51302000-04-16 11:18:38 +1000525void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000526do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000527{
Damien Millerb38eff82000-04-01 11:09:21 +1000528 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000529 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000530
531 if (s == NULL)
532 fatal("do_exec_pty: no session");
533 ptyfd = s->ptyfd;
534 ttyfd = s->ttyfd;
535
Damien Millerc5946332001-02-28 11:46:11 +1100536#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000537 if (options.use_pam) {
Damien Miller341c6e62003-09-02 23:18:52 +1000538 do_pam_set_tty(s->tty);
Darren Tuckeref3a4a22004-02-06 15:30:50 +1100539 if (!use_privsep)
540 do_pam_setcred(1);
Damien Miller4e448a32003-05-14 15:11:48 +1000541 }
Damien Miller5a761312001-02-27 09:28:23 +1100542#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000543
Damien Millerb38eff82000-04-01 11:09:21 +1000544 /* Fork the child. */
545 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000546 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000547
Damien Miller942da032000-08-18 13:59:06 +1000548 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000549 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000550 /* Close the master side of the pseudo tty. */
551 close(ptyfd);
552
553 /* Make the pseudo tty our controlling tty. */
554 pty_make_controlling_tty(&ttyfd, s->tty);
555
Damien Miller9c751422001-10-10 15:02:46 +1000556 /* Redirect stdin/stdout/stderr from the pseudo tty. */
557 if (dup2(ttyfd, 0) < 0)
558 error("dup2 stdin: %s", strerror(errno));
559 if (dup2(ttyfd, 1) < 0)
560 error("dup2 stdout: %s", strerror(errno));
561 if (dup2(ttyfd, 2) < 0)
562 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000563
564 /* Close the extra descriptor for the pseudo tty. */
565 close(ttyfd);
566
Damien Miller942da032000-08-18 13:59:06 +1000567 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000568#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700569 if (!(options.use_login && command == NULL)) {
570#ifdef _UNICOS
571 cray_init_job(s->pw); /* set up cray jid and tmpdir */
572#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100573 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700574 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000575# ifdef LOGIN_NEEDS_UTMPX
576 else
577 do_pre_login(s);
578# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000579#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000580
Damien Millerb38eff82000-04-01 11:09:21 +1000581 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000582 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000583 /* NOTREACHED */
584 }
Tim Rice81ed5182002-09-25 17:38:46 -0700585#ifdef _UNICOS
586 signal(WJSIGNAL, cray_job_termination_handler);
587#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100588#ifdef HAVE_CYGWIN
589 if (is_winnt)
590 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
591#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000592 if (pid < 0)
593 packet_disconnect("fork failed: %.100s", strerror(errno));
594 s->pid = pid;
595
596 /* Parent. Close the slave side of the pseudo tty. */
597 close(ttyfd);
598
599 /*
600 * Create another descriptor of the pty master side for use as the
601 * standard input. We could use the original descriptor, but this
602 * simplifies code in server_loop. The descriptor is bidirectional.
603 */
604 fdout = dup(ptyfd);
605 if (fdout < 0)
606 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
607
608 /* we keep a reference to the pty master */
609 ptymaster = dup(ptyfd);
610 if (ptymaster < 0)
611 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
612 s->ptymaster = ptymaster;
613
614 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000615 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000616 if (compat20) {
617 session_set_fds(s, ptyfd, fdout, -1);
618 } else {
619 server_loop(pid, ptyfd, fdout, -1);
620 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000621 }
Damien Millerb38eff82000-04-01 11:09:21 +1000622}
623
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000624#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000625static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000626do_pre_login(Session *s)
627{
628 socklen_t fromlen;
629 struct sockaddr_storage from;
630 pid_t pid = getpid();
631
632 /*
633 * Get IP address of client. If the connection is not a socket, let
634 * the address be 0.0.0.0.
635 */
636 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000637 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000638 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000639 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100640 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000641 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000642 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000643 }
644 }
645
646 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000647 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000648 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000649}
650#endif
651
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000652/*
653 * This is called to fork and execute a command. If another command is
654 * to be forced, execute that instead.
655 */
656void
657do_exec(Session *s, const char *command)
658{
659 if (forced_command) {
660 original_command = command;
661 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000662 debug("Forced command '%.900s'", command);
663 }
664
Damien Miller324948b2003-09-02 22:55:45 +1000665#ifdef GSSAPI
666 if (options.gss_authentication) {
667 temporarily_use_uid(s->pw);
668 ssh_gssapi_storecreds();
669 restore_uid();
670 }
671#endif
672
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000673 if (s->ttyfd != -1)
674 do_exec_pty(s, command);
675 else
676 do_exec_no_pty(s, command);
677
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000678 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000679}
680
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000681
Damien Miller942da032000-08-18 13:59:06 +1000682/* administrative, login(1)-like work */
683void
Damien Miller69b69aa2000-10-28 14:19:58 +1100684do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000685{
Damien Miller942da032000-08-18 13:59:06 +1000686 char *time_string;
Damien Miller942da032000-08-18 13:59:06 +1000687 socklen_t fromlen;
688 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000689 struct passwd * pw = s->pw;
690 pid_t pid = getpid();
691
692 /*
693 * Get IP address of client. If the connection is not a socket, let
694 * the address be 0.0.0.0.
695 */
696 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000697 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000698 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000699 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000700 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000701 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000702 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000703 }
704 }
705
706 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000707 if (!use_privsep)
708 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
709 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000710 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000711 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000712
Kevin Steves092f2ef2000-10-14 13:36:13 +0000713#ifdef USE_PAM
714 /*
715 * If password change is needed, do it now.
716 * This needs to occur before the ~/.hushlogin check.
717 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100718 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
719 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000720 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100721 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000722 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000723 }
724#endif
725
Damien Millercf205e82001-04-16 18:29:15 +1000726 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000727 return;
728
Darren Tucker1921ed92004-02-10 13:23:28 +1100729 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000730
Tim Rice81ed5182002-09-25 17:38:46 -0700731#ifndef NO_SSH_LASTLOG
Ben Lindstromc447fee2002-04-02 20:35:35 +0000732 if (options.print_lastlog && s->last_login_time != 0) {
733 time_string = ctime(&s->last_login_time);
Damien Miller942da032000-08-18 13:59:06 +1000734 if (strchr(time_string, '\n'))
735 *strchr(time_string, '\n') = 0;
Ben Lindstromc447fee2002-04-02 20:35:35 +0000736 if (strcmp(s->hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000737 printf("Last login: %s\r\n", time_string);
738 else
Ben Lindstromc447fee2002-04-02 20:35:35 +0000739 printf("Last login: %s from %s\r\n", time_string,
740 s->hostname);
Damien Miller942da032000-08-18 13:59:06 +1000741 }
Tim Rice81ed5182002-09-25 17:38:46 -0700742#endif /* NO_SSH_LASTLOG */
Damien Millercf205e82001-04-16 18:29:15 +1000743
744 do_motd();
745}
746
747/*
748 * Display the message of the day.
749 */
750void
751do_motd(void)
752{
753 FILE *f;
754 char buf[256];
755
Damien Miller942da032000-08-18 13:59:06 +1000756 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000757#ifdef HAVE_LOGIN_CAP
758 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
759 "/etc/motd"), "r");
760#else
Damien Miller942da032000-08-18 13:59:06 +1000761 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000762#endif
Damien Miller942da032000-08-18 13:59:06 +1000763 if (f) {
764 while (fgets(buf, sizeof(buf), f))
765 fputs(buf, stdout);
766 fclose(f);
767 }
768 }
769}
770
Kevin Steves8f63caa2001-07-04 18:23:02 +0000771
772/*
773 * Check for quiet login, either .hushlogin or command given.
774 */
775int
776check_quietlogin(Session *s, const char *command)
777{
778 char buf[256];
779 struct passwd *pw = s->pw;
780 struct stat st;
781
782 /* Return 1 if .hushlogin exists or a command given. */
783 if (command != NULL)
784 return 1;
785 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
786#ifdef HAVE_LOGIN_CAP
787 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
788 return 1;
789#else
790 if (stat(buf, &st) >= 0)
791 return 1;
792#endif
793 return 0;
794}
795
Damien Millerb38eff82000-04-01 11:09:21 +1000796/*
797 * Sets the value of the given variable in the environment. If the variable
798 * already exists, its value is overriden.
799 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000800void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000801child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100802 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000803{
Damien Millerb38eff82000-04-01 11:09:21 +1000804 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000805 u_int envsize;
806 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000807
808 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000809 * If we're passed an uninitialized list, allocate a single null
810 * entry before continuing.
811 */
812 if (*envp == NULL && *envsizep == 0) {
813 *envp = xmalloc(sizeof(char *));
814 *envp[0] = NULL;
815 *envsizep = 1;
816 }
817
818 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000819 * Find the slot where the value should be stored. If the variable
820 * already exists, we reuse the slot; otherwise we append a new slot
821 * at the end of the array, expanding if necessary.
822 */
823 env = *envp;
824 namelen = strlen(name);
825 for (i = 0; env[i]; i++)
826 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
827 break;
828 if (env[i]) {
829 /* Reuse the slot. */
830 xfree(env[i]);
831 } else {
832 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000833 envsize = *envsizep;
834 if (i >= envsize - 1) {
835 if (envsize >= 1000)
836 fatal("child_set_env: too many env vars");
837 envsize += 50;
838 env = (*envp) = xrealloc(env, envsize * sizeof(char *));
839 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000840 }
841 /* Need to set the NULL pointer at end of array beyond the new slot. */
842 env[i + 1] = NULL;
843 }
844
845 /* Allocate space and format the variable in the appropriate slot. */
846 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
847 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
848}
849
850/*
851 * Reads environment variables from the given file and adds/overrides them
852 * into the environment. If the file does not exist, this does nothing.
853 * Otherwise, it must consist of empty lines, comments (line starts with '#')
854 * and assignments of the form name=value. No other forms are allowed.
855 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000856static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000857read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100858 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000859{
860 FILE *f;
861 char buf[4096];
862 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000863 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000864
865 f = fopen(filename, "r");
866 if (!f)
867 return;
868
869 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000870 if (++lineno > 1000)
871 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000872 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
873 ;
874 if (!*cp || *cp == '#' || *cp == '\n')
875 continue;
876 if (strchr(cp, '\n'))
877 *strchr(cp, '\n') = '\0';
878 value = strchr(cp, '=');
879 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000880 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
881 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000882 continue;
883 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000884 /*
885 * Replace the equals sign by nul, and advance value to
886 * the value string.
887 */
Damien Millerb38eff82000-04-01 11:09:21 +1000888 *value = '\0';
889 value++;
890 child_set_env(env, envsize, cp, value);
891 }
892 fclose(f);
893}
894
Darren Tuckere1a790d2003-09-16 11:52:19 +1000895#ifdef HAVE_ETC_DEFAULT_LOGIN
896/*
897 * Return named variable from specified environment, or NULL if not present.
898 */
899static char *
900child_get_env(char **env, const char *name)
901{
902 int i;
903 size_t len;
904
905 len = strlen(name);
906 for (i=0; env[i] != NULL; i++)
907 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
908 return(env[i] + len + 1);
909 return NULL;
910}
911
912/*
913 * Read /etc/default/login.
914 * We pick up the PATH (or SUPATH for root) and UMASK.
915 */
916static void
917read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
918{
919 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000920 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000921 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000922
923 /*
924 * We don't want to copy the whole file to the child's environment,
925 * so we use a temporary environment and copy the variables we're
926 * interested in.
927 */
928 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
929
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000930 if (tmpenv == NULL)
931 return;
932
Darren Tuckere1a790d2003-09-16 11:52:19 +1000933 if (uid == 0)
934 var = child_get_env(tmpenv, "SUPATH");
935 else
936 var = child_get_env(tmpenv, "PATH");
937 if (var != NULL)
938 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100939
Darren Tuckere1a790d2003-09-16 11:52:19 +1000940 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
941 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000942 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100943
Darren Tuckere1a790d2003-09-16 11:52:19 +1000944 for (i = 0; tmpenv[i] != NULL; i++)
945 xfree(tmpenv[i]);
946 xfree(tmpenv);
947}
948#endif /* HAVE_ETC_DEFAULT_LOGIN */
949
Damien Millerbb9ffc12002-01-08 10:59:32 +1100950void copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000951{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100952 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000953 int i;
954
Damien Millerbb9ffc12002-01-08 10:59:32 +1100955 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000956 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000957
Damien Millerbb9ffc12002-01-08 10:59:32 +1100958 for(i = 0; source[i] != NULL; i++) {
959 var_name = xstrdup(source[i]);
960 if ((var_val = strstr(var_name, "=")) == NULL) {
961 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000962 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000963 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100964 *var_val++ = '\0';
965
966 debug3("Copy environment: %s=%s", var_name, var_val);
967 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +1100968
Damien Millerbb9ffc12002-01-08 10:59:32 +1100969 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000970 }
971}
Damien Millercb5e44a2000-09-29 12:12:36 +1100972
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000973static char **
974do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000975{
Damien Millerb38eff82000-04-01 11:09:21 +1000976 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000977 u_int i, envsize;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000978 char **env, *laddr, *path = NULL;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000979 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000980
Damien Millerb38eff82000-04-01 11:09:21 +1000981 /* Initialize the environment. */
982 envsize = 100;
983 env = xmalloc(envsize * sizeof(char *));
984 env[0] = NULL;
985
Damien Millerbac2d8a2000-09-05 16:13:06 +1100986#ifdef HAVE_CYGWIN
987 /*
988 * The Windows environment contains some setting which are
989 * important for a running system. They must not be dropped.
990 */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100991 copy_environment(environ, &env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100992#endif
993
Darren Tucker0efd1552003-08-26 11:49:55 +1000994#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100995 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +1000996 * the childs environment as they see fit
997 */
998 ssh_gssapi_do_child(&env, &envsize);
999#endif
1000
Damien Millerb38eff82000-04-01 11:09:21 +10001001 if (!options.use_login) {
1002 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001003 for (i = 0; i < s->num_env; i++)
1004 child_set_env(&env, &envsize, s->env[i].name,
1005 s->env[i].val);
1006
Damien Millerb38eff82000-04-01 11:09:21 +10001007 child_set_env(&env, &envsize, "USER", pw->pw_name);
1008 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001009#ifdef _AIX
1010 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1011#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001012 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001013#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001014 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1015 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1016 else
1017 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001018#else /* HAVE_LOGIN_CAP */
1019# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001020 /*
1021 * There's no standard path on Windows. The path contains
1022 * important components pointing to the system directories,
1023 * needed for loading shared libraries. So the path better
1024 * remains intact here.
1025 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001026# ifdef HAVE_ETC_DEFAULT_LOGIN
1027 read_etc_default_login(&env, &envsize, pw->pw_uid);
1028 path = child_get_env(env, "PATH");
1029# endif /* HAVE_ETC_DEFAULT_LOGIN */
1030 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001031 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001032 s->pw->pw_uid == 0 ?
1033 SUPERUSER_PATH : _PATH_STDPATH);
1034 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001035# endif /* HAVE_CYGWIN */
1036#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001037
1038 snprintf(buf, sizeof buf, "%.200s/%.50s",
1039 _PATH_MAILDIR, pw->pw_name);
1040 child_set_env(&env, &envsize, "MAIL", buf);
1041
1042 /* Normal systems set SHELL by default. */
1043 child_set_env(&env, &envsize, "SHELL", shell);
1044 }
1045 if (getenv("TZ"))
1046 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1047
1048 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001049 if (!options.use_login) {
1050 while (custom_environment) {
1051 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001052 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001053
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001054 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001055 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001056 if (str[i] == '=') {
1057 str[i] = 0;
1058 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001059 }
1060 custom_environment = ce->next;
1061 xfree(ce->s);
1062 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001063 }
Damien Millerb38eff82000-04-01 11:09:21 +10001064 }
1065
Damien Millerf37e2462002-09-19 11:47:55 +10001066 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001067 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001068 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001069 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1070
Damien Miller00111382003-03-10 11:21:17 +11001071 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001072 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001073 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1074 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001075 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1076
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001077 if (s->ttyfd != -1)
1078 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1079 if (s->term)
1080 child_set_env(&env, &envsize, "TERM", s->term);
1081 if (s->display)
1082 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001083 if (original_command)
1084 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1085 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001086
Tim Rice81ed5182002-09-25 17:38:46 -07001087#ifdef _UNICOS
1088 if (cray_tmpdir[0] != '\0')
1089 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1090#endif /* _UNICOS */
1091
Damien Millerb38eff82000-04-01 11:09:21 +10001092#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001093 {
1094 char *cp;
1095
1096 if ((cp = getenv("AUTHSTATE")) != NULL)
1097 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1098 if ((cp = getenv("KRB5CCNAME")) != NULL)
1099 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1100 read_environment_file(&env, &envsize, "/etc/environment");
1101 }
Damien Millerb38eff82000-04-01 11:09:21 +10001102#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001103#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001104 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001105 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001106 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001107#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001108#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001109 /*
1110 * Pull in any environment variables that may have
1111 * been set by PAM.
1112 */
Damien Miller4e448a32003-05-14 15:11:48 +10001113 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001114 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001115
Damien Millerc756e9b2003-11-17 21:41:42 +11001116 p = fetch_pam_child_environment();
1117 copy_environment(p, &env, &envsize);
1118 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001119
Damien Millerc756e9b2003-11-17 21:41:42 +11001120 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001121 copy_environment(p, &env, &envsize);
1122 free_pam_environment(p);
1123 }
Damien Millerb38eff82000-04-01 11:09:21 +10001124#endif /* USE_PAM */
1125
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001126 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001127 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001128 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001129
1130 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001131 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001132 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001133 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001134 read_environment_file(&env, &envsize, buf);
1135 }
1136 if (debug_flag) {
1137 /* dump the environment */
1138 fprintf(stderr, "Environment:\n");
1139 for (i = 0; env[i]; i++)
1140 fprintf(stderr, " %.200s\n", env[i]);
1141 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001142 return env;
1143}
1144
1145/*
1146 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1147 * first in this order).
1148 */
1149static void
1150do_rc_files(Session *s, const char *shell)
1151{
1152 FILE *f = NULL;
1153 char cmd[1024];
1154 int do_xauth;
1155 struct stat st;
1156
1157 do_xauth =
1158 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1159
1160 /* ignore _PATH_SSH_USER_RC for subsystems */
1161 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1162 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1163 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1164 if (debug_flag)
1165 fprintf(stderr, "Running %s\n", cmd);
1166 f = popen(cmd, "w");
1167 if (f) {
1168 if (do_xauth)
1169 fprintf(f, "%s %s\n", s->auth_proto,
1170 s->auth_data);
1171 pclose(f);
1172 } else
1173 fprintf(stderr, "Could not run %s\n",
1174 _PATH_SSH_USER_RC);
1175 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1176 if (debug_flag)
1177 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1178 _PATH_SSH_SYSTEM_RC);
1179 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1180 if (f) {
1181 if (do_xauth)
1182 fprintf(f, "%s %s\n", s->auth_proto,
1183 s->auth_data);
1184 pclose(f);
1185 } else
1186 fprintf(stderr, "Could not run %s\n",
1187 _PATH_SSH_SYSTEM_RC);
1188 } else if (do_xauth && options.xauth_location != NULL) {
1189 /* Add authority data to .Xauthority if appropriate. */
1190 if (debug_flag) {
1191 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001192 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001193 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001194 fprintf(stderr,
1195 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001196 options.xauth_location, s->auth_display,
1197 s->auth_proto, s->auth_data);
1198 }
1199 snprintf(cmd, sizeof cmd, "%s -q -",
1200 options.xauth_location);
1201 f = popen(cmd, "w");
1202 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001203 fprintf(f, "remove %s\n",
1204 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001205 fprintf(f, "add %s %s %s\n",
1206 s->auth_display, s->auth_proto,
1207 s->auth_data);
1208 pclose(f);
1209 } else {
1210 fprintf(stderr, "Could not run %s\n",
1211 cmd);
1212 }
1213 }
1214}
1215
1216static void
1217do_nologin(struct passwd *pw)
1218{
1219 FILE *f = NULL;
1220 char buf[1024];
1221
1222#ifdef HAVE_LOGIN_CAP
1223 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1224 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1225 _PATH_NOLOGIN), "r");
1226#else
1227 if (pw->pw_uid)
1228 f = fopen(_PATH_NOLOGIN, "r");
1229#endif
1230 if (f) {
1231 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001232 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001233 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001234 while (fgets(buf, sizeof(buf), f))
1235 fputs(buf, stderr);
1236 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001237 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001238 exit(254);
1239 }
1240}
1241
1242/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001244do_setusercontext(struct passwd *pw)
1245{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001246#ifndef HAVE_CYGWIN
1247 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001248#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001249 {
1250
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001251#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001252 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1253 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001254#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001255#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001256# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001257 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001258# endif
Damien Millerd3526362004-01-23 14:16:26 +11001259# ifdef USE_PAM
1260 if (options.use_pam) {
1261 do_pam_session();
1262 do_pam_setcred(0);
1263 }
1264# endif /* USE_PAM */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001265 if (setusercontext(lc, pw, pw->pw_uid,
1266 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1267 perror("unable to set user context");
1268 exit(1);
1269 }
1270#else
1271# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1272 /* Sets login uid for accounting */
1273 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1274 error("setluid: %s", strerror(errno));
1275# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1276
1277 if (setlogin(pw->pw_name) < 0)
1278 error("setlogin failed: %s", strerror(errno));
1279 if (setgid(pw->pw_gid) < 0) {
1280 perror("setgid");
1281 exit(1);
1282 }
1283 /* Initialize the group list. */
1284 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1285 perror("initgroups");
1286 exit(1);
1287 }
1288 endgrent();
1289# ifdef USE_PAM
1290 /*
Damien Millera8e06ce2003-11-21 23:48:55 +11001291 * PAM credentials may take the form of supplementary groups.
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001292 * These will have been wiped by the above initgroups() call.
1293 * Reestablish them here.
1294 */
Damien Miller341c6e62003-09-02 23:18:52 +10001295 if (options.use_pam) {
1296 do_pam_session();
Damien Miller4e448a32003-05-14 15:11:48 +10001297 do_pam_setcred(0);
Damien Miller341c6e62003-09-02 23:18:52 +10001298 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001299# endif /* USE_PAM */
1300# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1301 irix_setusercontext(pw);
1302# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001303# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001304 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001305# endif /* _AIX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001306 /* Permanently switch to the desired uid. */
1307 permanently_set_uid(pw);
1308#endif
1309 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001310
1311#ifdef HAVE_CYGWIN
1312 if (is_winnt)
1313#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001314 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1315 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1316}
1317
Ben Lindstrom08105192002-03-22 02:50:06 +00001318static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001319do_pwchange(Session *s)
1320{
1321 fprintf(stderr, "WARNING: Your password has expired.\n");
1322 if (s->ttyfd != -1) {
1323 fprintf(stderr,
1324 "You must change your password now and login again!\n");
1325 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
1326 perror("passwd");
1327 } else {
1328 fprintf(stderr,
1329 "Password change required but no TTY available.\n");
1330 }
1331 exit(1);
1332}
1333
1334static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335launch_login(struct passwd *pw, const char *hostname)
1336{
1337 /* Launch login(1). */
1338
Ben Lindstrom378a4172002-06-07 14:49:56 +00001339 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001340#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001341 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001342#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001343#ifdef LOGIN_NO_ENDOPT
1344 "-p", "-f", pw->pw_name, (char *)NULL);
1345#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001346 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001347#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348
1349 /* Login couldn't be executed, die. */
1350
1351 perror("login");
1352 exit(1);
1353}
1354
Darren Tucker23bc8d02004-02-06 16:24:31 +11001355static void
1356child_close_fds(void)
1357{
1358 int i;
1359
1360 if (packet_get_connection_in() == packet_get_connection_out())
1361 close(packet_get_connection_in());
1362 else {
1363 close(packet_get_connection_in());
1364 close(packet_get_connection_out());
1365 }
1366 /*
1367 * Close all descriptors related to channels. They will still remain
1368 * open in the parent.
1369 */
1370 /* XXX better use close-on-exec? -markus */
1371 channel_close_all();
1372
1373 /*
1374 * Close any extra file descriptors. Note that there may still be
1375 * descriptors left by system functions. They will be closed later.
1376 */
1377 endpwent();
1378
1379 /*
1380 * Close any extra open file descriptors so that we don\'t have them
1381 * hanging around in clients. Note that we want to do this after
1382 * initgroups, because at least on Solaris 2.3 it leaves file
1383 * descriptors open.
1384 */
1385 for (i = 3; i < 64; i++)
1386 close(i);
1387}
1388
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001389/*
1390 * Performs common processing for the child, such as setting up the
1391 * environment, closing extra file descriptors, setting the user and group
1392 * ids, and executing the command or shell.
1393 */
1394void
1395do_child(Session *s, const char *command)
1396{
1397 extern char **environ;
1398 char **env;
1399 char *argv[10];
1400 const char *shell, *shell0, *hostname = NULL;
1401 struct passwd *pw = s->pw;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001402
1403 /* remove hostkey from the child's memory */
1404 destroy_sensitive_data();
1405
Darren Tucker23bc8d02004-02-06 16:24:31 +11001406 /* Force a password change */
1407 if (s->authctxt->force_pwchange) {
1408 do_setusercontext(pw);
1409 child_close_fds();
1410 do_pwchange(s);
1411 exit(1);
1412 }
1413
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001414 /* login(1) is only called if we execute the login shell */
1415 if (options.use_login && command != NULL)
1416 options.use_login = 0;
1417
Tim Rice81ed5182002-09-25 17:38:46 -07001418#ifdef _UNICOS
1419 cray_setup(pw->pw_uid, pw->pw_name, command);
1420#endif /* _UNICOS */
1421
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001422 /*
1423 * Login(1) does this as well, and it needs uid 0 for the "-h"
1424 * switch, so we let login(1) to this for us.
1425 */
1426 if (!options.use_login) {
1427#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001428 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001429 if (!check_quietlogin(s, command))
1430 do_motd();
1431#else /* HAVE_OSF_SIA */
1432 do_nologin(pw);
1433 do_setusercontext(pw);
1434#endif /* HAVE_OSF_SIA */
1435 }
1436
1437 /*
1438 * Get the shell from the password data. An empty shell field is
1439 * legal, and means /bin/sh.
1440 */
1441 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001442
1443 /*
1444 * Make sure $SHELL points to the shell from the password file,
1445 * even if shell is overridden from login.conf
1446 */
1447 env = do_setup_env(s, shell);
1448
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001449#ifdef HAVE_LOGIN_CAP
1450 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1451#endif
1452
Damien Millerad833b32000-08-23 10:46:23 +10001453 /* we have to stash the hostname before we close our socket. */
1454 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001455 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001456 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001457 /*
1458 * Close the connection descriptors; note that this is the child, and
1459 * the server will still have the socket open, and it is important
1460 * that we do not shutdown it. Note that the descriptors cannot be
1461 * closed before building the environment, as we call
1462 * get_remote_ipaddr there.
1463 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001464 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001465
Damien Millerb38eff82000-04-01 11:09:21 +10001466 /*
Damien Miller05eda432002-02-10 18:32:28 +11001467 * Must take new environment into use so that .ssh/rc,
1468 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001469 */
1470 environ = env;
1471
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001472#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001473 /*
1474 * At this point, we check to see if AFS is active and if we have
1475 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1476 * if we can (and need to) extend the ticket into an AFS token. If
1477 * we don't do this, we run into potential problems if the user's
1478 * home directory is in AFS and it's not world-readable.
1479 */
1480
1481 if (options.kerberos_get_afs_token && k_hasafs() &&
1482 (s->authctxt->krb5_ctx != NULL)) {
1483 char cell[64];
1484
1485 debug("Getting AFS token");
1486
1487 k_setpag();
1488
1489 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1490 krb5_afslog(s->authctxt->krb5_ctx,
1491 s->authctxt->krb5_fwd_ccache, cell, NULL);
1492
1493 krb5_afslog_home(s->authctxt->krb5_ctx,
1494 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1495 }
1496#endif
1497
Damien Miller139d4cd2001-10-10 15:07:44 +10001498 /* Change current directory to the user\'s home directory. */
1499 if (chdir(pw->pw_dir) < 0) {
1500 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1501 pw->pw_dir, strerror(errno));
1502#ifdef HAVE_LOGIN_CAP
1503 if (login_getcapbool(lc, "requirehome", 0))
1504 exit(1);
1505#endif
1506 }
1507
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001508 if (!options.use_login)
1509 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001510
1511 /* restore SIGPIPE for child */
1512 signal(SIGPIPE, SIG_DFL);
1513
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001514 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515 launch_login(pw, hostname);
1516 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001517 }
1518
1519 /* Get the last component of the shell name. */
1520 if ((shell0 = strrchr(shell, '/')) != NULL)
1521 shell0++;
1522 else
1523 shell0 = shell;
1524
Damien Millerb38eff82000-04-01 11:09:21 +10001525 /*
1526 * If we have no command, execute the shell. In this case, the shell
1527 * name to be passed in argv[0] is preceded by '-' to indicate that
1528 * this is a login shell.
1529 */
1530 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001531 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001532
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001533 /* Start the shell. Set initial character to '-'. */
1534 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001535
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001536 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1537 >= sizeof(argv0) - 1) {
1538 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001539 perror(shell);
1540 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001541 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001542
1543 /* Execute the shell. */
1544 argv[0] = argv0;
1545 argv[1] = NULL;
1546 execve(shell, argv, env);
1547
1548 /* Executing the shell failed. */
1549 perror(shell);
1550 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001551 }
1552 /*
1553 * Execute the command using the user's shell. This uses the -c
1554 * option to execute the command.
1555 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001556 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001557 argv[1] = "-c";
1558 argv[2] = (char *) command;
1559 argv[3] = NULL;
1560 execve(shell, argv, env);
1561 perror(shell);
1562 exit(1);
1563}
1564
1565Session *
1566session_new(void)
1567{
1568 int i;
1569 static int did_init = 0;
1570 if (!did_init) {
1571 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001572 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001573 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001574 }
1575 did_init = 1;
1576 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001577 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001578 Session *s = &sessions[i];
1579 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001580 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001581 s->chanid = -1;
1582 s->ptyfd = -1;
1583 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001584 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001585 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001586 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001587 return s;
1588 }
1589 }
1590 return NULL;
1591}
1592
Ben Lindstrombba81212001-06-25 05:01:22 +00001593static void
Damien Millerb38eff82000-04-01 11:09:21 +10001594session_dump(void)
1595{
1596 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001597 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001598 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001599 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001600 s->used,
1601 s->self,
1602 s,
1603 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001604 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001605 }
1606}
1607
Damien Millerefb4afe2000-04-12 18:45:05 +10001608int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001609session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001610{
1611 Session *s = session_new();
1612 debug("session_open: channel %d", chanid);
1613 if (s == NULL) {
1614 error("no more sessions");
1615 return 0;
1616 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001617 s->authctxt = authctxt;
1618 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001619 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001620 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001621 debug("session_open: session %d: link with channel %d", s->self, chanid);
1622 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001623 return 1;
1624}
1625
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626Session *
1627session_by_tty(char *tty)
1628{
1629 int i;
1630 for (i = 0; i < MAX_SESSIONS; i++) {
1631 Session *s = &sessions[i];
1632 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1633 debug("session_by_tty: session %d tty %s", i, tty);
1634 return s;
1635 }
1636 }
1637 debug("session_by_tty: unknown tty %.100s", tty);
1638 session_dump();
1639 return NULL;
1640}
1641
Ben Lindstrombba81212001-06-25 05:01:22 +00001642static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001643session_by_channel(int id)
1644{
1645 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001646 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001647 Session *s = &sessions[i];
1648 if (s->used && s->chanid == id) {
1649 debug("session_by_channel: session %d channel %d", i, id);
1650 return s;
1651 }
1652 }
1653 debug("session_by_channel: unknown channel %d", id);
1654 session_dump();
1655 return NULL;
1656}
1657
Ben Lindstrombba81212001-06-25 05:01:22 +00001658static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001659session_by_pid(pid_t pid)
1660{
1661 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001662 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001663 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001664 Session *s = &sessions[i];
1665 if (s->used && s->pid == pid)
1666 return s;
1667 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001668 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001669 session_dump();
1670 return NULL;
1671}
1672
Ben Lindstrombba81212001-06-25 05:01:22 +00001673static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001674session_window_change_req(Session *s)
1675{
1676 s->col = packet_get_int();
1677 s->row = packet_get_int();
1678 s->xpixel = packet_get_int();
1679 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001680 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001681 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1682 return 1;
1683}
1684
Ben Lindstrombba81212001-06-25 05:01:22 +00001685static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001686session_pty_req(Session *s)
1687{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001688 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001689 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001690
Ben Lindstrom49c12602001-06-13 04:37:36 +00001691 if (no_pty_flag) {
1692 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001693 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001694 }
1695 if (s->ttyfd != -1) {
1696 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001697 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001698 }
Ben Lindstromc447fee2002-04-02 20:35:35 +00001699 /* Get the time and hostname when the user last logged in. */
1700 if (options.print_lastlog) {
1701 s->hostname[0] = '\0';
1702 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1703 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1704 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001705
Damien Millerefb4afe2000-04-12 18:45:05 +10001706 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001707
1708 if (compat20) {
1709 s->col = packet_get_int();
1710 s->row = packet_get_int();
1711 } else {
1712 s->row = packet_get_int();
1713 s->col = packet_get_int();
1714 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001715 s->xpixel = packet_get_int();
1716 s->ypixel = packet_get_int();
1717
1718 if (strcmp(s->term, "") == 0) {
1719 xfree(s->term);
1720 s->term = NULL;
1721 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001722
Damien Millerefb4afe2000-04-12 18:45:05 +10001723 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001724 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001726 if (s->term)
1727 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001728 s->term = NULL;
1729 s->ptyfd = -1;
1730 s->ttyfd = -1;
1731 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001732 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001733 }
1734 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001735
1736 /* for SSH1 the tty modes length is not given */
1737 if (!compat20)
1738 n_bytes = packet_remaining();
1739 tty_parse_modes(s->ttyfd, &n_bytes);
1740
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741 if (!use_privsep)
1742 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001743
1744 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001745 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1746
Damien Miller48b03fc2002-01-22 23:11:40 +11001747 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001748 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001749 return 1;
1750}
1751
Ben Lindstrombba81212001-06-25 05:01:22 +00001752static int
Damien Millerbd483e72000-04-30 10:00:53 +10001753session_subsystem_req(Session *s)
1754{
Damien Millerae452462001-10-10 15:08:06 +10001755 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001756 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001757 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001758 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001759 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001760
Damien Miller48b03fc2002-01-22 23:11:40 +11001761 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001762 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001763
Damien Millerf6d9e222000-06-18 14:50:44 +10001764 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001765 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1766 cmd = options.subsystem_command[i];
1767 if (stat(cmd, &st) < 0) {
1768 error("subsystem: cannot stat %s: %s", cmd,
1769 strerror(errno));
1770 break;
1771 }
1772 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001773 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001774 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001775 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001776 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001777 }
1778 }
1779
1780 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10001781 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001782 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001783
Damien Millerbd483e72000-04-30 10:00:53 +10001784 xfree(subsys);
1785 return success;
1786}
1787
Ben Lindstrombba81212001-06-25 05:01:22 +00001788static int
Damien Millerbd483e72000-04-30 10:00:53 +10001789session_x11_req(Session *s)
1790{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001791 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001792
1793 s->single_connection = packet_get_char();
1794 s->auth_proto = packet_get_string(NULL);
1795 s->auth_data = packet_get_string(NULL);
1796 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001797 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001798
Ben Lindstrom768176b2001-06-09 01:29:12 +00001799 success = session_setup_x11fwd(s);
1800 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001801 xfree(s->auth_proto);
1802 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001803 s->auth_proto = NULL;
1804 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001805 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001806 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001807}
1808
Ben Lindstrombba81212001-06-25 05:01:22 +00001809static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001810session_shell_req(Session *s)
1811{
Damien Miller48b03fc2002-01-22 23:11:40 +11001812 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001813 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001814 return 1;
1815}
1816
Ben Lindstrombba81212001-06-25 05:01:22 +00001817static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001818session_exec_req(Session *s)
1819{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001820 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001821 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001822 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001823 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001824 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001825 return 1;
1826}
1827
Ben Lindstrombba81212001-06-25 05:01:22 +00001828static int
Damien Miller54c45982003-05-15 10:20:13 +10001829session_break_req(Session *s)
1830{
Damien Miller54c45982003-05-15 10:20:13 +10001831
Darren Tucker1f8311c2004-05-13 16:39:33 +10001832 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10001833 packet_check_eom();
1834
Darren Tucker3bdbd842003-08-13 20:31:05 +10001835 if (s->ttyfd == -1 ||
1836 tcsendbreak(s->ttyfd, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10001837 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10001838 return 1;
1839}
1840
1841static int
Darren Tucker46bc0752004-05-02 22:11:30 +10001842session_env_req(Session *s)
1843{
1844 char *name, *val;
1845 u_int name_len, val_len, i;
1846
1847 name = packet_get_string(&name_len);
1848 val = packet_get_string(&val_len);
1849 packet_check_eom();
1850
1851 /* Don't set too many environment variables */
1852 if (s->num_env > 128) {
1853 debug2("Ignoring env request %s: too many env vars", name);
1854 goto fail;
1855 }
1856
1857 for (i = 0; i < options.num_accept_env; i++) {
1858 if (match_pattern(name, options.accept_env[i])) {
1859 debug2("Setting env %d: %s=%s", s->num_env, name, val);
1860 s->env = xrealloc(s->env, sizeof(*s->env) *
1861 (s->num_env + 1));
1862 s->env[s->num_env].name = name;
1863 s->env[s->num_env].val = val;
1864 s->num_env++;
1865 return (1);
1866 }
1867 }
1868 debug2("Ignoring env request %s: disallowed name", name);
1869
1870 fail:
1871 xfree(name);
1872 xfree(val);
1873 return (0);
1874}
1875
1876static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001877session_auth_agent_req(Session *s)
1878{
1879 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001880 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001881 if (no_agent_forwarding_flag) {
1882 debug("session_auth_agent_req: no_agent_forwarding_flag");
1883 return 0;
1884 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001885 if (called) {
1886 return 0;
1887 } else {
1888 called = 1;
1889 return auth_input_request_forwarding(s->pw);
1890 }
1891}
1892
Damien Millerc7ef63d2002-02-05 12:21:42 +11001893int
1894session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001895{
Damien Millerefb4afe2000-04-12 18:45:05 +10001896 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001897 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001898
Damien Millerc7ef63d2002-02-05 12:21:42 +11001899 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10001900 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11001901 c->self, rtype);
1902 return 0;
1903 }
1904 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001905
1906 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001907 * a session is in LARVAL state until a shell, a command
1908 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001909 */
1910 if (c->type == SSH_CHANNEL_LARVAL) {
1911 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001912 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001913 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001914 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001915 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001916 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001917 } else if (strcmp(rtype, "x11-req") == 0) {
1918 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001919 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1920 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001921 } else if (strcmp(rtype, "subsystem") == 0) {
1922 success = session_subsystem_req(s);
Damien Miller54c45982003-05-15 10:20:13 +10001923 } else if (strcmp(rtype, "break") == 0) {
1924 success = session_break_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10001925 } else if (strcmp(rtype, "env") == 0) {
1926 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001927 }
1928 }
1929 if (strcmp(rtype, "window-change") == 0) {
1930 success = session_window_change_req(s);
1931 }
Damien Millerc7ef63d2002-02-05 12:21:42 +11001932 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10001933}
1934
1935void
1936session_set_fds(Session *s, int fdin, int fdout, int fderr)
1937{
1938 if (!compat20)
1939 fatal("session_set_fds: called for proto != 2.0");
1940 /*
1941 * now that have a child and a pipe to the child,
1942 * we can activate our channel and register the fd's
1943 */
1944 if (s->chanid == -1)
1945 fatal("no channel for session %d", s->self);
1946 channel_set_fds(s->chanid,
1947 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001948 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11001949 1,
1950 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10001951}
1952
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001953/*
1954 * Function to perform pty cleanup. Also called if we get aborted abnormally
1955 * (e.g., due to a dropped connection).
1956 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001957void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001958session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10001959{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001960 if (s == NULL) {
1961 error("session_pty_cleanup: no session");
1962 return;
1963 }
1964 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001965 return;
1966
Kevin Steves43cdef32001-02-11 14:12:08 +00001967 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001968
Damien Millerb38eff82000-04-01 11:09:21 +10001969 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001970 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08001971 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001972
1973 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001974 if (getuid() == 0)
1975 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001976
1977 /*
1978 * Close the server side of the socket pairs. We must do this after
1979 * the pty cleanup, so that another process doesn't get this pty
1980 * while we're still cleaning up.
1981 */
1982 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001983 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001984
1985 /* unlink pty from session */
1986 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001987}
Damien Millerefb4afe2000-04-12 18:45:05 +10001988
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001989void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001990session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001991{
Darren Tucker3e33cec2003-10-02 16:12:36 +10001992 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001993}
1994
Damien Miller5a80bba2002-09-04 16:39:02 +10001995static char *
1996sig2name(int sig)
1997{
1998#define SSH_SIG(x) if (sig == SIG ## x) return #x
1999 SSH_SIG(ABRT);
2000 SSH_SIG(ALRM);
2001 SSH_SIG(FPE);
2002 SSH_SIG(HUP);
2003 SSH_SIG(ILL);
2004 SSH_SIG(INT);
2005 SSH_SIG(KILL);
2006 SSH_SIG(PIPE);
2007 SSH_SIG(QUIT);
2008 SSH_SIG(SEGV);
2009 SSH_SIG(TERM);
2010 SSH_SIG(USR1);
2011 SSH_SIG(USR2);
2012#undef SSH_SIG
2013 return "SIG@openssh.com";
2014}
2015
Ben Lindstrombba81212001-06-25 05:01:22 +00002016static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002017session_exit_message(Session *s, int status)
2018{
2019 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002020
2021 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002022 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002023 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002024 debug("session_exit_message: session %d channel %d pid %ld",
2025 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002026
2027 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002028 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002029 packet_put_int(WEXITSTATUS(status));
2030 packet_send();
2031 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002032 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002033 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002034#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10002035 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002036#else /* WCOREDUMP */
2037 packet_put_char(0);
2038#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002039 packet_put_cstring("");
2040 packet_put_cstring("");
2041 packet_send();
2042 } else {
2043 /* Some weird exit cause. Just exit. */
2044 packet_disconnect("wait returned status %04x.", status);
2045 }
2046
2047 /* disconnect channel */
2048 debug("session_exit_message: release channel %d", s->chanid);
2049 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10002050 /*
2051 * emulate a write failure with 'chan_write_failed', nobody will be
2052 * interested in data we write.
2053 * Note that we must not call 'chan_read_failed', since there could
2054 * be some more data waiting in the pipe.
2055 */
Damien Millerbd483e72000-04-30 10:00:53 +10002056 if (c->ostate != CHAN_OUTPUT_CLOSED)
2057 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002058 s->chanid = -1;
2059}
2060
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002061void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002062session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002063{
Darren Tucker46bc0752004-05-02 22:11:30 +10002064 int i;
2065
Ben Lindstromce0f6342002-06-11 16:42:49 +00002066 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002067 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002068 session_pty_cleanup(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002069 if (s->term)
2070 xfree(s->term);
2071 if (s->display)
2072 xfree(s->display);
Damien Miller512bccb2002-02-05 12:11:02 +11002073 if (s->auth_display)
2074 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10002075 if (s->auth_data)
2076 xfree(s->auth_data);
2077 if (s->auth_proto)
2078 xfree(s->auth_proto);
2079 s->used = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +10002080 for (i = 0; i < s->num_env; i++) {
2081 xfree(s->env[i].name);
2082 xfree(s->env[i].val);
2083 }
2084 if (s->env != NULL)
2085 xfree(s->env);
Damien Millere247cc42000-05-07 12:03:14 +10002086 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002087}
2088
2089void
2090session_close_by_pid(pid_t pid, int status)
2091{
2092 Session *s = session_by_pid(pid);
2093 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002094 debug("session_close_by_pid: no session for pid %ld",
2095 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002096 return;
2097 }
2098 if (s->chanid != -1)
2099 session_exit_message(s, status);
2100 session_close(s);
2101}
2102
2103/*
2104 * this is called when a channel dies before
2105 * the session 'child' itself dies
2106 */
2107void
2108session_close_by_channel(int id, void *arg)
2109{
2110 Session *s = session_by_channel(id);
2111 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002112 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002113 return;
2114 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002115 debug("session_close_by_channel: channel %d child %ld",
2116 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002117 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002118 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002119 /*
2120 * delay detach of session, but release pty, since
2121 * the fd's to the child are already closed
2122 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002123 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002124 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002125 return;
2126 }
2127 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002128 channel_cancel_cleanup(s->chanid);
2129 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002130 session_close(s);
2131}
2132
2133void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002134session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002135{
2136 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002137 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002138 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002139 if (s->used) {
2140 if (closefunc != NULL)
2141 closefunc(s);
2142 else
2143 session_close(s);
2144 }
Damien Miller52b77be2001-10-10 15:14:37 +10002145 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002146}
2147
Ben Lindstrombba81212001-06-25 05:01:22 +00002148static char *
Damien Millere247cc42000-05-07 12:03:14 +10002149session_tty_list(void)
2150{
2151 static char buf[1024];
2152 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002153 char *cp;
2154
Damien Millere247cc42000-05-07 12:03:14 +10002155 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002156 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002157 Session *s = &sessions[i];
2158 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002159
Damien Millera8ed44b2003-01-10 09:53:12 +11002160 if (strncmp(s->tty, "/dev/", 5) != 0) {
2161 cp = strrchr(s->tty, '/');
2162 cp = (cp == NULL) ? s->tty : cp + 1;
2163 } else
2164 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002165
Damien Millere247cc42000-05-07 12:03:14 +10002166 if (buf[0] != '\0')
2167 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002168 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002169 }
2170 }
2171 if (buf[0] == '\0')
2172 strlcpy(buf, "notty", sizeof buf);
2173 return buf;
2174}
2175
2176void
2177session_proctitle(Session *s)
2178{
2179 if (s->pw == NULL)
2180 error("no user for session %d", s->self);
2181 else
2182 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2183}
2184
Ben Lindstrom768176b2001-06-09 01:29:12 +00002185int
2186session_setup_x11fwd(Session *s)
2187{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002188 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002189 char display[512], auth_display[512];
2190 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00002191
2192 if (no_x11_forwarding_flag) {
2193 packet_send_debug("X11 forwarding disabled in user configuration file.");
2194 return 0;
2195 }
2196 if (!options.x11_forwarding) {
2197 debug("X11 forwarding disabled in server configuration file.");
2198 return 0;
2199 }
2200 if (!options.xauth_location ||
2201 (stat(options.xauth_location, &st) == -1)) {
2202 packet_send_debug("No xauth program; cannot forward with spoofing.");
2203 return 0;
2204 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002205 if (options.use_login) {
2206 packet_send_debug("X11 forwarding disabled; "
2207 "not compatible with UseLogin=yes.");
2208 return 0;
2209 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002210 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002211 debug("X11 display already set.");
2212 return 0;
2213 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002214 if (x11_create_display_inet(options.x11_display_offset,
2215 options.x11_use_localhost, s->single_connection,
2216 &s->display_number) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002217 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002218 return 0;
2219 }
Kevin Steves366298c2001-12-19 17:58:01 +00002220
2221 /* Set up a suitable value for the DISPLAY variable. */
2222 if (gethostname(hostname, sizeof(hostname)) < 0)
2223 fatal("gethostname: %.100s", strerror(errno));
2224 /*
2225 * auth_display must be used as the displayname when the
2226 * authorization entry is added with xauth(1). This will be
2227 * different than the DISPLAY string for localhost displays.
2228 */
Damien Miller95c249f2002-02-05 12:11:34 +11002229 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002230 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002231 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002232 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002233 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002234 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002235 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002236 } else {
2237#ifdef IPADDR_IN_DISPLAY
2238 struct hostent *he;
2239 struct in_addr my_addr;
2240
2241 he = gethostbyname(hostname);
2242 if (he == NULL) {
2243 error("Can't get IP address for X11 DISPLAY.");
2244 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2245 return 0;
2246 }
2247 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002248 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002249 s->display_number, s->screen);
2250#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002251 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002252 s->display_number, s->screen);
2253#endif
2254 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002255 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002256 }
2257
Ben Lindstrom768176b2001-06-09 01:29:12 +00002258 return 1;
2259}
2260
Ben Lindstrombba81212001-06-25 05:01:22 +00002261static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002262do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002263{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002264 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002265}
2266
2267void
2268do_cleanup(Authctxt *authctxt)
2269{
2270 static int called = 0;
2271
2272 debug("do_cleanup");
2273
2274 /* no cleanup if we're in the child for login shell */
2275 if (is_child)
2276 return;
2277
2278 /* avoid double cleanup */
2279 if (called)
2280 return;
2281 called = 1;
2282
2283 if (authctxt == NULL)
2284 return;
2285#ifdef KRB5
2286 if (options.kerberos_ticket_cleanup &&
2287 authctxt->krb5_ctx)
2288 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002289#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002290
2291#ifdef GSSAPI
2292 if (compat20 && options.gss_cleanup_creds)
2293 ssh_gssapi_cleanup_creds();
2294#endif
2295
Darren Tucker8846a072003-10-07 11:30:15 +10002296#ifdef USE_PAM
2297 if (options.use_pam) {
2298 sshpam_cleanup();
2299 sshpam_thread_cleanup();
2300 }
2301#endif
2302
Darren Tucker3e33cec2003-10-02 16:12:36 +10002303 /* remove agent socket */
2304 auth_sock_cleanup_proc(authctxt->pw);
2305
2306 /*
2307 * Cleanup ptys/utmp only if privsep is disabled,
2308 * or if running in monitor.
2309 */
2310 if (!use_privsep || mm_is_monitor())
2311 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002312}