]> andersk Git - openssh.git/blobdiff - session.c
- markus@cvs.openbsd.org 2001/06/19 15:40:45
[openssh.git] / session.c
index a29546d2fc38e81b2b66661dfe1aa92330823692..fbdc419eb5d73d4d6e67487e8d275c8ee3c58cc1 100644 (file)
--- a/session.c
+++ b/session.c
@@ -33,7 +33,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: session.c,v 1.82 2001/06/05 16:46:19 markus Exp $");
+RCSID("$OpenBSD: session.c,v 1.92 2001/06/19 15:40:45 markus Exp $");
 
 #include "ssh.h"
 #include "ssh1.h"
@@ -121,12 +121,14 @@ struct Session {
 
 Session *session_new(void);
 void   session_set_fds(Session *s, int fdin, int fdout, int fderr);
-void   session_pty_cleanup(Session *s);
+void   session_pty_cleanup(void *session);
+int    session_pty_req(Session *s);
 void   session_proctitle(Session *s);
 int    session_setup_x11fwd(Session *s);
 void   session_close(Session *s);
 void   do_exec_pty(Session *s, const char *command);
 void   do_exec_no_pty(Session *s, const char *command);
+void   do_exec(Session *s, const char *command);
 void   do_login(Session *s, const char *command);
 #ifdef LOGIN_NEEDS_UTMPX
 void   do_pre_login(Session *s);
@@ -134,7 +136,6 @@ void        do_pre_login(Session *s);
 void   do_child(Session *s, const char *command);
 void   do_motd(void);
 int    check_quietlogin(Session *s, const char *command);
-void   xauthfile_cleanup_proc(void *pw);
 
 void   do_authenticated1(Authctxt *authctxt);
 void   do_authenticated2(Authctxt *authctxt);
@@ -148,11 +149,8 @@ extern u_int utmp_len;
 extern int startup_pipe;
 extern void destroy_sensitive_data(void);
 
-/* Local Xauthority file. */
-static char *xauthfile;
-
 /* original command from peer. */
-char *original_command = NULL;
+const char *original_command = NULL;
 
 /* data */
 #define MAX_SESSIONS 10
@@ -200,58 +198,11 @@ do_authenticated(Authctxt *authctxt)
        else
                do_authenticated1(authctxt);
 
-       /* remote user's local Xauthority file and agent socket */
-       if (xauthfile)
-               xauthfile_cleanup_proc(authctxt->pw);
+       /* remove agent socket */
        if (auth_get_socket_name())
                auth_sock_cleanup_proc(authctxt->pw);
 }
 
-/*
- * Remove local Xauthority file.
- */
-void
-xauthfile_cleanup_proc(void *_pw)
-{
-       struct passwd *pw = _pw;
-       char *p;
-
-       debug("xauthfile_cleanup_proc called");
-       if (xauthfile != NULL) {
-               temporarily_use_uid(pw);
-               unlink(xauthfile);
-               p = strrchr(xauthfile, '/');
-               if (p != NULL) {
-                       *p = '\0';
-                       rmdir(xauthfile);
-               }
-               xfree(xauthfile);
-               xauthfile = NULL;
-               restore_uid();
-       }
-}
-
-/*
- * Function to perform cleanup if we get aborted abnormally (e.g., due to a
- * dropped connection).
- */
-void
-pty_cleanup_proc(void *session)
-{
-       Session *s=session;
-       if (s == NULL)
-               fatal("pty_cleanup_proc: no session");
-       debug("pty_cleanup_proc: %s", s->tty);
-
-       if (s->pid != 0) {
-               /* Record that the user has logged out. */
-               record_logout(s->pid, s->tty);
-       }
-
-       /* Release the pseudo-tty. */
-       pty_release(s->tty);
-}
-
 /*
  * Prepares for an interactive session.  This is called after the user has
  * been successfully authenticated.  During this message exchange, pseudo
@@ -263,7 +214,7 @@ do_authenticated1(Authctxt *authctxt)
 {
        Session *s;
        char *command;
-       int success, type, n_bytes, plen, screen_flag, have_pty = 0;
+       int success, type, plen, screen_flag;
        int compression_level = 0, enable_compression_after_reply = 0;
        u_int proto_len, data_len, dlen;
 
@@ -296,51 +247,7 @@ do_authenticated1(Authctxt *authctxt)
                        break;
 
                case SSH_CMSG_REQUEST_PTY:
-                       if (no_pty_flag) {
-                               debug("Allocating a pty not permitted for this authentication.");
-                               break;
-                       }
-                       if (have_pty)
-                               packet_disconnect("Protocol error: you already have a pty.");
-
-                       debug("Allocating pty.");
-
-                       /* Allocate a pty and open it. */
-                       if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
-                           sizeof(s->tty))) {
-                               error("Failed to allocate pty.");
-                               break;
-                       }
-                       fatal_add_cleanup(pty_cleanup_proc, (void *)s);
-                       pty_setowner(s->pw, s->tty);
-
-                       /* Get TERM from the packet.  Note that the value may be of arbitrary length. */
-                       s->term = packet_get_string(&dlen);
-                       packet_integrity_check(dlen, strlen(s->term), type);
-                       /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
-                       /* Remaining bytes */
-                       n_bytes = plen - (4 + dlen + 4 * 4);
-
-                       if (strcmp(s->term, "") == 0) {
-                               xfree(s->term);
-                               s->term = NULL;
-                       }
-                       /* Get window size from the packet. */
-                       s->row = packet_get_int();
-                       s->col = packet_get_int();
-                       s->xpixel = packet_get_int();
-                       s->ypixel = packet_get_int();
-                       pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
-
-                       /* Get tty modes from the packet. */
-                       tty_parse_modes(s->ttyfd, &n_bytes);
-                       packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
-
-                       session_proctitle(s);
-
-                       /* Indicate that we now have a pty. */
-                       success = 1;
-                       have_pty = 1;
+                       success = session_pty_req(s);
                        break;
 
                case SSH_CMSG_X11_REQUEST_FORWARDING:
