1 /* $OpenBSD: session.c,v 1.245 2009/01/22 09:46:01 djm Exp $ */
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
12 * SSH2 support by Markus Friedl.
13 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
43 #include <sys/socket.h>
47 #include <arpa/inet.h>
62 #include "openbsd-compat/sys-queue.h"
81 #include "auth-options.h"
82 #include "pathnames.h"
86 #include "serverloop.h"
91 #include "monitor_wrap.h"
94 #if defined(KRB5) && defined(USE_AFS)
98 #define IS_INTERNAL_SFTP(c) \
99 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
100 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
101 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
102 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
106 Session *session_new(void);
107 void session_set_fds(Session *, int, int, int, int);
108 void session_pty_cleanup(Session *);
109 void session_proctitle(Session *);
110 int session_setup_x11fwd(Session *);
111 int do_exec_pty(Session *, const char *);
112 int do_exec_no_pty(Session *, const char *);
113 int do_exec(Session *, const char *);
114 void do_login(Session *, const char *);
115 #ifdef LOGIN_NEEDS_UTMPX
116 static void do_pre_login(Session *s);
118 void do_child(Session *, const char *);
120 int check_quietlogin(Session *, const char *);
122 static void do_authenticated1(Authctxt *);
123 static void do_authenticated2(Authctxt *);
125 static int session_pty_req(Session *);
128 static void execute_session_hook(char* prog, Authctxt *authctxt,
129 int startup, int save);
133 extern ServerOptions options;
134 extern char *__progname;
135 extern int log_stderr;
136 extern int debug_flag;
137 extern u_int utmp_len;
138 extern int startup_pipe;
139 extern void destroy_sensitive_data(void);
140 extern Buffer loginmsg;
142 /* original command from peer. */
143 const char *original_command = NULL;
146 static int sessions_first_unused = -1;
147 static int sessions_nalloc = 0;
148 static Session *sessions = NULL;
150 #define SUBSYSTEM_NONE 0
151 #define SUBSYSTEM_EXT 1
152 #define SUBSYSTEM_INT_SFTP 2
154 #ifdef HAVE_LOGIN_CAP
158 static int is_child = 0;
160 /* Name and directory of socket for authentication agent forwarding. */
161 static char *auth_sock_name = NULL;
162 static char *auth_sock_dir = NULL;
164 /* removes the agent forwarding socket */
167 auth_sock_cleanup_proc(struct passwd *pw)
169 if (auth_sock_name != NULL) {
170 temporarily_use_uid(pw);
171 unlink(auth_sock_name);
172 rmdir(auth_sock_dir);
173 auth_sock_name = NULL;
179 auth_input_request_forwarding(struct passwd * pw)
183 struct sockaddr_un sunaddr;
185 if (auth_sock_name != NULL) {
186 error("authentication forwarding requested twice.");
190 /* Temporarily drop privileged uid for mkdir/bind. */
191 temporarily_use_uid(pw);
193 /* Allocate a buffer for the socket name, and format the name. */
194 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
196 /* Create private directory for socket */
197 if (mkdtemp(auth_sock_dir) == NULL) {
198 packet_send_debug("Agent forwarding disabled: "
199 "mkdtemp() failed: %.100s", strerror(errno));
201 xfree(auth_sock_dir);
202 auth_sock_dir = NULL;
206 xasprintf(&auth_sock_name, "%s/agent.%ld",
207 auth_sock_dir, (long) getpid());
209 /* Create the socket. */
210 sock = socket(AF_UNIX, SOCK_STREAM, 0);
212 error("socket: %.100s", strerror(errno));
217 /* Bind it to the name. */
218 memset(&sunaddr, 0, sizeof(sunaddr));
219 sunaddr.sun_family = AF_UNIX;
220 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
222 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
223 error("bind: %.100s", strerror(errno));
228 /* Restore the privileged uid. */
231 /* Start listening on the socket. */
232 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
233 error("listen: %.100s", strerror(errno));
237 /* Allocate a channel for the authentication agent socket. */
238 /* this shouldn't matter if its hpn or not - cjr */
239 nc = channel_new("auth socket",
240 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
241 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
242 0, "auth socket", 1);
243 nc->path = xstrdup(auth_sock_name);
247 if (auth_sock_name != NULL)
248 xfree(auth_sock_name);
249 if (auth_sock_dir != NULL) {
250 rmdir(auth_sock_dir);
251 xfree(auth_sock_dir);
255 auth_sock_name = NULL;
256 auth_sock_dir = NULL;
261 display_loginmsg(void)
263 if (buffer_len(&loginmsg) > 0) {
264 buffer_append(&loginmsg, "\0", 1);
265 printf("%s", (char *)buffer_ptr(&loginmsg));
266 buffer_clear(&loginmsg);
271 do_authenticated(Authctxt *authctxt)
273 setproctitle("%s", authctxt->pw->pw_name);
275 /* setup the channel layer */
276 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
277 channel_permit_all_opens();
280 do_authenticated2(authctxt);
282 do_authenticated1(authctxt);
285 if (options.session_hooks_allow &&
286 options.session_hooks_shutdown_cmd)
288 execute_session_hook(options.session_hooks_shutdown_cmd,
290 /* startup = */ 0, /* save = */ 0);
292 if (authctxt->session_env_file)
294 free(authctxt->session_env_file);
299 do_cleanup(authctxt);
303 * Prepares for an interactive session. This is called after the user has
304 * been successfully authenticated. During this message exchange, pseudo
305 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
306 * are requested, etc.
309 do_authenticated1(Authctxt *authctxt)
313 int success, type, screen_flag;
314 int enable_compression_after_reply = 0;
315 u_int proto_len, data_len, dlen, compression_level = 0;
319 error("no more sessions");
322 s->authctxt = authctxt;
323 s->pw = authctxt->pw;
326 * We stay in this loop until the client requests to execute a shell
332 /* Get a packet from the client. */
333 type = packet_read();
335 /* Process the packet. */
337 case SSH_CMSG_REQUEST_COMPRESSION:
338 compression_level = packet_get_int();
340 if (compression_level < 1 || compression_level > 9) {
341 packet_send_debug("Received invalid compression level %d.",
345 if (options.compression == COMP_NONE) {
346 debug2("compression disabled");
349 /* Enable compression after we have responded with SUCCESS. */
350 enable_compression_after_reply = 1;
354 case SSH_CMSG_REQUEST_PTY:
355 success = session_pty_req(s);
358 case SSH_CMSG_X11_REQUEST_FORWARDING:
359 s->auth_proto = packet_get_string(&proto_len);
360 s->auth_data = packet_get_string(&data_len);
362 screen_flag = packet_get_protocol_flags() &
363 SSH_PROTOFLAG_SCREEN_NUMBER;
364 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
366 if (packet_remaining() == 4) {
368 debug2("Buggy client: "
369 "X11 screen flag missing");
370 s->screen = packet_get_int();
375 success = session_setup_x11fwd(s);
377 xfree(s->auth_proto);
379 s->auth_proto = NULL;
384 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
385 if (!options.allow_agent_forwarding ||
386 no_agent_forwarding_flag || compat13) {
387 debug("Authentication agent forwarding not permitted for this authentication.");
390 debug("Received authentication agent forwarding request.");
391 success = auth_input_request_forwarding(s->pw);
394 case SSH_CMSG_PORT_FORWARD_REQUEST:
395 if (no_port_forwarding_flag) {
396 debug("Port forwarding not permitted for this authentication.");
399 if (!options.allow_tcp_forwarding) {
400 debug("Port forwarding not permitted.");
403 debug("Received TCP/IP port forwarding request.");
404 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
405 options.gateway_ports) < 0) {
406 debug("Port forwarding failed.");
412 case SSH_CMSG_MAX_PACKET_SIZE:
413 if (packet_set_maxsize(packet_get_int()) > 0)
417 case SSH_CMSG_EXEC_SHELL:
418 case SSH_CMSG_EXEC_CMD:
419 if (type == SSH_CMSG_EXEC_CMD) {
420 command = packet_get_string(&dlen);
421 debug("Exec command '%.500s'", command);
422 if (do_exec(s, command) != 0)
424 "command execution failed");
427 if (do_exec(s, NULL) != 0)
429 "shell execution failed");
437 * Any unknown messages in this phase are ignored,
438 * and a failure message is returned.
440 logit("Unknown packet type received after authentication: %d", type);
442 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
446 /* Enable compression now that we have replied if appropriate. */
447 if (enable_compression_after_reply) {
448 enable_compression_after_reply = 0;
449 packet_start_compression(compression_level);
456 * This is called to fork and execute a command when we have no tty. This
457 * will call do_child from the child, and server_loop from the parent after
458 * setting up file descriptors and such.
461 do_exec_no_pty(Session *s, const char *command)
466 int pin[2], pout[2], perr[2];
468 /* Allocate pipes for communicating with the program. */
470 error("%s: pipe in: %.100s", __func__, strerror(errno));
473 if (pipe(pout) < 0) {
474 error("%s: pipe out: %.100s", __func__, strerror(errno));
479 if (pipe(perr) < 0) {
480 error("%s: pipe err: %.100s", __func__, strerror(errno));
488 int inout[2], err[2];
490 /* Uses socket pairs to communicate with the program. */
491 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
492 error("%s: socketpair #1: %.100s", __func__, strerror(errno));
495 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
496 error("%s: socketpair #2: %.100s", __func__, strerror(errno));
504 fatal("do_exec_no_pty: no session");
506 session_proctitle(s);
508 /* Fork the child. */
509 switch ((pid = fork())) {
511 error("%s: fork: %.100s", __func__, strerror(errno));
529 /* Child. Reinitialize the log since the pid has changed. */
530 log_init(__progname, options.log_level,
531 options.log_facility, log_stderr);
534 * Create a new session and process group since the 4.4BSD
535 * setlogin() affects the entire process group.
538 error("setsid failed: %.100s", strerror(errno));
542 * Redirect stdin. We close the parent side of the socket
543 * pair, and make the child side the standard input.
546 if (dup2(pin[0], 0) < 0)
547 perror("dup2 stdin");
550 /* Redirect stdout. */
552 if (dup2(pout[1], 1) < 0)
553 perror("dup2 stdout");
556 /* Redirect stderr. */
558 if (dup2(perr[1], 2) < 0)
559 perror("dup2 stderr");
563 * Redirect stdin, stdout, and stderr. Stdin and stdout will
564 * use the same socket, as some programs (particularly rdist)
565 * seem to depend on it.
569 if (dup2(inout[0], 0) < 0) /* stdin */
570 perror("dup2 stdin");
571 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
572 perror("dup2 stdout");
574 if (dup2(err[0], 2) < 0) /* stderr */
575 perror("dup2 stderr");
581 cray_init_job(s->pw); /* set up cray jid and tmpdir */
584 /* Do processing for the child (exec command etc). */
585 do_child(s, command);
592 signal(WJSIGNAL, cray_job_termination_handler);
596 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
600 /* Set interactive/non-interactive mode. */
601 packet_set_interactive(s->display != NULL);
604 * Clear loginmsg, since it's the child's responsibility to display
605 * it to the user, otherwise multiple sessions may accumulate
606 * multiple copies of the login messages.
608 buffer_clear(&loginmsg);
611 /* We are the parent. Close the child sides of the pipes. */
617 if (s->is_subsystem) {
621 session_set_fds(s, pin[1], pout[0], perr[0], 0);
623 /* Enter the interactive session. */
624 server_loop(pid, pin[1], pout[0], perr[0]);
625 /* server_loop has closed pin[1], pout[0], and perr[0]. */
628 /* We are the parent. Close the child sides of the socket pairs. */
633 * Enter the interactive session. Note: server_loop must be able to
634 * handle the case that fdin and fdout are the same.
637 session_set_fds(s, inout[1], inout[1],
638 s->is_subsystem ? -1 : err[1], 0);
642 server_loop(pid, inout[1], inout[1], err[1]);
643 /* server_loop has closed inout[1] and err[1]. */
650 * This is called to fork and execute a command when we have a tty. This
651 * will call do_child from the child, and server_loop from the parent after
652 * setting up file descriptors, controlling tty, updating wtmp, utmp,
653 * lastlog, and other such operations.
656 do_exec_pty(Session *s, const char *command)
658 int fdout, ptyfd, ttyfd, ptymaster;
662 fatal("do_exec_pty: no session");
667 * Create another descriptor of the pty master side for use as the
668 * standard input. We could use the original descriptor, but this
669 * simplifies code in server_loop. The descriptor is bidirectional.
670 * Do this before forking (and cleanup in the child) so as to
671 * detect and gracefully fail out-of-fd conditions.
673 if ((fdout = dup(ptyfd)) < 0) {
674 error("%s: dup #1: %s", __func__, strerror(errno));
679 /* we keep a reference to the pty master */
680 if ((ptymaster = dup(ptyfd)) < 0) {
681 error("%s: dup #2: %s", __func__, strerror(errno));
688 /* Fork the child. */
689 switch ((pid = fork())) {
691 error("%s: fork: %.100s", __func__, strerror(errno));
703 /* Child. Reinitialize the log because the pid has changed. */
704 log_init(__progname, options.log_level,
705 options.log_facility, log_stderr);
706 /* Close the master side of the pseudo tty. */
709 /* Make the pseudo tty our controlling tty. */
710 pty_make_controlling_tty(&ttyfd, s->tty);
712 /* Redirect stdin/stdout/stderr from the pseudo tty. */
713 if (dup2(ttyfd, 0) < 0)
714 error("dup2 stdin: %s", strerror(errno));
715 if (dup2(ttyfd, 1) < 0)
716 error("dup2 stdout: %s", strerror(errno));
717 if (dup2(ttyfd, 2) < 0)
718 error("dup2 stderr: %s", strerror(errno));
720 /* Close the extra descriptor for the pseudo tty. */
723 /* record login, etc. similar to login(1) */
725 if (!(options.use_login && command == NULL)) {
727 cray_init_job(s->pw); /* set up cray jid and tmpdir */
729 do_login(s, command);
731 # ifdef LOGIN_NEEDS_UTMPX
737 * Do common processing for the child, such as execing
740 do_child(s, command);
747 signal(WJSIGNAL, cray_job_termination_handler);
751 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
756 /* Parent. Close the slave side of the pseudo tty. */
759 /* Enter interactive session. */
760 s->ptymaster = ptymaster;
761 packet_set_interactive(1);
763 session_set_fds(s, ptyfd, fdout, -1, 1);
765 server_loop(pid, ptyfd, fdout, -1);
766 /* server_loop _has_ closed ptyfd and fdout. */
771 #ifdef LOGIN_NEEDS_UTMPX
773 do_pre_login(Session *s)
776 struct sockaddr_storage from;
777 pid_t pid = getpid();
780 * Get IP address of client. If the connection is not a socket, let
781 * the address be 0.0.0.0.
783 memset(&from, 0, sizeof(from));
784 fromlen = sizeof(from);
785 if (packet_connection_is_on_socket()) {
786 if (getpeername(packet_get_connection_in(),
787 (struct sockaddr *)&from, &fromlen) < 0) {
788 debug("getpeername: %.100s", strerror(errno));
793 record_utmp_only(pid, s->tty, s->pw->pw_name,
794 get_remote_name_or_ip(utmp_len, options.use_dns),
795 (struct sockaddr *)&from, fromlen);
800 * This is called to fork and execute a command. If another command is
801 * to be forced, execute that instead.
804 do_exec(Session *s, const char *command)
808 if (options.adm_forced_command) {
809 original_command = command;
810 command = options.adm_forced_command;
811 if (IS_INTERNAL_SFTP(command))
812 s->is_subsystem = SUBSYSTEM_INT_SFTP;
813 else if (s->is_subsystem)
814 s->is_subsystem = SUBSYSTEM_EXT;
815 debug("Forced command (config) '%.900s'", command);
816 } else if (forced_command) {
817 original_command = command;
818 command = forced_command;
819 if (IS_INTERNAL_SFTP(command))
820 s->is_subsystem = SUBSYSTEM_INT_SFTP;
821 else if (s->is_subsystem)
822 s->is_subsystem = SUBSYSTEM_EXT;
823 debug("Forced command (key option) '%.900s'", command);
826 #if defined(SESSION_HOOKS)
827 if (options.session_hooks_allow &&
828 (options.session_hooks_startup_cmd ||
829 options.session_hooks_shutdown_cmd))
837 "/tmp/ssh_env_%d%d%d",
841 } while (stat(env_file, &st)==0);
842 s->authctxt->session_env_file = strdup(env_file);
846 #ifdef SSH_AUDIT_EVENTS
848 PRIVSEP(audit_run_command(command));
849 else if (s->ttyfd == -1) {
850 char *shell = s->pw->pw_shell;
852 if (shell[0] == '\0') /* empty shell means /bin/sh */
854 PRIVSEP(audit_run_command(shell));
858 ret = do_exec_pty(s, command);
860 ret = do_exec_no_pty(s, command);
862 original_command = NULL;
865 * Clear loginmsg: it's the child's responsibility to display
866 * it to the user, otherwise multiple sessions may accumulate
867 * multiple copies of the login messages.
869 buffer_clear(&loginmsg);
874 /* administrative, login(1)-like work */
876 do_login(Session *s, const char *command)
879 struct sockaddr_storage from;
880 struct passwd * pw = s->pw;
881 pid_t pid = getpid();
884 * Get IP address of client. If the connection is not a socket, let
885 * the address be 0.0.0.0.
887 memset(&from, 0, sizeof(from));
888 fromlen = sizeof(from);
889 if (packet_connection_is_on_socket()) {
890 if (getpeername(packet_get_connection_in(),
891 (struct sockaddr *) & from, &fromlen) < 0) {
892 debug("getpeername: %.100s", strerror(errno));
897 /* Record that there was a login on that tty from the remote host. */
899 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
900 get_remote_name_or_ip(utmp_len,
902 (struct sockaddr *)&from, fromlen);
906 * If password change is needed, do it now.
907 * This needs to occur before the ~/.hushlogin check.
909 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
912 s->authctxt->force_pwchange = 0;
913 /* XXX - signal [net] parent to enable forwardings */
917 if (check_quietlogin(s, command))
926 * Display the message of the day.
934 if (options.print_motd) {
935 #ifdef HAVE_LOGIN_CAP
936 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
939 f = fopen("/etc/motd", "r");
942 while (fgets(buf, sizeof(buf), f))
951 * Check for quiet login, either .hushlogin or command given.
954 check_quietlogin(Session *s, const char *command)
957 struct passwd *pw = s->pw;
960 /* Return 1 if .hushlogin exists or a command given. */
963 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
964 #ifdef HAVE_LOGIN_CAP
965 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
968 if (stat(buf, &st) >= 0)
975 * Sets the value of the given variable in the environment. If the variable
976 * already exists, its value is overridden.
979 child_set_env(char ***envp, u_int *envsizep, const char *name,
987 * If we're passed an uninitialized list, allocate a single null
988 * entry before continuing.
990 if (*envp == NULL && *envsizep == 0) {
991 *envp = xmalloc(sizeof(char *));
997 * Find the slot where the value should be stored. If the variable
998 * already exists, we reuse the slot; otherwise we append a new slot
999 * at the end of the array, expanding if necessary.
1002 namelen = strlen(name);
1003 for (i = 0; env[i]; i++)
1004 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
1007 /* Reuse the slot. */
1010 /* New variable. Expand if necessary. */
1011 envsize = *envsizep;
1012 if (i >= envsize - 1) {
1013 if (envsize >= 1000)
1014 fatal("child_set_env: too many env vars");
1016 env = (*envp) = xrealloc(env, envsize, sizeof(char *));
1017 *envsizep = envsize;
1019 /* Need to set the NULL pointer at end of array beyond the new slot. */
1023 /* Allocate space and format the variable in the appropriate slot. */
1024 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
1025 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
1029 * Reads environment variables from the given file and adds/overrides them
1030 * into the environment. If the file does not exist, this does nothing.
1031 * Otherwise, it must consist of empty lines, comments (line starts with '#')
1032 * and assignments of the form name=value. No other forms are allowed.
1035 read_environment_file(char ***env, u_int *envsize,
1036 const char *filename)
1043 f = fopen(filename, "r");
1047 while (fgets(buf, sizeof(buf), f)) {
1048 if (++lineno > 1000)
1049 fatal("Too many lines in environment file %s", filename);
1050 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
1052 if (!*cp || *cp == '#' || *cp == '\n')
1055 cp[strcspn(cp, "\n")] = '\0';
1057 value = strchr(cp, '=');
1058 if (value == NULL) {
1059 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
1064 * Replace the equals sign by nul, and advance value to
1069 child_set_env(env, envsize, cp, value);
1074 #ifdef SESSION_HOOKS
1075 #define SSH_SESSION_ENV_FILE "SSH_SESSION_ENV_FILE"
1077 typedef enum { no_op, execute, clear_env, restore_env,
1078 read_env, save_or_rm_env } session_action_t;
1080 static session_action_t action_order[2][5] = {
1081 { clear_env, read_env, execute, save_or_rm_env, restore_env }, /*shutdown*/
1082 { execute, read_env, save_or_rm_env, no_op, no_op } /*startup */
1086 void execute_session_hook(char* prog, Authctxt *authctxt,
1087 int startup, int save)
1089 extern char **environ;
1092 char **saved_env, **tmpenv;
1093 char *env_file = authctxt->session_env_file;
1098 switch (action_order[startup][i])
1111 authctxt->pw->pw_shell,
1114 debug("executing session hook: [%s]", buf);
1115 setenv(SSH_SESSION_ENV_FILE, env_file, /* overwrite = */ 1);
1117 /* flusing is recommended in the popen(3) man page, to avoid
1118 intermingling of output */
1121 if ((fp=popen(buf, "w")) == NULL)
1123 perror("Unable to run session hook");
1126 status = pclose(fp);
1127 debug2("session hook executed, status=%d", status);
1128 unsetenv(SSH_SESSION_ENV_FILE);
1133 saved_env = environ;
1134 tmpenv = (char**) malloc(sizeof(char*));
1140 environ = saved_env;
1145 if (status==0 && stat(env_file, &st)==0)
1149 debug("reading environment from %s", env_file);
1150 while (environ[envsize++]) ;
1151 read_environment_file(&environ, &envsize, env_file);
1155 case save_or_rm_env:
1156 if (status==0 && save)
1161 debug2("saving environment to %s", env_file);
1162 if ((fp = fopen(env_file, "w")) == NULL) /* hmm: file perms? */
1164 perror("Unable to save session hook info");
1166 while (environ[envcount])
1168 fprintf(fp, "%s\n", environ[envcount++]);
1173 else if (stat(env_file, &st)==0)
1175 debug2("removing environment file %s", env_file);
1185 #ifdef HAVE_ETC_DEFAULT_LOGIN
1187 * Return named variable from specified environment, or NULL if not present.
1190 child_get_env(char **env, const char *name)
1196 for (i=0; env[i] != NULL; i++)
1197 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
1198 return(env[i] + len + 1);
1203 * Read /etc/default/login.
1204 * We pick up the PATH (or SUPATH for root) and UMASK.
1207 read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
1209 char **tmpenv = NULL, *var;
1210 u_int i, tmpenvsize = 0;
1214 * We don't want to copy the whole file to the child's environment,
1215 * so we use a temporary environment and copy the variables we're
1218 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
1224 var = child_get_env(tmpenv, "SUPATH");
1226 var = child_get_env(tmpenv, "PATH");
1228 child_set_env(env, envsize, "PATH", var);
1230 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
1231 if (sscanf(var, "%5lo", &mask) == 1)
1232 umask((mode_t)mask);
1234 for (i = 0; tmpenv[i] != NULL; i++)
1238 #endif /* HAVE_ETC_DEFAULT_LOGIN */
1241 copy_environment(char **source, char ***env, u_int *envsize)
1243 char *var_name, *var_val;
1249 for(i = 0; source[i] != NULL; i++) {
1250 var_name = xstrdup(source[i]);
1251 if ((var_val = strstr(var_name, "=")) == NULL) {
1257 debug3("Copy environment: %s=%s", var_name, var_val);
1258 child_set_env(env, envsize, var_name, var_val);
1265 do_setup_env(Session *s, const char *shell)
1270 struct passwd *pw = s->pw;
1271 #ifndef HAVE_LOGIN_CAP
1275 /* Initialize the environment. */
1277 env = xcalloc(envsize, sizeof(char *));
1282 * The Windows environment contains some setting which are
1283 * important for a running system. They must not be dropped.
1288 p = fetch_windows_environment();
1289 copy_environment(p, &env, &envsize);
1290 free_windows_environment(p);
1295 /* Allow any GSSAPI methods that we've used to alter
1296 * the childs environment as they see fit
1298 ssh_gssapi_do_child(&env, &envsize);
1301 if (!options.use_login) {
1302 /* Set basic environment. */
1303 for (i = 0; i < s->num_env; i++)
1304 child_set_env(&env, &envsize, s->env[i].name,
1307 child_set_env(&env, &envsize, "USER", pw->pw_name);
1308 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1310 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1312 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1313 #ifdef HAVE_LOGIN_CAP
1314 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1315 child_set_env(&env, &envsize, "PATH",
1316 _PATH_STDPATH_WITH_SCP);
1318 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1319 #else /* HAVE_LOGIN_CAP */
1320 # ifndef HAVE_CYGWIN
1322 * There's no standard path on Windows. The path contains
1323 * important components pointing to the system directories,
1324 * needed for loading shared libraries. So the path better
1325 * remains intact here.
1327 # ifdef HAVE_ETC_DEFAULT_LOGIN
1328 read_etc_default_login(&env, &envsize, pw->pw_uid);
1329 path = child_get_env(env, "PATH");
1330 # endif /* HAVE_ETC_DEFAULT_LOGIN */
1331 if (path == NULL || *path == '\0') {
1332 child_set_env(&env, &envsize, "PATH",
1333 s->pw->pw_uid == 0 ?
1334 SUPERUSER_PATH : _PATH_STDPATH_WITH_SCP);
1336 # endif /* HAVE_CYGWIN */
1337 #endif /* HAVE_LOGIN_CAP */
1339 snprintf(buf, sizeof buf, "%.200s/%.50s",
1340 _PATH_MAILDIR, pw->pw_name);
1341 child_set_env(&env, &envsize, "MAIL", buf);
1343 /* Normal systems set SHELL by default. */
1344 child_set_env(&env, &envsize, "SHELL", shell);
1347 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1349 #ifdef GSI /* GSI shared libs typically installed in non-system locations. */
1353 if ((cp = getenv("LD_LIBRARY_PATH")) != NULL)
1354 child_set_env(&env, &envsize, "LD_LIBRARY_PATH", cp);
1355 if ((cp = getenv("LIBPATH")) != NULL)
1356 child_set_env(&env, &envsize, "LIBPATH", cp);
1357 if ((cp = getenv("SHLIB_PATH")) != NULL)
1358 child_set_env(&env, &envsize, "SHLIB_PATH", cp);
1359 if ((cp = getenv("LD_LIBRARYN32_PATH")) != NULL)
1360 child_set_env(&env, &envsize, "LD_LIBRARYN32_PATH",cp);
1361 if ((cp = getenv("LD_LIBRARY64_PATH")) != NULL)
1362 child_set_env(&env, &envsize, "LD_LIBRARY64_PATH",cp);
1363 if ((cp = getenv("GLOBUS_LOCATION")) != NULL)
1364 child_set_env(&env, &envsize, "GLOBUS_LOCATION",cp);
1368 /* Set custom environment options from RSA authentication. */
1369 if (!options.use_login) {
1370 while (custom_environment) {
1371 struct envstring *ce = custom_environment;
1374 for (i = 0; str[i] != '=' && str[i]; i++)
1376 if (str[i] == '=') {
1378 child_set_env(&env, &envsize, str, str + i + 1);
1380 custom_environment = ce->next;
1386 /* SSH_CLIENT deprecated */
1387 snprintf(buf, sizeof buf, "%.50s %d %d",
1388 get_remote_ipaddr(), get_remote_port(), get_local_port());
1389 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1391 laddr = get_local_ipaddr(packet_get_connection_in());
1392 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1393 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1395 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1398 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1400 child_set_env(&env, &envsize, "TERM", s->term);
1402 child_set_env(&env, &envsize, "DISPLAY", s->display);
1403 if (original_command)
1404 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1408 if (cray_tmpdir[0] != '\0')
1409 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1410 #endif /* _UNICOS */
1413 * Since we clear KRB5CCNAME at startup, if it's set now then it
1414 * must have been set by a native authentication method (eg AIX or
1415 * SIA), so copy it to the child.
1420 if ((cp = getenv("KRB5CCNAME")) != NULL)
1421 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1428 if ((cp = getenv("AUTHSTATE")) != NULL)
1429 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1430 read_environment_file(&env, &envsize, "/etc/environment");
1434 if (s->authctxt->krb5_ccname)
1435 child_set_env(&env, &envsize, "KRB5CCNAME",
1436 s->authctxt->krb5_ccname);
1440 * Pull in any environment variables that may have
1443 if (options.use_pam) {
1446 p = fetch_pam_child_environment();
1447 copy_environment(p, &env, &envsize);
1448 free_pam_environment(p);
1450 p = fetch_pam_environment();
1451 copy_environment(p, &env, &envsize);
1452 free_pam_environment(p);
1454 #endif /* USE_PAM */
1456 if (auth_sock_name != NULL)
1457 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1460 /* read $HOME/.ssh/environment. */
1461 if (options.permit_user_env && !options.use_login) {
1462 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1463 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
1464 read_environment_file(&env, &envsize, buf);
1467 /* dump the environment */
1468 fprintf(stderr, "Environment:\n");
1469 for (i = 0; env[i]; i++)
1470 fprintf(stderr, " %.200s\n", env[i]);
1476 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1477 * first in this order).
1480 do_rc_files(Session *s, const char *shell)
1488 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1490 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
1491 if (!s->is_subsystem && options.adm_forced_command == NULL &&
1492 !no_user_rc && stat(_PATH_SSH_USER_RC, &st) >= 0) {
1493 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1494 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1496 fprintf(stderr, "Running %s\n", cmd);
1497 f = popen(cmd, "w");
1500 fprintf(f, "%s %s\n", s->auth_proto,
1504 fprintf(stderr, "Could not run %s\n",
1506 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1507 snprintf(cmd, sizeof cmd, "%s %s", _PATH_BSHELL,
1508 _PATH_SSH_SYSTEM_RC);
1510 fprintf(stderr, "Running %s\n", cmd);
1511 f = popen(cmd, "w");
1514 fprintf(f, "%s %s\n", s->auth_proto,
1518 fprintf(stderr, "Could not run %s\n",
1519 _PATH_SSH_SYSTEM_RC);
1520 } else if (do_xauth && options.xauth_location != NULL) {
1521 /* Add authority data to .Xauthority if appropriate. */
1524 "Running %.500s remove %.100s\n",
1525 options.xauth_location, s->auth_display);
1527 "%.500s add %.100s %.100s %.100s\n",
1528 options.xauth_location, s->auth_display,
1529 s->auth_proto, s->auth_data);
1531 snprintf(cmd, sizeof cmd, "%s -q -",
1532 options.xauth_location);
1533 f = popen(cmd, "w");
1535 fprintf(f, "remove %s\n",
1537 fprintf(f, "add %s %s %s\n",
1538 s->auth_display, s->auth_proto,
1542 fprintf(stderr, "Could not run %s\n",
1549 do_nologin(struct passwd *pw)
1554 #ifdef HAVE_LOGIN_CAP
1555 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1556 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1557 _PATH_NOLOGIN), "r");
1560 f = fopen(_PATH_NOLOGIN, "r");
1563 /* /etc/nologin exists. Print its contents and exit. */
1564 logit("User %.100s not allowed because %s exists",
1565 pw->pw_name, _PATH_NOLOGIN);
1566 while (fgets(buf, sizeof(buf), f))
1575 * Chroot into a directory after checking it for safety: all path components
1576 * must be root-owned directories with strict permissions.
1579 safely_chroot(const char *path, uid_t uid)
1582 char component[MAXPATHLEN];
1586 fatal("chroot path does not begin at root");
1587 if (strlen(path) >= sizeof(component))
1588 fatal("chroot path too long");
1591 * Descend the path, checking that each component is a
1592 * root-owned directory with strict permissions.
1594 for (cp = path; cp != NULL;) {
1595 if ((cp = strchr(cp, '/')) == NULL)
1596 strlcpy(component, path, sizeof(component));
1599 memcpy(component, path, cp - path);
1600 component[cp - path] = '\0';
1603 debug3("%s: checking '%s'", __func__, component);
1605 if (stat(component, &st) != 0)
1606 fatal("%s: stat(\"%s\"): %s", __func__,
1607 component, strerror(errno));
1608 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1609 fatal("bad ownership or modes for chroot "
1610 "directory %s\"%s\"",
1611 cp == NULL ? "" : "component ", component);
1612 if (!S_ISDIR(st.st_mode))
1613 fatal("chroot path %s\"%s\" is not a directory",
1614 cp == NULL ? "" : "component ", component);
1618 if (chdir(path) == -1)
1619 fatal("Unable to chdir to chroot path \"%s\": "
1620 "%s", path, strerror(errno));
1621 if (chroot(path) == -1)
1622 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1623 if (chdir("/") == -1)
1624 fatal("%s: chdir(/) after chroot: %s",
1625 __func__, strerror(errno));
1626 verbose("Changed root directory to \"%s\"", path);
1629 /* Set login name, uid, gid, and groups. */
1631 do_setusercontext(struct passwd *pw)
1633 char *chroot_path, *tmp;
1636 /* Cache selinux status for later use */
1637 (void)ssh_selinux_enabled();
1641 if (getuid() == 0 || geteuid() == 0)
1642 #endif /* HAVE_CYGWIN */
1645 #ifdef HAVE_SETPCRED
1646 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1647 fatal("Failed to set process credentials");
1648 #endif /* HAVE_SETPCRED */
1649 #ifdef HAVE_LOGIN_CAP
1654 if (options.use_pam) {
1655 do_pam_setcred(use_privsep);
1657 # endif /* USE_PAM */
1658 if (setusercontext(lc, pw, pw->pw_uid,
1659 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
1660 perror("unable to set user context");
1664 # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1665 /* Sets login uid for accounting */
1666 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1667 error("setluid: %s", strerror(errno));
1668 # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1670 if (setlogin(pw->pw_name) < 0)
1671 error("setlogin failed: %s", strerror(errno));
1672 if (setgid(pw->pw_gid) < 0) {
1676 /* Initialize the group list. */
1677 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1678 perror("initgroups");
1684 * PAM credentials may take the form of supplementary groups.
1685 * These will have been wiped by the above initgroups() call.
1686 * Reestablish them here.
1688 if (options.use_pam) {
1689 do_pam_setcred(use_privsep);
1691 # endif /* USE_PAM */
1692 # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1693 irix_setusercontext(pw);
1694 # endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
1699 if (set_id(pw->pw_name) != 0) {
1702 # endif /* USE_LIBIAF */
1705 if (options.chroot_directory != NULL &&
1706 strcasecmp(options.chroot_directory, "none") != 0) {
1707 tmp = tilde_expand_filename(options.chroot_directory,
1709 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1710 "u", pw->pw_name, (char *)NULL);
1711 safely_chroot(chroot_path, pw->pw_uid);
1716 #ifdef HAVE_LOGIN_CAP
1717 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1718 perror("unable to set user context (setuser)");
1722 /* Permanently switch to the desired uid. */
1723 permanently_set_uid(pw);
1730 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1731 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1734 ssh_selinux_setup_exec_context(pw->pw_name);
1739 do_pwchange(Session *s)
1742 fprintf(stderr, "WARNING: Your password has expired.\n");
1743 if (s->ttyfd != -1) {
1745 "You must change your password now and login again!\n");
1746 #ifdef PASSWD_NEEDS_USERNAME
1747 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1750 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
1755 "Password change required but no TTY available.\n");
1761 launch_login(struct passwd *pw, const char *hostname)
1763 /* Launch login(1). */
1765 execl(LOGIN_PROGRAM, "login", "-h", hostname,
1766 #ifdef xxxLOGIN_NEEDS_TERM
1767 (s->term ? s->term : "unknown"),
1768 #endif /* LOGIN_NEEDS_TERM */
1769 #ifdef LOGIN_NO_ENDOPT
1770 "-p", "-f", pw->pw_name, (char *)NULL);
1772 "-p", "-f", "--", pw->pw_name, (char *)NULL);
1775 /* Login couldn't be executed, die. */
1782 child_close_fds(void)
1786 if (packet_get_connection_in() == packet_get_connection_out())
1787 close(packet_get_connection_in());
1789 close(packet_get_connection_in());
1790 close(packet_get_connection_out());
1793 * Close all descriptors related to channels. They will still remain
1794 * open in the parent.
1796 /* XXX better use close-on-exec? -markus */
1797 channel_close_all();
1800 * Close any extra file descriptors. Note that there may still be
1801 * descriptors left by system functions. They will be closed later.
1806 * Close any extra open file descriptors so that we don't have them
1807 * hanging around in clients. Note that we want to do this after
1808 * initgroups, because at least on Solaris 2.3 it leaves file
1811 for (i = 3; i < 64; i++)
1816 * Performs common processing for the child, such as setting up the
1817 * environment, closing extra file descriptors, setting the user and group
1818 * ids, and executing the command or shell.
1822 do_child(Session *s, const char *command)
1824 extern char **environ;
1826 char *argv[ARGV_MAX];
1827 const char *shell, *shell0, *hostname = NULL;
1828 struct passwd *pw = s->pw;
1832 /* Default place to look for aklog. */
1834 #define KPROGDIR AKLOG_PATH
1836 #define KPROGDIR "/usr/bin/aklog"
1837 #endif /* AKLOG_PATH */
1841 #endif /* AFS_KRB5 */
1843 /* remove hostkey from the child's memory */
1844 destroy_sensitive_data();
1846 /* Force a password change */
1847 if (s->authctxt->force_pwchange) {
1848 do_setusercontext(pw);
1854 /* login(1) is only called if we execute the login shell */
1855 if (options.use_login && command != NULL)
1856 options.use_login = 0;
1859 cray_setup(pw->pw_uid, pw->pw_name, command);
1860 #endif /* _UNICOS */
1863 * Login(1) does this as well, and it needs uid 0 for the "-h"
1864 * switch, so we let login(1) to this for us.
1866 if (!options.use_login) {
1868 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
1869 if (!check_quietlogin(s, command))
1871 #else /* HAVE_OSF_SIA */
1872 /* When PAM is enabled we rely on it to do the nologin check */
1873 if (!options.use_pam)
1875 do_setusercontext(pw);
1877 * PAM session modules in do_setusercontext may have
1878 * generated messages, so if this in an interactive
1879 * login then display them too.
1881 if (!check_quietlogin(s, command))
1883 #endif /* HAVE_OSF_SIA */
1887 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1888 debug3("PAM session not opened, exiting");
1895 * Get the shell from the password data. An empty shell field is
1896 * legal, and means /bin/sh.
1898 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1901 * Make sure $SHELL points to the shell from the password file,
1902 * even if shell is overridden from login.conf
1904 env = do_setup_env(s, shell);
1906 #ifdef HAVE_LOGIN_CAP
1907 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1910 /* we have to stash the hostname before we close our socket. */
1911 if (options.use_login)
1912 hostname = get_remote_name_or_ip(utmp_len,
1915 * Close the connection descriptors; note that this is the child, and
1916 * the server will still have the socket open, and it is important
1917 * that we do not shutdown it. Note that the descriptors cannot be
1918 * closed before building the environment, as we call
1919 * get_remote_ipaddr there.
1924 * Must take new environment into use so that .ssh/rc,
1925 * /etc/ssh/sshrc and xauth are run in the proper environment.
1929 #if defined(KRB5) && defined(USE_AFS)
1931 * At this point, we check to see if AFS is active and if we have
1932 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1933 * if we can (and need to) extend the ticket into an AFS token. If
1934 * we don't do this, we run into potential problems if the user's
1935 * home directory is in AFS and it's not world-readable.
1938 if (options.kerberos_get_afs_token && k_hasafs() &&
1939 (s->authctxt->krb5_ctx != NULL)) {
1942 debug("Getting AFS token");
1946 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1947 krb5_afslog(s->authctxt->krb5_ctx,
1948 s->authctxt->krb5_fwd_ccache, cell, NULL);
1950 krb5_afslog_home(s->authctxt->krb5_ctx,
1951 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1957 /* User has authenticated, and if a ticket was going to be
1958 * passed we would have it. KRB5CCNAME should already be set.
1959 * Now try to get an AFS token using aklog.
1961 if (k_hasafs()) { /* Do we have AFS? */
1963 aklog_path = xstrdup(KPROGDIR);
1966 * Make sure it exists before we try to run it
1968 if (stat(aklog_path, &st) == 0) {
1969 debug("Running %s to get afs token.",aklog_path);
1972 debug("%s does not exist.",aklog_path);
1977 #endif /* AFS_KRB5 */
1979 #ifdef SESSION_HOOKS
1980 if (options.session_hooks_allow &&
1981 options.session_hooks_startup_cmd)
1983 execute_session_hook(options.session_hooks_startup_cmd,
1986 options.session_hooks_shutdown_cmd != NULL);
1990 /* Change current directory to the user's home directory. */
1991 if (chdir(pw->pw_dir) < 0) {
1992 /* Suppress missing homedir warning for chroot case */
1993 #ifdef HAVE_LOGIN_CAP
1994 r = login_getcapbool(lc, "requirehome", 0);
1996 if (r || options.chroot_directory == NULL)
1997 fprintf(stderr, "Could not chdir to home "
1998 "directory %s: %s\n", pw->pw_dir,
2004 closefrom(STDERR_FILENO + 1);
2006 if (!options.use_login)
2007 do_rc_files(s, shell);
2009 /* restore SIGPIPE for child */
2010 signal(SIGPIPE, SIG_DFL);
2012 if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
2013 extern int optind, optreset;
2017 setproctitle("%s@internal-sftp-server", s->pw->pw_name);
2018 args = xstrdup(command ? command : "sftp-server");
2019 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
2020 if (i < ARGV_MAX - 1)
2023 optind = optreset = 1;
2024 __progname = argv[0];
2025 exit(sftp_server_main(i, argv, s->pw));
2028 if (options.use_login) {
2029 launch_login(pw, hostname);
2033 /* Get the last component of the shell name. */
2034 if ((shell0 = strrchr(shell, '/')) != NULL)
2040 * If we have no command, execute the shell. In this case, the shell
2041 * name to be passed in argv[0] is preceded by '-' to indicate that
2042 * this is a login shell.
2047 /* Start the shell. Set initial character to '-'. */
2050 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
2051 >= sizeof(argv0) - 1) {
2057 /* Execute the shell. */
2060 execve(shell, argv, environ);
2062 /* Executing the shell failed. */
2067 * Execute the command using the user's shell. This uses the -c
2068 * option to execute the command.
2070 argv[0] = (char *) shell0;
2072 argv[2] = (char *) command;
2074 execve(shell, argv, environ);
2080 session_unused(int id)
2082 debug3("%s: session id %d unused", __func__, id);
2083 if (id >= options.max_sessions ||
2084 id >= sessions_nalloc) {
2085 fatal("%s: insane session id %d (max %d nalloc %d)",
2086 __func__, id, options.max_sessions, sessions_nalloc);
2088 bzero(&sessions[id], sizeof(*sessions));
2089 sessions[id].self = id;
2090 sessions[id].used = 0;
2091 sessions[id].chanid = -1;
2092 sessions[id].ptyfd = -1;
2093 sessions[id].ttyfd = -1;
2094 sessions[id].ptymaster = -1;
2095 sessions[id].x11_chanids = NULL;
2096 sessions[id].next_unused = sessions_first_unused;
2097 sessions_first_unused = id;
2105 if (sessions_first_unused == -1) {
2106 if (sessions_nalloc >= options.max_sessions)
2108 debug2("%s: allocate (allocated %d max %d)",
2109 __func__, sessions_nalloc, options.max_sessions);
2110 tmp = xrealloc(sessions, sessions_nalloc + 1,
2113 error("%s: cannot allocate %d sessions",
2114 __func__, sessions_nalloc + 1);
2118 session_unused(sessions_nalloc++);
2121 if (sessions_first_unused >= sessions_nalloc ||
2122 sessions_first_unused < 0) {
2123 fatal("%s: insane first_unused %d max %d nalloc %d",
2124 __func__, sessions_first_unused, options.max_sessions,
2128 s = &sessions[sessions_first_unused];
2130 fatal("%s: session %d already used",
2131 __func__, sessions_first_unused);
2133 sessions_first_unused = s->next_unused;
2135 s->next_unused = -1;
2136 debug("session_new: session %d", s->self);
2145 for (i = 0; i < sessions_nalloc; i++) {
2146 Session *s = &sessions[i];
2148 debug("dump: used %d next_unused %d session %d %p "
2149 "channel %d pid %ld",
2160 session_open(Authctxt *authctxt, int chanid)
2162 Session *s = session_new();
2163 debug("session_open: channel %d", chanid);
2165 error("no more sessions");
2168 s->authctxt = authctxt;
2169 s->pw = authctxt->pw;
2170 if (s->pw == NULL || !authctxt->valid)
2171 fatal("no user for session %d", s->self);
2172 debug("session_open: session %d: link with channel %d", s->self, chanid);
2178 session_by_tty(char *tty)
2181 for (i = 0; i < sessions_nalloc; i++) {
2182 Session *s = &sessions[i];
2183 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
2184 debug("session_by_tty: session %d tty %s", i, tty);
2188 debug("session_by_tty: unknown tty %.100s", tty);
2194 session_by_channel(int id)
2197 for (i = 0; i < sessions_nalloc; i++) {
2198 Session *s = &sessions[i];
2199 if (s->used && s->chanid == id) {
2200 debug("session_by_channel: session %d channel %d",
2205 debug("session_by_channel: unknown channel %d", id);
2211 session_by_x11_channel(int id)
2215 for (i = 0; i < sessions_nalloc; i++) {
2216 Session *s = &sessions[i];
2218 if (s->x11_chanids == NULL || !s->used)
2220 for (j = 0; s->x11_chanids[j] != -1; j++) {
2221 if (s->x11_chanids[j] == id) {
2222 debug("session_by_x11_channel: session %d "
2223 "channel %d", s->self, id);
2228 debug("session_by_x11_channel: unknown channel %d", id);
2234 session_by_pid(pid_t pid)
2237 debug("session_by_pid: pid %ld", (long)pid);
2238 for (i = 0; i < sessions_nalloc; i++) {
2239 Session *s = &sessions[i];
2240 if (s->used && s->pid == pid)
2243 error("session_by_pid: unknown pid %ld", (long)pid);
2249 session_window_change_req(Session *s)
2251 s->col = packet_get_int();
2252 s->row = packet_get_int();
2253 s->xpixel = packet_get_int();
2254 s->ypixel = packet_get_int();
2256 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2261 session_pty_req(Session *s)
2267 debug("Allocating a pty not permitted for this authentication.");
2270 if (s->ttyfd != -1) {
2271 packet_disconnect("Protocol error: you already have a pty.");
2275 s->term = packet_get_string(&len);
2278 s->col = packet_get_int();
2279 s->row = packet_get_int();
2281 s->row = packet_get_int();
2282 s->col = packet_get_int();
2284 s->xpixel = packet_get_int();
2285 s->ypixel = packet_get_int();
2287 if (strcmp(s->term, "") == 0) {
2292 /* Allocate a pty and open it. */
2293 debug("Allocating pty.");
2294 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
2301 error("session_pty_req: session %d alloc failed", s->self);
2304 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
2306 /* for SSH1 the tty modes length is not given */
2308 n_bytes = packet_remaining();
2309 tty_parse_modes(s->ttyfd, &n_bytes);
2312 pty_setowner(s->pw, s->tty);
2314 /* Set window size from the packet. */
2315 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
2318 session_proctitle(s);
2323 session_subsystem_req(Session *s)
2328 char *prog, *cmd, *subsys = packet_get_string(&len);
2332 logit("subsystem request for %.100s", subsys);
2334 for (i = 0; i < options.num_subsystems; i++) {
2335 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
2336 prog = options.subsystem_command[i];
2337 cmd = options.subsystem_args[i];
2338 if (!strcmp(INTERNAL_SFTP_NAME, prog)) {
2339 s->is_subsystem = SUBSYSTEM_INT_SFTP;
2340 } else if (stat(prog, &st) < 0) {
2341 error("subsystem: cannot stat %s: %s", prog,
2345 s->is_subsystem = SUBSYSTEM_EXT;
2347 debug("subsystem: exec() %s", cmd);
2348 success = do_exec(s, cmd) == 0;
2354 logit("subsystem request for %.100s failed, subsystem not found",
2362 session_x11_req(Session *s)
2366 if (s->auth_proto != NULL || s->auth_data != NULL) {
2367 error("session_x11_req: session %d: "
2368 "x11 forwarding already active", s->self);
2371 s->single_connection = packet_get_char();
2372 s->auth_proto = packet_get_string(NULL);
2373 s->auth_data = packet_get_string(NULL);
2374 s->screen = packet_get_int();
2377 success = session_setup_x11fwd(s);
2379 xfree(s->auth_proto);
2380 xfree(s->auth_data);
2381 s->auth_proto = NULL;
2382 s->auth_data = NULL;
2388 session_shell_req(Session *s)
2391 return do_exec(s, NULL) == 0;
2395 session_exec_req(Session *s)
2399 char *command = packet_get_string(&len);
2401 success = do_exec(s, command) == 0;
2407 session_break_req(Session *s)
2410 packet_get_int(); /* ignored */
2413 if (s->ttyfd == -1 || tcsendbreak(s->ttyfd, 0) < 0)
2419 session_env_req(Session *s)
2422 u_int name_len, val_len, i;
2424 name = packet_get_string(&name_len);
2425 val = packet_get_string(&val_len);
2428 /* Don't set too many environment variables */
2429 if (s->num_env > 128) {
2430 debug2("Ignoring env request %s: too many env vars", name);
2434 for (i = 0; i < options.num_accept_env; i++) {
2435 if (match_pattern(name, options.accept_env[i])) {
2436 debug2("Setting env %d: %s=%s", s->num_env, name, val);
2437 s->env = xrealloc(s->env, s->num_env + 1,
2439 s->env[s->num_env].name = name;
2440 s->env[s->num_env].val = val;
2445 debug2("Ignoring env request %s: disallowed name", name);
2454 session_auth_agent_req(Session *s)
2456 static int called = 0;
2458 if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
2459 debug("session_auth_agent_req: no_agent_forwarding_flag");
2466 return auth_input_request_forwarding(s->pw);
2471 session_input_channel_req(Channel *c, const char *rtype)
2476 if ((s = session_by_channel(c->self)) == NULL) {
2477 logit("session_input_channel_req: no session %d req %.100s",
2481 debug("session_input_channel_req: session %d req %s", s->self, rtype);
2484 * a session is in LARVAL state until a shell, a command
2485 * or a subsystem is executed
2487 if (c->type == SSH_CHANNEL_LARVAL) {
2488 if (strcmp(rtype, "shell") == 0) {
2489 success = session_shell_req(s);
2490 } else if (strcmp(rtype, "exec") == 0) {
2491 success = session_exec_req(s);
2492 } else if (strcmp(rtype, "pty-req") == 0) {
2493 success = session_pty_req(s);
2494 } else if (strcmp(rtype, "x11-req") == 0) {
2495 success = session_x11_req(s);
2496 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2497 success = session_auth_agent_req(s);
2498 } else if (strcmp(rtype, "subsystem") == 0) {
2499 success = session_subsystem_req(s);
2500 } else if (strcmp(rtype, "env") == 0) {
2501 success = session_env_req(s);
2504 if (strcmp(rtype, "window-change") == 0) {
2505 success = session_window_change_req(s);
2506 } else if (strcmp(rtype, "break") == 0) {
2507 success = session_break_req(s);
2514 session_set_fds(Session *s, int fdin, int fdout, int fderr, int is_tty)
2517 fatal("session_set_fds: called for proto != 2.0");
2519 * now that have a child and a pipe to the child,
2520 * we can activate our channel and register the fd's
2522 if (s->chanid == -1)
2523 fatal("no channel for session %d", s->self);
2524 if (options.hpn_disabled)
2525 channel_set_fds(s->chanid,
2527 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
2528 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
2530 channel_set_fds(s->chanid,
2532 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
2533 1, is_tty, options.hpn_buffer_size);
2537 * Function to perform pty cleanup. Also called if we get aborted abnormally
2538 * (e.g., due to a dropped connection).
2541 session_pty_cleanup2(Session *s)
2544 error("session_pty_cleanup: no session");
2550 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
2552 /* Record that the user has logged out. */
2554 record_logout(s->pid, s->tty, s->pw->pw_name);
2556 /* Release the pseudo-tty. */
2558 pty_release(s->tty);
2561 * Close the server side of the socket pairs. We must do this after
2562 * the pty cleanup, so that another process doesn't get this pty
2563 * while we're still cleaning up.
2565 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
2566 error("close(s->ptymaster/%d): %s",
2567 s->ptymaster, strerror(errno));
2569 /* unlink pty from session */
2574 session_pty_cleanup(Session *s)
2576 PRIVSEP(session_pty_cleanup2(s));
2582 #define SSH_SIG(x) if (sig == SIG ## x) return #x
2597 return "SIG@openssh.com";
2601 session_close_x11(int id)
2605 if ((c = channel_by_id(id)) == NULL) {
2606 debug("session_close_x11: x11 channel %d missing", id);
2608 /* Detach X11 listener */
2609 debug("session_close_x11: detach x11 channel %d", id);
2610 channel_cancel_cleanup(id);
2611 if (c->ostate != CHAN_OUTPUT_CLOSED)
2617 session_close_single_x11(int id, void *arg)
2622 debug3("session_close_single_x11: channel %d", id);
2623 channel_cancel_cleanup(id);
2624 if ((s = session_by_x11_channel(id)) == NULL)
2625 fatal("session_close_single_x11: no x11 channel %d", id);
2626 for (i = 0; s->x11_chanids[i] != -1; i++) {
2627 debug("session_close_single_x11: session %d: "
2628 "closing channel %d", s->self, s->x11_chanids[i]);
2630 * The channel "id" is already closing, but make sure we
2631 * close all of its siblings.
2633 if (s->x11_chanids[i] != id)
2634 session_close_x11(s->x11_chanids[i]);
2636 xfree(s->x11_chanids);
2637 s->x11_chanids = NULL;
2642 if (s->auth_proto) {
2643 xfree(s->auth_proto);
2644 s->auth_proto = NULL;
2647 xfree(s->auth_data);
2648 s->auth_data = NULL;
2650 if (s->auth_display) {
2651 xfree(s->auth_display);
2652 s->auth_display = NULL;
2657 session_exit_message(Session *s, int status)
2661 if ((c = channel_lookup(s->chanid)) == NULL)
2662 fatal("session_exit_message: session %d: no channel %d",
2663 s->self, s->chanid);
2664 debug("session_exit_message: session %d channel %d pid %ld",
2665 s->self, s->chanid, (long)s->pid);
2667 if (WIFEXITED(status)) {
2668 channel_request_start(s->chanid, "exit-status", 0);
2669 packet_put_int(WEXITSTATUS(status));
2671 } else if (WIFSIGNALED(status)) {
2672 channel_request_start(s->chanid, "exit-signal", 0);
2673 packet_put_cstring(sig2name(WTERMSIG(status)));
2675 packet_put_char(WCOREDUMP(status)? 1 : 0);
2676 #else /* WCOREDUMP */
2678 #endif /* WCOREDUMP */
2679 packet_put_cstring("");
2680 packet_put_cstring("");
2683 /* Some weird exit cause. Just exit. */
2684 packet_disconnect("wait returned status %04x.", status);
2687 /* disconnect channel */
2688 debug("session_exit_message: release channel %d", s->chanid);
2691 * Adjust cleanup callback attachment to send close messages when
2692 * the channel gets EOF. The session will be then be closed
2693 * by session_close_by_channel when the childs close their fds.
2695 channel_register_cleanup(c->self, session_close_by_channel, 1);
2698 * emulate a write failure with 'chan_write_failed', nobody will be
2699 * interested in data we write.
2700 * Note that we must not call 'chan_read_failed', since there could
2701 * be some more data waiting in the pipe.
2703 if (c->ostate != CHAN_OUTPUT_CLOSED)
2704 chan_write_failed(c);
2708 session_close(Session *s)
2712 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
2714 session_pty_cleanup(s);
2720 xfree(s->x11_chanids);
2721 if (s->auth_display)
2722 xfree(s->auth_display);
2724 xfree(s->auth_data);
2726 xfree(s->auth_proto);
2727 if (s->env != NULL) {
2728 for (i = 0; i < s->num_env; i++) {
2729 xfree(s->env[i].name);
2730 xfree(s->env[i].val);
2734 session_proctitle(s);
2735 session_unused(s->self);
2739 session_close_by_pid(pid_t pid, int status)
2741 Session *s = session_by_pid(pid);
2743 debug("session_close_by_pid: no session for pid %ld",
2747 if (s->chanid != -1)
2748 session_exit_message(s, status);
2750 session_pty_cleanup(s);
2755 * this is called when a channel dies before
2756 * the session 'child' itself dies
2759 session_close_by_channel(int id, void *arg)
2761 Session *s = session_by_channel(id);
2765 debug("session_close_by_channel: no session for id %d", id);
2768 debug("session_close_by_channel: channel %d child %ld",
2771 debug("session_close_by_channel: channel %d: has child", id);
2773 * delay detach of session, but release pty, since
2774 * the fd's to the child are already closed
2777 session_pty_cleanup(s);
2780 /* detach by removing callback */
2781 channel_cancel_cleanup(s->chanid);
2783 /* Close any X11 listeners associated with this session */
2784 if (s->x11_chanids != NULL) {
2785 for (i = 0; s->x11_chanids[i] != -1; i++) {
2786 session_close_x11(s->x11_chanids[i]);
2787 s->x11_chanids[i] = -1;
2796 session_destroy_all(void (*closefunc)(Session *))
2799 for (i = 0; i < sessions_nalloc; i++) {
2800 Session *s = &sessions[i];
2802 if (closefunc != NULL)
2811 session_tty_list(void)
2813 static char buf[1024];
2818 for (i = 0; i < sessions_nalloc; i++) {
2819 Session *s = &sessions[i];
2820 if (s->used && s->ttyfd != -1) {
2822 if (strncmp(s->tty, "/dev/", 5) != 0) {
2823 cp = strrchr(s->tty, '/');
2824 cp = (cp == NULL) ? s->tty : cp + 1;
2829 strlcat(buf, ",", sizeof buf);
2830 strlcat(buf, cp, sizeof buf);
2834 strlcpy(buf, "notty", sizeof buf);
2839 session_proctitle(Session *s)
2842 error("no user for session %d", s->self);
2844 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2848 session_setup_x11fwd(Session *s)
2851 char display[512], auth_display[512];
2852 char hostname[MAXHOSTNAMELEN];
2855 if (no_x11_forwarding_flag) {
2856 packet_send_debug("X11 forwarding disabled in user configuration file.");
2859 if (!options.x11_forwarding) {
2860 debug("X11 forwarding disabled in server configuration file.");
2863 if (!options.xauth_location ||
2864 (stat(options.xauth_location, &st) == -1)) {
2865 packet_send_debug("No xauth program; cannot forward with spoofing.");
2868 if (options.use_login) {
2869 packet_send_debug("X11 forwarding disabled; "
2870 "not compatible with UseLogin=yes.");
2873 if (s->display != NULL) {
2874 debug("X11 display already set.");
2877 if (x11_create_display_inet(options.x11_display_offset,
2878 options.x11_use_localhost, s->single_connection,
2879 &s->display_number, &s->x11_chanids) == -1) {
2880 debug("x11_create_display_inet failed.");
2883 for (i = 0; s->x11_chanids[i] != -1; i++) {
2884 channel_register_cleanup(s->x11_chanids[i],
2885 session_close_single_x11, 0);
2888 /* Set up a suitable value for the DISPLAY variable. */
2889 if (gethostname(hostname, sizeof(hostname)) < 0)
2890 fatal("gethostname: %.100s", strerror(errno));
2892 * auth_display must be used as the displayname when the
2893 * authorization entry is added with xauth(1). This will be
2894 * different than the DISPLAY string for localhost displays.
2896 if (options.x11_use_localhost) {
2897 snprintf(display, sizeof display, "localhost:%u.%u",
2898 s->display_number, s->screen);
2899 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
2900 s->display_number, s->screen);
2901 s->display = xstrdup(display);
2902 s->auth_display = xstrdup(auth_display);
2904 #ifdef IPADDR_IN_DISPLAY
2906 struct in_addr my_addr;
2908 he = gethostbyname(hostname);
2910 error("Can't get IP address for X11 DISPLAY.");
2911 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2914 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2915 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
2916 s->display_number, s->screen);
2918 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
2919 s->display_number, s->screen);
2921 s->display = xstrdup(display);
2922 s->auth_display = xstrdup(display);
2929 do_authenticated2(Authctxt *authctxt)
2931 server_loop2(authctxt);
2935 do_cleanup(Authctxt *authctxt)
2937 static int called = 0;
2939 debug("do_cleanup");
2941 /* no cleanup if we're in the child for login shell */
2945 /* avoid double cleanup */
2950 if (authctxt == NULL)
2954 if (options.use_pam) {
2956 sshpam_thread_cleanup();
2960 if (!authctxt->authenticated)
2964 if (options.kerberos_ticket_cleanup &&
2966 krb5_cleanup_proc(authctxt);
2970 if (compat20 && options.gss_cleanup_creds)
2971 ssh_gssapi_cleanup_creds();
2974 /* remove agent socket */
2975 auth_sock_cleanup_proc(authctxt->pw);
2978 * Cleanup ptys/utmp only if privsep is disabled,
2979 * or if running in monitor.
2981 if (!use_privsep || mm_is_monitor())
2982 session_destroy_all(session_pty_cleanup2);