From 5633dcfd37067ac6aaac644f23bf8616d7aafcc2 Mon Sep 17 00:00:00 2001 From: Denis Kulicek Date: Tue, 20 Jun 2017 15:12:35 +0200 Subject: [PATCH] Plugin ownership fix. Added utils module with chown function that changed ownership from root:root to owner:group of craftbeerpi folder. --- modules/addon/endpoints.py | 13 +++++++++---- modules/utils/__init__.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 modules/utils/__init__.py diff --git a/modules/addon/endpoints.py b/modules/addon/endpoints.py index 4a5e4c8..18e4d8c 100644 --- a/modules/addon/endpoints.py +++ b/modules/addon/endpoints.py @@ -4,7 +4,7 @@ import sys from flask import Blueprint, request, send_from_directory from importlib import import_module from modules import socketio, cbpi - +from modules.utils import chown_unroot from git import Repo import os @@ -138,7 +138,7 @@ def plugins(): value["installed"] = os.path.isdir("./modules/plugins/%s/" % (key)) return json.dumps(cbpi.cache["plugins"]) - + @blueprint.route('//download', methods=['POST']) def download_addon(name): @@ -148,7 +148,10 @@ def download_addon(name): if plugin is None: return ('', 404) try: - Repo.clone_from(plugin.get("repo_url"), "./modules/plugins/%s/" % (name)) + module_path= "./modules/plugins/%s/" % (name) + Repo.clone_from(plugin.get("repo_url"), module_path) + chown_unroot(module_path) + cbpi.notify("Download successful", "Plugin %s downloaded successfully" % name) finally: plugin["loading"] = False @@ -157,9 +160,11 @@ def download_addon(name): @blueprint.route('//update', methods=['POST']) def update_addon(name): - repo = Repo("./modules/plugins/%s/" % (name)) + module_path= "./modules/plugins/%s/" % (name) + repo = Repo(module_path) o = repo.remotes.origin info = o.pull() + chown_unroot(module_path) cbpi.notify("Plugin Updated", "Plugin %s updated successfully. Please restart the system" % name) return ('', 204) diff --git a/modules/utils/__init__.py b/modules/utils/__init__.py new file mode 100644 index 0000000..0afb364 --- /dev/null +++ b/modules/utils/__init__.py @@ -0,0 +1,20 @@ +from modules import cbpi +import os + +def chown_unroot(path): + """ + Changes owner of the path recursively from root:root to the user:group who installed craftbeerpi + can be used to unroot plugins and craftbeerpi updates + """ + dir_stat = os.stat('.') + uid = dir_stat.st_uid + gid = dir_stat.st_gid + + cbpi.app.logger.info("Executing chown -R for: " + path) + + for root, dirs, files in os.walk(path): + os.chown(root, uid, gid) + for name in dirs: + os.chown(os.path.join(root, name), uid, gid) + for name in files: + os.chown(os.path.join(root, name), uid, gid) \ No newline at end of file