1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59:
62: public class PasswordFile
63: {
64: private static String DEFAULT_FILE;
65: static
66: {
67: DEFAULT_FILE = (String) AccessController.doPrivileged
68: (new GetPropertyAction(PlainRegistry.PASSWORD_FILE,
69: PlainRegistry.DEFAULT_PASSWORD_FILE));
70: }
71: private Hashtable entries;
72: private File passwdFile;
73: private long lastmod;
74:
75: public PasswordFile() throws IOException
76: {
77: this(DEFAULT_FILE);
78: }
79:
80: public PasswordFile(File pwFile) throws IOException
81: {
82: this(pwFile.getAbsolutePath());
83: }
84:
85: public PasswordFile(String fileName) throws IOException
86: {
87: passwdFile = new File(fileName);
88: update();
89: }
90:
91: public synchronized void add(String user, String passwd, String[] attributes)
92: throws IOException
93: {
94: checkCurrent();
95: if (entries.containsKey(user))
96: throw new UserAlreadyExistsException(user);
97: if (attributes.length != 5)
98: throw new IllegalArgumentException("Wrong number of attributes");
99:
100: String[] fields = new String[7];
101: fields[0] = user;
102: fields[1] = passwd;
103: System.arraycopy(attributes, 0, fields, 2, 5);
104: entries.put(user, fields);
105: savePasswd();
106: }
107:
108: public synchronized void changePasswd(String user, String passwd)
109: throws IOException
110: {
111: checkCurrent();
112: if (! entries.containsKey(user))
113: throw new NoSuchUserException(user);
114: String[] fields = (String[]) entries.get(user);
115: fields[1] = passwd;
116: entries.remove(user);
117: entries.put(user, fields);
118: savePasswd();
119: }
120:
121: public synchronized String[] lookup(String user) throws IOException
122: {
123: checkCurrent();
124: if (! entries.containsKey(user))
125: throw new NoSuchUserException(user);
126: return (String[]) entries.get(user);
127: }
128:
129: public synchronized boolean contains(String s) throws IOException
130: {
131: checkCurrent();
132: return entries.containsKey(s);
133: }
134:
135: private synchronized void update() throws IOException
136: {
137: lastmod = passwdFile.lastModified();
138: readPasswd(new FileInputStream(passwdFile));
139: }
140:
141: private void checkCurrent() throws IOException
142: {
143: if (passwdFile.lastModified() > lastmod)
144: update();
145: }
146:
147: private synchronized void readPasswd(InputStream in) throws IOException
148: {
149: BufferedReader din = new BufferedReader(new InputStreamReader(in));
150: String line;
151: entries = new Hashtable();
152: String[] fields = new String[7];
153: while ((line = din.readLine()) != null)
154: {
155: StringTokenizer st = new StringTokenizer(line, ":", true);
156: try
157: {
158: fields[0] = st.nextToken();
159: st.nextToken();
160: fields[1] = st.nextToken();
161: if (fields[1].equals(":"))
162: fields[1] = "";
163: else
164: st.nextToken();
165: fields[2] = st.nextToken();
166: if (fields[2].equals(":"))
167: fields[2] = "";
168: else
169: st.nextToken();
170: fields[3] = st.nextToken();
171: if (fields[3].equals(":"))
172: fields[3] = "";
173: else
174: st.nextToken();
175: fields[4] = st.nextToken();
176: if (fields[4].equals(":"))
177: fields[4] = "";
178: else
179: st.nextToken();
180: fields[5] = st.nextToken();
181: if (fields[5].equals(":"))
182: fields[5] = "";
183: else
184: st.nextToken();
185: fields[6] = st.nextToken();
186: if (fields[6].equals(":"))
187: fields[6] = "";
188: }
189: catch (NoSuchElementException ignored)
190: {
191: continue;
192: }
193: entries.put(fields[0], fields);
194: }
195: }
196:
197: private synchronized void savePasswd() throws IOException
198: {
199: if (passwdFile != null)
200: {
201: FileOutputStream fos = new FileOutputStream(passwdFile);
202: PrintWriter pw = null;
203: try
204: {
205: pw = new PrintWriter(fos);
206: String key;
207: String[] fields;
208: StringBuffer sb;
209: Enumeration keys = entries.keys();
210: while (keys.hasMoreElements())
211: {
212: key = (String) keys.nextElement();
213: fields = (String[]) entries.get(key);
214: sb = new StringBuffer(fields[0]);
215: for (int i = 1; i < fields.length; i++)
216: sb.append(":" + fields[i]);
217: pw.println(sb.toString());
218: }
219: }
220: finally
221: {
222: if (pw != null)
223: try
224: {
225: pw.flush();
226: }
227: finally
228: {
229: pw.close();
230: }
231: if (fos != null)
232: try
233: {
234: fos.close();
235: }
236: catch (IOException ignored)
237: {
238: }
239: lastmod = passwdFile.lastModified();
240: }
241: }
242: }
243: }