RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / smbd / chgpasswd.c
index e3df8a11d02e459f3a042dfd0fdcfb42bfa09588..fb6e6c0f09125bb9180b3da48c824aee5dc938d6 100644 (file)
@@ -6,7 +6,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -15,8 +15,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /* These comments regard the code to change the user's unix password: */
 
 #include "includes.h"
 
-#ifdef HAVE_WORKING_CRACKLIB
-#include <crack.h>
-
-#ifndef HAVE_CRACKLIB_DICTPATH
-#ifndef CRACKLIB_DICTPATH
-#define CRACKLIB_DICTPATH SAMBA_CRACKLIB_DICTPATH
-#endif
-#endif
-#endif
-
 extern struct passdb_ops pdb_ops;
 
 static NTSTATUS check_oem_password(const char *user,
@@ -65,7 +54,7 @@ static NTSTATUS check_oem_password(const char *user,
                                   const uchar old_lm_hash_encrypted[16],
                                   uchar password_encrypted_with_nt_hash[516], 
                                   const uchar old_nt_hash_encrypted[16],
-                                  SAM_ACCOUNT **hnd, char *new_passwd,
+                                  struct samu **hnd, char *new_passwd,
                                   int new_passwd_size);
 
 #if ALLOW_CHANGE_PASSWORD
@@ -74,7 +63,7 @@ static int findpty(char **slave)
 {
        int master;
        static fstring line;
-       DIR *dirp;
+       SMB_STRUCT_DIR *dirp;
        const char *dpname;
 
 #if defined(HAVE_GRANTPT)
@@ -103,7 +92,7 @@ static int findpty(char **slave)
 
        fstrcpy(line, "/dev/ptyXX");
 
-       dirp = opendir("/dev");
+       dirp = sys_opendir("/dev");
        if (!dirp)
                return (-1);
        while ((dpname = readdirname(dirp)) != NULL)
@@ -120,17 +109,17 @@ static int findpty(char **slave)
                                DEBUG(3, ("pty: opened %s\n", line));
                                line[5] = 't';
                                *slave = line;
-                               closedir(dirp);
+                               sys_closedir(dirp);
                                return (master);
                        }
                }
        }
