From cd5d484f7adc8e1f5c4875fa8f06701edfbbe960 Mon Sep 17 00:00:00 2001 From: joren485 Date: Tue, 4 Sep 2018 16:06:57 +0200 Subject: [PATCH] Fix dead code in files module (#43479) * Use pwd.getpwnam instead of non-existent pwd.getpwname * Add specific exceptions to pwd and grp calls --- lib/ansible/modules/files/stat.py | 4 ++-- lib/ansible/modules/files/unarchive.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/ansible/modules/files/stat.py b/lib/ansible/modules/files/stat.py index 096dc4b41c..1c4271ee50 100644 --- a/lib/ansible/modules/files/stat.py +++ b/lib/ansible/modules/files/stat.py @@ -496,13 +496,13 @@ def main(): try: # user data pw = pwd.getpwuid(st.st_uid) output['pw_name'] = pw.pw_name - except: + except (TypeError, KeyError): pass try: # group data grp_info = grp.getgrgid(st.st_gid) output['gr_name'] = grp_info.gr_name - except: + except (KeyError, ValueError, OverflowError): pass # checksums diff --git a/lib/ansible/modules/files/unarchive.py b/lib/ansible/modules/files/unarchive.py index 8a0bea8849..1f639b84db 100644 --- a/lib/ansible/modules/files/unarchive.py +++ b/lib/ansible/modules/files/unarchive.py @@ -303,22 +303,22 @@ class ZipArchive(object): run_gid = os.getgid() try: run_owner = pwd.getpwuid(run_uid).pw_name - except: + except (TypeError, KeyError): run_owner = run_uid try: run_group = grp.getgrgid(run_gid).gr_name - except: + except (KeyError, ValueError, OverflowError): run_group = run_gid # Get future user ownership fut_owner = fut_uid = None if self.file_args['owner']: try: - tpw = pwd.getpwname(self.file_args['owner']) - except: + tpw = pwd.getpwnam(self.file_args['owner']) + except KeyError: try: tpw = pwd.getpwuid(self.file_args['owner']) - except: + except (TypeError, KeyError): tpw = pwd.getpwuid(run_uid) fut_owner = tpw.pw_name fut_uid = tpw.pw_uid @@ -334,10 +334,10 @@ class ZipArchive(object): if self.file_args['group']: try: tgr = grp.getgrnam(self.file_args['group']) - except: + except (ValueError, KeyError): try: tgr = grp.getgrgid(self.file_args['group']) - except: + except (KeyError, ValueError, OverflowError): tgr = grp.getgrgid(run_gid) fut_group = tgr.gr_name fut_gid = tgr.gr_gid @@ -528,7 +528,7 @@ class ZipArchive(object): owner = uid = None try: owner = pwd.getpwuid(st.st_uid).pw_name - except: + except (TypeError, KeyError): uid = st.st_uid # If we are not root and requested owner is not our user, fail @@ -548,7 +548,7 @@ class ZipArchive(object): group = gid = None try: group = grp.getgrgid(st.st_gid).gr_name - except: + except (KeyError, ValueError, OverflowError): gid = st.st_gid if run_uid != 0 and fut_gid not in groups: