blob: d3ef89a0c95d9da64146fdfed07d12cab4b31a6b [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
Damien Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000012 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Darren Tucker3bdbd842003-08-13 20:31:05 +100036RCSID("$OpenBSD: session.c,v 1.160 2003/08/13 08:33:02 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000042#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "packet.h"
44#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100045#include "mpaux.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "uidswap.h"
47#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000048#include "channels.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100051#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
53#include "log.h"
54#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000055#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "serverloop.h"
57#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000058#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000059#include "monitor_wrap.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060
Damien Millerb38eff82000-04-01 11:09:21 +100061/* func */
62
63Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000064void session_set_fds(Session *, int, int, int);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000065void session_pty_cleanup(void *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000066void session_proctitle(Session *);
67int session_setup_x11fwd(Session *);
68void do_exec_pty(Session *, const char *);
69void do_exec_no_pty(Session *, const char *);
70void do_exec(Session *, const char *);
71void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +000072#ifdef LOGIN_NEEDS_UTMPX
73static void do_pre_login(Session *s);
74#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +000075void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +100076void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000077int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +100078
Ben Lindstrombba81212001-06-25 05:01:22 +000079static void do_authenticated1(Authctxt *);
80static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000081
Ben Lindstrombba81212001-06-25 05:01:22 +000082static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +000083
Damien Millerb38eff82000-04-01 11:09:21 +100084/* import */
85extern ServerOptions options;
86extern char *__progname;
87extern int log_stderr;
88extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +000089extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +100090extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +000091extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +100092extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +100093
Damien Miller7b28dc52000-09-05 13:34:53 +110094/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +000095const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +110096
Damien Millerb38eff82000-04-01 11:09:21 +100097/* data */
98#define MAX_SESSIONS 10
99Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100100
Damien Millerad833b32000-08-23 10:46:23 +1000101#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000102login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000103#endif
104
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000105/* Name and directory of socket for authentication agent forwarding. */
106static char *auth_sock_name = NULL;
107static char *auth_sock_dir = NULL;
108
109/* removes the agent forwarding socket */
110
111static void
112auth_sock_cleanup_proc(void *_pw)
113{
114 struct passwd *pw = _pw;
115
116 if (auth_sock_name != NULL) {
117 temporarily_use_uid(pw);
118 unlink(auth_sock_name);
119 rmdir(auth_sock_dir);
120 auth_sock_name = NULL;
121 restore_uid();
122 }
123}
124
125static int
126auth_input_request_forwarding(struct passwd * pw)
127{
128 Channel *nc;
129 int sock;
130 struct sockaddr_un sunaddr;
131
132 if (auth_sock_name != NULL) {
133 error("authentication forwarding requested twice.");
134 return 0;
135 }
136
137 /* Temporarily drop privileged uid for mkdir/bind. */
138 temporarily_use_uid(pw);
139
140 /* Allocate a buffer for the socket name, and format the name. */
141 auth_sock_name = xmalloc(MAXPATHLEN);
142 auth_sock_dir = xmalloc(MAXPATHLEN);
143 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
144
145 /* Create private directory for socket */
146 if (mkdtemp(auth_sock_dir) == NULL) {
147 packet_send_debug("Agent forwarding disabled: "
148 "mkdtemp() failed: %.100s", strerror(errno));
149 restore_uid();
150 xfree(auth_sock_name);
151 xfree(auth_sock_dir);
152 auth_sock_name = NULL;
153 auth_sock_dir = NULL;
154 return 0;
155 }
Ben Lindstromce0f6342002-06-11 16:42:49 +0000156 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
157 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000158
159 /* delete agent socket on fatal() */
160 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
161
162 /* Create the socket. */
163 sock = socket(AF_UNIX, SOCK_STREAM, 0);
164 if (sock < 0)
165 packet_disconnect("socket: %.100s", strerror(errno));
166
167 /* Bind it to the name. */
168 memset(&sunaddr, 0, sizeof(sunaddr));
169 sunaddr.sun_family = AF_UNIX;
170 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
171
172 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
173 packet_disconnect("bind: %.100s", strerror(errno));
174
175 /* Restore the privileged uid. */
176 restore_uid();
177
178 /* Start listening on the socket. */
179 if (listen(sock, 5) < 0)
180 packet_disconnect("listen: %.100s", strerror(errno));
181
182 /* Allocate a channel for the authentication agent socket. */
183 nc = channel_new("auth socket",
184 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
185 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000186 0, "auth socket", 1);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000187 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
188 return 1;
189}
190
191
Ben Lindstromb31783d2001-03-22 02:02:12 +0000192void
193do_authenticated(Authctxt *authctxt)
194{
Damien Miller97f39ae2003-02-24 11:57:01 +1100195 setproctitle("%s", authctxt->pw->pw_name);
196
Ben Lindstromb31783d2001-03-22 02:02:12 +0000197 /*
198 * Cancel the alarm we set to limit the time taken for
199 * authentication.
200 */
201 alarm(0);
202 if (startup_pipe != -1) {
203 close(startup_pipe);
204 startup_pipe = -1;
205 }
Damien Millere49d0962001-11-13 23:46:18 +1100206
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
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000216 /* remove agent socket */
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000217 if (auth_sock_name != NULL)
Ben Lindstrom983c0982001-06-09 01:20:06 +0000218 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000219#ifdef KRB5
220 if (options.kerberos_ticket_cleanup)
221 krb5_cleanup_proc(authctxt);
222#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000223}
224
Damien Millerb38eff82000-04-01 11:09:21 +1000225/*
Damien Millerb38eff82000-04-01 11:09:21 +1000226 * Prepares for an interactive session. This is called after the user has
227 * been successfully authenticated. During this message exchange, pseudo
228 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
229 * are requested, etc.
230 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000231static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000232do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000233{
234 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000235 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100236 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000237 int enable_compression_after_reply = 0;
238 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000239
240 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000241 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000242 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000243
Damien Millerb38eff82000-04-01 11:09:21 +1000244 /*
245 * We stay in this loop until the client requests to execute a shell
246 * or a command.
247 */
248 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000249 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000250
251 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100252 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000253
254 /* Process the packet. */
255 switch (type) {
256 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000257 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100258 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000259 if (compression_level < 1 || compression_level > 9) {
260 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100261 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000262 break;
263 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000264 if (!options.compression) {
265 debug2("compression disabled");
266 break;
267 }
Damien Millerb38eff82000-04-01 11:09:21 +1000268 /* Enable compression after we have responded with SUCCESS. */
269 enable_compression_after_reply = 1;
270 success = 1;
271 break;
272
273 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000274 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000275 break;
276
277 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000278 s->auth_proto = packet_get_string(&proto_len);
279 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000280
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000281 screen_flag = packet_get_protocol_flags() &
282 SSH_PROTOFLAG_SCREEN_NUMBER;
283 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
284
285 if (packet_remaining() == 4) {
286 if (!screen_flag)
287 debug2("Buggy client: "
288 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000289 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000290 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000291 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000292 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100293 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000294 success = session_setup_x11fwd(s);
295 if (!success) {
296 xfree(s->auth_proto);
297 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000298 s->auth_proto = NULL;
299 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000300 }
Damien Millerb38eff82000-04-01 11:09:21 +1000301 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000302
303 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
304 if (no_agent_forwarding_flag || compat13) {
305 debug("Authentication agent forwarding not permitted for this authentication.");
306 break;
307 }
308 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000309 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000310 break;
311
312 case SSH_CMSG_PORT_FORWARD_REQUEST:
313 if (no_port_forwarding_flag) {
314 debug("Port forwarding not permitted for this authentication.");
315 break;
316 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100317 if (!options.allow_tcp_forwarding) {
318 debug("Port forwarding not permitted.");
319 break;
320 }
Damien Millerb38eff82000-04-01 11:09:21 +1000321 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000322 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000323 success = 1;
324 break;
325
326 case SSH_CMSG_MAX_PACKET_SIZE:
327 if (packet_set_maxsize(packet_get_int()) > 0)
328 success = 1;
329 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100330
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000331#ifdef KRB5
Ben Lindstromec95ed92001-07-04 04:21:14 +0000332 case SSH_CMSG_HAVE_KERBEROS_TGT:
333 if (!options.kerberos_tgt_passing) {
334 verbose("Kerberos TGT passing disabled.");
335 } else {
336 char *kdata = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100337 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100338
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000339 /* XXX - 0x41, used for AFS */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000340 if (kdata[0] != 0x41) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000341 krb5_data tgt;
342 tgt.data = kdata;
343 tgt.length = dlen;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100344
Ben Lindstromec95ed92001-07-04 04:21:14 +0000345 if (auth_krb5_tgt(s->authctxt, &tgt))
346 success = 1;
347 else
348 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000349 }
350 xfree(kdata);
351 }
352 break;
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000353#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000354
355 case SSH_CMSG_EXEC_SHELL:
356 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000357 if (type == SSH_CMSG_EXEC_CMD) {
358 command = packet_get_string(&dlen);
359 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000360 do_exec(s, command);
361 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000362 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000363 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000364 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100365 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000366 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000367 return;
368
369 default:
370 /*
371 * Any unknown messages in this phase are ignored,
372 * and a failure message is returned.
373 */
Damien Miller996acd22003-04-09 20:59:48 +1000374 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000375 }
376 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
377 packet_send();
378 packet_write_wait();
379
380 /* Enable compression now that we have replied if appropriate. */
381 if (enable_compression_after_reply) {
382 enable_compression_after_reply = 0;
383 packet_start_compression(compression_level);
384 }
385 }
386}
387
388/*
389 * This is called to fork and execute a command when we have no tty. This
390 * will call do_child from the child, and server_loop from the parent after
391 * setting up file descriptors and such.
392 */
Damien Miller4af51302000-04-16 11:18:38 +1000393void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000394do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000395{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000396 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000397
398#ifdef USE_PIPES
399 int pin[2], pout[2], perr[2];
400 /* Allocate pipes for communicating with the program. */
401 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
402 packet_disconnect("Could not create pipes: %.100s",
403 strerror(errno));
404#else /* USE_PIPES */
405 int inout[2], err[2];
406 /* Uses socket pairs to communicate with the program. */
407 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
408 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
409 packet_disconnect("Could not create socket pairs: %.100s",
410 strerror(errno));
411#endif /* USE_PIPES */
412 if (s == NULL)
413 fatal("do_exec_no_pty: no session");
414
Damien Millere247cc42000-05-07 12:03:14 +1000415 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000416
Damien Millerc5946332001-02-28 11:46:11 +1100417#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000418 if (options.use_pam) {
419 do_pam_session(s->pw->pw_name, NULL);
420 do_pam_setcred(1);
421 if (is_pam_password_change_required())
422 packet_disconnect("Password change required but no "
423 "TTY available");
424 }
Kevin Stevesff793a22001-02-21 16:36:51 +0000425#endif /* USE_PAM */
426
Damien Millerb38eff82000-04-01 11:09:21 +1000427 /* Fork the child. */
428 if ((pid = fork()) == 0) {
Ben Lindstrom264ee302002-07-23 21:01:56 +0000429 fatal_remove_all_cleanups();
430
Damien Millerb38eff82000-04-01 11:09:21 +1000431 /* Child. Reinitialize the log since the pid has changed. */
432 log_init(__progname, options.log_level, options.log_facility, log_stderr);
433
434 /*
435 * Create a new session and process group since the 4.4BSD
436 * setlogin() affects the entire process group.
437 */
438 if (setsid() < 0)
439 error("setsid failed: %.100s", strerror(errno));
440
441#ifdef USE_PIPES
442 /*
443 * Redirect stdin. We close the parent side of the socket
444 * pair, and make the child side the standard input.
445 */
446 close(pin[1]);
447 if (dup2(pin[0], 0) < 0)
448 perror("dup2 stdin");
449 close(pin[0]);
450
451 /* Redirect stdout. */
452 close(pout[0]);
453 if (dup2(pout[1], 1) < 0)
454 perror("dup2 stdout");
455 close(pout[1]);
456
457 /* Redirect stderr. */
458 close(perr[0]);
459 if (dup2(perr[1], 2) < 0)
460 perror("dup2 stderr");
461 close(perr[1]);
462#else /* USE_PIPES */
463 /*
464 * Redirect stdin, stdout, and stderr. Stdin and stdout will
465 * use the same socket, as some programs (particularly rdist)
466 * seem to depend on it.
467 */
468 close(inout[1]);
469 close(err[1]);
470 if (dup2(inout[0], 0) < 0) /* stdin */
471 perror("dup2 stdin");
472 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
473 perror("dup2 stdout");
474 if (dup2(err[0], 2) < 0) /* stderr */
475 perror("dup2 stderr");
476#endif /* USE_PIPES */
477
Tim Rice81ed5182002-09-25 17:38:46 -0700478#ifdef _UNICOS
479 cray_init_job(s->pw); /* set up cray jid and tmpdir */
480#endif
481
Damien Millerb38eff82000-04-01 11:09:21 +1000482 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000483 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000484 /* NOTREACHED */
485 }
Tim Rice81ed5182002-09-25 17:38:46 -0700486#ifdef _UNICOS
487 signal(WJSIGNAL, cray_job_termination_handler);
488#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100489#ifdef HAVE_CYGWIN
490 if (is_winnt)
491 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
492#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000493 if (pid < 0)
494 packet_disconnect("fork failed: %.100s", strerror(errno));
495 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000496 /* Set interactive/non-interactive mode. */
497 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000498#ifdef USE_PIPES
499 /* We are the parent. Close the child sides of the pipes. */
500 close(pin[0]);
501 close(pout[1]);
502 close(perr[1]);
503
Damien Millerefb4afe2000-04-12 18:45:05 +1000504 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000505 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000506 } else {
507 /* Enter the interactive session. */
508 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000509 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000510 }
Damien Millerb38eff82000-04-01 11:09:21 +1000511#else /* USE_PIPES */
512 /* We are the parent. Close the child sides of the socket pairs. */
513 close(inout[0]);
514 close(err[0]);
515
516 /*
517 * Enter the interactive session. Note: server_loop must be able to
518 * handle the case that fdin and fdout are the same.
519 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000520 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000521 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000522 } else {
523 server_loop(pid, inout[1], inout[1], err[1]);
524 /* server_loop has closed inout[1] and err[1]. */
525 }
Damien Millerb38eff82000-04-01 11:09:21 +1000526#endif /* USE_PIPES */
527}
528
529/*
530 * This is called to fork and execute a command when we have a tty. This
531 * will call do_child from the child, and server_loop from the parent after
532 * setting up file descriptors, controlling tty, updating wtmp, utmp,
533 * lastlog, and other such operations.
534 */
Damien Miller4af51302000-04-16 11:18:38 +1000535void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000536do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000537{
Damien Millerb38eff82000-04-01 11:09:21 +1000538 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000539 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000540
541 if (s == NULL)
542 fatal("do_exec_pty: no session");
543 ptyfd = s->ptyfd;
544 ttyfd = s->ttyfd;
545
Damien Millerc5946332001-02-28 11:46:11 +1100546#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000547 if (options.use_pam) {
548 do_pam_session(s->pw->pw_name, s->tty);
549 do_pam_setcred(1);
550 }
Damien Miller5a761312001-02-27 09:28:23 +1100551#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000552
Damien Millerb38eff82000-04-01 11:09:21 +1000553 /* Fork the child. */
554 if ((pid = fork()) == 0) {
Ben Lindstrom264ee302002-07-23 21:01:56 +0000555 fatal_remove_all_cleanups();
Ben Lindstrombddd5512001-07-04 04:53:53 +0000556
Damien Miller942da032000-08-18 13:59:06 +1000557 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000558 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000559 /* Close the master side of the pseudo tty. */
560 close(ptyfd);
561
562 /* Make the pseudo tty our controlling tty. */
563 pty_make_controlling_tty(&ttyfd, s->tty);
564
Damien Miller9c751422001-10-10 15:02:46 +1000565 /* Redirect stdin/stdout/stderr from the pseudo tty. */
566 if (dup2(ttyfd, 0) < 0)
567 error("dup2 stdin: %s", strerror(errno));
568 if (dup2(ttyfd, 1) < 0)
569 error("dup2 stdout: %s", strerror(errno));
570 if (dup2(ttyfd, 2) < 0)
571 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000572
573 /* Close the extra descriptor for the pseudo tty. */
574 close(ttyfd);
575
Damien Miller942da032000-08-18 13:59:06 +1000576 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000577#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700578 if (!(options.use_login && command == NULL)) {
579#ifdef _UNICOS
580 cray_init_job(s->pw); /* set up cray jid and tmpdir */
581#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100582 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700583 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000584# ifdef LOGIN_NEEDS_UTMPX
585 else
586 do_pre_login(s);
587# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000588#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000589
Damien Millerb38eff82000-04-01 11:09:21 +1000590 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000591 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000592 /* NOTREACHED */
593 }
Tim Rice81ed5182002-09-25 17:38:46 -0700594#ifdef _UNICOS
595 signal(WJSIGNAL, cray_job_termination_handler);
596#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100597#ifdef HAVE_CYGWIN
598 if (is_winnt)
599 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
600#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000601 if (pid < 0)
602 packet_disconnect("fork failed: %.100s", strerror(errno));
603 s->pid = pid;
604
605 /* Parent. Close the slave side of the pseudo tty. */
606 close(ttyfd);
607
608 /*
609 * Create another descriptor of the pty master side for use as the
610 * standard input. We could use the original descriptor, but this
611 * simplifies code in server_loop. The descriptor is bidirectional.
612 */
613 fdout = dup(ptyfd);
614 if (fdout < 0)
615 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
616
617 /* we keep a reference to the pty master */
618 ptymaster = dup(ptyfd);
619 if (ptymaster < 0)
620 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
621 s->ptymaster = ptymaster;
622
623 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000624 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000625 if (compat20) {
626 session_set_fds(s, ptyfd, fdout, -1);
627 } else {
628 server_loop(pid, ptyfd, fdout, -1);
629 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000630 }
Damien Millerb38eff82000-04-01 11:09:21 +1000631}
632
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000633#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000634static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000635do_pre_login(Session *s)
636{
637 socklen_t fromlen;
638 struct sockaddr_storage from;
639 pid_t pid = getpid();
640
641 /*
642 * Get IP address of client. If the connection is not a socket, let
643 * the address be 0.0.0.0.
644 */
645 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000646 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000647 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000648 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100649 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000650 debug("getpeername: %.100s", strerror(errno));
651 fatal_cleanup();
652 }
653 }
654
655 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000656 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000657 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000658}
659#endif
660
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000661/*
662 * This is called to fork and execute a command. If another command is
663 * to be forced, execute that instead.
664 */
665void
666do_exec(Session *s, const char *command)
667{
668 if (forced_command) {
669 original_command = command;
670 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000671 debug("Forced command '%.900s'", command);
672 }
673
674 if (s->ttyfd != -1)
675 do_exec_pty(s, command);
676 else
677 do_exec_no_pty(s, command);
678
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000679 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000680}
681
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000682
Damien Miller942da032000-08-18 13:59:06 +1000683/* administrative, login(1)-like work */
684void
Damien Miller69b69aa2000-10-28 14:19:58 +1100685do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000686{
Damien Miller942da032000-08-18 13:59:06 +1000687 char *time_string;
Damien Miller942da032000-08-18 13:59:06 +1000688 socklen_t fromlen;
689 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000690 struct passwd * pw = s->pw;
691 pid_t pid = getpid();
692
693 /*
694 * Get IP address of client. If the connection is not a socket, let
695 * the address be 0.0.0.0.
696 */
697 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000698 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000699 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000700 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000701 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000702 debug("getpeername: %.100s", strerror(errno));
703 fatal_cleanup();
704 }
705 }
706
707 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000708 if (!use_privsep)
709 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
710 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000711 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000712 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000713
Kevin Steves092f2ef2000-10-14 13:36:13 +0000714#ifdef USE_PAM
715 /*
716 * If password change is needed, do it now.
717 * This needs to occur before the ~/.hushlogin check.
718 */
Damien Miller4e448a32003-05-14 15:11:48 +1000719 if (options.use_pam && is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000720 print_pam_messages();
721 do_pam_chauthtok();
722 }
723#endif
724
Damien Millercf205e82001-04-16 18:29:15 +1000725 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000726 return;
727
728#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000729 if (options.use_pam && !is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000730 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000731#endif /* USE_PAM */
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000732
733 /* display post-login message */
734 if (buffer_len(&loginmsg) > 0) {
735 buffer_append(&loginmsg, "\0", 1);
736 printf("%s\n", (char *)buffer_ptr(&loginmsg));
737 }
738 buffer_free(&loginmsg);
Damien Miller942da032000-08-18 13:59:06 +1000739
Tim Rice81ed5182002-09-25 17:38:46 -0700740#ifndef NO_SSH_LASTLOG
Ben Lindstromc447fee2002-04-02 20:35:35 +0000741 if (options.print_lastlog && s->last_login_time != 0) {
742 time_string = ctime(&s->last_login_time);
Damien Miller942da032000-08-18 13:59:06 +1000743 if (strchr(time_string, '\n'))
744 *strchr(time_string, '\n') = 0;
Ben Lindstromc447fee2002-04-02 20:35:35 +0000745 if (strcmp(s->hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000746 printf("Last login: %s\r\n", time_string);
747 else
Ben Lindstromc447fee2002-04-02 20:35:35 +0000748 printf("Last login: %s from %s\r\n", time_string,
749 s->hostname);
Damien Miller942da032000-08-18 13:59:06 +1000750 }
Tim Rice81ed5182002-09-25 17:38:46 -0700751#endif /* NO_SSH_LASTLOG */
Damien Millercf205e82001-04-16 18:29:15 +1000752
753 do_motd();
754}
755
756/*
757 * Display the message of the day.
758 */
759void
760do_motd(void)
761{
762 FILE *f;
763 char buf[256];
764
Damien Miller942da032000-08-18 13:59:06 +1000765 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000766#ifdef HAVE_LOGIN_CAP
767 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
768 "/etc/motd"), "r");
769#else
Damien Miller942da032000-08-18 13:59:06 +1000770 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000771#endif
Damien Miller942da032000-08-18 13:59:06 +1000772 if (f) {
773 while (fgets(buf, sizeof(buf), f))
774 fputs(buf, stdout);
775 fclose(f);
776 }
777 }
778}
779
Kevin Steves8f63caa2001-07-04 18:23:02 +0000780
781/*
782 * Check for quiet login, either .hushlogin or command given.
783 */
784int
785check_quietlogin(Session *s, const char *command)
786{
787 char buf[256];
788 struct passwd *pw = s->pw;
789 struct stat st;
790
791 /* Return 1 if .hushlogin exists or a command given. */
792 if (command != NULL)
793 return 1;
794 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
795#ifdef HAVE_LOGIN_CAP
796 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
797 return 1;
798#else
799 if (stat(buf, &st) >= 0)
800 return 1;
801#endif
802 return 0;
803}
804
Damien Millerb38eff82000-04-01 11:09:21 +1000805/*
806 * Sets the value of the given variable in the environment. If the variable
807 * already exists, its value is overriden.
808 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000809static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000810child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100811 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000812{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000813 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000814 char **env;
815
816 /*
817 * Find the slot where the value should be stored. If the variable
818 * already exists, we reuse the slot; otherwise we append a new slot
819 * at the end of the array, expanding if necessary.
820 */
821 env = *envp;
822 namelen = strlen(name);
823 for (i = 0; env[i]; i++)
824 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
825 break;
826 if (env[i]) {
827 /* Reuse the slot. */
828 xfree(env[i]);
829 } else {
830 /* New variable. Expand if necessary. */
831 if (i >= (*envsizep) - 1) {
Damien Millera0796ca2002-06-26 19:15:07 +1000832 if (*envsizep >= 1000)
833 fatal("child_set_env: too many env vars,"
834 " skipping: %.100s", name);
Damien Millerb38eff82000-04-01 11:09:21 +1000835 (*envsizep) += 50;
836 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
837 }
838 /* Need to set the NULL pointer at end of array beyond the new slot. */
839 env[i + 1] = NULL;
840 }
841
842 /* Allocate space and format the variable in the appropriate slot. */
843 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
844 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
845}
846
847/*
848 * Reads environment variables from the given file and adds/overrides them
849 * into the environment. If the file does not exist, this does nothing.
850 * Otherwise, it must consist of empty lines, comments (line starts with '#')
851 * and assignments of the form name=value. No other forms are allowed.
852 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000853static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000854read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100855 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000856{
857 FILE *f;
858 char buf[4096];
859 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000860 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000861
862 f = fopen(filename, "r");
863 if (!f)
864 return;
865
866 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000867 if (++lineno > 1000)
868 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000869 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
870 ;
871 if (!*cp || *cp == '#' || *cp == '\n')
872 continue;
873 if (strchr(cp, '\n'))
874 *strchr(cp, '\n') = '\0';
875 value = strchr(cp, '=');
876 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000877 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
878 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000879 continue;
880 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000881 /*
882 * Replace the equals sign by nul, and advance value to
883 * the value string.
884 */
Damien Millerb38eff82000-04-01 11:09:21 +1000885 *value = '\0';
886 value++;
887 child_set_env(env, envsize, cp, value);
888 }
889 fclose(f);
890}
891
Damien Millerbb9ffc12002-01-08 10:59:32 +1100892void copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000893{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100894 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000895 int i;
896
Damien Millerbb9ffc12002-01-08 10:59:32 +1100897 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000898 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000899
Damien Millerbb9ffc12002-01-08 10:59:32 +1100900 for(i = 0; source[i] != NULL; i++) {
901 var_name = xstrdup(source[i]);
902 if ((var_val = strstr(var_name, "=")) == NULL) {
903 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000904 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000905 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100906 *var_val++ = '\0';
907
908 debug3("Copy environment: %s=%s", var_name, var_val);
909 child_set_env(env, envsize, var_name, var_val);
910
911 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000912 }
913}
Damien Millercb5e44a2000-09-29 12:12:36 +1100914
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000915static char **
916do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000917{
Damien Millerb38eff82000-04-01 11:09:21 +1000918 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000919 u_int i, envsize;
Damien Miller00111382003-03-10 11:21:17 +1100920 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000921 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000922
Damien Millerb38eff82000-04-01 11:09:21 +1000923 /* Initialize the environment. */
924 envsize = 100;
925 env = xmalloc(envsize * sizeof(char *));
926 env[0] = NULL;
927
Damien Millerbac2d8a2000-09-05 16:13:06 +1100928#ifdef HAVE_CYGWIN
929 /*
930 * The Windows environment contains some setting which are
931 * important for a running system. They must not be dropped.
932 */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100933 copy_environment(environ, &env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100934#endif
935
Damien Millerb38eff82000-04-01 11:09:21 +1000936 if (!options.use_login) {
937 /* Set basic environment. */
938 child_set_env(&env, &envsize, "USER", pw->pw_name);
939 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +1100940#ifdef _AIX
941 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
942#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000943 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000944#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +0000945 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
946 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
947 else
948 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +1100949#else /* HAVE_LOGIN_CAP */
950# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +1100951 /*
952 * There's no standard path on Windows. The path contains
953 * important components pointing to the system directories,
954 * needed for loading shared libraries. So the path better
955 * remains intact here.
956 */
Damien Millera18bbd32002-05-13 10:48:57 +1000957# ifdef SUPERUSER_PATH
958 child_set_env(&env, &envsize, "PATH",
959 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
960# else
Damien Millerb38eff82000-04-01 11:09:21 +1000961 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millera18bbd32002-05-13 10:48:57 +1000962# endif /* SUPERUSER_PATH */
Damien Millercb5e44a2000-09-29 12:12:36 +1100963# endif /* HAVE_CYGWIN */
964#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +1000965
966 snprintf(buf, sizeof buf, "%.200s/%.50s",
967 _PATH_MAILDIR, pw->pw_name);
968 child_set_env(&env, &envsize, "MAIL", buf);
969
970 /* Normal systems set SHELL by default. */
971 child_set_env(&env, &envsize, "SHELL", shell);
972 }
973 if (getenv("TZ"))
974 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
975
976 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +0000977 if (!options.use_login) {
978 while (custom_environment) {
979 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000980 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000981
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000982 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +0000983 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000984 if (str[i] == '=') {
985 str[i] = 0;
986 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +0000987 }
988 custom_environment = ce->next;
989 xfree(ce->s);
990 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +1000991 }
Damien Millerb38eff82000-04-01 11:09:21 +1000992 }
993
Damien Millerf37e2462002-09-19 11:47:55 +1000994 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +1000995 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000996 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +1000997 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
998
Damien Miller00111382003-03-10 11:21:17 +1100999 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001000 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001001 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1002 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001003 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1004
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001005 if (s->ttyfd != -1)
1006 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1007 if (s->term)
1008 child_set_env(&env, &envsize, "TERM", s->term);
1009 if (s->display)
1010 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001011 if (original_command)
1012 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1013 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001014
Tim Rice81ed5182002-09-25 17:38:46 -07001015#ifdef _UNICOS
1016 if (cray_tmpdir[0] != '\0')
1017 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1018#endif /* _UNICOS */
1019
Damien Millerb38eff82000-04-01 11:09:21 +10001020#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001021 {
1022 char *cp;
1023
1024 if ((cp = getenv("AUTHSTATE")) != NULL)
1025 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1026 if ((cp = getenv("KRB5CCNAME")) != NULL)
1027 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1028 read_environment_file(&env, &envsize, "/etc/environment");
1029 }
Damien Millerb38eff82000-04-01 11:09:21 +10001030#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001031#ifdef KRB5
1032 if (s->authctxt->krb5_ticket_file)
1033 child_set_env(&env, &envsize, "KRB5CCNAME",
1034 s->authctxt->krb5_ticket_file);
1035#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001036#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001037 /*
1038 * Pull in any environment variables that may have
1039 * been set by PAM.
1040 */
Damien Miller4e448a32003-05-14 15:11:48 +10001041 if (options.use_pam) {
1042 char **p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001043
Kevin Steves38b050a2002-07-23 00:44:07 +00001044 copy_environment(p, &env, &envsize);
1045 free_pam_environment(p);
1046 }
Damien Millerb38eff82000-04-01 11:09:21 +10001047#endif /* USE_PAM */
1048
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001049 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001050 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001051 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001052
1053 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001054 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001055 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001056 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001057 read_environment_file(&env, &envsize, buf);
1058 }
1059 if (debug_flag) {
1060 /* dump the environment */
1061 fprintf(stderr, "Environment:\n");
1062 for (i = 0; env[i]; i++)
1063 fprintf(stderr, " %.200s\n", env[i]);
1064 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001065 return env;
1066}
1067
1068/*
1069 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1070 * first in this order).
1071 */
1072static void
1073do_rc_files(Session *s, const char *shell)
1074{
1075 FILE *f = NULL;
1076 char cmd[1024];
1077 int do_xauth;
1078 struct stat st;
1079
1080 do_xauth =
1081 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1082
1083 /* ignore _PATH_SSH_USER_RC for subsystems */
1084 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1085 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1086 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1087 if (debug_flag)
1088 fprintf(stderr, "Running %s\n", cmd);
1089 f = popen(cmd, "w");
1090 if (f) {
1091 if (do_xauth)
1092 fprintf(f, "%s %s\n", s->auth_proto,
1093 s->auth_data);
1094 pclose(f);
1095 } else
1096 fprintf(stderr, "Could not run %s\n",
1097 _PATH_SSH_USER_RC);
1098 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1099 if (debug_flag)
1100 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1101 _PATH_SSH_SYSTEM_RC);
1102 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1103 if (f) {
1104 if (do_xauth)
1105 fprintf(f, "%s %s\n", s->auth_proto,
1106 s->auth_data);
1107 pclose(f);
1108 } else
1109 fprintf(stderr, "Could not run %s\n",
1110 _PATH_SSH_SYSTEM_RC);
1111 } else if (do_xauth && options.xauth_location != NULL) {
1112 /* Add authority data to .Xauthority if appropriate. */
1113 if (debug_flag) {
1114 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001115 "Running %.500s remove %.100s\n",
1116 options.xauth_location, s->auth_display);
1117 fprintf(stderr,
1118 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001119 options.xauth_location, s->auth_display,
1120 s->auth_proto, s->auth_data);
1121 }
1122 snprintf(cmd, sizeof cmd, "%s -q -",
1123 options.xauth_location);
1124 f = popen(cmd, "w");
1125 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001126 fprintf(f, "remove %s\n",
1127 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001128 fprintf(f, "add %s %s %s\n",
1129 s->auth_display, s->auth_proto,
1130 s->auth_data);
1131 pclose(f);
1132 } else {
1133 fprintf(stderr, "Could not run %s\n",
1134 cmd);
1135 }
1136 }
1137}
1138
1139static void
1140do_nologin(struct passwd *pw)
1141{
1142 FILE *f = NULL;
1143 char buf[1024];
1144
1145#ifdef HAVE_LOGIN_CAP
1146 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1147 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1148 _PATH_NOLOGIN), "r");
1149#else
1150 if (pw->pw_uid)
1151 f = fopen(_PATH_NOLOGIN, "r");
1152#endif
1153 if (f) {
1154 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001155 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001156 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001157 while (fgets(buf, sizeof(buf), f))
1158 fputs(buf, stderr);
1159 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001160 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001161 exit(254);
1162 }
1163}
1164
1165/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001167do_setusercontext(struct passwd *pw)
1168{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001169#ifndef HAVE_CYGWIN
1170 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001171#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001172 {
1173
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001174#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001175 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1176 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001177#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001178#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001179# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001180 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001181# endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001182 if (setusercontext(lc, pw, pw->pw_uid,
1183 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1184 perror("unable to set user context");
1185 exit(1);
1186 }
1187#else
1188# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1189 /* Sets login uid for accounting */
1190 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1191 error("setluid: %s", strerror(errno));
1192# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1193
1194 if (setlogin(pw->pw_name) < 0)
1195 error("setlogin failed: %s", strerror(errno));
1196 if (setgid(pw->pw_gid) < 0) {
1197 perror("setgid");
1198 exit(1);
1199 }
1200 /* Initialize the group list. */
1201 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1202 perror("initgroups");
1203 exit(1);
1204 }
1205 endgrent();
1206# ifdef USE_PAM
1207 /*
1208 * PAM credentials may take the form of supplementary groups.
1209 * These will have been wiped by the above initgroups() call.
1210 * Reestablish them here.
1211 */
Damien Miller4e448a32003-05-14 15:11:48 +10001212 if (options.use_pam)
1213 do_pam_setcred(0);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001214# endif /* USE_PAM */
1215# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1216 irix_setusercontext(pw);
1217# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001218# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001219 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001220# endif /* _AIX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001221 /* Permanently switch to the desired uid. */
1222 permanently_set_uid(pw);
1223#endif
1224 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001225
1226#ifdef HAVE_CYGWIN
1227 if (is_winnt)
1228#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001229 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1230 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1231}
1232
Ben Lindstrom08105192002-03-22 02:50:06 +00001233static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234launch_login(struct passwd *pw, const char *hostname)
1235{
1236 /* Launch login(1). */
1237
Ben Lindstrom378a4172002-06-07 14:49:56 +00001238 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001239#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001240 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001242#ifdef LOGIN_NO_ENDOPT
1243 "-p", "-f", pw->pw_name, (char *)NULL);
1244#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001246#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247
1248 /* Login couldn't be executed, die. */
1249
1250 perror("login");
1251 exit(1);
1252}
1253
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001254/*
1255 * Performs common processing for the child, such as setting up the
1256 * environment, closing extra file descriptors, setting the user and group
1257 * ids, and executing the command or shell.
1258 */
1259void
1260do_child(Session *s, const char *command)
1261{
1262 extern char **environ;
1263 char **env;
1264 char *argv[10];
1265 const char *shell, *shell0, *hostname = NULL;
1266 struct passwd *pw = s->pw;
1267 u_int i;
1268
1269 /* remove hostkey from the child's memory */
1270 destroy_sensitive_data();
1271
1272 /* login(1) is only called if we execute the login shell */
1273 if (options.use_login && command != NULL)
1274 options.use_login = 0;
1275
Tim Rice81ed5182002-09-25 17:38:46 -07001276#ifdef _UNICOS
1277 cray_setup(pw->pw_uid, pw->pw_name, command);
1278#endif /* _UNICOS */
1279
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001280 /*
1281 * Login(1) does this as well, and it needs uid 0 for the "-h"
1282 * switch, so we let login(1) to this for us.
1283 */
1284 if (!options.use_login) {
1285#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001286 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001287 if (!check_quietlogin(s, command))
1288 do_motd();
1289#else /* HAVE_OSF_SIA */
1290 do_nologin(pw);
1291 do_setusercontext(pw);
1292#endif /* HAVE_OSF_SIA */
1293 }
1294
1295 /*
1296 * Get the shell from the password data. An empty shell field is
1297 * legal, and means /bin/sh.
1298 */
1299 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001300
1301 /*
1302 * Make sure $SHELL points to the shell from the password file,
1303 * even if shell is overridden from login.conf
1304 */
1305 env = do_setup_env(s, shell);
1306
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001307#ifdef HAVE_LOGIN_CAP
1308 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1309#endif
1310
Damien Millerad833b32000-08-23 10:46:23 +10001311 /* we have to stash the hostname before we close our socket. */
1312 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001313 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001314 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001315 /*
1316 * Close the connection descriptors; note that this is the child, and
1317 * the server will still have the socket open, and it is important
1318 * that we do not shutdown it. Note that the descriptors cannot be
1319 * closed before building the environment, as we call
1320 * get_remote_ipaddr there.
1321 */
1322 if (packet_get_connection_in() == packet_get_connection_out())
1323 close(packet_get_connection_in());
1324 else {
1325 close(packet_get_connection_in());
1326 close(packet_get_connection_out());
1327 }
1328 /*
1329 * Close all descriptors related to channels. They will still remain
1330 * open in the parent.
1331 */
1332 /* XXX better use close-on-exec? -markus */
1333 channel_close_all();
1334
1335 /*
1336 * Close any extra file descriptors. Note that there may still be
1337 * descriptors left by system functions. They will be closed later.
1338 */
1339 endpwent();
1340
1341 /*
1342 * Close any extra open file descriptors so that we don\'t have them
1343 * hanging around in clients. Note that we want to do this after
1344 * initgroups, because at least on Solaris 2.3 it leaves file
1345 * descriptors open.
1346 */
1347 for (i = 3; i < 64; i++)
1348 close(i);
1349
Damien Millerb38eff82000-04-01 11:09:21 +10001350 /*
Damien Miller05eda432002-02-10 18:32:28 +11001351 * Must take new environment into use so that .ssh/rc,
1352 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001353 */
1354 environ = env;
1355
Damien Miller139d4cd2001-10-10 15:07:44 +10001356 /* Change current directory to the user\'s home directory. */
1357 if (chdir(pw->pw_dir) < 0) {
1358 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1359 pw->pw_dir, strerror(errno));
1360#ifdef HAVE_LOGIN_CAP
1361 if (login_getcapbool(lc, "requirehome", 0))
1362 exit(1);
1363#endif
1364 }
1365
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001366 if (!options.use_login)
1367 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001368
1369 /* restore SIGPIPE for child */
1370 signal(SIGPIPE, SIG_DFL);
1371
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001372 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001373 launch_login(pw, hostname);
1374 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001375 }
1376
1377 /* Get the last component of the shell name. */
1378 if ((shell0 = strrchr(shell, '/')) != NULL)
1379 shell0++;
1380 else
1381 shell0 = shell;
1382
Damien Millerb38eff82000-04-01 11:09:21 +10001383 /*
1384 * If we have no command, execute the shell. In this case, the shell
1385 * name to be passed in argv[0] is preceded by '-' to indicate that
1386 * this is a login shell.
1387 */
1388 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001389 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001390
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001391 /* Start the shell. Set initial character to '-'. */
1392 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001393
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001394 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1395 >= sizeof(argv0) - 1) {
1396 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001397 perror(shell);
1398 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001399 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001400
1401 /* Execute the shell. */
1402 argv[0] = argv0;
1403 argv[1] = NULL;
1404 execve(shell, argv, env);
1405
1406 /* Executing the shell failed. */
1407 perror(shell);
1408 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001409 }
1410 /*
1411 * Execute the command using the user's shell. This uses the -c
1412 * option to execute the command.
1413 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001414 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001415 argv[1] = "-c";
1416 argv[2] = (char *) command;
1417 argv[3] = NULL;
1418 execve(shell, argv, env);
1419 perror(shell);
1420 exit(1);
1421}
1422
1423Session *
1424session_new(void)
1425{
1426 int i;
1427 static int did_init = 0;
1428 if (!did_init) {
1429 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001430 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001431 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001432 }
1433 did_init = 1;
1434 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001435 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001436 Session *s = &sessions[i];
1437 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001438 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001439 s->chanid = -1;
1440 s->ptyfd = -1;
1441 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001442 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001443 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001444 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001445 return s;
1446 }
1447 }
1448 return NULL;
1449}
1450
Ben Lindstrombba81212001-06-25 05:01:22 +00001451static void
Damien Millerb38eff82000-04-01 11:09:21 +10001452session_dump(void)
1453{
1454 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001455 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001456 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001457 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001458 s->used,
1459 s->self,
1460 s,
1461 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001462 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001463 }
1464}
1465
Damien Millerefb4afe2000-04-12 18:45:05 +10001466int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001467session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001468{
1469 Session *s = session_new();
1470 debug("session_open: channel %d", chanid);
1471 if (s == NULL) {
1472 error("no more sessions");
1473 return 0;
1474 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001475 s->authctxt = authctxt;
1476 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001477 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001478 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001479 debug("session_open: session %d: link with channel %d", s->self, chanid);
1480 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001481 return 1;
1482}
1483
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484Session *
1485session_by_tty(char *tty)
1486{
1487 int i;
1488 for (i = 0; i < MAX_SESSIONS; i++) {
1489 Session *s = &sessions[i];
1490 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1491 debug("session_by_tty: session %d tty %s", i, tty);
1492 return s;
1493 }
1494 }
1495 debug("session_by_tty: unknown tty %.100s", tty);
1496 session_dump();
1497 return NULL;
1498}
1499
Ben Lindstrombba81212001-06-25 05:01:22 +00001500static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001501session_by_channel(int id)
1502{
1503 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001504 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001505 Session *s = &sessions[i];
1506 if (s->used && s->chanid == id) {
1507 debug("session_by_channel: session %d channel %d", i, id);
1508 return s;
1509 }
1510 }
1511 debug("session_by_channel: unknown channel %d", id);
1512 session_dump();
1513 return NULL;
1514}
1515
Ben Lindstrombba81212001-06-25 05:01:22 +00001516static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001517session_by_pid(pid_t pid)
1518{
1519 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001520 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001521 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001522 Session *s = &sessions[i];
1523 if (s->used && s->pid == pid)
1524 return s;
1525 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001526 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001527 session_dump();
1528 return NULL;
1529}
1530
Ben Lindstrombba81212001-06-25 05:01:22 +00001531static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001532session_window_change_req(Session *s)
1533{
1534 s->col = packet_get_int();
1535 s->row = packet_get_int();
1536 s->xpixel = packet_get_int();
1537 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001538 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001539 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1540 return 1;
1541}
1542
Ben Lindstrombba81212001-06-25 05:01:22 +00001543static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001544session_pty_req(Session *s)
1545{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001546 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001547 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001548
Ben Lindstrom49c12602001-06-13 04:37:36 +00001549 if (no_pty_flag) {
1550 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001551 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001552 }
1553 if (s->ttyfd != -1) {
1554 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001555 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001556 }
Ben Lindstromc447fee2002-04-02 20:35:35 +00001557 /* Get the time and hostname when the user last logged in. */
1558 if (options.print_lastlog) {
1559 s->hostname[0] = '\0';
1560 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1561 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1562 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001563
Damien Millerefb4afe2000-04-12 18:45:05 +10001564 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001565
1566 if (compat20) {
1567 s->col = packet_get_int();
1568 s->row = packet_get_int();
1569 } else {
1570 s->row = packet_get_int();
1571 s->col = packet_get_int();
1572 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001573 s->xpixel = packet_get_int();
1574 s->ypixel = packet_get_int();
1575
1576 if (strcmp(s->term, "") == 0) {
1577 xfree(s->term);
1578 s->term = NULL;
1579 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001580
Damien Millerefb4afe2000-04-12 18:45:05 +10001581 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001582 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001583 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001584 if (s->term)
1585 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001586 s->term = NULL;
1587 s->ptyfd = -1;
1588 s->ttyfd = -1;
1589 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001590 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001591 }
1592 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001593
1594 /* for SSH1 the tty modes length is not given */
1595 if (!compat20)
1596 n_bytes = packet_remaining();
1597 tty_parse_modes(s->ttyfd, &n_bytes);
1598
Damien Millerefb4afe2000-04-12 18:45:05 +10001599 /*
1600 * Add a cleanup function to clear the utmp entry and record logout
1601 * time in case we call fatal() (e.g., the connection gets closed).
1602 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001603 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604 if (!use_privsep)
1605 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001606
1607 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001608 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1609
Damien Miller48b03fc2002-01-22 23:11:40 +11001610 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001611 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001612 return 1;
1613}
1614
Ben Lindstrombba81212001-06-25 05:01:22 +00001615static int
Damien Millerbd483e72000-04-30 10:00:53 +10001616session_subsystem_req(Session *s)
1617{
Damien Millerae452462001-10-10 15:08:06 +10001618 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001619 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001620 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001621 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001622 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001623
Damien Miller48b03fc2002-01-22 23:11:40 +11001624 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001625 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001626
Damien Millerf6d9e222000-06-18 14:50:44 +10001627 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001628 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1629 cmd = options.subsystem_command[i];
1630 if (stat(cmd, &st) < 0) {
1631 error("subsystem: cannot stat %s: %s", cmd,
1632 strerror(errno));
1633 break;
1634 }
1635 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001636 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001637 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001638 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001639 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001640 }
1641 }
1642
1643 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10001644 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001645 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001646
Damien Millerbd483e72000-04-30 10:00:53 +10001647 xfree(subsys);
1648 return success;
1649}
1650
Ben Lindstrombba81212001-06-25 05:01:22 +00001651static int
Damien Millerbd483e72000-04-30 10:00:53 +10001652session_x11_req(Session *s)
1653{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001654 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001655
1656 s->single_connection = packet_get_char();
1657 s->auth_proto = packet_get_string(NULL);
1658 s->auth_data = packet_get_string(NULL);
1659 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001660 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001661
Ben Lindstrom768176b2001-06-09 01:29:12 +00001662 success = session_setup_x11fwd(s);
1663 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001664 xfree(s->auth_proto);
1665 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001666 s->auth_proto = NULL;
1667 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001668 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001669 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001670}
1671
Ben Lindstrombba81212001-06-25 05:01:22 +00001672static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001673session_shell_req(Session *s)
1674{
Damien Miller48b03fc2002-01-22 23:11:40 +11001675 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001676 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001677 return 1;
1678}
1679
Ben Lindstrombba81212001-06-25 05:01:22 +00001680static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001681session_exec_req(Session *s)
1682{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001683 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001684 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001685 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001686 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001687 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001688 return 1;
1689}
1690
Ben Lindstrombba81212001-06-25 05:01:22 +00001691static int
Damien Miller54c45982003-05-15 10:20:13 +10001692session_break_req(Session *s)
1693{
1694 u_int break_length;
1695
Darren Tucker3bdbd842003-08-13 20:31:05 +10001696 break_length = packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10001697 packet_check_eom();
1698
Darren Tucker3bdbd842003-08-13 20:31:05 +10001699 if (s->ttyfd == -1 ||
1700 tcsendbreak(s->ttyfd, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10001701 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10001702 return 1;
1703}
1704
1705static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001706session_auth_agent_req(Session *s)
1707{
1708 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001709 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001710 if (no_agent_forwarding_flag) {
1711 debug("session_auth_agent_req: no_agent_forwarding_flag");
1712 return 0;
1713 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001714 if (called) {
1715 return 0;
1716 } else {
1717 called = 1;
1718 return auth_input_request_forwarding(s->pw);
1719 }
1720}
1721
Damien Millerc7ef63d2002-02-05 12:21:42 +11001722int
1723session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001724{
Damien Millerefb4afe2000-04-12 18:45:05 +10001725 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001726 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001727
Damien Millerc7ef63d2002-02-05 12:21:42 +11001728 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10001729 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11001730 c->self, rtype);
1731 return 0;
1732 }
1733 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001734
1735 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001736 * a session is in LARVAL state until a shell, a command
1737 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001738 */
1739 if (c->type == SSH_CHANNEL_LARVAL) {
1740 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001741 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001742 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001743 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001744 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001745 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001746 } else if (strcmp(rtype, "x11-req") == 0) {
1747 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001748 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1749 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001750 } else if (strcmp(rtype, "subsystem") == 0) {
1751 success = session_subsystem_req(s);
Damien Miller54c45982003-05-15 10:20:13 +10001752 } else if (strcmp(rtype, "break") == 0) {
1753 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001754 }
1755 }
1756 if (strcmp(rtype, "window-change") == 0) {
1757 success = session_window_change_req(s);
1758 }
Damien Millerc7ef63d2002-02-05 12:21:42 +11001759 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10001760}
1761
1762void
1763session_set_fds(Session *s, int fdin, int fdout, int fderr)
1764{
1765 if (!compat20)
1766 fatal("session_set_fds: called for proto != 2.0");
1767 /*
1768 * now that have a child and a pipe to the child,
1769 * we can activate our channel and register the fd's
1770 */
1771 if (s->chanid == -1)
1772 fatal("no channel for session %d", s->self);
1773 channel_set_fds(s->chanid,
1774 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001775 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11001776 1,
1777 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10001778}
1779
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001780/*
1781 * Function to perform pty cleanup. Also called if we get aborted abnormally
1782 * (e.g., due to a dropped connection).
1783 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784void
1785session_pty_cleanup2(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001786{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001787 Session *s = session;
1788
1789 if (s == NULL) {
1790 error("session_pty_cleanup: no session");
1791 return;
1792 }
1793 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001794 return;
1795
Kevin Steves43cdef32001-02-11 14:12:08 +00001796 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001797
Damien Millerb38eff82000-04-01 11:09:21 +10001798 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001799 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08001800 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001801
1802 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803 if (getuid() == 0)
1804 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001805
1806 /*
1807 * Close the server side of the socket pairs. We must do this after
1808 * the pty cleanup, so that another process doesn't get this pty
1809 * while we're still cleaning up.
1810 */
1811 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001812 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001813
1814 /* unlink pty from session */
1815 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001816}
Damien Millerefb4afe2000-04-12 18:45:05 +10001817
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001818void
1819session_pty_cleanup(void *session)
1820{
1821 PRIVSEP(session_pty_cleanup2(session));
1822}
1823
Damien Miller5a80bba2002-09-04 16:39:02 +10001824static char *
1825sig2name(int sig)
1826{
1827#define SSH_SIG(x) if (sig == SIG ## x) return #x
1828 SSH_SIG(ABRT);
1829 SSH_SIG(ALRM);
1830 SSH_SIG(FPE);
1831 SSH_SIG(HUP);
1832 SSH_SIG(ILL);
1833 SSH_SIG(INT);
1834 SSH_SIG(KILL);
1835 SSH_SIG(PIPE);
1836 SSH_SIG(QUIT);
1837 SSH_SIG(SEGV);
1838 SSH_SIG(TERM);
1839 SSH_SIG(USR1);
1840 SSH_SIG(USR2);
1841#undef SSH_SIG
1842 return "SIG@openssh.com";
1843}
1844
Ben Lindstrombba81212001-06-25 05:01:22 +00001845static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001846session_exit_message(Session *s, int status)
1847{
1848 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001849
1850 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001851 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001852 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001853 debug("session_exit_message: session %d channel %d pid %ld",
1854 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001855
1856 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001857 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001858 packet_put_int(WEXITSTATUS(status));
1859 packet_send();
1860 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001861 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10001862 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001863#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001864 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001865#else /* WCOREDUMP */
1866 packet_put_char(0);
1867#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001868 packet_put_cstring("");
1869 packet_put_cstring("");
1870 packet_send();
1871 } else {
1872 /* Some weird exit cause. Just exit. */
1873 packet_disconnect("wait returned status %04x.", status);
1874 }
1875
1876 /* disconnect channel */
1877 debug("session_exit_message: release channel %d", s->chanid);
1878 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001879 /*
1880 * emulate a write failure with 'chan_write_failed', nobody will be
1881 * interested in data we write.
1882 * Note that we must not call 'chan_read_failed', since there could
1883 * be some more data waiting in the pipe.
1884 */
Damien Millerbd483e72000-04-30 10:00:53 +10001885 if (c->ostate != CHAN_OUTPUT_CLOSED)
1886 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001887 s->chanid = -1;
1888}
1889
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001890void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001891session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001892{
Ben Lindstromce0f6342002-06-11 16:42:49 +00001893 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001894 if (s->ttyfd != -1) {
1895 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1896 session_pty_cleanup(s);
1897 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001898 if (s->term)
1899 xfree(s->term);
1900 if (s->display)
1901 xfree(s->display);
Damien Miller512bccb2002-02-05 12:11:02 +11001902 if (s->auth_display)
1903 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10001904 if (s->auth_data)
1905 xfree(s->auth_data);
1906 if (s->auth_proto)
1907 xfree(s->auth_proto);
1908 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001909 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001910}
1911
1912void
1913session_close_by_pid(pid_t pid, int status)
1914{
1915 Session *s = session_by_pid(pid);
1916 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001917 debug("session_close_by_pid: no session for pid %ld",
1918 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001919 return;
1920 }
1921 if (s->chanid != -1)
1922 session_exit_message(s, status);
1923 session_close(s);
1924}
1925
1926/*
1927 * this is called when a channel dies before
1928 * the session 'child' itself dies
1929 */
1930void
1931session_close_by_channel(int id, void *arg)
1932{
1933 Session *s = session_by_channel(id);
1934 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001935 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001936 return;
1937 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001938 debug("session_close_by_channel: channel %d child %ld",
1939 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10001940 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10001941 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10001942 /*
1943 * delay detach of session, but release pty, since
1944 * the fd's to the child are already closed
1945 */
1946 if (s->ttyfd != -1) {
1947 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1948 session_pty_cleanup(s);
1949 }
Damien Miller3ec27592001-10-12 11:35:04 +10001950 return;
1951 }
1952 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10001953 channel_cancel_cleanup(s->chanid);
1954 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10001955 session_close(s);
1956}
1957
1958void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001959session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10001960{
1961 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001962 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10001963 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001964 if (s->used) {
1965 if (closefunc != NULL)
1966 closefunc(s);
1967 else
1968 session_close(s);
1969 }
Damien Miller52b77be2001-10-10 15:14:37 +10001970 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001971}
1972
Ben Lindstrombba81212001-06-25 05:01:22 +00001973static char *
Damien Millere247cc42000-05-07 12:03:14 +10001974session_tty_list(void)
1975{
1976 static char buf[1024];
1977 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11001978 char *cp;
1979
Damien Millere247cc42000-05-07 12:03:14 +10001980 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11001981 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10001982 Session *s = &sessions[i];
1983 if (s->used && s->ttyfd != -1) {
Damien Millera8ed44b2003-01-10 09:53:12 +11001984
1985 if (strncmp(s->tty, "/dev/", 5) != 0) {
1986 cp = strrchr(s->tty, '/');
1987 cp = (cp == NULL) ? s->tty : cp + 1;
1988 } else
1989 cp = s->tty + 5;
1990
Damien Millere247cc42000-05-07 12:03:14 +10001991 if (buf[0] != '\0')
1992 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11001993 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10001994 }
1995 }
1996 if (buf[0] == '\0')
1997 strlcpy(buf, "notty", sizeof buf);
1998 return buf;
1999}
2000
2001void
2002session_proctitle(Session *s)
2003{
2004 if (s->pw == NULL)
2005 error("no user for session %d", s->self);
2006 else
2007 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2008}
2009
Ben Lindstrom768176b2001-06-09 01:29:12 +00002010int
2011session_setup_x11fwd(Session *s)
2012{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002013 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002014 char display[512], auth_display[512];
2015 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00002016
2017 if (no_x11_forwarding_flag) {
2018 packet_send_debug("X11 forwarding disabled in user configuration file.");
2019 return 0;
2020 }
2021 if (!options.x11_forwarding) {
2022 debug("X11 forwarding disabled in server configuration file.");
2023 return 0;
2024 }
2025 if (!options.xauth_location ||
2026 (stat(options.xauth_location, &st) == -1)) {
2027 packet_send_debug("No xauth program; cannot forward with spoofing.");
2028 return 0;
2029 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002030 if (options.use_login) {
2031 packet_send_debug("X11 forwarding disabled; "
2032 "not compatible with UseLogin=yes.");
2033 return 0;
2034 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002035 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002036 debug("X11 display already set.");
2037 return 0;
2038 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002039 if (x11_create_display_inet(options.x11_display_offset,
2040 options.x11_use_localhost, s->single_connection,
2041 &s->display_number) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002042 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002043 return 0;
2044 }
Kevin Steves366298c2001-12-19 17:58:01 +00002045
2046 /* Set up a suitable value for the DISPLAY variable. */
2047 if (gethostname(hostname, sizeof(hostname)) < 0)
2048 fatal("gethostname: %.100s", strerror(errno));
2049 /*
2050 * auth_display must be used as the displayname when the
2051 * authorization entry is added with xauth(1). This will be
2052 * different than the DISPLAY string for localhost displays.
2053 */
Damien Miller95c249f2002-02-05 12:11:34 +11002054 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002055 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002056 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002057 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002058 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002059 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002060 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002061 } else {
2062#ifdef IPADDR_IN_DISPLAY
2063 struct hostent *he;
2064 struct in_addr my_addr;
2065
2066 he = gethostbyname(hostname);
2067 if (he == NULL) {
2068 error("Can't get IP address for X11 DISPLAY.");
2069 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2070 return 0;
2071 }
2072 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002073 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002074 s->display_number, s->screen);
2075#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002076 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002077 s->display_number, s->screen);
2078#endif
2079 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002080 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002081 }
2082
Ben Lindstrom768176b2001-06-09 01:29:12 +00002083 return 1;
2084}
2085
Ben Lindstrombba81212001-06-25 05:01:22 +00002086static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002087do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002088{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002089 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002090}