-       closedir(dirp);
+       sys_closedir(dirp);
        return (-1);
 }
 
 static int dochild(int master, const char *slavedev, const struct passwd *pass,
-                  const char *passwordprogram, BOOL as_root)
+                  const char *passwordprogram, bool as_root)
 {
        int slave;
        struct termios stermios;
@@ -163,9 +152,13 @@ static int dochild(int master, const char *slavedev, const struct passwd *pass,
                DEBUG(3, ("More weirdness, could not open %s\n", slavedev));
                return (False);
        }
-#ifdef I_PUSH
-       ioctl(slave, I_PUSH, "ptem");
-       ioctl(slave, I_PUSH, "ldterm");
+#if defined(I_PUSH) && defined(I_FIND)
+       if (ioctl(slave, I_FIND, "ptem") == 0) {
+               ioctl(slave, I_PUSH, "ptem");
+       }
+       if (ioctl(slave, I_FIND, "ldterm") == 0) {
+               ioctl(slave, I_PUSH, "ldterm");
+       }
 #elif defined(TIOCSCTTY)
        if (ioctl(slave, TIOCSCTTY, 0) < 0)
        {
@@ -240,14 +233,14 @@ static int expect(int master, char *issue, char *expected)
 {
        pstring buffer;
        int attempts, timeout, nread, len;
-       BOOL match = False;
+       bool match = False;
 
        for (attempts = 0; attempts < 2; attempts++) {
                if (!strequal(issue, ".")) {
                        if (lp_passwd_chat_debug())
                                DEBUG(100, ("expect: sending [%s]\n", issue));
 
-                       if ((len = write(master, issue, strlen(issue))) != strlen(issue)) {
+                       if ((len = sys_write(master, issue, strlen(issue))) != strlen(issue)) {
                                DEBUG(2,("expect: (short) write returned %d\n", len ));
                                return False;
                        }
@@ -273,7 +266,7 @@ static int expect(int master, char *issue, char *expected)
                                pstrcpy( str, buffer);
                                trim_char( str, ' ', ' ');
 
-                               if ((match = (unix_wild_match(expected, str) == 0))) {
+                               if ((match = unix_wild_match(expected, str)) == True) {
                                        /* Now data has started to return, lower timeout. */
                                        timeout = lp_passwd_chat_timeout() * 100;
                                }
@@ -338,14 +331,14 @@ static int talktochild(int master, const char *seq)
        return (count > 0);
 }
 
-static BOOL chat_with_program(char *passwordprogram, struct passwd *pass,
-                             char *chatsequence, BOOL as_root)
+static bool chat_with_program(char *passwordprogram, const struct passwd *pass,
+                             char *chatsequence, bool as_root)
 {
        char *slavedev;
        int master;
        pid_t pid, wpid;
        int wstat;
-       BOOL chstat = False;
+       bool chstat = False;
 
        if (pass == NULL) {
                DEBUG(0, ("chat_with_program: user doesn't exist in the UNIX password database.\n"));
@@ -421,9 +414,10 @@ while we were waiting\n", WTERMSIG(wstat)));
                /* CHILD */
 
                /*
-                * Lose any oplock capabilities.
+                * Lose any elevated privileges.
                 */
-               oplock_set_capability(False, False);
+               drop_effective_capability(KERNEL_OPLOCK_CAPABILITY);
+               drop_effective_capability(DMAPI_ACCESS_CAPABILITY);
 
                /* make sure it doesn't freeze */
                alarm(20);
@@ -452,8 +446,8 @@ while we were waiting\n", WTERMSIG(wstat)));
        return (chstat);
 }
 
-BOOL chgpasswd(const char *name, const struct passwd *pass, 
-              const char *oldpass, const char *newpass, BOOL as_root)
+bool chgpasswd(const char *name, const struct passwd *pass, 
+              const char *oldpass, const char *newpass, bool as_root)
 {
        pstring passwordprogram;
        pstring chatsequence;
@@ -466,7 +460,7 @@ BOOL chgpasswd(const char *name, const struct passwd *pass,
 
        DEBUG(3, ("chgpasswd: Password change (as_root=%s) for user: %s\n", BOOLSTR(as_root), name));
 
-#if DEBUG_PASSWORD
+#ifdef DEBUG_PASSWORD
        DEBUG(100, ("chgpasswd: Passwords: old=%s new=%s\n", oldpass, newpass));
 #endif
 
@@ -502,7 +496,7 @@ BOOL chgpasswd(const char *name, const struct passwd *pass,
        
 #ifdef WITH_PAM
        if (lp_pam_password_change()) {
-               BOOL ret;
+               bool ret;
 
                if (as_root)
                        become_root();
@@ -542,7 +536,7 @@ BOOL chgpasswd(const char *name, const struct passwd *pass,
 
        if (as_root) {
                /* The password program *must* contain the user name to work. Fail if not. */
-               if (strstr(passwordprogram, "%u") == NULL) {
+               if (strstr_m(passwordprogram, "%u") == NULL) {
                        DEBUG(0,("chgpasswd: Running as root the 'passwd program' parameter *MUST* contain \
 the string %%u, and the given string %s does not.\n", passwordprogram ));
                        return False;
@@ -563,8 +557,8 @@ the string %%u, and the given string %s does not.\n", passwordprogram ));
 
 #else /* ALLOW_CHANGE_PASSWORD */
 
-BOOL chgpasswd(const char *name, const struct passwd *pass, 
-              const char *oldpass, const char *newpass, BOOL as_root)
+bool chgpasswd(const char *name, const struct passwd *pass, 
+              const char *oldpass, const char *newpass, bool as_root)
 {
        DEBUG(0, ("chgpasswd: Unix Password changing not compiled in (user=%s)\n", name));
        return (False);
@@ -575,15 +569,20 @@ BOOL chgpasswd(const char *name, const struct passwd *pass,
  Code to check the lanman hashed password.
 ************************************************************/
 
-BOOL check_lanman_password(char *user, uchar * pass1,
-                          uchar * pass2, SAM_ACCOUNT **hnd)
+bool check_lanman_password(char *user, uchar * pass1,
+                          uchar * pass2, struct samu **hnd)
 {
        uchar unenc_new_pw[16];
        uchar unenc_old_pw[16];
-       SAM_ACCOUNT *sampass = NULL;
-       uint16 acct_ctrl;
+       struct samu *sampass = NULL;
+       uint32 acct_ctrl;
        const uint8 *lanman_pw;
-       BOOL ret;
+       bool ret;
+
+       if ( !(sampass = samu_new(NULL)) ) {
+               DEBUG(0, ("samu_new() failed!\n"));
+               return False;
+       }
        
        become_root();
        ret = pdb_getsampwnam(sampass, user);
@@ -591,7 +590,7 @@ BOOL check_lanman_password(char *user, uchar * pass1,
 
        if (ret == False) {
                DEBUG(0,("check_lanman_password: getsampwnam returned NULL\n"));
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return False;
        }
        
@@ -600,7 +599,7 @@ BOOL check_lanman_password(char *user, uchar * pass1,
 
        if (acct_ctrl & ACB_DISABLED) {
                DEBUG(0,("check_lanman_password: account %s disabled.\n", user));
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return False;
        }
 
@@ -611,7 +610,7 @@ BOOL check_lanman_password(char *user, uchar * pass1,
                        return True;
                } else {
                        DEBUG(0, ("check_lanman_password: no lanman password !\n"));
-                       pdb_free_sam(&sampass);
+                       TALLOC_FREE(sampass);
                        return False;
                }
        }
@@ -625,7 +624,7 @@ BOOL check_lanman_password(char *user, uchar * pass1,
        /* Check that the two old passwords match. */
        if (memcmp(lanman_pw, unenc_old_pw, 16)) {
                DEBUG(0,("check_lanman_password: old password doesn't match.\n"));
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return False;
        }
 
@@ -642,12 +641,12 @@ BOOL check_lanman_password(char *user, uchar * pass1,
  is correct before calling. JRA.
 ************************************************************/
 
-BOOL change_lanman_password(SAM_ACCOUNT *sampass, uchar *pass2)
+bool change_lanman_password(struct samu *sampass, uchar *pass2)
 {
        static uchar null_pw[16];
        uchar unenc_new_pw[16];
-       BOOL ret;
-       uint16 acct_ctrl;
+       bool ret;
+       uint32 acct_ctrl;
        const uint8 *pwd;
 
        if (sampass == NULL) {
@@ -689,14 +688,14 @@ BOOL change_lanman_password(SAM_ACCOUNT *sampass, uchar *pass2)
                return False;   /* We lose the NT hash. Sorry. */
        }
 
-       if (!pdb_set_pass_changed_now  (sampass)) {
-               pdb_free_sam(&sampass);
+       if (!pdb_set_pass_last_set_time  (sampass, time(NULL), PDB_CHANGED)) {
+               TALLOC_FREE(sampass);
                /* Not quite sure what this one qualifies as, but this will do */
                return False; 
        }
  
        /* Now flush the sam_passwd struct to persistent storage */
-       ret = pdb_update_sam_account (sampass);
+       ret = NT_STATUS_IS_OK(pdb_update_sam_account (sampass));
 
        return ret;
 }
@@ -709,10 +708,11 @@ NTSTATUS pass_oem_change(char *user,
                         uchar password_encrypted_with_lm_hash[516], 
                         const uchar old_lm_hash_encrypted[16],
                         uchar password_encrypted_with_nt_hash[516], 
-                        const uchar old_nt_hash_encrypted[16])
+                        const uchar old_nt_hash_encrypted[16],
+                        uint32 *reject_reason)
 {
        pstring new_passwd;
-       SAM_ACCOUNT *sampass = NULL;
+       struct samu *sampass = NULL;
        NTSTATUS nt_status = check_oem_password(user, password_encrypted_with_lm_hash, 
                                                old_lm_hash_encrypted, 
                                                password_encrypted_with_nt_hash, 
@@ -724,12 +724,12 @@ NTSTATUS pass_oem_change(char *user,
 
        /* We've already checked the old password here.... */
        become_root();
-       nt_status = change_oem_password(sampass, NULL, new_passwd, True);
+       nt_status = change_oem_password(sampass, NULL, new_passwd, True, reject_reason);
        unbecome_root();
 
        memset(new_passwd, 0, sizeof(new_passwd));
 
-       pdb_free_sam(&sampass);
+       TALLOC_FREE(sampass);
 
        return nt_status;
 }
@@ -751,30 +751,31 @@ static NTSTATUS check_oem_password(const char *user,
                                   const uchar old_lm_hash_encrypted[16],
                                   uchar password_encrypted_with_nt_hash[516], 
                                   const uchar old_nt_hash_encrypted[16],
-                                  SAM_ACCOUNT **hnd, char *new_passwd,
+                                  struct samu **hnd, char *new_passwd,
                                   int new_passwd_size)
 {
        static uchar null_pw[16];
        static uchar null_ntpw[16];
-       SAM_ACCOUNT *sampass = NULL;
-       char *password_encrypted;
-       const char *encryption_key;
+       struct samu *sampass = NULL;
+       uint8 *password_encrypted;
+       const uint8 *encryption_key;
        const uint8 *lanman_pw, *nt_pw;
-       uint16 acct_ctrl;
+       uint32 acct_ctrl;
        uint32 new_pw_len;
        uchar new_nt_hash[16];
-       uchar old_nt_hash_plain[16];
        uchar new_lm_hash[16];
-       uchar old_lm_hash_plain[16];
+       uchar verifier[16];
        char no_pw[2];
-       BOOL ret;
+       bool ret;
 
-       BOOL nt_pass_set = (password_encrypted_with_nt_hash && old_nt_hash_encrypted);
-       BOOL lm_pass_set = (password_encrypted_with_lm_hash && old_lm_hash_encrypted);
+       bool nt_pass_set = (password_encrypted_with_nt_hash && old_nt_hash_encrypted);
+       bool lm_pass_set = (password_encrypted_with_lm_hash && old_lm_hash_encrypted);
 
        *hnd = NULL;
 
-       pdb_init_sam(&sampass);
+       if ( !(sampass = samu_new( NULL )) ) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        become_root();
        ret = pdb_getsampwnam(sampass, user);
@@ -782,7 +783,7 @@ static NTSTATUS check_oem_password(const char *user,
 
        if (ret == False) {
                DEBUG(0, ("check_oem_password: getsmbpwnam returned NULL\n"));
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return NT_STATUS_NO_SUCH_USER; 
        }
 
@@ -790,11 +791,11 @@ static NTSTATUS check_oem_password(const char *user,
        
        if (acct_ctrl & ACB_DISABLED) {
                DEBUG(2,("check_lanman_password: account %s disabled.\n", user));
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return NT_STATUS_ACCOUNT_DISABLED;
        }
 
-       if (acct_ctrl & ACB_PWNOTREQ && lp_null_passwords()) {
+       if ((acct_ctrl & ACB_PWNOTREQ) && lp_null_passwords()) {
                /* construct a null password (in case one is needed */
                no_pw[0] = 0;
                no_pw[1] = 0;
@@ -825,17 +826,22 @@ static NTSTATUS check_oem_password(const char *user,
        } else if (nt_pass_set) {
                DEBUG(1, ("NT password change supplied for user %s, but we have no NT password to check it with\n", 
                          user));
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return NT_STATUS_WRONG_PASSWORD;        
        } else if (lm_pass_set) {
-               DEBUG(1, ("LM password change supplied for user %s, but we have no LanMan password to check it with\n", 
-                         user));
-               pdb_free_sam(&sampass);
+               if (lp_lanman_auth()) {
+                       DEBUG(1, ("LM password change supplied for user %s, but we have no LanMan password to check it with\n", 
+                                 user));
+               } else {
+                       DEBUG(1, ("LM password change supplied for user %s, but we have disabled LanMan authentication\n", 
+                                 user));
+               }
+               TALLOC_FREE(sampass);
                return NT_STATUS_WRONG_PASSWORD;
        } else {
                DEBUG(1, ("password change requested for user %s, but no password supplied!\n", 
                          user));
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return NT_STATUS_WRONG_PASSWORD;
        }
 
@@ -846,7 +852,7 @@ static NTSTATUS check_oem_password(const char *user,
 
        if ( !decode_pw_buffer(password_encrypted, new_passwd, new_passwd_size, &new_pw_len, 
                               nt_pass_set ? STR_UNICODE : STR_ASCII)) {
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
                return NT_STATUS_WRONG_PASSWORD;
        }
 
@@ -864,14 +870,12 @@ static NTSTATUS check_oem_password(const char *user,
 
                if (nt_pw) {
                        /*
-                        * Now use new_nt_hash as the key to see if the old
-                        * password matches.
+                        * check the NT verifier
                         */
-                       D_P16(new_nt_hash, old_nt_hash_encrypted, old_nt_hash_plain);
-                       
-                       if (memcmp(nt_pw, old_nt_hash_plain, 16)) {
+                       E_old_pw_hash(new_nt_hash, nt_pw, verifier);
+                       if (memcmp(verifier, old_nt_hash_encrypted, 16)) {
                                DEBUG(0,("check_oem_password: old lm password doesn't match.\n"));
-                               pdb_free_sam(&sampass);
+                               TALLOC_FREE(sampass);
                                return NT_STATUS_WRONG_PASSWORD;
                        }
                        
@@ -894,14 +898,12 @@ static NTSTATUS check_oem_password(const char *user,
                
                if (lanman_pw) {
                        /*
-                        * Now use new_nt_hash as the key to see if the old
-                        * LM password matches.
+                        * check the lm verifier
                         */
-                       D_P16(new_nt_hash, old_lm_hash_encrypted, old_lm_hash_plain);
-                       
-                       if (memcmp(lanman_pw, old_lm_hash_plain, 16)) {
+                       E_old_pw_hash(new_nt_hash, lanman_pw, verifier);
+                       if (memcmp(verifier, old_lm_hash_encrypted, 16)) {
                                DEBUG(0,("check_oem_password: old lm password doesn't match.\n"));
-                               pdb_free_sam(&sampass);
+                               TALLOC_FREE(sampass);
                                return NT_STATUS_WRONG_PASSWORD;
                        }
 #ifdef DEBUG_PASSWORD
@@ -918,14 +920,12 @@ static NTSTATUS check_oem_password(const char *user,
                E_deshash(new_passwd, new_lm_hash);
 
                /*
-                * Now use new_lm_hash as the key to see if the old
-                * password matches.
+                * check the lm verifier
                 */
-               D_P16(new_lm_hash, old_lm_hash_encrypted, old_lm_hash_plain);
-               
-               if (memcmp(lanman_pw, old_lm_hash_plain, 16)) {
+               E_old_pw_hash(new_lm_hash, lanman_pw, verifier);
+               if (memcmp(verifier, old_lm_hash_encrypted, 16)) {
                        DEBUG(0,("check_oem_password: old lm password doesn't match.\n"));
-                       pdb_free_sam(&sampass);
+                       TALLOC_FREE(sampass);
                        return NT_STATUS_WRONG_PASSWORD;
                }
                
@@ -938,10 +938,76 @@ static NTSTATUS check_oem_password(const char *user,
        }
 
        /* should not be reached */
-       pdb_free_sam(&sampass);
+       TALLOC_FREE(sampass);
        return NT_STATUS_WRONG_PASSWORD;
 }
 
+/***********************************************************
+ This routine takes the given password and checks it against
+ the password history. Returns True if this password has been
+ found in the history list.
+************************************************************/
+
+static bool check_passwd_history(struct samu *sampass, const char *plaintext)
+{
+       uchar new_nt_p16[NT_HASH_LEN];
+       uchar zero_md5_nt_pw[SALTED_MD5_HASH_LEN];
+       const uint8 *nt_pw;
+       const uint8 *pwhistory;
+       bool found = False;
+       int i;
+       uint32 pwHisLen, curr_pwHisLen;
+
+       pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHisLen);
+       if (pwHisLen == 0) {
+               return False;
+       }
+
+       pwhistory = pdb_get_pw_history(sampass, &curr_pwHisLen);
+       if (!pwhistory || curr_pwHisLen == 0) {
+               return False;
+       }
+
+       /* Only examine the minimum of the current history len and
+          the stored history len. Avoids race conditions. */
+       pwHisLen = MIN(pwHisLen,curr_pwHisLen);
+
+       nt_pw = pdb_get_nt_passwd(sampass);
+
+       E_md4hash(plaintext, new_nt_p16);
+
+       if (!memcmp(nt_pw, new_nt_p16, NT_HASH_LEN)) {
+               DEBUG(10,("check_passwd_history: proposed new password for user %s is the same as the current password !\n",
+                       pdb_get_username(sampass) ));
+               return True;
+       }
+
+       dump_data(100, new_nt_p16, NT_HASH_LEN);
+       dump_data(100, pwhistory, PW_HISTORY_ENTRY_LEN*pwHisLen);
+
+       memset(zero_md5_nt_pw, '\0', SALTED_MD5_HASH_LEN);
+       for (i=0; i<pwHisLen; i++) {
+               uchar new_nt_pw_salted_md5_hash[SALTED_MD5_HASH_LEN];
+               const uchar *current_salt = &pwhistory[i*PW_HISTORY_ENTRY_LEN];
+               const uchar *old_nt_pw_salted_md5_hash = &pwhistory[(i*PW_HISTORY_ENTRY_LEN)+
+                                                       PW_HISTORY_SALT_LEN];
+               if (!memcmp(zero_md5_nt_pw, old_nt_pw_salted_md5_hash, SALTED_MD5_HASH_LEN)) {
+                       /* Ignore zero valued entries. */
+                       continue;
+               }
+               /* Create salted versions of new to compare. */
+               E_md5hash(current_salt, new_nt_p16, new_nt_pw_salted_md5_hash);
+
+               if (!memcmp(new_nt_pw_salted_md5_hash, old_nt_pw_salted_md5_hash, SALTED_MD5_HASH_LEN)) {
+                       DEBUG(1,("check_passwd_history: proposed new password for user %s found in history list !\n",
+                               pdb_get_username(sampass) ));
+                       found = True;
+                       break;
+               }
+       }
+       return found;
+}
+
 /***********************************************************
  Code to change the oem password. Changes both the lanman
  and NT hashes.  Old_passwd is almost always NULL.
@@ -949,79 +1015,94 @@ static NTSTATUS check_oem_password(const char *user,
  is correct before calling. JRA.
 ************************************************************/
 
-NTSTATUS change_oem_password(SAM_ACCOUNT *hnd, char *old_passwd, char *new_passwd, BOOL as_root)
+NTSTATUS change_oem_password(struct samu *hnd, char *old_passwd, char *new_passwd, bool as_root, uint32 *samr_reject_reason)
 {
-       struct passwd *pass;
-
-       BOOL ret;
        uint32 min_len;
+       uint32 refuse;
+       struct passwd *pass = NULL;
+       const char *username = pdb_get_username(hnd);
+       time_t can_change_time = pdb_get_pass_can_change_time(hnd);
 
-       if (time(NULL) < pdb_get_pass_can_change_time(hnd)) {
-               DEBUG(1, ("user %s cannot change password now, must wait until %s\n", 
-                         pdb_get_username(hnd), http_timestring(pdb_get_pass_can_change_time(hnd))));
-               return NT_STATUS_PASSWORD_RESTRICTION;
+       if (samr_reject_reason) {
+               *samr_reject_reason = Undefined;
+       }
+
+       /* check to see if the secdesc has previously been set to disallow */
+       if (!pdb_get_pass_can_change(hnd)) {
+               DEBUG(1, ("user %s does not have permissions to change password\n", username));
+               if (samr_reject_reason) {
+                       *samr_reject_reason = REJECT_REASON_OTHER;
+               }
+               return NT_STATUS_ACCOUNT_RESTRICTION;
+       }
+
+       /* check to see if it is a Machine account and if the policy
+        * denies machines to change the password. *
+        * Should we deny also SRVTRUST and/or DOMSTRUST ? .SSS. */
+       if (pdb_get_acct_ctrl(hnd) & ACB_WSTRUST) {
+               if (pdb_get_account_policy(AP_REFUSE_MACHINE_PW_CHANGE, &refuse) && refuse) {
+                       DEBUG(1, ("Machine %s cannot change password now, "
+                                 "denied by Refuse Machine Password Change policy\n",
+                                 username));
+                       if (samr_reject_reason) {
+                               *samr_reject_reason = REJECT_REASON_OTHER;
+                       }
+                       return NT_STATUS_ACCOUNT_RESTRICTION;
+               }
+       }
+
+       /* removed calculation here, becuase passdb now calculates
+          based on policy.  jmcd */
+       if ((can_change_time != 0) && (time(NULL) < can_change_time)) {
+               DEBUG(1, ("user %s cannot change password now, must "
+                         "wait until %s\n", username,
+                         http_timestring(can_change_time)));
+               if (samr_reject_reason) {
+                       *samr_reject_reason = REJECT_REASON_OTHER;
+               }
+               return NT_STATUS_ACCOUNT_RESTRICTION;
        }
 
-       if (account_policy_get(AP_MIN_PASSWORD_LEN, &min_len) && (strlen(new_passwd) < min_len)) {
+       if (pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &min_len) && (str_charnum(new_passwd) < min_len)) {
                DEBUG(1, ("user %s cannot change password - password too short\n", 
-                         pdb_get_username(hnd)));
+                         username));
                DEBUGADD(1, (" account policy min password len = %d\n", min_len));
+               if (samr_reject_reason) {
+                       *samr_reject_reason = REJECT_REASON_TOO_SHORT;
+               }
                return NT_STATUS_PASSWORD_RESTRICTION;
 /*             return NT_STATUS_PWD_TOO_SHORT; */
        }
 
-       /* Take the passed information and test it for minimum criteria */
-       /* Minimum password length */
-       if (strlen(new_passwd) < lp_min_passwd_length()) {
-               /* too short, must be at least MINPASSWDLENGTH */
-               DEBUG(1, ("Password Change: user %s, New password is shorter than minimum password length = %d\n",
-                      pdb_get_username(hnd), lp_min_passwd_length()));
+       if (check_passwd_history(hnd,new_passwd)) {
+               if (samr_reject_reason) {
+                       *samr_reject_reason = REJECT_REASON_IN_HISTORY;
+               }
                return NT_STATUS_PASSWORD_RESTRICTION;
-/*             return NT_STATUS_PWD_TOO_SHORT; */
        }
 
-       pass = Get_Pwnam(pdb_get_username(hnd));
+       pass = Get_Pwnam(username);
        if (!pass) {
-               DEBUG(1, ("check_oem_password: Username does not exist in system !?!\n"));
+               DEBUG(1, ("change_oem_password: Username %s does not exist in system !?!\n", username));
+               return NT_STATUS_ACCESS_DENIED;
        }
 
-#ifdef HAVE_WORKING_CRACKLIB
-       if (pass) {
-               /* if we can, become the user to overcome internal cracklib sillyness */
-               if (!push_sec_ctx())
-                       return NT_STATUS_UNSUCCESSFUL;
-               
-               set_sec_ctx(pass->pw_uid, pass->pw_gid, 0, NULL, NULL);
-               set_re_uid();
-       }
-
-       if (lp_use_cracklib()) {
-               const char *crack_check_reason;
-               DEBUG(4, ("change_oem_password: Checking password for user [%s]"
-                         " against cracklib. \n", pdb_get_username(hnd)));
-               DEBUGADD(4, ("If this is your last message, then something is "
-                            "wrong with cracklib, it might be missing it's "
-                            "dictionaries at %s\n", 
-                            CRACKLIB_DICTPATH));
-               dbgflush();
-
-               crack_check_reason = FascistCheck(new_passwd, (char *)CRACKLIB_DICTPATH);
-               if (crack_check_reason) {
-                       DEBUG(1, ("Password Change: user [%s], "
-                                 "New password failed cracklib test - %s\n",
-                         pdb_get_username(hnd), crack_check_reason));
-                       
-                       /* get back to where we should be */
-                       if (pass)
-                               pop_sec_ctx();
+       /* Use external script to check password complexity */
+       if (lp_check_password_script() && *(lp_check_password_script())) {
+               int check_ret;
+
+               check_ret = smbrunsecret(lp_check_password_script(), new_passwd);
+               DEBUG(5, ("change_oem_password: check password script (%s) returned [%d]\n", lp_check_password_script(), check_ret));
+
+               if (check_ret != 0) {
+                       DEBUG(1, ("change_oem_password: check password script said new password is not good enough!\n"));
+                       if (samr_reject_reason) {
+                               *samr_reject_reason = REJECT_REASON_NOT_COMPLEX;
+                       }
                        return NT_STATUS_PASSWORD_RESTRICTION;
                }
        }
 
-       if (pass)
-               pop_sec_ctx();
-#endif
-
        /*
         * If unix password sync was requested, attempt to change
         * the /etc/passwd database first. Return failure if this cannot
@@ -1035,7 +1116,7 @@ NTSTATUS change_oem_password(SAM_ACCOUNT *hnd, char *old_passwd, char *new_passw
         */
        
        if(lp_unix_password_sync() &&
-               !chgpasswd(pdb_get_username(hnd), pass, old_passwd, new_passwd, as_root)) {
+               !chgpasswd(username, pass, old_passwd, new_passwd, as_root)) {
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -1044,11 +1125,5 @@ NTSTATUS change_oem_password(SAM_ACCOUNT *hnd, char *old_passwd, char *new_passw
        }
 
        /* Now write it into the file. */
-       ret = pdb_update_sam_account (hnd);
-
-       if (!ret) {
-               return NT_STATUS_ACCESS_DENIED;
-       }
-       
-       return NT_STATUS_OK;
+       return pdb_update_sam_account (hnd);
 }