/**
 * After a user's password has been updated to use the new hashing strategy wipe out the old hash value.
 * 
 * 
 * @param type $username
 * @param type $userid
 */
function purgeCompatabilityPassword($username, $userid)
{
    $purgeSQL = " UPDATE " . TBL_USERS . " SET " . COL_PWD . "='NoLongerUsed' " . " WHERE " . COL_UNM . "=? " . " AND " . COL_ID . "=?";
    privStatement($purgeSQL, array($username, $userid));
}
示例#2
0
/**
 * 
 * Wrapper for privStatement that just returns the first row of a query or FALSE
 * if there were no results.
 * 
 * @param type $sql
 * @param type $params
 * @return boolean
 */
function privQuery($sql, $params = null)
{
    $recordset = privStatement($sql, $params);
    if ($recordset->EOF) {
        return FALSE;
    }
    $rez = $recordset->FetchRow();
    if ($rez == FALSE) {
        return FALSE;
    }
    return $rez;
}
示例#3
0
/**
 * Setup or change a user's password
 * 
 * @param type $activeUser      ID of who is trying to make the change (either the user himself, or an administrator)
 * @param type $targetUser      ID of what account's password is to be updated (for a new user this doesn't exist yet).
 * @param type $currentPwd      the active user's current password 
 * @param type $newPwd          the new password for the target user
 * @param type $errMsg          passed by reference to return any 
 * @param type $create          Are we creating a new user or 
 * @param type $insert_sql      SQL to run to create the row in "users" (and generate a new id) when needed.
 * @param type $new_username    The username for a new user
 * @param type $newid           Return by reference of the ID of a created user
 * @return boolean              Was the password successfully updated/created? If false, then $errMsg will tell you why it failed.
 */
