diff options
author | Christian Breunig <christian@breunig.cc> | 2023-12-30 10:56:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-30 10:56:22 +0100 |
commit | e70e7dba736fec27145f2b4abeb70278dce0deb9 (patch) | |
tree | ddde9726bf950a6f72032161879f8d73d9b92e12 /python | |
parent | 0591d03464248c3a50516cd00b04989b3f16f1e5 (diff) | |
parent | 3c990f49e2bf9347bd2cc478995baa995ee822fd (diff) | |
download | vyos-1x-e70e7dba736fec27145f2b4abeb70278dce0deb9.tar.gz vyos-1x-e70e7dba736fec27145f2b4abeb70278dce0deb9.zip |
Merge pull request #2716 from c-po/login-t5875
login: T5875: restore home directory permissions when re-adding user account
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/utils/file.py | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/python/vyos/utils/file.py b/python/vyos/utils/file.py index 70ac1753b..c566f0334 100644 --- a/python/vyos/utils/file.py +++ b/python/vyos/utils/file.py @@ -83,21 +83,34 @@ def read_json(fname, defaultonfailure=None): return defaultonfailure raise e -def chown(path, user, group): +def chown(path, user=None, group=None, recursive=False): """ change file/directory owner """ from pwd import getpwnam from grp import getgrnam - if user is None or group is None: + if user is None and group is None: return False # path may also be an open file descriptor if not isinstance(path, int) and not os.path.exists(path): return False - uid = getpwnam(user).pw_uid - gid = getgrnam(group).gr_gid - os.chown(path, uid, gid) + # keep current value if not specified otherwise + uid = -1 + gid = -1 + + if user: + uid = getpwnam(user).pw_uid + if group: + gid = getgrnam(group).gr_gid + + if recursive: + for dirpath, dirnames, filenames in os.walk(path): + os.chown(dirpath, uid, gid) + for filename in filenames: + os.chown(os.path.join(dirpath, filename), uid, gid) + else: + os.chown(path, uid, gid) return True |