r6149: Fixes bugs #2498 and 2484.
[samba.git] / source / passdb / pdb_get_set.c
index 37530d0e4672a721bbe6affabf9cda49f9f290b3..99afac133acbc3a729a97908284a9ce0d4133863 100644 (file)
@@ -28,7 +28,7 @@
 #define DBGC_CLASS DBGC_PASSDB
 
 /**
- * @todo Redefine this to NULL, but this changes the API becouse
+ * @todo Redefine this to NULL, but this changes the API because
  *       much of samba assumes that the pdb_get...() funtions 
  *       return pstrings.  (ie not null-pointers).
  *       See also pdb_fill_default_sam().
@@ -37,7 +37,7 @@
 #define PDB_NOT_QUITE_NULL ""
 
 /*********************************************************************
- Collection of get...() functions for SAM_ACCOUNT_INFO.
+ Collection of get...() functions for SAM_ACCOUNT.
  ********************************************************************/
 
 uint16 pdb_get_acct_ctrl (const SAM_ACCOUNT *sampass)
@@ -72,6 +72,14 @@ time_t pdb_get_kickoff_time (const SAM_ACCOUNT *sampass)
                return (-1);
 }
 
+time_t pdb_get_bad_password_time (const SAM_ACCOUNT *sampass)
+{
+       if (sampass)
+               return (sampass->private.bad_password_time);
+       else
+               return (-1);
+}
+
 time_t pdb_get_pass_last_set_time (const SAM_ACCOUNT *sampass)
 {
        if (sampass)
@@ -142,6 +150,19 @@ const uint8* pdb_get_lanman_passwd (const SAM_ACCOUNT *sampass)
                return (NULL);
 }
 
+const uint8* pdb_get_pw_history (const SAM_ACCOUNT *sampass, uint32 *current_hist_len)
+{
+       if (sampass) {
+               SMB_ASSERT((!sampass->private.nt_pw_his.data) 
+                  || ((sampass->private.nt_pw_his.length % PW_HISTORY_ENTRY_LEN) == 0));
+               *current_hist_len = sampass->private.nt_pw_his.length / PW_HISTORY_ENTRY_LEN;
+               return ((uint8*)sampass->private.nt_pw_his.data);
+       } else {
+               *current_hist_len = 0;
+               return (NULL);
+       }
+}
+
 /* Return the plaintext password if known.  Most of the time
    it isn't, so don't assume anything magic about this function.
    
@@ -151,7 +172,7 @@ const uint8* pdb_get_lanman_passwd (const SAM_ACCOUNT *sampass)
 const char* pdb_get_plaintext_passwd (const SAM_ACCOUNT *sampass)
 {
        if (sampass) {
-               return ((char*)sampass->private.plaintext_pw.data);
+               return (sampass->private.plaintext_pw);
        }
        else
                return (NULL);
@@ -172,55 +193,34 @@ const DOM_SID *pdb_get_group_sid(const SAM_ACCOUNT *sampass)
                return (NULL);
 }      
 
-uint32 pdb_get_user_rid (const SAM_ACCOUNT *sampass)
-{
-       uint32 u_rid;
-
-       if (sampass)
-               if (sid_peek_check_rid(get_global_sam_sid(), pdb_get_user_sid(sampass),&u_rid))
-                       return u_rid;
-       
-       return (0);
-}
-
-uint32 pdb_get_group_rid (const SAM_ACCOUNT *sampass)
-{
-       uint32 g_rid;
-
-       if (sampass)
-               if (sid_peek_check_rid(get_global_sam_sid(), pdb_get_group_sid(sampass),&g_rid))
-                       return g_rid;
-       return (0);
-}
-
 /**
  * Get flags showing what is initalised in the SAM_ACCOUNT
  * @param sampass the SAM_ACCOUNT in question
  * @return the flags indicating the members initialised in the struct.
  **/
  
