]> andersk Git - openssh.git/blobdiff - session.c
missed sshconnect.c part of:
[openssh.git] / session.c
index f5bbf19d4b02841ff450b697249f1b07327cdeaa..92fb31e09c8b2e920cc26184f3e333499aea7239 100644 (file)
--- a/session.c
+++ b/session.c
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: session.c,v 1.37 2000/09/07 20:27:53 deraadt Exp $");
+RCSID("$OpenBSD: session.c,v 1.55 2001/02/08 19:30:52 itojun Exp $");
 
-#include "xmalloc.h"
 #include "ssh.h"
+#include "ssh1.h"
+#include "ssh2.h"
+#include "xmalloc.h"
 #include "pty.h"
 #include "packet.h"
 #include "buffer.h"
-#include "cipher.h"
 #include "mpaux.h"
-#include "servconf.h"
 #include "uidswap.h"
 #include "compat.h"
 #include "channels.h"
 #include "nchan.h"
-
 #include "bufaux.h"
-#include "ssh2.h"
 #include "auth.h"
 #include "auth-options.h"
+#include "pathnames.h"
+#include "log.h"
+#include "servconf.h"
+#include "login.h"
+#include "serverloop.h"
+#include "canohost.h"
+#include "session.h"
 
 #ifdef WITH_IRIX_PROJECT
 #include <proj.h>
 #endif /* WITH_IRIX_PROJECT */
+#ifdef WITH_IRIX_JOBS
+#include <sys/resource.h>
+#endif
+#ifdef WITH_IRIX_AUDIT
+#include <sat.h>
+#endif /* WITH_IRIX_AUDIT */
 
 #if defined(HAVE_USERSEC_H)
 #include <usersec.h>
@@ -120,7 +131,7 @@ void        session_pty_cleanup(Session *s);
 void   session_proctitle(Session *s);
 void   do_exec_pty(Session *s, const char *command, struct passwd * pw);
 void   do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
