2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
11 * SSH2 support by Markus Friedl.
12 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
36 RCSID("$OpenBSD: session.c,v 1.150 2002/09/16 19:55:33 stevesk Exp $");
51 #include "auth-options.h"
52 #include "pathnames.h"
56 #include "serverloop.h"
59 #include "monitor_wrap.h"
67 #include <sys/cygwin.h>
68 #define is_winnt (GetVersion() < 0x80000000)
73 Session *session_new(void);
74 void session_set_fds(Session *, int, int, int);
75 void session_pty_cleanup(void *);
76 void session_proctitle(Session *);
77 int session_setup_x11fwd(Session *);
78 void do_exec_pty(Session *, const char *);
79 void do_exec_no_pty(Session *, const char *);
80 void do_exec(Session *, const char *);
81 void do_login(Session *, const char *);
82 #ifdef LOGIN_NEEDS_UTMPX
83 static void do_pre_login(Session *s);
85 void do_child(Session *, const char *);
87 int check_quietlogin(Session *, const char *);
89 static void do_authenticated1(Authctxt *);
90 static void do_authenticated2(Authctxt *);
92 static int session_pty_req(Session *);
95 extern ServerOptions options;
96 extern char *__progname;
97 extern int log_stderr;
98 extern int debug_flag;
99 extern u_int utmp_len;
100 extern int startup_pipe;
101 extern void destroy_sensitive_data(void);
103 /* original command from peer. */
104 const char *original_command = NULL;
107 #define MAX_SESSIONS 10
108 Session sessions[MAX_SESSIONS];
110 #ifdef WITH_AIXAUTHENTICATE
112 #endif /* WITH_AIXAUTHENTICATE */
114 #ifdef HAVE_LOGIN_CAP
118 /* Name and directory of socket for authentication agent forwarding. */
119 static char *auth_sock_name = NULL;
120 static char *auth_sock_dir = NULL;
122 /* removes the agent forwarding socket */
125 auth_sock_cleanup_proc(void *_pw)
127 struct passwd *pw = _pw;
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;
139 auth_input_request_forwarding(struct passwd * pw)
143 struct sockaddr_un sunaddr;
145 if (auth_sock_name != NULL) {
146 error("authentication forwarding requested twice.");
150 /* Temporarily drop privileged uid for mkdir/bind. */
151 temporarily_use_uid(pw);
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);
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));
163 xfree(auth_sock_name);
164 xfree(auth_sock_dir);
165 auth_sock_name = NULL;
166 auth_sock_dir = NULL;
169 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
170 auth_sock_dir, (long) getpid());
172 /* delete agent socket on fatal() */
173 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
175 /* Create the socket. */
176 sock = socket(AF_UNIX, SOCK_STREAM, 0);
178 packet_disconnect("socket: %.100s", strerror(errno));
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));
185 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
186 packet_disconnect("bind: %.100s", strerror(errno));
188 /* Restore the privileged uid. */
191 /* Start listening on the socket. */
192 if (listen(sock, 5) < 0)
193 packet_disconnect("listen: %.100s", strerror(errno));
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));
206 do_authenticated(Authctxt *authctxt)
209 * Cancel the alarm we set to limit the time taken for
213 if (startup_pipe != -1) {
218 /* setup the channel layer */
219 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
220 channel_permit_all_opens();
223 do_authenticated2(authctxt);
225 do_authenticated1(authctxt);
227 /* remove agent socket */
228 if (auth_sock_name != NULL)
229 auth_sock_cleanup_proc(authctxt->pw);
231 if (options.kerberos_ticket_cleanup)
232 krb4_cleanup_proc(authctxt);
235 if (options.kerberos_ticket_cleanup)
236 krb5_cleanup_proc(authctxt);
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.
247 do_authenticated1(Authctxt *authctxt)
251 int success, type, screen_flag;
252 int enable_compression_after_reply = 0;
253 u_int proto_len, data_len, dlen, compression_level = 0;
256 s->authctxt = authctxt;
257 s->pw = authctxt->pw;
260 * We stay in this loop until the client requests to execute a shell
266 /* Get a packet from the client. */
267 type = packet_read();
269 /* Process the packet. */
271 case SSH_CMSG_REQUEST_COMPRESSION:
272 compression_level = packet_get_int();
274 if (compression_level < 1 || compression_level > 9) {
275 packet_send_debug("Received illegal compression level %d.",
279 if (!options.compression) {
280 debug2("compression disabled");
283 /* Enable compression after we have responded with SUCCESS. */
284 enable_compression_after_reply = 1;
288 case SSH_CMSG_REQUEST_PTY:
289 success = session_pty_req(s);
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);
296 screen_flag = packet_get_protocol_flags() &
297 SSH_PROTOFLAG_SCREEN_NUMBER;
298 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
300 if (packet_remaining() == 4) {
302 debug2("Buggy client: "
303 "X11 screen flag missing");
304 s->screen = packet_get_int();
309 success = session_setup_x11fwd(s);
311 xfree(s->auth_proto);
313 s->auth_proto = NULL;
318 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
319 if (no_agent_forwarding_flag || compat13) {
320 debug("Authentication agent forwarding not permitted for this authentication.");
323 debug("Received authentication agent forwarding request.");
324 success = auth_input_request_forwarding(s->pw);
327 case SSH_CMSG_PORT_FORWARD_REQUEST:
328 if (no_port_forwarding_flag) {
329 debug("Port forwarding not permitted for this authentication.");
332 if (!options.allow_tcp_forwarding) {
333 debug("Port forwarding not permitted.");
336 debug("Received TCP/IP port forwarding request.");
337 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
341 case SSH_CMSG_MAX_PACKET_SIZE:
342 if (packet_set_maxsize(packet_get_int()) > 0)
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.");
351 char *kdata = packet_get_string(&dlen);
354 /* XXX - 0x41, see creds_to_radix version */
355 if (kdata[0] != 0x41) {
361 if (auth_krb5_tgt(s->authctxt, &tgt))
364 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
368 if (auth_krb4_tgt(s->authctxt, kdata))
371 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
377 #endif /* AFS || KRB5 */
380 case SSH_CMSG_HAVE_AFS_TOKEN:
381 if (!options.afs_token_passing || !k_hasafs()) {
382 verbose("AFS token passing disabled.");
384 /* Accept AFS token. */
385 char *token = packet_get_string(&dlen);
388 if (auth_afs_token(s->authctxt, token))
391 verbose("AFS token refused for %.100s",
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);
414 * Any unknown messages in this phase are ignored,
415 * and a failure message is returned.
417 log("Unknown packet type received after authentication: %d", type);
419 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
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);
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.
437 do_exec_no_pty(Session *s, const char *command)
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",
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",
454 #endif /* USE_PIPES */
456 fatal("do_exec_no_pty: no session");
458 session_proctitle(s);
461 temporarily_use_uid(s->pw);
462 ssh_gssapi_storecreds();
467 do_pam_session(s->pw->pw_name, NULL);
469 if (is_pam_password_change_required())
470 packet_disconnect("Password change required but no "
474 /* Fork the child. */
475 if ((pid = fork()) == 0) {
476 fatal_remove_all_cleanups();
478 /* Child. Reinitialize the log since the pid has changed. */
479 log_init(__progname, options.log_level, options.log_facility, log_stderr);
482 * Create a new session and process group since the 4.4BSD
483 * setlogin() affects the entire process group.
486 error("setsid failed: %.100s", strerror(errno));
490 * Redirect stdin. We close the parent side of the socket
491 * pair, and make the child side the standard input.
494 if (dup2(pin[0], 0) < 0)
495 perror("dup2 stdin");
498 /* Redirect stdout. */
500 if (dup2(pout[1], 1) < 0)
501 perror("dup2 stdout");
504 /* Redirect stderr. */
506 if (dup2(perr[1], 2) < 0)
507 perror("dup2 stderr");
509 #else /* USE_PIPES */
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.
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 */
526 cray_init_job(s->pw); /* set up cray jid and tmpdir */
529 /* Do processing for the child (exec command etc). */
530 do_child(s, command);
534 signal(WJSIGNAL, cray_job_termination_handler);
538 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
541 packet_disconnect("fork failed: %.100s", strerror(errno));
543 /* Set interactive/non-interactive mode. */
544 packet_set_interactive(s->display != NULL);
546 /* We are the parent. Close the child sides of the pipes. */
552 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
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]. */
558 #else /* USE_PIPES */
559 /* We are the parent. Close the child sides of the socket pairs. */
564 * Enter the interactive session. Note: server_loop must be able to
565 * handle the case that fdin and fdout are the same.
568 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
570 server_loop(pid, inout[1], inout[1], err[1]);
571 /* server_loop has closed inout[1] and err[1]. */
573 #endif /* USE_PIPES */
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.
583 do_exec_pty(Session *s, const char *command)
585 int fdout, ptyfd, ttyfd, ptymaster;
589 fatal("do_exec_pty: no session");
594 temporarily_use_uid(s->pw);
595 ssh_gssapi_storecreds();
600 do_pam_session(s->pw->pw_name, s->tty);
604 /* Fork the child. */
605 if ((pid = fork()) == 0) {
606 fatal_remove_all_cleanups();
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. */
613 /* Make the pseudo tty our controlling tty. */
614 pty_make_controlling_tty(&ttyfd, s->tty);
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));
624 /* Close the extra descriptor for the pseudo tty. */
627 /* record login, etc. similar to login(1) */
629 if (!(options.use_login && command == NULL)) {
631 cray_init_job(s->pw); /* set up cray jid and tmpdir */
633 do_login(s, command);
635 # ifdef LOGIN_NEEDS_UTMPX
641 /* Do common processing for the child, such as execing the command. */
642 do_child(s, command);
646 signal(WJSIGNAL, cray_job_termination_handler);
650 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
653 packet_disconnect("fork failed: %.100s", strerror(errno));
656 /* Parent. Close the slave side of the pseudo tty. */
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.
666 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
668 /* we keep a reference to the pty master */
669 ptymaster = dup(ptyfd);
671 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
672 s->ptymaster = ptymaster;
674 /* Enter interactive session. */
675 packet_set_interactive(1);
677 session_set_fds(s, ptyfd, fdout, -1);
679 server_loop(pid, ptyfd, fdout, -1);
680 /* server_loop _has_ closed ptyfd and fdout. */
684 #ifdef LOGIN_NEEDS_UTMPX
686 do_pre_login(Session *s)
689 struct sockaddr_storage from;
690 pid_t pid = getpid();
693 * Get IP address of client. If the connection is not a socket, let
694 * the address be 0.0.0.0.
696 memset(&from, 0, sizeof(from));
697 fromlen = sizeof(from);
698 if (packet_connection_is_on_socket()) {
699 if (getpeername(packet_get_connection_in(),
700 (struct sockaddr *) & from, &fromlen) < 0) {
701 debug("getpeername: %.100s", strerror(errno));
706 record_utmp_only(pid, s->tty, s->pw->pw_name,
707 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
708 (struct sockaddr *)&from);
713 * This is called to fork and execute a command. If another command is
714 * to be forced, execute that instead.
717 do_exec(Session *s, const char *command)
719 if (forced_command) {
720 original_command = command;
721 command = forced_command;
722 debug("Forced command '%.900s'", command);
726 do_exec_pty(s, command);
728 do_exec_no_pty(s, command);
730 original_command = NULL;
734 /* administrative, login(1)-like work */
736 do_login(Session *s, const char *command)
740 struct sockaddr_storage from;
741 struct passwd * pw = s->pw;
742 pid_t pid = getpid();
745 * Get IP address of client. If the connection is not a socket, let
746 * the address be 0.0.0.0.
748 memset(&from, 0, sizeof(from));
749 if (packet_connection_is_on_socket()) {
750 fromlen = sizeof(from);
751 if (getpeername(packet_get_connection_in(),
752 (struct sockaddr *) & from, &fromlen) < 0) {
753 debug("getpeername: %.100s", strerror(errno));
758 /* Record that there was a login on that tty from the remote host. */
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),
763 (struct sockaddr *)&from, fromlen);
767 * If password change is needed, do it now.
768 * This needs to occur before the ~/.hushlogin check.
770 if (is_pam_password_change_required()) {
771 print_pam_messages();
776 if (check_quietlogin(s, command))
780 if (!is_pam_password_change_required())
781 print_pam_messages();
783 #ifdef WITH_AIXAUTHENTICATE
784 if (aixloginmsg && *aixloginmsg)
785 printf("%s\n", aixloginmsg);
786 #endif /* WITH_AIXAUTHENTICATE */
788 #ifndef NO_SSH_LASTLOG
789 if (options.print_lastlog && s->last_login_time != 0) {
790 time_string = ctime(&s->last_login_time);
791 if (strchr(time_string, '\n'))
792 *strchr(time_string, '\n') = 0;
793 if (strcmp(s->hostname, "") == 0)
794 printf("Last login: %s\r\n", time_string);
796 printf("Last login: %s from %s\r\n", time_string,
799 #endif /* NO_SSH_LASTLOG */
805 * Display the message of the day.
813 if (options.print_motd) {
814 #ifdef HAVE_LOGIN_CAP
815 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
818 f = fopen("/etc/motd", "r");
821 while (fgets(buf, sizeof(buf), f))
830 * Check for quiet login, either .hushlogin or command given.
833 check_quietlogin(Session *s, const char *command)
836 struct passwd *pw = s->pw;
839 /* Return 1 if .hushlogin exists or a command given. */
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)
847 if (stat(buf, &st) >= 0)
854 * Sets the value of the given variable in the environment. If the variable
855 * already exists, its value is overriden.
858 child_set_env(char ***envp, u_int *envsizep, const char *name,
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.
870 namelen = strlen(name);
871 for (i = 0; env[i]; i++)
872 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
875 /* Reuse the slot. */
878 /* New variable. Expand if necessary. */
879 if (i >= (*envsizep) - 1) {
880 if (*envsizep >= 1000)
881 fatal("child_set_env: too many env vars,"
882 " skipping: %.100s", name);
884 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
886 /* Need to set the NULL pointer at end of array beyond the new slot. */
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);
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.
902 read_environment_file(char ***env, u_int *envsize,
903 const char *filename)
910 f = fopen(filename, "r");
914 while (fgets(buf, sizeof(buf), f)) {
916 fatal("Too many lines in environment file %s", filename);
917 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
919 if (!*cp || *cp == '#' || *cp == '\n')
921 if (strchr(cp, '\n'))
922 *strchr(cp, '\n') = '\0';
923 value = strchr(cp, '=');
925 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
930 * Replace the equals sign by nul, and advance value to
935 child_set_env(env, envsize, cp, value);
940 void copy_environment(char **source, char ***env, u_int *envsize)
942 char *var_name, *var_val;
948 for(i = 0; source[i] != NULL; i++) {
949 var_name = xstrdup(source[i]);
950 if ((var_val = strstr(var_name, "=")) == NULL) {
956 debug3("Copy environment: %s=%s", var_name, var_val);
957 child_set_env(env, envsize, var_name, var_val);
964 do_setup_env(Session *s, const char *shell)
969 struct passwd *pw = s->pw;
971 /* Initialize the environment. */
973 env = xmalloc(envsize * sizeof(char *));
978 * The Windows environment contains some setting which are
979 * important for a running system. They must not be dropped.
981 copy_environment(environ, &env, &envsize);
985 /* Allow any GSSAPI methods that we've used to alter
986 * the childs environment as they see fit
988 ssh_gssapi_do_child(&env,&envsize);
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
997 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
998 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1000 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1001 #else /* HAVE_LOGIN_CAP */
1002 # ifndef HAVE_CYGWIN
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.
1009 # ifdef SUPERUSER_PATH
1010 child_set_env(&env, &envsize, "PATH",
1011 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1013 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1014 # endif /* SUPERUSER_PATH */
1015 # endif /* HAVE_CYGWIN */
1016 #endif /* HAVE_LOGIN_CAP */
1018 snprintf(buf, sizeof buf, "%.200s/%.50s",
1019 _PATH_MAILDIR, pw->pw_name);
1020 child_set_env(&env, &envsize, "MAIL", buf);
1022 /* Normal systems set SHELL by default. */
1023 child_set_env(&env, &envsize, "SHELL", shell);
1026 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1028 /* Set custom environment options from RSA authentication. */
1029 if (!options.use_login) {
1030 while (custom_environment) {
1031 struct envstring *ce = custom_environment;
1034 for (i = 0; str[i] != '=' && str[i]; i++)
1036 if (str[i] == '=') {
1038 child_set_env(&env, &envsize, str, str + i + 1);
1040 custom_environment = ce->next;
1046 /* SSH_CLIENT deprecated */
1047 snprintf(buf, sizeof buf, "%.50s %d %d",
1048 get_remote_ipaddr(), get_remote_port(), get_local_port());
1049 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
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);
1057 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1059 child_set_env(&env, &envsize, "TERM", s->term);
1061 child_set_env(&env, &envsize, "DISPLAY", s->display);
1062 if (original_command)
1063 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1067 if (cray_tmpdir[0] != '\0')
1068 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1069 #endif /* _UNICOS */
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");
1083 if (s->authctxt->krb4_ticket_file)
1084 child_set_env(&env, &envsize, "KRBTKFILE",
1085 s->authctxt->krb4_ticket_file);
1088 if (s->authctxt->krb5_ticket_file)
1089 child_set_env(&env, &envsize, "KRB5CCNAME",
1090 s->authctxt->krb5_ticket_file);
1094 * Pull in any environment variables that may have
1100 p = fetch_pam_environment();
1101 copy_environment(p, &env, &envsize);
1102 free_pam_environment(p);
1104 #endif /* USE_PAM */
1106 if (auth_sock_name != NULL)
1107 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1110 /* read $HOME/.ssh/environment. */
1111 if (options.permit_user_env && !options.use_login) {
1112 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1113 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
1114 read_environment_file(&env, &envsize, buf);
1117 /* dump the environment */
1118 fprintf(stderr, "Environment:\n");
1119 for (i = 0; env[i]; i++)
1120 fprintf(stderr, " %.200s\n", env[i]);
1126 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1127 * first in this order).
1130 do_rc_files(Session *s, const char *shell)
1138 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
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);
1145 fprintf(stderr, "Running %s\n", cmd);
1146 f = popen(cmd, "w");
1149 fprintf(f, "%s %s\n", s->auth_proto,
1153 fprintf(stderr, "Could not run %s\n",
1155 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1157 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1158 _PATH_SSH_SYSTEM_RC);
1159 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1162 fprintf(f, "%s %s\n", s->auth_proto,
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. */
1172 "Running %.500s add "
1173 "%.100s %.100s %.100s\n",
1174 options.xauth_location, s->auth_display,
1175 s->auth_proto, s->auth_data);
1177 snprintf(cmd, sizeof cmd, "%s -q -",
1178 options.xauth_location);
1179 f = popen(cmd, "w");
1181 fprintf(f, "add %s %s %s\n",
1182 s->auth_display, s->auth_proto,
1186 fprintf(stderr, "Could not run %s\n",
1193 do_nologin(struct passwd *pw)
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");
1204 f = fopen(_PATH_NOLOGIN, "r");
1207 /* /etc/nologin exists. Print its contents and exit. */
1208 log("User %.100s not allowed because %s exists",
1209 pw->pw_name, _PATH_NOLOGIN);
1210 while (fgets(buf, sizeof(buf), f))
1217 /* Set login name, uid, gid, and groups. */
1219 do_setusercontext(struct passwd *pw)
1223 #else /* HAVE_CYGWIN */
1224 if (getuid() == 0 || geteuid() == 0) {
1225 #endif /* HAVE_CYGWIN */
1226 #ifdef HAVE_SETPCRED
1227 setpcred(pw->pw_name);
1228 #endif /* HAVE_SETPCRED */
1229 #ifdef HAVE_LOGIN_CAP
1233 if (setusercontext(lc, pw, pw->pw_uid,
1234 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1235 perror("unable to set user context");
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) */
1245 if (setlogin(pw->pw_name) < 0)
1246 error("setlogin failed: %s", strerror(errno));
1247 if (setgid(pw->pw_gid) < 0) {
1251 /* Initialize the group list. */
1252 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1253 perror("initgroups");
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.
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) */
1271 /* Permanently switch to the desired uid. */
1272 permanently_set_uid(pw);
1275 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1276 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1280 launch_login(struct passwd *pw, const char *hostname)
1282 /* Launch login(1). */
1284 execl(LOGIN_PROGRAM, "login", "-h", hostname,
1285 #ifdef xxxLOGIN_NEEDS_TERM
1286 (s->term ? s->term : "unknown"),
1287 #endif /* LOGIN_NEEDS_TERM */
1288 #ifdef LOGIN_NO_ENDOPT
1289 "-p", "-f", pw->pw_name, (char *)NULL);
1291 "-p", "-f", "--", pw->pw_name, (char *)NULL);
1294 /* Login couldn't be executed, die. */
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.
1306 do_child(Session *s, const char *command)
1308 extern char **environ;
1311 const char *shell, *shell0, *hostname = NULL;
1312 struct passwd *pw = s->pw;
1315 /* remove hostkey from the child's memory */
1316 destroy_sensitive_data();
1318 /* login(1) is only called if we execute the login shell */
1319 if (options.use_login && command != NULL)
1320 options.use_login = 0;
1323 cray_setup(pw->pw_uid, pw->pw_name, command);
1324 #endif /* _UNICOS */
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.
1330 if (!options.use_login) {
1332 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
1333 if (!check_quietlogin(s, command))
1335 #else /* HAVE_OSF_SIA */
1337 do_setusercontext(pw);
1338 #endif /* HAVE_OSF_SIA */
1342 * Get the shell from the password data. An empty shell field is
1343 * legal, and means /bin/sh.
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);
1350 env = do_setup_env(s, shell);
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,
1355 options.verify_reverse_mapping);
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.
1363 if (packet_get_connection_in() == packet_get_connection_out())
1364 close(packet_get_connection_in());
1366 close(packet_get_connection_in());
1367 close(packet_get_connection_out());
1370 * Close all descriptors related to channels. They will still remain
1371 * open in the parent.
1373 /* XXX better use close-on-exec? -markus */
1374 channel_close_all();
1377 * Close any extra file descriptors. Note that there may still be
1378 * descriptors left by system functions. They will be closed later.
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
1388 for (i = 3; i < 64; i++)
1392 * Must take new environment into use so that .ssh/rc,
1393 * /etc/ssh/sshrc and xauth are run in the proper environment.
1398 /* Try to get AFS tokens for the local cell. */
1402 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1403 krb_afslog(cell, 0);
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))
1419 if (!options.use_login)
1420 do_rc_files(s, shell);
1422 /* restore SIGPIPE for child */
1423 signal(SIGPIPE, SIG_DFL);
1425 if (options.use_login) {
1426 launch_login(pw, hostname);
1430 /* Get the last component of the shell name. */
1431 if ((shell0 = strrchr(shell, '/')) != NULL)
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.
1444 /* Start the shell. Set initial character to '-'. */
1447 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1448 >= sizeof(argv0) - 1) {
1454 /* Execute the shell. */
1457 execve(shell, argv, env);
1459 /* Executing the shell failed. */
1464 * Execute the command using the user's shell. This uses the -c
1465 * option to execute the command.
1467 argv[0] = (char *) shell0;
1469 argv[2] = (char *) command;
1471 execve(shell, argv, env);
1480 static int did_init = 0;
1482 debug("session_new: init");
1483 for (i = 0; i < MAX_SESSIONS; i++) {
1484 sessions[i].used = 0;
1488 for (i = 0; i < MAX_SESSIONS; i++) {
1489 Session *s = &sessions[i];
1491 memset(s, 0, sizeof(*s));
1497 debug("session_new: session %d", i);
1508 for (i = 0; i < MAX_SESSIONS; i++) {
1509 Session *s = &sessions[i];
1510 debug("dump: used %d session %d %p channel %d pid %ld",
1520 session_open(Authctxt *authctxt, int chanid)
1522 Session *s = session_new();
1523 debug("session_open: channel %d", chanid);
1525 error("no more sessions");
1528 s->authctxt = authctxt;
1529 s->pw = authctxt->pw;
1531 fatal("no user for session %d", s->self);
1532 debug("session_open: session %d: link with channel %d", s->self, chanid);
1538 session_by_tty(char *tty)
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);
1548 debug("session_by_tty: unknown tty %.100s", tty);
1554 session_by_channel(int id)
1557 for (i = 0; i < MAX_SESSIONS; i++) {
1558 Session *s = &sessions[i];
1559 if (s->used && s->chanid == id) {
1560 debug("session_by_channel: session %d channel %d", i, id);
1564 debug("session_by_channel: unknown channel %d", id);
1570 session_by_pid(pid_t pid)
1573 debug("session_by_pid: pid %ld", (long)pid);
1574 for (i = 0; i < MAX_SESSIONS; i++) {
1575 Session *s = &sessions[i];
1576 if (s->used && s->pid == pid)
1579 error("session_by_pid: unknown pid %ld", (long)pid);
1585 session_window_change_req(Session *s)
1587 s->col = packet_get_int();
1588 s->row = packet_get_int();
1589 s->xpixel = packet_get_int();
1590 s->ypixel = packet_get_int();
1592 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1597 session_pty_req(Session *s)
1603 debug("Allocating a pty not permitted for this authentication.");
1606 if (s->ttyfd != -1) {
1607 packet_disconnect("Protocol error: you already have a pty.");
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));
1617 s->term = packet_get_string(&len);
1620 s->col = packet_get_int();
1621 s->row = packet_get_int();
1623 s->row = packet_get_int();
1624 s->col = packet_get_int();
1626 s->xpixel = packet_get_int();
1627 s->ypixel = packet_get_int();
1629 if (strcmp(s->term, "") == 0) {
1634 /* Allocate a pty and open it. */
1635 debug("Allocating pty.");
1636 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
1642 error("session_pty_req: session %d alloc failed", s->self);
1645 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1647 /* for SSH1 the tty modes length is not given */
1649 n_bytes = packet_remaining();
1650 tty_parse_modes(s->ttyfd, &n_bytes);
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).
1656 fatal_add_cleanup(session_pty_cleanup, (void *)s);
1658 pty_setowner(s->pw, s->tty);
1660 /* Set window size from the packet. */
1661 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1664 session_proctitle(s);
1669 session_subsystem_req(Session *s)
1674 char *cmd, *subsys = packet_get_string(&len);
1678 log("subsystem request for %.100s", subsys);
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,
1688 debug("subsystem: exec() %s", cmd);
1689 s->is_subsystem = 1;
1697 log("subsystem request for %.100s failed, subsystem not found",
1705 session_x11_req(Session *s)
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();
1715 success = session_setup_x11fwd(s);
1717 xfree(s->auth_proto);
1718 xfree(s->auth_data);
1719 s->auth_proto = NULL;
1720 s->auth_data = NULL;
1726 session_shell_req(Session *s)
1734 session_exec_req(Session *s)
1737 char *command = packet_get_string(&len);
1739 do_exec(s, command);
1745 session_auth_agent_req(Session *s)
1747 static int called = 0;
1749 if (no_agent_forwarding_flag) {
1750 debug("session_auth_agent_req: no_agent_forwarding_flag");
1757 return auth_input_request_forwarding(s->pw);
1762 session_input_channel_req(Channel *c, const char *rtype)
1767 if ((s = session_by_channel(c->self)) == NULL) {
1768 log("session_input_channel_req: no session %d req %.100s",
1772 debug("session_input_channel_req: session %d req %s", s->self, rtype);
1775 * a session is in LARVAL state until a shell, a command
1776 * or a subsystem is executed
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);
1793 if (strcmp(rtype, "window-change") == 0) {
1794 success = session_window_change_req(s);
1800 session_set_fds(Session *s, int fdin, int fdout, int fderr)
1803 fatal("session_set_fds: called for proto != 2.0");
1805 * now that have a child and a pipe to the child,
1806 * we can activate our channel and register the fd's
1808 if (s->chanid == -1)
1809 fatal("no channel for session %d", s->self);
1810 channel_set_fds(s->chanid,
1812 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1814 CHAN_SES_WINDOW_DEFAULT);
1818 * Function to perform pty cleanup. Also called if we get aborted abnormally
1819 * (e.g., due to a dropped connection).
1822 session_pty_cleanup2(void *session)
1824 Session *s = session;
1827 error("session_pty_cleanup: no session");
1833 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1835 /* Record that the user has logged out. */
1837 record_logout(s->pid, s->tty, s->pw->pw_name);
1839 /* Release the pseudo-tty. */
1841 pty_release(s->tty);
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.
1848 if (close(s->ptymaster) < 0)
1849 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
1851 /* unlink pty from session */
1856 session_pty_cleanup(void *session)
1858 PRIVSEP(session_pty_cleanup2(session));
1864 #define SSH_SIG(x) if (sig == SIG ## x) return #x
1879 return "SIG@openssh.com";
1883 session_exit_message(Session *s, int status)
1887 if ((c = channel_lookup(s->chanid)) == NULL)
1888 fatal("session_exit_message: session %d: no channel %d",
1889 s->self, s->chanid);
1890 debug("session_exit_message: session %d channel %d pid %ld",
1891 s->self, s->chanid, (long)s->pid);
1893 if (WIFEXITED(status)) {
1894 channel_request_start(s->chanid, "exit-status", 0);
1895 packet_put_int(WEXITSTATUS(status));
1897 } else if (WIFSIGNALED(status)) {
1898 channel_request_start(s->chanid, "exit-signal", 0);
1899 packet_put_cstring(sig2name(WTERMSIG(status)));
1901 packet_put_char(WCOREDUMP(status));
1902 #else /* WCOREDUMP */
1904 #endif /* WCOREDUMP */
1905 packet_put_cstring("");
1906 packet_put_cstring("");
1909 /* Some weird exit cause. Just exit. */
1910 packet_disconnect("wait returned status %04x.", status);
1913 /* disconnect channel */
1914 debug("session_exit_message: release channel %d", s->chanid);
1915 channel_cancel_cleanup(s->chanid);
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.
1922 if (c->ostate != CHAN_OUTPUT_CLOSED)
1923 chan_write_failed(c);
1928 session_close(Session *s)
1930 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
1931 if (s->ttyfd != -1) {
1932 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1933 session_pty_cleanup(s);
1939 if (s->auth_display)
1940 xfree(s->auth_display);
1942 xfree(s->auth_data);
1944 xfree(s->auth_proto);
1946 session_proctitle(s);
1950 session_close_by_pid(pid_t pid, int status)
1952 Session *s = session_by_pid(pid);
1954 debug("session_close_by_pid: no session for pid %ld",
1958 if (s->chanid != -1)
1959 session_exit_message(s, status);
1964 * this is called when a channel dies before
1965 * the session 'child' itself dies
1968 session_close_by_channel(int id, void *arg)
1970 Session *s = session_by_channel(id);
1972 debug("session_close_by_channel: no session for id %d", id);
1975 debug("session_close_by_channel: channel %d child %ld",
1978 debug("session_close_by_channel: channel %d: has child", id);
1980 * delay detach of session, but release pty, since
1981 * the fd's to the child are already closed
1983 if (s->ttyfd != -1) {
1984 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1985 session_pty_cleanup(s);
1989 /* detach by removing callback */
1990 channel_cancel_cleanup(s->chanid);
1996 session_destroy_all(void (*closefunc)(Session *))
1999 for (i = 0; i < MAX_SESSIONS; i++) {
2000 Session *s = &sessions[i];
2002 if (closefunc != NULL)
2011 session_tty_list(void)
2013 static char buf[1024];
2016 for (i = 0; i < MAX_SESSIONS; i++) {
2017 Session *s = &sessions[i];
2018 if (s->used && s->ttyfd != -1) {
2020 strlcat(buf, ",", sizeof buf);
2021 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2025 strlcpy(buf, "notty", sizeof buf);
2030 session_proctitle(Session *s)
2033 error("no user for session %d", s->self);
2035 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2039 session_setup_x11fwd(Session *s)
2042 char display[512], auth_display[512];
2043 char hostname[MAXHOSTNAMELEN];
2045 if (no_x11_forwarding_flag) {
2046 packet_send_debug("X11 forwarding disabled in user configuration file.");
2049 if (!options.x11_forwarding) {
2050 debug("X11 forwarding disabled in server configuration file.");
2053 if (!options.xauth_location ||
2054 (stat(options.xauth_location, &st) == -1)) {
2055 packet_send_debug("No xauth program; cannot forward with spoofing.");
2058 if (options.use_login) {
2059 packet_send_debug("X11 forwarding disabled; "
2060 "not compatible with UseLogin=yes.");
2063 if (s->display != NULL) {
2064 debug("X11 display already set.");
2067 if (x11_create_display_inet(options.x11_display_offset,
2068 options.x11_use_localhost, s->single_connection,
2069 &s->display_number) == -1) {
2070 debug("x11_create_display_inet failed.");
2074 /* Set up a suitable value for the DISPLAY variable. */
2075 if (gethostname(hostname, sizeof(hostname)) < 0)
2076 fatal("gethostname: %.100s", strerror(errno));
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.
2082 if (options.x11_use_localhost) {
2083 snprintf(display, sizeof display, "localhost:%u.%u",
2084 s->display_number, s->screen);
2085 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
2086 s->display_number, s->screen);
2087 s->display = xstrdup(display);
2088 s->auth_display = xstrdup(auth_display);
2090 #ifdef IPADDR_IN_DISPLAY
2092 struct in_addr my_addr;
2094 he = gethostbyname(hostname);
2096 error("Can't get IP address for X11 DISPLAY.");
2097 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2100 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2101 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
2102 s->display_number, s->screen);
2104 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
2105 s->display_number, s->screen);
2107 s->display = xstrdup(display);
2108 s->auth_display = xstrdup(display);
2115 do_authenticated2(Authctxt *authctxt)
2117 server_loop2(authctxt);
2119 ssh_gssapi_cleanup_creds(NULL);