Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In "change_file_owner," use python library instead of shelling out to the OS. #277

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions protonvpn_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import random
import ipaddress
import math
import pwd
# External Libraries
import requests
from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -310,17 +311,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))

Expand Down