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 $this->outside_groups = array();
368 while($attrs = $ldap->fetch()){
369 $group_db_base = preg_replace("/^[^,]+,".normalizePreg($group_ou)."+,/i","",$attrs['dn']);
371 /* Check if entry is not an addressbook only user
372 * and verify that he is in a valid department
373 */
374 if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$group_db_base) &&
375 !in_array($group_db_base,$valid_deps)
376 ){
377 $attrs['selected'] = FALSE;
378 $attrs['ldif'] = "";
379 $this->outside_groups[base64_encode($attrs['dn'])] = $attrs;
380 }
381 }
383 if(count($this->outside_groups)){
384 $this->checks['outside_groups']['STATUS'] = FALSE;
385 $this->checks['outside_groups']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
386 $this->checks['outside_groups']['ERROR_MSG'] =
387 sprintf(_("Found %s groups outside the configured tree '%s'."),count($this->outside_groups),$group_ou);
388 $this->checks['outside_groups']['ERROR_MSG'].= " <input type='submit' name='outside_groups_dialog' value='"._("Move")."...'>";
389 return(false);
390 }else{
391 $this->checks['outside_groups']['STATUS'] = TRUE;
392 $this->checks['outside_groups']['STATUS_MSG']= _("Ok");
393 $this->checks['outside_groups']['ERROR_MSG'] = "";
394 return(TRUE);
395 }
396 }
398 /* Search for users outside the people ou
399 */
400 function search_outside_users()
401 {
402 $cv = $this->parent->captured_values;
403 $ldap = new LDAP($cv['admin'],
404 $cv['password'],
405 $cv['connection'],
406 FALSE,
407 $cv['tls']);
409 $ldap->cd($cv['base']);
412 /***********
413 * Get all gosaDepartments to be able to
414 * validate correct ldap tree position of every single user
415 ***********/
416 $valid_deps = array();
417 $valid_deps['/'] = $cv['base'];
418 $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
419 while($attrs = $ldap->fetch()){
420 $valid_deps[$attrs['ou'][0]] = $attrs['dn'];
421 }
423 /***********
424 * Search for all users
425 ***********/
426 $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))",array("dn"));
427 if(!$res){
428 $this->checks['outside_users']['STATUS'] = FALSE;
429 $this->checks['outside_users']['STATUS_MSG']= _("LDAP query failed");
430 $this->checks['outside_users']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
431 return(false);
432 }
434 /***********
435 * Check if returned users are within a valid GOsa deparmtment. (peopleou,gosaDepartment,base)
436 ***********/
437 $this->outside_users = array();
438 $people_ou = trim($cv['peopleou']);
439 if(!empty($people_ou)){
440 $people_ou = $people_ou.",";
441 }
443 while($attrs = $ldap->fetch()){
444 $people_db_base = preg_replace("/^[^,]+,".normalizePreg($people_ou)."/i","",$attrs['dn']);
446 /* Check if entry is not an addressbook only user
447 * and verify that he is in a valid department
448 */
449 if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$people_db_base) &&
450 !in_array($people_db_base,$valid_deps)
451 ){
452 $attrs['selected'] = FALSE;
453 $attrs['ldif'] = "";
454 $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
455 }
456 }
458 if(count($this->outside_users)){
459 $this->checks['outside_users']['STATUS'] = FALSE;
460 $this->checks['outside_users']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
461 $this->checks['outside_users']['ERROR_MSG'] =
462 sprintf(_("Found %s user(s) outside the configured tree '%s'."),count($this->outside_users),$people_ou);
463 $this->checks['outside_users']['ERROR_MSG'].= "<input type='submit' name='outside_users_dialog' value='"._("Move")."...'>";
464 return(false);
465 }else{
466 $this->checks['outside_users']['STATUS'] = TRUE;
467 $this->checks['outside_users']['STATUS_MSG']= _("Ok");
468 $this->checks['outside_users']['ERROR_MSG'] = "";
469 return(TRUE);
470 }
471 }
474 /* Check ldap accessibility
475 * Create and remove a dummy object,
476 * to ensure that we have the necessary permissions
477 */
478 function check_ldap_permissions()
479 {
480 $cv = $this->parent->captured_values;
481 $ldap = new LDAP($cv['admin'],
482 $cv['password'],
483 $cv['connection'],
484 FALSE,
485 $cv['tls']);
487 /* Create dummy entry
488 */
489 $name = "GOsa_setup_text_entry_".session_id().rand(0,999999);
490 $dn = "ou=".$name.",".$cv['base'];
491 $testEntry= array();
492 $testEntry['objectClass'][]= "top";
493 $testEntry['objectClass'][]= "organizationalUnit";
494 $testEntry['objectClass'][]= "gosaDepartment";
495 $testEntry['description']= "Created by GOsa setup, this object can be removed.";
496 $testEntry['ou'] = $name;
498 /* check if simple ldap cat will be successful
499 */
500 $res = $ldap->cat($cv['base']);
501 if(!$res){
502 $this->checks['permissions']['STATUS'] = FALSE;
503 $this->checks['permissions']['STATUS_MSG']= _("LDAP query failed");
504 $this->checks['permissions']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
505 return(false);
506 }
508 /* Try to create dummy object
509 */
510 $ldap->cd ($dn);
511 $ldap->create_missing_trees($dn);
512 $res = $ldap->add($testEntry);
513 $ldap->cat($dn);
514 if(!$ldap->count()){
515 new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
517 $this->checks['permissions']['STATUS'] = FALSE;
518 $this->checks['permissions']['STATUS_MSG']= _("Failed");
519 $this->checks['permissions']['ERROR_MSG'] =
520 sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
521 return(false);
522 }
524 /* Try to remove created entry
525 */
526 $res = $ldap->rmDir($dn);
527 $ldap->cat($dn);
528 if($ldap->count()){
529 new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
530 $this->checks['permissions']['STATUS'] = FALSE;
531 $this->checks['permissions']['STATUS_MSG']= _("Failed");
532 $this->checks['permissions']['ERROR_MSG'] =
533 sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
534 return(false);
535 }
537 /* Create & remove of dummy object was successful */
538 $this->checks['permissions']['STATUS'] = TRUE;
539 $this->checks['permissions']['STATUS_MSG']= _("Ok");
540 $this->checks['permissions']['ERROR_MSG'] = "";
541 return(true);
542 }
545 /* Check if there are users which will
546 * be invisible for GOsa
547 */
548 function check_gosaAccounts()
549 {
550 /* Remember old list of ivisible users, to be able to set
551 * the 'html checked' status for the checkboxes again
552 */
553 $cnt_ok = 0;
554 $old = $this->users_to_migrate;
555 $this->users_to_migrate = array();
557 /* Get collected configuration settings */
558 $cv = $this->parent->captured_values;
560 /* Establish ldap connection */
561 $ldap = new LDAP($cv['admin'],
562 $cv['password'],
563 $cv['connection'],
564 FALSE,
565 $cv['tls']);
567 /* Get all invisible users
568 */
569 $ldap->cd($cv['base']);
570 $res =$ldap->search("(&(|(objectClass=posixAccount)(&(objectClass=inetOrgPerson)(objectClass=organizationalPerson)))(!(objectClass=gosaAccount))(uid=*))",array("sn","givenName","cn","uid"));
571 while($attrs = $ldap->fetch()){
572 if(!preg_match("/,dc=addressbook,/",$attrs['dn'])){
573 $attrs['checked'] = FALSE;
574 $attrs['before'] = "";
575 $attrs['after'] = "";
577 /* Set objects to selected, that were selected before reload */
578 if(isset($old[base64_encode($attrs['dn'])])){
579 $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
580 }
581 $this->users_to_migrate[base64_encode($attrs['dn'])] = $attrs;
582 }
583 }
585 /* No invisible */
586 if(!$res){
587 $this->checks['users_visible']['STATUS'] = FALSE;
588 $this->checks['users_visible']['STATUS_MSG']= _("LDAP query failed");
589 $this->checks['users_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
590 }elseif(count($this->users_to_migrate) == 0){
591 $this->checks['users_visible']['STATUS'] = TRUE;
592 $this->checks['users_visible']['STATUS_MSG']= _("Ok");
593 $this->checks['users_visible']['ERROR_MSG'] = "";
594 }else{
595 $this->checks['users_visible']['STATUS'] = FALSE;
596 $this->checks['users_visible']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
597 $this->checks['users_visible']['ERROR_MSG'] = sprintf(_("Found %s user(s) that will not be visible in GOsa."),
598 count($this->users_to_migrate));
599 $this->checks['users_visible']['ERROR_MSG'] .= "<input type='submit' name='users_visible_migrate' value='"._("Migrate")."...'>";
600 }
601 }
604 /* Start user account migration
605 */
606 function migrate_gosaAccounts($only_ldif = FALSE)
607 {
608 $this->show_details= $only_ldif;
610 /* Get collected configuration settings */
611 $cv = $this->parent->captured_values;
613 /* Establish ldap connection */
614 $ldap = new LDAP($cv['admin'],
615 $cv['password'],
616 $cv['connection'],
617 FALSE,
618 $cv['tls']);
620 /* Add gosaAccount objectClass to the selected users
621 */
622 foreach($this->users_to_migrate as $key => $dep){
623 if($dep['checked']){
625 /* Get old objectClasses */
626 $ldap->cat($dep['dn'],array("objectClass"));
627 $attrs = $ldap->fetch();
629 /* Create new objectClass array */
630 $new_attrs = array();
631 $new_attrs['objectClass']= array("gosaAccount","inetOrgPerson","organizationalPerson");
632 for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
633 if(!in_array_ics($attrs['objectClass'][$i], $new_attrs['objectClass'])){
634 $new_attrs['objectClass'][] = $attrs['objectClass'][$i];
635 }
636 }
638 /* Set info attributes for current object,
639 * or write changes to the ldap database
640 */
641 if($only_ldif){
642 $this->users_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
643 $this->users_to_migrate[$key]['after'] = $this->array_to_ldif($new_attrs);
644 }else{
645 $ldap->cd($attrs['dn']);
646 if(!$ldap->modify($new_attrs)){
647 print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
648 return(false);
649 }
650 }
651 }
652 }
653 return(TRUE);
654 }
657 /* Check if there are invisible organizational Units
658 */
659 function check_organizationalUnits()
660 {
661 $cnt_ok = 0;
662 $old = $this->deps_to_migrate;
663 $this->deps_to_migrate = array();
665 /* Get collected configuration settings */
666 $cv = $this->parent->captured_values;
668 /* Establish ldap connection */
669 $ldap = new LDAP($cv['admin'],
670 $cv['password'],
671 $cv['connection'],
672 FALSE,
673 $cv['tls']);
675 /* Skip GOsa internal departments */
676 $skip_dns = array("/".$cv['peopleou']."/","/".$cv['groupou']."/","/^ou=people,/","/^ou=groups,/",
677 "/(,|)ou=configs,/","/(,|)ou=systems,/",
678 "/(,|)ou=apps,/","/(,|)ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
679 "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
680 "/(,|)ou=winstations,/");
682 /* Get all invisible departments */
683 $ldap->cd($cv['base']);
684 $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
685 while($attrs = $ldap->fetch()){
686 $attrs['checked'] = FALSE;
687 $attrs['before'] = "";
688 $attrs['after'] = "";
690 /* Set objects to selected, that were selected before reload */
691 if(isset($old[base64_encode($attrs['dn'])])){
692 $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
693 }
694 $this->deps_to_migrate[base64_encode($attrs['dn'])] = $attrs;
695 }
697 /* Filter returned list of departments and ensure that
698 * GOsa internal departments will not be listed
699 */
700 foreach($this->deps_to_migrate as $key => $attrs){
701 $dn = $attrs['dn'];
702 $skip = false;
703 foreach($skip_dns as $skip_dn){
704 if(preg_match($skip_dn,$dn)){
705 $skip = true;
706 }
707 }
708 if($skip){
709 unset($this->deps_to_migrate[$key]);
710 }
711 }
713 /* If we have no invisible departments found
714 * tell the user that everything is ok
715 */
716 if(!$res){
717 $this->checks['deps_visible']['STATUS'] = FALSE;
718 $this->checks['deps_visible']['STATUS_MSG']= _("LDAP query failed");
719 $this->checks['deps_visible']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
720 }elseif(count($this->deps_to_migrate) == 0 ){
721 $this->checks['deps_visible']['STATUS'] = TRUE;
722 $this->checks['deps_visible']['STATUS_MSG']= _("Ok");
723 $this->checks['deps_visible']['ERROR_MSG'] = "";
724 }else{
725 $this->checks['deps_visible']['STATUS'] = TRUE;
726 $this->checks['deps_visible']['STATUS_MSG']= '<font style="color:#FFA500">'._("Warning").'</font>';
727 $this->checks['deps_visible']['ERROR_MSG'] = sprintf(_("Found %s department(s) that will not be visible in GOsa."),count($this->deps_to_migrate));
728 $this->checks['deps_visible']['ERROR_MSG'] .= " <input type='submit' name='deps_visible_migrate' value='"._("Migrate")."...'>";
729 }
730 }
734 /* Start deparmtment migration */
735 function migrate_organizationalUnits($only_ldif = FALSE)
736 {
737 $this->show_details= $only_ldif;
739 /* Get collected configuration settings */
740 $cv = $this->parent->captured_values;
742 /* Establish ldap connection */
743 $ldap = new LDAP($cv['admin'],
744 $cv['password'],
745 $cv['connection'],
746 FALSE,
747 $cv['tls']);
749 /* Add gosaDepartment objectClass to each selected entry
750 */
751 foreach($this->deps_to_migrate as $key => $dep){
752 if($dep['checked']){
754 /* Get current objectClasses */
755 $ldap->cat($dep['dn'],array("objectClass","description"));
756 $attrs = $ldap->fetch();
758 /* Create new objectClass attribute including gosaDepartment*/
759 $new_attrs = array();
760 for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
761 $new_attrs['objectClass'][] = $attrs['objectClass'][$i];
762 }
763 $new_attrs['objectClass'][] = "gosaDepartment";
765 /* Append description it is missing */
766 if(!isset($attrs['description'])){
767 $new_attrs['description'][] = "GOsa department";
768 }
770 /* Depending on the parameter >only_diff< we save the changes as ldif
771 * or we write our changes directly to the ldap database
772 */
773 if($only_ldif){
774 $this->deps_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
775 $this->deps_to_migrate[$key]['after'] = $this->array_to_ldif($new_attrs);
776 }else{
777 $ldap->cd($attrs['dn']);
778 if(!$ldap->modify($new_attrs)){
779 print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
780 return(false);
781 }
782 }
783 }
784 }
785 return(TRUE);
786 }
789 /* Check Acls if there is at least one object with acls defined
790 */
791 function check_administrativeAccount()
792 {
793 /* Establish ldap connection */
794 $cv = $this->parent->captured_values;
795 $ldap = new LDAP($cv['admin'],
796 $cv['password'],
797 $cv['connection'],
798 FALSE,
799 $cv['tls']);
801 /* Search for gosaAcls */
802 $ldap->cd($cv['base']);
803 $res = $ldap->cat($cv['base']);
805 if(!$res){
806 $this->checks['acls']['STATUS'] = FALSE;
807 $this->checks['acls']['STATUS_MSG']= _("LDAP query failed");
808 $this->checks['acls']['ERROR_MSG'] = _("Possibly the 'root object' is missing.");
809 }else{
810 $found = false;
811 $username = "";
812 $attrs = $ldap->fetch();
813 if(isset($attrs['gosaAclEntry'])){
814 $acls = $attrs['gosaAclEntry'];
815 for($i = 0 ; $i < $acls['count'] ; $i++){
816 $acl = $acls[$i];
817 $tmp = split(":",$acl);
818 if($tmp[1] == "psub"){
819 $members = split(",",$tmp[2]);
820 foreach($members as $member){
821 $member = base64_decode($member);
823 /* Check if acl owner is a valid GOsa user account */
824 $ldap->cat($member,array("objectClass","uid","cn"));
825 $ret = $ldap->fetch();
827 if(isset($ret['objectClass']) && in_array("posixGroup",$ret['objectClass'])){
828 $found = TRUE;
829 $username .= "ACL-Group: ".$ret['cn'][0]."<br>";
830 }elseif(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
831 in_array("organizationalPerson",$ret['objectClass']) &&
832 in_array("inetOrgPerson",$ret['objectClass'])){
833 $found = TRUE;
834 $username .= "ACL: ".$ret['uid'][0]."<br>";
835 }
836 }
837 }elseif($tmp[1] == "role"){
839 /* Check if acl owner is a valid GOsa user account */
840 $ldap->cat(base64_decode($tmp[2]),array("gosaAclTemplate"));
841 $ret = $ldap->fetch();
843 if(isset($ret['gosaAclTemplate'])){
844 $cnt = $ret['gosaAclTemplate']['count'];
845 for($e = 0 ; $e < $cnt ; $e++){
847 $a_str = $ret['gosaAclTemplate'][$e];
848 if(preg_match("/^[0-9]*:psub:/",$a_str) && preg_match("/:all;cmdrw$/",$a_str)){
850 $members = split(",",$tmp[3]);
851 foreach($members as $member){
852 $member = base64_decode($member);
854 /* Check if acl owner is a valid GOsa user account */
855 $ldap->cat($member,array("objectClass","uid"));
856 $ret = $ldap->fetch();
858 if(isset($ret['objectClass']) && in_array("gosaAccount",$ret['objectClass']) &&
859 in_array("organizationalPerson",$ret['objectClass']) &&
860 in_array("inetOrgPerson",$ret['objectClass'])){
861 $found = TRUE;
862 $username .= "ACL Role: ".$ret['uid'][0]."<br>";
863 }
864 }
865 }
866 }
867 }
868 }
869 }
870 }
872 # For debugging
873 #echo $username;
875 if($found){
876 $this->checks['acls']['STATUS'] = TRUE;
877 $this->checks['acls']['STATUS_MSG']= _("Ok");
878 $this->checks['acls']['ERROR_MSG'] = "";
879 }else{
880 $this->checks['acls']['STATUS'] = FALSE;
881 $this->checks['acls']['STATUS_MSG']= _("Failed");
882 $this->checks['acls']['ERROR_MSG']= _("There is no GOsa administrator account inside your LDAP.")." ";
883 $this->checks['acls']['ERROR_MSG'].= "<input type='submit' name='create_acls' value='"._("Create")."'>";
884 }
885 }
886 return($ldap->count()>=1);
887 }
891 function create_admin($only_ldif = FALSE)
892 {
893 /* Reset '' */
894 $this->acl_create_changes="";
896 /* Object that should receive admin acls */
897 $dn = $this->acl_create_selected;
899 /* Get collected configuration settings */
900 $cv = $this->parent->captured_values;
902 /* Establish ldap connection */
903 $ldap = new LDAP($cv['admin'],
904 $cv['password'],
905 $cv['connection'],
906 FALSE,
907 $cv['tls']);
909 /* Get current base attributes */
910 $ldap->cd($cv['base']);
911 $ldap->cat($cv['base'],array("dn","objectClass","gosaAclEntry"));
912 $attrs = $ldap->fetch();
914 /* Add acls for the selcted user to the base */
915 $attrs_new['objectClass'] = array("gosaACL");
917 for($i = 0; $i < $attrs['objectClass']['count']; $i ++){
918 if(!in_array_ics($attrs['objectClass'][$i],$attrs_new['objectClass'])){
919 $attrs_new['objectClass'][] = $attrs['objectClass'][$i];
920 }
921 }
923 $acl = "0:psub:".base64_encode($dn).":all;cmdrw";
924 $attrs_new['gosaAclEntry'][] = $acl;
925 if(isset($attrs['gosaAclEntry'])){
926 for($i = 0 ; $i < $attrs['gosaAclEntry']['count']; $i ++){
928 $prio = preg_replace("/[:].*$/","",$attrs['gosaAclEntry'][$i]);
929 $rest = preg_replace("/^[^:]/","",$attrs['gosaAclEntry'][$i]);
931 $data = ($prio+1).$rest;
932 $attrs_new['gosaAclEntry'][] = $data;
933 }
934 }
936 if($only_ldif){
937 $this->acl_create_changes ="\n".$cv['base']."\n";
938 $this->acl_create_changes.=$this->array_to_ldif($attrs)."\n";
939 $this->acl_create_changes.="\n".$cv['base']."\n";
940 $this->acl_create_changes.=$this->array_to_ldif($attrs_new);
941 }else{
943 $ldap->cd($cv['base']);
944 if(!$ldap->modify($attrs_new)){
945 print_red(sprintf(_("Adding acls for user '%s' failed, ldap says '%s'."),$dn,$ldap->get_error()));
946 return(FALSE);
947 }else{
948 return(TRUE);
949 }
950 }
951 }
954 function create_admin_user()
955 {
956 $pw1 = $pw2 = "";
957 $uid = "";
959 if(isset($_POST['new_user_uid'])){
960 $uid = $_POST['new_user_uid'];
961 }
962 if(isset($_POST['new_user_password'])){
963 $pw1 = $_POST['new_user_password'];
964 }
965 if(isset($_POST['new_user_password2'])){
966 $pw2 = $_POST['new_user_password2'];
967 }
969 if(empty($pw1) || empty($pw2) | ($pw1 != $pw2)){
970 print_red(_("Specified passwords are empty or not equal."));
971 return false;
972 }
974 if(!is_uid($uid) || empty($uid)){
975 print_red(_("Please specify a valid uid."));
976 return false;
977 }
980 /* Establish ldap connection */
981 $cv = $this->parent->captured_values;
982 $ldap = new LDAP($cv['admin'],
983 $cv['password'],
984 $cv['connection'],
985 FALSE,
986 $cv['tls']);
988 /* Get current base attributes */
989 $ldap->cd($cv['base']);
991 $people_ou = trim($cv['peopleou']);
992 if(!empty($people_ou)){
993 $people_ou = trim($people_ou).",";
994 }
996 if($cv['peopledn'] == "cn"){
997 $dn = "cn=System Administrator,".$people_ou.$cv['base'];
998 }else{
999 $dn = "uid=".$uid.",".$people_ou.$cv['base'];
1000 }
1002 $methods = @passwordMethod::get_available_methods();
1003 $p_m = $methods[$cv['encryption']];
1004 $p_c = new $p_m(array());
1005 $hash = $p_c->generate_hash($pw2);
1007 $new_user=array();
1008 $new_user['objectClass']= array("top","person","gosaAccount","organizationalPerson","inetOrgPerson");
1009 $new_user['givenName'] = "System";
1010 $new_user['sn'] = "Administrator";
1011 $new_user['cn'] = "System Administrator";
1012 $new_user['uid'] = $uid;
1013 $new_user['userPassword'] = $hash;
1015 $ldap->cd($cv['base']);
1017 $ldap->cat($dn,array("dn"));
1018 if($ldap->count()){
1019 print_red(sprintf(_("Could not add administrative user, there is already an object with the same dn '%s' in your ldap database."),$dn));
1020 return(FALSE);
1021 }
1023 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$dn));
1024 $ldap->cd($dn);
1025 $res = $ldap->add($new_user);
1026 $this->acl_create_selected = $dn;
1027 $this->create_admin();
1029 if(!$res){
1030 print_red($ldap->get_error());
1031 return(FALSE);
1032 }
1034 $this->acl_create_dialog=FALSE;
1035 $this->check_administrativeAccount();
1036 return(TRUE);
1037 }
1040 function migrate_outside_winstations($perform = FALSE)
1041 {
1042 /* Establish ldap connection */
1043 $cv = $this->parent->captured_values;
1044 $ldap = new LDAP($cv['admin'],
1045 $cv['password'],
1046 $cv['connection'],
1047 FALSE,
1048 $cv['tls']);
1050 $ldap->cd($cv['base']);
1052 /* Check if there was a destination department posted */
1053 if(isset($_POST['move_winstation_to'])){
1054 $destination_dep = $_POST['move_winstation_to'];
1055 }else{
1056 print_red(_("Couldn't move users to specified department."));
1057 return(false);
1058 }
1060 foreach($this->outside_winstations as $b_dn => $data){
1061 $this->outside_winstations[$b_dn]['ldif'] ="";
1062 if($data['selected']){
1063 $dn = base64_decode($b_dn);
1064 $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1065 if(!$perform){
1066 $this->outside_winstations[$b_dn]['ldif'] = _("Winstation will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1069 /* Check if there are references to this object */
1070 $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1071 $refs = "";
1072 while($attrs = $ldap->fetch()){
1073 $ref_dn = $attrs['dn'];
1074 $refs .= "<br />\t".$ref_dn;
1075 }
1076 if(!empty($refs)){
1077 $this->outside_winstations[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1078 }
1080 }else{
1081 $this->move($dn,$d_dn);
1082 }
1083 }
1084 }
1085 }
1088 function migrate_outside_groups($perform = FALSE)
1089 {
1090 /* Establish ldap connection */
1091 $cv = $this->parent->captured_values;
1092 $ldap = new LDAP($cv['admin'],
1093 $cv['password'],
1094 $cv['connection'],
1095 FALSE,
1096 $cv['tls']);
1098 $ldap->cd($cv['base']);
1100 /* Check if there was a destination department posted */
1101 if(isset($_POST['move_group_to'])){
1102 $destination_dep = $_POST['move_group_to'];
1103 }else{
1104 print_red(_("Couldn't move users to specified department."));
1105 return(false);
1106 }
1108 foreach($this->outside_groups as $b_dn => $data){
1109 $this->outside_groups[$b_dn]['ldif'] ="";
1110 if($data['selected']){
1111 $dn = base64_decode($b_dn);
1112 $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1113 if(!$perform){
1114 $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1117 /* Check if there are references to this object */
1118 $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1119 $refs = "";
1120 while($attrs = $ldap->fetch()){
1121 $ref_dn = $attrs['dn'];
1122 $refs .= "<br />\t".$ref_dn;
1123 }
1124 if(!empty($refs)){
1125 $this->outside_groups[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
1126 }
1128 }else{
1129 $this->move($dn,$d_dn);
1130 }
1131 }
1132 }
1133 }
1136 function migrate_outside_users($perform = FALSE)
1137 {
1138 /* Establish ldap connection */
1139 $cv = $this->parent->captured_values;
1140 $ldap = new LDAP($cv['admin'],
1141 $cv['password'],
1142 $cv['connection'],
1143 FALSE,
1144 $cv['tls']);
1146 $ldap->cd($cv['base']);
1148 /* Check if there was a destination department posted */
1149 if(isset($_POST['move_user_to'])){
1150 $destination_dep = $_POST['move_user_to'];
1151 }else{
1152 print_red(_("Couldn't move users to specified department."));
1153 return(false);
1154 }
1156 foreach($this->outside_users as $b_dn => $data){
1157 $this->outside_users[$b_dn]['ldif'] ="";
1158 if($data['selected']){
1159 $dn = base64_decode($b_dn);
1160 $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
1161 if(!$perform){
1162 $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
1164 /* Check if there are references to this object */
1165 $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
1166 $refs = "";
1167 while($attrs = $ldap->fetch()){
1168 $ref_dn = $attrs['dn'];
1169 $refs .= "<br />\t".$ref_dn;
1170 }
1171 if(!empty($refs)){
1172 $this->outside_users[$b_dn]['ldif'] .= "<br /><br /><i>"._("The following references will be updated").":</i>".$refs;
1173 }
1175 }else{
1176 $this->move($dn,$d_dn);
1177 }
1178 }
1179 }
1180 }
1183 function execute()
1184 {
1185 /* Initialise checks if this is the first call */
1186 if(!$this->checks_initialised || isset($_POST['reload'])){
1187 $this->initialize_checks();
1188 $this->checks_initialised = TRUE;
1189 }
1191 /*************
1192 * Winstations outside the group ou
1193 *************/
1195 if(isset($_POST['outside_winstations_dialog_cancel'])){
1196 $this->outside_winstations_dialog = FALSE;
1197 $this->dialog = FALSE;
1198 $this->show_details = FALSE;
1199 }
1201 if(isset($_POST['outside_winstations_dialog_whats_done'])){
1202 $this->migrate_outside_winstations(FALSE);
1203 }
1205 if(isset($_POST['outside_winstations_dialog_perform'])){
1206 $this->migrate_outside_winstations(TRUE);
1207 $this->search_outside_winstations();
1208 $this->dialog = FALSE;
1209 $this->show_details = FALSE;
1210 $this->outside_winstations_dialog = FALSE;
1211 }
1213 if(isset($_POST['outside_winstations_dialog'])){
1214 $this->outside_winstations_dialog = TRUE;
1215 $this->dialog = TRUE;
1216 }
1218 if($this->outside_winstations_dialog){
1219 $smarty = get_smarty();
1220 $smarty->assign("ous",$this->get_all_winstation_ous());
1221 $smarty->assign("method","outside_winstations");
1222 $smarty->assign("outside_winstations",$this->outside_winstations);
1223 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1224 }
1225 /*************
1226 * Groups outside the group ou
1227 *************/
1229 if(isset($_POST['outside_groups_dialog_cancel'])){
1230 $this->outside_groups_dialog = FALSE;
1231 $this->show_details = FALSE;
1232 $this->dialog = FALSE;
1233 }
1235 if(isset($_POST['outside_groups_dialog_whats_done'])){
1236 $this->show_details= TRUE;
1237 $this->migrate_outside_groups(FALSE);
1238 }
1240 if(isset($_POST['outside_groups_dialog_refresh'])){
1241 $this->show_details= FALSE;
1242 }
1244 if(isset($_POST['outside_groups_dialog_perform'])){
1245 $this->migrate_outside_groups(TRUE);
1246 $this->dialog = FALSE;
1247 $this->show_details = FALSE;
1248 $this->outside_groups_dialog = FALSE;
1249 $this->initialize_checks();
1250 }
1252 if(isset($_POST['outside_groups_dialog'])){
1253 $this->outside_groups_dialog = TRUE;
1254 $this->dialog = TRUE;
1255 }
1257 if($this->outside_groups_dialog){
1258 $smarty = get_smarty();
1259 $smarty->assign("ous",$this->get_all_group_ous());
1260 $smarty->assign("method","outside_groups");
1261 $smarty->assign("outside_groups",$this->outside_groups);
1262 $smarty->assign("group_details", $this->show_details);
1263 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1264 }
1266 /*************
1267 * User outside the people ou
1268 *************/
1270 if(isset($_POST['outside_users_dialog_cancel'])){
1271 $this->outside_users_dialog = FALSE;
1272 $this->dialog = FALSE;
1273 $this->show_details = FALSE;
1274 }
1276 if(isset($_POST['outside_users_dialog_whats_done'])){
1277 $this->show_details= TRUE;
1278 $this->migrate_outside_users(FALSE);
1279 }
1281 if(isset($_POST['outside_users_dialog_perform'])){
1282 $this->migrate_outside_users(TRUE);
1283 $this->initialize_checks();
1284 $this->dialog = FALSE;
1285 $this->show_details = FALSE;
1286 $this->outside_users_dialog = FALSE;
1287 }
1289 if (isset($_POST['outside_users_dialog_refresh'])){
1290 $this->show_details= FALSE;
1291 }
1293 if(isset($_POST['outside_users_dialog'])){
1294 $this->outside_users_dialog = TRUE;
1295 $this->dialog = TRUE;
1296 }
1298 if($this->outside_users_dialog){
1299 $smarty = get_smarty();
1300 $smarty->assign("ous",$this->get_all_people_ous());
1301 $smarty->assign("method","outside_users");
1302 $smarty->assign("outside_users",$this->outside_users);
1303 $smarty->assign("user_details", $this->show_details);
1304 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1305 }
1307 /*************
1308 * Root object check
1309 *************/
1311 if(isset($_POST['retry_root_create'])){
1313 $state = $this->checks['root']['STATUS'];
1314 $this->checkBase(FALSE);
1315 if($state != $this->checks['root']['STATUS']){
1316 $this->initialize_checks();
1317 }
1318 }
1320 /*************
1321 * User Migration handling
1322 *************/
1324 if(isset($_POST['retry_acls'])){
1325 $this->check_administrativeAccount();
1326 }
1328 if(isset($_POST['create_acls'])){
1329 $this->acl_create_dialog = TRUE;
1330 $this->dialog = TRUE;
1331 }
1333 if(isset($_POST['create_acls_cancel'])){
1334 $this->acl_create_dialog = FALSE;
1335 $this->dialog = FALSE;
1336 $this->show_details = FALSE;
1337 }
1339 # if(isset($_POST['create_acls_create_confirmed'])){
1340 # if($this->create_admin()){
1341 # $this->acl_create_dialog = FALSE;
1342 # $this->dialog = FALSE;
1343 # $this->show_details = FALSE;
1344 # $this->initialize_checks();
1345 # }
1346 # }
1348 if(isset($_POST['create_acls_create'])){
1349 $this->create_admin(TRUE);
1350 }
1352 if(isset($_POST['create_admin_user'])){
1353 if($this->create_admin_user()){
1354 $this->dialog = FALSE;
1355 $this->show_details = FALSE;
1356 }
1357 }
1359 if($this->acl_create_dialog){
1360 $smarty = get_smarty();
1362 $uid = "admin";
1363 if(isset($_POST['new_user_uid'])){
1364 $uid = $_POST['new_user_uid'];
1365 }
1367 $smarty->assign("new_user_uid",$uid);
1368 $smarty->assign("new_user_password",@$_POST['new_user_password']);
1369 $smarty->assign("new_user_password2",@$_POST['new_user_password2']);
1370 $smarty->assign("method","create_acls");
1371 $smarty->assign("acl_create_selected",$this->acl_create_selected);
1372 $smarty->assign("what_will_be_done_now",$this->acl_create_changes);
1373 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1374 }
1376 /*************
1377 * User Migration handling
1378 *************/
1380 /* Refresh list of deparments */
1381 if(isset($_POST['users_visible_migrate_refresh'])){
1382 $this->check_gosaAccounts();
1383 }
1385 /* Open migration dialog */
1386 if(isset($_POST['users_visible_migrate'])){
1387 $this->show_details= FALSE;
1388 $this->users_migration_dialog = TRUE;
1389 $this->dialog =TRUE;
1390 }
1392 /* Close migration dialog */
1393 if(isset($_POST['users_visible_migrate_close'])){
1394 $this->users_migration_dialog = FALSE;
1395 $this->dialog =FALSE;
1396 $this->show_details = FALSE;
1397 }
1399 /* Start migration */
1400 if(isset($_POST['users_visible_migrate_migrate'])){
1401 if($this->migrate_gosaAccounts()){
1402 $this->initialize_checks();
1403 $this->dialog = FALSE;
1404 $this->show_details = FALSE;
1405 $this->users_migration_dialog = FALSE;
1406 }
1407 }
1409 /* Start migration */
1410 if(isset($_POST['users_visible_migrate_whatsdone'])){
1411 $this->migrate_gosaAccounts(TRUE);
1412 }
1414 /* Display migration dialog */
1415 if($this->users_migration_dialog){
1416 $smarty = get_smarty();
1417 $smarty->assign("users_to_migrate",$this->users_to_migrate);
1418 $smarty->assign("method","migrate_users");
1419 $smarty->assign("user_details", $this->show_details);
1420 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1421 }
1424 /*************
1425 * Department Migration handling
1426 *************/
1428 /* Refresh list of deparments */
1429 if(isset($_POST['deps_visible_migrate_refresh'])){
1430 $this->check_organizationalUnits();
1431 $this->show_details= FALSE;
1432 }
1434 /* Open migration dialog */
1435 if(isset($_POST['deps_visible_migrate'])){
1436 $this->dep_migration_dialog = TRUE;
1437 $this->dialog =TRUE;
1438 }
1440 /* Close migration dialog */
1441 if(isset($_POST['deps_visible_migrate_close'])){
1442 $this->dep_migration_dialog = FALSE;
1443 $this->dialog =FALSE;
1444 $this->show_details = FALSE;
1445 }
1447 /* Start migration */
1448 if(isset($_POST['deps_visible_migrate_migrate'])){
1449 if($this->migrate_organizationalUnits()){
1450 $this->show_details= FALSE;
1451 $this->check_organizationalUnits();
1452 $this->dialog = FALSE;
1453 $this->dep_migration_dialog = FALSE;
1454 }
1455 }
1457 /* Start migration */
1458 if(isset($_POST['deps_visible_migrate_whatsdone'])){
1459 $this->migrate_organizationalUnits(TRUE);
1460 }
1462 /* Display migration dialog */
1463 if($this->dep_migration_dialog){
1464 $smarty = get_smarty();
1465 $smarty->assign("deps_to_migrate",$this->deps_to_migrate);
1466 $smarty->assign("method","migrate_deps");
1467 $smarty->assign("deps_details", $this->show_details);
1468 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1469 }
1471 $smarty = get_smarty();
1472 $smarty->assign("checks",$this->checks);
1473 $smarty->assign("method","default");
1474 return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
1475 }
1478 function save_object()
1479 {
1480 $this->is_completed= TRUE;
1482 /* Capture all selected winstations from outside_winstations_dialog */
1483 if($this->outside_winstations_dialog){
1484 foreach($this->outside_winstations as $dn => $data){
1485 if(isset($_POST['select_winstation_'.$dn])){
1486 $this->outside_winstations[$dn]['selected'] = TRUE;
1487 }else{
1488 $this->outside_winstations[$dn]['selected'] = FALSE;
1489 }
1490 }
1491 }
1493 /* Capture all selected groups from outside_groups_dialog */
1494 if($this->outside_groups_dialog){
1495 foreach($this->outside_groups as $dn => $data){
1496 if(isset($_POST['select_group_'.$dn])){
1497 $this->outside_groups[$dn]['selected'] = TRUE;
1498 }else{
1499 $this->outside_groups[$dn]['selected'] = FALSE;
1500 }
1501 }
1502 }
1504 /* Capture all selected users from outside_users_dialog */
1505 if($this->outside_users_dialog){
1506 foreach($this->outside_users as $dn => $data){
1507 if(isset($_POST['select_user_'.$dn])){
1508 $this->outside_users[$dn]['selected'] = TRUE;
1509 }else{
1510 $this->outside_users[$dn]['selected'] = FALSE;
1511 }
1512 }
1513 }
1515 /* Get "create acl" dialog posts */
1516 if($this->acl_create_dialog){
1518 if(isset($_POST['create_acls_create_abort'])){
1519 $this->acl_create_selected = "";
1520 }
1521 }
1523 /* Get selected departments */
1524 if($this->dep_migration_dialog){
1525 foreach($this->deps_to_migrate as $id => $data){
1526 if(isset($_POST['migrate_'.$id])){
1527 $this->deps_to_migrate[$id]['checked'] = TRUE;
1528 }else{
1529 $this->deps_to_migrate[$id]['checked'] = FALSE;
1530 }
1531 }
1532 }
1534 /* Get selected users */
1535 if($this->users_migration_dialog){
1536 foreach($this->users_to_migrate as $id => $data){
1537 if(isset($_POST['migrate_'.$id])){
1538 $this->users_to_migrate[$id]['checked'] = TRUE;
1539 }else{
1540 $this->users_to_migrate[$id]['checked'] = FALSE;
1541 }
1542 }
1543 }
1544 }
1547 /* Check if the root object exists.
1548 * If the parameter just_check is true, then just check if the
1549 * root object is missing and update the info messages.
1550 * If the Parameter is false, try to create a new root object.
1551 */
1552 function checkBase($just_check = TRUE)
1553 {
1554 /* Get collected setup informations */
1555 $cv = $this->parent->captured_values;
1557 /* Establish ldap connection */
1558 $ldap = new LDAP($cv['admin'],
1559 $cv['password'],
1560 $cv['connection'],
1561 FALSE,
1562 $cv['tls']);
1564 /* Check if root object exists */
1565 $ldap->cd($cv['base']);
1566 $res = $ldap->search("(objectClass=*)");
1567 $err = ldap_errno($ldap->cid);
1569 if( !$res ||
1570 $err == 0x20 || # LDAP_NO_SUCH_OBJECT
1571 $err == 0x40) { # LDAP_NAMING_VIOLATION
1573 /* Root object doesn't exists
1574 */
1575 if($just_check){
1576 $this->checks['root']['STATUS'] = FALSE;
1577 $this->checks['root']['STATUS_MSG']= _("Failed");
1578 $this->checks['root']['ERROR_MSG'] = _("The LDAP root object is missing. It is required to use your LDAP service.").' ';
1579 $this->checks['root']['ERROR_MSG'].= "<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1580 return(FALSE);
1581 }else{
1583 /* Add root object */
1584 $ldap->cd($cv['base']);
1585 $res = $ldap->create_missing_trees($cv['base']);
1587 /* If adding failed, tell the user */
1588 if(!$res){
1589 $this->checks['root']['STATUS'] = FALSE;
1590 $this->checks['root']['STATUS_MSG']= _("Failed");
1591 $this->checks['root']['ERROR_MSG'] = _("Root object couldn't be created, you should try it on your own.");
1592 $this->checks['root']['ERROR_MSG'].= " <input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
1593 return($res);;
1594 }
1595 }
1596 }
1598 /* Create & remove of dummy object was successful */
1599 $this->checks['root']['STATUS'] = TRUE;
1600 $this->checks['root']['STATUS_MSG']= _("Ok");
1601 }
1604 /* Return ldif information for a
1605 * given attribute array
1606 */
1607 function array_to_ldif($atts)
1608 {
1609 $ret = "";
1610 unset($atts['count']);
1611 unset($atts['dn']);
1612 foreach($atts as $name => $value){
1613 if(is_numeric($name)) {
1614 continue;
1615 }
1616 if(is_array($value)){
1617 unset($value['count']);
1618 foreach($value as $a_val){
1619 $ret .= $name.": ". $a_val."\n";
1620 }
1621 }else{
1622 $ret .= $name.": ". $value."\n";
1623 }
1624 }
1625 return(preg_replace("/\n$/","",$ret));
1626 }
1629 function get_user_list()
1630 {
1631 /* Get collected configuration settings */
1632 $cv = $this->parent->captured_values;
1634 /* Establish ldap connection */
1635 $ldap = new LDAP($cv['admin'],
1636 $cv['password'],
1637 $cv['connection'],
1638 FALSE,
1639 $cv['tls']);
1641 $ldap->cd($cv['base']);
1642 $ldap->search("(objectClass=gosaAccount)",array("dn"));
1644 $tmp = array();
1645 while($attrs = $ldap->fetch()){
1646 $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1647 }
1648 return($tmp);
1649 }
1652 function get_all_people_ous()
1653 {
1654 /* Get collected configuration settings */
1655 $cv = $this->parent->captured_values;
1656 $people_ou = trim($cv['peopleou']);
1658 /* Establish ldap connection */
1659 $ldap = new LDAP($cv['admin'],
1660 $cv['password'],
1661 $cv['connection'],
1662 FALSE,
1663 $cv['tls']);
1666 /*****************
1667 * If people ou is NOT empty
1668 * search for for all objects matching the given container
1669 *****************/
1670 if(!empty($people_ou)){
1671 $ldap->search("(".$people_ou.")",array("dn"));
1673 /* Create people ou if there is currently none */
1674 if($ldap->count() == 0 ){
1675 $add_dn = $cv['peopleou'].",".$cv['base'];
1676 $naming_attr = preg_replace("/=.*$/","",$add_dn);
1677 $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1678 $add = array();
1679 $add['objectClass'] = array("organizationalUnit");
1680 $add[$naming_attr] = $naming_value;
1681 $ldap->cd($cv['base']);
1682 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1683 $ldap->cd($add_dn);
1684 $ldap->add($add);
1685 }
1687 /* Create result */
1688 $ldap->search("(".$cv['peopleou'].")",array("dn"));
1689 $tmp = array();
1690 while($attrs= $ldap->fetch()){
1691 if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1692 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1693 }
1694 }
1695 } else{
1697 /************
1698 * If people ou is empty
1699 * Get all valid gosaDepartments
1700 ************/
1701 $ldap->cd($cv['base']);
1702 $tmp = array();
1703 $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
1704 $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
1705 while($attrs = $ldap->fetch()){
1706 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
1707 }
1708 }
1709 return($tmp);
1710 }
1713 function get_all_winstation_ous()
1714 {
1715 /* Get collected configuration settings */
1716 $cv = $this->parent->captured_values;
1718 /* Establish ldap connection */
1719 $ldap = new LDAP($cv['admin'],
1720 $cv['password'],
1721 $cv['connection'],
1722 FALSE,
1723 $cv['tls']);
1725 /* Get winstation ou */
1726 if($cv['generic_settings']['wws_ou_active']) {
1727 $winstation_ou = $cv['generic_settings']['ws_ou'];
1728 }else{
1729 $winstation_ou = "ou=winstations";
1730 }
1732 $ldap->cd($cv['base']);
1733 $ldap->search("(".$winstation_ou.")",array("dn"));
1735 if($ldap->count() == 0 ){
1736 $add_dn = $winstation_ou.",ou=systems,".$cv['base'];
1737 $naming_attr = preg_replace("/=.*$/","",$add_dn);
1738 $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1739 $add = array();
1740 $add['objectClass'] = array("organizationalUnit");
1741 $add[$naming_attr] = $naming_value;
1743 $ldap->cd($cv['base']);
1744 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1745 $ldap->cd($add_dn);
1746 $ldap->add($add);
1747 }
1749 $ldap->search("(".$winstation_ou.")",array("dn"));
1750 $tmp = array();
1751 while($attrs= $ldap->fetch()){
1752 if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1753 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1754 }
1755 }
1756 return($tmp);
1757 }
1760 function get_all_group_ous()
1761 {
1762 /* Get collected configuration settings */
1763 $cv = $this->parent->captured_values;
1765 /* Establish ldap connection */
1766 $ldap = new LDAP($cv['admin'],
1767 $cv['password'],
1768 $cv['connection'],
1769 FALSE,
1770 $cv['tls']);
1772 $group_ou = trim($cv['groupou']);
1773 if(!empty($group_ou)){
1774 $group_ou = trim($group_ou);
1775 }
1777 /************
1778 * If group ou is NOT empty
1779 * Get all valid group ous, create one if necessary
1780 ************/
1781 $ldap->cd($cv['base']);
1782 if(!empty($group_ou)){
1783 $ldap->search("(".$group_ou.")",array("dn"));
1784 if($ldap->count() == 0 ){
1785 $add_dn = $group_ou.$cv['base'];
1786 $naming_attr = preg_replace("/=.*$/","",$add_dn);
1787 $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
1788 $add = array();
1789 $add['objectClass'] = array("organizationalUnit");
1790 $add[$naming_attr] = $naming_value;
1792 $ldap->cd($cv['base']);
1793 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
1794 $ldap->cd($add_dn);
1795 $ldap->add($add);
1796 }
1797 $ldap->search("(".$group_ou.")",array("dn"));
1798 $tmp = array();
1799 while($attrs= $ldap->fetch()){
1800 if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
1801 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
1802 }
1803 }
1804 }else{
1805 /************
1806 * If group ou is empty
1807 * Get all valid gosaDepartments
1808 ************/
1809 $ldap->cd($cv['base']);
1810 $tmp = array();
1811 $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
1812 $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
1813 while($attrs = $ldap->fetch()){
1814 $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
1815 }
1816 }
1817 return($tmp);
1818 }
1821 function get_group_list()
1822 {
1823 /* Get collected configuration settings */
1824 $cv = $this->parent->captured_values;
1826 /* Establish ldap connection */
1827 $ldap = new LDAP($cv['admin'],
1828 $cv['password'],
1829 $cv['connection'],
1830 FALSE,
1831 $cv['tls']);
1833 $ldap->cd($cv['base']);
1834 $ldap->search("(objectClass=posixGroup)",array("dn"));
1836 $tmp = array();
1837 while($attrs = $ldap->fetch()){
1838 $tmp[base64_encode($attrs['dn'])] = @LDAP::fix($attrs['dn']);
1839 }
1840 return($tmp);
1841 }
1844 function move($source,$destination)
1845 {
1846 /* Get collected configuration settings */
1847 $cv = $this->parent->captured_values;
1849 /* Establish ldap connection */
1850 $ldap = new LDAP($cv['admin'],
1851 $cv['password'],
1852 $cv['connection'],
1853 FALSE,
1854 $cv['tls']);
1856 /* Update object references in gosaGroupOfNames */
1857 $ogs_to_fix = array();
1858 $ldap->cd($cv['base']);
1859 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($source).'))', array('cn','member'));
1860 while ($attrs= $ldap->fetch()){
1861 $dn = $attrs['dn'];
1862 $attrs = $this->cleanup_array($attrs);
1863 $member_new = array($destination);
1864 foreach($attrs['member'] as $member){
1865 if($member != $source){
1866 $member_new[] = $member;
1867 }
1868 }
1869 $attrs['member'] = $member_new;
1870 $ogs_to_fix[$dn] = $attrs;
1871 }
1873 /* Copy source to destination dn */
1874 $ldap->cat($source);
1875 $new_data = $this->cleanup_array($ldap->fetch());
1876 $ldap->cd($destination);
1877 $res = $ldap->add($new_data);
1879 /* Display warning if copy failed */
1880 if(!$res){
1881 print_red(_("Failed to copy '%s' to '%s'. LDAP says '%s'."),$source,$destination,$ldap->get_error());
1882 }else{
1883 $res = $ldap->rmDir($source);
1884 show_ldap_error($ldap->get_error(),_("Something went wrong while copying dns."));
1886 /* Object is copied, so update its references */
1887 foreach($ogs_to_fix as $dn => $data){
1888 $ldap->cd($dn);
1889 $ldap->modify($data);
1890 }
1891 }
1892 }
1895 /* Cleanup ldap result to be able to write it be to ldap */
1896 function cleanup_array($attrs)
1897 {
1898 foreach($attrs as $key => $value) {
1899 if(is_numeric($key) || in_array($key,array("count","dn"))){
1900 unset($attrs[$key]);
1901 }
1902 if(is_array($value) && isset($value['count'])){
1903 unset($attrs[$key]['count']);
1904 }
1905 }
1906 return($attrs);
1907 }
1908 }
1910 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1911 ?>