1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#!/usr/bin/python3
from gi.repository import GLib
from pydbus import SystemBus
import isodumper.raw_write as raw_write
import threading
import gettext
import logging
import time
DEAMON_ORG = 'org.mageia.Magiback'
DEAMON_INTERFACE = DEAMON_ORG
ISODUMPER_INTERFACE = DEAMON_ORG + ".Isodumper"
# Keep the authorization during KEEPER (in seconds)
KEEPER = 3600
auth = False
last_auth = 0
logging.basicConfig(filename="/var/log/magiback.log",
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.DEBUG)
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'/>
<arg type='x' name='size' direction='in'/>
</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='do_persistence'>
<arg type='s' name='target' direction='in'/>
<arg type='s' name='label' direction='in'/>
<arg type='s' name='key' direction='in'/>
<arg type='s' name='fs_type' direction='in'/>
</method>
<method name='set_backup_mode'>
<arg type='x' name='uid' direction='in'/>
<arg type='x' name='gid' direction='in'/>
</method>
<method name='unset_backup_mode'>
</method>
<method name='end'>
<arg type='b' name='success' direction='out'/>
<arg type='s' name='message' direction='out'/>
</method>
<method name='close'>
</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__()
APP = "isodumper"
DIR = "/usr/share/locale"
# Call translation catalog
gettext.install(APP, localedir=DIR,)
self.finished = threading.Event()
# finished is cleared at start of operation, and set at the end
self.finished.clear()
self._progress = 0
self.loop = GLib.MainLoop()
self.return_state = False
self.return_message = ""
self.authorized_sender_read = set()
self.authorized_sender_write = set()
self.sum_check = ""
self.sum_check_searched = False
self.signature_checked = False
self.writing_perm = False
self.writing_target = ""
self.backup_mode = False
self.uid = 0
self.gid = 0
self.last_auth = time.perf_counter()
def do_write(self, source, target, size, dbus_context):
self.finished.clear()
if check_permission('org.mageia.Magiback.Isodumper.write', dbus_context):
self.thread = threading.Thread(target=self._do_write, args=(source, target, size, self.backup_mode, self.uid, self.gid))
self.thread.start()
logging.info("Writing thread started")
self.writing_perm = True
self.writing_target = target
else:
self.return_message = "Writing: Access denied"
self.return_state = False
logging.debug(self.return_message)
self.finished.set()
def do_persistence(self, target, label, key, fs_type):
self.finished.clear()
if self.writing_perm and self.writing_target == target:
self.thread = threading.Thread(target=self._do_persistence, args=(target, label, key, fs_type))
self.thread.start()
logging.info("Persistence thread started")
else:
self.return_message = "Persistence: Access denied"
logging.info(self.return_message)
self.finished.set()
self.writing_perm = False
self.writing_target = ""
@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):
if hasattr(self, 'thread'):
self.thread.join()
self._progress = 0
self.finished.clear()
return self.return_state, self.return_message
def close(self):
logging.info("Closing")
# Close the loop from the main function
loop.quit()
@property
def progress(self):
return self._progress
def check_write(self, target, source):
if hasattr(self, 'key_thread'):
self.key_thread.join()
self.thread = threading.Thread(target=self._check_write, args=(target, source,))
self.thread.start()
def set_backup_mode(self, uid, gid):
logging.info("Set in backup mode")
self.backup_mode = True
self.uid = uid
self.gid = gid
def unset_backup_mode(self):
logging.info("Unset the backup mode")
self.backup_mode = False
self.uid = 0
self.gid = 0
def run(self):
self.loop.run()
def check_permission(action, dbus_context):
""" Check permission
"""
global auth
global last_auth
cur = time.perf_counter()
if cur - last_auth > KEEPER or not auth:
auth = dbus_context.is_authorized(action, {'polkit.icon_name': 'isodumper.png', }, interactive=True)
if auth:
last_auth = cur
return auth
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, dbus_context):
if check_permission('org.mageia.Magiback.write', dbus_context):
try:
with open(self.file_name, 'w') as tcf:
tcf.write(tc)
except:
return False
return True
class Logging(object):
"""
<node>
<interface name='org.mageia.Magiback.Logging'>
<method name='debug'>
<arg type='s' name='message' direction='in'/>
</method>
<method name='info'>
<arg type='s' name='message' direction='in'/>
</method>
<method name='warning'>
<arg type='s' name='message' direction='in'/>
</method>
<method name='error'>
<arg type='s' name='message' direction='in'/>
</method>
</interface>
</node>
"""
def __init__(self):
super().__init__()
def debug(self, message):
logging.debug(message)
def info(self, message):
logging.info(message)
def warning(self, message):
logging.warning(message)
def error(self, message):
logging.error(message)
if __name__ == '__main__':
print("Running Magiback service.")
bus = SystemBus()
conf_file = ConfFile()
log = Logging()
bus.publish(DEAMON_ORG, conf_file, ("Isodumper", Isodumper()), ('Logging', Logging()))
loop = GLib.MainLoop()
loop.run()
logging.shutdown()
|