Code

- Removed obsoleted dir /usr/lib/gosa
[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;
25   var $attrs= array();
27   // Konstructor
28   function passwordMethod($config)
29   {
30   }
32   // Loads Methods in annother way as  get_available_methods do, (For setup ..)
33   // and loads them,.
34   function get_available_methods_if_not_loaded($path_to_load="../include")
35   {
36     $oh = opendir($path_to_load);
37     $i = 0; 
38     $ret = false;
39     while ($file = readdir($oh)) {
40       $one = strtolower($file); 
41       if((strstr($one,"class_password-methods-" ))&&($one[0]!=".")){
42         require_once($file);
43       }
44     }
45     return(passwordMethod::get_available_methods());
46   }
51   // Crypts a single string, with given Method
52   function crypt_single_str($string,$method)
53   {
54     $available = passwordMethod::get_available_methods();
55     if(!is_array($available))
56     {
57       $available = passwordMethod::get_available_methods_if_not_loaded();
58     }
60     $test = new  $available[$method](false);
61     $newpass =  $test->generate_hash($string);
62     return( $newpass); 
63   }
66   // this function returns all loaded classes for password encryption
67   function get_available_methods()
68   {
69     $ret =false;
70     $all = get_declared_classes();
71     $i = 0;
72     foreach($all as $one) {
73       if(preg_match('/passwordMethod/i', $one) && !preg_match("/^passwordMethod$/i", $one)){
74         $name = preg_replace ("/passwordMethod/i", "", $one);
75         $test = new $one(false);
76         if($test->is_available()) {
77           $plugname= strtolower(preg_replace ("/passwordMethod/i","",$one));
78           $ret['name'][$i]= $plugname;
79           $ret['class'][$i]=$one;
80           $ret[$i]['name']= $plugname;
81           $ret[$i]['class']= $one;
82           $ret[$plugname]=$one;                    
83           $i++;
84         }
85       }
86     }
87     return($ret);
88   }
90 }
92 // change_password, changes the Password, of the given dn
93 function change_password ($dn, $password, $mode=0, $hash= "")
94 {
95   global $config;
96   $newpass= "";
98   // Get all available encryption Methods 
100   // NON STATIC CALL :)
101   $tmp = new passwordMethod($_SESSION['config']);
102   $available = $tmp->get_available_methods();
104   // read current password entry for $dn, to detect the encryption Method
105   $ldap       = $config->get_ldap_link();
106   $ldap->cat ($dn, array("shadowLastChange", "userPassword", "uid"));
107   $attrs      = $ldap->fetch ();
109   // Check if user account was deactivated, indicated by ! after } ... {crypt}!###
110   if(isset($attrs['userPassword'][0]) && preg_match("/^[^\}]*+\}!/",$attrs['userPassword'][0])){
111     $deactivated = TRUE;
112   }else{
113     $deactivated = FALSE;
114   }
116 #  // Get current password hash method if available 
117 #  if($hash == "" && isset($attrs['userPassword'][0]) && preg_match("/[\{\}]/",$attrs['userPassword'][0])){
118 #    $hash = preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$attrs['userPassword'][0]);
119 #    $hash = strtolower($hash);
120 #  }
122 #  // Set encryption type to clear if required 
123 #  if (!isset($attrs['userPassword'][0]) || $hash == ""){
124 #    $hash= "clear";
125 #  }
127   /* Is ensure that clear passwords will stay clear */
128   if($hash == "" && isset($attrs['userPassword'][0]) && !preg_match ("/^{([^}]+)}(.+)/", $attrs['userPassword'][0])){
129     $hash = "clear";
130   }
132   // Detect the encryption Method
133   if ( (isset($attrs['userPassword'][0]) &&  preg_match ("/^{([^}]+)}(.+)/", $attrs['userPassword'][0], $matches)) ||  $hash != ""){
135     /* Check for supported algorithm */
136     mt_srand((double) microtime()*1000000);
138     /* Extract used hash */
139     if ($hash == ""){
140       $hash= strtolower($matches[1]);
141     }
143     // Crypt with the detected Method
144     $test = new  $available[$hash]($config);
145     $test->attrs= $attrs;
146     $newpass =  $test->generate_hash($password);
148   } else {
149     // Crypt it by default 
150     $test = new  $available['md5']($config);
151     $newpass =  $test->generate_hash($password);
152   }
154   // Update shadow timestamp? 
155   if (isset($attrs["shadowLastChange"][0])){
156     $shadow= (int)(date("U") / 86400);
157   } else {
158     $shadow= 0;
159   }
161   // Write back modified entry 
162   $ldap->cd($dn);
163   $attrs= array();
165   // Not for groups 
166   if ($mode == 0){
168     if ($shadow != 0){
169       $attrs['shadowLastChange']= $shadow;
170     }
172     // Create SMB Password 
173     $attrs= generate_smb_nt_hash($password);
174   }
176   /* Readd ! if user was deactivated */
177   if($deactivated){
178     $newpass = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$newpass);
179   }
181   $attrs['userPassword']= array();
182   $attrs['userPassword']= $newpass;
185   $ldap->modify($attrs);
188   if ($ldap->error != 'Success') {
189     print_red(sprintf(_("Setting the password failed. LDAP server says '%s'."),
190           $ldap->get_error()));
191   } else {
193     /* Find postmodify entries for this class */
194     $command= search_config($config->data['MENU'], "password", "POSTMODIFY");
196     if ($command != ""){
197       /* Walk through attribute list */
198       $command= preg_replace("/%userPassword/", $password, $command);
199       $command= preg_replace("/%dn/", $dn, $command);
201       if (check_command($command)){
202         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, "Execute");
203         exec($command);
204       } else {
205         $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, "password");
206         print_red ($message);
207       }
208     }
209   }
213 // Return something like array['sambaLMPassword']= "lalla..." 
214 function generate_smb_nt_hash($password)
216   global $config;
217   $tmp= $config->data['MAIN']['SMBHASH']." ".escapeshellarg($password);
218   @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $tmp, "Execute");
220   exec($tmp, $ar);
221   flush();
222   reset($ar);
223   $hash= current($ar);
224   if ($hash == "")
225   {
226     print_red (sprintf(_("Setting for SMBHASH in %s is incorrect! Can't change Samba password."),CONFIG_FILE));
227   }
228   else 
229   {
230     list($lm,$nt)= split (":", trim($hash));
232     if ($config->current['SAMBAVERSION'] == 3)
233     {
234       $attrs['sambaLMPassword']= $lm;
235       $attrs['sambaNTPassword']= $nt;
236       $attrs['sambaPwdLastSet']= date('U');
237       $attrs['sambaBadPasswordCount']= "0";
238       $attrs['sambaBadPasswordTime']= "0";
239     } else {
240       $attrs['lmPassword']= $lm;
241       $attrs['ntPassword']= $nt;
242       $attrs['pwdLastSet']= date('U');
243     }
244     return($attrs);
245   }
246
248 function crypt_single($string,$enc_type )
250   if(!class_exists("passwordMethod")){
251     require_once("class_password-methods.inc");
252   }
253   return( passwordMethod::crypt_single_str($string,$enc_type));
256 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
257 ?>