Code

Migration-> Admin -> Added error message if requested admin user already exists
[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']);
344     $res = $ldap->search("(objectClass=posixGroup)",array("dn"));
345     if(!$res){
346       $this->checks['outside_groups']['STATUS']    = FALSE;
347       $this->checks['outside_groups']['STATUS_MSG']= _("LDAP query failed");
348       $this->checks['outside_groups']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
349       return(false);
350     }
353     $this->outside_groups = array();
354     while($attrs = $ldap->fetch()){
355       if((!preg_match("/^[^,]+,".normalizePreg($group_ou)."/",$attrs['dn'])) && !preg_match("/,dc=addressbook,/",$attrs['dn'])){
356         $attrs['selected'] = FALSE;
357         $attrs['ldif']     = "";
358         $this->outside_groups[base64_encode($attrs['dn'])] = $attrs;
359       }
360     }
362     if(count($this->outside_groups)){
363       $this->checks['outside_groups']['STATUS']    = FALSE;
364       $this->checks['outside_groups']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
365       $this->checks['outside_groups']['ERROR_MSG'] = 
366         sprintf(_("Found %s groups outside the configured tree '%s'."),count($this->outside_groups),$group_ou);
367       $this->checks['outside_groups']['ERROR_MSG'].= "&nbsp;<input type='submit' name='outside_groups_dialog' value='"._("Move")."...'>";
368       return(false);
369     }else{
370       $this->checks['outside_groups']['STATUS']    = TRUE;
371       $this->checks['outside_groups']['STATUS_MSG']= _("Ok");
372       $this->checks['outside_groups']['ERROR_MSG'] = "";
373       return(TRUE);
374     }
375   }
378   /* Search for users outside the people ou 
379    */
380   function search_outside_users()
381   {
382     $cv = $this->parent->captured_values;
383     $ldap = new LDAP($cv['admin'],
384         $cv['password'],
385         $cv['connection'],
386         FALSE,
387         $cv['tls']);
388     $people_ou = $cv['peopleou'];
389     $ldap->cd($cv['base']);
390     $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))",array("dn"));
391     if(!$res){
392       $this->checks['outside_users']['STATUS']    = FALSE;
393       $this->checks['outside_users']['STATUS_MSG']= _("LDAP query failed");
394       $this->checks['outside_users']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
395       return(false);
396     }
399     $this->outside_users = array();
400     while($attrs = $ldap->fetch()){
401       if((!preg_match("/^[^,]+,".normalizePreg($people_ou)."/",$attrs['dn'])  && !preg_match("/,dc=addressbook,/",$attrs['dn']))){
402         $attrs['selected'] = FALSE;
403         $attrs['ldif']     = "";
404         $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
405       }
406     }
408     if(count($this->outside_users)){
409       $this->checks['outside_users']['STATUS']    = FALSE;
410       $this->checks['outside_users']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
411       $this->checks['outside_users']['ERROR_MSG'] = 
412         sprintf(_("Found %s user(s) outside the configured tree '%s'."),count($this->outside_users),$people_ou);
413       $this->checks['outside_users']['ERROR_MSG'].= "<input type='submit' name='outside_users_dialog' value='"._("Move")."...'>";
414       return(false);
415     }else{
416       $this->checks['outside_users']['STATUS']    = TRUE;
417       $this->checks['outside_users']['STATUS_MSG']= _("Ok");
418       $this->checks['outside_users']['ERROR_MSG'] = "";
419       return(TRUE);
420     }
421   }
424   /* Check ldap accessibility 
425    * Create and remove a dummy object, 
426    *  to ensure that we have the necessary permissions
427    */
428   function check_ldap_permissions()
429   {
430     $cv = $this->parent->captured_values;
431     $ldap = new LDAP($cv['admin'],
432         $cv['password'],
433         $cv['connection'],
434         FALSE,
435         $cv['tls']);
437     /* Create dummy entry 
438      */
439     $name     = "GOsa_setup_text_entry_".session_id().rand(0,999999);
440     $dn       = "ou=".$name.",".$cv['base'];
441     $testEntry= array();
442     $testEntry['objectClass'][]= "top";
443     $testEntry['objectClass'][]= "organizationalUnit";
444     $testEntry['objectClass'][]= "gosaDepartment";
445     $testEntry['description']= "Created by GOsa setup, this object can be removed.";
446     $testEntry['ou']  = $name;
448     /* check if simple ldap cat will be successful 
449      */
450     $res = $ldap->cat($cv['base']);  
451     if(!$res){
452       $this->checks['permissions']['STATUS']    = FALSE;
453       $this->checks['permissions']['STATUS_MSG']= _("LDAP query failed");
454       $this->checks['permissions']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
455       return(false);
456     }
457   
458     /* Try to create dummy object 
459      */ 
460     $ldap->cd ($dn);
461     $ldap->create_missing_trees($dn);
462     $res = $ldap->add($testEntry);
463     $ldap->cat($dn);
464     if(!$ldap->count()){
465       gosa_log($ldap->get_error());
466       $this->checks['permissions']['STATUS']    = FALSE;
467       $this->checks['permissions']['STATUS_MSG']= _("Failed");
468       $this->checks['permissions']['ERROR_MSG'] = 
469         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
470       return(false);
471     }
473     /* Try to remove created entry 
474      */
475     $res = $ldap->rmDir($dn);
476     $ldap->cat($dn);
477     if($ldap->count()){
478       gosa_log($ldap->get_error());
479       $this->checks['permissions']['STATUS']    = FALSE;
480       $this->checks['permissions']['STATUS_MSG']= _("Failed");
481       $this->checks['permissions']['ERROR_MSG'] = 
482         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
483       return(false);
484     }
486     /* Create & remove of dummy object was successful */
487     $this->checks['permissions']['STATUS']    = TRUE;
488     $this->checks['permissions']['STATUS_MSG']= _("Ok");
489     $this->checks['permissions']['ERROR_MSG'] = "";
490     return(true);
491   } 
494   /* Check if there are users which will 
495    *  be invisible for GOsa 
496    */
497   function check_gosaAccounts()
498   {
499     /* Remember old list of ivisible users, to be able to set 
500      *  the 'html checked' status for the checkboxes again 
501      */
502     $cnt_ok = 0;
503     $old    = $this->users_to_migrate;
504     $this->users_to_migrate = array();
506     /* Get collected configuration settings */
507     $cv = $this->parent->captured_values;
509     /* Establish ldap connection */
510     $ldap = new LDAP($cv['admin'],
511         $cv['password'],
512         $cv['connection'],
513         FALSE,
514         $cv['tls']);
516     /* Get all invisible users 
517      */
518     $ldap->cd($cv['base']); 
519     $res =$ldap->search("(&(|(objectClass=posixAccount)(&(objectClass=inetOrgPerson)(objectClass=organizationalPerson)))(!(objectClass=gosaAccount))(uid=*))",array("sn","givenName","cn","uid"));
520     while($attrs = $ldap->fetch()){
521       if(!preg_match("/,dc=addressbook,/",$attrs['dn'])){
522         $attrs['checked'] = FALSE;
523         $attrs['before']  = "";
524         $attrs['after']   = "";
526         /* Set objects to selected, that were selected before reload */
527         if(isset($old[base64_encode($attrs['dn'])])){
528           $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
529         }
530         $this->users_to_migrate[base64_encode($attrs['dn'])] = $attrs;
531       }
532     }
534     /* No invisible */
535     if(!$res){
536       $this->checks['users_visible']['STATUS']    = FALSE;
537       $this->checks['users_visible']['STATUS_MSG']= _("LDAP query failed");
538       $this->checks['users_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
539     }elseif(count($this->users_to_migrate) == 0){
540       $this->checks['users_visible']['STATUS']    = TRUE;
541       $this->checks['users_visible']['STATUS_MSG']= _("Ok");
542       $this->checks['users_visible']['ERROR_MSG'] = "";
543     }else{
544       $this->checks['users_visible']['STATUS']    = FALSE;
545       $this->checks['users_visible']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
546       $this->checks['users_visible']['ERROR_MSG'] = sprintf(_("Found %s user(s) that will not be visible in GOsa."), 
547           count($this->users_to_migrate));
548       $this->checks['users_visible']['ERROR_MSG'] .= "<input type='submit' name='users_visible_migrate' value='"._("Migrate")."...'>";
549     }
550   }
553   /* Start user account migration 
554    */  
555   function migrate_gosaAccounts($only_ldif = FALSE)
556   {
557     $this->show_details= $only_ldif;
559     /* Get collected configuration settings */
560     $cv = $this->parent->captured_values;
562     /* Establish ldap connection */
563     $ldap = new LDAP($cv['admin'],
564         $cv['password'],
565         $cv['connection'],
566         FALSE,
567         $cv['tls']);
569     /* Add gosaAccount objectClass to the selected users  
570      */
571     foreach($this->users_to_migrate as $key => $dep){
572       if($dep['checked']){
574         /* Get old objectClasses */
575         $ldap->cat($dep['dn'],array("objectClass"));
576         $attrs      = $ldap->fetch();
578         /* Create new objectClass array */
579         $new_attrs  = array();
580         $new_attrs['objectClass']= array("gosaAccount","inetOrgPerson","organizationalPerson");
581         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
582           if(!in_array_ics($attrs['objectClass'][$i], $new_attrs['objectClass'])){
583             $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
584           }
585         }
587         /* Set info attributes for current object, 
588          *  or write changes to the ldap database 
589          */
590         if($only_ldif){
591           $this->users_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
592           $this->users_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
593         }else{
594           $ldap->cd($attrs['dn']);
595           if(!$ldap->modify($new_attrs)){
596             print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
597             return(false);
598           }
599         }
600       }
601     }
602     return(TRUE);
603   }
606   /* Check if there are invisible organizational Units 
607    */
608   function check_organizationalUnits()
609   {
610     $cnt_ok = 0;
611     $old = $this->deps_to_migrate;
612     $this->deps_to_migrate = array();
614     /* Get collected configuration settings */
615     $cv = $this->parent->captured_values;
617     /* Establish ldap connection */
618     $ldap = new LDAP($cv['admin'],
619         $cv['password'],
620         $cv['connection'],
621         FALSE,
622         $cv['tls']);
624     /* Skip GOsa internal departments */
625     $skip_dns = array("/^ou=people,/","/^ou=groups,/","/(,|)ou=configs,/","/(,|)ou=systems,/",
626         "/^ou=apps,/","/^ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
627         "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
628         "/(,|)ou=winstations,/");
631     /* Get all invisible departments */
632     $ldap->cd($cv['base']); 
633     $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
634     while($attrs = $ldap->fetch()){
635       $attrs['checked'] = FALSE;
636       $attrs['before']  = "";
637       $attrs['after']   = "";
639       /* Set objects to selected, that were selected before reload */
640       if(isset($old[base64_encode($attrs['dn'])])){
641         $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
642       }
643       $this->deps_to_migrate[base64_encode($attrs['dn'])] = $attrs;
644     }
646     /* Filter returned list of departments and ensure that 
647      *  GOsa internal departments will not be listed 
648      */
649     foreach($this->deps_to_migrate as $key => $attrs){
650       $dn = $attrs['dn'];
651       $skip = false;
652       foreach($skip_dns as $skip_dn){
653         if(preg_match($skip_dn,$dn)){
654           $skip = true;
655         }
656       }
657       if($skip){
658         unset($this->deps_to_migrate[$key]);
659       }
660     }
662     /* If we have no invisible departments found  
663      *  tell the user that everything is ok 
664      */
665     if(!$res){
666       $this->checks['deps_visible']['STATUS']    = FALSE;
667       $this->checks['deps_visible']['STATUS_MSG']= _("LDAP query failed");
668       $this->checks['deps_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
669     }elseif(count($this->deps_to_migrate) == 0 ){
670       $this->checks['deps_visible']['STATUS']    = TRUE;
671       $this->checks['deps_visible']['STATUS_MSG']= _("Ok");
672       $this->checks['deps_visible']['ERROR_MSG'] = "";
673     }else{
674       $this->checks['deps_visible']['STATUS']    = TRUE;
675       $this->checks['deps_visible']['STATUS_MSG']= '<font style="color:#FFA500">'._("Warning").'</font>';
676       $this->checks['deps_visible']['ERROR_MSG'] = sprintf(_("Found %s department(s) that will not be visible in GOsa."),count($this->deps_to_migrate));
677       $this->checks['deps_visible']['ERROR_MSG'] .= "&nbsp;<input type='submit' name='deps_visible_migrate' value='"._("Migrate")."...'>";
678     }
679   }
683   /* Start deparmtment migration */  
684   function migrate_organizationalUnits($only_ldif = FALSE)
685   {
686     $this->show_details= $only_ldif;
688     /* Get collected configuration settings */
689     $cv = $this->parent->captured_values;
691     /* Establish ldap connection */
692     $ldap = new LDAP($cv['admin'],
693         $cv['password'],
694         $cv['connection'],
695         FALSE,
696         $cv['tls']);
698     /* Add gosaDepartment objectClass to each selected entry 
699      */
700     foreach($this->deps_to_migrate as $key => $dep){
701       if($dep['checked']){
703         /* Get current objectClasses */
704         $ldap->cat($dep['dn'],array("objectClass","description"));
705         $attrs      = $ldap->fetch();
707         /* Create new objectClass attribute including gosaDepartment*/
708         $new_attrs  = array();
709         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
710           $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
711         }
712         $new_attrs['objectClass'][] = "gosaDepartment";
714         /* Append description it is missing */
715         if(!isset($attrs['description'])){
716           $new_attrs['description'][] = "GOsa department";
717         }
719         /* Depending on the parameter >only_diff< we save the changes as ldif
720          *  or we write our changes directly to the ldap database
721          */
722         if($only_ldif){
723           $this->deps_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
724           $this->deps_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
725         }else{
726           $ldap->cd($attrs['dn']);
727           if(!$ldap->modify($new_attrs)){
728             print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
729             return(false);
730           }
731         }
732       }
733     }
734     return(TRUE);
735   }
738   /* Check Acls if there is at least one object with acls defined 
739    */
740   function check_administrativeAccount()
741   {
742     /* Establish ldap connection */
743     $cv = $this->parent->captured_values;
744     $ldap = new LDAP($cv['admin'],
745         $cv['password'],
746         $cv['connection'],
747         FALSE,
748         $cv['tls']);
750     /* Search for gosaAcls */ 
751     $ldap->cd($cv['base']);
752     $res = $ldap->cat($cv['base']);
753     
754     if(!$res){
755       $this->checks['acls']['STATUS']    = FALSE;
756       $this->checks['acls']['STATUS_MSG']= _("LDAP query failed");
757       $this->checks['acls']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
758     }else{
759       $found = false;
760       $username = "";
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           if($tmp[1] == "psub"){
768             $members = split(",",$tmp[2]);
769             foreach($members as $member){
770               $member = base64_decode($member);
772               /* Check if acl owner is a valid GOsa user account */
773               $ldap->cat($member,array("objectClass","uid","cn"));
774               $ret = $ldap->fetch();
776               if(isset($ret['objectClass']) && in_array("posixGroup",$ret['objectClass'])){
777                 $found = TRUE;
778                 $username .= "ACL-Group:&nbsp;".$ret['cn'][0]."<br>";
779               }elseif(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
780                   in_array("organizationalPerson",$ret['objectClass']) &&
781                   in_array("inetOrgPerson",$ret['objectClass'])){
782                 $found = TRUE;
783                 $username .= "ACL:&nbsp;".$ret['uid'][0]."<br>";
784               }
785             }
786           }elseif($tmp[1] == "role"){
788             /* Check if acl owner is a valid GOsa user account */
789             $ldap->cat(base64_decode($tmp[2]),array("gosaAclTemplate"));
790             $ret = $ldap->fetch();
792             if(isset($ret['gosaAclTemplate'])){
793               $cnt = $ret['gosaAclTemplate']['count'];
794               for($e = 0 ; $e < $cnt ; $e++){
796                 $a_str = $ret['gosaAclTemplate'][$e];
797                 if(preg_match("/^[0-9]*:psub:/",$a_str) && preg_match("/:all;cmdrw$/",$a_str)){
799                   $members = split(",",$tmp[3]);
800                   foreach($members as $member){
801                     $member = base64_decode($member);
803                     /* Check if acl owner is a valid GOsa user account */
804                     $ldap->cat($member,array("objectClass","uid"));
805                     $ret = $ldap->fetch();
806   
807                     if(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
808                         in_array("organizationalPerson",$ret['objectClass']) &&
809                         in_array("inetOrgPerson",$ret['objectClass'])){
810                       $found = TRUE;
811                       $username .= "ACL Role:&nbsp;".$ret['uid'][0]."<br>";
812                     }
813                   }
814                 }
815               }
816             }
817           }
818         }
819       }
821       # For debugging
822       #echo $username;
824       if($found){
825         $this->checks['acls']['STATUS']    = TRUE;
826         $this->checks['acls']['STATUS_MSG']= _("Ok");
827         $this->checks['acls']['ERROR_MSG'] = "";
828       }else{
829         $this->checks['acls']['STATUS']    = FALSE;
830         $this->checks['acls']['STATUS_MSG']= _("Failed");
831         $this->checks['acls']['ERROR_MSG']= _("There is no GOsa administrator account inside your LDAP.")."&nbsp;";
832         $this->checks['acls']['ERROR_MSG'].= "<input type='submit' name='create_acls' value='"._("Create")."'>";
833       }
834     }
835     return($ldap->count()>=1);
836   }
840   function create_admin($only_ldif = FALSE)
841   {
842     /* Reset '' */
843     $this->acl_create_changes="";
845     /* Object that should receive admin acls */
846     $dn = $this->acl_create_selected;
848     /* Get collected configuration settings */
849     $cv = $this->parent->captured_values;
851     /* Establish ldap connection */
852     $ldap = new LDAP($cv['admin'],
853         $cv['password'],
854         $cv['connection'],
855         FALSE,
856         $cv['tls']);
858     /* Get current base attributes */
859     $ldap->cd($cv['base']);
860     $ldap->cat($cv['base'],array("dn","objectClass","gosaAclEntry"));
861     $attrs = $ldap->fetch();
863     /* Add acls for the selcted user to the base */
864     $attrs_new['objectClass'] = array("gosaACL");
866     for($i = 0; $i < $attrs['objectClass']['count']; $i ++){
867       if(!in_array_ics($attrs['objectClass'][$i],$attrs_new['objectClass'])){
868         $attrs_new['objectClass'][] = $attrs['objectClass'][$i];
869       }
870     }
872     $acl = "0:psub:".base64_encode($dn).":all;cmdrw";    
873     $attrs_new['gosaAclEntry'][] = $acl;
874     if(isset($attrs['gosaAclEntry'])){
875       for($i = 0 ; $i < $attrs['gosaAclEntry']['count']; $i ++){
876           
877         $prio = preg_replace("/[:].*$/","",$attrs['gosaAclEntry'][$i]);
878         $rest = preg_replace("/^[^:]/","",$attrs['gosaAclEntry'][$i]);
879  
880         $data = ($prio+1).$rest;
881         $attrs_new['gosaAclEntry'][] = $data;
882       }
883     }
885     if($only_ldif){
886       $this->acl_create_changes ="\n".$cv['base']."\n";
887       $this->acl_create_changes.=$this->array_to_ldif($attrs)."\n";
888       $this->acl_create_changes.="\n".$cv['base']."\n";
889       $this->acl_create_changes.=$this->array_to_ldif($attrs_new);
890     }else{
891    
892       $ldap->cd($cv['base']);
893       if(!$ldap->modify($attrs_new)){
894         print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
895         return(FALSE);
896       }else{
897         return(TRUE);
898       }
899     }
900   }
901  
902   
903   function create_admin_user()
904   {
905     $pw1 = $pw2 = "";
906     $uid = "";
907   
908     if(isset($_POST['new_user_uid'])){
909       $uid = $_POST['new_user_uid'];
910     }
911     if(isset($_POST['new_user_password'])){
912       $pw1 = $_POST['new_user_password'];
913     }
914     if(isset($_POST['new_user_password2'])){
915       $pw2 = $_POST['new_user_password2'];
916     }
917   
918     if(empty($pw1) || empty($pw2) | ($pw1 != $pw2)){
919       print_red(_("Specified passwords are empty or not equal."));
920       return false;
921     }
922  
923     if(!is_uid($uid) || empty($uid)){
924       print_red(_("Please specify a valid uid."));
925       return false;
926     }
927     
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     /* Get current base attributes */
938     $ldap->cd($cv['base']);
939   
940     if($cv['peopledn'] == "cn"){
941       $dn = "cn=System Administrator,".$cv['peopleou'].",".$cv['base'];
942     }else{
943       $dn = "uid=".$uid.",".$cv['peopleou'].",".$cv['base'];
944     }
946     $methods = @passwordMethod::get_available_methods_if_not_loaded();
947     $p_m = $methods[$cv['encryption']];
948     $p_c = new $p_m(array());
949     $hash = $p_c->generate_hash($pw2);
951     $new_user=array();
952     $new_user['objectClass']= array("top","person","gosaAccount","organizationalPerson","inetOrgPerson");
953     $new_user['givenName']  = "System";
954     $new_user['sn']  = "Administrator";
955     $new_user['cn']  = "System Administrator";
956     $new_user['uid'] = $uid;
957     $new_user['userPassword'] = $hash;
958    
959     $ldap->cd($cv['base']);
960   
961     $ldap->cat($dn,array("dn"));
962     if($ldap->count()){
963       print_red(sprintf(_("Could not add administrative user, there is already an object with the same dn '%s' in your ldap database."),$dn));
964       return(FALSE);  
965     }
967     $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$dn));
968     $ldap->cd($dn);  
969     $res = $ldap->add($new_user);
970     $this->acl_create_selected = $dn;
971     $this->create_admin();
972     
973     if(!$res){
974       print_red($ldap->get_error());
975       return(FALSE);
976     }
977   
978     $this->acl_create_dialog=FALSE;        
979     $this->check_administrativeAccount();
980     return(TRUE);
981   }
982  
984   function migrate_outside_winstations($perform = FALSE)
985   {
986     /* Establish ldap connection */
987     $cv = $this->parent->captured_values;
988     $ldap = new LDAP($cv['admin'],
989         $cv['password'],
990         $cv['connection'],
991         FALSE,
992         $cv['tls']);
994     $ldap->cd($cv['base']);
996     /* Check if there was a destination department posted */
997     if(isset($_POST['move_winstation_to'])){
998       $destination_dep = $_POST['move_winstation_to'];
999     }else{
1000       print_red(_("Couldn't move users to specified department."));
1001       return(false);
1002     }
1003  
1004     foreach($this->outside_winstations as $b_dn => $data){
1005       $this->outside_winstations[$b_dn]['ldif'] ="";
1006       if($data['selected']){
1007         $dn = base64_decode($b_dn);
1008         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1009         if(!$perform){
1010           $this->outside_winstations[$b_dn]['ldif'] = _("Winstation will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1013           /* Check if there are references to this object */
1014           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1015           $refs = "";
1016           while($attrs = $ldap->fetch()){
1017             $ref_dn = $attrs['dn'];
1018             $refs .= "<br />\t".$ref_dn;
1019           } 
1020           if(!empty($refs)){ 
1021             $this->outside_winstations[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1022           }
1024         }else{
1025           $this->move($dn,$d_dn);
1026         }
1027       }
1028     }
1029   }
1030   
1032   function migrate_outside_groups($perform = FALSE)
1033   {
1034     /* Establish ldap connection */
1035     $cv = $this->parent->captured_values;
1036     $ldap = new LDAP($cv['admin'],
1037         $cv['password'],
1038         $cv['connection'],
1039         FALSE,
1040         $cv['tls']);
1042     $ldap->cd($cv['base']);
1044     /* Check if there was a destination department posted */
1045     if(isset($_POST['move_group_to'])){
1046       $destination_dep = $_POST['move_group_to'];
1047     }else{
1048       print_red(_("Couldn't move users to specified department."));
1049       return(false);
1050     }
1051  
1052     foreach($this->outside_groups as $b_dn => $data){
1053       $this->outside_groups[$b_dn]['ldif'] ="";
1054       if($data['selected']){
1055         $dn = base64_decode($b_dn);
1056         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1057         if(!$perform){
1058           $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1061           /* Check if there are references to this object */
1062           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1063           $refs = "";
1064           while($attrs = $ldap->fetch()){
1065             $ref_dn = $attrs['dn'];
1066             $refs .= "<br />\t".$ref_dn;
1067           } 
1068           if(!empty($refs)){ 
1069             $this->outside_groups[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1070           }
1072         }else{
1073           $this->move($dn,$d_dn);
1074         }
1075       }
1076     }
1077   }
1078   
1080   function migrate_outside_users($perform = FALSE)
1081   {
1082     /* Establish ldap connection */
1083     $cv = $this->parent->captured_values;
1084     $ldap = new LDAP($cv['admin'],
1085         $cv['password'],
1086         $cv['connection'],
1087         FALSE,
1088         $cv['tls']);
1090     $ldap->cd($cv['base']);
1092     /* Check if there was a destination department posted */
1093     if(isset($_POST['move_user_to'])){
1094       $destination_dep = $_POST['move_user_to'];
1095     }else{
1096       print_red(_("Couldn't move users to specified department."));
1097       return(false);
1098     }
1099  
1100     foreach($this->outside_users as $b_dn => $data){
1101       $this->outside_users[$b_dn]['ldif'] ="";
1102       if($data['selected']){
1103         $dn = base64_decode($b_dn);
1104         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1105         if(!$perform){
1106           $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1108           /* Check if there are references to this object */
1109           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1110           $refs = "";
1111           while($attrs = $ldap->fetch()){
1112             $ref_dn = $attrs['dn'];
1113             $refs .= "<br />\t".$ref_dn;
1114           } 
1115           if(!empty($refs)){ 
1116             $this->outside_users[$b_dn]['ldif'] .= "<br /><br /><i>"._("The following references will be updated").":</i>".$refs;
1117           }
1119         }else{
1120           $this->move($dn,$d_dn);
1121         }
1122       }
1123     }
1124   }
1125   
1127   function execute()
1128   {
1129     /* Initialise checks if this is the first call */
1130     if(!$this->checks_initialised || isset($_POST['reload'])){
1131       $this->initialize_checks();
1132       $this->checks_initialised = TRUE;
1133     }
1135     /*************
1136      * Winstations outside the group ou 
1137      *************/
1138     
1139     if(isset($_POST['outside_winstations_dialog_cancel'])){
1140       $this->outside_winstations_dialog = FALSE;
1141       $this->dialog = FALSE;
1142       $this->show_details = FALSE;
1143     }
1144    
1145     if(isset($_POST['outside_winstations_dialog_whats_done'])){
1146       $this->migrate_outside_winstations(FALSE);
1147     }
1148  
1149     if(isset($_POST['outside_winstations_dialog_perform'])){
1150       $this->migrate_outside_winstations(TRUE);
1151       $this->search_outside_winstations();
1152       $this->dialog = FALSE;
1153       $this->show_details = FALSE;
1154       $this->outside_winstations_dialog = FALSE;
1155     }
1157     if(isset($_POST['outside_winstations_dialog'])){
1158       $this->outside_winstations_dialog = TRUE;
1159       $this->dialog = TRUE;
1160     }
1161     
1162     if($this->outside_winstations_dialog){
1163       $smarty = get_smarty();
1164       $smarty->assign("ous",$this->get_all_winstation_ous());
1165       $smarty->assign("method","outside_winstations");
1166       $smarty->assign("outside_winstations",$this->outside_winstations);
1167       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1168     }
1169     /*************
1170      * Groups outside the group ou 
1171      *************/
1172     
1173     if(isset($_POST['outside_groups_dialog_cancel'])){
1174       $this->outside_groups_dialog = FALSE;
1175       $this->show_details = FALSE;
1176       $this->dialog = FALSE;
1177     }
1178    
1179     if(isset($_POST['outside_groups_dialog_whats_done'])){
1180       $this->show_details= TRUE;
1181       $this->migrate_outside_groups(FALSE);
1182     }
1183  
1184     if(isset($_POST['outside_groups_dialog_refresh'])){
1185       $this->show_details= FALSE;
1186     }
1188     if(isset($_POST['outside_groups_dialog_perform'])){
1189       $this->migrate_outside_groups(TRUE);
1190       $this->dialog = FALSE;
1191       $this->show_details = FALSE;
1192       $this->outside_groups_dialog = FALSE;
1193       $this->initialize_checks();
1194     }
1196     if(isset($_POST['outside_groups_dialog'])){
1197       $this->outside_groups_dialog = TRUE;
1198       $this->dialog = TRUE;
1199     }
1200     
1201     if($this->outside_groups_dialog){
1202       $smarty = get_smarty();
1203       $smarty->assign("ous",$this->get_all_group_ous());
1204       $smarty->assign("method","outside_groups");
1205       $smarty->assign("outside_groups",$this->outside_groups);
1206       $smarty->assign("group_details", $this->show_details);
1207       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1208     }
1209  
1210     /*************
1211      * User outside the people ou 
1212      *************/
1213     
1214     if(isset($_POST['outside_users_dialog_cancel'])){
1215       $this->outside_users_dialog = FALSE;
1216       $this->dialog = FALSE;
1217       $this->show_details = FALSE;
1218     }
1219    
1220     if(isset($_POST['outside_users_dialog_whats_done'])){
1221       $this->show_details= TRUE;
1222       $this->migrate_outside_users(FALSE);
1223     }
1224  
1225     if(isset($_POST['outside_users_dialog_perform'])){
1226       $this->migrate_outside_users(TRUE);
1227       $this->initialize_checks();
1228       $this->dialog = FALSE;
1229       $this->show_details = FALSE;
1230       $this->outside_users_dialog = FALSE;
1231     }
1233     if (isset($_POST['outside_users_dialog_refresh'])){
1234       $this->show_details= FALSE;
1235     }
1237     if(isset($_POST['outside_users_dialog'])){
1238       $this->outside_users_dialog = TRUE;
1239       $this->dialog = TRUE;
1240     }
1241     
1242     if($this->outside_users_dialog){
1243       $smarty = get_smarty();
1244       $smarty->assign("ous",$this->get_all_people_ous());
1245       $smarty->assign("method","outside_users");
1246       $smarty->assign("outside_users",$this->outside_users);
1247       $smarty->assign("user_details", $this->show_details);
1248       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1249     }
1250  
1251     /*************
1252      * Root object check  
1253      *************/
1254   
1255     if(isset($_POST['retry_root_create'])){
1257       $state = $this->checks['root']['STATUS'];
1258       $this->checkBase(FALSE);
1259       if($state != $this->checks['root']['STATUS']){
1260         $this->initialize_checks();
1261       }
1262     }
1263  
1264     /*************
1265      * User Migration handling 
1266      *************/
1268     if(isset($_POST['retry_acls'])){
1269       $this->check_administrativeAccount();
1270     }
1272     if(isset($_POST['create_acls'])){
1273       $this->acl_create_dialog = TRUE;
1274       $this->dialog = TRUE;
1275     }
1276   
1277     if(isset($_POST['create_acls_cancel'])){
1278       $this->acl_create_dialog = FALSE;
1279       $this->dialog = FALSE;
1280       $this->show_details = FALSE;
1281     }
1283 #    if(isset($_POST['create_acls_create_confirmed'])){
1284 #      if($this->create_admin()){
1285 #        $this->acl_create_dialog = FALSE;
1286 #        $this->dialog = FALSE;
1287 #      $this->show_details = FALSE;
1288 #        $this->initialize_checks();
1289 #      }
1290 #    }
1292     if(isset($_POST['create_acls_create'])){
1293       $this->create_admin(TRUE);
1294     }
1296     if(isset($_POST['create_admin_user'])){
1297       if($this->create_admin_user()){
1298         $this->dialog = FALSE;
1299       $this->show_details = FALSE;
1300       }
1301     }
1303     if($this->acl_create_dialog){
1304       $smarty = get_smarty();
1305   
1306       $uid = "admin";
1307       if(isset($_POST['new_user_uid'])){
1308         $uid = $_POST['new_user_uid'];
1309       }
1311       $smarty->assign("new_user_uid",$uid);
1312       $smarty->assign("new_user_password",@$_POST['new_user_password']);
1313       $smarty->assign("new_user_password2",@$_POST['new_user_password2']);
1314       $smarty->assign("method","create_acls");
1315       $smarty->assign("acl_create_selected",$this->acl_create_selected);
1316       $smarty->assign("what_will_be_done_now",$this->acl_create_changes);
1317       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1318     }
1320     /*************
1321      * User Migration handling 
1322      *************/
1324     /* Refresh list of deparments */
1325     if(isset($_POST['users_visible_migrate_refresh'])){
1326       $this->check_gosaAccounts();
1327     }
1329     /* Open migration dialog */
1330     if(isset($_POST['users_visible_migrate'])){
1331       $this->show_details= FALSE;
1332       $this->users_migration_dialog = TRUE;
1333       $this->dialog =TRUE;
1334     }
1336     /* Close migration dialog */
1337     if(isset($_POST['users_visible_migrate_close'])){
1338       $this->users_migration_dialog = FALSE;
1339       $this->dialog =FALSE;
1340       $this->show_details = FALSE;
1341     }
1343     /* Start migration */
1344     if(isset($_POST['users_visible_migrate_migrate'])){
1345       if($this->migrate_gosaAccounts()){
1346         $this->initialize_checks();
1347         $this->dialog = FALSE;
1348         $this->show_details = FALSE;
1349         $this->users_migration_dialog = FALSE;
1350       }
1351     }
1353     /* Start migration */
1354     if(isset($_POST['users_visible_migrate_whatsdone'])){
1355       $this->migrate_gosaAccounts(TRUE);
1356     }
1358     /* Display migration dialog */
1359     if($this->users_migration_dialog){
1360       $smarty = get_smarty();
1361       $smarty->assign("users_to_migrate",$this->users_to_migrate);
1362       $smarty->assign("method","migrate_users");
1363       $smarty->assign("user_details", $this->show_details);
1364       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1365     }
1368     /*************
1369      * Department Migration handling 
1370      *************/
1372     /* Refresh list of deparments */
1373     if(isset($_POST['deps_visible_migrate_refresh'])){
1374       $this->check_organizationalUnits();
1375       $this->show_details= FALSE;
1376     }
1378     /* Open migration dialog */
1379     if(isset($_POST['deps_visible_migrate'])){
1380       $this->dep_migration_dialog = TRUE;
1381       $this->dialog =TRUE;
1382     }
1384     /* Close migration dialog */
1385     if(isset($_POST['deps_visible_migrate_close'])){
1386       $this->dep_migration_dialog = FALSE;
1387       $this->dialog =FALSE;
1388       $this->show_details = FALSE;
1389     }
1391     /* Start migration */
1392     if(isset($_POST['deps_visible_migrate_migrate'])){
1393       if($this->migrate_organizationalUnits()){
1394         $this->show_details= FALSE;
1395         $this->check_organizationalUnits();
1396         $this->dialog = FALSE;
1397         $this->dep_migration_dialog = FALSE;
1398       }
1399     }
1401     /* Start migration */
1402     if(isset($_POST['deps_visible_migrate_whatsdone'])){
1403       $this->migrate_organizationalUnits(TRUE);
1404     }
1406     /* Display migration dialog */
1407     if($this->dep_migration_dialog){
1408       $smarty = get_smarty();
1409       $smarty->assign("deps_to_migrate",$this->deps_to_migrate);
1410       $smarty->assign("method","migrate_deps");
1411       $smarty->assign("deps_details", $this->show_details);
1412       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1413     }
1415     $smarty = get_smarty();
1416     $smarty->assign("checks",$this->checks);
1417     $smarty->assign("method","default");
1418     return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1419   }
1422   function save_object()
1423   {
1424     $this->is_completed= TRUE;
1426     /* Capture all selected winstations from outside_winstations_dialog */
1427     if($this->outside_winstations_dialog){
1428       foreach($this->outside_winstations as $dn => $data){
1429         if(isset($_POST['select_winstation_'.$dn])){
1430           $this->outside_winstations[$dn]['selected'] = TRUE;
1431         }else{
1432           $this->outside_winstations[$dn]['selected'] = FALSE;
1433         }
1434       }
1435     }
1437     /* Capture all selected groups from outside_groups_dialog */
1438     if($this->outside_groups_dialog){
1439       foreach($this->outside_groups as $dn => $data){
1440         if(isset($_POST['select_group_'.$dn])){
1441           $this->outside_groups[$dn]['selected'] = TRUE;
1442         }else{
1443           $this->outside_groups[$dn]['selected'] = FALSE;
1444         }
1445       }
1446     }
1448     /* Capture all selected users from outside_users_dialog */
1449     if($this->outside_users_dialog){
1450       foreach($this->outside_users as $dn => $data){
1451         if(isset($_POST['select_user_'.$dn])){
1452           $this->outside_users[$dn]['selected'] = TRUE;
1453         }else{
1454           $this->outside_users[$dn]['selected'] = FALSE;
1455         }
1456       }
1457     }
1459     /* Get "create acl" dialog posts */
1460     if($this->acl_create_dialog){
1462       if(isset($_POST['create_acls_create_abort'])){
1463         $this->acl_create_selected = "";
1464       }
1465     }
1467     /* Get selected departments */
1468     if($this->dep_migration_dialog){
1469       foreach($this->deps_to_migrate as $id => $data){
1470         if(isset($_POST['migrate_'.$id])){
1471           $this->deps_to_migrate[$id]['checked'] = TRUE;
1472         }else{
1473           $this->deps_to_migrate[$id]['checked'] = FALSE;
1474         }
1475       }
1476     }
1478     /* Get selected users */
1479     if($this->users_migration_dialog){
1480       foreach($this->users_to_migrate as $id => $data){
1481         if(isset($_POST['migrate_'.$id])){
1482           $this->users_to_migrate[$id]['checked'] = TRUE;
1483         }else{
1484           $this->users_to_migrate[$id]['checked'] = FALSE;
1485         }
1486       }
1487     }
1488   }
1491   /* Check if the root object exists.
1492    * If the parameter just_check is true, then just check if the 
1493    *  root object is missing and update the info messages.
1494    * If the Parameter is false, try to create a new root object.
1495    */
1496   function checkBase($just_check = TRUE)
1497   {
1498     /* Get collected setup informations */
1499     $cv = $this->parent->captured_values;
1501     /* Establish ldap connection */
1502     $ldap = new LDAP($cv['admin'],
1503         $cv['password'],
1504         $cv['connection'],
1505         FALSE,
1506         $cv['tls']);
1508     /* Check if root object exists */
1509     $ldap->cd($cv['base']);
1510     $res = $ldap->search("(objectClass=*)");
1511     $err = ldap_errno($ldap->cid); 
1513     if( !$res || 
1514         $err == 0x20 ||  # LDAP_NO_SUCH_OBJECT
1515         $err == 0x40) {  # LDAP_NAMING_VIOLATION
1517       /* Root object doesn't exists 
1518        */
1519       if($just_check){
1520         $this->checks['root']['STATUS']    = FALSE;
1521         $this->checks['root']['STATUS_MSG']= _("Failed");
1522         $this->checks['root']['ERROR_MSG'] =  _("The LDAP root object is missing. It is required to use your LDAP service.").'&nbsp;';
1523         $this->checks['root']['ERROR_MSG'].=  "<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1524         return(FALSE);
1525       }else{
1527         /* Add root object */ 
1528         $ldap->cd($cv['base']);
1529         $res = $ldap->create_missing_trees($cv['base']);
1531         /* If adding failed, tell the user */
1532         if(!$res){
1533           $this->checks['root']['STATUS']    = FALSE;
1534           $this->checks['root']['STATUS_MSG']= _("Failed");
1535           $this->checks['root']['ERROR_MSG'] = _("Root object couldn't be created, you should try it on your own.");
1536           $this->checks['root']['ERROR_MSG'].= "&nbsp;<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1537           return($res);;
1538         }
1539       }
1540     }
1542     /* Create & remove of dummy object was successful */
1543     $this->checks['root']['STATUS']    = TRUE;
1544     $this->checks['root']['STATUS_MSG']= _("Ok");
1545   }
1548   /* Return ldif information for a 
1549    * given attribute array 
1550    */
1551   function array_to_ldif($atts)
1552   {
1553     $ret = "";
1554     unset($atts['count']);
1555     unset($atts['dn']);
1556     foreach($atts as $name => $value){
1557       if(is_numeric($name)) {
1558         continue;
1559       }
1560       if(is_array($value)){
1561         unset($value['count']);
1562         foreach($value as $a_val){
1563           $ret .= $name.": ". $a_val."\n";
1564         }
1565       }else{
1566         $ret .= $name.": ". $value."\n";
1567       }
1568     }
1569     return(preg_replace("/\n$/","",$ret));
1570   }
1573   function get_user_list()
1574   {
1575     /* Get collected configuration settings */
1576     $cv = $this->parent->captured_values;
1578     /* Establish ldap connection */
1579     $ldap = new LDAP($cv['admin'],
1580         $cv['password'],
1581         $cv['connection'],
1582         FALSE,
1583         $cv['tls']);
1584     
1585     $ldap->cd($cv['base']);
1586     $ldap->search("(objectClass=gosaAccount)",array("dn"));
1587   
1588     $tmp = array();
1589     while($attrs = $ldap->fetch()){
1590       $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1591     }
1592     return($tmp);
1593   }
1596   function get_all_people_ous()
1597   {
1598     /* Get collected configuration settings */
1599     $cv = $this->parent->captured_values;
1601     /* Establish ldap connection */
1602     $ldap = new LDAP($cv['admin'],
1603         $cv['password'],
1604         $cv['connection'],
1605         FALSE,
1606         $cv['tls']);
1608     $ldap->cd($cv['base']);
1609     $ldap->search("(".$cv['peopleou'].")",array("dn"));
1610   
1611     if($ldap->count() == 0 ){
1612       $add_dn = $cv['peopleou'].",".$cv['base'];
1613       $naming_attr = preg_replace("/=.*$/","",$add_dn);
1614       $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1615       $add = array();
1616       $add['objectClass'] = array("organizationalUnit");
1617       $add[$naming_attr] = $naming_value;
1619       $ldap->cd($cv['base']);
1620       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1621       $ldap->cd($add_dn);
1622       $ldap->add($add);
1623     }
1625     $ldap->search("(".$cv['peopleou'].")",array("dn"));
1626     $tmp = array();
1627     while($attrs= $ldap->fetch()){
1628       if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1629         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1630       }
1631     }
1632     return($tmp); 
1633   }
1635   function get_all_winstation_ous()
1636   {
1637     /* Get collected configuration settings */
1638     $cv = $this->parent->captured_values;
1640     /* Establish ldap connection */
1641     $ldap = new LDAP($cv['admin'],
1642         $cv['password'],
1643         $cv['connection'],
1644         FALSE,
1645         $cv['tls']);
1647     /* Get winstation ou */
1648     if($cv['generic_settings']['wws_ou_active']) {
1649       $winstation_ou = $cv['generic_settings']['ws_ou'];
1650     }else{
1651       $winstation_ou = "ou=winstations";
1652     }
1654     $ldap->cd($cv['base']);
1655     $ldap->search("(".$winstation_ou.")",array("dn"));
1656   
1657     if($ldap->count() == 0 ){
1658       $add_dn = $winstation_ou.",ou=systems,".$cv['base'];
1659       $naming_attr = preg_replace("/=.*$/","",$add_dn);
1660       $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1661       $add = array();
1662       $add['objectClass'] = array("organizationalUnit");
1663       $add[$naming_attr] = $naming_value;
1665       $ldap->cd($cv['base']);
1666       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1667       $ldap->cd($add_dn);
1668       $ldap->add($add);
1669     }
1671     $ldap->search("(".$winstation_ou.")",array("dn"));
1672     $tmp = array();
1673     while($attrs= $ldap->fetch()){
1674       if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1675         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1676       }
1677     }
1678     return($tmp); 
1679   }
1682   function get_all_group_ous()
1683   {
1684     /* Get collected configuration settings */
1685     $cv = $this->parent->captured_values;
1687     /* Establish ldap connection */
1688     $ldap = new LDAP($cv['admin'],
1689         $cv['password'],
1690         $cv['connection'],
1691         FALSE,
1692         $cv['tls']);
1694     $ldap->cd($cv['base']);
1695     $ldap->search("(".$cv['groupou'].")",array("dn"));
1696   
1697     if($ldap->count() == 0 ){
1698       $add_dn = $cv['groupou'].",".$cv['base'];
1699       $naming_attr = preg_replace("/=.*$/","",$add_dn);
1700       $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1701       $add = array();
1702       $add['objectClass'] = array("organizationalUnit");
1703       $add[$naming_attr] = $naming_value;
1705       $ldap->cd($cv['base']);
1706       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1707       $ldap->cd($add_dn);
1708       $ldap->add($add);
1709     }
1711     $ldap->search("(".$cv['groupou'].")",array("dn"));
1712     $tmp = array();
1713     while($attrs= $ldap->fetch()){
1714       if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1715         $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1716       }
1717     }
1718     return($tmp); 
1719   }
1722   function get_group_list()
1723   {
1724     /* Get collected configuration settings */
1725     $cv = $this->parent->captured_values;
1727     /* Establish ldap connection */
1728     $ldap = new LDAP($cv['admin'],
1729         $cv['password'],
1730         $cv['connection'],
1731         FALSE,
1732         $cv['tls']);
1733     
1734     $ldap->cd($cv['base']);
1735     $ldap->search("(objectClass=posixGroup)",array("dn"));
1736   
1737     $tmp = array();
1738     while($attrs = $ldap->fetch()){
1739       $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1740     }
1741     return($tmp);
1742   }
1745   function move($source,$destination)
1746   {
1747     /* Get collected configuration settings */
1748     $cv = $this->parent->captured_values;
1750     /* Establish ldap connection */
1751     $ldap = new LDAP($cv['admin'],
1752         $cv['password'],
1753         $cv['connection'],
1754         FALSE,
1755         $cv['tls']);
1757      /* Update object references in gosaGroupOfNames */
1758     $ogs_to_fix = array();
1759     $ldap->cd($cv['base']);
1760     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($source).'))', array('cn','member'));
1761     while ($attrs= $ldap->fetch()){
1762       $dn = $attrs['dn'];
1763       $attrs = $this->cleanup_array($attrs);
1764       $member_new = array($destination);
1765       foreach($attrs['member'] as $member){
1766         if($member != $source){
1767           $member_new[] = $member;
1768         }
1769       }
1770       $attrs['member'] = $member_new;
1771       $ogs_to_fix[$dn] = $attrs;
1772     }
1774     /* Copy source to destination dn */
1775     $ldap->cat($source);
1776     $new_data = $this->cleanup_array($ldap->fetch());
1777     $ldap->cd($destination);
1778     $res = $ldap->add($new_data);
1780     /* Display warning if copy failed */
1781     if(!$res){
1782       print_red(_("Failed to copy '%s' to '%s'. LDAP says '%s'."),$source,$destination,$ldap->get_error());
1783     }else{
1784       $res = $ldap->rmDir($source);
1785       show_ldap_error($ldap->get_error(),_("Something went wrong while copying dns."));
1787       /* Object is copied, so update its references */
1788       foreach($ogs_to_fix as $dn => $data){
1789         $ldap->cd($dn);
1790         $ldap->modify($data);
1791       }
1792     }
1793   }
1795   
1796   /* Cleanup ldap result to be able to write it be to ldap */
1797   function cleanup_array($attrs)
1798   {
1799     foreach($attrs as $key => $value) {
1800       if(is_numeric($key) || in_array($key,array("count","dn"))){
1801         unset($attrs[$key]);
1802       }
1803       if(is_array($value) && isset($value['count'])){
1804         unset($attrs[$key]['count']);
1805       }
1806     }
1807     return($attrs);
1808   }
1811 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1812 ?>