|
9 | 9 | changed_when: false |
10 | 10 | always_run: true |
11 | 11 |
|
| 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 | + |
12 | 26 | - name: check the ssh_password policy state |
13 | 27 | shell: semodule -l | grep "ssh_password" | awk '{print $3}' |
14 | 28 | register: selinux_policy_state |
|
25 | 39 | template: src='openssh.conf.j2' dest='/etc/ssh/ssh_config' mode=0644 owner=root group=root |
26 | 40 | when: ssh_client_hardening |
27 | 41 |
|
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 |
31 | 48 |
|
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. |
33 | 50 | # See this issue for more info: https://github.com/hardening-io/ansible-ssh-hardening/issues/23 |
34 | 51 |
|
| 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 | + |
35 | 56 | - name: Distributing custom selinux policies |
36 | 57 | 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 |
39 | 59 |
|
40 | 60 | - name: check and compile policy |
41 | 61 | 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 |
43 | 63 |
|
44 | 64 | - name: create selinux policy module package |
45 | 65 | 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 |
47 | 67 |
|
48 | 68 | - name: install selinux policy |
49 | 69 | 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 |
51 | 71 |
|
| 72 | +# The following tasks only get executed when selinux is in state enforcing, UsePam is "yes" and the ssh_password module is installed. |
52 | 73 | - 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) |
53 | 74 | 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