Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/modules/complianceengine/src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ endif()
find_package(Lua REQUIRED)

set(PROCEDURES
procedures/EnsureAccountsWithoutShellAreLocked.cpp
procedures/AuditdRulesCheck.cpp
procedures/EnsureAccountsWithoutShellAreLocked.cpp
procedures/EnsureAllGroupsFromEtcPasswdExistInEtcGroup.cpp
procedures/EnsureApparmorProfiles.cpp
procedures/EnsureDconf.cpp
Expand All @@ -38,23 +38,23 @@ set(PROCEDURES
procedures/EnsureSshdOption.cpp
procedures/EnsureSysctl.cpp
procedures/EnsureSystemAccountsDoNotHaveValidShell.cpp
procedures/EnsureSystemdParameter.cpp
procedures/EnsureUserIsOnlyAccountWith.cpp
procedures/EnsureWirelessIsDisabled.cpp
procedures/EnsureXdmcp.cpp
procedures/ExecuteCommandGrep.cpp
procedures/FileRegexMatch.cpp
procedures/PackageInstalled.cpp
procedures/SCE.cpp
procedures/SystemdConfig.cpp
procedures/SystemdUnitState.cpp
procedures/TestingProcedures.cpp
procedures/UfwStatus.cpp
procedures/EnsureShadowContains.cpp
procedures/EnsureSshKeyPerms.cpp
)
set(SCHEMAS
procedures/EnsureAccountsWithoutShellAreLocked.schema.json
procedures/AuditdRulesCheck.schema.json
procedures/EnsureAccountsWithoutShellAreLocked.schema.json
procedures/EnsureAllGroupsFromEtcPasswdExistInEtcGroup.schema.json
procedures/EnsureApparmorProfiles.schema.json
procedures/EnsureDconf.schema.json
Expand All @@ -81,14 +81,14 @@ set(SCHEMAS
procedures/EnsureSshdOption.schema.json
procedures/EnsureSysctl.schema.json
procedures/EnsureSystemAccountsDoNotHaveValidShell.schema.json
procedures/EnsureSystemdParameter.schema.json
procedures/EnsureUserIsOnlyAccountWith.schema.json
procedures/EnsureWirelessIsDisabled.schema.json
procedures/EnsureXdmcp.schema.json
procedures/ExecuteCommandGrep.schema.json
procedures/FileRegexMatch.schema.json
procedures/PackageInstalled.schema.json
procedures/SCE.schema.json
procedures/SystemdConfig.schema.json
procedures/SystemdUnitState.schema.json
procedures/TestingProcedures.schema.json
procedures/UfwStatus.schema.json
Expand Down
22 changes: 19 additions & 3 deletions src/modules/complianceengine/src/lib/ProcedureMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ const char* Bindings<EnsureSshdOptionParams>::names[] = {"option", "value", "op"
// EnsureSysctl.h:21
const char* Bindings<EnsureSysctlParams>::names[] = {"sysctlName", "value"};

// EnsureSystemdParameter.h:25
const char* Bindings<SystemdParameterParams>::names[] = {"parameter", "valueRegex", "file", "dir"};

// EnsureSystemdParameter.h:66
const char* Bindings<EnsureSystemdParameterV4Params>::names[] = {"file", "section", "option", "expression", "value"};

// EnsureUserIsOnlyAccountWith.h:26
const char* Bindings<EnsureUserIsOnlyAccountWithParams>::names[] = {"username", "uid", "gid", "test_etcPasswdPath"};

Expand All @@ -77,9 +83,6 @@ const char* Bindings<PackageInstalledParams>::names[] = {"packageName", "minPack
// SCE.h:18
const char* Bindings<SCEParams>::names[] = {"scriptName", "ENVIRONMENT"};

// SystemdConfig.h:25
const char* Bindings<SystemdParameterParams>::names[] = {"parameter", "valueRegex", "file", "dir"};

// SystemdUnitState.h:28
const char* Bindings<SystemdUnitStateParams>::names[] = {"unitName", "ActiveState", "LoadState", "UnitFileState", "Unit"};

Expand Down Expand Up @@ -131,6 +134,7 @@ const ProcedureMap Evaluator::mProcedureMap = {
{"EnsureSshdOption", {MakeHandler(AuditEnsureSshdOption), nullptr}},
{"EnsureSysctl", {MakeHandler(AuditEnsureSysctl), nullptr}},
{"EnsureSystemAccountsDoNotHaveValidShell", {MakeHandler(AuditEnsureSystemAccountsDoNotHaveValidShell), nullptr}},
{"EnsureSystemdParameterV4", {MakeHandler(AuditEnsureSystemdParameterV4), nullptr}},
{"EnsureUfwOpenPorts", {MakeHandler(AuditEnsureUfwOpenPorts), nullptr}},
{"EnsureUserIsOnlyAccountWith", {MakeHandler(AuditEnsureUserIsOnlyAccountWith), nullptr}},
{"EnsureWirelessIsDisabled", {MakeHandler(AuditEnsureWirelessIsDisabled), nullptr}},
Expand Down Expand Up @@ -246,6 +250,18 @@ string to_string(const ComplianceEngine::EnsureSshdOptionMode value) noexcept(fa
return it->second;
}

string to_string(const ComplianceEngine::SystemdParameterExpression value) noexcept(false)
{
const auto& map = ComplianceEngine::MapEnum<ComplianceEngine::SystemdParameterExpression>();
static const auto revmap = ComplianceEngine::RevertMap(map);
const auto it = revmap.find(value);
if (revmap.end() == it)
{
throw std::out_of_range("Invalid enum value");
}
return it->second;
}

string to_string(const ComplianceEngine::RegexType value) noexcept(false)
{
const auto& map = ComplianceEngine::MapEnum<ComplianceEngine::RegexType>();
Expand Down
49 changes: 38 additions & 11 deletions src/modules/complianceengine/src/lib/ProcedureMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
#include <EnsureSshdOption.h>
#include <EnsureSysctl.h>
#include <EnsureSystemAccountsDoNotHaveValidShell.h>
#include <EnsureSystemdParameter.h>
#include <EnsureUserIsOnlyAccountWith.h>
#include <EnsureWirelessIsDisabled.h>
#include <EnsureXdmcp.h>
#include <ExecuteCommandGrep.h>
#include <FileRegexMatch.h>
#include <PackageInstalled.h>
#include <SCE.h>
#include <SystemdConfig.h>
#include <SystemdUnitState.h>
#include <TestingProcedures.h>
#include <UfwStatus.h>
Expand Down Expand Up @@ -163,6 +163,20 @@ inline const std::map<std::string, EnsureSshdOptionMode>& MapEnum<EnsureSshdOpti
return map;
}

// Maps the SystemdParameterExpression enum labels to the enum values.
template <>
inline const std::map<std::string, SystemdParameterExpression>& MapEnum<SystemdParameterExpression>()
{
static const std::map<std::string, SystemdParameterExpression> map = {
{"lt", SystemdParameterExpression::LessThan},
{"le", SystemdParameterExpression::LessOrEqual},
{"gt", SystemdParameterExpression::GreaterThan},
{"ge", SystemdParameterExpression::GreaterOrEqual},
{"eq", SystemdParameterExpression::Equal},
};
return map;
}

// Maps the RegexType enum labels to the enum values.
template <>
inline const std::map<std::string, RegexType>& MapEnum<RegexType>()
Expand Down Expand Up @@ -404,6 +418,26 @@ struct Bindings<EnsureSysctlParams>
static constexpr auto members = std::make_tuple(&T::sysctlName, &T::value);
};

// Defines the bindings for the SystemdParameterParams structure.
template <>
struct Bindings<SystemdParameterParams>
{
using T = SystemdParameterParams;
static constexpr size_t size = 4;
static const char* names[];
static constexpr auto members = std::make_tuple(&T::parameter, &T::valueRegex, &T::file, &T::dir);
};

// Defines the bindings for the EnsureSystemdParameterV4Params structure.
template <>
struct Bindings<EnsureSystemdParameterV4Params>
{
using T = EnsureSystemdParameterV4Params;
static constexpr size_t size = 5;
static const char* names[];
static constexpr auto members = std::make_tuple(&T::file, &T::section, &T::option, &T::expression, &T::value);
};

// Defines the bindings for the EnsureUserIsOnlyAccountWithParams structure.
template <>
struct Bindings<EnsureUserIsOnlyAccountWithParams>
Expand Down Expand Up @@ -464,16 +498,6 @@ struct Bindings<SCEParams>
static constexpr auto members = std::make_tuple(&T::scriptName, &T::ENVIRONMENT);
};

// Defines the bindings for the SystemdParameterParams structure.
template <>
struct Bindings<SystemdParameterParams>
{
using T = SystemdParameterParams;
static constexpr size_t size = 4;
static const char* names[];
static constexpr auto members = std::make_tuple(&T::parameter, &T::valueRegex, &T::file, &T::dir);
};

// Defines the bindings for the SystemdUnitStateParams structure.
template <>
struct Bindings<SystemdUnitStateParams>
Expand Down Expand Up @@ -552,6 +576,9 @@ string to_string(ComplianceEngine::EnsureSshdOptionOperation value) noexcept(fal
// Returns a string representation of the EnsureSshdOptionMode enum value.
string to_string(ComplianceEngine::EnsureSshdOptionMode value) noexcept(false); // NOLINT(*-identifier-naming)

// Returns a string representation of the SystemdParameterExpression enum value.
string to_string(ComplianceEngine::SystemdParameterExpression value) noexcept(false); // NOLINT(*-identifier-naming)

// Returns a string representation of the RegexType enum value.
string to_string(ComplianceEngine::RegexType value) noexcept(false); // NOLINT(*-identifier-naming)

Expand Down
36 changes: 34 additions & 2 deletions src/modules/complianceengine/src/lib/SystemdCatConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,46 @@
// Licensed under the MIT License.

#include <SystemdCatConfig.h>
#include <Telemetry.h>

namespace ComplianceEngine
{
Result<std::string> SystemdCatConfig(const std::string& filename, ContextInterface& context)
namespace
{
auto result = context.ExecuteCommand("systemd-analyze cat-config " + filename);
Result<std::string> DetermineCommandPath(const std::string& command, const ContextInterface& context)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it more generic by passing const std::vectorstd::string paths, etc ... ?

{
auto result = context.ExecuteCommand("readlink -e /bin/" + command);
if (result.HasValue())
{
OsConfigLogInfo(context.GetLogHandle(), "'%s' path is: %s", command.c_str(), result->c_str());
return std::move(result.Value());
}

result = context.ExecuteCommand("readlink -e /usr/bin/" + command);
if (result.HasValue())
{
OsConfigLogInfo(context.GetLogHandle(), "'%s' path is: %s", command.c_str(), result->c_str());
return std::move(result.Value());
}

OsConfigLogError(context.GetLogHandle(), "Failed to determine systemd-analyze command path");
return result.Error();
}
} // anonymous namespace

Result<std::string> SystemdCatConfig(const std::string& filename, const ContextInterface& context)
{
static const auto commandPath = DetermineCommandPath("systemd-analyze", context);
if (!commandPath.HasValue())
{
return commandPath.Error();
}

auto result = context.ExecuteCommand(commandPath.Value() + " cat-config " + filename);
if (!result.HasValue())
{
OsConfigLogError(context.GetLogHandle(), "Failed to execute systemd-analyze command: %s", result.Error().message.c_str());
OSConfigTelemetryStatusTrace("ExecuteCommand", result.Error().code);
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/complianceengine/src/lib/SystemdCatConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace ComplianceEngine
{
Result<std::string> SystemdCatConfig(const std::string& filename, ContextInterface& context);
Result<std::string> SystemdCatConfig(const std::string& filename, const ContextInterface& context);
} // namespace ComplianceEngine

#endif // COMPLIANCEENGINE_SYSTEMD_CAT_CONFIG_H
6 changes: 6 additions & 0 deletions src/modules/complianceengine/src/lib/payload.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@
{
"$ref": "procedures/EnsureSystemAccountsDoNotHaveValidShell.schema.json#/definitions/audit"
},
{
"$ref": "procedures/EnsureSystemdParameter.schema.json#/definitions/audit"
},
{
"$ref": "procedures/EnsureUserIsOnlyAccountWith.schema.json#/definitions/audit"
},
Expand Down Expand Up @@ -369,6 +372,9 @@
{
"$ref": "procedures/EnsureSystemAccountsDoNotHaveValidShell.schema.json#/definitions/remediation"
},
{
"$ref": "procedures/EnsureSystemdParameter.schema.json#/definitions/remediation"
},
{
"$ref": "procedures/EnsureUserIsOnlyAccountWith.schema.json#/definitions/remediation"
},
Expand Down
Loading
Loading