Code

Fixed acls for system management
[gosa.git] / plugins / admin / systems / class_goMailServer.inc
index cdab67ce1b1e651d019d5ab147693405cec7f894..f72af1d2b7eb3a1e485bcf93cc07e631ce12daae 100644 (file)
@@ -10,12 +10,10 @@ class goMailServer extends plugin{
   var $objectclasses    = array("goMailServer");
 
   /* This class can't be assigned twice so it conflicts with itsself */
-  var $conflicts        = array("goMailServer");
-
   var $DisplayName      = "";
   var $dn               = NULL;
   var $StatusFlag       = "goMailServerStatus";
-  var $attributes       = array("goMailServerStatus","description","postfixHeaderSizeLimit",
+  var $attributes       = array("description","postfixHeaderSizeLimit",
       "postfixMailboxSizeLimit","postfixMessageSizeLimit",
       "postfixMyDestinations","postfixMyDomain","postfixMyhostname",
       "postfixMyNetworks","postfixRelayhost","postfixTransportTable",
@@ -37,17 +35,17 @@ class goMailServer extends plugin{
   var $RestrictionFilters               = array();
   var $TransportProtocols               = array(); 
   var $Actions                          = array();
-
+  var $cn                               = "";
+  var $conflicts                        = array("goMailServer","kolab");
 
   function goMailServer($config,$dn)
   {
     plugin::plugin($config,$dn);
-    $this->DisplayName = _("Postfix service");
+    $this->DisplayName = _("Mail service (SMTP)");
 
     $this->Actions = array( SERVICE_STOPPED=>SERVICE_STOPPED,
                             SERVICE_STARTED => SERVICE_STARTED,
-                            SERVICE_RESETTED=>SERVICE_RESETTED, 
-                            "mailqueue"=>"mailqueue");
+                            SERVICE_RESTARTED=>SERVICE_RESTARTED); 
 
     /* Fill  RestrictionFilters   TransportProtocols  from external hooks */
     $str = $this->config->data['TABS']['SERVERSERVICE'];
@@ -80,6 +78,7 @@ class goMailServer extends plugin{
 
     /* Get postfix my networks */
     $this->postfixMyNetworks = array();
+    $tmp = array();
     if(isset($this->attrs['postfixMyNetworks'][0])){
       $tmp = split(",",$this->attrs['postfixMyNetworks'][0]);
       foreach($tmp as $str){
@@ -107,22 +106,29 @@ class goMailServer extends plugin{
 
 
     /* Get transport tables */
+    $tmp = array();
     $this->postfixTransportTable = array();
     if(isset($this->attrs['postfixTransportTable'])){
       $tmp = array();
       unset($this->attrs['postfixTransportTable']['count']);
-      foreach($this->attrs['postfixTransportTable'] as $entry){ 
-        $nr  = preg_replace("/:.*$/","",$entry);
-        $rest= trim(preg_replace("/^[^:]+:/","",$entry));
-        $src = preg_replace("/ .*$/","",$rest);
-        $rest= preg_replace("/^[^ ]+ /","",$rest);
-        $dst = preg_replace("/^.*:/","",$rest); 
-        $prt = preg_replace("/:.*$/","",$rest);
-      
-        $tmp[$nr]['src'] = $src;
-        $tmp[$nr]['dst'] = $dst;
-        $tmp[$nr]['prt'] = $prt;
-        
+      foreach($this->attrs['postfixTransportTable'] as $entry){
+
+        //0: offshore.vip.ms-europa.lhsystems.com smtp:172.28.0.2
+
+        $Number   = preg_replace('/^([^:]+):.*$/', '\\1', $entry);
+        $Rest     = trim(preg_replace("/^[0-9]*:/","",$entry));
+
+        $Protocol_Destination = preg_replace("/^.*\ /","",$Rest);
+        $Source               = preg_replace("/\ .*$/","",$Rest);
+
+        $Protocol     = preg_replace ('/^([^:]+):.*$/', '\\1' ,trim($Protocol_Destination));
+        $Destination  = preg_replace ('/^[^:]+:(.*)$/', '\\1'    ,trim($Protocol_Destination));
+
+        $Destination  = preg_replace ("/[\[\]]/","",$Destination);
+
+        $tmp[$Number]['src'] = $Source;
+        $tmp[$Number]['dst'] = $Destination;
+        $tmp[$Number]['prt'] = $Protocol;
       }
       ksort($tmp);
       foreach($tmp as $entry){
@@ -132,6 +138,7 @@ class goMailServer extends plugin{
    
 
     /* Get sender restrictions */
+    $tmp = array();
     $this->postfixSenderRestrictions = array();
     if(isset($this->attrs['postfixSenderRestrictions'])){
       unset($this->attrs['postfixSenderRestrictions']['count']);
@@ -155,6 +162,7 @@ class goMailServer extends plugin{
 
  
     /* Get sender restrictions */
+    $tmp = array();
     $this->postfixRecipientRestrictions = array();
     if(isset($this->attrs['postfixRecipientRestrictions'])){
       unset($this->attrs['postfixRecipientRestrictions']['count']);
@@ -246,7 +254,7 @@ class goMailServer extends plugin{
       $this->postfixMyNetworks[base64_encode($str)] = $str;
     }
 
-    if((isset($_POST['DelpostfixMyNetworks'])) && (count($_POST['Select_postfixMyNetworks']))){
+    if((isset($_POST['DelpostfixMyNetworks'])) && isset($_POST['Select_postfixMyNetworks']) &&(count($_POST['Select_postfixMyNetworks']))){
       foreach($_POST['Select_postfixMyNetworks'] as $str ){
         unset($this->postfixMyNetworks[$str]);
       }
@@ -259,7 +267,7 @@ class goMailServer extends plugin{
       $this->postfixMyDestinations[base64_encode($str)] = $str;
     }
 
-    if((isset($_POST['DelpostfixMyDestinations'])) && (count($_POST['Select_postfixMyDestinations']))){
+    if((isset($_POST['DelpostfixMyDestinations'])) && isset($_POST['Select_postfixMyDestinations']) &&(count($_POST['Select_postfixMyDestinations']))){
       foreach($_POST['Select_postfixMyDestinations'] as $str ){
         unset($this->postfixMyDestinations[$str]);
       }
@@ -338,7 +346,7 @@ class goMailServer extends plugin{
 
     /* Create divList for translation tables */
     $divTranslation = new divSelectBox("TransportProtocols");
-    $divTranslation->SetHeight(110);
+    $divTranslation->SetHeight(90);
     foreach($this->postfixTransportTable as $key => $entry){
       $img = "";
 
@@ -365,7 +373,7 @@ class goMailServer extends plugin{
 
     /* Create divList for sender restrictions */
     $DivSenderRestrict = new divSelectBox("postfixSenderRestrictions");
-    $DivSenderRestrict->SetHeight(110);
+    $DivSenderRestrict->SetHeight(90);
     foreach($this->postfixSenderRestrictions as $key => $entry){
       $img ="";
 
@@ -393,7 +401,8 @@ class goMailServer extends plugin{
 
     /* Create divList for translation tables */
     $DivRecipientRestrict = new divSelectBox("postfixRecipientRestrictions");
-    $DivRecipientRestrict->SetHeight(110);
+    $DivRecipientRestrict->SetHeight(90);
+
     foreach($this->postfixRecipientRestrictions as $key => $entry){
       $img = "";
       if($key != 0){
@@ -425,6 +434,7 @@ class goMailServer extends plugin{
 
 
     $smarty->assign("is_new",                       $this->dn);
+    $smarty->assign("is_acc",                       $this->initially_was_account);
     $smarty->assign("TransportProtocols",           $this->TransportProtocols);
     $smarty->assign("Actions",                      $this->Actions);
     $smarty->assign("RestrictionFilters",           $this->RestrictionFilters);
@@ -472,12 +482,13 @@ class goMailServer extends plugin{
   /* Return list entry */
   function getListEntry()
   {
+    $this->updateStatusState();
     $flag = $this->StatusFlag;
     $fields['Status']     = $this->$flag;
-    $fields['Message']    = _("Postfix"). " - ".$this->postfixMyhostname;
+    $fields['Message']    = _("Postfix");
     $fields['AllowStart'] = true;
     $fields['AllowStop']  = true;
-    $fields['AllowReset'] = true;
+    $fields['AllowRestart'] = true;
     $fields['AllowRemove']= true;
     $fields['AllowEdit']  = true;
     return($fields);
@@ -487,6 +498,13 @@ class goMailServer extends plugin{
   function remove_from_parent()
   {
     plugin::remove_from_parent();
+
+    /* Remove status flag, it is not a memeber of 
+        this->attributes, so ensure that it is deleted too */
+    if(!empty($this->StatusFlag)){
+      $this->attrs[$this->StatusFlag] = array();
+    }
+
     /* Check if this is a new entry ... add/modify */
     $ldap = $this->config->get_ldap_link();
     $ldap->cat($this->dn,array("objectClass"));
@@ -497,12 +515,16 @@ class goMailServer extends plugin{
       $ldap->cd($this->dn);
       $ldap->add($this->attrs);
     }
-    show_ldap_error($ldap->get_error());
+    show_ldap_error($ldap->get_error(), sprintf(_("Removing server services/mail with dn '%s' failed."),$this->dn));
+    $this->handle_post_events("remove");
   }
 
 
   function save()
   {
+    $this->postfixMyDomain   = preg_replace("/^[^\.]+\./","",$this->postfixMyhostname);
+    $this->postfixMyhostname = preg_replace("/\..*$/","",$this->postfixMyhostname);
+
     plugin::save();
 
     /* Fix transport table*/
@@ -545,14 +567,11 @@ class goMailServer extends plugin{
       foreach($this->postfixMyNetworks as $entry){
         $this->attrs['postfixMyNetworks'] .=$entry.",";
       }
+      $this->attrs['postfixMyNetworks'] = preg_replace("/,$/","",$this->attrs['postfixMyNetworks']);
     }else{
       $this->attrs['postfixMyNetworks']  = array();
     }
 
-    $this->attrs['postfixMyNetworks'] = preg_replace("/,$/","",$this->attrs['postfixMyNetworks']);
-
-    $this->attrs['postfixMyhostname'] = preg_replace("/\..*$/","",$this->postfixMyhostname);
-    $this->attrs['postfixMyDomain']   = preg_replace("/^[^\.]+\./","",$this->postfixMyhostname);
  
     /* Check if this is a new entry ... add/modify */
     $ldap = $this->config->get_ldap_link();
@@ -564,33 +583,36 @@ class goMailServer extends plugin{
       $ldap->cd($this->dn);
       $ldap->add($this->attrs);
     }
-    show_ldap_error($ldap->get_error());
+    show_ldap_error($ldap->get_error(), sprintf(_("Saving server services/mail with dn '%s' failed."),$this->dn));
+    if($this->initially_was_account){
+      $this->handle_post_events("modify");
+    }else{
+      $this->handle_post_events("add");
+    }
+
   }
 
 
   /* Directly save new status flag */
   function setStatus($value)
   {
+    if($value == "none") return;
+    if(!$this->initially_was_account) return;
     $ldap = $this->config->get_ldap_link();
     $ldap->cd($this->dn);
     $ldap->cat($this->dn,array("objectClass"));
-
     if($ldap->count()){
 
-      $attrs =array();
       $tmp = $ldap->fetch();
       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
         $attrs['objectClass'][] = $tmp['objectClass'][$i];
       }
-      if(!in_array("goMailServer",$attrs['objectClass'])){
-        $attrs['objectClass'][] = "goMailServer";
-      }
-
       $flag = $this->StatusFlag;
       $attrs[$flag] = $value;
       $this->$flag = $value;
       $ldap->modify($attrs);
-      show_ldap_error($ldap->get_error());
+      show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/mail with dn '%s' failed."),$this->dn));
+      $this->action_hook();
     }
   }
 
@@ -598,7 +620,7 @@ class goMailServer extends plugin{
   function check()
   { 
     $message =plugin::check();
-    
+  
     if(!is_numeric($this->postfixHeaderSizeLimit)){
       $message[] = _("Please specify a numeric value for header size limit.");
     }
@@ -682,11 +704,99 @@ class goMailServer extends plugin{
     return($ret);
   }
 
+  
+  function action_hook($add_attrs= array())
+  {
+    /* Find postcreate entries for this class */
+    $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
+    if ($command == "" && isset($this->config->data['TABS'])){
+      $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
+    }
+    if ($command != ""){
+      /* Walk through attribute list */
+      foreach ($this->attributes as $attr){
+        if (!is_array($this->$attr)){
+          $command= preg_replace("/%$attr/", $this->$attr, $command);
+        }
+      }
+      $command= preg_replace("/%dn/", $this->dn, $command);
+      /* Additional attributes */
+      foreach ($add_attrs as $name => $value){
+        $command= preg_replace("/%$name/", $value, $command);
+      }
+
+      /* If there are still some %.. in our command, try to fill these with some other class vars */
+      if(preg_match("/%/",$command)){
+        $attrs = get_object_vars($this);
+        foreach($attrs as $name => $value){
+          if(!is_string($value)) continue;
+          $command= preg_replace("/%$name/", $value, $command);
+        }
+      }
+
+      if (check_command($command)){
+        @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
+            $command, "Execute");
+
+        exec($command);
+      } else {
+        $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
+        print_red ($message);
+      }
+    }
+  }
+
+
+  /* Get updates for status flag */
+  function updateStatusState()
+  {
+    if(empty($this->StatusFlag)) return;
+
+    $attrs = array();
+    $flag = $this->StatusFlag;
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->cn);
+    $ldap->cat($this->dn,array($flag));
+    if($ldap->count()){
+      $attrs = $ldap->fetch();
+    }
+    if(isset($attrs[$flag][0])){
+      $this->$flag = $attrs[$flag][0];
+    }
+  }
 
   function save_object()
   {
     plugin::save_object();  
   }
+
+
+
+  /* Return plugin informations for acl handling */
+  function plInfo()
+  {
+    return (array(
+          "plShortName"   => _("Mail"),
+          "plDescription" => _("Mail service"),
+          "plSelfModify"  => FALSE,
+          "plDepends"     => array(),
+          "plPriority"    => 0,
+          "plSection"     => array("administration"),
+          "plCategory"    => array("server"),
+
+          "plProvidedAcls"=> array(
+            "description"                 => _("Description"), 
+            "postfixHeaderSizeLimit"      => _("Header size limit"), 
+            "postfixMailboxSizeLimit"     => _("Max mailbox size"), 
+            "postfixMessageSizeLimit"     => _("Max message size"), 
+            "postfixMyDestinations"       => _("Domains to accept mail for"), 
+            "postfixMyNetworks"           => _("Local networks"), 
+            "postfixRelayhost"            => _("Relay host"), 
+            "postfixTransportTable"       => _("Transport table"), 
+            "postfixSenderRestrictions"   => _("Restrictions for sender"), 
+            "postfixRecipientRestrictions"=> _("Restrictions for recipient"))
+            ));
+  }
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>