-uint32 pdb_get_init_flag (const SAM_ACCOUNT *sampass)
+enum pdb_value_state pdb_get_init_flags (const SAM_ACCOUNT *sampass, enum pdb_elements element)
 {
-        if (sampass)
-               return sampass->private.init_flag;
-       else 
-                return FLAG_SAM_UNINIT;
-}
+       enum pdb_value_state ret = PDB_DEFAULT;
+       
+        if (!sampass || !sampass->private.change_flags || !sampass->private.set_flags)
+               return ret;
+               
+        if (bitmap_query(sampass->private.set_flags, element)) {
+               DEBUG(11, ("element %d: SET\n", element)); 
+               ret = PDB_SET;
+       }
+               
+        if (bitmap_query(sampass->private.change_flags, element)) {
+               DEBUG(11, ("element %d: CHANGED\n", element)); 
+               ret = PDB_CHANGED;
+       }
 
-uid_t pdb_get_uid (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.uid);
-       else
-               return (-1);
-}
+       if (ret == PDB_DEFAULT) {
+               DEBUG(11, ("element %d: DEFAULT\n", element)); 
+       }
 
-gid_t pdb_get_gid (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.gid);
-       else
-               return (-1);
+        return ret;
 }
 
 const char* pdb_get_username (const SAM_ACCOUNT *sampass)
@@ -271,7 +271,7 @@ const char* pdb_get_unix_homedir (const SAM_ACCOUNT *sampass)
                return (NULL);
 }
 
-const char* pdb_get_dirdrive (const SAM_ACCOUNT *sampass)
+const char* pdb_get_dir_drive (const SAM_ACCOUNT *sampass)
 {
        if (sampass)
                return (sampass->private.dir_drive);
@@ -327,23 +327,23 @@ const char* pdb_get_munged_dial (const SAM_ACCOUNT *sampass)
                return (NULL);
 }
 
-uint32 pdb_get_unknown3 (const SAM_ACCOUNT *sampass)
+uint16 pdb_get_bad_password_count(const SAM_ACCOUNT *sampass)
 {
        if (sampass)
-               return (sampass->private.unknown_3);
+               return (sampass->private.bad_password_count);
        else
-               return (-1);
+               return 0;
 }
 
-uint32 pdb_get_unknown5 (const SAM_ACCOUNT *sampass)
+uint16 pdb_get_logon_count(const SAM_ACCOUNT *sampass)
 {
        if (sampass)
-               return (sampass->private.unknown_5);
+               return (sampass->private.logon_count);
        else
-               return (-1);
+               return 0;
 }
 
-uint32 pdb_get_unknown6 (const SAM_ACCOUNT *sampass)
+uint32 pdb_get_unknown_6 (const SAM_ACCOUNT *sampass)
 {
        if (sampass)
                return (sampass->private.unknown_6);
@@ -351,114 +351,117 @@ uint32 pdb_get_unknown6 (const SAM_ACCOUNT *sampass)
                return (-1);
 }
 
+void *pdb_get_backend_private_data (const SAM_ACCOUNT *sampass, const struct pdb_methods *my_methods)
+{
+       if (sampass && my_methods == sampass->private.backend_private_methods)
+               return sampass->private.backend_private_data;
+       else
+               return NULL;
+}
+
 /*********************************************************************
- Collection of set...() functions for SAM_ACCOUNT_INFO.
+ Collection of set...() functions for SAM_ACCOUNT.
  ********************************************************************/
 
-BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
+BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 acct_ctrl, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
                
-       if (sampass) {
-               sampass->private.acct_ctrl = flags;
-               return True;
-       }
-       
-       return False;
+       sampass->private.acct_ctrl = acct_ctrl;
+
+       return pdb_set_init_flags(sampass, PDB_ACCTCTRL, flag);
 }
 
-BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
+BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.logon_time = mytime;
 
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_LOGONTIME); 
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_LOGONTIME, flag);
 }
 
-BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
+BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.logoff_time = mytime;
 
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_LOGOFFTIME); 
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_LOGOFFTIME, flag);
 }
 
-BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
+BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.kickoff_time = mytime;
 
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_KICKOFFTIME); 
+       return pdb_set_init_flags(sampass, PDB_KICKOFFTIME, flag);
+}
 
-       return True;
+BOOL pdb_set_bad_password_time (SAM_ACCOUNT *sampass, time_t mytime, 
+                               enum pdb_value_state flag)
+{
+       if (!sampass)
+               return False;
+
+       sampass->private.bad_password_time = mytime;
+
+       return pdb_set_init_flags(sampass, PDB_BAD_PASSWORD_TIME, flag);
 }
 
-BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
+BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.pass_can_change_time = mytime;
 
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_CANCHANGETIME); 
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_CANCHANGETIME, flag);
 }
 
-BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
+BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.pass_must_change_time = mytime;
 
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_MUSTCHANGETIME); 
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_MUSTCHANGETIME, flag);
 }
 
-BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
+BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.pass_last_set_time = mytime;
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_PASSLASTSET, flag);
 }
 
-BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
+BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.hours_len = len;
-       return True;
+
+       return pdb_set_init_flags(sampass, PDB_HOURSLEN, flag);
 }
 
-BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
+BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.logon_divs = hours;
-       return True;
+
+       return pdb_set_init_flags(sampass, PDB_LOGONDIVS, flag);
 }
 
 /**
@@ -468,47 +471,69 @@ BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
  *             this flag is only added.  
  **/
  
-BOOL pdb_set_init_flag (SAM_ACCOUNT *sampass, uint32 flag)
+BOOL pdb_set_init_flags (SAM_ACCOUNT *sampass, enum pdb_elements element, enum pdb_value_state value_flag)
 {
-        if (!sampass)
+        if (!sampass || !sampass->mem_ctx)
                 return False;
 
-        sampass->private.init_flag |= flag;
+        if (!sampass->private.set_flags) {
+               if ((sampass->private.set_flags = 
+                       bitmap_talloc(sampass->mem_ctx, 
+                                       PDB_COUNT))==NULL) {
+                       DEBUG(0,("bitmap_talloc failed\n"));
+                       return False;
+               }
+        }
+        if (!sampass->private.change_flags) {
+               if ((sampass->private.change_flags = 
+                       bitmap_talloc(sampass->mem_ctx, 
+                                       PDB_COUNT))==NULL) {
+                       DEBUG(0,("bitmap_talloc failed\n"));
+                       return False;
+               }
+        }
+        
+        switch(value_flag) {
+               case PDB_CHANGED:
+                       if (!bitmap_set(sampass->private.change_flags, element)) {
+                               DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
+                               return False;
+                       }
+                       if (!bitmap_set(sampass->private.set_flags, element)) {
+                               DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
+                               return False;
+                       }
+                       DEBUG(11, ("element %d -> now CHANGED\n", element)); 
+                       break;
+               case PDB_SET:
+                       if (!bitmap_clear(sampass->private.change_flags, element)) {
+                               DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
+                               return False;
+                       }
+                       if (!bitmap_set(sampass->private.set_flags, element)) {
+                               DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
+                               return False;
+                       }
+                       DEBUG(11, ("element %d -> now SET\n", element)); 
+                       break;
+               case PDB_DEFAULT:
+               default:
+                       if (!bitmap_clear(sampass->private.change_flags, element)) {
+                               DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
+                               return False;
+                       }
+                       if (!bitmap_clear(sampass->private.set_flags, element)) {
+                               DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
+                               return False;
+                       }
+                       DEBUG(11, ("element %d -> now DEFAULT\n", element)); 
+                       break;
+       }
 
         return True;
 }
 
-BOOL pdb_set_uid (SAM_ACCOUNT *sampass, const uid_t uid)
-{
-       if (!sampass)
-               return False;
-       
-       DEBUG(10, ("pdb_set_uid: setting uid %d, was %d\n", 
-                  (int)uid, (int)sampass->private.uid));
-       sampass->private.uid = uid;
-       pdb_set_init_flag(sampass, FLAG_SAM_UID); 
-
-       return True;
-
-}
-
-BOOL pdb_set_gid (SAM_ACCOUNT *sampass, const gid_t gid)
-{
-       if (!sampass)
-               return False;
-               
-       DEBUG(10, ("pdb_set_gid: setting gid %d, was %d\n", 
-                  (int)gid, (int)sampass->private.gid));
-       sampass->private.gid = gid; 
-       pdb_set_init_flag(sampass, FLAG_SAM_GID); 
-
-       return True;
-
-}
-
-BOOL pdb_set_user_sid (SAM_ACCOUNT *sampass, DOM_SID *u_sid)
+BOOL pdb_set_user_sid (SAM_ACCOUNT *sampass, const DOM_SID *u_sid, enum pdb_value_state flag)
 {
        if (!sampass || !u_sid)
                return False;
@@ -517,13 +542,14 @@ BOOL pdb_set_user_sid (SAM_ACCOUNT *sampass, DOM_SID *u_sid)
 
        DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n", 
                    sid_string_static(&sampass->private.user_sid)));
