Source for gnu.javax.crypto.sasl.crammd5.PasswordFile

   1: /* PasswordFile.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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
  20: 
  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.
  25: 
  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.  */
  37: 
  38: 
  39: package gnu.javax.crypto.sasl.crammd5;
  40: 
  41: import gnu.java.lang.CPStringBuilder;
  42: 
  43: import gnu.javax.crypto.sasl.NoSuchUserException;
  44: import gnu.javax.crypto.sasl.UserAlreadyExistsException;
  45: 
  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;
  58: 
  59: /**
  60:  * The CRAM-MD5 password file representation.
  61:  */
  62: public class PasswordFile
  63: {
  64:   private static String DEFAULT_FILE;
  65:   static
  66:     {
  67:       DEFAULT_FILE = System.getProperty(CramMD5Registry.PASSWORD_FILE,
  68:                                         CramMD5Registry.DEFAULT_PASSWORD_FILE);
  69:     }
  70:   private HashMap entries;
  71:   private File passwdFile;
  72:   private long lastmod;
  73: 
  74:   public PasswordFile() throws IOException
  75:   {
  76:     this(DEFAULT_FILE);
  77:   }
  78: 
  79:   public PasswordFile(final File pwFile) throws IOException
  80:   {
  81:     this(pwFile.getAbsolutePath());
  82:   }
  83: 
  84:   public PasswordFile(final String fileName) throws IOException
  85:   {
  86:     passwdFile = new File(fileName);
  87:     update();
  88:   }
  89: 
  90:   public synchronized void add(final String user, final String passwd,
  91:                                final String[] attributes) throws IOException
  92:   {
  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();
 104:   }
 105: 
 106:   public synchronized void changePasswd(final String user, final String passwd)
 107:       throws IOException
 108:   {
 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();
 117:   }
 118: 
 119:   public synchronized String[] lookup(final String user) throws IOException
 120:   {
 121:     checkCurrent();
 122:     if (! entries.containsKey(user))
 123:       throw new NoSuchUserException(user);
 124:     return (String[]) entries.get(user);
 125:   }
 126: 
 127:   public synchronized boolean contains(final String s) throws IOException
 128:   {
 129:     checkCurrent();
 130:     return entries.containsKey(s);
 131:   }
 132: 
 133:   private synchronized void update() throws IOException
 134:   {
 135:     lastmod = passwdFile.lastModified();
 136:     readPasswd(new FileInputStream(passwdFile));
 137:   }
 138: 
 139:   private void checkCurrent() throws IOException
 140:   {
 141:     if (passwdFile.lastModified() > lastmod)
 142:       update();
 143:   }
 144: 
 145:   private synchronized void readPasswd(final InputStream in) throws IOException
 146:   {
 147:     final BufferedReader din = new BufferedReader(new InputStreamReader(in));
 148:     String line;
 149:     entries = new HashMap();
 150:     while ((line = din.readLine()) != null)
 151:       {
 152:         final String[] fields = new String[7];
 153:         final StringTokenizer st = new StringTokenizer(line, ":", true);
 154:         try
 155:           {
 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] = "";
 186:           }
 187:         catch (NoSuchElementException x)
 188:           {
 189:             continue;
 190:           }
 191:         entries.put(fields[0], fields);
 192:       }
 193:   }
 194: 
 195:   private synchronized void savePasswd() throws IOException
 196:   {
 197:     if (passwdFile != null)
 198:       {
 199:         final FileOutputStream fos = new FileOutputStream(passwdFile);
 200:         PrintWriter pw = null;
 201:         try
 202:           {
 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();)
 209:               {
 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());
 216:               }
 217:           }
 218:         finally
 219:           {
 220:             if (pw != null)
 221:               try
 222:                 {
 223:                   pw.flush();
 224:                 }
 225:               finally
 226:                 {
 227:                   pw.close();
 228:                 }
 229:             try
 230:               {
 231:                 fos.close();
 232:               }
 233:             catch (IOException ignored)
 234:               {
 235:               }
 236:             lastmod = passwdFile.lastModified();
 237:           }
 238:       }
 239:   }
 240: }