ServerFactory.java 5.5 KB

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