Author: gnodet
Date: Mon Jul 27 07:15:01 2009
New Revision: 798048

URL: http://svn.apache.org/viewvc?rev=798048&view=rev
Log:
SSHD-36: PasswordAuthenticator and Shell should have access to ServerSession 
(patch provided by Frank)

Modified:
    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/SshServer.java
    
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AttributeKey.java
    
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
    
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/ShellFactory.java
    
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPassword.java
    
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
    
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticator.java
    
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusPasswordAuthenticator.java
    
mina/sshd/trunk/sshd-pam/src/main/java/org/apache/sshd/server/pam/PAMPasswordAuthenticator.java

Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/SshServer.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/SshServer.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/SshServer.java 
(original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/SshServer.java Mon 
Jul 27 07:15:01 2009
@@ -65,6 +65,7 @@
 import org.apache.sshd.server.channel.ChannelSession;
 import org.apache.sshd.server.kex.DHG1;
 import org.apache.sshd.server.kex.DHG14;
+import org.apache.sshd.server.session.ServerSession;
 import org.apache.sshd.server.shell.ProcessShellFactory;
 
 /**
@@ -343,7 +344,7 @@
         sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", 
"-i", "-l" }));
         //sshd.setPasswordAuthenticator(new PAMPasswordAuthenticator());
         sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
-            public Object authenticate(String username, String password) {
+            public Object authenticate(String username, String password, 
ServerSession session) {
                 return (username != null && username.equals(password)) ? 
username : null;
             }
         });

Modified: 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AttributeKey.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AttributeKey.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AttributeKey.java
 (original)
+++ 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AttributeKey.java
 Mon Jul 27 07:15:01 2009
@@ -20,6 +20,19 @@
 
 /**
  * Type safe key for storage within the user attributes of {...@link 
AbstractSession}.
+ * Typically it is used as a static variable that is shared between the 
producer
+ * and the consumer. To further restrict access the setting or getting it from
+ * the ServerSession you can add static get and set methods, e.g:
+ * 
+ * private static final AttributeKey<MyValue> MY_KEY = new 
AttributeKey<MyValue>();
+ *
+ * public static MyValue getMyValue(ServerSession s) {
+ *   return s.getAttribute(MY_KEY);
+ * }
+ *
+ * private void setMyValye(ServerSession s, MyValue value) {
+ *   s.setAttribute(MY_KEY, value);
+ * }
  *
  * @param T type of value stored in the attribute.
  *

Modified: 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
 (original)
+++ 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
 Mon Jul 27 07:15:01 2009
@@ -18,6 +18,8 @@
  */
 package org.apache.sshd.server;
 
+import org.apache.sshd.server.session.ServerSession;
+
 /**
  * The <code>PasswordAuthenticator</code> is used to authenticate
  * users based on a password.
@@ -34,6 +36,6 @@
      * @param password the password
      * @return a non null identity object or <code>null</code if 
authentication fail
      */
-    Object authenticate(String username, String password);
+    Object authenticate(String username, String password, ServerSession 
session);
 
 }

Modified: 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/ShellFactory.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/ShellFactory.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/ShellFactory.java
 (original)
+++ 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/ShellFactory.java
 Mon Jul 27 07:15:01 2009
@@ -23,6 +23,8 @@
 import java.io.OutputStream;
 import java.util.Map;
 
+import org.apache.sshd.server.session.ServerSession;
+
 /**
  * This factory is used by SSH server when the client connected requests the 
creation
  * of a shell.
@@ -162,5 +164,19 @@
         void onExit(int exitValue);
 
     }
+    
+    /**
+     * Interface that can be implemented by a shell to be able to access the
+     * server session in which this shell will be used.
+     */
+    public interface SessionAware {
+
+        /**
+         * Set the server session in which this shell will be executed.
+         *
+         * @param session
+         */
+        void setSession(ServerSession session);
+    }
 
 }

Modified: 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPassword.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPassword.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPassword.java
 (original)
+++ 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPassword.java
 Mon Jul 27 07:15:01 2009
