changed string_sub() to replace " ; and ` in the inserted string with _
[samba.git] / source / smbd / chgpasswd.c
index 560d989b478075f25699f5cfee72a15511bee258..30b9b3fed60aac3ddc2f67748295436ca502e7ae 100644 (file)
 
 extern int DEBUGLEVEL;
 
-#ifdef ALLOW_CHANGE_PASSWORD
-
+#if ALLOW_CHANGE_PASSWORD
 #define MINPASSWDLENGTH 5
 #define BUFSIZE 512
 
 static int findpty(char **slave)
 {
   int master;
-#if defined(USE_GRANTPT)
-#if defined(SVR4) || defined(SUNOS5)
-  extern char *ptsname();
-#endif /* defined(SVR4) || defined(SUNOS5) */
-#else /* USE_GRANTPT */
+#ifndef HAVE_GRANTPT
   static fstring line;
   void *dirp;
   char *dpname;
-#endif /* USE_GRANTPT */
+#endif /* !HAVE_GRANTPT */
   
-#if defined(USE_GRANTPT)
-  if ((master = open("/dev/ptmx", O_RDWR)) >= 1) {
+#if defined(HAVE_GRANTPT)
+  if ((master = sys_open("/dev/ptmx", O_RDWR, 0)) >= 1) {
     grantpt(master);
     unlockpt(master);
     *slave = ptsname(master);
-    if(*slave == NULL) {
+    if (*slave == NULL) {
       DEBUG(0,("findpty: Unable to create master/slave pty pair.\n"));
+      /* Stop fd leak on error. */
+      close(master);
       return -1;
     } else {
       DEBUG(10, ("findpty: Allocated slave pty %s\n", *slave));
       return (master);
     }
   }
-#else /* USE_GRANTPT */
+#else /* HAVE_GRANTPT */
   fstrcpy( line, "/dev/ptyXX" );
 
-  dirp = OpenDir(-1, "/dev", False);
-  if (!dirp) return(-1);
+  dirp = OpenDir(NULL, "/dev", False);
+  if (!dirp)
+    return(-1);
   while ((dpname = ReadDirName(dirp)) != NULL) {
     if (strncmp(dpname, "pty", 3) == 0 && strlen(dpname) == 5) {
       DEBUG(3,("pty: try to open %s, line was %s\n", dpname, line ) );
       line[8] = dpname[3];
       line[9] = dpname[4];
-      if ((master = open(line, O_RDWR)) >= 0) {
+      if ((master = sys_open(line, O_RDWR, 0)) >= 0) {
         DEBUG(3,("pty: opened %s\n", line ) );
-       line[5] = 't';
-       *slave = line;
-       CloseDir(dirp);
-       return (master);
+        line[5] = 't';
+        *slave = line;
+        CloseDir(dirp);
+        return (master);
       }
     }
   }
   CloseDir(dirp);
-#endif /* USE_GRANTPT */
+#endif /* HAVE_GRANTPT */
   return (-1);
 }
 
@@ -114,7 +112,7 @@ static int dochild(int master,char *slavedev, char *name, char *passwordprogram,
   int gid;
   int uid;
 
-  if(pass == NULL) {
+  if (pass == NULL) {
     DEBUG(0,("dochild: user name %s doesn't exist in the UNIX password database.\n",
               name));
     return False;
@@ -122,11 +120,11 @@ static int dochild(int master,char *slavedev, char *name, char *passwordprogram,
 
   gid = pass->pw_gid;
   uid = pass->pw_uid;
-#ifdef USE_SETRES
+#ifdef HAVE_SETRESUID
   setresuid(0,0,0);
-#else /* USE_SETRES */
+#else 
   setuid(0);
-#endif /* USE_SETRES */
+#endif
 
   /* Start new session - gets rid of controlling terminal. */
   if (setsid() < 0) {
@@ -135,22 +133,20 @@ static int dochild(int master,char *slavedev, char *name, char *passwordprogram,
   }
 
   /* Open slave pty and acquire as new controlling terminal. */
-  if ((slave = open(slavedev, O_RDWR)) < 0) {
+  if ((slave = sys_open(slavedev, O_RDWR, 0)) < 0) {
     DEBUG(3,("More weirdness, could not open %s\n", 
             slavedev));
     return(False);
   }
-#if defined(SVR4) || defined(SUNOS5) || defined(SCO)
+#ifdef I_PUSH
   ioctl(slave, I_PUSH, "ptem");
   ioctl(slave, I_PUSH, "ldterm");
-#else /* defined(SVR4) || defined(SUNOS5) || defined(SCO) */
-#if defined(TIOCSCTTY)
+#elif defined(TIOCSCTTY)
   if (ioctl(slave,TIOCSCTTY,0) <0) {
      DEBUG(3,("Error in ioctl call for slave pty\n"));
      /* return(False); */
   }
-#endif /* defined(TIOCSCTTY) */
-#endif /* defined(SVR4) || defined(SUNOS5) || defined(SCO) */
+#endif 
 
   /* Close master. */
   close(master);
@@ -187,19 +183,19 @@ static int dochild(int master,char *slavedev, char *name, char *passwordprogram,
   }
 
   /* make us completely into the right uid */
-  if(!as_root) {
-#ifdef USE_SETRES
-    setresgid(0,0,0);
-    setresuid(0,0,0);
-    setresgid(gid,gid,gid);
-    setresuid(uid,uid,uid);      
+  if (!as_root) {
+#ifdef HAVE_SETRESUID
+         setresgid(0,0,0);
+         setresuid(0,0,0);
+         setresgid(gid,gid,gid);
+         setresuid(uid,uid,uid);      
 #else      
-    setuid(0);
-    seteuid(0);
-    setgid(gid);
-    setegid(gid);
-    setuid(uid);
-    seteuid(uid);
+         setuid(0);
+         seteuid(0);
+         setgid(gid);
+         setegid(gid);
+         setuid(uid);
+         seteuid(uid);
 #endif
   }
 
@@ -269,14 +265,14 @@ static int talktochild(int master, char *chatsequence)
   *buf = 0;
   sleep(1);
 
-  while (next_token(&ptr,chatbuf,NULL)) {
+  while (next_token(&ptr,chatbuf,NULL,sizeof(chatbuf))) {
     BOOL ok=True;
     count++;
     pwd_sub(chatbuf);
     if (!strequal(chatbuf,"."))
       ok = expect(master,chatbuf,buf);
 
-    if(lp_passwd_chat_debug())
+    if (lp_passwd_chat_debug())
       DEBUG(100,("talktochild: chatbuf=[%s] responsebuf=[%s]\n",chatbuf,buf));
 
     if (!ok) {
@@ -284,12 +280,12 @@ static int talktochild(int master, char *chatsequence)
       return(False);
     }
 
-    if (!next_token(&ptr,chatbuf,NULL)) break;
+    if (!next_token(&ptr,chatbuf,NULL,sizeof(chatbuf))) break;
     pwd_sub(chatbuf);
     if (!strequal(chatbuf,"."))
       writestring(master,chatbuf);
 
-    if(lp_passwd_chat_debug())
+    if (lp_passwd_chat_debug())
       DEBUG(100,("talktochild: sendbuf=[%s]\n",chatbuf));
   }
 
@@ -299,7 +295,7 @@ static int talktochild(int master, char *chatsequence)
 }
 
 
-BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence, BOOL as_root)
+static BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence, BOOL as_root)
 {
   char *slavedev;
   int master;
@@ -309,12 +305,13 @@ BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence, BOOL
 
   /* allocate a pseudo-terminal device */
   if ((master = findpty (&slavedev)) < 0) {
-    DEBUG(3,("Cannot Allocate pty for password change: %s",name));
+    DEBUG(3,("Cannot Allocate pty for password change: %s\n",name));
     return(False);
   }
 
   if ((pid = fork()) < 0) {
-    DEBUG(3,("Cannot fork() child for password change: %s",name));
+    DEBUG(3,("Cannot fork() child for password change: %s\n",name));
+    close(master);
     return(False);
   }
 
@@ -324,10 +321,15 @@ BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence, BOOL
       DEBUG(3,("Child failed to change password: %s\n",name));
       kill(pid, SIGKILL); /* be sure to end this process */
     }
+
     if ((wpid = sys_waitpid(pid, &wstat, 0)) < 0) {
       DEBUG(3,("The process is no longer waiting!\n\n"));
+      close(master);
       return(False);
     }
+
+    close(master);
+
     if (pid != wpid) {
       DEBUG(3,("We were waiting for the wrong process ID\n")); 
       return(False);
@@ -344,19 +346,25 @@ BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence, BOOL
   } else {
     /* CHILD */
 
+    /*
+     * Lose any oplock capabilities.
+     */
+    set_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
+    set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
+
     /* make sure it doesn't freeze */
     alarm(20);
 
-    if(as_root)
+    if (as_root)
       become_root(False);
-    DEBUG(3,("Dochild for user %s (uid=%d,gid=%d)\n",name,getuid(),getgid()));
+    DEBUG(3,("Dochild for user %s (uid=%d,gid=%d)\n",name,(int)getuid(),(int)getgid()));
     chstat = dochild(master, slavedev, name, passwordprogram, as_root);
 
-    if(as_root)
+    if (as_root)
       unbecome_root(False);
   }
 
-  if(chstat)
+  if (chstat)
     DEBUG(3,("Password change %ssuccessful for user %s\n", (chstat?"":"un"), name));
   return (chstat);
 }
@@ -391,13 +399,8 @@ BOOL chgpasswd(char *name,char *oldpass,char *newpass, BOOL as_root)
       return (False);          /* inform the user */
     }
 
-#if (defined(PASSWD_PROGRAM) && defined(PASSWD_CHAT))
-  pstrcpy(passwordprogram,PASSWD_PROGRAM);
-  pstrcpy(chatsequence,PASSWD_CHAT);
-#else
   pstrcpy(passwordprogram,lp_passwd_program());
   pstrcpy(chatsequence,lp_passwd_chat());
-#endif
 
   if (!*chatsequence) {
     DEBUG(2,("Null chat sequence - no password changing\n"));
@@ -416,7 +419,7 @@ BOOL chgpasswd(char *name,char *oldpass,char *newpass, BOOL as_root)
 
   len = strlen(oldpass); 
   for(i = 0; i < len; i++) {
-    if(iscntrl(oldpass[i])) {
+    if (iscntrl((int)oldpass[i])) {
       DEBUG(0,("chat_with_program: oldpass contains control characters (disallowed).\n"));
       return False;
     }
@@ -424,19 +427,19 @@ BOOL chgpasswd(char *name,char *oldpass,char *newpass, BOOL as_root)
 
   len = strlen(newpass);
   for(i = 0; i < len; i++) {
-    if(iscntrl(newpass[i])) {
+    if (iscntrl((int)newpass[i])) {
       DEBUG(0,("chat_with_program: newpass contains control characters (disallowed).\n"));
       return False;
     }
   }
 
   string_sub(passwordprogram,"%u",name);
-  string_sub(passwordprogram,"%o",oldpass);
-  string_sub(passwordprogram,"%n",newpass);
+  all_string_sub(passwordprogram,"%o",oldpass);
+  all_string_sub(passwordprogram,"%n",newpass);
 
   string_sub(chatsequence,"%u",name);
-  string_sub(chatsequence,"%o",oldpass);
-  string_sub(chatsequence,"%n",newpass);
+  all_string_sub(chatsequence,"%o",oldpass);
+  all_string_sub(chatsequence,"%n",newpass);
   return(chat_with_program(passwordprogram,name,chatsequence, as_root));
 }
 
@@ -452,12 +455,12 @@ BOOL chgpasswd(char *name,char *oldpass,char *newpass, BOOL as_root)
  Code to check the lanman hashed password.
 ************************************************************/
 
-BOOL check_lanman_password(char *user, unsigned char *pass1, 
-                           unsigned char *pass2, struct smb_passwd **psmbpw)
+BOOL check_lanman_password(char *user, uchar *pass1, 
+                           uchar *pass2, struct smb_passwd **psmbpw)
 {
-  unsigned char unenc_new_pw[16];
-  unsigned char unenc_old_pw[16];
-  unsigned char null_pw[16];
+  static uchar null_pw[16];
+  uchar unenc_new_pw[16];
+  uchar unenc_old_pw[16];
   struct smb_passwd *smbpw;
 
   *psmbpw = NULL;
@@ -466,23 +469,23 @@ BOOL check_lanman_password(char *user, unsigned char *pass1,
   smbpw = getsmbpwnam(user);
   unbecome_root(0);
 
-  if(smbpw == NULL)
+  if (smbpw == NULL)
   {
     DEBUG(0,("check_lanman_password: getsmbpwnam returned NULL\n"));
     return False;
   }
 
-  if(smbpw->acct_ctrl & ACB_DISABLED)
+  if (smbpw->acct_ctrl & ACB_DISABLED)
   {
     DEBUG(0,("check_lanman_password: account %s disabled.\n", user));
     return False;
   }
 
-  if((smbpw->smb_passwd == NULL) && (smbpw->acct_ctrl & ACB_PWNOTREQ))
+  if ((smbpw->smb_passwd == NULL) && (smbpw->acct_ctrl & ACB_PWNOTREQ))
   {
-    unsigned char no_pw[14];
+    uchar no_pw[14];
     memset(no_pw, '\0', 14);
-    E_P16((uchar *)no_pw, (uchar *)null_pw);
+    E_P16(no_pw, null_pw);
     smbpw->smb_passwd = null_pw;
   } else if (smbpw->smb_passwd == NULL) {
     DEBUG(0,("check_lanman_password: no lanman password !\n"));
@@ -496,7 +499,7 @@ BOOL check_lanman_password(char *user, unsigned char *pass1,
   D_P16(unenc_new_pw, pass1, unenc_old_pw);
 
   /* Check that the two old passwords match. */
-  if(memcmp(smbpw->smb_passwd, unenc_old_pw, 16))
+  if (memcmp(smbpw->smb_passwd, unenc_old_pw, 16))
   {
     DEBUG(0,("check_lanman_password: old password doesn't match.\n"));
     return False;
@@ -512,29 +515,29 @@ BOOL check_lanman_password(char *user, unsigned char *pass1,
  no longer be valid.
 ************************************************************/
 
-BOOL change_lanman_password(struct smb_passwd *smbpw, unsigned char *pass1, unsigned char *pass2)
+BOOL change_lanman_password(struct smb_passwd *smbpw, uchar *pass1, uchar *pass2)
 {
-  unsigned char unenc_new_pw[16];
-  unsigned char null_pw[16];
+  static uchar null_pw[16];
+  uchar unenc_new_pw[16];
   BOOL ret;
 
-  if(smbpw == NULL)
+  if (smbpw == NULL)
   { 
     DEBUG(0,("change_lanman_password: no smb password entry.\n"));
     return False;
   }
 
-  if(smbpw->acct_ctrl & ACB_DISABLED)
+  if (smbpw->acct_ctrl & ACB_DISABLED)
   {
     DEBUG(0,("change_lanman_password: account %s disabled.\n", smbpw->smb_name));
     return False;
   }
 
-  if((smbpw->smb_passwd == NULL) && (smbpw->acct_ctrl & ACB_PWNOTREQ))
+  if ((smbpw->smb_passwd == NULL) && (smbpw->acct_ctrl & ACB_PWNOTREQ))
   {
-    unsigned char no_pw[14];
+    uchar no_pw[14];
     memset(no_pw, '\0', 14);
-    E_P16((uchar *)no_pw, (uchar *)null_pw);
+    E_P16(no_pw, null_pw);
     smbpw->smb_passwd = null_pw;
   } else if (smbpw->smb_passwd == NULL) {
     DEBUG(0,("change_lanman_password: no lanman password !\n"));
@@ -556,90 +559,198 @@ BOOL change_lanman_password(struct smb_passwd *smbpw, unsigned char *pass1, unsi
 }
 
 /***********************************************************
- Code to check the OEM hashed password.
+ Code to check and change the OEM hashed password.
 ************************************************************/
-
-BOOL check_oem_password(char *user, unsigned char *data,
-                        struct smb_passwd **psmbpw, char *new_passwd,
-                        int new_passwd_size)
+BOOL pass_oem_change(char *user,
+                       uchar *lmdata, uchar *lmhash,
+                       uchar *ntdata, uchar *nthash)
 {
-  struct smb_passwd *smbpw = NULL;
-  int new_pw_len;
-  fstring upper_case_new_passwd;
-  unsigned char new_p16[16];
-  unsigned char unenc_old_pw[16];
-  unsigned char null_pw[16];
-
-  become_root(0);
-  *psmbpw = smbpw = getsmbpwnam(user);
-  unbecome_root(0);
-
-  if(smbpw == NULL)
-  {
-    DEBUG(0,("check_oem_password: getsmbpwnam returned NULL\n"));
-    return False;
-  }
-
-  if(smbpw->acct_ctrl & ACB_DISABLED)
-  {
-    DEBUG(0,("check_lanman_password: account %s disabled.\n", user));
-    return False;
-  }
-
-  if((smbpw->smb_passwd == NULL) && (smbpw->acct_ctrl & ACB_PWNOTREQ))
-  {
-    unsigned char no_pw[14];
-    memset(no_pw, '\0', 14);
-    E_P16((uchar *)no_pw, (uchar *)null_pw);
-    smbpw->smb_passwd = null_pw;
-  } else if (smbpw->smb_passwd == NULL) {
-    DEBUG(0,("check_oem_password: no lanman password !\n"));
-    return False;
-  }
-
-  /* 
-   * Call the hash function to get the new password.
-   */
-  SamOEMhash( (unsigned char *)data, (unsigned char *)smbpw->smb_passwd, True);
-
-  /* 
-   * The length of the new password is in the last 4 bytes of
-   * the data buffer.
-   */
-  new_pw_len = IVAL(data,512);
-  if(new_pw_len < 0 || new_pw_len > new_passwd_size - 1) {
-    DEBUG(0,("check_oem_password: incorrect password length (%d).\n", new_pw_len));
-    return False;
-  }
-
-  memcpy(new_passwd, &data[512-new_pw_len], new_pw_len);
-  new_passwd[new_pw_len] = '\0';
-
-  /*
-   * To ensure we got the correct new password, hash it and
-   * use it as a key to test the passed old password.
-   */
-
-  memset(upper_case_new_passwd, '\0', sizeof(upper_case_new_passwd));
-  fstrcpy(upper_case_new_passwd, new_passwd);
-  strupper(upper_case_new_passwd);
-
-  E_P16((uchar *)upper_case_new_passwd, new_p16);
-
-  /*
-   * Now use new_p16 as the key to see if the old
-   * password matches.
-   */
-  D_P16(new_p16, &data[516], unenc_old_pw);
+       fstring new_passwd;
+       struct smb_passwd *sampw;
+       BOOL ret = check_oem_password( user, lmdata, lmhash, ntdata, nthash,
+                                      &sampw, 
+                                      new_passwd, sizeof(new_passwd));
+
+       /* 
+        * At this point we have the new case-sensitive plaintext
+        * password in the fstring new_passwd. If we wanted to synchronise
+        * with UNIX passwords we would call a UNIX password changing 
+        * function here. However it would have to be done as root
+        * as the plaintext of the old users password is not 
+        * available. JRA.
+        */
+
+       if ( ret && lp_unix_password_sync())
+       {
+               ret = chgpasswd(user,"", new_passwd, True);
+       }
+
+       if (ret)
+       {
+               ret = change_oem_password( sampw, new_passwd, False );
+       }
+
+       memset(new_passwd, 0, sizeof(new_passwd));
+
+       return ret;
+}
 
-  if(memcmp(smbpw->smb_passwd, unenc_old_pw, 16)) {
-    DEBUG(0,("check_oem_password: old password doesn't match.\n"));
-    return False;
-  }
+/***********************************************************
+ Code to check the OEM hashed password.
 
-  memset(upper_case_new_passwd, '\0', strlen(upper_case_new_passwd));
+ this function ignores the 516 byte nt OEM hashed password
+ but does use the lm OEM password to check the nt hashed-hash.
 
-  return True;
+************************************************************/
+BOOL check_oem_password(char *user,
+                       uchar *lmdata, uchar *lmhash,
+                       uchar *ntdata, uchar *nthash,
+                        struct smb_passwd **psmbpw, char *new_passwd,
+                        int new_passwd_size)
+{
+       static uchar null_pw[16];
+       static uchar null_ntpw[16];
+       struct smb_passwd *smbpw = NULL;
+       int new_pw_len;
+       uchar new_ntp16[16];
+       uchar unenc_old_ntpw[16];
+       uchar new_p16[16];
+       uchar unenc_old_pw[16];
+       char no_pw[2];
+
+       BOOL nt_pass_set = (ntdata != NULL && nthash != NULL);
+
+       become_root(False);
+       *psmbpw = smbpw = getsmbpwnam(user);
+       unbecome_root(False);
+
+       if (smbpw == NULL)
+       {
+               DEBUG(0,("check_oem_password: getsmbpwnam returned NULL\n"));
+               return False;
+       }
+
+       if (smbpw->acct_ctrl & ACB_DISABLED)
+       {
+               DEBUG(0,("check_lanman_password: account %s disabled.\n", user));
+               return False;
+       }
+
+       /* construct a null password (in case one is needed */
+       no_pw[0] = 0;
+       no_pw[1] = 0;
+       nt_lm_owf_gen(no_pw, null_ntpw, null_pw);
+
+       /* check for null passwords */
+       if (smbpw->smb_passwd == NULL)
+       {
+               if (smbpw->acct_ctrl & ACB_PWNOTREQ)
+               {
+                       smbpw->smb_passwd = null_pw;
+               }
+               else 
+               {
+                       DEBUG(0,("check_oem_password: no lanman password !\n"));
+                       return False;
+               }
+       }
+
+       if (smbpw->smb_nt_passwd == NULL && nt_pass_set)
+       {
+               if (smbpw->acct_ctrl & ACB_PWNOTREQ)
+               {
+                       smbpw->smb_nt_passwd = null_pw;
+               }
+               else 
+               {
+                       DEBUG(0,("check_oem_password: no ntlm password !\n"));
+                       return False;
+               }
+       }
+
+       /* 
+        * Call the hash function to get the new password.
+        */
+       SamOEMhash( (uchar *)lmdata, (uchar *)smbpw->smb_passwd, True);
+
+       /* 
+        * The length of the new password is in the last 4 bytes of
+        * the data buffer.
+        */
+
+       new_pw_len = IVAL(lmdata, 512);
+       if (new_pw_len < 0 || new_pw_len > new_passwd_size - 1)
+       {
+               DEBUG(0,("check_oem_password: incorrect password length (%d).\n", new_pw_len));
+               return False;
+       }
+
+       if (nt_pass_set)
+       {
+               /*
+                * nt passwords are in unicode
+                */
+               int uni_pw_len = new_pw_len;
+               char *pw;
+               new_pw_len /= 2;
+               pw = unistrn2((uint16*)(&lmdata[512-uni_pw_len]), new_pw_len);
+               memcpy(new_passwd, pw, new_pw_len+1);
+       }
+       else
+       {
+               memcpy(new_passwd, &lmdata[512-new_pw_len], new_pw_len);
+               new_passwd[new_pw_len] = '\0';
+       }
+
+       /*
+        * To ensure we got the correct new password, hash it and
+        * use it as a key to test the passed old password.
+        */
+
+       nt_lm_owf_gen(new_passwd, new_ntp16, new_p16);
+
+       if (!nt_pass_set)
+       {
+               /*
+                * Now use new_p16 as the key to see if the old
+                * password matches.
+                */
+               D_P16(new_p16  , lmhash, unenc_old_pw);
+
+               if (memcmp(smbpw->smb_passwd, unenc_old_pw, 16))
+               {
+                       DEBUG(0,("check_oem_password: old lm password doesn't match.\n"));
+                       return False;
+               }
+
+#ifdef DEBUG_PASSWORD
+               DEBUG(100,("check_oem_password: password %s ok\n", new_passwd));
+#endif
+               return True;
+       }
+
+       /*
+        * Now use new_p16 as the key to see if the old
+        * password matches.
+        */
+       D_P16(new_ntp16, lmhash, unenc_old_pw);
+       D_P16(new_ntp16, nthash, unenc_old_ntpw);
+
+       if (memcmp(smbpw->smb_passwd, unenc_old_pw, 16))
+       {
+               DEBUG(0,("check_oem_password: old lm password doesn't match.\n"));
+               return False;
+       }
+
+       if (memcmp(smbpw->smb_nt_passwd, unenc_old_ntpw, 16))
+       {
+               DEBUG(0,("check_oem_password: old nt password doesn't match.\n"));
+               return False;
+       }
+#ifdef DEBUG_PASSWORD
+       DEBUG(100,("check_oem_password: password %s ok\n", new_passwd));
+#endif
+       return True;
 }
 
 /***********************************************************
@@ -652,19 +763,12 @@ BOOL check_oem_password(char *user, unsigned char *data,
 BOOL change_oem_password(struct smb_passwd *smbpw, char *new_passwd, BOOL override)
 {
   int ret;
-  fstring upper_case_new_passwd;
-  unsigned char new_nt_p16[16];
-  unsigned char new_p16[16];
-
-  memset(upper_case_new_passwd, '\0', sizeof(upper_case_new_passwd));
-  fstrcpy(upper_case_new_passwd, new_passwd);
-  strupper(upper_case_new_passwd);
+  uchar new_nt_p16[16];
+  uchar new_p16[16];
 
-  E_P16((uchar *)upper_case_new_passwd, new_p16);
+  nt_lm_owf_gen(new_passwd, new_nt_p16, new_p16);
 
   smbpw->smb_passwd = new_p16;
-  
-  E_md4hash((uchar *) new_passwd, new_nt_p16);
   smbpw->smb_nt_passwd = new_nt_p16;
   
   /* Now write it into the file. */
@@ -672,7 +776,6 @@ BOOL change_oem_password(struct smb_passwd *smbpw, char *new_passwd, BOOL overri
   ret = mod_smbpwd_entry(smbpw,override);
   unbecome_root(0);
 
-  memset(upper_case_new_passwd, '\0', strlen(upper_case_new_passwd));
   memset(new_passwd, '\0', strlen(new_passwd));
 
   return ret;