1 <?php
3 class servservice extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 var $goExportEntry = array();
11 var $goExportEntryList= array();
12 var $goTimeSource = array();
13 var $goLdapBase = "";
14 var $goXdmcpIsEnabled = "";
15 var $goFontPath = "";
16 var $goNTPServer = "";
17 var $goShareServer = false;
18 var $goLdapServer = "";
19 var $goTerminalServer = "";
20 var $goSyslogServer = "";
21 var $goCupsServer = "";
22 var $goMailServer = "";
23 var $o_subWindow = NULL;
25 /* attribute list for save action */
26 var $ignore_account= TRUE;
27 var $attributes = array("goLdapBase","goXdmcpIsEnabled","goFontPath","goExportEntry","goTimeSource");
28 var $possible_objectclasses= array( "goShareServer", "goNtpServer", "goServer", "GOhard", "goLdapServer",
29 "goTerminalServer", "goSyslogServer", "goCupsServer","goMailServer");
30 var $objectclasses = array( "top","goServer", "GOhard");
31 var $additionaloc = array( "goShareServer" => array("goExportEntry"),
32 "goNtpServer" => array("goTimeSource"),
33 "goLdapServer" => array("goLdapBase"),
34 "goTerminalServer"=> array("goXdmcpIsEnabled", "goFontPath"),
35 "goSyslogServer" => array(),
36 "goMailServer" => array(),
37 "goCupsServer" => array());
39 var $allow_mounts = false; //do we allow mount entries?
40 var $mounts_to_add = array();
41 var $mounts_to_remove = array();
42 var $oldone = NULL; //temp dave for old mount entry
44 function servservice ($config, $dn= NULL)
45 {
47 plugin::plugin ($config, $dn);
49 /* Check if mount objectClass is available */
50 $ldap = $this->config->get_ldap_link();
51 $avl_objectclasses = $ldap->get_objectclasses();
52 if (isset($avl_objectclasses["mount"])) {
53 $this->allow_mounts = true;
54 }
56 /* Assemble final object class list */
57 foreach ($this->additionaloc as $oc => $dummy){
58 if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
59 $this->objectclasses[$oc]= $oc;
60 }
61 }
63 /* Load arrays */
64 foreach (array("goTimeSource") as $name){
65 $tmp= array();
66 if (isset($this->attrs[$name])){
67 for ($i= 0; $i<$this->attrs[$name]['count']; $i++){
68 $tmp[$this->attrs[$name][$i]]= $this->attrs[$name][$i];
69 }
70 }
71 $this->$name= $tmp;
72 }
74 $tmp =array();
75 $tmp2=array();
76 if(isset($this->attrs['goExportEntry'])){
77 if(isset($this->attrs['goExportEntry']['count'])){
78 for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
79 $entry= $this->attrs['goExportEntry'][$i];
80 $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
81 }
82 }
83 }
84 $this->goExportEntryList = $tmp;
86 /* Always is account... */
87 $this->is_account= TRUE;
89 /* Check if goShareServer is defined */
90 if((isset($this->attrs['objectClass']))&&(is_array($this->attrs['objectClass']))){
91 if(in_array("goShareServer",$this->attrs['objectClass'])){
92 $this->goShareServer = true;
93 }
94 }
95 }
97 function addToList($entry){
98 $key = key($entry);
99 $this->goExportEntryList[$key]=$entry[$key];
100 }
102 function deleteFromList($id){
103 unset($this->goExportEntryList[$id]);
104 }
106 function addToMountList($entry) {
107 $key = key($entry);
108 $type = $this->get_share_type($entry[$key]);
109 if (($type == "netatalk") || ($type == "NFS")) {
110 $this->mounts_to_add[$entry[$key]] = $entry[$key];
111 unset($this->mounts_to_remove[$entry[$key]]);
112 }
113 }
115 function deleteFromMountList($entry) {
116 $type = $this->get_share_type($entry);
117 if (($type == "netatalk") || ($type == "NFS")) {
118 $this->mounts_to_remove[$entry] = $entry;
119 unset($this->mounts_to_add[$entry]);
120 }
121 }
123 function execute()
124 {
125 /* Call parent execute */
126 plugin::execute();
128 /* Fill templating stuff */
129 $smarty= get_smarty();
130 $smarty->assign("staticAddress", "");
132 if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList']))){
133 if($this->allow_mounts){
134 foreach($_POST['goExportEntryList'] as $entry){
135 $this->deleteFromMountList($this->goExportEntryList[$entry]);
136 }
137 }
138 foreach($_POST['goExportEntryList'] as $entry){
139 $this->deleteFromList($entry);
140 }
141 }
143 if(isset($_POST['NewNfsAdd'])){
144 $this->oldone = NULL;
145 $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
146 $this->dialog = true;
147 }
149 if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
150 $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
151 $add_mount=isset($this->mounts_to_add[$entry]);
152 $this->oldone=$entry;
153 $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
154 $this->dialog = true;
155 }
157 if(isset($this->o_subWindow)){
158 $this->o_subWindow->save_object(TRUE);
159 }
161 /* Save NFS setup */
162 if(isset($_POST['NFSsave'])){
163 if(count($this->o_subWindow->check())>0){
164 foreach($this->o_subWindow->check() as $msg) {
165 print_red($msg);
166 }
167 }else{
168 $this->o_subWindow->save_object();
169 $newone = $this->o_subWindow->save();
170 $this->addToList($newone);
171 if($this->allow_mounts){
172 if($this->oldone) {
173 $this->deleteFromMountList($this->oldone);
174 }
175 if ($this->o_subWindow->should_create_mount()) {
176 $this->addToMountList($newone);
177 }
178 }
179 unset($this->o_subWindow);
180 $this->dialog = false;
181 }
182 }
184 /* Cancel NFS setup */
185 if(isset($_POST['NFScancel'])){
186 $this->oldone = NULL;
187 unset($this->o_subWindow);
188 $this->dialog = false;
189 }
191 /* Execute NFS setup dialog*/
192 if(isset($this->o_subWindow)){
193 return $this->o_subWindow->execute();
194 }
196 /* Here we add a new entry */
197 if(isset($_POST['NewNTPAdd']) && $_POST['NewNTPExport'] != "") {
198 $this->goTimeSource[$_POST['NewNTPExport']]= $_POST['NewNTPExport'];
199 asort($this->goTimeSource);
200 }
202 /* Deleting an Entry, is a bit more complicated than adding one*/
203 if(isset($_POST['DelNTPEnt'])) {
204 foreach ($_POST['goTimeSource'] as $entry){
205 if (isset($this->goTimeSource[$entry])){
206 unset($this->goTimeSource[$entry]);
207 }
208 }
209 }
211 /* Attributes */
212 foreach ($this->attributes as $attr){
213 $smarty->assign("$attr", $this->$attr);
214 $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
215 $smarty->assign($attr."State","");
216 }
218 $tellSmarty=array();
219 ksort($this->goExportEntryList);
220 foreach($this->goExportEntryList as $name=>$values){
221 $tmp = split("\|",$values);
222 $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
223 }
224 $smarty->assign("goExportEntry",array_keys($tellSmarty));
225 $smarty->assign("goExportEntryKeys",($tellSmarty));
226 $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
228 $smarty->assign("goTimeSource", $this->goTimeSource);
229 $smarty->assign("goTimeSourceACL", chkacl($this->acl, "goTimeSource"));
230 $smarty->assign("goTimeSourceState","");
233 /* Classes... */
234 foreach ($this->additionaloc as $oc => $dummy){
235 if (isset($this->objectclasses[$oc])){
236 $smarty->assign("$oc", "checked");
237 $smarty->assign("$oc"."State", "");
238 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
240 } else {
241 $smarty->assign("$oc", "");
242 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
243 $smarty->assign("$oc"."State", "disabled");
244 }
245 }
247 if(!$this->goShareServer){
248 $smarty->assign("goShareServerState", " disabled ");
249 $smarty->assign("goExportEntryACL", " disabled ");
250 }else{
251 $smarty->assign("goShareServerState", " ");
252 $smarty->assign("goExportEntryACL", " ");
253 }
255 /* Different handling for checkbox */
256 if($this->goXdmcpIsEnabled == "true"){
257 $smarty->assign("goXdmcpIsEnabled","checked");
258 } else {
259 $smarty->assign("goXdmcpIsEnabled","");
260 }
262 return($smarty->fetch (get_template_path('servservice.tpl', TRUE)));
263 }
266 function remove_from_parent()
267 {
268 /* This cannot be removed... */
269 }
272 /* Save data to object */
273 function save_object()
274 {
275 plugin::save_object();
276 if (isset($_POST['servicetab'])){
277 $tmp = $this->goTimeSource;
279 if(isset($_POST['goLdapBase'])){
280 $this->goLdapBase = $_POST['goLdapBase'];
281 }
283 /* Save checkbox state */
284 foreach ($this->additionaloc as $oc => $dummy){
285 // if($oc == "goNtpServer") continue;
286 if (chkacl($this->acl, $oc) == ""){
287 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
288 $this->objectclasses[$oc]= $oc;
289 } else {
290 unset($this->objectclasses[$oc]);
291 }
292 }
293 }
295 /* Save xdmcp is enabled flag */
296 if (isset($_POST['goXdmcpIsEnabled'])){
297 $this->goXdmcpIsEnabled= "true";
298 } else {
299 $this->goXdmcpIsEnabled= "false";
300 }
302 /* Save xdmcp is enabled flag */
303 if (isset($_POST['goShareServer'])){
304 $this->goShareServer = true;
305 } else {
306 $this->goShareServer = false;
307 }
308 $this->goTimeSource = array();
309 $this->goTimeSource = $tmp;
310 }
311 }
314 /* Check supplied data */
315 function check()
316 {
317 /* Call common method to give check the hook */
318 $message= plugin::check();
320 if((isset($_POST['goTerminalServer']))&&(empty($this->goFontPath))){
321 $message[]=_("Terminal server, must have fontpath specified.");
322 }
324 return ($message);
325 }
328 /* Save to LDAP */
329 function save()
330 {
331 plugin::save();
333 $tmp= array();
335 /* Remove all from this plugin */
336 foreach($this->attrs['objectClass'] as $oc){
337 if (!in_array_ics($oc, $this->possible_objectclasses)){
338 $tmp[]= $oc;
339 }
340 }
342 /* Merge our current objectclasses */
343 foreach($this->objectclasses as $oc){
344 if (!in_array_ics($oc, $tmp)){
345 $tmp[]= $oc;
346 }
347 }
349 /* Reassign cleaned value */
350 $this->attrs['objectClass']= $tmp;
352 /* Arrays */
353 foreach (array("goTimeSource"=>"goTimeSource", "goExportEntryList"=>"goExportEntry") as $source => $destination){
354 $this->attrs[$destination]= array();
355 foreach ($this->$source as $element){
356 $this->attrs[$destination][]= $element;
357 }
358 }
360 /* Process netatalk mounts */
361 if($this->allow_mounts) {
362 $this->process_mounts();
363 }
365 /* Remove illegal attributes */
366 foreach ($this->additionaloc as $oc => $attrs){
367 if (!in_array($oc, $this->objectclasses)){
368 foreach ($attrs as $attr){
369 $this->attrs[$attr]= array();
370 }
371 }
372 }
374 /* Write to LDAP */
375 $ldap= $this->config->get_ldap_link();
376 $ldap->cd($this->dn);
377 $this->cleanup();
378 $ldap->modify ($this->attrs);
380 show_ldap_error($ldap->get_error(), _("Saving server service object failed"));
382 /* Optionally execute a command after we're done */
383 if ($this->initially_was_account == $this->is_account){
384 if ($this->is_modified){
385 $this->handle_post_events("mofify");
386 }
387 } else {
388 $this->handle_post_events("add");
389 }
390 }
392 function process_mounts() {
394 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
395 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
397 $mounts = array(
398 "objectClass" => "container",
399 "cn" => "mounts"
400 );
402 # load data from mounts container
403 $ldap = $this->config->get_ldap_link();
404 $ldap->cat($mountsdn, array('dn'));
405 $attrs = $ldap->fetch();
407 # mounts container not present yet, so we create it
408 if (count($attrs) == 0) {
409 $ldap->cd($mountsdn);
410 $ldap->add($mounts);
411 show_ldap_error($ldap->get_error(), _("Creating mount container failed"));
412 gosa_log("Mount container '$mountsdn' has been created");
413 }
415 # remove deleted mounts from the container
416 foreach ($this->mounts_to_remove as $entry) {
417 $mount=$this->returnMountEntry($entry);
418 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
420 $ldap->cat($mountdn, array('dn'));
421 $attrs = $ldap->fetch();
423 if (count($attrs) != 0) {
424 $ldap->rmdir($mountdn);
425 show_ldap_error($ldap->get_error(), _("Removing mount container failed"));
426 gosa_log("Mount object '".$mountdn."' has been removed");
427 }
428 }
430 # add new mounts to the container
431 foreach ($this->mounts_to_add as $entry) {
433 $mount=$this->returnMountEntry($entry);
434 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
435 $ldap->cd($mountdn);
436 $ldap->add($mount);
437 show_ldap_error($ldap->get_error(), _("Saving mount container failed"));
438 gosa_log("Mount object '".$mountdn."' has been added");
439 }
440 }
442 function get_share_type($share) {
443 $tmp = split("\|", $share);
444 return $tmp[2];
445 }
448 function returnMountEntry($entry) {
449 $item = split("\|", $entry);
450 $name = $item[0];
451 $description = $item[1];
452 $type = $item[2];
453 $charset = $item[3];
454 $path = $item[4];
455 $options = $item[5];
457 switch ($type) {
458 case "netatalk" : {
459 $mount = array(
460 "mountDirectory" => "/Network/Servers/",
461 "mountOption" => array(
462 "net",
463 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
464 ),
465 "mountType" => "url",
466 "objectClass" => "mount",
467 "cn" => $this->cn .":/".$name
468 );
469 break;
470 }
471 case "NFS" : {
472 $mount = array(
473 "mountDirectory" => "/Network/Servers/",
474 "mountOption" => "net",
475 "mountType" => "nfs",
476 "objectClass" => "mount",
477 "cn" => $this->cn .":".$path
478 );
479 break;
480 }
481 default : {
482 continue;
483 }
484 }
485 return $mount;
486 }
488 }
489 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
490 ?>