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