void Data::getAllDependenciesToInstall(Config *config, std::vector<Config*>* installedConfigs, std::vector<Config*> *dependencies) { for (std::vector<std::string>::const_iterator configDependency = config->dependencies_.begin(); configDependency != config->dependencies_.end(); configDependency++) { bool found = false; for (std::vector<Config*>::const_iterator installedConfig = installedConfigs->begin(); installedConfig != installedConfigs->end(); installedConfig++) { if ((*configDependency) == (*installedConfig)->name_) { found = true; break; } } if (found) continue; // Check if already in vector for (std::vector<Config*>::const_iterator dependency = dependencies->begin(); dependency != dependencies->end(); dependency++) { if ((*dependency)->name_ == (*configDependency)) { found = true; break; } } if (found) { continue; } else { // Add to vector and check for further subdepends... Config *dependconfig = getDatabaseConfig((*configDependency), config->type_); if (dependconfig == nullptr) { continue; } else { dependencies->push_back(dependconfig); getAllDependenciesToInstall(dependconfig, installedConfigs, dependencies); } } } }
void DatabaseManager::registerDatabaseConfig(const std::string& config, const std::string& value) { Database* db = Database::getInstance(); DBQuery query; std::string tmp; if(!getDatabaseConfig(config, tmp)) query << "INSERT INTO `server_config` VALUES (" << db->escapeString(config) << ", " << db->escapeString(value) << ");"; else query << "UPDATE `server_config` SET `value` = " << db->escapeString(value) << " WHERE `config` = " << db->escapeString(config) << ";"; db->executeQuery(query.str()); }
int32_t DatabaseManager::getDatabaseVersion() { if (!tableExists("server_config")) { Database* db = Database::getInstance(); db->executeQuery("CREATE TABLE `server_config` (`config` VARCHAR(50) NOT nullptr, `value` VARCHAR(256) NOT nullptr DEFAULT '', UNIQUE(`config`)) ENGINE = InnoDB"); db->executeQuery("INSERT INTO `server_config` VALUES ('db_version', 0)"); return 0; } int32_t version = 0; if (getDatabaseConfig("db_version", version)) { return version; } return -1; }
void DatabaseManager::registerDatabaseConfig(const std::string& config, int32_t value) { Database* db = Database::getInstance(); std::ostringstream query; int32_t tmp; if (!getDatabaseConfig(config, tmp)) { query << "INSERT INTO `server_config` VALUES (" << db->escapeString(config) << ", '" << value << "')"; } else { query << "UPDATE `server_config` SET `value` = '" << value << "' WHERE `config` = " << db->escapeString(config); } db->executeQuery(query.str()); }
void DatabaseManager::checkEncryption() { int32_t currentValue = g_config.getNumber(ConfigManager::PASSWORD_TYPE); int32_t oldValue = 0; if (getDatabaseConfig("encryption", oldValue)) { if (currentValue == oldValue) { return; } if (oldValue != PASSWORD_TYPE_PLAIN) { std::string oldName; if (oldValue == PASSWORD_TYPE_MD5) { oldName = "md5"; } else if (oldValue == PASSWORD_TYPE_SHA1) { oldName = "sha1"; } else { oldName = "plain"; } g_config.setNumber(ConfigManager::PASSWORD_TYPE, oldValue); std::cout << "> WARNING: Unsupported password hashing switch! Change back passwordType in config.lua to \"" << oldName << "\"!" << std::endl; return; } switch (currentValue) { case PASSWORD_TYPE_MD5: { Database::getInstance()->executeQuery("UPDATE `accounts` SET `password` = MD5(`password`)"); std::cout << "> Password type has been updated to MD5." << std::endl; break; } case PASSWORD_TYPE_SHA1: { Database::getInstance()->executeQuery("UPDATE `accounts` SET `password` = SHA1(`password`)"); std::cout << "> Password type has been updated to SHA1." << std::endl; break; } default: break; } } registerDatabaseConfig("encryption", currentValue); }
int32_t DatabaseManager::getDatabaseVersion() { if(!tableExists("server_config")) { Database* db = Database::getInstance(); if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL) db->executeQuery("CREATE TABLE `server_config` (`config` VARCHAR(50) NOT NULL, `value` VARCHAR(256) NOT NULL DEFAULT '', UNIQUE(`config`)) ENGINE = InnoDB;"); else db->executeQuery("CREATE TABLE `server_config` (`config` VARCHAR(50) NOT NULL, `value` VARCHAR(256) NOT NULL DEFAULT '', UNIQUE(`config`));"); db->executeQuery("INSERT INTO `server_config` VALUES ('db_version', 0);"); return 0; } int32_t version = 0; if(getDatabaseConfig("db_version", version)) return version; return -1; }
int Mhwd::launch(int argc, char *argv[]) { std::vector<std::string> missingDirs { checkEnvironment() }; if (!missingDirs.empty()) { consoleWriter_.printError("Following directories do not exist:"); for (const auto& dir : missingDirs) { consoleWriter_.printStatus(dir); } return 1; } std::string operationType; bool autoConfigureNonFreeDriver = false; std::string autoConfigureClassID; try { tryToParseCmdLineOptions(argc, argv, autoConfigureNonFreeDriver, operationType, autoConfigureClassID); } catch(const std::runtime_error& e) { consoleWriter_.printError(e.what()); consoleWriter_.printHelp(); return 1; } if (!optionsDontInterfereWithEachOther()) { return 1; } // Check for invalid configs for (auto&& invalidConfig : data_.invalidConfigs) { consoleWriter_.printWarning("config '" + invalidConfig->configPath_ + "' is invalid!"); } // > Perform operations: // List all configs if (arguments_.LIST_ALL && arguments_.SHOW_PCI) { if (!data_.allPCIConfigs.empty()) { consoleWriter_.listConfigs(data_.allPCIConfigs, "All PCI configs:"); } else { consoleWriter_.printWarning("No PCI configs found!"); } } if (arguments_.LIST_ALL && arguments_.SHOW_USB) { if (!data_.allUSBConfigs.empty()) { consoleWriter_.listConfigs(data_.allUSBConfigs, "All USB configs:"); } else { consoleWriter_.printWarning("No USB configs found!"); } } // List installed configs if (arguments_.LIST_INSTALLED && arguments_.SHOW_PCI) { if (arguments_.DETAIL) { consoleWriter_.printInstalledConfigs("PCI", data_.installedPCIConfigs); } else { if (!data_.installedPCIConfigs.empty()) { consoleWriter_.listConfigs(data_.installedPCIConfigs, "Installed PCI configs:"); } else { consoleWriter_.printWarning("No installed PCI configs!"); } } } if (arguments_.LIST_INSTALLED && arguments_.SHOW_USB) { if (arguments_.DETAIL) { consoleWriter_.printInstalledConfigs("USB", data_.installedUSBConfigs); } else { if (!data_.installedUSBConfigs.empty()) { consoleWriter_.listConfigs(data_.installedUSBConfigs, "Installed USB configs:"); } else { consoleWriter_.printWarning("No installed USB configs!"); } } } // List available configs if (arguments_.LIST_AVAILABLE && arguments_.SHOW_PCI) { if (arguments_.DETAIL) { consoleWriter_.printAvailableConfigsInDetail("PCI", data_.PCIDevices); } else { for (auto&& PCIDevice : data_.PCIDevices) { if (!PCIDevice->availableConfigs_.empty()) { consoleWriter_.listConfigs(PCIDevice->availableConfigs_, PCIDevice->sysfsBusID_ + " (" + PCIDevice->classID_ + ":" + PCIDevice->vendorID_ + ":" + PCIDevice->deviceID_ + ") " + PCIDevice->className_ + " " + PCIDevice->vendorName_ + ":"); } } } } if (arguments_.LIST_AVAILABLE && arguments_.SHOW_USB) { if (arguments_.DETAIL) { consoleWriter_.printAvailableConfigsInDetail("USB", data_.USBDevices); } else { for (auto&& USBdevice : data_.USBDevices) { if (!USBdevice->availableConfigs_.empty()) { consoleWriter_.listConfigs(USBdevice->availableConfigs_, USBdevice->sysfsBusID_ + " (" + USBdevice->classID_ + ":" + USBdevice->vendorID_ + ":" + USBdevice->deviceID_ + ") " + USBdevice->className_ + " " + USBdevice->vendorName_ + ":"); } } } } // List hardware information if (arguments_.LIST_HARDWARE && arguments_.SHOW_PCI) { if (arguments_.DETAIL) { consoleWriter_.printDeviceDetails(hw_pci); } else { consoleWriter_.listDevices(data_.PCIDevices, "PCI"); } } if (arguments_.LIST_HARDWARE && arguments_.SHOW_USB) { if (arguments_.DETAIL) { consoleWriter_.printDeviceDetails(hw_usb); } else { consoleWriter_.listDevices(data_.USBDevices, "USB"); } } // Auto configuration if (arguments_.AUTOCONFIGURE) { std::vector<std::shared_ptr<Device>> *devices; std::vector<std::shared_ptr<Config>> *installedConfigs; if ("USB" == operationType) { devices = &data_.USBDevices; installedConfigs = &data_.installedUSBConfigs; } else { devices = &data_.PCIDevices; installedConfigs = &data_.installedPCIConfigs; } bool foundDevice = false; for (auto&& device : *devices) { if (device->classID_ != autoConfigureClassID) { continue; } else { foundDevice = true; std::shared_ptr<Config> config; for (auto&& availableConfig : device->availableConfigs_) { if (autoConfigureNonFreeDriver || availableConfig->freedriver_) { config = availableConfig; break; } } if (nullptr == config) { consoleWriter_.printWarning( "No config found for device: " + device->sysfsBusID_ + " (" + device->classID_ + ":" + device->vendorID_ + ":" + device->deviceID_ + ") " + device->className_ + " " + device->vendorName_ + " " + device->deviceName_); continue; } else { // If force is not set then skip found config bool skip = false; if (!arguments_.FORCE) { skip = std::find_if(installedConfigs->begin(), installedConfigs->end(), [&config](const std::shared_ptr<Config>& conf) -> bool { return conf->name_ == config->name_; }) != installedConfigs->end(); } // Print found config if (skip) { consoleWriter_.printStatus( "Skipping already installed config '" + config->name_ + "' for device: " + device->sysfsBusID_ + " (" + device->classID_ + ":" + device->vendorID_ + ":" + device->deviceID_ + ") " + device->className_ + " " + device->vendorName_ + " " + device->deviceName_); } else { consoleWriter_.printStatus( "Using config '" + config->name_ + "' for device: " + device->sysfsBusID_ + " (" + device->classID_ + ":" + device->vendorID_ + ":" + device->deviceID_ + ") " + device->className_ + " " + device->vendorName_ + " " + device->deviceName_); } bool alreadyInList = std::find(configs_.begin(), configs_.end(), config->name_) != configs_.end(); if (!alreadyInList && !skip) { configs_.push_back(config->name_); } } } } if (!foundDevice) { consoleWriter_.printWarning("No device of class " + autoConfigureClassID + " found!"); } else if (!configs_.empty()) { arguments_.INSTALL = true; } } // Transaction if (arguments_.INSTALL || arguments_.REMOVE) { if (!isUserRoot()) { consoleWriter_.printError("You cannot perform this operation unless you are root!"); } else { for (auto&& configName = configs_.begin(); configName != configs_.end(); configName++) { if (arguments_.CUSTOM_INSTALL) { // Custom install -> get configs struct stat filestatus; std::string filepath = (*configName) + "/MHWDCONFIG"; if (0 != stat(filepath.c_str(), &filestatus)) { consoleWriter_.printError("custom config '" + filepath + "' does not exist!"); return 1; } else if (!S_ISREG(filestatus.st_mode)) { consoleWriter_.printError("custom config '" + filepath + "' is invalid!"); return 1; } else { config_.reset(new Config(filepath, operationType)); if (!config_->readConfigFile(filepath)) { consoleWriter_.printError("failed to read custom config '" + filepath + "'!"); return 1; } else if (!performTransaction(config_, MHWD::TRANSACTIONTYPE::INSTALL)) { return 1; } } } else if (arguments_.INSTALL) { config_ = getAvailableConfig((*configName), operationType); if (config_ == nullptr) { config_ = getDatabaseConfig((*configName), operationType); if (config_ == nullptr) { consoleWriter_.printError("config '" + (*configName) + "' does not exist!"); return 1; } else { consoleWriter_.printWarning( "no matching device for config '" + (*configName) + "' found!"); } } if (!performTransaction(config_, MHWD::TRANSACTIONTYPE::INSTALL)) { return 1; } } else if (arguments_.REMOVE) { config_ = getInstalledConfig((*configName), operationType); if (nullptr == config_) { consoleWriter_.printError("config '" + (*configName) + "' is not installed!"); return 1; } else if (!performTransaction(config_, MHWD::TRANSACTIONTYPE::REMOVE)) { return 1; } } } } } return 0; }
void DatabaseManager::checkEncryption() { int32_t currentValue = g_config.getNumber(ConfigManager::PASSWORD_TYPE); int32_t oldValue = 0; if(getDatabaseConfig("encryption", oldValue)) { if(currentValue == oldValue) return; if(oldValue != PASSWORD_TYPE_PLAIN) { std::string oldName; if(oldValue == PASSWORD_TYPE_MD5) oldName = "md5"; else if(oldValue == PASSWORD_TYPE_SHA1) oldName = "sha1"; else oldName = "plain"; g_config.setNumber(ConfigManager::PASSWORD_TYPE, oldValue); std::cout << "> WARNING: Unsupported password hashing switch! Change back passwordType in config.lua to \"" << oldName << "\"!" << std::endl; return; } switch(currentValue) { case PASSWORD_TYPE_MD5: { Database* db = Database::getInstance(); DBQuery query; if(db->getDatabaseEngine() != DATABASE_ENGINE_MYSQL) { DBResult* result = db->storeQuery("SELECT `id`, `password`, `key` FROM `accounts`;"); if(result) { do { query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToMD5(result->getDataString("password"))) << ", `key` = " << db->escapeString(transformToMD5(result->getDataString("key"))) << " WHERE `id` = " << result->getDataInt("id") << ";"; db->executeQuery(query.str()); } while(result->next()); db->freeResult(result); } } else db->executeQuery("UPDATE `accounts` SET `password` = MD5(`password`), `key` = MD5(`key`);"); std::cout << "> Password type has been updated to MD5." << std::endl; break; } case PASSWORD_TYPE_SHA1: { Database* db = Database::getInstance(); DBQuery query; if(db->getDatabaseEngine() != DATABASE_ENGINE_MYSQL) { DBResult* result = db->storeQuery("SELECT `id`, `password`, `key` FROM `accounts`;"); if(result) { do { query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToSHA1(result->getDataString("password"))) << ", `key` = " << db->escapeString(transformToSHA1(result->getDataString("key"))) << " WHERE `id` = " << result->getDataInt("id") << ";"; db->executeQuery(query.str()); } while(result->next()); db->freeResult(result); } } else db->executeQuery("UPDATE `accounts` SET `password` = SHA1(`password`), `key` = SHA1(`key`);"); std::cout << "> Password type has been updated to SHA1." << std::endl; break; } default: break; } } else if(g_config.getBoolean(ConfigManager::ACCOUNT_MANAGER)) { switch(currentValue) { case PASSWORD_TYPE_MD5: { Database* db = Database::getInstance(); DBQuery query; query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToMD5("1")) << " WHERE `id` = 1 AND `password` = '1';"; db->executeQuery(query.str()); break; } case PASSWORD_TYPE_SHA1: { Database* db = Database::getInstance(); DBQuery query; query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToSHA1("1")) << " WHERE `id` = 1 AND `password` = '1';"; db->executeQuery(query.str()); break; } default: break; } } registerDatabaseConfig("encryption", currentValue); }