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 $ldap = $this->config->get_ldap_link();
50 $avl_objectclasses = $ldap->get_objectclasses();
51 if (isset($avl_objectclasses["mount"])) {
52 $this->allow_mounts = true;
53 }
55 /* Assemble final object class list */
56 foreach ($this->additionaloc as $oc => $dummy){
57 if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
58 $this->objectclasses[$oc]= $oc;
59 }
60 }
62 /* Load arrays */
63 foreach (array("goTimeSource") as $name){
64 $tmp= array();
65 if (isset($this->attrs[$name])){
66 for ($i= 0; $i<$this->attrs[$name]['count']; $i++){
67 $tmp[$this->attrs[$name][$i]]= $this->attrs[$name][$i];
68 }
69 }
70 $this->$name= $tmp;
71 }
73 $tmp =array();
74 $tmp2=array();
75 if(isset($this->attrs['goExportEntry'])){
76 if(isset($this->attrs['goExportEntry']['count'])){
77 for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
78 $entry= $this->attrs['goExportEntry'][$i];
79 $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
80 }
81 }
82 }
83 $this->goExportEntryList = $tmp;
85 /* Always is account... */
86 $this->is_account= TRUE;
88 /* Check if goShareServer is defined */
89 if((isset($this->attrs['objectClass']))&&(is_array($this->attrs['objectClass']))){
90 if(in_array("goShareServer",$this->attrs['objectClass'])){
91 $this->goShareServer = true;
92 }
93 }
94 }
96 function addToList($entry){
97 $key = key($entry);
98 $this->goExportEntryList[$key]=$entry[$key];
99 }
101 function deleteFromList($id){
102 unset($this->goExportEntryList[$id]);
103 }
105 function addToMountList($entry) {
106 $key = key($entry);
107 $type = $this->get_share_type($entry[$key]);
108 if (($type == "netatalk") || ($type == "NFS")) {
109 $this->mounts_to_add[$entry[$key]] = $entry[$key];
110 unset($this->mounts_to_remove[$entry[$key]]);
111 }
112 }
114 function deleteFromMountList($entry) {
115 $type = $this->get_share_type($entry);
116 if (($type == "netatalk") || ($type == "NFS")) {
117 $this->mounts_to_remove[$entry] = $entry;
118 unset($this->mounts_to_add[$entry]);
119 }
120 }
122 function execute()
123 {
124 /* Call parent execute */
125 plugin::execute();
127 /* Fill templating stuff */
128 $smarty= get_smarty();
129 $smarty->assign("staticAddress", "");
131 if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList']))){
132 if($this->allow_mounts){
133 foreach($_POST['goExportEntryList'] as $entry){
134 $this->deleteFromMountList($this->goExportEntryList[$entry]);
135 }
136 }
137 foreach($_POST['goExportEntryList'] as $entry){
138 $this->deleteFromList($entry);
139 }
140 }
142 if(isset($_POST['NewNfsAdd'])){
143 $this->oldone = NULL;
144 $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
145 $this->dialog = true;
146 }
148 if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
149 $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
150 $add_mount=isset($this->mounts_to_add[$entry]);
151 $this->oldone=$entry;
152 $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
153 $this->dialog = true;
154 }
156 if(isset($this->o_subWindow)){
157 $this->o_subWindow->save_object(TRUE);
158 }
160 /* Save NFS setup */
161 if(isset($_POST['NFSsave'])){
162 if(count($this->o_subWindow->check())>0){
163 foreach($this->o_subWindow->check() as $msg) {
164 print_red($msg);
165 }
166 }else{
167 $this->o_subWindow->save_object();
168 $newone = $this->o_subWindow->save();
169 $this->addToList($newone);
170 if($this->allow_mounts){
171 if($this->oldone) {
172 $this->deleteFromMountList($this->oldone);
173 }
174 if ($this->o_subWindow->should_create_mount()) {
175 $this->addToMountList($newone);
176 }
177 }
178 unset($this->o_subWindow);
179 $this->dialog = false;
180 }
181 }
183 /* Cancel NFS setup */
184 if(isset($_POST['NFScancel'])){
185 $this->oldone = NULL;
186 unset($this->o_subWindow);
187 $this->dialog = false;
188 }
190 /* Execute NFS setup dialog*/
191 if(isset($this->o_subWindow)){
192 return $this->o_subWindow->execute();
193 }
195 /* Here we add a new entry */
196 if(isset($_POST['NewNTPAdd']) && $_POST['NewNTPExport'] != "") {
197 $this->goTimeSource[$_POST['NewNTPExport']]= $_POST['NewNTPExport'];
198 asort($this->goTimeSource);
199 }
201 /* Deleting an Entry, is a bit more complicated than adding one*/
202 if(isset($_POST['DelNTPEnt'])) {
203 foreach ($_POST['goTimeSource'] as $entry){
204 if (isset($this->goTimeSource[$entry])){
205 unset($this->goTimeSource[$entry]);
206 }
207 }
208 }
210 /* Attributes */
211 foreach ($this->attributes as $attr){
212 $smarty->assign("$attr", $this->$attr);
213 $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
214 $smarty->assign($attr."State","");
215 }
217 $tellSmarty=array();
218 ksort($this->goExportEntryList);
219 foreach($this->goExportEntryList as $name=>$values){
220 $tmp = split("\|",$values);
221 $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
222 }
223 $smarty->assign("goExportEntry",array_keys($tellSmarty));
224 $smarty->assign("goExportEntryKeys",($tellSmarty));
225 $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
227 $smarty->assign("goTimeSource", $this->goTimeSource);
228 $smarty->assign("goTimeSourceACL", chkacl($this->acl, "goTimeSource"));
229 $smarty->assign("goTimeSourceState","");
232 /* Classes... */
233 foreach ($this->additionaloc as $oc => $dummy){
234 if (isset($this->objectclasses[$oc])){
235 $smarty->assign("$oc", "checked");
236 $smarty->assign("$oc"."State", "");
237 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
239 } else {
240 $smarty->assign("$oc", "");
241 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
242 $smarty->assign("$oc"."State", "disabled");
243 }
244 }
246 if(!$this->goShareServer){
247 $smarty->assign("goShareServerState", " disabled ");
248 $smarty->assign("goExportEntryACL", " disabled ");
249 }else{
250 $smarty->assign("goShareServerState", " ");
251 $smarty->assign("goExportEntryACL", " ");
252 }
254 /* Different handling for checkbox */
255 if($this->goXdmcpIsEnabled == "true"){
256 $smarty->assign("goXdmcpIsEnabled","checked");
257 } else {
258 $smarty->assign("goXdmcpIsEnabled","");
259 }
261 return($smarty->fetch (get_template_path('servservice.tpl', TRUE)));
262 }
265 function remove_from_parent()
266 {
267 /* This cannot be removed... */
268 }
271 /* Save data to object */
272 function save_object()
273 {
274 plugin::save_object();
275 if (isset($_POST['servicetab'])){
276 $tmp = $this->goTimeSource;
278 if(isset($_POST['goLdapBase'])){
279 $this->goLdapBase = $_POST['goLdapBase'];
280 }
282 /* Save checkbox state */
283 foreach ($this->additionaloc as $oc => $dummy){
284 // if($oc == "goNtpServer") continue;
285 if (chkacl($this->acl, $oc) == ""){
286 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
287 $this->objectclasses[$oc]= $oc;
288 } else {
289 unset($this->objectclasses[$oc]);
290 }
291 }
292 }
294 /* Save xdmcp is enabled flag */
295 if (isset($_POST['goXdmcpIsEnabled'])){
296 $this->goXdmcpIsEnabled= "true";
297 } else {
298 $this->goXdmcpIsEnabled= "false";
299 }
301 /* Save xdmcp is enabled flag */
302 if (isset($_POST['goShareServer'])){
303 $this->goShareServer = true;
304 } else {
305 $this->goShareServer = false;
306 }
307 $this->goTimeSource = array();
308 $this->goTimeSource = $tmp;
309 }
310 }
313 /* Check supplied data */
314 function check()
315 {
316 /* Call common method to give check the hook */
317 $message= plugin::check();
319 if((isset($_POST['goTerminalServer']))&&(empty($this->goFontPath))){
320 $message[]=_("Terminal server, must have fontpath specified.");
321 }
323 return ($message);
324 }
327 /* Save to LDAP */
328 function save()
329 {
330 plugin::save();
332 $tmp= array();
334 /* Remove all from this plugin */
335 foreach($this->attrs['objectClass'] as $oc){
336 if (!in_array_ics($oc, $this->possible_objectclasses)){
337 $tmp[]= $oc;
338 }
339 }
341 /* Merge our current objectclasses */
342 foreach($this->objectclasses as $oc){
343 if (!in_array_ics($oc, $tmp)){
344 $tmp[]= $oc;
345 }
346 }
348 /* Reassign cleaned value */
349 $this->attrs['objectClass']= $tmp;
351 /* Arrays */
352 foreach (array("goTimeSource"=>"goTimeSource", "goExportEntryList"=>"goExportEntry") as $source => $destination){
353 $this->attrs[$destination]= array();
354 foreach ($this->$source as $element){
355 $this->attrs[$destination][]= $element;
356 }
357 }
359 /* Process netatalk mounts */
360 if($this->allow_mounts) {
361 $this->process_mounts();
362 }
364 /* Remove illegal attributes */
365 foreach ($this->additionaloc as $oc => $attrs){
366 if (!in_array($oc, $this->objectclasses)){
367 foreach ($attrs as $attr){
368 $this->attrs[$attr]= array();
369 }
370 }
371 }
373 /* Write to LDAP */
374 $ldap= $this->config->get_ldap_link();
375 $ldap->cd($this->dn);
376 $this->cleanup();
377 $ldap->modify ($this->attrs);
379 show_ldap_error($ldap->get_error(), _("Saving server service object failed"));
381 /* Optionally execute a command after we're done */
382 if ($this->initially_was_account == $this->is_account){
383 if ($this->is_modified){
384 $this->handle_post_events("mofify");
385 }
386 } else {
387 $this->handle_post_events("add");
388 }
389 }
391 function process_mounts() {
393 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
394 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
396 $mounts = array(
397 "objectClass" => "container",
398 "cn" => "mounts"
399 );
401 # load data from mounts container
402 $ldap = $this->config->get_ldap_link();
403 $ldap->cat($mountsdn, array('dn'));
404 $attrs = $ldap->fetch();
406 # mounts container not present yet, so we create it
407 if (count($attrs) == 0) {
408 $ldap->cd($mountsdn);
409 $ldap->add($mounts);
410 show_ldap_error($ldap->get_error(), _("Creating mount container failed"));
411 gosa_log("Mount container '$mountsdn' has been created");
412 }
414 # remove deleted mounts from the container
415 foreach ($this->mounts_to_remove as $entry) {
416 $mount=$this->returnMountEntry($entry);
417 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
419 $ldap->cat($mountdn, array('dn'));
420 $attrs = $ldap->fetch();
422 if (count($attrs) != 0) {
423 $ldap->rmdir($mountdn);
424 show_ldap_error($ldap->get_error(), _("Removing mount container failed"));
425 gosa_log("Mount object '".$mountdn."' has been removed");
426 }
427 }
429 # add new mounts to the container
430 foreach ($this->mounts_to_add as $entry) {
432 $mount=$this->returnMountEntry($entry);
433 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
434 $ldap->cd($mountdn);
435 $ldap->add($mount);
436 show_ldap_error($ldap->get_error(), _("Saving mount container failed"));
437 gosa_log("Mount object '".$mountdn."' has been added");
438 }
439 }
441 function get_share_type($share) {
442 $tmp = split("\|", $share);
443 return $tmp[2];
444 }
447 function returnMountEntry($entry) {
448 $item = split("\|", $entry);
449 $name = $item[0];
450 $description = $item[1];
451 $type = $item[2];
452 $charset = $item[3];
453 $path = $item[4];
454 $options = $item[5];
456 switch ($type) {
457 case "netatalk" : {
458 $mount = array(
459 "mountDirectory" => "/Network/Servers/",
460 "mountOption" => array(
461 "net",
462 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
463 ),
464 "mountType" => "url",
465 "objectClass" => "mount",
466 "cn" => $this->cn .":/".$name
467 );
468 break;
469 }
470 case "NFS" : {
471 $mount = array(
472 "mountDirectory" => "/Network/Servers/",
473 "mountOption" => "net",
474 "mountType" => "nfs",
475 "objectClass" => "mount",
476 "cn" => $this->cn .":".$path
477 );
478 break;
479 }
480 default : {
481 continue;
482 }
483 }
484 return $mount;
485 }
487 }
488 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
489 ?>