Kill off the old varient of 'check_plaintext_password' (new version just
[ira/wip.git] / source3 / smbd / chgpasswd.c
index 8dff2f7d817e1ab32eb0235ea325f97d627ae599..5e646b622580444311ab7e15a1a189e156f50a69 100644 (file)
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
 extern struct passdb_ops pdb_ops;
 
-static BOOL check_oem_password(char *user,
-                              uchar * lmdata, uchar * lmhash,
-                              uchar * ntdata, uchar * nthash,
+static BOOL check_oem_password(const char *user,
+                              uchar * lmdata, const uchar * lmhash,
+                              const uchar * ntdata, const uchar * nthash,
                               SAM_ACCOUNT **hnd, char *new_passwd,
                               int new_passwd_size);
 
@@ -119,20 +118,18 @@ static int findpty(char **slave)
        return (-1);
 }
 
-static int dochild(int master, char *slavedev, char *name,
-                  char *passwordprogram, BOOL as_root)
+static int dochild(int master, const char *slavedev, const struct passwd *pass,
+                  const char *passwordprogram, BOOL as_root)
 {
        int slave;
        struct termios stermios;
-       struct passwd *pass = Get_Pwnam(name, True);
        gid_t gid;
        uid_t uid;
 
        if (pass == NULL)
        {
                DEBUG(0,
-                     ("dochild: user name %s doesn't exist in the UNIX password database.\n",
-                      name));
+                     ("dochild: user doesn't exist in the UNIX password database.\n"));
                return False;
        }
 
@@ -200,7 +197,7 @@ static int dochild(int master, char *slavedev, char *name,
        }
        stermios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
        stermios.c_lflag |= ICANON;
-       stermios.c_oflag &= ~(ONLCR);
+       stermios.c_oflag &= ~(ONLCR);
        if (tcsetattr(0, TCSANOW, &stermios) < 0)
        {
                DEBUG(3, ("could not set attributes of pty\n"));
@@ -232,14 +229,15 @@ static int expect(int master, char *issue, char *expected)
        int attempts, timeout, nread, len;
        BOOL match = False;
 
-       for (attempts = 0; attempts < 2; attempts++)
-       {
-               if (!strequal(issue, "."))
-               {
+       for (attempts = 0; attempts < 2; attempts++) {
+               if (!strequal(issue, ".")) {
                        if (lp_passwd_chat_debug())
                                DEBUG(100, ("expect: sending [%s]\n", issue));
 
-                       write(master, issue, strlen(issue));
+                       if ((len = write(master, issue, strlen(issue))) != strlen(issue)) {
+                               DEBUG(2,("expect: (short) write returned %d\n", len ));
+                               return False;
+                       }
                }
 
                if (strequal(expected, "."))
@@ -251,29 +249,35 @@ static int expect(int master, char *issue, char *expected)
 
                while ((len = read_with_timeout(master, buffer + nread, 1,
                                                sizeof(buffer) - nread - 1,
-                                               timeout)) > 0)
-               {
+                                               timeout)) > 0) {
                        nread += len;
                        buffer[nread] = 0;
 
-                       if ((match = (wild_match(expected, buffer) == 0)))
-                               timeout = 200;
+                       {
+                               /* Eat leading/trailing whitespace before match. */
+                               pstring str;
+                               pstrcpy( str, buffer);
+                               trim_string( str, " ", " ");
+
+                               if ((match = (unix_wild_match(expected, str) == 0)))
+                                       timeout = 200;
+                       }
                }
 
                if (lp_passwd_chat_debug())
-                       DEBUG(100, ("expect: expected [%s] received [%s]\n",
-                                   expected, buffer));
+                       DEBUG(100, ("expect: expected [%s] received [%s] match %s\n",
+                                   expected, buffer, match ? "yes" : "no" ));
 
                if (match)
                        break;
 
-               if (len < 0)
-               {
+               if (len < 0) {
                        DEBUG(2, ("expect: %s\n", strerror(errno)));
                        return False;
                }
        }
 
+       DEBUG(10,("expect: returning %s\n", match ? "True" : "False" ));
        return match;
 }
 
@@ -308,11 +312,17 @@ static int talktochild(int master, char *seq)
 
                pwd_sub(issue);
        }
+       if (!strequal(issue, ".")) {
+               /* we have one final issue to send */
+               fstrcpy(expected, ".");
+               if (!expect(master, issue, expected))
+                       return False;
+       }
 
        return (count > 0);
 }
 