@@ -364,6 +271,8 @@ do_authenticated1(Authctxt *authctxt)
                        if (!success) {
                                xfree(s->auth_proto);
                                xfree(s->auth_data);
+                               s->auth_proto = NULL;
+                               s->auth_data = NULL;
                        }
                        break;
 
@@ -400,22 +309,12 @@ do_authenticated1(Authctxt *authctxt)
                        if (type == SSH_CMSG_EXEC_CMD) {
                                command = packet_get_string(&dlen);
                                debug("Exec command '%.500s'", command);
-                               packet_integrity_check(plen, 4 + dlen, type);
+                               do_exec(s, command);
+                               xfree(command);
                        } else {
-                               command = NULL;
-                               packet_integrity_check(plen, 0, type);
+                               do_exec(s, NULL);
                        }
-                       if (forced_command != NULL) {
-                               original_command = command;
-                               command = forced_command;
-                               debug("Forced command '%.500s'", forced_command);
-                       }
-                       if (have_pty)
-                               do_exec_pty(s, command);
-                       else
-                               do_exec_no_pty(s, command);
-                       if (command != NULL)
-                               xfree(command);
+                       packet_done();
                        session_close(s);
                        return;
 
@@ -690,6 +589,29 @@ do_pre_login(Session *s)
 }
 #endif
 
+/*
+ * This is called to fork and execute a command.  If another command is
+ * to be forced, execute that instead.
+ */
+void
+do_exec(Session *s, const char *command)
+{
+       if (forced_command) {
+               original_command = command;
+               command = forced_command;
+               forced_command = NULL;
+               debug("Forced command '%.900s'", command);
+       }
+
+       if (s->ttyfd != -1)
+               do_exec_pty(s, command);
+       else
+               do_exec_no_pty(s, command);
+
+       original_command = NULL;
+}
+
+
 /* administrative, login(1)-like work */
 void
 do_login(Session *s, const char *command)
@@ -1047,7 +969,7 @@ do_child(Session *s, const char *command)
        extern char **environ;
        struct stat st;
        char *argv[10];
-       int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
+       int do_xauth;
 #ifdef WITH_IRIX_PROJECT
        prid_t projid;
 #endif /* WITH_IRIX_PROJECT */
@@ -1059,6 +981,9 @@ do_child(Session *s, const char *command)
 #endif /* WITH_IRIX_ARRAY */
 #endif /* WITH_IRIX_JOBS */
 
+       do_xauth =
+           s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
+
        /* remove hostkey from the child's memory */
        destroy_sensitive_data();
 
