Code

Updated password method.
[gosa.git] / gosa-plugins / heimdal / admin / systems / services / kerberos / class_password-methods-MIT.inc
index 835decf1c19c6d5f8e1c66686854b939842c945a..fbfd17c91ebc41b62c4ae7fa4991dfe6149d027c 100644 (file)
 class passwordMethodMIT extends passwordMethod
 {
 
-  var $dn          = "new";     // DN of the current object
-  var $parent_dn   = "new";     // parents DN
-  var $is_account  = FALSE;     // This is TRUE if this object already has a krb extension
-  var $server_list = array();   // A list with all configured servers
-  var $map         = array();   // Mapping array, maps SERVER-REALM, REALM-SERVER ...
+  var $dn             = "new";   // DN of the current object
+  var $parent_dn      = "new";   // parents DN
+  var $is_account     = FALSE;   // This is TRUE if this object already has a krb extension
+  var $server_list    = array(); // A list with all configured servers
+  var $map            = array(); // Mapping array, maps SERVER-REALM, REALM-SERVER ...
 
-  var $goKrbRealm  = "";        // The realm name this principal belongs to
-  var $principal   = "";        // The principals name (e.g. user@MY-DOMAIN.SYS)
-  var $is_new      = TRUE;      // Is TRUE if principal is new 
+  var $goKrbRealm     = "";      // The realm name this principal belongs to
+  var $principal      = "";      // The principals name (e.g. user@MY-DOMAIN.SYS)
+  var $is_new         = TRUE;    // Is TRUE if principal is new 
 
-  var $si_error        = FALSE;
-  var $si_error_msg    = "";
+  var $si_error       = FALSE;   // TRUE is daemon communication failed
+  var $si_error_msg   = "";      // The last error message if above attribute is TRUE.
 
   var $values = array(
       "PRINC_EXPIRE_TIME",      // Expiry date of this principal
@@ -42,7 +42,13 @@ class passwordMethodMIT extends passwordMethod
       "MAX_RENEWABLE_LIFE",     // Max ticket lifetime when renewed
       "POLICY");                // The policy used by this principal
 
-  var $used_flags = 0;          // Flags, see below
+  var $PRINC_EXPIRE_TIME        = 0;
+  var $PW_EXPIRATION            = 0;
+  var $PRINC_EXPIRE_TIME_clear  = TRUE;
+  var $PW_EXPIRATION_clear      = TRUE;
+  var $MAX_LIFE                 = 36000;
+  var $MAX_RENEWABLE_LIFE       = 604800;
+  var $MASK                     = 0;
 
   var $flags = array(
       "DISALLOW_POSTDATED"    =>0x00000001 ,  // Pohibit postdated tickets
@@ -61,6 +67,8 @@ class passwordMethodMIT extends passwordMethod
       "PWCHANGE_SERVICE"      =>0x00002000 ,  // Password change service
       "SUPPORT_DESMD5"        =>0x00004000 ,  // ?
       "NEW_PRINC"             =>0x00008000 ); // ?
+  
+  var $used_flags = 128;        // Flags, see below
 
   var $readonly = array(
       "FAIL_AUTH_COUNT",        // The number of failed logins 
@@ -70,18 +78,6 @@ class passwordMethodMIT extends passwordMethod
       "LAST_SUCCESS",           // Last successful login 
       "MOD_DATE");              // Last modification time
 
-
-  var $POLICY                   = "";
-  var $POLICIES                 = array(); // Policies provided by the corrently selected realm/server
-
-  var $PRINC_EXPIRE_TIME        = 0;
-  var $PW_EXPIRATION            = 0;
-  var $PRINC_EXPIRE_TIME_clear  = TRUE;
-  var $PW_EXPIRATION_clear      = TRUE;
-  var $MAX_LIFE                 = 0;
-  var $MAX_RENEWABLE_LIFE       = 0;
-  var $MASK                     = "";
-
   var $FAIL_AUTH_COUNT          = 0;
   var $KVNO                     = "";
   var $LAST_FAILED              = 0;
@@ -89,7 +85,11 @@ class passwordMethodMIT extends passwordMethod
   var $LAST_SUCCESS             = 0;
   var $MOD_DATE                 = 0;
 
-  function __construct(&$config,$dn = "new")  
+  var $POLICY                   = "";
+  var $POLICIES                 = array(); // Policies provided by the corrently selected realm/server
+
+
+  public function __construct(&$config,$dn = "new")  
   {
     $this->config= $config;
     $this->parent_dn = $dn;
@@ -101,12 +101,15 @@ class passwordMethodMIT extends passwordMethod
       return;
     }  
 
-    /* Keep cache until we try to configure a principal 
+    /* Keep the cached valued and skip loading principals 
+        from si until this method gets configured.
      */
+    $skip_si_access = TRUE;
     if($dn != "new" && $dn != ""){
       session::un_set("MIT_CACHE");
       session::un_set("MIT_PRINCIPAL_CACHE");
       session::un_set("MIT_POLICY_CACHE");
+      $skip_si_access = FALSE;
     }
 
     /* Get a list of all kerberos servers, defined in ldap
@@ -120,7 +123,11 @@ class passwordMethodMIT extends passwordMethod
       if(!isset($attrs['macAddress'][0])) continue;
       if(!isset($attrs['description'][0])) $attrs['description'][0] ="";
 
-      $principals = $this->load_principals_for_server($attrs['macAddress'][0]);
+      if($skip_si_access){
+        $principals = array();
+      }else{
+        $principals = $this->load_principals_for_server($attrs['macAddress'][0]);
+      }
 
       /* Create Realm/Server/Principal mapping.
        */
@@ -147,14 +154,21 @@ class passwordMethodMIT extends passwordMethod
           "cn"         => $attrs['cn'][0]);
     }
 