-void   do_login(Session *s);
+void   do_login(Session *s, const char *command);
 
 void
 do_child(const char *command, struct passwd * pw, const char *term,
@@ -129,15 +140,10 @@ do_child(const char *command, struct passwd * pw, const char *term,
 
 /* import */
 extern ServerOptions options;
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "sshd";
-#endif /* HAVE___PROGNAME */
-
 extern int log_stderr;
 extern int debug_flag;
-extern unsigned int utmp_len;
+extern u_int utmp_len;
 
 extern int startup_pipe;
 
@@ -145,12 +151,11 @@ extern int startup_pipe;
 static char *xauthfile;
 
 /* original command from peer. */
-char *original_command = NULL; 
+char *original_command = NULL;
 
 /* data */
 #define MAX_SESSIONS 10
 Session        sessions[MAX_SESSIONS];
-static int num_used_sessions;
 
 #ifdef WITH_AIXAUTHENTICATE
 /* AIX's lastlogin message, set in auth1.c */
@@ -219,7 +224,7 @@ do_authenticated(struct passwd * pw)
        char *command;
        int n_bytes;
        int plen;
-       unsigned int proto_len, data_len, dlen;
+       u_int proto_len, data_len, dlen;
 
        /*
         * Cancel the alarm we set to limit the time taken for
@@ -238,13 +243,13 @@ do_authenticated(struct passwd * pw)
         * by the client telling us, so we can equally well trust the client
         * not to request anything bogus.)
         */
-       if (!no_port_forwarding_flag)
+       if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
                channel_permit_all_opens();
 
        s = session_new();
        s->pw = pw;
 
-#ifdef HAVE_LOGIN_CAP
+#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                error("unable to get login class");
                return;
@@ -390,6 +395,10 @@ do_authenticated(struct passwd * pw)
                                debug("Port forwarding not permitted for this authentication.");
                                break;
                        }
+                       if (!options.allow_tcp_forwarding) {
+                               debug("Port forwarding not permitted.");
+                               break;
+                       }
                        debug("Received TCP/IP port forwarding request.");
                        channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
                        success = 1;
@@ -402,10 +411,6 @@ do_authenticated(struct passwd * pw)
 
                case SSH_CMSG_EXEC_SHELL:
                case SSH_CMSG_EXEC_CMD:
-                       /* Set interactive/non-interactive mode. */
-                       packet_set_interactive(have_pty || s->display != NULL,
-                           options.keepalives);
-
                        if (type == SSH_CMSG_EXEC_CMD) {
                                command = packet_get_string(&dlen);
                                debug("Exec command '%.500s'", command);
@@ -488,6 +493,8 @@ do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
                /* Child.  Reinitialize the log since the pid has changed. */
                log_init(__progname, options.log_level, options.log_facility, log_stderr);
 
+               signal(SIGPIPE, SIG_DFL);
+
                /*
                 * Create a new session and process group since the 4.4BSD
                 * setlogin() affects the entire process group.
@@ -543,6 +550,8 @@ do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
        if (pid < 0)
                packet_disconnect("fork failed: %.100s", strerror(errno));
        s->pid = pid;
+       /* Set interactive/non-interactive mode. */
+       packet_set_interactive(s->display != NULL);
 #ifdef USE_PIPES
        /* We are the parent.  Close the child sides of the pipes. */
        close(pin[0]);
@@ -601,6 +610,8 @@ do_exec_pty(Session *s, const char *command, struct passwd * pw)
                /* Child.  Reinitialize the log because the pid has changed. */
                log_init(__progname, options.log_level, options.log_facility, log_stderr);
 
+               signal(SIGPIPE, SIG_DFL);
+
                /* Close the master side of the pseudo tty. */
                close(ptyfd);
 
@@ -623,8 +634,8 @@ do_exec_pty(Session *s, const char *command, struct passwd * pw)
                close(ttyfd);
 
                /* record login, etc. similar to login(1) */
-               if (command == NULL && !options.use_login)
-                       do_login(s);
+               if (!(options.use_login && command == NULL))
+                       do_login(s, command);
 
                /* Do common processing for the child, such as execing the command. */
                do_child(command, pw, s->term, s->display, s->auth_proto,
@@ -658,6 +669,7 @@ do_exec_pty(Session *s, const char *command, struct passwd * pw)
        s->ptymaster = ptymaster;
 
        /* Enter interactive session. */
+       packet_set_interactive(1);
        if (compat20) {
                session_set_fds(s, ptyfd, fdout, -1);
        } else {
@@ -672,7 +684,7 @@ get_remote_name_or_ip(void)
 {
        static const char *remote = "";
        if (utmp_len > 0)
-               remote = get_canonical_hostname();
+               remote = get_canonical_hostname(options.reverse_mapping_check);
        if (utmp_len == 0 || strlen(remote) > utmp_len)
                remote = get_remote_ipaddr();
        return remote;
@@ -680,7 +692,7 @@ get_remote_name_or_ip(void)
 
 /* administrative, login(1)-like work */
 void
-do_login(Session *s)
+do_login(Session *s, const char *command)
 {
        FILE *f;
        char *time_string;
@@ -707,10 +719,6 @@ do_login(Session *s)
                }
        }
 
-       /* Get the time and hostname when the user last logged in. */
-       last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
-           hostname, sizeof(hostname));
-
        /* Get the time and hostname when the user last logged in. */
        hostname[0] = '\0';
        last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
@@ -720,7 +728,20 @@ do_login(Session *s)
        record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
            get_remote_name_or_ip(), (struct sockaddr *)&from);
 
-       /* Done if .hushlogin exists. */
+#ifdef USE_PAM
+       /*
+        * If password change is needed, do it now.
+        * This needs to occur before the ~/.hushlogin check.
+        */
+       if (pam_password_change_required()) {
+               print_pam_messages();
+               do_pam_chauthtok();
+       }
+#endif
+
+       /* Done if .hushlogin exists or a command given. */
+       if (command != NULL)
+               return;
        snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
 #ifdef HAVE_LOGIN_CAP
        if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
@@ -730,9 +751,8 @@ do_login(Session *s)
                return;
 
 #ifdef USE_PAM
-       print_pam_messages();
-       /* If password change is needed, do it now. */
-       do_pam_chauthtok();
+       if (!pam_password_change_required())
+               print_pam_messages();
 #endif /* USE_PAM */
 #ifdef WITH_AIXAUTHENTICATE
        if (aixloginmsg && *aixloginmsg)
@@ -743,7 +763,7 @@ do_login(Session *s)
                time_string = ctime(&last_login_time);
                if (strchr(time_string, '\n'))
                        *strchr(time_string, '\n') = 0;
-               if (strcmp(buf, "") == 0)
+               if (strcmp(hostname, "") == 0)
                        printf("Last login: %s\r\n", time_string);
                else
                        printf("Last login: %s from %s\r\n", time_string, hostname);
@@ -768,10 +788,10 @@ do_login(Session *s)
  * already exists, its value is overriden.
  */
 void
-child_set_env(char ***envp, unsigned int *envsizep, const char *name,
+child_set_env(char ***envp, u_int *envsizep, const char *name,
              const char *value)
 {
-       unsigned int i, namelen;
+       u_int i, namelen;
        char **env;
 
        /*
@@ -809,7 +829,7 @@ child_set_env(char ***envp, unsigned int *envsizep, const char *name,
  * and assignments of the form name=value.  No other forms are allowed.
  */
 void
-read_environment_file(char ***env, unsigned int *envsize,
+read_environment_file(char ***env, u_int *envsize,
                      const char *filename)
 {
        FILE *f;
@@ -855,11 +875,11 @@ void do_pam_environment(char ***env, int *envsize)
 
        if ((pam_env = fetch_pam_environment()) == NULL)
                return;
-       
+
        for(i = 0; pam_env[i] != NULL; i++) {
                if ((equals = strstr(pam_env[i], "=")) == NULL)
                        continue;
-                       
+
                if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
                        memset(var_name, '\0', sizeof(var_name));
                        memset(var_val, '\0', sizeof(var_val));
@@ -885,7 +905,7 @@ void copy_environment(char ***env, int *envsize)
        for(i = 0; environ[i] != NULL; i++) {
                if ((equals = strstr(environ[i], "=")) == NULL)
                        continue;
-                       
+
                if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
                        memset(var_name, '\0', sizeof(var_name));
                        memset(var_val, '\0', sizeof(var_val));
@@ -994,7 +1014,7 @@ do_child(const char *command, struct passwd * pw, const char *term,
        char buf[256];
        char cmd[1024];
        FILE *f = NULL;
-       unsigned int envsize, i;
+       u_int envsize, i;
        char **env;
        extern char **environ;
        struct stat st;
@@ -1002,6 +1022,14 @@ do_child(const char *command, struct passwd * pw, const char *term,
 #ifdef WITH_IRIX_PROJECT
        prid_t projid;
 #endif /* WITH_IRIX_PROJECT */
+#ifdef WITH_IRIX_JOBS
+       jid_t jid = 0;
+#else
+#ifdef WITH_IRIX_ARRAY
+       int jid = 0;
+#endif /* WITH_IRIX_ARRAY */
+#endif /* WITH_IRIX_JOBS */
+
 
        /* login(1) is only called if we execute the login shell */
        if (options.use_login && command != NULL)
@@ -1034,7 +1062,7 @@ do_child(const char *command, struct passwd * pw, const char *term,
 #ifdef HAVE_OSF_SIA
                extern char **saved_argv;
                extern int saved_argc;
-               char *host = get_canonical_hostname ();
+               char *host = get_canonical_hostname(options.reverse_mapping_check);
 
                if (sia_become_user(NULL, saved_argc, saved_argv, host,
                    pw->pw_name, ttyname, 0, NULL, NULL, SIA_BEU_SETLUID) !=
@@ -1074,11 +1102,20 @@ do_child(const char *command, struct passwd * pw, const char *term,
                                exit(1);
                        }
                        endgrent();
+#  ifdef WITH_IRIX_JOBS
+                       jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
+                       if (jid == -1) {
+                               fatal("Failed to create job container: %.100s",
+                                     strerror(errno));
+                       }
+#  endif /* WITH_IRIX_JOBS */
 #  ifdef WITH_IRIX_ARRAY
                        /* initialize array session */
-                       if (newarraysess() != 0)
-                               fatal("Failed to set up new array session: %.100s",
-                                     strerror(errno));
+                       if (jid == 0) {
+                               if (newarraysess() != 0)
+                                       fatal("Failed to set up new array session: %.100s",
+                                             strerror(errno));
+                       }
 #  endif /* WITH_IRIX_ARRAY */
 #  ifdef WITH_IRIX_PROJECT
                        /* initialize irix project info */
@@ -1090,6 +1127,14 @@ do_child(const char *command, struct passwd * pw, const char *term,
                          fatal("Failed to initialize project %d for %s: %.100s",
                                (int)projid, pw->pw_name, strerror(errno));
 #  endif /* WITH_IRIX_PROJECT */
+#ifdef WITH_IRIX_AUDIT
+                       if (sysconf(_SC_AUDIT)) {
+                               debug("Setting sat id to %d", (int) pw->pw_uid);
+                               if (satsetid(pw->pw_uid))
+                                       debug("error setting satid: %.100s", strerror(errno));
+                       }
+#endif /* WITH_IRIX_AUDIT */
+
                        /* Permanently switch to the desired uid. */
                        permanently_set_uid(pw->pw_uid);
 # endif /* HAVE_LOGIN_CAP */
@@ -1294,28 +1339,28 @@ do_child(const char *command, struct passwd * pw, const char *term,
         * in this order).
         */
        if (!options.use_login) {
-               if (stat(SSH_USER_RC, &st) >= 0) {
+               if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
                        if (debug_flag)
-                               fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_USER_RC);
+                               fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_USER_RC);
 
-                       f = popen(_PATH_BSHELL " " SSH_USER_RC, "w");
+                       f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
                        if (f) {
                                if (auth_proto != NULL && auth_data != NULL)
                                        fprintf(f, "%s %s\n", auth_proto, auth_data);
                                pclose(f);
                        } else
-                               fprintf(stderr, "Could not run %s\n", SSH_USER_RC);
-               } else if (stat(SSH_SYSTEM_RC, &st) >= 0) {
+                               fprintf(stderr, "Could not run %s\n", _PATH_SSH_USER_RC);
+               } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
                        if (debug_flag)
-                               fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_SYSTEM_RC);
+                               fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_SYSTEM_RC);
 
-                       f = popen(_PATH_BSHELL " " SSH_SYSTEM_RC, "w");
+                       f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
                        if (f) {
                                if (auth_proto != NULL && auth_data != NULL)
                                        fprintf(f, "%s %s\n", auth_proto, auth_data);
                                pclose(f);
                        } else
-                               fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC);
+                               fprintf(stderr, "Could not run %s\n", _PATH_SSH_SYSTEM_RC);
                } else if (options.xauth_location != NULL) {
                        /* Add authority data to .Xauthority if appropriate. */
                        if (auth_proto != NULL && auth_data != NULL) {
@@ -1325,11 +1370,13 @@ do_child(const char *command, struct passwd * pw, const char *term,
                                            "Running %.100s add %.100s %.100s %.100s\n",
                                            options.xauth_location, display,
                                            auth_proto, auth_data);
+#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
                                        if (screen != NULL)
                                                fprintf(stderr,
                                                    "Adding %.*s/unix%s %s %s\n",
                                                    (int)(screen-display), display,
                                                    screen, auth_proto, auth_data);
+#endif
                                }
                                snprintf(cmd, sizeof cmd, "%s -q -",
                                    options.xauth_location);
@@ -1337,10 +1384,12 @@ do_child(const char *command, struct passwd * pw, const char *term,
                                if (f) {
                                        fprintf(f, "add %s %s %s\n", display,
                                            auth_proto, auth_data);
-                                       if (screen != NULL) 
+#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
+                                       if (screen != NULL)
                                                fprintf(f, "add %.*s/unix%s %s %s\n",
                                                    (int)(screen-display), display,
                                                    screen, auth_proto, auth_data);
+#endif
                                        pclose(f);
                                } else {
                                        fprintf(stderr, "Could not run %s\n",
@@ -1432,7 +1481,6 @@ session_new(void)
                        sessions[i].used = 0;
                        sessions[i].self = i;
                }
-               num_used_sessions = 0;
                did_init = 1;
        }
        for(i = 0; i < MAX_SESSIONS; i++) {
@@ -1451,8 +1499,7 @@ session_new(void)
                        s->auth_proto = NULL;
                        s->used = 1;
                        s->pw = NULL;
-                       num_used_sessions++;
-                       debug("session_new: session %d (%d used)", i, num_used_sessions);
+                       debug("session_new: session %d", i);
                        return s;
                }
        }
@@ -1537,7 +1584,7 @@ session_window_change_req(Session *s)
 int
 session_pty_req(Session *s)
 {
-       unsigned int len;
+       u_int len;
        char *term_modes;       /* encoded terminal modes */
 
        if (no_pty_flag)
@@ -1586,7 +1633,7 @@ session_pty_req(Session *s)
 int
 session_subsystem_req(Session *s)
 {
-       unsigned int len;
+       u_int len;
        int success = 0;
        char *subsys = packet_get_string(&len);
        int i;
@@ -1682,7 +1729,7 @@ session_shell_req(Session *s)
 int
 session_exec_req(Session *s)
 {
-       unsigned int len;
+       u_int len;
        char *command = packet_get_string(&len);
        packet_done();
        if (forced_command) {
@@ -1700,10 +1747,27 @@ session_exec_req(Session *s)
        return 1;
 }
 
+int
+session_auth_agent_req(Session *s)
+{
+       static int called = 0;
+       packet_done();
+       if (no_agent_forwarding_flag) {
+               debug("session_auth_agent_req: no_agent_forwarding_flag");
+               return 0;
+       }
+       if (called) {
+               return 0;
+       } else {
+               called = 1;
+               return auth_input_request_forwarding(s->pw);
+       }
+}
+
 void
 session_input_channel_req(int id, void *arg)
 {
-       unsigned int len;
+       u_int len;
        int reply;
        int success = 0;
        char *rtype;
@@ -1736,6 +1800,8 @@ session_input_channel_req(int id, void *arg)
                        success =  session_pty_req(s);
                } else if (strcmp(rtype, "x11-req") == 0) {
                        success = session_x11_req(s);
+               } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
+                       success = session_auth_agent_req(s);
                } else if (strcmp(rtype, "subsystem") == 0) {
                        success = session_subsystem_req(s);
                }
@@ -1766,7 +1832,8 @@ session_set_fds(Session *s, int fdin, int fdout, int fderr)
                fatal("no channel for session %d", s->self);
        channel_set_fds(s->chanid,
            fdout, fdin, fderr,
-           fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ);
+           fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
+           1);
 }
 
 void
@@ -1865,7 +1932,6 @@ session_close(Session *s)
        session_pty_cleanup(s);
        session_free(s);
        session_proctitle(s);
-       num_used_sessions--;
 }
 
 void
@@ -1903,7 +1969,7 @@ session_close_by_channel(int id, void *arg)
                session_close(s);
        } else {
                /* notify child, delay session cleanup */
-               if (s->pid <= 1) 
+               if (s->pid <= 1)
                        fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
                if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
                        error("session_close_by_channel: kill %d: %s",
@@ -1911,11 +1977,6 @@ session_close_by_channel(int id, void *arg)
        }
 }
 
-int used_sessions(void)
-{
-       return(num_used_sessions);
-}
-
 char *
 session_tty_list(void)
 {
@@ -1945,11 +2006,8 @@ session_proctitle(Session *s)
 }
 
 void
-do_authenticated2(void)
+do_authenticated2(Authctxt *authctxt)
 {
-#ifdef HAVE_LOGIN_CAP
-       struct passwd *pw;
-#endif
 
        /*
         * Cancel the alarm we set to limit the time taken for
@@ -1960,9 +2018,8 @@ do_authenticated2(void)
                close(startup_pipe);
                startup_pipe = -1;
        }
-#ifdef HAVE_LOGIN_CAP
-       pw = auth_get_user();
-       if ((lc = login_getclass(pw->pw_class)) == NULL) {
+#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
+       if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
                error("unable to get login class");
                return;
        }
This page took 0.060717 seconds and 4 git commands to generate.