docs: fix a typo in history file
[bbaumbach/samba-autobuild/.git] / source3 / passdb / pdb_get_set.c
index 3e2510e74c89c2e1ae9404f46ab3b20239b298e4..d598fe51e1f91a444ce53f0b07a3b43c39941ca0 100644 (file)
@@ -1,8 +1,8 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    struct samu access routines
-   Copyright (C) Jeremy Allison                1996-2001
-   Copyright (C) Luke Kenneth Casson Leighton  1996-1998
+   Copyright (C) Jeremy Allison                        1996-2001
+   Copyright (C) Luke Kenneth Casson Leighton  1996-1998
    Copyright (C) Gerald (Jerry) Carter         2000-2006
    Copyright (C) Andrew Bartlett               2001-2002
    Copyright (C) Stefan (metze) Metzmacher     2002
 */
 
 #include "includes.h"
+#include "passdb.h"
 #include "../libcli/auth/libcli_auth.h"
+#include "../libcli/security/security.h"
+#include "../lib/util/bitmap.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
 
 /**
  * @todo Redefine this to NULL, but this changes the API because
- *       much of samba assumes that the pdb_get...() funtions 
+ *       much of samba assumes that the pdb_get...() funtions
  *       return strings.  (ie not null-pointers).
  *       See also pdb_fill_default_sam().
  */
 
 #define PDB_NOT_QUITE_NULL ""
 
+/*********************************************************************
+ Test if a change time is a max value. Copes with old and new values
+ of max.
+ ********************************************************************/
+
+bool pdb_is_password_change_time_max(time_t test_time)
+{
+       if (test_time == get_time_t_max()) {
+               return true;
+       }
+#if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
+       if (test_time == 0x7FFFFFFFFFFFFFFFLL) {
+               return true;
+       }
+#endif
+       if (test_time == 0x7FFFFFFF) {
+               return true;
+       }
+       return false;
+}
+
+/*********************************************************************
+ Return an unchanging version of max password change time - 0x7FFFFFFF.
+ ********************************************************************/
+
+static time_t pdb_password_change_time_max(void)
+{
+       return 0x7FFFFFFF;
+}
+
 /*********************************************************************
  Collection of get...() functions for struct samu.
  ********************************************************************/
@@ -74,8 +107,8 @@ time_t pdb_get_pass_can_change_time(const struct samu *sampass)
 {
        uint32_t allow;
 
-       /* if the last set time is zero, it means the user cannot 
-          change their password, and this time must be zero.   jmcd 
+       /* if the last set time is zero, it means the user cannot
+          change their password, and this time must be zero.   jmcd
        */
        if (sampass->pass_last_set_time == 0)
                return (time_t) 0;
@@ -84,7 +117,7 @@ time_t pdb_get_pass_can_change_time(const struct samu *sampass)
           we're trying to update this real value from the sampass
           to indicate that the user cannot change their password.  jmcd
        */
-       if (sampass->pass_can_change_time == get_time_t_max() &&
+       if (pdb_is_password_change_time_max(sampass->pass_can_change_time) &&
            IS_SAM_CHANGED(sampass, PDB_CANCHANGETIME))
                return sampass->pass_can_change_time;
 
@@ -110,7 +143,7 @@ time_t pdb_get_pass_must_change_time(const struct samu *sampass)
                return (time_t) 0;
 
        if (sampass->acct_ctrl & ACB_PWNOEXP)
-               return get_time_t_max();
+               return pdb_password_change_time_max();
 
        if (!pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &expire)
            || expire == (uint32_t)-1 || expire == 0)
@@ -121,8 +154,7 @@ time_t pdb_get_pass_must_change_time(const struct samu *sampass)
 
 bool pdb_get_pass_can_change(const struct samu *sampass)
 {
-       if (sampass->pass_can_change_time == get_time_t_max() &&
-           sampass->pass_last_set_time != 0)
+       if (pdb_is_password_change_time_max(sampass->pass_can_change_time))
                return False;
        return True;
 }
@@ -137,37 +169,37 @@ uint32_t pdb_get_hours_len(const struct samu *sampass)
        return sampass->hours_len;
 }
 
