#include "user.h" #include #include #include //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // json ... static std::string device_opt_json[] = { "{\"login-hint\":{\"cat\":\"none\",\"group\":\"user\",\"title\":\"\\u767b\\u5f55\\u63d0\\u793a\",\"desc\":\"\\u767b\\u5f55\\u5e10\\u6237\\u63d0\\u793a\",\"type\":\"string\",\"ui-pos\":10,\"auth\":0,\"readonly\":true,\"size\":32,\"ownread\":true,\"cur\":\"\",\"default\":\"\"},\"user-name\":{\"cat\":\"none\",\"group\":\"user\",\"title\":\"\\u7528\\u6237\\u540d\",\"desc\":\"\\u767b\\u5f55\\u7528\\u6237\\u8d26\\u53f7\",\"type\":\"string\",\"fix-id\":39173,\"ui-pos\":15,\"auth\":0,\"size\":32,\"cur\":\"\",\"default\":\"\"},\"user-pwd\":{\"cat\":\"none\",\"group\":\"user\",\"title\":\"\\u5bc6\\u7801\",\"desc\":\"\\u767b\\u5f55\\u7528\\u6237\\u8d26\\u53f7\\u5bc6\\u7801\",\"type\":\"string\",\"fix-id\":39174,\"ui-pos\":16,\"auth\":0,\"size\":32,\"cur\":\"\",\"default\":\"\"},\"login\":{\"cat\":\"none\",\"group\":\"user\",\"title\":\"\\u767b\\u5f55\",\"desc\":\"\\u7528\\u6237\\u767b\\u5f55\",\"type\":\"button\",\"fix-id\":39168,\"ui-pos\":20,\"auth\":0,\"affect\":6,\"size\":4,\"auto\":false},\"logout\":{\"cat\":\"none\",\"group\":\"user\",\"title\":\"\\u6ce8\\u9500\",\"desc\":\"\\u7528\\u6237\\u767b\\u51fa\",\"type\":\"button\",\"fix-id\":39169,\"ui-pos\":21,\"auth\":0,\"affect\":6,\"size\":4,\"auto\":false},\"dev-sn\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u5e8f\\u5217\\u53f7\",\"desc\":\"\\u8bbe\\u5907\\u5e8f\\u5217\\u53f7\",\"type\":\"string\",\"pos\":100,\"fix-id\":34902,\"ui-pos\":14,\"auth\":0,\"size\":32,\"auto\":false,\"cur\":\"\",\"default\":\"\"}}" }; static bool check_user_password(const char* pwd, void* param) { return true; } static bool check_admin_password(const char* pwd, void* param) { return STRICMP(pwd, "HuaGoScan") == 0; } static bool check_owner_password(const char* pwd, void* param) { return STRICMP(pwd, "HuaGoScan") == 0; } static bool check_dev_password(const char* pwd, void* param) { return STRICMP(pwd, "HuaGoScan") == 0; } struct { std::string name; int priv; bool(*chkpwd)(const char* pwd, void* param); }g_user_priv[] = { {"user", USER_PRIVILEGE_COMMON, check_user_password} // no password needed , {"admin", USER_PRIVILEGE_LOCAL_MGR, check_admin_password} // fixed password - "HuaGoScan" , {"owner", USER_PRIVILEGE_TECH_SUPPORTING, check_owner_password} // calc - 1, valid in 1 hour , {"developer", USER_PRIVILEGE_DEVLOPER, check_dev_password} // calc - 2, valid in 5 minutes }; static std::string base64_table = "!@#$_~=;/+"; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // user_priv user_priv::user_priv() { simple_ini ini; std::string text(""); set_where("user"); for(auto& v : device_opt_json) text += v; set_opt_json_text(&text[0]); } user_priv::~user_priv() { } bool user_priv::login(void) { bool ok = false; for(auto& v: g_user_priv) { if(v.name == name_) { ok = v.chkpwd(pwd_.c_str(), &hint_[0]); break; } } return ok; } void user_priv::reset_privilege(void) { priv_ = USER_PRIVILEGE_COMMON; for(auto& v : g_user_priv) { if(v.name == name_) { priv_ = v.priv; break; } } } // sane_opt_provider int user_priv::set_value(const char* name, void* val) { int ret = SCANNER_ERR_OK; if(strcmp(name, SANE_OPT_NAME(USER_NAME)) == 0) { ret = SCANNER_ERR_INVALID_USER_NAME; for(auto& v: g_user_priv) { if(v.name == (char*)val) { ret = SCANNER_ERR_OK; name_ = (char*)val; break; } } priv_ = USER_PRIVILEGE_COMMON; } else if(strcmp(name, SANE_OPT_NAME(USER_PASSWORD)) == 0) { pwd_ = (char*)val; priv_ = USER_PRIVILEGE_COMMON; } else if(strcmp(name, SANE_OPT_NAME(DEVICE_SERIAL_NO)) == 0) { dev_sn_ = (char*)val; utils::to_log(LOG_LEVEL_DEBUG, "set user device SN: %s\n", dev_sn_.c_str()); priv_ = USER_PRIVILEGE_COMMON; } else if(strcmp(name, SANE_OPT_NAME(LOGIN)) == 0) { // login ... int prev = priv_; if(name_.empty()) { ret = SCANNER_ERR_INVALID_USER_NAME; } else if(login()) { reset_privilege(); } else { ret = SCANNER_ERR_INVALID_PASSWORD; } if(prev != priv_) ret = SCANNER_ERR_CONFIGURATION_CHANGED; *(int*)val = priv_; } else if(strcmp(name, SANE_OPT_NAME(LOGOUT)) == 0) { int prev = priv_; name_ = pwd_ = ""; reset_privilege(); *(int*)val = priv_; if(prev != priv_) ret = SCANNER_ERR_CONFIGURATION_CHANGED; } else { ret = SCANNER_ERR_DEVICE_NOT_SUPPORT; } return ret; } char* user_priv::get_value(const char* name, void* value, size_t* size, int* err) { char *ret = nullptr; int e = SCANNER_ERR_DEVICE_NOT_FOUND; if(strcmp(name, SANE_OPT_NAME(USER_LOGIN_HINT)) == 0) { ret = (char*)malloc(hint_.length() + 1); memcpy(ret, hint_.c_str(), hint_.length()); ret[hint_.length()] = 0; e = SCANNER_ERR_OK; } if(err) *err = e; return ret; } bool user_priv::has_privilege(int priv) { return priv_ >= priv; } int user_priv::get_current_privilege(void) { return priv_; }