Code

Reverted "periodical" changes in Daemon Handler
[gosa.git] / gosa-core / include / functions.inc
index 2f137ba65dcbc4e3b3ed96f804345417c1c015f7..d9d12986fd5aa5ba65846f3604594aa4025f3899 100644 (file)
@@ -88,6 +88,7 @@ define ("DEBUG_CONFIG", 64);
 define ("DEBUG_ACL",    128);
 define ("DEBUG_SI",     256);
 define ("DEBUG_MAIL",   512); // mailAccounts, imap, sieve etc.
+define ("DEBUG_FAI",   1024); // FAI (incomplete)
 
 /* Rewrite german 'umlauts' and spanish 'accents'
    to get better results */
@@ -404,6 +405,12 @@ function ldap_login_user_htaccess ($username)
   $ui= new userinfo($config, $ldap->getDN());
   $ui->username= $attrs['uid'][0];
 
+  /* Bail out if we have login restrictions set, for security reasons
+     the message is the same than failed user/pw */
+  if (!$ui->loginAllowed()){
+    return (NULL);
+  }
+
   /* No password check needed - the webserver did it for us */
   $ldap->disconnect();
 
@@ -484,6 +491,12 @@ function ldap_login_user ($username, $password)
   $ui= new userinfo($config, $ldap->getDN());
   $ui->username= $attrs['uid'][0];
 
