blob: 5742296d5173e665e0e2e2d395c31be47685ff5f [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 Tucker23bc8d02004-02-06 16:24:31 +110036RCSID("$OpenBSD: session.c,v 1.172 2004/01/30 09:48:57 markus 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"
Damien Millerb38eff82000-04-01 11:09:21 +100045#include "mpaux.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
196
Ben Lindstromb31783d2001-03-22 02:02:12 +0000197void
198do_authenticated(Authctxt *authctxt)
199{
Damien Miller97f39ae2003-02-24 11:57:01 +1100200 setproctitle("%s", authctxt->pw->pw_name);
201
Ben Lindstromb31783d2001-03-22 02:02:12 +0000202 /*
203 * Cancel the alarm we set to limit the time taken for
204 * authentication.
205 */
206 alarm(0);
207 if (startup_pipe != -1) {
208 close(startup_pipe);
209 startup_pipe = -1;
210 }
Ben Lindstromb31783d2001-03-22 02:02:12 +0000211 /* setup the channel layer */
212 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
213 channel_permit_all_opens();
214
215 if (compat20)
216 do_authenticated2(authctxt);
217 else
218 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000219
Darren Tucker3e33cec2003-10-02 16:12:36 +1000220 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000221}
222
Damien Millerb38eff82000-04-01 11:09:21 +1000223/*
Damien Millerb38eff82000-04-01 11:09:21 +1000224 * Prepares for an interactive session. This is called after the user has
225 * been successfully authenticated. During this message exchange, pseudo
226 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
227 * are requested, etc.
228 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000230do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000231{
232 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000233 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100234 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000235 int enable_compression_after_reply = 0;
236 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000237
238 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000239 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000240 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000241
Damien Millerb38eff82000-04-01 11:09:21 +1000242 /*
243 * We stay in this loop until the client requests to execute a shell
244 * or a command.
245 */
246 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000247 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000248
249 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100250 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000251
252 /* Process the packet. */
253 switch (type) {
254 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000255 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100256 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000257 if (compression_level < 1 || compression_level > 9) {
258 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100259 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000260 break;
261 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000262 if (!options.compression) {
263 debug2("compression disabled");
264 break;
265 }
Damien Millerb38eff82000-04-01 11:09:21 +1000266 /* Enable compression after we have responded with SUCCESS. */
267 enable_compression_after_reply = 1;
268 success = 1;
269 break;
270
271 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000272 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000273 break;
274
275 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000276 s->auth_proto = packet_get_string(&proto_len);
277 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000278
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000279 screen_flag = packet_get_protocol_flags() &
280 SSH_PROTOFLAG_SCREEN_NUMBER;
281 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
282
283 if (packet_remaining() == 4) {
284 if (!screen_flag)
285 debug2("Buggy client: "
286 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000287 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000288 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000289 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000290 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100291 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000292 success = session_setup_x11fwd(s);
293 if (!success) {
294 xfree(s->auth_proto);
295 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000296 s->auth_proto = NULL;
297 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000298 }
Damien Millerb38eff82000-04-01 11:09:21 +1000299 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000300
301 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
302 if (no_agent_forwarding_flag || compat13) {
303 debug("Authentication agent forwarding not permitted for this authentication.");
304 break;
305 }
306 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000307 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000308 break;
309
310 case SSH_CMSG_PORT_FORWARD_REQUEST:
311 if (no_port_forwarding_flag) {
312 debug("Port forwarding not permitted for this authentication.");
313 break;
314 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100315 if (!options.allow_tcp_forwarding) {
316 debug("Port forwarding not permitted.");
317 break;
318 }
Damien Millerb38eff82000-04-01 11:09:21 +1000319 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000320 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000321 success = 1;
322 break;
323
324 case SSH_CMSG_MAX_PACKET_SIZE:
325 if (packet_set_maxsize(packet_get_int()) > 0)
326 success = 1;
327 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100328
Damien Millerb38eff82000-04-01 11:09:21 +1000329 case SSH_CMSG_EXEC_SHELL:
330 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000331 if (type == SSH_CMSG_EXEC_CMD) {
332 command = packet_get_string(&dlen);
333 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000334 do_exec(s, command);
335 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000336 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000337 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000338 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100339 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000340 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000341 return;
342
343 default:
344 /*
345 * Any unknown messages in this phase are ignored,
346 * and a failure message is returned.
347 */
Damien Miller996acd22003-04-09 20:59:48 +1000348 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000349 }
350 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
351 packet_send();
352 packet_write_wait();
353
354 /* Enable compression now that we have replied if appropriate. */
355 if (enable_compression_after_reply) {
356 enable_compression_after_reply = 0;
357 packet_start_compression(compression_level);
358 }
359 }
360}
361
362/*
363 * This is called to fork and execute a command when we have no tty. This
364 * will call do_child from the child, and server_loop from the parent after
365 * setting up file descriptors and such.
366 */
Damien Miller4af51302000-04-16 11:18:38 +1000367void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000368do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000369{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000370 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000371
372#ifdef USE_PIPES
373 int pin[2], pout[2], perr[2];
374 /* Allocate pipes for communicating with the program. */
375 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
376 packet_disconnect("Could not create pipes: %.100s",
377 strerror(errno));
378#else /* USE_PIPES */
379 int inout[2], err[2];
380 /* Uses socket pairs to communicate with the program. */
381 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
382 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
383 packet_disconnect("Could not create socket pairs: %.100s",
384 strerror(errno));
385#endif /* USE_PIPES */
386 if (s == NULL)
387 fatal("do_exec_no_pty: no session");
388
Damien Millere247cc42000-05-07 12:03:14 +1000389 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000390
Damien Millerc5946332001-02-28 11:46:11 +1100391#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000392 if (options.use_pam) {
Damien Miller4e448a32003-05-14 15:11:48 +1000393 do_pam_setcred(1);
394 if (is_pam_password_change_required())
395 packet_disconnect("Password change required but no "
396 "TTY available");
397 }
Kevin Stevesff793a22001-02-21 16:36:51 +0000398#endif /* USE_PAM */
399
Damien Millerb38eff82000-04-01 11:09:21 +1000400 /* Fork the child. */
401 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000402 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000403
Damien Millerb38eff82000-04-01 11:09:21 +1000404 /* Child. Reinitialize the log since the pid has changed. */
405 log_init(__progname, options.log_level, options.log_facility, log_stderr);
406
407 /*
408 * Create a new session and process group since the 4.4BSD
409 * setlogin() affects the entire process group.
410 */
411 if (setsid() < 0)
412 error("setsid failed: %.100s", strerror(errno));
413
414#ifdef USE_PIPES
415 /*
416 * Redirect stdin. We close the parent side of the socket
417 * pair, and make the child side the standard input.
418 */
419 close(pin[1]);
420 if (dup2(pin[0], 0) < 0)
421 perror("dup2 stdin");
422 close(pin[0]);
423
424 /* Redirect stdout. */
425 close(pout[0]);
426 if (dup2(pout[1], 1) < 0)
427 perror("dup2 stdout");
428 close(pout[1]);
429
430 /* Redirect stderr. */
431 close(perr[0]);
432 if (dup2(perr[1], 2) < 0)
433 perror("dup2 stderr");
434 close(perr[1]);
435#else /* USE_PIPES */
436 /*
437 * Redirect stdin, stdout, and stderr. Stdin and stdout will
438 * use the same socket, as some programs (particularly rdist)
439 * seem to depend on it.
440 */
441 close(inout[1]);
442 close(err[1]);
443 if (dup2(inout[0], 0) < 0) /* stdin */
444 perror("dup2 stdin");
445 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
446 perror("dup2 stdout");
447 if (dup2(err[0], 2) < 0) /* stderr */
448 perror("dup2 stderr");
449#endif /* USE_PIPES */
450
Tim Rice81ed5182002-09-25 17:38:46 -0700451#ifdef _UNICOS
452 cray_init_job(s->pw); /* set up cray jid and tmpdir */
453#endif
454
Damien Millerb38eff82000-04-01 11:09:21 +1000455 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000456 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000457 /* NOTREACHED */
458 }
Tim Rice81ed5182002-09-25 17:38:46 -0700459#ifdef _UNICOS
460 signal(WJSIGNAL, cray_job_termination_handler);
461#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100462#ifdef HAVE_CYGWIN
463 if (is_winnt)
464 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
465#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000466 if (pid < 0)
467 packet_disconnect("fork failed: %.100s", strerror(errno));
468 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000469 /* Set interactive/non-interactive mode. */
470 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000471#ifdef USE_PIPES
472 /* We are the parent. Close the child sides of the pipes. */
473 close(pin[0]);
474 close(pout[1]);
475 close(perr[1]);
476
Damien Millerefb4afe2000-04-12 18:45:05 +1000477 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000478 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000479 } else {
480 /* Enter the interactive session. */
481 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000482 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000483 }
Damien Millerb38eff82000-04-01 11:09:21 +1000484#else /* USE_PIPES */
485 /* We are the parent. Close the child sides of the socket pairs. */
486 close(inout[0]);
487 close(err[0]);
488
489 /*
490 * Enter the interactive session. Note: server_loop must be able to
491 * handle the case that fdin and fdout are the same.
492 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000493 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000494 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000495 } else {
496 server_loop(pid, inout[1], inout[1], err[1]);
497 /* server_loop has closed inout[1] and err[1]. */
498 }
Damien Millerb38eff82000-04-01 11:09:21 +1000499#endif /* USE_PIPES */
500}
501
502/*
503 * This is called to fork and execute a command when we have a tty. This
504 * will call do_child from the child, and server_loop from the parent after
505 * setting up file descriptors, controlling tty, updating wtmp, utmp,
506 * lastlog, and other such operations.
507 */
Damien Miller4af51302000-04-16 11:18:38 +1000508void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000509do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000510{
Damien Millerb38eff82000-04-01 11:09:21 +1000511 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000512 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000513
514 if (s == NULL)
515 fatal("do_exec_pty: no session");
516 ptyfd = s->ptyfd;
517 ttyfd = s->ttyfd;
518
Damien Millerc5946332001-02-28 11:46:11 +1100519#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000520 if (options.use_pam) {
Damien Miller341c6e62003-09-02 23:18:52 +1000521 do_pam_set_tty(s->tty);
Darren Tuckeref3a4a22004-02-06 15:30:50 +1100522 if (!use_privsep)
523 do_pam_setcred(1);
Damien Miller4e448a32003-05-14 15:11:48 +1000524 }
Damien Miller5a761312001-02-27 09:28:23 +1100525#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000526
Damien Millerb38eff82000-04-01 11:09:21 +1000527 /* Fork the child. */
528 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000529 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000530
Damien Miller942da032000-08-18 13:59:06 +1000531 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000532 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000533 /* Close the master side of the pseudo tty. */
534 close(ptyfd);
535
536 /* Make the pseudo tty our controlling tty. */
537 pty_make_controlling_tty(&ttyfd, s->tty);
538
Damien Miller9c751422001-10-10 15:02:46 +1000539 /* Redirect stdin/stdout/stderr from the pseudo tty. */
540 if (dup2(ttyfd, 0) < 0)
541 error("dup2 stdin: %s", strerror(errno));
542 if (dup2(ttyfd, 1) < 0)
543 error("dup2 stdout: %s", strerror(errno));
544 if (dup2(ttyfd, 2) < 0)
545 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000546
547 /* Close the extra descriptor for the pseudo tty. */
548 close(ttyfd);
549
Damien Miller942da032000-08-18 13:59:06 +1000550 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000551#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700552 if (!(options.use_login && command == NULL)) {
553#ifdef _UNICOS
554 cray_init_job(s->pw); /* set up cray jid and tmpdir */
555#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100556 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700557 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000558# ifdef LOGIN_NEEDS_UTMPX
559 else
560 do_pre_login(s);
561# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000562#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000563
Damien Millerb38eff82000-04-01 11:09:21 +1000564 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000565 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000566 /* NOTREACHED */
567 }
Tim Rice81ed5182002-09-25 17:38:46 -0700568#ifdef _UNICOS
569 signal(WJSIGNAL, cray_job_termination_handler);
570#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100571#ifdef HAVE_CYGWIN
572 if (is_winnt)
573 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
574#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000575 if (pid < 0)
576 packet_disconnect("fork failed: %.100s", strerror(errno));
577 s->pid = pid;
578
579 /* Parent. Close the slave side of the pseudo tty. */
580 close(ttyfd);
581
582 /*
583 * Create another descriptor of the pty master side for use as the
584 * standard input. We could use the original descriptor, but this
585 * simplifies code in server_loop. The descriptor is bidirectional.
586 */
587 fdout = dup(ptyfd);
588 if (fdout < 0)
589 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
590
591 /* we keep a reference to the pty master */
592 ptymaster = dup(ptyfd);
593 if (ptymaster < 0)
594 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
595 s->ptymaster = ptymaster;
596
597 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000598 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000599 if (compat20) {
600 session_set_fds(s, ptyfd, fdout, -1);
601 } else {
602 server_loop(pid, ptyfd, fdout, -1);
603 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000604 }
Damien Millerb38eff82000-04-01 11:09:21 +1000605}
606
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000607#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000608static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000609do_pre_login(Session *s)
610{
611 socklen_t fromlen;
612 struct sockaddr_storage from;
613 pid_t pid = getpid();
614
615 /*
616 * Get IP address of client. If the connection is not a socket, let
617 * the address be 0.0.0.0.
618 */
619 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000620 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000621 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000622 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100623 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000624 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000625 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000626 }
627 }
628
629 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000630 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000631 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000632}
633#endif
634
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000635/*
636 * This is called to fork and execute a command. If another command is
637 * to be forced, execute that instead.
638 */
639void
640do_exec(Session *s, const char *command)
641{
642 if (forced_command) {
643 original_command = command;
644 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000645 debug("Forced command '%.900s'", command);
646 }
647
Damien Miller324948b2003-09-02 22:55:45 +1000648#ifdef GSSAPI
649 if (options.gss_authentication) {
650 temporarily_use_uid(s->pw);
651 ssh_gssapi_storecreds();
652 restore_uid();
653 }
654#endif
655
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000656 if (s->ttyfd != -1)
657 do_exec_pty(s, command);
658 else
659 do_exec_no_pty(s, command);
660
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000661 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000662}
663
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000664
Damien Miller942da032000-08-18 13:59:06 +1000665/* administrative, login(1)-like work */
666void
Damien Miller69b69aa2000-10-28 14:19:58 +1100667do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000668{
Damien Miller942da032000-08-18 13:59:06 +1000669 char *time_string;
Damien Miller942da032000-08-18 13:59:06 +1000670 socklen_t fromlen;
671 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000672 struct passwd * pw = s->pw;
673 pid_t pid = getpid();
674
675 /*
676 * Get IP address of client. If the connection is not a socket, let
677 * the address be 0.0.0.0.
678 */
679 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000680 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000681 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000682 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000683 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000684 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000685 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000686 }
687 }
688
689 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000690 if (!use_privsep)
691 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
692 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000693 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000694 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000695
Kevin Steves092f2ef2000-10-14 13:36:13 +0000696#ifdef USE_PAM
697 /*
698 * If password change is needed, do it now.
699 * This needs to occur before the ~/.hushlogin check.
700 */
Damien Miller4e448a32003-05-14 15:11:48 +1000701 if (options.use_pam && is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000702 print_pam_messages();
703 do_pam_chauthtok();
Damien Miller1f499fd2003-08-25 13:08:49 +1000704 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000705 }
706#endif
707
Damien Millercf205e82001-04-16 18:29:15 +1000708 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000709 return;
710
711#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000712 if (options.use_pam && !is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000713 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000714#endif /* USE_PAM */
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000715
716 /* display post-login message */
717 if (buffer_len(&loginmsg) > 0) {
718 buffer_append(&loginmsg, "\0", 1);
719 printf("%s\n", (char *)buffer_ptr(&loginmsg));
720 }
721 buffer_free(&loginmsg);
Damien Miller942da032000-08-18 13:59:06 +1000722
Tim Rice81ed5182002-09-25 17:38:46 -0700723#ifndef NO_SSH_LASTLOG
Ben Lindstromc447fee2002-04-02 20:35:35 +0000724 if (options.print_lastlog && s->last_login_time != 0) {
725 time_string = ctime(&s->last_login_time);
Damien Miller942da032000-08-18 13:59:06 +1000726 if (strchr(time_string, '\n'))
727 *strchr(time_string, '\n') = 0;
Ben Lindstromc447fee2002-04-02 20:35:35 +0000728 if (strcmp(s->hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000729 printf("Last login: %s\r\n", time_string);
730 else
Ben Lindstromc447fee2002-04-02 20:35:35 +0000731 printf("Last login: %s from %s\r\n", time_string,
732 s->hostname);
Damien Miller942da032000-08-18 13:59:06 +1000733 }
Tim Rice81ed5182002-09-25 17:38:46 -0700734#endif /* NO_SSH_LASTLOG */
Damien Millercf205e82001-04-16 18:29:15 +1000735
736 do_motd();
737}
738
739/*
740 * Display the message of the day.
741 */
742void
743do_motd(void)
744{
745 FILE *f;
746 char buf[256];
747
Damien Miller942da032000-08-18 13:59:06 +1000748 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000749#ifdef HAVE_LOGIN_CAP
750 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
751 "/etc/motd"), "r");
752#else
Damien Miller942da032000-08-18 13:59:06 +1000753 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000754#endif
Damien Miller942da032000-08-18 13:59:06 +1000755 if (f) {
756 while (fgets(buf, sizeof(buf), f))
757 fputs(buf, stdout);
758 fclose(f);
759 }
760 }
761}
762
Kevin Steves8f63caa2001-07-04 18:23:02 +0000763
764/*
765 * Check for quiet login, either .hushlogin or command given.
766 */
767int
768check_quietlogin(Session *s, const char *command)
769{
770 char buf[256];
771 struct passwd *pw = s->pw;
772 struct stat st;
773
774 /* Return 1 if .hushlogin exists or a command given. */
775 if (command != NULL)
776 return 1;
777 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
778#ifdef HAVE_LOGIN_CAP
779 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
780 return 1;
781#else
782 if (stat(buf, &st) >= 0)
783 return 1;
784#endif
785 return 0;
786}
787
Damien Millerb38eff82000-04-01 11:09:21 +1000788/*
789 * Sets the value of the given variable in the environment. If the variable
790 * already exists, its value is overriden.
791 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000792void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000793child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100794 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000795{
Damien Millerb38eff82000-04-01 11:09:21 +1000796 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000797 u_int envsize;
798 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000799
800 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000801 * If we're passed an uninitialized list, allocate a single null
802 * entry before continuing.
803 */
804 if (*envp == NULL && *envsizep == 0) {
805 *envp = xmalloc(sizeof(char *));
806 *envp[0] = NULL;
807 *envsizep = 1;
808 }
809
810 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000811 * Find the slot where the value should be stored. If the variable
812 * already exists, we reuse the slot; otherwise we append a new slot
813 * at the end of the array, expanding if necessary.
814 */
815 env = *envp;
816 namelen = strlen(name);
817 for (i = 0; env[i]; i++)
818 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
819 break;
820 if (env[i]) {
821 /* Reuse the slot. */
822 xfree(env[i]);
823 } else {
824 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000825 envsize = *envsizep;
826 if (i >= envsize - 1) {
827 if (envsize >= 1000)
828 fatal("child_set_env: too many env vars");
829 envsize += 50;
830 env = (*envp) = xrealloc(env, envsize * sizeof(char *));
831 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000832 }
833 /* Need to set the NULL pointer at end of array beyond the new slot. */
834 env[i + 1] = NULL;
835 }
836
837 /* Allocate space and format the variable in the appropriate slot. */
838 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
839 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
840}
841
842/*
843 * Reads environment variables from the given file and adds/overrides them
844 * into the environment. If the file does not exist, this does nothing.
845 * Otherwise, it must consist of empty lines, comments (line starts with '#')
846 * and assignments of the form name=value. No other forms are allowed.
847 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000848static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000849read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100850 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000851{
852 FILE *f;
853 char buf[4096];
854 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000855 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000856
857 f = fopen(filename, "r");
858 if (!f)
859 return;
860
861 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000862 if (++lineno > 1000)
863 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000864 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
865 ;
866 if (!*cp || *cp == '#' || *cp == '\n')
867 continue;
868 if (strchr(cp, '\n'))
869 *strchr(cp, '\n') = '\0';
870 value = strchr(cp, '=');
871 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000872 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
873 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000874 continue;
875 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000876 /*
877 * Replace the equals sign by nul, and advance value to
878 * the value string.
879 */
Damien Millerb38eff82000-04-01 11:09:21 +1000880 *value = '\0';
881 value++;
882 child_set_env(env, envsize, cp, value);
883 }
884 fclose(f);
885}
886
Darren Tuckere1a790d2003-09-16 11:52:19 +1000887#ifdef HAVE_ETC_DEFAULT_LOGIN
888/*
889 * Return named variable from specified environment, or NULL if not present.
890 */
891static char *
892child_get_env(char **env, const char *name)
893{
894 int i;
895 size_t len;
896
897 len = strlen(name);
898 for (i=0; env[i] != NULL; i++)
899 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
900 return(env[i] + len + 1);
901 return NULL;
902}
903
904/*
905 * Read /etc/default/login.
906 * We pick up the PATH (or SUPATH for root) and UMASK.
907 */
908static void
909read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
910{
911 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000912 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000913 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000914
915 /*
916 * We don't want to copy the whole file to the child's environment,
917 * so we use a temporary environment and copy the variables we're
918 * interested in.
919 */
920 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
921
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000922 if (tmpenv == NULL)
923 return;
924
Darren Tuckere1a790d2003-09-16 11:52:19 +1000925 if (uid == 0)
926 var = child_get_env(tmpenv, "SUPATH");
927 else
928 var = child_get_env(tmpenv, "PATH");
929 if (var != NULL)
930 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100931
Darren Tuckere1a790d2003-09-16 11:52:19 +1000932 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
933 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000934 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100935
Darren Tuckere1a790d2003-09-16 11:52:19 +1000936 for (i = 0; tmpenv[i] != NULL; i++)
937 xfree(tmpenv[i]);
938 xfree(tmpenv);
939}
940#endif /* HAVE_ETC_DEFAULT_LOGIN */
941
Damien Millerbb9ffc12002-01-08 10:59:32 +1100942void copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000943{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100944 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000945 int i;
946
Damien Millerbb9ffc12002-01-08 10:59:32 +1100947 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000948 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000949
Damien Millerbb9ffc12002-01-08 10:59:32 +1100950 for(i = 0; source[i] != NULL; i++) {
951 var_name = xstrdup(source[i]);
952 if ((var_val = strstr(var_name, "=")) == NULL) {
953 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000954 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000955 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100956 *var_val++ = '\0';
957
958 debug3("Copy environment: %s=%s", var_name, var_val);
959 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +1100960
Damien Millerbb9ffc12002-01-08 10:59:32 +1100961 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000962 }
963}
Damien Millercb5e44a2000-09-29 12:12:36 +1100964
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000965static char **
966do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000967{
Damien Millerb38eff82000-04-01 11:09:21 +1000968 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000969 u_int i, envsize;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000970 char **env, *laddr, *path = NULL;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000971 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000972
Damien Millerb38eff82000-04-01 11:09:21 +1000973 /* Initialize the environment. */
974 envsize = 100;
975 env = xmalloc(envsize * sizeof(char *));
976 env[0] = NULL;
977
Damien Millerbac2d8a2000-09-05 16:13:06 +1100978#ifdef HAVE_CYGWIN
979 /*
980 * The Windows environment contains some setting which are
981 * important for a running system. They must not be dropped.
982 */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100983 copy_environment(environ, &env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100984#endif
985
Darren Tucker0efd1552003-08-26 11:49:55 +1000986#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100987 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +1000988 * the childs environment as they see fit
989 */
990 ssh_gssapi_do_child(&env, &envsize);
991#endif
992
Damien Millerb38eff82000-04-01 11:09:21 +1000993 if (!options.use_login) {
994 /* Set basic environment. */
995 child_set_env(&env, &envsize, "USER", pw->pw_name);
996 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +1100997#ifdef _AIX
998 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
999#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001000 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001001#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001002 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1003 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1004 else
1005 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001006#else /* HAVE_LOGIN_CAP */
1007# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001008 /*
1009 * There's no standard path on Windows. The path contains
1010 * important components pointing to the system directories,
1011 * needed for loading shared libraries. So the path better
1012 * remains intact here.
1013 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001014# ifdef HAVE_ETC_DEFAULT_LOGIN
1015 read_etc_default_login(&env, &envsize, pw->pw_uid);
1016 path = child_get_env(env, "PATH");
1017# endif /* HAVE_ETC_DEFAULT_LOGIN */
1018 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001019 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001020 s->pw->pw_uid == 0 ?
1021 SUPERUSER_PATH : _PATH_STDPATH);
1022 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001023# endif /* HAVE_CYGWIN */
1024#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001025
1026 snprintf(buf, sizeof buf, "%.200s/%.50s",
1027 _PATH_MAILDIR, pw->pw_name);
1028 child_set_env(&env, &envsize, "MAIL", buf);
1029
1030 /* Normal systems set SHELL by default. */
1031 child_set_env(&env, &envsize, "SHELL", shell);
1032 }
1033 if (getenv("TZ"))
1034 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1035
1036 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001037 if (!options.use_login) {
1038 while (custom_environment) {
1039 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001040 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001041
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001042 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001043 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001044 if (str[i] == '=') {
1045 str[i] = 0;
1046 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001047 }
1048 custom_environment = ce->next;
1049 xfree(ce->s);
1050 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001051 }
Damien Millerb38eff82000-04-01 11:09:21 +10001052 }
1053
Damien Millerf37e2462002-09-19 11:47:55 +10001054 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001055 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001056 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001057 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1058
Damien Miller00111382003-03-10 11:21:17 +11001059 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001060 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001061 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1062 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001063 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1064
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001065 if (s->ttyfd != -1)
1066 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1067 if (s->term)
1068 child_set_env(&env, &envsize, "TERM", s->term);
1069 if (s->display)
1070 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001071 if (original_command)
1072 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1073 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001074
Tim Rice81ed5182002-09-25 17:38:46 -07001075#ifdef _UNICOS
1076 if (cray_tmpdir[0] != '\0')
1077 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1078#endif /* _UNICOS */
1079
Damien Millerb38eff82000-04-01 11:09:21 +10001080#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001081 {
1082 char *cp;
1083
1084 if ((cp = getenv("AUTHSTATE")) != NULL)
1085 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1086 if ((cp = getenv("KRB5CCNAME")) != NULL)
1087 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1088 read_environment_file(&env, &envsize, "/etc/environment");
1089 }
Damien Millerb38eff82000-04-01 11:09:21 +10001090#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001091#ifdef KRB5
1092 if (s->authctxt->krb5_ticket_file)
1093 child_set_env(&env, &envsize, "KRB5CCNAME",
1094 s->authctxt->krb5_ticket_file);
1095#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001096#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001097 /*
1098 * Pull in any environment variables that may have
1099 * been set by PAM.
1100 */
Damien Miller4e448a32003-05-14 15:11:48 +10001101 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001102 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001103
Damien Millerc756e9b2003-11-17 21:41:42 +11001104 p = fetch_pam_child_environment();
1105 copy_environment(p, &env, &envsize);
1106 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001107
Damien Millerc756e9b2003-11-17 21:41:42 +11001108 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001109 copy_environment(p, &env, &envsize);
1110 free_pam_environment(p);
1111 }
Damien Millerb38eff82000-04-01 11:09:21 +10001112#endif /* USE_PAM */
1113
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001114 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001115 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001116 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001117
1118 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001119 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001120 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001121 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001122 read_environment_file(&env, &envsize, buf);
1123 }
1124 if (debug_flag) {
1125 /* dump the environment */
1126 fprintf(stderr, "Environment:\n");
1127 for (i = 0; env[i]; i++)
1128 fprintf(stderr, " %.200s\n", env[i]);
1129 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001130 return env;
1131}
1132
1133/*
1134 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1135 * first in this order).
1136 */
1137static void
1138do_rc_files(Session *s, const char *shell)
1139{
1140 FILE *f = NULL;
1141 char cmd[1024];
1142 int do_xauth;
1143 struct stat st;
1144
1145 do_xauth =
1146 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1147
1148 /* ignore _PATH_SSH_USER_RC for subsystems */
1149 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1150 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1151 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1152 if (debug_flag)
1153 fprintf(stderr, "Running %s\n", cmd);
1154 f = popen(cmd, "w");
1155 if (f) {
1156 if (do_xauth)
1157 fprintf(f, "%s %s\n", s->auth_proto,
1158 s->auth_data);
1159 pclose(f);
1160 } else
1161 fprintf(stderr, "Could not run %s\n",
1162 _PATH_SSH_USER_RC);
1163 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1164 if (debug_flag)
1165 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1166 _PATH_SSH_SYSTEM_RC);
1167 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1168 if (f) {
1169 if (do_xauth)
1170 fprintf(f, "%s %s\n", s->auth_proto,
1171 s->auth_data);
1172 pclose(f);
1173 } else
1174 fprintf(stderr, "Could not run %s\n",
1175 _PATH_SSH_SYSTEM_RC);
1176 } else if (do_xauth && options.xauth_location != NULL) {
1177 /* Add authority data to .Xauthority if appropriate. */
1178 if (debug_flag) {
1179 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001180 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001181 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001182 fprintf(stderr,
1183 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001184 options.xauth_location, s->auth_display,
1185 s->auth_proto, s->auth_data);
1186 }
1187 snprintf(cmd, sizeof cmd, "%s -q -",
1188 options.xauth_location);
1189 f = popen(cmd, "w");
1190 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001191 fprintf(f, "remove %s\n",
1192 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001193 fprintf(f, "add %s %s %s\n",
1194 s->auth_display, s->auth_proto,
1195 s->auth_data);
1196 pclose(f);
1197 } else {
1198 fprintf(stderr, "Could not run %s\n",
1199 cmd);
1200 }
1201 }
1202}
1203
1204static void
1205do_nologin(struct passwd *pw)
1206{
1207 FILE *f = NULL;
1208 char buf[1024];
1209
1210#ifdef HAVE_LOGIN_CAP
1211 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1212 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1213 _PATH_NOLOGIN), "r");
1214#else
1215 if (pw->pw_uid)
1216 f = fopen(_PATH_NOLOGIN, "r");
1217#endif
1218 if (f) {
1219 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001220 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001221 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001222 while (fgets(buf, sizeof(buf), f))
1223 fputs(buf, stderr);
1224 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001225 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001226 exit(254);
1227 }
1228}
1229
1230/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001231void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001232do_setusercontext(struct passwd *pw)
1233{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001234#ifndef HAVE_CYGWIN
1235 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001236#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001237 {
1238
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001239#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001240 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1241 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001242#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001243#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001244# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001245 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001246# endif
Damien Millerd3526362004-01-23 14:16:26 +11001247# ifdef USE_PAM
1248 if (options.use_pam) {
1249 do_pam_session();
1250 do_pam_setcred(0);
1251 }
1252# endif /* USE_PAM */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001253 if (setusercontext(lc, pw, pw->pw_uid,
1254 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1255 perror("unable to set user context");
1256 exit(1);
1257 }
1258#else
1259# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1260 /* Sets login uid for accounting */
1261 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1262 error("setluid: %s", strerror(errno));
1263# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1264
1265 if (setlogin(pw->pw_name) < 0)
1266 error("setlogin failed: %s", strerror(errno));
1267 if (setgid(pw->pw_gid) < 0) {
1268 perror("setgid");
1269 exit(1);
1270 }
1271 /* Initialize the group list. */
1272 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1273 perror("initgroups");
1274 exit(1);
1275 }
1276 endgrent();
1277# ifdef USE_PAM
1278 /*
Damien Millera8e06ce2003-11-21 23:48:55 +11001279 * PAM credentials may take the form of supplementary groups.
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001280 * These will have been wiped by the above initgroups() call.
1281 * Reestablish them here.
1282 */
Damien Miller341c6e62003-09-02 23:18:52 +10001283 if (options.use_pam) {
1284 do_pam_session();
Damien Miller4e448a32003-05-14 15:11:48 +10001285 do_pam_setcred(0);
Damien Miller341c6e62003-09-02 23:18:52 +10001286 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001287# endif /* USE_PAM */
1288# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1289 irix_setusercontext(pw);
1290# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001291# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001292 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001293# endif /* _AIX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001294 /* Permanently switch to the desired uid. */
1295 permanently_set_uid(pw);
1296#endif
1297 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001298
1299#ifdef HAVE_CYGWIN
1300 if (is_winnt)
1301#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001302 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1303 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1304}
1305
Ben Lindstrom08105192002-03-22 02:50:06 +00001306static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001307do_pwchange(Session *s)
1308{
1309 fprintf(stderr, "WARNING: Your password has expired.\n");
1310 if (s->ttyfd != -1) {
1311 fprintf(stderr,
1312 "You must change your password now and login again!\n");
1313 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
1314 perror("passwd");
1315 } else {
1316 fprintf(stderr,
1317 "Password change required but no TTY available.\n");
1318 }
1319 exit(1);
1320}
1321
1322static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001323launch_login(struct passwd *pw, const char *hostname)
1324{
1325 /* Launch login(1). */
1326
Ben Lindstrom378a4172002-06-07 14:49:56 +00001327 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001328#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001329 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001330#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001331#ifdef LOGIN_NO_ENDOPT
1332 "-p", "-f", pw->pw_name, (char *)NULL);
1333#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001335#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001336
1337 /* Login couldn't be executed, die. */
1338
1339 perror("login");
1340 exit(1);
1341}
1342
Darren Tucker23bc8d02004-02-06 16:24:31 +11001343static void
1344child_close_fds(void)
1345{
1346 int i;
1347
1348 if (packet_get_connection_in() == packet_get_connection_out())
1349 close(packet_get_connection_in());
1350 else {
1351 close(packet_get_connection_in());
1352 close(packet_get_connection_out());
1353 }
1354 /*
1355 * Close all descriptors related to channels. They will still remain
1356 * open in the parent.
1357 */
1358 /* XXX better use close-on-exec? -markus */
1359 channel_close_all();
1360
1361 /*
1362 * Close any extra file descriptors. Note that there may still be
1363 * descriptors left by system functions. They will be closed later.
1364 */
1365 endpwent();
1366
1367 /*
1368 * Close any extra open file descriptors so that we don\'t have them
1369 * hanging around in clients. Note that we want to do this after
1370 * initgroups, because at least on Solaris 2.3 it leaves file
1371 * descriptors open.
1372 */
1373 for (i = 3; i < 64; i++)
1374 close(i);
1375}
1376
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001377/*
1378 * Performs common processing for the child, such as setting up the
1379 * environment, closing extra file descriptors, setting the user and group
1380 * ids, and executing the command or shell.
1381 */
1382void
1383do_child(Session *s, const char *command)
1384{
1385 extern char **environ;
1386 char **env;
1387 char *argv[10];
1388 const char *shell, *shell0, *hostname = NULL;
1389 struct passwd *pw = s->pw;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001390
1391 /* remove hostkey from the child's memory */
1392 destroy_sensitive_data();
1393
Darren Tucker23bc8d02004-02-06 16:24:31 +11001394 /* Force a password change */
1395 if (s->authctxt->force_pwchange) {
1396 do_setusercontext(pw);
1397 child_close_fds();
1398 do_pwchange(s);
1399 exit(1);
1400 }
1401
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001402 /* login(1) is only called if we execute the login shell */
1403 if (options.use_login && command != NULL)
1404 options.use_login = 0;
1405
Tim Rice81ed5182002-09-25 17:38:46 -07001406#ifdef _UNICOS
1407 cray_setup(pw->pw_uid, pw->pw_name, command);
1408#endif /* _UNICOS */
1409
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001410 /*
1411 * Login(1) does this as well, and it needs uid 0 for the "-h"
1412 * switch, so we let login(1) to this for us.
1413 */
1414 if (!options.use_login) {
1415#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001416 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001417 if (!check_quietlogin(s, command))
1418 do_motd();
1419#else /* HAVE_OSF_SIA */
1420 do_nologin(pw);
1421 do_setusercontext(pw);
1422#endif /* HAVE_OSF_SIA */
1423 }
1424
1425 /*
1426 * Get the shell from the password data. An empty shell field is
1427 * legal, and means /bin/sh.
1428 */
1429 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001430
1431 /*
1432 * Make sure $SHELL points to the shell from the password file,
1433 * even if shell is overridden from login.conf
1434 */
1435 env = do_setup_env(s, shell);
1436
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001437#ifdef HAVE_LOGIN_CAP
1438 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1439#endif
1440
Damien Millerad833b32000-08-23 10:46:23 +10001441 /* we have to stash the hostname before we close our socket. */
1442 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001443 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001444 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001445 /*
1446 * Close the connection descriptors; note that this is the child, and
1447 * the server will still have the socket open, and it is important
1448 * that we do not shutdown it. Note that the descriptors cannot be
1449 * closed before building the environment, as we call
1450 * get_remote_ipaddr there.
1451 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001452 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001453
Damien Millerb38eff82000-04-01 11:09:21 +10001454 /*
Damien Miller05eda432002-02-10 18:32:28 +11001455 * Must take new environment into use so that .ssh/rc,
1456 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001457 */
1458 environ = env;
1459
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001460#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001461 /*
1462 * At this point, we check to see if AFS is active and if we have
1463 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1464 * if we can (and need to) extend the ticket into an AFS token. If
1465 * we don't do this, we run into potential problems if the user's
1466 * home directory is in AFS and it's not world-readable.
1467 */
1468
1469 if (options.kerberos_get_afs_token && k_hasafs() &&
1470 (s->authctxt->krb5_ctx != NULL)) {
1471 char cell[64];
1472
1473 debug("Getting AFS token");
1474
1475 k_setpag();
1476
1477 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1478 krb5_afslog(s->authctxt->krb5_ctx,
1479 s->authctxt->krb5_fwd_ccache, cell, NULL);
1480
1481 krb5_afslog_home(s->authctxt->krb5_ctx,
1482 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1483 }
1484#endif
1485
Damien Miller139d4cd2001-10-10 15:07:44 +10001486 /* Change current directory to the user\'s home directory. */
1487 if (chdir(pw->pw_dir) < 0) {
1488 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1489 pw->pw_dir, strerror(errno));
1490#ifdef HAVE_LOGIN_CAP
1491 if (login_getcapbool(lc, "requirehome", 0))
1492 exit(1);
1493#endif
1494 }
1495
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001496 if (!options.use_login)
1497 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001498
1499 /* restore SIGPIPE for child */
1500 signal(SIGPIPE, SIG_DFL);
1501
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001502 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503 launch_login(pw, hostname);
1504 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001505 }
1506
1507 /* Get the last component of the shell name. */
1508 if ((shell0 = strrchr(shell, '/')) != NULL)
1509 shell0++;
1510 else
1511 shell0 = shell;
1512
Damien Millerb38eff82000-04-01 11:09:21 +10001513 /*
1514 * If we have no command, execute the shell. In this case, the shell
1515 * name to be passed in argv[0] is preceded by '-' to indicate that
1516 * this is a login shell.
1517 */
1518 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001519 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001520
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001521 /* Start the shell. Set initial character to '-'. */
1522 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001523
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001524 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1525 >= sizeof(argv0) - 1) {
1526 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001527 perror(shell);
1528 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001529 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001530
1531 /* Execute the shell. */
1532 argv[0] = argv0;
1533 argv[1] = NULL;
1534 execve(shell, argv, env);
1535
1536 /* Executing the shell failed. */
1537 perror(shell);
1538 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001539 }
1540 /*
1541 * Execute the command using the user's shell. This uses the -c
1542 * option to execute the command.
1543 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001544 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001545 argv[1] = "-c";
1546 argv[2] = (char *) command;
1547 argv[3] = NULL;
1548 execve(shell, argv, env);
1549 perror(shell);
1550 exit(1);
1551}
1552
1553Session *
1554session_new(void)
1555{
1556 int i;
1557 static int did_init = 0;
1558 if (!did_init) {
1559 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001560 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001561 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001562 }
1563 did_init = 1;
1564 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001565 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001566 Session *s = &sessions[i];
1567 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001568 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001569 s->chanid = -1;
1570 s->ptyfd = -1;
1571 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001572 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001573 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001574 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001575 return s;
1576 }
1577 }
1578 return NULL;
1579}
1580
Ben Lindstrombba81212001-06-25 05:01:22 +00001581static void
Damien Millerb38eff82000-04-01 11:09:21 +10001582session_dump(void)
1583{
1584 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001585 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001586 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001587 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001588 s->used,
1589 s->self,
1590 s,
1591 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001592 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001593 }
1594}
1595
Damien Millerefb4afe2000-04-12 18:45:05 +10001596int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001597session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001598{
1599 Session *s = session_new();
1600 debug("session_open: channel %d", chanid);
1601 if (s == NULL) {
1602 error("no more sessions");
1603 return 0;
1604 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001605 s->authctxt = authctxt;
1606 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001607 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001608 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001609 debug("session_open: session %d: link with channel %d", s->self, chanid);
1610 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001611 return 1;
1612}
1613
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614Session *
1615session_by_tty(char *tty)
1616{
1617 int i;
1618 for (i = 0; i < MAX_SESSIONS; i++) {
1619 Session *s = &sessions[i];
1620 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1621 debug("session_by_tty: session %d tty %s", i, tty);
1622 return s;
1623 }
1624 }
1625 debug("session_by_tty: unknown tty %.100s", tty);
1626 session_dump();
1627 return NULL;
1628}
1629
Ben Lindstrombba81212001-06-25 05:01:22 +00001630static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001631session_by_channel(int id)
1632{
1633 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001634 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001635 Session *s = &sessions[i];
1636 if (s->used && s->chanid == id) {
1637 debug("session_by_channel: session %d channel %d", i, id);
1638 return s;
1639 }
1640 }
1641 debug("session_by_channel: unknown channel %d", id);
1642 session_dump();
1643 return NULL;
1644}
1645
Ben Lindstrombba81212001-06-25 05:01:22 +00001646static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001647session_by_pid(pid_t pid)
1648{
1649 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001650 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001651 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001652 Session *s = &sessions[i];
1653 if (s->used && s->pid == pid)
1654 return s;
1655 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001656 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001657 session_dump();
1658 return NULL;
1659}
1660
Ben Lindstrombba81212001-06-25 05:01:22 +00001661static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001662session_window_change_req(Session *s)
1663{
1664 s->col = packet_get_int();
1665 s->row = packet_get_int();
1666 s->xpixel = packet_get_int();
1667 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001668 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001669 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1670 return 1;
1671}
1672
Ben Lindstrombba81212001-06-25 05:01:22 +00001673static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001674session_pty_req(Session *s)
1675{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001676 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001677 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001678
Ben Lindstrom49c12602001-06-13 04:37:36 +00001679 if (no_pty_flag) {
1680 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001681 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001682 }
1683 if (s->ttyfd != -1) {
1684 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001685 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001686 }
Ben Lindstromc447fee2002-04-02 20:35:35 +00001687 /* Get the time and hostname when the user last logged in. */
1688 if (options.print_lastlog) {
1689 s->hostname[0] = '\0';
1690 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1691 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1692 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001693
Damien Millerefb4afe2000-04-12 18:45:05 +10001694 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001695
1696 if (compat20) {
1697 s->col = packet_get_int();
1698 s->row = packet_get_int();
1699 } else {
1700 s->row = packet_get_int();
1701 s->col = packet_get_int();
1702 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001703 s->xpixel = packet_get_int();
1704 s->ypixel = packet_get_int();
1705
1706 if (strcmp(s->term, "") == 0) {
1707 xfree(s->term);
1708 s->term = NULL;
1709 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001710
Damien Millerefb4afe2000-04-12 18:45:05 +10001711 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001712 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001713 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001714 if (s->term)
1715 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001716 s->term = NULL;
1717 s->ptyfd = -1;
1718 s->ttyfd = -1;
1719 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001720 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001721 }
1722 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001723
1724 /* for SSH1 the tty modes length is not given */
1725 if (!compat20)
1726 n_bytes = packet_remaining();
1727 tty_parse_modes(s->ttyfd, &n_bytes);
1728
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001729 if (!use_privsep)
1730 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001731
1732 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001733 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1734
Damien Miller48b03fc2002-01-22 23:11:40 +11001735 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001736 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001737 return 1;
1738}
1739
Ben Lindstrombba81212001-06-25 05:01:22 +00001740static int
Damien Millerbd483e72000-04-30 10:00:53 +10001741session_subsystem_req(Session *s)
1742{
Damien Millerae452462001-10-10 15:08:06 +10001743 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001744 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001745 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001746 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001747 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001748
Damien Miller48b03fc2002-01-22 23:11:40 +11001749 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001750 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001751
Damien Millerf6d9e222000-06-18 14:50:44 +10001752 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001753 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1754 cmd = options.subsystem_command[i];
1755 if (stat(cmd, &st) < 0) {
1756 error("subsystem: cannot stat %s: %s", cmd,
1757 strerror(errno));
1758 break;
1759 }
1760 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001761 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001762 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001763 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001764 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001765 }
1766 }
1767
1768 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10001769 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001770 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001771
Damien Millerbd483e72000-04-30 10:00:53 +10001772 xfree(subsys);
1773 return success;
1774}
1775
Ben Lindstrombba81212001-06-25 05:01:22 +00001776static int
Damien Millerbd483e72000-04-30 10:00:53 +10001777session_x11_req(Session *s)
1778{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001779 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001780
1781 s->single_connection = packet_get_char();
1782 s->auth_proto = packet_get_string(NULL);
1783 s->auth_data = packet_get_string(NULL);
1784 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001785 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001786
Ben Lindstrom768176b2001-06-09 01:29:12 +00001787 success = session_setup_x11fwd(s);
1788 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001789 xfree(s->auth_proto);
1790 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001791 s->auth_proto = NULL;
1792 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001793 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001794 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001795}
1796
Ben Lindstrombba81212001-06-25 05:01:22 +00001797static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001798session_shell_req(Session *s)
1799{
Damien Miller48b03fc2002-01-22 23:11:40 +11001800 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001801 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001802 return 1;
1803}
1804
Ben Lindstrombba81212001-06-25 05:01:22 +00001805static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001806session_exec_req(Session *s)
1807{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001808 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001809 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001810 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001811 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001812 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001813 return 1;
1814}
1815
Ben Lindstrombba81212001-06-25 05:01:22 +00001816static int
Damien Miller54c45982003-05-15 10:20:13 +10001817session_break_req(Session *s)
1818{
1819 u_int break_length;
1820
Darren Tucker3bdbd842003-08-13 20:31:05 +10001821 break_length = packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10001822 packet_check_eom();
1823
Darren Tucker3bdbd842003-08-13 20:31:05 +10001824 if (s->ttyfd == -1 ||
1825 tcsendbreak(s->ttyfd, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10001826 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10001827 return 1;
1828}
1829
1830static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001831session_auth_agent_req(Session *s)
1832{
1833 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001834 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001835 if (no_agent_forwarding_flag) {
1836 debug("session_auth_agent_req: no_agent_forwarding_flag");
1837 return 0;
1838 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001839 if (called) {
1840 return 0;
1841 } else {
1842 called = 1;
1843 return auth_input_request_forwarding(s->pw);
1844 }
1845}
1846
Damien Millerc7ef63d2002-02-05 12:21:42 +11001847int
1848session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001849{
Damien Millerefb4afe2000-04-12 18:45:05 +10001850 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001851 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001852
Damien Millerc7ef63d2002-02-05 12:21:42 +11001853 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10001854 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11001855 c->self, rtype);
1856 return 0;
1857 }
1858 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001859
1860 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001861 * a session is in LARVAL state until a shell, a command
1862 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001863 */
1864 if (c->type == SSH_CHANNEL_LARVAL) {
1865 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001866 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001867 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001868 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001869 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001870 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001871 } else if (strcmp(rtype, "x11-req") == 0) {
1872 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001873 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1874 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001875 } else if (strcmp(rtype, "subsystem") == 0) {
1876 success = session_subsystem_req(s);
Damien Miller54c45982003-05-15 10:20:13 +10001877 } else if (strcmp(rtype, "break") == 0) {
1878 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001879 }
1880 }
1881 if (strcmp(rtype, "window-change") == 0) {
1882 success = session_window_change_req(s);
1883 }
Damien Millerc7ef63d2002-02-05 12:21:42 +11001884 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10001885}
1886
1887void
1888session_set_fds(Session *s, int fdin, int fdout, int fderr)
1889{
1890 if (!compat20)
1891 fatal("session_set_fds: called for proto != 2.0");
1892 /*
1893 * now that have a child and a pipe to the child,
1894 * we can activate our channel and register the fd's
1895 */
1896 if (s->chanid == -1)
1897 fatal("no channel for session %d", s->self);
1898 channel_set_fds(s->chanid,
1899 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001900 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11001901 1,
1902 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10001903}
1904
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001905/*
1906 * Function to perform pty cleanup. Also called if we get aborted abnormally
1907 * (e.g., due to a dropped connection).
1908 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001909void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001910session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10001911{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001912 if (s == NULL) {
1913 error("session_pty_cleanup: no session");
1914 return;
1915 }
1916 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001917 return;
1918
Kevin Steves43cdef32001-02-11 14:12:08 +00001919 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001920
Damien Millerb38eff82000-04-01 11:09:21 +10001921 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001922 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08001923 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001924
1925 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001926 if (getuid() == 0)
1927 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001928
1929 /*
1930 * Close the server side of the socket pairs. We must do this after
1931 * the pty cleanup, so that another process doesn't get this pty
1932 * while we're still cleaning up.
1933 */
1934 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001935 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001936
1937 /* unlink pty from session */
1938 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001939}
Damien Millerefb4afe2000-04-12 18:45:05 +10001940
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001941void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001942session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001943{
Darren Tucker3e33cec2003-10-02 16:12:36 +10001944 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001945}
1946
Damien Miller5a80bba2002-09-04 16:39:02 +10001947static char *
1948sig2name(int sig)
1949{
1950#define SSH_SIG(x) if (sig == SIG ## x) return #x
1951 SSH_SIG(ABRT);
1952 SSH_SIG(ALRM);
1953 SSH_SIG(FPE);
1954 SSH_SIG(HUP);
1955 SSH_SIG(ILL);
1956 SSH_SIG(INT);
1957 SSH_SIG(KILL);
1958 SSH_SIG(PIPE);
1959 SSH_SIG(QUIT);
1960 SSH_SIG(SEGV);
1961 SSH_SIG(TERM);
1962 SSH_SIG(USR1);
1963 SSH_SIG(USR2);
1964#undef SSH_SIG
1965 return "SIG@openssh.com";
1966}
1967
Ben Lindstrombba81212001-06-25 05:01:22 +00001968static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001969session_exit_message(Session *s, int status)
1970{
1971 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001972
1973 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001974 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001975 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001976 debug("session_exit_message: session %d channel %d pid %ld",
1977 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001978
1979 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001980 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001981 packet_put_int(WEXITSTATUS(status));
1982 packet_send();
1983 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001984 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10001985 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001986#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001987 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001988#else /* WCOREDUMP */
1989 packet_put_char(0);
1990#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001991 packet_put_cstring("");
1992 packet_put_cstring("");
1993 packet_send();
1994 } else {
1995 /* Some weird exit cause. Just exit. */
1996 packet_disconnect("wait returned status %04x.", status);
1997 }
1998
1999 /* disconnect channel */
2000 debug("session_exit_message: release channel %d", s->chanid);
2001 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10002002 /*
2003 * emulate a write failure with 'chan_write_failed', nobody will be
2004 * interested in data we write.
2005 * Note that we must not call 'chan_read_failed', since there could
2006 * be some more data waiting in the pipe.
2007 */
Damien Millerbd483e72000-04-30 10:00:53 +10002008 if (c->ostate != CHAN_OUTPUT_CLOSED)
2009 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002010 s->chanid = -1;
2011}
2012
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002013void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002014session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002015{
Ben Lindstromce0f6342002-06-11 16:42:49 +00002016 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002017 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002018 session_pty_cleanup(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002019 if (s->term)
2020 xfree(s->term);
2021 if (s->display)
2022 xfree(s->display);
Damien Miller512bccb2002-02-05 12:11:02 +11002023 if (s->auth_display)
2024 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10002025 if (s->auth_data)
2026 xfree(s->auth_data);
2027 if (s->auth_proto)
2028 xfree(s->auth_proto);
2029 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10002030 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002031}
2032
2033void
2034session_close_by_pid(pid_t pid, int status)
2035{
2036 Session *s = session_by_pid(pid);
2037 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002038 debug("session_close_by_pid: no session for pid %ld",
2039 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002040 return;
2041 }
2042 if (s->chanid != -1)
2043 session_exit_message(s, status);
2044 session_close(s);
2045}
2046
2047/*
2048 * this is called when a channel dies before
2049 * the session 'child' itself dies
2050 */
2051void
2052session_close_by_channel(int id, void *arg)
2053{
2054 Session *s = session_by_channel(id);
2055 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002056 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002057 return;
2058 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002059 debug("session_close_by_channel: channel %d child %ld",
2060 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002061 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002062 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002063 /*
2064 * delay detach of session, but release pty, since
2065 * the fd's to the child are already closed
2066 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002067 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002068 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002069 return;
2070 }
2071 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002072 channel_cancel_cleanup(s->chanid);
2073 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002074 session_close(s);
2075}
2076
2077void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002078session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002079{
2080 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002081 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002082 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002083 if (s->used) {
2084 if (closefunc != NULL)
2085 closefunc(s);
2086 else
2087 session_close(s);
2088 }
Damien Miller52b77be2001-10-10 15:14:37 +10002089 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002090}
2091
Ben Lindstrombba81212001-06-25 05:01:22 +00002092static char *
Damien Millere247cc42000-05-07 12:03:14 +10002093session_tty_list(void)
2094{
2095 static char buf[1024];
2096 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002097 char *cp;
2098
Damien Millere247cc42000-05-07 12:03:14 +10002099 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002100 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002101 Session *s = &sessions[i];
2102 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002103
Damien Millera8ed44b2003-01-10 09:53:12 +11002104 if (strncmp(s->tty, "/dev/", 5) != 0) {
2105 cp = strrchr(s->tty, '/');
2106 cp = (cp == NULL) ? s->tty : cp + 1;
2107 } else
2108 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002109
Damien Millere247cc42000-05-07 12:03:14 +10002110 if (buf[0] != '\0')
2111 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002112 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002113 }
2114 }
2115 if (buf[0] == '\0')
2116 strlcpy(buf, "notty", sizeof buf);
2117 return buf;
2118}
2119
2120void
2121session_proctitle(Session *s)
2122{
2123 if (s->pw == NULL)
2124 error("no user for session %d", s->self);
2125 else
2126 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2127}
2128
Ben Lindstrom768176b2001-06-09 01:29:12 +00002129int
2130session_setup_x11fwd(Session *s)
2131{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002132 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002133 char display[512], auth_display[512];
2134 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00002135
2136 if (no_x11_forwarding_flag) {
2137 packet_send_debug("X11 forwarding disabled in user configuration file.");
2138 return 0;
2139 }
2140 if (!options.x11_forwarding) {
2141 debug("X11 forwarding disabled in server configuration file.");
2142 return 0;
2143 }
2144 if (!options.xauth_location ||
2145 (stat(options.xauth_location, &st) == -1)) {
2146 packet_send_debug("No xauth program; cannot forward with spoofing.");
2147 return 0;
2148 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002149 if (options.use_login) {
2150 packet_send_debug("X11 forwarding disabled; "
2151 "not compatible with UseLogin=yes.");
2152 return 0;
2153 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002154 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002155 debug("X11 display already set.");
2156 return 0;
2157 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002158 if (x11_create_display_inet(options.x11_display_offset,
2159 options.x11_use_localhost, s->single_connection,
2160 &s->display_number) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002161 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002162 return 0;
2163 }
Kevin Steves366298c2001-12-19 17:58:01 +00002164
2165 /* Set up a suitable value for the DISPLAY variable. */
2166 if (gethostname(hostname, sizeof(hostname)) < 0)
2167 fatal("gethostname: %.100s", strerror(errno));
2168 /*
2169 * auth_display must be used as the displayname when the
2170 * authorization entry is added with xauth(1). This will be
2171 * different than the DISPLAY string for localhost displays.
2172 */
Damien Miller95c249f2002-02-05 12:11:34 +11002173 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002174 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002175 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002176 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002177 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002178 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002179 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002180 } else {
2181#ifdef IPADDR_IN_DISPLAY
2182 struct hostent *he;
2183 struct in_addr my_addr;
2184
2185 he = gethostbyname(hostname);
2186 if (he == NULL) {
2187 error("Can't get IP address for X11 DISPLAY.");
2188 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2189 return 0;
2190 }
2191 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002192 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002193 s->display_number, s->screen);
2194#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002195 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002196 s->display_number, s->screen);
2197#endif
2198 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002199 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002200 }
2201
Ben Lindstrom768176b2001-06-09 01:29:12 +00002202 return 1;
2203}
2204
Ben Lindstrombba81212001-06-25 05:01:22 +00002205static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002206do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002207{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002208 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002209}
2210
2211void
2212do_cleanup(Authctxt *authctxt)
2213{
2214 static int called = 0;
2215
2216 debug("do_cleanup");
2217
2218 /* no cleanup if we're in the child for login shell */
2219 if (is_child)
2220 return;
2221
2222 /* avoid double cleanup */
2223 if (called)
2224 return;
2225 called = 1;
2226
2227 if (authctxt == NULL)
2228 return;
2229#ifdef KRB5
2230 if (options.kerberos_ticket_cleanup &&
2231 authctxt->krb5_ctx)
2232 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002233#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002234
2235#ifdef GSSAPI
2236 if (compat20 && options.gss_cleanup_creds)
2237 ssh_gssapi_cleanup_creds();
2238#endif
2239
Darren Tucker8846a072003-10-07 11:30:15 +10002240#ifdef USE_PAM
2241 if (options.use_pam) {
2242 sshpam_cleanup();
2243 sshpam_thread_cleanup();
2244 }
2245#endif
2246
Darren Tucker3e33cec2003-10-02 16:12:36 +10002247 /* remove agent socket */
2248 auth_sock_cleanup_proc(authctxt->pw);
2249
2250 /*
2251 * Cleanup ptys/utmp only if privsep is disabled,
2252 * or if running in monitor.
2253 */
2254 if (!use_privsep || mm_is_monitor())
2255 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002256}