Skip to content
This repository was archived by the owner on Dec 26, 2020. It is now read-only.

Commit 61f00ec

Browse files
authored
Merge pull request #79 from dev-sec/check_selinux_module
install selinux dependencies, check for already installed semodule
2 parents 1f63b35 + a2c4656 commit 61f00ec

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

tasks/main.yml

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
changed_when: false
1010
always_run: true
1111

12+
- name: install selinux dependencies when selinux is installed on RHEL or Oracle Linux
13+
yum: name="{{item}}" state=installed
14+
with_items:
15+
- policycoreutils-python
16+
- checkpolicy
17+
when: sestatus.rc == 0 and (ansible_os_family == 'RedHat' or ansible_os_family == 'Oracle Linux')
18+
19+
- name: install selinux dependencies when selinux is installed on Debian or Ubuntu
20+
apt: name="{{item}}" state=installed
21+
with_items:
22+
- policycoreutils
23+
- checkpolicy
24+
when: sestatus.rc == 0 and (ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu')
25+
1226
- name: check the ssh_password policy state
1327
shell: semodule -l | grep "ssh_password" | awk '{print $3}'
1428
register: selinux_policy_state
@@ -25,30 +39,37 @@
2539
template: src='openssh.conf.j2' dest='/etc/ssh/ssh_config' mode=0644 owner=root group=root
2640
when: ssh_client_hardening
2741

28-
- name: Create selinux custom policy drop folder
29-
file: path={{ ssh_custom_selinux_dir }} state=directory owner=root group=root mode=0750
30-
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled'
42+
- name: check if ssh_password module is already installed
43+
shell: "semodule -l| grep ssh_password"
44+
register: ssh_password_module
45+
failed_when: false
46+
changed_when: false
47+
always_run: true
3148

32-
# The following tasks only get executed when selinux is in state enforcing and UsePam is "no".
49+
# The following tasks only get executed when selinux is in state enforcing, UsePam is "no" and the ssh_password module is installed.
3350
# See this issue for more info: https://github.com/hardening-io/ansible-ssh-hardening/issues/23
3451

52+
- name: Create selinux custom policy drop folder
53+
file: path={{ ssh_custom_selinux_dir }} state=directory owner=root group=root mode=0750
54+
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0
55+
3556
- name: Distributing custom selinux policies
3657
copy: src='ssh_password' dest='{{ ssh_custom_selinux_dir }}'
37-
register: custom_policies_output
38-
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled'
58+
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0
3959

4060
- name: check and compile policy
4161
shell: checkmodule -M -m -o {{ ssh_custom_selinux_dir }}/ssh_password.mod {{ ssh_custom_selinux_dir }}/ssh_password
42-
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled'
62+
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0
4363

4464
- name: create selinux policy module package
4565
shell: semodule_package -o {{ ssh_custom_selinux_dir }}/ssh_password.pp -m {{ ssh_custom_selinux_dir }}/ssh_password.mod
46-
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled'
66+
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0
4767

4868
- name: install selinux policy
4969
shell: semodule -i {{ ssh_custom_selinux_dir }}/ssh_password.pp
50-
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled'
70+
when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0
5171

72+
# The following tasks only get executed when selinux is in state enforcing, UsePam is "yes" and the ssh_password module is installed.
5273
- name: remove selinux-policy when Pam is used, because Allowing sshd to read the shadow file directly is considered a potential security risk (http://danwalsh.livejournal.com/12333.html)
5374
shell: semodule -r ssh_password
54-
when: sestatus.rc == 0 and ssh_use_pam
75+
when: sestatus.rc == 0 and ssh_use_pam and ssh_password_module.stdout.find('ssh_password') == 0

0 commit comments

Comments
 (0)