PasswordFile.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* PasswordFile.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.lang.CPStringBuilder;
  33. import gnu.javax.crypto.sasl.NoSuchUserException;
  34. import gnu.javax.crypto.sasl.UserAlreadyExistsException;
  35. import java.io.BufferedReader;
  36. import java.io.File;
  37. import java.io.FileInputStream;
  38. import java.io.FileOutputStream;
  39. import java.io.InputStream;
  40. import java.io.InputStreamReader;
  41. import java.io.IOException;
  42. import java.io.PrintWriter;
  43. import java.util.HashMap;
  44. import java.util.Iterator;
  45. import java.util.NoSuchElementException;
  46. import java.util.StringTokenizer;
  47. /**
  48. * The CRAM-MD5 password file representation.
  49. */
  50. public class PasswordFile
  51. {
  52. private static String DEFAULT_FILE;
  53. static
  54. {
  55. DEFAULT_FILE = System.getProperty(CramMD5Registry.PASSWORD_FILE,
  56. CramMD5Registry.DEFAULT_PASSWORD_FILE);
  57. }
  58. private HashMap entries;
  59. private File passwdFile;
  60. private long lastmod;
  61. public PasswordFile() throws IOException
  62. {
  63. this(DEFAULT_FILE);
  64. }
  65. public PasswordFile(final File pwFile) throws IOException
  66. {
  67. this(pwFile.getAbsolutePath());
  68. }
  69. public PasswordFile(final String fileName) throws IOException
  70. {
  71. passwdFile = new File(fileName);
  72. update();
  73. }
  74. public synchronized void add(final String user, final String passwd,
  75. final String[] attributes) throws IOException
  76. {
  77. checkCurrent(); // check if the entry exists
  78. if (entries.containsKey(user))
  79. throw new UserAlreadyExistsException(user);
  80. if (attributes.length != 5)
  81. throw new IllegalArgumentException("Wrong number of attributes");
  82. final String[] fields = new String[7]; // create the new entry
  83. fields[0] = user;
  84. fields[1] = passwd;
  85. System.arraycopy(attributes, 0, fields, 2, 5);
  86. entries.put(user, fields);
  87. savePasswd();
  88. }
  89. public synchronized void changePasswd(final String user, final String passwd)
  90. throws IOException
  91. {
  92. checkCurrent();
  93. if (! entries.containsKey(user))
  94. throw new NoSuchUserException(user);
  95. final String[] fields = (String[]) entries.get(user); // get existing entry
  96. fields[1] = passwd; // modify the password field
  97. entries.remove(user); // delete the existing entry
  98. entries.put(user, fields); // add the new entry
  99. savePasswd();
  100. }
  101. public synchronized String[] lookup(final String user) throws IOException
  102. {
  103. checkCurrent();
  104. if (! entries.containsKey(user))
  105. throw new NoSuchUserException(user);
  106. return (String[]) entries.get(user);
  107. }
  108. public synchronized boolean contains(final String s) throws IOException
  109. {
  110. checkCurrent();
  111. return entries.containsKey(s);
  112. }
  113. private synchronized void update() throws IOException
  114. {
  115. lastmod = passwdFile.lastModified();
  116. readPasswd(new FileInputStream(passwdFile));
  117. }
  118. private void checkCurrent() throws IOException
  119. {
  120. if (passwdFile.lastModified() > lastmod)
  121. update();
  122. }
  123. private synchronized void readPasswd(final InputStream in) throws IOException
  124. {
  125. final BufferedReader din = new BufferedReader(new InputStreamReader(in));
  126. String line;
  127. entries = new HashMap();
  128. while ((line = din.readLine()) != null)
  129. {
  130. final String[] fields = new String[7];
  131. final StringTokenizer st = new StringTokenizer(line, ":", true);
  132. try
  133. {
  134. fields[0] = st.nextToken(); // username
  135. st.nextToken();
  136. fields[1] = st.nextToken(); // passwd
  137. if (fields[1].equals(":"))
  138. fields[1] = "";
  139. else
  140. st.nextToken();
  141. fields[2] = st.nextToken(); // uid
  142. if (fields[2].equals(":"))
  143. fields[2] = "";
  144. else
  145. st.nextToken();
  146. fields[3] = st.nextToken(); // gid
  147. if (fields[3].equals(":"))
  148. fields[3] = "";
  149. else
  150. st.nextToken();
  151. fields[4] = st.nextToken(); // gecos
  152. if (fields[4].equals(":"))
  153. fields[4] = "";
  154. else
  155. st.nextToken();
  156. fields[5] = st.nextToken(); // dir
  157. if (fields[5].equals(":"))
  158. fields[5] = "";
  159. else
  160. st.nextToken();
  161. fields[6] = st.nextToken(); // shell
  162. if (fields[6].equals(":"))
  163. fields[6] = "";
  164. }
  165. catch (NoSuchElementException x)
  166. {
  167. continue;
  168. }
  169. entries.put(fields[0], fields);
  170. }
  171. }
  172. private synchronized void savePasswd() throws IOException
  173. {
  174. if (passwdFile != null)
  175. {
  176. final FileOutputStream fos = new FileOutputStream(passwdFile);
  177. PrintWriter pw = null;
  178. try
  179. {
  180. pw = new PrintWriter(fos);
  181. String key;
  182. String[] fields;
  183. CPStringBuilder sb;
  184. int i;
  185. for (Iterator it = entries.keySet().iterator(); it.hasNext();)
  186. {
  187. key = (String) it.next();
  188. fields = (String[]) entries.get(key);
  189. sb = new CPStringBuilder(fields[0]);
  190. for (i = 1; i < fields.length; i++)
  191. sb.append(":").append(fields[i]);
  192. pw.println(sb.toString());
  193. }
  194. }
  195. finally
  196. {
  197. if (pw != null)
  198. try
  199. {
  200. pw.flush();
  201. }
  202. finally
  203. {
  204. pw.close();
  205. }
  206. try
  207. {
  208. fos.close();
  209. }
  210. catch (IOException ignored)
  211. {
  212. }
  213. lastmod = passwdFile.lastModified();
  214. }
  215. }
  216. }
  217. }