+  /* Bail out if we have login restrictions set, for security reasons
+     the message is the same than failed user/pw */
+  if (!$ui->loginAllowed()){
+    return (NULL);
+  }
+
   /* password check, bind as user with supplied password  */
   $ldap->disconnect();
   $ldap= new LDAP($ui->dn, $password, $config->current['SERVER'],
@@ -606,6 +619,13 @@ function add_lock($object, $user)
   if(!session::global_is_set("LOCK_CACHE")){
     session::global_set("LOCK_CACHE",array(""));
   }
+  if(is_array($object)){
+    foreach($object as $obj){
+      add_lock($obj,$user);
+    }
+    return;
+  }
+
   $cache = &session::global_get("LOCK_CACHE");
   if(isset($_POST['open_readonly'])){
     $cache['READ_ONLY'][$object] = TRUE;
@@ -615,12 +635,6 @@ function add_lock($object, $user)
     unset($cache['READ_ONLY'][$object]);
   }
 
-  if(is_array($object)){
-    foreach($object as $obj){
-      add_lock($obj,$user);
-    }
-    return;
-  }
 
   /* Just a sanity check... */
   if ($object == "" || $user == ""){
@@ -1164,6 +1178,7 @@ function get_ou($name)
   global $config;
 
   $map = array( 
+                "roleRDN"      => "ou=roles,",
                 "ogroupRDN"      => "ou=groups,",
                 "applicationRDN" => "ou=apps,",
                 "systemRDN"     => "ou=systems,",
@@ -1293,7 +1308,9 @@ function gen_locked_message($user, $dn, $allow_readonly = FALSE)
   /* Save variables from LOCK_VARS_TO_USE in session - for further editing */
   if( session::is_set('LOCK_VARS_TO_USE') && count(session::get('LOCK_VARS_TO_USE'))){
 
-    $LOCK_VARS_USED   = array();
+    $LOCK_VARS_USED_GET   = array();
+    $LOCK_VARS_USED_POST   = array();
+    $LOCK_VARS_USED_REQUEST   = array();
     $LOCK_VARS_TO_USE = session::get('LOCK_VARS_TO_USE');
 
     foreach($LOCK_VARS_TO_USE as $name){
@@ -1304,18 +1321,26 @@ function gen_locked_message($user, $dn, $allow_readonly = FALSE)
 
       foreach($_POST as $Pname => $Pvalue){
         if(preg_match($name,$Pname)){
-          $LOCK_VARS_USED[$Pname] = $_POST[$Pname];
+          $LOCK_VARS_USED_POST[$Pname] = $_POST[$Pname];
         }
       }
 
       foreach($_GET as $Pname => $Pvalue){
         if(preg_match($name,$Pname)){
-          $LOCK_VARS_USED[$Pname] = $_GET[$Pname];
+          $LOCK_VARS_USED_GET[$Pname] = $_GET[$Pname];
+        }
+      }
+
+      foreach($_REQUEST as $Pname => $Pvalue){
+        if(preg_match($name,$Pname)){
+          $LOCK_VARS_USED_REQUEST[$Pname] = $_REQUEST[$Pname];
         }
       }
     }
     session::set('LOCK_VARS_TO_USE',array());
-    session::set('LOCK_VARS_USED'  , $LOCK_VARS_USED);
+    session::set('LOCK_VARS_USED_GET'  , $LOCK_VARS_USED_GET);
+    session::set('LOCK_VARS_USED_POST'  , $LOCK_VARS_USED_POST);
+    session::set('LOCK_VARS_USED_REQUEST'  , $LOCK_VARS_USED_REQUEST);
   }
 
   /* Prepare and show template */
@@ -1637,8 +1662,8 @@ function recurse($rule, $variables)
 function expand_id($rule, $attributes)
 {
   /* Check for id rule */
-  if(preg_match('/^id(:|#)\d+$/',$rule)){
-    return (array("\{$rule}"));
+  if(preg_match('/^id(:|#|!)\d+$/',$rule)){
+    return (array("{$rule}"));
   }
 
   /* Check for clean attribute */
@@ -1706,27 +1731,28 @@ function gen_uids($rule, $attributes)
   $proposed= recurse($stripped, $variables);
 
   /* Get list of used ID's */
-  $used= array();
   $ldap= $config->get_ldap_link();
   $ldap->cd($config->current['BASE']);
-  $ldap->search('(uid=*)');
-
-  while($attrs= $ldap->fetch()){
-    $used[]= $attrs['uid'][0];
-  }
 
   /* Remove used uids and watch out for id tags */
   $ret= array();
   foreach($proposed as $uid){
 
     /* Check for id tag and modify uid if needed */
-    if(preg_match('/\{id:\d+}/',$uid)){
-      $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
+    if(preg_match('/\{id(:|!)\d+}/',$uid, $m)){
+      $size= preg_replace('/^.*{id(:|!)(\d+)}.*$/', '\\2', $uid);
+
+      $start= $m[1]==":"?0:-1;
+      for ($i= $start, $p= pow(10,$size); $i < $p; $i++){
+        if ($i == -1) {
+          $number= "";
+        } else {
+          $number= sprintf("%0".$size."d", $i+1);
+        }
+        $res= preg_replace('/{id(:|!)\d+}/', $number, $uid);
 
-      for ($i= 0, $p= pow(10,$size); $i < $p; $i++){
-        $number= sprintf("%0".$size."d", $i);
-        $res= preg_replace('/{id:(\d+)}/', $number, $uid);
-        if (!in_array($res, $used)){
+        $ldap->search("(uid=".preg_replace('/[{}]/', '', $res).")",array('dn'));
+        if($ldap->count() == 0){
           $uid= $res;
           break;
         }
@@ -1740,7 +1766,8 @@ function gen_uids($rule, $attributes)
         mt_srand((double) microtime()*1000000);
         $number= sprintf("%0".$size."d", mt_rand(0, pow(10, $size)-1));
         $res= preg_replace('/{id#(\d+)}/', $number, $uid);
-        if (!in_array($res, $used)){
+        $ldap->search("(uid=".preg_replace('/[{}]/', '', $res).")",array('dn'));
+        if($ldap->count() == 0){
           $uid= $res;
           break;
         }
@@ -1748,8 +1775,10 @@ function gen_uids($rule, $attributes)
     }
 
     /* Don't assign used ones */
-    if (!in_array($uid, $used)){
-      $ret[]= $uid;
+    $ldap->search("(uid=".preg_replace('/[{}]/', '', $uid).")",array('dn'));
+    if($ldap->count() == 0){
+      /* Add uid, but remove {} first. These are invalid anyway. */
+      $ret[]= preg_replace('/[{}]/', '', $uid);
     }
   }
 
@@ -1980,40 +2009,7 @@ function compare_revision($revision_file, $revision)
 
 function progressbar($percentage,$width=100,$height=15,$showvalue=false)
 {
-  $str = ""; // Our return value will be saved in this var
-
-  $color  = dechex($percentage+150);
-  $color2 = dechex(150 - $percentage);
-  $bgcolor= $showvalue?"FFFFFF":"DDDDDD";
-
-  $progress = (int)(($percentage /100)*$width);
-
-  /* If theres a better solution for this, use it... */
-  $str = "\n   <div style=\" width:".($width)."px; ";
-  $str.= "\n       height:".($height)."px; ";
-  $str.= "\n       background-color:#000000; ";
-  $str.= "\n       padding:1px;\" > ";
-
-  $str.= "\n     <div style=\" width:".($width)."px; ";
-  $str.= "\n         background-color:#$bgcolor; ";
-  $str.= "\n         height:".($height)."px;\" > ";
-
-  if(($height >10)&&($showvalue)){
-    $str.= "\n   <font style=\"font-size:".($height-2)."px; ";
-    $str.= "\n     color:#FF0000; align:middle; ";
-    $str.= "\n     padding-left:".((int)(($width*0.4)))."px; \"> ";
-    $str.= "\n     <b>".$percentage."%</b> ";
-    $str.= "\n   </font> ";
-  }
-
-  $str.= "\n       <div style=\" width:".$progress."px; ";
-  $str.= "\n         height:".$height."px; ";
-  $str.= "\n         background-color:#".$color2.$color2.$color."; \" >";
-  $str.= "\n       </div>";
-  $str.= "\n     </div>";
-  $str.= "\n   </div>";
-
-  return($str);
+  return("<img src='progress.php?x=$width&amp;y=$height&amp;p=$percentage'>");
 }
 
 
@@ -2243,7 +2239,7 @@ function check_schema($cfg,$rfc2307bis = FALSE)
   $checks['gosaObject']['IS_MUST_HAVE']     = TRUE;
 
   /* GOsa Account class */
-  $checks["gosaAccount"]["REQUIRED_VERSION"]= "2.6.1";
+  $checks["gosaAccount"]["REQUIRED_VERSION"]= "2.6.6";
   $checks["gosaAccount"]["SCHEMA_FILES"]    = array("gosa+samba3.schema","gosa.schema");
   $checks["gosaAccount"]["CLASSES_REQUIRED"]= array("gosaAccount");
   $checks["gosaAccount"]["IS_MUST_HAVE"]    = TRUE;
@@ -2574,6 +2570,11 @@ function generate_smb_nt_hash($password)
     } else {
       $hash= "";
     }
+
+    if ($hash == "") {
+      msg_dialog::display(_("Configuration error"), _("Cannot generate samba hash!"), ERROR_DIALOG);
+      return ("");
+    }
   } else {
          $tmp= $config->get_cfg_value('sambaHashHook')." ".escapeshellarg($password);
          @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $tmp, "Execute");
@@ -2582,11 +2583,11 @@ function generate_smb_nt_hash($password)
          flush();
          reset($ar);
          $hash= current($ar);
-  }
 
-  if ($hash == "") {
-         msg_dialog::display(_("Configuration error"), _("Cannot generate samba hash!"), ERROR_DIALOG);
-         return ("");
+    if ($hash == "") {
+      msg_dialog::display(_("Configuration error"), sprintf(_("Cannot generate samba hash: running '%s' failed, check the 'sambaHashHook'!"),$config->get_cfg_value('sambaHashHook')), ERROR_DIALOG);
+      return ("");
+    }
   }
 
   list($lm,$nt)= split (":", trim($hash));
@@ -2801,6 +2802,7 @@ function cred_encrypt($input, $password) {
 
 }
 
+
 function cred_decrypt($input,$password) {
   $size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
   $iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
@@ -2809,5 +2811,30 @@ function cred_decrypt($input,$password) {
 }
 
 
+function get_object_info()
+{
+  return(session::get('objectinfo'));
+}
+
+
+function set_object_info($str = "")
+{
+  session::set('objectinfo',$str);
+}
+
+
+function isIpInNet($ip, $net, $mask) {
+   // Move to long ints
+   $ip= ip2long($ip);
+   $net= ip2long($net);
+   $mask= ip2long($mask);
+
+   // Mask given IP with mask. If it returns "net", we're in...
+   $res= $ip & $mask;
+
+   return ($res == $net);
+}
+
+
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>