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.javax.crypto.sasl.NoSuchUserException;
  42: import gnu.javax.crypto.sasl.UserAlreadyExistsException;
  43: 
  44: import java.io.BufferedReader;
  45: import java.io.File;
  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;
  56: 
  57: /**
  58:  * The CRAM-MD5 password file representation.
  59:  */
  60: public class PasswordFile
  61: {
  62:   private static String DEFAULT_FILE;
  63:   static
  64:     {
  65:       DEFAULT_FILE = System.getProperty(CramMD5Registry.PASSWORD_FILE,
  66:                                         CramMD5Registry.DEFAULT_PASSWORD_FILE);
  67:     }
  68:   private HashMap entries;
  69:   private File passwdFile;
  70:   private long lastmod;
  71: 
  72:   public PasswordFile() throws IOException
  73:   {
  74:     this(DEFAULT_FILE);
  75:   }
  76: 
  77:   public PasswordFile(final File pwFile) throws IOException
  78:   {
  79:     this(pwFile.getAbsolutePath());
  80:   }
  81: 
  82:   public PasswordFile(final String fileName) throws IOException
  83:   {
  84:     passwdFile = new File(fileName);
  85:     update();
  86:   }
  87: 
  88:   public synchronized void add(final String user, final String passwd,
  89:                                final String[] attributes) throws IOException
  90:   {
  91:     checkCurrent(); // check if the entry exists
  92:     if (entries.containsKey(user))
  93:       throw new UserAlreadyExistsException(user);
  94:     if (attributes.length != 5)
  95:       throw new IllegalArgumentException("Wrong number of attributes");
  96:     final String[] fields = new String[7]; // create the new entry
  97:     fields[0] = user;
  98:     fields[1] = passwd;
  99:     System.arraycopy(attributes, 0, fields, 2, 5);
 100:     entries.put(user, fields);
 101:     savePasswd();
 102:   }
 103: 
 104:   public synchronized void changePasswd(final String user, final String passwd)
 105:       throws IOException
 106:   {
 107:     checkCurrent();
 108:     if (! entries.containsKey(user))
 109:       throw new NoSuchUserException(user);
 110:     final String[] fields = (String[]) entries.get(user); // get existing entry
 111:     fields[1] = passwd; // modify the password field
 112:     entries.remove(user); // delete the existing entry
 113:     entries.put(user, fields); // add the new entry
 114:     savePasswd();
 115:   }
 116: 
 117:   public synchronized String[] lookup(final String user) throws IOException
 118:   {
 119:     checkCurrent();
 120:     if (! entries.containsKey(user))
 121:       throw new NoSuchUserException(user);
 122:     return (String[]) entries.get(user);
 123:   }
 124: 
 125:   public synchronized boolean contains(final String s) throws IOException
 126:   {
 127:     checkCurrent();
 128:     return entries.containsKey(s);
 129:   }
 130: 
 131:   private synchronized void update() throws IOException
 132:   {
 133:     lastmod = passwdFile.lastModified();
 134:     readPasswd(new FileInputStream(passwdFile));
 135:   }
 136: 
 137:   private void checkCurrent() throws IOException
 138:   {
 139:     if (passwdFile.lastModified() > lastmod)
 140:       update();
 141:   }
 142: 
 143:   private synchronized void readPasswd(final InputStream in) throws IOException
 144:   {
 145:     final BufferedReader din = new BufferedReader(new InputStreamReader(in));
 146:     String line;
 147:     entries = new HashMap();
 148:     while ((line = din.readLine()) != null)
 149:       {
 150:         final String[] fields = new String[7];
 151:         final StringTokenizer st = new StringTokenizer(line, ":", true);
 152:         try
 153:           {
 154:             fields[0] = st.nextToken(); // username
 155:             st.nextToken();
 156:             fields[1] = st.nextToken(); // passwd
 157:             if (fields[1].equals(":"))
 158:               fields[1] = "";
 159:             else
 160:               st.nextToken();
 161:             fields[2] = st.nextToken(); // uid
 162:             if (fields[2].equals(":"))
 163:               fields[2] = "";
 164:             else
 165:               st.nextToken();
 166:             fields[3] = st.nextToken(); // gid
 167:             if (fields[3].equals(":"))
 168:               fields[3] = "";
 169:             else
 170:               st.nextToken();
 171:             fields[4] = st.nextToken(); // gecos
 172:             if (fields[4].equals(":"))
 173:               fields[4] = "";
 174:             else
 175:               st.nextToken();
 176:             fields[5] = st.nextToken(); // dir
 177:             if (fields[5].equals(":"))
 178:               fields[5] = "";
 179:             else
 180:               st.nextToken();
 181:             fields[6] = st.nextToken(); // shell
 182:             if (fields[6].equals(":"))
 183:               fields[6] = "";
 184:           }
 185:         catch (NoSuchElementException x)
 186:           {
 187:             continue;
 188:           }
 189:         entries.put(fields[0], fields);
 190:       }
 191:   }
 192: 
 193:   private synchronized void savePasswd() throws IOException
 194:   {
 195:     if (passwdFile != null)
 196:       {
 197:         final FileOutputStream fos = new FileOutputStream(passwdFile);
 198:         PrintWriter pw = null;
 199:         try
 200:           {
 201:             pw = new PrintWriter(fos);
 202:             String key;
 203:             String[] fields;
 204:             StringBuffer sb;
 205:             int i;
 206:             for (Iterator it = entries.keySet().iterator(); it.hasNext();)
 207:               {
 208:                 key = (String) it.next();
 209:                 fields = (String[]) entries.get(key);
 210:                 sb = new StringBuffer(fields[0]);
 211:                 for (i = 1; i < fields.length; i++)
 212:                   sb.append(":").append(fields[i]);
 213:                 pw.println(sb.toString());
 214:               }
 215:           }
 216:         finally
 217:           {
 218:             if (pw != null)
 219:               try
 220:                 {
 221:                   pw.flush();
 222:                 }
 223:               finally
 224:                 {
 225:                   pw.close();
 226:                 }
 227:             try
 228:               {
 229:                 fos.close();
 230:               }
 231:             catch (IOException ignored)
 232:               {
 233:               }
 234:             lastmod = passwdFile.lastModified();
 235:           }
 236:       }
 237:   }
 238: }