function update_password($activeUser, $targetUser, &$currentPwd, &$newPwd, &$errMsg, $create = false, $insert_sql = "", $new_username = null, &$newid = null)
{
    $userSQL = "SELECT " . implode(",", array(COL_PWD, COL_SALT, COL_PWD_H1, COL_SALT_H1, COL_PWD_H2, COL_SALT_H2)) . " FROM " . TBL_USERS_SECURE . " WHERE " . COL_ID . "=?";
    $userInfo = privQuery($userSQL, array($targetUser));
    // Verify the active user's password
    $changingOwnPassword = $activeUser == $targetUser;
    // True if this is the current user changing their own password
    if ($changingOwnPassword) {
        if ($create) {
            $errMsg = xl("Trying to create user with existing username!");
            return false;
        }
        // If this user is changing his own password, then confirm that they have the current password correct
        $hash_current = oemr_password_hash($currentPwd, $userInfo[COL_SALT]);
        if ($hash_current != $userInfo[COL_PWD]) {
            $errMsg = xl("Incorrect password!");
            return false;
        }
    } else {
        // If this is an administrator changing someone else's password, then check that they have the password right
        $adminSQL = " SELECT " . implode(",", array(COL_PWD, COL_SALT)) . " FROM " . TBL_USERS_SECURE . " WHERE " . COL_ID . "=?";
        $adminInfo = privQuery($adminSQL, array($activeUser));
        $hash_admin = oemr_password_hash($currentPwd, $adminInfo[COL_SALT]);
        if ($hash_admin != $adminInfo[COL_PWD]) {
            $errMsg = xl("Incorrect password!");
            return false;
        }
        if (!acl_check('admin', 'users')) {
            $errMsg = xl("Not authorized to manage users!");
            return false;
        }
    }
    // End active user check
    //Test password validity
    if (strlen($newPwd) == 0) {
        $errMsg = xl("Empty Password Not Allowed");
        return false;
    }
    if (!test_password_strength($newPwd, $errMsg)) {
        return false;
    }
    // End password validty checks
    if ($userInfo === false) {
        // No userInfo means either a new user, or an existing user who has not been migrated to blowfish yet
        // In these cases don't worry about password history
        if ($create) {
            privStatement($insert_sql, array());
            $getUserID = " SELECT " . COL_ID . " FROM " . TBL_USERS . " WHERE " . COL_UNM . "=?";
            $user_id = privQuery($getUserID, array($new_username));
            initializePassword($new_username, $user_id[COL_ID], $newPwd);
            $newid = $user_id[COL_ID];
        } else {
            $getUserNameSQL = "SELECT " . COL_UNM . " FROM " . TBL_USERS . " WHERE " . COL_ID . "=?";
            $unm = privQuery($getUserNameSQL, array($targetUser));
            if ($unm === false) {
                $errMsg = xl("Unknown user id:" . $targetUser);
                return false;
            }
            initializePassword($unm[COL_UNM], $targetUser, $newPwd);
            purgeCompatabilityPassword($unm[COL_UNM], $targetUser);
        }
    } else {
        // We are trying to update the password of an existing user
        if ($create) {
            $errMsg = xl("Trying to create user with existing username!");
            return false;
        }
        $forbid_reuse = $GLOBALS['password_history'] != 0;
        if ($forbid_reuse) {
            // password reuse disallowed
            $hash_current = oemr_password_hash($newPwd, $userInfo[COL_SALT]);
            $hash_history1 = oemr_password_hash($newPwd, $userInfo[COL_SALT_H1]);
            $hash_history2 = oemr_password_hash($newPwd, $userInfo[COL_SALT_H2]);
            if ($hash_current == $userInfo[COL_PWD] || $hash_history1 == $userInfo[COL_PWD_H1] || $hash_history2 == $userInfo[COL_PWD_H2]) {
                $errMsg = xl("Reuse of three previous passwords not allowed!");
                return false;
            }
        }
        // Everything checks out at this point, so update the password record
        $newSalt = oemr_password_salt();
        $newHash = oemr_password_hash($newPwd, $newSalt);
        $updateParams = array();
        $updateSQL = "UPDATE " . TBL_USERS_SECURE;
        $updateSQL .= " SET " . COL_PWD . "=?," . COL_SALT . "=?";
        array_push($updateParams, $newHash);
        array_push($updateParams, $newSalt);
        if ($forbid_reuse) {
            $updateSQL .= "," . COL_PWD_H1 . "=?" . "," . COL_SALT_H1 . "=?";
            array_push($updateParams, $userInfo[COL_PWD]);
            array_push($updateParams, $userInfo[COL_SALT]);
            $updateSQL .= "," . COL_PWD_H2 . "=?" . "," . COL_SALT_H2 . "=?";
            array_push($updateParams, $userInfo[COL_PWD_H1]);
            array_push($updateParams, $userInfo[COL_SALT_H1]);
        }
        $updateSQL .= " WHERE " . COL_ID . "=?";
        array_push($updateParams, $targetUser);
        privStatement($updateSQL, $updateParams);
        // If the user is changing their own password, we need to update the session
        if ($changingOwnPassword) {
            $_SESSION['authPass'] = $newHash;
        }
    }
    if ($GLOBALS['password_expiration_days'] != 0) {
        $exp_days = $GLOBALS['password_expiration_days'];
        $exp_date = date('Y-m-d', strtotime("+{$exp_days} days"));
        privStatement("update users set pwd_expiration_date=? where id=?", array($exp_date, $targetUser));
    }
    return true;
}
            // Not sure if this is even possible, but should escape if this happens
            session_destroy();
            header('Location: '.$landingpage.'&w');
            exit;
        }

        if ( $password_update)
            {
                $code_new=$_POST['pass_new'];
                $code_new_confirm=$_POST['pass_new_confirm'];
                if(!(empty($_POST['pass_new'])) && !(empty($_POST['pass_new_confirm'])) && ($code_new == $code_new_confirm)) {
                $new_salt=oemr_password_salt();
                $new_hash=oemr_password_hash($code_new,$new_salt);

                // Update the password and continue (patient is authorized)
                privStatement("UPDATE ".TBL_PAT_ACC_ON
                              ."  SET ".COL_POR_PWD."=?,".COL_POR_SALT."=?,".COL_POR_PWD_STAT."=1 WHERE id=?", array($new_hash,$new_salt,$auth['id']) );
                $authorizedPortal = true;
            }
        }
        if ($auth['portal_pwd_status'] == 0) {
            if(!$authorizedPortal) {
                // Need to enter a new password in the index.php script
                $_SESSION['password_update'] = 1;
                                header('Location: '.$landingpage);
                exit;
            }
        }

        if ($auth['portal_pwd_status'] == 1) {
            // continue (patient is authorized)
            $authorizedPortal = true;