Code

Added function to passwordMethod, create_template_hash()
[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 $display = FALSE;
28   var $hash= "";
30   // Konstructor
31   function passwordMethod($config)
32   {
33   }
35   function create_template_hash($attrs)
36   {
37     if($this->get_hash_name() == ""){
38       return("{crypt}N0T$3T4N0W");
39     }else{
40       return('{'.$this->get_hash_name().'}').'N0T$3T4N0W';
41     }
42   }
44   function get_hash_name()
45   {
46   }
49   // this function returns all loaded classes for password encryption
50   static function get_available_methods()
51   {
52     global $class_mapping, $config;
53     $ret =false;
54     $i =0;
56     /* Only */
57     if(!session::is_set("passwordMethod::get_available_methods")){
58       foreach($class_mapping as $class => $path) {
59         if(preg_match('/passwordMethod/i', $class) && !preg_match("/^passwordMethod$/i", $class)){
60           $name = preg_replace ("/passwordMethod/i", "", $class);
61           $test = new $class($config, "");
62           if($test->is_available()) {
63             $plugs= $test->get_hash_name();
64             if (!is_array($plugs)){
65               $plugs= array($plugs);
66             }
68             foreach ($plugs as $plugname){
70               $cfg = $test->is_configurable();
72               $ret['name'][$i]= $plugname;
73               $ret['class'][$i]=$class;
74               $ret['is_configurable'][$i]= $cfg;
75               $ret['object'][$i]= $test;
76               $ret['desc'][$i] = $test->get_description();
77               $ret[$i]['name']  = $plugname;
78               $ret[$i]['class'] = $class;
79               $ret[$i]['object']= $test;
80               $ret[$i]['is_configurable']= $cfg;
81               $ret[$i]['desc'] = $test->get_description();
82               $ret[$plugname]=$class;                    
83               $i++;
84             }
85           }
86         }
87       }
88       session::set("passwordMethod::get_available_methods",$ret);
89     }
90     return(session::get("passwordMethod::get_available_methods"));
91   }
92   
94   function get_description()
95   {
96     return("");
97   }
100   // Method to let password backends remove additional information besides
101   // the userPassword attribute
102   function remove_from_parent()
103   {
104   }
107   // Method to let passwords backends manage additional information
108   // besides the userAttribute entry
109   function set_password($password)
110   {
111     return(TRUE);
112   }
115   // Return true if this password method provides a configuration dialog
116   function is_configurable()
117   {
118     return FALSE;
119   }
122   // Provide a subdialog to configure a password method
123   function configure()
124   {
125     return "";
126   }
128   
129   // Save information to LDAP
130   function save($dn)
131   {
132   }
135   // Try to find out if it's our hash...
136   static function get_method($password_hash,$dn = "")
137   {
138     global $config;
140     $methods= passwordMethod::get_available_methods();
142     foreach ($methods['class'] as $class){
144         $test = new $class($config,$dn);
145 #        All listed methods are available. 
146 #        if(!$test->is_available())continue;
147         $method= $test->_extract_method($password_hash);
148         if ($method != ""){
149           $test->set_hash($method);
150           return $test;
151         }
152     }
154     msg_dialog::display(_("Error"), _("Cannot find a suitable password method for the current hash!"), ERROR_DIALOG);
156     return NULL;
157   }
160   function _extract_method($password_hash)
161   {
162     $hash= $this->get_hash_name();
163     if (preg_match("/^\{$hash\}/i", $password_hash)){
164       return $hash;
165     }
167     return "";
168   }
171   static function make_hash($password, $hash)
172   {
173     global $config;
175     $methods= passwordMethod::get_available_methods();
176     $tmp= new $methods[$hash]($config);
177     $tmp->set_hash($hash);
178     return $tmp->generate_hash($password);
179   }
182   function set_hash($hash)
183   {
184     $this->hash= $hash;
185   }
188   function get_hash()
189   {
190     return $this->hash;
191   }
194 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
195 ?>