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
.javax
.crypto
.sasl
.NoSuchUserException
;
42 import gnu
.javax
.crypto
.sasl
.UserAlreadyExistsException
;
44 import java
.io
.BufferedReader
;
46 import java
.io
.FileInputStream
;
47 import java
.io
.FileOutputStream
;
48 import java
.io
.InputStream
;
49 import java
.io
.InputStreamReader
;
50 import java
.io
.IOException
;
51 import java
.io
.PrintWriter
;
52 import java
.util
.HashMap
;
53 import java
.util
.Iterator
;
54 import java
.util
.NoSuchElementException
;
55 import java
.util
.StringTokenizer
;
58 * The CRAM-MD5 password file representation.
60 public class PasswordFile
63 // Constants and variables
64 // -------------------------------------------------------------------------
66 private static String DEFAULT_FILE
;
69 DEFAULT_FILE
= System
.getProperty(CramMD5Registry
.PASSWORD_FILE
,
70 CramMD5Registry
.DEFAULT_PASSWORD_FILE
);
73 private HashMap entries
;
75 private File passwdFile
;
80 // -------------------------------------------------------------------------
82 public PasswordFile() throws IOException
87 public PasswordFile(final File pwFile
) throws IOException
89 this(pwFile
.getAbsolutePath());
92 public PasswordFile(final String fileName
) throws IOException
94 passwdFile
= new File(fileName
);
99 // -------------------------------------------------------------------------
102 // -------------------------------------------------------------------------
104 public synchronized void add(final String user
, final String passwd
,
105 final String
[] attributes
) throws IOException
107 checkCurrent(); // check if the entry exists
108 if (entries
.containsKey(user
))
110 throw new UserAlreadyExistsException(user
);
112 if (attributes
.length
!= 5)
114 throw new IllegalArgumentException("Wrong number of attributes");
117 final String
[] fields
= new String
[7]; // create the new entry
120 System
.arraycopy(attributes
, 0, fields
, 2, 5);
121 entries
.put(user
, fields
);
125 public synchronized void changePasswd(final String user
, final String passwd
)
129 if (!entries
.containsKey(user
))
130 { // check if the entry exists
131 throw new NoSuchUserException(user
);
134 final String
[] fields
= (String
[]) entries
.get(user
); // get the existing entry
135 fields
[1] = passwd
; // modify the password field
136 entries
.remove(user
); // delete the existing entry
137 entries
.put(user
, fields
); // add the new entry
142 public synchronized String
[] lookup(final String user
) throws IOException
145 if (!entries
.containsKey(user
))
147 throw new NoSuchUserException(user
);
149 return (String
[]) entries
.get(user
);
152 public synchronized boolean contains(final String s
) throws IOException
156 return entries
.containsKey(s
);
159 private synchronized void update() throws IOException
161 lastmod
= passwdFile
.lastModified();
162 readPasswd(new FileInputStream(passwdFile
));
165 private void checkCurrent() throws IOException
167 if (passwdFile
.lastModified() > lastmod
)
173 private synchronized void readPasswd(final InputStream in
) throws IOException
175 final BufferedReader din
= new BufferedReader(new InputStreamReader(in
));
177 entries
= new HashMap();
178 while ((line
= din
.readLine()) != null)
180 final String
[] fields
= new String
[7];
181 final StringTokenizer st
= new StringTokenizer(line
, ":", true);
184 fields
[0] = st
.nextToken(); // username
187 fields
[1] = st
.nextToken(); // passwd
188 if (fields
[1].equals(":"))
197 fields
[2] = st
.nextToken(); // uid
198 if (fields
[2].equals(":"))
207 fields
[3] = st
.nextToken(); // gid
208 if (fields
[3].equals(":"))
217 fields
[4] = st
.nextToken(); // gecos
218 if (fields
[4].equals(":"))
227 fields
[5] = st
.nextToken(); // dir
228 if (fields
[5].equals(":"))
237 fields
[6] = st
.nextToken(); // shell
238 if (fields
[6].equals(":"))
243 catch (NoSuchElementException x
)
248 entries
.put(fields
[0], fields
);
252 private synchronized void savePasswd() throws IOException
254 if (passwdFile
!= null)
256 final FileOutputStream fos
= new FileOutputStream(passwdFile
);
257 PrintWriter pw
= null;
260 pw
= new PrintWriter(fos
);
265 for (Iterator it
= entries
.keySet().iterator(); it
.hasNext();)
267 key
= (String
) it
.next();
268 fields
= (String
[]) entries
.get(key
);
269 sb
= new StringBuffer(fields
[0]);
270 for (i
= 1; i
< fields
.length
; i
++)
272 sb
.append(":").append(fields
[i
]);
274 pw
.println(sb
.toString());
294 catch (IOException ignored
)
297 lastmod
= passwdFile
.lastModified();