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