-static BOOL chat_with_program(char *passwordprogram, char *name,
+static BOOL chat_with_program(char *passwordprogram, struct passwd *pass,
                              char *chatsequence, BOOL as_root)
 {
        char *slavedev;
@@ -321,12 +331,19 @@ static BOOL chat_with_program(char *passwordprogram, char *name,
        int wstat;
        BOOL chstat = False;
 
+       if (pass == NULL)
+       {
+               DEBUG(0,
+                     ("chat_with_program: user doesn't exist in the UNIX password database.\n"));
+               return False;
+       }
+
        /* allocate a pseudo-terminal device */
        if ((master = findpty(&slavedev)) < 0)
        {
                DEBUG(3,
                      ("Cannot Allocate pty for password change: %s\n",
-                      name));
+                      pass->pw_name));
                return (False);
        }
 
@@ -341,7 +358,7 @@ static BOOL chat_with_program(char *passwordprogram, char *name,
        {
                DEBUG(3,
                      ("Cannot fork() child for password change: %s\n",
-                      name));
+                      pass->pw_name));
                close(master);
                CatchChild();
                return (False);
@@ -354,7 +371,7 @@ static BOOL chat_with_program(char *passwordprogram, char *name,
                {
                        DEBUG(3,
                              ("Child failed to change password: %s\n",
-                              name));
+                              pass->pw_name));
                        kill(pid, SIGKILL);     /* be sure to end this process */
                }
 
@@ -420,10 +437,10 @@ static BOOL chat_with_program(char *passwordprogram, char *name,
                        become_root();
 
                DEBUG(3,
-                     ("Dochild for user %s (uid=%d,gid=%d)\n", name,
+                     ("Dochild for user %s (uid=%d,gid=%d)\n", pass->pw_name,
                       (int)getuid(), (int)getgid()));
                chstat =
-                       dochild(master, slavedev, name, passwordprogram,
+                       dochild(master, slavedev, pass, passwordprogram,
                                as_root);
 
                if (as_root)
@@ -442,19 +459,20 @@ static BOOL chat_with_program(char *passwordprogram, char *name,
        if (chstat)
                DEBUG(3,
                      ("Password change %ssuccessful for user %s\n",
-                      (chstat ? "" : "un"), name));
+                      (chstat ? "" : "un"), pass->pw_name));
        return (chstat);
 }
 
 
-BOOL chgpasswd(char *name, char *oldpass, char *newpass, BOOL as_root)
+BOOL chgpasswd(const char *name, const char *oldpass, const char *newpass, BOOL as_root)
 {
        pstring passwordprogram;
        pstring chatsequence;
        size_t i;
        size_t len;
 
-       strlower(name);
+       struct passwd *pass;
+
        DEBUG(3, ("Password change for user: %s\n", name));
 
 #if DEBUG_PASSWORD
@@ -499,6 +517,8 @@ BOOL chgpasswd(char *name, char *oldpass, char *newpass, BOOL as_root)
                        return False;
                }
        }
+       
+       pass = Get_Pwnam(name);
 
 #ifdef WITH_PAM
        if (lp_pam_password_change()) {
@@ -507,8 +527,12 @@ BOOL chgpasswd(char *name, char *oldpass, char *newpass, BOOL as_root)
                if (as_root)
                        become_root();
 
-               ret = smb_pam_passchange(name, oldpass, newpass);
-
+               if (pass) {
+                       ret = smb_pam_passchange(pass->pw_name, oldpass, newpass);
+               } else {
+                       ret = smb_pam_passchange(name, oldpass, newpass);
+               }
+                       
                if (as_root)
                        unbecome_root();
 
@@ -516,19 +540,38 @@ BOOL chgpasswd(char *name, char *oldpass, char *newpass, BOOL as_root)
        }
 #endif
 
