Code

Added snmp-community to gosa setup
[gosa.git] / setup / class_setupStep_Migrate.inc
index abd24c2a4ad5859818a02d333ce468527345558b..05954715014de87bbc5cd21fbd393dd7181419f1 100644 (file)
@@ -67,7 +67,6 @@ class Step_Migrate extends setup_step
 
   /* Create Acl attributes */
   var $acl_create_dialog  = FALSE;
-  var $acl_create_type    = "group";
   var $acl_create_selected= ""; // Currently selected element, that should receive admin rights 
   var $acl_create_changes = ""; // Contains ldif information about changes 
   var $acl_create_confirmed= FALSE;
@@ -342,6 +341,21 @@ class Step_Migrate extends setup_step
 
     $group_ou = $cv['groupou'];
     $ldap->cd($cv['base']);
+
+    /***********
+     * Get all gosaDepartments to be able to 
+     *  validate correct ldap tree position of every single user
+     ***********/
+    $valid_deps = array();
+    $valid_deps['/'] = $cv['base'];
+    $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
+    while($attrs = $ldap->fetch()){
+      $valid_deps[$attrs['ou'][0]] = $attrs['dn'];
+    }
+  
+    /***********
+     * Get all groups 
+     ***********/
     $res = $ldap->search("(objectClass=posixGroup)",array("dn"));
     if(!$res){
       $this->checks['outside_groups']['STATUS']    = FALSE;
@@ -350,10 +364,16 @@ class Step_Migrate extends setup_step
       return(false);
     }
 
-
     $this->outside_groups = array();
     while($attrs = $ldap->fetch()){
-      if((!preg_match("/^[^,]+,".normalizePreg($group_ou)."/",$attrs['dn'])) && !preg_match("/,dc=addressbook,/",$attrs['dn'])){
+      $group_db_base = preg_replace("/^[^,]+,".normalizePreg($group_ou)."+,/i","",$attrs['dn']);
+
+      /* Check if entry is not an addressbook only user
+       *  and verify that he is in a valid department
+       */
+      if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$group_db_base) &&
+          !in_array($group_db_base,$valid_deps)
+         ){
         $attrs['selected'] = FALSE;
         $attrs['ldif']     = "";
         $this->outside_groups[base64_encode($attrs['dn'])] = $attrs;
@@ -386,8 +406,24 @@ class Step_Migrate extends setup_step
         $cv['connection'],
         FALSE,
         $cv['tls']);
-    $people_ou = $cv['peopleou'];
+
     $ldap->cd($cv['base']);
+
+  
+    /***********
+     * Get all gosaDepartments to be able to 
+     *  validate correct ldap tree position of every single user
+     ***********/
+    $valid_deps = array();
+    $valid_deps['/'] = $cv['base'];
+    $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
+    while($attrs = $ldap->fetch()){
+      $valid_deps[$attrs['ou'][0]] = $attrs['dn'];
+    }
+  
+    /***********
+     * Search for all users 
+     ***********/
     $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))",array("dn"));
     if(!$res){
       $this->checks['outside_users']['STATUS']    = FALSE;
@@ -396,10 +432,24 @@ class Step_Migrate extends setup_step
       return(false);
     }
 
-
+    /***********
+     * Check if returned users are within a valid GOsa deparmtment. (peopleou,gosaDepartment,base)
+     ***********/
     $this->outside_users = array();
+    $people_ou = trim($cv['peopleou']);
+    if(!empty($people_ou)){
+      $people_ou = $people_ou.",";
+    } 
+
     while($attrs = $ldap->fetch()){
-      if((!preg_match("/^[^,]+,".normalizePreg($people_ou)."/",$attrs['dn'])  && !preg_match("/,dc=addressbook,/",$attrs['dn']))){
+      $people_db_base = preg_replace("/^[^,]+,".normalizePreg($people_ou)."/i","",$attrs['dn']);
+      /* Check if entry is not an addressbook only user 
+       *  and verify that he is in a valid department
+       */
+      if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$people_db_base) &&
+          !in_array($people_db_base,$valid_deps)
+         ){
         $attrs['selected'] = FALSE;
         $attrs['ldif']     = "";
         $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
@@ -623,12 +673,12 @@ class Step_Migrate extends setup_step
         $cv['tls']);
 
     /* Skip GOsa internal departments */
