Index: SMTPHandler.java
===================================================================
RCS file: /home/cvspublic/jakarta-james/src/java/org/apache/james/smtpserver/SMTPHandler.java,v
retrieving revision 1.8
diff -u -r1.8 SMTPHandler.java
--- SMTPHandler.java	2001/08/11 21:25:15	1.8
+++ SMTPHandler.java	2001/11/18 16:17:43
@@ -34,6 +34,7 @@
 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,11 +89,13 @@
     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(false);
+           = configuration.getChild("authRequired").getValueAsBoolean(true);
         verifyIdentity
            = configuration.getChild("verifyIdentity").getValueAsBoolean(false);
         // get the message size limit from the conf file and multiply
@@ -102,6 +105,8 @@
         if (DEEP_DEBUG) {
             getLogger().debug("Max message size is: " + maxmessagesize);
         }
+
+        Sasl.setSaslServerFactory(serverFactory);
     }
 
     public void compose( final ComponentManager componentManager )
@@ -272,7 +277,7 @@
             state.put(CURRENT_HELO_MODE, command);
             state.put(NAME_GIVEN, argument);
 	    if (authRequired) {
-	        out.println("250-AUTH LOGIN PLAIN");
+	        out.println("250-AUTH "+getAuthString());
             }
 	    if (maxmessagesize > 0) {
 	        out.println("250-SIZE " + maxmessagesize);
@@ -287,37 +292,51 @@
 
     }
 
+    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 (authentication type) <challenge>");
+            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("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,12 +358,37 @@
                 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;
         }
+
+        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) {
