Add checks and validations for existing users' upload directories and targets in SFTP configuration
This commit is contained in:
1 parent
03c19d29f4
commit
54fe7f6c90
1 file changed
+95
@@ -367,3 +367,98 @@
|
|||||||
label: "{{ item.0.name }}"
|
label: "{{ item.0.name }}"
|
||||||
tags:
|
tags:
|
||||||
- sftpd
|
- sftpd
|
||||||
|
|
||||||
|
# 'upload_dir' of existing users (optional):
|
||||||
|
# without 'upload_target' - directory owned by the user
|
||||||
|
# with 'upload_target' - symlink to 'upload_target'. The target must be
|
||||||
|
# inside the home (= chroot), the link is created
|
||||||
|
# relative, so it works inside and outside the chroot.
|
||||||
|
- name: (sftpd.yml) Check upload_dir/upload_target of existing users
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- item.upload_dir is not match('/')
|
||||||
|
- ('/' ~ item.upload_dir ~ '/') is not search('/\.\.?/')
|
||||||
|
- item.upload_target is not defined or item.upload_target is match('/')
|
||||||
|
- item.upload_target is not defined or ('/' ~ item.upload_target ~ '/') is not search('/\.\.?/')
|
||||||
|
- item.upload_target is not defined or
|
||||||
|
(item.upload_target | regex_replace('/+$', '')) is match(ansible_facts.getent_passwd[item.name][4] | regex_escape ~ '/')
|
||||||
|
fail_msg: >-
|
||||||
|
User '{{ item.name }}': 'upload_dir' must be a relative path and 'upload_target' an
|
||||||
|
absolute path inside the home '{{ ansible_facts.getent_passwd[item.name][4] }}'
|
||||||
|
(no '.' or '..' components) - symlinks pointing outside the chroot don't work.
|
||||||
|
quiet: true
|
||||||
|
loop: "{{ common_fact_sftp_existing_users | selectattr('upload_dir', 'defined') | list }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.name }}: {{ item.upload_dir }}"
|
||||||
|
tags:
|
||||||
|
- sftpd
|
||||||
|
|
||||||
|
- name: (sftpd.yml) Get upload_target of existing users
|
||||||
|
ansible.builtin.stat:
|
||||||
|
path: "{{ item.upload_target }}"
|
||||||
|
register: common_sftpd_upload_target_stat
|
||||||
|
loop: "{{ common_fact_sftp_existing_users | selectattr('upload_dir', 'defined') | selectattr('upload_target', 'defined') | list }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.name }}: {{ item.upload_target }}"
|
||||||
|
tags:
|
||||||
|
- sftpd
|
||||||
|
|
||||||
|
- name: (sftpd.yml) Fail if upload_target of an existing user is not a directory
|
||||||
|
ansible.builtin.fail:
|
||||||
|
msg: "User '{{ item.item.name }}': upload_target '{{ item.item.upload_target }}' does not exist or is not a directory."
|
||||||
|
when: not (item.stat.isdir | default(false))
|
||||||
|
loop: "{{ common_sftpd_upload_target_stat.results }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.item.name }}"
|
||||||
|
tags:
|
||||||
|
- sftpd
|
||||||
|
|
||||||
|
- name: (sftpd.yml) Get upload_dir (symlink path) of existing users
|
||||||
|
ansible.builtin.stat:
|
||||||
|
path: "{{ ansible_facts.getent_passwd[item.name][4] }}/{{ item.upload_dir | regex_replace('/+$', '') }}"
|
||||||
|
follow: false
|
||||||
|
register: common_sftpd_upload_link_stat
|
||||||
|
loop: "{{ common_fact_sftp_existing_users | selectattr('upload_dir', 'defined') | selectattr('upload_target', 'defined') | list }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.name }}: {{ item.upload_dir }}"
|
||||||
|
tags:
|
||||||
|
- sftpd
|
||||||
|
|
||||||
|
- name: (sftpd.yml) Fail if upload_dir of an existing user exists but is not a symlink
|
||||||
|
ansible.builtin.fail:
|
||||||
|
msg: >-
|
||||||
|
User '{{ item.item.name }}': '{{ item.stat.path }}' already exists and is not a symlink -
|
||||||
|
it is never replaced, remove/rename it or choose another 'upload_dir'.
|
||||||
|
when: item.stat.exists and not item.stat.islnk
|
||||||
|
loop: "{{ common_sftpd_upload_link_stat.results }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.item.name }}"
|
||||||
|
tags:
|
||||||
|
- sftpd
|
||||||
|
|
||||||
|
# No mode: permissions of an already existing directory (e.g. html) stay untouched.
|
||||||
|
- name: (sftpd.yml) Ensure upload_dir (directory) of existing users exists # noqa: risky-file-permissions
|
||||||
|
ansible.builtin.file:
|
||||||
|
path: "{{ ansible_facts.getent_passwd[item.name][4] }}/{{ item.upload_dir }}"
|
||||||
|
state: directory
|
||||||
|
owner: "{{ item.name }}"
|
||||||
|
group: "{{ ansible_facts.getent_passwd[item.name][2] }}"
|
||||||
|
loop: "{{ common_fact_sftp_existing_users | selectattr('upload_dir', 'defined') | rejectattr('upload_target', 'defined') | list }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.name }}: {{ ansible_facts.getent_passwd[item.name][4] }}/{{ item.upload_dir }}"
|
||||||
|
tags:
|
||||||
|
- sftpd
|
||||||
|
|
||||||
|
# An existing file/directory at the link path is never replaced (no force).
|
||||||
|
- name: (sftpd.yml) Ensure upload_dir (symlink) of existing users exists
|
||||||
|
ansible.builtin.file:
|
||||||
|
path: "{{ _link }}"
|
||||||
|
src: "{{ item.upload_target | regex_replace('/+$', '') | relpath(_link | dirname) }}"
|
||||||
|
state: link
|
||||||
|
vars:
|
||||||
|
_link: "{{ ansible_facts.getent_passwd[item.name][4] }}/{{ item.upload_dir | regex_replace('/+$', '') }}"
|
||||||
|
loop: "{{ common_fact_sftp_existing_users | selectattr('upload_dir', 'defined') | selectattr('upload_target', 'defined') | list }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.name }}: {{ item.upload_dir }} -> {{ item.upload_target }}"
|
||||||
|
tags:
|
||||||
|
- sftpd
|
||||||
Reference in new issue
Block a user