+       /* A non-PAM password change just doen't make sense without a valid local user */
+
+       if (pass == NULL)
+       {
+               DEBUG(0,
+                     ("chgpasswd: user %s doesn't exist in the UNIX password database.\n",
+                      name));
+               return False;
+       }
+
        pstrcpy(passwordprogram, lp_passwd_program());
        pstrcpy(chatsequence, lp_passwd_chat());
 
        if (!*chatsequence) {
-               DEBUG(2, ("Null chat sequence - no password changing\n"));
+               DEBUG(2, ("chgpasswd: Null chat sequence - no password changing\n"));
                return (False);
        }
 
        if (!*passwordprogram) {
-               DEBUG(2, ("Null password program - no password changing\n"));
+               DEBUG(2, ("chgpasswd: Null password program - no password changing\n"));
                return (False);
        }
 
+       if (as_root) {
+               /* The password program *must* contain the user name to work. Fail if not. */
+               if (strstr(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;
+               }
+       }
+
        pstring_sub(passwordprogram, "%u", name);
        /* note that we do NOT substitute the %o and %n in the password program
           as this would open up a security hole where the user could use
@@ -538,12 +581,12 @@ BOOL chgpasswd(char *name, char *oldpass, char *newpass, BOOL as_root)
        all_string_sub(chatsequence, "%o", oldpass, sizeof(pstring));
        all_string_sub(chatsequence, "%n", newpass, sizeof(pstring));
        return (chat_with_program
-               (passwordprogram, name, chatsequence, as_root));
+               (passwordprogram, pass, chatsequence, as_root));
 }
 
 #else /* ALLOW_CHANGE_PASSWORD */
 
-BOOL chgpasswd(char *name, char *oldpass, char *newpass, BOOL as_root)
+BOOL chgpasswd(const char *name, const char *oldpass, const char *newpass, BOOL as_root)
 {
        DEBUG(0, ("Password changing not compiled in (user=%s)\n", name));
        return (False);
@@ -557,12 +600,11 @@ BOOL chgpasswd(char *name, char *oldpass, char *newpass, BOOL as_root)
 BOOL check_lanman_password(char *user, uchar * pass1,
                           uchar * pass2, SAM_ACCOUNT **hnd)
 {
-       static uchar null_pw[16];
        uchar unenc_new_pw[16];
        uchar unenc_old_pw[16];
        SAM_ACCOUNT *sampass = NULL;
        uint16 acct_ctrl;
-       uint8 *lanman_pw;
+       const uint8 *lanman_pw;
        BOOL ret;
        
        become_root();
@@ -571,7 +613,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);
+               pdb_free_sam(&sampass);
                return False;
        }
        
@@ -580,20 +622,20 @@ 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);
+               pdb_free_sam(&sampass);
                return False;
        }
 
-       if ((lanman_pw == NULL) && (acct_ctrl & ACB_PWNOTREQ)) {
-               uchar no_pw[14];
-               memset(no_pw, '\0', 14);
-               E_P16(no_pw, null_pw);
-               pdb_set_lanman_passwd (sampass, null_pw);
-       }
-       else if (lanman_pw == NULL) {
-               DEBUG(0, ("check_lanman_password: no lanman password !\n"));
-               pdb_free_sam(sampass);
-               return False;
+       if (lanman_pw == NULL) {
+               if (acct_ctrl & ACB_PWNOTREQ) {
+                       /* this saves the pointer for the caller */
+                       *hnd = sampass;
+                       return True;
+               } else {
+                       DEBUG(0, ("check_lanman_password: no lanman password !\n"));
+                       pdb_free_sam(&sampass);
+                       return False;
+               }
        }
 
        /* Get the new lanman hash. */
@@ -605,13 +647,12 @@ 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);
+               pdb_free_sam(&sampass);
                return False;
        }
 
        /* this saves the pointer for the caller */
        *hnd = sampass;
-       
        return True;
 }
 
