From a79dd4fe04d145206a5fb432c2a6817d8018f49f Mon Sep 17 00:00:00 2001 From: James Date: Mon, 6 Feb 2012 12:26:20 -0500 Subject: [PATCH] CC-3299: Media monitor changes the owner of watched files? - media monitor now tries to set the permission to www-data if the file/dir cannot be read by www-data --- .../airtimefilemonitor/mediamonitorcommon.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py index f0c033a3b..6cdac0e63 100644 --- a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py +++ b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py @@ -46,9 +46,13 @@ class MediaMonitorCommon: return False #check if file is readable by "nobody" - def has_correct_permissions(self, filepath): + def has_correct_permissions(self, filepath, euid='nobody', egid='nogroup'): + uid = pwd.getpwnam(euid)[2] + gid = grp.getgrnam(egid)[2] + #drop root permissions and become "nobody" - os.seteuid(65534) + os.seteuid(uid) + os.setegid(gid) try: open(filepath) @@ -65,19 +69,18 @@ class MediaMonitorCommon: return readable + # the function only changes the permission if its not readable by www-data def set_needed_file_permissions(self, item, is_dir): try: omask = os.umask(0) - - uid = pwd.getpwnam('www-data')[2] - gid = grp.getgrnam('www-data')[2] - - os.chown(item, uid, gid) - - if is_dir is True: - os.chmod(item, 02777) - else: - os.chmod(item, 0666) + + if not has_correct_permissions(item, 'www-data', 'www-data'): + os.chown(item, uid, gid) + + if is_dir is True: + os.chmod(item, 02777) + else: + os.chmod(item, 0666) except Exception, e: self.logger.error("Failed to change file's owner/group/permissions. %s", e)