diff --git a/library/qbittorrent_passwd.py b/library/qbittorrent_passwd.py new file mode 100644 index 0000000000..e583b9af5e --- /dev/null +++ b/library/qbittorrent_passwd.py @@ -0,0 +1,41 @@ +#!/usr/bin/python +from ansible.module_utils.basic import AnsibleModule +import base64 +import hashlib +import os + +def qbittorrent_passwd(plain_passwd): + try: + ITERATIONS = 100_000 + SALT_SIZE = 16 + + salt = os.urandom(SALT_SIZE) + h = hashlib.pbkdf2_hmac("sha512", plain_passwd.encode(), salt, ITERATIONS) + return "@ByteArray({}:{})".format(base64.b64encode(salt).decode(), base64.b64encode(h).decode()) + except Exception as e: + raise ValueError(f"Error generating password hash: {str(e)}") + +def main(): + module_args = dict( + password=dict(type='str', required=True, no_log=True) + ) + + result = dict( + changed=False, + msg='' + ) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + + try: + result['msg'] = qbittorrent_passwd(module.params['password']) + except ValueError as err: + module.fail_json(msg=str(err)) + + module.exit_json(**result) + +if __name__ == '__main__': + main() diff --git a/roles/qbittorrentvpn/tasks/subtasks/post-install/settings/main.yml b/roles/qbittorrentvpn/tasks/subtasks/post-install/settings/main.yml index 5fdad8f78c..c69e0fc0b9 100644 --- a/roles/qbittorrentvpn/tasks/subtasks/post-install/settings/main.yml +++ b/roles/qbittorrentvpn/tasks/subtasks/post-install/settings/main.yml @@ -42,3 +42,30 @@ owner: "{{ user.name }}" group: "{{ user.name }}" mode: "0775" + +- name: Post-Install | Settings | Generate Password Hash + qbittorrent_passwd: + password: "{{ user.pass }}" + register: qbittorrent_hashed_passwd + +- name: Post-Install | Settings | Set qBittorrent 'WebUI\Username' + community.general.ini_file: + section: Preferences + option: WebUI\Username + value: '{{ user.name }}' + path: "{{ qbittorrentvpn_paths_conf }}" + state: present + owner: "{{ user.name }}" + group: "{{ user.name }}" + mode: "0775" + +- name: Post-Install | Settings | Set qBittorrent 'WebUI\Password_PBKDF2' + community.general.ini_file: + section: Preferences + option: WebUI\Password_PBKDF2 + value: '{{ qbittorrent_hashed_passwd.msg }}' + path: "{{ qbittorrentvpn_paths_conf }}" + state: present + owner: "{{ user.name }}" + group: "{{ user.name }}" + mode: "0775"