-       
-       return True;
+
+       return pdb_set_init_flags(sampass, PDB_USERSID, flag);
 }
 
-BOOL pdb_set_user_sid_from_string (SAM_ACCOUNT *sampass, fstring u_sid)
+BOOL pdb_set_user_sid_from_string (SAM_ACCOUNT *sampass, fstring u_sid, enum pdb_value_state flag)
 {
        DOM_SID new_sid;
+       
        if (!sampass || !u_sid)
                return False;
 
@@ -535,7 +561,7 @@ BOOL pdb_set_user_sid_from_string (SAM_ACCOUNT *sampass, fstring u_sid)
                return False;
        }
         
-       if (!pdb_set_user_sid(sampass, &new_sid)) {
+       if (!pdb_set_user_sid(sampass, &new_sid, flag)) {
                DEBUG(1, ("pdb_set_user_sid_from_string: could not set sid %s on SAM_ACCOUNT!\n", u_sid));
                return False;
        }
@@ -543,7 +569,7 @@ BOOL pdb_set_user_sid_from_string (SAM_ACCOUNT *sampass, fstring u_sid)
        return True;
 }
 
-BOOL pdb_set_group_sid (SAM_ACCOUNT *sampass, DOM_SID *g_sid)
+BOOL pdb_set_group_sid (SAM_ACCOUNT *sampass, const DOM_SID *g_sid, enum pdb_value_state flag)
 {
        if (!sampass || !g_sid)
                return False;
@@ -553,10 +579,10 @@ BOOL pdb_set_group_sid (SAM_ACCOUNT *sampass, DOM_SID *g_sid)
        DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n", 
                    sid_string_static(&sampass->private.group_sid)));
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_GROUPSID, flag);
 }
 
-BOOL pdb_set_group_sid_from_string (SAM_ACCOUNT *sampass, fstring g_sid)
+BOOL pdb_set_group_sid_from_string (SAM_ACCOUNT *sampass, fstring g_sid, enum pdb_value_state flag)
 {
        DOM_SID new_sid;
        if (!sampass || !g_sid)
@@ -570,73 +596,19 @@ BOOL pdb_set_group_sid_from_string (SAM_ACCOUNT *sampass, fstring g_sid)
                return False;
        }
         
-       if (!pdb_set_group_sid(sampass, &new_sid)) {
+       if (!pdb_set_group_sid(sampass, &new_sid, flag)) {
                DEBUG(1, ("pdb_set_group_sid_from_string: could not set sid %s on SAM_ACCOUNT!\n", g_sid));
                return False;
        }
        return True;
 }
 
-BOOL pdb_set_user_sid_from_rid (SAM_ACCOUNT *sampass, uint32 rid)
-{
-       DOM_SID u_sid;
-       const DOM_SID *global_sam_sid;
-       
-       if (!sampass)
-               return False;
-
-       if (!(global_sam_sid = get_global_sam_sid())) {
-               DEBUG(1, ("pdb_set_user_sid_from_rid: Could not read global sam sid!\n"));
-               return False;
-       }
-
-       sid_copy(&u_sid, global_sam_sid);
-
-       if (!sid_append_rid(&u_sid, rid))
-               return False;
-
-       if (!pdb_set_user_sid(sampass, &u_sid))
-               return False;
-
-       DEBUG(10, ("pdb_set_user_sid_from_rid:\n\tsetting user sid %s from rid %d\n", 
-                   sid_string_static(&u_sid),rid));
-
-       return True;
-}
-
-BOOL pdb_set_group_sid_from_rid (SAM_ACCOUNT *sampass, uint32 grid)
-{
-       DOM_SID g_sid;
-       const DOM_SID *global_sam_sid;
-
-       if (!sampass)
-               return False;
-       
-       if (!(global_sam_sid = get_global_sam_sid())) {
-               DEBUG(1, ("pdb_set_user_sid_from_rid: Could not read global sam sid!\n"));
-               return False;
-       }
-
-       sid_copy(&g_sid, global_sam_sid);
-       
-       if (!sid_append_rid(&g_sid, grid))
-               return False;
-
-       if (!pdb_set_group_sid(sampass, &g_sid))
-               return False;
-
-       DEBUG(10, ("pdb_set_group_sid_from_rid:\n\tsetting group sid %s from rid %d\n", 
-                   sid_string_static(&g_sid), grid));
-
-       return True;
-}
-
 /*********************************************************************
  Set the user's UNIX name.
  ********************************************************************/
 
-BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
-{      
+BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username, enum pdb_value_state flag)
+{
        if (!sampass)
                return False;
  
@@ -654,16 +626,16 @@ BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
        } else {
                sampass->private.username = PDB_NOT_QUITE_NULL;
        }
-
-       return True;
+       
+       return pdb_set_init_flags(sampass, PDB_USERNAME, flag);
 }
 
 /*********************************************************************
  Set the domain name.
  ********************************************************************/
 
-BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
-{      
+BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain, enum pdb_value_state flag)
+{
        if (!sampass)
                return False;
 
@@ -682,14 +654,14 @@ BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
                sampass->private.domain = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_DOMAIN, flag);
 }
 
 /*********************************************************************
  Set the user's NT name.
  ********************************************************************/
 
-BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
+BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -709,14 +681,14 @@ BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
                sampass->private.nt_username = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_NTUSERNAME, flag);
 }
 
 /*********************************************************************
  Set the user's full name.
  ********************************************************************/
 
-BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *full_name)
+BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *full_name, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -736,14 +708,14 @@ BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *full_name)
                sampass->private.full_name = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_FULLNAME, flag);
 }
 
 /*********************************************************************
  Set the user's logon script.
  ********************************************************************/
 
-BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL store)
+BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -763,19 +735,14 @@ BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL s
                sampass->private.logon_script = PDB_NOT_QUITE_NULL;
        }
        
-       if (store) {
-               DEBUG(10, ("pdb_set_logon_script: setting logon script sam flag!\n"));
-               pdb_set_init_flag(sampass, FLAG_SAM_LOGONSCRIPT);
-       }
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_LOGONSCRIPT, flag);
 }
 
 /*********************************************************************
  Set the user's profile path.
  ********************************************************************/
 
-BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL store)
+BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -795,19 +762,14 @@ BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL
                sampass->private.profile_path = PDB_NOT_QUITE_NULL;
        }
 
-       if (store) {
-               DEBUG(10, ("pdb_set_profile_path: setting profile path sam flag!\n"));
-               pdb_set_init_flag(sampass, FLAG_SAM_PROFILE);
-       }
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_PROFILE, flag);
 }
 
 /*********************************************************************
  Set the user's directory drive.
  ********************************************************************/
 
-BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
+BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -827,19 +789,14 @@ BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
                sampass->private.dir_drive = PDB_NOT_QUITE_NULL;
        }
        
-       if (store) {
-               DEBUG(10, ("pdb_set_dir_drive: setting dir drive sam flag!\n"));
-               pdb_set_init_flag(sampass, FLAG_SAM_DRIVE);
-       }
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_DRIVE, flag);
 }
 
 /*********************************************************************
  Set the user's home directory.
  ********************************************************************/
 
-BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *home_dir, BOOL store)
+BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *home_dir, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -859,19 +816,14 @@ BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *home_dir, BOOL store)
                sampass->private.home_dir = PDB_NOT_QUITE_NULL;
        }
 
-       if (store) {
-               DEBUG(10, ("pdb_set_homedir: setting home dir sam flag!\n"));
-               pdb_set_init_flag(sampass, FLAG_SAM_SMBHOME);
-       }
-
-       return True;
+       return pdb_set_init_flags(sampass, PDB_SMBHOME, flag);
 }
 
 /*********************************************************************
  Set the user's unix home directory.
  ********************************************************************/
 
-BOOL pdb_set_unix_homedir (SAM_ACCOUNT *sampass, const char *unix_home_dir)
+BOOL pdb_set_unix_homedir (SAM_ACCOUNT *sampass, const char *unix_home_dir, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -892,14 +844,14 @@ BOOL pdb_set_unix_homedir (SAM_ACCOUNT *sampass, const char *unix_home_dir)
                sampass->private.unix_home_dir = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_UNIXHOMEDIR, flag);
 }
 
 /*********************************************************************
  Set the user's account description.
  ********************************************************************/
 
-BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
+BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -916,14 +868,14 @@ BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
                sampass->private.acct_desc = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_ACCTDESC, flag);
 }
 
 /*********************************************************************
  Set the user's workstation allowed list.
  ********************************************************************/
 
-BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
+BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -943,14 +895,14 @@ BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
                sampass->private.workstations = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_WORKSTATIONS, flag);
 }
 
 /*********************************************************************
  Set the user's 'unknown_str', whatever the heck this actually is...
  ********************************************************************/
 
-BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
+BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -967,14 +919,14 @@ BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
                sampass->private.unknown_str = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_UNKNOWNSTR, flag);
 }
 
 /*********************************************************************
  Set the user's dial string.
  ********************************************************************/
 
-BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
+BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -991,39 +943,73 @@ BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
                sampass->private.munged_dial = PDB_NOT_QUITE_NULL;
        }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_MUNGEDDIAL, flag);
 }
 
 /*********************************************************************
  Set the user's NT hash.
  ********************************************************************/
 
-BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
+BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 pwd[NT_HASH_LEN], enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        data_blob_clear_free(&sampass->private.nt_pw);
        
-       sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
+       if (pwd) {
+               sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
+       } else {
+               sampass->private.nt_pw = data_blob(NULL, 0);
+       }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_NTPASSWD, flag);
 }
 
 /*********************************************************************
  Set the user's LM hash.
  ********************************************************************/
 
-BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 pwd[16])
+BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 pwd[LM_HASH_LEN], enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        data_blob_clear_free(&sampass->private.lm_pw);
        
-       sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
+       if (pwd) {
+               sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
+       } else {
+               sampass->private.lm_pw = data_blob(NULL, 0);
+       }
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_LMPASSWD, flag);
+}
+
+/*********************************************************************
+ Set the user's password history hash. historyLen is the number of 
+ PW_HISTORY_SALT_LEN+SALTED_MD5_HASH_LEN length
+ entries to store in the history - this must match the size of the uint8 array
+ in pwd.
+********************************************************************/
+
+BOOL pdb_set_pw_history (SAM_ACCOUNT *sampass, const uint8 *pwd, uint32 historyLen, enum pdb_value_state flag)
+{
+       if (!sampass)
+               return False;
+
+       if (historyLen && pwd){
+               sampass->private.nt_pw_his = data_blob_talloc(sampass->mem_ctx,
+                                               pwd, historyLen*PW_HISTORY_ENTRY_LEN);
+               if (!sampass->private.nt_pw_his.length) {
+                       DEBUG(0, ("pdb_set_pw_history: data_blob_talloc() failed!\n"));
+                       return False;
+               }
+       } else {
+               sampass->private.nt_pw_his = data_blob_talloc(sampass->mem_ctx, NULL, 0);
+       }
+
+       return pdb_set_init_flags(sampass, PDB_PWHISTORY, flag);
 }
 
 /*********************************************************************
@@ -1031,46 +1017,60 @@ BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 pwd[16])
  below)
  ********************************************************************/
 
-BOOL pdb_set_plaintext_pw_only (SAM_ACCOUNT *sampass, const uint8 *password, size_t len)
+BOOL pdb_set_plaintext_pw_only (SAM_ACCOUNT *sampass, const char *password, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
-       data_blob_clear_free(&sampass->private.plaintext_pw);
-       
-       sampass->private.plaintext_pw = data_blob(password, len);
+       if (password) { 
+               if (sampass->private.plaintext_pw!=NULL) 
+                       memset(sampass->private.plaintext_pw,'\0',strlen(sampass->private.plaintext_pw)+1);
 
-       return True;
+               sampass->private.plaintext_pw = talloc_strdup(sampass->mem_ctx, password);
+               
+               if (!sampass->private.plaintext_pw) {
+                       DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
+                       return False;
+               }
+
+       } else {
+               sampass->private.plaintext_pw = NULL;
+       }
+
+       return pdb_set_init_flags(sampass, PDB_PLAINTEXT_PW, flag);
 }
 
-BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
+BOOL pdb_set_bad_password_count(SAM_ACCOUNT *sampass, uint16 bad_password_count, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
-       sampass->private.unknown_3 = unkn;
-       return True;
+       sampass->private.bad_password_count = bad_password_count;
+
+       return pdb_set_init_flags(sampass, PDB_BAD_PASSWORD_COUNT, flag);
 }
 
-BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
+BOOL pdb_set_logon_count(SAM_ACCOUNT *sampass, uint16 logon_count, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
-       sampass->private.unknown_5 = unkn;
-       return True;
+       sampass->private.logon_count = logon_count;
+
+       return pdb_set_init_flags(sampass, PDB_LOGON_COUNT, flag);
 }
 
-BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
+BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
 
        sampass->private.unknown_6 = unkn;
-       return True;
+
+       return pdb_set_init_flags(sampass, PDB_UNKNOWN6, flag);
 }
 
-BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
+BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours, enum pdb_value_state flag)
 {
        if (!sampass)
                return False;
@@ -1082,7 +1082,26 @@ BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
        
        memcpy (sampass->private.hours, hours, MAX_HOURS_LEN);
 
-       return True;
+       return pdb_set_init_flags(sampass, PDB_HOURS, flag);
+}
+
+BOOL pdb_set_backend_private_data (SAM_ACCOUNT *sampass, void *private_data, 
+                                  void (*free_fn)(void **), 
+                                  const struct pdb_methods *my_methods, 
+                                  enum pdb_value_state flag)
+{
+       if (!sampass)
+               return False;
+
+       if (sampass->private.backend_private_data && sampass->private.backend_private_data_free_fn) {
+               sampass->private.backend_private_data_free_fn(&sampass->private.backend_private_data);
+       }
+
+       sampass->private.backend_private_data = private_data;
+       sampass->private.backend_private_data_free_fn = free_fn;
+       sampass->private.backend_private_methods = my_methods;
+
+       return pdb_set_init_flags(sampass, PDB_BACKEND_PRIVATE_DATA, flag);
 }
 
 
@@ -1096,25 +1115,35 @@ BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
 BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
 {
        uint32 expire;
+       uint32 min_age;
 
        if (!sampass)
                return False;
        
-       if (!pdb_set_pass_last_set_time (sampass, time(NULL)))
+       if (!pdb_set_pass_last_set_time (sampass, time(NULL), PDB_CHANGED))
                return False;
 
-       account_policy_get(AP_MAX_PASSWORD_AGE, &expire);
-
-       if (expire==(uint32)-1) {
-               if (!pdb_set_pass_must_change_time (sampass, get_time_t_max(), False))
+       if (!account_policy_get(AP_MAX_PASSWORD_AGE, &expire) 
+           || (expire==(uint32)-1) || (expire == 0)) {
+               if (!pdb_set_pass_must_change_time (sampass, get_time_t_max(), PDB_CHANGED))
                        return False;
        } else {
                if (!pdb_set_pass_must_change_time (sampass, 
-                                           pdb_get_pass_last_set_time(sampass)
-                                           + expire, True))
+                                                   pdb_get_pass_last_set_time(sampass)
+                                                   + expire, PDB_CHANGED))
                        return False;
        }
        
+       if (!account_policy_get(AP_MIN_PASSWORD_AGE, &min_age) 
+           || (min_age==(uint32)-1)) {
+               if (!pdb_set_pass_can_change_time (sampass, 0, PDB_CHANGED))
+                       return False;
+       } else {
+               if (!pdb_set_pass_can_change_time (sampass, 
+                                                   pdb_get_pass_last_set_time(sampass)
+                                                   + min_age, PDB_CHANGED))
+                       return False;
+       }
        return True;
 }
 
