1 <?php
3 define("DEBUG_FAI_FUNC",FALSE);
5 /* TEST PHASE .... */
7 /* Returns all object for the given release.
8 This function resolves the releases
9 from base up to the given dn.
10 */
11 function get_all_objects_for_given_base($Current_DN,$filter,$detailed = false)
12 {
13 global $config;
14 $ldap = $config->get_ldap_link();
15 $ldap->cd($config->current['BASE']);
17 /* Collect some basic informations and initialize some variables */
18 $base_release = get_release_dn($Current_DN);
19 $previous_releases = array_reverse(get_previous_releases_of_this_release($base_release,true));
20 $res = array();
21 $tmp = array();
23 /* We must also include the given release dn */
24 $previous_releases[] = $base_release;
26 /* Walk through all releases */
27 foreach($previous_releases as $release){
29 /* Get fai departments */
30 $deps_to_search = get_FAI_departments($release);
32 /* For every single department (ou=hoos,ou ..) */
33 foreach($deps_to_search as $fai_base){
35 /* Ldap search for fai classes specified in this release */
36 $ldap->cd($fai_base);
37 $ldap->search($filter,array("dn","objectClass","FAIstate"));
39 /* check the returned objects, and add/replace them in our return variable */
40 while($attr = $ldap->fetch()){
42 $buffer = array();
43 $name = str_ireplace($release,"",$attr['dn']);
45 if(isset($attr['FAIstate'][0])){
46 if(preg_match("/removed$/",$attr['FAIstate'][0])){
47 if(isset($res[$name])){
48 unset($res[$name]);
49 }
50 continue;
51 }
52 }
55 /* In detailed mode are some additonal informations visible */
56 if($detailed){
58 /* Create list of parents */
59 if(isset($res[$name])){
60 $buffer = $res[$name];
61 $buffer['parents'][] = $res[$name]['dn'];
62 }else{
63 $buffer['parents'] = array();
64 }
66 /* Append objectClass to resulsts */
67 $buffer['objectClass'] = $attr['objectClass'];
68 unset($buffer['objectClass'][0]);
69 }
71 /* Add this object to our list */
72 $buffer['dn'] = $attr['dn'];
73 $res[$name] = $buffer;
74 }
75 }
76 }
77 return($res);
78 }
82 /* Return the object defining ObjectClass e.g. FAIscriptEntry */
83 function get_FAI_type($attr)
84 {
85 $arr = array( "FAIprofile","FAIpartitionTable", "FAIpartitionDisk","FAIpartitionEntry","FAIhook","FAIhookEntry",
86 "FAIscriptEntry","FAIscript","FAIvariable","FAIvariableEntry","FAIpackageList","FAItemplate",
87 "FAItemplateEntry","FAIdebconfInfo","FAIrepository","FAIrepositoryServer","FAIbranch","FAIreleaseTag");
88 foreach($arr as $name){
89 if(in_array($name,$attr['objectClass'])){
90 preg_match("");
91 return($name);
92 }
93 }
94 if(DEBUG_FAI_FUNC) { echo "Not found"; }
95 return("");
96 }
99 /* Return all relevant FAI departments */
100 function get_FAI_departments($suffix = "")
101 {
102 $arr = array("hooks","scripts","disk","packages","profiles","templates","variables");
103 $tmp = array();
104 if(preg_match("/^,/",$suffix)){
105 $suffix = preg_replace("/^,/","",$suffix);
106 }
107 foreach($arr as $name){
108 if(empty($suffix)){
109 $tmp[$name] = "ou=".$name;
110 }else{
111 $tmp[$name] = "ou=".$name.",".$suffix;
112 }
113 }
114 return($tmp);
115 }
118 /* Return all releases within the given base */
119 function get_all_releases_from_base($dn,$appendedName=false)
120 {
121 global $config;
123 if(!preg_match("/ou=fai,ou=configs,ou=systems,/",$dn)){
124 $base = "ou=fai,ou=configs,ou=systems,".$dn;
125 }else{
126 $base = $dn;
127 }
128 $res = array();
130 $ldap = $config->get_ldap_link();
131 $ldap->cd($base);
132 $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
133 while($attrs = $ldap->fetch()){
134 if($appendedName){
135 $res[$attrs['dn']] = convert_department_dn(preg_replace("/,ou=fai,ou=configs,ou=system.*$/","",$attrs['dn']));
136 }else{
137 $res[$attrs['dn']] = $attrs['ou'][0];
138 }
139 }
140 return($res);
141 }
144 /* Add this object to list of objects, that must be checked for release saving */
145 function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
146 {
147 /* Get ldap object */
148 global $config;
149 $addObj['Current_DN'] = $Current_DN;
150 $addObj['objectAttrs']= $objectAttrs;
151 $addObj['removed'] = $removed;
152 $addObj['diff'] = TRUE;
154 if(!$removed){
155 $ldap = $config->get_ldap_link();
156 $ldap->cd($config->current['BASE']);
158 /* Get some basic informations */
159 $parent_obj = get_parent_release_object($Current_DN);
160 if(!empty($parent_obj)){
161 $ldap->cat($parent_obj,array("*"));
162 $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
164 if(!array_diff_FAI( $attrs,$objectAttrs)){
165 $addObj['diff'] = FALSE;
166 }
167 }
168 }
170 $_SESSION['FAI_objects_to_save'][$Current_DN] = $addObj;
171 }
174 /* Detect differences in attribute arrays */
175 function array_diff_FAI($ar1,$ar2)
176 {
177 foreach($ar1 as $key1 => $val1){
179 if((is_array($val1)) && (count($val1)==1)){
180 $ar1[$key1] = $val1[0];
181 }
183 if(!isset($ar2[$key1])){
184 return(true);
185 }
187 if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
188 $val1 = $val1[0];
189 $ar2[$key1] = $ar2[$key1][0];
190 }
191 }
192 if(count( array_diff($ar1,$ar2))){
193 return(true);
194 }else{
195 return(false);
196 }
197 }
200 /* check which objects must be saved, and save them */
201 function save_release_changes_now()
202 {
203 /* Variable init*/
204 $to_save = array();
206 /* check which objects must be saved */
207 foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
208 if($object['diff']){
209 $sub_name = $Current_DN;
210 while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
211 $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name];
212 unset($_SESSION['FAI_objects_to_save'][$sub_name]);
213 $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
214 }
215 }
216 }
218 /* Sort list of objects that must be saved, and ensure that
219 container objects are safed, before their childs are saved */
220 ksort($to_save);
221 $tmp = array();
222 foreach($to_save as $SubObjects){
223 foreach($SubObjects as $object){
224 $tmp[] = $object;
225 }
226 }
227 $to_save = $tmp;
229 /* Save objects and manage the correct release behavior*/
230 foreach($to_save as $save){
232 $Current_DN = $save['Current_DN'];
233 $removed = $save['removed'];
234 $objectAttrs= $save['objectAttrs'];
236 /* Get ldap object */
237 global $config;
238 $ldap = $config->get_ldap_link();
239 $ldap->cd($config->current['BASE']);
241 /* Get some basic informations */
242 $base_release = get_release_dn($Current_DN);
243 $sub_releases = get_sub_releases_of_this_release($base_release,true);
244 $parent_obj = get_parent_release_object($Current_DN);
245 $following_releases = get_sub_releases_of_this_release($base_release,true);
247 /* Check if given dn exists or if is a new entry */
248 $ldap->cat($Current_DN);
249 if(!$ldap->count()){
250 $is_new = true;
251 }else{
252 $is_new = false;
253 }
255 /* if parameter removed is true, we have to add FAIstate to the current attrs
256 FAIstate should end with ...|removed after this operation */
257 if($removed ){
258 $ldap->cat($Current_DN);
260 /* Get current object, because we must add the FAIstate ...|removed */
261 if((!$ldap->count()) && !empty($parent_obj)){
262 $ldap->cat($parent_obj);
263 }
265 /* Check if we have found a suiteable object */
266 if(!$ldap->count()){
267 echo "Error can't remove this object ".$Current_DN;
268 return;
269 }else{
271 /* Set FAIstate to current objectAttrs */
272 $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
273 if(isset($objectAttrs['FAIstate'][0])){
274 if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
275 $objectAttrs['FAIstate'][0] .= "|removed";
276 }
277 }else{
278 $objectAttrs['FAIstate'][0] = "|removed";
279 }
280 }
281 }
283 /* Check if this a leaf release or not */
284 if(count($following_releases) == 0 ){
286 /* This is a leaf object. It isn't unherited by any other object */
287 if(DEBUG_FAI_FUNC) {
288 echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
289 print_a($objectAttrs);
290 }
291 save_FAI_object($Current_DN,$objectAttrs);
292 }else{
294 /* This object is inherited by some sub releases */
296 /* Get all releases, that inherit this object */
297 $r = get_following_releases_that_inherit_this_object($Current_DN);
299 /* Get parent object */
300 $ldap->cat($parent_obj);
301 $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
303 /* New objects require special handling */
304 if($is_new){
306 /* check if there is already an entry named like this,
307 in one of our parent releases */
308 if(!empty($parent_obj)){
309 if(DEBUG_FAI_FUNC) {
310 echo "There is already an entry named like this.</b><br>";
312 echo "<b>Saving main object</b>".$Current_DN;
313 print_a($objectAttrs);
314 }
315 save_FAI_object($Current_DN,$objectAttrs);
317 foreach($r as $key){
318 if(DEBUG_FAI_FUNC) {
319 echo "<b>Saving parent to following release</b> ".$key;
320 print_a($parent_attrs);
321 }
322 save_FAI_object($key,$parent_attrs);
323 }
324 }else{
326 if(DEBUG_FAI_FUNC) {
327 echo "<b>Saving main object</b>".$Current_DN;
328 print_a($objectAttrs);
329 }
330 save_FAI_object($Current_DN,$objectAttrs);
332 if(isset($objectAttrs['FAIstate'])){
333 $objectAttrs['FAIstate'] .= "|removed";
334 }else{
335 $objectAttrs['FAIstate'] = "|removed";
336 }
338 foreach($r as $key ){
339 if(DEBUG_FAI_FUNC) {
340 echo "<b>Create an empty placeholder in follwing release</b> ".$key;
341 print_a($objectAttrs);
342 }
343 save_FAI_object($key,$objectAttrs);
344 }
345 }
346 }else{
348 /* check if we must patch the follwing release */
349 if(!empty($r)){
350 foreach($r as $key ){
351 if(DEBUG_FAI_FUNC) {
352 echo "<b>Copy current objects original attributes to next release</b> ".$key;
353 print_a($parent_attrs);
354 }
355 save_FAI_object($key,$parent_attrs);
356 }
357 }
359 if(DEBUG_FAI_FUNC) {
360 echo "<b>Saving current object</b>".$parent_obj;
361 print_a($objectAttrs);
362 }
363 save_FAI_object($parent_obj,$objectAttrs);
365 if(($parent_obj != $Current_DN)){
366 print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
367 }
368 }
369 }
370 }
371 $_SESSION['FAI_objects_to_save'] = array();
372 }
375 /* this function will remove all unused (deleted) objects,
376 that have no parent object */
377 function clean_up_releases($Current_DN)
378 {
379 global $config;
380 $ldap = $config->get_ldap_link();
381 $ldap->cd($config->current['BASE']);
383 /* Collect some basic informations and initialize some variables */
384 $base_release = get_release_dn($Current_DN);
385 $previous_releases = array_reverse(get_previous_releases_of_this_release($base_release,true));
386 $Kill = array();
387 $Skip = array();
389 /* We must also include the given release dn */
390 $previous_releases[] = $base_release;
392 /* Walk through all releases */
393 foreach($previous_releases as $release){
395 /* Get fai departments */
396 $deps_to_search = get_FAI_departments($release);
398 /* For every single department (ou=hoos,ou ..) */
399 foreach($deps_to_search as $fai_base){
401 /* Ldap search for fai classes specified in this release */
402 $ldap->cd($fai_base);
403 $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
405 /* check the returned objects, and add/replace them in our return variable */
406 while($attr = $ldap->fetch()){
408 $buffer = array();
409 $name = str_ireplace($release,"",$attr['dn']);
411 if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
413 /* Check if this object is required somehow */
414 if(!isset($Skip[$name])){
415 $Kill[$attr['dn']] = $attr['dn'];
416 }
417 }else{
419 /* This object is required (not removed), so do not
420 delete any following sub releases of this object */
421 $Skip[$name] = $attr['dn'];
422 }
423 }
424 }
425 }
426 return($Kill);
427 }
430 /* Remove numeric index and 'count' from ldap->fetch result */
431 function prepare_ldap_fetch_to_be_saved($attrs)
432 {
433 foreach($attrs as $key => $value){
434 if(is_numeric($key) || ($key == "count") || ($key == "dn")){
435 unset($attrs[$key]);
436 }
437 if(is_array($value) && isset($value['count'])){
438 unset($attrs[$key]['count']);
439 }
440 }
441 return($attrs);
442 }
445 /* Save given attrs to specified dn*/
446 function save_FAI_object($dn,$attrs)
447 {
448 global $config;
449 $ldap = $config->get_ldap_link();
450 $ldap->cd($config->current['BASE']);
451 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
452 $ldap->cd($dn);
454 $ldap->cat($dn,array("dn"));
455 if($ldap->count()){
457 /* Remove FAIstate*/
458 if(!isset($attrs['FAIstate'])){
459 $attrs['FAIstate'] = array();
460 }
462 $ldap->modify($attrs);
463 }else{
465 /* Unset description if empty */
466 if(empty($attrs['description'])){
467 unset($attrs['description']);
468 }
470 $ldap->add($attrs);
471 }
472 show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
473 }
476 /* Return FAIstate freeze branch or "" for specified release department */
477 function get_release_tag($dn)
478 {
479 global $config;
480 $ldap = $config->get_ldap_link();
481 $ldap->cd($dn);
482 $ldap->cat($dn,array("FAIstate"));
484 if($ldap->count()){
486 $attr = $ldap->fetch();
487 if(isset($attr['FAIstate'][0])){
488 if(preg_match("/freeze/",$attr['FAIstate'][0])){
489 return("freeze");
490 }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
491 return("branch");
492 }
493 }
494 }
495 return("");
496 }
499 function get_following_releases_that_inherit_this_object($dn)
500 {
501 global $config;
502 $ldap = $config->get_ldap_link();
503 $ldap->cd($config->current['BASE']);
505 $ret = array();
507 /* Get base release */
508 $base_release = get_release_dn($dn);
510 /* Get previous release dns */
511 $sub_releases = get_sub_releases_of_this_release($base_release);
513 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
514 $dn_suffix = str_ireplace($base_release,"",$dn);
516 /* Check if given object also exists whitin one of these releases */
517 foreach($sub_releases as $p_release => $name){
519 $check_dn = $dn_suffix.$p_release;
521 $ldap->cat($check_dn,array("dn","objectClass"));
523 if($ldap->count()){
524 //return($ret);
525 }else{
526 $ret[$check_dn]=$check_dn;
527 }
528 }
529 return($ret);
530 }
533 /* Get previous version of the object dn */
534 function get_parent_release_object($dn,$include_myself=true)
535 {
536 global $config;
537 $ldap = $config->get_ldap_link();
538 $ldap->cd($config->current['BASE']);
539 $previous_releases= array();
541 /* Get base release */
542 $base_release = get_release_dn($dn);
543 if($include_myself){
544 $previous_releases[] = $base_release;
545 }
547 /* Get previous release dns */
548 $tmp = get_previous_releases_of_this_release($base_release,true);
549 foreach($tmp as $release){
550 $previous_releases[] = $release;
551 }
553 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
554 $dn_suffix = str_ireplace($base_release,"",$dn);
556 /* Check if given object also exists whitin one of these releases */
557 foreach($previous_releases as $p_release){
558 $check_dn = $dn_suffix.$p_release;
559 $ldap->cat($check_dn,array("dn","objectClass"));
561 if($ldap->count()){
562 return($check_dn);
563 }
564 }
565 return("");
566 }
569 /* return release names of all parent releases */
570 function get_previous_releases_of_this_release($dn,$flat)
571 {
572 global $config;
573 $ldap = $config->get_ldap_link();
574 $ldap->cd($config->current['BASE']);
575 $ret = array();
577 /* Explode dns into pieces, to be able to build parent dns */
578 $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
579 if(!is_array($dns_to_check)){
580 return;
581 }
583 /* Unset first entry which represents the given dn */
584 unset($dns_to_check['count']);
585 unset($dns_to_check[key($dns_to_check)]);
587 /* Create dns addresses and check if this dn is a release dn */
588 $id = 0;
589 while(count($dns_to_check)){
591 /* build parent dn */
592 $new_dn = "";
593 foreach($dns_to_check as $part){
594 $new_dn .= $part.",";
595 }
596 $new_dn .= $config->current['BASE'];
598 /* check if this dn is a release */
599 if(is_release_department($new_dn)){
600 if($flat){
601 $ret[$id] = $new_dn;
602 }else{
603 $ret = array($new_dn=>$ret);
604 }
605 $id ++;
606 }else{
607 return($ret);
608 }
609 reset($dns_to_check);
610 unset($dns_to_check[key($dns_to_check)]);
611 }
612 return($ret);
613 }
616 /* This function returns all sub release names, recursivly */
617 function get_sub_releases_of_this_release($dn,$flat = false)
618 {
619 global $config;
620 $res = array();
621 $ldap = $config->get_ldap_link();
622 $ldap->cd($config->current['BASE']);
623 $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
624 while($attr = $ldap->fetch()){
626 /* Append department name */
627 if($flat){
628 $res[$attr['dn']] = $attr['ou'][0];
629 }else{
630 $res[$attr['dn']] = array();
631 }
633 /* Get sub release departments of this department */
634 if(in_array("FAIbranch",$attr['objectClass'])) {
635 if($flat){
636 $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
637 foreach($tmp as $dn => $value){
638 $res[$dn]=$value;
639 }
640 }else{
641 $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
642 }
643 }
644 }
645 return($res);
646 }
649 /* Check if the given department is a release department */
650 function is_release_department($dn)
651 {
652 global $config;
653 $ldap = $config->get_ldap_link();
654 $ldap->cd($config->current['BASE']);
655 $ldap->cat($dn,array("objectClass","ou"));
657 /* Check objectClasses and name to check if this is a release department */
658 if($ldap->count()){
659 $attrs = $ldap->fetch();
661 $ou = "";
662 if(isset($attrs['ou'][0])){
663 $ou = $attrs['ou'][0];
664 }
666 if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
667 return($attrs['dn']);
668 }
669 }
670 return(false);
671 }
674 /* This function returns the dn of the object release */
675 function get_release_dn($Current_DN)
676 {
677 global $config;
678 $ldap = $config->get_ldap_link();
679 $ldap->cd($config->current['BASE']);
681 /* Split dn into pices */
682 $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
683 if(!is_array($dns_to_check)){
684 return;
685 }
687 /* Use dn pieces, to create sub dns like
688 ou=test,ou=1,ou=0...
689 ou=1,ou=0...
690 ou=0...
691 To check which dn is our release container.
692 */
693 unset($dns_to_check['count']);
694 while(count($dns_to_check)){
696 /* Create dn */
697 $new_dn = "";
698 foreach($dns_to_check as $part){
699 $new_dn .= $part.",";
700 }
701 $new_dn .= $config->current['BASE'];
703 /* Check if this dn is a release dn */
704 if(is_release_department($new_dn)){
705 return($new_dn);
706 }
708 /* Remove first element of dn pieces */
709 reset($dns_to_check);
710 unset($dns_to_check[key($dns_to_check)]);
711 }
712 return("");
713 }
715 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
716 ?>