Code

Reset show detail status, if subdialog is closed
[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_type    = "group";
71   var $acl_create_selected= ""; // Currently selected element, that should receive admin rights 
72   var $acl_create_changes = ""; // Contains ldif information about changes 
73   var $acl_create_confirmed= FALSE;
75   /* Checks initialised ? */
76   var $checks_initialised = FALSE;
78   /* Users outside to people ou */
79   var $outside_users        = array();
80   var $outside_users_dialog = FALSE;
82   /* Users outside to groups ou */
83   var $outside_groups        = array();
84   var $outside_groups_dialog = FALSE;
86   /* Win-Workstations outside to reserved ou */
87   var $outside_winstations        = array();
88   var $outside_winstations_dialog = FALSE;
90   /* check for multiple use of same uidNumber */
91   var $check_uidNumbers        = array();
92   var $check_uidNumbers_dialog = FALSE;
94   /* check for multiple use of same gidNumber */
95   var $check_gidNumbers        = array();
96   var $check_gidNumbers_dialog = FALSE;
99   function Step_Migrate()
100   {
101     $this->update_strings(); 
102   }
104   function update_strings()
105   {
106     $this->s_title      = _("LDAP inspection");
107     $this->s_title_long = _("LDAP inspection");
108     $this->s_info       = _("Analyze your current LDAP for GOsa compatibility");
109   }
111   function initialize_checks()
112   {
113     $this->checks = array();
114     $this->checks['root']['TITLE']     = _("Checking for root object");
115     $this->checks['root']['STATUS']    = FALSE;
116     $this->checks['root']['STATUS_MSG']= "";
117     $this->checks['root']['ERROR_MSG'] = "";
118     $this->checkBase();
120     $this->checks['permissions']['TITLE']     = _("Checking permissions on LDAP database");
121     $this->checks['permissions']['STATUS']    = FALSE;
122     $this->checks['permissions']['STATUS_MSG']= "";
123     $this->checks['permissions']['ERROR_MSG'] = "";
124     $this->check_ldap_permissions();
126     $this->checks['deps_visible']['TITLE']     = _("Checking for invisible deparmtments");
127     $this->checks['deps_visible']['STATUS']    = FALSE;
128     $this->checks['deps_visible']['STATUS_MSG']= "";
129     $this->checks['deps_visible']['ERROR_MSG'] = "";
130     $this->check_organizationalUnits();
132     $this->checks['users_visible']['TITLE']     = _("Checking for invisible users");
133     $this->checks['users_visible']['STATUS']    = FALSE;
134     $this->checks['users_visible']['STATUS_MSG']= "";
135     $this->checks['users_visible']['ERROR_MSG'] = "";
136     $this->check_gosaAccounts();
138     $this->checks['acls']['TITLE']     = _("Checking for super administrator");
139     $this->checks['acls']['STATUS']    = FALSE;
140     $this->checks['acls']['STATUS_MSG']= "";
141     $this->checks['acls']['ERROR_MSG'] = "";
142     $this->check_administrativeAccount();
144     $this->checks['outside_users']['TITLE']     = _("Checking for users outside the people tree");
145     $this->checks['outside_users']['STATUS']    = FALSE;
146     $this->checks['outside_users']['STATUS_MSG']= "";
147     $this->checks['outside_users']['ERROR_MSG'] = "";
148     $this->search_outside_users();
149     
150     $this->checks['outside_groups']['TITLE']     = _("Checking for groups outside the groups tree");
151     $this->checks['outside_groups']['STATUS']    = FALSE;
152     $this->checks['outside_groups']['STATUS_MSG']= "";
153     $this->checks['outside_groups']['ERROR_MSG'] = "";
154     $this->search_outside_groups();
156     $this->checks['outside_winstations']['TITLE']     = _("Checking for windows workstations outside the winstation tree");
157     $this->checks['outside_winstations']['STATUS']    = FALSE;
158     $this->checks['outside_winstations']['STATUS_MSG']= "";
159     $this->checks['outside_winstations']['ERROR_MSG'] = "";
160     $this->search_outside_winstations();
162     $this->checks['uidNumber_usage']['TITLE']     = _("Checking for duplicate uid numbers");
163     $this->checks['uidNumber_usage']['STATUS']    = FALSE;
164     $this->checks['uidNumber_usage']['STATUS_MSG']= "";
165     $this->checks['uidNumber_usage']['ERROR_MSG'] = "";
166     $this->check_uidNumber();
167     
168     $this->checks['gidNumber_usage']['TITLE']     = _("Checking for duplicate gid numbers");
169     $this->checks['gidNumber_usage']['STATUS']    = FALSE;
170     $this->checks['gidNumber_usage']['STATUS_MSG']= "";
171     $this->checks['gidNumber_usage']['ERROR_MSG'] = "";
172     $this->check_gidNumber();
173   }
176   /* Check if there are uidNumbers which are used more than once. 
177    */
178   function check_uidNumber()
179   {
180     $cv = $this->parent->captured_values;
181     $ldap = new LDAP($cv['admin'],
182         $cv['password'],
183         $cv['connection'],
184         FALSE,
185         $cv['tls']);
187     $ldap->cd($cv['base']);
188     $res = $ldap->search("uidNumber=*",array("dn","uidNumber"));
189     if(!$res){
190       $this->checks['uidNumber_usage']['STATUS']    = FALSE;
191       $this->checks['uidNumber_usage']['STATUS_MSG']= _("LDAP query failed");
192       $this->checks['uidNumber_usage']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
193       return(false);
194     }
196     $this->check_uidNumbers= array(); 
197     $tmp = array();
198     while($attrs = $ldap->fetch()){
199       $tmp[$attrs['uidNumber'][0]][] = $attrs;
200     }
202     foreach($tmp as $id => $entries){
203       if(count($entries) > 1){
204         foreach($entries as $entry){
205           $this->check_uidNumbers[base64_encode($entry['dn'])] = $entry;
206         }
207       }
208     }
210     if($this->check_uidNumbers){
211       $this->checks['uidNumber_usage']['STATUS']    = FALSE;
212       $this->checks['uidNumber_usage']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
213       $this->checks['uidNumber_usage']['ERROR_MSG'] =
214         sprintf(_("Found %s duplicate values for attribute 'uidNumber'."),count($this->check_uidNumbers));
215       return(false);
216     }else{
217       $this->checks['uidNumber_usage']['STATUS']    = TRUE;
218       $this->checks['uidNumber_usage']['STATUS_MSG']= _("Ok");
219       $this->checks['uidNumber_usage']['ERROR_MSG'] = "";
220       return(TRUE);
221     }
222   }
224   
225   /* Check if there are duplicated gidNumbers present in ldap
226    */
227   function check_gidNumber()
228   {
229     $cv = $this->parent->captured_values;
230     $ldap = new LDAP($cv['admin'],
231         $cv['password'],
232         $cv['connection'],
233         FALSE,
234         $cv['tls']);
236     $ldap->cd($cv['base']);
237     $res = $ldap->search("(&(objectClass=posixGroup)(gidNumber=*))",array("dn","gidNumber"));
238     if(!$res){
239       $this->checks['gidNumber_usage']['STATUS']    = FALSE;
240       $this->checks['gidNumber_usage']['STATUS_MSG']= _("LDAP query failed");
241       $this->checks['gidNumber_usage']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
242       return(false);
243     }
245     $this->check_gidNumbers= array(); 
246     $tmp = array();
247     while($attrs = $ldap->fetch()){
248       $tmp[$attrs['gidNumber'][0]][] = $attrs;
249     }
251     foreach($tmp as $id => $entries){
252       if(count($entries) > 1){
253         foreach($entries as $entry){
254           $this->check_gidNumbers[base64_encode($entry['dn'])] = $entry;
255         }
256       }
257     }
259     if($this->check_gidNumbers){
260       $this->checks['gidNumber_usage']['STATUS']    = FALSE;
261       $this->checks['gidNumber_usage']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
262       $this->checks['gidNumber_usage']['ERROR_MSG'] =
263         sprintf(_("Found %s duplicate values for attribute 'gidNumber'."),count($this->check_gidNumbers));
264       return(false);
265     }else{
266       $this->checks['gidNumber_usage']['STATUS']    = TRUE;
267       $this->checks['gidNumber_usage']['STATUS_MSG']= _("Ok");
268       $this->checks['gidNumber_usage']['ERROR_MSG'] = "";
269       return(TRUE);
270     }
271   }
274   /* Search for winstations outside the winstation ou 
275    */
276   function search_outside_winstations()
277   {
278     $cv = $this->parent->captured_values;
279     $ldap = new LDAP($cv['admin'],
280         $cv['password'],
281         $cv['connection'],
282         FALSE,
283         $cv['tls']);
285     /* Get winstation ou */
286     if($cv['generic_settings']['wws_ou_active']) {
287       $winstation_ou = $cv['generic_settings']['ws_ou'];
288     }else{
289       $winstation_ou = "ou=winstations";
290     }
292     if($cv['samba_version'] == 3){
293       $oc = "sambaSamAccount";
294     }else{
295       $oc = "sambaAccount";
296     }
297  
298     $ldap->cd($cv['base']);
299     $res = $ldap->search("(&(objectClass=".$oc.")(uid=*$))",array("dn","sambaSID"));
300     if(!$res){
301       $this->checks['outside_winstations']['STATUS']    = FALSE;
302       $this->checks['outside_winstations']['STATUS_MSG']= _("LDAP query failed");
303       $this->checks['outside_winstations']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
304       return(false);
305     }
307     $this->outside_winstations = array();
308     while($attrs = $ldap->fetch()){
309       if((!preg_match("/^[^,]+,".normalizePreg($winstation_ou)."/",$attrs['dn'])) && !preg_match("/,dc=addressbook,/",$attrs['dn'])){
310         $attrs['selected'] = FALSE;
311         $attrs['ldif']     = "";
312         $this->outside_winstations[base64_encode($attrs['dn'])] = $attrs;
313       }
314     }
316     if(count($this->outside_winstations)){
317       $this->checks['outside_winstations']['STATUS']    = FALSE;
318       $this->checks['outside_winstations']['STATUS_MSG']= _("Failed");
319       $this->checks['outside_winstations']['ERROR_MSG'] = 
320         sprintf(_("Found %s winstations outside the predefined winstation department ou '%s'."),count($this->outside_winstations),$winstation_ou);
321       $this->checks['outside_winstations']['ERROR_MSG'].= "<input type='submit' name='outside_winstations_dialog' value='"._("Migrate")."...'>";
322       return(false);
323     }else{
324       $this->checks['outside_winstations']['STATUS']    = TRUE;
325       $this->checks['outside_winstations']['STATUS_MSG']= _("Ok");
326       $this->checks['outside_winstations']['ERROR_MSG'] = "";
327       return(TRUE);
328     }
329   }
332   /* Search for groups outside the group ou 
333    */
334   function search_outside_groups()
335   {
336     $cv = $this->parent->captured_values;
337     $ldap = new LDAP($cv['admin'],
338         $cv['password'],
339         $cv['connection'],
340         FALSE,
341         $cv['tls']);
343     $group_ou = $cv['groupou'];
344     $ldap->cd($cv['base']);
345     $res = $ldap->search("(objectClass=posixGroup)",array("dn"));
346     if(!$res){
347       $this->checks['outside_groups']['STATUS']    = FALSE;
348       $this->checks['outside_groups']['STATUS_MSG']= _("LDAP query failed");
349       $this->checks['outside_groups']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
350       return(false);
351     }
354     $this->outside_groups = array();
355     while($attrs = $ldap->fetch()){
356       if((!preg_match("/^[^,]+,".normalizePreg($group_ou)."/",$attrs['dn'])) && !preg_match("/,dc=addressbook,/",$attrs['dn'])){
357         $attrs['selected'] = FALSE;
358         $attrs['ldif']     = "";
359         $this->outside_groups[base64_encode($attrs['dn'])] = $attrs;
360       }
361     }
363     if(count($this->outside_groups)){
364       $this->checks['outside_groups']['STATUS']    = FALSE;
365       $this->checks['outside_groups']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
366       $this->checks['outside_groups']['ERROR_MSG'] = 
367         sprintf(_("Found %s groups outside the configured tree '%s'."),count($this->outside_groups),$group_ou);
368       $this->checks['outside_groups']['ERROR_MSG'].= "&nbsp;<input type='submit' name='outside_groups_dialog' value='"._("Move")."...'>";
369       return(false);
370     }else{
371       $this->checks['outside_groups']['STATUS']    = TRUE;
372       $this->checks['outside_groups']['STATUS_MSG']= _("Ok");
373       $this->checks['outside_groups']['ERROR_MSG'] = "";
374       return(TRUE);
375     }
376   }
379   /* Search for users outside the people ou 
380    */
381   function search_outside_users()
382   {
383     $cv = $this->parent->captured_values;
384     $ldap = new LDAP($cv['admin'],
385         $cv['password'],
386         $cv['connection'],
387         FALSE,
388         $cv['tls']);
389     $people_ou = $cv['peopleou'];
390     $ldap->cd($cv['base']);
391     $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))",array("dn"));
392     if(!$res){
393       $this->checks['outside_users']['STATUS']    = FALSE;
394       $this->checks['outside_users']['STATUS_MSG']= _("LDAP query failed");
395       $this->checks['outside_users']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
396       return(false);
397     }
400     $this->outside_users = array();
401     while($attrs = $ldap->fetch()){
402       if((!preg_match("/^[^,]+,".normalizePreg($people_ou)."/",$attrs['dn'])  && !preg_match("/,dc=addressbook,/",$attrs['dn']))){
403         $attrs['selected'] = FALSE;
404         $attrs['ldif']     = "";
405         $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
406       }
407     }
409     if(count($this->outside_users)){
410       $this->checks['outside_users']['STATUS']    = FALSE;
411       $this->checks['outside_users']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
412       $this->checks['outside_users']['ERROR_MSG'] = 
413         sprintf(_("Found %s user(s) outside the configured tree '%s'."),count($this->outside_users),$people_ou);
414       $this->checks['outside_users']['ERROR_MSG'].= "<input type='submit' name='outside_users_dialog' value='"._("Move")."...'>";
415       return(false);
416     }else{
417       $this->checks['outside_users']['STATUS']    = TRUE;
418       $this->checks['outside_users']['STATUS_MSG']= _("Ok");
419       $this->checks['outside_users']['ERROR_MSG'] = "";
420       return(TRUE);
421     }
422   }
425   /* Check ldap accessibility 
426    * Create and remove a dummy object, 
427    *  to ensure that we have the necessary permissions
428    */
429   function check_ldap_permissions()
430   {
431     $cv = $this->parent->captured_values;
432     $ldap = new LDAP($cv['admin'],
433         $cv['password'],
434         $cv['connection'],
435         FALSE,
436         $cv['tls']);
438     /* Create dummy entry 
439      */
440     $name     = "GOsa_setup_text_entry_".session_id().rand(0,999999);
441     $dn       = "ou=".$name.",".$cv['base'];
442     $testEntry= array();
443     $testEntry['objectClass'][]= "top";
444     $testEntry['objectClass'][]= "organizationalUnit";
445     $testEntry['objectClass'][]= "gosaDepartment";
446     $testEntry['description']= "Created by GOsa setup, this object can be removed.";
447     $testEntry['ou']  = $name;
449     /* check if simple ldap cat will be successful 
450      */
451     $res = $ldap->cat($cv['base']);  
452     if(!$res){
453       $this->checks['permissions']['STATUS']    = FALSE;
454       $this->checks['permissions']['STATUS_MSG']= _("LDAP query failed");
455       $this->checks['permissions']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
456       return(false);
457     }
458   
459     /* Try to create dummy object 
460      */ 
461     $ldap->cd ($dn);
462     $ldap->create_missing_trees($dn);
463     $res = $ldap->add($testEntry);
464     $ldap->cat($dn);
465     if(!$ldap->count()){
466       gosa_log($ldap->get_error());
467       $this->checks['permissions']['STATUS']    = FALSE;
468       $this->checks['permissions']['STATUS_MSG']= _("Failed");
469       $this->checks['permissions']['ERROR_MSG'] = 
470         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
471       return(false);
472     }
474     /* Try to remove created entry 
475      */
476     $res = $ldap->rmDir($dn);
477     $ldap->cat($dn);
478     if($ldap->count()){
479       gosa_log($ldap->get_error());
480       $this->checks['permissions']['STATUS']    = FALSE;
481       $this->checks['permissions']['STATUS_MSG']= _("Failed");
482       $this->checks['permissions']['ERROR_MSG'] = 
483         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
484       return(false);
485     }
487     /* Create & remove of dummy object was successful */
488     $this->checks['permissions']['STATUS']    = TRUE;
489     $this->checks['permissions']['STATUS_MSG']= _("Ok");
490     $this->checks['permissions']['ERROR_MSG'] = "";
491     return(true);
492   } 
495   /* Check if there are users which will 
496    *  be invisible for GOsa 
497    */
498   function check_gosaAccounts()
499   {
500     /* Remember old list of ivisible users, to be able to set 
501      *  the 'html checked' status for the checkboxes again 
502      */
503     $cnt_ok = 0;
504     $old    = $this->users_to_migrate;
505     $this->users_to_migrate = array();
507     /* Get collected configuration settings */
508     $cv = $this->parent->captured_values;
510     /* Establish ldap connection */
511     $ldap = new LDAP($cv['admin'],
512         $cv['password'],
513         $cv['connection'],
514         FALSE,
515         $cv['tls']);
517     /* Get all invisible users 
518      */
519     $ldap->cd($cv['base']); 
520     $res =$ldap->search("(&(|(objectClass=posixAccount)(&(objectClass=inetOrgPerson)(objectClass=organizationalPerson)))(!(objectClass=gosaAccount))(uid=*))",array("sn","givenName","cn","uid"));
521     while($attrs = $ldap->fetch()){
522       if(!preg_match("/,dc=addressbook,/",$attrs['dn'])){
523         $attrs['checked'] = FALSE;
524         $attrs['before']  = "";
525         $attrs['after']   = "";
527         /* Set objects to selected, that were selected before reload */
528         if(isset($old[base64_encode($attrs['dn'])])){
529           $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
530         }
531         $this->users_to_migrate[base64_encode($attrs['dn'])] = $attrs;
532       }
533     }
535     /* No invisible */
536     if(!$res){
537       $this->checks['users_visible']['STATUS']    = FALSE;
538       $this->checks['users_visible']['STATUS_MSG']= _("LDAP query failed");
539       $this->checks['users_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
540     }elseif(count($this->users_to_migrate) == 0){
541       $this->checks['users_visible']['STATUS']    = TRUE;
542       $this->checks['users_visible']['STATUS_MSG']= _("Ok");
543       $this->checks['users_visible']['ERROR_MSG'] = "";
544     }else{
545       $this->checks['users_visible']['STATUS']    = FALSE;
546       $this->checks['users_visible']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
547       $this->checks['users_visible']['ERROR_MSG'] = sprintf(_("Found %s user(s) that will not be visible in GOsa."), 
548           count($this->users_to_migrate));
549       $this->checks['users_visible']['ERROR_MSG'] .= "<input type='submit' name='users_visible_migrate' value='"._("Migrate")."...'>";
550     }
551   }
554   /* Start user account migration 
555    */  
556   function migrate_gosaAccounts($only_ldif = FALSE)
557   {
558     $this->show_details= $only_ldif;
560     /* Get collected configuration settings */
561     $cv = $this->parent->captured_values;
563     /* Establish ldap connection */
564     $ldap = new LDAP($cv['admin'],
565         $cv['password'],
566         $cv['connection'],
567         FALSE,
568         $cv['tls']);
570     /* Add gosaAccount objectClass to the selected users  
571      */
572     foreach($this->users_to_migrate as $key => $dep){
573       if($dep['checked']){
575         /* Get old objectClasses */
576         $ldap->cat($dep['dn'],array("objectClass"));
577         $attrs      = $ldap->fetch();
579         /* Create new objectClass array */
580         $new_attrs  = array();
581         $new_attrs['objectClass']= array("gosaAccount","inetOrgPerson","organizationalPerson");
582         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
583           if(!in_array_ics($attrs['objectClass'][$i], $new_attrs['objectClass'])){
584             $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
585           }
586         }
588         /* Set info attributes for current object, 
589          *  or write changes to the ldap database 
590          */
591         if($only_ldif){
592           $this->users_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
593           $this->users_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
594         }else{
595           $ldap->cd($attrs['dn']);
596           if(!$ldap->modify($new_attrs)){
597             print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
598             return(false);
599           }
600         }
601       }
602     }
603     return(TRUE);
604   }
607   /* Check if there are invisible organizational Units 
608    */
609   function check_organizationalUnits()
610   {
611     $cnt_ok = 0;
612     $old = $this->deps_to_migrate;
613     $this->deps_to_migrate = array();
615     /* Get collected configuration settings */
616     $cv = $this->parent->captured_values;
618     /* Establish ldap connection */
619     $ldap = new LDAP($cv['admin'],
620         $cv['password'],
621         $cv['connection'],
622         FALSE,
623         $cv['tls']);
625     /* Skip GOsa internal departments */
626     $skip_dns = array("/^ou=people,/","/^ou=groups,/","/(,|)ou=configs,/","/(,|)ou=systems,/",
627         "/^ou=apps,/","/^ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
628         "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
629         "/(,|)ou=winstations,/");
632     /* Get all invisible departments */
633     $ldap->cd($cv['base']); 
634     $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
635     while($attrs = $ldap->fetch()){
636       $attrs['checked'] = FALSE;
637       $attrs['before']  = "";
638       $attrs['after']   = "";
640       /* Set objects to selected, that were selected before reload */
641       if(isset($old[base64_encode($attrs['dn'])])){
642         $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
643       }
644       $this->deps_to_migrate[base64_encode($attrs['dn'])] = $attrs;
645     }
647     /* Filter returned list of departments and ensure that 
648      *  GOsa internal departments will not be listed 
649      */
650     foreach($this->deps_to_migrate as $key => $attrs){
651       $dn = $attrs['dn'];
652       $skip = false;
653       foreach($skip_dns as $skip_dn){
654         if(preg_match($skip_dn,$dn)){
655           $skip = true;
656         }
657       }
658       if($skip){
659         unset($this->deps_to_migrate[$key]);
660       }
661     }
663     /* If we have no invisible departments found  
664      *  tell the user that everything is ok 
665      */
666     if(!$res){
667       $this->checks['deps_visible']['STATUS']    = FALSE;
668       $this->checks['deps_visible']['STATUS_MSG']= _("LDAP query failed");
669       $this->checks['deps_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
670     }elseif(count($this->deps_to_migrate) == 0 ){
671       $this->checks['deps_visible']['STATUS']    = TRUE;
672       $this->checks['deps_visible']['STATUS_MSG']= _("Ok");
673       $this->checks['deps_visible']['ERROR_MSG'] = "";
674     }else{
675       $this->checks['deps_visible']['STATUS']    = TRUE;
676       $this->checks['deps_visible']['STATUS_MSG']= '<font style="color:#FFA500">'._("Warning").'</font>';
677       $this->checks['deps_visible']['ERROR_MSG'] = sprintf(_("Found %s department(s) that will not be visible in GOsa."),count($this->deps_to_migrate));
678       $this->checks['deps_visible']['ERROR_MSG'] .= "&nbsp;<input type='submit' name='deps_visible_migrate' value='"._("Migrate")."...'>";
679     }
680   }
684   /* Start deparmtment migration */  
685   function migrate_organizationalUnits($only_ldif = FALSE)
686   {
687     $this->show_details= $only_ldif;
689     /* Get collected configuration settings */
690     $cv = $this->parent->captured_values;
692     /* Establish ldap connection */
693     $ldap = new LDAP($cv['admin'],
694         $cv['password'],
695         $cv['connection'],
696         FALSE,
697         $cv['tls']);
699     /* Add gosaDepartment objectClass to each selected entry 
700      */
701     foreach($this->deps_to_migrate as $key => $dep){
702       if($dep['checked']){
704         /* Get current objectClasses */
705         $ldap->cat($dep['dn'],array("objectClass","description"));
706         $attrs      = $ldap->fetch();
708         /* Create new objectClass attribute including gosaDepartment*/
709         $new_attrs  = array();
710         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
711           $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
712         }
713         $new_attrs['objectClass'][] = "gosaDepartment";
715         /* Append description it is missing */
716         if(!isset($attrs['description'])){
717           $new_attrs['description'][] = "GOsa department";
718         }
720         /* Depending on the parameter >only_diff< we save the changes as ldif
721          *  or we write our changes directly to the ldap database
722          */
723         if($only_ldif){
724           $this->deps_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
725           $this->deps_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
726         }else{
727           $ldap->cd($attrs['dn']);
728           if(!$ldap->modify($new_attrs)){
729             print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
730             return(false);
731           }
732         }
733       }
734     }
735     return(TRUE);
736   }
739   /* Check Acls if there is at least one object with acls defined 
740    */
741   function check_administrativeAccount()
742   {
743     /* Establish ldap connection */
744     $cv = $this->parent->captured_values;
745     $ldap = new LDAP($cv['admin'],
746         $cv['password'],
747         $cv['connection'],
748         FALSE,
749         $cv['tls']);
751     /* Search for gosaAcls */ 
752     $ldap->cd($cv['base']);
753     $res = $ldap->cat($cv['base']);
754     
755     if(!$res){
756       $this->checks['acls']['STATUS']    = FALSE;
757       $this->checks['acls']['STATUS_MSG']= _("LDAP query failed");
758       $this->checks['acls']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
759     }else{
760       $found = false;
761       $attrs = $ldap->fetch();
762       if(isset($attrs['gosaAclEntry'])){
763         $acls = $attrs['gosaAclEntry'];
764         for($i = 0 ; $i < $acls['count'] ; $i++){
765           $acl = $acls[$i];
766           $tmp = split(":",$acl);
767   
768           /* Only check permanent acls */
769           if($tmp[1] == "psub"){
771             /* Check if acl owner is a valid GOsa user account */
772             $ldap->cat(base64_decode($tmp[2]),array("objectClass"));
773             $ret = $ldap->fetch();
775             if(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) && 
776                in_array("organizationalPerson",$ret['objectClass']) &&
777                in_array("inetOrgPerson",$ret['objectClass'])){
778               $found = TRUE;
779             }
780           }
781         }
782       }
784       if($found){
785         $this->checks['acls']['STATUS']    = TRUE;
786         $this->checks['acls']['STATUS_MSG']= _("Ok");
787         $this->checks['acls']['ERROR_MSG'] = "";
788       }else{
789         $this->checks['acls']['STATUS']    = FALSE;
790         $this->checks['acls']['STATUS_MSG']= _("Failed");
791         $this->checks['acls']['ERROR_MSG']= _("There is no GOsa administrator account inside your LDAP.")."&nbsp;";
792         $this->checks['acls']['ERROR_MSG'].= "<input type='submit' name='create_acls' value='"._("Create")."'>";
793       }
794     }
795     return($ldap->count()>=1);
796   }
800   function create_admin($only_ldif = FALSE)
801   {
802     /* Reset '' */
803     $this->acl_create_changes="";
805     /* Object that should receive admin acls */
806     $dn = $this->acl_create_selected;
808     /* Get collected configuration settings */
809     $cv = $this->parent->captured_values;
811     /* Establish ldap connection */
812     $ldap = new LDAP($cv['admin'],
813         $cv['password'],
814         $cv['connection'],
815         FALSE,
816         $cv['tls']);
818     /* Get current base attributes */
819     $ldap->cd($cv['base']);
820     $ldap->cat($cv['base'],array("dn","objectClass","gosaAclEntry"));
821     $attrs = $ldap->fetch();
823     /* Add acls for the selcted user to the base */
824     $attrs_new['objectClass'] = array("gosaACL");
826     for($i = 0; $i < $attrs['objectClass']['count']; $i ++){
827       if(!in_array_ics($attrs['objectClass'][$i],$attrs_new['objectClass'])){
828         $attrs_new['objectClass'][] = $attrs['objectClass'][$i];
829       }
830     }
832     $acl = "0:psub:".base64_encode($dn).":all;cmdrw";    
833     $attrs_new['gosaAclEntry'][] = $acl;
834     if(isset($attrs['gosaAclEntry'])){
835       for($i = 0 ; $i < $attrs['gosaAclEntry']['count']; $i ++){
836           
837         $prio = preg_replace("/[:].*$/","",$attrs['gosaAclEntry'][$i]);
838         $rest = preg_replace("/^[^:]/","",$attrs['gosaAclEntry'][$i]);
839  
840         $data = ($prio+1).$rest;
841         $attrs_new['gosaAclEntry'][] = $data;
842       }
843     }
845     if($only_ldif){
846       $this->acl_create_changes ="\n".$cv['base']."\n";
847       $this->acl_create_changes.=$this->array_to_ldif($attrs)."\n";
848       $this->acl_create_changes.="\n".$cv['base']."\n";
849       $this->acl_create_changes.=$this->array_to_ldif($attrs_new);
850     }else{
851    
852       $ldap->cd($cv['base']);
853       if(!$ldap->modify($attrs_new)){
854         print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
855         return(FALSE);
856       }else{
857         return(TRUE);
858       }
859     }
860   }
861  
862   
863   function create_admin_user()
864   {
865     $pw1 = $pw2 = "";
867     if(isset($_POST['new_user_password'])){
868       $pw1 = $_POST['new_user_password'];
869     }
870     if(isset($_POST['new_user_password2'])){
871       $pw2 = $_POST['new_user_password2'];
872     }
873   
874     if(empty($pw1) || empty($pw2) | ($pw1 != $pw2)){
875       print_red(_("Specified passwords are empty or not equal."));
876       return false;
877     }
878  
879     /* Establish ldap connection */
880     $cv = $this->parent->captured_values;
881     $ldap = new LDAP($cv['admin'],
882         $cv['password'],
883         $cv['connection'],
884         FALSE,
885         $cv['tls']);
887     /* Get current base attributes */
888     $ldap->cd($cv['base']);
889   
890     if($cv['peopledn'] == "cn"){
891       $dn = "cn=System Administrator,".$cv['peopleou'].",".$cv['base'];
892     }else{
893       $dn = "uid=admin,".$cv['peopleou'].",".$cv['base'];
894     }
896     $methods = @passwordMethod::get_available_methods_if_not_loaded();
897     $p_m = $methods[$cv['encryption']];
898     $p_c = new $p_m(array());
899     $hash = $p_c->generate_hash($pw2);
901     $new_user=array();
902     $new_user['objectClass']= array("top","person","gosaAccount","organizationalPerson","inetOrgPerson");
903     $new_user['givenName']  = "System";
904     $new_user['sn']  = "Administrator";
905     $new_user['cn']  = "System Administrator";
906     $new_user['uid'] = "admin";
907     $new_user['userPassword'] = $hash;
908     
909     $ldap->cd($cv['base']);
910     $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$dn));
911     $ldap->cd($dn);  
912     $res = $ldap->add($new_user);
913     $this->acl_create_selected = $dn;
914     $this->create_admin();
915     
916     if(!$res){
917       print_red($ldap->get_error());
918       return(FALSE);
919     }
920   
921     $this->acl_create_dialog=FALSE;        
922     $this->check_administrativeAccount();
923     return(TRUE);
924   }
925  
927   function migrate_outside_winstations($perform = FALSE)
928   {
929     /* Establish ldap connection */
930     $cv = $this->parent->captured_values;
931     $ldap = new LDAP($cv['admin'],
932         $cv['password'],
933         $cv['connection'],
934         FALSE,
935         $cv['tls']);
937     $ldap->cd($cv['base']);
939     /* Check if there was a destination department posted */
940     if(isset($_POST['move_winstation_to'])){
941       $destination_dep = $_POST['move_winstation_to'];
942     }else{
943       print_red(_("Couldn't move users to specified department."));
944       return(false);
945     }
946  
947     foreach($this->outside_winstations as $b_dn => $data){
948       $this->outside_winstations[$b_dn]['ldif'] ="";
949       if($data['selected']){
950         $dn = base64_decode($b_dn);
951         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
952         if(!$perform){
953           $this->outside_winstations[$b_dn]['ldif'] = _("Winstation will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
956           /* Check if there are references to this object */
957           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
958           $refs = "";
959           while($attrs = $ldap->fetch()){
960             $ref_dn = $attrs['dn'];
961             $refs .= "<br />\t".$ref_dn;
962           } 
963           if(!empty($refs)){ 
964             $this->outside_winstations[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
965           }
967         }else{
968           $this->move($dn,$d_dn);
969         }
970       }
971     }
972   }
973   
975   function migrate_outside_groups($perform = FALSE)
976   {
977     /* Establish ldap connection */
978     $cv = $this->parent->captured_values;
979     $ldap = new LDAP($cv['admin'],
980         $cv['password'],
981         $cv['connection'],
982         FALSE,
983         $cv['tls']);
985     $ldap->cd($cv['base']);
987     /* Check if there was a destination department posted */
988     if(isset($_POST['move_group_to'])){
989       $destination_dep = $_POST['move_group_to'];
990     }else{
991       print_red(_("Couldn't move users to specified department."));
992       return(false);
993     }
994  
995     foreach($this->outside_groups as $b_dn => $data){
996       $this->outside_groups[$b_dn]['ldif'] ="";
997       if($data['selected']){
998         $dn = base64_decode($b_dn);
999         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1000         if(!$perform){
1001           $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1004           /* Check if there are references to this object */
1005           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1006           $refs = "";
1007           while($attrs = $ldap->fetch()){
1008             $ref_dn = $attrs['dn'];
1009             $refs .= "<br />\t".$ref_dn;
1010           } 
1011           if(!empty($refs)){ 
1012             $this->outside_groups[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1013           }
1015         }else{
1016           $this->move($dn,$d_dn);
1017         }
1018       }
1019     }
1020   }
1021   
1023   function migrate_outside_users($perform = FALSE)
1024   {
1025     /* Establish ldap connection */
1026     $cv = $this->parent->captured_values;
1027     $ldap = new LDAP($cv['admin'],
1028         $cv['password'],
1029         $cv['connection'],
1030         FALSE,
1031         $cv['tls']);
1033     $ldap->cd($cv['base']);
1035     /* Check if there was a destination department posted */
1036     if(isset($_POST['move_user_to'])){
1037       $destination_dep = $_POST['move_user_to'];
1038     }else{
1039       print_red(_("Couldn't move users to specified department."));
1040       return(false);
1041     }
1042  
1043     foreach($this->outside_users as $b_dn => $data){
1044       $this->outside_users[$b_dn]['ldif'] ="";
1045       if($data['selected']){
1046         $dn = base64_decode($b_dn);
1047         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1048         if(!$perform){
1049           $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1051           /* Check if there are references to this object */
1052           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1053           $refs = "";
1054           while($attrs = $ldap->fetch()){
1055             $ref_dn = $attrs['dn'];
1056             $refs .= "<br />\t".$ref_dn;
1057           } 
1058           if(!empty($refs)){ 
1059             $this->outside_users[$b_dn]['ldif'] .= "<br /><br /><i>"._("The following references will be updated").":</i>".$refs;
1060           }
1062         }else{
1063           $this->move($dn,$d_dn);
1064         }
1065       }
1066     }
1067   }
1068   
1070   function execute()
1071   {
1072     /* Initialise checks if this is the first call */
1073     if(!$this->checks_initialised || isset($_POST['reload'])){
1074       $this->initialize_checks();
1075       $this->checks_initialised = TRUE;
1076     }
1078     /*************
1079      * Winstations outside the group ou 
1080      *************/
1081     
1082     if(isset($_POST['outside_winstations_dialog_cancel'])){
1083       $this->outside_winstations_dialog = FALSE;
1084       $this->dialog = FALSE;
1085       $this->show_details = FALSE;
1086     }
1087    
1088     if(isset($_POST['outside_winstations_dialog_whats_done'])){
1089       $this->migrate_outside_winstations(FALSE);
1090     }
1091  
1092     if(isset($_POST['outside_winstations_dialog_perform'])){
1093       $this->migrate_outside_winstations(TRUE);
1094       $this->search_outside_winstations();
1095       $this->dialog = FALSE;
1096       $this->show_details = FALSE;
1097       $this->outside_winstations_dialog = FALSE;
1098     }
1100     if(isset($_POST['outside_winstations_dialog'])){
1101       $this->outside_winstations_dialog = TRUE;
1102       $this->dialog = TRUE;
1103     }
1104     
1105     if($this->outside_winstations_dialog){
1106       $smarty = get_smarty();
1107       $smarty->assign("ous",$this->get_all_winstation_ous());
1108       $smarty->assign("method","outside_winstations");
1109       $smarty->assign("outside_winstations",$this->outside_winstations);
1110       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1111     }
1112     /*************
1113      * Groups outside the group ou 
1114      *************/
1115     
1116     if(isset($_POST['outside_groups_dialog_cancel'])){
1117       $this->outside_groups_dialog = FALSE;
1118       $this->show_details = FALSE;
1119       $this->dialog = FALSE;
1120     }
1121    
1122     if(isset($_POST['outside_groups_dialog_whats_done'])){
1123       $this->show_details= TRUE;
1124       $this->migrate_outside_groups(FALSE);
1125     }
1126  
1127     if(isset($_POST['outside_groups_dialog_refresh'])){
1128       $this->show_details= FALSE;
1129     }
1131     if(isset($_POST['outside_groups_dialog_perform'])){
1132       $this->migrate_outside_groups(TRUE);
1133       $this->search_outside_groups();
1134       $this->dialog = FALSE;
1135       $this->show_details = FALSE;
1136       $this->outside_groups_dialog = FALSE;
1137     }
1139     if(isset($_POST['outside_groups_dialog'])){
1140       $this->outside_groups_dialog = TRUE;
1141       $this->dialog = TRUE;
1142     }
1143     
1144     if($this->outside_groups_dialog){
1145       $smarty = get_smarty();
1146       $smarty->assign("ous",$this->get_all_group_ous());
1147       $smarty->assign("method","outside_groups");
1148       $smarty->assign("outside_groups",$this->outside_groups);
1149       $smarty->assign("group_details", $this->show_details);
1150       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1151     }
1152  
1153     /*************
1154      * User outside the people ou 
1155      *************/
1156     
1157     if(isset($_POST['outside_users_dialog_cancel'])){
1158       $this->outside_users_dialog = FALSE;
1159       $this->dialog = FALSE;
1160       $this->show_details = FALSE;
1161     }
1162    
1163     if(isset($_POST['outside_users_dialog_whats_done'])){
1164       $this->show_details= TRUE;
1165       $this->migrate_outside_users(FALSE);
1166     }
1167  
1168     if(isset($_POST['outside_users_dialog_perform'])){
1169       $this->migrate_outside_users(TRUE);
1170       $this->search_outside_users();
1171       $this->dialog = FALSE;
1172       $this->show_details = FALSE;
1173       $this->outside_users_dialog = FALSE;
1174     }
1176     if (isset($_POST['outside_users_dialog_refresh'])){
1177       $this->show_details= FALSE;
1178     }
1180     if(isset($_POST['outside_users_dialog'])){
1181       $this->outside_users_dialog = TRUE;
1182       $this->dialog = TRUE;
1183     }
1184     
1185     if($this->outside_users_dialog){
1186       $smarty = get_smarty();
1187       $smarty->assign("ous",$this->get_all_people_ous());
1188       $smarty->assign("method","outside_users");
1189       $smarty->assign("outside_users",$this->outside_users);
1190       $smarty->assign("user_details", $this->show_details);
1191       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1192     }
1193  
1194     /*************
1195      * Root object check  
1196      *************/
1197   
1198     if(isset($_POST['retry_root_create'])){
1200       $state = $this->checks['root']['STATUS'];
1201       $this->checkBase(FALSE);
1202       if($state != $this->checks['root']['STATUS']){
1203         $this->initialize_checks();
1204       }
1205     }
1206  
1207     /*************
1208      * User Migration handling 
1209      *************/
1211     if(isset($_POST['retry_acls'])){
1212       $this->check_administrativeAccount();
1213     }
1215     if(isset($_POST['create_acls'])){
1216       $this->acl_create_dialog = TRUE;
1217       $this->dialog = TRUE;
1218     }
1219   
1220     if(isset($_POST['create_acls_cancel'])){
1221       $this->acl_create_dialog = FALSE;
1222       $this->dialog = FALSE;
1223       $this->show_details = FALSE;
1224     }
1226     if(isset($_POST['create_acls_create_confirmed'])){
1227       if($this->create_admin()){
1228         $this->acl_create_dialog = FALSE;
1229         $this->dialog = FALSE;
1230       $this->show_details = FALSE;
1231         $this->initialize_checks();
1232       }
1233     }
1235     if(isset($_POST['create_acls_create'])){
1236       $this->create_admin(TRUE);
1237     }
1239     if(isset($_POST['create_admin_user'])){
1240       if($this->create_admin_user()){
1241         $this->dialog = FALSE;
1242       $this->show_details = FALSE;
1243       }
1244     }
1246     if($this->acl_create_dialog){
1247       $smarty = get_smarty();
1248       $smarty->assign("new_user_password",@$_POST['new_user_password']);
1249       $smarty->assign("new_user_password2",@$_POST['new_user_password2']);
1250       $smarty->assign("users" ,$this->get_user_list());
1251       $smarty->assign("users_cnt" ,count($this->get_user_list()));
1252       $smarty->assign("groups",$this->get_group_list());
1253       $smarty->assign("groups_cnt",count($this->get_group_list()));
1254       $smarty->assign("type"  ,$this->acl_create_type);
1255       $smarty->assign("method","create_acls");
1256       $smarty->assign("acl_create_selected",$this->acl_create_selected);
1257       $smarty->assign("what_will_be_done_now",$this->acl_create_changes);
1258       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1259     }
1261     /*************
1262      * User Migration handling 
1263      *************/
1265     /* Refresh list of deparments */
1266     if(isset($_POST['users_visible_migrate_refresh'])){
1267       $this->check_gosaAccounts();
1268     }
1270     /* Open migration dialog */
1271     if(isset($_POST['users_visible_migrate'])){
1272       $this->show_details= FALSE;
1273       $this->users_migration_dialog = TRUE;
1274       $this->dialog =TRUE;
1275     }
1277     /* Close migration dialog */
1278     if(isset($_POST['users_visible_migrate_close'])){
1279       $this->users_migration_dialog = FALSE;
1280       $this->dialog =FALSE;
1281       $this->show_details = FALSE;
1282     }
1284     /* Start migration */
1285     if(isset($_POST['users_visible_migrate_migrate'])){
1286       if($this->migrate_gosaAccounts()){
1287         $this->check_gosaAccounts();
1288         $this->dialog = FALSE;
1289       $this->show_details = FALSE;
1290         $this->users_migration_dialog = FALSE;
1291       }
1292     }
1294     /* Start migration */
1295     if(isset($_POST['users_visible_migrate_whatsdone'])){
1296       $this->migrate_gosaAccounts(TRUE);
1297     }
1299     /* Display migration dialog */
1300     if($this->users_migration_dialog){
1301       $smarty = get_smarty();
1302       $smarty->assign("users_to_migrate",$this->users_to_migrate);
1303       $smarty->assign("method","migrate_users");
1304       $smarty->assign("user_details", $this->show_details);
1305       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1306     }
1309     /*************
1310      * Department Migration handling 
1311      *************/
1313     /* Refresh list of deparments */
1314     if(isset($_POST['deps_visible_migrate_refresh'])){
1315       $this->check_organizationalUnits();
1316       $this->show_details= FALSE;
1317     }
1319     /* Open migration dialog */
1320     if(isset($_POST['deps_visible_migrate'])){
1321       $this->dep_migration_dialog = TRUE;
1322       $this->dialog =TRUE;
1323     }
1325     /* Close migration dialog */
1326     if(isset($_POST['deps_visible_migrate_close'])){
1327       $this->dep_migration_dialog = FALSE;
1328       $this->dialog =FALSE;
1329       $this->show_details = FALSE;
1330     }
1332     /* Start migration */
1333     if(isset($_POST['deps_visible_migrate_migrate'])){
1334       if($this->migrate_organizationalUnits()){
1335         $this->show_details= FALSE;
1336         $this->check_organizationalUnits();
1337         $this->dialog = FALSE;
1338         $this->dep_migration_dialog = FALSE;
1339       }
1340     }
1342     /* Start migration */
1343     if(isset($_POST['deps_visible_migrate_whatsdone'])){
1344       $this->migrate_organizationalUnits(TRUE);
1345     }
1347     /* Display migration dialog */
1348     if($this->dep_migration_dialog){
1349       $smarty = get_smarty();
1350       $smarty->assign("deps_to_migrate",$this->deps_to_migrate);
1351       $smarty->assign("method","migrate_deps");
1352       $smarty->assign("deps_details", $this->show_details);
1353       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1354     }
1356     $smarty = get_smarty();
1357     $smarty->assign("checks",$this->checks);
1358     $smarty->assign("method","default");
1359     return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1360   }
1363   function save_object()
1364   {
1365     $this->is_completed= TRUE;
1367     /* Capture all selected winstations from outside_winstations_dialog */
1368     if($this->outside_winstations_dialog){
1369       foreach($this->outside_winstations as $dn => $data){
1370         if(isset($_POST['select_winstation_'.$dn])){
1371           $this->outside_winstations[$dn]['selected'] = TRUE;
1372         }else{
1373           $this->outside_winstations[$dn]['selected'] = FALSE;
1374         }
1375       }
1376     }
1378     /* Capture all selected groups from outside_groups_dialog */
1379     if($this->outside_groups_dialog){
1380       foreach($this->outside_groups as $dn => $data){
1381         if(isset($_POST['select_group_'.$dn])){
1382           $this->outside_groups[$dn]['selected'] = TRUE;
1383         }else{
1384           $this->outside_groups[$dn]['selected'] = FALSE;
1385         }
1386       }
1387     }
1389     /* Capture all selected users from outside_users_dialog */
1390     if($this->outside_users_dialog){
1391       foreach($this->outside_users as $dn => $data){
1392         if(isset($_POST['select_user_'.$dn])){
1393           $this->outside_users[$dn]['selected'] = TRUE;
1394         }else{
1395           $this->outside_users[$dn]['selected'] = FALSE;
1396         }
1397       }
1398     }
1400     /* Get "create acl" dialog posts */
1401     if($this->acl_create_dialog){
1402       if(isset($_POST['create_acls_create'])){
1403         if(isset($_POST['create_acls_selected'])){
1404           $this->acl_create_selected = base64_decode($_POST['create_acls_selected']);
1405         }else{
1406           $this->acl_create_selected = ""; 
1407         }
1408       }
1410       if(isset($_POST['create_acls_create_abort'])){
1411         $this->acl_create_selected = "";
1412       }
1414       if(isset($_POST['acl_create_type'])){
1415         $this->acl_create_type = $_POST['acl_create_type'];
1416       }
1417     }
1419     /* Get selected departments */
1420     if($this->dep_migration_dialog){
1421       foreach($this->deps_to_migrate as $id => $data){
1422         if(isset($_POST['migrate_'.$id])){
1423           $this->deps_to_migrate[$id]['checked'] = TRUE;
1424         }else{
1425           $this->deps_to_migrate[$id]['checked'] = FALSE;
1426         }
1427       }
1428     }
1430     /* Get selected users */
1431     if($this->users_migration_dialog){
1432       foreach($this->users_to_migrate as $id => $data){
1433         if(isset($_POST['migrate_'.$id])){
1434           $this->users_to_migrate[$id]['checked'] = TRUE;
1435         }else{
1436           $this->users_to_migrate[$id]['checked'] = FALSE;
1437         }
1438       }
1439     }
1440   }
1443   /* Check if the root object exists.
1444    * If the parameter just_check is true, then just check if the 
1445    *  root object is missing and update the info messages.
1446    * If the Parameter is false, try to create a new root object.
1447    */
1448   function checkBase($just_check = TRUE)
1449   {
1450     /* Get collected setup informations */
1451     $cv = $this->parent->captured_values;
1453     /* Establish ldap connection */
1454     $ldap = new LDAP($cv['admin'],
1455         $cv['password'],
1456         $cv['connection'],
1457         FALSE,
1458         $cv['tls']);
1460     /* Check if root object exists */
1461     $ldap->cd($cv['base']);
1462     $res = $ldap->search("(objectClass=*)");
1463     $err = ldap_errno($ldap->cid); 
1465     if( !$res || 
1466         $err == 0x20 ||  # LDAP_NO_SUCH_OBJECT
1467         $err == 0x40) {  # LDAP_NAMING_VIOLATION
1469       /* Root object doesn't exists 
1470        */
1471       if($just_check){
1472         $this->checks['root']['STATUS']    = FALSE;
1473         $this->checks['root']['STATUS_MSG']= _("Failed");
1474         $this->checks['root']['ERROR_MSG'] =  _("The LDAP root object is missing. It is required to use your LDAP service.").'&nbsp;';
1475         $this->checks['root']['ERROR_MSG'].=  "<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1476         return(FALSE);
1477       }else{
1479         /* Add root object */ 
1480         $ldap->cd($cv['base']);
1481         $res = $ldap->create_missing_trees($cv['base']);
1483         /* If adding failed, tell the user */
1484         if(!$res){
1485           $this->checks['root']['STATUS']    = FALSE;
1486           $this->checks['root']['STATUS_MSG']= _("Failed");
1487           $this->checks['root']['ERROR_MSG'] = _("Root object couldn't be created, you should try it on your own.");
1488           $this->checks['root']['ERROR_MSG'].= "&nbsp;<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1489           return($res);;
1490         }
1491       }
1492     }
1494     /* Create & remove of dummy object was successful */
1495     $this->checks['root']['STATUS']    = TRUE;
1496     $this->checks['root']['STATUS_MSG']= _("Ok");
1497   }
1500   /* Return ldif information for a 
1501    * given attribute array 
1502    */
1503   function array_to_ldif($atts)
1504   {
1505     $ret = "";
1506     unset($atts['count']);
1507     unset($atts['dn']);
1508     foreach($atts as $name => $value){
1509       if(is_numeric($name)) {
1510         continue;
1511       }
1512       if(is_array($value)){
1513         unset($value['count']);
1514         foreach($value as $a_val){
1515           $ret .= $name.": ". $a_val."\n";
1516         }
1517       }else{
1518         $ret .= $name.": ". $value."\n";
1519       }
1520     }
1521     return(preg_replace("/\n$/","",$ret));
1522   }
1525   function get_user_list()
1526   {
1527     /* Get collected configuration settings */
1528     $cv = $this->parent->captured_values;
1530     /* Establish ldap connection */
1531     $ldap = new LDAP($cv['admin'],
1532         $cv['password'],
1533         $cv['connection'],
1534         FALSE,
1535         $cv['tls']);
1536     
1537     $ldap->cd($cv['base']);
1538     $ldap->search("(objectClass=gosaAccount)",array("dn"));
1539   
1540     $tmp = array();
1541     while($attrs = $ldap->fetch()){
1542       $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1543     }
1544     return($tmp);
1545   }
1548   function get_all_people_ous()
1549   {
1550     /* Get collected configuration settings */
1551     $cv = $this->parent->captured_values;
1553     /* Establish ldap connection */
1554     $ldap = new LDAP($cv['admin'],
1555         $cv['password'],
1556         $cv['connection'],
1557         FALSE,
1558         $cv['tls']);
1560     $ldap->cd($cv['base']);
1561     $ldap->search("(".$cv['peopleou'].")",array("dn"));
1562   
1563     if($ldap->count() == 0 ){
1564       $add_dn = $cv['peopleou'].",".$cv['base'];
1565       $naming_attr = preg_replace("/=.*$/","",$add_dn);
1566       $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1567       $add = array();
1568       $add['objectClass'] = array("organizationalUnit");
1569       $add[$naming_attr] = $naming_value;
1571       $ldap->cd($cv['base']);
1572       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1573       $ldap->cd($add_dn);
1574       $ldap->add($add);
1575     }
1577     $ldap->search("(".$cv['peopleou'].")",array("dn"));
1578     $tmp = array();
1579     while($attrs= $ldap->fetch()){
1580       if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1581         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1582       }
1583     }
1584     return($tmp); 
1585   }
1587   function get_all_winstation_ous()
1588   {
1589     /* Get collected configuration settings */
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     /* Get winstation ou */
1600     if($cv['generic_settings']['wws_ou_active']) {
1601       $winstation_ou = $cv['generic_settings']['ws_ou'];
1602     }else{
1603       $winstation_ou = "ou=winstations";
1604     }
1606     $ldap->cd($cv['base']);
1607     $ldap->search("(".$winstation_ou.")",array("dn"));
1608   
1609     if($ldap->count() == 0 ){
1610       $add_dn = $winstation_ou.",ou=systems,".$cv['base'];
1611       $naming_attr = preg_replace("/=.*$/","",$add_dn);
1612       $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1613       $add = array();
1614       $add['objectClass'] = array("organizationalUnit");
1615       $add[$naming_attr] = $naming_value;
1617       $ldap->cd($cv['base']);
1618       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1619       $ldap->cd($add_dn);
1620       $ldap->add($add);
1621     }
1623     $ldap->search("(".$winstation_ou.")",array("dn"));
1624     $tmp = array();
1625     while($attrs= $ldap->fetch()){
1626       if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1627         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1628       }
1629     }
1630     return($tmp); 
1631   }
1634   function get_all_group_ous()
1635   {
1636     /* Get collected configuration settings */
1637     $cv = $this->parent->captured_values;
1639     /* Establish ldap connection */
1640     $ldap = new LDAP($cv['admin'],
1641         $cv['password'],
1642         $cv['connection'],
1643         FALSE,
1644         $cv['tls']);
1646     $ldap->cd($cv['base']);
1647     $ldap->search("(".$cv['groupou'].")",array("dn"));
1648   
1649     if($ldap->count() == 0 ){
1650       $add_dn = $cv['groupou'].",".$cv['base'];
1651       $naming_attr = preg_replace("/=.*$/","",$add_dn);
1652       $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1653       $add = array();
1654       $add['objectClass'] = array("organizationalUnit");
1655       $add[$naming_attr] = $naming_value;
1657       $ldap->cd($cv['base']);
1658       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1659       $ldap->cd($add_dn);
1660       $ldap->add($add);
1661     }
1663     $ldap->search("(".$cv['groupou'].")",array("dn"));
1664     $tmp = array();
1665     while($attrs= $ldap->fetch()){
1666       if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1667         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1668       }
1669     }
1670     return($tmp); 
1671   }
1674   function get_group_list()
1675   {
1676     /* Get collected configuration settings */
1677     $cv = $this->parent->captured_values;
1679     /* Establish ldap connection */
1680     $ldap = new LDAP($cv['admin'],
1681         $cv['password'],
1682         $cv['connection'],
1683         FALSE,
1684         $cv['tls']);
1685     
1686     $ldap->cd($cv['base']);
1687     $ldap->search("(objectClass=posixGroup)",array("dn"));
1688   
1689     $tmp = array();
1690     while($attrs = $ldap->fetch()){
1691       $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1692     }
1693     return($tmp);
1694   }
1697   function move($source,$destination)
1698   {
1699     /* Get collected configuration settings */
1700     $cv = $this->parent->captured_values;
1702     /* Establish ldap connection */
1703     $ldap = new LDAP($cv['admin'],
1704         $cv['password'],
1705         $cv['connection'],
1706         FALSE,
1707         $cv['tls']);
1709      /* Update object references in gosaGroupOfNames */
1710     $ogs_to_fix = array();
1711     $ldap->cd($cv['base']);
1712     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($source).'))', array('cn','member'));
1713     while ($attrs= $ldap->fetch()){
1714       $dn = $attrs['dn'];
1715       $attrs = $this->cleanup_array($attrs);
1716       $member_new = array($destination);
1717       foreach($attrs['member'] as $member){
1718         if($member != $source){
1719           $member_new[] = $member;
1720         }
1721       }
1722       $attrs['member'] = $member_new;
1723       $ogs_to_fix[$dn] = $attrs;
1724     }
1726     /* Copy source to destination dn */
1727     $ldap->cat($source);
1728     $new_data = $this->cleanup_array($ldap->fetch());
1729     $ldap->cd($destination);
1730     $res = $ldap->add($new_data);
1732     /* Display warning if copy failed */
1733     if(!$res){
1734       print_red(_("Failed to copy '%s' to '%s'. LDAP says '%s'."),$source,$destination,$ldap->get_error());
1735     }else{
1736       $res = $ldap->rmDir($source);
1737       show_ldap_error($ldap->get_error(),_("Something went wrong while copying dns."));
1739       /* Object is copied, so update its references */
1740       foreach($ogs_to_fix as $dn => $data){
1741         $ldap->cd($dn);
1742         $ldap->modify($data);
1743       }
1744     }
1745   }
1747   
1748   /* Cleanup ldap result to be able to write it be to ldap */
1749   function cleanup_array($attrs)
1750   {
1751     foreach($attrs as $key => $value) {
1752       if(is_numeric($key) || in_array($key,array("count","dn"))){
1753         unset($attrs[$key]);
1754       }
1755       if(is_array($value) && isset($value['count'])){
1756         unset($attrs[$key]['count']);
1757       }
1758     }
1759     return($attrs);
1760   }
1763 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1764 ?>