From fd93a937a4a3da87ee17fa27499ec06d7fcc22aa Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 23 Oct 2023 22:44:49 -0700 Subject: [PATCH] Basic debian put. Unverified. --- CMakeLists.txt | 2 +- src/packager.cc | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index de8d647..8352a47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ include_directories(SYSTEM ${CURLPP_INCLUDE_DIRS}) ### THE REAL TARGETS ### add_executable(${PROJECT_NAME}-packager src/packager.cc ${SOURCES} ${HEADERS}) -target_link_libraries(${PROJECT_NAME}-packager ${SCSL_LIBRARIES}) +target_link_libraries(${PROJECT_NAME}-packager ${SCSL_LIBRARIES} ${CURLPP_LIBRARIES}) ### INCLUDES ### diff --git a/src/packager.cc b/src/packager.cc index 4bf2832..abc8c55 100644 --- a/src/packager.cc +++ b/src/packager.cc @@ -9,11 +9,21 @@ #include #include +#include +#include +#include +#include + +#include #include #include #include +const auto defaultOwner = std::string("sc"); +const auto defaultDistribution = std::string("ubuntu"); +const auto defaultComponent = std::string("main"); + void usage(std::ostream &outs, int exc) { @@ -52,13 +62,82 @@ getDefaultConfigFile() return userHome; } + +static std::string +debianEndpoint(std::string endpoint) +{ + if (!endpoint.empty() && endpoint.at(0) != '/') { + endpoint.insert(endpoint.cbegin(), '/'); + } + + auto urlBase = scsl::SimpleConfig::GetGlobal("server"); + return urlBase + "/api/packages/" + + scsl::SimpleConfig::GetGlobal("owner", defaultOwner) + + "/debian/pool/" + + scsl::SimpleConfig::GetGlobal("distribution", defaultDistribution) + + "/" + + scsl::SimpleConfig::GetGlobal("component", defaultComponent) + + endpoint; +} + +static std::string +getUserPass() +{ + return scsl::SimpleConfig::GetGlobal("http_user") + ":" + + scsl::SimpleConfig::GetGlobal("http_password"); +} + static bool putDebian(std::vector args) { + auto ok = true; std::cout << "[+] put Debian package\n"; for (auto &arg: args) { std::cout << "\t[*] " << arg << "\n"; } + + auto url = debianEndpoint("upload"); + auto userPass = getUserPass(); + for (auto &arg: args) { + try { + std::list header; + header.emplace_back("Content-Type: application/octet-stream"); + + curlpp::Cleanup cleaner; + curlpp::Easy request; + + if (!std::filesystem::exists(arg)) { + std::cerr << "[!] " << arg << " does not exist!\n"; + ok &= false; + continue; + } + auto fileSize = static_cast(std::filesystem::file_size(arg)); + + std::ifstream debFile(arg); + if (!debFile.good()) { + std::cerr << "[!] " << arg << " does not exist!\n"; + ok &= false; + continue; + } + + request.setOpt(new curlpp::options::Url(url)); + request.setOpt(new curlpp::options::Verbose(true)); + request.setOpt(new curlpp::options::UserPwd(userPass)); + request.setOpt(new curlpp::Options::ReadStream(&debFile)); + request.setOpt(new curlpp::Options::InfileSize(fileSize)); + request.setOpt(new curlpp::Options::Upload(true)); + request.perform(); + } catch (curlpp::LogicError &e) { + std::cout << e.what() << "\n"; + ok &= false; + } catch (curlpp::RuntimeError &e) { + std::cerr << e.what() << "\n"; + ok &= false; + } + + std::cout << "[+] " << std::filesystem::path(arg).filename() << ": OK\n"; + } + return false; }