-const uint8 *pdb_get_hours(const struct samu *sampass)
+const uint8_t *pdb_get_hours(const struct samu *sampass)
 {
        return (sampass->hours);
 }
 
-const uint8 *pdb_get_nt_passwd(const struct samu *sampass)
+const uint8_t *pdb_get_nt_passwd(const struct samu *sampass)
 {
-       SMB_ASSERT((!sampass->nt_pw.data) 
+       SMB_ASSERT((!sampass->nt_pw.data)
                   || sampass->nt_pw.length == NT_HASH_LEN);
-       return (uint8 *)sampass->nt_pw.data;
+       return (uint8_t *)sampass->nt_pw.data;
 }
 
-const uint8 *pdb_get_lanman_passwd(const struct samu *sampass)
+const uint8_t *pdb_get_lanman_passwd(const struct samu *sampass)
 {
-       SMB_ASSERT((!sampass->lm_pw.data) 
+       SMB_ASSERT((!sampass->lm_pw.data)
                   || sampass->lm_pw.length == LM_HASH_LEN);
-       return (uint8 *)sampass->lm_pw.data;
+       return (uint8_t *)sampass->lm_pw.data;
 }
 
-const uint8 *pdb_get_pw_history(const struct samu *sampass, uint32_t *current_hist_len)
+const uint8_t *pdb_get_pw_history(const struct samu *sampass, uint32_t *current_hist_len)
 {
-       SMB_ASSERT((!sampass->nt_pw_his.data) 
+       SMB_ASSERT((!sampass->nt_pw_his.data)
           || ((sampass->nt_pw_his.length % PW_HISTORY_ENTRY_LEN) == 0));
        *current_hist_len = sampass->nt_pw_his.length / PW_HISTORY_ENTRY_LEN;
-       return (uint8 *)sampass->nt_pw_his.data;
+       return (uint8_t *)sampass->nt_pw_his.data;
 }
 
 /* Return the plaintext password if known.  Most of the time
    it isn't, so don't assume anything magic about this function.
 
-   Used to pass the plaintext to passdb backends that might 
+   Used to pass the plaintext to passdb backends that might
    want to store more than just the NTLM hashes.
 */
 const char *pdb_get_plaintext_passwd(const struct samu *sampass)
@@ -205,7 +237,7 @@ const struct dom_sid *pdb_get_group_sid(struct samu *sampass)
 }
 
 /**
- * Get flags showing what is initalised in the struct samu
+ * Get flags showing what is initialised in the struct samu
  * @param sampass the struct samu in question
  * @return the flags indicating the members initialised in the struct.
  **/
@@ -215,20 +247,20 @@ enum pdb_value_state pdb_get_init_flags(const struct samu *sampass, enum pdb_ele
        enum pdb_value_state ret = PDB_DEFAULT;
 
         if (!sampass->change_flags || !sampass->set_flags)
-               return ret;
+               return ret;
 
         if (bitmap_query(sampass->set_flags, element)) {
-               DEBUG(11, ("element %d: SET\n", element)); 
-               ret = PDB_SET;
+               DEBUG(11, ("element %d: SET\n", element));
+               ret = PDB_SET;
        }
 
         if (bitmap_query(sampass->change_flags, element)) {
-               DEBUG(11, ("element %d: CHANGED\n", element)); 
-               ret = PDB_CHANGED;
+               DEBUG(11, ("element %d: CHANGED\n", element));
+               ret = PDB_CHANGED;
        }
 
        if (ret == PDB_DEFAULT) {
-               DEBUG(11, ("element %d: DEFAULT\n", element)); 
+               DEBUG(11, ("element %d: DEFAULT\n", element));
        }
 
         return ret;
@@ -304,6 +336,16 @@ uint16_t pdb_get_logon_count(const struct samu *sampass)
        return sampass->logon_count;
 }
 
+uint16_t pdb_get_country_code(const struct samu *sampass)
+{
+       return sampass->country_code;
+}
+
+uint16_t pdb_get_code_page(const struct samu *sampass)
+{
+       return sampass->code_page;
+}
+
 uint32_t pdb_get_unknown_6(const struct samu *sampass)
 {
        return sampass->unknown_6;
@@ -358,12 +400,6 @@ bool pdb_set_pass_can_change_time(struct samu *sampass, time_t mytime, enum pdb_
        return pdb_set_init_flags(sampass, PDB_CANCHANGETIME, flag);
 }
 
-bool pdb_set_pass_must_change_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
-{
-       sampass->pass_must_change_time = mytime;
-       return pdb_set_init_flags(sampass, PDB_MUSTCHANGETIME, flag);
-}
-
 bool pdb_set_pass_last_set_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
 {
        sampass->pass_last_set_time = mytime;
@@ -383,66 +419,66 @@ bool pdb_set_logon_divs(struct samu *sampass, uint16_t hours, enum pdb_value_sta
 }
 
 /**
- * Set flags showing what is initalised in the struct samu
+ * Set flags showing what is initialised in the struct samu
  * @param sampass the struct samu in question
  * @param flag The *new* flag to be set.  Old flags preserved
- *             this flag is only added.  
+ *             this flag is only added.
  **/
 
 bool pdb_set_init_flags(struct samu *sampass, enum pdb_elements element, enum pdb_value_state value_flag)
 {
-        if (!sampass->set_flags) {
-               if ((sampass->set_flags = 
-                       bitmap_talloc(sampass, 
-                                       PDB_COUNT))==NULL) {
-                       DEBUG(0,("bitmap_talloc failed\n"));
-                       return False;
-               }
-        }
-        if (!sampass->change_flags) {
-               if ((sampass->change_flags = 
-                       bitmap_talloc(sampass, 
-                                       PDB_COUNT))==NULL) {
-                       DEBUG(0,("bitmap_talloc failed\n"));
-                       return False;
-               }
-        }
-
-        switch(value_flag) {
-               case PDB_CHANGED:
-                       if (!bitmap_set(sampass->change_flags, element)) {
+       if (!sampass->set_flags) {
+               if ((sampass->set_flags =
+                       bitmap_talloc(sampass,
+                                       PDB_COUNT))==NULL) {
+                       DEBUG(0,("bitmap_talloc failed\n"));
+                       return False;
+               }
+       }
+       if (!sampass->change_flags) {
+               if ((sampass->change_flags =
+                       bitmap_talloc(sampass,
+                                       PDB_COUNT))==NULL) {
+                       DEBUG(0,("bitmap_talloc failed\n"));
+                       return False;
+               }
+       }
+
+       switch(value_flag) {
+       case PDB_CHANGED:
+               if (!bitmap_set(sampass->change_flags, element)) {
                                DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
                                return False;
                        }
-                       if (!bitmap_set(sampass->set_flags, element)) {
+                       if (!bitmap_set(sampass->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->change_flags, element)) {
+                       DEBUG(11, ("element %d -> now CHANGED\n", element));
+                       break;
+               case PDB_SET:
+                       if (!bitmap_clear(sampass->change_flags, element)) {
                                DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
                                return False;
                        }
-                       if (!bitmap_set(sampass->set_flags, element)) {
+                       if (!bitmap_set(sampass->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->change_flags, element)) {
+                       DEBUG(11, ("element %d -> now SET\n", element));
+                       break;
+               case PDB_DEFAULT:
+               default:
+                       if (!bitmap_clear(sampass->change_flags, element)) {
                                DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
                                return False;
                        }
-                       if (!bitmap_clear(sampass->set_flags, element)) {
+                       if (!bitmap_clear(sampass->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;
+                       DEBUG(11, ("element %d -> now DEFAULT\n", element));
+                       break;
        }
 
         return True;
@@ -450,18 +486,20 @@ bool pdb_set_init_flags(struct samu *sampass, enum pdb_elements element, enum pd
 
 bool pdb_set_user_sid(struct samu *sampass, const struct dom_sid *u_sid, enum pdb_value_state flag)
 {
+       struct dom_sid_buf buf;
+
        if (!u_sid)
                return False;
 
        sid_copy(&sampass->user_sid, u_sid);
 
-       DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n", 
-                   sid_string_dbg(&sampass->user_sid)));
+       DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n",
+                  dom_sid_str_buf(&sampass->user_sid, &buf)));
 
        return pdb_set_init_flags(sampass, PDB_USERSID, flag);
 }
 
-bool pdb_set_user_sid_from_string(struct samu *sampass, fstring u_sid, enum pdb_value_state flag)
+bool pdb_set_user_sid_from_string(struct samu *sampass, const char *u_sid, enum pdb_value_state flag)
 {
        struct dom_sid new_sid;
 
@@ -471,7 +509,7 @@ bool pdb_set_user_sid_from_string(struct samu *sampass, fstring u_sid, enum pdb_
        DEBUG(10, ("pdb_set_user_sid_from_string: setting user sid %s\n",
                   u_sid));
 
-       if (!string_to_sid(&new_sid, u_sid)) { 
+       if (!string_to_sid(&new_sid, u_sid)) {
                DEBUG(1, ("pdb_set_user_sid_from_string: %s isn't a valid SID!\n", u_sid));
                return False;
        }
@@ -485,9 +523,9 @@ bool pdb_set_user_sid_from_string(struct samu *sampass, fstring u_sid, enum pdb_
 }
 
 /********************************************************************
- We never fill this in from a passdb backend but rather set is 
- based on the user's primary group membership.  However, the 
- struct samu* is overloaded and reused in domain memship code 
+ We never fill this in from a passdb backend but rather set is
+ based on the user's primary group membership.  However, the
+ struct samu* is overloaded and reused in domain memship code
  as well and built from the netr_SamInfo3 or PAC so we
  have to allow the explicitly setting of a group SID here.
 ********************************************************************/
@@ -496,20 +534,21 @@ bool pdb_set_group_sid(struct samu *sampass, const struct dom_sid *g_sid, enum p
 {
        gid_t gid;
        struct dom_sid dug_sid;
+       struct dom_sid_buf buf;
 
        if (!g_sid)
                return False;
 
-       if ( !(sampass->group_sid = TALLOC_P( sampass, struct dom_sid )) ) {
+       if ( !(sampass->group_sid = talloc( sampass, struct dom_sid )) ) {
                return False;
        }
 
-       /* if we cannot resolve the SID to gid, then just ignore it and 
+       /* if we cannot resolve the SID to gid, then just ignore it and
           store DOMAIN_USERS as the primary groupSID */
 
        sid_compose(&dug_sid, get_global_sam_sid(), DOMAIN_RID_USERS);
 
-       if (sid_equal(&dug_sid, g_sid)) {
+       if (dom_sid_equal(&dug_sid, g_sid)) {
                sid_copy(sampass->group_sid, &dug_sid);
        } else if (sid_to_gid( g_sid, &gid ) ) {
                sid_copy(sampass->group_sid, g_sid);
@@ -517,8 +556,8 @@ bool pdb_set_group_sid(struct samu *sampass, const struct dom_sid *g_sid, enum p
                sid_copy(sampass->group_sid, &dug_sid);
        }
 
-       DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n", 
-                  sid_string_dbg(sampass->group_sid)));
+       DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n",
+                  dom_sid_str_buf(sampass->group_sid, &buf)));
 
        return pdb_set_init_flags(sampass, PDB_GROUPSID, flag);
 }
@@ -529,7 +568,7 @@ bool pdb_set_group_sid(struct samu *sampass, const struct dom_sid *g_sid, enum p
 
 bool pdb_set_username(struct samu *sampass, const char *username, enum pdb_value_state flag)
 {
-       if (username) { 
+       if (username) {
                DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", username,
                        (sampass->username)?(sampass->username):"NULL"));
 
@@ -552,7 +591,7 @@ bool pdb_set_username(struct samu *sampass, const char *username, enum pdb_value
 
 bool pdb_set_domain(struct samu *sampass, const char *domain, enum pdb_value_state flag)
 {
-       if (domain) { 
+       if (domain) {
                DEBUG(10, ("pdb_set_domain: setting domain %s, was %s\n", domain,
                        (sampass->domain)?(sampass->domain):"NULL"));
 
@@ -575,10 +614,10 @@ bool pdb_set_domain(struct samu *sampass, const char *domain, enum pdb_value_sta
 
 bool pdb_set_nt_username(struct samu *sampass, const char *nt_username, enum pdb_value_state flag)
 {
-       if (nt_username) { 
+       if (nt_username) {
                DEBUG(10, ("pdb_set_nt_username: setting nt username %s, was %s\n", nt_username,
                        (sampass->nt_username)?(sampass->nt_username):"NULL"));
+
                sampass->nt_username = talloc_strdup(sampass, nt_username);
 
                if (!sampass->nt_username) {
@@ -598,7 +637,7 @@ bool pdb_set_nt_username(struct samu *sampass, const char *nt_username, enum pdb
 
 bool pdb_set_fullname(struct samu *sampass, const char *full_name, enum pdb_value_state flag)
 {
-       if (full_name) { 
+       if (full_name) {
                DEBUG(10, ("pdb_set_full_name: setting full name %s, was %s\n", full_name,
                        (sampass->full_name)?(sampass->full_name):"NULL"));
 
@@ -621,7 +660,7 @@ bool pdb_set_fullname(struct samu *sampass, const char *full_name, enum pdb_valu
 
 bool pdb_set_logon_script(struct samu *sampass, const char *logon_script, enum pdb_value_state flag)
 {
-       if (logon_script) { 
+       if (logon_script) {
                DEBUG(10, ("pdb_set_logon_script: setting logon script %s, was %s\n", logon_script,
                        (sampass->logon_script)?(sampass->logon_script):"NULL"));
 
@@ -644,7 +683,7 @@ bool pdb_set_logon_script(struct samu *sampass, const char *logon_script, enum p
 
 bool pdb_set_profile_path(struct samu *sampass, const char *profile_path, enum pdb_value_state flag)
 {
-       if (profile_path) { 
+       if (profile_path) {
                DEBUG(10, ("pdb_set_profile_path: setting profile path %s, was %s\n", profile_path,
                        (sampass->profile_path)?(sampass->profile_path):"NULL"));
 
@@ -667,7 +706,7 @@ bool pdb_set_profile_path(struct samu *sampass, const char *profile_path, enum p
 
 bool pdb_set_dir_drive(struct samu *sampass, const char *dir_drive, enum pdb_value_state flag)
 {
-       if (dir_drive) { 
+       if (dir_drive) {
                DEBUG(10, ("pdb_set_dir_drive: setting dir drive %s, was %s\n", dir_drive,
                        (sampass->dir_drive)?(sampass->dir_drive):"NULL"));
 
@@ -691,7 +730,7 @@ bool pdb_set_dir_drive(struct samu *sampass, const char *dir_drive, enum pdb_val
 
 bool pdb_set_homedir(struct samu *sampass, const char *home_dir, enum pdb_value_state flag)
 {
-       if (home_dir) { 
+       if (home_dir) {
                DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", home_dir,
                        (sampass->home_dir)?(sampass->home_dir):"NULL"));
 
@@ -714,7 +753,7 @@ bool pdb_set_homedir(struct samu *sampass, const char *home_dir, enum pdb_value_
 
 bool pdb_set_acct_desc(struct samu *sampass, const char *acct_desc, enum pdb_value_state flag)
 {
-       if (acct_desc) { 
+       if (acct_desc) {
                sampass->acct_desc = talloc_strdup(sampass, acct_desc);
 
                if (!sampass->acct_desc) {
@@ -734,7 +773,7 @@ bool pdb_set_acct_desc(struct samu *sampass, const char *acct_desc, enum pdb_val
 
 bool pdb_set_workstations(struct samu *sampass, const char *workstations, enum pdb_value_state flag)
 {
-       if (workstations) { 
+       if (workstations) {
                DEBUG(10, ("pdb_set_workstations: setting workstations %s, was %s\n", workstations,
                        (sampass->workstations)?(sampass->workstations):"NULL"));
 
@@ -756,7 +795,7 @@ bool pdb_set_workstations(struct samu *sampass, const char *workstations, enum p
 
 bool pdb_set_comment(struct samu *sampass, const char *comment, enum pdb_value_state flag)
 {
-       if (comment) { 
+       if (comment) {
                sampass->comment = talloc_strdup(sampass, comment);
 
                if (!sampass->comment) {
@@ -776,7 +815,7 @@ bool pdb_set_comment(struct samu *sampass, const char *comment, enum pdb_value_s
 
 bool pdb_set_munged_dial(struct samu *sampass, const char *munged_dial, enum pdb_value_state flag)
 {
-       if (munged_dial) { 
+       if (munged_dial) {
                sampass->munged_dial = talloc_strdup(sampass, munged_dial);
 
                if (!sampass->munged_dial) {
@@ -794,7 +833,7 @@ bool pdb_set_munged_dial(struct samu *sampass, const char *munged_dial, enum pdb
  Set the user's NT hash.
  ********************************************************************/
 
-bool pdb_set_nt_passwd(struct samu *sampass, const uint8 pwd[NT_HASH_LEN], enum pdb_value_state flag)
+bool pdb_set_nt_passwd(struct samu *sampass, const uint8_t pwd[NT_HASH_LEN], enum pdb_value_state flag)
 {
        data_blob_clear_free(&sampass->nt_pw);
 
@@ -812,7 +851,7 @@ bool pdb_set_nt_passwd(struct samu *sampass, const uint8 pwd[NT_HASH_LEN], enum
  Set the user's LM hash.
  ********************************************************************/
 
-bool pdb_set_lanman_passwd(struct samu *sampass, const uint8 pwd[LM_HASH_LEN], enum pdb_value_state flag)
+bool pdb_set_lanman_passwd(struct samu *sampass, const uint8_t pwd[LM_HASH_LEN], enum pdb_value_state flag)
 {
        data_blob_clear_free(&sampass->lm_pw);
 
@@ -828,26 +867,28 @@ bool pdb_set_lanman_passwd(struct samu *sampass, const uint8 pwd[LM_HASH_LEN], e
 }
 
 /*********************************************************************
- Set the user's password history hash. historyLen is the number of 
+ 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
+ entries to store in the history - this must match the size of the uint8_t array
  in pwd.
 ********************************************************************/
 
-bool pdb_set_pw_history(struct samu *sampass, const uint8 *pwd, uint32_t historyLen, enum pdb_value_state flag)
+bool pdb_set_pw_history(struct samu *sampass, const uint8_t *pwd, uint32_t historyLen, enum pdb_value_state flag)
 {
+       DATA_BLOB new_nt_pw_his = {};
+
        if (historyLen && pwd){
-               data_blob_free(&(sampass->nt_pw_his));
-               sampass->nt_pw_his = data_blob_talloc(sampass,
-                                               pwd, historyLen*PW_HISTORY_ENTRY_LEN);
-               if (!sampass->nt_pw_his.length) {
+               new_nt_pw_his = data_blob_talloc(sampass,
+                                                pwd, historyLen*PW_HISTORY_ENTRY_LEN);
+               if (new_nt_pw_his.length == 0) {
                        DEBUG(0, ("pdb_set_pw_history: data_blob_talloc() failed!\n"));
                        return False;
                }
-       } else {
-               sampass->nt_pw_his = data_blob_talloc(sampass, NULL, 0);
        }
 
+       data_blob_clear_free(&sampass->nt_pw_his);
+       sampass->nt_pw_his = new_nt_pw_his;
+
        return pdb_set_init_flags(sampass, PDB_PWHISTORY, flag);
 }
 
@@ -858,10 +899,9 @@ bool pdb_set_pw_history(struct samu *sampass, const uint8 *pwd, uint32_t history
 
 bool pdb_set_plaintext_pw_only(struct samu *sampass, const char *password, enum pdb_value_state flag)
 {
-       if (password) { 
-               if (sampass->plaintext_pw!=NULL) 
-                       memset(sampass->plaintext_pw,'\0',strlen(sampass->plaintext_pw)+1);
+       BURN_STR(sampass->plaintext_pw);
 
+       if (password != NULL) {
                sampass->plaintext_pw = talloc_strdup(sampass, password);
 
                if (!sampass->plaintext_pw) {
@@ -887,26 +927,45 @@ bool pdb_set_logon_count(struct samu *sampass, uint16_t logon_count, enum pdb_va
        return pdb_set_init_flags(sampass, PDB_LOGON_COUNT, flag);
 }
 
+bool pdb_set_country_code(struct samu *sampass, uint16_t country_code,
+                         enum pdb_value_state flag)
+{
+       sampass->country_code = country_code;
+       return pdb_set_init_flags(sampass, PDB_COUNTRY_CODE, flag);
+}
+
+bool pdb_set_code_page(struct samu *sampass, uint16_t code_page,
+                      enum pdb_value_state flag)
+{
+       sampass->code_page = code_page;
+       return pdb_set_init_flags(sampass, PDB_CODE_PAGE, flag);
+}
+
 bool pdb_set_unknown_6(struct samu *sampass, uint32_t unkn, enum pdb_value_state flag)
 {
        sampass->unknown_6 = unkn;
        return pdb_set_init_flags(sampass, PDB_UNKNOWN6, flag);
 }
 
-bool pdb_set_hours(struct samu *sampass, const uint8 *hours, enum pdb_value_state flag)
+bool pdb_set_hours(struct samu *sampass, const uint8_t *hours, int hours_len,
+                  enum pdb_value_state flag)
 {
+       if (hours_len > sizeof(sampass->hours)) {
+               return false;
+       }
+
        if (!hours) {
-               memset ((char *)sampass->hours, 0, MAX_HOURS_LEN);
+               memset ((char *)sampass->hours, 0, hours_len);
        } else {
-               memcpy (sampass->hours, hours, MAX_HOURS_LEN);
+               memcpy (sampass->hours, hours, hours_len);
        }
 
        return pdb_set_init_flags(sampass, PDB_HOURS, flag);
 }
 
-bool pdb_set_backend_private_data(struct samu *sampass, void *private_data, 
-                                  void (*free_fn)(void **), 
-                                  const struct pdb_methods *my_methods, 
+bool pdb_set_backend_private_data(struct samu *sampass, void *private_data,
+                                  void (*free_fn)(void **),
+                                  const struct pdb_methods *my_methods,
                                   enum pdb_value_state flag)
 {
        if (sampass->backend_private_data &&
@@ -927,8 +986,8 @@ bool pdb_set_backend_private_data(struct samu *sampass, void *private_data,
 
 bool pdb_set_pass_can_change(struct samu *sampass, bool canchange)
 {
-       return pdb_set_pass_can_change_time(sampass, 
-                                    canchange ? 0 : get_time_t_max(),
+       return pdb_set_pass_can_change_time(sampass,
+                                    canchange ? 0 : pdb_password_change_time_max(),
                                     PDB_CHANGED);
 }
 
@@ -942,9 +1001,7 @@ bool pdb_set_plaintext_passwd(struct samu *sampass, const char *plaintext)
 {
        uchar new_lanman_p16[LM_HASH_LEN];
        uchar new_nt_p16[NT_HASH_LEN];
-       uchar *pwhistory;
-       uint32_t pwHistLen;
-       uint32_t current_history_len;
+       bool ok;
 
        if (!plaintext)
                return False;
@@ -952,8 +1009,10 @@ bool pdb_set_plaintext_passwd(struct samu *sampass, const char *plaintext)
        /* 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)) 
+       if (!pdb_set_nt_passwd (sampass, new_nt_p16, PDB_CHANGED)) {
+               ZERO_STRUCT(new_nt_p16);
                return False;
+       }
 
        if (!E_deshash(plaintext, new_lanman_p16)) {
                /* E_deshash returns false for 'long' passwords (> 14
@@ -961,18 +1020,45 @@ bool pdb_set_plaintext_passwd(struct samu *sampass, const char *plaintext)
                   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)) 
+               if (!pdb_set_lanman_passwd (sampass, NULL, PDB_CHANGED)) {
+                       ZERO_STRUCT(new_nt_p16);
+                       ZERO_STRUCT(new_lanman_p16);
                        return False;
+               }
        } else {
-               if (!pdb_set_lanman_passwd (sampass, new_lanman_p16, PDB_CHANGED)) 
+               if (!pdb_set_lanman_passwd (sampass, new_lanman_p16, PDB_CHANGED)) {
+                       ZERO_STRUCT(new_nt_p16);
+                       ZERO_STRUCT(new_lanman_p16);
                        return False;
+               }
        }
+       ZERO_STRUCT(new_lanman_p16);
 
-       if (!pdb_set_plaintext_pw_only (sampass, plaintext, PDB_CHANGED)) 
+       if (!pdb_set_plaintext_pw_only (sampass, plaintext, PDB_CHANGED)) {
+               ZERO_STRUCT(new_nt_p16);
                return False;
+       }
 
-       if (!pdb_set_pass_last_set_time (sampass, time(NULL), PDB_CHANGED))
+       if (!pdb_set_pass_last_set_time (sampass, time(NULL), PDB_CHANGED)) {
+               ZERO_STRUCT(new_nt_p16);
                return False;
+       }
+
+       ok = pdb_update_history(sampass, new_nt_p16);
+       ZERO_STRUCT(new_nt_p16);
+       return ok;
+}
+
+/*********************************************************************
+ Update password history after change
+ ********************************************************************/
+
+bool pdb_update_history(struct samu *sampass, const uint8_t new_nt[NT_HASH_LEN])
+{
+       uchar *pwhistory;
+       uint32_t pwHistLen;
+       uint32_t current_history_len;
+       const uint8_t *current_history;
 
        if ((pdb_get_acct_ctrl(sampass) & ACB_NORMAL) == 0) {
                /*
@@ -995,33 +1081,27 @@ bool pdb_set_plaintext_passwd(struct samu *sampass, const char *plaintext)
         * the pw_history was first loaded into the struct samu struct
         * and now.... JRA.
         */
-       pwhistory = (uchar *)pdb_get_pw_history(sampass, &current_history_len);
-
-       if ((current_history_len != 0) && (pwhistory == NULL)) {
-               DEBUG(1, ("pdb_set_plaintext_passwd: pwhistory == NULL!\n"));
+       current_history = pdb_get_pw_history(sampass, &current_history_len);
+       if ((current_history_len != 0) && (current_history == NULL)) {
+               DEBUG(1, ("pdb_update_history: pwhistory == NULL!\n"));
                return false;
        }
 
-       if (current_history_len < pwHistLen) {
-               /*
-                * Ensure we have space for the needed history. This
-                * also takes care of an account which did not have
-                * any history at all so far, i.e. pwhistory==NULL
-                */
-               uchar *new_history = talloc_zero_array(
+       /*
+        * Ensure we have space for the needed history. This
+        * also takes care of an account which did not have
+        * any history at all so far, i.e. pwhistory==NULL
+        */
+       pwhistory = talloc_zero_array(
                        sampass, uchar,
                        pwHistLen*PW_HISTORY_ENTRY_LEN);
-
-               if (!new_history) {
-                       return False;
-               }
-
-               memcpy(new_history, pwhistory,
-                      current_history_len*PW_HISTORY_ENTRY_LEN);
-
-               pwhistory = new_history;
+       if (!pwhistory) {
+               return false;
        }
 
+       memcpy(pwhistory, current_history,
+              current_history_len*PW_HISTORY_ENTRY_LEN);
+
        /*
         * Make room for the new password in the history list.
         */
@@ -1044,11 +1124,12 @@ bool pdb_set_plaintext_passwd(struct samu *sampass, const char *plaintext)
         * The old format was to store the md5 hash of
         * the salt+newpw.
         */
-       memcpy(&pwhistory[PW_HISTORY_SALT_LEN], new_nt_p16, SALTED_MD5_HASH_LEN);
+       memcpy(&pwhistory[PW_HISTORY_SALT_LEN], new_nt, SALTED_MD5_HASH_LEN);
 
        pdb_set_pw_history(sampass, pwhistory, pwHistLen, PDB_CHANGED);
 
        return True;
+
 }
 
 /* check for any PDB_SET/CHANGED field and fill the appropriate mask bit */
@@ -1057,3 +1138,26 @@ uint32_t pdb_build_fields_present(struct samu *sampass)
        /* value set to all for testing */
        return 0x00ffffff;
 }
+
+/**********************************************************************
+ Helper function to determine for update_sam_account whether
+ we need LDAP modification.
+*********************************************************************/
+
+bool pdb_element_is_changed(const struct samu *sampass,
+                           enum pdb_elements element)
+{
+       return IS_SAM_CHANGED(sampass, element);
+}
+
+/**********************************************************************
+ Helper function to determine for update_sam_account whether
+ we need LDAP modification.
+ *********************************************************************/
+
+bool pdb_element_is_set_or_changed(const struct samu *sampass,
+                                  enum pdb_elements element)
+{
+       return (IS_SAM_SET(sampass, element) ||
+               IS_SAM_CHANGED(sampass, element));
+}