//
// BasicAuthorization.java
//
// Portions of this file are subject to the SNIA Public License Version 1.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
//      http://www.snia.org/resources/openSource.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is BasicAuthorization.java
//
// The Initial Developer of the Original Code is David Simons.
//

//
//
// $Id: BasicAuthorization.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: BasicAuthorization.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:17:10  karl
// import of SNIA 0_7
//
// Revision 1.3  2001/07/22 13:22:20  davidsi
// - Added access control. Thanks to Viktor and Quan Wang.
//
// Revision 1.2  2000/08/16 05:59:59  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.1.6.1  2000/08/10 00:03:36  shawn
// head merge
//
// Revision 1.1  2000/07/10 18:58:39  davidsi
// - Initial version.
//
//
//


package org.snia.wbemcmd.xml;

import java.io.*;
import java.util.Hashtable;
import org.snia.wbemcmd.utils.Log;


/*
 * Example authorization module - be sure to protect the file with passwords
 * using standard file permissions!
 */
public class BasicAuthorization implements Authorizer {

   /*
    * Identifies the file that contains passwords
    */
    private static String passwordPath="passwords";
	
    private Hashtable passwords=new Hashtable();
	
   /**
    * Called when authorizer is loaded
    * 
    */
    public void init() {
	loadPasswordFile();
    }
	

	
   /**
    * Authenticates a user
    * 
    * @param userName
    *   The name of the user
    * @param password
    *   The password for the user
    * @return
    *   True if user is authenticated
    */
    public boolean authenticate(String userName, String iPassword) {

	String password=(String)(passwords.get(userName));
	if (password==null)
	    return(false);
	return(password.equals(iPassword));
    }
	
	
   /**
    * Determines whether CIM options require authentication
    * 
    * @return
    *   True if options require authenticated access
    */
    public boolean optionsRequireLogin() {
	return(false);
    }
	
   /**
    * Determines whether anon access is allowed
    * 
    * @return
    *   True if anon access allowed
    */
    public boolean allowAnonymous() {
	return(false);
    }	

    /**
     * Determines whether access checking is implemented
     *
     * @return
     *   True if this authorizer performs access checking
     */
    public boolean accessCheckEnabled() {
	return(false);
    }

    /**
     * Executes a piece of code with access checks performed.
     * No checks performed
     */
    public void executeChecked(java.lang.Runnable r) {
	r.run();
    }

   /**
    * Loads the password file into a hash map so it only has to
    * take the hit of loading from a file once.
    */
    private void loadPasswordFile() {

	BufferedReader in;
	String userName;
	String password;
	
	try {
	    in=new BufferedReader(new FileReader(new File(passwordPath)));
	} catch (FileNotFoundException io) {
		Log.println("Failed to open password file:" + passwordPath);
		return;
	}
	for(;;) {
	    try {
		String line=in.readLine();
		if (line==null)
	 	    break;
		int index=line.indexOf(':');
		if ( index>0 && (index+1<line.length()) ) {
		    userName=line.substring(0,index);
		    password=line.substring(index+1);
		    passwords.put(userName,password);
		}	
	    } catch (Exception e) {
		break;
	    }
	}
    }
}
