REC-418 Disable NFS services
[ta/infra-ansible.git] / roles / ops-hardening / tasks / main.yaml
1 ---
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 #
18 # Linux password hardening
19 #
20
21 - name: "Set Password Strength Minimum Digit Characters."
22   lineinfile:
23     path: /etc/security/pwquality.conf
24     regexp: '^[#\s]*dcredit'
25     line: 'dcredit = -1'
26
27 - name: "Set Password Minimum Length."
28   lineinfile:
29     path: /etc/security/pwquality.conf
30     regexp: '^[#\s]*minlen'
31     line: 'minlen = 8'
32
33 - name: "Set Password Strength Minimum Uppercase Characters."
34   lineinfile:
35     path: /etc/security/pwquality.conf
36     regexp: '^[#\s]*ucredit'
37     line: 'ucredit = -1'
38
39 - name: "Set Password Strength Minimum Special Characters."
40   lineinfile:
41     path: /etc/security/pwquality.conf
42     regexp: '^[#\s]*ocredit'
43     line: 'ocredit = -1'
44
45 - name: "Set Password Strength Minimum Lowercase Characters."
46   lineinfile:
47     path: /etc/security/pwquality.conf
48     regexp: '^[#\s]*lcredit'
49     line: 'lcredit = -1'
50
51 - name: "Set Password Strength Minimum Different Categories."
52   lineinfile:
53     path: /etc/security/pwquality.conf
54     regexp: '^[#\s]*minclass'
55     line: 'minclass = 3'
56
57 - name: "Set Password Minimum Length in login.defs"
58   lineinfile:
59     path: /etc/login.defs
60     regexp: '^PASS_MIN_LEN[\s]*[0-9]*$'
61     line: 'PASS_MIN_LEN   8'
62
63 - name: "Set Password Minimum Age"
64   lineinfile:
65     path: /etc/login.defs
66     regexp: '^PASS_MIN_DAYS[\s]*[0-9]*$'
67     line: 'PASS_MIN_DAYS   0'
68
69 #
70 # YUM config
71 #
72
73 - name: "Ensure YUM Removes Previous Package Versions"
74   lineinfile:
75     path: /etc/yum.conf
76     insertafter: '^[#\s]*\[main\]'
77     line: 'clean_requirements_on_remove = 1'
78
79 - name: "Ensure gpgcheck Enabled for Local Packages"
80   lineinfile:
81     path: /etc/yum.conf
82     insertafter: '^[#\s]*\[main\]'
83     line: 'localpkg_gpgcheck = 1'
84
85 #
86 # Setting Ctrl-Alt-Del action
87 #
88
89 - name: "Disable Ctrl-Alt-Del Burst Action"
90   lineinfile:
91     path: /etc/systemd/system.conf
92     insertafter: '^[#\s]*CtrlAltDelBurstAction'
93     line: 'CtrlAltDelBurstAction=none'
94
95 - name: "Disable Ctrl-Alt-Del Reboot Activation"
96   command: systemctl mask ctrl-alt-del.target
97
98 #
99 # Configure kernel modules
100 #
101
102 - name: "kernel module setting"
103   lineinfile:
104     create=yes
105     dest="/etc/modprobe.d/{{item}}.conf"
106     regexp="{{item}}"
107     line="install {{item}} /bin/true"
108   with_items:
109     - bluetooth
110     - dccp
111     - squashfs
112     - hfsplus
113     - hfs
114     - jffs2
115     - freevxfs
116     - cramfs
117     - usb-storage
118     - udf
119     - nfsd
120
121 #
122 # Disable interactive boot
123 #
124
125 - name:  Verify that Interactive Boot is Disabled GRUB_CMDLINE_LINUX Setting
126   lineinfile:
127     path: /etc/default/grub
128     backrefs: yes
129     regexp: '^GRUB_CMDLINE_LINUX=(.*)systemd\.confirm_spawn=(1|yes|true|on)\s*(.*)$'
130     line: 'GRUB_CMDLINE_LINUX=\1\3'
131
132 - name:  Verify that Interactive Boot is Disabled GRUB_CMDLINE_LINUX_DEFAULT Setting
133   lineinfile:
134     path: /etc/default/grub
135     backrefs: yes
136     regexp: '^GRUB_CMDLINE_LINUX_DEFAULT=(.*)systemd\.confirm_spawn=(1|yes|true|on)\s*(.*)$'
137     line: 'GRUB_CMDLINE_LINUX_DEFAULT=\1\3'
138
139 #
140 # Set file permissions
141 #
142
143 - name: "Set set the 600 file permissions"
144   file:
145     path: "{{item}}"
146     state: touch
147     mode: 600
148   with_items:
149     - /boot/grub2/grub.cfg
150     - /var/log/boot.log
151     - /var/log/cron
152
153 #
154 # Disable direct root login
155 #
156
157 - name: "Direct root Logins Not Allowed"
158   shell: echo > /etc/securetty
159
160 - name: Change 'root' shell to nologin
161   user:
162     name: root
163     shell: /sbin/nologin
164
165 - name: Lock 'root' password
166   user:
167     name: root
168     password: '!!'
169
170 #
171 # Configure IPv6
172 #
173
174 - name: Disable ipv6 support if the ipv6 is not needed
175   when: ansible_default_ipv6|length == 0
176   sysctl:
177     name: net.ipv6.conf.all.disable_ipv6
178     value: 1
179     state: present
180     reload: yes
181
182 - name: Disable Support for udp6
183   when: ansible_default_ipv6|length == 0
184   lineinfile:
185     path: /etc/netconfig
186     state: absent
187     regexp: '^udp6.*'
188
189 - name: Disable Support for tcp6
190   when: ansible_default_ipv6|length == 0
191   lineinfile:
192     path: /etc/netconfig
193     state: absent
194     regexp: '^tcp6.*'
195
196 - name: Disable automatic ipv6 configuration
197   when: ansible_default_ipv6|length > 0
198   sysctl:
199     name: "{{ item.name }}"
200     value: "{{ item.value }}"
201     state: present
202     reload: yes
203   with_items:
204     - { name: 'net.ipv6.conf.all.accept_source_route', value: 0 }
205     - { name: 'net.ipv6.conf.all.accept_ra', value: 0 }
206     - { name: 'net.ipv6.conf.default.accept_ra', value: 0 }
207     - { name: 'net.ipv6.conf.all.accept_redirects', value: 0 }
208     - { name: 'net.ipv6.conf.default.accept_redirects', value: 0 }
209     - { name: 'net.ipv6.conf.default.accept_source_route', value: 0 }
210     - { name: 'net.ipv6.conf.all.forwarding', value: 0 }
211
212 #
213 # Configure kernel parameters
214 #
215
216 - name: Configure the kernel parameters
217   sysctl:
218     name: "{{ item.name }}"
219     value: "{{ item.value }}"
220     state: present
221     reload: yes
222   with_items:
223     - { name: 'net.ipv4.conf.default.send_redirects', value: 0 }
224     - { name: 'net.ipv4.conf.all.send_redirects', value: 0 }
225     - { name: 'net.ipv4.ip_forward', value: 0 }
226     - { name: 'net.ipv4.conf.all.accept_redirects', value: 0 }
227     - { name: 'net.ipv4.conf.all.secure_redirects', value: 0 }
228     - { name: 'net.ipv4.conf.all.log_martians', value: 1 }
229     - { name: 'net.ipv4.conf.default.log_martians', value: 1 }
230     - { name: 'net.ipv4.conf.default.accept_redirects', value: 0 }
231     - { name: 'net.ipv4.conf.default.secure_redirects', value: 0 }
232     - { name: 'net.ipv4.icmp_echo_ignore_broadcasts', value: 1 }
233     - { name: 'net.ipv4.icmp_ignore_bogus_error_responses', value: 1 }
234     - { name: 'net.ipv4.tcp_syncookies', value: 1 }
235     - { name: 'fs.suid_dumpable', value: 0 }
236     - { name: 'kernel.dmesg_restrict', value: 1 }
237     - { name: 'kernel.core_uses_pid', value: 1 }
238     - { name: 'kernel.randomize_va_space', value: 2 }
239     - { name: 'kernel.core_pattern', value: '/var/core/core'}
240
241 #
242 # Configure core dump
243 #
244
245 - name: "Disable core dump for all user"
246   lineinfile:
247     path: /etc/security/limits.conf
248     insertbefore: '^[a-z].*'
249     line: '*               hard    core            0'
250
251 - name: "Configure systemd not to store core dumps"
252   lineinfile:
253     path: /etc/systemd/coredump.conf
254     insertafter: '^\[Coredump\]'
255     line: 'Storage=none'
256
257 #
258 # Configure syslog
259 #
260 - name: "Stop rsyslog Service"
261   shell: systemctl stop rsyslog.service
262
263 - name: "Disable rsyslog Service"
264   shell: systemctl disable rsyslog.service
265
266 - name: "Ensure the /var/log/boot.log Rotated by logrotate"
267   lineinfile:
268     path: /etc/logrotate.d/syslog
269     insertbefore: 'cron$'
270     line: /var/log/boot.log
271
272 - name: "Set the umasks by profile file"
273   lineinfile:
274     path: /etc/profile
275     regexp: '{{ item.old }}'
276     line: '{{ item.new }}'
277   with_items:
278     - { old: 'umask 002', new: umask 027 }
279     - { old: 'umask 022', new: umask 077 }
280
281 #
282 # Keystone config
283 #
284
285 - name: Set the max_request_body_size in the keystone.conf
286   lineinfile:
287     path: /etc/keystone/keystone.conf
288     insertafter: 'DEFAULT'
289     line: "# enforced by optional sizelimit middleware (keystone.middleware:RequestBodySizeLimiter)\nmax_request_body_size = 114688\n"
290
291 - name: Set the insecure_debug in the keystone.conf
292   lineinfile:
293     path: /etc/keystone/keystone.conf
294     insertafter: 'DEFAULT'
295     line: "# If set to true, then the server will return information in HTTP responses\n# that may allow an unauthenticated or authenticated user to get more\n# information than normal, such as additional details about why authentication\n# failed. This may be useful for debugging but is insecure. (boolean value)\ninsecure_debug = false\n"
296
297 #
298 #Setting bootloader password
299 #
300 - name: set host os variable
301   when: host_os is defined
302   set_fact:
303     grub2_pass: "{{  host_os.grub2_password | default('Empty')  }}"
304
305 - name: protect grub with root password
306   when: grub2_pass is defined and grub2_pass != 'Empty'
307   blockinfile:
308     dest: /etc/grub.d/40_custom
309     state: present
310     insertafter: 'EOF'
311     content: |
312       # define superusers
313       set superusers="root"
314       #define users
315       password_pbkdf2 root "{{ grub2_pass }}"
316
317 - name: generate grub config
318   when: grub2_pass is defined and grub2_pass != 'Empty'
319   command: /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
320
321 #
322 #Setting the noexec option to the /dev/shm mount dir
323 #
324
325 - name: get back device associated to mountpoint
326   shell: mount | grep ' /dev/shm ' |cut -d ' ' -f 1
327   register: device_name
328   check_mode: no
329
330 - name: get back device previous mount option
331   shell: mount | grep ' /dev/shm ' | sed -re 's:.*\((.*)\):\1:'
332   register: device_cur_mountoption
333   check_mode: no
334
335 - name: get back device fstype
336   shell: mount | grep ' /dev/shm ' | cut -d ' ' -f 5
337   register: device_fstype
338   check_mode: no
339
340 - name: Ensure permission noexec are set on /dev/shm
341   mount:
342     path: "/dev/shm"
343     src: "{{device_name.stdout}}"
344     opts: "{{device_cur_mountoption.stdout}},noexec"
345     state: "mounted"
346     fstype: "{{device_fstype.stdout}}"
347
348 #
349 # Disable NFS service
350 #
351
352 - name: disable NFS related services
353   service:
354     name: "{{ item }}"
355     enabled: no
356     state: stopped
357   ignore_errors: yes
358   with_items:
359     - nfslock
360     - rpcgssd
361     - rpcidmapd
362     - nfs-idmap
363     - nfs-server
364     - nfs
365
366 - name: remove nfs-utils package
367   yum:
368     name: nfs-utils
369     state: absent
370
371 #
372 # Setting file permissions
373 #
374
375 #- name: "Remove the other user write permission from the system directorys"
376 #  command: find / -xdev \( -perm -0002 -a ! -perm -1000 \) -type d -exec chmod o-w {} \;
377 #
378 #- name: "Remove the other user write permission from the system files"
379 #  command: find / -xdev -perm -0002 -type f -exec chmod o-w {} \;
380 #
381 #- name: "Modified the unauthorized SUID/SGID system executables"
382 #  command: sudo chmod -s $(sudo find / -xdev \( -perm -4000 -o -perm -2000 \) -type f | grep -v sudo)