Skip to content

Commit 5254320

Browse files
authored
Merge pull request #2144 from jan-cerny/firewall_ks
Add firewall command to Kickstart remediation
2 parents 3e5b01e + d4ea0eb commit 5254320

4 files changed

Lines changed: 75 additions & 18 deletions

File tree

docs/manual/manual.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,8 @@ Supported commands:
11551155
* `logvol path size` - adds `logvol` entry to the commands section of the kickstart that will mount a partition of the given `size` in MB to the given `path` as a mount point
11561156
* `bootloader option` or `bootloader option=value` - adds `option` or `option=value` to the list in the `--append=` option in the `bootloader` command in commands section in the kickstart
11571157
* `kdump disable` - this will disable K-Dump by adding the `com_redhat_kdump` Addon section to the kickstart with a `--disable` option
1158+
* `firewall enable service_name` - adds `service_name` to list in the `--service=` option in the `firewall` command in commands section in the kickstart
1159+
* `firewall disable service_name` - adds `service_name` to list in the `--remove-service=` option in the `firewall` command in commands section in the kickstart
11581160

11591161
For example, to generate a kickstart for RHEL 9 STIG profile, run:
11601162

src/XCCDF_POLICY/xccdf_policy_remediate.c

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct kickstart_commands {
5858
struct oscap_list *post;
5959
struct oscap_list *logvol;
6060
struct oscap_list *bootloader;
61+
struct oscap_list *firewall_enable;
62+
struct oscap_list *firewall_disable;
6163
bool enable_kdump;
6264
};
6365

@@ -927,6 +929,9 @@ static int _parse_line(const char *line, struct kickstart_commands *cmds)
927929
KS_LOGVOL_SIZE,
928930
KS_BOOTLOADER,
929931
KS_KDUMP,
932+
KS_FIREWALL,
933+
KS_FIREWALL_ENABLE,
934+
KS_FIREWALL_DISABLE,
930935
KS_ERROR
931936
};
932937
int state = KS_START;
@@ -947,6 +952,8 @@ static int _parse_line(const char *line, struct kickstart_commands *cmds)
947952
state = KS_BOOTLOADER;
948953
} else if (!strcmp(word, "kdump")) {
949954
state = KS_KDUMP;
955+
} else if (!strcmp(word, "firewall")) {
956+
state = KS_FIREWALL;
950957
} else {
951958
ret = 1;
952959
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported command keyword '%s' in command: '%s'", word, line);
@@ -1010,6 +1017,23 @@ static int _parse_line(const char *line, struct kickstart_commands *cmds)
10101017
goto cleanup;
10111018
}
10121019
break;
1020+
case KS_FIREWALL:
1021+
if (!strcmp(word, "enable")) {
1022+
state = KS_FIREWALL_ENABLE;
1023+
} else if (!strcmp(word, "disable")) {
1024+
state = KS_FIREWALL_DISABLE;
1025+
} else {
1026+
ret = 1;
1027+
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'firewall' command keyword '%s' in command: '%s'", word, line);
1028+
goto cleanup;
1029+
}
1030+
break;
1031+
case KS_FIREWALL_ENABLE:
1032+
oscap_list_add(cmds->firewall_enable, strdup(word));
1033+
break;
1034+
case KS_FIREWALL_DISABLE:
1035+
oscap_list_add(cmds->firewall_disable, strdup(word));
1036+
break;
10131037
case KS_ERROR:
10141038
ret = 1;
10151039
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unexpected string '%s' in command: '%s'", word, line);
@@ -1502,38 +1526,52 @@ static int _xccdf_policy_generate_fix_other(struct oscap_list *rules_to_fix, str
15021526
return ret;
15031527
}
15041528

1529+
static void _write_it_comma_list(struct oscap_iterator *it, const char *option, int output_fd)
1530+
{
1531+
if (!oscap_iterator_has_more(it))
1532+
return;
1533+
_write_text_to_fd(output_fd, " ");
1534+
_write_text_to_fd(output_fd, option);
1535+
while (oscap_iterator_has_more(it)) {
1536+
char *item = (char *) oscap_iterator_next(it);
1537+
_write_text_to_fd(output_fd, item);
1538+
if (oscap_iterator_has_more(it))
1539+
_write_text_to_fd(output_fd, ",");
1540+
}
1541+
}
1542+
15051543
static int _generate_kickstart_services(struct kickstart_commands *cmds, int output_fd)
15061544
{
15071545
struct oscap_iterator *service_disable_it = oscap_iterator_new(cmds->service_disable);
15081546
struct oscap_iterator *service_enable_it = oscap_iterator_new(cmds->service_enable);
15091547
if (oscap_iterator_has_more(service_disable_it) || oscap_iterator_has_more(service_enable_it)) {
15101548
_write_text_to_fd(output_fd, "# Disable and enable systemd services (required for security compliance)\n");
15111549
_write_text_to_fd(output_fd, "services");
1512-
if (oscap_iterator_has_more(service_disable_it)) {
1513-
_write_text_to_fd(output_fd, " --disabled=");
1514-
while (oscap_iterator_has_more(service_disable_it)) {
1515-
char *command = (char *) oscap_iterator_next(service_disable_it);
1516-
_write_text_to_fd(output_fd, command);
1517-
if (oscap_iterator_has_more(service_disable_it))
1518-
_write_text_to_fd(output_fd, ",");
1519-
}
1520-
}
1521-
if (oscap_iterator_has_more(service_enable_it)) {
1522-
_write_text_to_fd(output_fd, " --enabled=");
1523-
while (oscap_iterator_has_more(service_enable_it)) {
1524-
char *command = (char *) oscap_iterator_next(service_enable_it);
1525-
_write_text_to_fd(output_fd, command);
1526-
if (oscap_iterator_has_more(service_enable_it))
1527-
_write_text_to_fd(output_fd, ",");
1528-
}
1529-
}
1550+
_write_it_comma_list(service_disable_it, "--disabled=", output_fd);
1551+
_write_it_comma_list(service_enable_it, "--enabled=", output_fd);
15301552
_write_text_to_fd(output_fd, "\n\n");
15311553
}
15321554
oscap_iterator_free(service_disable_it);
15331555
oscap_iterator_free(service_enable_it);
15341556
return 0;
15351557
}
15361558

