Source for gnu.javax.crypto.sasl.ServerFactory

   1: /* ServerFactory.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.sasl;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.javax.crypto.sasl.anonymous.AnonymousServer;
  43: import gnu.javax.crypto.sasl.crammd5.CramMD5Server;
  44: import gnu.javax.crypto.sasl.plain.PlainServer;
  45: import gnu.javax.crypto.sasl.srp.SRPServer;
  46: 
  47: import java.util.ArrayList;
  48: import java.util.Arrays;
  49: import java.util.Collections;
  50: import java.util.HashMap;
  51: import java.util.HashSet;
  52: import java.util.List;
  53: import java.util.Map;
  54: import java.util.Set;
  55: 
  56: import javax.security.auth.callback.CallbackHandler;
  57: import javax.security.sasl.Sasl;
  58: import javax.security.sasl.SaslException;
  59: import javax.security.sasl.SaslServer;
  60: import javax.security.sasl.SaslServerFactory;
  61: 
  62: /**
  63:  * The implementation of the {@link SaslServerFactory}.
  64:  */
  65: public class ServerFactory
  66:     implements SaslServerFactory
  67: {
  68:   // implicit 0-arguments constructor
  69: 
  70:   public static final Set getNames()
  71:   {
  72:     return Collections.unmodifiableSet(new HashSet(Arrays.asList(getNamesInternal(null))));
  73:   }
  74: 
  75:   private static final String[] getNamesInternal(Map props)
  76:   {
  77:     String[] all = new String[] {
  78:         Registry.SASL_SRP_MECHANISM,
  79:         Registry.SASL_CRAM_MD5_MECHANISM,
  80:         Registry.SASL_PLAIN_MECHANISM,
  81:         Registry.SASL_ANONYMOUS_MECHANISM };
  82:     List result = new ArrayList(4);
  83:     int i;
  84:     for (i = 0; i < all.length;)
  85:       result.add(all[i++]);
  86:     if (props == null)
  87:       return (String[]) result.toArray(new String[0]); // all
  88:     if (hasPolicy(Sasl.POLICY_PASS_CREDENTIALS, props)) // none
  89:       return new String[0];
  90:     if (hasPolicy(Sasl.POLICY_NOPLAINTEXT, props))
  91:       result.remove(Registry.SASL_PLAIN_MECHANISM);
  92:     if (hasPolicy(Sasl.POLICY_NOACTIVE, props))
  93:       {
  94:         result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
  95:         result.remove(Registry.SASL_PLAIN_MECHANISM);
  96:       }
  97:     if (hasPolicy(Sasl.POLICY_NODICTIONARY, props))
  98:       {
  99:         result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
 100:         result.remove(Registry.SASL_PLAIN_MECHANISM);
 101:       }
 102:     if (hasPolicy(Sasl.POLICY_NOANONYMOUS, props))
 103:       {
 104:         result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
 105:       }
 106:     if (hasPolicy(Sasl.POLICY_FORWARD_SECRECY, props))
 107:       {
 108:         result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
 109:         result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
 110:         result.remove(Registry.SASL_PLAIN_MECHANISM);
 111:       }
 112:     return (String[]) result.toArray(new String[0]);
 113:   }
 114: 
 115:   public static final ServerMechanism getInstance(String mechanism)
 116:   {
 117:     if (mechanism == null)
 118:       return null;
 119:     mechanism = mechanism.trim().toUpperCase();
 120:     if (mechanism.equals(Registry.SASL_SRP_MECHANISM))
 121:       return new SRPServer();
 122:     if (mechanism.equals(Registry.SASL_CRAM_MD5_MECHANISM))
 123:       return new CramMD5Server();
 124:     if (mechanism.equals(Registry.SASL_PLAIN_MECHANISM))
 125:       return new PlainServer();
 126:     if (mechanism.equals(Registry.SASL_ANONYMOUS_MECHANISM))
 127:       return new AnonymousServer();
 128:     return null;
 129:   }
 130: 
 131:   public SaslServer createSaslServer(String mechanism, String protocol,
 132:                                      String serverName, Map props,
 133:                                      CallbackHandler cbh) throws SaslException
 134:   {
 135:     ServerMechanism result = getInstance(mechanism);
 136:     if (result != null)
 137:       {
 138:         HashMap attributes = new HashMap();
 139:         if (props != null)
 140:           attributes.putAll(props);
 141:         attributes.put(Registry.SASL_PROTOCOL, protocol);
 142:         attributes.put(Registry.SASL_SERVER_NAME, serverName);
 143:         attributes.put(Registry.SASL_CALLBACK_HANDLER, cbh);
 144:         result.init(attributes);
 145:       }
 146:     return result;
 147:   }
 148: 
 149:   public String[] getMechanismNames(Map props)
 150:   {
 151:     return getNamesInternal(props);
 152:   }
 153: 
 154:   private static boolean hasPolicy(String propertyName, Map props)
 155:   {
 156:     return "true".equalsIgnoreCase(String.valueOf(props.get(propertyName)));
 157:   }
 158: }