From 0057ebdfe7691ddc03d2dbedf17aad00986cfb8c Mon Sep 17 00:00:00 2001 From: Igor Belitskiy Date: Mon, 3 Apr 2023 11:06:41 +0600 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/ubl-settings-bootloader.cc | 73 ++++++++++++++++++++++------ source/ubl-settings-bootloader.h | 3 +- source/util.cc | 81 +++++++++++++++++++++++++++++++ source/util.h | 12 ++++- 4 files changed, 151 insertions(+), 18 deletions(-) diff --git a/source/ubl-settings-bootloader.cc b/source/ubl-settings-bootloader.cc index ddd4367..8dd814e 100755 --- a/source/ubl-settings-bootloader.cc +++ b/source/ubl-settings-bootloader.cc @@ -167,6 +167,7 @@ void MainWindow::item_selected_IPT(const Gtk::TreeModel::Path& path, const Gtk:: } void MainWindow::get_download_mode(string cmd, std::map &map_temp) { + int start_error = index_error; struct Result res_response = this->wrapper_call(cmd); if (res_response.error == 0) { str_replace_all(res_response.response, "\"",""); @@ -194,6 +195,9 @@ void MainWindow::get_download_mode(string cmd, std::map &map_te else if (res_response.error == 3) { cmbDownloadMode->set_active(-1); map_temp["GRUB_BOOT_SILENT"] = ""; + if (start_error != index_error) { + index_error = start_error; + } } else { cmbDownloadMode->set_active(-1); @@ -250,6 +254,7 @@ void MainWindow::set_security_login(string cmd="") { } void MainWindow::get_security_login(string cmd, std::map &map_temp) { + int start_error = index_error; struct Result res_response = this->wrapper_call(cmd); // TODO: // Убрать в будущем @@ -277,6 +282,9 @@ void MainWindow::get_security_login(string cmd, std::map &map_t else if (res_response.error == 3) { chbSecurityLogin->set_active(-1); map_temp["GRUB_USER"] = ""; + if (start_error != index_error) { + index_error = start_error; + } } else { chbSecurityLogin->set_active(-1); @@ -315,8 +323,8 @@ void MainWindow::set_password_protec(string cmd = "", string cmd_remove = "") { } void MainWindow::get_password_protec(string cmd, std::map &map_temp) { + int start_error = index_error; struct Result res_response = this->wrapper_call(cmd); - cout << res_response.response << " " << res_response.error << endl; if (res_response.error == 0) { str_replace_all(res_response.response, "\"",""); entryPasswordProtecc->set_text(res_response.response); @@ -340,6 +348,9 @@ void MainWindow::get_password_protec(string cmd, std::map &map_ chbPasswordProtecc->set_active(2); map_temp["GRUB_PASSWORD"] = ""; entryPasswordProtecc->set_text(""); + if (start_error != index_error) { + index_error = start_error; + } } else { map_temp["GRUB_PASSWORD"] = ""; @@ -513,6 +524,7 @@ void MainWindow::localization(){ } vector MainWindow::get_setting_entry_all(string cmd, Gtk::Entry &entry_text, std::map &map_temp) { + int start_error = index_error; vector vec_params; struct Result res_response = this->wrapper_call(cmd); if (res_response.error == 0) { @@ -526,7 +538,9 @@ vector MainWindow::get_setting_entry_all(string cmd, Gtk::Entry &entry_t } } else if (res_response.error == 3) { - + if (start_error != index_error) { + index_error = start_error; + } } else { @@ -538,6 +552,10 @@ bool MainWindow::focus_out_txt_password(GdkEventFocus*) { if (entryPasswordProtecc->get_text().length() == 0) { info_status_app("boxInfoMessError"); lblWarning->set_text(gettext("Enter your password")); + boxSave->set_sensitive(false); + } + else { + boxSave->set_sensitive(true); } return true; } @@ -545,6 +563,10 @@ bool MainWindow::focus_out_txt_login(GdkEventFocus*) { if (entrySecurityLogin->get_text().length() == 0) { info_status_app("boxInfoMessError"); lblWarning->set_text(gettext("Enter login")); + boxSave->set_sensitive(false); + } + else { + boxSave->set_sensitive(true); } return true; } @@ -578,13 +600,16 @@ void MainWindow::set_entry_to_tree_view(Glib::RefPtr &list_store flag_blocked_tree_view = false; } void MainWindow::get_menu_boot(string cmd, std::map &map_temp) { + int start_error = index_error; struct Result res_response = this->wrapper_call(cmd); if (res_response.error == 0) { map_temp["GRUB_TIMEOUT"] = res_response.response; spbSecond->set_value(atoi(res_response.response.c_str())); } else if (res_response.error == 3) { - + if (start_error != index_error) { + index_error = start_error; + } } else { @@ -602,12 +627,17 @@ void MainWindow::set_menu_boot(string cmd="") { } -void MainWindow::cmd_entry_all(Gtk::Entry &entry, string cmd_settings) { +void MainWindow::cmd_entry_all(Gtk::Entry &entry, string cmd_settings, string cmd_remove) { string cmds = entry.get_text(); str_remove(cmds, ","); - cmd_settings += "\"" + cmds + "\""; - str_replace_all(cmd_settings, " \"", "\""); - this->wrapper_system(cmd_settings, ""); + if (cmds.length() != 0) { + cmd_settings += "\"" + cmds + "\""; + str_replace_all(cmd_settings, " \"", "\""); + this->wrapper_system(cmd_settings, ""); + } + else { + this->wrapper_system(cmd_remove, ""); + } } void MainWindow::event(){ @@ -647,14 +677,17 @@ void MainWindow::change_security_login() { // Добавить заполнение логина по умолчанию string default_login = "superadmin"; entrySecurityLogin->set_text(default_login); + boxSave->set_sensitive(true); } else if (index == 1) { entrySecurityLogin->set_sensitive(true); entrySecurityLogin->set_text(""); + boxSave->set_sensitive(true); } else { entrySecurityLogin->set_sensitive(false); entrySecurityLogin->set_text(""); + boxSave->set_sensitive(true); } } @@ -663,10 +696,15 @@ void MainWindow::change_password_protecc() { if (index == 0 || index == 2 || index == -1) { entryPasswordProtecc->set_sensitive(false); entryPasswordProtecc->set_text(""); + boxSave->set_sensitive(true); } else if (index == 1) { entryPasswordProtecc->set_sensitive(true); entryPasswordProtecc->set_text(""); + boxSave->set_sensitive(true); + } + else { + boxSave->set_sensitive(true); } } @@ -709,17 +747,20 @@ bool MainWindow::save_global_cfg() { } if (this->check_save(flag_save, "GRUB_CMDLINE_LINUX")) { string cmd_settings_kernel = "ubconfig --target global set boot GRUB_CMDLINE_LINUX="; - this->cmd_entry_all(*entryKernel, cmd_settings_kernel); + string cmd_remove = "ubconfig --target global remove boot GRUB_CMDLINE_LINUX"; + this->cmd_entry_all(*entryKernel, cmd_settings_kernel, cmd_remove); flag_no_save = false; } if (this->check_save(flag_save, "GRUB_TERMINAL_OUTPUT")) { string cmd_settings_IPT = "ubconfig --target global set boot GRUB_TERMINAL_OUTPUT="; - this->cmd_entry_all(*entryIPT, cmd_settings_IPT); + string cmd_remove = "ubconfig --target global remove boot GRUB_TERMINAL_OUTPUT"; + this->cmd_entry_all(*entryIPT, cmd_settings_IPT,cmd_remove); flag_no_save = false; } if (this->check_save(flag_save, "GRUB_TERMINAL_INPUT")) { string cmd_settings_OTT = "ubconfig --target global set boot GRUB_TERMINAL_INPUT="; - this->cmd_entry_all(*entryOTT, cmd_settings_OTT); + string cmd_remove = "ubconfig --target global remove boot GRUB_TERMINAL_INPUT"; + this->cmd_entry_all(*entryOTT, cmd_settings_OTT, cmd_remove); flag_no_save = false; } if (this->check_save(flag_save, "GRUB_BOOT_SILENT")) { @@ -766,17 +807,20 @@ bool MainWindow::save_local_cfg() { } if (this->check_save(flag_save, "GRUB_CMDLINE_LINUX")) { string cmd_settings_kernel = "ubconfig --target system set boot GRUB_CMDLINE_LINUX="; - this->cmd_entry_all(*entryKernel, cmd_settings_kernel); + string cmd_remove = "ubconfig --target system remove boot GRUB_CMDLINE_LINUX"; + this->cmd_entry_all(*entryKernel, cmd_settings_kernel, cmd_remove); flag_no_save = false; } if (this->check_save(flag_save, "GRUB_TERMINAL_OUTPUT")) { string cmd_settings_IPT = "ubconfig --target system set boot GRUB_TERMINAL_OUTPUT="; - this->cmd_entry_all(*entryIPT, cmd_settings_IPT); + string cmd_remove = "ubconfig --target system remove boot GRUB_TERMINAL_OUTPUT"; + this->cmd_entry_all(*entryIPT, cmd_settings_IPT, cmd_remove); flag_no_save = false; } if (this->check_save(flag_save, "GRUB_TERMINAL_INPUT")) { string cmd_settings_OTT = "ubconfig --target system set boot GRUB_TERMINAL_INPUT="; - this->cmd_entry_all(*entryOTT, cmd_settings_OTT); + string cmd_remove = "ubconfig --target system remove boot GRUB_TERMINAL_INPUT"; + this->cmd_entry_all(*entryOTT, cmd_settings_OTT, cmd_remove); flag_no_save = false; } if (this->check_save(flag_save, "GRUB_BOOT_SILENT")) { @@ -870,8 +914,6 @@ void MainWindow::download_local_cfg() { } bool MainWindow::check_save(string flag_save, string key_name="") { - if (flag_save.empty() && key_name.empty()) {} - /* std::map :: iterator iter_map_data; std::map :: iterator iter_map_data_old; std::map map_config_data_old; @@ -909,7 +951,6 @@ bool MainWindow::check_save(string flag_save, string key_name="") { return true; } cout << 7 << endl; - */ return true; } diff --git a/source/ubl-settings-bootloader.h b/source/ubl-settings-bootloader.h index 05b4bbd..dd5fb1f 100644 --- a/source/ubl-settings-bootloader.h +++ b/source/ubl-settings-bootloader.h @@ -121,6 +121,7 @@ class MainWindow : public Gtk::ApplicationWindow { bool flag_save_global = false; bool flag_save_local = false; bool flag_blocked_tree_view = false; + bool flag_save_block = false; size_t size_kernel = 0; size_t size_IPT = 0; size_t size_OTT = 0; @@ -183,7 +184,7 @@ class MainWindow : public Gtk::ApplicationWindow { void get_password_protec(string cmd, std::map &map_temp); void set_password_protec(string cmd, string cmd_remove); vector get_setting_entry_all(string cmd, Gtk::Entry &entry_text, std::map &map_temp); - void cmd_entry_all(Gtk::Entry &entry, string cmd_settings); + void cmd_entry_all(Gtk::Entry &entry, string cmd_settings, string cmd_remove); string dynamic_update_entry(std::map &map_view, vector &vec_allowed); void item_selected_kernel(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator&); void item_selected_OTT(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator&); diff --git a/source/util.cc b/source/util.cc index 5c66276..18b5446 100644 --- a/source/util.cc +++ b/source/util.cc @@ -101,4 +101,85 @@ std::vector split(std::string text, char delim) { vec.push_back(line); } return vec; +} +unsigned short read_uid_min_max(string filename, string search) { + std::string line; + int uid = 0; + string remove_tab = "\t"; + string remove_space = " "; + std::ifstream in(filename); // окрываем файл для чтения + if (in.is_open()){ + while (getline(in, line)){ + try{ + if (line.find(search) != string::npos && (line.find("SYS_"+search) == string::npos)) { + str_remove(line, search); + str_remove(line, remove_space); + str_remove(line, remove_tab); + uid = atoi(line.c_str()); + } + } + catch (int x) { + if (search == "UID_MIN"){ + uid = 1000; + } + else{ + uid = 65534; + } + + } + + } + } + else{ + if (search == "UID_MIN") { + uid = 1000; + } + else{ + uid = 65534; + } + } + in.close(); + return uid; + +} + +vector pars_users() { + vector vec_users; + unsigned short uid_min = read_uid_min_max("/etc/login.defs", "UID_MIN"); + unsigned short uid_max =read_uid_min_max("/etc/login.defs", "UID_MAX"); + while (true) { + errno = 0; + passwd* entry = getpwent(); + if (!entry) { + if (errno) { + break; + } + break; + } + if ((entry->pw_uid >= uid_min && entry->pw_uid < uid_max) || entry->pw_uid == 0) { + vec_users.push_back(string(entry->pw_name)); + } + } + endpwent(); + return vec_users; +} + +string read_passwd(string username) { + string passwd = ""; + std::string line; + std::ifstream in("/etc/shadow"); + if (in.is_open()) { + while (getline(in, line)) { + if (line.find(username) != string::npos) { + size_t index_start = line.find(":"); + if (index_start != string::npos) { + size_t index_end = line.find(":", index_start + 1); + passwd = line.substr(index_start+1, index_end - index_start-1); + break; + } + } + } + } + in.close(); + return passwd; } \ No newline at end of file diff --git a/source/util.h b/source/util.h index 9bb331f..23b89fb 100644 --- a/source/util.h +++ b/source/util.h @@ -6,13 +6,23 @@ #include #include #include +#include +#include + using namespace std; +struct Passwd { + string user; + int uid; +}; std::array, 5> read_csv(const std::string& filename); std::string call(std::string cmd); vector find_all(string &str_ntp, string substr); void str_remove(std::string& source, std::string to_remove); void str_replace_all(string &str_base, string str_find, string str_replace); -std::vector split(std::string text, char delim); \ No newline at end of file +std::vector split(std::string text, char delim); +unsigned short read_uid_min_max(string filename, string search); +vector pars_users(); +string read_passwd(string username);