1559+
static int _generate_kickstart_firewall(struct kickstart_commands *cmds, int output_fd)
1560+
{
1561+
struct oscap_iterator *disable_it = oscap_iterator_new(cmds->firewall_disable);
1562+
struct oscap_iterator *enable_it = oscap_iterator_new(cmds->firewall_enable);
1563+
if (oscap_iterator_has_more(disable_it) || oscap_iterator_has_more(enable_it)) {
1564+
_write_text_to_fd(output_fd, "# Disable and enable services in firewall (required for security compliance)\n");
1565+
_write_text_to_fd(output_fd, "firewall");
1566+
_write_it_comma_list(disable_it, "--remove-service=", output_fd);
1567+
_write_it_comma_list(enable_it, "--service=", output_fd);
1568+
_write_text_to_fd(output_fd, "\n\n");
1569+
}
1570+
oscap_iterator_free(disable_it);
1571+
oscap_iterator_free(enable_it);
1572+
return 0;
1573+
}
1574+
15371575
static int _generate_kickstart_packages(struct kickstart_commands *cmds, int output_fd)
15381576
{
15391577
_write_text_to_fd(output_fd, "# Packages selection (required for security compliance)\n");
@@ -1723,6 +1761,8 @@ static int _xccdf_policy_generate_fix_kickstart(struct oscap_list *rules_to_fix,
17231761
.post = oscap_list_new(),
17241762
.logvol = oscap_list_new(),
17251763
.bootloader = oscap_list_new(),
1764+
.firewall_enable = oscap_list_new(),
1765+
.firewall_disable = oscap_list_new(),
17261766
.enable_kdump = true,
17271767
};
17281768

@@ -1756,6 +1796,8 @@ static int _xccdf_policy_generate_fix_kickstart(struct oscap_list *rules_to_fix,
17561796

17571797
_generate_kickstart_kdump(&cmds, output_fd);
17581798

1799+
_generate_kickstart_firewall(&cmds, output_fd);
1800+
17591801
_generate_kickstart_services(&cmds, output_fd);
17601802

17611803
_generate_kickstart_packages(&cmds, output_fd);
@@ -1775,6 +1817,8 @@ static int _xccdf_policy_generate_fix_kickstart(struct oscap_list *rules_to_fix,
17751817
oscap_list_free(cmds.post, free);
17761818
oscap_list_free(cmds.logvol, logvol_cmd_free);
17771819
oscap_list_free(cmds.bootloader, free);
1820+
oscap_list_free(cmds.firewall_enable, free);
1821+
oscap_list_free(cmds.firewall_disable, free);
17781822
return ret;
17791823
}
17801824

tests/API/XCCDF/unittests/test_remediation_kickstart.ds.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<select idref="xccdf_org.openscap.www_rule_7" selected="true"/>
6363
<select idref="xccdf_org.openscap.www_rule_8" selected="true"/>
6464
<select idref="xccdf_org.openscap.www_rule_9" selected="true"/>
65+
<select idref="xccdf_org.openscap.www_rule_10" selected="true"/>
6566
</Profile>
6667
<Rule selected="false" id="xccdf_org.openscap.www_rule_1">
6768
<title>Rule 1: Enable Audit Service</title>
@@ -137,6 +138,13 @@
137138
kdump disable
138139
</fix>
139140
</Rule>
141+
<Rule selected="false" id="xccdf_org.openscap.www_rule_10">
142+
<title>Rule 10: Firewall</title>
143+
<fix system="urn:xccdf:fix:script:kickstart">
144+
firewall enable sshd
145+
firewall disable httpd
146+
</fix>
147+
</Rule>
140148
</Benchmark>
141149
</ds:component>
142150
</ds:data-stream-collection>

tests/API/XCCDF/unittests/test_remediation_kickstart_expected.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ bootloader --append="quick audit=1"
4949
%addon com_redhat_kdump --disable
5050
%end
5151

52+
# Disable and enable services in firewall (required for security compliance)
53+
firewall --remove-service=httpd --service=sshd
54+
5255
# Disable and enable systemd services (required for security compliance)
5356
services --disabled=telnet,httpd --enabled=auditd,rsyslog,sshd
5457

0 commit comments

Comments
 (0)