FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
bwrap_apparmor.cpp
Go to the documentation of this file.
1
8
9#include <filesystem>
10
11#include "../std/expected.hpp"
12#include "../lib/log.hpp"
13#include "../lib/subprocess.hpp"
14#include "../lib/env.hpp"
15#include "../macro.hpp"
16
17constexpr std::string_view const profile_bwrap =
18R"(abi <abi/4.0>,
19include <tunables/global>
20profile bwrap /opt/flatimage/bwrap flags=(unconfined) {
21 userns,
22}
23)";
24
25namespace fs = std::filesystem;
26
34int main(int argc, char const* argv[])
35{
36 auto __expected_fn = [](auto&&){ return EXIT_FAILURE; };
37 // Check arguments
38 return_if(argc != 3, EXIT_FAILURE, "E::Incorrect # of arguments for bwrap-apparmor");
39 // Set log file location
40 fs::path path_file_log = std::string{argv[1]};
41 ns_log::set_sink_file(path_file_log);
42 // Find apparmor_parser
43 fs::path path_file_apparmor_parser = Pop(ns_env::search_path("apparmor_parser"));
44 // Define paths
45 fs::path path_file_bwrap_src{argv[2]};
46 fs::path path_dir_bwrap{"/opt/flatimage"};
47 fs::path path_file_bwrap_dst{path_dir_bwrap / "bwrap"};
48 fs::path path_file_profile{"/etc/apparmor.d/flatimage"};
49 // Try to create /opt/bwrap directory
50 Try(fs::create_directories(path_dir_bwrap));
51 // Try copy bwrap to /opt/bwrap
52 Try(fs::copy_file(path_file_bwrap_src, path_file_bwrap_dst, fs::copy_options::overwrite_existing));
53 // Try to set permissions for bwrap binary ( chmod 755 )
54 using enum fs::perms;
55 auto perms = owner_all | group_read | group_exec | others_read | others_exec;
56 Catch(fs::permissions(path_file_bwrap_dst, perms, fs::perm_options::replace))
57 .discard("C::Failed to set permissions to '{}'", path_file_bwrap_dst);
58 // Try to create profile
59 std::ofstream file_profile(path_file_profile);
60 return_if(not file_profile.is_open(), EXIT_FAILURE, "E::Could not open profile file");
61 file_profile << profile_bwrap;
62 file_profile.close();
63 // Reload profile
64 return Pop(ns_subprocess::Subprocess(path_file_apparmor_parser)
65 .with_args("-r", path_file_profile)
66 .spawn()->wait()
67 );
68} // main
Enhanced error handling framework built on std::expected.
constexpr auto __expected_fn
Lambda helper for Pop macro error returns.
Definition expected.hpp:147
A library for manipulating environment variables.
A library for file logging.
Simplified macros for common control flow patterns with optional logging.
Value< fs::path > search_path(std::string const &query)
Search the directories in the PATH variable for the given input file name.
Definition env.hpp:150
void set_sink_file(fs::path const &path_file_sink)
Sets the sink file of the logger.
Definition log.hpp:359
int main()
Entry point for the portal daemon.
A library to spawn sub-processes in linux.