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