Index: SMTPHandler.java
===================================================================
RCS file: /home/cvspublic/jakarta-james/src/java/org/apache/james/smtpserver/SMTPHandler.java,v
retrieving revision 1.8
diff -c -r1.8 SMTPHandler.java
*** SMTPHandler.java	2001/08/11 21:25:15	1.8
--- SMTPHandler.java	2001/11/18 16:17:21
***************
*** 34,39 ****
--- 34,40 ----
  import org.apache.james.services.UsersStore;
  import org.apache.james.util.*;
  import org.apache.mailet.*;
+ import javax.security.sasl.*;
  
  /**
   * This handles an individual incoming message.  It handles regular SMTP
***************
*** 88,98 ****
      private Random random       = new Random();
      private long maxmessagesize = 0;
  
      public void configure ( Configuration configuration )
             throws ConfigurationException {
          super.configure(configuration);
          authRequired
!            = configuration.getChild("authRequired").getValueAsBoolean(false);
          verifyIdentity
             = configuration.getChild("verifyIdentity").getValueAsBoolean(false);
          // get the message size limit from the conf file and multiply
--- 89,101 ----
      private Random random       = new Random();
      private long maxmessagesize = 0;
  
+     private static SaslServerFactory serverFactory = new cryptix.sasl.ServerFactory();
+ 
      public void configure ( Configuration configuration )
             throws ConfigurationException {
          super.configure(configuration);
          authRequired
!            = configuration.getChild("authRequired").getValueAsBoolean(true);
          verifyIdentity
             = configuration.getChild("verifyIdentity").getValueAsBoolean(false);
          // get the message size limit from the conf file and multiply
***************
*** 102,107 ****
--- 105,112 ----
          if (DEEP_DEBUG) {
              getLogger().debug("Max message size is: " + maxmessagesize);
          }
+ 
+         Sasl.setSaslServerFactory(serverFactory);
      }
  
      public void compose( final ComponentManager componentManager )
***************
*** 272,278 ****
              state.put(CURRENT_HELO_MODE, command);
              state.put(NAME_GIVEN, argument);
  	    if (authRequired) {
! 	        out.println("250-AUTH LOGIN PLAIN");
              }
  	    if (maxmessagesize > 0) {
  	        out.println("250-SIZE " + maxmessagesize);
--- 277,283 ----
              state.put(CURRENT_HELO_MODE, command);
              state.put(NAME_GIVEN, argument);
  	    if (authRequired) {
! 	        out.println("250-AUTH "+getAuthString());
              }
  	    if (maxmessagesize > 0) {
  	        out.println("250-SIZE " + maxmessagesize);
***************
*** 287,323 ****
  
      }
  
      private void doAUTH(String command,String argument,String argument1)
              throws Exception {
          if (state.containsKey(AUTH)) {
              out.println("503 User has previously authenticated."
                          + " Further authentication is not required!");
              return;
          } else if (argument == null) {
!             out.println("501 Usage: AUTH (authentication type) <challenge>");
              return;
!         } else if (argument.equalsIgnoreCase("PLAIN")) {
!             String userpass, user, pass;
!             StringTokenizer authTokenizer;
!             if (argument1 == null) {
!                 out.println("334 OK. Continue authentication");
!                 userpass = in.readLine().trim();
!             } else
!                 userpass = argument1.trim();
!             authTokenizer = new StringTokenizer(
!                               Base64.decodeAsString(userpass), "\0");
!             user = authTokenizer.nextToken();
!             pass = authTokenizer.nextToken();
!             // Authenticate user
!             if (users.test(user, pass)) {
!                 state.put(AUTH, user);
!                 out.println("235 Authentication Successful");
!                 getLogger().info("AUTH method PLAIN succeeded");
!             } else {
!                 out.println("535 Authentication Failed");
!                 getLogger().error("AUTH method PLAIN failed");
!             }
!             return;
          } else if (argument.equalsIgnoreCase("LOGIN")) {
              String user, pass;
  
--- 292,342 ----
  
      }
  
+     private String getAuthString() {
+         StringBuffer authString = new StringBuffer("LOGIN ");
+         String[] mechanisms = serverFactory.getMechanismNames(null);
+         if (mechanisms.length > 0) {
+             authString.append(mechanisms[0]);
+             for (int i=1;i<mechanisms.length;i++) {
+                 authString.append(" "+mechanisms[i]);
+             }
+         }
+         return authString.toString();
+     }
+ 
      private void doAUTH(String command,String argument,String argument1)
              throws Exception {
+         String[] mechanisms = serverFactory.getMechanismNames(null);
          if (state.containsKey(AUTH)) {
              out.println("503 User has previously authenticated."
                          + " Further authentication is not required!");
              return;
          } else if (argument == null) {
!             out.println("501 Usage: AUTH <"+getAuthString()+
!                         "> [<initial response>]");
              return;
! //      } else if (argument.equalsIgnoreCase("PLAIN")) {
! //          String userpass, user, pass;
! //          StringTokenizer authTokenizer;
! //          if (argument1 == null) {
! //              out.println("334 OK. Continue authentication");
! //              userpass = in.readLine().trim();
! //          } else
! //              userpass = argument1.trim();
! //          authTokenizer = new StringTokenizer(
! //                            Base64.decodeAsString(userpass), "\0");
! //          user = authTokenizer.nextToken();
! //          pass = authTokenizer.nextToken();
! //          // Authenticate user
! //          if (users.test(user, pass)) {
! //              state.put(AUTH, user);
! //              out.println("235 Authentication Successful");
! //              getLogger().info("AUTH method PLAIN succeeded");
! //          } else {
! //              out.println("535 Authentication Failed");
! //              getLogger().error("AUTH method PLAIN failed");
! //          }
! //          return;
          } else if (argument.equalsIgnoreCase("LOGIN")) {
              String user, pass;
  
***************
*** 339,350 ****
                  getLogger().error("AUTH method LOGIN failed");
              }
              return;
-         } else {
-             out.println("504 Unrecognized Authentication Type");
-             getLogger().error("AUTH method " + argument
-                               + " is an unrecognized authentication type");
-             return;
          }
      }
  
      private void doMAIL(String command,String argument,String argument1) {
--- 358,394 ----
                  getLogger().error("AUTH method LOGIN failed");
              }
              return;
          }
+ 
+         for (int i=0;i<mechanisms.length;i++) {
+             if (argument.equalsIgnoreCase(mechanisms[i])) {
+                 java.util.Hashtable properties = new java.util.Hashtable();
+                 properties.put("cryptix.sasl.srp.password.file","/tmp/cryptix-sasl/etc/tpasswd");
+                 properties.put("cryptix.sasl.plain.password.file","/tmp/cryptix-sasl/etc/passwd");
+                 SaslServer server =
+                     Sasl.createSaslServer(mechanisms[i],
+                                           "SMTP",
+                                           (String)state.get(SERVER_NAME),
+                                           properties,
+                                           null);
+                 
+                 SaslProfile profile = new SaslProfile(server, in, out);
+                 if (profile.doAUTH(argument1)) { 
+                     state.put(AUTH, server.getAuthorizationID());
+                     out.println("235 Authentication Successful");
+                     getLogger().info("AUTH method "+mechanisms[i]+" succeeded");
+                 } else {
+                     out.println("535 Authentication Failed");
+                     getLogger().error("AUTH method "+mechanisms[i]+" failed");
+                 }
+                 return;
+             }
+         }
+ 
+         out.println("504 Unrecognized Authentication Type");
+         getLogger().error("AUTH method " + argument
+                           + " is an unrecognized authentication type");
+         return;
      }
  
      private void doMAIL(String command,String argument,String argument1) {
