Code

a54100c03e90b5125f14a7af7d982b81e3b6b26e
[gosa.git] / include / functions_FAI.inc
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()){
41         
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 = "")
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);
118 /* Return all releases within the given base */
119 function get_all_releases_from_base($dn,$appendedName=false)
121   global $config;
122   $base = "ou=fai,ou=configs,ou=systems,".$dn;
123   $res = array();  
124   
125   $ldap = $config->get_ldap_link();
126   $ldap->cd($base);
127   $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
128   while($attrs = $ldap->fetch()){
129     if($appendedName){
130       $res[$attrs['dn']] = convert_department_dn(preg_replace("/,ou=fai,ou=configs,ou=system.*$/","",$attrs['dn']));
131     }else{
132       $res[$attrs['dn']] = $attrs['ou'][0];
133     }
134   }
135   return($res);
139 /* Add this object to list of objects, that must be checked for release saving */
140 function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
142   /* Get ldap object */  
143   global $config;
144   $addObj['Current_DN'] = $Current_DN;
145   $addObj['objectAttrs']= $objectAttrs;
146   $addObj['removed']    = $removed;
147   $addObj['diff']       = TRUE;
149   if(!$removed){
150     $ldap = $config->get_ldap_link();
151     $ldap->cd($config->current['BASE']);
153     /* Get some basic informations */
154     $parent_obj   = get_parent_release_object($Current_DN);
155     if(!empty($parent_obj)){
156       $ldap->cat($parent_obj,array("*"));
157       $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
159       if(!array_diff_FAI( $attrs,$objectAttrs)){
160         $addObj['diff'] = FALSE;
161       }
162     } 
163   }
164    
165   $_SESSION['FAI_objects_to_save'][$Current_DN] =  $addObj;
169 /* Detect differences in attribute arrays  */
170 function array_diff_FAI($ar1,$ar2)
172   foreach($ar1 as $key1 => $val1){
174     if((is_array($val1)) && (count($val1)==1)){
175       $ar1[$key1] = $val1[0];
176     }
178     if(!isset($ar2[$key1])){
179       return(true);
180     }
182     if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
183       $val1 = $val1[0];
184       $ar2[$key1] = $ar2[$key1][0];
185     }
186   }
187   if(count( array_diff($ar1,$ar2))){
188     return(true);
189   }else{
190     return(false);
191   }
195 /* check which objects must be saved, and save them */
196 function save_release_changes_now()
198   /* Variable init*/
199   $to_save = array();
201   /* check which objects must be saved */
202   foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
203     if($object['diff']){
204       $sub_name = $Current_DN;
205       while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
206         $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name]; 
207         unset($_SESSION['FAI_objects_to_save'][$sub_name]);
208         $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
209       }
210     }
211   }
213   /* Sort list of objects that must be saved, and ensure that 
214       container   objects are safed, before their childs are saved */
215   ksort($to_save);
216   $tmp = array();
217   foreach($to_save as $SubObjects){
218     foreach($SubObjects as $object){
219       $tmp[] = $object;
220     }
221   }
222   $to_save = $tmp;
224   /* Save objects and manage the correct release behavior*/
225   foreach($to_save as $save){
227     $Current_DN = $save['Current_DN'];
228     $removed    = $save['removed'];
229     $objectAttrs= $save['objectAttrs'];
231     /* Get ldap object */ 
232     global $config;
233     $ldap = $config->get_ldap_link();
234     $ldap->cd($config->current['BASE']);
236     /* Get some basic informations */
237     $base_release       = get_release_dn($Current_DN);
238     $sub_releases       = get_sub_releases_of_this_release($base_release,true);
239     $parent_obj         = get_parent_release_object($Current_DN);
240     $following_releases = get_sub_releases_of_this_release($base_release,true);
241     
242     /* Check if given dn exists or if is a new entry */
243     $ldap->cat($Current_DN);
244     if(!$ldap->count()){
245       $is_new = true;
246     }else{
247       $is_new = false;
248     }
249    
250     /* if parameter removed is true, we have to add FAIstate to the current attrs 
251           FAIstate should end with ...|removed after this operation */  
252     if($removed ){
253       $ldap->cat($Current_DN);
255       /* Get current object, because we must add the FAIstate ...|removed */
256       if((!$ldap->count()) && !empty($parent_obj)){
257         $ldap->cat($parent_obj);
258       }
260       /* Check if we have found a suiteable object */ 
261       if(!$ldap->count()){
262         echo "Error can't remove this object ".$Current_DN;
263         return;
264       }else{
266         /* Set FAIstate to current objectAttrs */
267         $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
268         if(isset($objectAttrs['FAIstate'][0])){
269           if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
270             $objectAttrs['FAIstate'][0] .= "|removed";
271           }
272         }else{
273           $objectAttrs['FAIstate'][0] = "|removed";
274         }
275       }
276     }
277    
278     /* Check if this a leaf release or not */ 
279     if(count($following_releases) == 0 ){
281       /* This is a leaf object. It isn't unherited by any other object */    
282       if(DEBUG_FAI_FUNC) { 
283         echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
284         print_a($objectAttrs);
285       }
286       save_FAI_object($Current_DN,$objectAttrs);
287     }else{
289       /* This object is inherited by some sub releases */  
291       /* Get all releases, that inherit this object */ 
292       $r = get_following_releases_that_inherit_this_object($Current_DN);
294       /* Get parent object */
295       $ldap->cat($parent_obj);
296       $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
297         
298       /* New objects require special handling */
299       if($is_new){
301         /* check if there is already an entry named like this,
302             in one of our parent releases */
303         if(!empty($parent_obj)){
304           if(DEBUG_FAI_FUNC) { 
305             echo "There is already an entry named like this.</b><br>";
307             echo "<b>Saving main object</b>".$Current_DN;
308             print_a($objectAttrs);
309           }    
310           save_FAI_object($Current_DN,$objectAttrs);
312           foreach($r as $key){
313             if(DEBUG_FAI_FUNC) { 
314               echo "<b>Saving parent to following release</b> ".$key;
315               print_a($parent_attrs);
316             }
317             save_FAI_object($key,$parent_attrs);
318           }
319         }else{
321           if(DEBUG_FAI_FUNC) { 
322             echo "<b>Saving main object</b>".$Current_DN;
323             print_a($objectAttrs);
324           }
325           save_FAI_object($Current_DN,$objectAttrs);
327           if(isset($objectAttrs['FAIstate'])){
328             $objectAttrs['FAIstate'] .= "|removed"; 
329           }else{
330             $objectAttrs['FAIstate'] = "|removed";
331           }
333           foreach($r as $key ){
334             if(DEBUG_FAI_FUNC) { 
335               echo "<b>Create an empty placeholder in follwing release</b> ".$key; 
336               print_a($objectAttrs);
337             }
338             save_FAI_object($key,$objectAttrs);
339           }
340         }
341       }else{
343         /* check if we must patch the follwing release */
344         if(!empty($r)){
345           foreach($r as $key ){
346             if(DEBUG_FAI_FUNC) { 
347               echo "<b>Copy current objects original attributes to next release</b> ".$key;
348               print_a($parent_attrs);
349             }
350             save_FAI_object($key,$parent_attrs);
351           }
352         }
354         if(DEBUG_FAI_FUNC) { 
355           echo "<b>Saving current object</b>".$parent_obj;
356           print_a($objectAttrs);
357         }
358         save_FAI_object($parent_obj,$objectAttrs);
360         if(($parent_obj != $Current_DN)){
361           print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
362         }
363       }
364     }
365   } 
366   $_SESSION['FAI_objects_to_save'] = array();
370 /* this function will remove all unused (deleted) objects,
371     that have no parent object */
372 function clean_up_releases($Current_DN)
374   global $config;
375   $ldap = $config->get_ldap_link();
376   $ldap->cd($config->current['BASE']);
378   /* Collect some basic informations and initialize some variables */ 
379   $base_release       = get_release_dn($Current_DN);
380   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
381   $Kill = array();
382   $Skip = array();
384   /* We must also include the given release dn */
385   $previous_releases[] = $base_release;
387   /* Walk through all releases */
388   foreach($previous_releases as $release){
390     /* Get fai departments */
391     $deps_to_search = get_FAI_departments($release); 
393     /* For every single department  (ou=hoos,ou ..) */
394     foreach($deps_to_search as $fai_base){
396       /* Ldap search for fai classes specified in this release */
397       $ldap->cd($fai_base);
398       $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
400       /* check the returned objects, and add/replace them in our return variable */
401       while($attr = $ldap->fetch()){
402         
403         $buffer = array();
404         $name = str_ireplace($release,"",$attr['dn']);
406         if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
408           /* Check if this object is required somehow */    
409           if(!isset($Skip[$name])){
410             $Kill[$attr['dn']] = $attr['dn'];
411           }
412         }else{
413       
414           /* This object is required (not removed), so do not 
415               delete any following sub releases of this object */
416           $Skip[$name] = $attr['dn'];
417         }
418       }
419     }
420   }
421   return($Kill);
425 function prepare_ldap_fetch_to_be_saved($attrs)
427   foreach($attrs as $key => $value){
428     if(is_numeric($key) || ($key == "count") || ($key == "dn")){
429       unset($attrs[$key]);
430     }
431     if(is_array($value) && isset($value['count'])){
432       unset($attrs[$key]['count']);
433     }
434   }
435   return($attrs);
439 function save_FAI_object($dn,$attrs)
441   global $config;
442   $ldap = $config->get_ldap_link();
443   $ldap->cd($config->current['BASE']);
444   $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
445   $ldap->cd($dn);
446  
447   $ldap->cat($dn,array("dn"));
448   if($ldap->count()){
450     if(!isset($attrs['FAIstate'])){
451       $attrs['FAIstate'] = array();
452     }
454     $ldap->modify($attrs);
455   }else{
456     $ldap->add($attrs);
457   }
458   show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
462 function get_release_tag($dn)
464   global $config;
465   $ldap = $config->get_ldap_link();
466   $ldap->cd($dn);
467   $ldap->cat($dn,array("FAIstate"));
469   if($ldap->count()){
470   
471     $attr = $ldap->fetch();
472     if(isset($attr['FAIstate'][0])){
473       if(preg_match("/freeze/",$attr['FAIstate'][0])){
474         return("freeze");
475       }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
476         return("branch");
477       }
478     }
479   }
480   return("");
484 function get_following_releases_that_inherit_this_object($dn)
486   global $config;
487   $ldap = $config->get_ldap_link();
488   $ldap->cd($config->current['BASE']);
490   $ret = array();
492   /* Get base release */
493   $base_release = get_release_dn($dn);
495   /* Get previous release dns */
496   $sub_releases = get_sub_releases_of_this_release($base_release);
498   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
499   $dn_suffix = str_ireplace($base_release,"",$dn);
501   /* Check if given object also exists whitin one of these releases */
502   foreach($sub_releases as $p_release => $name){
504     $check_dn = $dn_suffix.$p_release;
505   
506     $ldap->cat($check_dn,array("dn","objectClass"));
507     
508     if($ldap->count()){
509       //return($ret);
510     }else{
511       $ret[$check_dn]=$check_dn;
512     }
513   }
514   return($ret);
518 /* Get previous version of the object dn */
519 function get_parent_release_object($dn,$include_myself=true)
521   global $config;
522   $ldap = $config->get_ldap_link();
523   $ldap->cd($config->current['BASE']);
524   $previous_releases= array();
526   /* Get base release */
527   $base_release = get_release_dn($dn);
528   if($include_myself){
529     $previous_releases[] = $base_release;  
530   }
532   /* Get previous release dns */
533   $tmp = get_previous_releases_of_this_release($base_release,true);
534   foreach($tmp as $release){
535     $previous_releases[] = $release;
536   }
538   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
539   $dn_suffix = str_ireplace($base_release,"",$dn);
540     
541   /* Check if given object also exists whitin one of these releases */
542   foreach($previous_releases as $p_release){
543     $check_dn = $dn_suffix.$p_release;
544     $ldap->cat($check_dn,array("dn","objectClass"));
545     
546     if($ldap->count()){
547       return($check_dn);
548     }
549   }
550   return("");
554 /* return release names of all parent releases */
555 function get_previous_releases_of_this_release($dn,$flat)
557   global $config;
558   $ldap = $config->get_ldap_link();
559   $ldap->cd($config->current['BASE']);
560   $ret = array();
562   /* Explode dns into pieces, to be able to build parent dns */
563   $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
564   if(!is_array($dns_to_check)){
565     return;  
566   }
568   /* Unset first entry which represents the given dn */
569   unset($dns_to_check['count']); 
570   unset($dns_to_check[key($dns_to_check)]);
572   /* Create dns addresses and check if this dn is a release dn */
573   $id = 0;
574   while(count($dns_to_check)){
576     /* build parent dn */
577     $new_dn = "";
578     foreach($dns_to_check as $part){
579       $new_dn .= $part.",";
580     }
581     $new_dn .= $config->current['BASE'];
583     /* check if this dn is a release */
584     if(is_release_department($new_dn)){
585       if($flat){
586         $ret[$id] = $new_dn; 
587       }else{
588         $ret = array($new_dn=>$ret); 
589       }
590       $id ++;
591     }else{
592       return($ret);
593     }
594     reset($dns_to_check);
595     unset($dns_to_check[key($dns_to_check)]);
596   }
597   return($ret);
598
601 /* This function returns all sub release names, recursivly  */
602 function get_sub_releases_of_this_release($dn,$flat = false)
604   global $config;
605   $res  = array();
606   $ldap = $config->get_ldap_link();
607   $ldap->cd($config->current['BASE']);
608   $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
609   while($attr = $ldap->fetch()){
611     /* Append department name */
612     if($flat){
613       $res[$attr['dn']] = $attr['ou'][0];
614     }else{
615       $res[$attr['dn']] = array();
616     }
618     /* Get sub release departments of this department */
619     if(in_array("FAIbranch",$attr['objectClass'])) {
620       if($flat){
621         $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
622         foreach($tmp as $dn => $value){
623           $res[$dn]=$value;
624         }
625       }else{
626         $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
627       }
628     }
629   }
630   return($res);
634 /* Check if the given department is a release department */
635 function is_release_department($dn)
637   global $config;
638   $ldap = $config->get_ldap_link();
639   $ldap->cd($config->current['BASE']);
640   $ldap->cat($dn,array("objectClass","ou"));
642   /* Check objectClasses and name to check if this is a release department */
643   if($ldap->count()){
644     $attrs = $ldap->fetch();
645                         
646     $ou = "";
647     if(isset($attrs['ou'][0])){
648       $ou = $attrs['ou'][0];    
649     }
650         
651     if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
652       return($attrs['dn']);
653     }
654   }
655   return(false);
659 /* This function returns the dn of the object release */
660 function get_release_dn($Current_DN)
662   global $config;
663   $ldap = $config->get_ldap_link();
664   $ldap->cd($config->current['BASE']);
666   /* Split dn into pices */ 
667   $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
668   if(!is_array($dns_to_check)){
669     return;  
670   }
672   /* Use dn pieces, to create sub dns like 
673       ou=test,ou=1,ou=0...
674               ou=1,ou=0...
675                    ou=0... 
676     To check which dn is our release container.
677   */
678   unset($dns_to_check['count']); 
679   while(count($dns_to_check)){
681     /* Create dn */
682     $new_dn = "";
683     foreach($dns_to_check as $part){
684       $new_dn .= $part.",";
685     }
686     $new_dn .= $config->current['BASE'];
688     /* Check if this dn is a release dn */
689     if(is_release_department($new_dn)){
690       return($new_dn);
691     }
693     /* Remove first element of dn pieces */
694     reset($dns_to_check);
695     unset($dns_to_check[key($dns_to_check)]);
696   }
697   return("");
700 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
701 ?>