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