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