@@ -1125,23 +1154,101 @@ BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
 
 BOOL pdb_set_plaintext_passwd (SAM_ACCOUNT *sampass, const char *plaintext)
 {
-       uchar new_lanman_p16[16];
-       uchar new_nt_p16[16];
+       uchar new_lanman_p16[LM_HASH_LEN];
+       uchar new_nt_p16[NT_HASH_LEN];
 
        if (!sampass || !plaintext)
                return False;
-       
-       nt_lm_owf_gen (plaintext, new_nt_p16, new_lanman_p16);
 
-       if (!pdb_set_nt_passwd (sampass, new_nt_p16)) 
+       /* Calculate the MD4 hash (NT compatible) of the password */
+       E_md4hash(plaintext, new_nt_p16);
+
+       if (!pdb_set_nt_passwd (sampass, new_nt_p16, PDB_CHANGED)) 
                return False;
 
-       if (!pdb_set_lanman_passwd (sampass, new_lanman_p16)) 
+       if (!E_deshash(plaintext, new_lanman_p16)) {
+               /* E_deshash returns false for 'long' passwords (> 14
+                  DOS chars).  This allows us to match Win2k, which
+                  does not store a LM hash for these passwords (which
+                  would reduce the effective password length to 14 */
+
+               if (!pdb_set_lanman_passwd (sampass, NULL, PDB_CHANGED)) 
+                       return False;
+       } else {
+               if (!pdb_set_lanman_passwd (sampass, new_lanman_p16, PDB_CHANGED)) 
+                       return False;
+       }
+
+       if (!pdb_set_plaintext_pw_only (sampass, plaintext, PDB_CHANGED)) 
                return False;
-       
+
        if (!pdb_set_pass_changed_now (sampass))
                return False;
 
+       /* Store the password history. */
+       if (pdb_get_acct_ctrl(sampass) & ACB_NORMAL) {
+               uchar *pwhistory;
+               uint32 pwHistLen;
+               account_policy_get(AP_PASSWORD_HISTORY, &pwHistLen);
+               if (pwHistLen != 0){
+                       uint32 current_history_len;
+                       /* We need to make sure we don't have a race condition here - the
+                          account policy history length can change between when the pw_history
+                          was first loaded into the SAM_ACCOUNT struct and now.... JRA. */
+                       pwhistory = CONST_DISCARD(uchar *, pdb_get_pw_history(sampass, &current_history_len));
+
+                       if (current_history_len != pwHistLen) {
+                               /* After closing and reopening SAM_ACCOUNT the history
+                                       values will sync up. We can't do this here. */
+
+                               /* current_history_len > pwHistLen is not a problem - we
+                                       have more history than we need. */
+
+                               if (current_history_len < pwHistLen) {
+                                       /* Ensure we have space for the needed history. */
+                                       uchar *new_history = TALLOC(sampass->mem_ctx,
+                                                               pwHistLen*PW_HISTORY_ENTRY_LEN);
+                                       /* And copy it into the new buffer. */
+                                       if (current_history_len) {
+                                               memcpy(new_history, pwhistory,
+                                                       current_history_len*PW_HISTORY_ENTRY_LEN);
+                                       }
+                                       /* Clearing out any extra space. */
+                                       memset(&new_history[current_history_len*PW_HISTORY_ENTRY_LEN],
+                                               '\0', (pwHistLen-current_history_len)*PW_HISTORY_ENTRY_LEN);
+                                       /* Finally replace it. */
+                                       pwhistory = new_history;
+                               }
+                       }
+                       if (pwhistory && pwHistLen){
+                               /* Make room for the new password in the history list. */
+                               if (pwHistLen > 1) {
+                                       memmove(&pwhistory[PW_HISTORY_ENTRY_LEN],
+                                               pwhistory, (pwHistLen -1)*PW_HISTORY_ENTRY_LEN );
+                               }
+                               /* Create the new salt as the first part of the history entry. */
+                               generate_random_buffer(pwhistory, PW_HISTORY_SALT_LEN);
+
+                               /* Generate the md5 hash of the salt+new password as the second
+                                       part of the history entry. */
+
+                               E_md5hash(pwhistory, new_nt_p16, &pwhistory[PW_HISTORY_SALT_LEN]);
+                               pdb_set_pw_history(sampass, pwhistory, pwHistLen, PDB_CHANGED);
+                       } else {
+                               DEBUG (10,("pdb_get_set.c: pdb_set_plaintext_passwd: pwhistory was NULL!\n"));
+                       }
+               } else {
+                       /* Set the history length to zero. */
+                       pdb_set_pw_history(sampass, NULL, 0, PDB_CHANGED);
+               }
+       }
+
        return True;
 }
 
+/* check for any PDB_SET/CHANGED field and fill the appropriate mask bit */
+uint32 pdb_build_fields_present (SAM_ACCOUNT *sampass)
+{
+       /* value set to all for testing */
+       return 0x00ffffff;
+}