/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_SECURITY_CHECK_HPP
#define NMM_SECURITY_CHECK_HPP NMM_SECURITY_CHECK_HPP

#include "nmm/nmm_prefix.hpp"

#include "NMMConfig.hpp"
#include "Singleton.hpp"
#include "NamedObject.hpp"

#include "visibility.hpp"

namespace NMM {
  /** Security check.
   * This class is a simple security manager which is used to protect a server registry with a password
   * and restrict access to directories. NMM plug-ins that access files should use this security manager
   * to check if they have permission to access specific files.
   */
  class DLLEXPORT SecurityCheck : public NamedObject {
    DECLARE_SINGLETON(SecurityCheck, CreateUsingNew, DefaultLifetime, MultiThreaded);
	friend class CreateUsingNew<SecurityCheck>;
  public:
    /** Check file name for read permission.
     * This method checks if an NMM plug-in is allowed to read from the given file.
     *
     * @param dir_plus_filename File name to check
     * @return @c true if the file may be accessed, @c false otherwise
     */
    bool checkFilenameForReading(const string& dir_plus_filename);

    /** Check file name for write permission.
     * This method checks if an NMM plug-in is allowed to write to the given file.
     *
     * @param dir_plus_filename File name to check
     * @return @c true if the file may be accessed, @c false otherwise
     */
    bool checkFilenameForWriting(const string& dir_plus_filename);

    /** Get the passwort for the server registry.
     *
     * @return The Password
     */
    const string getPasswd();

    /** Check if the given server registry password is correct.
     *
     * @param passwd Password to check
     * @return @c true if the password matches, @c false otherwise
     */
    bool checkPasswd(const string& passwd);

    /** Read configuration from a file.
     *
     * @param filename File name of configuration file
     */
    void parseFile(const string& filename);

  private:
    /** Singleton constructor.
     */
    SecurityCheck();

    /** Disabled copy constructor.
     */
    SecurityCheck(const SecurityCheck& other) : NamedObject(other) {}

    /** Disabled assignment operator.
     */
    const SecurityCheck& operator=(const SecurityCheck&) { return *this; }

    /** Singleton destructor.
     */
    ~SecurityCheck() {}

    /** Check if file name is in a subdirectory of the given directory set.
     *
     * @param dir_plus_filename File name to check
     * @param directories List of directories
     */
    bool isFilenameWithinDirectories(const string& dir_plus_filename, const string& directories);

  private:
    NMMConfig nmmconfig;      ///< Configuration
  };

  /** Singleton interface for SecurityCheck.
   */
  typedef SecurityCheck::Singleton SSecurityCheck; 
}

#include "nmm/nmm_suffix.hpp"

#endif
