Code

5dcd8e649d9ccba0c9fdf273b1e8a08f0dc6d26c
[gosa.git] / setup / class_setupStep_Migrate.inc
1 <?php
3 /*
4    This code is part of GOsa (https://gosa.gonicus.de)
5    Copyright (C) 2007 Fabian Hickert
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
24 /****************
25  * FUNCTIONS 
27 Step_Migrate                - Constructor.
28 update_strings              - Used to update the displayed step informations.
29 initialize_checks           - Initialize migration steps.
30 check_ldap_permissions      - Check if the used admin account has full access to the ldap database.
31 check_gosaAccounts          - Check if there are users without the required objectClasses.
32 migrate_gosaAccounts        - Migrate selected users to GOsa user accounts.
33 check_organizationalUnits   - Check if there are departments, that are not visible for GOsa
34 migrate_organizationalUnits - Migrate selected departments 
35 check_administrativeAccount - Check if there is at least one acl entry available                  
36 checkBase                   - Check if there is a root object available 
38 get_user_list               - Get list of available users
39 get_group_list              - Get list of groups
40   
41 create_admin                
42 create_admin_user           
44 execute                     - Generate html output of this plugin
45 save_object                 - Save posts 
46 array_to_ldif               - Create ldif output of an ldap result array 
47  
48  ****************/
52 class Step_Migrate extends setup_step
53 {
54   var $languages      = array();
55   var $attributes     = array();
56   var $header_image   = "images/monitoring.png";
57   var $checks         = array();
59   /* Department migration attributes */
60   var $dep_migration_dialog = FALSE;
61   var $deps_to_migrate      = array();
62   var $show_details         = FALSE;
64   /* Department migration attributes */
65   var $users_migration_dialog= FALSE;
66   var $users_to_migrate      = array();
68   /* Create Acl attributes */
69   var $acl_create_dialog  = FALSE;
70   var $acl_create_selected= ""; // Currently selected element, that should receive admin rights 
71   var $acl_create_changes = ""; // Contains ldif information about changes 
72   var $acl_create_confirmed= FALSE;
74   /* Checks initialised ? */
75   var $checks_initialised = FALSE;
77   /* Users outside to people ou */
78   var $outside_users        = array();
79   var $outside_users_dialog = FALSE;
81   /* Users outside to groups ou */
82   var $outside_groups        = array();
83   var $outside_groups_dialog = FALSE;
85   /* Win-Workstations outside to reserved ou */
86   var $outside_winstations        = array();
87   var $outside_winstations_dialog = FALSE;
89   /* check for multiple use of same uidNumber */
90   var $check_uidNumbers        = array();
91   var $check_uidNumbers_dialog = FALSE;
93   /* check for multiple use of same gidNumber */
94   var $check_gidNumbers        = array();
95   var $check_gidNumbers_dialog = FALSE;
98   function Step_Migrate()
99   {
100     $this->update_strings(); 
101   }
103   function update_strings()
104   {
105     $this->s_title      = _("LDAP inspection");
106     $this->s_title_long = _("LDAP inspection");
107     $this->s_info       = _("Analyze your current LDAP for GOsa compatibility");
108   }
110   function initialize_checks()
111   {
112     $this->checks = array();
113     $this->checks['root']['TITLE']     = _("Checking for root object");
114     $this->checks['root']['STATUS']    = FALSE;
115     $this->checks['root']['STATUS_MSG']= "";
116     $this->checks['root']['ERROR_MSG'] = "";
117     $this->checkBase();
119     $this->checks['permissions']['TITLE']     = _("Checking permissions on LDAP database");
120     $this->checks['permissions']['STATUS']    = FALSE;
121     $this->checks['permissions']['STATUS_MSG']= "";
122     $this->checks['permissions']['ERROR_MSG'] = "";
123     $this->check_ldap_permissions();
125     $this->checks['deps_visible']['TITLE']     = _("Checking for invisible deparmtments");
126     $this->checks['deps_visible']['STATUS']    = FALSE;
127     $this->checks['deps_visible']['STATUS_MSG']= "";
128     $this->checks['deps_visible']['ERROR_MSG'] = "";
129     $this->check_organizationalUnits();
131     $this->checks['users_visible']['TITLE']     = _("Checking for invisible users");
132     $this->checks['users_visible']['STATUS']    = FALSE;
133     $this->checks['users_visible']['STATUS_MSG']= "";
134     $this->checks['users_visible']['ERROR_MSG'] = "";
135     $this->check_gosaAccounts();
137     $this->checks['acls']['TITLE']     = _("Checking for super administrator");
138     $this->checks['acls']['STATUS']    = FALSE;
139     $this->checks['acls']['STATUS_MSG']= "";
140     $this->checks['acls']['ERROR_MSG'] = "";
141     $this->check_administrativeAccount();
143     $this->checks['outside_users']['TITLE']     = _("Checking for users outside the people tree");
144     $this->checks['outside_users']['STATUS']    = FALSE;
145     $this->checks['outside_users']['STATUS_MSG']= "";
146     $this->checks['outside_users']['ERROR_MSG'] = "";
147     $this->search_outside_users();
148     
149     $this->checks['outside_groups']['TITLE']     = _("Checking for groups outside the groups tree");
150     $this->checks['outside_groups']['STATUS']    = FALSE;
151     $this->checks['outside_groups']['STATUS_MSG']= "";
152     $this->checks['outside_groups']['ERROR_MSG'] = "";
153     $this->search_outside_groups();
155     $this->checks['outside_winstations']['TITLE']     = _("Checking for windows workstations outside the winstation tree");
156     $this->checks['outside_winstations']['STATUS']    = FALSE;
157     $this->checks['outside_winstations']['STATUS_MSG']= "";
158     $this->checks['outside_winstations']['ERROR_MSG'] = "";
159     $this->search_outside_winstations();
161     $this->checks['uidNumber_usage']['TITLE']     = _("Checking for duplicate uid numbers");
162     $this->checks['uidNumber_usage']['STATUS']    = FALSE;
163     $this->checks['uidNumber_usage']['STATUS_MSG']= "";
164     $this->checks['uidNumber_usage']['ERROR_MSG'] = "";
165     $this->check_uidNumber();
166     
167     $this->checks['gidNumber_usage']['TITLE']     = _("Checking for duplicate gid numbers");
168     $this->checks['gidNumber_usage']['STATUS']    = FALSE;
169     $this->checks['gidNumber_usage']['STATUS_MSG']= "";
170     $this->checks['gidNumber_usage']['ERROR_MSG'] = "";
171     $this->check_gidNumber();
172   }
175   /* Check if there are uidNumbers which are used more than once. 
176    */
177   function check_uidNumber()
178   {
179     $cv = $this->parent->captured_values;
180     $ldap = new LDAP($cv['admin'],
181         $cv['password'],
182         $cv['connection'],
183         FALSE,
184         $cv['tls']);
186     $ldap->cd($cv['base']);
187     $res = $ldap->search("uidNumber=*",array("dn","uidNumber"));
188     if(!$res){
189       $this->checks['uidNumber_usage']['STATUS']    = FALSE;
190       $this->checks['uidNumber_usage']['STATUS_MSG']= _("LDAP query failed");
191       $this->checks['uidNumber_usage']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
192       return(false);
193     }
195     $this->check_uidNumbers= array(); 
196     $tmp = array();
197     while($attrs = $ldap->fetch()){
198       $tmp[$attrs['uidNumber'][0]][] = $attrs;
199     }
201     foreach($tmp as $id => $entries){
202       if(count($entries) > 1){
203         foreach($entries as $entry){
204           $this->check_uidNumbers[base64_encode($entry['dn'])] = $entry;
205         }
206       }
207     }
209     if($this->check_uidNumbers){
210       $this->checks['uidNumber_usage']['STATUS']    = FALSE;
211       $this->checks['uidNumber_usage']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
212       $this->checks['uidNumber_usage']['ERROR_MSG'] =
213         sprintf(_("Found %s duplicate values for attribute 'uidNumber'."),count($this->check_uidNumbers));
214       return(false);
215     }else{
216       $this->checks['uidNumber_usage']['STATUS']    = TRUE;
217       $this->checks['uidNumber_usage']['STATUS_MSG']= _("Ok");
218       $this->checks['uidNumber_usage']['ERROR_MSG'] = "";
219       return(TRUE);
220     }
221   }
223   
224   /* Check if there are duplicated gidNumbers present in ldap
225    */
226   function check_gidNumber()
227   {
228     $cv = $this->parent->captured_values;
229     $ldap = new LDAP($cv['admin'],
230         $cv['password'],
231         $cv['connection'],
232         FALSE,
233         $cv['tls']);
235     $ldap->cd($cv['base']);
236     $res = $ldap->search("(&(objectClass=posixGroup)(gidNumber=*))",array("dn","gidNumber"));
237     if(!$res){
238       $this->checks['gidNumber_usage']['STATUS']    = FALSE;
239       $this->checks['gidNumber_usage']['STATUS_MSG']= _("LDAP query failed");
240       $this->checks['gidNumber_usage']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
241       return(false);
242     }
244     $this->check_gidNumbers= array(); 
245     $tmp = array();
246     while($attrs = $ldap->fetch()){
247       $tmp[$attrs['gidNumber'][0]][] = $attrs;
248     }
250     foreach($tmp as $id => $entries){
251       if(count($entries) > 1){
252         foreach($entries as $entry){
253           $this->check_gidNumbers[base64_encode($entry['dn'])] = $entry;
254         }
255       }
256     }
258     if($this->check_gidNumbers){
259       $this->checks['gidNumber_usage']['STATUS']    = FALSE;
260       $this->checks['gidNumber_usage']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
261       $this->checks['gidNumber_usage']['ERROR_MSG'] =
262         sprintf(_("Found %s duplicate values for attribute 'gidNumber'."),count($this->check_gidNumbers));
263       return(false);
264     }else{
265       $this->checks['gidNumber_usage']['STATUS']    = TRUE;
266       $this->checks['gidNumber_usage']['STATUS_MSG']= _("Ok");
267       $this->checks['gidNumber_usage']['ERROR_MSG'] = "";
268       return(TRUE);
269     }
270   }
273   /* Search for winstations outside the winstation ou 
274    */
275   function search_outside_winstations()
276   {
277     $cv = $this->parent->captured_values;
278     $ldap = new LDAP($cv['admin'],
279         $cv['password'],
280         $cv['connection'],
281         FALSE,
282         $cv['tls']);
284     /* Get winstation ou */
285     if($cv['generic_settings']['wws_ou_active']) {
286       $winstation_ou = $cv['generic_settings']['ws_ou'];
287     }else{
288       $winstation_ou = "ou=winstations";
289     }
291     if($cv['samba_version'] == 3){
292       $oc = "sambaSamAccount";
293     }else{
294       $oc = "sambaAccount";
295     }
296  
297     $ldap->cd($cv['base']);
298     $res = $ldap->search("(&(objectClass=".$oc.")(uid=*$))",array("dn","sambaSID"));
299     if(!$res){
300       $this->checks['outside_winstations']['STATUS']    = FALSE;
301       $this->checks['outside_winstations']['STATUS_MSG']= _("LDAP query failed");
302       $this->checks['outside_winstations']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
303       return(false);
304     }
306     $this->outside_winstations = array();
307     while($attrs = $ldap->fetch()){
308       if((!preg_match("/^[^,]+,".normalizePreg($winstation_ou)."/",$attrs['dn'])) && !preg_match("/,dc=addressbook,/",$attrs['dn'])){
309         $attrs['selected'] = FALSE;
310         $attrs['ldif']     = "";
311         $this->outside_winstations[base64_encode($attrs['dn'])] = $attrs;
312       }
313     }
315     if(count($this->outside_winstations)){
316       $this->checks['outside_winstations']['STATUS']    = FALSE;
317       $this->checks['outside_winstations']['STATUS_MSG']= _("Failed");
318       $this->checks['outside_winstations']['ERROR_MSG'] = 
319         sprintf(_("Found %s winstations outside the predefined winstation department ou '%s'."),count($this->outside_winstations),$winstation_ou);
320       $this->checks['outside_winstations']['ERROR_MSG'].= "<input type='submit' name='outside_winstations_dialog' value='"._("Migrate")."...'>";
321       return(false);
322     }else{
323       $this->checks['outside_winstations']['STATUS']    = TRUE;
324       $this->checks['outside_winstations']['STATUS_MSG']= _("Ok");
325       $this->checks['outside_winstations']['ERROR_MSG'] = "";
326       return(TRUE);
327     }
328   }
331   /* Search for groups outside the group ou 
332    */
333   function search_outside_groups()
334   {
335     $cv = $this->parent->captured_values;
336     $ldap = new LDAP($cv['admin'],
337         $cv['password'],
338         $cv['connection'],
339         FALSE,
340         $cv['tls']);
342     $group_ou = $cv['groupou'];
343     $ldap->cd($cv['base']);
345     /***********
346      * Get all gosaDepartments to be able to
347      *  validate correct ldap tree position of every single user
348      ***********/
349     $valid_deps = array();
350     $valid_deps['/'] = $cv['base'];
351     $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
352     while($attrs = $ldap->fetch()){
353       $valid_deps[] = $attrs['dn'];
354     }
356     /***********
357      * Get all groups
358      ***********/
359     $res = $ldap->search("(objectClass=posixGroup)",array("dn"));
360     if(!$res){
361       $this->checks['outside_groups']['STATUS']    = FALSE;
362       $this->checks['outside_groups']['STATUS_MSG']= _("LDAP query failed");
363       $this->checks['outside_groups']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
364       return(false);
365     }
367     $this->outside_groups = array();
368     while($attrs = $ldap->fetch()){
369       $group_db_base = preg_replace("/^[^,]+,".normalizePreg($group_ou)."+,/i","",$attrs['dn']);
371       /* Check if entry is not an addressbook only user
372        *  and verify that he is in a valid department
373        */
374       if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$group_db_base) &&
375           !in_array($group_db_base,$valid_deps)
376         ){
377         $attrs['selected'] = FALSE;
378         $attrs['ldif']     = "";
379         $this->outside_groups[base64_encode($attrs['dn'])] = $attrs;
380       }
381     }
383     if(count($this->outside_groups)){
384       $this->checks['outside_groups']['STATUS']    = FALSE;
385       $this->checks['outside_groups']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
386       $this->checks['outside_groups']['ERROR_MSG'] =
387         sprintf(_("Found %s groups outside the configured tree '%s'."),count($this->outside_groups),$group_ou);
388       $this->checks['outside_groups']['ERROR_MSG'].= "&nbsp;<input type='submit' name='outside_groups_dialog' value='"._("Move")."...'>";
389       return(false);
390     }else{
391       $this->checks['outside_groups']['STATUS']    = TRUE;
392       $this->checks['outside_groups']['STATUS_MSG']= _("Ok");
393       $this->checks['outside_groups']['ERROR_MSG'] = "";
394       return(TRUE);
395     }
396   }
398  /* Search for users outside the people ou
399    */
400   function search_outside_users()
401   {
402     $cv = $this->parent->captured_values;
403     $ldap = new LDAP($cv['admin'],
404         $cv['password'],
405         $cv['connection'],
406         FALSE,
407         $cv['tls']);
409     $ldap->cd($cv['base']);
412     /***********
413      * Get all gosaDepartments to be able to
414      *  validate correct ldap tree position of every single user
415      ***********/
416     $valid_deps = array();
417     $valid_deps['/'] = $cv['base'];
418     $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
419     while($attrs = $ldap->fetch()){
420       $valid_deps[] = $attrs['dn'];
421     }
423     /***********
424      * Search for all users
425      ***********/
426     $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))",array("dn"));
427     if(!$res){
428       $this->checks['outside_users']['STATUS']    = FALSE;
429       $this->checks['outside_users']['STATUS_MSG']= _("LDAP query failed");
430       $this->checks['outside_users']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
431       return(false);
432     }
434     /***********
435      * Check if returned users are within a valid GOsa deparmtment. (peopleou,gosaDepartment,base)
436      ***********/
437     $this->outside_users = array();
438     $people_ou = trim($cv['peopleou']);
439     if(!empty($people_ou)){
440       $people_ou = $people_ou.",";
441     }
443     while($attrs = $ldap->fetch()){
444       $people_db_base = preg_replace("/^[^,]+,".normalizePreg($people_ou)."/i","",$attrs['dn']);
446       /* Check if entry is not an addressbook only user
447        *  and verify that he is in a valid department
448        */
449       if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$people_db_base) &&
450           !in_array($people_db_base,$valid_deps)
451          ){
452         $attrs['selected'] = FALSE;
453         $attrs['ldif']     = "";
454         $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
455       }
456     }
458     if(count($this->outside_users)){
459       $this->checks['outside_users']['STATUS']    = FALSE;
460       $this->checks['outside_users']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
461       $this->checks['outside_users']['ERROR_MSG'] =
462         sprintf(_("Found %s user(s) outside the configured tree '%s'."),count($this->outside_users),$people_ou);
463       $this->checks['outside_users']['ERROR_MSG'].= "<input type='submit' name='outside_users_dialog' value='"._("Move")."...'>";
464       return(false);
465     }else{
466       $this->checks['outside_users']['STATUS']    = TRUE;
467       $this->checks['outside_users']['STATUS_MSG']= _("Ok");
468       $this->checks['outside_users']['ERROR_MSG'] = "";
469       return(TRUE);
470     }
471   }
474   /* Check ldap accessibility 
475    * Create and remove a dummy object, 
476    *  to ensure that we have the necessary permissions
477    */
478   function check_ldap_permissions()
479   {
480     $cv = $this->parent->captured_values;
481     $ldap = new LDAP($cv['admin'],
482         $cv['password'],
483         $cv['connection'],
484         FALSE,
485         $cv['tls']);
487     /* Create dummy entry 
488      */
489     $name     = "GOsa_setup_text_entry_".session_id().rand(0,999999);
490     $dn       = "ou=".$name.",".$cv['base'];
491     $testEntry= array();
492     $testEntry['objectClass'][]= "top";
493     $testEntry['objectClass'][]= "organizationalUnit";
494     $testEntry['objectClass'][]= "gosaDepartment";
495     $testEntry['description']= "Created by GOsa setup, this object can be removed.";
496     $testEntry['ou']  = $name;
498     /* check if simple ldap cat will be successful 
499      */
500     $res = $ldap->cat($cv['base']);  
501     if(!$res){
502       $this->checks['permissions']['STATUS']    = FALSE;
503       $this->checks['permissions']['STATUS_MSG']= _("LDAP query failed");
504       $this->checks['permissions']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
505       return(false);
506     }
507   
508     /* Try to create dummy object 
509      */ 
510     $ldap->cd ($dn);
511     $ldap->create_missing_trees($dn);
512     $res = $ldap->add($testEntry);
513     $ldap->cat($dn);
514     if(!$ldap->count()){
515       new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
517       $this->checks['permissions']['STATUS']    = FALSE;
518       $this->checks['permissions']['STATUS_MSG']= _("Failed");
519       $this->checks['permissions']['ERROR_MSG'] = 
520         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
521       return(false);
522     }
524     /* Try to remove created entry 
525      */
526     $res = $ldap->rmDir($dn);
527     $ldap->cat($dn);
528     if($ldap->count()){
529       new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
530       $this->checks['permissions']['STATUS']    = FALSE;
531       $this->checks['permissions']['STATUS_MSG']= _("Failed");
532       $this->checks['permissions']['ERROR_MSG'] = 
533         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
534       return(false);
535     }
537     /* Create & remove of dummy object was successful */
538     $this->checks['permissions']['STATUS']    = TRUE;
539     $this->checks['permissions']['STATUS_MSG']= _("Ok");
540     $this->checks['permissions']['ERROR_MSG'] = "";
541     return(true);
542   } 
545   /* Check if there are users which will 
546    *  be invisible for GOsa 
547    */
548   function check_gosaAccounts()
549   {
550     /* Remember old list of ivisible users, to be able to set 
551      *  the 'html checked' status for the checkboxes again 
552      */
553     $cnt_ok = 0;
554     $old    = $this->users_to_migrate;
555     $this->users_to_migrate = array();
557     /* Get collected configuration settings */
558     $cv = $this->parent->captured_values;
560     /* Establish ldap connection */
561     $ldap = new LDAP($cv['admin'],
562         $cv['password'],
563         $cv['connection'],
564         FALSE,
565         $cv['tls']);
567     /* Get all invisible users 
568      */
569     $ldap->cd($cv['base']); 
570     $res =$ldap->search("(&(|(objectClass=posixAccount)(&(objectClass=inetOrgPerson)(objectClass=organizationalPerson)))(!(objectClass=gosaAccount))(uid=*))",array("sn","givenName","cn","uid"));
571     while($attrs = $ldap->fetch()){
572       if(!preg_match("/,dc=addressbook,/",$attrs['dn'])){
573         $attrs['checked'] = FALSE;
574         $attrs['before']  = "";
575         $attrs['after']   = "";
577         /* Set objects to selected, that were selected before reload */
578         if(isset($old[base64_encode($attrs['dn'])])){
579           $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
580         }
581         $this->users_to_migrate[base64_encode($attrs['dn'])] = $attrs;
582       }
583     }
585     /* No invisible */
586     if(!$res){
587       $this->checks['users_visible']['STATUS']    = FALSE;
588       $this->checks['users_visible']['STATUS_MSG']= _("LDAP query failed");
589       $this->checks['users_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
590     }elseif(count($this->users_to_migrate) == 0){
591       $this->checks['users_visible']['STATUS']    = TRUE;
592       $this->checks['users_visible']['STATUS_MSG']= _("Ok");
593       $this->checks['users_visible']['ERROR_MSG'] = "";
594     }else{
595       $this->checks['users_visible']['STATUS']    = FALSE;
596       $this->checks['users_visible']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
597       $this->checks['users_visible']['ERROR_MSG'] = sprintf(_("Found %s user(s) that will not be visible in GOsa."), 
598           count($this->users_to_migrate));
599       $this->checks['users_visible']['ERROR_MSG'] .= "<input type='submit' name='users_visible_migrate' value='"._("Migrate")."...'>";
600     }
601   }
604   /* Start user account migration 
605    */  
606   function migrate_gosaAccounts($only_ldif = FALSE)
607   {
608     $this->show_details= $only_ldif;
610     /* Get collected configuration settings */
611     $cv = $this->parent->captured_values;
613     /* Establish ldap connection */
614     $ldap = new LDAP($cv['admin'],
615         $cv['password'],
616         $cv['connection'],
617         FALSE,
618         $cv['tls']);
620     /* Add gosaAccount objectClass to the selected users  
621      */
622     foreach($this->users_to_migrate as $key => $dep){
623       if($dep['checked']){
625         /* Get old objectClasses */
626         $ldap->cat($dep['dn'],array("objectClass"));
627         $attrs      = $ldap->fetch();
629         /* Create new objectClass array */
630         $new_attrs  = array();
631         $new_attrs['objectClass']= array("gosaAccount","inetOrgPerson","organizationalPerson");
632         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
633           if(!in_array_ics($attrs['objectClass'][$i], $new_attrs['objectClass'])){
634             $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
635           }
636         }
638         /* Set info attributes for current object, 
639          *  or write changes to the ldap database 
640          */
641         if($only_ldif){
642           $this->users_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
643           $this->users_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
644         }else{
645           $ldap->cd($attrs['dn']);
646           if(!$ldap->modify($new_attrs)){
647             print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
648             return(false);
649           }
650         }
651       }
652     }
653     return(TRUE);
654   }
657   /* Check if there are invisible organizational Units 
658    */
659   function check_organizationalUnits()
660   {
661     $cnt_ok = 0;
662     $old = $this->deps_to_migrate;
663     $this->deps_to_migrate = array();
665     /* Get collected configuration settings */
666     $cv = $this->parent->captured_values;
668     /* Establish ldap connection */
669     $ldap = new LDAP($cv['admin'],
670         $cv['password'],
671         $cv['connection'],
672         FALSE,
673         $cv['tls']);
675     /* Skip GOsa internal departments */
676     $skip_dns = array("/".$cv['peopleou']."/","/".$cv['groupou']."/","/^ou=people,/","/^ou=groups,/",
677         "/(,|)ou=configs,/","/(,|)ou=systems,/",
678         "/(,|)ou=apps,/","/(,|)ou=mime,/","/(,|)ou=devices/","/^ou=aclroles,/","/^ou=incoming,/",
679         "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
680         "/(,|)ou=winstations,/");
682     /* Get all invisible departments */
683     $ldap->cd($cv['base']); 
684     $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
685     while($attrs = $ldap->fetch()){
686       $attrs['checked'] = FALSE;
687       $attrs['before']  = "";
688       $attrs['after']   = "";
690       /* Set objects to selected, that were selected before reload */
691       if(isset($old[base64_encode($attrs['dn'])])){
692         $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
693       }
694       $this->deps_to_migrate[base64_encode($attrs['dn'])] = $attrs;
695     }
697     /* Filter returned list of departments and ensure that 
698      *  GOsa internal departments will not be listed 
699      */
700     foreach($this->deps_to_migrate as $key => $attrs){
701       $dn = $attrs['dn'];
702       $skip = false;;
703       foreach($skip_dns as $skip_dn){
704         if(preg_match($skip_dn,$dn)){
705           $skip = true;
706         }
707       }
708       if($skip){
709         unset($this->deps_to_migrate[$key]);
710       }
711     }
713     /* If we have no invisible departments found  
714      *  tell the user that everything is ok 
715      */
716     if(!$res){
717       $this->checks['deps_visible']['STATUS']    = FALSE;
718       $this->checks['deps_visible']['STATUS_MSG']= _("LDAP query failed");
719       $this->checks['deps_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
720     }elseif(count($this->deps_to_migrate) == 0 ){
721       $this->checks['deps_visible']['STATUS']    = TRUE;
722       $this->checks['deps_visible']['STATUS_MSG']= _("Ok");
723       $this->checks['deps_visible']['ERROR_MSG'] = "";
724     }else{
725       $this->checks['deps_visible']['STATUS']    = TRUE;
726       $this->checks['deps_visible']['STATUS_MSG']= '<font style="color:#FFA500">'._("Warning").'</font>';
727       $this->checks['deps_visible']['ERROR_MSG'] = sprintf(_("Found %s department(s) that will not be visible in GOsa."),count($this->deps_to_migrate));
728       $this->checks['deps_visible']['ERROR_MSG'] .= "&nbsp;<input type='submit' name='deps_visible_migrate' value='"._("Migrate")."...'>";
729     }
730   }
734   /* Start deparmtment migration */  
735   function migrate_organizationalUnits($only_ldif = FALSE)
736   {
737     $this->show_details= $only_ldif;
739     /* Get collected configuration settings */
740     $cv = $this->parent->captured_values;
742     /* Establish ldap connection */
743     $ldap = new LDAP($cv['admin'],
744         $cv['password'],
745         $cv['connection'],
746         FALSE,
747         $cv['tls']);
749     /* Add gosaDepartment objectClass to each selected entry 
750      */
751     foreach($this->deps_to_migrate as $key => $dep){
752       if($dep['checked']){
754         /* Get current objectClasses */
755         $ldap->cat($dep['dn'],array("objectClass","description"));
756         $attrs      = $ldap->fetch();
758         /* Create new objectClass attribute including gosaDepartment*/
759         $new_attrs  = array();
760         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
761           $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
762         }
763         $new_attrs['objectClass'][] = "gosaDepartment";
765         /* Append description it is missing */
766         if(!isset($attrs['description'])){
767           $new_attrs['description'][] = "GOsa department";
768         }
770         /* Depending on the parameter >only_diff< we save the changes as ldif
771          *  or we write our changes directly to the ldap database
772          */
773         if($only_ldif){
774           $this->deps_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
775           $this->deps_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
776         }else{
777           $ldap->cd($attrs['dn']);
778           if(!$ldap->modify($new_attrs)){
779             print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
780             return(false);
781           }
782         }
783       }
784     }
785     return(TRUE);
786   }
789   /* Check Acls if there is at least one object with acls defined 
790    */
791   function check_administrativeAccount()
792   {
793     /* Establish ldap connection */
794     $cv = $this->parent->captured_values;
795     $ldap = new LDAP($cv['admin'],
796         $cv['password'],
797         $cv['connection'],
798         FALSE,
799         $cv['tls']);
801     /* Search for gosaAcls */ 
802     $ldap->cd($cv['base']);
803     $res = $ldap->cat($cv['base']);
804     
805     if(!$res){
806       $this->checks['acls']['STATUS']    = FALSE;
807       $this->checks['acls']['STATUS_MSG']= _("LDAP query failed");
808       $this->checks['acls']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
809     }else{
810       $found = false;
811       $username = "";
812       $attrs = $ldap->fetch();
813       if(isset($attrs['gosaAclEntry'])){
814         $acls = $attrs['gosaAclEntry'];
815         for($i = 0 ; $i < $acls['count'] ; $i++){
816           $acl = $acls[$i];
817           $tmp = split(":",$acl);
818           if($tmp[1] == "psub"){
819             $members = split(",",$tmp[2]);
820             foreach($members as $member){
821               $member = base64_decode($member);
823               /* Check if acl owner is a valid GOsa user account */
824               $ldap->cat($member,array("objectClass","uid","cn"));
825               $ret = $ldap->fetch();
827               if(isset($ret['objectClass']) && in_array("posixGroup",$ret['objectClass'])){
828                 $found = TRUE;
829                 $username .= "ACL-Group:&nbsp;".$ret['cn'][0]."<br>";
830               }elseif(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
831                   in_array("organizationalPerson",$ret['objectClass']) &&
832                   in_array("inetOrgPerson",$ret['objectClass'])){
833                 $found = TRUE;
834                 $username .= "ACL:&nbsp;".$ret['uid'][0]."<br>";
835               }
836             }
837           }elseif($tmp[1] == "role"){
839             /* Check if acl owner is a valid GOsa user account */
840             $ldap->cat(base64_decode($tmp[2]),array("gosaAclTemplate"));
841             $ret = $ldap->fetch();
843             if(isset($ret['gosaAclTemplate'])){
844               $cnt = $ret['gosaAclTemplate']['count'];
845               for($e = 0 ; $e < $cnt ; $e++){
847                 $a_str = $ret['gosaAclTemplate'][$e];
848                 if(preg_match("/^[0-9]*:psub:/",$a_str) && preg_match("/:all;cmdrw$/",$a_str)){
850                   $members = split(",",$tmp[3]);
851                   foreach($members as $member){
852                     $member = base64_decode($member);
854                     /* Check if acl owner is a valid GOsa user account */
855                     $ldap->cat($member,array("objectClass","uid"));
856                     $ret = $ldap->fetch();
857   
858                     if(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
859                         in_array("organizationalPerson",$ret['objectClass']) &&
860                         in_array("inetOrgPerson",$ret['objectClass'])){
861                       $found = TRUE;
862                       $username .= "ACL Role:&nbsp;".$ret['uid'][0]."<br>";
863                     }
864                   }
865                 }
866               }
867             }
868           }
869         }
870       }
872       # For debugging
873       #echo $username;
875       if($found){
876         $this->checks['acls']['STATUS']    = TRUE;
877         $this->checks['acls']['STATUS_MSG']= _("Ok");
878         $this->checks['acls']['ERROR_MSG'] = "";
879       }else{
880         $this->checks['acls']['STATUS']    = FALSE;
881         $this->checks['acls']['STATUS_MSG']= _("Failed");
882         $this->checks['acls']['ERROR_MSG']= _("There is no GOsa administrator account inside your LDAP.")."&nbsp;";
883         $this->checks['acls']['ERROR_MSG'].= "<input type='submit' name='create_acls' value='"._("Create")."'>";
884       }
885     }
886     return($ldap->count()>=1);
887   }
891   function create_admin($only_ldif = FALSE)
892   {
893     /* Reset '' */
894     $this->acl_create_changes="";
896     /* Object that should receive admin acls */
897     $dn = $this->acl_create_selected;
899     /* Get collected configuration settings */
900     $cv = $this->parent->captured_values;
902     /* Establish ldap connection */
903     $ldap = new LDAP($cv['admin'],
904         $cv['password'],
905         $cv['connection'],
906         FALSE,
907         $cv['tls']);
909     /* Get current base attributes */
910     $ldap->cd($cv['base']);
911     $ldap->cat($cv['base'],array("dn","objectClass","gosaAclEntry"));
912     $attrs = $ldap->fetch();
914     /* Add acls for the selcted user to the base */
915     $attrs_new['objectClass'] = array("gosaACL");
917     for($i = 0; $i < $attrs['objectClass']['count']; $i ++){
918       if(!in_array_ics($attrs['objectClass'][$i],$attrs_new['objectClass'])){
919         $attrs_new['objectClass'][] = $attrs['objectClass'][$i];
920       }
921     }
923     $acl = "0:psub:".base64_encode($dn).":all;cmdrw";    
924     $attrs_new['gosaAclEntry'][] = $acl;
925     if(isset($attrs['gosaAclEntry'])){
926       for($i = 0 ; $i < $attrs['gosaAclEntry']['count']; $i ++){
927           
928         $prio = preg_replace("/[:].*$/","",$attrs['gosaAclEntry'][$i]);
929         $rest = preg_replace("/^[^:]/","",$attrs['gosaAclEntry'][$i]);
930  
931         $data = ($prio+1).$rest;
932         $attrs_new['gosaAclEntry'][] = $data;
933       }
934     }
936     if($only_ldif){
937       $this->acl_create_changes ="\n".($ldap->fix($cv['base']))."\n";
938       $this->acl_create_changes.=$this->array_to_ldif($attrs)."\n";
939       $this->acl_create_changes.="\n".($ldap->fix($cv['base']))."\n";
940       $this->acl_create_changes.=$this->array_to_ldif($attrs_new);
941     }else{
942    
943       $ldap->cd($cv['base']);
944       if(!$ldap->modify($attrs_new)){
945         print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
946         return(FALSE);
947       }else{
948         return(TRUE);
949       }
950     }
951   }
952  
953   
954   function create_admin_user()
955   {
956     $pw1 = $pw2 = "";
957     $uid = "";
958   
959     if(isset($_POST['new_user_uid'])){
960       $uid = $_POST['new_user_uid'];
961     }
962     if(isset($_POST['new_user_password'])){
963       $pw1 = $_POST['new_user_password'];
964     }
965     if(isset($_POST['new_user_password2'])){
966       $pw2 = $_POST['new_user_password2'];
967     }
968   
969     if(empty($pw1) || empty($pw2) | ($pw1 != $pw2)){
970       print_red(_("Specified passwords are empty or not equal."));
971       return false;
972     }
973  
974     if(!is_uid($uid) || empty($uid)){
975       print_red(_("Please specify a valid uid."));
976       return false;
977     }
978     
980     /* Establish ldap connection */
981     $cv = $this->parent->captured_values;
982     $ldap = new LDAP($cv['admin'],
983         $cv['password'],
984         $cv['connection'],
985         FALSE,
986         $cv['tls']);
988     /* Get current base attributes */
989     $ldap->cd($cv['base']);
990   
991     $people_ou = trim($cv['peopleou']);
992     if(!empty($people_ou)){
993       $people_ou = trim($people_ou).",";
994     }
996     if($cv['peopledn'] == "cn"){
997       $dn = "cn=System Administrator,".$people_ou.$cv['base'];
998     }else{
999       $dn = "uid=".$uid.",".$people_ou.$cv['base'];
1000     }
1002     $methods = @passwordMethod::get_available_methods();
1003     $p_m = $methods[$cv['encryption']];
1004     $p_c = new $p_m(array());
1005     $hash = $p_c->generate_hash($pw2);
1007     $new_user=array();
1008     $new_user['objectClass']= array("top","person","gosaAccount","organizationalPerson","inetOrgPerson");
1009     $new_user['givenName']  = "System";
1010     $new_user['sn']  = "Administrator";
1011     $new_user['cn']  = "System Administrator";
1012     $new_user['uid'] = $uid;
1013     $new_user['userPassword'] = $hash;
1014    
1015     $ldap->cd($cv['base']);
1016   
1017     $ldap->cat($dn,array("dn"));
1018     if($ldap->count()){
1019       print_red(sprintf(_("Could not add administrative user, there is already an object with the same dn '%s' in your ldap database."),$dn));
1020       return(FALSE);  
1021     }
1023     $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$dn));
1024     $ldap->cd($dn);  
1025     $res = $ldap->add($new_user);
1026     $this->acl_create_selected = $dn;
1027     $this->create_admin();
1028     
1029     if(!$res){
1030       print_red($ldap->get_error());
1031       return(FALSE);
1032     }
1033   
1034     $this->acl_create_dialog=FALSE;        
1035     $this->check_administrativeAccount();
1036     return(TRUE);
1037   }
1038  
1040   function migrate_outside_winstations($perform = FALSE)
1041   {
1042     /* Establish ldap connection */
1043     $cv = $this->parent->captured_values;
1044     $ldap = new LDAP($cv['admin'],
1045         $cv['password'],
1046         $cv['connection'],
1047         FALSE,
1048         $cv['tls']);
1050     $ldap->cd($cv['base']);
1052     /* Check if there was a destination department posted */
1053     if(isset($_POST['move_winstation_to'])){
1054       $destination_dep = $_POST['move_winstation_to'];
1055     }else{
1056       print_red(_("Couldn't move users to specified department."));
1057       return(false);
1058     }
1059  
1060     foreach($this->outside_winstations as $b_dn => $data){
1061       $this->outside_winstations[$b_dn]['ldif'] ="";
1062       if($data['selected']){
1063         $dn = base64_decode($b_dn);
1064         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1065         if(!$perform){
1066           $this->outside_winstations[$b_dn]['ldif'] = _("Winstation will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));
1069           /* Check if there are references to this object */
1070           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1071           $refs = "";
1072           while($attrs = $ldap->fetch()){
1073             $ref_dn = $attrs['dn'];
1074             $refs .= "<br />\t".$ref_dn;
1075           } 
1076           if(!empty($refs)){ 
1077             $this->outside_winstations[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1078           }
1080         }else{
1081           $this->move($dn,$d_dn);
1082         }
1083       }
1084     }
1085   }
1086   
1088   function migrate_outside_groups($perform = FALSE)
1089   {
1090     /* Establish ldap connection */
1091     $cv = $this->parent->captured_values;
1092     $ldap = new LDAP($cv['admin'],
1093         $cv['password'],
1094         $cv['connection'],
1095         FALSE,
1096         $cv['tls']);
1098     $ldap->cd($cv['base']);
1100     /* Check if there was a destination department posted */
1101     if(isset($_POST['move_group_to'])){
1102       $destination_dep = $_POST['move_group_to'];
1103     }else{
1104       print_red(_("Couldn't move users to specified department."));
1105       return(false);
1106     }
1107  
1108     foreach($this->outside_groups as $b_dn => $data){
1109       $this->outside_groups[$b_dn]['ldif'] ="";
1110       if($data['selected']){
1111         $dn = base64_decode($b_dn);
1112         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1113         if(!$perform){
1115           $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));
1117           /* Check if there are references to this object */
1118           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1119           $refs = "";
1120           while($attrs = $ldap->fetch()){
1121             $ref_dn = $attrs['dn'];
1122             $refs .= "<br />\t".$ref_dn;
1123           } 
1124           if(!empty($refs)){ 
1125             $this->outside_groups[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1126           }
1128         }else{
1129           $this->move($dn,$d_dn);
1130         }
1131       }
1132     }
1133   }
1134   
1136   function migrate_outside_users($perform = FALSE)
1137   {
1138     /* Establish ldap connection */
1139     $cv = $this->parent->captured_values;
1140     $ldap = new LDAP($cv['admin'],
1141         $cv['password'],
1142         $cv['connection'],
1143         FALSE,
1144         $cv['tls']);
1146     $ldap->cd($cv['base']);
1148     /* Check if there was a destination department posted */
1149     if(isset($_POST['move_user_to'])){
1150       $destination_dep = $_POST['move_user_to'];
1151     }else{
1152       print_red(_("Couldn't move users to specified department."));
1153       return(false);
1154     }
1155       
1156     foreach($this->outside_users as $b_dn => $data){
1157       $this->outside_users[$b_dn]['ldif'] ="";
1158       if($data['selected']){
1159         $dn = base64_decode($b_dn);
1160         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1161         if(!$perform){
1162           $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));
1164           /* Check if there are references to this object */
1165           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1166           $refs = "";
1167           while($attrs = $ldap->fetch()){
1168             $ref_dn = $attrs['dn'];
1169             $refs .= "<br />\t".$ref_dn;
1170           } 
1171           if(!empty($refs)){ 
1172             $this->outside_users[$b_dn]['ldif'] .= "<br /><br /><i>"._("The following references will be updated").":</i>".$refs;
1173           }
1175         }else{
1176           $this->move($dn,$d_dn);
1177         }
1178       }
1179     }
1180   }
1181   
1183   function execute()
1184   {
1185     /* Initialise checks if this is the first call */
1186     if(!$this->checks_initialised || isset($_POST['reload'])){
1187       $this->initialize_checks();
1188       $this->checks_initialised = TRUE;
1189     }
1191     /*************
1192      * Winstations outside the group ou 
1193      *************/
1194     
1195     if(isset($_POST['outside_winstations_dialog_cancel'])){
1196       $this->outside_winstations_dialog = FALSE;
1197       $this->dialog = FALSE;
1198       $this->show_details = FALSE;
1199     }
1200    
1201     if(isset($_POST['outside_winstations_dialog_whats_done'])){
1202       $this->migrate_outside_winstations(FALSE);
1203     }
1204  
1205     if(isset($_POST['outside_winstations_dialog_perform'])){
1206       $this->migrate_outside_winstations(TRUE);
1207       $this->search_outside_winstations();
1208       $this->dialog = FALSE;
1209       $this->show_details = FALSE;
1210       $this->outside_winstations_dialog = FALSE;
1211     }
1213     if(isset($_POST['outside_winstations_dialog'])){
1214       $this->outside_winstations_dialog = TRUE;
1215       $this->dialog = TRUE;
1216     }
1217     
1218     if($this->outside_winstations_dialog){
1220       /* Fix displayed dn syntax */ 
1221       $tmp = $this->outside_winstations;
1222       foreach($tmp as $key => $data){
1223         $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
1224       }
1226       $smarty = get_smarty();
1227       $smarty->assign("ous",$this->get_all_winstation_ous());
1228       $smarty->assign("method","outside_winstations");
1229       $smarty->assign("outside_winstations",$tmp);
1230       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1231     }
1232     /*************
1233      * Groups outside the group ou 
1234      *************/
1235     
1236     if(isset($_POST['outside_groups_dialog_cancel'])){
1237       $this->outside_groups_dialog = FALSE;
1238       $this->show_details = FALSE;
1239       $this->dialog = FALSE;
1240     }
1241    
1242     if(isset($_POST['outside_groups_dialog_whats_done'])){
1243       $this->show_details= TRUE;
1244       $this->migrate_outside_groups(FALSE);
1245     }
1246  
1247     if(isset($_POST['outside_groups_dialog_refresh'])){
1248       $this->show_details= FALSE;
1249     }
1251     if(isset($_POST['outside_groups_dialog_perform'])){
1252       $this->migrate_outside_groups(TRUE);
1253       $this->dialog = FALSE;
1254       $this->show_details = FALSE;
1255       $this->outside_groups_dialog = FALSE;
1256       $this->initialize_checks();
1257     }
1259     if(isset($_POST['outside_groups_dialog'])){
1260       $this->outside_groups_dialog = TRUE;
1261       $this->dialog = TRUE;
1262     }
1263     
1264     if($this->outside_groups_dialog){
1266       /* Fix displayed dn syntax */ 
1267       $tmp = $this->outside_groups;
1268       foreach($tmp as $key => $data){
1269         $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
1270       }
1272       $smarty = get_smarty();
1273       $smarty->assign("ous",$this->get_all_group_ous());
1274       $smarty->assign("method","outside_groups");
1275       $smarty->assign("outside_groups",$tmp);
1276       $smarty->assign("group_details", $this->show_details);
1277       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1278     }
1279  
1280     /*************
1281      * User outside the people ou 
1282      *************/
1283     
1284     if(isset($_POST['outside_users_dialog_cancel'])){
1285       $this->outside_users_dialog = FALSE;
1286       $this->dialog = FALSE;
1287       $this->show_details = FALSE;
1288     }
1289    
1290     if(isset($_POST['outside_users_dialog_whats_done'])){
1291       $this->show_details= TRUE;
1292       $this->migrate_outside_users(FALSE);
1293     }
1294  
1295     if(isset($_POST['outside_users_dialog_perform'])){
1296       $this->migrate_outside_users(TRUE);
1297       $this->initialize_checks();
1298       $this->dialog = FALSE;
1299       $this->show_details = FALSE;
1300       $this->outside_users_dialog = FALSE;
1301     }
1303     if (isset($_POST['outside_users_dialog_refresh'])){
1304       $this->show_details= FALSE;
1305     }
1307     if(isset($_POST['outside_users_dialog'])){
1308       $this->outside_users_dialog = TRUE;
1309       $this->dialog = TRUE;
1310     }
1311     
1312     if($this->outside_users_dialog){
1314       /* Fix displayed dn syntax */ 
1315       $tmp = $this->outside_users;
1316       foreach($tmp as $key => $data){
1317         $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
1318       }
1320       $smarty = get_smarty();
1321       $smarty->assign("ous",$this->get_all_people_ous());
1322       $smarty->assign("method","outside_users");
1323       $smarty->assign("outside_users",$tmp);
1324       $smarty->assign("user_details", $this->show_details);
1325       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1326     }
1327  
1328     /*************
1329      * Root object check  
1330      *************/
1331   
1332     if(isset($_POST['retry_root_create'])){
1334       $state = $this->checks['root']['STATUS'];
1335       $this->checkBase(FALSE);
1336       if($state != $this->checks['root']['STATUS']){
1337         $this->initialize_checks();
1338       }
1339     }
1340  
1341     /*************
1342      * User Migration handling 
1343      *************/
1345     if(isset($_POST['retry_acls'])){
1346       $this->check_administrativeAccount();
1347     }
1349     if(isset($_POST['create_acls'])){
1350       $this->acl_create_dialog = TRUE;
1351       $this->dialog = TRUE;
1352     }
1353   
1354     if(isset($_POST['create_acls_cancel'])){
1355       $this->acl_create_dialog = FALSE;
1356       $this->dialog = FALSE;
1357       $this->show_details = FALSE;
1358     }
1360 #    if(isset($_POST['create_acls_create_confirmed'])){
1361 #      if($this->create_admin()){
1362 #        $this->acl_create_dialog = FALSE;
1363 #        $this->dialog = FALSE;
1364 #      $this->show_details = FALSE;
1365 #        $this->initialize_checks();
1366 #      }
1367 #    }
1369     if(isset($_POST['create_acls_create'])){
1370       $this->create_admin(TRUE);
1371     }
1373     if(isset($_POST['create_admin_user'])){
1374       if($this->create_admin_user()){
1375         $this->dialog = FALSE;
1376       $this->show_details = FALSE;
1377       }
1378     }
1380     if($this->acl_create_dialog){
1381       $smarty = get_smarty();
1382   
1383       $uid = "admin";
1384       if(isset($_POST['new_user_uid'])){
1385         $uid = $_POST['new_user_uid'];
1386       }
1388       $smarty->assign("new_user_uid",$uid);
1389       $smarty->assign("new_user_password",@$_POST['new_user_password']);
1390       $smarty->assign("new_user_password2",@$_POST['new_user_password2']);
1391       $smarty->assign("method","create_acls");
1392       $smarty->assign("acl_create_selected",$this->acl_create_selected);
1393       $smarty->assign("what_will_be_done_now",$this->acl_create_changes);
1394       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1395     }
1397     /*************
1398      * User Migration handling 
1399      *************/
1401     /* Refresh list of deparments */
1402     if(isset($_POST['users_visible_migrate_refresh'])){
1403       $this->check_gosaAccounts();
1404     }
1406     /* Open migration dialog */
1407     if(isset($_POST['users_visible_migrate'])){
1408       $this->show_details= FALSE;
1409       $this->users_migration_dialog = TRUE;
1410       $this->dialog =TRUE;
1411     }
1413     /* Close migration dialog */
1414     if(isset($_POST['users_visible_migrate_close'])){
1415       $this->users_migration_dialog = FALSE;
1416       $this->dialog =FALSE;
1417       $this->show_details = FALSE;
1418     }
1420     /* Start migration */
1421     if(isset($_POST['users_visible_migrate_migrate'])){
1422       if($this->migrate_gosaAccounts()){
1423         $this->initialize_checks();
1424         $this->dialog = FALSE;
1425         $this->show_details = FALSE;
1426         $this->users_migration_dialog = FALSE;
1427       }
1428     }
1430     /* Start migration */
1431     if(isset($_POST['users_visible_migrate_whatsdone'])){
1432       $this->migrate_gosaAccounts(TRUE);
1433     }
1435     /* Display migration dialog */
1436     if($this->users_migration_dialog){
1438       /* Fix displayed dn syntax */ 
1439       $tmp = $this->users_to_migrate;
1440       foreach($tmp as $key => $data){
1441         $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
1442       }
1444       $smarty = get_smarty();
1445       $smarty->assign("users_to_migrate",$tmp);
1446       $smarty->assign("method","migrate_users");
1447       $smarty->assign("user_details", $this->show_details);
1448       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1449     }
1452     /*************
1453      * Department Migration handling 
1454      *************/
1456     /* Refresh list of deparments */
1457     if(isset($_POST['deps_visible_migrate_refresh'])){
1458       $this->check_organizationalUnits();
1459       $this->show_details= FALSE;
1460     }
1462     /* Open migration dialog */
1463     if(isset($_POST['deps_visible_migrate'])){
1464       $this->dep_migration_dialog = TRUE;
1465       $this->dialog =TRUE;
1466     }
1468     /* Close migration dialog */
1469     if(isset($_POST['deps_visible_migrate_close'])){
1470       $this->dep_migration_dialog = FALSE;
1471       $this->dialog =FALSE;
1472       $this->show_details = FALSE;
1473     }
1475     /* Start migration */
1476     if(isset($_POST['deps_visible_migrate_migrate'])){
1477       if($this->migrate_organizationalUnits()){
1478         $this->show_details= FALSE;
1479         $this->check_organizationalUnits();
1480         $this->dialog = FALSE;
1481         $this->dep_migration_dialog = FALSE;
1482       }
1483     }
1485     /* Start migration */
1486     if(isset($_POST['deps_visible_migrate_whatsdone'])){
1487       $this->migrate_organizationalUnits(TRUE);
1488     }
1490     /* Display migration dialog */
1491     if($this->dep_migration_dialog){
1492       $smarty = get_smarty();
1493    
1494       /* Fix displayed dn syntax */ 
1495       $tmp = $this->deps_to_migrate;
1496       foreach($tmp as $key => $data){
1497         $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
1498       }
1500       $smarty->assign("deps_to_migrate",$tmp);
1501       $smarty->assign("method","migrate_deps");
1502       $smarty->assign("deps_details", $this->show_details);
1503       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1504     }
1506     $smarty = get_smarty();
1507     $smarty->assign("checks",$this->checks);
1508     $smarty->assign("method","default");
1509     return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1510   }
1513   function save_object()
1514   {
1515     $this->is_completed= TRUE;
1517     /* Capture all selected winstations from outside_winstations_dialog */
1518     if($this->outside_winstations_dialog){
1519       foreach($this->outside_winstations as $dn => $data){
1520         if(isset($_POST['select_winstation_'.$dn])){
1521           $this->outside_winstations[$dn]['selected'] = TRUE;
1522         }else{
1523           $this->outside_winstations[$dn]['selected'] = FALSE;
1524         }
1525       }
1526     }
1528     /* Capture all selected groups from outside_groups_dialog */
1529     if($this->outside_groups_dialog){
1530       foreach($this->outside_groups as $dn => $data){
1531         if(isset($_POST['select_group_'.$dn])){
1532           $this->outside_groups[$dn]['selected'] = TRUE;
1533         }else{
1534           $this->outside_groups[$dn]['selected'] = FALSE;
1535         }
1536       }
1537     }
1539     /* Capture all selected users from outside_users_dialog */
1540     if($this->outside_users_dialog){
1541       foreach($this->outside_users as $dn => $data){
1542         if(isset($_POST['select_user_'.$dn])){
1543           $this->outside_users[$dn]['selected'] = TRUE;
1544         }else{
1545           $this->outside_users[$dn]['selected'] = FALSE;
1546         }
1547       }
1548     }
1550     /* Get "create acl" dialog posts */
1551     if($this->acl_create_dialog){
1553       if(isset($_POST['create_acls_create_abort'])){
1554         $this->acl_create_selected = "";
1555       }
1556     }
1558     /* Get selected departments */
1559     if($this->dep_migration_dialog){
1560       foreach($this->deps_to_migrate as $id => $data){
1561         if(isset($_POST['migrate_'.$id])){
1562           $this->deps_to_migrate[$id]['checked'] = TRUE;
1563         }else{
1564           $this->deps_to_migrate[$id]['checked'] = FALSE;
1565         }
1566       }
1567     }
1569     /* Get selected users */
1570     if($this->users_migration_dialog){
1571       foreach($this->users_to_migrate as $id => $data){
1572         if(isset($_POST['migrate_'.$id])){
1573           $this->users_to_migrate[$id]['checked'] = TRUE;
1574         }else{
1575           $this->users_to_migrate[$id]['checked'] = FALSE;
1576         }
1577       }
1578     }
1579   }
1582   /* Check if the root object exists.
1583    * If the parameter just_check is true, then just check if the 
1584    *  root object is missing and update the info messages.
1585    * If the Parameter is false, try to create a new root object.
1586    */
1587   function checkBase($just_check = TRUE)
1588   {
1589     /* Get collected setup informations */
1590     $cv = $this->parent->captured_values;
1592     /* Establish ldap connection */
1593     $ldap = new LDAP($cv['admin'],
1594         $cv['password'],
1595         $cv['connection'],
1596         FALSE,
1597         $cv['tls']);
1599     /* Check if root object exists */
1600     $ldap->cd($cv['base']);
1601     $res = $ldap->search("(objectClass=*)");
1602     $err = ldap_errno($ldap->cid); 
1604     if( !$res || 
1605         $err == 0x20 ||  # LDAP_NO_SUCH_OBJECT
1606         $err == 0x40) {  # LDAP_NAMING_VIOLATION
1608       /* Root object doesn't exists 
1609        */
1610       if($just_check){
1611         $this->checks['root']['STATUS']    = FALSE;
1612         $this->checks['root']['STATUS_MSG']= _("Failed");
1613         $this->checks['root']['ERROR_MSG'] =  _("The LDAP root object is missing. It is required to use your LDAP service.").'&nbsp;';
1614         $this->checks['root']['ERROR_MSG'].=  "<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1615         return(FALSE);
1616       }else{
1618         /* Add root object */ 
1619         $ldap->cd($cv['base']);
1620         $res = $ldap->create_missing_trees($cv['base']);
1622         /* If adding failed, tell the user */
1623         if(!$res){
1624           $this->checks['root']['STATUS']    = FALSE;
1625           $this->checks['root']['STATUS_MSG']= _("Failed");
1626           $this->checks['root']['ERROR_MSG'] = _("Root object couldn't be created, you should try it on your own.");
1627           $this->checks['root']['ERROR_MSG'].= "&nbsp;<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1628           return($res);;
1629         }
1630       }
1631     }
1633     /* Create & remove of dummy object was successful */
1634     $this->checks['root']['STATUS']    = TRUE;
1635     $this->checks['root']['STATUS_MSG']= _("Ok");
1636   }
1639   /* Return ldif information for a 
1640    * given attribute array 
1641    */
1642   function array_to_ldif($atts)
1643   {
1644     $ret = "";
1645     unset($atts['count']);
1646     unset($atts['dn']);
1647     foreach($atts as $name => $value){
1648       if(is_numeric($name)) {
1649         continue;
1650       }
1651       if(is_array($value)){
1652         unset($value['count']);
1653         foreach($value as $a_val){
1654           $ret .= $name.": ". $a_val."\n";
1655         }
1656       }else{
1657         $ret .= $name.": ". $value."\n";
1658       }
1659     }
1660     return(preg_replace("/\n$/","",$ret));
1661   }
1664   function get_user_list()
1665   {
1666     /* Get collected configuration settings */
1667     $cv = $this->parent->captured_values;
1669     /* Establish ldap connection */
1670     $ldap = new LDAP($cv['admin'],
1671         $cv['password'],
1672         $cv['connection'],
1673         FALSE,
1674         $cv['tls']);
1675     
1676     $ldap->cd($cv['base']);
1677     $ldap->search("(objectClass=gosaAccount)",array("dn"));
1678   
1679     $tmp = array();
1680     while($attrs = $ldap->fetch()){
1681       $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1682     }
1683     return($tmp);
1684   }
1687  function get_all_people_ous()
1688   {
1689     /* Get collected configuration settings */
1690     $cv = $this->parent->captured_values;
1691     $people_ou = trim($cv['peopleou']);
1693     /* Establish ldap connection */
1694     $ldap = new LDAP($cv['admin'],
1695         $cv['password'],
1696         $cv['connection'],
1697         FALSE,
1698         $cv['tls']);
1701     /*****************
1702      * If people ou is NOT empty
1703      * search for for all objects matching the given container
1704      *****************/
1705     if(!empty($people_ou)){
1706       $ldap->search("(".$people_ou.")",array("dn"));
1708       /* Create people ou if there is currently none */
1709       if($ldap->count() == 0 ){
1710         $add_dn = $cv['peopleou'].",".$cv['base'];
1711         $naming_attr = preg_replace("/=.*$/","",$add_dn);
1712         $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1713         $add = array();
1714         $add['objectClass'] = array("organizationalUnit");
1715         $add[$naming_attr] = $naming_value;
1716         $ldap->cd($cv['base']);
1717         $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1718         $ldap->cd($add_dn);
1719         $ldap->add($add);
1720       }
1722       /* Create result */
1723       $ldap->search("(".$cv['peopleou'].")",array("dn"));
1724       $tmp = array();
1725       while($attrs= $ldap->fetch()){
1726         if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1727           $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1728         }
1729       }
1730     } else{
1732       /************
1733        * If people ou is empty
1734        * Get all valid gosaDepartments
1735        ************/
1736       $ldap->cd($cv['base']);
1737       $tmp = array();
1738       $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
1739       $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
1740       while($attrs = $ldap->fetch()){
1741         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
1742       }
1743     }
1744     return($tmp);
1745   }
1748   function get_all_winstation_ous()
1749   {
1750     /* Get collected configuration settings */
1751     $cv = $this->parent->captured_values;
1753     /* Establish ldap connection */
1754     $ldap = new LDAP($cv['admin'],
1755         $cv['password'],
1756         $cv['connection'],
1757         FALSE,
1758         $cv['tls']);
1760     /* Get winstation ou */
1761     if($cv['generic_settings']['wws_ou_active']) {
1762       $winstation_ou = $cv['generic_settings']['ws_ou'];
1763     }else{
1764       $winstation_ou = "ou=winstations";
1765     }
1767     $ldap->cd($cv['base']);
1768     $ldap->search("(".$winstation_ou.")",array("dn"));
1769   
1770     if($ldap->count() == 0 ){
1771       $add_dn = $winstation_ou.",ou=systems,".$cv['base'];
1772       $naming_attr = preg_replace("/=.*$/","",$add_dn);
1773       $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1774       $add = array();
1775       $add['objectClass'] = array("organizationalUnit");
1776       $add[$naming_attr] = $naming_value;
1778       $ldap->cd($cv['base']);
1779       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1780       $ldap->cd($add_dn);
1781       $ldap->add($add);
1782     }
1784     $ldap->search("(".$winstation_ou.")",array("dn"));
1785     $tmp = array();
1786     while($attrs= $ldap->fetch()){
1787       if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1788         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1789       }
1790     }
1791     return($tmp); 
1792   }
1795  function get_all_group_ous()
1796   {
1797     /* Get collected configuration settings */
1798     $cv = $this->parent->captured_values;
1800     /* Establish ldap connection */
1801     $ldap = new LDAP($cv['admin'],
1802         $cv['password'],
1803         $cv['connection'],
1804         FALSE,
1805         $cv['tls']);
1807     $group_ou = trim($cv['groupou']);
1808     if(!empty($group_ou)){
1809       $group_ou = trim($group_ou);
1810     }
1812     /************
1813      * If group ou is NOT empty
1814      * Get all valid group ous, create one if necessary
1815      ************/
1816     $ldap->cd($cv['base']);
1817     if(!empty($group_ou)){
1818       $ldap->search("(".$group_ou.")",array("dn"));
1819       if($ldap->count() == 0 ){
1820         $add_dn = $group_ou.$cv['base'];
1821         $naming_attr = preg_replace("/=.*$/","",$add_dn);
1822         $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1823         $add = array();
1824         $add['objectClass'] = array("organizationalUnit");
1825         $add[$naming_attr] = $naming_value;
1827         $ldap->cd($cv['base']);
1828         $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1829         $ldap->cd($add_dn);
1830         $ldap->add($add);
1831       }
1832       $ldap->search("(".$group_ou.")",array("dn"));
1833       $tmp = array();
1834       while($attrs= $ldap->fetch()){
1835         if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1836           $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1837         }
1838       }
1839     }else{
1840       /************
1841        * If group ou is empty
1842        * Get all valid gosaDepartments
1843        ************/
1844       $ldap->cd($cv['base']);
1845       $tmp = array();
1846       $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
1847       $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
1848       while($attrs = $ldap->fetch()){
1849         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
1850       }
1851     }
1852     return($tmp);
1853   }
1856   function get_group_list()
1857   {
1858     /* Get collected configuration settings */
1859     $cv = $this->parent->captured_values;
1861     /* Establish ldap connection */
1862     $ldap = new LDAP($cv['admin'],
1863         $cv['password'],
1864         $cv['connection'],
1865         FALSE,
1866         $cv['tls']);
1867     
1868     $ldap->cd($cv['base']);
1869     $ldap->search("(objectClass=posixGroup)",array("dn"));
1870   
1871     $tmp = array();
1872     while($attrs = $ldap->fetch()){
1873       $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1874     }
1875     return($tmp);
1876   }
1879   function move($source,$destination)
1880   {
1881     /* Get collected configuration settings */
1882     $cv = $this->parent->captured_values;
1884     /* Establish ldap connection */
1885     $ldap = new LDAP($cv['admin'],
1886         $cv['password'],
1887         $cv['connection'],
1888         FALSE,
1889         $cv['tls']);
1891      /* Update object references in gosaGroupOfNames */
1892     $ogs_to_fix = array();
1893     $ldap->cd($cv['base']);
1894     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($source).'))', array('cn','member'));
1895     while ($attrs= $ldap->fetch()){
1896       $dn = $attrs['dn'];
1897       $attrs = $this->cleanup_array($attrs);
1898       $member_new = array($destination);
1899       foreach($attrs['member'] as $member){
1900         if($member != $source){
1901           $member_new[] = $member;
1902         }
1903       }
1904       $attrs['member'] = $member_new;
1905       $ogs_to_fix[$dn] = $attrs;
1906     }
1908     /* Copy source to destination dn */
1909     $ldap->cat($source);
1910     $new_data = $this->cleanup_array($ldap->fetch());
1911     $ldap->cd($destination);
1912     $res = $ldap->add($new_data);
1914     /* Display warning if copy failed */
1915     if(!$res){
1916       print_red(_("Failed to copy '%s' to '%s'. LDAP says '%s'."),$source,$destination,$ldap->get_error());
1917     }else{
1918       $res = $ldap->rmDir($source);
1919       show_ldap_error($ldap->get_error(),_("Something went wrong while copying dns."));
1921       /* Object is copied, so update its references */
1922       foreach($ogs_to_fix as $dn => $data){
1923         $ldap->cd($dn);
1924         $ldap->modify($data);
1925       }
1926     }
1927   }
1929   
1930   /* Cleanup ldap result to be able to write it be to ldap */
1931   function cleanup_array($attrs)
1932   {
1933     foreach($attrs as $key => $value) {
1934       if(is_numeric($key) || in_array($key,array("count","dn"))){
1935         unset($attrs[$key]);
1936       }
1937       if(is_array($value) && isset($value['count'])){
1938         unset($attrs[$key]['count']);
1939       }
1940     }
1941     return($attrs);
1942   }
1945 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1946 ?>