CramMD5AuthInfoProvider.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* CramMD5AuthInfoProvider.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.crammd5;
  32. import gnu.java.security.Registry;
  33. import gnu.javax.crypto.sasl.IAuthInfoProvider;
  34. import gnu.javax.crypto.sasl.NoSuchUserException;
  35. import java.io.IOException;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import javax.security.sasl.AuthenticationException;
  39. /**
  40. * The CRAM-MD5 mechanism authentication information provider implementation.
  41. */
  42. public class CramMD5AuthInfoProvider
  43. implements IAuthInfoProvider
  44. {
  45. private PasswordFile passwordFile = null;
  46. // implicit 0-args constrcutor
  47. public void activate(Map context) throws AuthenticationException
  48. {
  49. try
  50. {
  51. if (context == null)
  52. passwordFile = new PasswordFile();
  53. else
  54. {
  55. String pfn = (String) context.get(CramMD5Registry.PASSWORD_FILE);
  56. if (pfn == null)
  57. passwordFile = new PasswordFile();
  58. else
  59. passwordFile = new PasswordFile(pfn);
  60. }
  61. }
  62. catch (IOException x)
  63. {
  64. throw new AuthenticationException("activate()", x);
  65. }
  66. }
  67. public void passivate() throws AuthenticationException
  68. {
  69. passwordFile = null;
  70. }
  71. public boolean contains(String userName) throws AuthenticationException
  72. {
  73. if (passwordFile == null)
  74. throw new AuthenticationException("contains()",
  75. new IllegalStateException());
  76. boolean result = false;
  77. try
  78. {
  79. result = passwordFile.contains(userName);
  80. }
  81. catch (IOException x)
  82. {
  83. throw new AuthenticationException("contains()", x);
  84. }
  85. return result;
  86. }
  87. public Map lookup(Map userID) throws AuthenticationException
  88. {
  89. if (passwordFile == null)
  90. throw new AuthenticationException("lookup()", new IllegalStateException());
  91. Map result = new HashMap();
  92. try
  93. {
  94. String userName = (String) userID.get(Registry.SASL_USERNAME);
  95. if (userName == null)
  96. throw new NoSuchUserException("");
  97. String[] data = passwordFile.lookup(userName);
  98. result.put(Registry.SASL_USERNAME, data[0]);
  99. result.put(Registry.SASL_PASSWORD, data[1]);
  100. result.put(CramMD5Registry.UID_FIELD, data[2]);
  101. result.put(CramMD5Registry.GID_FIELD, data[3]);
  102. result.put(CramMD5Registry.GECOS_FIELD, data[4]);
  103. result.put(CramMD5Registry.DIR_FIELD, data[5]);
  104. result.put(CramMD5Registry.SHELL_FIELD, data[6]);
  105. }
  106. catch (Exception x)
  107. {
  108. if (x instanceof AuthenticationException)
  109. throw (AuthenticationException) x;
  110. throw new AuthenticationException("lookup()", x);
  111. }
  112. return result;
  113. }
  114. public void update(Map userCredentials) throws AuthenticationException
  115. {
  116. if (passwordFile == null)
  117. throw new AuthenticationException("update()", new IllegalStateException());
  118. try
  119. {
  120. String userName = (String) userCredentials.get(Registry.SASL_USERNAME);
  121. String password = (String) userCredentials.get(Registry.SASL_PASSWORD);
  122. String uid = (String) userCredentials.get(CramMD5Registry.UID_FIELD);
  123. String gid = (String) userCredentials.get(CramMD5Registry.GID_FIELD);
  124. String gecos = (String) userCredentials.get(CramMD5Registry.GECOS_FIELD);
  125. String dir = (String) userCredentials.get(CramMD5Registry.DIR_FIELD);
  126. String shell = (String) userCredentials.get(CramMD5Registry.SHELL_FIELD);
  127. if (uid == null || gid == null || gecos == null || dir == null
  128. || shell == null)
  129. passwordFile.changePasswd(userName, password);
  130. else
  131. {
  132. String[] attributes = new String[] { uid, gid, gecos, dir, shell };
  133. passwordFile.add(userName, password, attributes);
  134. }
  135. }
  136. catch (Exception x)
  137. {
  138. if (x instanceof AuthenticationException)
  139. throw (AuthenticationException) x;
  140. throw new AuthenticationException("update()", x);
  141. }
  142. }
  143. public Map getConfiguration(String mode) throws AuthenticationException
  144. {
  145. throw new AuthenticationException("", new UnsupportedOperationException());
  146. }
  147. }