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 $goTimeSource = array();
12 var $goLdapBase = "";
13 var $goXdmcpIsEnabled = "";
14 var $goFontPath = "";
15 var $goNTPServer = "";
16 var $goShareServer = false;
17 var $goLdapServer = "";
18 var $goTerminalServer = "";
19 var $goSyslogServer = "";
20 var $goCupsServer = "";
21 var $goMailServer = "";
22 var $o_subWindow = NULL;
24 /* attribute list for save action */
25 var $ignore_account= TRUE;
26 var $attributes = array("goLdapBase","goXdmcpIsEnabled","goFontPath","goExportEntry","goTimeSource");
27 var $possible_objectclasses= array( "goShareServer", "goNtpServer", "goServer", "GOhard", "goLdapServer",
28 "goTerminalServer", "goSyslogServer", "goCupsServer","goMailServer");
29 var $objectclasses = array( "top","goServer", "GOhard");
30 var $additionaloc = array( "goShareServer" => array("goExportEntry"),
31 "goNtpServer" => array("goTimeSource"),
32 "goLdapServer" => array("goLdapBase"),
33 "goTerminalServer"=> array("goXdmcpIsEnabled", "goFontPath"),
34 "goSyslogServer" => array(),
35 "goMailServer" => array(),
36 "goCupsServer" => array());
38 var $allow_mounts = false; //do we allow mount entries?
39 var $mounts_to_add = array();
40 var $mounts_to_remove = array();
41 var $oldone = NULL; //temp dave for old mount entry
43 function servservice ($config, $dn= NULL)
44 {
46 plugin::plugin ($config, $dn);
48 $ldap = $this->config->get_ldap_link();
49 $avl_objectclasses = @$ldap->get_objectclasses();
50 if (isset($avl_objectclasses["mount"])) {
51 $this->allow_mounts = true;
52 }
54 /* Assemble final object class list */
55 foreach ($this->additionaloc as $oc => $dummy){
56 if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
57 $this->objectclasses[$oc]= $oc;
58 }
59 }
61 /* Load arrays */
62 foreach (array("goTimeSource") as $name){
63 $tmp= array();
64 if (isset($this->attrs[$name])){
65 for ($i= 0; $i<$this->attrs[$name]['count']; $i++){
66 $tmp[$this->attrs[$name][$i]]= $this->attrs[$name][$i];
67 }
68 }
69 $this->$name= $tmp;
70 }
73 $tmp =array();
74 $tmp2=array();
75 if(isset($this->attrs['goExportEntry'])){
76 unset($this->attrs['goExportEntry']['count']);
77 if((isset($this->attrs['goExportEntry']))&&(isset($this->attrs['goExportEntry']))){
78 foreach($this->attrs['goExportEntry'] as $entry){
79 $tmp2= split("\|",$entry);
80 $tmp[$tmp2[0]]= $entry;
81 }
82 }
83 }
84 $this->goExportEntry = $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->goExportEntry[$key]=$entry[$key];
100 }
102 function deleteFromList($id){
103 unset($this->goExportEntry[$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();
131 $smarty->assign("staticAddress", "");
133 if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntry']))){
134 if($this->allow_mounts){
135 $this->deleteFromMountList($this->goExportEntry[$_POST['goExportEntry']]);
136 }
137 $this->deleteFromList($_POST['goExportEntry']);
138 }
140 if(isset($_POST['NewNfsAdd'])){
141 $this->oldone = NULL;
142 $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
143 $this->dialog = true;
144 }
146 if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntry']))){
147 $entry = $this->goExportEntry[$_POST['goExportEntry']];
148 $add_mount=isset($this->mounts_to_add[$entry]);
149 $this->oldone=$entry;
150 $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
151 $this->dialog = true;
152 }
154 if(isset($this->o_subWindow)){
155 $this->o_subWindow->save_object(TRUE);
156 }
158 /* Save NFS setup */
159 if(isset($_POST['NFSsave'])){
160 if(count($this->o_subWindow->check())>0){
161 foreach($this->o_subWindow->check() as $msg) {
162 print_red($msg);
163 }
164 }else{
165 $this->o_subWindow->save_object();
166 $newone = $this->o_subWindow->save();
167 $this->addToList($newone);
168 if($this->allow_mounts){
169 if($this->oldone) {
170 $this->deleteFromMountList($this->oldone);
171 }
172 if ($this->o_subWindow->should_create_mount()) {
173 $this->addToMountList($newone);
174 }
175 }
176 unset($this->o_subWindow);
177 $this->dialog = false;
178 }
179 }
181 /* Cancel NFS setup */
182 if(isset($_POST['NFScancel'])){
183 $this->oldone = NULL;
184 unset($this->o_subWindow);
185 $this->dialog = false;
186 }
188 /* Execute NFS setup dialog*/
189 if(isset($this->o_subWindow)){
190 return $this->o_subWindow->execute();
191 }
193 /* Here we add a new entry */
194 if(isset($_POST['NewNTPAdd']) && $_POST['NewNTPExport'] != "") {
195 $this->goTimeSource[$_POST['NewNTPExport']]= $_POST['NewNTPExport'];
196 asort($this->goTimeSource);
197 }
199 /* Deleting an Entry, is a bit more complicated than adding one*/
200 if(isset($_POST['DelNTPEnt'])) {
201 foreach ($_POST['goTimeSource'] as $entry){
202 if (isset($this->goTimeSource[$entry])){
203 unset($this->goTimeSource[$entry]);
204 }
205 }
206 }
208 /* Attributes */
209 foreach ($this->attributes as $attr){
210 $smarty->assign("$attr", $this->$attr);
211 $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
212 $smarty->assign($attr."State","");
213 }
215 $tellSmarty=array();
216 ksort($this->goExportEntry);
217 foreach($this->goExportEntry as $name=>$values){
218 $tmp = split("\|",$values);
219 $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
220 }
221 $smarty->assign("goExportEntry",array_keys($tellSmarty));
222 $smarty->assign("goExportEntryKeys",($tellSmarty));
223 $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
225 $smarty->assign("goTimeSource", $this->goTimeSource);
226 $smarty->assign("goTimeSourceACL", chkacl($this->acl, "goTimeSource"));
227 $smarty->assign("goTimeSourceState","");
230 /* Classes... */
231 foreach ($this->additionaloc as $oc => $dummy){
232 if (isset($this->objectclasses[$oc])){
233 $smarty->assign("$oc", "checked");
234 $smarty->assign("$oc"."State", "");
235 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
237 } else {
238 $smarty->assign("$oc", "");
239 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
240 $smarty->assign("$oc"."State", "disabled");
241 }
242 }
244 if(!$this->goShareServer){
245 $smarty->assign("goShareServerState", " disabled ");
246 $smarty->assign("goExportEntryACL", " disabled ");
247 }else{
248 $smarty->assign("goShareServerState", " ");
249 $smarty->assign("goExportEntryACL", " ");
250 }
252 /* Different handling for checkbox */
253 if($this->goXdmcpIsEnabled == "true"){
254 $smarty->assign("goXdmcpIsEnabled","checked");
255 } else {
256 $smarty->assign("goXdmcpIsEnabled","");
257 }
259 return($smarty->fetch (get_template_path('servservice.tpl', TRUE)));
260 }
263 function remove_from_parent()
264 {
265 /* This cannot be removed... */
266 }
269 /* Save data to object */
270 function save_object()
271 {
272 if (isset($_POST['servicetab'])){
273 $tmp = $this->goTimeSource;
275 if(isset($_POST['goLdapBase'])){
276 $this->goLdapBase = $_POST['goLdapBase'];
277 }
279 /* Save checkbox state */
280 foreach ($this->additionaloc as $oc => $dummy){
281 // if($oc == "goNtpServer") continue;
282 if (chkacl($this->acl, $oc) == ""){
283 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
284 $this->objectclasses[$oc]= $oc;
285 } else {
286 unset($this->objectclasses[$oc]);
287 }
288 }
289 }
291 /* Save xdmcp is enabled flag */
292 if (isset($_POST['goXdmcpIsEnabled'])){
293 $this->goXdmcpIsEnabled= "true";
294 } else {
295 $this->goXdmcpIsEnabled= "false";
296 }
298 /* Save xdmcp is enabled flag */
299 if (isset($_POST['goShareServer'])){
300 $this->goShareServer = true;
301 } else {
302 $this->goShareServer = false;
303 }
304 $this->goTimeSource = array();
305 $this->goTimeSource = $tmp;
306 }
307 }
310 /* Check supplied data */
311 function check()
312 {
313 /* Call common method to give check the hook */
314 $message= plugin::check();
316 if((isset($_POST['goTerminalServer']))&&(empty($this->goFontPath))){
317 $message[]=_("Terminal server, must have fontpath specified.");
318 }
320 return ($message);
321 }
324 /* Save to LDAP */
325 function save()
326 {
327 plugin::save();
329 $tmp= array();
331 /* Remove all from this plugin */
332 foreach($this->attrs['objectClass'] as $oc){
333 if (!in_array_ics($oc, $this->possible_objectclasses)){
334 $tmp[]= $oc;
335 }
336 }
338 /* Merge our current objectclasses */
339 foreach($this->objectclasses as $oc){
340 if (!in_array_ics($oc, $tmp)){
341 $tmp[]= $oc;
342 }
343 }
345 /* Reassign cleaned value */
346 $this->attrs['objectClass']= $tmp;
348 /* Arrays */
349 foreach (array("goTimeSource", "goExportEntry") as $name){
350 $this->attrs[$name]= array();
351 foreach ($this->$name as $element){
352 $this->attrs[$name][]= $element;
353 }
354 }
356 /* Process netatalk mounts */
357 if($this->allow_mounts) {
358 $this->process_mounts();
359 }
361 /* Remove illegal attributes */
362 foreach ($this->additionaloc as $oc => $attrs){
363 if (!in_array($oc, $this->objectclasses)){
364 foreach ($attrs as $attr){
365 $this->attrs[$attr]= array();
366 }
367 }
368 }
370 /* Write to LDAP */
371 $ldap= $this->config->get_ldap_link();
372 $ldap->cd($this->dn);
373 $this->cleanup();
374 $ldap->modify ($this->attrs);
376 show_ldap_error($ldap->get_error());
378 /* Optionally execute a command after we're done */
379 if ($this->initially_was_account == $this->is_account){
380 if ($this->is_modified){
381 $this->handle_post_events("mofify");
382 }
383 } else {
384 $this->handle_post_events("add");
385 }
386 }
388 function process_mounts() {
390 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
391 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
393 $mounts = array(
394 "objectClass" => "container",
395 "cn" => "mounts"
396 );
398 # load data from mounts container
399 $ldap = $this->config->get_ldap_link();
400 $ldap->cat($mountsdn);
401 $attrs = $ldap->fetch();
403 # mounts container not present yet, so we create it
404 if (count($attrs) == 0) {
405 $ldap->cd($mountsdn);
406 $ldap->add($mounts);
407 show_ldap_error($ldap->get_error());
408 gosa_log("Mount container '$mountsdn' has been created");
409 }
411 # remove deleted mounts from the container
412 foreach ($this->mounts_to_remove as $entry) {
413 $mount=$this->returnMountEntry($entry);
414 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
416 $ldap->cat($mountdn);
417 $attrs = $ldap->fetch();
419 if (count($attrs) != 0) {
420 $ldap->rmdir($mountdn);
421 show_ldap_error($ldap->get_error());
422 gosa_log("Mount object '".$mountdn."' has been removed");
423 }
424 }
426 # add new mounts to the container
427 foreach ($this->mounts_to_add as $entry) {
429 $mount=$this->returnMountEntry($entry);
430 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
431 $ldap->cd($mountdn);
432 $ldap->add($mount);
433 show_ldap_error($ldap->get_error());
434 gosa_log("Mount object '".$mountdn."' has been added");
435 }
436 }
438 function get_share_type($share) {
439 $tmp = split("\|", $share);
440 return $tmp[2];
441 }
444 function returnMountEntry($entry) {
445 $item = split("\|", $entry);
446 $name = $item[0];
447 $description = $item[1];
448 $type = $item[2];
449 $charset = $item[3];
450 $path = $item[4];
451 $options = $item[5];
453 switch ($type) {
454 case "netatalk" : {
455 $mount = array(
456 "mountDirectory" => "/Network/Servers/",
457 "mountOption" => array(
458 "net",
459 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
460 ),
461 "mountType" => "url",
462 "objectClass" => "mount",
463 "cn" => $this->cn .":/".$name
464 );
465 break;
466 }
467 case "NFS" : {
468 $mount = array(
469 "mountDirectory" => "/Network/Servers/",
470 "mountOption" => "net",
471 "mountType" => "nfs",
472 "objectClass" => "mount",
473 "cn" => $this->cn .":".$path
474 );
475 break;
476 }
477 default : {
478 continue;
479 }
480 }
481 return $mount;
482 }
484 }
485 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
486 ?>