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
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
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();
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();
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 }
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 }
297 $ldap->cd($cv['base']);
298 $res = $ldap->search("(&(objectClass=".$oc.")(uid=*$))",array("dn","sambaSID"));
299 if(!$res){
300 $this->checks['outside_winstations']['STATUS'] = FALSE;
301 $this->checks['outside_winstations']['STATUS_MSG']= _("LDAP query failed");
302 $this->checks['outside_winstations']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
303 return(false);
304 }
306 $this->outside_winstations = array();
307 while($attrs = $ldap->fetch()){
308 if((!preg_match("/^[^,]+,".normalizePreg($winstation_ou)."/",$attrs['dn'])) && !preg_match("/,dc=addressbook,/",$attrs['dn'])){
309 $attrs['selected'] = FALSE;
310 $attrs['ldif'] = "";
311 $this->outside_winstations[base64_encode($attrs['dn'])] = $attrs;
312 }
313 }
315 if(count($this->outside_winstations)){
316 $this->checks['outside_winstations']['STATUS'] = FALSE;
317 $this->checks['outside_winstations']['STATUS_MSG']= _("Failed");
318 $this->checks['outside_winstations']['ERROR_MSG'] =
319 sprintf(_("Found %s winstations outside the predefined winstation department ou '%s'."),count($this->outside_winstations),$winstation_ou);
320 $this->checks['outside_winstations']['ERROR_MSG'].= "<input type='submit' name='outside_winstations_dialog' value='"._("Migrate")."...'>";
321 return(false);
322 }else{
323 $this->checks['outside_winstations']['STATUS'] = TRUE;
324 $this->checks['outside_winstations']['STATUS_MSG']= _("Ok");
325 $this->checks['outside_winstations']['ERROR_MSG'] = "";
326 return(TRUE);
327 }
328 }
331 /* Search for groups outside the group ou
332 */
333 function search_outside_groups()
334 {
335 $cv = $this->parent->captured_values;
336 $ldap = new LDAP($cv['admin'],
337 $cv['password'],
338 $cv['connection'],
339 FALSE,
340 $cv['tls']);
342 $group_ou = $cv['groupou'];
343 $ldap->cd($cv['base']);
345 /***********
346 * Get all gosaDepartments to be able to
347 * validate correct ldap tree position of every single user
348 ***********/
349 $valid_deps = array();
350 $valid_deps['/'] = $cv['base'];
351 $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
352 while($attrs = $ldap->fetch()){
353 $valid_deps[$attrs['ou'][0]] = $attrs['dn'];
354 }
356 /***********
357 * Get all groups
358 ***********/
359 $res = $ldap->search("(objectClass=posixGroup)",array("dn"));
360 if(!$res){
361 $this->checks['outside_groups']['STATUS'] = FALSE;
362 $this->checks['outside_groups']['STATUS_MSG']= _("LDAP query failed");
363 $this->checks['outside_groups']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
364 return(false);
365 }
367 while($attrs = $ldap->fetch()){
368 $group_db_base = preg_replace("/^[^,]+,".normalizePreg($group_ou)."/","",$attrs['dn']);
370 /* Check if entry is not an addressbook only user
371 * and verify that he is in a valid department
372 */
373 if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$group_db_base) &&
374 !in_array($group_db_base,$valid_deps)
375 ){
376 $attrs['selected'] = FALSE;
377 $attrs['ldif'] = "";
378 $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
379 }
380 }
382 if(count($this->outside_groups)){
383 $this->checks['outside_groups']['STATUS'] = FALSE;
384 $this->checks['outside_groups']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
385 $this->checks['outside_groups']['ERROR_MSG'] =
386 sprintf(_("Found %s groups outside the configured tree '%s'."),count($this->outside_groups),$group_ou);
387 $this->checks['outside_groups']['ERROR_MSG'].= " <input type='submit' name='outside_groups_dialog' value='"._("Move")."...'>";
388 return(false);
389 }else{
390 $this->checks['outside_groups']['STATUS'] = TRUE;
391 $this->checks['outside_groups']['STATUS_MSG']= _("Ok");
392 $this->checks['outside_groups']['ERROR_MSG'] = "";
393 return(TRUE);
394 }
395 }
397 /* Search for users outside the people ou
398 */
399 function search_outside_users()
400 {
401 $cv = $this->parent->captured_values;
402 $ldap = new LDAP($cv['admin'],
403 $cv['password'],
404 $cv['connection'],
405 FALSE,
406 $cv['tls']);
408 $ldap->cd($cv['base']);
411 /***********
412 * Get all gosaDepartments to be able to
413 * validate correct ldap tree position of every single user
414 ***********/
415 $valid_deps = array();
416 $valid_deps['/'] = $cv['base'];
417 $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
418 while($attrs = $ldap->fetch()){
419 $valid_deps[$attrs['ou'][0]] = $attrs['dn'];
420 }
422 /***********
423 * Search for all users
424 ***********/
425 $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))",array("dn"));
426 if(!$res){
427 $this->checks['outside_users']['STATUS'] = FALSE;
428 $this->checks['outside_users']['STATUS_MSG']= _("LDAP query failed");
429 $this->checks['outside_users']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
430 return(false);
431 }
433 /***********
434 * Check if returned users are within a valid GOsa deparmtment. (peopleou,gosaDepartment,base)
435 ***********/
436 $this->outside_users = array();
437 $people_ou = trim($cv['peopleou']);
438 if(!empty($people_ou)){
439 $people_ou = $people_ou.",";
440 }
442 while($attrs = $ldap->fetch()){
443 $people_db_base = preg_replace("/^[^,]+,".normalizePreg($people_ou)."/","",$attrs['dn']);
445 /* Check if entry is not an addressbook only user
446 * and verify that he is in a valid department
447 */
448 if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$people_db_base) &&
449 !in_array($people_db_base,$valid_deps)
450 ){
451 $attrs['selected'] = FALSE;
452 $attrs['ldif'] = "";
453 $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
454 }
455 }
457 if(count($this->outside_users)){
458 $this->checks['outside_users']['STATUS'] = FALSE;
459 $this->checks['outside_users']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
460 $this->checks['outside_users']['ERROR_MSG'] =
461 sprintf(_("Found %s user(s) outside the configured tree '%s'."),count($this->outside_users),$people_ou);
462 $this->checks['outside_users']['ERROR_MSG'].= "<input type='submit' name='outside_users_dialog' value='"._("Move")."...'>";
463 return(false);
464 }else{
465 $this->checks['outside_users']['STATUS'] = TRUE;
466 $this->checks['outside_users']['STATUS_MSG']= _("Ok");
467 $this->checks['outside_users']['ERROR_MSG'] = "";
468 return(TRUE);
469 }
470 }
473 /* Check ldap accessibility
474 * Create and remove a dummy object,
475 * to ensure that we have the necessary permissions
476 */
477 function check_ldap_permissions()
478 {
479 $cv = $this->parent->captured_values;
480 $ldap = new LDAP($cv['admin'],
481 $cv['password'],
482 $cv['connection'],
483 FALSE,
484 $cv['tls']);
486 /* Create dummy entry
487 */
488 $name = "GOsa_setup_text_entry_".session_id().rand(0,999999);
489 $dn = "ou=".$name.",".$cv['base'];
490 $testEntry= array();
491 $testEntry['objectClass'][]= "top";
492 $testEntry['objectClass'][]= "organizationalUnit";
493 $testEntry['objectClass'][]= "gosaDepartment";
494 $testEntry['description']= "Created by GOsa setup, this object can be removed.";
495 $testEntry['ou'] = $name;
497 /* check if simple ldap cat will be successful
498 */
499 $res = $ldap->cat($cv['base']);
500 if(!$res){
501 $this->checks['permissions']['STATUS'] = FALSE;
502 $this->checks['permissions']['STATUS_MSG']= _("LDAP query failed");
503 $this->checks['permissions']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
504 return(false);
505 }
507 /* Try to create dummy object
508 */
509 $ldap->cd ($dn);
510 $ldap->create_missing_trees($dn);
511 $res = $ldap->add($testEntry);
512 $ldap->cat($dn);
513 if(!$ldap->count()){
514 new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
516 $this->checks['permissions']['STATUS'] = FALSE;
517 $this->checks['permissions']['STATUS_MSG']= _("Failed");
518 $this->checks['permissions']['ERROR_MSG'] =
519 sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
520 return(false);
521 }
523 /* Try to remove created entry
524 */
525 $res = $ldap->rmDir($dn);
526 $ldap->cat($dn);
527 if($ldap->count()){
528 new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
529 $this->checks['permissions']['STATUS'] = FALSE;
530 $this->checks['permissions']['STATUS_MSG']= _("Failed");
531 $this->checks['permissions']['ERROR_MSG'] =
532 sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
533 return(false);
534 }
536 /* Create & remove of dummy object was successful */
537 $this->checks['permissions']['STATUS'] = TRUE;
538 $this->checks['permissions']['STATUS_MSG']= _("Ok");
539 $this->checks['permissions']['ERROR_MSG'] = "";
540 return(true);
541 }
544 /* Check if there are users which will
545 * be invisible for GOsa
546 */
547 function check_gosaAccounts()
548 {
549 /* Remember old list of ivisible users, to be able to set
550 * the 'html checked' status for the checkboxes again
551 */
552 $cnt_ok = 0;
553 $old = $this->users_to_migrate;
554 $this->users_to_migrate = array();
556 /* Get collected configuration settings */
557 $cv = $this->parent->captured_values;
559 /* Establish ldap connection */
560 $ldap = new LDAP($cv['admin'],
561 $cv['password'],
562 $cv['connection'],
563 FALSE,
564 $cv['tls']);
566 /* Get all invisible users
567 */
568 $ldap->cd($cv['base']);
569 $res =$ldap->search("(&(|(objectClass=posixAccount)(&(objectClass=inetOrgPerson)(objectClass=organizationalPerson)))(!(objectClass=gosaAccount))(uid=*))",array("sn","givenName","cn","uid"));
570 while($attrs = $ldap->fetch()){
571 if(!preg_match("/,dc=addressbook,/",$attrs['dn'])){
572 $attrs['checked'] = FALSE;
573 $attrs['before'] = "";
574 $attrs['after'] = "";
576 /* Set objects to selected, that were selected before reload */
577 if(isset($old[base64_encode($attrs['dn'])])){
578 $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
579 }
580 $this->users_to_migrate[base64_encode($attrs['dn'])] = $attrs;
581 }
582 }
584 /* No invisible */
585 if(!$res){
586 $this->checks['users_visible']['STATUS'] = FALSE;
587 $this->checks['users_visible']['STATUS_MSG']= _("LDAP query failed");
588 $this->checks['users_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
589 }elseif(count($this->users_to_migrate) == 0){
590 $this->checks['users_visible']['STATUS'] = TRUE;
591 $this->checks['users_visible']['STATUS_MSG']= _("Ok");
592 $this->checks['users_visible']['ERROR_MSG'] = "";
593 }else{
594 $this->checks['users_visible']['STATUS'] = FALSE;
595 $this->checks['users_visible']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
596 $this->checks['users_visible']['ERROR_MSG'] = sprintf(_("Found %s user(s) that will not be visible in GOsa."),
597 count($this->users_to_migrate));
598 $this->checks['users_visible']['ERROR_MSG'] .= "<input type='submit' name='users_visible_migrate' value='"._("Migrate")."...'>";
599 }
600 }
603 /* Start user account migration
604 */
605 function migrate_gosaAccounts($only_ldif = FALSE)
606 {
607 $this->show_details= $only_ldif;
609 /* Get collected configuration settings */
610 $cv = $this->parent->captured_values;
612 /* Establish ldap connection */
613 $ldap = new LDAP($cv['admin'],
614 $cv['password'],
615 $cv['connection'],
616 FALSE,
617 $cv['tls']);
619 /* Add gosaAccount objectClass to the selected users
620 */
621 foreach($this->users_to_migrate as $key => $dep){
622 if($dep['checked']){
624 /* Get old objectClasses */
625 $ldap->cat($dep['dn'],array("objectClass"));
626 $attrs = $ldap->fetch();
628 /* Create new objectClass array */
629 $new_attrs = array();
630 $new_attrs['objectClass']= array("gosaAccount","inetOrgPerson","organizationalPerson");
631 for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
632 if(!in_array_ics($attrs['objectClass'][$i], $new_attrs['objectClass'])){
633 $new_attrs['objectClass'][] = $attrs['objectClass'][$i];
634 }
635 }
637 /* Set info attributes for current object,
638 * or write changes to the ldap database
639 */
640 if($only_ldif){
641 $this->users_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
642 $this->users_to_migrate[$key]['after'] = $this->array_to_ldif($new_attrs);
643 }else{
644 $ldap->cd($attrs['dn']);
645 if(!$ldap->modify($new_attrs)){
646 print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
647 return(false);
648 }
649 }
650 }
651 }
652 return(TRUE);
653 }
656 /* Check if there are invisible organizational Units
657 */
658 function check_organizationalUnits()
659 {
660 $cnt_ok = 0;
661 $old = $this->deps_to_migrate;
662 $this->deps_to_migrate = array();
664 /* Get collected configuration settings */
665 $cv = $this->parent->captured_values;
667 /* Establish ldap connection */
668 $ldap = new LDAP($cv['admin'],
669 $cv['password'],
670 $cv['connection'],
671 FALSE,
672 $cv['tls']);
674 /* Skip GOsa internal departments */
675 $skip_dns = array("/".$cv['peopleou']."/","/".$cv['groupou']."/","/^ou=people,/","/^ou=groups,/",
676 "/(,|)ou=configs,/","/(,|)ou=systems,/",
677 "/^ou=apps,/","/^ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
678 "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
679 "/(,|)ou=winstations,/");
681 /* Get all invisible departments */
682 $ldap->cd($cv['base']);
683 $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
684 while($attrs = $ldap->fetch()){
685 $attrs['checked'] = FALSE;
686 $attrs['before'] = "";
687 $attrs['after'] = "";
689 /* Set objects to selected, that were selected before reload */
690 if(isset($old[base64_encode($attrs['dn'])])){
691 $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
692 }
693 $this->deps_to_migrate[base64_encode($attrs['dn'])] = $attrs;
694 }
696 /* Filter returned list of departments and ensure that
697 * GOsa internal departments will not be listed
698 */
699 foreach($this->deps_to_migrate as $key => $attrs){
700 $dn = $attrs['dn'];
701 $skip = false;
702 foreach($skip_dns as $skip_dn){
703 if(preg_match($skip_dn,$dn)){
704 $skip = true;
705 }
706 }
707 if($skip){
708 unset($this->deps_to_migrate[$key]);
709 }
710 }
712 /* If we have no invisible departments found
713 * tell the user that everything is ok
714 */
715 if(!$res){
716 $this->checks['deps_visible']['STATUS'] = FALSE;
717 $this->checks['deps_visible']['STATUS_MSG']= _("LDAP query failed");
718 $this->checks['deps_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
719 }elseif(count($this->deps_to_migrate) == 0 ){
720 $this->checks['deps_visible']['STATUS'] = TRUE;
721 $this->checks['deps_visible']['STATUS_MSG']= _("Ok");
722 $this->checks['deps_visible']['ERROR_MSG'] = "";
723 }else{
724 $this->checks['deps_visible']['STATUS'] = TRUE;
725 $this->checks['deps_visible']['STATUS_MSG']= '<font style="color:#FFA500">'._("Warning").'</font>';
726 $this->checks['deps_visible']['ERROR_MSG'] = sprintf(_("Found %s department(s) that will not be visible in GOsa."),count($this->deps_to_migrate));
727 $this->checks['deps_visible']['ERROR_MSG'] .= " <input type='submit' name='deps_visible_migrate' value='"._("Migrate")."...'>";
728 }
729 }
733 /* Start deparmtment migration */
734 function migrate_organizationalUnits($only_ldif = FALSE)
735 {
736 $this->show_details= $only_ldif;
738 /* Get collected configuration settings */
739 $cv = $this->parent->captured_values;
741 /* Establish ldap connection */
742 $ldap = new LDAP($cv['admin'],
743 $cv['password'],
744 $cv['connection'],
745 FALSE,
746 $cv['tls']);
748 /* Add gosaDepartment objectClass to each selected entry
749 */
750 foreach($this->deps_to_migrate as $key => $dep){
751 if($dep['checked']){
753 /* Get current objectClasses */
754 $ldap->cat($dep['dn'],array("objectClass","description"));
755 $attrs = $ldap->fetch();
757 /* Create new objectClass attribute including gosaDepartment*/
758 $new_attrs = array();
759 for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
760 $new_attrs['objectClass'][] = $attrs['objectClass'][$i];
761 }
762 $new_attrs['objectClass'][] = "gosaDepartment";
764 /* Append description it is missing */
765 if(!isset($attrs['description'])){
766 $new_attrs['description'][] = "GOsa department";
767 }
769 /* Depending on the parameter >only_diff< we save the changes as ldif
770 * or we write our changes directly to the ldap database
771 */
772 if($only_ldif){
773 $this->deps_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
774 $this->deps_to_migrate[$key]['after'] = $this->array_to_ldif($new_attrs);
775 }else{
776 $ldap->cd($attrs['dn']);
777 if(!$ldap->modify($new_attrs)){
778 print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
779 return(false);
780 }
781 }
782 }
783 }
784 return(TRUE);
785 }
788 /* Check Acls if there is at least one object with acls defined
789 */
790 function check_administrativeAccount()
791 {
792 /* Establish ldap connection */
793 $cv = $this->parent->captured_values;
794 $ldap = new LDAP($cv['admin'],
795 $cv['password'],
796 $cv['connection'],
797 FALSE,
798 $cv['tls']);
800 /* Search for gosaAcls */
801 $ldap->cd($cv['base']);
802 $res = $ldap->cat($cv['base']);
804 if(!$res){
805 $this->checks['acls']['STATUS'] = FALSE;
806 $this->checks['acls']['STATUS_MSG']= _("LDAP query failed");
807 $this->checks['acls']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
808 }else{
809 $found = false;
810 $username = "";
811 $attrs = $ldap->fetch();
812 if(isset($attrs['gosaAclEntry'])){
813 $acls = $attrs['gosaAclEntry'];
814 for($i = 0 ; $i < $acls['count'] ; $i++){
815 $acl = $acls[$i];
816 $tmp = split(":",$acl);
817 if($tmp[1] == "psub"){
818 $members = split(",",$tmp[2]);
819 foreach($members as $member){
820 $member = base64_decode($member);
822 /* Check if acl owner is a valid GOsa user account */
823 $ldap->cat($member,array("objectClass","uid","cn"));
824 $ret = $ldap->fetch();
826 if(isset($ret['objectClass']) && in_array("posixGroup",$ret['objectClass'])){
827 $found = TRUE;
828 $username .= "ACL-Group: ".$ret['cn'][0]."<br>";
829 }elseif(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
830 in_array("organizationalPerson",$ret['objectClass']) &&
831 in_array("inetOrgPerson",$ret['objectClass'])){
832 $found = TRUE;
833 $username .= "ACL: ".$ret['uid'][0]."<br>";
834 }
835 }
836 }elseif($tmp[1] == "role"){
838 /* Check if acl owner is a valid GOsa user account */
839 $ldap->cat(base64_decode($tmp[2]),array("gosaAclTemplate"));
840 $ret = $ldap->fetch();
842 if(isset($ret['gosaAclTemplate'])){
843 $cnt = $ret['gosaAclTemplate']['count'];
844 for($e = 0 ; $e < $cnt ; $e++){
846 $a_str = $ret['gosaAclTemplate'][$e];
847 if(preg_match("/^[0-9]*:psub:/",$a_str) && preg_match("/:all;cmdrw$/",$a_str)){
849 $members = split(",",$tmp[3]);
850 foreach($members as $member){
851 $member = base64_decode($member);
853 /* Check if acl owner is a valid GOsa user account */
854 $ldap->cat($member,array("objectClass","uid"));
855 $ret = $ldap->fetch();
857 if(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
858 in_array("organizationalPerson",$ret['objectClass']) &&
859 in_array("inetOrgPerson",$ret['objectClass'])){
860 $found = TRUE;
861 $username .= "ACL Role: ".$ret['uid'][0]."<br>";
862 }
863 }
864 }
865 }
866 }
867 }
868 }
869 }
871 # For debugging
872 #echo $username;
874 if($found){
875 $this->checks['acls']['STATUS'] = TRUE;
876 $this->checks['acls']['STATUS_MSG']= _("Ok");
877 $this->checks['acls']['ERROR_MSG'] = "";
878 }else{
879 $this->checks['acls']['STATUS'] = FALSE;
880 $this->checks['acls']['STATUS_MSG']= _("Failed");
881 $this->checks['acls']['ERROR_MSG']= _("There is no GOsa administrator account inside your LDAP.")." ";
882 $this->checks['acls']['ERROR_MSG'].= "<input type='submit' name='create_acls' value='"._("Create")."'>";
883 }
884 }
885 return($ldap->count()>=1);
886 }
890 function create_admin($only_ldif = FALSE)
891 {
892 /* Reset '' */
893 $this->acl_create_changes="";
895 /* Object that should receive admin acls */
896 $dn = $this->acl_create_selected;
898 /* Get collected configuration settings */
899 $cv = $this->parent->captured_values;
901 /* Establish ldap connection */
902 $ldap = new LDAP($cv['admin'],
903 $cv['password'],
904 $cv['connection'],
905 FALSE,
906 $cv['tls']);
908 /* Get current base attributes */
909 $ldap->cd($cv['base']);
910 $ldap->cat($cv['base'],array("dn","objectClass","gosaAclEntry"));
911 $attrs = $ldap->fetch();
913 /* Add acls for the selcted user to the base */
914 $attrs_new['objectClass'] = array("gosaACL");
916 for($i = 0; $i < $attrs['objectClass']['count']; $i ++){
917 if(!in_array_ics($attrs['objectClass'][$i],$attrs_new['objectClass'])){
918 $attrs_new['objectClass'][] = $attrs['objectClass'][$i];
919 }
920 }
922 $acl = "0:psub:".base64_encode($dn).":all;cmdrw";
923 $attrs_new['gosaAclEntry'][] = $acl;
924 if(isset($attrs['gosaAclEntry'])){
925 for($i = 0 ; $i < $attrs['gosaAclEntry']['count']; $i ++){
927 $prio = preg_replace("/[:].*$/","",$attrs['gosaAclEntry'][$i]);
928 $rest = preg_replace("/^[^:]/","",$attrs['gosaAclEntry'][$i]);
930 $data = ($prio+1).$rest;
931 $attrs_new['gosaAclEntry'][] = $data;
932 }
933 }
935 if($only_ldif){
936 $this->acl_create_changes ="\n".$cv['base']."\n";
937 $this->acl_create_changes.=$this->array_to_ldif($attrs)."\n";
938 $this->acl_create_changes.="\n".$cv['base']."\n";
939 $this->acl_create_changes.=$this->array_to_ldif($attrs_new);
940 }else{
942 $ldap->cd($cv['base']);
943 if(!$ldap->modify($attrs_new)){
944 print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
945 return(FALSE);
946 }else{
947 return(TRUE);
948 }
949 }
950 }
953 function create_admin_user()
954 {
955 $pw1 = $pw2 = "";
956 $uid = "";
958 if(isset($_POST['new_user_uid'])){
959 $uid = $_POST['new_user_uid'];
960 }
961 if(isset($_POST['new_user_password'])){
962 $pw1 = $_POST['new_user_password'];
963 }
964 if(isset($_POST['new_user_password2'])){
965 $pw2 = $_POST['new_user_password2'];
966 }
968 if(empty($pw1) || empty($pw2) | ($pw1 != $pw2)){
969 print_red(_("Specified passwords are empty or not equal."));
970 return false;
971 }
973 if(!is_uid($uid) || empty($uid)){
974 print_red(_("Please specify a valid uid."));
975 return false;
976 }
979 /* Establish ldap connection */
980 $cv = $this->parent->captured_values;
981 $ldap = new LDAP($cv['admin'],
982 $cv['password'],
983 $cv['connection'],
984 FALSE,
985 $cv['tls']);
987 /* Get current base attributes */
988 $ldap->cd($cv['base']);
990 $people_ou = trim($cv['peopleou']);
991 if(!empty($people_ou)){
992 $people_ou = trim($people_ou).",";
993 }
995 if($cv['peopledn'] == "cn"){
996 $dn = "cn=System Administrator,".$people_ou.$cv['base'];
997 }else{
998 $dn = "uid=".$uid.",".$people_ou.$cv['base'];
999 }
1001 $methods = @passwordMethod::get_available_methods_if_not_loaded();
1002 $p_m = $methods[$cv['encryption']];
1003 $p_c = new $p_m(array());
1004 $hash = $p_c->generate_hash($pw2);
1006 $new_user=array();
1007 $new_user['objectClass']= array("top","person","gosaAccount","organizationalPerson","inetOrgPerson");
1008 $new_user['givenName'] = "System";
1009 $new_user['sn'] = "Administrator";
1010 $new_user['cn'] = "System Administrator";
1011 $new_user['uid'] = $uid;
1012 $new_user['userPassword'] = $hash;
1014 $ldap->cd($cv['base']);
1016 $ldap->cat($dn,array("dn"));
1017 if($ldap->count()){
1018 print_red(sprintf(_("Could not add administrative user, there is already an object with the same dn '%s' in your ldap database."),$dn));
1019 return(FALSE);
1020 }
1022 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$dn));
1023 $ldap->cd($dn);
1024 $res = $ldap->add($new_user);
1025 $this->acl_create_selected = $dn;
1026 $this->create_admin();
1028 if(!$res){
1029 print_red($ldap->get_error());
1030 return(FALSE);
1031 }
1033 $this->acl_create_dialog=FALSE;
1034 $this->check_administrativeAccount();
1035 return(TRUE);
1036 }
1039 function migrate_outside_winstations($perform = FALSE)
1040 {
1041 /* Establish ldap connection */
1042 $cv = $this->parent->captured_values;
1043 $ldap = new LDAP($cv['admin'],
1044 $cv['password'],
1045 $cv['connection'],
1046 FALSE,
1047 $cv['tls']);
1049 $ldap->cd($cv['base']);
1051 /* Check if there was a destination department posted */
1052 if(isset($_POST['move_winstation_to'])){
1053 $destination_dep = $_POST['move_winstation_to'];
1054 }else{
1055 print_red(_("Couldn't move users to specified department."));
1056 return(false);
1057 }
1059 foreach($this->outside_winstations as $b_dn => $data){
1060 $this->outside_winstations[$b_dn]['ldif'] ="";
1061 if($data['selected']){
1062 $dn = base64_decode($b_dn);
1063 $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1064 if(!$perform){
1065 $this->outside_winstations[$b_dn]['ldif'] = _("Winstation will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1068 /* Check if there are references to this object */
1069 $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1070 $refs = "";
1071 while($attrs = $ldap->fetch()){
1072 $ref_dn = $attrs['dn'];
1073 $refs .= "<br />\t".$ref_dn;
1074 }
1075 if(!empty($refs)){
1076 $this->outside_winstations[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1077 }
1079 }else{
1080 $this->move($dn,$d_dn);
1081 }
1082 }
1083 }
1084 }
1087 function migrate_outside_groups($perform = FALSE)
1088 {
1089 /* Establish ldap connection */
1090 $cv = $this->parent->captured_values;
1091 $ldap = new LDAP($cv['admin'],
1092 $cv['password'],
1093 $cv['connection'],
1094 FALSE,
1095 $cv['tls']);
1097 $ldap->cd($cv['base']);
1099 /* Check if there was a destination department posted */
1100 if(isset($_POST['move_group_to'])){
1101 $destination_dep = $_POST['move_group_to'];
1102 }else{
1103 print_red(_("Couldn't move users to specified department."));
1104 return(false);
1105 }
1107 foreach($this->outside_groups as $b_dn => $data){
1108 $this->outside_groups[$b_dn]['ldif'] ="";
1109 if($data['selected']){
1110 $dn = base64_decode($b_dn);
1111 $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1112 if(!$perform){
1113 $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1116 /* Check if there are references to this object */
1117 $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1118 $refs = "";
1119 while($attrs = $ldap->fetch()){
1120 $ref_dn = $attrs['dn'];
1121 $refs .= "<br />\t".$ref_dn;
1122 }
1123 if(!empty($refs)){
1124 $this->outside_groups[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1125 }
1127 }else{
1128 $this->move($dn,$d_dn);
1129 }
1130 }
1131 }
1132 }
1135 function migrate_outside_users($perform = FALSE)
1136 {
1137 /* Establish ldap connection */
1138 $cv = $this->parent->captured_values;
1139 $ldap = new LDAP($cv['admin'],
1140 $cv['password'],
1141 $cv['connection'],
1142 FALSE,
1143 $cv['tls']);
1145 $ldap->cd($cv['base']);
1147 /* Check if there was a destination department posted */
1148 if(isset($_POST['move_user_to'])){
1149 $destination_dep = $_POST['move_user_to'];
1150 }else{
1151 print_red(_("Couldn't move users to specified department."));
1152 return(false);
1153 }
1155 foreach($this->outside_users as $b_dn => $data){
1156 $this->outside_users[$b_dn]['ldif'] ="";
1157 if($data['selected']){
1158 $dn = base64_decode($b_dn);
1159 $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1160 if(!$perform){
1161 $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1163 /* Check if there are references to this object */
1164 $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1165 $refs = "";
1166 while($attrs = $ldap->fetch()){
1167 $ref_dn = $attrs['dn'];
1168 $refs .= "<br />\t".$ref_dn;
1169 }
1170 if(!empty($refs)){
1171 $this->outside_users[$b_dn]['ldif'] .= "<br /><br /><i>"._("The following references will be updated").":</i>".$refs;
1172 }
1174 }else{
1175 $this->move($dn,$d_dn);
1176 }
1177 }
1178 }
1179 }
1182 function execute()
1183 {
1184 /* Initialise checks if this is the first call */
1185 if(!$this->checks_initialised || isset($_POST['reload'])){
1186 $this->initialize_checks();
1187 $this->checks_initialised = TRUE;
1188 }
1190 /*************
1191 * Winstations outside the group ou
1192 *************/
1194 if(isset($_POST['outside_winstations_dialog_cancel'])){
1195 $this->outside_winstations_dialog = FALSE;
1196 $this->dialog = FALSE;
1197 $this->show_details = FALSE;
1198 }
1200 if(isset($_POST['outside_winstations_dialog_whats_done'])){
1201 $this->migrate_outside_winstations(FALSE);
1202 }
1204 if(isset($_POST['outside_winstations_dialog_perform'])){
1205 $this->migrate_outside_winstations(TRUE);
1206 $this->search_outside_winstations();
1207 $this->dialog = FALSE;
1208 $this->show_details = FALSE;
1209 $this->outside_winstations_dialog = FALSE;
1210 }
1212 if(isset($_POST['outside_winstations_dialog'])){
1213 $this->outside_winstations_dialog = TRUE;
1214 $this->dialog = TRUE;
1215 }
1217 if($this->outside_winstations_dialog){
1218 $smarty = get_smarty();
1219 $smarty->assign("ous",$this->get_all_winstation_ous());
1220 $smarty->assign("method","outside_winstations");
1221 $smarty->assign("outside_winstations",$this->outside_winstations);
1222 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1223 }
1224 /*************
1225 * Groups outside the group ou
1226 *************/
1228 if(isset($_POST['outside_groups_dialog_cancel'])){
1229 $this->outside_groups_dialog = FALSE;
1230 $this->show_details = FALSE;
1231 $this->dialog = FALSE;
1232 }
1234 if(isset($_POST['outside_groups_dialog_whats_done'])){
1235 $this->show_details= TRUE;
1236 $this->migrate_outside_groups(FALSE);
1237 }
1239 if(isset($_POST['outside_groups_dialog_refresh'])){
1240 $this->show_details= FALSE;
1241 }
1243 if(isset($_POST['outside_groups_dialog_perform'])){
1244 $this->migrate_outside_groups(TRUE);
1245 $this->dialog = FALSE;
1246 $this->show_details = FALSE;
1247 $this->outside_groups_dialog = FALSE;
1248 $this->initialize_checks();
1249 }
1251 if(isset($_POST['outside_groups_dialog'])){
1252 $this->outside_groups_dialog = TRUE;
1253 $this->dialog = TRUE;
1254 }
1256 if($this->outside_groups_dialog){
1257 $smarty = get_smarty();
1258 $smarty->assign("ous",$this->get_all_group_ous());
1259 $smarty->assign("method","outside_groups");
1260 $smarty->assign("outside_groups",$this->outside_groups);
1261 $smarty->assign("group_details", $this->show_details);
1262 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1263 }
1265 /*************
1266 * User outside the people ou
1267 *************/
1269 if(isset($_POST['outside_users_dialog_cancel'])){
1270 $this->outside_users_dialog = FALSE;
1271 $this->dialog = FALSE;
1272 $this->show_details = FALSE;
1273 }
1275 if(isset($_POST['outside_users_dialog_whats_done'])){
1276 $this->show_details= TRUE;
1277 $this->migrate_outside_users(FALSE);
1278 }
1280 if(isset($_POST['outside_users_dialog_perform'])){
1281 $this->migrate_outside_users(TRUE);
1282 $this->initialize_checks();
1283 $this->dialog = FALSE;
1284 $this->show_details = FALSE;
1285 $this->outside_users_dialog = FALSE;
1286 }
1288 if (isset($_POST['outside_users_dialog_refresh'])){
1289 $this->show_details= FALSE;
1290 }
1292 if(isset($_POST['outside_users_dialog'])){
1293 $this->outside_users_dialog = TRUE;
1294 $this->dialog = TRUE;
1295 }
1297 if($this->outside_users_dialog){
1298 $smarty = get_smarty();
1299 $smarty->assign("ous",$this->get_all_people_ous());
1300 $smarty->assign("method","outside_users");
1301 $smarty->assign("outside_users",$this->outside_users);
1302 $smarty->assign("user_details", $this->show_details);
1303 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1304 }
1306 /*************
1307 * Root object check
1308 *************/
1310 if(isset($_POST['retry_root_create'])){
1312 $state = $this->checks['root']['STATUS'];
1313 $this->checkBase(FALSE);
1314 if($state != $this->checks['root']['STATUS']){
1315 $this->initialize_checks();
1316 }
1317 }
1319 /*************
1320 * User Migration handling
1321 *************/
1323 if(isset($_POST['retry_acls'])){
1324 $this->check_administrativeAccount();
1325 }
1327 if(isset($_POST['create_acls'])){
1328 $this->acl_create_dialog = TRUE;
1329 $this->dialog = TRUE;
1330 }
1332 if(isset($_POST['create_acls_cancel'])){
1333 $this->acl_create_dialog = FALSE;
1334 $this->dialog = FALSE;
1335 $this->show_details = FALSE;
1336 }
1338 # if(isset($_POST['create_acls_create_confirmed'])){
1339 # if($this->create_admin()){
1340 # $this->acl_create_dialog = FALSE;
1341 # $this->dialog = FALSE;
1342 # $this->show_details = FALSE;
1343 # $this->initialize_checks();
1344 # }
1345 # }
1347 if(isset($_POST['create_acls_create'])){
1348 $this->create_admin(TRUE);
1349 }
1351 if(isset($_POST['create_admin_user'])){
1352 if($this->create_admin_user()){
1353 $this->dialog = FALSE;
1354 $this->show_details = FALSE;
1355 }
1356 }
1358 if($this->acl_create_dialog){
1359 $smarty = get_smarty();
1361 $uid = "admin";
1362 if(isset($_POST['new_user_uid'])){
1363 $uid = $_POST['new_user_uid'];
1364 }
1366 $smarty->assign("new_user_uid",$uid);
1367 $smarty->assign("new_user_password",@$_POST['new_user_password']);
1368 $smarty->assign("new_user_password2",@$_POST['new_user_password2']);
1369 $smarty->assign("method","create_acls");
1370 $smarty->assign("acl_create_selected",$this->acl_create_selected);
1371 $smarty->assign("what_will_be_done_now",$this->acl_create_changes);
1372 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1373 }
1375 /*************
1376 * User Migration handling
1377 *************/
1379 /* Refresh list of deparments */
1380 if(isset($_POST['users_visible_migrate_refresh'])){
1381 $this->check_gosaAccounts();
1382 }
1384 /* Open migration dialog */
1385 if(isset($_POST['users_visible_migrate'])){
1386 $this->show_details= FALSE;
1387 $this->users_migration_dialog = TRUE;
1388 $this->dialog =TRUE;
1389 }
1391 /* Close migration dialog */
1392 if(isset($_POST['users_visible_migrate_close'])){
1393 $this->users_migration_dialog = FALSE;
1394 $this->dialog =FALSE;
1395 $this->show_details = FALSE;
1396 }
1398 /* Start migration */
1399 if(isset($_POST['users_visible_migrate_migrate'])){
1400 if($this->migrate_gosaAccounts()){
1401 $this->initialize_checks();
1402 $this->dialog = FALSE;
1403 $this->show_details = FALSE;
1404 $this->users_migration_dialog = FALSE;
1405 }
1406 }
1408 /* Start migration */
1409 if(isset($_POST['users_visible_migrate_whatsdone'])){
1410 $this->migrate_gosaAccounts(TRUE);
1411 }
1413 /* Display migration dialog */
1414 if($this->users_migration_dialog){
1415 $smarty = get_smarty();
1416 $smarty->assign("users_to_migrate",$this->users_to_migrate);
1417 $smarty->assign("method","migrate_users");
1418 $smarty->assign("user_details", $this->show_details);
1419 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1420 }
1423 /*************
1424 * Department Migration handling
1425 *************/
1427 /* Refresh list of deparments */
1428 if(isset($_POST['deps_visible_migrate_refresh'])){
1429 $this->check_organizationalUnits();
1430 $this->show_details= FALSE;
1431 }
1433 /* Open migration dialog */
1434 if(isset($_POST['deps_visible_migrate'])){
1435 $this->dep_migration_dialog = TRUE;
1436 $this->dialog =TRUE;
1437 }
1439 /* Close migration dialog */
1440 if(isset($_POST['deps_visible_migrate_close'])){
1441 $this->dep_migration_dialog = FALSE;
1442 $this->dialog =FALSE;
1443 $this->show_details = FALSE;
1444 }
1446 /* Start migration */
1447 if(isset($_POST['deps_visible_migrate_migrate'])){
1448 if($this->migrate_organizationalUnits()){
1449 $this->show_details= FALSE;
1450 $this->check_organizationalUnits();
1451 $this->dialog = FALSE;
1452 $this->dep_migration_dialog = FALSE;
1453 }
1454 }
1456 /* Start migration */
1457 if(isset($_POST['deps_visible_migrate_whatsdone'])){
1458 $this->migrate_organizationalUnits(TRUE);
1459 }
1461 /* Display migration dialog */
1462 if($this->dep_migration_dialog){
1463 $smarty = get_smarty();
1464 $smarty->assign("deps_to_migrate",$this->deps_to_migrate);
1465 $smarty->assign("method","migrate_deps");
1466 $smarty->assign("deps_details", $this->show_details);
1467 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1468 }
1470 $smarty = get_smarty();
1471 $smarty->assign("checks",$this->checks);
1472 $smarty->assign("method","default");
1473 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1474 }
1477 function save_object()
1478 {
1479 $this->is_completed= TRUE;
1481 /* Capture all selected winstations from outside_winstations_dialog */
1482 if($this->outside_winstations_dialog){
1483 foreach($this->outside_winstations as $dn => $data){
1484 if(isset($_POST['select_winstation_'.$dn])){
1485 $this->outside_winstations[$dn]['selected'] = TRUE;
1486 }else{
1487 $this->outside_winstations[$dn]['selected'] = FALSE;
1488 }
1489 }
1490 }
1492 /* Capture all selected groups from outside_groups_dialog */
1493 if($this->outside_groups_dialog){
1494 foreach($this->outside_groups as $dn => $data){
1495 if(isset($_POST['select_group_'.$dn])){
1496 $this->outside_groups[$dn]['selected'] = TRUE;
1497 }else{
1498 $this->outside_groups[$dn]['selected'] = FALSE;
1499 }
1500 }
1501 }
1503 /* Capture all selected users from outside_users_dialog */
1504 if($this->outside_users_dialog){
1505 foreach($this->outside_users as $dn => $data){
1506 if(isset($_POST['select_user_'.$dn])){
1507 $this->outside_users[$dn]['selected'] = TRUE;
1508 }else{
1509 $this->outside_users[$dn]['selected'] = FALSE;
1510 }
1511 }
1512 }
1514 /* Get "create acl" dialog posts */
1515 if($this->acl_create_dialog){
1517 if(isset($_POST['create_acls_create_abort'])){
1518 $this->acl_create_selected = "";
1519 }
1520 }
1522 /* Get selected departments */
1523 if($this->dep_migration_dialog){
1524 foreach($this->deps_to_migrate as $id => $data){
1525 if(isset($_POST['migrate_'.$id])){
1526 $this->deps_to_migrate[$id]['checked'] = TRUE;
1527 }else{
1528 $this->deps_to_migrate[$id]['checked'] = FALSE;
1529 }
1530 }
1531 }
1533 /* Get selected users */
1534 if($this->users_migration_dialog){
1535 foreach($this->users_to_migrate as $id => $data){
1536 if(isset($_POST['migrate_'.$id])){
1537 $this->users_to_migrate[$id]['checked'] = TRUE;
1538 }else{
1539 $this->users_to_migrate[$id]['checked'] = FALSE;
1540 }
1541 }
1542 }
1543 }
1546 /* Check if the root object exists.
1547 * If the parameter just_check is true, then just check if the
1548 * root object is missing and update the info messages.
1549 * If the Parameter is false, try to create a new root object.
1550 */
1551 function checkBase($just_check = TRUE)
1552 {
1553 /* Get collected setup informations */
1554 $cv = $this->parent->captured_values;
1556 /* Establish ldap connection */
1557 $ldap = new LDAP($cv['admin'],
1558 $cv['password'],
1559 $cv['connection'],
1560 FALSE,
1561 $cv['tls']);
1563 /* Check if root object exists */
1564 $ldap->cd($cv['base']);
1565 $res = $ldap->search("(objectClass=*)");
1566 $err = ldap_errno($ldap->cid);
1568 if( !$res ||
1569 $err == 0x20 || # LDAP_NO_SUCH_OBJECT
1570 $err == 0x40) { # LDAP_NAMING_VIOLATION
1572 /* Root object doesn't exists
1573 */
1574 if($just_check){
1575 $this->checks['root']['STATUS'] = FALSE;
1576 $this->checks['root']['STATUS_MSG']= _("Failed");
1577 $this->checks['root']['ERROR_MSG'] = _("The LDAP root object is missing. It is required to use your LDAP service.").' ';
1578 $this->checks['root']['ERROR_MSG'].= "<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1579 return(FALSE);
1580 }else{
1582 /* Add root object */
1583 $ldap->cd($cv['base']);
1584 $res = $ldap->create_missing_trees($cv['base']);
1586 /* If adding failed, tell the user */
1587 if(!$res){
1588 $this->checks['root']['STATUS'] = FALSE;
1589 $this->checks['root']['STATUS_MSG']= _("Failed");
1590 $this->checks['root']['ERROR_MSG'] = _("Root object couldn't be created, you should try it on your own.");
1591 $this->checks['root']['ERROR_MSG'].= " <input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1592 return($res);;
1593 }
1594 }
1595 }
1597 /* Create & remove of dummy object was successful */
1598 $this->checks['root']['STATUS'] = TRUE;
1599 $this->checks['root']['STATUS_MSG']= _("Ok");
1600 }
1603 /* Return ldif information for a
1604 * given attribute array
1605 */
1606 function array_to_ldif($atts)
1607 {
1608 $ret = "";
1609 unset($atts['count']);
1610 unset($atts['dn']);
1611 foreach($atts as $name => $value){
1612 if(is_numeric($name)) {
1613 continue;
1614 }
1615 if(is_array($value)){
1616 unset($value['count']);
1617 foreach($value as $a_val){
1618 $ret .= $name.": ". $a_val."\n";
1619 }
1620 }else{
1621 $ret .= $name.": ". $value."\n";
1622 }
1623 }
1624 return(preg_replace("/\n$/","",$ret));
1625 }
1628 function get_user_list()
1629 {
1630 /* Get collected configuration settings */
1631 $cv = $this->parent->captured_values;
1633 /* Establish ldap connection */
1634 $ldap = new LDAP($cv['admin'],
1635 $cv['password'],
1636 $cv['connection'],
1637 FALSE,
1638 $cv['tls']);
1640 $ldap->cd($cv['base']);
1641 $ldap->search("(objectClass=gosaAccount)",array("dn"));
1643 $tmp = array();
1644 while($attrs = $ldap->fetch()){
1645 $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1646 }
1647 return($tmp);
1648 }
1651 function get_all_people_ous()
1652 {
1653 /* Get collected configuration settings */
1654 $cv = $this->parent->captured_values;
1655 $people_ou = trim($cv['peopleou']);
1657 /* Establish ldap connection */
1658 $ldap = new LDAP($cv['admin'],
1659 $cv['password'],
1660 $cv['connection'],
1661 FALSE,
1662 $cv['tls']);
1665 /*****************
1666 * If people ou is NOT empty
1667 * search for for all objects matching the given container
1668 *****************/
1669 if(!empty($people_ou)){
1670 $ldap->search("(".$people_ou.")",array("dn"));
1672 /* Create people ou if there is currently none */
1673 if($ldap->count() == 0 ){
1674 $add_dn = $cv['peopleou'].",".$cv['base'];
1675 $naming_attr = preg_replace("/=.*$/","",$add_dn);
1676 $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1677 $add = array();
1678 $add['objectClass'] = array("organizationalUnit");
1679 $add[$naming_attr] = $naming_value;
1680 $ldap->cd($cv['base']);
1681 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1682 $ldap->cd($add_dn);
1683 $ldap->add($add);
1684 }
1686 /* Create result */
1687 $ldap->search("(".$cv['peopleou'].")",array("dn"));
1688 $tmp = array();
1689 while($attrs= $ldap->fetch()){
1690 if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1691 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1692 }
1693 }
1694 } else{
1696 /************
1697 * If people ou is empty
1698 * Get all valid gosaDepartments
1699 ************/
1700 $ldap->cd($cv['base']);
1701 $tmp = array();
1702 $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
1703 $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
1704 while($attrs = $ldap->fetch()){
1705 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
1706 }
1707 }
1708 return($tmp);
1709 }
1712 function get_all_winstation_ous()
1713 {
1714 /* Get collected configuration settings */
1715 $cv = $this->parent->captured_values;
1717 /* Establish ldap connection */
1718 $ldap = new LDAP($cv['admin'],
1719 $cv['password'],
1720 $cv['connection'],
1721 FALSE,
1722 $cv['tls']);
1724 /* Get winstation ou */
1725 if($cv['generic_settings']['wws_ou_active']) {
1726 $winstation_ou = $cv['generic_settings']['ws_ou'];
1727 }else{
1728 $winstation_ou = "ou=winstations";
1729 }
1731 $ldap->cd($cv['base']);
1732 $ldap->search("(".$winstation_ou.")",array("dn"));
1734 if($ldap->count() == 0 ){
1735 $add_dn = $winstation_ou.",ou=systems,".$cv['base'];
1736 $naming_attr = preg_replace("/=.*$/","",$add_dn);
1737 $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1738 $add = array();
1739 $add['objectClass'] = array("organizationalUnit");
1740 $add[$naming_attr] = $naming_value;
1742 $ldap->cd($cv['base']);
1743 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1744 $ldap->cd($add_dn);
1745 $ldap->add($add);
1746 }
1748 $ldap->search("(".$winstation_ou.")",array("dn"));
1749 $tmp = array();
1750 while($attrs= $ldap->fetch()){
1751 if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1752 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1753 }
1754 }
1755 return($tmp);
1756 }
1759 function get_all_group_ous()
1760 {
1761 /* Get collected configuration settings */
1762 $cv = $this->parent->captured_values;
1764 /* Establish ldap connection */
1765 $ldap = new LDAP($cv['admin'],
1766 $cv['password'],
1767 $cv['connection'],
1768 FALSE,
1769 $cv['tls']);
1771 $group_ou = trim($cv['groupou']);
1772 if(!empty($group_ou)){
1773 $group_ou = trim($group_ou);
1774 }
1776 /************
1777 * If group ou is NOT empty
1778 * Get all valid group ous, create one if necessary
1779 ************/
1780 $ldap->cd($cv['base']);
1781 if(!empty($group_ou)){
1782 $ldap->search("(".$group_ou.")",array("dn"));
1783 if($ldap->count() == 0 ){
1784 $add_dn = $group_ou.$cv['base'];
1785 $naming_attr = preg_replace("/=.*$/","",$add_dn);
1786 $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1787 $add = array();
1788 $add['objectClass'] = array("organizationalUnit");
1789 $add[$naming_attr] = $naming_value;
1791 $ldap->cd($cv['base']);
1792 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1793 $ldap->cd($add_dn);
1794 $ldap->add($add);
1795 }
1796 $ldap->search("(".$group_ou.")",array("dn"));
1797 $tmp = array();
1798 while($attrs= $ldap->fetch()){
1799 if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1800 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1801 }
1802 }
1803 }else{
1804 /************
1805 * If group ou is empty
1806 * Get all valid gosaDepartments
1807 ************/
1808 $ldap->cd($cv['base']);
1809 $tmp = array();
1810 $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
1811 $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
1812 while($attrs = $ldap->fetch()){
1813 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
1814 }
1815 }
1816 return($tmp);
1817 }
1820 function get_group_list()
1821 {
1822 /* Get collected configuration settings */
1823 $cv = $this->parent->captured_values;
1825 /* Establish ldap connection */
1826 $ldap = new LDAP($cv['admin'],
1827 $cv['password'],
1828 $cv['connection'],
1829 FALSE,
1830 $cv['tls']);
1832 $ldap->cd($cv['base']);
1833 $ldap->search("(objectClass=posixGroup)",array("dn"));
1835 $tmp = array();
1836 while($attrs = $ldap->fetch()){
1837 $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1838 }
1839 return($tmp);
1840 }
1843 function move($source,$destination)
1844 {
1845 /* Get collected configuration settings */
1846 $cv = $this->parent->captured_values;
1848 /* Establish ldap connection */
1849 $ldap = new LDAP($cv['admin'],
1850 $cv['password'],
1851 $cv['connection'],
1852 FALSE,
1853 $cv['tls']);
1855 /* Update object references in gosaGroupOfNames */
1856 $ogs_to_fix = array();
1857 $ldap->cd($cv['base']);
1858 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($source).'))', array('cn','member'));
1859 while ($attrs= $ldap->fetch()){
1860 $dn = $attrs['dn'];
1861 $attrs = $this->cleanup_array($attrs);
1862 $member_new = array($destination);
1863 foreach($attrs['member'] as $member){
1864 if($member != $source){
1865 $member_new[] = $member;
1866 }
1867 }
1868 $attrs['member'] = $member_new;
1869 $ogs_to_fix[$dn] = $attrs;
1870 }
1872 /* Copy source to destination dn */
1873 $ldap->cat($source);
1874 $new_data = $this->cleanup_array($ldap->fetch());
1875 $ldap->cd($destination);
1876 $res = $ldap->add($new_data);
1878 /* Display warning if copy failed */
1879 if(!$res){
1880 print_red(_("Failed to copy '%s' to '%s'. LDAP says '%s'."),$source,$destination,$ldap->get_error());
1881 }else{
1882 $res = $ldap->rmDir($source);
1883 show_ldap_error($ldap->get_error(),_("Something went wrong while copying dns."));
1885 /* Object is copied, so update its references */
1886 foreach($ogs_to_fix as $dn => $data){
1887 $ldap->cd($dn);
1888 $ldap->modify($data);
1889 }
1890 }
1891 }
1894 /* Cleanup ldap result to be able to write it be to ldap */
1895 function cleanup_array($attrs)
1896 {
1897 foreach($attrs as $key => $value) {
1898 if(is_numeric($key) || in_array($key,array("count","dn"))){
1899 unset($attrs[$key]);
1900 }
1901 if(is_array($value) && isset($value['count'])){
1902 unset($attrs[$key]['count']);
1903 }
1904 }
1905 return($attrs);
1906 }
1907 }
1909 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1910 ?>