Create embedded-5_0-branch branch for development on ARM embedded cores.
[official-gcc.git] / embedded-5_0-branch / libjava / classpath / gnu / javax / crypto / sasl / crammd5 / PasswordFile.java
blob65da4afddb6d711281e981238e43e00b6bd945ce
1 /* PasswordFile.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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.
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. */
39 package gnu.javax.crypto.sasl.crammd5;
41 import gnu.java.lang.CPStringBuilder;
43 import gnu.javax.crypto.sasl.NoSuchUserException;
44 import gnu.javax.crypto.sasl.UserAlreadyExistsException;
46 import java.io.BufferedReader;
47 import java.io.File;
48 import java.io.FileInputStream;
49 import java.io.FileOutputStream;
50 import java.io.InputStream;
51 import java.io.InputStreamReader;
52 import java.io.IOException;
53 import java.io.PrintWriter;
54 import java.util.HashMap;
55 import java.util.Iterator;
56 import java.util.NoSuchElementException;
57 import java.util.StringTokenizer;
59 /**
60 * The CRAM-MD5 password file representation.
62 public class PasswordFile
64 private static String DEFAULT_FILE;
65 static
67 DEFAULT_FILE = System.getProperty(CramMD5Registry.PASSWORD_FILE,
68 CramMD5Registry.DEFAULT_PASSWORD_FILE);
70 private HashMap entries;
71 private File passwdFile;
72 private long lastmod;
74 public PasswordFile() throws IOException
76 this(DEFAULT_FILE);
79 public PasswordFile(final File pwFile) throws IOException
81 this(pwFile.getAbsolutePath());
84 public PasswordFile(final String fileName) throws IOException
86 passwdFile = new File(fileName);
87 update();
90 public synchronized void add(final String user, final String passwd,
91 final String[] attributes) throws IOException
93 checkCurrent(); // check if the entry exists
94 if (entries.containsKey(user))
95 throw new UserAlreadyExistsException(user);
96 if (attributes.length != 5)
97 throw new IllegalArgumentException("Wrong number of attributes");
98 final String[] fields = new String[7]; // create the new entry
99 fields[0] = user;
100 fields[1] = passwd;
101 System.arraycopy(attributes, 0, fields, 2, 5);
102 entries.put(user, fields);
103 savePasswd();
106 public synchronized void changePasswd(final String user, final String passwd)
107 throws IOException
109 checkCurrent();
110 if (! entries.containsKey(user))
111 throw new NoSuchUserException(user);
112 final String[] fields = (String[]) entries.get(user); // get existing entry
113 fields[1] = passwd; // modify the password field
114 entries.remove(user); // delete the existing entry
115 entries.put(user, fields); // add the new entry
116 savePasswd();
119 public synchronized String[] lookup(final String user) throws IOException
121 checkCurrent();
122 if (! entries.containsKey(user))
123 throw new NoSuchUserException(user);
124 return (String[]) entries.get(user);
127 public synchronized boolean contains(final String s) throws IOException
129 checkCurrent();
130 return entries.containsKey(s);
133 private synchronized void update() throws IOException
135 lastmod = passwdFile.lastModified();
136 readPasswd(new FileInputStream(passwdFile));
139 private void checkCurrent() throws IOException
141 if (passwdFile.lastModified() > lastmod)
142 update();
145 private synchronized void readPasswd(final InputStream in) throws IOException
147 final BufferedReader din = new BufferedReader(new InputStreamReader(in));
148 String line;
149 entries = new HashMap();
150 while ((line = din.readLine()) != null)
152 final String[] fields = new String[7];
153 final StringTokenizer st = new StringTokenizer(line, ":", true);
156 fields[0] = st.nextToken(); // username
157 st.nextToken();
158 fields[1] = st.nextToken(); // passwd
159 if (fields[1].equals(":"))
160 fields[1] = "";
161 else
162 st.nextToken();
163 fields[2] = st.nextToken(); // uid
164 if (fields[2].equals(":"))
165 fields[2] = "";
166 else
167 st.nextToken();
168 fields[3] = st.nextToken(); // gid
169 if (fields[3].equals(":"))
170 fields[3] = "";
171 else
172 st.nextToken();
173 fields[4] = st.nextToken(); // gecos
174 if (fields[4].equals(":"))
175 fields[4] = "";
176 else
177 st.nextToken();
178 fields[5] = st.nextToken(); // dir
179 if (fields[5].equals(":"))
180 fields[5] = "";
181 else
182 st.nextToken();
183 fields[6] = st.nextToken(); // shell
184 if (fields[6].equals(":"))
185 fields[6] = "";
187 catch (NoSuchElementException x)
189 continue;
191 entries.put(fields[0], fields);
195 private synchronized void savePasswd() throws IOException
197 if (passwdFile != null)
199 final FileOutputStream fos = new FileOutputStream(passwdFile);
200 PrintWriter pw = null;
203 pw = new PrintWriter(fos);
204 String key;
205 String[] fields;
206 CPStringBuilder sb;
207 int i;
208 for (Iterator it = entries.keySet().iterator(); it.hasNext();)
210 key = (String) it.next();
211 fields = (String[]) entries.get(key);
212 sb = new CPStringBuilder(fields[0]);
213 for (i = 1; i < fields.length; i++)
214 sb.append(":").append(fields[i]);
215 pw.println(sb.toString());
218 finally
220 if (pw != null)
223 pw.flush();
225 finally
227 pw.close();
231 fos.close();
233 catch (IOException ignored)
236 lastmod = passwdFile.lastModified();