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, $parent= NULL)
45 {
47 plugin::plugin ($config, $dn, $parent);
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 /* Check if the share is used by someone */
103 $ldap = $this->config->get_ldap_link();
104 $ldap->cd($this->config->current['BASE']);
105 $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*))", array("cn"));
106 $cnt= $ldap->count();
107 if ($cnt){
108 $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
109 $msg.= "<br><br><ul>";
110 while ($attrs= $ldap->fetch()){
111 $msg.= "<li>".$attrs["cn"][0]."</li>";
112 }
113 $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
114 print_red($msg);
116 } else {
117 /* Finally remove it */
118 unset($this->goExportEntryList[$id]);
119 }
120 }
122 function addToMountList($entry) {
123 $key = key($entry);
124 $type = $this->get_share_type($entry[$key]);
125 if (($type == "netatalk") || ($type == "NFS")) {
126 $this->mounts_to_add[$entry[$key]] = $entry[$key];
127 unset($this->mounts_to_remove[$entry[$key]]);
128 }
129 }
131 function deleteFromMountList($entry) {
132 $type = $this->get_share_type($entry);
133 if (($type == "netatalk") || ($type == "NFS")) {
134 $this->mounts_to_remove[$entry] = $entry;
135 unset($this->mounts_to_add[$entry]);
136 }
137 }
139 function execute()
140 {
141 /* Call parent execute */
142 plugin::execute();
144 /* Fill templating stuff */
145 $smarty= get_smarty();
146 $smarty->assign("staticAddress", "");
148 if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList']))){
149 if($this->allow_mounts){
150 foreach($_POST['goExportEntryList'] as $entry){
151 $this->deleteFromMountList($this->goExportEntryList[$entry]);
152 }
153 }
154 foreach($_POST['goExportEntryList'] as $entry){
155 $this->deleteFromList($entry);
156 }
157 }
159 if(isset($_POST['NewNfsAdd'])){
160 $this->oldone = NULL;
161 $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
162 $this->dialog = true;
163 }
165 if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
166 $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
167 $add_mount=isset($this->mounts_to_add[$entry]);
168 $this->oldone=$entry;
169 $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
170 $this->dialog = true;
171 }
173 if(isset($this->o_subWindow)){
174 $this->o_subWindow->save_object(TRUE);
175 }
177 /* Save NFS setup */
178 if(isset($_POST['NFSsave'])){
179 if(count($this->o_subWindow->check())>0){
180 foreach($this->o_subWindow->check() as $msg) {
181 print_red($msg);
182 }
183 }else{
184 $this->o_subWindow->save_object();
185 $newone = $this->o_subWindow->save();
186 $this->addToList($newone);
187 if($this->allow_mounts){
188 if($this->oldone) {
189 $this->deleteFromMountList($this->oldone);
190 }
191 if ($this->o_subWindow->should_create_mount()) {
192 $this->addToMountList($newone);
193 }
194 }
195 unset($this->o_subWindow);
196 $this->dialog = false;
197 }
198 }
200 /* Cancel NFS setup */
201 if(isset($_POST['NFScancel'])){
202 $this->oldone = NULL;
203 unset($this->o_subWindow);
204 $this->dialog = false;
205 }
207 /* Execute NFS setup dialog*/
208 if(isset($this->o_subWindow)){
209 return $this->o_subWindow->execute();
210 }
212 /* Here we add a new entry */
213 if(isset($_POST['NewNTPAdd']) && $_POST['NewNTPExport'] != "") {
214 $this->goTimeSource[$_POST['NewNTPExport']]= $_POST['NewNTPExport'];
215 asort($this->goTimeSource);
216 }
218 /* Deleting an Entry, is a bit more complicated than adding one*/
219 if(isset($_POST['DelNTPEnt'])) {
220 foreach ($_POST['goTimeSource'] as $entry){
221 if (isset($this->goTimeSource[$entry])){
222 unset($this->goTimeSource[$entry]);
223 }
224 }
225 }
227 /* Attributes */
228 foreach ($this->attributes as $attr){
229 $smarty->assign("$attr", $this->$attr);
230 $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
231 $smarty->assign($attr."State","");
232 }
234 $tellSmarty=array();
235 ksort($this->goExportEntryList);
236 foreach($this->goExportEntryList as $name=>$values){
237 $tmp = split("\|",$values);
238 $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
239 }
240 $smarty->assign("goExportEntry",array_keys($tellSmarty));
241 $smarty->assign("goExportEntryKeys",($tellSmarty));
242 $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
244 $smarty->assign("goTimeSource", $this->goTimeSource);
245 $smarty->assign("goTimeSourceACL", chkacl($this->acl, "goTimeSource"));
246 $smarty->assign("goTimeSourceState","");
249 /* Classes... */
250 foreach ($this->additionaloc as $oc => $dummy){
251 if (isset($this->objectclasses[$oc])){
252 $smarty->assign("$oc", "checked");
253 $smarty->assign("$oc"."State", "");
254 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
256 } else {
257 $smarty->assign("$oc", "");
258 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
259 $smarty->assign("$oc"."State", "disabled");
260 }
261 }
263 if(!$this->goShareServer){
264 $smarty->assign("goShareServerState", " disabled ");
265 $smarty->assign("goExportEntryACL", " disabled ");
266 }else{
267 $smarty->assign("goShareServerState", " ");
268 $smarty->assign("goExportEntryACL", " ");
269 }
271 /* Different handling for checkbox */
272 if($this->goXdmcpIsEnabled == "true"){
273 $smarty->assign("goXdmcpIsEnabled","checked");
274 } else {
275 $smarty->assign("goXdmcpIsEnabled","");
276 }
278 return($smarty->fetch (get_template_path('servservice.tpl', TRUE)));
279 }
282 function remove_from_parent()
283 {
284 /* This cannot be removed... */
285 }
288 /* Save data to object */
289 function save_object()
290 {
291 plugin::save_object();
292 if (isset($_POST['servicetab'])){
293 $tmp = $this->goTimeSource;
295 if(isset($_POST['goLdapBase'])){
296 $this->goLdapBase = $_POST['goLdapBase'];
297 }
299 /* Save checkbox state */
300 foreach ($this->additionaloc as $oc => $dummy){
301 // if($oc == "goNtpServer") continue;
302 if (chkacl($this->acl, $oc) == ""){
303 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
304 $this->objectclasses[$oc]= $oc;
305 } else {
306 unset($this->objectclasses[$oc]);
307 }
308 }
309 }
311 /* Save xdmcp is enabled flag */
312 if (isset($_POST['goXdmcpIsEnabled'])){
313 $this->goXdmcpIsEnabled= "true";
314 } else {
315 $this->goXdmcpIsEnabled= "false";
316 }
318 /* Save xdmcp is enabled flag */
319 if (isset($_POST['goShareServer'])){
320 $this->goShareServer = true;
321 } else {
322 $this->goShareServer = false;
323 }
324 $this->goTimeSource = array();
325 $this->goTimeSource = $tmp;
326 }
327 }
330 /* Check supplied data */
331 function check()
332 {
333 /* Call common method to give check the hook */
334 $message= plugin::check();
336 if((isset($_POST['goTerminalServer']))&&(empty($this->goFontPath))){
337 $message[]=_("Terminal server, must have fontpath specified.");
338 }
340 return ($message);
341 }
344 /* Save to LDAP */
345 function save()
346 {
347 plugin::save();
349 $tmp= array();
351 /* Remove all from this plugin */
352 foreach($this->attrs['objectClass'] as $oc){
353 if (!in_array_ics($oc, $this->possible_objectclasses)){
354 $tmp[]= $oc;
355 }
356 }
358 /* Merge our current objectclasses */
359 foreach($this->objectclasses as $oc){
360 if (!in_array_ics($oc, $tmp)){
361 $tmp[]= $oc;
362 }
363 }
365 /* Reassign cleaned value */
366 $this->attrs['objectClass']= $tmp;
368 /* Arrays */
369 foreach (array("goTimeSource"=>"goTimeSource", "goExportEntryList"=>"goExportEntry") as $source => $destination){
370 $this->attrs[$destination]= array();
371 foreach ($this->$source as $element){
372 $this->attrs[$destination][]= $element;
373 }
374 }
376 /* Process netatalk mounts */
377 if($this->allow_mounts) {
378 $this->process_mounts();
379 }
381 /* Remove illegal attributes */
382 foreach ($this->additionaloc as $oc => $attrs){
383 if (!in_array($oc, $this->objectclasses)){
384 foreach ($attrs as $attr){
385 $this->attrs[$attr]= array();
386 }
387 }
388 }
390 /* Write to LDAP */
391 $ldap= $this->config->get_ldap_link();
392 $ldap->cd($this->dn);
393 $this->cleanup();
394 $ldap->modify ($this->attrs);
396 show_ldap_error($ldap->get_error(), _("Saving server service object failed"));
398 /* Optionally execute a command after we're done */
399 if ($this->initially_was_account == $this->is_account){
400 if ($this->is_modified){
401 $this->handle_post_events("modify");
402 }
403 } else {
404 $this->handle_post_events("add");
405 }
406 }
408 function process_mounts() {
410 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
411 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
413 $mounts = array(
414 "objectClass" => "container",
415 "cn" => "mounts"
416 );
418 # load data from mounts container
419 $ldap = $this->config->get_ldap_link();
420 $ldap->cat($mountsdn, array('dn'));
421 $attrs = $ldap->fetch();
423 # mounts container not present yet, so we create it
424 if (count($attrs) == 0) {
425 $ldap->cd($mountsdn);
426 $ldap->add($mounts);
427 show_ldap_error($ldap->get_error(), _("Creating mount container failed"));
428 gosa_log("Mount container '$mountsdn' has been created");
429 }
431 # remove deleted mounts from the container
432 foreach ($this->mounts_to_remove as $entry) {
433 $mount=$this->returnMountEntry($entry);
434 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
436 $ldap->cat($mountdn, array('dn'));
437 $attrs = $ldap->fetch();
439 if (count($attrs) != 0) {
440 $ldap->rmdir($mountdn);
441 show_ldap_error($ldap->get_error(), _("Removing mount container failed"));
442 gosa_log("Mount object '".$mountdn."' has been removed");
443 }
444 }
446 # add new mounts to the container
447 foreach ($this->mounts_to_add as $entry) {
449 $mount=$this->returnMountEntry($entry);
450 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
451 $ldap->cd($mountdn);
452 $ldap->add($mount);
453 show_ldap_error($ldap->get_error(), _("Saving mount container failed"));
454 gosa_log("Mount object '".$mountdn."' has been added");
455 }
456 }
458 function get_share_type($share) {
459 $tmp = split("\|", $share);
460 return $tmp[2];
461 }
464 function returnMountEntry($entry) {
465 $item = split("\|", $entry);
466 $name = $item[0];
467 $description = $item[1];
468 $type = $item[2];
469 $charset = $item[3];
470 $path = $item[4];
471 $options = $item[5];
473 switch ($type) {
474 case "netatalk" : {
475 $mount = array(
476 "mountDirectory" => "/Network/Servers/",
477 "mountOption" => array(
478 "net",
479 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
480 ),
481 "mountType" => "url",
482 "objectClass" => "mount",
483 "cn" => $this->cn .":/".$name
484 );
485 break;
486 }
487 case "NFS" : {
488 $mount = array(
489 "mountDirectory" => "/Network/Servers/",
490 "mountOption" => "net",
491 "mountType" => "nfs",
492 "objectClass" => "mount",
493 "cn" => $this->cn .":".$path
494 );
495 break;
496 }
497 default : {
498 continue;
499 }
500 }
501 return $mount;
502 }
504 }
505 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
506 ?>