Code

- Added ssh plugin
[gosa.git] / plugins / admin / systems / class_servService.inc
1 <?php
3 class servservice extends plugin
4 {
5   var $goExportEntry    = array();
6   var $goExportEntryList= array();
7   var $goTimeSource     = array();
8   var $goLdapBase       = "";
9   var $goXdmcpIsEnabled = "";
10   var $goFontPath       = "";
11   var $goNTPServer      = "";
12   var $goShareServer    = false;
13   var $goLdapServer     = "";
14   var $goTerminalServer = "";
15   var $goSyslogServer   = "";
16   var $goCupsServer     = "";
17   var $goMailServer     = "";
18   var $o_subWindow      = NULL;
20   /* attribute list for save action */
21   var $ignore_account= TRUE;
22   var $attributes       = array("goLdapBase","goXdmcpIsEnabled","goFontPath","goExportEntry","goTimeSource");
23   var $possible_objectclasses= array( "goShareServer", "goNtpServer", "goServer", "GOhard", "goLdapServer",
24                                       "goTerminalServer", "goSyslogServer", "goCupsServer","goMailServer");
25   var $objectclasses    = array( "top","goServer", "GOhard"); 
26   var $additionaloc     = array( "goShareServer"     => array("goExportEntry"),
27                                  "goNtpServer"     => array("goTimeSource"),
28                                  "goLdapServer"    => array("goLdapBase"),
29                                  "goTerminalServer"=> array("goXdmcpIsEnabled", "goFontPath"),
30                                  "goSyslogServer"  => array(),
31                                  "goMailServer"    => array(),
32                                  "goCupsServer"    => array());
34   var $allow_mounts = false; //do we allow mount entries?
35   var $mounts_to_add = array();
36   var $mounts_to_remove = array();
37   var $oldone = NULL; //temp dave for old mount entry
39   function servservice ($config, $dn= NULL, $parent= NULL)
40   {
41     
42     plugin::plugin ($config, $dn, $parent);
43     
44     $ldap = $this->config->get_ldap_link();
45     $avl_objectclasses = $ldap->get_objectclasses();
46     if (isset($avl_objectclasses["mount"])) {
47         $this->allow_mounts = true;
48     }
49     
50     /* Assemble final object class list */
51     foreach ($this->additionaloc as $oc => $dummy){
52       if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
53         $this->objectclasses[$oc]= $oc;
54       }
55     }
57     /* Load arrays */
58     foreach (array("goTimeSource") as $name){
59       $tmp= array();
60       if (isset($this->attrs[$name])){
61         for ($i= 0; $i<$this->attrs[$name]['count']; $i++){
62           $tmp[$this->attrs[$name][$i]]= $this->attrs[$name][$i];
63         }
64       }
65       $this->$name= $tmp;
66     }
68     $tmp =array();
69     $tmp2=array();
70     if(isset($this->attrs['goExportEntry'])){
71       if(isset($this->attrs['goExportEntry']['count'])){
72         for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
73           $entry= $this->attrs['goExportEntry'][$i];
74           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
75         }
76       }
77     }
78     $this->goExportEntryList = $tmp;
80     /* Always is account... */
81     $this->is_account= TRUE;
83     /* Check if goShareServer is defined */
84     if((isset($this->attrs['objectClass']))&&(is_array($this->attrs['objectClass']))){ 
85       if(in_array("goShareServer",$this->attrs['objectClass'])){
86         $this->goShareServer = true;    
87       }
88     }
89   }
91   function addToList($entry){
92     $key =  key($entry);
93     $this->goExportEntryList[$key]=$entry[$key];
94   }
96   function deleteFromList($id){
97     /* Check if the share is used by someone */
98     $ldap = $this->config->get_ldap_link();
99     $ldap->cd($this->config->current['BASE']);
100     $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*))", array("cn"));
101     $cnt= $ldap->count();
102     if ($cnt){
103       $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
104       $msg.= "<br><br><ul>";
105       while ($attrs= $ldap->fetch()){
106         $msg.= "<li>".$attrs["cn"][0]."</li>";
107       }
108       $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
109       print_red($msg);
110       
111     } else {
112       /* Finally remove it */
113       unset($this->goExportEntryList[$id]);
114     }
115   }
116   
117   function addToMountList($entry) {
118     $key =  key($entry);
119     $type = $this->get_share_type($entry[$key]);
120     if (($type == "netatalk") || ($type == "NFS")) {
121         $this->mounts_to_add[$entry[$key]] = $entry[$key];
122       unset($this->mounts_to_remove[$entry[$key]]);
123     }
124   }
125   
126   function deleteFromMountList($entry) {
127     $type = $this->get_share_type($entry);
128     if (($type == "netatalk") || ($type == "NFS")) {
129       $this->mounts_to_remove[$entry] = $entry;
130       unset($this->mounts_to_add[$entry]);
131     }
132   }
134   function execute()
135   {
136     /* Call parent execute */
137     plugin::execute();
139     /* Fill templating stuff */
140     $smarty= get_smarty();
141     $smarty->assign("staticAddress", "");
143     if((isset($_POST['DelNfsEnt'])) && (isset($_POST['goExportEntryList'])) && chkacl($this->acl,"goExportEntry") == ""){
144       if($this->allow_mounts){
145         foreach($_POST['goExportEntryList'] as $entry){
146           $this->deleteFromMountList($this->goExportEntryList[$entry]);
147         }
148       }
149       foreach($_POST['goExportEntryList'] as $entry){
150         $this->deleteFromList($entry);
151       }
152     }
154     if(isset($_POST['NewNfsAdd']) && chkacl($this->acl,"goExportEntry") == ""){
155       $this->oldone = NULL;
156       $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
157       $this->dialog = true;
158     }
160     if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList'])) && chkacl($this->acl,"goExportEntry") == ""){
161       $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
162       $add_mount=isset($this->mounts_to_add[$entry]);
163       $this->oldone=$entry;
164       $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
165       $this->dialog = true;
166     }
168     if(isset($this->o_subWindow)){
169     $this->o_subWindow->save_object(TRUE);
170     }
172     /* Save NFS setup */
173     if(isset($_POST['NFSsave'])){
174       if(count($this->o_subWindow->check())>0){
175         foreach($this->o_subWindow->check() as $msg) {
176           print_red($msg);
177         }
178       }else{
179         $this->o_subWindow->save_object();
180         $newone = $this->o_subWindow->save();
181         $this->addToList($newone);
182         if($this->allow_mounts){
183           if($this->oldone) {
184               $this->deleteFromMountList($this->oldone);
185           }
186           if ($this->o_subWindow->should_create_mount()) {
187               $this->addToMountList($newone);
188           }
189         }
190         unset($this->o_subWindow);
191         $this->dialog = false;
192       }
193     }
194     
195     /* Cancel NFS setup */
196     if(isset($_POST['NFScancel'])){
197       $this->oldone = NULL;
198       unset($this->o_subWindow);
199       $this->dialog = false;
200     }
201   
202     /* Execute NFS setup dialog*/
203     if(isset($this->o_subWindow)){
204       return $this->o_subWindow->execute(); 
205     }
207     /* Here we add a new entry  */
208     if(isset($_POST['NewNTPAdd']) && $_POST['NewNTPExport'] != "" && chkacl($this->acl,"goNtpServer") == "") {
209       $this->goTimeSource[$_POST['NewNTPExport']]= $_POST['NewNTPExport'];
210       asort($this->goTimeSource);
211     }
213     /* Deleting an Entry, is a bit more complicated than adding one*/
214     if(isset($_POST['DelNTPEnt']) && chkacl($this->acl,"goNtpServer") == "") {
215       foreach ($_POST['goTimeSource'] as $entry){
216         if (isset($this->goTimeSource[$entry])){
217           unset($this->goTimeSource[$entry]);
218         }
219       }
220     }
222     /* Attributes */
223     foreach ($this->attributes as $attr){
224       $smarty->assign("$attr", $this->$attr);
225       $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
226       $smarty->assign($attr."State","");
227     }
228     
229     $tellSmarty=array();
230     ksort($this->goExportEntryList);
231     foreach($this->goExportEntryList as $name=>$values){
232        $tmp = split("\|",$values);
233        $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
234     }
235     $smarty->assign("goExportEntry",array_keys($tellSmarty));
236     $smarty->assign("goExportEntryKeys",($tellSmarty));
237     $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
239     $smarty->assign("goTimeSource", $this->goTimeSource);
240     $smarty->assign("goTimeSourceACL", chkacl($this->acl, "goTimeSource"));
241     $smarty->assign("goTimeSourceState","");
242     
244     /* Classes... */
245     foreach ($this->additionaloc as $oc => $dummy){
246       if (isset($this->objectclasses[$oc])){
247         $smarty->assign("$oc", "checked");
248         $smarty->assign("$oc"."State", "");
249         $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
251       } else {
252         $smarty->assign("$oc", "");
253         $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
254         $smarty->assign("$oc"."State", "disabled");
255       }
256     }
258     if(!$this->goShareServer){
259       $smarty->assign("goShareServerState", " disabled ");
260       $smarty->assign("goExportEntryACL", " disabled ");
261     }else{
262       $smarty->assign("goShareServerState", "  ");
263       $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
264     }
266     /* Different handling for checkbox */
267     if($this->goXdmcpIsEnabled == "true"){
268       $smarty->assign("goXdmcpIsEnabled","checked");
269     } else {
270       $smarty->assign("goXdmcpIsEnabled","");
271     }
273     return($smarty->fetch (get_template_path('servservice.tpl', TRUE)));
274   }
277   function remove_from_parent()
278   {
279     /* This cannot be removed... */
280   }
283   /* Save data to object */
284   function save_object()
285   {
286     plugin::save_object();
287     if (isset($_POST['servicetab'])){
288       $tmp = $this->goTimeSource;
289   
290       if(isset($_POST['goLdapBase']) && chkacl($this->acl,"goLdapBase") == ""){
291         $this->goLdapBase = $_POST['goLdapBase'];
292       }
293   
294       /* Save checkbox state */
295       foreach ($this->additionaloc as $oc => $dummy){
296 //        if($oc == "goNtpServer") continue;
297         if (chkacl($this->acl, $oc) == ""){
298           if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
299             $this->objectclasses[$oc]= $oc;
300           } else {
301             unset($this->objectclasses[$oc]);
302           }
303         }
304       }
306       /* Save xdmcp is enabled flag */
307       if(chkacl($this->acl,"goXdmcpIsEnabled") == ""){
308         if (isset($_POST['goXdmcpIsEnabled'])){
309           $this->goXdmcpIsEnabled= "true";
310         } else {
311           $this->goXdmcpIsEnabled= "false";
312         }
313       }
314         
315       /* Save xdmcp is enabled flag */
316       if(chkacl($this->acl,"goExportEntry") == ""){
317         if (isset($_POST['goShareServer'])){
318           $this->goShareServer = true;
319         } else {
320           $this->goShareServer = false;
321         }
322       }
323       $this->goTimeSource = array();
324       $this->goTimeSource = $tmp;
325     }
326   }
329   /* Check supplied data */
330   function check()
331   {
332     /* Call common method to give check the hook */
333     $message= plugin::check();
334   
335     if((isset($_POST['goTerminalServer']))&&(empty($this->goFontPath))){
336       $message[]=_("Terminal server, must have fontpath specified.");
337     }
339     return ($message);
340   }
343   /* Save to LDAP */
344   function save()
345   {
346     plugin::save();
348     $tmp= array();
350     /* Remove all from this plugin */
351     foreach($this->attrs['objectClass'] as $oc){
352       if (!in_array_ics($oc, $this->possible_objectclasses)){
353         $tmp[]= $oc;
354       }
355     }
356     
357     /* Merge our current objectclasses */
358     foreach($this->objectclasses as $oc){
359       if (!in_array_ics($oc, $tmp)){
360         $tmp[]= $oc;
361       }
362     }
364     /* Reassign cleaned value */
365     $this->attrs['objectClass']= $tmp;
367     /* Arrays */
368     foreach (array("goTimeSource"=>"goTimeSource", "goExportEntryList"=>"goExportEntry") as $source => $destination){
369       $this->attrs[$destination]= array();
370       foreach ($this->$source as $element){
371         $this->attrs[$destination][]= $element;
372       }
373     }
374     
375     /* Process netatalk mounts */
376     if($this->allow_mounts) {
377       $this->process_mounts();
378     }
379  
380     /* Remove illegal attributes */
381     foreach ($this->additionaloc as $oc => $attrs){
382       if (!in_array($oc, $this->objectclasses)){
383         foreach ($attrs as $attr){
384           $this->attrs[$attr]= array();
385         }
386       }
387     }
389     /* Write to LDAP */
390     $ldap= $this->config->get_ldap_link();
391     $ldap->cd($this->dn);
392     $this->cleanup();
393     $ldap->modify ($this->attrs); 
395     show_ldap_error($ldap->get_error(), _("Saving server service object failed"));
396     
397     /* Optionally execute a command after we're done */
398     if ($this->initially_was_account == $this->is_account){
399       if ($this->is_modified){
400         $this->handle_post_events("modify");
401       }
402     } else {
403       $this->handle_post_events("add");
404     }
405   }
406   
407   function process_mounts() {
408     
409     $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
410     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
411     
412     $mounts = array(
413       "objectClass" => "container",
414       "cn" => "mounts"
415     );
416     
417     # load data from mounts container
418     $ldap = $this->config->get_ldap_link();
419     $ldap->cat($mountsdn, array('dn'));
420     $attrs = $ldap->fetch();
421     
422     # mounts container not present yet, so we create it
423     if (count($attrs) == 0) {
424         $ldap->cd($mountsdn);
425         $ldap->add($mounts);
426         show_ldap_error($ldap->get_error(), _("Creating mount container failed"));
427         gosa_log("Mount container '$mountsdn' has been created");
428     }
430     # remove deleted mounts from the container
431     foreach ($this->mounts_to_remove as $entry) {
432       $mount=$this->returnMountEntry($entry);
433       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
435       $ldap->cat($mountdn, array('dn'));
436       $attrs = $ldap->fetch();
438       if (count($attrs) != 0) {
439         $ldap->rmdir($mountdn);
440         show_ldap_error($ldap->get_error(), _("Removing mount container failed"));
441         gosa_log("Mount object '".$mountdn."' has been removed");
442       }
443     }
445     # add new mounts to the container
446     foreach ($this->mounts_to_add as $entry) {
448       $mount=$this->returnMountEntry($entry);
449       $mountdn = "cn=".$mount["cn"].","."$mountsdn";  
450       $ldap->cd($mountdn);
451       $ldap->add($mount);
452       show_ldap_error($ldap->get_error(), _("Saving mount container failed"));
453       gosa_log("Mount object '".$mountdn."' has been added");
454     }
455   }
456   
457   function get_share_type($share) {
458     $tmp = split("\|", $share);
459     return $tmp[2];
460   }
463   function returnMountEntry($entry) {
464   $item = split("\|", $entry);
465     $name = $item[0];
466     $description = $item[1];
467     $type = $item[2];
468     $charset = $item[3];
469     $path = $item[4];
470     $options = $item[5];
471     
472     switch ($type) {
473       case "netatalk" : {
474             $mount = array(
475             "mountDirectory" => "/Network/Servers/",
476             "mountOption" => array(
477               "net",
478               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
479             ),
480             "mountType" => "url",
481             "objectClass" => "mount",
482               "cn" => $this->cn .":/".$name
483           );
484           break;
485       }
486       case "NFS" : {
487         $mount = array(
488             "mountDirectory" => "/Network/Servers/",
489             "mountOption" => "net",
490             "mountType" => "nfs",
491             "objectClass" => "mount",
492               "cn" => $this->cn .":".$path
493           );
494           break;
495       }        
496       default : {
497         continue;
498       }
499     }
500   return $mount;
501   }
504 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
505 ?>