blob: 63b7da1f0f886f8234f0dfcdda5fcd0a78e13cac [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 Millera8e06ce2003-11-21 23:48:55 +110036RCSID("$OpenBSD: session.c,v 1.168 2003/11/21 11:57:03 djm Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000042#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "packet.h"
44#include "buffer.h"
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 Tucker0efd1552003-08-26 11:49:55 +100061#ifdef GSSAPI
62#include "ssh-gss.h"
63#endif
64
Damien Millerb38eff82000-04-01 11:09:21 +100065/* func */
66
67Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000068void session_set_fds(Session *, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +100069void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000070void session_proctitle(Session *);
71int session_setup_x11fwd(Session *);
72void do_exec_pty(Session *, const char *);
73void do_exec_no_pty(Session *, const char *);
74void do_exec(Session *, const char *);
75void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +000076#ifdef LOGIN_NEEDS_UTMPX
77static void do_pre_login(Session *s);
78#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +000079void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +100080void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000081int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +100082
Ben Lindstrombba81212001-06-25 05:01:22 +000083static void do_authenticated1(Authctxt *);
84static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000085
Ben Lindstrombba81212001-06-25 05:01:22 +000086static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +000087
Damien Millerb38eff82000-04-01 11:09:21 +100088/* import */
89extern ServerOptions options;
90extern char *__progname;
91extern int log_stderr;
92extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +000093extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +100094extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +000095extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +100096extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +100097
Damien Miller7b28dc52000-09-05 13:34:53 +110098/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +000099const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100100
Damien Millerb38eff82000-04-01 11:09:21 +1000101/* data */
102#define MAX_SESSIONS 10
103Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100104
Damien Millerad833b32000-08-23 10:46:23 +1000105#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000106login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000107#endif
108
Darren Tucker3e33cec2003-10-02 16:12:36 +1000109static int is_child = 0;
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
Darren Tucker3e33cec2003-10-02 16:12:36 +1000118auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000119{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000120 if (auth_sock_name != NULL) {
121 temporarily_use_uid(pw);
122 unlink(auth_sock_name);
123 rmdir(auth_sock_dir);
124 auth_sock_name = NULL;
125 restore_uid();
126 }
127}
128
129static int
130auth_input_request_forwarding(struct passwd * pw)
131{
132 Channel *nc;
133 int sock;
134 struct sockaddr_un sunaddr;
135
136 if (auth_sock_name != NULL) {
137 error("authentication forwarding requested twice.");
138 return 0;
139 }
140
141 /* Temporarily drop privileged uid for mkdir/bind. */
142 temporarily_use_uid(pw);
143
144 /* Allocate a buffer for the socket name, and format the name. */
145 auth_sock_name = xmalloc(MAXPATHLEN);
146 auth_sock_dir = xmalloc(MAXPATHLEN);
Darren Tucker072a7b12003-10-15 16:10:25 +1000147 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000148
149 /* Create private directory for socket */
150 if (mkdtemp(auth_sock_dir) == NULL) {
151 packet_send_debug("Agent forwarding disabled: "
152 "mkdtemp() failed: %.100s", strerror(errno));
153 restore_uid();
154 xfree(auth_sock_name);
155 xfree(auth_sock_dir);
156 auth_sock_name = NULL;
157 auth_sock_dir = NULL;
158 return 0;
159 }
Ben Lindstromce0f6342002-06-11 16:42:49 +0000160 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
161 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000162
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000163 /* Create the socket. */
164 sock = socket(AF_UNIX, SOCK_STREAM, 0);
165 if (sock < 0)
166 packet_disconnect("socket: %.100s", strerror(errno));
167
168 /* Bind it to the name. */
169 memset(&sunaddr, 0, sizeof(sunaddr));
170 sunaddr.sun_family = AF_UNIX;
171 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
172
173 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
174 packet_disconnect("bind: %.100s", strerror(errno));
175
176 /* Restore the privileged uid. */
177 restore_uid();
178
179 /* Start listening on the socket. */
180 if (listen(sock, 5) < 0)
181 packet_disconnect("listen: %.100s", strerror(errno));
182
183 /* Allocate a channel for the authentication agent socket. */
184 nc = channel_new("auth socket",
185 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
186 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000187 0, "auth socket", 1);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000188 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
189 return 1;
190}
191
192
Ben Lindstromb31783d2001-03-22 02:02:12 +0000193void
194do_authenticated(Authctxt *authctxt)
195{
Damien Miller97f39ae2003-02-24 11:57:01 +1100196 setproctitle("%s", authctxt->pw->pw_name);
197
Ben Lindstromb31783d2001-03-22 02:02:12 +0000198 /*
199 * Cancel the alarm we set to limit the time taken for
200 * authentication.
201 */
202 alarm(0);
203 if (startup_pipe != -1) {
204 close(startup_pipe);
205 startup_pipe = -1;
206 }
Ben Lindstromb31783d2001-03-22 02:02:12 +0000207 /* setup the channel layer */
208 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
209 channel_permit_all_opens();
210
211 if (compat20)
212 do_authenticated2(authctxt);
213 else
214 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000215
Darren Tucker3e33cec2003-10-02 16:12:36 +1000216 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000217}
218
Damien Millerb38eff82000-04-01 11:09:21 +1000219/*
Damien Millerb38eff82000-04-01 11:09:21 +1000220 * Prepares for an interactive session. This is called after the user has
221 * been successfully authenticated. During this message exchange, pseudo
222 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
223 * are requested, etc.
224 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000226do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000227{
228 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000229 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100230 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000231 int enable_compression_after_reply = 0;
232 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000233
234 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000235 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000236 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000237
Damien Millerb38eff82000-04-01 11:09:21 +1000238 /*
239 * We stay in this loop until the client requests to execute a shell
240 * or a command.
241 */
242 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000243 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000244
245 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100246 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000247
248 /* Process the packet. */
249 switch (type) {
250 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000251 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100252 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000253 if (compression_level < 1 || compression_level > 9) {
254 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100255 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000256 break;
257 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000258 if (!options.compression) {
259 debug2("compression disabled");
260 break;
261 }
Damien Millerb38eff82000-04-01 11:09:21 +1000262 /* Enable compression after we have responded with SUCCESS. */
263 enable_compression_after_reply = 1;
264 success = 1;
265 break;
266
267 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000268 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000269 break;
270
271 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000272 s->auth_proto = packet_get_string(&proto_len);
273 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000274
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000275 screen_flag = packet_get_protocol_flags() &
276 SSH_PROTOFLAG_SCREEN_NUMBER;
277 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
278
279 if (packet_remaining() == 4) {
280 if (!screen_flag)
281 debug2("Buggy client: "
282 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000283 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000284 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000285 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000286 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100287 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000288 success = session_setup_x11fwd(s);
289 if (!success) {
290 xfree(s->auth_proto);
291 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000292 s->auth_proto = NULL;
293 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000294 }
Damien Millerb38eff82000-04-01 11:09:21 +1000295 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000296
297 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
298 if (no_agent_forwarding_flag || compat13) {
299 debug("Authentication agent forwarding not permitted for this authentication.");
300 break;
301 }
302 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000303 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000304 break;
305
306 case SSH_CMSG_PORT_FORWARD_REQUEST:
307 if (no_port_forwarding_flag) {
308 debug("Port forwarding not permitted for this authentication.");
309 break;
310 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100311 if (!options.allow_tcp_forwarding) {
312 debug("Port forwarding not permitted.");
313 break;
314 }
Damien Millerb38eff82000-04-01 11:09:21 +1000315 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000316 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000317 success = 1;
318 break;
319
320 case SSH_CMSG_MAX_PACKET_SIZE:
321 if (packet_set_maxsize(packet_get_int()) > 0)
322 success = 1;
323 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100324
Damien Millerb38eff82000-04-01 11:09:21 +1000325 case SSH_CMSG_EXEC_SHELL:
326 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000327 if (type == SSH_CMSG_EXEC_CMD) {
328 command = packet_get_string(&dlen);
329 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000330 do_exec(s, command);
331 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000332 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000333 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000334 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100335 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000336 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000337 return;
338
339 default:
340 /*
341 * Any unknown messages in this phase are ignored,
342 * and a failure message is returned.
343 */
Damien Miller996acd22003-04-09 20:59:48 +1000344 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000345 }
346 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
347 packet_send();
348 packet_write_wait();
349
350 /* Enable compression now that we have replied if appropriate. */
351 if (enable_compression_after_reply) {
352 enable_compression_after_reply = 0;
353 packet_start_compression(compression_level);
354 }
355 }
356}
357
358/*
359 * This is called to fork and execute a command when we have no tty. This
360 * will call do_child from the child, and server_loop from the parent after
361 * setting up file descriptors and such.
362 */
Damien Miller4af51302000-04-16 11:18:38 +1000363void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000364do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000365{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000366 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000367
368#ifdef USE_PIPES
369 int pin[2], pout[2], perr[2];
370 /* Allocate pipes for communicating with the program. */
371 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
372 packet_disconnect("Could not create pipes: %.100s",
373 strerror(errno));
374#else /* USE_PIPES */
375 int inout[2], err[2];
376 /* Uses socket pairs to communicate with the program. */
377 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
378 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
379 packet_disconnect("Could not create socket pairs: %.100s",
380 strerror(errno));
381#endif /* USE_PIPES */
382 if (s == NULL)
383 fatal("do_exec_no_pty: no session");
384
Damien Millere247cc42000-05-07 12:03:14 +1000385 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000386
Damien Millerc5946332001-02-28 11:46:11 +1100387#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000388 if (options.use_pam) {
Damien Miller4e448a32003-05-14 15:11:48 +1000389 do_pam_setcred(1);
390 if (is_pam_password_change_required())
391 packet_disconnect("Password change required but no "
392 "TTY available");
393 }
Kevin Stevesff793a22001-02-21 16:36:51 +0000394#endif /* USE_PAM */
395
Damien Millerb38eff82000-04-01 11:09:21 +1000396 /* Fork the child. */
397 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000398 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000399
Damien Millerb38eff82000-04-01 11:09:21 +1000400 /* Child. Reinitialize the log since the pid has changed. */
401 log_init(__progname, options.log_level, options.log_facility, log_stderr);
402
403 /*
404 * Create a new session and process group since the 4.4BSD
405 * setlogin() affects the entire process group.
406 */
407 if (setsid() < 0)
408 error("setsid failed: %.100s", strerror(errno));
409
410#ifdef USE_PIPES
411 /*
412 * Redirect stdin. We close the parent side of the socket
413 * pair, and make the child side the standard input.
414 */
415 close(pin[1]);
416 if (dup2(pin[0], 0) < 0)
417 perror("dup2 stdin");
418 close(pin[0]);
419
420 /* Redirect stdout. */
421 close(pout[0]);
422 if (dup2(pout[1], 1) < 0)
423 perror("dup2 stdout");
424 close(pout[1]);
425
426 /* Redirect stderr. */
427 close(perr[0]);
428 if (dup2(perr[1], 2) < 0)
429 perror("dup2 stderr");
430 close(perr[1]);
431#else /* USE_PIPES */
432 /*
433 * Redirect stdin, stdout, and stderr. Stdin and stdout will
434 * use the same socket, as some programs (particularly rdist)
435 * seem to depend on it.
436 */
437 close(inout[1]);
438 close(err[1]);
439 if (dup2(inout[0], 0) < 0) /* stdin */
440 perror("dup2 stdin");
441 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
442 perror("dup2 stdout");
443 if (dup2(err[0], 2) < 0) /* stderr */
444 perror("dup2 stderr");
445#endif /* USE_PIPES */
446
Tim Rice81ed5182002-09-25 17:38:46 -0700447#ifdef _UNICOS
448 cray_init_job(s->pw); /* set up cray jid and tmpdir */
449#endif
450
Damien Millerb38eff82000-04-01 11:09:21 +1000451 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000452 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000453 /* NOTREACHED */
454 }
Tim Rice81ed5182002-09-25 17:38:46 -0700455#ifdef _UNICOS
456 signal(WJSIGNAL, cray_job_termination_handler);
457#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100458#ifdef HAVE_CYGWIN
459 if (is_winnt)
460 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
461#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000462 if (pid < 0)
463 packet_disconnect("fork failed: %.100s", strerror(errno));
464 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000465 /* Set interactive/non-interactive mode. */
466 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000467#ifdef USE_PIPES
468 /* We are the parent. Close the child sides of the pipes. */
469 close(pin[0]);
470 close(pout[1]);
471 close(perr[1]);
472
Damien Millerefb4afe2000-04-12 18:45:05 +1000473 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000474 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000475 } else {
476 /* Enter the interactive session. */
477 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000478 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000479 }
Damien Millerb38eff82000-04-01 11:09:21 +1000480#else /* USE_PIPES */
481 /* We are the parent. Close the child sides of the socket pairs. */
482 close(inout[0]);
483 close(err[0]);
484
485 /*
486 * Enter the interactive session. Note: server_loop must be able to
487 * handle the case that fdin and fdout are the same.
488 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000489 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000490 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000491 } else {
492 server_loop(pid, inout[1], inout[1], err[1]);
493 /* server_loop has closed inout[1] and err[1]. */
494 }
Damien Millerb38eff82000-04-01 11:09:21 +1000495#endif /* USE_PIPES */
496}
497
498/*
499 * This is called to fork and execute a command when we have a tty. This
500 * will call do_child from the child, and server_loop from the parent after
501 * setting up file descriptors, controlling tty, updating wtmp, utmp,
502 * lastlog, and other such operations.
503 */
Damien Miller4af51302000-04-16 11:18:38 +1000504void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000505do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000506{
Damien Millerb38eff82000-04-01 11:09:21 +1000507 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000508 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000509
510 if (s == NULL)
511 fatal("do_exec_pty: no session");
512 ptyfd = s->ptyfd;
513 ttyfd = s->ttyfd;
514
Damien Millerc5946332001-02-28 11:46:11 +1100515#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000516 if (options.use_pam) {
Damien Miller341c6e62003-09-02 23:18:52 +1000517 do_pam_set_tty(s->tty);
Damien Miller4e448a32003-05-14 15:11:48 +1000518 do_pam_setcred(1);
519 }
Damien Miller5a761312001-02-27 09:28:23 +1100520#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000521
Damien Millerb38eff82000-04-01 11:09:21 +1000522 /* Fork the child. */
523 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000524 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000525
Damien Miller942da032000-08-18 13:59:06 +1000526 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000527 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000528 /* Close the master side of the pseudo tty. */
529 close(ptyfd);
530
531 /* Make the pseudo tty our controlling tty. */
532 pty_make_controlling_tty(&ttyfd, s->tty);
533
Damien Miller9c751422001-10-10 15:02:46 +1000534 /* Redirect stdin/stdout/stderr from the pseudo tty. */
535 if (dup2(ttyfd, 0) < 0)
536 error("dup2 stdin: %s", strerror(errno));
537 if (dup2(ttyfd, 1) < 0)
538 error("dup2 stdout: %s", strerror(errno));
539 if (dup2(ttyfd, 2) < 0)
540 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000541
542 /* Close the extra descriptor for the pseudo tty. */
543 close(ttyfd);
544
Damien Miller942da032000-08-18 13:59:06 +1000545 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000546#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700547 if (!(options.use_login && command == NULL)) {
548#ifdef _UNICOS
549 cray_init_job(s->pw); /* set up cray jid and tmpdir */
550#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100551 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700552 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000553# ifdef LOGIN_NEEDS_UTMPX
554 else
555 do_pre_login(s);
556# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000557#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000558
Damien Millerb38eff82000-04-01 11:09:21 +1000559 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000560 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000561 /* NOTREACHED */
562 }
Tim Rice81ed5182002-09-25 17:38:46 -0700563#ifdef _UNICOS
564 signal(WJSIGNAL, cray_job_termination_handler);
565#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100566#ifdef HAVE_CYGWIN
567 if (is_winnt)
568 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
569#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000570 if (pid < 0)
571 packet_disconnect("fork failed: %.100s", strerror(errno));
572 s->pid = pid;
573
574 /* Parent. Close the slave side of the pseudo tty. */
575 close(ttyfd);
576
577 /*
578 * Create another descriptor of the pty master side for use as the
579 * standard input. We could use the original descriptor, but this
580 * simplifies code in server_loop. The descriptor is bidirectional.
581 */
582 fdout = dup(ptyfd);
583 if (fdout < 0)
584 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
585
586 /* we keep a reference to the pty master */
587 ptymaster = dup(ptyfd);
588 if (ptymaster < 0)
589 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
590 s->ptymaster = ptymaster;
591
592 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000593 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000594 if (compat20) {
595 session_set_fds(s, ptyfd, fdout, -1);
596 } else {
597 server_loop(pid, ptyfd, fdout, -1);
598 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000599 }
Damien Millerb38eff82000-04-01 11:09:21 +1000600}
601
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000602#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000603static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000604do_pre_login(Session *s)
605{
606 socklen_t fromlen;
607 struct sockaddr_storage from;
608 pid_t pid = getpid();
609
610 /*
611 * Get IP address of client. If the connection is not a socket, let
612 * the address be 0.0.0.0.
613 */
614 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000615 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000616 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000617 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100618 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000619 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000620 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000621 }
622 }
623
624 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000625 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000626 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000627}
628#endif
629
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000630/*
631 * This is called to fork and execute a command. If another command is
632 * to be forced, execute that instead.
633 */
634void
635do_exec(Session *s, const char *command)
636{
637 if (forced_command) {
638 original_command = command;
639 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000640 debug("Forced command '%.900s'", command);
641 }
642
Damien Miller324948b2003-09-02 22:55:45 +1000643#ifdef GSSAPI
644 if (options.gss_authentication) {
645 temporarily_use_uid(s->pw);
646 ssh_gssapi_storecreds();
647 restore_uid();
648 }
649#endif
650
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000651 if (s->ttyfd != -1)
652 do_exec_pty(s, command);
653 else
654 do_exec_no_pty(s, command);
655
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000656 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000657}
658
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000659
Damien Miller942da032000-08-18 13:59:06 +1000660/* administrative, login(1)-like work */
661void
Damien Miller69b69aa2000-10-28 14:19:58 +1100662do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000663{
Damien Miller942da032000-08-18 13:59:06 +1000664 char *time_string;
Damien Miller942da032000-08-18 13:59:06 +1000665 socklen_t fromlen;
666 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000667 struct passwd * pw = s->pw;
668 pid_t pid = getpid();
669
670 /*
671 * Get IP address of client. If the connection is not a socket, let
672 * the address be 0.0.0.0.
673 */
674 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000675 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000676 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000677 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000678 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000679 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000680 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000681 }
682 }
683
684 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000685 if (!use_privsep)
686 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
687 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000688 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000689 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000690
Kevin Steves092f2ef2000-10-14 13:36:13 +0000691#ifdef USE_PAM
692 /*
693 * If password change is needed, do it now.
694 * This needs to occur before the ~/.hushlogin check.
695 */
Damien Miller4e448a32003-05-14 15:11:48 +1000696 if (options.use_pam && is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000697 print_pam_messages();
698 do_pam_chauthtok();
Damien Miller1f499fd2003-08-25 13:08:49 +1000699 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000700 }
701#endif
702
Damien Millercf205e82001-04-16 18:29:15 +1000703 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000704 return;
705
706#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000707 if (options.use_pam && !is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000708 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000709#endif /* USE_PAM */
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000710
711 /* display post-login message */
712 if (buffer_len(&loginmsg) > 0) {
713 buffer_append(&loginmsg, "\0", 1);
714 printf("%s\n", (char *)buffer_ptr(&loginmsg));
715 }
716 buffer_free(&loginmsg);
Damien Miller942da032000-08-18 13:59:06 +1000717
Tim Rice81ed5182002-09-25 17:38:46 -0700718#ifndef NO_SSH_LASTLOG
Ben Lindstromc447fee2002-04-02 20:35:35 +0000719 if (options.print_lastlog && s->last_login_time != 0) {
720 time_string = ctime(&s->last_login_time);
Damien Miller942da032000-08-18 13:59:06 +1000721 if (strchr(time_string, '\n'))
722 *strchr(time_string, '\n') = 0;
Ben Lindstromc447fee2002-04-02 20:35:35 +0000723 if (strcmp(s->hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000724 printf("Last login: %s\r\n", time_string);
725 else
Ben Lindstromc447fee2002-04-02 20:35:35 +0000726 printf("Last login: %s from %s\r\n", time_string,
727 s->hostname);
Damien Miller942da032000-08-18 13:59:06 +1000728 }
Tim Rice81ed5182002-09-25 17:38:46 -0700729#endif /* NO_SSH_LASTLOG */
Damien Millercf205e82001-04-16 18:29:15 +1000730
731 do_motd();
732}
733
734/*
735 * Display the message of the day.
736 */
737void
738do_motd(void)
739{
740 FILE *f;
741 char buf[256];
742
Damien Miller942da032000-08-18 13:59:06 +1000743 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000744#ifdef HAVE_LOGIN_CAP
745 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
746 "/etc/motd"), "r");
747#else
Damien Miller942da032000-08-18 13:59:06 +1000748 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000749#endif
Damien Miller942da032000-08-18 13:59:06 +1000750 if (f) {
751 while (fgets(buf, sizeof(buf), f))
752 fputs(buf, stdout);
753 fclose(f);
754 }
755 }
756}
757
Kevin Steves8f63caa2001-07-04 18:23:02 +0000758
759/*
760 * Check for quiet login, either .hushlogin or command given.
761 */
762int
763check_quietlogin(Session *s, const char *command)
764{
765 char buf[256];
766 struct passwd *pw = s->pw;
767 struct stat st;
768
769 /* Return 1 if .hushlogin exists or a command given. */
770 if (command != NULL)
771 return 1;
772 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
773#ifdef HAVE_LOGIN_CAP
774 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
775 return 1;
776#else
777 if (stat(buf, &st) >= 0)
778 return 1;
779#endif
780 return 0;
781}
782
Damien Millerb38eff82000-04-01 11:09:21 +1000783/*
784 * Sets the value of the given variable in the environment. If the variable
785 * already exists, its value is overriden.
786 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000787void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000788child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100789 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000790{
Damien Millerb38eff82000-04-01 11:09:21 +1000791 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000792 u_int envsize;
793 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000794
795 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000796 * If we're passed an uninitialized list, allocate a single null
797 * entry before continuing.
798 */
799 if (*envp == NULL && *envsizep == 0) {
800 *envp = xmalloc(sizeof(char *));
801 *envp[0] = NULL;
802 *envsizep = 1;
803 }
804
805 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000806 * Find the slot where the value should be stored. If the variable
807 * already exists, we reuse the slot; otherwise we append a new slot
808 * at the end of the array, expanding if necessary.
809 */
810 env = *envp;
811 namelen = strlen(name);
812 for (i = 0; env[i]; i++)
813 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
814 break;
815 if (env[i]) {
816 /* Reuse the slot. */
817 xfree(env[i]);
818 } else {
819 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000820 envsize = *envsizep;
821 if (i >= envsize - 1) {
822 if (envsize >= 1000)
823 fatal("child_set_env: too many env vars");
824 envsize += 50;
825 env = (*envp) = xrealloc(env, envsize * sizeof(char *));
826 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000827 }
828 /* Need to set the NULL pointer at end of array beyond the new slot. */
829 env[i + 1] = NULL;
830 }
831
832 /* Allocate space and format the variable in the appropriate slot. */
833 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
834 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
835}
836
837/*
838 * Reads environment variables from the given file and adds/overrides them
839 * into the environment. If the file does not exist, this does nothing.
840 * Otherwise, it must consist of empty lines, comments (line starts with '#')
841 * and assignments of the form name=value. No other forms are allowed.
842 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000843static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000844read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100845 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000846{
847 FILE *f;
848 char buf[4096];
849 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000850 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000851
852 f = fopen(filename, "r");
853 if (!f)
854 return;
855
856 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000857 if (++lineno > 1000)
858 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000859 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
860 ;
861 if (!*cp || *cp == '#' || *cp == '\n')
862 continue;
863 if (strchr(cp, '\n'))
864 *strchr(cp, '\n') = '\0';
865 value = strchr(cp, '=');
866 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000867 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
868 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000869 continue;
870 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000871 /*
872 * Replace the equals sign by nul, and advance value to
873 * the value string.
874 */
Damien Millerb38eff82000-04-01 11:09:21 +1000875 *value = '\0';
876 value++;
877 child_set_env(env, envsize, cp, value);
878 }
879 fclose(f);
880}
881
Darren Tuckere1a790d2003-09-16 11:52:19 +1000882#ifdef HAVE_ETC_DEFAULT_LOGIN
883/*
884 * Return named variable from specified environment, or NULL if not present.
885 */
886static char *
887child_get_env(char **env, const char *name)
888{
889 int i;
890 size_t len;
891
892 len = strlen(name);
893 for (i=0; env[i] != NULL; i++)
894 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
895 return(env[i] + len + 1);
896 return NULL;
897}
898
899/*
900 * Read /etc/default/login.
901 * We pick up the PATH (or SUPATH for root) and UMASK.
902 */
903static void
904read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
905{
906 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000907 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000908 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000909
910 /*
911 * We don't want to copy the whole file to the child's environment,
912 * so we use a temporary environment and copy the variables we're
913 * interested in.
914 */
915 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
916
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000917 if (tmpenv == NULL)
918 return;
919
Darren Tuckere1a790d2003-09-16 11:52:19 +1000920 if (uid == 0)
921 var = child_get_env(tmpenv, "SUPATH");
922 else
923 var = child_get_env(tmpenv, "PATH");
924 if (var != NULL)
925 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100926
Darren Tuckere1a790d2003-09-16 11:52:19 +1000927 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
928 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000929 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100930
Darren Tuckere1a790d2003-09-16 11:52:19 +1000931 for (i = 0; tmpenv[i] != NULL; i++)
932 xfree(tmpenv[i]);
933 xfree(tmpenv);
934}
935#endif /* HAVE_ETC_DEFAULT_LOGIN */
936
Damien Millerbb9ffc12002-01-08 10:59:32 +1100937void copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000938{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100939 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000940 int i;
941
Damien Millerbb9ffc12002-01-08 10:59:32 +1100942 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000943 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000944
Damien Millerbb9ffc12002-01-08 10:59:32 +1100945 for(i = 0; source[i] != NULL; i++) {
946 var_name = xstrdup(source[i]);
947 if ((var_val = strstr(var_name, "=")) == NULL) {
948 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000949 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000950 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100951 *var_val++ = '\0';
952
953 debug3("Copy environment: %s=%s", var_name, var_val);
954 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +1100955
Damien Millerbb9ffc12002-01-08 10:59:32 +1100956 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000957 }
958}
Damien Millercb5e44a2000-09-29 12:12:36 +1100959
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000960static char **
961do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000962{
Damien Millerb38eff82000-04-01 11:09:21 +1000963 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000964 u_int i, envsize;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000965 char **env, *laddr, *path = NULL;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000966 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000967
Damien Millerb38eff82000-04-01 11:09:21 +1000968 /* Initialize the environment. */
969 envsize = 100;
970 env = xmalloc(envsize * sizeof(char *));
971 env[0] = NULL;
972
Damien Millerbac2d8a2000-09-05 16:13:06 +1100973#ifdef HAVE_CYGWIN
974 /*
975 * The Windows environment contains some setting which are
976 * important for a running system. They must not be dropped.
977 */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100978 copy_environment(environ, &env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100979#endif
980
Darren Tucker0efd1552003-08-26 11:49:55 +1000981#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100982 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +1000983 * the childs environment as they see fit
984 */
985 ssh_gssapi_do_child(&env, &envsize);
986#endif
987
Damien Millerb38eff82000-04-01 11:09:21 +1000988 if (!options.use_login) {
989 /* Set basic environment. */
990 child_set_env(&env, &envsize, "USER", pw->pw_name);
991 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +1100992#ifdef _AIX
993 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
994#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000995 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000996#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +0000997 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
998 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
999 else
1000 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001001#else /* HAVE_LOGIN_CAP */
1002# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001003 /*
1004 * There's no standard path on Windows. The path contains
1005 * important components pointing to the system directories,
1006 * needed for loading shared libraries. So the path better
1007 * remains intact here.
1008 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001009# ifdef HAVE_ETC_DEFAULT_LOGIN
1010 read_etc_default_login(&env, &envsize, pw->pw_uid);
1011 path = child_get_env(env, "PATH");
1012# endif /* HAVE_ETC_DEFAULT_LOGIN */
1013 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001014 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001015 s->pw->pw_uid == 0 ?
1016 SUPERUSER_PATH : _PATH_STDPATH);
1017 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001018# endif /* HAVE_CYGWIN */
1019#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001020
1021 snprintf(buf, sizeof buf, "%.200s/%.50s",
1022 _PATH_MAILDIR, pw->pw_name);
1023 child_set_env(&env, &envsize, "MAIL", buf);
1024
1025 /* Normal systems set SHELL by default. */
1026 child_set_env(&env, &envsize, "SHELL", shell);
1027 }
1028 if (getenv("TZ"))
1029 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1030
1031 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001032 if (!options.use_login) {
1033 while (custom_environment) {
1034 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001035 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001036
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001037 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001038 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001039 if (str[i] == '=') {
1040 str[i] = 0;
1041 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001042 }
1043 custom_environment = ce->next;
1044 xfree(ce->s);
1045 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001046 }
Damien Millerb38eff82000-04-01 11:09:21 +10001047 }
1048
Damien Millerf37e2462002-09-19 11:47:55 +10001049 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001050 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001051 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001052 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1053
Damien Miller00111382003-03-10 11:21:17 +11001054 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001055 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001056 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1057 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001058 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1059
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001060 if (s->ttyfd != -1)
1061 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1062 if (s->term)
1063 child_set_env(&env, &envsize, "TERM", s->term);
1064 if (s->display)
1065 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001066 if (original_command)
1067 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1068 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001069
Tim Rice81ed5182002-09-25 17:38:46 -07001070#ifdef _UNICOS
1071 if (cray_tmpdir[0] != '\0')
1072 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1073#endif /* _UNICOS */
1074
Damien Millerb38eff82000-04-01 11:09:21 +10001075#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001076 {
1077 char *cp;
1078
1079 if ((cp = getenv("AUTHSTATE")) != NULL)
1080 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1081 if ((cp = getenv("KRB5CCNAME")) != NULL)
1082 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1083 read_environment_file(&env, &envsize, "/etc/environment");
1084 }
Damien Millerb38eff82000-04-01 11:09:21 +10001085#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001086#ifdef KRB5
1087 if (s->authctxt->krb5_ticket_file)
1088 child_set_env(&env, &envsize, "KRB5CCNAME",
1089 s->authctxt->krb5_ticket_file);
1090#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001091#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001092 /*
1093 * Pull in any environment variables that may have
1094 * been set by PAM.
1095 */
Damien Miller4e448a32003-05-14 15:11:48 +10001096 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001097 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001098
Damien Millerc756e9b2003-11-17 21:41:42 +11001099 p = fetch_pam_child_environment();
1100 copy_environment(p, &env, &envsize);
1101 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001102
Damien Millerc756e9b2003-11-17 21:41:42 +11001103 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001104 copy_environment(p, &env, &envsize);
1105 free_pam_environment(p);
1106 }
Damien Millerb38eff82000-04-01 11:09:21 +10001107#endif /* USE_PAM */
1108
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001109 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001110 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001111 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001112
1113 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001114 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001115 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001116 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001117 read_environment_file(&env, &envsize, buf);
1118 }
1119 if (debug_flag) {
1120 /* dump the environment */
1121 fprintf(stderr, "Environment:\n");
1122 for (i = 0; env[i]; i++)
1123 fprintf(stderr, " %.200s\n", env[i]);
1124 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001125 return env;
1126}
1127
1128/*
1129 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1130 * first in this order).
1131 */
1132static void
1133do_rc_files(Session *s, const char *shell)
1134{
1135 FILE *f = NULL;
1136 char cmd[1024];
1137 int do_xauth;
1138 struct stat st;
1139
1140 do_xauth =
1141 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1142
1143 /* ignore _PATH_SSH_USER_RC for subsystems */
1144 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1145 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1146 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1147 if (debug_flag)
1148 fprintf(stderr, "Running %s\n", cmd);
1149 f = popen(cmd, "w");
1150 if (f) {
1151 if (do_xauth)
1152 fprintf(f, "%s %s\n", s->auth_proto,
1153 s->auth_data);
1154 pclose(f);
1155 } else
1156 fprintf(stderr, "Could not run %s\n",
1157 _PATH_SSH_USER_RC);
1158 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1159 if (debug_flag)
1160 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1161 _PATH_SSH_SYSTEM_RC);
1162 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1163 if (f) {
1164 if (do_xauth)
1165 fprintf(f, "%s %s\n", s->auth_proto,
1166 s->auth_data);
1167 pclose(f);
1168 } else
1169 fprintf(stderr, "Could not run %s\n",
1170 _PATH_SSH_SYSTEM_RC);
1171 } else if (do_xauth && options.xauth_location != NULL) {
1172 /* Add authority data to .Xauthority if appropriate. */
1173 if (debug_flag) {
1174 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001175 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001176 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001177 fprintf(stderr,
1178 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001179 options.xauth_location, s->auth_display,
1180 s->auth_proto, s->auth_data);
1181 }
1182 snprintf(cmd, sizeof cmd, "%s -q -",
1183 options.xauth_location);
1184 f = popen(cmd, "w");
1185 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001186 fprintf(f, "remove %s\n",
1187 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001188 fprintf(f, "add %s %s %s\n",
1189 s->auth_display, s->auth_proto,
1190 s->auth_data);
1191 pclose(f);
1192 } else {
1193 fprintf(stderr, "Could not run %s\n",
1194 cmd);
1195 }
1196 }
1197}
1198
1199static void
1200do_nologin(struct passwd *pw)
1201{
1202 FILE *f = NULL;
1203 char buf[1024];
1204
1205#ifdef HAVE_LOGIN_CAP
1206 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1207 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1208 _PATH_NOLOGIN), "r");
1209#else
1210 if (pw->pw_uid)
1211 f = fopen(_PATH_NOLOGIN, "r");
1212#endif
1213 if (f) {
1214 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001215 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001216 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001217 while (fgets(buf, sizeof(buf), f))
1218 fputs(buf, stderr);
1219 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001220 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001221 exit(254);
1222 }
1223}
1224
1225/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001227do_setusercontext(struct passwd *pw)
1228{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001229#ifndef HAVE_CYGWIN
1230 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001231#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001232 {
1233
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001234#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001235 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1236 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001237#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001238#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001239# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001240 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001241# endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001242 if (setusercontext(lc, pw, pw->pw_uid,
1243 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1244 perror("unable to set user context");
1245 exit(1);
1246 }
1247#else
1248# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1249 /* Sets login uid for accounting */
1250 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1251 error("setluid: %s", strerror(errno));
1252# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1253
1254 if (setlogin(pw->pw_name) < 0)
1255 error("setlogin failed: %s", strerror(errno));
1256 if (setgid(pw->pw_gid) < 0) {
1257 perror("setgid");
1258 exit(1);
1259 }
1260 /* Initialize the group list. */
1261 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1262 perror("initgroups");
1263 exit(1);
1264 }
1265 endgrent();
1266# ifdef USE_PAM
1267 /*
Damien Millera8e06ce2003-11-21 23:48:55 +11001268 * PAM credentials may take the form of supplementary groups.
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001269 * These will have been wiped by the above initgroups() call.
1270 * Reestablish them here.
1271 */
Damien Miller341c6e62003-09-02 23:18:52 +10001272 if (options.use_pam) {
1273 do_pam_session();
Damien Miller4e448a32003-05-14 15:11:48 +10001274 do_pam_setcred(0);
Damien Miller341c6e62003-09-02 23:18:52 +10001275 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001276# endif /* USE_PAM */
1277# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1278 irix_setusercontext(pw);
1279# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001280# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001281 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001282# endif /* _AIX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001283 /* Permanently switch to the desired uid. */
1284 permanently_set_uid(pw);
1285#endif
1286 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001287
1288#ifdef HAVE_CYGWIN
1289 if (is_winnt)
1290#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001291 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1292 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1293}
1294
Ben Lindstrom08105192002-03-22 02:50:06 +00001295static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296launch_login(struct passwd *pw, const char *hostname)
1297{
1298 /* Launch login(1). */
1299
Ben Lindstrom378a4172002-06-07 14:49:56 +00001300 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001301#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001302 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001303#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001304#ifdef LOGIN_NO_ENDOPT
1305 "-p", "-f", pw->pw_name, (char *)NULL);
1306#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001308#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309
1310 /* Login couldn't be executed, die. */
1311
1312 perror("login");
1313 exit(1);
1314}
1315
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001316/*
1317 * Performs common processing for the child, such as setting up the
1318 * environment, closing extra file descriptors, setting the user and group
1319 * ids, and executing the command or shell.
1320 */
1321void
1322do_child(Session *s, const char *command)
1323{
1324 extern char **environ;
1325 char **env;
1326 char *argv[10];
1327 const char *shell, *shell0, *hostname = NULL;
1328 struct passwd *pw = s->pw;
1329 u_int i;
1330
1331 /* remove hostkey from the child's memory */
1332 destroy_sensitive_data();
1333
1334 /* login(1) is only called if we execute the login shell */
1335 if (options.use_login && command != NULL)
1336 options.use_login = 0;
1337
Tim Rice81ed5182002-09-25 17:38:46 -07001338#ifdef _UNICOS
1339 cray_setup(pw->pw_uid, pw->pw_name, command);
1340#endif /* _UNICOS */
1341
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001342 /*
1343 * Login(1) does this as well, and it needs uid 0 for the "-h"
1344 * switch, so we let login(1) to this for us.
1345 */
1346 if (!options.use_login) {
1347#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001348 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001349 if (!check_quietlogin(s, command))
1350 do_motd();
1351#else /* HAVE_OSF_SIA */
1352 do_nologin(pw);
1353 do_setusercontext(pw);
1354#endif /* HAVE_OSF_SIA */
1355 }
1356
1357 /*
1358 * Get the shell from the password data. An empty shell field is
1359 * legal, and means /bin/sh.
1360 */
1361 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001362
1363 /*
1364 * Make sure $SHELL points to the shell from the password file,
1365 * even if shell is overridden from login.conf
1366 */
1367 env = do_setup_env(s, shell);
1368
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001369#ifdef HAVE_LOGIN_CAP
1370 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1371#endif
1372
Damien Millerad833b32000-08-23 10:46:23 +10001373 /* we have to stash the hostname before we close our socket. */
1374 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001375 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001376 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001377 /*
1378 * Close the connection descriptors; note that this is the child, and
1379 * the server will still have the socket open, and it is important
1380 * that we do not shutdown it. Note that the descriptors cannot be
1381 * closed before building the environment, as we call
1382 * get_remote_ipaddr there.
1383 */
1384 if (packet_get_connection_in() == packet_get_connection_out())
1385 close(packet_get_connection_in());
1386 else {
1387 close(packet_get_connection_in());
1388 close(packet_get_connection_out());
1389 }
1390 /*
1391 * Close all descriptors related to channels. They will still remain
1392 * open in the parent.
1393 */
1394 /* XXX better use close-on-exec? -markus */
1395 channel_close_all();
1396
1397 /*
1398 * Close any extra file descriptors. Note that there may still be
1399 * descriptors left by system functions. They will be closed later.
1400 */
1401 endpwent();
1402
1403 /*
1404 * Close any extra open file descriptors so that we don\'t have them
1405 * hanging around in clients. Note that we want to do this after
1406 * initgroups, because at least on Solaris 2.3 it leaves file
1407 * descriptors open.
1408 */
1409 for (i = 3; i < 64; i++)
1410 close(i);
1411
Damien Millerb38eff82000-04-01 11:09:21 +10001412 /*
Damien Miller05eda432002-02-10 18:32:28 +11001413 * Must take new environment into use so that .ssh/rc,
1414 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001415 */
1416 environ = env;
1417
Damien Miller139d4cd2001-10-10 15:07:44 +10001418 /* Change current directory to the user\'s home directory. */
1419 if (chdir(pw->pw_dir) < 0) {
1420 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1421 pw->pw_dir, strerror(errno));
1422#ifdef HAVE_LOGIN_CAP
1423 if (login_getcapbool(lc, "requirehome", 0))
1424 exit(1);
1425#endif
1426 }
1427
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001428 if (!options.use_login)
1429 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001430
1431 /* restore SIGPIPE for child */
1432 signal(SIGPIPE, SIG_DFL);
1433
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001434 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435 launch_login(pw, hostname);
1436 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001437 }
1438
1439 /* Get the last component of the shell name. */
1440 if ((shell0 = strrchr(shell, '/')) != NULL)
1441 shell0++;
1442 else
1443 shell0 = shell;
1444
Damien Millerb38eff82000-04-01 11:09:21 +10001445 /*
1446 * If we have no command, execute the shell. In this case, the shell
1447 * name to be passed in argv[0] is preceded by '-' to indicate that
1448 * this is a login shell.
1449 */
1450 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001451 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001452
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001453 /* Start the shell. Set initial character to '-'. */
1454 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001455
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001456 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1457 >= sizeof(argv0) - 1) {
1458 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001459 perror(shell);
1460 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001461 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001462
1463 /* Execute the shell. */
1464 argv[0] = argv0;
1465 argv[1] = NULL;
1466 execve(shell, argv, env);
1467
1468 /* Executing the shell failed. */
1469 perror(shell);
1470 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001471 }
1472 /*
1473 * Execute the command using the user's shell. This uses the -c
1474 * option to execute the command.
1475 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001476 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001477 argv[1] = "-c";
1478 argv[2] = (char *) command;
1479 argv[3] = NULL;
1480 execve(shell, argv, env);
1481 perror(shell);
1482 exit(1);
1483}
1484
1485Session *
1486session_new(void)
1487{
1488 int i;
1489 static int did_init = 0;
1490 if (!did_init) {
1491 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001492 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001493 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001494 }
1495 did_init = 1;
1496 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001497 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001498 Session *s = &sessions[i];
1499 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001500 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001501 s->chanid = -1;
1502 s->ptyfd = -1;
1503 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001504 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001505 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001506 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001507 return s;
1508 }
1509 }
1510 return NULL;
1511}
1512
Ben Lindstrombba81212001-06-25 05:01:22 +00001513static void
Damien Millerb38eff82000-04-01 11:09:21 +10001514session_dump(void)
1515{
1516 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001517 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001518 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001519 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001520 s->used,
1521 s->self,
1522 s,
1523 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001524 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001525 }
1526}
1527
Damien Millerefb4afe2000-04-12 18:45:05 +10001528int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001529session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001530{
1531 Session *s = session_new();
1532 debug("session_open: channel %d", chanid);
1533 if (s == NULL) {
1534 error("no more sessions");
1535 return 0;
1536 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001537 s->authctxt = authctxt;
1538 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001539 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001540 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001541 debug("session_open: session %d: link with channel %d", s->self, chanid);
1542 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001543 return 1;
1544}
1545
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001546Session *
1547session_by_tty(char *tty)
1548{
1549 int i;
1550 for (i = 0; i < MAX_SESSIONS; i++) {
1551 Session *s = &sessions[i];
1552 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1553 debug("session_by_tty: session %d tty %s", i, tty);
1554 return s;
1555 }
1556 }
1557 debug("session_by_tty: unknown tty %.100s", tty);
1558 session_dump();
1559 return NULL;
1560}
1561
Ben Lindstrombba81212001-06-25 05:01:22 +00001562static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001563session_by_channel(int id)
1564{
1565 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001566 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001567 Session *s = &sessions[i];
1568 if (s->used && s->chanid == id) {
1569 debug("session_by_channel: session %d channel %d", i, id);
1570 return s;
1571 }
1572 }
1573 debug("session_by_channel: unknown channel %d", id);
1574 session_dump();
1575 return NULL;
1576}
1577
Ben Lindstrombba81212001-06-25 05:01:22 +00001578static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001579session_by_pid(pid_t pid)
1580{
1581 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001582 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001583 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001584 Session *s = &sessions[i];
1585 if (s->used && s->pid == pid)
1586 return s;
1587 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001588 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001589 session_dump();
1590 return NULL;
1591}
1592
Ben Lindstrombba81212001-06-25 05:01:22 +00001593static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001594session_window_change_req(Session *s)
1595{
1596 s->col = packet_get_int();
1597 s->row = packet_get_int();
1598 s->xpixel = packet_get_int();
1599 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001600 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001601 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1602 return 1;
1603}
1604
Ben Lindstrombba81212001-06-25 05:01:22 +00001605static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001606session_pty_req(Session *s)
1607{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001608 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001609 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001610
Ben Lindstrom49c12602001-06-13 04:37:36 +00001611 if (no_pty_flag) {
1612 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001613 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001614 }
1615 if (s->ttyfd != -1) {
1616 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001617 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001618 }
Ben Lindstromc447fee2002-04-02 20:35:35 +00001619 /* Get the time and hostname when the user last logged in. */
1620 if (options.print_lastlog) {
1621 s->hostname[0] = '\0';
1622 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1623 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1624 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001625
Damien Millerefb4afe2000-04-12 18:45:05 +10001626 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001627
1628 if (compat20) {
1629 s->col = packet_get_int();
1630 s->row = packet_get_int();
1631 } else {
1632 s->row = packet_get_int();
1633 s->col = packet_get_int();
1634 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001635 s->xpixel = packet_get_int();
1636 s->ypixel = packet_get_int();
1637
1638 if (strcmp(s->term, "") == 0) {
1639 xfree(s->term);
1640 s->term = NULL;
1641 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001642
Damien Millerefb4afe2000-04-12 18:45:05 +10001643 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001644 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001646 if (s->term)
1647 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001648 s->term = NULL;
1649 s->ptyfd = -1;
1650 s->ttyfd = -1;
1651 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001652 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001653 }
1654 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001655
1656 /* for SSH1 the tty modes length is not given */
1657 if (!compat20)
1658 n_bytes = packet_remaining();
1659 tty_parse_modes(s->ttyfd, &n_bytes);
1660
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661 if (!use_privsep)
1662 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001663
1664 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001665 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1666
Damien Miller48b03fc2002-01-22 23:11:40 +11001667 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001668 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001669 return 1;
1670}
1671
Ben Lindstrombba81212001-06-25 05:01:22 +00001672static int
Damien Millerbd483e72000-04-30 10:00:53 +10001673session_subsystem_req(Session *s)
1674{
Damien Millerae452462001-10-10 15:08:06 +10001675 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001676 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001677 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001678 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001679 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001680
Damien Miller48b03fc2002-01-22 23:11:40 +11001681 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001682 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001683
Damien Millerf6d9e222000-06-18 14:50:44 +10001684 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001685 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1686 cmd = options.subsystem_command[i];
1687 if (stat(cmd, &st) < 0) {
1688 error("subsystem: cannot stat %s: %s", cmd,
1689 strerror(errno));
1690 break;
1691 }
1692 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001693 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001694 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001695 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001696 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001697 }
1698 }
1699
1700 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10001701 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001702 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001703
Damien Millerbd483e72000-04-30 10:00:53 +10001704 xfree(subsys);
1705 return success;
1706}
1707
Ben Lindstrombba81212001-06-25 05:01:22 +00001708static int
Damien Millerbd483e72000-04-30 10:00:53 +10001709session_x11_req(Session *s)
1710{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001711 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001712
1713 s->single_connection = packet_get_char();
1714 s->auth_proto = packet_get_string(NULL);
1715 s->auth_data = packet_get_string(NULL);
1716 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001717 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001718
Ben Lindstrom768176b2001-06-09 01:29:12 +00001719 success = session_setup_x11fwd(s);
1720 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001721 xfree(s->auth_proto);
1722 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001723 s->auth_proto = NULL;
1724 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001725 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001726 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001727}
1728
Ben Lindstrombba81212001-06-25 05:01:22 +00001729static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001730session_shell_req(Session *s)
1731{
Damien Miller48b03fc2002-01-22 23:11:40 +11001732 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001733 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001734 return 1;
1735}
1736
Ben Lindstrombba81212001-06-25 05:01:22 +00001737static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001738session_exec_req(Session *s)
1739{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001740 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001741 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001742 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001743 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001744 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001745 return 1;
1746}
1747
Ben Lindstrombba81212001-06-25 05:01:22 +00001748static int
Damien Miller54c45982003-05-15 10:20:13 +10001749session_break_req(Session *s)
1750{
1751 u_int break_length;
1752
Darren Tucker3bdbd842003-08-13 20:31:05 +10001753 break_length = packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10001754 packet_check_eom();
1755
Darren Tucker3bdbd842003-08-13 20:31:05 +10001756 if (s->ttyfd == -1 ||
1757 tcsendbreak(s->ttyfd, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10001758 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10001759 return 1;
1760}
1761
1762static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001763session_auth_agent_req(Session *s)
1764{
1765 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001766 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001767 if (no_agent_forwarding_flag) {
1768 debug("session_auth_agent_req: no_agent_forwarding_flag");
1769 return 0;
1770 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001771 if (called) {
1772 return 0;
1773 } else {
1774 called = 1;
1775 return auth_input_request_forwarding(s->pw);
1776 }
1777}
1778
Damien Millerc7ef63d2002-02-05 12:21:42 +11001779int
1780session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001781{
Damien Millerefb4afe2000-04-12 18:45:05 +10001782 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001783 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001784
Damien Millerc7ef63d2002-02-05 12:21:42 +11001785 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10001786 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11001787 c->self, rtype);
1788 return 0;
1789 }
1790 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001791
1792 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001793 * a session is in LARVAL state until a shell, a command
1794 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001795 */
1796 if (c->type == SSH_CHANNEL_LARVAL) {
1797 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001798 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001799 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001800 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001801 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001802 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001803 } else if (strcmp(rtype, "x11-req") == 0) {
1804 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001805 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1806 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001807 } else if (strcmp(rtype, "subsystem") == 0) {
1808 success = session_subsystem_req(s);
Damien Miller54c45982003-05-15 10:20:13 +10001809 } else if (strcmp(rtype, "break") == 0) {
1810 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001811 }
1812 }
1813 if (strcmp(rtype, "window-change") == 0) {
1814 success = session_window_change_req(s);
1815 }
Damien Millerc7ef63d2002-02-05 12:21:42 +11001816 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10001817}
1818
1819void
1820session_set_fds(Session *s, int fdin, int fdout, int fderr)
1821{
1822 if (!compat20)
1823 fatal("session_set_fds: called for proto != 2.0");
1824 /*
1825 * now that have a child and a pipe to the child,
1826 * we can activate our channel and register the fd's
1827 */
1828 if (s->chanid == -1)
1829 fatal("no channel for session %d", s->self);
1830 channel_set_fds(s->chanid,
1831 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001832 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11001833 1,
1834 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10001835}
1836
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001837/*
1838 * Function to perform pty cleanup. Also called if we get aborted abnormally
1839 * (e.g., due to a dropped connection).
1840 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001841void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001842session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10001843{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001844 if (s == NULL) {
1845 error("session_pty_cleanup: no session");
1846 return;
1847 }
1848 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001849 return;
1850
Kevin Steves43cdef32001-02-11 14:12:08 +00001851 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001852
Damien Millerb38eff82000-04-01 11:09:21 +10001853 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001854 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08001855 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001856
1857 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001858 if (getuid() == 0)
1859 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001860
1861 /*
1862 * Close the server side of the socket pairs. We must do this after
1863 * the pty cleanup, so that another process doesn't get this pty
1864 * while we're still cleaning up.
1865 */
1866 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001867 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001868
1869 /* unlink pty from session */
1870 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001871}
Damien Millerefb4afe2000-04-12 18:45:05 +10001872
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001873void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001874session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001875{
Darren Tucker3e33cec2003-10-02 16:12:36 +10001876 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001877}
1878
Damien Miller5a80bba2002-09-04 16:39:02 +10001879static char *
1880sig2name(int sig)
1881{
1882#define SSH_SIG(x) if (sig == SIG ## x) return #x
1883 SSH_SIG(ABRT);
1884 SSH_SIG(ALRM);
1885 SSH_SIG(FPE);
1886 SSH_SIG(HUP);
1887 SSH_SIG(ILL);
1888 SSH_SIG(INT);
1889 SSH_SIG(KILL);
1890 SSH_SIG(PIPE);
1891 SSH_SIG(QUIT);
1892 SSH_SIG(SEGV);
1893 SSH_SIG(TERM);
1894 SSH_SIG(USR1);
1895 SSH_SIG(USR2);
1896#undef SSH_SIG
1897 return "SIG@openssh.com";
1898}
1899
Ben Lindstrombba81212001-06-25 05:01:22 +00001900static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001901session_exit_message(Session *s, int status)
1902{
1903 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001904
1905 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001906 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001907 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001908 debug("session_exit_message: session %d channel %d pid %ld",
1909 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001910
1911 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001912 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001913 packet_put_int(WEXITSTATUS(status));
1914 packet_send();
1915 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001916 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10001917 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001918#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001919 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001920#else /* WCOREDUMP */
1921 packet_put_char(0);
1922#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001923 packet_put_cstring("");
1924 packet_put_cstring("");
1925 packet_send();
1926 } else {
1927 /* Some weird exit cause. Just exit. */
1928 packet_disconnect("wait returned status %04x.", status);
1929 }
1930
1931 /* disconnect channel */
1932 debug("session_exit_message: release channel %d", s->chanid);
1933 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001934 /*
1935 * emulate a write failure with 'chan_write_failed', nobody will be
1936 * interested in data we write.
1937 * Note that we must not call 'chan_read_failed', since there could
1938 * be some more data waiting in the pipe.
1939 */
Damien Millerbd483e72000-04-30 10:00:53 +10001940 if (c->ostate != CHAN_OUTPUT_CLOSED)
1941 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001942 s->chanid = -1;
1943}
1944
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001945void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001946session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001947{
Ben Lindstromce0f6342002-06-11 16:42:49 +00001948 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001949 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001950 session_pty_cleanup(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001951 if (s->term)
1952 xfree(s->term);
1953 if (s->display)
1954 xfree(s->display);
Damien Miller512bccb2002-02-05 12:11:02 +11001955 if (s->auth_display)
1956 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10001957 if (s->auth_data)
1958 xfree(s->auth_data);
1959 if (s->auth_proto)
1960 xfree(s->auth_proto);
1961 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001962 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001963}
1964
1965void
1966session_close_by_pid(pid_t pid, int status)
1967{
1968 Session *s = session_by_pid(pid);
1969 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001970 debug("session_close_by_pid: no session for pid %ld",
1971 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001972 return;
1973 }
1974 if (s->chanid != -1)
1975 session_exit_message(s, status);
1976 session_close(s);
1977}
1978
1979/*
1980 * this is called when a channel dies before
1981 * the session 'child' itself dies
1982 */
1983void
1984session_close_by_channel(int id, void *arg)
1985{
1986 Session *s = session_by_channel(id);
1987 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001988 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001989 return;
1990 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001991 debug("session_close_by_channel: channel %d child %ld",
1992 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10001993 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10001994 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10001995 /*
1996 * delay detach of session, but release pty, since
1997 * the fd's to the child are already closed
1998 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10001999 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002000 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002001 return;
2002 }
2003 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002004 channel_cancel_cleanup(s->chanid);
2005 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002006 session_close(s);
2007}
2008
2009void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002010session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002011{
2012 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002013 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002014 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002015 if (s->used) {
2016 if (closefunc != NULL)
2017 closefunc(s);
2018 else
2019 session_close(s);
2020 }
Damien Miller52b77be2001-10-10 15:14:37 +10002021 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002022}
2023
Ben Lindstrombba81212001-06-25 05:01:22 +00002024static char *
Damien Millere247cc42000-05-07 12:03:14 +10002025session_tty_list(void)
2026{
2027 static char buf[1024];
2028 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002029 char *cp;
2030
Damien Millere247cc42000-05-07 12:03:14 +10002031 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002032 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002033 Session *s = &sessions[i];
2034 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002035
Damien Millera8ed44b2003-01-10 09:53:12 +11002036 if (strncmp(s->tty, "/dev/", 5) != 0) {
2037 cp = strrchr(s->tty, '/');
2038 cp = (cp == NULL) ? s->tty : cp + 1;
2039 } else
2040 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002041
Damien Millere247cc42000-05-07 12:03:14 +10002042 if (buf[0] != '\0')
2043 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002044 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002045 }
2046 }
2047 if (buf[0] == '\0')
2048 strlcpy(buf, "notty", sizeof buf);
2049 return buf;
2050}
2051
2052void
2053session_proctitle(Session *s)
2054{
2055 if (s->pw == NULL)
2056 error("no user for session %d", s->self);
2057 else
2058 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2059}
2060
Ben Lindstrom768176b2001-06-09 01:29:12 +00002061int
2062session_setup_x11fwd(Session *s)
2063{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002064 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002065 char display[512], auth_display[512];
2066 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00002067
2068 if (no_x11_forwarding_flag) {
2069 packet_send_debug("X11 forwarding disabled in user configuration file.");
2070 return 0;
2071 }
2072 if (!options.x11_forwarding) {
2073 debug("X11 forwarding disabled in server configuration file.");
2074 return 0;
2075 }
2076 if (!options.xauth_location ||
2077 (stat(options.xauth_location, &st) == -1)) {
2078 packet_send_debug("No xauth program; cannot forward with spoofing.");
2079 return 0;
2080 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002081 if (options.use_login) {
2082 packet_send_debug("X11 forwarding disabled; "
2083 "not compatible with UseLogin=yes.");
2084 return 0;
2085 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002086 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002087 debug("X11 display already set.");
2088 return 0;
2089 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002090 if (x11_create_display_inet(options.x11_display_offset,
2091 options.x11_use_localhost, s->single_connection,
2092 &s->display_number) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002093 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002094 return 0;
2095 }
Kevin Steves366298c2001-12-19 17:58:01 +00002096
2097 /* Set up a suitable value for the DISPLAY variable. */
2098 if (gethostname(hostname, sizeof(hostname)) < 0)
2099 fatal("gethostname: %.100s", strerror(errno));
2100 /*
2101 * auth_display must be used as the displayname when the
2102 * authorization entry is added with xauth(1). This will be
2103 * different than the DISPLAY string for localhost displays.
2104 */
Damien Miller95c249f2002-02-05 12:11:34 +11002105 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002106 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002107 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002108 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002109 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002110 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002111 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002112 } else {
2113#ifdef IPADDR_IN_DISPLAY
2114 struct hostent *he;
2115 struct in_addr my_addr;
2116
2117 he = gethostbyname(hostname);
2118 if (he == NULL) {
2119 error("Can't get IP address for X11 DISPLAY.");
2120 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2121 return 0;
2122 }
2123 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002124 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002125 s->display_number, s->screen);
2126#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002127 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002128 s->display_number, s->screen);
2129#endif
2130 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002131 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002132 }
2133
Ben Lindstrom768176b2001-06-09 01:29:12 +00002134 return 1;
2135}
2136
Ben Lindstrombba81212001-06-25 05:01:22 +00002137static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002138do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002139{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002140 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002141}
2142
2143void
2144do_cleanup(Authctxt *authctxt)
2145{
2146 static int called = 0;
2147
2148 debug("do_cleanup");
2149
2150 /* no cleanup if we're in the child for login shell */
2151 if (is_child)
2152 return;
2153
2154 /* avoid double cleanup */
2155 if (called)
2156 return;
2157 called = 1;
2158
2159 if (authctxt == NULL)
2160 return;
2161#ifdef KRB5
2162 if (options.kerberos_ticket_cleanup &&
2163 authctxt->krb5_ctx)
2164 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002165#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002166
2167#ifdef GSSAPI
2168 if (compat20 && options.gss_cleanup_creds)
2169 ssh_gssapi_cleanup_creds();
2170#endif
2171
Darren Tucker8846a072003-10-07 11:30:15 +10002172#ifdef USE_PAM
2173 if (options.use_pam) {
2174 sshpam_cleanup();
2175 sshpam_thread_cleanup();
2176 }
2177#endif
2178
Darren Tucker3e33cec2003-10-02 16:12:36 +10002179 /* remove agent socket */
2180 auth_sock_cleanup_proc(authctxt->pw);
2181
2182 /*
2183 * Cleanup ptys/utmp only if privsep is disabled,
2184 * or if running in monitor.
2185 */
2186 if (!use_privsep || mm_is_monitor())
2187 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002188}