/* * This file is a part of Winix * and is not publicly distributed * * Copyright (c) 2011-2012, Tomasz Sowa * All rights reserved. * */ #include "passwd.h" #include "core/slog.h" #include "functions/functions.h" namespace Fun { Passwd::Passwd() { fun.url = L"passwd"; } bool Passwd::HasAccess() { return cur->session->puser != 0; } bool Passwd::IsPasswordCorrect(const std::wstring & pass, const std::wstring & conf_pass, bool use_ses_log) { if( pass != conf_pass ) { log << log2 << "Passwd: passwords are different" << logend; if( use_ses_log ) slog << logerror << T("adduser_err_passwords_different") << logend; return false; } if( pass.size() < config->pass_min_size ) { log << log2 << "Passwd: password is too small" << logend; if( use_ses_log ) slog << logerror << T("adduser_err_password_too_small") << " " << config->pass_min_size << " " << T("adduser_err_password_too_small2") << logend; return false; } if( pass.size() > WINIX_ACCOUNT_MAX_PASSWORD_SIZE ) { log << log2 << "Passwd: password can't be longer than: " << WINIX_ACCOUNT_MAX_PASSWORD_SIZE << " characters" << logend; if( use_ses_log ) slog << logerror << T("adduser_err_password_too_big") << " " << WINIX_ACCOUNT_MAX_PASSWORD_SIZE << " " << T("adduser_err_password_too_big2") << logend; return false; } return true; } bool Passwd::ChangePassword(long user_id, const std::wstring & new_password) { bool result = false; User * puser = system->users.GetUser(user_id); if( puser ) { up.pass = new_password; system->crypt.PassHashCrypt(up); result = (db->ChangeUserPass(user_id, up) == WINIX_ERR_OK); if( result ) log << log2 << "Passwd: password for user " << puser->name << " has been changed" << logend; else log << log1 << "Passwd: I cannot change password -- database problem" << logend; } else { log << log1 << "Passwd: there is no a user with id: " << user_id << logend; } return result; } void Passwd::ChangePassword(User * puser) { long user_id; const std::wstring & pass_cur = cur->request->PostVar(L"passwordcur"); const std::wstring & pass_new = cur->request->PostVar(L"passwordnew"); const std::wstring & pass_conf = cur->request->PostVar(L"passwordconfirm"); if( !cur->session->puser->super_user && !functions->fun_login.CheckUserPass(puser->name, pass_cur, user_id) ) { log << log3 << "Passwd: incorrect current password" << logend; slog << logerror << T("passwd_err_bad_current_password") << logend; return; } if( !IsPasswordCorrect(pass_new, pass_conf) ) return; if( ChangePassword(cur->session->puser->id, pass_new) ) { slog << loginfo << T("passwd_password_changed") << logend; system->RedirectToLastItem(); } } /* if you are a root (super_user) you can change a password for everyone (the html form has a select option) but if you are not a root you can change only your password and you should provide your current password as well */ void Passwd::MakePost() { const std::wstring * plogin; if( cur->session->puser ) { if( cur->session->puser->super_user ) plogin = &cur->request->PostVar(L"login"); else plogin = &cur->session->puser->name; User * puser = system->users.GetUser(*plogin); if( puser ) ChangePassword(puser); else log << log1 << "Passwd: there is no such a user: " << *plogin << logend; } } } // namespace