@@ -1316,8 +1241,6 @@ do_child(Session *s, const char *command)
        do_pam_environment(&env, &envsize);
 #endif /* USE_PAM */
 
-       if (xauthfile)
-               child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
        if (auth_get_socket_name() != NULL)
                child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
                              auth_get_socket_name());
@@ -1547,7 +1470,6 @@ session_new(void)
                debug("session_new: init");
                for(i = 0; i < MAX_SESSIONS; i++) {
                        sessions[i].used = 0;
-                       sessions[i].self = i;
                }
                did_init = 1;
        }
@@ -1559,6 +1481,7 @@ session_new(void)
                        s->ptyfd = -1;
                        s->ttyfd = -1;
                        s->used = 1;
+                       s->self = i;
                        debug("session_new: session %d", i);
                        return s;
                }
@@ -1647,13 +1570,24 @@ session_pty_req(Session *s)
        u_int len;
        int n_bytes;
 
-       if (no_pty_flag)
+       if (no_pty_flag) {
+               debug("Allocating a pty not permitted for this authentication.");
                return 0;
-       if (s->ttyfd != -1)
+       }
+       if (s->ttyfd != -1) {
+               packet_disconnect("Protocol error: you already have a pty.");
                return 0;
+       }
+
        s->term = packet_get_string(&len);
-       s->col = packet_get_int();
-       s->row = packet_get_int();
+
+       if (compat20) {
+               s->col = packet_get_int();
+               s->row = packet_get_int();
+       } else {
+               s->row = packet_get_int();
+               s->col = packet_get_int();
+       }
        s->xpixel = packet_get_int();
        s->ypixel = packet_get_int();
 
@@ -1661,9 +1595,12 @@ session_pty_req(Session *s)
                xfree(s->term);
                s->term = NULL;
        }
+
        /* Allocate a pty and open it. */
+       debug("Allocating pty.");
        if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
-               xfree(s->term);
+               if (s->term)
+                       xfree(s->term);
                s->term = NULL;
                s->ptyfd = -1;
                s->ttyfd = -1;
@@ -1671,21 +1608,24 @@ session_pty_req(Session *s)
                return 0;
        }
        debug("session_pty_req: session %d alloc %s", s->self, s->tty);
+
+       /* for SSH1 the tty modes length is not given */
+       if (!compat20)
+               n_bytes = packet_remaining();
+       tty_parse_modes(s->ttyfd, &n_bytes);
+
        /*
         * Add a cleanup function to clear the utmp entry and record logout
         * time in case we call fatal() (e.g., the connection gets closed).
         */
-       fatal_add_cleanup(pty_cleanup_proc, (void *)s);
+       fatal_add_cleanup(session_pty_cleanup, (void *)s);
        pty_setowner(s->pw, s->tty);
-       /* Get window size from the packet. */
+
+       /* Set window size from the packet. */
        pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
 
-       /* Get tty modes from the packet. */
-       tty_parse_modes(s->ttyfd, &n_bytes);
        packet_done();
-
        session_proctitle(s);
-
        return 1;
 }
 
@@ -1731,6 +1671,8 @@ session_x11_req(Session *s)
        if (!success) {
                xfree(s->auth_proto);
                xfree(s->auth_data);
+               s->auth_proto = NULL;
+               s->auth_data = NULL;
        }
        return success;
 }
@@ -1738,13 +1680,8 @@ session_x11_req(Session *s)
 int
 session_shell_req(Session *s)
 {
-       /* if forced_command == NULL, the shell is execed */
-       char *shell = forced_command;
        packet_done();
-       if (s->ttyfd == -1)
-               do_exec_no_pty(s, shell);
-       else
-               do_exec_pty(s, shell);
+       do_exec(s, NULL);
        return 1;
 }
 
@@ -1754,17 +1691,8 @@ session_exec_req(Session *s)
        u_int len;
        char *command = packet_get_string(&len);
        packet_done();
-       if (forced_command) {
-               original_command = command;
-               command = forced_command;
-               debug("Forced command '%.500s'", forced_command);
-       }
-       if (s->ttyfd == -1)
-               do_exec_no_pty(s, command);
-       else
-               do_exec_pty(s, command);
-       if (forced_command == NULL)
-               xfree(command);
+       do_exec(s, command);
+       xfree(command);
        return 1;
 }
 
