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