@@ -628,7 +669,7 @@ BOOL change_lanman_password(SAM_ACCOUNT *sampass, uchar * pass1,
        uchar unenc_new_pw[16];
        BOOL ret;
        uint16 acct_ctrl;
-       uint8 *pwd;
+       const uint8 *pwd;
 
        if (sampass == NULL) {
                DEBUG(0,("change_lanman_password: no smb password entry.\n"));
@@ -644,23 +685,37 @@ BOOL change_lanman_password(SAM_ACCOUNT *sampass, uchar * pass1,
                return False;
        }
 
-       if ((pwd == NULL) && (acct_ctrl & ACB_PWNOTREQ)) {
-               uchar no_pw[14];
-               memset(no_pw, '\0', 14);
-               E_P16(no_pw, null_pw);
-               pdb_set_lanman_passwd(sampass, null_pw);
+       if (pwd == NULL) { 
+               if (acct_ctrl & ACB_PWNOTREQ) {
+                       uchar no_pw[14];
+                       memset(no_pw, '\0', 14);
+                       E_P16(no_pw, null_pw);
+
+                       /* Get the new lanman hash. */
+                       D_P16(null_pw, pass2, unenc_new_pw);
+               } else {
+                       DEBUG(0,("change_lanman_password: no lanman password !\n"));
+                       return False;
+               }
+       } else {
+               /* Get the new lanman hash. */
+               D_P16(pwd, pass2, unenc_new_pw);
        }
-       else if (pwd == NULL) {
-               DEBUG(0,("change_lanman_password: no lanman password !\n"));
+
+       if (!pdb_set_lanman_passwd(sampass, unenc_new_pw)) {
                return False;
        }
 
-       /* Get the new lanman hash. */
-       D_P16(pwd, pass2, unenc_new_pw);
-
-       pdb_set_lanman_passwd(sampass, unenc_new_pw);
-       pdb_set_nt_passwd    (sampass, NULL);   /* We lose the NT hash. Sorry. */
+       if (!pdb_set_nt_passwd    (sampass, NULL)) {
+               return False;   /* We lose the NT hash. Sorry. */
+       }
 
+       if (!pdb_set_pass_changed_now  (sampass)) {
+               pdb_free_sam(&sampass);
+               /* Not quite sure what this one qualifies as, but this will do */
+               return False; 
+       }
        /* Now flush the sam_passwd struct to persistent storage */
        become_root();
        ret = pdb_update_sam_account (sampass, False);
@@ -677,6 +732,7 @@ BOOL pass_oem_change(char *user,
                     uchar * ntdata, uchar * nthash)
 {
        fstring new_passwd;
+       const char *unix_user;
        SAM_ACCOUNT *sampass = NULL;
        BOOL ret = check_oem_password(user, lmdata, lmhash, ntdata, nthash,
                                      &sampass, new_passwd, sizeof(new_passwd));
@@ -690,15 +746,17 @@ BOOL pass_oem_change(char *user,
         * available. JRA.
         */
 
-       if (ret && lp_unix_password_sync())
-               ret = chgpasswd(user, "", new_passwd, True);
+       unix_user = pdb_get_username(sampass);
+
+       if ((ret) && (unix_user) && (*unix_user) && lp_unix_password_sync())
+               ret = chgpasswd(unix_user, "", new_passwd, True);
 
        if (ret)
-               ret = change_oem_password(sampass, new_passwd, False);
+               ret = change_oem_password(sampass, new_passwd);
 
        memset(new_passwd, 0, sizeof(new_passwd));
 
-       pdb_free_sam(sampass);
+       pdb_free_sam(&sampass);
 
        return ret;
 }
@@ -710,16 +768,16 @@ BOOL pass_oem_change(char *user,
  but does use the lm OEM password to check the nt hashed-hash.
 
 ************************************************************/
-static BOOL check_oem_password(char *user,
-                              uchar * lmdata, uchar * lmhash,
-                              uchar * ntdata, uchar * nthash,
+static BOOL check_oem_password(const char *user,
+                              uchar * lmdata, const uchar * lmhash,
+                              const uchar * ntdata, const uchar * nthash,
                               SAM_ACCOUNT **hnd, char *new_passwd,
                               int new_passwd_size)
 {
        static uchar null_pw[16];
        static uchar null_ntpw[16];
        SAM_ACCOUNT *sampass = NULL;
-       uint8 *lanman_pw, *nt_pw;
+       const uint8 *lanman_pw, *nt_pw;
        uint16 acct_ctrl;
        int new_pw_len;
        uchar new_ntp16[16];
@@ -762,27 +820,23 @@ static BOOL check_oem_password(char *user,
 
        /* check for null passwords */
        if (lanman_pw == NULL) {
-               if (acct_ctrl & ACB_PWNOTREQ)
-                       pdb_set_lanman_passwd(sampass, null_pw);
-               else {
+               if (!(acct_ctrl & ACB_PWNOTREQ)) {
                        DEBUG(0,("check_oem_password: no lanman password !\n"));
                        return False;
                }
        }
-
+       
        if (pdb_get_nt_passwd(sampass) == NULL && nt_pass_set) {
-               if (acct_ctrl & ACB_PWNOTREQ)
-                       pdb_set_nt_passwd(sampass, null_pw);
-               else {
+               if (!(acct_ctrl & ACB_PWNOTREQ)) {
                        DEBUG(0,("check_oem_password: no ntlm password !\n"));
                        return False;
                }
        }
-
+       
        /* 
         * Call the hash function to get the new password.
         */
-       SamOEMhash((uchar *) lmdata, (uchar *)lanman_pw, 516);
+       SamOEMhash( lmdata, lanman_pw, 516);
 
        /* 
         * The length of the new password is in the last 4 bytes of
@@ -862,79 +916,23 @@ static BOOL check_oem_password(char *user,
 /***********************************************************
  Code to change the oem password. Changes both the lanman
  and NT hashes.
- override = False, normal
- override = True, override XXXXXXXXXX'd password
 ************************************************************/
 
-BOOL change_oem_password(SAM_ACCOUNT *hnd, char *new_passwd,
-                        BOOL override)
+BOOL change_oem_password(SAM_ACCOUNT *hnd, char *new_passwd)
 {
-       int ret;
-       uchar new_nt_p16[16];
-       uchar new_p16[16];
-
-       nt_lm_owf_gen(new_passwd, new_nt_p16, new_p16);
+       BOOL ret;
 
-       pdb_set_lanman_passwd (hnd, new_p16);
-       pdb_set_nt_passwd     (hnd, new_nt_p16);
+       if (!pdb_set_plaintext_passwd (hnd, new_passwd)) {
+               return False;
+       }
 
        /* Now write it into the file. */
        become_root();
-       ret = pdb_update_sam_account (hnd, override);
+       ret = pdb_update_sam_account (hnd, False);
        unbecome_root();
 
-       memset(new_passwd, '\0', strlen(new_passwd));
-
        return ret;
 }
 
-/***********************************************************
- Code to check a plaintext password against smbpasswd entries.
-***********************************************************/
 
-BOOL check_plaintext_password(char *user, char *old_passwd,
-                             int old_passwd_size, SAM_ACCOUNT **hnd)
-{
-       SAM_ACCOUNT  *sampass = NULL;
-       uchar old_pw[16], old_ntpw[16];
-       BOOL ret;
 
-       pdb_init_sam(&sampass);
-
-       become_root();
-       ret = pdb_getsampwnam(sampass, user);
-       unbecome_root();
-
-       *hnd = sampass;
-
-       if (ret == False)
-       {
-               DEBUG(0,("check_plaintext_password: getsmbpwnam returned NULL\n"));
-               return False;
-       }
-
-       if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED)
-       {
-               DEBUG(0,("check_plaintext_password: account %s disabled.\n", user));
-               return (False);
-       }
-
-       nt_lm_owf_gen(old_passwd, old_ntpw, old_pw);
-
-#ifdef DEBUG_PASSWORD
-       DEBUG(100, ("check_plaintext_password: nt_passwd \n"));
-       dump_data(100, pdb_get_nt_passwd(sampass), 16);
-       DEBUG(100, ("check_plaintext_password: old_ntpw \n"));
-       dump_data(100, old_ntpw, 16);
-       DEBUG(100, ("check_plaintext_password: lanman_passwd \n"));
-       dump_data(100, pdb_get_lanman_passwd(sampass), 16);
-       DEBUG(100, ("check_plaintext_password: old_pw\n"));
-       dump_data(100, old_pw, 16);
-#endif
-
-       if (memcmp(pdb_get_nt_passwd(sampass), old_ntpw, 16)
-           && memcmp(pdb_get_lanman_passwd(sampass), old_pw, 16))
-               return (False);
-       else
-               return (True);
-}