]> andersk Git - openssh.git/blame - session.c
- markus@cvs.openbsd.org 2002/01/25 22:07:40
[openssh.git] / session.c
CommitLineData
7368a6c8 1/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
bcbf86ec 4 *
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 *
e78a59f5 11 * SSH2 support by Markus Friedl.
a96070d4 12 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 13 *
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.
e78a59f5 33 */
7368a6c8 34
35#include "includes.h"
54a5250f 36RCSID("$OpenBSD: session.c,v 1.117 2001/12/28 14:50:54 markus Exp $");
7368a6c8 37
7368a6c8 38#include "ssh.h"
42f11eb2 39#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
1729c161 42#include "sshpty.h"
7368a6c8 43#include "packet.h"
44#include "buffer.h"
7368a6c8 45#include "mpaux.h"
7368a6c8 46#include "uidswap.h"
47#include "compat.h"
a5efa1bb 48#include "channels.h"
e78a59f5 49#include "bufaux.h"
e78a59f5 50#include "auth.h"
38c295d6 51#include "auth-options.h"
42f11eb2 52#include "pathnames.h"
53#include "log.h"
54#include "servconf.h"
1729c161 55#include "sshlogin.h"
42f11eb2 56#include "serverloop.h"
57#include "canohost.h"
5ca51e19 58#include "session.h"
e78a59f5 59
3206bb3b 60#ifdef WITH_IRIX_PROJECT
61#include <proj.h>
62#endif /* WITH_IRIX_PROJECT */
d287c664 63#ifdef WITH_IRIX_JOBS
64#include <sys/resource.h>
2b87da3b 65#endif
260d427b 66#ifdef WITH_IRIX_AUDIT
67#include <sat.h>
68#endif /* WITH_IRIX_AUDIT */
3206bb3b 69
089fbbd2 70#if defined(HAVE_USERSEC_H)
71#include <usersec.h>
72#endif
73
3c62e7eb 74#ifdef HAVE_CYGWIN
75#include <windows.h>
76#include <sys/cygwin.h>
77#define is_winnt (GetVersion() < 0x80000000)
78#endif
79
3fcce26c 80/* AIX limits */
81#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
2a2cb9e7 82# define S_UFSIZE_HARD S_UFSIZE "_hard"
83# define S_UCPU_HARD S_UCPU "_hard"
84# define S_UDATA_HARD S_UDATA "_hard"
85# define S_USTACK_HARD S_USTACK "_hard"
86# define S_URSS_HARD S_URSS "_hard"
87# define S_UCORE_HARD S_UCORE "_hard"
88# define S_UNOFILE_HARD S_UNOFILE "_hard"
3fcce26c 89#endif
90
8a968c25 91#ifdef _AIX
92# include <uinfo.h>
93#endif
94
7368a6c8 95/* types */
96
97#define TTYSZ 64
98typedef struct Session Session;
99struct Session {
100 int used;
101 int self;
ced49be2 102 struct passwd *pw;
103 Authctxt *authctxt;
7368a6c8 104 pid_t pid;
105 /* tty */
106 char *term;
107 int ptyfd, ttyfd, ptymaster;
108 int row, col, xpixel, ypixel;
109 char tty[TTYSZ];
110 /* X11 */
c9d0ad9b 111 int display_number;
7368a6c8 112 char *display;
113 int screen;
c9d0ad9b 114 char *auth_display[2];
7368a6c8 115 char *auth_proto;
116 char *auth_data;
0b242b12 117 int single_connection;
7368a6c8 118 /* proto 2 */
119 int chanid;
e5d7a405 120 int is_subsystem;
7368a6c8 121};
122
123/* func */
124
125Session *session_new(void);
9c328529 126void session_set_fds(Session *, int, int, int);
127static void session_pty_cleanup(void *);
128void session_proctitle(Session *);
129int session_setup_x11fwd(Session *);
130void do_exec_pty(Session *, const char *);
131void do_exec_no_pty(Session *, const char *);
132void do_exec(Session *, const char *);
133void do_login(Session *, const char *);
8a9ac95d 134#ifdef LOGIN_NEEDS_UTMPX
135static void do_pre_login(Session *s);
136#endif
9c328529 137void do_child(Session *, const char *);
f2c2fd71 138void do_motd(void);
9c328529 139int check_quietlogin(Session *, const char *);
7368a6c8 140
396c147e 141static void do_authenticated1(Authctxt *);
142static void do_authenticated2(Authctxt *);
9c328529 143
396c147e 144static void session_close(Session *);
145static int session_pty_req(Session *);
bb5639fe 146
7368a6c8 147/* import */
148extern ServerOptions options;
149extern char *__progname;
150extern int log_stderr;
151extern int debug_flag;
1e3b8b07 152extern u_int utmp_len;
089fbbd2 153extern int startup_pipe;
e78e738a 154extern void destroy_sensitive_data(void);
089fbbd2 155
b5e300c2 156/* original command from peer. */
c95add71 157const char *original_command = NULL;
b5e300c2 158
7368a6c8 159/* data */
160#define MAX_SESSIONS 10
161Session sessions[MAX_SESSIONS];
c5d85828 162
c1ef8333 163#ifdef WITH_AIXAUTHENTICATE
c1ef8333 164char *aixloginmsg;
165#endif /* WITH_AIXAUTHENTICATE */
7368a6c8 166
2e73a022 167#ifdef HAVE_LOGIN_CAP
168static login_cap_t *lc;
169#endif
170
bb5639fe 171void
172do_authenticated(Authctxt *authctxt)
173{
174 /*
175 * Cancel the alarm we set to limit the time taken for
176 * authentication.
177 */
178 alarm(0);
179 if (startup_pipe != -1) {
180 close(startup_pipe);
181 startup_pipe = -1;
182 }
183#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
184 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
185 error("unable to get login class");
186 return;
187 }
e78e738a 188#ifdef BSD_AUTH
189 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
190 packet_disconnect("Approval failure for %s",
191 authctxt->pw->pw_name);
192 }
193#endif
bb5639fe 194#endif
19e810f6 195#ifdef WITH_AIXAUTHENTICATE
196 /* We don't have a pty yet, so just label the line as "ssh" */
197 if (loginsuccess(authctxt->user,
198 get_canonical_hostname(options.reverse_mapping_check),
199 "ssh", &aixloginmsg) < 0)
200 aixloginmsg = NULL;
201#endif /* WITH_AIXAUTHENTICATE */
202
bb5639fe 203 /* setup the channel layer */
204 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
205 channel_permit_all_opens();
206
207 if (compat20)
208 do_authenticated2(authctxt);
209 else
210 do_authenticated1(authctxt);
e4f7282d 211
b44de2b1 212 /* remove agent socket */
e4f7282d 213 if (auth_get_socket_name())
f0f32b8e 214 auth_sock_cleanup_proc(authctxt->pw);
ced49be2 215#ifdef KRB4
216 if (options.kerberos_ticket_cleanup)
217 krb4_cleanup_proc(authctxt);
218#endif
219#ifdef KRB5
220 if (options.kerberos_ticket_cleanup)
221 krb5_cleanup_proc(authctxt);
222#endif
bb5639fe 223}
224
7368a6c8 225/*
226 * 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 */
396c147e 231static void
bb5639fe 232do_authenticated1(Authctxt *authctxt)
7368a6c8 233{
234 Session *s;
7368a6c8 235 char *command;
54a5250f 236 int success, type, screen_flag;
bb5639fe 237 int compression_level = 0, enable_compression_after_reply = 0;
1e3b8b07 238 u_int proto_len, data_len, dlen;
7368a6c8 239
240 s = session_new();
ced49be2 241 s->authctxt = authctxt;
bb5639fe 242 s->pw = authctxt->pw;
2e73a022 243
7368a6c8 244 /*
245 * We stay in this loop until the client requests to execute a shell
246 * or a command.
247 */
248 for (;;) {
bb5639fe 249 success = 0;
7368a6c8 250
251 /* Get a packet from the client. */
54a5250f 252 type = packet_read();
7368a6c8 253
254 /* Process the packet. */
255 switch (type) {
256 case SSH_CMSG_REQUEST_COMPRESSION:
7368a6c8 257 compression_level = packet_get_int();
95500969 258 packet_check_eom();
7368a6c8 259 if (compression_level < 1 || compression_level > 9) {
260 packet_send_debug("Received illegal compression level %d.",
184eed6a 261 compression_level);
7368a6c8 262 break;
263 }
264 /* Enable compression after we have responded with SUCCESS. */
265 enable_compression_after_reply = 1;
266 success = 1;
267 break;
268
269 case SSH_CMSG_REQUEST_PTY:
653d5f86 270 success = session_pty_req(s);
7368a6c8 271 break;
272
273 case SSH_CMSG_X11_REQUEST_FORWARDING:
7368a6c8 274 s->auth_proto = packet_get_string(&proto_len);
275 s->auth_data = packet_get_string(&data_len);
7368a6c8 276
2af09193 277 screen_flag = packet_get_protocol_flags() &
278 SSH_PROTOFLAG_SCREEN_NUMBER;
279 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
280
281 if (packet_remaining() == 4) {
282 if (!screen_flag)
283 debug2("Buggy client: "
284 "X11 screen flag missing");
7368a6c8 285 s->screen = packet_get_int();
c8b058b4 286 } else {
7368a6c8 287 s->screen = 0;
c8b058b4 288 }
95500969 289 packet_check_eom();
8dcd9d5c 290 success = session_setup_x11fwd(s);
291 if (!success) {
292 xfree(s->auth_proto);
293 xfree(s->auth_data);
1500bcdd 294 s->auth_proto = NULL;
295 s->auth_data = NULL;
7368a6c8 296 }
7368a6c8 297 break;
7368a6c8 298
299 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
300 if (no_agent_forwarding_flag || compat13) {
301 debug("Authentication agent forwarding not permitted for this authentication.");
302 break;
303 }
304 debug("Received authentication agent forwarding request.");
bb5639fe 305 success = auth_input_request_forwarding(s->pw);
7368a6c8 306 break;
307
308 case SSH_CMSG_PORT_FORWARD_REQUEST:
309 if (no_port_forwarding_flag) {
310 debug("Port forwarding not permitted for this authentication.");
311 break;
312 }
33de75a3 313 if (!options.allow_tcp_forwarding) {
314 debug("Port forwarding not permitted.");
315 break;
316 }
7368a6c8 317 debug("Received TCP/IP port forwarding request.");
bb5639fe 318 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
7368a6c8 319 success = 1;
320 break;
321
322 case SSH_CMSG_MAX_PACKET_SIZE:
323 if (packet_set_maxsize(packet_get_int()) > 0)
324 success = 1;
325 break;
184eed6a 326
ced49be2 327#if defined(AFS) || defined(KRB5)
328 case SSH_CMSG_HAVE_KERBEROS_TGT:
329 if (!options.kerberos_tgt_passing) {
330 verbose("Kerberos TGT passing disabled.");
331 } else {
332 char *kdata = packet_get_string(&dlen);
95500969 333 packet_check_eom();
184eed6a 334
ced49be2 335 /* XXX - 0x41, see creds_to_radix version */
336 if (kdata[0] != 0x41) {
337#ifdef KRB5
338 krb5_data tgt;
339 tgt.data = kdata;
340 tgt.length = dlen;
184eed6a 341
ced49be2 342 if (auth_krb5_tgt(s->authctxt, &tgt))
343 success = 1;
344 else
345 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
346#endif /* KRB5 */
347 } else {
348#ifdef AFS
349 if (auth_krb4_tgt(s->authctxt, kdata))
350 success = 1;
351 else
352 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
353#endif /* AFS */
354 }
355 xfree(kdata);
356 }
357 break;
358#endif /* AFS || KRB5 */
184eed6a 359
ced49be2 360#ifdef AFS
361 case SSH_CMSG_HAVE_AFS_TOKEN:
362 if (!options.afs_token_passing || !k_hasafs()) {
363 verbose("AFS token passing disabled.");
364 } else {
365 /* Accept AFS token. */
366 char *token = packet_get_string(&dlen);
95500969 367 packet_check_eom();
184eed6a 368
ced49be2 369 if (auth_afs_token(s->authctxt, token))
370 success = 1;
371 else
372 verbose("AFS token refused for %.100s",
373 s->authctxt->user);
374 xfree(token);
375 }
376 break;
377#endif /* AFS */
7368a6c8 378
379 case SSH_CMSG_EXEC_SHELL:
380 case SSH_CMSG_EXEC_CMD:
7368a6c8 381 if (type == SSH_CMSG_EXEC_CMD) {
382 command = packet_get_string(&dlen);
383 debug("Exec command '%.500s'", command);
c95add71 384 do_exec(s, command);
385 xfree(command);
7368a6c8 386 } else {
c95add71 387 do_exec(s, NULL);
7368a6c8 388 }
95500969 389 packet_check_eom();
fc2a1d28 390 session_close(s);
7368a6c8 391 return;
392
393 default:
394 /*
395 * Any unknown messages in this phase are ignored,
396 * and a failure message is returned.
397 */
398 log("Unknown packet type received after authentication: %d", type);
399 }
400 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
401 packet_send();
402 packet_write_wait();
403
404 /* Enable compression now that we have replied if appropriate. */
405 if (enable_compression_after_reply) {
406 enable_compression_after_reply = 0;
407 packet_start_compression(compression_level);
408 }
409 }
410}
411
412/*
413 * This is called to fork and execute a command when we have no tty. This
414 * will call do_child from the child, and server_loop from the parent after
415 * setting up file descriptors and such.
416 */
6ae2364d 417void
65068d16 418do_exec_no_pty(Session *s, const char *command)
7368a6c8 419{
420 int pid;
421
422#ifdef USE_PIPES
423 int pin[2], pout[2], perr[2];
424 /* Allocate pipes for communicating with the program. */
425 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
426 packet_disconnect("Could not create pipes: %.100s",
427 strerror(errno));
428#else /* USE_PIPES */
429 int inout[2], err[2];
430 /* Uses socket pairs to communicate with the program. */
431 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
432 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
433 packet_disconnect("Could not create socket pairs: %.100s",
434 strerror(errno));
435#endif /* USE_PIPES */
436 if (s == NULL)
437 fatal("do_exec_no_pty: no session");
438
1d1ffb87 439 session_proctitle(s);
7368a6c8 440
b19d61d7 441#if defined(USE_PAM)
78220944 442 do_pam_session(s->pw->pw_name, NULL);
9afbfcfa 443 do_pam_setcred(1);
7eb73cc1 444 if (is_pam_password_change_required())
445 packet_disconnect("Password change required but no "
446 "TTY available");
266140a8 447#endif /* USE_PAM */
448
7368a6c8 449 /* Fork the child. */
450 if ((pid = fork()) == 0) {
451 /* Child. Reinitialize the log since the pid has changed. */
452 log_init(__progname, options.log_level, options.log_facility, log_stderr);
453
454 /*
455 * Create a new session and process group since the 4.4BSD
456 * setlogin() affects the entire process group.
457 */
458 if (setsid() < 0)
459 error("setsid failed: %.100s", strerror(errno));
460
461#ifdef USE_PIPES
462 /*
463 * Redirect stdin. We close the parent side of the socket
464 * pair, and make the child side the standard input.
465 */
466 close(pin[1]);
467 if (dup2(pin[0], 0) < 0)
468 perror("dup2 stdin");
469 close(pin[0]);
470
471 /* Redirect stdout. */
472 close(pout[0]);
473 if (dup2(pout[1], 1) < 0)
474 perror("dup2 stdout");
475 close(pout[1]);
476
477 /* Redirect stderr. */
478 close(perr[0]);
479 if (dup2(perr[1], 2) < 0)
480 perror("dup2 stderr");
481 close(perr[1]);
482#else /* USE_PIPES */
483 /*
484 * Redirect stdin, stdout, and stderr. Stdin and stdout will
485 * use the same socket, as some programs (particularly rdist)
486 * seem to depend on it.
487 */
488 close(inout[1]);
489 close(err[1]);
490 if (dup2(inout[0], 0) < 0) /* stdin */
491 perror("dup2 stdin");
492 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
493 perror("dup2 stdout");
494 if (dup2(err[0], 2) < 0) /* stderr */
495 perror("dup2 stderr");
496#endif /* USE_PIPES */
497
498 /* Do processing for the child (exec command etc). */
56b3e9ce 499 do_child(s, command);
7368a6c8 500 /* NOTREACHED */
501 }
3c62e7eb 502#ifdef HAVE_CYGWIN
503 if (is_winnt)
504 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
505#endif
7368a6c8 506 if (pid < 0)
507 packet_disconnect("fork failed: %.100s", strerror(errno));
508 s->pid = pid;
b5c334cc 509 /* Set interactive/non-interactive mode. */
510 packet_set_interactive(s->display != NULL);
7368a6c8 511#ifdef USE_PIPES
512 /* We are the parent. Close the child sides of the pipes. */
513 close(pin[0]);
514 close(pout[1]);
515 close(perr[1]);
516
e78a59f5 517 if (compat20) {
e5d7a405 518 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
e78a59f5 519 } else {
520 /* Enter the interactive session. */
521 server_loop(pid, pin[1], pout[0], perr[0]);
e5d7a405 522 /* server_loop has closed pin[1], pout[0], and perr[0]. */
e78a59f5 523 }
7368a6c8 524#else /* USE_PIPES */
525 /* We are the parent. Close the child sides of the socket pairs. */
526 close(inout[0]);
527 close(err[0]);
528
529 /*
530 * Enter the interactive session. Note: server_loop must be able to
531 * handle the case that fdin and fdout are the same.
532 */
e78a59f5 533 if (compat20) {
e5d7a405 534 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
e78a59f5 535 } else {
536 server_loop(pid, inout[1], inout[1], err[1]);
537 /* server_loop has closed inout[1] and err[1]. */
538 }
7368a6c8 539#endif /* USE_PIPES */
540}
541
542/*
543 * This is called to fork and execute a command when we have a tty. This
544 * will call do_child from the child, and server_loop from the parent after
545 * setting up file descriptors, controlling tty, updating wtmp, utmp,
546 * lastlog, and other such operations.
547 */
6ae2364d 548void
65068d16 549do_exec_pty(Session *s, const char *command)
7368a6c8 550{
7368a6c8 551 int fdout, ptyfd, ttyfd, ptymaster;
7368a6c8 552 pid_t pid;
7368a6c8 553
554 if (s == NULL)
555 fatal("do_exec_pty: no session");
556 ptyfd = s->ptyfd;
557 ttyfd = s->ttyfd;
558
b19d61d7 559#if defined(USE_PAM)
bb5639fe 560 do_pam_session(s->pw->pw_name, s->tty);
9afbfcfa 561 do_pam_setcred(1);
27cf96de 562#endif
266140a8 563
7368a6c8 564 /* Fork the child. */
565 if ((pid = fork()) == 0) {
57156994 566
c345cf9d 567 /* Child. Reinitialize the log because the pid has changed. */
7368a6c8 568 log_init(__progname, options.log_level, options.log_facility, log_stderr);
7368a6c8 569 /* Close the master side of the pseudo tty. */
570 close(ptyfd);
571
572 /* Make the pseudo tty our controlling tty. */
573 pty_make_controlling_tty(&ttyfd, s->tty);
574
05fd093c 575 /* Redirect stdin/stdout/stderr from the pseudo tty. */
576 if (dup2(ttyfd, 0) < 0)
577 error("dup2 stdin: %s", strerror(errno));
578 if (dup2(ttyfd, 1) < 0)
579 error("dup2 stdout: %s", strerror(errno));
580 if (dup2(ttyfd, 2) < 0)
581 error("dup2 stderr: %s", strerror(errno));
7368a6c8 582
583 /* Close the extra descriptor for the pseudo tty. */
584 close(ttyfd);
585
c345cf9d 586 /* record login, etc. similar to login(1) */
c96a4aaf 587#ifndef HAVE_OSF_SIA
a22aff1f 588 if (!(options.use_login && command == NULL))
589 do_login(s, command);
7e2d5fa4 590# ifdef LOGIN_NEEDS_UTMPX
591 else
592 do_pre_login(s);
593# endif
c96a4aaf 594#endif
7368a6c8 595
7368a6c8 596 /* Do common processing for the child, such as execing the command. */
56b3e9ce 597 do_child(s, command);
7368a6c8 598 /* NOTREACHED */
599 }
3c62e7eb 600#ifdef HAVE_CYGWIN
601 if (is_winnt)
602 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
603#endif
7368a6c8 604 if (pid < 0)
605 packet_disconnect("fork failed: %.100s", strerror(errno));
606 s->pid = pid;
607
608 /* Parent. Close the slave side of the pseudo tty. */
609 close(ttyfd);
610
611 /*
612 * Create another descriptor of the pty master side for use as the
613 * standard input. We could use the original descriptor, but this
614 * simplifies code in server_loop. The descriptor is bidirectional.
615 */
616 fdout = dup(ptyfd);
617 if (fdout < 0)
618 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
619
620 /* we keep a reference to the pty master */
621 ptymaster = dup(ptyfd);
622 if (ptymaster < 0)
623 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
624 s->ptymaster = ptymaster;
625
626 /* Enter interactive session. */
b5c334cc 627 packet_set_interactive(1);
e78a59f5 628 if (compat20) {
629 session_set_fds(s, ptyfd, fdout, -1);
630 } else {
631 server_loop(pid, ptyfd, fdout, -1);
632 /* server_loop _has_ closed ptyfd and fdout. */
e78a59f5 633 }
7368a6c8 634}
635
7e2d5fa4 636#ifdef LOGIN_NEEDS_UTMPX
0e0144b7 637static void
7e2d5fa4 638do_pre_login(Session *s)
639{
640 socklen_t fromlen;
641 struct sockaddr_storage from;
642 pid_t pid = getpid();
643
644 /*
645 * Get IP address of client. If the connection is not a socket, let
646 * the address be 0.0.0.0.
647 */
648 memset(&from, 0, sizeof(from));
649 if (packet_connection_is_on_socket()) {
650 fromlen = sizeof(from);
651 if (getpeername(packet_get_connection_in(),
184eed6a 652 (struct sockaddr *) & from, &fromlen) < 0) {
7e2d5fa4 653 debug("getpeername: %.100s", strerror(errno));
654 fatal_cleanup();
655 }
656 }
657
658 record_utmp_only(pid, s->tty, s->pw->pw_name,
659 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
660 (struct sockaddr *)&from);
661}
662#endif
663
dac6753b 664/*
665 * This is called to fork and execute a command. If another command is
666 * to be forced, execute that instead.
667 */
668void
669do_exec(Session *s, const char *command)
670{
671 if (forced_command) {
672 original_command = command;
673 command = forced_command;
dac6753b 674 debug("Forced command '%.900s'", command);
675 }
676
677 if (s->ttyfd != -1)
678 do_exec_pty(s, command);
679 else
680 do_exec_no_pty(s, command);
681
c95add71 682 original_command = NULL;
dac6753b 683}
684
c345cf9d 685/* administrative, login(1)-like work */
686void
a22aff1f 687do_login(Session *s, const char *command)
c345cf9d 688{
c345cf9d 689 char *time_string;
b5e300c2 690 char hostname[MAXHOSTNAMELEN];
c345cf9d 691 socklen_t fromlen;
692 struct sockaddr_storage from;
c345cf9d 693 time_t last_login_time;
694 struct passwd * pw = s->pw;
695 pid_t pid = getpid();
696
697 /*
698 * Get IP address of client. If the connection is not a socket, let
699 * the address be 0.0.0.0.
700 */
701 memset(&from, 0, sizeof(from));
702 if (packet_connection_is_on_socket()) {
703 fromlen = sizeof(from);
704 if (getpeername(packet_get_connection_in(),
705 (struct sockaddr *) & from, &fromlen) < 0) {
706 debug("getpeername: %.100s", strerror(errno));
707 fatal_cleanup();
708 }
709 }
710
bcbf86ec 711 /* Get the time and hostname when the user last logged in. */
4f4648f9 712 if (options.print_lastlog) {
713 hostname[0] = '\0';
714 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
715 hostname, sizeof(hostname));
716 }
bcbf86ec 717
c345cf9d 718 /* Record that there was a login on that tty from the remote host. */
719 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
46e3af7f 720 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
721 (struct sockaddr *)&from);
c345cf9d 722
2919e060 723#ifdef USE_PAM
724 /*
725 * If password change is needed, do it now.
726 * This needs to occur before the ~/.hushlogin check.
727 */
e11aab29 728 if (is_pam_password_change_required()) {
2919e060 729 print_pam_messages();
730 do_pam_chauthtok();
731 }
732#endif
733
f2c2fd71 734 if (check_quietlogin(s, command))
c345cf9d 735 return;
736
737#ifdef USE_PAM
e11aab29 738 if (!is_pam_password_change_required())
2919e060 739 print_pam_messages();
c345cf9d 740#endif /* USE_PAM */
741#ifdef WITH_AIXAUTHENTICATE
742 if (aixloginmsg && *aixloginmsg)
743 printf("%s\n", aixloginmsg);
744#endif /* WITH_AIXAUTHENTICATE */
745
4f4648f9 746 if (options.print_lastlog && last_login_time != 0) {
c345cf9d 747 time_string = ctime(&last_login_time);
748 if (strchr(time_string, '\n'))
749 *strchr(time_string, '\n') = 0;
f478712e 750 if (strcmp(hostname, "") == 0)
c345cf9d 751 printf("Last login: %s\r\n", time_string);
752 else
bcbf86ec 753 printf("Last login: %s from %s\r\n", time_string, hostname);
c345cf9d 754 }
f2c2fd71 755
756 do_motd();
757}
758
759/*
760 * Display the message of the day.
761 */
762void
763do_motd(void)
764{
765 FILE *f;
766 char buf[256];
767
c345cf9d 768 if (options.print_motd) {
2e73a022 769#ifdef HAVE_LOGIN_CAP
770 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
771 "/etc/motd"), "r");
772#else
c345cf9d 773 f = fopen("/etc/motd", "r");
2e73a022 774#endif
c345cf9d 775 if (f) {
776 while (fgets(buf, sizeof(buf), f))
777 fputs(buf, stdout);
778 fclose(f);
779 }
780 }
781}
782
9c328529 783
784/*
785 * Check for quiet login, either .hushlogin or command given.
786 */
787int
788check_quietlogin(Session *s, const char *command)
789{
790 char buf[256];
791 struct passwd *pw = s->pw;
792 struct stat st;
793
794 /* Return 1 if .hushlogin exists or a command given. */
795 if (command != NULL)
796 return 1;
797 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
798#ifdef HAVE_LOGIN_CAP
799 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
800 return 1;
801#else
802 if (stat(buf, &st) >= 0)
803 return 1;
804#endif
805 return 0;
806}
807
7368a6c8 808/*
809 * Sets the value of the given variable in the environment. If the variable
810 * already exists, its value is overriden.
811 */
396c147e 812static void
1e3b8b07 813child_set_env(char ***envp, u_int *envsizep, const char *name,
184eed6a 814 const char *value)
7368a6c8 815{
1e3b8b07 816 u_int i, namelen;
7368a6c8 817 char **env;
818
819 /*
820 * Find the slot where the value should be stored. If the variable
821 * already exists, we reuse the slot; otherwise we append a new slot
822 * at the end of the array, expanding if necessary.
823 */
824 env = *envp;
825 namelen = strlen(name);
826 for (i = 0; env[i]; i++)
827 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
828 break;
829 if (env[i]) {
830 /* Reuse the slot. */
831 xfree(env[i]);
832 } else {
833 /* New variable. Expand if necessary. */
834 if (i >= (*envsizep) - 1) {
835 (*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 */
396c147e 853static void
1e3b8b07 854read_environment_file(char ***env, u_int *envsize,
184eed6a 855 const char *filename)
7368a6c8 856{
857 FILE *f;
858 char buf[4096];
859 char *cp, *value;
860
861 f = fopen(filename, "r");
862 if (!f)
863 return;
864
865 while (fgets(buf, sizeof(buf), f)) {
866 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
867 ;
868 if (!*cp || *cp == '#' || *cp == '\n')
869 continue;
870 if (strchr(cp, '\n'))
871 *strchr(cp, '\n') = '\0';
872 value = strchr(cp, '=');
873 if (value == NULL) {
874 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
875 continue;
876 }
71276795 877 /*
878 * Replace the equals sign by nul, and advance value to
879 * the value string.
880 */
7368a6c8 881 *value = '\0';
882 value++;
883 child_set_env(env, envsize, cp, value);
884 }
885 fclose(f);
886}
887
760b35a6 888void copy_environment(char **source, char ***env, u_int *envsize)
7368a6c8 889{
760b35a6 890 char *var_name, *var_val;
7368a6c8 891 int i;
892
760b35a6 893 if (source == NULL)
7368a6c8 894 return;
2b87da3b 895
760b35a6 896 for(i = 0; source[i] != NULL; i++) {
897 var_name = xstrdup(source[i]);
898 if ((var_val = strstr(var_name, "=")) == NULL) {
899 xfree(var_name);
7368a6c8 900 continue;
7368a6c8 901 }
760b35a6 902 *var_val++ = '\0';
7368a6c8 903
760b35a6 904 debug3("Copy environment: %s=%s", var_name, var_val);
905 child_set_env(env, envsize, var_name, var_val);
906
907 xfree(var_name);
22d89d24 908 }
909}
22d89d24 910
5fa45897 911#if defined(HAVE_GETUSERATTR)
912/*
913 * AIX-specific login initialisation
914 */
915void set_limit(char *user, char *soft, char *hard, int resource, int mult)
916{
917 struct rlimit rlim;
56118702 918 int slim, hlim;
5fa45897 919
920 getrlimit(resource, &rlim);
921
56118702 922 slim = 0;
923 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
924 if (slim < 0) {
925 rlim.rlim_cur = RLIM_INFINITY;
926 } else if (slim != 0) {
927 /* See the wackiness below */
928 if (rlim.rlim_cur == slim * mult)
929 slim = 0;
930 else
931 rlim.rlim_cur = slim * mult;
932 }
933 }
5fa45897 934
56118702 935 hlim = 0;
936 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
937 if (hlim < 0) {
938 rlim.rlim_max = RLIM_INFINITY;
939 } else if (hlim != 0) {
940 rlim.rlim_max = hlim * mult;
941 }
942 }
5fa45897 943
56118702 944 /*
945 * XXX For cpu and fsize the soft limit is set to the hard limit
946 * if the hard limit is left at its default value and the soft limit
947 * is changed from its default value, either by requesting it
948 * (slim == 0) or by setting it to the current default. At least
949 * that's how rlogind does it. If you're confused you're not alone.
950 * Bug or feature? AIX 4.3.1.2
951 */
952 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
953 && hlim == 0 && slim != 0)
954 rlim.rlim_max = rlim.rlim_cur;
955 /* A specified hard limit limits the soft limit */
956 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
957 rlim.rlim_cur = rlim.rlim_max;
958 /* A soft limit can increase a hard limit */
959 else if (rlim.rlim_cur > rlim.rlim_max)
5fa45897 960 rlim.rlim_max = rlim.rlim_cur;
961
962 if (setrlimit(resource, &rlim) != 0)
56118702 963 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
5fa45897 964}
965
966void set_limits_from_userattr(char *user)
967{
968 int mask;
969 char buf[16];
970
971 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
972 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
973 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
974 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
975 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
976 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
977#if defined(S_UNOFILE)
978 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
979#endif
980
981 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
982 /* Convert decimal to octal */
983 (void) snprintf(buf, sizeof(buf), "%d", mask);
984 if (sscanf(buf, "%o", &mask) == 1)
985 umask(mask);
986 }
987}
988#endif /* defined(HAVE_GETUSERATTR) */
989
7368a6c8 990/*
991 * Performs common processing for the child, such as setting up the
992 * environment, closing extra file descriptors, setting the user and group
993 * ids, and executing the command or shell.
994 */
6ae2364d 995void
56b3e9ce 996do_child(Session *s, const char *command)
7368a6c8 997{
b5e300c2 998 const char *shell, *hostname = NULL, *cp = NULL;
ced49be2 999 struct passwd *pw = s->pw;
7368a6c8 1000 char buf[256];
6fc1e9f3 1001 char cmd[1024];
2e73a022 1002 FILE *f = NULL;
1e3b8b07 1003 u_int envsize, i;
7368a6c8 1004 char **env;
1005 extern char **environ;
1006 struct stat st;
1007 char *argv[10];
1500bcdd 1008 int do_xauth;
3206bb3b 1009#ifdef WITH_IRIX_PROJECT
1010 prid_t projid;
1011#endif /* WITH_IRIX_PROJECT */
d287c664 1012#ifdef WITH_IRIX_JOBS
1013 jid_t jid = 0;
1014#else
1015#ifdef WITH_IRIX_ARRAY
1016 int jid = 0;
1017#endif /* WITH_IRIX_ARRAY */
1018#endif /* WITH_IRIX_JOBS */
1019
1500bcdd 1020 do_xauth =
1021 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1022
e78e738a 1023 /* remove hostkey from the child's memory */
1024 destroy_sensitive_data();
1025
fa649821 1026 /* login(1) is only called if we execute the login shell */
1027 if (options.use_login && command != NULL)
1028 options.use_login = 0;
1029
453b4bd0 1030#if !defined(HAVE_OSF_SIA)
2e73a022 1031 if (!options.use_login) {
1032# ifdef HAVE_LOGIN_CAP
1033 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1034 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1035 _PATH_NOLOGIN), "r");
1036# else /* HAVE_LOGIN_CAP */
1037 if (pw->pw_uid)
1038 f = fopen(_PATH_NOLOGIN, "r");
1039# endif /* HAVE_LOGIN_CAP */
1040 if (f) {
1041 /* /etc/nologin exists. Print its contents and exit. */
1042 while (fgets(buf, sizeof(buf), f))
1043 fputs(buf, stderr);
1044 fclose(f);
7368a6c8 1045 exit(254);
2e73a022 1046 }
7368a6c8 1047 }
453b4bd0 1048#endif /* HAVE_OSF_SIA */
7368a6c8 1049
2e73a022 1050 /* Set login name, uid, gid, and groups. */
7368a6c8 1051 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1052 switch, so we let login(1) to this for us. */
1053 if (!options.use_login) {
4d33e531 1054#ifdef HAVE_OSF_SIA
7a7101ec 1055 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
c96a4aaf 1056 if (!check_quietlogin(s, command))
1057 do_motd();
815800e1 1058#else /* HAVE_OSF_SIA */
3c62e7eb 1059#ifdef HAVE_CYGWIN
1060 if (is_winnt) {
1061#else
7368a6c8 1062 if (getuid() == 0 || geteuid() == 0) {
3c62e7eb 1063#endif
2e73a022 1064# ifdef HAVE_GETUSERATTR
5fa45897 1065 set_limits_from_userattr(pw->pw_name);
2e73a022 1066# endif /* HAVE_GETUSERATTR */
1067# ifdef HAVE_LOGIN_CAP
1068 if (setusercontext(lc, pw, pw->pw_uid,
1069 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1070 perror("unable to set user context");
1071 exit(1);
1072 }
1073# else /* HAVE_LOGIN_CAP */
73d6d7fa 1074#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1075 /* Sets login uid for accounting */
1076 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1077 error("setluid: %s", strerror(errno));
1078#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1079
2e73a022 1080 if (setlogin(pw->pw_name) < 0)
1081 error("setlogin failed: %s", strerror(errno));
7368a6c8 1082 if (setgid(pw->pw_gid) < 0) {
1083 perror("setgid");
1084 exit(1);
1085 }
1086 /* Initialize the group list. */
1087 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1088 perror("initgroups");
1089 exit(1);
1090 }
1091 endgrent();
9afbfcfa 1092# ifdef USE_PAM
1093 /*
1094 * PAM credentials may take the form of
1095 * supplementary groups. These will have been
1096 * wiped by the above initgroups() call.
1097 * Reestablish them here.
1098 */
1099 do_pam_setcred(0);
1100# endif /* USE_PAM */
d287c664 1101# ifdef WITH_IRIX_JOBS
1102 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1103 if (jid == -1) {
1104 fatal("Failed to create job container: %.100s",
1105 strerror(errno));
2b87da3b 1106 }
d287c664 1107# endif /* WITH_IRIX_JOBS */
2e73a022 1108# ifdef WITH_IRIX_ARRAY
3206bb3b 1109 /* initialize array session */
d287c664 1110 if (jid == 0) {
1111 if (newarraysess() != 0)
1112 fatal("Failed to set up new array session: %.100s",
1113 strerror(errno));
1114 }
2e73a022 1115# endif /* WITH_IRIX_ARRAY */
1116# ifdef WITH_IRIX_PROJECT
3206bb3b 1117 /* initialize irix project info */
1118 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1119 debug("Failed to get project id, using projid 0");
1120 projid = 0;
1121 }
3206bb3b 1122 if (setprid(projid))
1123 fatal("Failed to initialize project %d for %s: %.100s",
1124 (int)projid, pw->pw_name, strerror(errno));
2e73a022 1125# endif /* WITH_IRIX_PROJECT */
260d427b 1126#ifdef WITH_IRIX_AUDIT
1127 if (sysconf(_SC_AUDIT)) {
1128 debug("Setting sat id to %d", (int) pw->pw_uid);
1129 if (satsetid(pw->pw_uid))
1130 debug("error setting satid: %.100s", strerror(errno));
1131 }
1132#endif /* WITH_IRIX_AUDIT */
1133
8a968c25 1134#ifdef _AIX
1135 /*
1136 * AIX has a "usrinfo" area where logname and
1137 * other stuff is stored - a few applications
1138 * actually use this and die if it's not set
1139 */
ee110bfb 1140 if (s->ttyfd == -1)
1141 s->tty[0] = '\0';
f5f6020e 1142 cp = xmalloc(22 + strlen(s->tty) +
8a968c25 1143 2 * strlen(pw->pw_name));
1144 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
ee110bfb 1145 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
8a968c25 1146 if (usrinfo(SETUINFO, cp, i) == -1)
1147 fatal("Couldn't set usrinfo: %s",
1148 strerror(errno));
1149 debug3("AIX/UsrInfo: set len %d", i);
1150 xfree(cp);
1151#endif
1152
7368a6c8 1153 /* Permanently switch to the desired uid. */
63bd8c36 1154 permanently_set_uid(pw);
2e73a022 1155# endif /* HAVE_LOGIN_CAP */
7368a6c8 1156 }
2e73a022 1157#endif /* HAVE_OSF_SIA */
1158
3c62e7eb 1159#ifdef HAVE_CYGWIN
1160 if (is_winnt)
1161#endif
7368a6c8 1162 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
14a9a859 1163 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
7368a6c8 1164 }
1165 /*
1166 * Get the shell from the password data. An empty shell field is
1167 * legal, and means /bin/sh.
1168 */
1169 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
2e73a022 1170#ifdef HAVE_LOGIN_CAP
1171 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1172#endif
7368a6c8 1173
7368a6c8 1174 /* Initialize the environment. */
1175 envsize = 100;
1176 env = xmalloc(envsize * sizeof(char *));
1177 env[0] = NULL;
1178
3c62e7eb 1179#ifdef HAVE_CYGWIN
1180 /*
1181 * The Windows environment contains some setting which are
1182 * important for a running system. They must not be dropped.
1183 */
760b35a6 1184 copy_environment(environ, &env, &envsize);
3c62e7eb 1185#endif
1186
7368a6c8 1187 if (!options.use_login) {
1188 /* Set basic environment. */
1189 child_set_env(&env, &envsize, "USER", pw->pw_name);
1190 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1191 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
2e73a022 1192#ifdef HAVE_LOGIN_CAP
1193 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1194 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
22d89d24 1195#else /* HAVE_LOGIN_CAP */
1196# ifndef HAVE_CYGWIN
3c62e7eb 1197 /*
1198 * There's no standard path on Windows. The path contains
1199 * important components pointing to the system directories,
1200 * needed for loading shared libraries. So the path better
1201 * remains intact here.
1202 */
7368a6c8 1203 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
22d89d24 1204# endif /* HAVE_CYGWIN */
1205#endif /* HAVE_LOGIN_CAP */
7368a6c8 1206
1207 snprintf(buf, sizeof buf, "%.200s/%.50s",
1208 _PATH_MAILDIR, pw->pw_name);
1209 child_set_env(&env, &envsize, "MAIL", buf);
1210
1211 /* Normal systems set SHELL by default. */
1212 child_set_env(&env, &envsize, "SHELL", shell);
1213 }
1214 if (getenv("TZ"))
1215 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1216
1217 /* Set custom environment options from RSA authentication. */
2548961d 1218 if (!options.use_login) {
1219 while (custom_environment) {
1220 struct envstring *ce = custom_environment;
1221 char *s = ce->s;
1222 int i;
1223 for (i = 0; s[i] != '=' && s[i]; i++)
1224 ;
1225 if (s[i] == '=') {
1226 s[i] = 0;
1227 child_set_env(&env, &envsize, s, s + i + 1);
1228 }
1229 custom_environment = ce->next;
1230 xfree(ce->s);
1231 xfree(ce);
7368a6c8 1232 }
7368a6c8 1233 }
1234
1235 snprintf(buf, sizeof buf, "%.50s %d %d",
1236 get_remote_ipaddr(), get_remote_port(), get_local_port());
1237 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1238
56b3e9ce 1239 if (s->ttyfd != -1)
1240 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1241 if (s->term)
1242 child_set_env(&env, &envsize, "TERM", s->term);
1243 if (s->display)
1244 child_set_env(&env, &envsize, "DISPLAY", s->display);
b5e300c2 1245 if (original_command)
1246 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1247 original_command);
7368a6c8 1248
1249#ifdef _AIX
22d89d24 1250 if ((cp = getenv("AUTHSTATE")) != NULL)
1251 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1252 if ((cp = getenv("KRB5CCNAME")) != NULL)
1253 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1254 read_environment_file(&env, &envsize, "/etc/environment");
7368a6c8 1255#endif
7368a6c8 1256#ifdef KRB4
ced49be2 1257 if (s->authctxt->krb4_ticket_file)
1258 child_set_env(&env, &envsize, "KRBTKFILE",
1259 s->authctxt->krb4_ticket_file);
1260#endif
1261#ifdef KRB5
1262 if (s->authctxt->krb5_ticket_file)
1263 child_set_env(&env, &envsize, "KRB5CCNAME",
1264 s->authctxt->krb5_ticket_file);
1265#endif
7368a6c8 1266#ifdef USE_PAM
1267 /* Pull in any environment variables that may have been set by PAM. */
760b35a6 1268 copy_environment(fetch_pam_environment(), &env, &envsize);
7368a6c8 1269#endif /* USE_PAM */
1270
7368a6c8 1271 if (auth_get_socket_name() != NULL)
1272 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
184eed6a 1273 auth_get_socket_name());
7368a6c8 1274
1275 /* read $HOME/.ssh/environment. */
1276 if (!options.use_login) {
71276795 1277 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1278 pw->pw_dir);
7368a6c8 1279 read_environment_file(&env, &envsize, buf);
1280 }
1281 if (debug_flag) {
1282 /* dump the environment */
1283 fprintf(stderr, "Environment:\n");
1284 for (i = 0; env[i]; i++)
1285 fprintf(stderr, " %.200s\n", env[i]);
1286 }
2e73a022 1287 /* we have to stash the hostname before we close our socket. */
1288 if (options.use_login)
46e3af7f 1289 hostname = get_remote_name_or_ip(utmp_len,
1290 options.reverse_mapping_check);
7368a6c8 1291 /*
1292 * Close the connection descriptors; note that this is the child, and
1293 * the server will still have the socket open, and it is important
1294 * that we do not shutdown it. Note that the descriptors cannot be
1295 * closed before building the environment, as we call
1296 * get_remote_ipaddr there.
1297 */
1298 if (packet_get_connection_in() == packet_get_connection_out())
1299 close(packet_get_connection_in());
1300 else {
1301 close(packet_get_connection_in());
1302 close(packet_get_connection_out());
1303 }
1304 /*
1305 * Close all descriptors related to channels. They will still remain
1306 * open in the parent.
1307 */
1308 /* XXX better use close-on-exec? -markus */
1309 channel_close_all();
1310
1311 /*
1312 * Close any extra file descriptors. Note that there may still be
1313 * descriptors left by system functions. They will be closed later.
1314 */
1315 endpwent();
1316
1317 /*
1318 * Close any extra open file descriptors so that we don\'t have them
1319 * hanging around in clients. Note that we want to do this after
1320 * initgroups, because at least on Solaris 2.3 it leaves file
1321 * descriptors open.
1322 */
1323 for (i = 3; i < 64; i++)
1324 close(i);
1325
7368a6c8 1326 /*
1327 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1328 * xauth are run in the proper environment.
1329 */
1330 environ = env;
1331
6fcf67f7 1332#ifdef AFS
1333 /* Try to get AFS tokens for the local cell. */
1334 if (k_hasafs()) {
1335 char cell[64];
184eed6a 1336
6fcf67f7 1337 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1338 krb_afslog(cell, 0);
184eed6a 1339
6fcf67f7 1340 krb_afslog(0, 0);
1341 }
1342#endif /* AFS */
1343
dab89049 1344 /* Change current directory to the user\'s home directory. */
1345 if (chdir(pw->pw_dir) < 0) {
1346 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1347 pw->pw_dir, strerror(errno));
1348#ifdef HAVE_LOGIN_CAP
1349 if (login_getcapbool(lc, "requirehome", 0))
1350 exit(1);
1351#endif
1352 }
1353
7368a6c8 1354 /*
1355 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1356 * in this order).
1357 */
1358 if (!options.use_login) {
e78e738a 1359 /* ignore _PATH_SSH_USER_RC for subsystems */
1360 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
503e7e5b 1361 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1362 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
7368a6c8 1363 if (debug_flag)
503e7e5b 1364 fprintf(stderr, "Running %s\n", cmd);
1365 f = popen(cmd, "w");
7368a6c8 1366 if (f) {
56b3e9ce 1367 if (do_xauth)
1368 fprintf(f, "%s %s\n", s->auth_proto,
1369 s->auth_data);
7368a6c8 1370 pclose(f);
1371 } else
96c63318 1372 fprintf(stderr, "Could not run %s\n",
56b3e9ce 1373 _PATH_SSH_USER_RC);
42f11eb2 1374 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
7368a6c8 1375 if (debug_flag)
56b3e9ce 1376 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1377 _PATH_SSH_SYSTEM_RC);
42f11eb2 1378 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
7368a6c8 1379 if (f) {
56b3e9ce 1380 if (do_xauth)
1381 fprintf(f, "%s %s\n", s->auth_proto,
1382 s->auth_data);
7368a6c8 1383 pclose(f);
1384 } else
56b3e9ce 1385 fprintf(stderr, "Could not run %s\n",
1386 _PATH_SSH_SYSTEM_RC);
1387 } else if (do_xauth && options.xauth_location != NULL) {
7368a6c8 1388 /* Add authority data to .Xauthority if appropriate. */
56b3e9ce 1389 if (debug_flag) {
1390 fprintf(stderr,
1391 "Running %.100s add "
1392 "%.100s %.100s %.100s\n",
c9d0ad9b 1393 options.xauth_location, s->auth_display[0],
56b3e9ce 1394 s->auth_proto, s->auth_data);
c9d0ad9b 1395 if (s->auth_display[1])
71276795 1396 fprintf(stderr,
c9d0ad9b 1397 "add %.100s %.100s %.100s\n",
1398 s->auth_display[1],
56b3e9ce 1399 s->auth_proto, s->auth_data);
1400 }
1401 snprintf(cmd, sizeof cmd, "%s -q -",
1402 options.xauth_location);
1403 f = popen(cmd, "w");
1404 if (f) {
c9d0ad9b 1405 fprintf(f, "add %s %s %s\n",
1406 s->auth_display[0], s->auth_proto,
1407 s->auth_data);
1408 if (s->auth_display[1])
1409 fprintf(f, "add %s %s %s\n",
1410 s->auth_display[1], s->auth_proto,
56b3e9ce 1411 s->auth_data);
1412 pclose(f);
1413 } else {
1414 fprintf(stderr, "Could not run %s\n",
1415 cmd);
7368a6c8 1416 }
1417 }
7368a6c8 1418 /* Get the last component of the shell name. */
1419 cp = strrchr(shell, '/');
1420 if (cp)
1421 cp++;
1422 else
1423 cp = shell;
1424 }
1d3c30db 1425
1426 /* restore SIGPIPE for child */
1427 signal(SIGPIPE, SIG_DFL);
1428
7368a6c8 1429 /*
1430 * If we have no command, execute the shell. In this case, the shell
1431 * name to be passed in argv[0] is preceded by '-' to indicate that
1432 * this is a login shell.
1433 */
1434 if (!command) {
1435 if (!options.use_login) {
1436 char buf[256];
1437
7368a6c8 1438 /* Start the shell. Set initial character to '-'. */
1439 buf[0] = '-';
74860245 1440 strlcpy(buf + 1, cp, sizeof(buf) - 1);
7368a6c8 1441
1442 /* Execute the shell. */
1443 argv[0] = buf;
1444 argv[1] = NULL;
1445 execve(shell, argv, env);
1446
1447 /* Executing the shell failed. */
1448 perror(shell);
1449 exit(1);
1450
1451 } else {
1452 /* Launch login(1). */
1453
2e73a022 1454 execl(LOGIN_PROGRAM, "login", "-h", hostname,
7e2d5fa4 1455#ifdef LOGIN_NEEDS_TERM
184eed6a 1456 s->term? s->term : "unknown",
7e2d5fa4 1457#endif
184eed6a 1458 "-p", "-f", "--", pw->pw_name, (char *)NULL);
7368a6c8 1459
1460 /* Login couldn't be executed, die. */
1461
1462 perror("login");
1463 exit(1);
1464 }
1465 }
1466 /*
1467 * Execute the command using the user's shell. This uses the -c
1468 * option to execute the command.
1469 */
1470 argv[0] = (char *) cp;
1471 argv[1] = "-c";
1472 argv[2] = (char *) command;
1473 argv[3] = NULL;
1474 execve(shell, argv, env);
1475 perror(shell);
1476 exit(1);
1477}
1478
1479Session *
1480session_new(void)
1481{
1482 int i;
1483 static int did_init = 0;
1484 if (!did_init) {
1485 debug("session_new: init");
184eed6a 1486 for (i = 0; i < MAX_SESSIONS; i++) {
7368a6c8 1487 sessions[i].used = 0;
7368a6c8 1488 }
1489 did_init = 1;
1490 }
184eed6a 1491 for (i = 0; i < MAX_SESSIONS; i++) {
7368a6c8 1492 Session *s = &sessions[i];
1493 if (! s->used) {
690d0d7f 1494 memset(s, 0, sizeof(*s));
7368a6c8 1495 s->chanid = -1;
1496 s->ptyfd = -1;
1497 s->ttyfd = -1;
7368a6c8 1498 s->used = 1;
86066315 1499 s->self = i;
02323c45 1500 debug("session_new: session %d", i);
7368a6c8 1501 return s;
1502 }
1503 }
1504 return NULL;
1505}
1506
396c147e 1507static void
7368a6c8 1508session_dump(void)
1509{
1510 int i;
184eed6a 1511 for (i = 0; i < MAX_SESSIONS; i++) {
7368a6c8 1512 Session *s = &sessions[i];
1513 debug("dump: used %d session %d %p channel %d pid %d",
1514 s->used,
1515 s->self,
1516 s,
1517 s->chanid,
1518 s->pid);
1519 }
1520}
1521
e78a59f5 1522int
57156994 1523session_open(Authctxt *authctxt, int chanid)
e78a59f5 1524{
1525 Session *s = session_new();
1526 debug("session_open: channel %d", chanid);
1527 if (s == NULL) {
1528 error("no more sessions");
1529 return 0;
1530 }
57156994 1531 s->authctxt = authctxt;
1532 s->pw = authctxt->pw;
e78a59f5 1533 if (s->pw == NULL)
0725ea77 1534 fatal("no user for session %d", s->self);
0b242b12 1535 debug("session_open: session %d: link with channel %d", s->self, chanid);
1536 s->chanid = chanid;
e78a59f5 1537 return 1;
1538}
1539
396c147e 1540static Session *
e78a59f5 1541session_by_channel(int id)
1542{
1543 int i;
184eed6a 1544 for (i = 0; i < MAX_SESSIONS; i++) {
e78a59f5 1545 Session *s = &sessions[i];
1546 if (s->used && s->chanid == id) {
1547 debug("session_by_channel: session %d channel %d", i, id);
1548 return s;
1549 }
1550 }
1551 debug("session_by_channel: unknown channel %d", id);
1552 session_dump();
1553 return NULL;
1554}
1555
396c147e 1556static Session *
e78a59f5 1557session_by_pid(pid_t pid)
1558{
1559 int i;
1560 debug("session_by_pid: pid %d", pid);
184eed6a 1561 for (i = 0; i < MAX_SESSIONS; i++) {
e78a59f5 1562 Session *s = &sessions[i];
1563 if (s->used && s->pid == pid)
1564 return s;
1565 }
1566 error("session_by_pid: unknown pid %d", pid);
1567 session_dump();
1568 return NULL;
1569}
1570
396c147e 1571static int
e78a59f5 1572session_window_change_req(Session *s)
1573{
1574 s->col = packet_get_int();
1575 s->row = packet_get_int();
1576 s->xpixel = packet_get_int();
1577 s->ypixel = packet_get_int();
95500969 1578 packet_check_eom();
e78a59f5 1579 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1580 return 1;
1581}
1582
396c147e 1583static int
e78a59f5 1584session_pty_req(Session *s)
1585{
1e3b8b07 1586 u_int len;
30dcc918 1587 int n_bytes;
e78a59f5 1588
653d5f86 1589 if (no_pty_flag) {
1590 debug("Allocating a pty not permitted for this authentication.");
38c295d6 1591 return 0;
653d5f86 1592 }
1593 if (s->ttyfd != -1) {
1594 packet_disconnect("Protocol error: you already have a pty.");
6ae2364d 1595 return 0;
653d5f86 1596 }
1597
e78a59f5 1598 s->term = packet_get_string(&len);
653d5f86 1599
1600 if (compat20) {
1601 s->col = packet_get_int();
1602 s->row = packet_get_int();
1603 } else {
1604 s->row = packet_get_int();
1605 s->col = packet_get_int();
1606 }
e78a59f5 1607 s->xpixel = packet_get_int();
1608 s->ypixel = packet_get_int();
1609
1610 if (strcmp(s->term, "") == 0) {
1611 xfree(s->term);
1612 s->term = NULL;
1613 }
653d5f86 1614
e78a59f5 1615 /* Allocate a pty and open it. */
653d5f86 1616 debug("Allocating pty.");
e78a59f5 1617 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
653d5f86 1618 if (s->term)
1619 xfree(s->term);
e78a59f5 1620 s->term = NULL;
1621 s->ptyfd = -1;
1622 s->ttyfd = -1;
1623 error("session_pty_req: session %d alloc failed", s->self);
6ae2364d 1624 return 0;
e78a59f5 1625 }
1626 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
653d5f86 1627
1628 /* for SSH1 the tty modes length is not given */
1629 if (!compat20)
1630 n_bytes = packet_remaining();
1631 tty_parse_modes(s->ttyfd, &n_bytes);
1632
e78a59f5 1633 /*
1634 * Add a cleanup function to clear the utmp entry and record logout
1635 * time in case we call fatal() (e.g., the connection gets closed).
1636 */
86066315 1637 fatal_add_cleanup(session_pty_cleanup, (void *)s);
e78a59f5 1638 pty_setowner(s->pw, s->tty);
653d5f86 1639
1640 /* Set window size from the packet. */
e78a59f5 1641 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1642
95500969 1643 packet_check_eom();
1d1ffb87 1644 session_proctitle(s);
e78a59f5 1645 return 1;
1646}
1647
396c147e 1648static int
0b242b12 1649session_subsystem_req(Session *s)
1650{
01855277 1651 struct stat st;
1e3b8b07 1652 u_int len;
0b242b12 1653 int success = 0;
01855277 1654 char *cmd, *subsys = packet_get_string(&len);
38c295d6 1655 int i;
0b242b12 1656
95500969 1657 packet_check_eom();
0b242b12 1658 log("subsystem request for %s", subsys);
1659
38c295d6 1660 for (i = 0; i < options.num_subsystems; i++) {
01855277 1661 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1662 cmd = options.subsystem_command[i];
1663 if (stat(cmd, &st) < 0) {
1664 error("subsystem: cannot stat %s: %s", cmd,
1665 strerror(errno));
1666 break;
1667 }
1668 debug("subsystem: exec() %s", cmd);
e5d7a405 1669 s->is_subsystem = 1;
01855277 1670 do_exec(s, cmd);
38c295d6 1671 success = 1;
1672 }
1673 }
1674
1675 if (!success)
01855277 1676 log("subsystem request for %s failed, subsystem not found",
1677 subsys);
38c295d6 1678
0b242b12 1679 xfree(subsys);
1680 return success;
1681}
1682
396c147e 1683static int
0b242b12 1684session_x11_req(Session *s)
1685{
8dcd9d5c 1686 int success;
0b242b12 1687
1688 s->single_connection = packet_get_char();
1689 s->auth_proto = packet_get_string(NULL);
1690 s->auth_data = packet_get_string(NULL);
1691 s->screen = packet_get_int();
95500969 1692 packet_check_eom();
0b242b12 1693
8dcd9d5c 1694 success = session_setup_x11fwd(s);
1695 if (!success) {
0b242b12 1696 xfree(s->auth_proto);
1697 xfree(s->auth_data);
1500bcdd 1698 s->auth_proto = NULL;
1699 s->auth_data = NULL;
0b242b12 1700 }
8dcd9d5c 1701 return success;
0b242b12 1702}
1703
396c147e 1704static int
38c295d6 1705session_shell_req(Session *s)
1706{
95500969 1707 packet_check_eom();
dac6753b 1708 do_exec(s, NULL);
38c295d6 1709 return 1;
1710}
1711
396c147e 1712static int
38c295d6 1713session_exec_req(Session *s)
1714{
1e3b8b07 1715 u_int len;
38c295d6 1716 char *command = packet_get_string(&len);
95500969 1717 packet_check_eom();
dac6753b 1718 do_exec(s, command);
c95add71 1719 xfree(command);
38c295d6 1720 return 1;
1721}
1722
396c147e 1723static int
fa08c86b 1724session_auth_agent_req(Session *s)
1725{
1726 static int called = 0;
95500969 1727 packet_check_eom();
d343d900 1728 if (no_agent_forwarding_flag) {
1729 debug("session_auth_agent_req: no_agent_forwarding_flag");
1730 return 0;
1731 }
fa08c86b 1732 if (called) {
1733 return 0;
1734 } else {
1735 called = 1;
1736 return auth_input_request_forwarding(s->pw);
1737 }
1738}
1739
e78a59f5 1740void
1741session_input_channel_req(int id, void *arg)
1742{
1e3b8b07 1743 u_int len;
e78a59f5 1744 int reply;
1745 int success = 0;
1746 char *rtype;
1747 Session *s;
1748 Channel *c;
1749
1750 rtype = packet_get_string(&len);
1751 reply = packet_get_char();
1752
1753 s = session_by_channel(id);
1754 if (s == NULL)
1755 fatal("session_input_channel_req: channel %d: no session", id);
1756 c = channel_lookup(id);
1757 if (c == NULL)
1758 fatal("session_input_channel_req: channel %d: bad channel", id);
1759
1760 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1761 s->self, id, rtype, reply);
1762
1763 /*
e5d7a405 1764 * a session is in LARVAL state until a shell, a command
1765 * or a subsystem is executed
e78a59f5 1766 */
1767 if (c->type == SSH_CHANNEL_LARVAL) {
1768 if (strcmp(rtype, "shell") == 0) {
38c295d6 1769 success = session_shell_req(s);
e78a59f5 1770 } else if (strcmp(rtype, "exec") == 0) {
38c295d6 1771 success = session_exec_req(s);
e78a59f5 1772 } else if (strcmp(rtype, "pty-req") == 0) {
35484284 1773 success = session_pty_req(s);
0b242b12 1774 } else if (strcmp(rtype, "x11-req") == 0) {
1775 success = session_x11_req(s);
fa08c86b 1776 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1777 success = session_auth_agent_req(s);
0b242b12 1778 } else if (strcmp(rtype, "subsystem") == 0) {
1779 success = session_subsystem_req(s);
e78a59f5 1780 }
1781 }
1782 if (strcmp(rtype, "window-change") == 0) {
1783 success = session_window_change_req(s);
1784 }
1785
1786 if (reply) {
1787 packet_start(success ?
1788 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1789 packet_put_int(c->remote_id);
1790 packet_send();
1791 }
1792 xfree(rtype);
1793}
1794
1795void
1796session_set_fds(Session *s, int fdin, int fdout, int fderr)
1797{
1798 if (!compat20)
1799 fatal("session_set_fds: called for proto != 2.0");
1800 /*
1801 * now that have a child and a pipe to the child,
1802 * we can activate our channel and register the fd's
1803 */
1804 if (s->chanid == -1)
1805 fatal("no channel for session %d", s->self);
1806 channel_set_fds(s->chanid,
1807 fdout, fdin, fderr,
a22aff1f 1808 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1809 1);
e78a59f5 1810}
1811
86066315 1812/*
1813 * Function to perform pty cleanup. Also called if we get aborted abnormally
1814 * (e.g., due to a dropped connection).
1815 */
396c147e 1816static void
86066315 1817session_pty_cleanup(void *session)
7368a6c8 1818{
86066315 1819 Session *s = session;
1820
1821 if (s == NULL) {
1822 error("session_pty_cleanup: no session");
1823 return;
1824 }
1825 if (s->ttyfd == -1)
7368a6c8 1826 return;
1827
0725ea77 1828 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
7368a6c8 1829
7368a6c8 1830 /* Record that the user has logged out. */
86066315 1831 if (s->pid != 0)
1832 record_logout(s->pid, s->tty);
7368a6c8 1833
1834 /* Release the pseudo-tty. */
1835 pty_release(s->tty);
1836
1837 /*
1838 * Close the server side of the socket pairs. We must do this after
1839 * the pty cleanup, so that another process doesn't get this pty
1840 * while we're still cleaning up.
1841 */
1842 if (close(s->ptymaster) < 0)
1843 error("close(s->ptymaster): %s", strerror(errno));
efcc9957 1844
1845 /* unlink pty from session */
1846 s->ttyfd = -1;
7368a6c8 1847}
e78a59f5 1848
396c147e 1849static void
e78a59f5 1850session_exit_message(Session *s, int status)
1851{
1852 Channel *c;
1853 if (s == NULL)
1854 fatal("session_close: no session");
1855 c = channel_lookup(s->chanid);
1856 if (c == NULL)
86066315 1857 fatal("session_exit_message: session %d: no channel %d",
e78a59f5 1858 s->self, s->chanid);
1859 debug("session_exit_message: session %d channel %d pid %d",
1860 s->self, s->chanid, s->pid);
1861
1862 if (WIFEXITED(status)) {
1863 channel_request_start(s->chanid,
1864 "exit-status", 0);
1865 packet_put_int(WEXITSTATUS(status));
1866 packet_send();
1867 } else if (WIFSIGNALED(status)) {
1868 channel_request_start(s->chanid,
1869 "exit-signal", 0);
1870 packet_put_int(WTERMSIG(status));
a64009ad 1871#ifdef WCOREDUMP
e78a59f5 1872 packet_put_char(WCOREDUMP(status));
a64009ad 1873#else /* WCOREDUMP */
1874 packet_put_char(0);
1875#endif /* WCOREDUMP */
e78a59f5 1876 packet_put_cstring("");
1877 packet_put_cstring("");
1878 packet_send();
1879 } else {
1880 /* Some weird exit cause. Just exit. */
1881 packet_disconnect("wait returned status %04x.", status);
1882 }
1883
1884 /* disconnect channel */
1885 debug("session_exit_message: release channel %d", s->chanid);
1886 channel_cancel_cleanup(s->chanid);
9da5c3c9 1887 /*
1888 * emulate a write failure with 'chan_write_failed', nobody will be
1889 * interested in data we write.
1890 * Note that we must not call 'chan_read_failed', since there could
1891 * be some more data waiting in the pipe.
1892 */
0b242b12 1893 if (c->ostate != CHAN_OUTPUT_CLOSED)
1894 chan_write_failed(c);
e78a59f5 1895 s->chanid = -1;
1896}
1897
396c147e 1898static void
86066315 1899session_close(Session *s)
e78a59f5 1900{
86066315 1901 debug("session_close: session %d pid %d", s->self, s->pid);
1902 if (s->ttyfd != -1) {
1903 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1904 session_pty_cleanup(s);
1905 }
e78a59f5 1906 if (s->term)
1907 xfree(s->term);
1908 if (s->display)
1909 xfree(s->display);
c9d0ad9b 1910 if (s->auth_display[0])
1911 xfree(s->auth_display[0]);
1912 if (s->auth_display[1])
1913 xfree(s->auth_display[1]);
e78a59f5 1914 if (s->auth_data)
1915 xfree(s->auth_data);
1916 if (s->auth_proto)
1917 xfree(s->auth_proto);
1918 s->used = 0;
1d1ffb87 1919 session_proctitle(s);
e78a59f5 1920}
1921
1922void
1923session_close_by_pid(pid_t pid, int status)
1924{
1925 Session *s = session_by_pid(pid);
1926 if (s == NULL) {
eb26141e 1927 debug("session_close_by_pid: no session for pid %d", pid);
e78a59f5 1928 return;
1929 }
1930 if (s->chanid != -1)
1931 session_exit_message(s, status);
1932 session_close(s);
1933}
1934
1935/*
1936 * this is called when a channel dies before
1937 * the session 'child' itself dies
1938 */
1939void
1940session_close_by_channel(int id, void *arg)
1941{
1942 Session *s = session_by_channel(id);
1943 if (s == NULL) {
418e724c 1944 debug("session_close_by_channel: no session for id %d", id);
e78a59f5 1945 return;
1946 }
418e724c 1947 debug("session_close_by_channel: channel %d child %d", id, s->pid);
d5f24f94 1948 if (s->pid != 0) {
418e724c 1949 debug("session_close_by_channel: channel %d: has child", id);
efcc9957 1950 /*
1951 * delay detach of session, but release pty, since
1952 * the fd's to the child are already closed
1953 */
1954 if (s->ttyfd != -1) {
1955 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1956 session_pty_cleanup(s);
1957 }
418e724c 1958 return;
e78a59f5 1959 }
418e724c 1960 /* detach by removing callback */
1961 channel_cancel_cleanup(s->chanid);
1962 s->chanid = -1;
d5f24f94 1963 session_close(s);
1964}
1965
1966void
418e724c 1967session_destroy_all(void)
d5f24f94 1968{
1969 int i;
184eed6a 1970 for (i = 0; i < MAX_SESSIONS; i++) {
d5f24f94 1971 Session *s = &sessions[i];
184eed6a 1972 if (s->used)
d5f24f94 1973 session_close(s);
d5f24f94 1974 }
e78a59f5 1975}
1976
396c147e 1977static char *
1d1ffb87 1978session_tty_list(void)
1979{
1980 static char buf[1024];
1981 int i;
1982 buf[0] = '\0';
184eed6a 1983 for (i = 0; i < MAX_SESSIONS; i++) {
1d1ffb87 1984 Session *s = &sessions[i];
1985 if (s->used && s->ttyfd != -1) {
1986 if (buf[0] != '\0')
1987 strlcat(buf, ",", sizeof buf);
1988 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1989 }
1990 }
1991 if (buf[0] == '\0')
1992 strlcpy(buf, "notty", sizeof buf);
1993 return buf;
1994}
1995
1996void
1997session_proctitle(Session *s)
1998{
1999 if (s->pw == NULL)
2000 error("no user for session %d", s->self);
2001 else
2002 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2003}
2004
8dcd9d5c 2005int
2006session_setup_x11fwd(Session *s)
2007{
8dcd9d5c 2008 struct stat st;
c9d0ad9b 2009 char display[512], auth_display[512];
2010 char hostname[MAXHOSTNAMELEN];
8dcd9d5c 2011
2012 if (no_x11_forwarding_flag) {
2013 packet_send_debug("X11 forwarding disabled in user configuration file.");
2014 return 0;
2015 }
2016 if (!options.x11_forwarding) {
2017 debug("X11 forwarding disabled in server configuration file.");
2018 return 0;
2019 }
2020 if (!options.xauth_location ||
2021 (stat(options.xauth_location, &st) == -1)) {
2022 packet_send_debug("No xauth program; cannot forward with spoofing.");
2023 return 0;
2024 }
ff027d84 2025 if (options.use_login) {
2026 packet_send_debug("X11 forwarding disabled; "
2027 "not compatible with UseLogin=yes.");
2028 return 0;
2029 }
b44de2b1 2030 if (s->display != NULL) {
8dcd9d5c 2031 debug("X11 display already set.");
2032 return 0;
2033 }
c9d0ad9b 2034 s->display_number = x11_create_display_inet(options.x11_display_offset,
ac151b18 2035 options.gateway_ports, s->single_connection);
c9d0ad9b 2036 if (s->display_number == -1) {
b44de2b1 2037 debug("x11_create_display_inet failed.");
8dcd9d5c 2038 return 0;
2039 }
c9d0ad9b 2040
2041 /* Set up a suitable value for the DISPLAY variable. */
2042 if (gethostname(hostname, sizeof(hostname)) < 0)
2043 fatal("gethostname: %.100s", strerror(errno));
2044 /*
2045 * auth_display must be used as the displayname when the
2046 * authorization entry is added with xauth(1). This will be
2047 * different than the DISPLAY string for localhost displays.
2048 */
2049 s->auth_display[1] = NULL;
2050 if (!options.gateway_ports) {
2051 struct utsname uts;
2052
2053 snprintf(display, sizeof display, "localhost:%d.%d",
2054 s->display_number, s->screen);
2055 snprintf(auth_display, sizeof auth_display, "%.400s/unix:%d.%d",
2056 hostname, s->display_number, s->screen);
2057 s->display = xstrdup(display);
2058 s->auth_display[0] = xstrdup(auth_display);
2059 /*
2060 * Xlib may use gethostbyname() or uname() hostname to
2061 * look up authorization data for FamilyLocal; see:
2062 * xc/lib/xtrans/Xtrans.c:TRANS(GetHostname)
2063 * We just add authorization entries with both
2064 * hostname and nodename if they are different.
2065 */
2066 if (uname(&uts) == -1)
2067 fatal("uname: %.100s", strerror(errno));
2068 if (strcmp(hostname, uts.nodename) != 0) {
2069 snprintf(auth_display, sizeof auth_display,
2070 "%.400s/unix:%d.%d", uts.nodename,
2071 s->display_number, s->screen);
2072 s->auth_display[1] = xstrdup(auth_display);
2073 }
2074 } else {
2075#ifdef IPADDR_IN_DISPLAY
2076 struct hostent *he;
2077 struct in_addr my_addr;
2078
2079 he = gethostbyname(hostname);
2080 if (he == NULL) {
2081 error("Can't get IP address for X11 DISPLAY.");
2082 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2083 return 0;
2084 }
2085 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2086 snprintf(display, sizeof display, "%.50s:%d.%d", inet_ntoa(my_addr),
2087 s->display_number, s->screen);
2088#else
2089 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
2090 s->display_number, s->screen);
2091#endif
2092 s->display = xstrdup(display);
2093 s->auth_display[0] = xstrdup(display);
2094 }
2095
8dcd9d5c 2096 return 1;
2097}
2098
396c147e 2099static void
59c97189 2100do_authenticated2(Authctxt *authctxt)
e78a59f5 2101{
57156994 2102 server_loop2(authctxt);
e78a59f5 2103}
This page took 0.518501 seconds and 5 git commands to generate.