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
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
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
;
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
;
60 * The CRAM-MD5 password file representation.
62 public class PasswordFile
64 private static String DEFAULT_FILE
;
67 DEFAULT_FILE
= System
.getProperty(CramMD5Registry
.PASSWORD_FILE
,
68 CramMD5Registry
.DEFAULT_PASSWORD_FILE
);
70 private HashMap entries
;
71 private File passwdFile
;
74 public PasswordFile() throws IOException
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
);
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
101 System
.arraycopy(attributes
, 0, fields
, 2, 5);
102 entries
.put(user
, fields
);
106 public synchronized void changePasswd(final String user
, final String passwd
)
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
119 public synchronized String
[] lookup(final String user
) throws IOException
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
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
)
145 private synchronized void readPasswd(final InputStream in
) throws IOException
147 final BufferedReader din
= new BufferedReader(new InputStreamReader(in
));
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
158 fields
[1] = st
.nextToken(); // passwd
159 if (fields
[1].equals(":"))
163 fields
[2] = st
.nextToken(); // uid
164 if (fields
[2].equals(":"))
168 fields
[3] = st
.nextToken(); // gid
169 if (fields
[3].equals(":"))
173 fields
[4] = st
.nextToken(); // gecos
174 if (fields
[4].equals(":"))
178 fields
[5] = st
.nextToken(); // dir
179 if (fields
[5].equals(":"))
183 fields
[6] = st
.nextToken(); // shell
184 if (fields
[6].equals(":"))
187 catch (NoSuchElementException x
)
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
);
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());
233 catch (IOException ignored
)
236 lastmod
= passwdFile
.lastModified();