]> andersk Git - openssh.git/blame - session.c
- stevesk@cvs.openbsd.org 2001/04/05 15:48:18
[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"
46e3af7f 36RCSID("$OpenBSD: session.c,v 1.70 2001/04/05 15:48:18 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"
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)
9afbfcfa 491 do_pam_setcred(1);
266140a8 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);
9afbfcfa 606 do_pam_setcred(1);
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 680/* administrative, login(1)-like work */
681void
a22aff1f 682do_login(Session *s, const char *command)
c345cf9d 683{
684 FILE *f;
685 char *time_string;
686 char buf[256];
b5e300c2 687 char hostname[MAXHOSTNAMELEN];
c345cf9d 688 socklen_t fromlen;
689 struct sockaddr_storage from;
690 struct stat st;
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
a22aff1f 732 /* Done if .hushlogin exists or a command given. */
733 if (command != NULL)
734 return;
c345cf9d 735 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
2e73a022 736#ifdef HAVE_LOGIN_CAP
737 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
738#else
c345cf9d 739 if (stat(buf, &st) >= 0)
2e73a022 740#endif
c345cf9d 741 return;
742
743#ifdef USE_PAM
e11aab29 744 if (!is_pam_password_change_required())
2919e060 745 print_pam_messages();
c345cf9d 746#endif /* USE_PAM */
747#ifdef WITH_AIXAUTHENTICATE
748 if (aixloginmsg && *aixloginmsg)
749 printf("%s\n", aixloginmsg);
750#endif /* WITH_AIXAUTHENTICATE */
751
4f4648f9 752 if (options.print_lastlog && last_login_time != 0) {
c345cf9d 753 time_string = ctime(&last_login_time);
754 if (strchr(time_string, '\n'))
755 *strchr(time_string, '\n') = 0;
f478712e 756 if (strcmp(hostname, "") == 0)
c345cf9d 757 printf("Last login: %s\r\n", time_string);
758 else
bcbf86ec 759 printf("Last login: %s from %s\r\n", time_string, hostname);
c345cf9d 760 }
761 if (options.print_motd) {
2e73a022 762#ifdef HAVE_LOGIN_CAP
763 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
764 "/etc/motd"), "r");
765#else
c345cf9d 766 f = fopen("/etc/motd", "r");
2e73a022 767#endif
c345cf9d 768 if (f) {
769 while (fgets(buf, sizeof(buf), f))
770 fputs(buf, stdout);
771 fclose(f);
772 }
773 }
774}
775
7368a6c8 776/*
777 * Sets the value of the given variable in the environment. If the variable
778 * already exists, its value is overriden.
779 */
6ae2364d 780void
1e3b8b07 781child_set_env(char ***envp, u_int *envsizep, const char *name,
7368a6c8 782 const char *value)
783{
1e3b8b07 784 u_int i, namelen;
7368a6c8 785 char **env;
786
787 /*
788 * Find the slot where the value should be stored. If the variable
789 * already exists, we reuse the slot; otherwise we append a new slot
790 * at the end of the array, expanding if necessary.
791 */
792 env = *envp;
793 namelen = strlen(name);
794 for (i = 0; env[i]; i++)
795 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
796 break;
797 if (env[i]) {
798 /* Reuse the slot. */
799 xfree(env[i]);
800 } else {
801 /* New variable. Expand if necessary. */
802 if (i >= (*envsizep) - 1) {
803 (*envsizep) += 50;
804 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
805 }
806 /* Need to set the NULL pointer at end of array beyond the new slot. */
807 env[i + 1] = NULL;
808 }
809
810 /* Allocate space and format the variable in the appropriate slot. */
811 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
812 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
813}
814
815/*
816 * Reads environment variables from the given file and adds/overrides them
817 * into the environment. If the file does not exist, this does nothing.
818 * Otherwise, it must consist of empty lines, comments (line starts with '#')
819 * and assignments of the form name=value. No other forms are allowed.
820 */
6ae2364d 821void
1e3b8b07 822read_environment_file(char ***env, u_int *envsize,
7368a6c8 823 const char *filename)
824{
825 FILE *f;
826 char buf[4096];
827 char *cp, *value;
828
829 f = fopen(filename, "r");
830 if (!f)
831 return;
832
833 while (fgets(buf, sizeof(buf), f)) {
834 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
835 ;
836 if (!*cp || *cp == '#' || *cp == '\n')
837 continue;
838 if (strchr(cp, '\n'))
839 *strchr(cp, '\n') = '\0';
840 value = strchr(cp, '=');
841 if (value == NULL) {
842 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
843 continue;
844 }
71276795 845 /*
846 * Replace the equals sign by nul, and advance value to
847 * the value string.
848 */
7368a6c8 849 *value = '\0';
850 value++;
851 child_set_env(env, envsize, cp, value);
852 }
853 fclose(f);
854}
855
856#ifdef USE_PAM
857/*
858 * Sets any environment variables which have been specified by PAM
859 */
860void do_pam_environment(char ***env, int *envsize)
861{
862 char *equals, var_name[512], var_val[512];
863 char **pam_env;
864 int i;
865
866 if ((pam_env = fetch_pam_environment()) == NULL)
867 return;
2b87da3b 868
7368a6c8 869 for(i = 0; pam_env[i] != NULL; i++) {
870 if ((equals = strstr(pam_env[i], "=")) == NULL)
871 continue;
2b87da3b 872
7368a6c8 873 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
874 memset(var_name, '\0', sizeof(var_name));
875 memset(var_val, '\0', sizeof(var_val));
876
877 strncpy(var_name, pam_env[i], equals - pam_env[i]);
878 strcpy(var_val, equals + 1);
879
22d89d24 880 debug3("PAM environment: %s=%s", var_name, var_val);
7368a6c8 881
882 child_set_env(env, envsize, var_name, var_val);
883 }
884 }
885}
886#endif /* USE_PAM */
887
22d89d24 888#ifdef HAVE_CYGWIN
889void copy_environment(char ***env, int *envsize)
890{
891 char *equals, var_name[512], var_val[512];
892 int i;
893
894 for(i = 0; environ[i] != NULL; i++) {
895 if ((equals = strstr(environ[i], "=")) == NULL)
896 continue;
2b87da3b 897
22d89d24 898 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
899 memset(var_name, '\0', sizeof(var_name));
900 memset(var_val, '\0', sizeof(var_val));
901
902 strncpy(var_name, environ[i], equals - environ[i]);
903 strcpy(var_val, equals + 1);
904
905 debug3("Copy environment: %s=%s", var_name, var_val);
906
907 child_set_env(env, envsize, var_name, var_val);
908 }
909 }
910}
911#endif
912
5fa45897 913#if defined(HAVE_GETUSERATTR)
914/*
915 * AIX-specific login initialisation
916 */
917void set_limit(char *user, char *soft, char *hard, int resource, int mult)
918{
919 struct rlimit rlim;
56118702 920 int slim, hlim;
5fa45897 921
922 getrlimit(resource, &rlim);
923
56118702 924 slim = 0;
925 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
926 if (slim < 0) {
927 rlim.rlim_cur = RLIM_INFINITY;
928 } else if (slim != 0) {
929 /* See the wackiness below */
930 if (rlim.rlim_cur == slim * mult)
931 slim = 0;
932 else
933 rlim.rlim_cur = slim * mult;
934 }
935 }
5fa45897 936
56118702 937 hlim = 0;
938 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
939 if (hlim < 0) {
940 rlim.rlim_max = RLIM_INFINITY;
941 } else if (hlim != 0) {
942 rlim.rlim_max = hlim * mult;
943 }
944 }
5fa45897 945
56118702 946 /*
947 * XXX For cpu and fsize the soft limit is set to the hard limit
948 * if the hard limit is left at its default value and the soft limit
949 * is changed from its default value, either by requesting it
950 * (slim == 0) or by setting it to the current default. At least
951 * that's how rlogind does it. If you're confused you're not alone.
952 * Bug or feature? AIX 4.3.1.2
953 */
954 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
955 && hlim == 0 && slim != 0)
956 rlim.rlim_max = rlim.rlim_cur;
957 /* A specified hard limit limits the soft limit */
958 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
959 rlim.rlim_cur = rlim.rlim_max;
960 /* A soft limit can increase a hard limit */
961 else if (rlim.rlim_cur > rlim.rlim_max)
5fa45897 962 rlim.rlim_max = rlim.rlim_cur;
963
964 if (setrlimit(resource, &rlim) != 0)
56118702 965 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
5fa45897 966}
967
968void set_limits_from_userattr(char *user)
969{
970 int mask;
971 char buf[16];
972
973 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
974 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
975 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
976 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
977 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
978 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
979#if defined(S_UNOFILE)
980 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
981#endif
982
983 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
984 /* Convert decimal to octal */
985 (void) snprintf(buf, sizeof(buf), "%d", mask);
986 if (sscanf(buf, "%o", &mask) == 1)
987 umask(mask);
988 }
989}
990#endif /* defined(HAVE_GETUSERATTR) */
991
7368a6c8 992/*
993 * Performs common processing for the child, such as setting up the
994 * environment, closing extra file descriptors, setting the user and group
995 * ids, and executing the command or shell.
996 */
6ae2364d 997void
56b3e9ce 998do_child(Session *s, const char *command)
7368a6c8 999{
b5e300c2 1000 const char *shell, *hostname = NULL, *cp = NULL;
56b3e9ce 1001 struct passwd * pw = s->pw;
7368a6c8 1002 char buf[256];
6fc1e9f3 1003 char cmd[1024];
2e73a022 1004 FILE *f = NULL;
1e3b8b07 1005 u_int envsize, i;
7368a6c8 1006 char **env;
1007 extern char **environ;
1008 struct stat st;
1009 char *argv[10];
56b3e9ce 1010 int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
3206bb3b 1011#ifdef WITH_IRIX_PROJECT
1012 prid_t projid;
1013#endif /* WITH_IRIX_PROJECT */
d287c664 1014#ifdef WITH_IRIX_JOBS
1015 jid_t jid = 0;
1016#else
1017#ifdef WITH_IRIX_ARRAY
1018 int jid = 0;
1019#endif /* WITH_IRIX_ARRAY */
1020#endif /* WITH_IRIX_JOBS */
1021
fa649821 1022 /* login(1) is only called if we execute the login shell */
1023 if (options.use_login && command != NULL)
1024 options.use_login = 0;
1025
7368a6c8 1026#ifndef USE_PAM /* pam_nologin handles this */
2e73a022 1027 if (!options.use_login) {
1028# ifdef HAVE_LOGIN_CAP
1029 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1030 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1031 _PATH_NOLOGIN), "r");
1032# else /* HAVE_LOGIN_CAP */
1033 if (pw->pw_uid)
1034 f = fopen(_PATH_NOLOGIN, "r");
1035# endif /* HAVE_LOGIN_CAP */
1036 if (f) {
1037 /* /etc/nologin exists. Print its contents and exit. */
1038 while (fgets(buf, sizeof(buf), f))
1039 fputs(buf, stderr);
1040 fclose(f);
7368a6c8 1041 exit(254);
2e73a022 1042 }
7368a6c8 1043 }
1044#endif /* USE_PAM */
1045
2e73a022 1046 /* Set login name, uid, gid, and groups. */
7368a6c8 1047 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1048 switch, so we let login(1) to this for us. */
1049 if (!options.use_login) {
4d33e531 1050#ifdef HAVE_OSF_SIA
7a7101ec 1051 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
815800e1 1052#else /* HAVE_OSF_SIA */
3c62e7eb 1053#ifdef HAVE_CYGWIN
1054 if (is_winnt) {
1055#else
7368a6c8 1056 if (getuid() == 0 || geteuid() == 0) {
3c62e7eb 1057#endif
2e73a022 1058# ifdef HAVE_GETUSERATTR
5fa45897 1059 set_limits_from_userattr(pw->pw_name);
2e73a022 1060# endif /* HAVE_GETUSERATTR */
1061# ifdef HAVE_LOGIN_CAP
1062 if (setusercontext(lc, pw, pw->pw_uid,
1063 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1064 perror("unable to set user context");
1065 exit(1);
1066 }
af774732 1067#ifdef BSD_AUTH
1068 if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
1069 error("approval failure for %s", pw->pw_name);
1070 fprintf(stderr, "Approval failure");
1071 exit(1);
1072 }
1073#endif
2e73a022 1074# else /* HAVE_LOGIN_CAP */
73d6d7fa 1075#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1076 /* Sets login uid for accounting */
1077 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1078 error("setluid: %s", strerror(errno));
1079#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1080
2e73a022 1081 if (setlogin(pw->pw_name) < 0)
1082 error("setlogin failed: %s", strerror(errno));
7368a6c8 1083 if (setgid(pw->pw_gid) < 0) {
1084 perror("setgid");
1085 exit(1);
1086 }
1087 /* Initialize the group list. */
1088 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1089 perror("initgroups");
1090 exit(1);
1091 }
1092 endgrent();
9afbfcfa 1093# ifdef USE_PAM
1094 /*
1095 * PAM credentials may take the form of
1096 * supplementary groups. These will have been
1097 * wiped by the above initgroups() call.
1098 * Reestablish them here.
1099 */
1100 do_pam_setcred(0);
1101# endif /* USE_PAM */
d287c664 1102# ifdef WITH_IRIX_JOBS
1103 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1104 if (jid == -1) {
1105 fatal("Failed to create job container: %.100s",
1106 strerror(errno));
2b87da3b 1107 }
d287c664 1108# endif /* WITH_IRIX_JOBS */
2e73a022 1109# ifdef WITH_IRIX_ARRAY
3206bb3b 1110 /* initialize array session */
d287c664 1111 if (jid == 0) {
1112 if (newarraysess() != 0)
1113 fatal("Failed to set up new array session: %.100s",
1114 strerror(errno));
1115 }
2e73a022 1116# endif /* WITH_IRIX_ARRAY */
1117# ifdef WITH_IRIX_PROJECT
3206bb3b 1118 /* initialize irix project info */
1119 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1120 debug("Failed to get project id, using projid 0");
1121 projid = 0;
1122 }
3206bb3b 1123 if (setprid(projid))
1124 fatal("Failed to initialize project %d for %s: %.100s",
1125 (int)projid, pw->pw_name, strerror(errno));
2e73a022 1126# endif /* WITH_IRIX_PROJECT */
260d427b 1127#ifdef WITH_IRIX_AUDIT
1128 if (sysconf(_SC_AUDIT)) {
1129 debug("Setting sat id to %d", (int) pw->pw_uid);
1130 if (satsetid(pw->pw_uid))
1131 debug("error setting satid: %.100s", strerror(errno));
1132 }
1133#endif /* WITH_IRIX_AUDIT */
1134
8a968c25 1135#ifdef _AIX
1136 /*
1137 * AIX has a "usrinfo" area where logname and
1138 * other stuff is stored - a few applications
1139 * actually use this and die if it's not set
1140 */
ee110bfb 1141 if (s->ttyfd == -1)
1142 s->tty[0] = '\0';
f5f6020e 1143 cp = xmalloc(22 + strlen(s->tty) +
8a968c25 1144 2 * strlen(pw->pw_name));
1145 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
ee110bfb 1146 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
8a968c25 1147 if (usrinfo(SETUINFO, cp, i) == -1)
1148 fatal("Couldn't set usrinfo: %s",
1149 strerror(errno));
1150 debug3("AIX/UsrInfo: set len %d", i);
1151 xfree(cp);
1152#endif
1153
7368a6c8 1154 /* Permanently switch to the desired uid. */
1155 permanently_set_uid(pw->pw_uid);
2e73a022 1156# endif /* HAVE_LOGIN_CAP */
7368a6c8 1157 }
2e73a022 1158#endif /* HAVE_OSF_SIA */
1159
3c62e7eb 1160#ifdef HAVE_CYGWIN
1161 if (is_winnt)
1162#endif
7368a6c8 1163 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
14a9a859 1164 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
7368a6c8 1165 }
1166 /*
1167 * Get the shell from the password data. An empty shell field is
1168 * legal, and means /bin/sh.
1169 */
1170 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
2e73a022 1171#ifdef HAVE_LOGIN_CAP
1172 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1173#endif
7368a6c8 1174
1175#ifdef AFS
1176 /* Try to get AFS tokens for the local cell. */
1177 if (k_hasafs()) {
1178 char cell[64];
1179
1180 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1181 krb_afslog(cell, 0);
1182
1183 krb_afslog(0, 0);
1184 }
1185#endif /* AFS */
1186
1187 /* Initialize the environment. */
1188 envsize = 100;
1189 env = xmalloc(envsize * sizeof(char *));
1190 env[0] = NULL;
1191
3c62e7eb 1192#ifdef HAVE_CYGWIN
1193 /*
1194 * The Windows environment contains some setting which are
1195 * important for a running system. They must not be dropped.
1196 */
22d89d24 1197 copy_environment(&env, &envsize);
3c62e7eb 1198#endif
1199
7368a6c8 1200 if (!options.use_login) {
1201 /* Set basic environment. */
1202 child_set_env(&env, &envsize, "USER", pw->pw_name);
1203 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1204 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
2e73a022 1205#ifdef HAVE_LOGIN_CAP
1206 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1207 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
22d89d24 1208#else /* HAVE_LOGIN_CAP */
1209# ifndef HAVE_CYGWIN
3c62e7eb 1210 /*
1211 * There's no standard path on Windows. The path contains
1212 * important components pointing to the system directories,
1213 * needed for loading shared libraries. So the path better
1214 * remains intact here.
1215 */
7368a6c8 1216 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
22d89d24 1217# endif /* HAVE_CYGWIN */
1218#endif /* HAVE_LOGIN_CAP */
7368a6c8 1219
1220 snprintf(buf, sizeof buf, "%.200s/%.50s",
1221 _PATH_MAILDIR, pw->pw_name);
1222 child_set_env(&env, &envsize, "MAIL", buf);
1223
1224 /* Normal systems set SHELL by default. */
1225 child_set_env(&env, &envsize, "SHELL", shell);
1226 }
1227 if (getenv("TZ"))
1228 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1229
1230 /* Set custom environment options from RSA authentication. */
1231 while (custom_environment) {
1232 struct envstring *ce = custom_environment;
1233 char *s = ce->s;
1234 int i;
1235 for (i = 0; s[i] != '=' && s[i]; i++);
1236 if (s[i] == '=') {
1237 s[i] = 0;
1238 child_set_env(&env, &envsize, s, s + i + 1);
1239 }
1240 custom_environment = ce->next;
1241 xfree(ce->s);
1242 xfree(ce);
1243 }
1244
1245 snprintf(buf, sizeof buf, "%.50s %d %d",
1246 get_remote_ipaddr(), get_remote_port(), get_local_port());
1247 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1248
56b3e9ce 1249 if (s->ttyfd != -1)
1250 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1251 if (s->term)
1252 child_set_env(&env, &envsize, "TERM", s->term);
1253 if (s->display)
1254 child_set_env(&env, &envsize, "DISPLAY", s->display);
b5e300c2 1255 if (original_command)
1256 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1257 original_command);
7368a6c8 1258
1259#ifdef _AIX
22d89d24 1260 if ((cp = getenv("AUTHSTATE")) != NULL)
1261 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1262 if ((cp = getenv("KRB5CCNAME")) != NULL)
1263 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1264 read_environment_file(&env, &envsize, "/etc/environment");
7368a6c8 1265#endif
1266
1267#ifdef KRB4
1268 {
1269 extern char *ticket;
1270
1271 if (ticket)
1272 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1273 }
1274#endif /* KRB4 */
1275
1276#ifdef USE_PAM
1277 /* Pull in any environment variables that may have been set by PAM. */
1278 do_pam_environment(&env, &envsize);
1279#endif /* USE_PAM */
1280
7368a6c8 1281 if (xauthfile)
1282 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1283 if (auth_get_socket_name() != NULL)
1284 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1285 auth_get_socket_name());
1286
1287 /* read $HOME/.ssh/environment. */
1288 if (!options.use_login) {
71276795 1289 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1290 pw->pw_dir);
7368a6c8 1291 read_environment_file(&env, &envsize, buf);
1292 }
1293 if (debug_flag) {
1294 /* dump the environment */
1295 fprintf(stderr, "Environment:\n");
1296 for (i = 0; env[i]; i++)
1297 fprintf(stderr, " %.200s\n", env[i]);
1298 }
2e73a022 1299 /* we have to stash the hostname before we close our socket. */
1300 if (options.use_login)
46e3af7f 1301 hostname = get_remote_name_or_ip(utmp_len,
1302 options.reverse_mapping_check);
7368a6c8 1303 /*
1304 * Close the connection descriptors; note that this is the child, and
1305 * the server will still have the socket open, and it is important
1306 * that we do not shutdown it. Note that the descriptors cannot be
1307 * closed before building the environment, as we call
1308 * get_remote_ipaddr there.
1309 */
1310 if (packet_get_connection_in() == packet_get_connection_out())
1311 close(packet_get_connection_in());
1312 else {
1313 close(packet_get_connection_in());
1314 close(packet_get_connection_out());
1315 }
1316 /*
1317 * Close all descriptors related to channels. They will still remain
1318 * open in the parent.
1319 */
1320 /* XXX better use close-on-exec? -markus */
1321 channel_close_all();
1322
1323 /*
1324 * Close any extra file descriptors. Note that there may still be
1325 * descriptors left by system functions. They will be closed later.
1326 */
1327 endpwent();
1328
1329 /*
1330 * Close any extra open file descriptors so that we don\'t have them
1331 * hanging around in clients. Note that we want to do this after
1332 * initgroups, because at least on Solaris 2.3 it leaves file
1333 * descriptors open.
1334 */
1335 for (i = 3; i < 64; i++)
1336 close(i);
1337
1338 /* Change current directory to the user\'s home directory. */
2e73a022 1339 if (chdir(pw->pw_dir) < 0) {
7368a6c8 1340 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1341 pw->pw_dir, strerror(errno));
2e73a022 1342#ifdef HAVE_LOGIN_CAP
1343 if (login_getcapbool(lc, "requirehome", 0))
1344 exit(1);
1345#endif
1346 }
7368a6c8 1347
1348 /*
1349 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1350 * xauth are run in the proper environment.
1351 */
1352 environ = env;
1353
1354 /*
1355 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1356 * in this order).
1357 */
1358 if (!options.use_login) {
42f11eb2 1359 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
7368a6c8 1360 if (debug_flag)
56b3e9ce 1361 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1362 _PATH_SSH_USER_RC);
42f11eb2 1363 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
7368a6c8 1364 if (f) {
56b3e9ce 1365 if (do_xauth)
1366 fprintf(f, "%s %s\n", s->auth_proto,
1367 s->auth_data);
7368a6c8 1368 pclose(f);
1369 } else
56b3e9ce 1370 fprintf(stderr, "Could not run %s\n",
1371 _PATH_SSH_USER_RC);
42f11eb2 1372 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
7368a6c8 1373 if (debug_flag)
56b3e9ce 1374 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1375 _PATH_SSH_SYSTEM_RC);
7368a6c8 1376
42f11eb2 1377 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
7368a6c8 1378 if (f) {
56b3e9ce 1379 if (do_xauth)
1380 fprintf(f, "%s %s\n", s->auth_proto,
1381 s->auth_data);
7368a6c8 1382 pclose(f);
1383 } else
56b3e9ce 1384 fprintf(stderr, "Could not run %s\n",
1385 _PATH_SSH_SYSTEM_RC);
1386 } else if (do_xauth && options.xauth_location != NULL) {
7368a6c8 1387 /* Add authority data to .Xauthority if appropriate. */
56b3e9ce 1388 char *screen = strchr(s->display, ':');
1389
1390 if (debug_flag) {
1391 fprintf(stderr,
1392 "Running %.100s add "
1393 "%.100s %.100s %.100s\n",
1394 options.xauth_location, s->display,
1395 s->auth_proto, s->auth_data);
1396 if (screen != NULL)
71276795 1397 fprintf(stderr,
56b3e9ce 1398 "Adding %.*s/unix%s %s %s\n",
1399 (int)(screen - s->display),
1400 s->display, screen,
1401 s->auth_proto, s->auth_data);
1402 }
1403 snprintf(cmd, sizeof cmd, "%s -q -",
1404 options.xauth_location);
1405 f = popen(cmd, "w");
1406 if (f) {
1407 fprintf(f, "add %s %s %s\n", s->display,
1408 s->auth_proto, s->auth_data);
1409 if (screen != NULL)
1410 fprintf(f, "add %.*s/unix%s %s %s\n",
1411 (int)(screen - s->display),
1412 s->display, screen,
1413 s->auth_proto,
1414 s->auth_data);
1415 pclose(f);
1416 } else {
1417 fprintf(stderr, "Could not run %s\n",
1418 cmd);
7368a6c8 1419 }
1420 }
7368a6c8 1421 /* Get the last component of the shell name. */
1422 cp = strrchr(shell, '/');
1423 if (cp)
1424 cp++;
1425 else
1426 cp = shell;
1427 }
1d3c30db 1428
1429 /* restore SIGPIPE for child */
1430 signal(SIGPIPE, SIG_DFL);
1431
7368a6c8 1432 /*
1433 * If we have no command, execute the shell. In this case, the shell
1434 * name to be passed in argv[0] is preceded by '-' to indicate that
1435 * this is a login shell.
1436 */
1437 if (!command) {
1438 if (!options.use_login) {
1439 char buf[256];
1440
1441 /*
1442 * Check for mail if we have a tty and it was enabled
1443 * in server options.
1444 */
56b3e9ce 1445 if (s->ttyfd != -1 && options.check_mail) {
7368a6c8 1446 char *mailbox;
1447 struct stat mailstat;
56b3e9ce 1448
7368a6c8 1449 mailbox = getenv("MAIL");
1450 if (mailbox != NULL) {
71276795 1451 if (stat(mailbox, &mailstat) != 0 ||
1452 mailstat.st_size == 0)
7368a6c8 1453 printf("No mail.\n");
1454 else if (mailstat.st_mtime < mailstat.st_atime)
1455 printf("You have mail.\n");
1456 else
1457 printf("You have new mail.\n");
1458 }
1459 }
1460 /* Start the shell. Set initial character to '-'. */
1461 buf[0] = '-';
1462 strncpy(buf + 1, cp, sizeof(buf) - 1);
1463 buf[sizeof(buf) - 1] = 0;
1464
1465 /* Execute the shell. */
1466 argv[0] = buf;
1467 argv[1] = NULL;
1468 execve(shell, argv, env);
1469
1470 /* Executing the shell failed. */
1471 perror(shell);
1472 exit(1);
1473
1474 } else {
1475 /* Launch login(1). */
1476
2e73a022 1477 execl(LOGIN_PROGRAM, "login", "-h", hostname,
c345cf9d 1478 "-p", "-f", "--", pw->pw_name, NULL);
7368a6c8 1479
1480 /* Login couldn't be executed, die. */
1481
1482 perror("login");
1483 exit(1);
1484 }
1485 }
1486 /*
1487 * Execute the command using the user's shell. This uses the -c
1488 * option to execute the command.
1489 */
1490 argv[0] = (char *) cp;
1491 argv[1] = "-c";
1492 argv[2] = (char *) command;
1493 argv[3] = NULL;
1494 execve(shell, argv, env);
1495 perror(shell);
1496 exit(1);
1497}
1498
1499Session *
1500session_new(void)
1501{
1502 int i;
1503 static int did_init = 0;
1504 if (!did_init) {
1505 debug("session_new: init");
1506 for(i = 0; i < MAX_SESSIONS; i++) {
1507 sessions[i].used = 0;
1508 sessions[i].self = i;
1509 }
1510 did_init = 1;
1511 }
1512 for(i = 0; i < MAX_SESSIONS; i++) {
1513 Session *s = &sessions[i];
1514 if (! s->used) {
690d0d7f 1515 memset(s, 0, sizeof(*s));
7368a6c8 1516 s->chanid = -1;
1517 s->ptyfd = -1;
1518 s->ttyfd = -1;
7368a6c8 1519 s->used = 1;
02323c45 1520 debug("session_new: session %d", i);
7368a6c8 1521 return s;
1522 }
1523 }
1524 return NULL;
1525}
1526
1527void
1528session_dump(void)
1529{
1530 int i;
1531 for(i = 0; i < MAX_SESSIONS; i++) {
1532 Session *s = &sessions[i];
1533 debug("dump: used %d session %d %p channel %d pid %d",
1534 s->used,
1535 s->self,
1536 s,
1537 s->chanid,
1538 s->pid);
1539 }
1540}
1541
e78a59f5 1542int
1543session_open(int chanid)
1544{
1545 Session *s = session_new();
1546 debug("session_open: channel %d", chanid);
1547 if (s == NULL) {
1548 error("no more sessions");
1549 return 0;
1550 }
e78a59f5 1551 s->pw = auth_get_user();
1552 if (s->pw == NULL)
0725ea77 1553 fatal("no user for session %d", s->self);
0b242b12 1554 debug("session_open: session %d: link with channel %d", s->self, chanid);
1555 s->chanid = chanid;
e78a59f5 1556 return 1;
1557}
1558
1559Session *
1560session_by_channel(int id)
1561{
1562 int i;
1563 for(i = 0; i < MAX_SESSIONS; i++) {
1564 Session *s = &sessions[i];
1565 if (s->used && s->chanid == id) {
1566 debug("session_by_channel: session %d channel %d", i, id);
1567 return s;
1568 }
1569 }
1570 debug("session_by_channel: unknown channel %d", id);
1571 session_dump();
1572 return NULL;
1573}
1574
1575Session *
1576session_by_pid(pid_t pid)
1577{
1578 int i;
1579 debug("session_by_pid: pid %d", pid);
1580 for(i = 0; i < MAX_SESSIONS; i++) {
1581 Session *s = &sessions[i];
1582 if (s->used && s->pid == pid)
1583 return s;
1584 }
1585 error("session_by_pid: unknown pid %d", pid);
1586 session_dump();
1587 return NULL;
1588}
1589
1590int
1591session_window_change_req(Session *s)
1592{
1593 s->col = packet_get_int();
1594 s->row = packet_get_int();
1595 s->xpixel = packet_get_int();
1596 s->ypixel = packet_get_int();
6ae2364d 1597 packet_done();
e78a59f5 1598 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1599 return 1;
1600}
1601
1602int
1603session_pty_req(Session *s)
1604{
1e3b8b07 1605 u_int len;
6ae2364d 1606 char *term_modes; /* encoded terminal modes */
e78a59f5 1607
38c295d6 1608 if (no_pty_flag)
1609 return 0;
e78a59f5 1610 if (s->ttyfd != -1)
6ae2364d 1611 return 0;
e78a59f5 1612 s->term = packet_get_string(&len);
1613 s->col = packet_get_int();
1614 s->row = packet_get_int();
1615 s->xpixel = packet_get_int();
1616 s->ypixel = packet_get_int();
6ae2364d 1617 term_modes = packet_get_string(&len);
1618 packet_done();
e78a59f5 1619
1620 if (strcmp(s->term, "") == 0) {
1621 xfree(s->term);
1622 s->term = NULL;
1623 }
1624 /* Allocate a pty and open it. */
1625 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1626 xfree(s->term);
1627 s->term = NULL;
1628 s->ptyfd = -1;
1629 s->ttyfd = -1;
1630 error("session_pty_req: session %d alloc failed", s->self);
6ae2364d 1631 xfree(term_modes);
1632 return 0;
e78a59f5 1633 }
1634 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1635 /*
1636 * Add a cleanup function to clear the utmp entry and record logout
1637 * time in case we call fatal() (e.g., the connection gets closed).
1638 */
1639 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1640 pty_setowner(s->pw, s->tty);
1641 /* Get window size from the packet. */
1642 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1643
1d1ffb87 1644 session_proctitle(s);
1645
35484284 1646 /* XXX parse and set terminal modes */
1647 xfree(term_modes);
e78a59f5 1648 return 1;
1649}
1650
0b242b12 1651int
1652session_subsystem_req(Session *s)
1653{
1e3b8b07 1654 u_int len;
0b242b12 1655 int success = 0;
1656 char *subsys = packet_get_string(&len);
38c295d6 1657 int i;
0b242b12 1658
1659 packet_done();
1660 log("subsystem request for %s", subsys);
1661
38c295d6 1662 for (i = 0; i < options.num_subsystems; i++) {
1663 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1664 debug("subsystem: exec() %s", options.subsystem_command[i]);
e5d7a405 1665 s->is_subsystem = 1;
65068d16 1666 do_exec_no_pty(s, options.subsystem_command[i]);
38c295d6 1667 success = 1;
1668 }
1669 }
1670
1671 if (!success)
1672 log("subsystem request for %s failed, subsystem not found", subsys);
1673
0b242b12 1674 xfree(subsys);
1675 return success;
1676}
1677
1678int
1679session_x11_req(Session *s)
1680{
b5e300c2 1681 int fd;
089fbbd2 1682 if (no_x11_forwarding_flag) {
38c295d6 1683 debug("X11 forwarding disabled in user configuration file.");
1684 return 0;
1685 }
0b242b12 1686 if (!options.x11_forwarding) {
1687 debug("X11 forwarding disabled in server configuration file.");
1688 return 0;
1689 }
1690 if (xauthfile != NULL) {
1691 debug("X11 fwd already started.");
1692 return 0;
1693 }
1694
1695 debug("Received request for X11 forwarding with auth spoofing.");
1696 if (s->display != NULL)
1697 packet_disconnect("Protocol error: X11 display already set.");
1698
1699 s->single_connection = packet_get_char();
1700 s->auth_proto = packet_get_string(NULL);
1701 s->auth_data = packet_get_string(NULL);
1702 s->screen = packet_get_int();
1703 packet_done();
1704
1705 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1706 if (s->display == NULL) {
1707 xfree(s->auth_proto);
1708 xfree(s->auth_data);
1709 return 0;
1710 }
1711 xauthfile = xmalloc(MAXPATHLEN);
1712 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1713 temporarily_use_uid(s->pw->pw_uid);
1714 if (mkdtemp(xauthfile) == NULL) {
1715 restore_uid();
1716 error("private X11 dir: mkdtemp %s failed: %s",
1717 xauthfile, strerror(errno));
1718 xfree(xauthfile);
1719 xauthfile = NULL;
1720 xfree(s->auth_proto);
1721 xfree(s->auth_data);
1722 /* XXXX remove listening channels */
1723 return 0;
1724 }
1725 strlcat(xauthfile, "/cookies", MAXPATHLEN);
b5e300c2 1726 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1727 if (fd >= 0)
1728 close(fd);
0b242b12 1729 restore_uid();
1730 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1731 return 1;
1732}
1733
38c295d6 1734int
1735session_shell_req(Session *s)
1736{
1737 /* if forced_command == NULL, the shell is execed */
1738 char *shell = forced_command;
1739 packet_done();
38c295d6 1740 if (s->ttyfd == -1)
65068d16 1741 do_exec_no_pty(s, shell);
38c295d6 1742 else
65068d16 1743 do_exec_pty(s, shell);
38c295d6 1744 return 1;
1745}
1746
1747int
1748session_exec_req(Session *s)
1749{
1e3b8b07 1750 u_int len;
38c295d6 1751 char *command = packet_get_string(&len);
1752 packet_done();
1753 if (forced_command) {
b5e300c2 1754 original_command = command;
38c295d6 1755 command = forced_command;
1756 debug("Forced command '%.500s'", forced_command);
1757 }
38c295d6 1758 if (s->ttyfd == -1)
65068d16 1759 do_exec_no_pty(s, command);
38c295d6 1760 else
65068d16 1761 do_exec_pty(s, command);
38c295d6 1762 if (forced_command == NULL)
1763 xfree(command);
1764 return 1;
1765}
1766
fa08c86b 1767int
1768session_auth_agent_req(Session *s)
1769{
1770 static int called = 0;
1771 packet_done();
d343d900 1772 if (no_agent_forwarding_flag) {
1773 debug("session_auth_agent_req: no_agent_forwarding_flag");
1774 return 0;
1775 }
fa08c86b 1776 if (called) {
1777 return 0;
1778 } else {
1779 called = 1;
1780 return auth_input_request_forwarding(s->pw);
1781 }
1782}
1783
e78a59f5 1784void
1785session_input_channel_req(int id, void *arg)
1786{
1e3b8b07 1787 u_int len;
e78a59f5 1788 int reply;
1789 int success = 0;
1790 char *rtype;
1791 Session *s;
1792 Channel *c;
1793
1794 rtype = packet_get_string(&len);
1795 reply = packet_get_char();
1796
1797 s = session_by_channel(id);
1798 if (s == NULL)
1799 fatal("session_input_channel_req: channel %d: no session", id);
1800 c = channel_lookup(id);
1801 if (c == NULL)
1802 fatal("session_input_channel_req: channel %d: bad channel", id);
1803
1804 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1805 s->self, id, rtype, reply);
1806
1807 /*
e5d7a405 1808 * a session is in LARVAL state until a shell, a command
1809 * or a subsystem is executed
e78a59f5 1810 */
1811 if (c->type == SSH_CHANNEL_LARVAL) {
1812 if (strcmp(rtype, "shell") == 0) {
38c295d6 1813 success = session_shell_req(s);
e78a59f5 1814 } else if (strcmp(rtype, "exec") == 0) {
38c295d6 1815 success = session_exec_req(s);
e78a59f5 1816 } else if (strcmp(rtype, "pty-req") == 0) {
35484284 1817 success = session_pty_req(s);
0b242b12 1818 } else if (strcmp(rtype, "x11-req") == 0) {
1819 success = session_x11_req(s);
fa08c86b 1820 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1821 success = session_auth_agent_req(s);
0b242b12 1822 } else if (strcmp(rtype, "subsystem") == 0) {
1823 success = session_subsystem_req(s);
e78a59f5 1824 }
1825 }
1826 if (strcmp(rtype, "window-change") == 0) {
1827 success = session_window_change_req(s);
1828 }
1829
1830 if (reply) {
1831 packet_start(success ?
1832 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1833 packet_put_int(c->remote_id);
1834 packet_send();
1835 }
1836 xfree(rtype);
1837}
1838
1839void
1840session_set_fds(Session *s, int fdin, int fdout, int fderr)
1841{
1842 if (!compat20)
1843 fatal("session_set_fds: called for proto != 2.0");
1844 /*
1845 * now that have a child and a pipe to the child,
1846 * we can activate our channel and register the fd's
1847 */
1848 if (s->chanid == -1)
1849 fatal("no channel for session %d", s->self);
1850 channel_set_fds(s->chanid,
1851 fdout, fdin, fderr,
a22aff1f 1852 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1853 1);
e78a59f5 1854}
1855
7368a6c8 1856void
1857session_pty_cleanup(Session *s)
1858{
1859 if (s == NULL || s->ttyfd == -1)
1860 return;
1861
0725ea77 1862 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
7368a6c8 1863
1864 /* Cancel the cleanup function. */
1865 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1866
1867 /* Record that the user has logged out. */
1868 record_logout(s->pid, s->tty);
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));
1880}
e78a59f5 1881
1882void
1883session_exit_message(Session *s, int status)
1884{
1885 Channel *c;
1886 if (s == NULL)
1887 fatal("session_close: no session");
1888 c = channel_lookup(s->chanid);
1889 if (c == NULL)
1890 fatal("session_close: session %d: no channel %d",
1891 s->self, s->chanid);
1892 debug("session_exit_message: session %d channel %d pid %d",
1893 s->self, s->chanid, s->pid);
1894
1895 if (WIFEXITED(status)) {
1896 channel_request_start(s->chanid,
1897 "exit-status", 0);
1898 packet_put_int(WEXITSTATUS(status));
1899 packet_send();
1900 } else if (WIFSIGNALED(status)) {
1901 channel_request_start(s->chanid,
1902 "exit-signal", 0);
1903 packet_put_int(WTERMSIG(status));
a64009ad 1904#ifdef WCOREDUMP
e78a59f5 1905 packet_put_char(WCOREDUMP(status));
a64009ad 1906#else /* WCOREDUMP */
1907 packet_put_char(0);
1908#endif /* WCOREDUMP */
e78a59f5 1909 packet_put_cstring("");
1910 packet_put_cstring("");
1911 packet_send();
1912 } else {
1913 /* Some weird exit cause. Just exit. */
1914 packet_disconnect("wait returned status %04x.", status);
1915 }
1916
1917 /* disconnect channel */
1918 debug("session_exit_message: release channel %d", s->chanid);
1919 channel_cancel_cleanup(s->chanid);
9da5c3c9 1920 /*
1921 * emulate a write failure with 'chan_write_failed', nobody will be
1922 * interested in data we write.
1923 * Note that we must not call 'chan_read_failed', since there could
1924 * be some more data waiting in the pipe.
1925 */
0b242b12 1926 if (c->ostate != CHAN_OUTPUT_CLOSED)
1927 chan_write_failed(c);
e78a59f5 1928 s->chanid = -1;
1929}
1930
1931void
1932session_free(Session *s)
1933{
1934 debug("session_free: session %d pid %d", s->self, s->pid);
1935 if (s->term)
1936 xfree(s->term);
1937 if (s->display)
1938 xfree(s->display);
1939 if (s->auth_data)
1940 xfree(s->auth_data);
1941 if (s->auth_proto)
1942 xfree(s->auth_proto);
1943 s->used = 0;
1944}
1945
1946void
1947session_close(Session *s)
1948{
1949 session_pty_cleanup(s);
1950 session_free(s);
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) {
1959 debug("session_close_by_pid: no session for pid %d", s->pid);
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) {
1976 debug("session_close_by_channel: no session for channel %d", id);
1977 return;
1978 }
1979 /* disconnect channel */
1980 channel_cancel_cleanup(s->chanid);
1981 s->chanid = -1;
1982
1983 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1984 if (s->pid == 0) {
1985 /* close session immediately */
1986 session_close(s);
1987 } else {
1988 /* notify child, delay session cleanup */
512ad3c0 1989 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
e78a59f5 1990 error("session_close_by_channel: kill %d: %s",
1991 s->pid, strerror(errno));
1992 }
1993}
1994
1d1ffb87 1995char *
1996session_tty_list(void)
1997{
1998 static char buf[1024];
1999 int i;
2000 buf[0] = '\0';
2001 for(i = 0; i < MAX_SESSIONS; i++) {
2002 Session *s = &sessions[i];
2003 if (s->used && s->ttyfd != -1) {
2004 if (buf[0] != '\0')
2005 strlcat(buf, ",", sizeof buf);
2006 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2007 }
2008 }
2009 if (buf[0] == '\0')
2010 strlcpy(buf, "notty", sizeof buf);
2011 return buf;
2012}
2013
2014void
2015session_proctitle(Session *s)
2016{
2017 if (s->pw == NULL)
2018 error("no user for session %d", s->self);
2019 else
2020 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2021}
2022
e78a59f5 2023void
59c97189 2024do_authenticated2(Authctxt *authctxt)
e78a59f5 2025{
bb5639fe 2026
e78a59f5 2027 server_loop2();
34bce9a5 2028 if (xauthfile)
2029 xauthfile_cleanup_proc(NULL);
e78a59f5 2030}
This page took 2.267898 seconds and 5 git commands to generate.