Code

Updated password methods to support sub-hash-methods
[gosa.git] / gosa-core / include / password-methods / class_password-methods.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class passwordMethod
24 {
25   var $config = false;
26   var $attrs= array();
27   var $hash= "";
29   // Konstructor
30   function passwordMethod($config)
31   {
32   }
35   function get_hash_name()
36   {
37   }
40   // Loads Methods in annother way as  get_available_methods do, (For setup ..)
41   // and loads them,.
42   #FIXME: This stopped working after moving around pw-methods
43   function get_available_methods_if_not_loaded($path_to_load="../include")
44   {
45     $oh = opendir($path_to_load);
46     $i = 0; 
47     $ret = false;
48     while ($file = readdir($oh)) {
49       $one = strtolower($file); 
50       if((strstr($one,"class_password-methods-" ))&&($one[0]!=".")){
51         require_once($file);
52       }
53     }
54     return(passwordMethod::get_available_methods());
55   }
58   // this function returns all loaded classes for password encryption
59   static function get_available_methods()
60   {
61     global $class_mapping, $config;
62     $ret =false;
63     $i =0;
64     foreach($class_mapping as $class => $path) {
65       if(preg_match('/passwordMethod/i', $class) && !preg_match("/^passwordMethod$/i", $class)){
66         $name = preg_replace ("/passwordMethod/i", "", $class);
67         $test = new $class($config, "");
68         if($test->is_available()) {
69           $plugs= $test->get_hash_name();
70           if (!is_array($plugs)){
71             $plugs= array($plugs);
72           }
74           foreach ($plugs as $plugname){
75             $ret['name'][$i]= $plugname;
76             $ret['class'][$i]=$class;
77             $ret[$i]['name']= $plugname;
78             $ret[$i]['class']= $class;
79             $ret[$plugname]=$class;                    
80             $i++;
81           }
82         }
83       }
84     }
86     return($ret);
87   }
88   
90   // Method to let password backends remove additional information besides
91   // the userPassword attribute
92   function remove_from_parent()
93   {
94   }
97   // Method to let passwords backends manage additional information
98   // besides the userAttribute entry
99   function set_password($password)
100   {
101   }
104   // Return true if this password method provides a configuration dialog
105   function is_configurable()
106   {
107     return FALSE;
108   }
111   // Provide a subdialog to configure a password method
112   function configure()
113   {
114     return "";
115   }
117   
118   // Save information to LDAP
119   function save($dn)
120   {
121   }
124   // Try to find out if it's our hash...
125   static function get_method($password_hash)
126   {
127     global $config;
129     $methods= passwordMethod::get_available_methods();
131     foreach ($methods['class'] as $class){
133         $test = new $class($config);
134         $method= $test->_extract_method($password_hash);
135         if ($method != ""){
136           $test->set_hash($method);
137           return $test;
138         }
139     }
141     msg_dialog::display(_("Error"), _("Cannot find a suitable password method for the current hash!"), ERROR_DIALOG);
143     return NULL;
144   }
147   function _extract_method($password_hash)
148   {
149     $hash= $this->get_hash_name();
150     if (preg_match("/^\{$hash\}/i", $password_hash)){
151       return $hash;
152     }
154     return "";
155   }
158   static function make_hash($password, $hash)
159   {
160     global $config;
162     $methods= passwordMethod::get_available_methods();
163     $tmp= new $methods[$hash]($config);
164     $tmp->set_hash($hash);
165     return $tmp->generate_hash($password);
166   }
169   function set_hash($hash)
170   {
171     $this->hash= $hash;
172   }
175   function get_hash()
176   {
177     return $this->hash;
178   }
181 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
182 ?>