@@ -1857,19 +1785,27 @@ session_set_fds(Session *s, int fdin, int fdout, int fderr)
            1);
 }
 
+/*
+ * Function to perform pty cleanup. Also called if we get aborted abnormally
+ * (e.g., due to a dropped connection).
+ */
 void
-session_pty_cleanup(Session *s)
+session_pty_cleanup(void *session)
 {
-       if (s == NULL || s->ttyfd == -1)
+       Session *s = session;
+
+       if (s == NULL) {
+               error("session_pty_cleanup: no session");
+               return;
+       }
+       if (s->ttyfd == -1)
                return;
 
        debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
 
-       /* Cancel the cleanup function. */
-       fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
-
        /* Record that the user has logged out. */
-       record_logout(s->pid, s->tty);
+       if (s->pid != 0)
+               record_logout(s->pid, s->tty);
 
        /* Release the pseudo-tty. */
        pty_release(s->tty);
@@ -1891,7 +1827,7 @@ session_exit_message(Session *s, int status)
                fatal("session_close: no session");
        c = channel_lookup(s->chanid);
        if (c == NULL)
-               fatal("session_close: session %d: no channel %d",
+               fatal("session_exit_message: session %d: no channel %d",
                    s->self, s->chanid);
        debug("session_exit_message: session %d channel %d pid %d",
            s->self, s->chanid, s->pid);
@@ -1933,9 +1869,13 @@ session_exit_message(Session *s, int status)
 }
 
 void
-session_free(Session *s)
+session_close(Session *s)
 {
-       debug("session_free: session %d pid %d", s->self, s->pid);
+       debug("session_close: session %d pid %d", s->self, s->pid);
+       if (s->ttyfd != -1) {
+               fatal_remove_cleanup(session_pty_cleanup, (void *)s);
+               session_pty_cleanup(s);
+       }
        if (s->term)
                xfree(s->term);
        if (s->display)
@@ -1945,17 +1885,6 @@ session_free(Session *s)
        if (s->auth_proto)
                xfree(s->auth_proto);
        s->used = 0;
-}
-
-void
-session_close(Session *s)
-{
-       if (s->display) {
-               xauthfile_cleanup_proc(s->pw);
-               fatal_remove_cleanup(xauthfile_cleanup_proc, s->pw);
-       }
-       session_pty_cleanup(s);
-       session_free(s);
        session_proctitle(s);
 }
 
@@ -1964,7 +1893,7 @@ session_close_by_pid(pid_t pid, int status)
 {
        Session *s = session_by_pid(pid);
        if (s == NULL) {
-               debug("session_close_by_pid: no session for pid %d", s->pid);
+               debug("session_close_by_pid: no session for pid %d", pid);
                return;
        }
        if (s->chanid != -1)
@@ -2031,7 +1960,6 @@ session_proctitle(Session *s)
 int
 session_setup_x11fwd(Session *s)
 {
-       int fd;
        struct stat st;
 
        if (no_x11_forwarding_flag) {
@@ -2047,32 +1975,20 @@ session_setup_x11fwd(Session *s)
                packet_send_debug("No xauth program; cannot forward with spoofing.");
                return 0;
        }
-       if (s->display != NULL || xauthfile != NULL) {
-               debug("X11 display already set.");
+       if (options.use_login) {
+               packet_send_debug("X11 forwarding disabled; "
+                   "not compatible with UseLogin=yes.");
                return 0;
        }
-       xauthfile = xmalloc(MAXPATHLEN);
-       strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
-       temporarily_use_uid(s->pw);
-       if (mkdtemp(xauthfile) == NULL) {
-               restore_uid();
-               error("private X11 dir: mkdtemp %s failed: %s",
-                   xauthfile, strerror(errno));
-               xfree(xauthfile);
-               xauthfile = NULL;
+       if (s->display != NULL) {
+               debug("X11 display already set.");
                return 0;
        }
-       strlcat(xauthfile, "/cookies", MAXPATHLEN);
-       fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
-       if (fd >= 0)
-               close(fd);
-       restore_uid();
        s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
        if (s->display == NULL) {
-               xauthfile_cleanup_proc(s->pw);
+               debug("x11_create_display_inet failed.");
                return 0;
        }
-       fatal_add_cleanup(xauthfile_cleanup_proc, s->pw);
        return 1;
 }
 
This page took 0.07415 seconds and 4 git commands to generate.