aboutsummaryrefslogtreecommitdiffstats
path: root/backend
diff options
context:
space:
mode:
authorPapoteur <papoteur@mageia.org>2018-03-11 13:54:12 +0100
committerPapoteur <papoteur@mageia.org>2018-03-16 11:17:42 +0100
commit2ac71badfd671cd397ac23c92be58ff2090bac21 (patch)
tree171d895cb3dd0a06c8cd88096c9814fb9c095b1b /backend
parentcf47ed2aa96097466370456eeb66709370707cb8 (diff)
downloadisodumper-2ac71badfd671cd397ac23c92be58ff2090bac21.tar
isodumper-2ac71badfd671cd397ac23c92be58ff2090bac21.tar.gz
isodumper-2ac71badfd671cd397ac23c92be58ff2090bac21.tar.bz2
isodumper-2ac71badfd671cd397ac23c92be58ff2090bac21.tar.xz
isodumper-2ac71badfd671cd397ac23c92be58ff2090bac21.zip
Add backend providing DBus service
Diffstat (limited to 'backend')
-rwxr-xr-xbackend/magiback174
-rw-r--r--backend/org.mageia.Magiback.conf22
-rw-r--r--backend/org.mageia.magiback.service4
3 files changed, 200 insertions, 0 deletions
diff --git a/backend/magiback b/backend/magiback
new file mode 100755
index 0000000..796ba78
--- /dev/null
+++ b/backend/magiback
@@ -0,0 +1,174 @@
+#!/usr/bin/python3
+
+from gi.repository import GLib
+from pydbus import SystemBus
+import raw_write
+import logging
+import threading
+
+DEAMON_ORG = 'org.mageia.Magiback'
+DEAMON_INTERFACE = DEAMON_ORG
+ISODUMPER_INTERFACE = DEAMON_ORG + ".Isodumper"
+logger = logging.getLogger('magiback.system')
+
+
+class Isodumper(raw_write.Dumper):
+ """
+ <node>
+ <interface name='org.mageia.Magiback.Isodumper'>
+ <method name='do_write'>
+ <arg type='s' name='source' direction='in'/>
+ <arg type='s' name='target' direction='in'/>
+ </method>
+ <method name='do_unmount'>
+ <arg type='s' name='device' direction='in'/>
+ <arg type='b' name='success' direction='out'/>
+ <arg type='s' name='message' direction='out'/>
+ </method>
+ <method name='do_format'>
+ <arg type='s' name='device' direction='in'/>
+ <arg type='s' name='format' direction='in'/>
+ <arg type='b' name='success' direction='out'/>
+ <arg type='s' name='message' direction='out'/>
+ </method>
+ <method name='check_write'>
+ <arg type='s' name='target' direction='in'/>
+ <arg type='s' name='source' direction='in'/>
+ </method>
+ <method name='end'>
+ <arg type='b' name='success' direction='out'/>
+ <arg type='s' name='message' direction='out'/>
+ </method>
+ <property name="done" type="b" access="read"/>
+ <property name="progress" type="i" access="read"/>
+ <property name="message" type="s" access="read"/>
+ <property name="state" type="b" access="read"/>
+ </interface>
+ </node>
+ """
+ def __init__(self):
+ super().__init__()
+ self.finished = threading.Event()
+ self._progress = 0
+ self.loop = GLib.MainLoop()
+ self.return_state = False
+ self.return_message = ""
+ self.authorized_sender_read = set()
+ self.authorized_sender_write = set()
+
+ def do_write(self,source, target):
+ print("Writing")
+ self.finished.clear()
+ self.thread = threading.Thread(target=self._do_write, args=(source, target, ))
+ self.thread.start()
+
+ @property
+ def done(self):
+ return self.finished.wait(1)
+
+ @property
+ def message(self):
+ return self.return_message
+
+ @property
+ def state(self):
+ return self.return_state
+
+ def end(self):
+ self.thread.join()
+ self._progress = 0
+ self.finished.clear()
+ return self.return_state, self.return_message
+
+ @property
+ def progress(self):
+ return self._progress
+
+ def do_unmount(self, device):
+ print("Unmounting")
+ return self._do_unmount([device])
+
+ def check_write(self,target, source):
+ self.thread = threading.Thread(target=self._check_write, args=(target, source,))
+ self.thread.start()
+
+ def run(self):
+ self.loop.run()
+
+ def check_permission_write(self, sender):
+ """ Check for senders permission to update system packages"""
+ if sender in self.authorized_sender_write:
+ return
+ else:
+ if self._check_permission(sender, 'org.baseurl.DnfSystem.write'):
+ self.authorized_sender_write.add(sender)
+
+ def _check_permission(self, sender, action):
+ """ Check senders permissions using PolicyKit1
+ """
+ if not sender:
+ raise ValueError('sender == None')
+
+ obj = SystemBus().get(
+ 'org.freedesktop.PolicyKit1',
+ '/org/freedesktop/PolicyKit1/Authority')
+ iface = obj['org.freedesktop.PolicyKit1.Authority']
+ (granted, _, details) = iface.CheckAuthorization(
+ ('system-bus-name', {'name': sender}), action, {},
+ 1, '', timeout=600)
+ if not granted:
+ return False
+ else:
+ return True
+
+
+class ConfFile(object):
+ """
+ <node>
+ <interface name='org.mageia.Magiback'>
+ <method name='setName'>
+ <arg type='s' name='a' direction='in'/>
+ </method>
+ <method name='getFile'>
+ <arg type='s' name='content' direction='out'/>
+ </method>
+ <method name='saveFile'>
+ <arg type='s' name='content' direction='in'/>
+ </method>
+ </interface>
+ </node>
+ """
+ def __init__(self):
+ super().__init__()
+
+ def setName(self,file_name):
+ self.file_name = file_name
+
+ def getFile(self):
+ with open(self.file_name,'r') as tcf :
+ # read the file
+ content=""
+ while 1:
+ line = tcf.readline()
+ if not line:
+ break
+ content += line
+ return content
+
+ def saveFile(self, tc):
+ try:
+ with open(self.file_name,'w') as tcf :
+ tcf.write(tc)
+ except:
+ return False
+ return True
+
+if __name__ == '__main__':
+ print("Running Magiback service.")
+
+ bus = SystemBus()
+ conf_file = ConfFile()
+ bus.publish(DEAMON_ORG, conf_file,
+ ("Isodumper", Isodumper()))
+ loop = GLib.MainLoop()
+ loop.run()
diff --git a/backend/org.mageia.Magiback.conf b/backend/org.mageia.Magiback.conf
new file mode 100644
index 0000000..af00707
--- /dev/null
+++ b/backend/org.mageia.Magiback.conf
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE busconfig PUBLIC
+ "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+ "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+ <!-- Only root can become service owner -->
+ <policy user="root">
+ <allow own="org.mageia.Magiback"/>
+ <allow send_destination="org.mageia.Magiback"/>
+ <allow send_interface="org.mageia.Magiback"/>
+ </policy>
+
+ <!-- Anyone can invoke method -->
+ <policy context="default">
+ <allow send_destination="org.mageia.Magiback"/>
+ <allow send_interface="org.mageia.Magiback"/>
+ </policy>
+ <policy context="default">
+ <allow send_destination="org.mageia.Magiback.Isodumper"/>
+ <allow send_interface="org.mageia.Magiback.Isodumper"/>
+ </policy>
+</busconfig>
diff --git a/backend/org.mageia.magiback.service b/backend/org.mageia.magiback.service
new file mode 100644
index 0000000..4f45720
--- /dev/null
+++ b/backend/org.mageia.magiback.service
@@ -0,0 +1,4 @@
+[D-BUS Service]
+Name=org.mageia.Magiback
+Exec=python3 /home/yves/dev/magiback/magiback
+User=root