Code

Added external resolution hook to environment
[gosa.git] / include / class_password-methods.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2004  Cajus Pollmeier
6    This program 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
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 class passwordMethod
23 {
24   var $config = false;
26   // Konstructor
27   function passwordMethod($config)
28   {
29   }
31   // Loads Methods in annother way as  get_available_methods do, (For setup ..)
32   // and loads them,.
33   function get_available_methods_if_not_loaded($path_to_load="../include")
34   {
35     $oh = opendir($path_to_load);
36     $i = 0; 
37     $ret = false;
38     while ($file = readdir($oh)) {
39       $one = strtolower($file); 
40       if((strstr($one,"class_password-methods-" ))&&($one[0]!=".")){
41         require_once($file);
42       }
43     }
44     return(passwordMethod::get_available_methods());
45   }
50   // Crypts a single string, with given Method
51   function crypt_single_str($string,$method)
52   {
53     $available = passwordMethod::get_available_methods();
54     if(!is_array($available))
55     {
56       $available = passwordMethod::get_available_methods_if_not_loaded();
57     }
59     $test = new  $available[$method](false);
60     $newpass =  $test->generate_hash($string);
61     return( $newpass); 
62   }
65   // this function returns all loaded classes for password encryption
66   function get_available_methods()
67   {
68     $ret =false;
69     $all = get_declared_classes();
70     $i = 0;
71     foreach($all as $one) {
72       if(preg_match('/passwordMethod/i', $one) && !preg_match("/^passwordMethod$/i", $one)){
73         $name = preg_replace ("/passwordMethod/i", "", $one);
74         $test = new $one(false);
75         if($test->is_available()) {
76           $plugname= strtolower(preg_replace ("/passwordMethod/i","",$one));
77           $ret['name'][$i]= $plugname;
78           $ret['class'][$i]=$one;
79           $ret[$i]['name']= $plugname;
80           $ret[$i]['class']= $one;
81           $ret[$plugname]=$one;                    
82           $i++;
83         }
84       }
85     }
86     return($ret);
87   }
89 }
91 // change_password, changes the Password, of the given dn
92 function change_password ($dn, $password, $mode=0, $hash= "")
93 {
94   global $config;
95   $newpass= "";
97   // Get all available encryption Methods 
99   // NON STATIC CALL :)
100   $tmp = new passwordMethod($_SESSION['config']);
101   $available = $tmp->get_available_methods();
103   // read current password entry for $dn, to detect the encryption Method
104   $ldap       = $config->get_ldap_link();
105   $ldap->cat ($dn, array("shadowLastChange", "userPassword"));
106   $attrs      = $ldap->fetch ();
108   // Set encryption type to clear if required 
109   if (isset($attrs['userPassword'][0]) && preg_match('/^[^{}]+$/', $attrs['userPassword'][0]) && $hash == ""){
110     $hash= "clear";
111   }
113   // Detect the encryption Method
114   if ( (isset($attrs['userPassword'][0]) &&  preg_match ("/^{([^}]+)}(.+)/", $attrs['userPassword'][0], $matches)) ||  $hash != ""){
116     /* Check for supported algorithm */
117     mt_srand((double) microtime()*1000000);
119     /* Extract used hash */
120     if ($hash == ""){
121       $hash= strtolower($matches[1]);
122     }
125     // Crypt with the detected Method
126     $test = new  $available[$hash]($config);
127     $newpass =  $test->generate_hash($password);
129   } else {
130     // Crypt it by default 
131     $test = new  $available['md5']($config);
132     $newpass =  $test->generate_hash($password);
133   }
135   // Update shadow timestamp? 
136   if (isset($attrs["shadowLastChange"][0])){
137     $shadow= (int)(date("U") / 86400);
138   } else {
139     $shadow= 0;
140   }
142   // Write back modified entry 
143   $ldap->cd($dn);
144   $attrs= array();
146   // Not for groups 
147   if ($mode == 0){
149     if ($shadow != 0){
150       $attrs['shadowLastChange']= $shadow;
151     }
153     // Create SMB Password 
154     $attrs= generate_smb_nt_hash($password);
155   }
157   $attrs['userPassword']= array();
158   $attrs['userPassword']= $newpass;
161   $ldap->modify($attrs);
164   if ($ldap->error != 'Success') {
165     print_red(sprintf(_("Setting the password failed. LDAP server says '%s'."),
166           $ldap->get_error()));
167   } else {
169     /* Find postmodify entries for this class */
170     $command= search_config($config->data['MENU'], "password", "POSTMODIFY");
172     if ($command != ""){
173       /* Walk through attribute list */
174       $command= preg_replace("/%userPassword/", $password, $command);
175       $command= preg_replace("/%dn/", $dn, $command);
177       if (check_command($command)){
178         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, "Execute");
179         exec($command);
180       } else {
181         $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, "password");
182         print_red ($message);
183       }
184     }
185   }
189 // Return something like array['sambaLMPassword']= "lalla..." 
190 function generate_smb_nt_hash($password)
192   global $config;
193   $tmp= $config->data['MAIN']['SMBHASH']." ".escapeshellarg($password);
194   @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $tmp, "Execute");
196   exec($tmp, $ar);
197   flush();
198   reset($ar);
199   $hash= current($ar);
200   if ($hash == "")
201   {
202     print_red (_("Setting for SMBHASH in gosa.conf is incorrect! Can't change Samba password."));
203   }
204   else 
205   {
206     list($lm,$nt)= split (":", trim($hash));
208     if ($config->current['SAMBAVERSION'] == 3)
209     {
210       $attrs['sambaLMPassword']= $lm;
211       $attrs['sambaNTPassword']= $nt;
212       $attrs['sambaPwdLastSet']= date('U');
213       $attrs['sambaBadPasswordCount']= "0";
214       $attrs['sambaBadPasswordTime']= "0";
215     } else {
216       $attrs['lmPassword']= $lm;
217       $attrs['ntPassword']= $nt;
218       $attrs['pwdLastSet']= date('U');
219     }
220     return($attrs);
221   }
222
224 function crypt_single($string,$enc_type )
226   if(!class_exists("passwordMethod")){
227     require_once("class_password-methods.inc");
228   }
229   return( passwordMethod::crypt_single_str($string,$enc_type));
232 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
233 ?>