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