Skip to content

Commit 6036e07

Browse files
authored
Merge pull request #12546 from mrkanon/ansible-mount_option_home
Add ansible remediation to mount_option_home template
2 parents adc8e35 + 434ad07 commit 6036e07

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# platform = multi_platform_all
2+
# reboot = false
3+
# strategy = configure
4+
# complexity = low
5+
# disruption = high
6+
7+
- name: "{{{ rule_title }}} - Initialize variables"
8+
ansible.builtin.set_fact:
9+
non_allowed_partitions: ["/", "/lib", "/opt", "/usr", "/bin", "/sbin", "/boot", "/dev", "/proc"]
10+
home_directories: []
11+
allowed_mount_point: []
12+
fstab_mount_point_info: []
13+
14+
- name: "{{{ rule_title }}} - Get home directories from passwd"
15+
ansible.builtin.getent:
16+
database: passwd
17+
18+
- name: "{{{ rule_title }}} - Filter home directories based on UID range"
19+
ansible.builtin.set_fact:
20+
home_directories: "{{ home_directories + [item.data[4]] }}"
21+
when:
22+
- item.data[4] is defined
23+
- item.data[2]|int >= {{{ uid_min }}}
24+
- item.data[2]|int != {{{ nobody_uid }}}
25+
- item.data[4] not in non_allowed_partitions
26+
with_items: "{{ ansible_facts.getent_passwd | dict2items(key_name='user', value_name='data')}}"
27+
28+
- name: "{{{ rule_title }}} - Gather mount points"
29+
ansible.builtin.setup:
30+
filter: ansible_mounts
31+
32+
- name: "{{{ rule_title }}} - Ensure mount options for home directories"
33+
block:
34+
35+
- name: " {{{ rule_title }}} - Obtain mount point using df and shell"
36+
ansible.builtin.shell: |
37+
df {{ item }} | awk '/^\/dev/ {print $6}'
38+
register: df_output
39+
with_items: "{{ home_directories }}"
40+
41+
- name: "{{{ rule_title }}} - Set mount point for each home directory"
42+
ansible.builtin.set_fact:
43+
allowed_mount_point: "{{ allowed_mount_point + [item.stdout_lines[0]] }}"
44+
with_items: "{{ df_output.results }}"
45+
when:
46+
- item.stdout_lines is defined
47+
- item.stdout_lines | length > 0
48+
- item.stdout_lines[0] != ""
49+
50+
- name: "{{{ rule_title }}} - Obtain full mount information for allowed mount point"
51+
ansible.builtin.set_fact:
52+
fstab_mount_point_info: "{{ fstab_mount_point_info + [ ansible_mounts | selectattr('mount', 'equalto', item) | first ]}}"
53+
with_items: "{{ allowed_mount_point }}"
54+
when: allowed_mount_point is defined
55+
56+
- name: "{{{ rule_title }}} - Ensure mount option {{{ MOUNTOPTION }}} is in fstab for allowed mount point"
57+
ansible.builtin.mount:
58+
path: "{{ item.mount }}"
59+
src: "{{ item.device }}"
60+
opts: "{{ item.options }},{{{ MOUNTOPTION }}}"
61+
state: mounted
62+
fstype: "{{ item.fstype }}"
63+
with_items: "{{ fstab_mount_point_info }}"
64+
when:
65+
- allowed_mount_point is defined
66+
- item.mount not in non_allowed_partitions
67+
- "'{{{ MOUNTOPTION }}}' not in item.options"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
supported_languages:
22
- bash
33
- oval
4+
- ansible
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# platform = multi_platform_all
3+
4+
. $SHARED/partition.sh
5+
6+
mkdir -p /srv/home
7+
awk -F':' '{if ($3>={{{ uid_min }}} && $3!= {{{ nobody_uid }}}) print $1}' /etc/passwd \
8+
| xargs -I{} userdel -r {}
9+
10+
umount /srv || true # no problem if not mounted
11+
12+
clean_up_partition /srv
13+
14+
create_partition
15+
16+
{{% if MOUNTOPTION != "nodev" %}}
17+
make_fstab_given_partition_line /srv ext2 nodev
18+
{{% else %}}
19+
make_fstab_given_partition_line /srv ext2 noexec
20+
{{% endif %}}
21+
22+
mount_partition /srv
23+
24+
mkdir -p /srv/home
25+
useradd -m -d /srv/home/testUser1 testUser1
26+
27+
useradd -m -d /home/testUser2 testUser2

0 commit comments

Comments
 (0)