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