blob: 4b443831bbd5253b4b2841fac7388fe366ea1113 [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
Damien Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000012 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Damien Miller3a961dc2003-06-03 10:25:48 +100036RCSID("$OpenBSD: session.c,v 1.158 2003/06/02 09:17:34 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
Damien Millerbac2d8a2000-09-05 16:13:06 +110061#ifdef HAVE_CYGWIN
62#include <windows.h>
63#include <sys/cygwin.h>
64#define is_winnt (GetVersion() < 0x80000000)
65#endif
66
Damien Millerb38eff82000-04-01 11:09:21 +100067/* func */
68
69Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000070void session_set_fds(Session *, int, int, int);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071void session_pty_cleanup(void *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000072void session_proctitle(Session *);
73int session_setup_x11fwd(Session *);
74void do_exec_pty(Session *, const char *);
75void do_exec_no_pty(Session *, const char *);
76void do_exec(Session *, const char *);
77void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +000078#ifdef LOGIN_NEEDS_UTMPX
79static void do_pre_login(Session *s);
80#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +000081void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +100082void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000083int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +100084
Ben Lindstrombba81212001-06-25 05:01:22 +000085static void do_authenticated1(Authctxt *);
86static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000087
Ben Lindstrombba81212001-06-25 05:01:22 +000088static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +000089
Damien Millerb38eff82000-04-01 11:09:21 +100090/* import */
91extern ServerOptions options;
92extern char *__progname;
93extern int log_stderr;
94extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +000095extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +100096extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +000097extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +100098extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +100099
Damien Miller7b28dc52000-09-05 13:34:53 +1100100/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000101const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100102
Damien Millerb38eff82000-04-01 11:09:21 +1000103/* data */
104#define MAX_SESSIONS 10
105Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100106
Damien Millerad833b32000-08-23 10:46:23 +1000107#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000108login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000109#endif
110
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000111/* Name and directory of socket for authentication agent forwarding. */
112static char *auth_sock_name = NULL;
113static char *auth_sock_dir = NULL;
114
115/* removes the agent forwarding socket */
116
117static void
118auth_sock_cleanup_proc(void *_pw)
119{
120 struct passwd *pw = _pw;
121
122 if (auth_sock_name != NULL) {
123 temporarily_use_uid(pw);
124 unlink(auth_sock_name);
125 rmdir(auth_sock_dir);
126 auth_sock_name = NULL;
127 restore_uid();
128 }
129}
130
131static int
132auth_input_request_forwarding(struct passwd * pw)
133{
134 Channel *nc;
135 int sock;
136 struct sockaddr_un sunaddr;
137
138 if (auth_sock_name != NULL) {
139 error("authentication forwarding requested twice.");
140 return 0;
141 }
142
143 /* Temporarily drop privileged uid for mkdir/bind. */
144 temporarily_use_uid(pw);
145
146 /* Allocate a buffer for the socket name, and format the name. */
147 auth_sock_name = xmalloc(MAXPATHLEN);
148 auth_sock_dir = xmalloc(MAXPATHLEN);
149 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
150
151 /* Create private directory for socket */
152 if (mkdtemp(auth_sock_dir) == NULL) {
153 packet_send_debug("Agent forwarding disabled: "
154 "mkdtemp() failed: %.100s", strerror(errno));
155 restore_uid();
156 xfree(auth_sock_name);
157 xfree(auth_sock_dir);
158 auth_sock_name = NULL;
159 auth_sock_dir = NULL;
160 return 0;
161 }
Ben Lindstromce0f6342002-06-11 16:42:49 +0000162 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
163 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000164
165 /* delete agent socket on fatal() */
166 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
167
168 /* Create the socket. */
169 sock = socket(AF_UNIX, SOCK_STREAM, 0);
170 if (sock < 0)
171 packet_disconnect("socket: %.100s", strerror(errno));
172
173 /* Bind it to the name. */
174 memset(&sunaddr, 0, sizeof(sunaddr));
175 sunaddr.sun_family = AF_UNIX;
176 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
177
178 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
179 packet_disconnect("bind: %.100s", strerror(errno));
180
181 /* Restore the privileged uid. */
182 restore_uid();
183
184 /* Start listening on the socket. */
185 if (listen(sock, 5) < 0)
186 packet_disconnect("listen: %.100s", strerror(errno));
187
188 /* Allocate a channel for the authentication agent socket. */
189 nc = channel_new("auth socket",
190 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
191 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000192 0, "auth socket", 1);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000193 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
194 return 1;
195}
196
197
Ben Lindstromb31783d2001-03-22 02:02:12 +0000198void
199do_authenticated(Authctxt *authctxt)
200{
Damien Miller97f39ae2003-02-24 11:57:01 +1100201 setproctitle("%s", authctxt->pw->pw_name);
202
Ben Lindstromb31783d2001-03-22 02:02:12 +0000203 /*
204 * Cancel the alarm we set to limit the time taken for
205 * authentication.
206 */
207 alarm(0);
208 if (startup_pipe != -1) {
209 close(startup_pipe);
210 startup_pipe = -1;
211 }
Damien Millere49d0962001-11-13 23:46:18 +1100212
Ben Lindstromb31783d2001-03-22 02:02:12 +0000213 /* setup the channel layer */
214 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
215 channel_permit_all_opens();
216
217 if (compat20)
218 do_authenticated2(authctxt);
219 else
220 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000221
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000222 /* remove agent socket */
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000223 if (auth_sock_name != NULL)
Ben Lindstrom983c0982001-06-09 01:20:06 +0000224 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000225#ifdef KRB4
226 if (options.kerberos_ticket_cleanup)
227 krb4_cleanup_proc(authctxt);
228#endif
229#ifdef KRB5
230 if (options.kerberos_ticket_cleanup)
231 krb5_cleanup_proc(authctxt);
232#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000233}
234
Damien Millerb38eff82000-04-01 11:09:21 +1000235/*
Damien Millerb38eff82000-04-01 11:09:21 +1000236 * Prepares for an interactive session. This is called after the user has
237 * been successfully authenticated. During this message exchange, pseudo
238 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
239 * are requested, etc.
240 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000241static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000242do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000243{
244 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000245 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100246 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000247 int enable_compression_after_reply = 0;
248 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000249
250 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000251 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000252 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000253
Damien Millerb38eff82000-04-01 11:09:21 +1000254 /*
255 * We stay in this loop until the client requests to execute a shell
256 * or a command.
257 */
258 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000259 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000260
261 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100262 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000263
264 /* Process the packet. */
265 switch (type) {
266 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000267 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100268 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000269 if (compression_level < 1 || compression_level > 9) {
270 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100271 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000272 break;
273 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000274 if (!options.compression) {
275 debug2("compression disabled");
276 break;
277 }
Damien Millerb38eff82000-04-01 11:09:21 +1000278 /* Enable compression after we have responded with SUCCESS. */
279 enable_compression_after_reply = 1;
280 success = 1;
281 break;
282
283 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000284 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000285 break;
286
287 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000288 s->auth_proto = packet_get_string(&proto_len);
289 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000290
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000291 screen_flag = packet_get_protocol_flags() &
292 SSH_PROTOFLAG_SCREEN_NUMBER;
293 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
294
295 if (packet_remaining() == 4) {
296 if (!screen_flag)
297 debug2("Buggy client: "
298 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000299 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000300 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000301 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000302 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100303 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000304 success = session_setup_x11fwd(s);
305 if (!success) {
306 xfree(s->auth_proto);
307 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000308 s->auth_proto = NULL;
309 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000310 }
Damien Millerb38eff82000-04-01 11:09:21 +1000311 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000312
313 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
314 if (no_agent_forwarding_flag || compat13) {
315 debug("Authentication agent forwarding not permitted for this authentication.");
316 break;
317 }
318 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000319 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000320 break;
321
322 case SSH_CMSG_PORT_FORWARD_REQUEST:
323 if (no_port_forwarding_flag) {
324 debug("Port forwarding not permitted for this authentication.");
325 break;
326 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100327 if (!options.allow_tcp_forwarding) {
328 debug("Port forwarding not permitted.");
329 break;
330 }
Damien Millerb38eff82000-04-01 11:09:21 +1000331 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000332 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000333 success = 1;
334 break;
335
336 case SSH_CMSG_MAX_PACKET_SIZE:
337 if (packet_set_maxsize(packet_get_int()) > 0)
338 success = 1;
339 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100340
Ben Lindstromec95ed92001-07-04 04:21:14 +0000341#if defined(AFS) || defined(KRB5)
342 case SSH_CMSG_HAVE_KERBEROS_TGT:
343 if (!options.kerberos_tgt_passing) {
344 verbose("Kerberos TGT passing disabled.");
345 } else {
346 char *kdata = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100347 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100348
Ben Lindstromec95ed92001-07-04 04:21:14 +0000349 /* XXX - 0x41, see creds_to_radix version */
350 if (kdata[0] != 0x41) {
351#ifdef KRB5
352 krb5_data tgt;
353 tgt.data = kdata;
354 tgt.length = dlen;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100355
Ben Lindstromec95ed92001-07-04 04:21:14 +0000356 if (auth_krb5_tgt(s->authctxt, &tgt))
357 success = 1;
358 else
359 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
360#endif /* KRB5 */
361 } else {
362#ifdef AFS
363 if (auth_krb4_tgt(s->authctxt, kdata))
364 success = 1;
365 else
366 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
367#endif /* AFS */
368 }
369 xfree(kdata);
370 }
371 break;
372#endif /* AFS || KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100373
Ben Lindstromec95ed92001-07-04 04:21:14 +0000374#ifdef AFS
375 case SSH_CMSG_HAVE_AFS_TOKEN:
376 if (!options.afs_token_passing || !k_hasafs()) {
377 verbose("AFS token passing disabled.");
378 } else {
379 /* Accept AFS token. */
380 char *token = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100381 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100382
Ben Lindstromec95ed92001-07-04 04:21:14 +0000383 if (auth_afs_token(s->authctxt, token))
384 success = 1;
385 else
386 verbose("AFS token refused for %.100s",
387 s->authctxt->user);
388 xfree(token);
389 }
390 break;
391#endif /* AFS */
Damien Millerb38eff82000-04-01 11:09:21 +1000392
393 case SSH_CMSG_EXEC_SHELL:
394 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000395 if (type == SSH_CMSG_EXEC_CMD) {
396 command = packet_get_string(&dlen);
397 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000398 do_exec(s, command);
399 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000400 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000401 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000402 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100403 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000404 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000405 return;
406
407 default:
408 /*
409 * Any unknown messages in this phase are ignored,
410 * and a failure message is returned.
411 */
Damien Miller996acd22003-04-09 20:59:48 +1000412 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000413 }
414 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
415 packet_send();
416 packet_write_wait();
417
418 /* Enable compression now that we have replied if appropriate. */
419 if (enable_compression_after_reply) {
420 enable_compression_after_reply = 0;
421 packet_start_compression(compression_level);
422 }
423 }
424}
425
426/*
427 * This is called to fork and execute a command when we have no tty. This
428 * will call do_child from the child, and server_loop from the parent after
429 * setting up file descriptors and such.
430 */
Damien Miller4af51302000-04-16 11:18:38 +1000431void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000432do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000433{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000434 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000435
436#ifdef USE_PIPES
437 int pin[2], pout[2], perr[2];
438 /* Allocate pipes for communicating with the program. */
439 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
440 packet_disconnect("Could not create pipes: %.100s",
441 strerror(errno));
442#else /* USE_PIPES */
443 int inout[2], err[2];
444 /* Uses socket pairs to communicate with the program. */
445 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
446 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
447 packet_disconnect("Could not create socket pairs: %.100s",
448 strerror(errno));
449#endif /* USE_PIPES */
450 if (s == NULL)
451 fatal("do_exec_no_pty: no session");
452
Damien Millere247cc42000-05-07 12:03:14 +1000453 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000454
Damien Millerc5946332001-02-28 11:46:11 +1100455#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000456 if (options.use_pam) {
457 do_pam_session(s->pw->pw_name, NULL);
458 do_pam_setcred(1);
459 if (is_pam_password_change_required())
460 packet_disconnect("Password change required but no "
461 "TTY available");
462 }
Kevin Stevesff793a22001-02-21 16:36:51 +0000463#endif /* USE_PAM */
464
Damien Millerb38eff82000-04-01 11:09:21 +1000465 /* Fork the child. */
466 if ((pid = fork()) == 0) {
Ben Lindstrom264ee302002-07-23 21:01:56 +0000467 fatal_remove_all_cleanups();
468
Damien Millerb38eff82000-04-01 11:09:21 +1000469 /* Child. Reinitialize the log since the pid has changed. */
470 log_init(__progname, options.log_level, options.log_facility, log_stderr);
471
472 /*
473 * Create a new session and process group since the 4.4BSD
474 * setlogin() affects the entire process group.
475 */
476 if (setsid() < 0)
477 error("setsid failed: %.100s", strerror(errno));
478
479#ifdef USE_PIPES
480 /*
481 * Redirect stdin. We close the parent side of the socket
482 * pair, and make the child side the standard input.
483 */
484 close(pin[1]);
485 if (dup2(pin[0], 0) < 0)
486 perror("dup2 stdin");
487 close(pin[0]);
488
489 /* Redirect stdout. */
490 close(pout[0]);
491 if (dup2(pout[1], 1) < 0)
492 perror("dup2 stdout");
493 close(pout[1]);
494
495 /* Redirect stderr. */
496 close(perr[0]);
497 if (dup2(perr[1], 2) < 0)
498 perror("dup2 stderr");
499 close(perr[1]);
500#else /* USE_PIPES */
501 /*
502 * Redirect stdin, stdout, and stderr. Stdin and stdout will
503 * use the same socket, as some programs (particularly rdist)
504 * seem to depend on it.
505 */
506 close(inout[1]);
507 close(err[1]);
508 if (dup2(inout[0], 0) < 0) /* stdin */
509 perror("dup2 stdin");
510 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
511 perror("dup2 stdout");
512 if (dup2(err[0], 2) < 0) /* stderr */
513 perror("dup2 stderr");
514#endif /* USE_PIPES */
515
Tim Rice81ed5182002-09-25 17:38:46 -0700516#ifdef _UNICOS
517 cray_init_job(s->pw); /* set up cray jid and tmpdir */
518#endif
519
Damien Millerb38eff82000-04-01 11:09:21 +1000520 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000521 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000522 /* NOTREACHED */
523 }
Tim Rice81ed5182002-09-25 17:38:46 -0700524#ifdef _UNICOS
525 signal(WJSIGNAL, cray_job_termination_handler);
526#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100527#ifdef HAVE_CYGWIN
528 if (is_winnt)
529 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
530#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000531 if (pid < 0)
532 packet_disconnect("fork failed: %.100s", strerror(errno));
533 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000534 /* Set interactive/non-interactive mode. */
535 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000536#ifdef USE_PIPES
537 /* We are the parent. Close the child sides of the pipes. */
538 close(pin[0]);
539 close(pout[1]);
540 close(perr[1]);
541
Damien Millerefb4afe2000-04-12 18:45:05 +1000542 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000543 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000544 } else {
545 /* Enter the interactive session. */
546 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000547 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000548 }
Damien Millerb38eff82000-04-01 11:09:21 +1000549#else /* USE_PIPES */
550 /* We are the parent. Close the child sides of the socket pairs. */
551 close(inout[0]);
552 close(err[0]);
553
554 /*
555 * Enter the interactive session. Note: server_loop must be able to
556 * handle the case that fdin and fdout are the same.
557 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000558 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000559 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000560 } else {
561 server_loop(pid, inout[1], inout[1], err[1]);
562 /* server_loop has closed inout[1] and err[1]. */
563 }
Damien Millerb38eff82000-04-01 11:09:21 +1000564#endif /* USE_PIPES */
565}
566
567/*
568 * This is called to fork and execute a command when we have a tty. This
569 * will call do_child from the child, and server_loop from the parent after
570 * setting up file descriptors, controlling tty, updating wtmp, utmp,
571 * lastlog, and other such operations.
572 */
Damien Miller4af51302000-04-16 11:18:38 +1000573void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000574do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000575{
Damien Millerb38eff82000-04-01 11:09:21 +1000576 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000577 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000578
579 if (s == NULL)
580 fatal("do_exec_pty: no session");
581 ptyfd = s->ptyfd;
582 ttyfd = s->ttyfd;
583
Damien Millerc5946332001-02-28 11:46:11 +1100584#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000585 if (options.use_pam) {
586 do_pam_session(s->pw->pw_name, s->tty);
587 do_pam_setcred(1);
588 }
Damien Miller5a761312001-02-27 09:28:23 +1100589#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000590
Damien Millerb38eff82000-04-01 11:09:21 +1000591 /* Fork the child. */
592 if ((pid = fork()) == 0) {
Ben Lindstrom264ee302002-07-23 21:01:56 +0000593 fatal_remove_all_cleanups();
Ben Lindstrombddd5512001-07-04 04:53:53 +0000594
Damien Miller942da032000-08-18 13:59:06 +1000595 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000596 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000597 /* Close the master side of the pseudo tty. */
598 close(ptyfd);
599
600 /* Make the pseudo tty our controlling tty. */
601 pty_make_controlling_tty(&ttyfd, s->tty);
602
Damien Miller9c751422001-10-10 15:02:46 +1000603 /* Redirect stdin/stdout/stderr from the pseudo tty. */
604 if (dup2(ttyfd, 0) < 0)
605 error("dup2 stdin: %s", strerror(errno));
606 if (dup2(ttyfd, 1) < 0)
607 error("dup2 stdout: %s", strerror(errno));
608 if (dup2(ttyfd, 2) < 0)
609 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000610
611 /* Close the extra descriptor for the pseudo tty. */
612 close(ttyfd);
613
Damien Miller942da032000-08-18 13:59:06 +1000614 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000615#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700616 if (!(options.use_login && command == NULL)) {
617#ifdef _UNICOS
618 cray_init_job(s->pw); /* set up cray jid and tmpdir */
619#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100620 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700621 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000622# ifdef LOGIN_NEEDS_UTMPX
623 else
624 do_pre_login(s);
625# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000626#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000627
Damien Millerb38eff82000-04-01 11:09:21 +1000628 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000629 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000630 /* NOTREACHED */
631 }
Tim Rice81ed5182002-09-25 17:38:46 -0700632#ifdef _UNICOS
633 signal(WJSIGNAL, cray_job_termination_handler);
634#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100635#ifdef HAVE_CYGWIN
636 if (is_winnt)
637 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
638#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000639 if (pid < 0)
640 packet_disconnect("fork failed: %.100s", strerror(errno));
641 s->pid = pid;
642
643 /* Parent. Close the slave side of the pseudo tty. */
644 close(ttyfd);
645
646 /*
647 * Create another descriptor of the pty master side for use as the
648 * standard input. We could use the original descriptor, but this
649 * simplifies code in server_loop. The descriptor is bidirectional.
650 */
651 fdout = dup(ptyfd);
652 if (fdout < 0)
653 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
654
655 /* we keep a reference to the pty master */
656 ptymaster = dup(ptyfd);
657 if (ptymaster < 0)
658 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
659 s->ptymaster = ptymaster;
660
661 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000662 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000663 if (compat20) {
664 session_set_fds(s, ptyfd, fdout, -1);
665 } else {
666 server_loop(pid, ptyfd, fdout, -1);
667 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000668 }
Damien Millerb38eff82000-04-01 11:09:21 +1000669}
670
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000671#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000672static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000673do_pre_login(Session *s)
674{
675 socklen_t fromlen;
676 struct sockaddr_storage from;
677 pid_t pid = getpid();
678
679 /*
680 * Get IP address of client. If the connection is not a socket, let
681 * the address be 0.0.0.0.
682 */
683 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000684 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000685 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000686 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100687 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000688 debug("getpeername: %.100s", strerror(errno));
689 fatal_cleanup();
690 }
691 }
692
693 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000694 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000695 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000696}
697#endif
698
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000699/*
700 * This is called to fork and execute a command. If another command is
701 * to be forced, execute that instead.
702 */
703void
704do_exec(Session *s, const char *command)
705{
706 if (forced_command) {
707 original_command = command;
708 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000709 debug("Forced command '%.900s'", command);
710 }
711
712 if (s->ttyfd != -1)
713 do_exec_pty(s, command);
714 else
715 do_exec_no_pty(s, command);
716
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000717 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000718}
719
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000720
Damien Miller942da032000-08-18 13:59:06 +1000721/* administrative, login(1)-like work */
722void
Damien Miller69b69aa2000-10-28 14:19:58 +1100723do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000724{
Damien Miller942da032000-08-18 13:59:06 +1000725 char *time_string;
Damien Miller942da032000-08-18 13:59:06 +1000726 socklen_t fromlen;
727 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000728 struct passwd * pw = s->pw;
729 pid_t pid = getpid();
730
731 /*
732 * Get IP address of client. If the connection is not a socket, let
733 * the address be 0.0.0.0.
734 */
735 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000736 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000737 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000738 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000739 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000740 debug("getpeername: %.100s", strerror(errno));
741 fatal_cleanup();
742 }
743 }
744
745 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000746 if (!use_privsep)
747 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
748 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000749 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000750 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000751
Kevin Steves092f2ef2000-10-14 13:36:13 +0000752#ifdef USE_PAM
753 /*
754 * If password change is needed, do it now.
755 * This needs to occur before the ~/.hushlogin check.
756 */
Damien Miller4e448a32003-05-14 15:11:48 +1000757 if (options.use_pam && is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000758 print_pam_messages();
759 do_pam_chauthtok();
760 }
761#endif
762
Damien Millercf205e82001-04-16 18:29:15 +1000763 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000764 return;
765
766#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000767 if (options.use_pam && !is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000768 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000769#endif /* USE_PAM */
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000770
771 /* display post-login message */
772 if (buffer_len(&loginmsg) > 0) {
773 buffer_append(&loginmsg, "\0", 1);
774 printf("%s\n", (char *)buffer_ptr(&loginmsg));
775 }
776 buffer_free(&loginmsg);
Damien Miller942da032000-08-18 13:59:06 +1000777
Tim Rice81ed5182002-09-25 17:38:46 -0700778#ifndef NO_SSH_LASTLOG
Ben Lindstromc447fee2002-04-02 20:35:35 +0000779 if (options.print_lastlog && s->last_login_time != 0) {
780 time_string = ctime(&s->last_login_time);
Damien Miller942da032000-08-18 13:59:06 +1000781 if (strchr(time_string, '\n'))
782 *strchr(time_string, '\n') = 0;
Ben Lindstromc447fee2002-04-02 20:35:35 +0000783 if (strcmp(s->hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000784 printf("Last login: %s\r\n", time_string);
785 else
Ben Lindstromc447fee2002-04-02 20:35:35 +0000786 printf("Last login: %s from %s\r\n", time_string,
787 s->hostname);
Damien Miller942da032000-08-18 13:59:06 +1000788 }
Tim Rice81ed5182002-09-25 17:38:46 -0700789#endif /* NO_SSH_LASTLOG */
Damien Millercf205e82001-04-16 18:29:15 +1000790
791 do_motd();
792}
793
794/*
795 * Display the message of the day.
796 */
797void
798do_motd(void)
799{
800 FILE *f;
801 char buf[256];
802
Damien Miller942da032000-08-18 13:59:06 +1000803 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000804#ifdef HAVE_LOGIN_CAP
805 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
806 "/etc/motd"), "r");
807#else
Damien Miller942da032000-08-18 13:59:06 +1000808 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000809#endif
Damien Miller942da032000-08-18 13:59:06 +1000810 if (f) {
811 while (fgets(buf, sizeof(buf), f))
812 fputs(buf, stdout);
813 fclose(f);
814 }
815 }
816}
817
Kevin Steves8f63caa2001-07-04 18:23:02 +0000818
819/*
820 * Check for quiet login, either .hushlogin or command given.
821 */
822int
823check_quietlogin(Session *s, const char *command)
824{
825 char buf[256];
826 struct passwd *pw = s->pw;
827 struct stat st;
828
829 /* Return 1 if .hushlogin exists or a command given. */
830 if (command != NULL)
831 return 1;
832 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
833#ifdef HAVE_LOGIN_CAP
834 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
835 return 1;
836#else
837 if (stat(buf, &st) >= 0)
838 return 1;
839#endif
840 return 0;
841}
842
Damien Millerb38eff82000-04-01 11:09:21 +1000843/*
844 * Sets the value of the given variable in the environment. If the variable
845 * already exists, its value is overriden.
846 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000847static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000848child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100849 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000850{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000851 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000852 char **env;
853
854 /*
855 * Find the slot where the value should be stored. If the variable
856 * already exists, we reuse the slot; otherwise we append a new slot
857 * at the end of the array, expanding if necessary.
858 */
859 env = *envp;
860 namelen = strlen(name);
861 for (i = 0; env[i]; i++)
862 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
863 break;
864 if (env[i]) {
865 /* Reuse the slot. */
866 xfree(env[i]);
867 } else {
868 /* New variable. Expand if necessary. */
869 if (i >= (*envsizep) - 1) {
Damien Millera0796ca2002-06-26 19:15:07 +1000870 if (*envsizep >= 1000)
871 fatal("child_set_env: too many env vars,"
872 " skipping: %.100s", name);
Damien Millerb38eff82000-04-01 11:09:21 +1000873 (*envsizep) += 50;
874 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
875 }
876 /* Need to set the NULL pointer at end of array beyond the new slot. */
877 env[i + 1] = NULL;
878 }
879
880 /* Allocate space and format the variable in the appropriate slot. */
881 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
882 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
883}
884
885/*
886 * Reads environment variables from the given file and adds/overrides them
887 * into the environment. If the file does not exist, this does nothing.
888 * Otherwise, it must consist of empty lines, comments (line starts with '#')
889 * and assignments of the form name=value. No other forms are allowed.
890 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000891static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000892read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100893 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000894{
895 FILE *f;
896 char buf[4096];
897 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000898 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000899
900 f = fopen(filename, "r");
901 if (!f)
902 return;
903
904 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000905 if (++lineno > 1000)
906 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000907 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
908 ;
909 if (!*cp || *cp == '#' || *cp == '\n')
910 continue;
911 if (strchr(cp, '\n'))
912 *strchr(cp, '\n') = '\0';
913 value = strchr(cp, '=');
914 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000915 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
916 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000917 continue;
918 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000919 /*
920 * Replace the equals sign by nul, and advance value to
921 * the value string.
922 */
Damien Millerb38eff82000-04-01 11:09:21 +1000923 *value = '\0';
924 value++;
925 child_set_env(env, envsize, cp, value);
926 }
927 fclose(f);
928}
929
Damien Millerbb9ffc12002-01-08 10:59:32 +1100930void copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000931{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100932 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000933 int i;
934
Damien Millerbb9ffc12002-01-08 10:59:32 +1100935 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000936 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000937
Damien Millerbb9ffc12002-01-08 10:59:32 +1100938 for(i = 0; source[i] != NULL; i++) {
939 var_name = xstrdup(source[i]);
940 if ((var_val = strstr(var_name, "=")) == NULL) {
941 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000942 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000943 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100944 *var_val++ = '\0';
945
946 debug3("Copy environment: %s=%s", var_name, var_val);
947 child_set_env(env, envsize, var_name, var_val);
948
949 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000950 }
951}
Damien Millercb5e44a2000-09-29 12:12:36 +1100952
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000953static char **
954do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000955{
Damien Millerb38eff82000-04-01 11:09:21 +1000956 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000957 u_int i, envsize;
Damien Miller00111382003-03-10 11:21:17 +1100958 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000959 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000960
Damien Millerb38eff82000-04-01 11:09:21 +1000961 /* Initialize the environment. */
962 envsize = 100;
963 env = xmalloc(envsize * sizeof(char *));
964 env[0] = NULL;
965
Damien Millerbac2d8a2000-09-05 16:13:06 +1100966#ifdef HAVE_CYGWIN
967 /*
968 * The Windows environment contains some setting which are
969 * important for a running system. They must not be dropped.
970 */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100971 copy_environment(environ, &env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100972#endif
973
Damien Millerb38eff82000-04-01 11:09:21 +1000974 if (!options.use_login) {
975 /* Set basic environment. */
976 child_set_env(&env, &envsize, "USER", pw->pw_name);
977 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +1100978#ifdef _AIX
979 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
980#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000981 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000982#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +0000983 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
984 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
985 else
986 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +1100987#else /* HAVE_LOGIN_CAP */
988# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +1100989 /*
990 * There's no standard path on Windows. The path contains
991 * important components pointing to the system directories,
992 * needed for loading shared libraries. So the path better
993 * remains intact here.
994 */
Damien Millera18bbd32002-05-13 10:48:57 +1000995# ifdef SUPERUSER_PATH
996 child_set_env(&env, &envsize, "PATH",
997 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
998# else
Damien Millerb38eff82000-04-01 11:09:21 +1000999 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millera18bbd32002-05-13 10:48:57 +10001000# endif /* SUPERUSER_PATH */
Damien Millercb5e44a2000-09-29 12:12:36 +11001001# endif /* HAVE_CYGWIN */
1002#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001003
1004 snprintf(buf, sizeof buf, "%.200s/%.50s",
1005 _PATH_MAILDIR, pw->pw_name);
1006 child_set_env(&env, &envsize, "MAIL", buf);
1007
1008 /* Normal systems set SHELL by default. */
1009 child_set_env(&env, &envsize, "SHELL", shell);
1010 }
1011 if (getenv("TZ"))
1012 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1013
1014 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001015 if (!options.use_login) {
1016 while (custom_environment) {
1017 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001018 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001019
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001020 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001021 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001022 if (str[i] == '=') {
1023 str[i] = 0;
1024 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001025 }
1026 custom_environment = ce->next;
1027 xfree(ce->s);
1028 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001029 }
Damien Millerb38eff82000-04-01 11:09:21 +10001030 }
1031
Damien Millerf37e2462002-09-19 11:47:55 +10001032 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001033 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001034 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001035 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1036
Damien Miller00111382003-03-10 11:21:17 +11001037 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001038 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001039 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1040 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001041 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1042
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001043 if (s->ttyfd != -1)
1044 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1045 if (s->term)
1046 child_set_env(&env, &envsize, "TERM", s->term);
1047 if (s->display)
1048 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001049 if (original_command)
1050 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1051 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001052
Tim Rice81ed5182002-09-25 17:38:46 -07001053#ifdef _UNICOS
1054 if (cray_tmpdir[0] != '\0')
1055 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1056#endif /* _UNICOS */
1057
Damien Millerb38eff82000-04-01 11:09:21 +10001058#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001059 {
1060 char *cp;
1061
1062 if ((cp = getenv("AUTHSTATE")) != NULL)
1063 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1064 if ((cp = getenv("KRB5CCNAME")) != NULL)
1065 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1066 read_environment_file(&env, &envsize, "/etc/environment");
1067 }
Damien Millerb38eff82000-04-01 11:09:21 +10001068#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001069#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +00001070 if (s->authctxt->krb4_ticket_file)
1071 child_set_env(&env, &envsize, "KRBTKFILE",
1072 s->authctxt->krb4_ticket_file);
1073#endif
1074#ifdef KRB5
1075 if (s->authctxt->krb5_ticket_file)
1076 child_set_env(&env, &envsize, "KRB5CCNAME",
1077 s->authctxt->krb5_ticket_file);
1078#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001079#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001080 /*
1081 * Pull in any environment variables that may have
1082 * been set by PAM.
1083 */
Damien Miller4e448a32003-05-14 15:11:48 +10001084 if (options.use_pam) {
1085 char **p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001086
Kevin Steves38b050a2002-07-23 00:44:07 +00001087 copy_environment(p, &env, &envsize);
1088 free_pam_environment(p);
1089 }
Damien Millerb38eff82000-04-01 11:09:21 +10001090#endif /* USE_PAM */
1091
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001092 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001093 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001094 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001095
1096 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001097 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001098 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001099 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001100 read_environment_file(&env, &envsize, buf);
1101 }
1102 if (debug_flag) {
1103 /* dump the environment */
1104 fprintf(stderr, "Environment:\n");
1105 for (i = 0; env[i]; i++)
1106 fprintf(stderr, " %.200s\n", env[i]);
1107 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001108 return env;
1109}
1110
1111/*
1112 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1113 * first in this order).
1114 */
1115static void
1116do_rc_files(Session *s, const char *shell)
1117{
1118 FILE *f = NULL;
1119 char cmd[1024];
1120 int do_xauth;
1121 struct stat st;
1122
1123 do_xauth =
1124 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1125
1126 /* ignore _PATH_SSH_USER_RC for subsystems */
1127 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1128 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1129 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1130 if (debug_flag)
1131 fprintf(stderr, "Running %s\n", cmd);
1132 f = popen(cmd, "w");
1133 if (f) {
1134 if (do_xauth)
1135 fprintf(f, "%s %s\n", s->auth_proto,
1136 s->auth_data);
1137 pclose(f);
1138 } else
1139 fprintf(stderr, "Could not run %s\n",
1140 _PATH_SSH_USER_RC);
1141 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1142 if (debug_flag)
1143 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1144 _PATH_SSH_SYSTEM_RC);
1145 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1146 if (f) {
1147 if (do_xauth)
1148 fprintf(f, "%s %s\n", s->auth_proto,
1149 s->auth_data);
1150 pclose(f);
1151 } else
1152 fprintf(stderr, "Could not run %s\n",
1153 _PATH_SSH_SYSTEM_RC);
1154 } else if (do_xauth && options.xauth_location != NULL) {
1155 /* Add authority data to .Xauthority if appropriate. */
1156 if (debug_flag) {
1157 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001158 "Running %.500s remove %.100s\n",
1159 options.xauth_location, s->auth_display);
1160 fprintf(stderr,
1161 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001162 options.xauth_location, s->auth_display,
1163 s->auth_proto, s->auth_data);
1164 }
1165 snprintf(cmd, sizeof cmd, "%s -q -",
1166 options.xauth_location);
1167 f = popen(cmd, "w");
1168 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001169 fprintf(f, "remove %s\n",
1170 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001171 fprintf(f, "add %s %s %s\n",
1172 s->auth_display, s->auth_proto,
1173 s->auth_data);
1174 pclose(f);
1175 } else {
1176 fprintf(stderr, "Could not run %s\n",
1177 cmd);
1178 }
1179 }
1180}
1181
1182static void
1183do_nologin(struct passwd *pw)
1184{
1185 FILE *f = NULL;
1186 char buf[1024];
1187
1188#ifdef HAVE_LOGIN_CAP
1189 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1190 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1191 _PATH_NOLOGIN), "r");
1192#else
1193 if (pw->pw_uid)
1194 f = fopen(_PATH_NOLOGIN, "r");
1195#endif
1196 if (f) {
1197 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001198 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001199 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001200 while (fgets(buf, sizeof(buf), f))
1201 fputs(buf, stderr);
1202 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001203 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001204 exit(254);
1205 }
1206}
1207
1208/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001209void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001210do_setusercontext(struct passwd *pw)
1211{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001212#ifndef HAVE_CYGWIN
1213 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001214#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001215 {
1216
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001217#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001218 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1219 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001220#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001221#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001222# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001223 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001224# endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001225 if (setusercontext(lc, pw, pw->pw_uid,
1226 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1227 perror("unable to set user context");
1228 exit(1);
1229 }
1230#else
1231# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1232 /* Sets login uid for accounting */
1233 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1234 error("setluid: %s", strerror(errno));
1235# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1236
1237 if (setlogin(pw->pw_name) < 0)
1238 error("setlogin failed: %s", strerror(errno));
1239 if (setgid(pw->pw_gid) < 0) {
1240 perror("setgid");
1241 exit(1);
1242 }
1243 /* Initialize the group list. */
1244 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1245 perror("initgroups");
1246 exit(1);
1247 }
1248 endgrent();
1249# ifdef USE_PAM
1250 /*
1251 * PAM credentials may take the form of supplementary groups.
1252 * These will have been wiped by the above initgroups() call.
1253 * Reestablish them here.
1254 */
Damien Miller4e448a32003-05-14 15:11:48 +10001255 if (options.use_pam)
1256 do_pam_setcred(0);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001257# endif /* USE_PAM */
1258# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1259 irix_setusercontext(pw);
1260# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001261# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001262 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001263# endif /* _AIX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001264 /* Permanently switch to the desired uid. */
1265 permanently_set_uid(pw);
1266#endif
1267 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001268
1269#ifdef HAVE_CYGWIN
1270 if (is_winnt)
1271#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001272 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1273 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1274}
1275
Ben Lindstrom08105192002-03-22 02:50:06 +00001276static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277launch_login(struct passwd *pw, const char *hostname)
1278{
1279 /* Launch login(1). */
1280
Ben Lindstrom378a4172002-06-07 14:49:56 +00001281 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001282#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001283 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001285#ifdef LOGIN_NO_ENDOPT
1286 "-p", "-f", pw->pw_name, (char *)NULL);
1287#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001289#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290
1291 /* Login couldn't be executed, die. */
1292
1293 perror("login");
1294 exit(1);
1295}
1296
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001297/*
1298 * Performs common processing for the child, such as setting up the
1299 * environment, closing extra file descriptors, setting the user and group
1300 * ids, and executing the command or shell.
1301 */
1302void
1303do_child(Session *s, const char *command)
1304{
1305 extern char **environ;
1306 char **env;
1307 char *argv[10];
1308 const char *shell, *shell0, *hostname = NULL;
1309 struct passwd *pw = s->pw;
1310 u_int i;
1311
1312 /* remove hostkey from the child's memory */
1313 destroy_sensitive_data();
1314
1315 /* login(1) is only called if we execute the login shell */
1316 if (options.use_login && command != NULL)
1317 options.use_login = 0;
1318
Tim Rice81ed5182002-09-25 17:38:46 -07001319#ifdef _UNICOS
1320 cray_setup(pw->pw_uid, pw->pw_name, command);
1321#endif /* _UNICOS */
1322
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001323 /*
1324 * Login(1) does this as well, and it needs uid 0 for the "-h"
1325 * switch, so we let login(1) to this for us.
1326 */
1327 if (!options.use_login) {
1328#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001329 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001330 if (!check_quietlogin(s, command))
1331 do_motd();
1332#else /* HAVE_OSF_SIA */
1333 do_nologin(pw);
1334 do_setusercontext(pw);
1335#endif /* HAVE_OSF_SIA */
1336 }
1337
1338 /*
1339 * Get the shell from the password data. An empty shell field is
1340 * legal, and means /bin/sh.
1341 */
1342 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001343
1344 /*
1345 * Make sure $SHELL points to the shell from the password file,
1346 * even if shell is overridden from login.conf
1347 */
1348 env = do_setup_env(s, shell);
1349
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001350#ifdef HAVE_LOGIN_CAP
1351 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1352#endif
1353
Damien Millerad833b32000-08-23 10:46:23 +10001354 /* we have to stash the hostname before we close our socket. */
1355 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001356 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001357 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001358 /*
1359 * Close the connection descriptors; note that this is the child, and
1360 * the server will still have the socket open, and it is important
1361 * that we do not shutdown it. Note that the descriptors cannot be
1362 * closed before building the environment, as we call
1363 * get_remote_ipaddr there.
1364 */
1365 if (packet_get_connection_in() == packet_get_connection_out())
1366 close(packet_get_connection_in());
1367 else {
1368 close(packet_get_connection_in());
1369 close(packet_get_connection_out());
1370 }
1371 /*
1372 * Close all descriptors related to channels. They will still remain
1373 * open in the parent.
1374 */
1375 /* XXX better use close-on-exec? -markus */
1376 channel_close_all();
1377
1378 /*
1379 * Close any extra file descriptors. Note that there may still be
1380 * descriptors left by system functions. They will be closed later.
1381 */
1382 endpwent();
1383
1384 /*
1385 * Close any extra open file descriptors so that we don\'t have them
1386 * hanging around in clients. Note that we want to do this after
1387 * initgroups, because at least on Solaris 2.3 it leaves file
1388 * descriptors open.
1389 */
1390 for (i = 3; i < 64; i++)
1391 close(i);
1392
Damien Millerb38eff82000-04-01 11:09:21 +10001393 /*
Damien Miller05eda432002-02-10 18:32:28 +11001394 * Must take new environment into use so that .ssh/rc,
1395 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001396 */
1397 environ = env;
1398
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001399#ifdef AFS
1400 /* Try to get AFS tokens for the local cell. */
1401 if (k_hasafs()) {
1402 char cell[64];
Damien Miller9f0f5c62001-12-21 14:45:46 +11001403
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001404 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1405 krb_afslog(cell, 0);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001406
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001407 krb_afslog(0, 0);
1408 }
1409#endif /* AFS */
1410
Damien Miller139d4cd2001-10-10 15:07:44 +10001411 /* Change current directory to the user\'s home directory. */
1412 if (chdir(pw->pw_dir) < 0) {
1413 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1414 pw->pw_dir, strerror(errno));
1415#ifdef HAVE_LOGIN_CAP
1416 if (login_getcapbool(lc, "requirehome", 0))
1417 exit(1);
1418#endif
1419 }
1420
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001421 if (!options.use_login)
1422 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001423
1424 /* restore SIGPIPE for child */
1425 signal(SIGPIPE, SIG_DFL);
1426
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001427 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428 launch_login(pw, hostname);
1429 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001430 }
1431
1432 /* Get the last component of the shell name. */
1433 if ((shell0 = strrchr(shell, '/')) != NULL)
1434 shell0++;
1435 else
1436 shell0 = shell;
1437
Damien Millerb38eff82000-04-01 11:09:21 +10001438 /*
1439 * If we have no command, execute the shell. In this case, the shell
1440 * name to be passed in argv[0] is preceded by '-' to indicate that
1441 * this is a login shell.
1442 */
1443 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001444 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001445
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001446 /* Start the shell. Set initial character to '-'. */
1447 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001448
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001449 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1450 >= sizeof(argv0) - 1) {
1451 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001452 perror(shell);
1453 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001454 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001455
1456 /* Execute the shell. */
1457 argv[0] = argv0;
1458 argv[1] = NULL;
1459 execve(shell, argv, env);
1460
1461 /* Executing the shell failed. */
1462 perror(shell);
1463 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001464 }
1465 /*
1466 * Execute the command using the user's shell. This uses the -c
1467 * option to execute the command.
1468 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001469 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001470 argv[1] = "-c";
1471 argv[2] = (char *) command;
1472 argv[3] = NULL;
1473 execve(shell, argv, env);
1474 perror(shell);
1475 exit(1);
1476}
1477
1478Session *
1479session_new(void)
1480{
1481 int i;
1482 static int did_init = 0;
1483 if (!did_init) {
1484 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001485 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001486 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001487 }
1488 did_init = 1;
1489 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001490 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001491 Session *s = &sessions[i];
1492 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001493 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001494 s->chanid = -1;
1495 s->ptyfd = -1;
1496 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001497 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001498 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001499 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001500 return s;
1501 }
1502 }
1503 return NULL;
1504}
1505
Ben Lindstrombba81212001-06-25 05:01:22 +00001506static void
Damien Millerb38eff82000-04-01 11:09:21 +10001507session_dump(void)
1508{
1509 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001510 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001511 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001512 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001513 s->used,
1514 s->self,
1515 s,
1516 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001517 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001518 }
1519}
1520
Damien Millerefb4afe2000-04-12 18:45:05 +10001521int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001522session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001523{
1524 Session *s = session_new();
1525 debug("session_open: channel %d", chanid);
1526 if (s == NULL) {
1527 error("no more sessions");
1528 return 0;
1529 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001530 s->authctxt = authctxt;
1531 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001532 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001533 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001534 debug("session_open: session %d: link with channel %d", s->self, chanid);
1535 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001536 return 1;
1537}
1538
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539Session *
1540session_by_tty(char *tty)
1541{
1542 int i;
1543 for (i = 0; i < MAX_SESSIONS; i++) {
1544 Session *s = &sessions[i];
1545 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1546 debug("session_by_tty: session %d tty %s", i, tty);
1547 return s;
1548 }
1549 }
1550 debug("session_by_tty: unknown tty %.100s", tty);
1551 session_dump();
1552 return NULL;
1553}
1554
Ben Lindstrombba81212001-06-25 05:01:22 +00001555static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001556session_by_channel(int id)
1557{
1558 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001559 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001560 Session *s = &sessions[i];
1561 if (s->used && s->chanid == id) {
1562 debug("session_by_channel: session %d channel %d", i, id);
1563 return s;
1564 }
1565 }
1566 debug("session_by_channel: unknown channel %d", id);
1567 session_dump();
1568 return NULL;
1569}
1570
Ben Lindstrombba81212001-06-25 05:01:22 +00001571static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001572session_by_pid(pid_t pid)
1573{
1574 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001575 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001576 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001577 Session *s = &sessions[i];
1578 if (s->used && s->pid == pid)
1579 return s;
1580 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001581 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001582 session_dump();
1583 return NULL;
1584}
1585
Ben Lindstrombba81212001-06-25 05:01:22 +00001586static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001587session_window_change_req(Session *s)
1588{
1589 s->col = packet_get_int();
1590 s->row = packet_get_int();
1591 s->xpixel = packet_get_int();
1592 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001593 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001594 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1595 return 1;
1596}
1597
Ben Lindstrombba81212001-06-25 05:01:22 +00001598static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001599session_pty_req(Session *s)
1600{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001601 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001602 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001603
Ben Lindstrom49c12602001-06-13 04:37:36 +00001604 if (no_pty_flag) {
1605 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001606 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001607 }
1608 if (s->ttyfd != -1) {
1609 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001610 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001611 }
Ben Lindstromc447fee2002-04-02 20:35:35 +00001612 /* Get the time and hostname when the user last logged in. */
1613 if (options.print_lastlog) {
1614 s->hostname[0] = '\0';
1615 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1616 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1617 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001618
Damien Millerefb4afe2000-04-12 18:45:05 +10001619 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001620
1621 if (compat20) {
1622 s->col = packet_get_int();
1623 s->row = packet_get_int();
1624 } else {
1625 s->row = packet_get_int();
1626 s->col = packet_get_int();
1627 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001628 s->xpixel = packet_get_int();
1629 s->ypixel = packet_get_int();
1630
1631 if (strcmp(s->term, "") == 0) {
1632 xfree(s->term);
1633 s->term = NULL;
1634 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001635
Damien Millerefb4afe2000-04-12 18:45:05 +10001636 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001637 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001639 if (s->term)
1640 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001641 s->term = NULL;
1642 s->ptyfd = -1;
1643 s->ttyfd = -1;
1644 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001645 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001646 }
1647 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001648
1649 /* for SSH1 the tty modes length is not given */
1650 if (!compat20)
1651 n_bytes = packet_remaining();
1652 tty_parse_modes(s->ttyfd, &n_bytes);
1653
Damien Millerefb4afe2000-04-12 18:45:05 +10001654 /*
1655 * Add a cleanup function to clear the utmp entry and record logout
1656 * time in case we call fatal() (e.g., the connection gets closed).
1657 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001658 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001659 if (!use_privsep)
1660 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001661
1662 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001663 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1664
Damien Miller48b03fc2002-01-22 23:11:40 +11001665 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001666 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001667 return 1;
1668}
1669
Ben Lindstrombba81212001-06-25 05:01:22 +00001670static int
Damien Millerbd483e72000-04-30 10:00:53 +10001671session_subsystem_req(Session *s)
1672{
Damien Millerae452462001-10-10 15:08:06 +10001673 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001674 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001675 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001676 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001677 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001678
Damien Miller48b03fc2002-01-22 23:11:40 +11001679 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001680 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001681
Damien Millerf6d9e222000-06-18 14:50:44 +10001682 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001683 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1684 cmd = options.subsystem_command[i];
1685 if (stat(cmd, &st) < 0) {
1686 error("subsystem: cannot stat %s: %s", cmd,
1687 strerror(errno));
1688 break;
1689 }
1690 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001691 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001692 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001693 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001694 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001695 }
1696 }
1697
1698 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10001699 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001700 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001701
Damien Millerbd483e72000-04-30 10:00:53 +10001702 xfree(subsys);
1703 return success;
1704}
1705
Ben Lindstrombba81212001-06-25 05:01:22 +00001706static int
Damien Millerbd483e72000-04-30 10:00:53 +10001707session_x11_req(Session *s)
1708{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001709 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001710
1711 s->single_connection = packet_get_char();
1712 s->auth_proto = packet_get_string(NULL);
1713 s->auth_data = packet_get_string(NULL);
1714 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001715 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001716
Ben Lindstrom768176b2001-06-09 01:29:12 +00001717 success = session_setup_x11fwd(s);
1718 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001719 xfree(s->auth_proto);
1720 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001721 s->auth_proto = NULL;
1722 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001723 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001724 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001725}
1726
Ben Lindstrombba81212001-06-25 05:01:22 +00001727static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001728session_shell_req(Session *s)
1729{
Damien Miller48b03fc2002-01-22 23:11:40 +11001730 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001731 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001732 return 1;
1733}
1734
Ben Lindstrombba81212001-06-25 05:01:22 +00001735static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001736session_exec_req(Session *s)
1737{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001738 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001739 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001740 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001741 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001742 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001743 return 1;
1744}
1745
Ben Lindstrombba81212001-06-25 05:01:22 +00001746static int
Damien Miller54c45982003-05-15 10:20:13 +10001747session_break_req(Session *s)
1748{
1749 u_int break_length;
1750
1751 break_length = packet_get_int();
1752 packet_check_eom();
1753
1754 if (s->ttyfd == -1)
1755 return 0;
1756 /* we will sleep from 500ms to 3000ms */
1757 break_length = MIN(break_length, 3000);
1758 break_length = MAX(break_length, 500);
1759 ioctl(s->ttyfd, TIOCSBRK, NULL);
1760 /* should we care about EINTR? */
1761 usleep(break_length * 1000);
1762 ioctl(s->ttyfd, TIOCCBRK, NULL);
1763 return 1;
1764}
1765
1766static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001767session_auth_agent_req(Session *s)
1768{
1769 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001770 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001771 if (no_agent_forwarding_flag) {
1772 debug("session_auth_agent_req: no_agent_forwarding_flag");
1773 return 0;
1774 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001775 if (called) {
1776 return 0;
1777 } else {
1778 called = 1;
1779 return auth_input_request_forwarding(s->pw);
1780 }
1781}
1782
Damien Millerc7ef63d2002-02-05 12:21:42 +11001783int
1784session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001785{
Damien Millerefb4afe2000-04-12 18:45:05 +10001786 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001787 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001788
Damien Millerc7ef63d2002-02-05 12:21:42 +11001789 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10001790 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11001791 c->self, rtype);
1792 return 0;
1793 }
1794 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001795
1796 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001797 * a session is in LARVAL state until a shell, a command
1798 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001799 */
1800 if (c->type == SSH_CHANNEL_LARVAL) {
1801 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001802 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001803 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001804 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001805 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001806 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001807 } else if (strcmp(rtype, "x11-req") == 0) {
1808 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001809 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1810 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001811 } else if (strcmp(rtype, "subsystem") == 0) {
1812 success = session_subsystem_req(s);
Damien Miller54c45982003-05-15 10:20:13 +10001813 } else if (strcmp(rtype, "break") == 0) {
1814 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001815 }
1816 }
1817 if (strcmp(rtype, "window-change") == 0) {
1818 success = session_window_change_req(s);
1819 }
Damien Millerc7ef63d2002-02-05 12:21:42 +11001820 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10001821}
1822
1823void
1824session_set_fds(Session *s, int fdin, int fdout, int fderr)
1825{
1826 if (!compat20)
1827 fatal("session_set_fds: called for proto != 2.0");
1828 /*
1829 * now that have a child and a pipe to the child,
1830 * we can activate our channel and register the fd's
1831 */
1832 if (s->chanid == -1)
1833 fatal("no channel for session %d", s->self);
1834 channel_set_fds(s->chanid,
1835 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001836 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11001837 1,
1838 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10001839}
1840
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001841/*
1842 * Function to perform pty cleanup. Also called if we get aborted abnormally
1843 * (e.g., due to a dropped connection).
1844 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001845void
1846session_pty_cleanup2(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001847{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001848 Session *s = session;
1849
1850 if (s == NULL) {
1851 error("session_pty_cleanup: no session");
1852 return;
1853 }
1854 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001855 return;
1856
Kevin Steves43cdef32001-02-11 14:12:08 +00001857 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001858
Damien Millerb38eff82000-04-01 11:09:21 +10001859 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001860 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08001861 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001862
1863 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001864 if (getuid() == 0)
1865 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001866
1867 /*
1868 * Close the server side of the socket pairs. We must do this after
1869 * the pty cleanup, so that another process doesn't get this pty
1870 * while we're still cleaning up.
1871 */
1872 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001873 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001874
1875 /* unlink pty from session */
1876 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001877}
Damien Millerefb4afe2000-04-12 18:45:05 +10001878
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001879void
1880session_pty_cleanup(void *session)
1881{
1882 PRIVSEP(session_pty_cleanup2(session));
1883}
1884
Damien Miller5a80bba2002-09-04 16:39:02 +10001885static char *
1886sig2name(int sig)
1887{
1888#define SSH_SIG(x) if (sig == SIG ## x) return #x
1889 SSH_SIG(ABRT);
1890 SSH_SIG(ALRM);
1891 SSH_SIG(FPE);
1892 SSH_SIG(HUP);
1893 SSH_SIG(ILL);
1894 SSH_SIG(INT);
1895 SSH_SIG(KILL);
1896 SSH_SIG(PIPE);
1897 SSH_SIG(QUIT);
1898 SSH_SIG(SEGV);
1899 SSH_SIG(TERM);
1900 SSH_SIG(USR1);
1901 SSH_SIG(USR2);
1902#undef SSH_SIG
1903 return "SIG@openssh.com";
1904}
1905
Ben Lindstrombba81212001-06-25 05:01:22 +00001906static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001907session_exit_message(Session *s, int status)
1908{
1909 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001910
1911 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001912 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001913 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001914 debug("session_exit_message: session %d channel %d pid %ld",
1915 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001916
1917 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001918 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001919 packet_put_int(WEXITSTATUS(status));
1920 packet_send();
1921 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001922 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10001923 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001924#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001925 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001926#else /* WCOREDUMP */
1927 packet_put_char(0);
1928#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001929 packet_put_cstring("");
1930 packet_put_cstring("");
1931 packet_send();
1932 } else {
1933 /* Some weird exit cause. Just exit. */
1934 packet_disconnect("wait returned status %04x.", status);
1935 }
1936
1937 /* disconnect channel */
1938 debug("session_exit_message: release channel %d", s->chanid);
1939 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001940 /*
1941 * emulate a write failure with 'chan_write_failed', nobody will be
1942 * interested in data we write.
1943 * Note that we must not call 'chan_read_failed', since there could
1944 * be some more data waiting in the pipe.
1945 */
Damien Millerbd483e72000-04-30 10:00:53 +10001946 if (c->ostate != CHAN_OUTPUT_CLOSED)
1947 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001948 s->chanid = -1;
1949}
1950
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001951void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001952session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001953{
Ben Lindstromce0f6342002-06-11 16:42:49 +00001954 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001955 if (s->ttyfd != -1) {
1956 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1957 session_pty_cleanup(s);
1958 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001959 if (s->term)
1960 xfree(s->term);
1961 if (s->display)
1962 xfree(s->display);
Damien Miller512bccb2002-02-05 12:11:02 +11001963 if (s->auth_display)
1964 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10001965 if (s->auth_data)
1966 xfree(s->auth_data);
1967 if (s->auth_proto)
1968 xfree(s->auth_proto);
1969 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001970 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001971}
1972
1973void
1974session_close_by_pid(pid_t pid, int status)
1975{
1976 Session *s = session_by_pid(pid);
1977 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001978 debug("session_close_by_pid: no session for pid %ld",
1979 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001980 return;
1981 }
1982 if (s->chanid != -1)
1983 session_exit_message(s, status);
1984 session_close(s);
1985}
1986
1987/*
1988 * this is called when a channel dies before
1989 * the session 'child' itself dies
1990 */
1991void
1992session_close_by_channel(int id, void *arg)
1993{
1994 Session *s = session_by_channel(id);
1995 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001996 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001997 return;
1998 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001999 debug("session_close_by_channel: channel %d child %ld",
2000 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002001 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002002 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002003 /*
2004 * delay detach of session, but release pty, since
2005 * the fd's to the child are already closed
2006 */
2007 if (s->ttyfd != -1) {
2008 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
2009 session_pty_cleanup(s);
2010 }
Damien Miller3ec27592001-10-12 11:35:04 +10002011 return;
2012 }
2013 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002014 channel_cancel_cleanup(s->chanid);
2015 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002016 session_close(s);
2017}
2018
2019void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002020session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002021{
2022 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002023 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002024 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002025 if (s->used) {
2026 if (closefunc != NULL)
2027 closefunc(s);
2028 else
2029 session_close(s);
2030 }
Damien Miller52b77be2001-10-10 15:14:37 +10002031 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002032}
2033
Ben Lindstrombba81212001-06-25 05:01:22 +00002034static char *
Damien Millere247cc42000-05-07 12:03:14 +10002035session_tty_list(void)
2036{
2037 static char buf[1024];
2038 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002039 char *cp;
2040
Damien Millere247cc42000-05-07 12:03:14 +10002041 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002042 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002043 Session *s = &sessions[i];
2044 if (s->used && s->ttyfd != -1) {
Damien Millera8ed44b2003-01-10 09:53:12 +11002045
2046 if (strncmp(s->tty, "/dev/", 5) != 0) {
2047 cp = strrchr(s->tty, '/');
2048 cp = (cp == NULL) ? s->tty : cp + 1;
2049 } else
2050 cp = s->tty + 5;
2051
Damien Millere247cc42000-05-07 12:03:14 +10002052 if (buf[0] != '\0')
2053 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002054 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002055 }
2056 }
2057 if (buf[0] == '\0')
2058 strlcpy(buf, "notty", sizeof buf);
2059 return buf;
2060}
2061
2062void
2063session_proctitle(Session *s)
2064{
2065 if (s->pw == NULL)
2066 error("no user for session %d", s->self);
2067 else
2068 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2069}
2070
Ben Lindstrom768176b2001-06-09 01:29:12 +00002071int
2072session_setup_x11fwd(Session *s)
2073{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002074 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002075 char display[512], auth_display[512];
2076 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00002077
2078 if (no_x11_forwarding_flag) {
2079 packet_send_debug("X11 forwarding disabled in user configuration file.");
2080 return 0;
2081 }
2082 if (!options.x11_forwarding) {
2083 debug("X11 forwarding disabled in server configuration file.");
2084 return 0;
2085 }
2086 if (!options.xauth_location ||
2087 (stat(options.xauth_location, &st) == -1)) {
2088 packet_send_debug("No xauth program; cannot forward with spoofing.");
2089 return 0;
2090 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002091 if (options.use_login) {
2092 packet_send_debug("X11 forwarding disabled; "
2093 "not compatible with UseLogin=yes.");
2094 return 0;
2095 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002096 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002097 debug("X11 display already set.");
2098 return 0;
2099 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002100 if (x11_create_display_inet(options.x11_display_offset,
2101 options.x11_use_localhost, s->single_connection,
2102 &s->display_number) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002103 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002104 return 0;
2105 }
Kevin Steves366298c2001-12-19 17:58:01 +00002106
2107 /* Set up a suitable value for the DISPLAY variable. */
2108 if (gethostname(hostname, sizeof(hostname)) < 0)
2109 fatal("gethostname: %.100s", strerror(errno));
2110 /*
2111 * auth_display must be used as the displayname when the
2112 * authorization entry is added with xauth(1). This will be
2113 * different than the DISPLAY string for localhost displays.
2114 */
Damien Miller95c249f2002-02-05 12:11:34 +11002115 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002116 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002117 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002118 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002119 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002120 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002121 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002122 } else {
2123#ifdef IPADDR_IN_DISPLAY
2124 struct hostent *he;
2125 struct in_addr my_addr;
2126
2127 he = gethostbyname(hostname);
2128 if (he == NULL) {
2129 error("Can't get IP address for X11 DISPLAY.");
2130 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2131 return 0;
2132 }
2133 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002134 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002135 s->display_number, s->screen);
2136#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002137 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002138 s->display_number, s->screen);
2139#endif
2140 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002141 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002142 }
2143
Ben Lindstrom768176b2001-06-09 01:29:12 +00002144 return 1;
2145}
2146
Ben Lindstrombba81212001-06-25 05:01:22 +00002147static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002148do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002149{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002150 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002151}