]> andersk Git - gssapi-openssh.git/blame - openssh/session.c
o Update to version 1.7.
[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"
d03f4262 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"
350391c5 59#include "monitor_wrap.h"
3c0ef626 60
5598e598 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);
350391c5 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
350391c5 115login_cap_t *lc;
3c0ef626 116#endif
117
44a053a3 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 */
44a053a3 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;
e9702f7d 251 int success, type, screen_flag;
276b07a3 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. */
e9702f7d 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();
e9702f7d 273 packet_check_eom();
3c0ef626 274 if (compression_level < 1 || compression_level > 9) {
275 packet_send_debug("Received illegal compression level %d.",
e9702f7d 276 compression_level);
3c0ef626 277 break;
278 }
44a053a3 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 }
e9702f7d 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;
e9702f7d 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);
e9702f7d 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;
e9702f7d 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 */
e9702f7d 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);
e9702f7d 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 }
e9702f7d 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{
44a053a3 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
5598e598 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) {
d03f4262 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
d03f4262 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 }
d03f4262 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
5598e598 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) {
d03f4262 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
d03f4262 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);
d03f4262 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 }
d03f4262 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));
d03f4262 697 fromlen = sizeof(from);
3c0ef626 698 if (packet_connection_is_on_socket()) {
3c0ef626 699 if (getpeername(packet_get_connection_in(),
e9702f7d 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,
e9702f7d 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
e9702f7d 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(),
e9702f7d 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. */
350391c5 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),
d03f4262 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
d03f4262 788#ifndef NO_SSH_LASTLOG
350391c5 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;
350391c5 793 if (strcmp(s->hostname, "") == 0)
3c0ef626 794 printf("Last login: %s\r\n", time_string);
795 else
350391c5 796 printf("Last login: %s from %s\r\n", time_string,
797 s->hostname);
3c0ef626 798 }
d03f4262 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 */
5598e598 857void
3c0ef626 858child_set_env(char ***envp, u_int *envsizep, const char *name,
e9702f7d 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) {
276b07a3 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,
e9702f7d 903 const char *filename)
3c0ef626 904{
905 FILE *f;
906 char buf[4096];
907 char *cp, *value;
276b07a3 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)) {
276b07a3 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) {
276b07a3 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
e9702f7d 940void copy_environment(char **source, char ***env, u_int *envsize)
3c0ef626 941{
e9702f7d 942 char *var_name, *var_val;
3c0ef626 943 int i;
944
e9702f7d 945 if (source == NULL)
3c0ef626 946 return;
947
e9702f7d 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 }
e9702f7d 954 *var_val++ = '\0';
3c0ef626 955
e9702f7d 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 }
3c0ef626 961}
962
e9702f7d 963static char **
964do_setup_env(Session *s, const char *shell)
3c0ef626 965{
3c0ef626 966 char buf[256];
e9702f7d 967 u_int i, envsize;
3c0ef626 968 char **env;
e9702f7d 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 */
e9702f7d 981 copy_environment(environ, &env, &envsize);
3c0ef626 982#endif
983
5598e598 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
d03f4262 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 */
350391c5 1009# ifdef SUPERUSER_PATH
1010 child_set_env(&env, &envsize, "PATH",
1011 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1012# else
3c0ef626 1013 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
350391c5 1014# endif /* SUPERUSER_PATH */
3c0ef626 1015# endif /* HAVE_CYGWIN */
1016#endif /* HAVE_LOGIN_CAP */
1017
1018 snprintf(buf, sizeof buf, "%.200s/%.50s",
1019 _PATH_MAILDIR, pw->pw_name);
1020 child_set_env(&env, &envsize, "MAIL", buf);
1021
1022 /* Normal systems set SHELL by default. */
1023 child_set_env(&env, &envsize, "SHELL", shell);
1024 }
1025 if (getenv("TZ"))
1026 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1027
1028 /* Set custom environment options from RSA authentication. */
1029 if (!options.use_login) {
1030 while (custom_environment) {
1031 struct envstring *ce = custom_environment;
d03f4262 1032 char *str = ce->s;
e9702f7d 1033
d03f4262 1034 for (i = 0; str[i] != '=' && str[i]; i++)
3c0ef626 1035 ;
d03f4262 1036 if (str[i] == '=') {
1037 str[i] = 0;
1038 child_set_env(&env, &envsize, str, str + i + 1);
3c0ef626 1039 }
1040 custom_environment = ce->next;
1041 xfree(ce->s);
1042 xfree(ce);
1043 }
1044 }
1045
d03f4262 1046 /* SSH_CLIENT deprecated */
3c0ef626 1047 snprintf(buf, sizeof buf, "%.50s %d %d",
e9702f7d 1048 get_remote_ipaddr(), get_remote_port(), get_local_port());
3c0ef626 1049 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1050
d03f4262 1051 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1052 get_remote_ipaddr(), get_remote_port(),
1053 get_local_ipaddr(packet_get_connection_in()), get_local_port());
1054 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1055
3c0ef626 1056 if (s->ttyfd != -1)
1057 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1058 if (s->term)
1059 child_set_env(&env, &envsize, "TERM", s->term);
1060 if (s->display)
1061 child_set_env(&env, &envsize, "DISPLAY", s->display);
1062 if (original_command)
1063 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1064 original_command);
1065
d03f4262 1066#ifdef _UNICOS
1067 if (cray_tmpdir[0] != '\0')
1068 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1069#endif /* _UNICOS */
1070
3c0ef626 1071#ifdef _AIX
e9702f7d 1072 {
1073 char *cp;
1074
1075 if ((cp = getenv("AUTHSTATE")) != NULL)
1076 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1077 if ((cp = getenv("KRB5CCNAME")) != NULL)
1078 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1079 read_environment_file(&env, &envsize, "/etc/environment");
1080 }
3c0ef626 1081#endif
1082#ifdef KRB4
1083 if (s->authctxt->krb4_ticket_file)
1084 child_set_env(&env, &envsize, "KRBTKFILE",
1085 s->authctxt->krb4_ticket_file);
1086#endif
1087#ifdef KRB5
1088 if (s->authctxt->krb5_ticket_file)
1089 child_set_env(&env, &envsize, "KRB5CCNAME",
1090 s->authctxt->krb5_ticket_file);
1091#endif
1092#ifdef USE_PAM
d03f4262 1093 /*
1094 * Pull in any environment variables that may have
1095 * been set by PAM.
1096 */
1097 {
1098 char **p;
1099
1100 p = fetch_pam_environment();
1101 copy_environment(p, &env, &envsize);
1102 free_pam_environment(p);
1103 }
3c0ef626 1104#endif /* USE_PAM */
1105
44a053a3 1106 if (auth_sock_name != NULL)
3c0ef626 1107 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
44a053a3 1108 auth_sock_name);
3c0ef626 1109
1110 /* read $HOME/.ssh/environment. */
d03f4262 1111 if (options.permit_user_env && !options.use_login) {
3c0ef626 1112 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
d03f4262 1113 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
3c0ef626 1114 read_environment_file(&env, &envsize, buf);
1115 }
1116 if (debug_flag) {
1117 /* dump the environment */
1118 fprintf(stderr, "Environment:\n");
1119 for (i = 0; env[i]; i++)
1120 fprintf(stderr, " %.200s\n", env[i]);
1121 }
e9702f7d 1122 return env;
1123}
1124
1125/*
1126 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1127 * first in this order).
1128 */
1129static void
1130do_rc_files(Session *s, const char *shell)
1131{
1132 FILE *f = NULL;
1133 char cmd[1024];
1134 int do_xauth;
1135 struct stat st;
1136
1137 do_xauth =
1138 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1139
1140 /* ignore _PATH_SSH_USER_RC for subsystems */
1141 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1142 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1143 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1144 if (debug_flag)
1145 fprintf(stderr, "Running %s\n", cmd);
1146 f = popen(cmd, "w");
1147 if (f) {
1148 if (do_xauth)
1149 fprintf(f, "%s %s\n", s->auth_proto,
1150 s->auth_data);
1151 pclose(f);
1152 } else
1153 fprintf(stderr, "Could not run %s\n",
1154 _PATH_SSH_USER_RC);
1155 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1156 if (debug_flag)
1157 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1158 _PATH_SSH_SYSTEM_RC);
1159 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1160 if (f) {
1161 if (do_xauth)
1162 fprintf(f, "%s %s\n", s->auth_proto,
1163 s->auth_data);
1164 pclose(f);
1165 } else
1166 fprintf(stderr, "Could not run %s\n",
1167 _PATH_SSH_SYSTEM_RC);
1168 } else if (do_xauth && options.xauth_location != NULL) {
1169 /* Add authority data to .Xauthority if appropriate. */
1170 if (debug_flag) {
1171 fprintf(stderr,
44a053a3 1172 "Running %.500s add "
e9702f7d 1173 "%.100s %.100s %.100s\n",
1174 options.xauth_location, s->auth_display,
1175 s->auth_proto, s->auth_data);
1176 }
1177 snprintf(cmd, sizeof cmd, "%s -q -",
1178 options.xauth_location);
1179 f = popen(cmd, "w");
1180 if (f) {
1181 fprintf(f, "add %s %s %s\n",
1182 s->auth_display, s->auth_proto,
1183 s->auth_data);
1184 pclose(f);
1185 } else {
1186 fprintf(stderr, "Could not run %s\n",
1187 cmd);
1188 }
1189 }
1190}
1191
1192static void
1193do_nologin(struct passwd *pw)
1194{
1195 FILE *f = NULL;
1196 char buf[1024];
1197
1198#ifdef HAVE_LOGIN_CAP
1199 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1200 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1201 _PATH_NOLOGIN), "r");
1202#else
1203 if (pw->pw_uid)
1204 f = fopen(_PATH_NOLOGIN, "r");
1205#endif
1206 if (f) {
1207 /* /etc/nologin exists. Print its contents and exit. */
d03f4262 1208 log("User %.100s not allowed because %s exists",
1209 pw->pw_name, _PATH_NOLOGIN);
e9702f7d 1210 while (fgets(buf, sizeof(buf), f))
1211 fputs(buf, stderr);
1212 fclose(f);
1213 exit(254);
1214 }
1215}
1216
1217/* Set login name, uid, gid, and groups. */
350391c5 1218void
e9702f7d 1219do_setusercontext(struct passwd *pw)
1220{
1221#ifdef HAVE_CYGWIN
1222 if (is_winnt) {
1223#else /* HAVE_CYGWIN */
1224 if (getuid() == 0 || geteuid() == 0) {
1225#endif /* HAVE_CYGWIN */
44a053a3 1226#ifdef HAVE_SETPCRED
1227 setpcred(pw->pw_name);
1228#endif /* HAVE_SETPCRED */
e9702f7d 1229#ifdef HAVE_LOGIN_CAP
d03f4262 1230# ifdef __bsdi__
276b07a3 1231 setpgid(0, 0);
d03f4262 1232# endif
e9702f7d 1233 if (setusercontext(lc, pw, pw->pw_uid,
1234 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1235 perror("unable to set user context");
1236 exit(1);
1237 }
1238#else
1239# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1240 /* Sets login uid for accounting */
1241 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1242 error("setluid: %s", strerror(errno));
1243# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1244
1245 if (setlogin(pw->pw_name) < 0)
1246 error("setlogin failed: %s", strerror(errno));
1247 if (setgid(pw->pw_gid) < 0) {
1248 perror("setgid");
1249 exit(1);
1250 }
1251 /* Initialize the group list. */
1252 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1253 perror("initgroups");
1254 exit(1);
1255 }
1256 endgrent();
1257# ifdef USE_PAM
1258 /*
1259 * PAM credentials may take the form of supplementary groups.
1260 * These will have been wiped by the above initgroups() call.
1261 * Reestablish them here.
1262 */
1263 do_pam_setcred(0);
1264# endif /* USE_PAM */
1265# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1266 irix_setusercontext(pw);
1267# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
276b07a3 1268# ifdef _AIX
d03f4262 1269 aix_usrinfo(pw);
276b07a3 1270# endif /* _AIX */
e9702f7d 1271 /* Permanently switch to the desired uid. */
1272 permanently_set_uid(pw);
1273#endif
1274 }
1275 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1276 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1277}
1278
350391c5 1279static void
1280launch_login(struct passwd *pw, const char *hostname)
1281{
1282 /* Launch login(1). */
1283
44a053a3 1284 execl(LOGIN_PROGRAM, "login", "-h", hostname,
350391c5 1285#ifdef xxxLOGIN_NEEDS_TERM
44a053a3 1286 (s->term ? s->term : "unknown"),
350391c5 1287#endif /* LOGIN_NEEDS_TERM */
1288#ifdef LOGIN_NO_ENDOPT
1289 "-p", "-f", pw->pw_name, (char *)NULL);
1290#else
1291 "-p", "-f", "--", pw->pw_name, (char *)NULL);
1292#endif
1293
1294 /* Login couldn't be executed, die. */
1295
1296 perror("login");
1297 exit(1);
1298}
1299
e9702f7d 1300/*
1301 * Performs common processing for the child, such as setting up the
1302 * environment, closing extra file descriptors, setting the user and group
1303 * ids, and executing the command or shell.
1304 */
1305void
1306do_child(Session *s, const char *command)
1307{
1308 extern char **environ;
1309 char **env;
1310 char *argv[10];
1311 const char *shell, *shell0, *hostname = NULL;
1312 struct passwd *pw = s->pw;
1313 u_int i;
1314
1315 /* remove hostkey from the child's memory */
1316 destroy_sensitive_data();
1317
1318 /* login(1) is only called if we execute the login shell */
1319 if (options.use_login && command != NULL)
1320 options.use_login = 0;
1321
d03f4262 1322#ifdef _UNICOS
1323 cray_setup(pw->pw_uid, pw->pw_name, command);
1324#endif /* _UNICOS */
1325
e9702f7d 1326 /*
1327 * Login(1) does this as well, and it needs uid 0 for the "-h"
1328 * switch, so we let login(1) to this for us.
1329 */
1330 if (!options.use_login) {
1331#ifdef HAVE_OSF_SIA
1332 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
1333 if (!check_quietlogin(s, command))
1334 do_motd();
1335#else /* HAVE_OSF_SIA */
1336 do_nologin(pw);
e9702f7d 1337 do_setusercontext(pw);
1338#endif /* HAVE_OSF_SIA */
1339 }
1340
1341 /*
1342 * Get the shell from the password data. An empty shell field is
1343 * legal, and means /bin/sh.
1344 */
1345 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1346#ifdef HAVE_LOGIN_CAP
1347 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1348#endif
1349
1350 env = do_setup_env(s, shell);
1351
3c0ef626 1352 /* we have to stash the hostname before we close our socket. */
1353 if (options.use_login)
1354 hostname = get_remote_name_or_ip(utmp_len,
e9702f7d 1355 options.verify_reverse_mapping);
3c0ef626 1356 /*
1357 * Close the connection descriptors; note that this is the child, and
1358 * the server will still have the socket open, and it is important
1359 * that we do not shutdown it. Note that the descriptors cannot be
1360 * closed before building the environment, as we call
1361 * get_remote_ipaddr there.
1362 */
1363 if (packet_get_connection_in() == packet_get_connection_out())
1364 close(packet_get_connection_in());
1365 else {
1366 close(packet_get_connection_in());
1367 close(packet_get_connection_out());
1368 }
1369 /*
1370 * Close all descriptors related to channels. They will still remain
1371 * open in the parent.
1372 */
1373 /* XXX better use close-on-exec? -markus */
1374 channel_close_all();
1375
1376 /*
1377 * Close any extra file descriptors. Note that there may still be
1378 * descriptors left by system functions. They will be closed later.
1379 */
1380 endpwent();
1381
1382 /*
1383 * Close any extra open file descriptors so that we don\'t have them
1384 * hanging around in clients. Note that we want to do this after
1385 * initgroups, because at least on Solaris 2.3 it leaves file
1386 * descriptors open.
1387 */
1388 for (i = 3; i < 64; i++)
1389 close(i);
1390
1391 /*
e9702f7d 1392 * Must take new environment into use so that .ssh/rc,
1393 * /etc/ssh/sshrc and xauth are run in the proper environment.
3c0ef626 1394 */
1395 environ = env;
1396
1397#ifdef AFS
1398 /* Try to get AFS tokens for the local cell. */
1399 if (k_hasafs()) {
1400 char cell[64];
e9702f7d 1401
3c0ef626 1402 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1403 krb_afslog(cell, 0);
e9702f7d 1404
3c0ef626 1405 krb_afslog(0, 0);
1406 }
1407#endif /* AFS */
1408
1409 /* Change current directory to the user\'s home directory. */
1410 if (chdir(pw->pw_dir) < 0) {
1411 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1412 pw->pw_dir, strerror(errno));
1413#ifdef HAVE_LOGIN_CAP
1414 if (login_getcapbool(lc, "requirehome", 0))
1415 exit(1);
1416#endif
1417 }
1418
e9702f7d 1419 if (!options.use_login)
1420 do_rc_files(s, shell);
3c0ef626 1421
1422 /* restore SIGPIPE for child */
1423 signal(SIGPIPE, SIG_DFL);
1424
e9702f7d 1425 if (options.use_login) {
350391c5 1426 launch_login(pw, hostname);
1427 /* NEVERREACHED */
e9702f7d 1428 }
1429
1430 /* Get the last component of the shell name. */
1431 if ((shell0 = strrchr(shell, '/')) != NULL)
1432 shell0++;
1433 else
1434 shell0 = shell;
1435
3c0ef626 1436 /*
1437 * If we have no command, execute the shell. In this case, the shell
1438 * name to be passed in argv[0] is preceded by '-' to indicate that
1439 * this is a login shell.
1440 */
1441 if (!command) {
e9702f7d 1442 char argv0[256];
3c0ef626 1443
e9702f7d 1444 /* Start the shell. Set initial character to '-'. */
1445 argv0[0] = '-';
3c0ef626 1446
e9702f7d 1447 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1448 >= sizeof(argv0) - 1) {
1449 errno = EINVAL;
3c0ef626 1450 perror(shell);
1451 exit(1);
e9702f7d 1452 }
3c0ef626 1453
e9702f7d 1454 /* Execute the shell. */
1455 argv[0] = argv0;
1456 argv[1] = NULL;
1457 execve(shell, argv, env);
3c0ef626 1458
e9702f7d 1459 /* Executing the shell failed. */
1460 perror(shell);
1461 exit(1);
3c0ef626 1462 }
1463 /*
1464 * Execute the command using the user's shell. This uses the -c
1465 * option to execute the command.
1466 */
e9702f7d 1467 argv[0] = (char *) shell0;
3c0ef626 1468 argv[1] = "-c";
1469 argv[2] = (char *) command;
1470 argv[3] = NULL;
1471 execve(shell, argv, env);
1472 perror(shell);
1473 exit(1);
1474}
1475
1476Session *
1477session_new(void)
1478{
1479 int i;
1480 static int did_init = 0;
1481 if (!did_init) {
1482 debug("session_new: init");
e9702f7d 1483 for (i = 0; i < MAX_SESSIONS; i++) {
3c0ef626 1484 sessions[i].used = 0;
1485 }
1486 did_init = 1;
1487 }
e9702f7d 1488 for (i = 0; i < MAX_SESSIONS; i++) {
3c0ef626 1489 Session *s = &sessions[i];
1490 if (! s->used) {
1491 memset(s, 0, sizeof(*s));
1492 s->chanid = -1;
1493 s->ptyfd = -1;
1494 s->ttyfd = -1;
1495 s->used = 1;
1496 s->self = i;
1497 debug("session_new: session %d", i);
1498 return s;
1499 }
1500 }
1501 return NULL;
1502}
1503
1504static void
1505session_dump(void)
1506{
1507 int i;
e9702f7d 1508 for (i = 0; i < MAX_SESSIONS; i++) {
3c0ef626 1509 Session *s = &sessions[i];
44a053a3 1510 debug("dump: used %d session %d %p channel %d pid %ld",
3c0ef626 1511 s->used,
1512 s->self,
1513 s,
1514 s->chanid,
44a053a3 1515 (long)s->pid);
3c0ef626 1516 }
1517}
1518
1519int
1520session_open(Authctxt *authctxt, int chanid)
1521{
1522 Session *s = session_new();
1523 debug("session_open: channel %d", chanid);
1524 if (s == NULL) {
1525 error("no more sessions");
1526 return 0;
1527 }
1528 s->authctxt = authctxt;
1529 s->pw = authctxt->pw;
1530 if (s->pw == NULL)
1531 fatal("no user for session %d", s->self);
1532 debug("session_open: session %d: link with channel %d", s->self, chanid);
1533 s->chanid = chanid;
1534 return 1;
1535}
1536
350391c5 1537Session *
1538session_by_tty(char *tty)
1539{
1540 int i;
1541 for (i = 0; i < MAX_SESSIONS; i++) {
1542 Session *s = &sessions[i];
1543 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1544 debug("session_by_tty: session %d tty %s", i, tty);
1545 return s;
1546 }
1547 }
1548 debug("session_by_tty: unknown tty %.100s", tty);
1549 session_dump();
1550 return NULL;
1551}
1552
3c0ef626 1553static Session *
1554session_by_channel(int id)
1555{
1556 int i;
e9702f7d 1557 for (i = 0; i < MAX_SESSIONS; i++) {
3c0ef626 1558 Session *s = &sessions[i];
1559 if (s->used && s->chanid == id) {
1560 debug("session_by_channel: session %d channel %d", i, id);
1561 return s;
1562 }
1563 }
1564 debug("session_by_channel: unknown channel %d", id);
1565 session_dump();
1566 return NULL;
1567}
1568
1569static Session *
1570session_by_pid(pid_t pid)
1571{
1572 int i;
44a053a3 1573 debug("session_by_pid: pid %ld", (long)pid);
e9702f7d 1574 for (i = 0; i < MAX_SESSIONS; i++) {
3c0ef626 1575 Session *s = &sessions[i];
1576 if (s->used && s->pid == pid)
1577 return s;
1578 }
44a053a3 1579 error("session_by_pid: unknown pid %ld", (long)pid);
3c0ef626 1580 session_dump();
1581 return NULL;
1582}
1583
1584static int
1585session_window_change_req(Session *s)
1586{
1587 s->col = packet_get_int();
1588 s->row = packet_get_int();
1589 s->xpixel = packet_get_int();
1590 s->ypixel = packet_get_int();
e9702f7d 1591 packet_check_eom();
3c0ef626 1592 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1593 return 1;
1594}
1595
1596static int
1597session_pty_req(Session *s)
1598{
1599 u_int len;
1600 int n_bytes;
1601
1602 if (no_pty_flag) {
1603 debug("Allocating a pty not permitted for this authentication.");
1604 return 0;
1605 }
1606 if (s->ttyfd != -1) {
1607 packet_disconnect("Protocol error: you already have a pty.");
1608 return 0;
1609 }
350391c5 1610 /* Get the time and hostname when the user last logged in. */
1611 if (options.print_lastlog) {
1612 s->hostname[0] = '\0';
1613 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1614 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1615 }
3c0ef626 1616
1617 s->term = packet_get_string(&len);
1618
1619 if (compat20) {
1620 s->col = packet_get_int();
1621 s->row = packet_get_int();
1622 } else {
1623 s->row = packet_get_int();
1624 s->col = packet_get_int();
1625 }
1626 s->xpixel = packet_get_int();
1627 s->ypixel = packet_get_int();
1628
1629 if (strcmp(s->term, "") == 0) {
1630 xfree(s->term);
1631 s->term = NULL;
1632 }
1633
1634 /* Allocate a pty and open it. */
1635 debug("Allocating pty.");
350391c5 1636 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
3c0ef626 1637 if (s->term)
1638 xfree(s->term);
1639 s->term = NULL;
1640 s->ptyfd = -1;
1641 s->ttyfd = -1;
1642 error("session_pty_req: session %d alloc failed", s->self);
1643 return 0;
1644 }
1645 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1646
1647 /* for SSH1 the tty modes length is not given */
1648 if (!compat20)
1649 n_bytes = packet_remaining();
1650 tty_parse_modes(s->ttyfd, &n_bytes);
1651
1652 /*
1653 * Add a cleanup function to clear the utmp entry and record logout
1654 * time in case we call fatal() (e.g., the connection gets closed).
1655 */
1656 fatal_add_cleanup(session_pty_cleanup, (void *)s);
350391c5 1657 if (!use_privsep)
1658 pty_setowner(s->pw, s->tty);
3c0ef626 1659
1660 /* Set window size from the packet. */
1661 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1662
e9702f7d 1663 packet_check_eom();
3c0ef626 1664 session_proctitle(s);
1665 return 1;
1666}
1667
1668static int
1669session_subsystem_req(Session *s)
1670{
1671 struct stat st;
1672 u_int len;
1673 int success = 0;
1674 char *cmd, *subsys = packet_get_string(&len);
1675 int i;
1676
e9702f7d 1677 packet_check_eom();
1678 log("subsystem request for %.100s", subsys);
3c0ef626 1679
1680 for (i = 0; i < options.num_subsystems; i++) {
1681 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1682 cmd = options.subsystem_command[i];
1683 if (stat(cmd, &st) < 0) {
1684 error("subsystem: cannot stat %s: %s", cmd,
1685 strerror(errno));
1686 break;
1687 }
1688 debug("subsystem: exec() %s", cmd);
1689 s->is_subsystem = 1;
1690 do_exec(s, cmd);
1691 success = 1;
e9702f7d 1692 break;
3c0ef626 1693 }
1694 }
1695
1696 if (!success)
e9702f7d 1697 log("subsystem request for %.100s failed, subsystem not found",
3c0ef626 1698 subsys);
1699
1700 xfree(subsys);
1701 return success;
1702}
1703
1704static int
1705session_x11_req(Session *s)
1706{
1707 int success;
1708
1709 s->single_connection = packet_get_char();
1710 s->auth_proto = packet_get_string(NULL);
1711 s->auth_data = packet_get_string(NULL);
1712 s->screen = packet_get_int();
e9702f7d 1713 packet_check_eom();
3c0ef626 1714
1715 success = session_setup_x11fwd(s);
1716 if (!success) {
1717 xfree(s->auth_proto);
1718 xfree(s->auth_data);
1719 s->auth_proto = NULL;
1720 s->auth_data = NULL;
1721 }
1722 return success;
1723}
1724
1725static int
1726session_shell_req(Session *s)
1727{
e9702f7d 1728 packet_check_eom();
3c0ef626 1729 do_exec(s, NULL);
1730 return 1;
1731}
1732
1733static int
1734session_exec_req(Session *s)
1735{
1736 u_int len;
1737 char *command = packet_get_string(&len);
e9702f7d 1738 packet_check_eom();
3c0ef626 1739 do_exec(s, command);
1740 xfree(command);
1741 return 1;
1742}
1743
1744static int
1745session_auth_agent_req(Session *s)
1746{
1747 static int called = 0;
e9702f7d 1748 packet_check_eom();
3c0ef626 1749 if (no_agent_forwarding_flag) {
1750 debug("session_auth_agent_req: no_agent_forwarding_flag");
1751 return 0;
1752 }
1753 if (called) {
1754 return 0;
1755 } else {
1756 called = 1;
1757 return auth_input_request_forwarding(s->pw);
1758 }
1759}
1760
e9702f7d 1761int
1762session_input_channel_req(Channel *c, const char *rtype)
3c0ef626 1763{
3c0ef626 1764 int success = 0;
3c0ef626 1765 Session *s;
3c0ef626 1766
e9702f7d 1767 if ((s = session_by_channel(c->self)) == NULL) {
1768 log("session_input_channel_req: no session %d req %.100s",
1769 c->self, rtype);
1770 return 0;
1771 }
1772 debug("session_input_channel_req: session %d req %s", s->self, rtype);
3c0ef626 1773
1774 /*
1775 * a session is in LARVAL state until a shell, a command
1776 * or a subsystem is executed
1777 */
1778 if (c->type == SSH_CHANNEL_LARVAL) {
1779 if (strcmp(rtype, "shell") == 0) {
1780 success = session_shell_req(s);
1781 } else if (strcmp(rtype, "exec") == 0) {
1782 success = session_exec_req(s);
1783 } else if (strcmp(rtype, "pty-req") == 0) {
1784 success = session_pty_req(s);
1785 } else if (strcmp(rtype, "x11-req") == 0) {
1786 success = session_x11_req(s);
1787 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1788 success = session_auth_agent_req(s);
1789 } else if (strcmp(rtype, "subsystem") == 0) {
1790 success = session_subsystem_req(s);
1791 }
1792 }
1793 if (strcmp(rtype, "window-change") == 0) {
1794 success = session_window_change_req(s);
1795 }
e9702f7d 1796 return success;
3c0ef626 1797}
1798
1799void
1800session_set_fds(Session *s, int fdin, int fdout, int fderr)
1801{
1802 if (!compat20)
1803 fatal("session_set_fds: called for proto != 2.0");
1804 /*
1805 * now that have a child and a pipe to the child,
1806 * we can activate our channel and register the fd's
1807 */
1808 if (s->chanid == -1)
1809 fatal("no channel for session %d", s->self);
1810 channel_set_fds(s->chanid,
1811 fdout, fdin, fderr,
1812 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
e9702f7d 1813 1,
1814 CHAN_SES_WINDOW_DEFAULT);
3c0ef626 1815}
1816
1817/*
1818 * Function to perform pty cleanup. Also called if we get aborted abnormally
1819 * (e.g., due to a dropped connection).
1820 */
350391c5 1821void
1822session_pty_cleanup2(void *session)
3c0ef626 1823{
1824 Session *s = session;
1825
1826 if (s == NULL) {
1827 error("session_pty_cleanup: no session");
1828 return;
1829 }
1830 if (s->ttyfd == -1)
1831 return;
1832
1833 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1834
1835 /* Record that the user has logged out. */
1836 if (s->pid != 0)
e9702f7d 1837 record_logout(s->pid, s->tty, s->pw->pw_name);
3c0ef626 1838
1839 /* Release the pseudo-tty. */
350391c5 1840 if (getuid() == 0)
1841 pty_release(s->tty);
3c0ef626 1842
1843 /*
1844 * Close the server side of the socket pairs. We must do this after
1845 * the pty cleanup, so that another process doesn't get this pty
1846 * while we're still cleaning up.
1847 */
1848 if (close(s->ptymaster) < 0)
350391c5 1849 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
3c0ef626 1850
1851 /* unlink pty from session */
1852 s->ttyfd = -1;
1853}
1854
350391c5 1855void
1856session_pty_cleanup(void *session)
1857{
1858 PRIVSEP(session_pty_cleanup2(session));
1859}
1860
d03f4262 1861static char *
1862sig2name(int sig)
1863{
1864#define SSH_SIG(x) if (sig == SIG ## x) return #x
1865 SSH_SIG(ABRT);
1866 SSH_SIG(ALRM);
1867 SSH_SIG(FPE);
1868 SSH_SIG(HUP);
1869 SSH_SIG(ILL);
1870 SSH_SIG(INT);
1871 SSH_SIG(KILL);
1872 SSH_SIG(PIPE);
1873 SSH_SIG(QUIT);
1874 SSH_SIG(SEGV);
1875 SSH_SIG(TERM);
1876 SSH_SIG(USR1);
1877 SSH_SIG(USR2);
1878#undef SSH_SIG
1879 return "SIG@openssh.com";
1880}
1881
3c0ef626 1882static void
1883session_exit_message(Session *s, int status)
1884{
1885 Channel *c;
e9702f7d 1886
1887 if ((c = channel_lookup(s->chanid)) == NULL)
3c0ef626 1888 fatal("session_exit_message: session %d: no channel %d",
1889 s->self, s->chanid);
44a053a3 1890 debug("session_exit_message: session %d channel %d pid %ld",
1891 s->self, s->chanid, (long)s->pid);
3c0ef626 1892
1893 if (WIFEXITED(status)) {
e9702f7d 1894 channel_request_start(s->chanid, "exit-status", 0);
3c0ef626 1895 packet_put_int(WEXITSTATUS(status));
1896 packet_send();
1897 } else if (WIFSIGNALED(status)) {
e9702f7d 1898 channel_request_start(s->chanid, "exit-signal", 0);
d03f4262 1899 packet_put_cstring(sig2name(WTERMSIG(status)));
3c0ef626 1900#ifdef WCOREDUMP
1901 packet_put_char(WCOREDUMP(status));
1902#else /* WCOREDUMP */
1903 packet_put_char(0);
1904#endif /* WCOREDUMP */
1905 packet_put_cstring("");
1906 packet_put_cstring("");
1907 packet_send();
1908 } else {
1909 /* Some weird exit cause. Just exit. */
1910 packet_disconnect("wait returned status %04x.", status);
1911 }
1912
1913 /* disconnect channel */
1914 debug("session_exit_message: release channel %d", s->chanid);
1915 channel_cancel_cleanup(s->chanid);
1916 /*
1917 * emulate a write failure with 'chan_write_failed', nobody will be
1918 * interested in data we write.
1919 * Note that we must not call 'chan_read_failed', since there could
1920 * be some more data waiting in the pipe.
1921 */
1922 if (c->ostate != CHAN_OUTPUT_CLOSED)
1923 chan_write_failed(c);
1924 s->chanid = -1;
1925}
1926
350391c5 1927void
3c0ef626 1928session_close(Session *s)
1929{
44a053a3 1930 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
3c0ef626 1931 if (s->ttyfd != -1) {
1932 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1933 session_pty_cleanup(s);
1934 }
1935 if (s->term)
1936 xfree(s->term);
1937 if (s->display)
1938 xfree(s->display);
e9702f7d 1939 if (s->auth_display)
1940 xfree(s->auth_display);
3c0ef626 1941 if (s->auth_data)
1942 xfree(s->auth_data);
1943 if (s->auth_proto)
1944 xfree(s->auth_proto);
1945 s->used = 0;
1946 session_proctitle(s);
1947}
1948
1949void
1950session_close_by_pid(pid_t pid, int status)
1951{
1952 Session *s = session_by_pid(pid);
1953 if (s == NULL) {
44a053a3 1954 debug("session_close_by_pid: no session for pid %ld",
1955 (long)pid);
3c0ef626 1956 return;
1957 }
1958 if (s->chanid != -1)
1959 session_exit_message(s, status);
1960 session_close(s);
1961}
1962
1963/*
1964 * this is called when a channel dies before
1965 * the session 'child' itself dies
1966 */
1967void
1968session_close_by_channel(int id, void *arg)
1969{
1970 Session *s = session_by_channel(id);
1971 if (s == NULL) {
1972 debug("session_close_by_channel: no session for id %d", id);
1973 return;
1974 }
44a053a3 1975 debug("session_close_by_channel: channel %d child %ld",
1976 id, (long)s->pid);
3c0ef626 1977 if (s->pid != 0) {
1978 debug("session_close_by_channel: channel %d: has child", id);
1979 /*
1980 * delay detach of session, but release pty, since
1981 * the fd's to the child are already closed
1982 */
1983 if (s->ttyfd != -1) {
1984 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1985 session_pty_cleanup(s);
1986 }
1987 return;
1988 }
1989 /* detach by removing callback */
1990 channel_cancel_cleanup(s->chanid);
1991 s->chanid = -1;
1992 session_close(s);
1993}
1994
1995void
350391c5 1996session_destroy_all(void (*closefunc)(Session *))
3c0ef626 1997{
1998 int i;
e9702f7d 1999 for (i = 0; i < MAX_SESSIONS; i++) {
3c0ef626 2000 Session *s = &sessions[i];
350391c5 2001 if (s->used) {
2002 if (closefunc != NULL)
2003 closefunc(s);
2004 else
2005 session_close(s);
2006 }
3c0ef626 2007 }
2008}
2009
2010static char *
2011session_tty_list(void)
2012{
2013 static char buf[1024];
2014 int i;
2015 buf[0] = '\0';
e9702f7d 2016 for (i = 0; i < MAX_SESSIONS; i++) {
3c0ef626 2017 Session *s = &sessions[i];
2018 if (s->used && s->ttyfd != -1) {
2019 if (buf[0] != '\0')
2020 strlcat(buf, ",", sizeof buf);
2021 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2022 }
2023 }
2024 if (buf[0] == '\0')
2025 strlcpy(buf, "notty", sizeof buf);
2026 return buf;
2027}
2028
2029void
2030session_proctitle(Session *s)
2031{
2032 if (s->pw == NULL)
2033 error("no user for session %d", s->self);
2034 else
2035 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2036}
2037
2038int
2039session_setup_x11fwd(Session *s)
2040{
2041 struct stat st;
e9702f7d 2042 char display[512], auth_display[512];
2043 char hostname[MAXHOSTNAMELEN];
3c0ef626 2044
2045 if (no_x11_forwarding_flag) {
2046 packet_send_debug("X11 forwarding disabled in user configuration file.");
2047 return 0;
2048 }
2049 if (!options.x11_forwarding) {
2050 debug("X11 forwarding disabled in server configuration file.");
2051 return 0;
2052 }
2053 if (!options.xauth_location ||
2054 (stat(options.xauth_location, &st) == -1)) {
2055 packet_send_debug("No xauth program; cannot forward with spoofing.");
2056 return 0;
2057 }
2058 if (options.use_login) {
2059 packet_send_debug("X11 forwarding disabled; "
2060 "not compatible with UseLogin=yes.");
2061 return 0;
2062 }
2063 if (s->display != NULL) {
2064 debug("X11 display already set.");
2065 return 0;
2066 }
276b07a3 2067 if (x11_create_display_inet(options.x11_display_offset,
2068 options.x11_use_localhost, s->single_connection,
2069 &s->display_number) == -1) {
3c0ef626 2070 debug("x11_create_display_inet failed.");
2071 return 0;
2072 }
e9702f7d 2073
2074 /* Set up a suitable value for the DISPLAY variable. */
2075 if (gethostname(hostname, sizeof(hostname)) < 0)
2076 fatal("gethostname: %.100s", strerror(errno));
2077 /*
2078 * auth_display must be used as the displayname when the
2079 * authorization entry is added with xauth(1). This will be
2080 * different than the DISPLAY string for localhost displays.
2081 */
2082 if (options.x11_use_localhost) {
276b07a3 2083 snprintf(display, sizeof display, "localhost:%u.%u",
e9702f7d 2084 s->display_number, s->screen);
276b07a3 2085 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
e9702f7d 2086 s->display_number, s->screen);
2087 s->display = xstrdup(display);
2088 s->auth_display = xstrdup(auth_display);
2089 } else {
2090#ifdef IPADDR_IN_DISPLAY
2091 struct hostent *he;
2092 struct in_addr my_addr;
2093
2094 he = gethostbyname(hostname);
2095 if (he == NULL) {
2096 error("Can't get IP address for X11 DISPLAY.");
2097 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2098 return 0;
2099 }
2100 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
276b07a3 2101 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
e9702f7d 2102 s->display_number, s->screen);
2103#else
276b07a3 2104 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
e9702f7d 2105 s->display_number, s->screen);
2106#endif
2107 s->display = xstrdup(display);
2108 s->auth_display = xstrdup(display);
2109 }
2110
3c0ef626 2111 return 1;
2112}
2113
2114static void
2115do_authenticated2(Authctxt *authctxt)
2116{
2117 server_loop2(authctxt);
5598e598 2118#if defined(GSSAPI)
2119 ssh_gssapi_cleanup_creds(NULL);
2120#endif
3c0ef626 2121}
This page took 3.645045 seconds and 5 git commands to generate.