From 06c8aec2214943c4d971c749dc76b633d06595c8 Mon Sep 17 00:00:00 2001 From: Tim Meehan Date: Thu, 28 Jan 2021 20:35:48 -0600 Subject: [PATCH 1/2] Use python library instead of shelling out to the OS. Rather than using subprocess to check the ownership, use the built-in library calls --- protonvpn_cli/utils.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/protonvpn_cli/utils.py b/protonvpn_cli/utils.py index b1d9487..f40d820 100644 --- a/protonvpn_cli/utils.py +++ b/protonvpn_cli/utils.py @@ -310,17 +310,14 @@ def create_openvpn_config(serverlist, protocol, ports): def change_file_owner(path): """Change the owner of specific files to the sudo user.""" - uid = int(subprocess.run(["id", "-u", USER], - stdout=subprocess.PIPE).stdout) - gid = int(subprocess.run(["id", "-u", USER], - stdout=subprocess.PIPE).stdout) + sudo_user = pwd.getpwnam(USER) + uid = sudo_user.pw_uid + gid = sudo_user.pw_gid - current_owner = subprocess.run(["id", "-nu", str(os.stat(path).st_uid)], - stdout=subprocess.PIPE).stdout - current_owner = current_owner.decode().rstrip("\n") + current_owner = os.stat(path).st_uid # Only change file owner if it wasn't owned by current running user. - if current_owner != USER: + if current_owner != uid: os.chown(path, uid, gid) logger.debug("Changed owner of {0} to {1}".format(path, USER)) From 4fa35f93b230ccb511ee43e0eeeccbc57a6c7d0f Mon Sep 17 00:00:00 2001 From: Tim Meehan Date: Thu, 28 Jan 2021 20:44:24 -0600 Subject: [PATCH 2/2] Added missing import. --- protonvpn_cli/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/protonvpn_cli/utils.py b/protonvpn_cli/utils.py index f40d820..59e4c9d 100644 --- a/protonvpn_cli/utils.py +++ b/protonvpn_cli/utils.py @@ -9,6 +9,7 @@ import random import ipaddress import math +import pwd # External Libraries import requests from jinja2 import Environment, FileSystemLoader