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