-    $skip_dns = array("/^ou=people,/","/^ou=groups,/","/(,|)ou=configs,/","/(,|)ou=systems,/",
-        "/^ou=apps,/","/^ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
+    $skip_dns = array("/".$cv['peopleou']."/","/".$cv['groupou']."/","/^ou=people,/",
+        "/^ou=groups,/","/(,|)ou=configs,/","/(,|)ou=systems,/",
+        "/(,|)ou=apps,/","/(,|)ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
         "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
         "/(,|)ou=winstations,/");
 
-
     /* Get all invisible departments */
     $ldap->cd($cv['base']); 
     $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
@@ -801,7 +851,7 @@ class Step_Migrate extends setup_step
 
       if($found){
         $this->checks['acls']['STATUS']    = TRUE;
-        $this->checks['acls']['STATUS_MSG']= _("Ok")." ";
+        $this->checks['acls']['STATUS_MSG']= _("Ok");
         $this->checks['acls']['ERROR_MSG'] = "";
       }else{
         $this->checks['acls']['STATUS']    = FALSE;
@@ -832,56 +882,107 @@ class Step_Migrate extends setup_step
         $cv['connection'],
         FALSE,
         $cv['tls']);
-
-    /* Get current base attributes */
+    
     $ldap->cd($cv['base']);
-    $ldap->cat($cv['base'],array("dn","objectClass","gosaAclEntry"));
-    $attrs = $ldap->fetch();
+    $ldap->cat($dn,array("objectClass","cn","uid"));
+    $object_attrs = $ldap->fetch();
+    $type = "none";
+  
+    /* Check object that should receive admin acls */
+    if(in_array("gosaAccount",$object_attrs['objectClass'])){
+      $type = "user";
+    }elseif(in_array("posixGroup",$object_attrs['objectClass'])){
+      $type = "group";
+    } 
+
+    /* If a user should get administrative acls, we  
+     *  should check if there is an administrational group 
+     *  and just assign the user to it.
+     * If there is no such group, we must create one.
+     */
+    if($type == "user"){
 
-    /* Add acls for the selcted user to the base */
-    $attrs_new['objectClass'] = array("gosaACL");
+      $ldap->search("(&(objectClass=posixGroup)(gosaSubtreeACL=:all)(memberUid=*))",array("memberUid"));
+      if($ldap->count()){
+        $fetched_attrs          = $ldap->fetch();
+        $attrs_admin_group      = $this->cleanup_array($fetched_attrs);
+        $attrs_admin_group_new  = $attrs_admin_group;
 
-    for($i = 0; $i < $attrs['objectClass']['count']; $i ++){
-      if(!in_array_ics($attrs['objectClass'][$i],$attrs_new['objectClass'])){
-        $attrs_new['objectClass'][] = $attrs['objectClass'][$i];
-      }
-    }
+        if(!isset($attrs_admin_group_new['memberUid'])){
+          $attrs_admin_group_new['memberUid'] = array();
+        }
+        if(!in_array($object_attrs['uid'][0],$attrs_admin_group_new['memberUid'])){
+          $attrs_admin_group_new['memberUid'][] = $object_attrs['uid'][0];
+        }
+       if ($cv['rfc2307bis']){
+         $attrs_admin_group_new['member'][] = $dn;
+       }
 
-    $acl = "0:psub:".base64_encode($dn).":all;cmdrw";    
-    $attrs_new['gosaAclEntry'][] = $acl;
-    if(isset($attrs['gosaAclEntry'])){
-      for($i = 0 ; $i < $attrs['gosaAclEntry']['count']; $i ++){
-          
-        $prio = preg_replace("/[:].*$/","",$attrs['gosaAclEntry'][$i]);
-        $rest = preg_replace("/^[^:]/","",$attrs['gosaAclEntry'][$i]);
-        $data = ($prio+1).$rest;
-        $attrs_new['gosaAclEntry'][] = $data;
-      }
-    }
+        if($only_ldif){
+          $this->acl_create_changes = _("Appending user to group administrational group:")." \n";
+          $this->acl_create_changes.= "\n"._("Before").":\n";
+          $this->acl_create_changes.= $fetched_attrs['dn']."\n";
+          $this->acl_create_changes.= $this->array_to_ldif($attrs_admin_group)."\n";
+          $this->acl_create_changes.= "\n"._("After").":\n";
+          $this->acl_create_changes.= $fetched_attrs['dn']."\n";
+          $this->acl_create_changes.= $this->array_to_ldif($attrs_admin_group_new)."\n";
+        }else{ 
+          $ldap->cd($fetched_attrs['dn']);
+          $ldap->modify($attrs_admin_group_new);
+          if(!preg_match("/success/i",$ldap->get_error())){
+            print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
+            return(FALSE);
+          }
+        }
+        
+      } else {
 
-    if($only_ldif){
-      $this->acl_create_changes ="\n".$cv['base']."\n";
-      $this->acl_create_changes.=$this->array_to_ldif($attrs)."\n";
-      $this->acl_create_changes.="\n".$cv['base']."\n";
-      $this->acl_create_changes.=$this->array_to_ldif($attrs_new);
-    }else{
-   
-      $ldap->cd($cv['base']);
-      if(!$ldap->modify($attrs_new)){
-        print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
-        return(FALSE);
-      }else{
-        return(TRUE);
+        $group_ou = trim($cv['groupou']);
+        if(!empty($group_ou)){
+          $group_ou = trim($group_ou).",";
+        }
+
+        $new_group_dn = "cn=GOsa Administrators,".$group_ou.$cv['base'];
+       if ($cv['rfc2307bis']){
+               $new_group_attrs['objectClass'] = array("gosaObject","posixGroup", "groupOfNames");
+               $new_group_attrs['member'][] = $dn;
+       } else {
+               $new_group_attrs['objectClass'] = array("gosaObject","posixGroup");
+       }
+        $new_group_attrs['cn'] = "GOsa Administrators";
+        $new_group_attrs['gosaSubtreeACL'] = ":all";
+        $new_group_attrs['gidNumber'] = "999";
+        $new_group_attrs['memberUid'] = array($object_attrs['uid'][0]);
+
+        if($only_ldif){
+          $this->acl_create_changes = _("Creating new administrational group:")." \n\n";
+          $this->acl_create_changes.= $new_group_dn."\n";
+          $this->acl_create_changes.= $this->array_to_ldif($new_group_attrs);
+        }else{ 
+          $ldap->cd($cv['base']);
+          $ldap->create_missing_trees($group_ou.$cv['base']);
+          $ldap->cd($new_group_dn);
+          $res = $ldap->add($new_group_attrs);
+          if(!$res){
+            print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
+            return(FALSE);
+          }
+        }
       }
     }
+    return(TRUE);
   }
  
   
   function create_admin_user()
   {
     $pw1 = $pw2 = "";
+    $uid = "";
 
+    if(isset($_POST['new_user_uid'])){
+      $uid = $_POST['new_user_uid'];
+    }
+  
     if(isset($_POST['new_user_password'])){
       $pw1 = $_POST['new_user_password'];
     }
@@ -893,7 +994,12 @@ class Step_Migrate extends setup_step
       print_red(_("Specified passwords are empty or not equal."));
       return false;
     }
+
+    if(!is_uid($uid) || empty($uid)){
+      print_red(_("Please specify a valid uid."));
+      return false;
+    }
+
     /* Establish ldap connection */
     $cv = $this->parent->captured_values;
     $ldap = new LDAP($cv['admin'],
@@ -904,11 +1010,16 @@ class Step_Migrate extends setup_step
 
     /* Get current base attributes */
     $ldap->cd($cv['base']);
-  
+
+    $people_ou = trim($cv['peopleou']);
+    if(!empty($people_ou)){
+      $people_ou = trim($people_ou).",";
+    }
+
     if($cv['peopledn'] == "cn"){
-      $dn = "cn=System Administrator,".$cv['peopleou'].",".$cv['base'];
+      $dn = "cn=System Administrator,".$people_ou.$cv['base'];
     }else{
-      $dn = "uid=admin,".$cv['peopleou'].",".$cv['base'];
+      $dn = "uid=".$uid.",".$people_ou.$cv['base'];
     }
 
     $methods = @passwordMethod::get_available_methods_if_not_loaded();
@@ -921,15 +1032,22 @@ class Step_Migrate extends setup_step
     $new_user['givenName']  = "System";
     $new_user['sn']  = "Administrator";
     $new_user['cn']  = "System Administrator";
-    $new_user['uid'] = "admin";
+    $new_user['uid'] = $uid;
     $new_user['userPassword'] = $hash;
     
     $ldap->cd($cv['base']);
+    $ldap->cat($dn,array("dn"));
+    if($ldap->count()){
+      print_red(sprintf(_("Could not add administrative user, there is already an object with the same dn '%s' in your ldap database."),
+            $dn));
+      return(FALSE);
+    }
+
     $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$dn));
     $ldap->cd($dn);  
     $res = $ldap->add($new_user);
     $this->acl_create_selected = $dn;
-    $this->create_admin();
+    $this->create_admin(FALSE, $dn);
     
     if(!$res){
       print_red($ldap->get_error());
@@ -1057,7 +1175,7 @@ class Step_Migrate extends setup_step
       print_red(_("Couldn't move users to specified department."));
       return(false);
     }
+      
     foreach($this->outside_users as $b_dn => $data){
       $this->outside_users[$b_dn]['ldif'] ="";
       if($data['selected']){
@@ -1241,14 +1359,14 @@ class Step_Migrate extends setup_step
       $this->show_details = FALSE;
     }
 
-    if(isset($_POST['create_acls_create_confirmed'])){
-      if($this->create_admin()){
-        $this->acl_create_dialog = FALSE;
-        $this->dialog = FALSE;
-      $this->show_details = FALSE;
-        $this->initialize_checks();
-      }
-    }
+#    if(isset($_POST['create_acls_create_confirmed'])){
+#      if($this->create_admin()){
+#        $this->acl_create_dialog = FALSE;
+#        $this->dialog = FALSE;
+#      $this->show_details = FALSE;
+#        $this->initialize_checks();
+#      }
+#    }
 
     if(isset($_POST['create_acls_create'])){
       $this->create_admin(TRUE);
@@ -1263,13 +1381,15 @@ class Step_Migrate extends setup_step
 
     if($this->acl_create_dialog){
       $smarty = get_smarty();
+
+      $uid = "admin";
+      if(isset($_POST['new_user_uid'])){
+        $uid = $_POST['new_user_uid'];
+      }
+
+      $smarty->assign("new_user_uid",$uid);
       $smarty->assign("new_user_password",@$_POST['new_user_password']);
       $smarty->assign("new_user_password2",@$_POST['new_user_password2']);
-      $smarty->assign("users" ,$this->get_user_list());
-      $smarty->assign("users_cnt" ,count($this->get_user_list()));
-      $smarty->assign("groups",$this->get_group_list());
-      $smarty->assign("groups_cnt",count($this->get_group_list()));
-      $smarty->assign("type"  ,$this->acl_create_type);
       $smarty->assign("method","create_acls");
       $smarty->assign("acl_create_selected",$this->acl_create_selected);
       $smarty->assign("what_will_be_done_now",$this->acl_create_changes);
@@ -1417,21 +1537,10 @@ class Step_Migrate extends setup_step
 
     /* Get "create acl" dialog posts */
     if($this->acl_create_dialog){
-      if(isset($_POST['create_acls_create'])){
-        if(isset($_POST['create_acls_selected'])){
-          $this->acl_create_selected = base64_decode($_POST['create_acls_selected']);
-        }else{
-          $this->acl_create_selected = ""; 
-        }
-      }
 
       if(isset($_POST['create_acls_create_abort'])){
         $this->acl_create_selected = "";
       }
-
-      if(isset($_POST['acl_create_type'])){
-        $this->acl_create_type = $_POST['acl_create_type'];
-      }
     }
 
     /* Get selected departments */
@@ -1567,6 +1676,7 @@ class Step_Migrate extends setup_step
   {
     /* Get collected configuration settings */
     $cv = $this->parent->captured_values;
+    $people_ou = trim($cv['peopleou']);
 
     /* Establish ldap connection */
     $ldap = new LDAP($cv['admin'],
@@ -1575,33 +1685,54 @@ class Step_Migrate extends setup_step
         FALSE,
         $cv['tls']);
 
-    $ldap->cd($cv['base']);
-    $ldap->search("(".$cv['peopleou'].")",array("dn"));
-  
-    if($ldap->count() == 0 ){
-      $add_dn = $cv['peopleou'].",".$cv['base'];
-      $naming_attr = preg_replace("/=.*$/","",$add_dn);
-      $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
-      $add = array();
-      $add['objectClass'] = array("organizationalUnit");
-      $add[$naming_attr] = $naming_value;
+    
+    /*****************
+     * If people ou is NOT empty 
+     * search for for all objects matching the given container
+     *****************/
+    if(!empty($people_ou)){
+      $ldap->search("(".$people_ou.")",array("dn"));
+
+      /* Create people ou if there is currently none */
+      if($ldap->count() == 0 ){
+        $add_dn = $cv['peopleou'].",".$cv['base'];
+        $naming_attr = preg_replace("/=.*$/","",$add_dn);
+        $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
+        $add = array();
+        $add['objectClass'] = array("organizationalUnit");
+        $add[$naming_attr] = $naming_value;
+        $ldap->cd($cv['base']);
+        $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
+        $ldap->cd($add_dn);
+        $ldap->add($add);
+      }
 
-      $ldap->cd($cv['base']);
-      $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
-      $ldap->cd($add_dn);
-      $ldap->add($add);
-    }
+      /* Create result */
+      $ldap->search("(".$cv['peopleou'].")",array("dn"));
+      $tmp = array();
+      while($attrs= $ldap->fetch()){
+        if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
+          $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+        }
+      }
+    } else{
 
-    $ldap->search("(".$cv['peopleou'].")",array("dn"));
-    $tmp = array();
-    while($attrs= $ldap->fetch()){
-      if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
-        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+      /************
+       * If people ou is empty 
+       * Get all valid gosaDepartments
+       ************/
+      $ldap->cd($cv['base']);
+      $tmp = array();
+      $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
+      $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
+      while($attrs = $ldap->fetch()){
+        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
       }
     }
     return($tmp); 
   }
 
+
   function get_all_winstation_ous()
   {
     /* Get collected configuration settings */
@@ -1660,29 +1791,50 @@ class Step_Migrate extends setup_step
         $cv['connection'],
         FALSE,
         $cv['tls']);
+    
+    $group_ou = trim($cv['groupou']);
+    if(!empty($group_ou)){
+      $group_ou = trim($group_ou);
+    }
 
+    /************
+     * If group ou is NOT empty
+     * Get all valid group ous, create one if necessary
+     ************/
     $ldap->cd($cv['base']);
-    $ldap->search("(".$cv['groupou'].")",array("dn"));
-  
-    if($ldap->count() == 0 ){
-      $add_dn = $cv['groupou'].",".$cv['base'];
-      $naming_attr = preg_replace("/=.*$/","",$add_dn);
-      $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
-      $add = array();
-      $add['objectClass'] = array("organizationalUnit");
-      $add[$naming_attr] = $naming_value;
+    if(!empty($group_ou)){
+      $ldap->search("(".$group_ou.")",array("dn"));
+      if($ldap->count() == 0 ){
+        $add_dn = $group_ou.$cv['base'];
+        $naming_attr = preg_replace("/=.*$/","",$add_dn);
+        $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
+        $add = array();
+        $add['objectClass'] = array("organizationalUnit");
+        $add[$naming_attr] = $naming_value;
 
+        $ldap->cd($cv['base']);
+        $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
+        $ldap->cd($add_dn);
+        $ldap->add($add);
+      }
+      $ldap->search("(".$group_ou.")",array("dn"));
+      $tmp = array();
+      while($attrs= $ldap->fetch()){
+        if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
+          $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+        }
+      }
+    }else{
+      /************
+       * If group ou is empty
+       * Get all valid gosaDepartments
+       ************/
       $ldap->cd($cv['base']);
-      $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
-      $ldap->cd($add_dn);
-      $ldap->add($add);
-    }
-
-    $ldap->search("(".$cv['groupou'].")",array("dn"));
-    $tmp = array();
-    while($attrs= $ldap->fetch()){
-      if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
-        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+      $tmp = array();
+      $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
+      $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
+      while($attrs = $ldap->fetch()){
+        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
       }
     }
     return($tmp); 
@@ -1778,5 +1930,5 @@ class Step_Migrate extends setup_step
   }
 }
 
-// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
+//vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>