-    /* Load object data from ldap && initialize this class  
+    /*  If this methods is initialized with a valid object dn then 
+         load the object data from ldap and the SI daemon && initialize this class.
      */
     $this->is_new = TRUE;
-    if($dn != "new" && $dn != ""){
+    if(!$skip_si_access){
       $ldap = $this->config->get_ldap_link();
       $ldap->cd($dn);
       $ldap->cat($dn);
       $this->attrs = $ldap->fetch();
+
+      /* Set initial pwd hash which take effect if no password method was set yet.  
+         Will be overwritten by the following lines, if the user has already a valid principal.
+       */
+      $this->principal = $this->attrs['uid'][0]."@".$this->goKrbRealm;
+
       if(isset($this->attrs['userPassword']) && preg_match("/^\{".$this->get_hash_name()."\}/",$this->attrs['userPassword'][0])){
 
         /* Extract principal name out of userPassword attribute 
@@ -182,7 +196,8 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  /*! \brief  Load this plugin with the values of the given principal
+  /*! \brief  Load a specific principal from the si daemon 
+               and initialize this plugin with it.
     @param  String  The macAddress of the kerberos server.
     @param  String  The name of the principal to load.
    */
@@ -193,17 +208,16 @@ class passwordMethodMIT extends passwordMethod
     $tmp = $o->krb5_get_principal($server,$name);
   
     if($o->is_error()){
-
       $this->si_error     = TRUE;
-      $this->si_error_msg = $o->get_error(); 
+      $this->si_error_msg = sprintf(_("While loading principal for '%s', from server '%s'."),$name,$server).":&nbsp;<br>".$o->get_error(); 
       msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);      
     }else{
 
       /* Load flags 
        */
-     if(isset($tmp['ATTRIBUTES'])){
-       $this->used_flags = $tmp['ATTRIBUTES'];
-     }
+      if(isset($tmp['ATTRIBUTES'])){
+        $this->used_flags = $tmp['ATTRIBUTES'];
+      }
 
       /* Load readonly attributes 
        */
@@ -234,9 +248,10 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  /*! \brief get list of all configured principals
-    for a given server. 
-    The results will cached.  
+  /*! \brief  Get the list of all configured principals for a given server.
+      @param  String The servers mac address.
+      @return Array A list with all principals
+      The results will cached.  
    */
   public function load_principals_for_server($server)
   {
@@ -249,7 +264,7 @@ class passwordMethodMIT extends passwordMethod
       $tmp = $o->krb5_list_principals($server);
       if($o->is_error()){
         $this->si_error     = TRUE;
-        $this->si_error_msg = $o->get_error(); 
+        $this->si_error_msg = sprintf(_("While loading principals from server '%s'."),$server).":&nbsp;<br>".$o->get_error(); 
         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
         return(array());
       }else{
@@ -276,7 +291,7 @@ class passwordMethodMIT extends passwordMethod
       $tmp = $o->krb5_list_policies($server);
       if($o->is_error()){
         $this->si_error     = TRUE;
-        $this->si_error_msg = $o->get_error();
+        $this->si_error_msg = sprintf(_("While loading policies from server '%s'."),$server).":&nbsp;<br>".$o->get_error(); 
         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
         return(array());
       }else{
@@ -297,7 +312,7 @@ class passwordMethodMIT extends passwordMethod
     This is the case if there is a si server running and at least one server configured.
     kerberos support. 
    */ 
-  function is_available()
+  public function is_available()
   {
     $o = new gosaSupportDaemon(FALSE);
     if(count($this->server_list) && $o->connect()){
@@ -307,7 +322,11 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  function generate_hash($pwd)
+  /*! \brief Create the password hash. In this case: {kerberos/sasl}name@RELAM 
+      @param  String  The password -in this case unusued.             
+      @return String  The generated hash
+   */
+  public function generate_hash($pwd = "")
   {
     $mode= "kerberos";
     if (isset($this->config->current['KRBSASL']) && preg_match('/^true$/i', $this->config->current['KRBSASL'])){
@@ -317,7 +336,9 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  function remove_from_parent()
+  /*! \brief  Removes this principal.
+   */
+  public function remove_from_parent()
   {
     if(!empty($this->principal) && $this->goKrbRealm){
       $server = $this->map['REALM_SERVER'][$this->goKrbRealm];
@@ -331,7 +352,10 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  function set_password($password)
+  /*! \brief  Set a new password for this principal 
+      @param  String The new password.
+   */
+  public function set_password($password)
   {
     if(!empty($this->principal) && $this->goKrbRealm){
       $server = $this->map['REALM_SERVER'][$this->goKrbRealm];
@@ -343,7 +367,10 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  function get_hash_name()
+  /*! \brief  Return the hash name of this mehtod,  e.g. to identify methods.
+      @return String  The hash used by this method.
+    */
+  public function get_hash_name()
   {
     $mode= "kerberos";
     if (isset($this->config->current['KRBSASL']) && preg_match('/^true$/i', $this->config->current['KRBSASL'])){
@@ -353,13 +380,28 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  function is_configurable()
+  /*! \brief  Returns TRUE if this method is configurable else FALSE
+      @retrun Boolena TRUE if configurable, else FALSE.
+   */
+  public function is_configurable()
   {
     return TRUE;
   }
 
 
-  function configure()
+  /*! \brief  Additional info displayed in the users password method drop down.
+      @retunr String  Additional password method info.
+   */
+  public function get_description()
+  {
+    return(_("Daemon based"));
+  }
+
+
+  /*! \brief  Display a HTML based configuration dialog for this plugin
+      @return String  HTML.
+   */
+  public function configure()
   {
     $this->save_object();
 
@@ -405,22 +447,17 @@ class passwordMethodMIT extends passwordMethod
       }
     }
 
-
     $smarty = get_smarty();
-
     $smarty->assign("si_error",$this->si_error);
     $smarty->assign("si_error_msg",$this->si_error_msg);
-  
     $smarty->assign("years",$years);
     $smarty->assign("month",$month);
     $smarty->assign("days",$days);
     $smarty->assign("hours",$hours);
     $smarty->assign("minutes",$minutes);
-
     $smarty->assign("server_list",$this->server_list);
     $smarty->assign("POLICY"  ,$this->POLICY);
     $smarty->assign("goKrbRealm" , $this->goKrbRealm);
-
     $server_name = $this->map['REALM_SERVER'][$this->goKrbRealm];
     $server_mac  = $this->server_list[$server_name]['macAddress'];
     $this->POLICIES = $this->load_policies_for_server($server_mac);
@@ -438,9 +475,6 @@ class passwordMethodMIT extends passwordMethod
 
     $date_values = array("PRINC_EXPIRE_TIME","PW_EXPIRATION");
     foreach($date_values as $date_val){
-
-
-
       $clear = $date_val."_clear";
       $smarty->assign($date_val."_clear",$this->$clear);
       $smarty->assign($date_val."_y",date("Y",$this->$date_val));
@@ -450,12 +484,13 @@ class passwordMethodMIT extends passwordMethod
       $smarty->assign($date_val."_i",date("i",$this->$date_val));
     }
 
-
-    return($smarty->fetch(get_template_path("pwd_kerberos_mit.tpl")));
+    return($smarty->fetch(get_template_path("pwd_kerberos_mit.tpl",TRUE,dirname(__FILE__))));
   }
 
 
-  function save_object()
+  /*! \brief  Saves all relevant HTML posts for this plugin
+   */
+  public function save_object()
   {
     /* If the communication with the si server failed, 
         you are able to retry to connect to the server.
@@ -510,31 +545,32 @@ class passwordMethodMIT extends passwordMethod
   }
 
 
-  function check()
+  /*! \brief  Checks the values specified in the configuration dialog. 
+      @return Array Containing all error messages.
+   */
+  public function check()
   {
     $message = array();
-    echo "Checks missing";
-    return($message);
-  }
-
 
-  function chk_times($str)
-  {
-    if(preg_match("/^([0-9]){12,12}[a-z]$/i",$str)){
-      return(true);
+    if(!preg_match("/^[0-9]*$/",$this->MAX_LIFE)){
+      $message[] = msgPool::invalid(_("Ticket max life"),$this->MAX_LIFE,"/[0-9]/");
+    }
+    if(!preg_match("/^[0-9]*$/",$this->MAX_RENEWABLE_LIFE)){
+      $message[] = msgPool::invalid(_("Ticket max renew"),$this->MAX_RENEWABLE_LIFE,"/[0-9]/");
     }
-    return(false);
+    return($message);
   }
 
 
-  function save($dn)
+  /*! \brief Saves changes back to the SI daemon.
+   */
+  public function save($dn)
   {
-    $realm = $this->config->data['SERVERS']['KERBEROS']['REALM'];
-
     $ldap = $this->config->get_ldap_link();
     $ldap->cd($dn);
     $ldap->cat($dn,array('uid'));
     $attrs = $ldap->fetch();
+
     if(isset($attrs['uid'][0])){
 
       /* Get servers mac */
@@ -564,7 +600,7 @@ class passwordMethodMIT extends passwordMethod
 
       /* Set date values 
        */
-      $date_values = array("PW_EXPIRATION","PRINC_EXPIRE_TIME");
+     $date_values = array("PW_EXPIRATION","PRINC_EXPIRE_TIME");
       foreach($date_values as $value){
         $clear = $value."_clear";
         if($this->$clear){