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.162 2003/08/28 12:54:34 markus Exp $");
51 #include "auth-options.h"
52 #include "pathnames.h"
56 #include "serverloop.h"
59 #include "monitor_wrap.h"
67 Session *session_new(void);
68 void session_set_fds(Session *, int, int, int);
69 void session_pty_cleanup(void *);
70 void session_proctitle(Session *);
71 int session_setup_x11fwd(Session *);
72 void do_exec_pty(Session *, const char *);
73 void do_exec_no_pty(Session *, const char *);
74 void do_exec(Session *, const char *);
75 void do_login(Session *, const char *);
76 #ifdef LOGIN_NEEDS_UTMPX
77 static void do_pre_login(Session *s);
79 void do_child(Session *, const char *);
81 int check_quietlogin(Session *, const char *);
83 static void do_authenticated1(Authctxt *);
84 static void do_authenticated2(Authctxt *);
86 static int session_pty_req(Session *);
89 extern ServerOptions options;
90 extern char *__progname;
91 extern int log_stderr;
92 extern int debug_flag;
93 extern u_int utmp_len;
94 extern int startup_pipe;
95 extern void destroy_sensitive_data(void);
96 extern Buffer loginmsg;
98 /* original command from peer. */
99 const char *original_command = NULL;
102 #define MAX_SESSIONS 10
103 Session sessions[MAX_SESSIONS];
105 #ifdef HAVE_LOGIN_CAP
109 /* Name and directory of socket for authentication agent forwarding. */
110 static char *auth_sock_name = NULL;
111 static char *auth_sock_dir = NULL;
113 /* removes the agent forwarding socket */
116 auth_sock_cleanup_proc(void *_pw)
118 struct passwd *pw = _pw;
120 if (auth_sock_name != NULL) {
121 temporarily_use_uid(pw);
122 unlink(auth_sock_name);
123 rmdir(auth_sock_dir);
124 auth_sock_name = NULL;
130 auth_input_request_forwarding(struct passwd * pw)
134 struct sockaddr_un sunaddr;
136 if (auth_sock_name != NULL) {
137 error("authentication forwarding requested twice.");
141 /* Temporarily drop privileged uid for mkdir/bind. */
142 temporarily_use_uid(pw);
144 /* Allocate a buffer for the socket name, and format the name. */
145 auth_sock_name = xmalloc(MAXPATHLEN);
146 auth_sock_dir = xmalloc(MAXPATHLEN);
147 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
149 /* Create private directory for socket */
150 if (mkdtemp(auth_sock_dir) == NULL) {
151 packet_send_debug("Agent forwarding disabled: "
152 "mkdtemp() failed: %.100s", strerror(errno));
154 xfree(auth_sock_name);
155 xfree(auth_sock_dir);
156 auth_sock_name = NULL;
157 auth_sock_dir = NULL;
160 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
161 auth_sock_dir, (long) getpid());
163 /* delete agent socket on fatal() */
164 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
166 /* Create the socket. */
167 sock = socket(AF_UNIX, SOCK_STREAM, 0);
169 packet_disconnect("socket: %.100s", strerror(errno));
171 /* Bind it to the name. */
172 memset(&sunaddr, 0, sizeof(sunaddr));
173 sunaddr.sun_family = AF_UNIX;
174 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
176 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
177 packet_disconnect("bind: %.100s", strerror(errno));
179 /* Restore the privileged uid. */
182 /* Start listening on the socket. */
183 if (listen(sock, 5) < 0)
184 packet_disconnect("listen: %.100s", strerror(errno));
186 /* Allocate a channel for the authentication agent socket. */
187 nc = channel_new("auth socket",
188 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
189 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
190 0, "auth socket", 1);
191 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
197 do_authenticated(Authctxt *authctxt)
199 setproctitle("%s", authctxt->pw->pw_name);
202 * Cancel the alarm we set to limit the time taken for
206 if (startup_pipe != -1) {
211 /* setup the channel layer */
212 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
213 channel_permit_all_opens();
216 do_authenticated2(authctxt);
218 do_authenticated1(authctxt);
220 /* remove agent socket */
221 if (auth_sock_name != NULL)
222 auth_sock_cleanup_proc(authctxt->pw);
224 if (options.kerberos_ticket_cleanup)
225 krb5_cleanup_proc(authctxt);
230 * Prepares for an interactive session. This is called after the user has
231 * been successfully authenticated. During this message exchange, pseudo
232 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
233 * are requested, etc.
236 do_authenticated1(Authctxt *authctxt)
240 int success, type, screen_flag;
241 int enable_compression_after_reply = 0;
242 u_int proto_len, data_len, dlen, compression_level = 0;
245 s->authctxt = authctxt;
246 s->pw = authctxt->pw;
249 * We stay in this loop until the client requests to execute a shell
255 /* Get a packet from the client. */
256 type = packet_read();
258 /* Process the packet. */
260 case SSH_CMSG_REQUEST_COMPRESSION:
261 compression_level = packet_get_int();
263 if (compression_level < 1 || compression_level > 9) {
264 packet_send_debug("Received illegal compression level %d.",
268 if (!options.compression) {
269 debug2("compression disabled");
272 /* Enable compression after we have responded with SUCCESS. */
273 enable_compression_after_reply = 1;
277 case SSH_CMSG_REQUEST_PTY:
278 success = session_pty_req(s);
281 case SSH_CMSG_X11_REQUEST_FORWARDING:
282 s->auth_proto = packet_get_string(&proto_len);
283 s->auth_data = packet_get_string(&data_len);
285 screen_flag = packet_get_protocol_flags() &
286 SSH_PROTOFLAG_SCREEN_NUMBER;
287 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
289 if (packet_remaining() == 4) {
291 debug2("Buggy client: "
292 "X11 screen flag missing");
293 s->screen = packet_get_int();
298 success = session_setup_x11fwd(s);
300 xfree(s->auth_proto);
302 s->auth_proto = NULL;
307 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
308 if (no_agent_forwarding_flag || compat13) {
309 debug("Authentication agent forwarding not permitted for this authentication.");
312 debug("Received authentication agent forwarding request.");
313 success = auth_input_request_forwarding(s->pw);
316 case SSH_CMSG_PORT_FORWARD_REQUEST:
317 if (no_port_forwarding_flag) {
318 debug("Port forwarding not permitted for this authentication.");
321 if (!options.allow_tcp_forwarding) {
322 debug("Port forwarding not permitted.");
325 debug("Received TCP/IP port forwarding request.");
326 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
330 case SSH_CMSG_MAX_PACKET_SIZE:
331 if (packet_set_maxsize(packet_get_int()) > 0)
335 case SSH_CMSG_EXEC_SHELL:
336 case SSH_CMSG_EXEC_CMD:
337 if (type == SSH_CMSG_EXEC_CMD) {
338 command = packet_get_string(&dlen);
339 debug("Exec command '%.500s'", command);
351 * Any unknown messages in this phase are ignored,
352 * and a failure message is returned.
354 logit("Unknown packet type received after authentication: %d", type);
356 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
360 /* Enable compression now that we have replied if appropriate. */
361 if (enable_compression_after_reply) {
362 enable_compression_after_reply = 0;
363 packet_start_compression(compression_level);
369 * This is called to fork and execute a command when we have no tty. This
370 * will call do_child from the child, and server_loop from the parent after
371 * setting up file descriptors and such.
374 do_exec_no_pty(Session *s, const char *command)
379 int pin[2], pout[2], perr[2];
380 /* Allocate pipes for communicating with the program. */
381 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
382 packet_disconnect("Could not create pipes: %.100s",
384 #else /* USE_PIPES */
385 int inout[2], err[2];
386 /* Uses socket pairs to communicate with the program. */
387 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
388 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
389 packet_disconnect("Could not create socket pairs: %.100s",
391 #endif /* USE_PIPES */
393 fatal("do_exec_no_pty: no session");
395 session_proctitle(s);
398 temporarily_use_uid(s->pw);
399 ssh_gssapi_storecreds();
404 if (options.use_pam) {
405 do_pam_session(s->pw->pw_name, NULL);
407 if (is_pam_password_change_required())
408 packet_disconnect("Password change required but no "
413 /* Fork the child. */
414 if ((pid = fork()) == 0) {
415 fatal_remove_all_cleanups();
417 /* Child. Reinitialize the log since the pid has changed. */
418 log_init(__progname, options.log_level, options.log_facility, log_stderr);
421 * Create a new session and process group since the 4.4BSD
422 * setlogin() affects the entire process group.
425 error("setsid failed: %.100s", strerror(errno));
429 * Redirect stdin. We close the parent side of the socket
430 * pair, and make the child side the standard input.
433 if (dup2(pin[0], 0) < 0)
434 perror("dup2 stdin");
437 /* Redirect stdout. */
439 if (dup2(pout[1], 1) < 0)
440 perror("dup2 stdout");
443 /* Redirect stderr. */
445 if (dup2(perr[1], 2) < 0)
446 perror("dup2 stderr");
448 #else /* USE_PIPES */
450 * Redirect stdin, stdout, and stderr. Stdin and stdout will
451 * use the same socket, as some programs (particularly rdist)
452 * seem to depend on it.
456 if (dup2(inout[0], 0) < 0) /* stdin */
457 perror("dup2 stdin");
458 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
459 perror("dup2 stdout");
460 if (dup2(err[0], 2) < 0) /* stderr */
461 perror("dup2 stderr");
462 #endif /* USE_PIPES */
465 cray_init_job(s->pw); /* set up cray jid and tmpdir */
468 /* Do processing for the child (exec command etc). */
469 do_child(s, command);
473 signal(WJSIGNAL, cray_job_termination_handler);
477 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
480 packet_disconnect("fork failed: %.100s", strerror(errno));
482 /* Set interactive/non-interactive mode. */
483 packet_set_interactive(s->display != NULL);
485 /* We are the parent. Close the child sides of the pipes. */
491 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
493 /* Enter the interactive session. */
494 server_loop(pid, pin[1], pout[0], perr[0]);
495 /* server_loop has closed pin[1], pout[0], and perr[0]. */
497 #else /* USE_PIPES */
498 /* We are the parent. Close the child sides of the socket pairs. */
503 * Enter the interactive session. Note: server_loop must be able to
504 * handle the case that fdin and fdout are the same.
507 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
509 server_loop(pid, inout[1], inout[1], err[1]);
510 /* server_loop has closed inout[1] and err[1]. */
512 #endif /* USE_PIPES */
516 * This is called to fork and execute a command when we have a tty. This
517 * will call do_child from the child, and server_loop from the parent after
518 * setting up file descriptors, controlling tty, updating wtmp, utmp,
519 * lastlog, and other such operations.
522 do_exec_pty(Session *s, const char *command)
524 int fdout, ptyfd, ttyfd, ptymaster;
528 fatal("do_exec_pty: no session");
533 temporarily_use_uid(s->pw);
534 ssh_gssapi_storecreds();
539 if (options.use_pam) {
540 do_pam_session(s->pw->pw_name, s->tty);
545 /* Fork the child. */
546 if ((pid = fork()) == 0) {
547 fatal_remove_all_cleanups();
549 /* Child. Reinitialize the log because the pid has changed. */
550 log_init(__progname, options.log_level, options.log_facility, log_stderr);
551 /* Close the master side of the pseudo tty. */
554 /* Make the pseudo tty our controlling tty. */
555 pty_make_controlling_tty(&ttyfd, s->tty);
557 /* Redirect stdin/stdout/stderr from the pseudo tty. */
558 if (dup2(ttyfd, 0) < 0)
559 error("dup2 stdin: %s", strerror(errno));
560 if (dup2(ttyfd, 1) < 0)
561 error("dup2 stdout: %s", strerror(errno));
562 if (dup2(ttyfd, 2) < 0)
563 error("dup2 stderr: %s", strerror(errno));
565 /* Close the extra descriptor for the pseudo tty. */
568 /* record login, etc. similar to login(1) */
570 if (!(options.use_login && command == NULL)) {
572 cray_init_job(s->pw); /* set up cray jid and tmpdir */
574 do_login(s, command);
576 # ifdef LOGIN_NEEDS_UTMPX
582 /* Do common processing for the child, such as execing the command. */
583 do_child(s, command);
587 signal(WJSIGNAL, cray_job_termination_handler);
591 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
594 packet_disconnect("fork failed: %.100s", strerror(errno));
597 /* Parent. Close the slave side of the pseudo tty. */
601 * Create another descriptor of the pty master side for use as the
602 * standard input. We could use the original descriptor, but this
603 * simplifies code in server_loop. The descriptor is bidirectional.
607 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
609 /* we keep a reference to the pty master */
610 ptymaster = dup(ptyfd);
612 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
613 s->ptymaster = ptymaster;
615 /* Enter interactive session. */
616 packet_set_interactive(1);
618 session_set_fds(s, ptyfd, fdout, -1);
620 server_loop(pid, ptyfd, fdout, -1);
621 /* server_loop _has_ closed ptyfd and fdout. */
625 #ifdef LOGIN_NEEDS_UTMPX
627 do_pre_login(Session *s)
630 struct sockaddr_storage from;
631 pid_t pid = getpid();
634 * Get IP address of client. If the connection is not a socket, let
635 * the address be 0.0.0.0.
637 memset(&from, 0, sizeof(from));
638 fromlen = sizeof(from);
639 if (packet_connection_is_on_socket()) {
640 if (getpeername(packet_get_connection_in(),
641 (struct sockaddr *) & from, &fromlen) < 0) {
642 debug("getpeername: %.100s", strerror(errno));
647 record_utmp_only(pid, s->tty, s->pw->pw_name,
648 get_remote_name_or_ip(utmp_len, options.use_dns),
649 (struct sockaddr *)&from, fromlen);
654 * This is called to fork and execute a command. If another command is
655 * to be forced, execute that instead.
658 do_exec(Session *s, const char *command)
660 if (forced_command) {
661 original_command = command;
662 command = forced_command;
663 debug("Forced command '%.900s'", command);
667 do_exec_pty(s, command);
669 do_exec_no_pty(s, command);
671 original_command = NULL;
675 /* administrative, login(1)-like work */
677 do_login(Session *s, const char *command)
681 struct sockaddr_storage from;
682 struct passwd * pw = s->pw;
683 pid_t pid = getpid();
686 * Get IP address of client. If the connection is not a socket, let
687 * the address be 0.0.0.0.
689 memset(&from, 0, sizeof(from));
690 fromlen = sizeof(from);
691 if (packet_connection_is_on_socket()) {
692 if (getpeername(packet_get_connection_in(),
693 (struct sockaddr *) & from, &fromlen) < 0) {
694 debug("getpeername: %.100s", strerror(errno));
699 /* Record that there was a login on that tty from the remote host. */
701 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
702 get_remote_name_or_ip(utmp_len,
704 (struct sockaddr *)&from, fromlen);
708 * If password change is needed, do it now.
709 * This needs to occur before the ~/.hushlogin check.
711 if (options.use_pam && is_pam_password_change_required()) {
712 print_pam_messages();
714 /* XXX - signal [net] parent to enable forwardings */
718 if (check_quietlogin(s, command))
722 if (options.use_pam && !is_pam_password_change_required())
723 print_pam_messages();
726 /* display post-login message */
727 if (buffer_len(&loginmsg) > 0) {
728 buffer_append(&loginmsg, "\0", 1);
729 printf("%s\n", (char *)buffer_ptr(&loginmsg));
731 buffer_free(&loginmsg);
733 #ifndef NO_SSH_LASTLOG
734 if (options.print_lastlog && s->last_login_time != 0) {
735 time_string = ctime(&s->last_login_time);
736 if (strchr(time_string, '\n'))
737 *strchr(time_string, '\n') = 0;
738 if (strcmp(s->hostname, "") == 0)
739 printf("Last login: %s\r\n", time_string);
741 printf("Last login: %s from %s\r\n", time_string,
744 #endif /* NO_SSH_LASTLOG */
750 * Display the message of the day.
758 if (options.print_motd) {
759 #ifdef HAVE_LOGIN_CAP
760 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
763 f = fopen("/etc/motd", "r");
766 while (fgets(buf, sizeof(buf), f))
775 * Check for quiet login, either .hushlogin or command given.
778 check_quietlogin(Session *s, const char *command)
781 struct passwd *pw = s->pw;
784 /* Return 1 if .hushlogin exists or a command given. */
787 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
788 #ifdef HAVE_LOGIN_CAP
789 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
792 if (stat(buf, &st) >= 0)
799 * Sets the value of the given variable in the environment. If the variable
800 * already exists, its value is overriden.
803 child_set_env(char ***envp, u_int *envsizep, const char *name,
810 * Find the slot where the value should be stored. If the variable
811 * already exists, we reuse the slot; otherwise we append a new slot
812 * at the end of the array, expanding if necessary.
815 namelen = strlen(name);
816 for (i = 0; env[i]; i++)
817 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
820 /* Reuse the slot. */
823 /* New variable. Expand if necessary. */
824 if (i >= (*envsizep) - 1) {
825 if (*envsizep >= 1000)
826 fatal("child_set_env: too many env vars,"
827 " skipping: %.100s", name);
829 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
831 /* Need to set the NULL pointer at end of array beyond the new slot. */
835 /* Allocate space and format the variable in the appropriate slot. */
836 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
837 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
841 * Reads environment variables from the given file and adds/overrides them
842 * into the environment. If the file does not exist, this does nothing.
843 * Otherwise, it must consist of empty lines, comments (line starts with '#')
844 * and assignments of the form name=value. No other forms are allowed.
847 read_environment_file(char ***env, u_int *envsize,
848 const char *filename)
855 f = fopen(filename, "r");
859 while (fgets(buf, sizeof(buf), f)) {
861 fatal("Too many lines in environment file %s", filename);
862 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
864 if (!*cp || *cp == '#' || *cp == '\n')
866 if (strchr(cp, '\n'))
867 *strchr(cp, '\n') = '\0';
868 value = strchr(cp, '=');
870 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
875 * Replace the equals sign by nul, and advance value to
880 child_set_env(env, envsize, cp, value);
885 void copy_environment(char **source, char ***env, u_int *envsize)
887 char *var_name, *var_val;
893 for(i = 0; source[i] != NULL; i++) {
894 var_name = xstrdup(source[i]);
895 if ((var_val = strstr(var_name, "=")) == NULL) {
901 debug3("Copy environment: %s=%s", var_name, var_val);
902 child_set_env(env, envsize, var_name, var_val);
909 do_setup_env(Session *s, const char *shell)
914 struct passwd *pw = s->pw;
916 /* Initialize the environment. */
918 env = xmalloc(envsize * sizeof(char *));
923 * The Windows environment contains some setting which are
924 * important for a running system. They must not be dropped.
926 copy_environment(environ, &env, &envsize);
930 /* Allow any GSSAPI methods that we've used to alter
931 * the childs environment as they see fit
933 ssh_gssapi_do_child(&env, &envsize);
936 if (!options.use_login) {
937 /* Set basic environment. */
938 child_set_env(&env, &envsize, "USER", pw->pw_name);
939 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
941 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
943 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
944 #ifdef HAVE_LOGIN_CAP
945 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
946 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
948 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
949 #else /* HAVE_LOGIN_CAP */
952 * There's no standard path on Windows. The path contains
953 * important components pointing to the system directories,
954 * needed for loading shared libraries. So the path better
955 * remains intact here.
957 # ifdef SUPERUSER_PATH
958 child_set_env(&env, &envsize, "PATH",
959 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
961 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
962 # endif /* SUPERUSER_PATH */
963 # endif /* HAVE_CYGWIN */
964 #endif /* HAVE_LOGIN_CAP */
966 snprintf(buf, sizeof buf, "%.200s/%.50s",
967 _PATH_MAILDIR, pw->pw_name);
968 child_set_env(&env, &envsize, "MAIL", buf);
970 /* Normal systems set SHELL by default. */
971 child_set_env(&env, &envsize, "SHELL", shell);
974 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
976 /* Set custom environment options from RSA authentication. */
977 if (!options.use_login) {
978 while (custom_environment) {
979 struct envstring *ce = custom_environment;
982 for (i = 0; str[i] != '=' && str[i]; i++)
986 child_set_env(&env, &envsize, str, str + i + 1);
988 custom_environment = ce->next;
994 /* SSH_CLIENT deprecated */
995 snprintf(buf, sizeof buf, "%.50s %d %d",
996 get_remote_ipaddr(), get_remote_port(), get_local_port());
997 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
999 laddr = get_local_ipaddr(packet_get_connection_in());
1000 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1001 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1003 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1006 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1008 child_set_env(&env, &envsize, "TERM", s->term);
1010 child_set_env(&env, &envsize, "DISPLAY", s->display);
1011 if (original_command)
1012 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1016 if (cray_tmpdir[0] != '\0')
1017 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1018 #endif /* _UNICOS */
1024 if ((cp = getenv("AUTHSTATE")) != NULL)
1025 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1026 if ((cp = getenv("KRB5CCNAME")) != NULL)
1027 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1028 read_environment_file(&env, &envsize, "/etc/environment");
1032 if (s->authctxt->krb5_ticket_file)
1033 child_set_env(&env, &envsize, "KRB5CCNAME",
1034 s->authctxt->krb5_ticket_file);
1038 * Pull in any environment variables that may have
1041 if (options.use_pam) {
1042 char **p = fetch_pam_environment();
1044 copy_environment(p, &env, &envsize);
1045 free_pam_environment(p);
1047 #endif /* USE_PAM */
1049 if (auth_sock_name != NULL)
1050 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1053 /* read $HOME/.ssh/environment. */
1054 if (options.permit_user_env && !options.use_login) {
1055 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1056 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
1057 read_environment_file(&env, &envsize, buf);
1060 /* dump the environment */
1061 fprintf(stderr, "Environment:\n");
1062 for (i = 0; env[i]; i++)
1063 fprintf(stderr, " %.200s\n", env[i]);
1069 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1070 * first in this order).
1073 do_rc_files(Session *s, const char *shell)
1081 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1083 /* ignore _PATH_SSH_USER_RC for subsystems */
1084 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1085 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1086 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1088 fprintf(stderr, "Running %s\n", cmd);
1089 f = popen(cmd, "w");
1092 fprintf(f, "%s %s\n", s->auth_proto,
1096 fprintf(stderr, "Could not run %s\n",
1098 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1100 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1101 _PATH_SSH_SYSTEM_RC);
1102 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1105 fprintf(f, "%s %s\n", s->auth_proto,
1109 fprintf(stderr, "Could not run %s\n",
1110 _PATH_SSH_SYSTEM_RC);
1111 } else if (do_xauth && options.xauth_location != NULL) {
1112 /* Add authority data to .Xauthority if appropriate. */
1115 "Running %.500s remove %.100s\n",
1116 options.xauth_location, s->auth_display);
1118 "%.500s add %.100s %.100s %.100s\n",
1119 options.xauth_location, s->auth_display,
1120 s->auth_proto, s->auth_data);
1122 snprintf(cmd, sizeof cmd, "%s -q -",
1123 options.xauth_location);
1124 f = popen(cmd, "w");
1126 fprintf(f, "remove %s\n",
1128 fprintf(f, "add %s %s %s\n",
1129 s->auth_display, s->auth_proto,
1133 fprintf(stderr, "Could not run %s\n",
1140 do_nologin(struct passwd *pw)
1145 #ifdef HAVE_LOGIN_CAP
1146 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1147 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1148 _PATH_NOLOGIN), "r");
1151 f = fopen(_PATH_NOLOGIN, "r");
1154 /* /etc/nologin exists. Print its contents and exit. */
1155 logit("User %.100s not allowed because %s exists",
1156 pw->pw_name, _PATH_NOLOGIN);
1157 while (fgets(buf, sizeof(buf), f))
1165 /* Set login name, uid, gid, and groups. */
1167 do_setusercontext(struct passwd *pw)
1170 if (getuid() == 0 || geteuid() == 0)
1171 #endif /* HAVE_CYGWIN */
1174 #ifdef HAVE_SETPCRED
1175 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1176 fatal("Failed to set process credentials");
1177 #endif /* HAVE_SETPCRED */
1178 #ifdef HAVE_LOGIN_CAP
1182 if (setusercontext(lc, pw, pw->pw_uid,
1183 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1184 perror("unable to set user context");
1188 # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1189 /* Sets login uid for accounting */
1190 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1191 error("setluid: %s", strerror(errno));
1192 # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1194 if (setlogin(pw->pw_name) < 0)
1195 error("setlogin failed: %s", strerror(errno));
1196 if (setgid(pw->pw_gid) < 0) {
1200 /* Initialize the group list. */
1201 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1202 perror("initgroups");
1208 * PAM credentials may take the form of supplementary groups.
1209 * These will have been wiped by the above initgroups() call.
1210 * Reestablish them here.
1212 if (options.use_pam)
1214 # endif /* USE_PAM */
1215 # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1216 irix_setusercontext(pw);
1217 # endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
1221 /* Permanently switch to the desired uid. */
1222 permanently_set_uid(pw);
1229 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1230 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1234 launch_login(struct passwd *pw, const char *hostname)
1236 /* Launch login(1). */
1238 execl(LOGIN_PROGRAM, "login", "-h", hostname,
1239 #ifdef xxxLOGIN_NEEDS_TERM
1240 (s->term ? s->term : "unknown"),
1241 #endif /* LOGIN_NEEDS_TERM */
1242 #ifdef LOGIN_NO_ENDOPT
1243 "-p", "-f", pw->pw_name, (char *)NULL);
1245 "-p", "-f", "--", pw->pw_name, (char *)NULL);
1248 /* Login couldn't be executed, die. */
1255 * Performs common processing for the child, such as setting up the
1256 * environment, closing extra file descriptors, setting the user and group
1257 * ids, and executing the command or shell.
1260 do_child(Session *s, const char *command)
1262 extern char **environ;
1265 const char *shell, *shell0, *hostname = NULL;
1266 struct passwd *pw = s->pw;
1269 /* remove hostkey from the child's memory */
1270 destroy_sensitive_data();
1272 /* login(1) is only called if we execute the login shell */
1273 if (options.use_login && command != NULL)
1274 options.use_login = 0;
1277 cray_setup(pw->pw_uid, pw->pw_name, command);
1278 #endif /* _UNICOS */
1281 * Login(1) does this as well, and it needs uid 0 for the "-h"
1282 * switch, so we let login(1) to this for us.
1284 if (!options.use_login) {
1286 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1287 if (!check_quietlogin(s, command))
1289 #else /* HAVE_OSF_SIA */
1291 do_setusercontext(pw);
1292 #endif /* HAVE_OSF_SIA */
1296 * Get the shell from the password data. An empty shell field is
1297 * legal, and means /bin/sh.
1299 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1302 * Make sure $SHELL points to the shell from the password file,
1303 * even if shell is overridden from login.conf
1305 env = do_setup_env(s, shell);
1307 #ifdef HAVE_LOGIN_CAP
1308 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1311 /* we have to stash the hostname before we close our socket. */
1312 if (options.use_login)
1313 hostname = get_remote_name_or_ip(utmp_len,
1316 * Close the connection descriptors; note that this is the child, and
1317 * the server will still have the socket open, and it is important
1318 * that we do not shutdown it. Note that the descriptors cannot be
1319 * closed before building the environment, as we call
1320 * get_remote_ipaddr there.
1322 if (packet_get_connection_in() == packet_get_connection_out())
1323 close(packet_get_connection_in());
1325 close(packet_get_connection_in());
1326 close(packet_get_connection_out());
1329 * Close all descriptors related to channels. They will still remain
1330 * open in the parent.
1332 /* XXX better use close-on-exec? -markus */
1333 channel_close_all();
1336 * Close any extra file descriptors. Note that there may still be
1337 * descriptors left by system functions. They will be closed later.
1342 * Close any extra open file descriptors so that we don\'t have them
1343 * hanging around in clients. Note that we want to do this after
1344 * initgroups, because at least on Solaris 2.3 it leaves file
1347 for (i = 3; i < 64; i++)
1351 * Must take new environment into use so that .ssh/rc,
1352 * /etc/ssh/sshrc and xauth are run in the proper environment.
1356 /* Change current directory to the user\'s home directory. */
1357 if (chdir(pw->pw_dir) < 0) {
1358 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1359 pw->pw_dir, strerror(errno));
1360 #ifdef HAVE_LOGIN_CAP
1361 if (login_getcapbool(lc, "requirehome", 0))
1366 if (!options.use_login)
1367 do_rc_files(s, shell);
1369 /* restore SIGPIPE for child */
1370 signal(SIGPIPE, SIG_DFL);
1372 if (options.use_login) {
1373 launch_login(pw, hostname);
1377 /* Get the last component of the shell name. */
1378 if ((shell0 = strrchr(shell, '/')) != NULL)
1384 * If we have no command, execute the shell. In this case, the shell
1385 * name to be passed in argv[0] is preceded by '-' to indicate that
1386 * this is a login shell.
1391 /* Start the shell. Set initial character to '-'. */
1394 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1395 >= sizeof(argv0) - 1) {
1401 /* Execute the shell. */
1404 execve(shell, argv, env);
1406 /* Executing the shell failed. */
1411 * Execute the command using the user's shell. This uses the -c
1412 * option to execute the command.
1414 argv[0] = (char *) shell0;
1416 argv[2] = (char *) command;
1418 execve(shell, argv, env);
1427 static int did_init = 0;
1429 debug("session_new: init");
1430 for (i = 0; i < MAX_SESSIONS; i++) {
1431 sessions[i].used = 0;
1435 for (i = 0; i < MAX_SESSIONS; i++) {
1436 Session *s = &sessions[i];
1438 memset(s, 0, sizeof(*s));
1444 debug("session_new: session %d", i);
1455 for (i = 0; i < MAX_SESSIONS; i++) {
1456 Session *s = &sessions[i];
1457 debug("dump: used %d session %d %p channel %d pid %ld",
1467 session_open(Authctxt *authctxt, int chanid)
1469 Session *s = session_new();
1470 debug("session_open: channel %d", chanid);
1472 error("no more sessions");
1475 s->authctxt = authctxt;
1476 s->pw = authctxt->pw;
1478 fatal("no user for session %d", s->self);
1479 debug("session_open: session %d: link with channel %d", s->self, chanid);
1485 session_by_tty(char *tty)
1488 for (i = 0; i < MAX_SESSIONS; i++) {
1489 Session *s = &sessions[i];
1490 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1491 debug("session_by_tty: session %d tty %s", i, tty);
1495 debug("session_by_tty: unknown tty %.100s", tty);
1501 session_by_channel(int id)
1504 for (i = 0; i < MAX_SESSIONS; i++) {
1505 Session *s = &sessions[i];
1506 if (s->used && s->chanid == id) {
1507 debug("session_by_channel: session %d channel %d", i, id);
1511 debug("session_by_channel: unknown channel %d", id);
1517 session_by_pid(pid_t pid)
1520 debug("session_by_pid: pid %ld", (long)pid);
1521 for (i = 0; i < MAX_SESSIONS; i++) {
1522 Session *s = &sessions[i];
1523 if (s->used && s->pid == pid)
1526 error("session_by_pid: unknown pid %ld", (long)pid);
1532 session_window_change_req(Session *s)
1534 s->col = packet_get_int();
1535 s->row = packet_get_int();
1536 s->xpixel = packet_get_int();
1537 s->ypixel = packet_get_int();
1539 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1544 session_pty_req(Session *s)
1550 debug("Allocating a pty not permitted for this authentication.");
1553 if (s->ttyfd != -1) {
1554 packet_disconnect("Protocol error: you already have a pty.");
1557 /* Get the time and hostname when the user last logged in. */
1558 if (options.print_lastlog) {
1559 s->hostname[0] = '\0';
1560 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1561 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1564 s->term = packet_get_string(&len);
1567 s->col = packet_get_int();
1568 s->row = packet_get_int();
1570 s->row = packet_get_int();
1571 s->col = packet_get_int();
1573 s->xpixel = packet_get_int();
1574 s->ypixel = packet_get_int();
1576 if (strcmp(s->term, "") == 0) {
1581 /* Allocate a pty and open it. */
1582 debug("Allocating pty.");
1583 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
1589 error("session_pty_req: session %d alloc failed", s->self);
1592 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1594 /* for SSH1 the tty modes length is not given */
1596 n_bytes = packet_remaining();
1597 tty_parse_modes(s->ttyfd, &n_bytes);
1600 * Add a cleanup function to clear the utmp entry and record logout
1601 * time in case we call fatal() (e.g., the connection gets closed).
1603 fatal_add_cleanup(session_pty_cleanup, (void *)s);
1605 pty_setowner(s->pw, s->tty);
1607 /* Set window size from the packet. */
1608 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1611 session_proctitle(s);
1616 session_subsystem_req(Session *s)
1621 char *cmd, *subsys = packet_get_string(&len);
1625 logit("subsystem request for %.100s", subsys);
1627 for (i = 0; i < options.num_subsystems; i++) {
1628 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1629 cmd = options.subsystem_command[i];
1630 if (stat(cmd, &st) < 0) {
1631 error("subsystem: cannot stat %s: %s", cmd,
1635 debug("subsystem: exec() %s", cmd);
1636 s->is_subsystem = 1;
1644 logit("subsystem request for %.100s failed, subsystem not found",
1652 session_x11_req(Session *s)
1656 s->single_connection = packet_get_char();
1657 s->auth_proto = packet_get_string(NULL);
1658 s->auth_data = packet_get_string(NULL);
1659 s->screen = packet_get_int();
1662 success = session_setup_x11fwd(s);
1664 xfree(s->auth_proto);
1665 xfree(s->auth_data);
1666 s->auth_proto = NULL;
1667 s->auth_data = NULL;
1673 session_shell_req(Session *s)
1681 session_exec_req(Session *s)
1684 char *command = packet_get_string(&len);
1686 do_exec(s, command);
1692 session_break_req(Session *s)
1696 break_length = packet_get_int(); /* ignored */
1699 if (s->ttyfd == -1 ||
1700 tcsendbreak(s->ttyfd, 0) < 0)
1706 session_auth_agent_req(Session *s)
1708 static int called = 0;
1710 if (no_agent_forwarding_flag) {
1711 debug("session_auth_agent_req: no_agent_forwarding_flag");
1718 return auth_input_request_forwarding(s->pw);
1723 session_input_channel_req(Channel *c, const char *rtype)
1728 if ((s = session_by_channel(c->self)) == NULL) {
1729 logit("session_input_channel_req: no session %d req %.100s",
1733 debug("session_input_channel_req: session %d req %s", s->self, rtype);
1736 * a session is in LARVAL state until a shell, a command
1737 * or a subsystem is executed
1739 if (c->type == SSH_CHANNEL_LARVAL) {
1740 if (strcmp(rtype, "shell") == 0) {
1741 success = session_shell_req(s);
1742 } else if (strcmp(rtype, "exec") == 0) {
1743 success = session_exec_req(s);
1744 } else if (strcmp(rtype, "pty-req") == 0) {
1745 success = session_pty_req(s);
1746 } else if (strcmp(rtype, "x11-req") == 0) {
1747 success = session_x11_req(s);
1748 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1749 success = session_auth_agent_req(s);
1750 } else if (strcmp(rtype, "subsystem") == 0) {
1751 success = session_subsystem_req(s);
1752 } else if (strcmp(rtype, "break") == 0) {
1753 success = session_break_req(s);
1756 if (strcmp(rtype, "window-change") == 0) {
1757 success = session_window_change_req(s);
1763 session_set_fds(Session *s, int fdin, int fdout, int fderr)
1766 fatal("session_set_fds: called for proto != 2.0");
1768 * now that have a child and a pipe to the child,
1769 * we can activate our channel and register the fd's
1771 if (s->chanid == -1)
1772 fatal("no channel for session %d", s->self);
1773 channel_set_fds(s->chanid,
1775 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1777 CHAN_SES_WINDOW_DEFAULT);
1781 * Function to perform pty cleanup. Also called if we get aborted abnormally
1782 * (e.g., due to a dropped connection).
1785 session_pty_cleanup2(void *session)
1787 Session *s = session;
1790 error("session_pty_cleanup: no session");
1796 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1798 /* Record that the user has logged out. */
1800 record_logout(s->pid, s->tty, s->pw->pw_name);
1802 /* Release the pseudo-tty. */
1804 pty_release(s->tty);
1807 * Close the server side of the socket pairs. We must do this after
1808 * the pty cleanup, so that another process doesn't get this pty
1809 * while we're still cleaning up.
1811 if (close(s->ptymaster) < 0)
1812 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
1814 /* unlink pty from session */
1819 session_pty_cleanup(void *session)
1821 PRIVSEP(session_pty_cleanup2(session));
1827 #define SSH_SIG(x) if (sig == SIG ## x) return #x
1842 return "SIG@openssh.com";
1846 session_exit_message(Session *s, int status)
1850 if ((c = channel_lookup(s->chanid)) == NULL)
1851 fatal("session_exit_message: session %d: no channel %d",
1852 s->self, s->chanid);
1853 debug("session_exit_message: session %d channel %d pid %ld",
1854 s->self, s->chanid, (long)s->pid);
1856 if (WIFEXITED(status)) {
1857 channel_request_start(s->chanid, "exit-status", 0);
1858 packet_put_int(WEXITSTATUS(status));
1860 } else if (WIFSIGNALED(status)) {
1861 channel_request_start(s->chanid, "exit-signal", 0);
1862 packet_put_cstring(sig2name(WTERMSIG(status)));
1864 packet_put_char(WCOREDUMP(status));
1865 #else /* WCOREDUMP */
1867 #endif /* WCOREDUMP */
1868 packet_put_cstring("");
1869 packet_put_cstring("");
1872 /* Some weird exit cause. Just exit. */
1873 packet_disconnect("wait returned status %04x.", status);
1876 /* disconnect channel */
1877 debug("session_exit_message: release channel %d", s->chanid);
1878 channel_cancel_cleanup(s->chanid);
1880 * emulate a write failure with 'chan_write_failed', nobody will be
1881 * interested in data we write.
1882 * Note that we must not call 'chan_read_failed', since there could
1883 * be some more data waiting in the pipe.
1885 if (c->ostate != CHAN_OUTPUT_CLOSED)
1886 chan_write_failed(c);
1891 session_close(Session *s)
1893 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
1894 if (s->ttyfd != -1) {
1895 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1896 session_pty_cleanup(s);
1902 if (s->auth_display)
1903 xfree(s->auth_display);
1905 xfree(s->auth_data);
1907 xfree(s->auth_proto);
1909 session_proctitle(s);
1913 session_close_by_pid(pid_t pid, int status)
1915 Session *s = session_by_pid(pid);
1917 debug("session_close_by_pid: no session for pid %ld",
1921 if (s->chanid != -1)
1922 session_exit_message(s, status);
1927 * this is called when a channel dies before
1928 * the session 'child' itself dies
1931 session_close_by_channel(int id, void *arg)
1933 Session *s = session_by_channel(id);
1935 debug("session_close_by_channel: no session for id %d", id);
1938 debug("session_close_by_channel: channel %d child %ld",
1941 debug("session_close_by_channel: channel %d: has child", id);
1943 * delay detach of session, but release pty, since
1944 * the fd's to the child are already closed
1946 if (s->ttyfd != -1) {
1947 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1948 session_pty_cleanup(s);
1952 /* detach by removing callback */
1953 channel_cancel_cleanup(s->chanid);
1959 session_destroy_all(void (*closefunc)(Session *))
1962 for (i = 0; i < MAX_SESSIONS; i++) {
1963 Session *s = &sessions[i];
1965 if (closefunc != NULL)
1974 session_tty_list(void)
1976 static char buf[1024];
1981 for (i = 0; i < MAX_SESSIONS; i++) {
1982 Session *s = &sessions[i];
1983 if (s->used && s->ttyfd != -1) {
1985 if (strncmp(s->tty, "/dev/", 5) != 0) {
1986 cp = strrchr(s->tty, '/');
1987 cp = (cp == NULL) ? s->tty : cp + 1;
1992 strlcat(buf, ",", sizeof buf);
1993 strlcat(buf, cp, sizeof buf);
1997 strlcpy(buf, "notty", sizeof buf);
2002 session_proctitle(Session *s)
2005 error("no user for session %d", s->self);
2007 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2011 session_setup_x11fwd(Session *s)
2014 char display[512], auth_display[512];
2015 char hostname[MAXHOSTNAMELEN];
2017 if (no_x11_forwarding_flag) {
2018 packet_send_debug("X11 forwarding disabled in user configuration file.");
2021 if (!options.x11_forwarding) {
2022 debug("X11 forwarding disabled in server configuration file.");
2025 if (!options.xauth_location ||
2026 (stat(options.xauth_location, &st) == -1)) {
2027 packet_send_debug("No xauth program; cannot forward with spoofing.");
2030 if (options.use_login) {
2031 packet_send_debug("X11 forwarding disabled; "
2032 "not compatible with UseLogin=yes.");
2035 if (s->display != NULL) {
2036 debug("X11 display already set.");
2039 if (x11_create_display_inet(options.x11_display_offset,
2040 options.x11_use_localhost, s->single_connection,
2041 &s->display_number) == -1) {
2042 debug("x11_create_display_inet failed.");
2046 /* Set up a suitable value for the DISPLAY variable. */
2047 if (gethostname(hostname, sizeof(hostname)) < 0)
2048 fatal("gethostname: %.100s", strerror(errno));
2050 * auth_display must be used as the displayname when the
2051 * authorization entry is added with xauth(1). This will be
2052 * different than the DISPLAY string for localhost displays.
2054 if (options.x11_use_localhost) {
2055 snprintf(display, sizeof display, "localhost:%u.%u",
2056 s->display_number, s->screen);
2057 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
2058 s->display_number, s->screen);
2059 s->display = xstrdup(display);
2060 s->auth_display = xstrdup(auth_display);
2062 #ifdef IPADDR_IN_DISPLAY
2064 struct in_addr my_addr;
2066 he = gethostbyname(hostname);
2068 error("Can't get IP address for X11 DISPLAY.");
2069 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2072 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2073 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
2074 s->display_number, s->screen);
2076 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
2077 s->display_number, s->screen);
2079 s->display = xstrdup(display);
2080 s->auth_display = xstrdup(display);
2087 do_authenticated2(Authctxt *authctxt)
2089 server_loop2(authctxt);
2091 if (options.gss_cleanup_creds)
2092 ssh_gssapi_cleanup_creds(NULL);