📄 chgpasswd.c
字号:
} 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 a new password containing shell escape characters */ pstring_sub(chatsequence, "%u", name); all_string_sub(chatsequence, "%o", oldpass, sizeof(pstring)); all_string_sub(chatsequence, "%n", newpass, sizeof(pstring)); return (chat_with_program (passwordprogram, pass, chatsequence, as_root));}#else /* ALLOW_CHANGE_PASSWORD */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);}#endif /* ALLOW_CHANGE_PASSWORD *//*********************************************************** Code to check the lanman hashed password.************************************************************/BOOL check_lanman_password(char *user, uchar * pass1, uchar * pass2, SAM_ACCOUNT **hnd){ uchar unenc_new_pw[16]; uchar unenc_old_pw[16]; SAM_ACCOUNT *sampass = NULL; uint16 acct_ctrl; const uint8 *lanman_pw; BOOL ret; become_root(); ret = pdb_getsampwnam(sampass, user); unbecome_root(); if (ret == False) { DEBUG(0,("check_lanman_password: getsampwnam returned NULL\n")); pdb_free_sam(&sampass); return False; } acct_ctrl = pdb_get_acct_ctrl (sampass); lanman_pw = pdb_get_lanman_passwd (sampass); if (acct_ctrl & ACB_DISABLED) { DEBUG(0,("check_lanman_password: account %s disabled.\n", user)); 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. */ D_P16(lanman_pw, pass2, unenc_new_pw); /* Use this to get the old lanman hash. */ D_P16(unenc_new_pw, pass1, unenc_old_pw); /* 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); return False; } /* this saves the pointer for the caller */ *hnd = sampass; return True;}/*********************************************************** Code to change the lanman hashed password. It nulls out the NT hashed password as it will no longer be valid. NOTE this function is designed to be called as root. Check the old password is correct before calling. JRA.************************************************************/BOOL change_lanman_password(SAM_ACCOUNT *sampass, uchar *pass2){ static uchar null_pw[16]; uchar unenc_new_pw[16]; BOOL ret; uint16 acct_ctrl; const uint8 *pwd; if (sampass == NULL) { DEBUG(0,("change_lanman_password: no smb password entry.\n")); return False; } acct_ctrl = pdb_get_acct_ctrl(sampass); pwd = pdb_get_lanman_passwd(sampass); if (acct_ctrl & ACB_DISABLED) { DEBUG(0,("change_lanman_password: account %s disabled.\n", pdb_get_username(sampass))); return False; } 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); } if (!pdb_set_lanman_passwd(sampass, unenc_new_pw, PDB_CHANGED)) { return False; } if (!pdb_set_nt_passwd (sampass, NULL, PDB_CHANGED)) { 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 */ ret = pdb_update_sam_account (sampass); return ret;}/*********************************************************** Code to check and change the OEM hashed password.************************************************************/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]){ pstring new_passwd; SAM_ACCOUNT *sampass = NULL; NTSTATUS nt_status = check_oem_password(user, password_encrypted_with_lm_hash, old_lm_hash_encrypted, password_encrypted_with_nt_hash, old_nt_hash_encrypted, &sampass, new_passwd, sizeof(new_passwd)); if (!NT_STATUS_IS_OK(nt_status)) return nt_status; /* We've already checked the old password here.... */ become_root(); nt_status = change_oem_password(sampass, NULL, new_passwd, True); unbecome_root(); memset(new_passwd, 0, sizeof(new_passwd)); pdb_free_sam(&sampass); return nt_status;}/*********************************************************** Decrypt and verify a user password change. The 516 byte long buffers are encrypted with the old NT and old LM passwords, and if the NT passwords are present, both buffers contain a unicode string. After decrypting the buffers, check the password is correct by matching the old hashed passwords with the passwords in the passdb. ************************************************************/static NTSTATUS check_oem_password(const 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], 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 *password_encrypted; const uint8 *encryption_key; const uint8 *lanman_pw, *nt_pw; uint16 acct_ctrl; uint32 new_pw_len; uchar new_nt_hash[16]; uchar new_lm_hash[16]; uchar verifier[16]; char no_pw[2]; 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); *hnd = NULL; pdb_init_sam(&sampass); become_root(); ret = pdb_getsampwnam(sampass, user); unbecome_root(); if (ret == False) { DEBUG(0, ("check_oem_password: getsmbpwnam returned NULL\n")); pdb_free_sam(&sampass); return NT_STATUS_NO_SUCH_USER; } acct_ctrl = pdb_get_acct_ctrl(sampass); if (acct_ctrl & ACB_DISABLED) { DEBUG(2,("check_lanman_password: account %s disabled.\n", user)); pdb_free_sam(&sampass); return NT_STATUS_ACCOUNT_DISABLED; } 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; nt_lm_owf_gen(no_pw, null_ntpw, null_pw); lanman_pw = null_pw; nt_pw = null_pw; } else { /* save pointers to passwords so we don't have to keep looking them up */ if (lp_lanman_auth()) { lanman_pw = pdb_get_lanman_passwd(sampass); } else { lanman_pw = NULL; } nt_pw = pdb_get_nt_passwd(sampass); } if (nt_pw && nt_pass_set) { /* IDEAL Case: passwords are in unicode, and we can * read use the password encrypted with the NT hash */ password_encrypted = password_encrypted_with_nt_hash; encryption_key = nt_pw; } else if (lanman_pw && lm_pass_set) { /* password may still be in unicode, but use LM hash version */ password_encrypted = password_encrypted_with_lm_hash; encryption_key = lanman_pw; } 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); return NT_STATUS_WRONG_PASSWORD; } else if (lm_pass_set) { 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)); } pdb_free_sam(&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); return NT_STATUS_WRONG_PASSWORD; } /* * Decrypt the password with the key */ SamOEMhash( password_encrypted, encryption_key, 516); 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); return NT_STATUS_WRONG_PASSWORD; } /* * To ensure we got the correct new password, hash it and * use it as a key to test the passed old password. */ if (nt_pass_set) { /* NT passwords, verify the NT hash. */ /* Calculate the MD4 hash (NT compatible) of the password */ memset(new_nt_hash, '\0', 16); E_md4hash(new_passwd, new_nt_hash); if (nt_pw) { /* * check the NT verifier */ 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); return NT_STATUS_WRONG_PASSWORD; } /* We could check the LM password here, but there is * little point, we already know the password is * correct, and the LM password might not even be * present. */ /* Further, LM hash generation algorithms * differ with charset, so we could * incorrectly fail a perfectly valid password * change */#ifdef DEBUG_PASSWORD DEBUG(100, ("check_oem_password: password %s ok\n", new_passwd));#endif *hnd = sampass; return NT_STATUS_OK; } if (lanman_pw) { /* * check the lm verifier */ 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); return NT_STATUS_WRONG_PASSWORD; }#ifdef DEBUG_PASSWORD DEBUG(100, ("check_oem_password: password %s ok\n", new_passwd));#endif *hnd = sampass; return NT_STATUS_OK; } } if (lanman_pw && lm_pass_set) { E_deshash(new_passwd, new_lm_hash); /* * check the lm verifier */ 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); return NT_STATUS_WRONG_PASSWORD; } #ifdef DEBUG_PASSWORD DEBUG(100, ("check_oem_password: password %s ok\n", new_passwd));#endif *hnd = sampass; return NT_STATUS_OK; } /* should not be reached */ pdb_free_sam(&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(SAM_ACCOUNT *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, (const char *)new_nt_p16, NT_HASH_LEN); dump_data(100, (const char *)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. NOTE this function is designed to be called as root. Check the old password is correct before calling. JRA.************************************************************/NTSTATUS change_oem_password(SAM_ACCOUNT *hnd, char *old_passwd, char *new_passwd, BOOL as_root){ BOOL ret; uint32 min_len; struct passwd *pass = NULL; const char *username = pdb_get_username(hnd); time_t can_change_time = pdb_get_pass_can_change_time(hnd); 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))); return NT_STATUS_ACCOUNT_RESTRICTION; } 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", username)); DEBUGADD(1, (" account policy min password len = %d\n", min_len)); return NT_STATUS_PASSWORD_RESTRICTION;/* return NT_STATUS_PWD_TOO_SHORT; */ } if (check_passwd_history(hnd,new_passwd)) { return NT_STATUS_PASSWORD_RESTRICTION; } pass = Get_Pwnam(username); if (!pass) { DEBUG(1, ("check_oem_password: Username %s does not exist in system !?!\n", username)); return NT_STATUS_ACCESS_DENIED; } /* 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")); return NT_STATUS_PASSWORD_RESTRICTION; } } /* * If unix password sync was requested, attempt to change * the /etc/passwd database first. Return failure if this cannot * be done. * * This occurs before the oem change, because we don't want to * update it if chgpasswd failed. * * Conditional on lp_unix_password_sync() because we don't want * to touch the unix db unless we have admin permission. */ if(lp_unix_password_sync() && !chgpasswd(username, pass, old_passwd, new_passwd, as_root)) { return NT_STATUS_ACCESS_DENIED; } if (!pdb_set_plaintext_passwd (hnd, new_passwd)) { return NT_STATUS_ACCESS_DENIED; } /* Now write it into the file. */ ret = pdb_update_sam_account (hnd); if (!ret) { return NT_STATUS_ACCESS_DENIED; } return NT_STATUS_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -