#include "module_plan.h" #include #include #include namespace bootplan { namespace { bool ascii_letter(unsigned char value) { return (value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z'); } bool ascii_digit(unsigned char value) { return value >= '0' && value <= '9'; } bool filename(const std::string &name) { if (name.empty() || name.size() > 128) { return false; } if (name.size() < 5 || name.substr(name.size() - 5) != ".conf") { return false; } for (unsigned char value : name) { if (!(ascii_letter(value) || ascii_digit(value) || value == '_' || value == '.' || value == '-')) { return false; } } return true; } bool text_domain(const std::string &text) { if (text.size() > 65536) { return false; } for (unsigned char value : text) { if (value > 127) { return false; } } return true; } bool option(const std::string &token) { if (token.empty()) { return false; } const std::string punctuation = "_.,=:+/-"; for (unsigned char value : token) { if (!(ascii_letter(value) || ascii_digit(value) || punctuation.find(static_cast(value)) != std::string::npos)) { return false; } } return true; } void append_option(std::vector &tokens, const std::string &token) { const std::string key = token.substr(0, token.find('=')); tokens.erase(std::remove_if(tokens.begin(), tokens.end(), [&](const std::string &previous) { return previous.substr(0, previous.find('=')) == key; }), tokens.end()); tokens.push_back(token); } struct Parsed { std::set blacklist; std::map> options; std::map dependencies; }; bool parse_softdep(const std::string &module, const std::vector &tokens, Dependency &result) { result = Dependency{}; result.module = module; bool pre_seen = false; bool post_seen = false; std::vector *destination = nullptr; if (tokens.size() < 3) { return false; } for (std::size_t index = 2; index < tokens.size(); ++index) { const std::string &token = tokens[index]; if (token == "pre:") { if (pre_seen || post_seen) { return false; } pre_seen = true; destination = &result.pre; } else if (token == "post:") { if (post_seen) { return false; } post_seen = true; destination = &result.post; } else { std::string canonical; if (destination == nullptr || !canonical_module(token, canonical)) { return false; } destination->push_back(std::move(canonical)); } } return pre_seen || post_seen; } bool parse_line(const std::vector &tokens, Parsed &parsed) { if (tokens.size() < 2) { return false; } std::string name; if (!canonical_module(tokens[1], name)) { return false; } if (tokens[0] == "blacklist") { if (tokens.size() != 2) { return false; } parsed.blacklist.insert(name); return true; } if (tokens[0] == "options") { if (tokens.size() < 3) { return false; } for (std::size_t index = 2; index < tokens.size(); ++index) { if (!option(tokens[index])) { return false; } append_option(parsed.options[name], tokens[index]); } return true; } if (tokens[0] == "softdep") { if (parsed.dependencies.count(name) != 0) { return false; } Dependency dependency; if (!parse_softdep(name, tokens, dependency)) { return false; } parsed.dependencies.emplace(name, std::move(dependency)); return true; } return false; } bool choose_files(const std::vector &directories, std::map &selected) { if (directories.size() > 5) { return false; } std::size_t total_files = 0; std::size_t total_bytes = 0; for (const Directory &directory : directories) { if (directory.size() > 64) { return false; } std::set names; for (const File &file : directory) { if (!filename(file.basename) || !text_domain(file.text)) { return false; } if (!names.insert(file.basename).second) { return false; } ++total_files; total_bytes += file.text.size(); if (total_files > 128 || total_bytes > 262144) { return false; } selected[file.basename] = file.text; } } return true; } bool normalize_available(const std::vector &available, std::set &names) { if (available.size() > 256) { return false; } for (const std::string &raw : available) { std::string canonical; if (!canonical_module(raw, canonical)) { return false; } if (!names.insert(canonical).second) { return false; } } return true; } } Error compile_configuration(const std::vector &directories, const std::vector &available, Configuration &output) { output = Configuration{}; std::map selected; std::set names; if (!choose_files(directories, selected) || !normalize_available(available, names)) { return Error::invalid; } Parsed parsed; for (const auto &file : selected) { std::vector> lines; if (statements(file.second, lines) != Error::none) { return Error::invalid; } for (const auto &line : lines) { if (!parse_line(line, parsed)) { return Error::invalid; } } } Configuration result; result.available.assign(names.begin(), names.end()); result.blacklist.assign(parsed.blacklist.begin(), parsed.blacklist.end()); for (const auto &file : selected) { result.selected_files.push_back(file.first); } for (auto &group : parsed.options) { OptionGroup item; item.module = group.first; item.tokens = std::move(group.second); result.options.push_back(std::move(item)); } for (auto &group : parsed.dependencies) { result.dependencies.push_back(std::move(group.second)); } output = std::move(result); return Error::none; } }