@@ -52,7 +52,7 @@
     private Object checkPassword(ServerSession session, String username, 
String password) throws Exception {
         PasswordAuthenticator auth = 
session.getServerFactoryManager().getPasswordAuthenticator();
         if (auth != null) {
-            Object identity = auth.authenticate(username, password);
+            Object identity = auth.authenticate(username, password, session);
             if (identity != null) {
                 return identity;
             } else {

Modified: 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
 (original)
+++ 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
 Mon Jul 27 07:15:01 2009
@@ -421,6 +421,10 @@
         }
         addEnvVariable("USER", ((ServerSession) session).getUsername());
         shell = ((ServerSession) 
session).getServerFactoryManager().getShellFactory().createShell();
+        // If the shell wants to be aware of the session, let's do that
+        if (shell instanceof ShellFactory.SessionAware) {
+            ((ShellFactory.SessionAware) shell).setSession((ServerSession) 
session);
+        }
         out = new ChannelOutputStream(this, remoteWindow, log, 
SshConstants.Message.SSH_MSG_CHANNEL_DATA);
         err = new ChannelOutputStream(this, remoteWindow, log, 
SshConstants.Message.SSH_MSG_CHANNEL_EXTENDED_DATA);
         // Wrap in logging filters

Modified: 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticator.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticator.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticator.java
 (original)
+++ 
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticator.java
 Mon Jul 27 07:15:01 2009
@@ -29,6 +29,7 @@
 import javax.security.auth.login.LoginContext;
 
 import org.apache.sshd.server.PasswordAuthenticator;
+import org.apache.sshd.server.session.ServerSession;
 
 /**
  * TODO Add javadoc
@@ -47,6 +48,10 @@
         this.domain = domain;
     }
 
+    public Object authenticate(final String username, final String password, 
final ServerSession session) {
+       return authenticate(username, password);
+    }
+    
     public Object authenticate(final String username, final String password) {
         try {
             Subject subject = new Subject();

Modified: 
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusPasswordAuthenticator.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusPasswordAuthenticator.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusPasswordAuthenticator.java
 (original)
+++ 
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusPasswordAuthenticator.java
 Mon Jul 27 07:15:01 2009
@@ -19,6 +19,7 @@
 package org.apache.sshd.util;
 
 import org.apache.sshd.server.PasswordAuthenticator;
+import org.apache.sshd.server.session.ServerSession;
 
 /**
  * TODO Add javadoc
@@ -27,7 +28,7 @@
  */
 public class BogusPasswordAuthenticator implements PasswordAuthenticator {
 
-    public Object authenticate(String username, String password) {
+    public Object authenticate(String username, String password, ServerSession 
session) {
         return (username != null && username.equals(password)) ? username : 
null;
     }
 }

Modified: 
mina/sshd/trunk/sshd-pam/src/main/java/org/apache/sshd/server/pam/PAMPasswordAuthenticator.java
URL: 
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-pam/src/main/java/org/apache/sshd/server/pam/PAMPasswordAuthenticator.java?rev=798048&r1=798047&r2=798048&view=diff
==============================================================================
--- 
mina/sshd/trunk/sshd-pam/src/main/java/org/apache/sshd/server/pam/PAMPasswordAuthenticator.java
 (original)
+++ 
mina/sshd/trunk/sshd-pam/src/main/java/org/apache/sshd/server/pam/PAMPasswordAuthenticator.java
 Mon Jul 27 07:15:01 2009
@@ -21,6 +21,7 @@
 import net.sf.jpam.Pam;
 import net.sf.jpam.PamReturnValue;
 import org.apache.sshd.server.PasswordAuthenticator;
+import org.apache.sshd.server.session.ServerSession;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,7 +46,7 @@
         this.service = service;
     }
 
-    public Object authenticate(String username, String password) {
+    public Object authenticate(String username, String password, ServerSession 
session) {
         LOG.info("Authenticating user {} using PAM", username);
         PamReturnValue val = new Pam(service).authenticate(username, password);
         LOG.info("Result: {}", val);


Reply via email to