#include "module_plan.h" #include #include #include #include namespace bootplan { namespace { bool basename_ok(const std::string &name) { if (name.size() < 5 || name.size() > 128 || name.compare(name.size() - 5, 5, ".conf") != 0) return false; for (unsigned char byte : name) { if (!((byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') || (byte >= '0' && byte <= '9') || byte == '_' || byte == '-' || byte == '.')) return false; } return true; } bool option_ok(const std::string &token) { if (token.empty()) return false; for (unsigned char byte : token) { if (!((byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') || (byte >= '0' && byte <= '9') || byte == '_' || byte == '.' || byte == ',' || byte == '=' || byte == ':' || byte == '+' || byte == '/' || byte == '-')) return false; } return true; } } Error compile_configuration(const std::vector &directories, const std::vector &available, Configuration &output) { output = Configuration{}; if (directories.size() > 5 || available.size() > 256) return Error::invalid; Configuration next; std::set modules; for (const auto &raw : available) { std::string module; if (!canonical_module(raw, module) || !modules.insert(module).second) return Error::invalid; } next.available.assign(modules.begin(), modules.end()); std::size_t files = 0, bytes = 0; std::map selected; for (const Directory &directory : directories) { if (directory.size() > 64) return Error::invalid; files += directory.size(); if (files > 128) return Error::invalid; std::set names; for (const File &file : directory) { if (!basename_ok(file.basename) || !names.insert(file.basename).second || file.text.size() > 65536) return Error::invalid; bytes += file.text.size(); if (bytes > 262144) return Error::invalid; for (unsigned char byte : file.text) if (byte > 127) return Error::invalid; selected.emplace(file.basename, &file); } } std::set blacklisted; std::map> options; std::map dependencies; for (const auto &entry : selected) { next.selected_files.push_back(entry.first); std::vector> rows; if (statements(entry.second->text, rows) != Error::none) return Error::invalid; for (const auto &tokens : rows) { std::string module; if (tokens.size() < 2 || !canonical_module(tokens[1], module)) return Error::invalid; if (tokens[0] == "blacklist") { if (tokens.size() != 2) return Error::invalid; blacklisted.insert(module); } else if (tokens[0] == "options") { if (tokens.size() < 3) return Error::invalid; for (std::size_t index = 2; index < tokens.size(); ++index) { if (!option_ok(tokens[index])) return Error::invalid; options[module].push_back(tokens[index]); } } else if (tokens[0] == "softdep") { if (dependencies.count(module)) return Error::invalid; Dependency dependency; dependency.module = module; bool saw_pre = false, saw_post = false; unsigned section = 0; for (std::size_t index = 2; index < tokens.size(); ++index) { const std::string &token = tokens[index]; if (token == "pre:") { if (saw_pre || saw_post) return Error::invalid; saw_pre = true; section = 1; } else if (token == "post:") { if (saw_post) return Error::invalid; saw_post = true; section = 2; } else { std::string target; if (!section || !canonical_module(token, target)) return Error::invalid; (section == 1 ? dependency.pre : dependency.post).push_back(std::move(target)); } } if (!saw_pre && !saw_post) return Error::invalid; dependencies.emplace(module, std::move(dependency)); } else return Error::invalid; } } next.blacklist.assign(blacklisted.begin(), blacklisted.end()); for (auto &entry : options) next.options.push_back(OptionGroup{entry.first, std::move(entry.second)}); for (auto &entry : dependencies) next.dependencies.push_back(std::move(entry.second)); output = std::move(next); return Error::none; } }