-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd-del-ppa.py
More file actions
executable file
·366 lines (323 loc) · 13 KB
/
add-del-ppa.py
File metadata and controls
executable file
·366 lines (323 loc) · 13 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/python3
'''
Copyright 2019 Ian Santopietro (ian@system76.com)
This file is part of Repoman.
Repoman is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Repoman is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Repoman. If not, see <http://www.gnu.org/licenses/>.
'''
import gi
from gi.repository import GObject
import dbus
import dbus.service
import dbus.mainloop.glib
import time
import os
import repolib
class RepomanException(dbus.DBusException):
_dbus_error_name = 'ro.santopiet.repoman.RepomanException'
class PermissionDeniedByPolicy(dbus.DBusException):
_dbus_error_name = 'ro.santopiet.repoman.PermissionDeniedByPolicy'
class AptException(Exception):
pass
class PPAObject(dbus.service.Object):
def __init__(self, conn=None, object_path=None, bus_name=None):
dbus.service.Object.__init__(self, conn, object_path, bus_name)
# The following variables are used bu _check_polkit_privilege
self.dbus_info = None
self.polkit = None
self.enforce_polkit = True
# self.sp = SoftwareProperties()
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='s', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def AddRepo(self, line, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
# PPA Add code here
try:
if line.startswith('deb'):
new_source = repolib.DebLine(line)
elif line.startswith('ppa:'):
new_source = repolib.PPALine(line)
new_source.save_to_disk()
return 0
except:
raise AptException("Could not Add the APT Source")
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='ssssb', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def AddFullRepo(self, name, uris, suites, components, code, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
# PPA Add code here
try:
new_source = repolib.Source(
name=name,
uris=uris.split(),
suites=suites.split(),
components=components.split(),
)
new_source.set_source_enabled(code)
new_source.filename = name.translate(repolib.util.CLEAN_CHARS)
new_source.filename += '.sources'
new_source.save_to_disk()
return 0
except:
raise AptException("Could not remove the APT Source")
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='s', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def DelRepo(self, filename, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
# PPA Remove code here
try:
os.remove(filename)
return 0
except:
raise AptException("Could not remove the APT Source")
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='ss', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def AddComp(self, repo, comp, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
try:
source = repolib.Source()
source.load_from_file(filename='{}.sources'.format(repo))
if not comp in source.components:
source.components.append(comp)
source.components.sort()
source.save_to_disk()
return 0
except:
raise AptException("Could not add %s to source %s" % (comp, repo))
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='ss', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def DelComp(self, repo, comp, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
try:
source = repolib.Source()
source.load_from_file(filename='{}.sources'.format(repo))
if comp in source.components:
source.components.remove(comp)
source.components.sort()
source.save_to_disk()
return 0
except:
raise AptException("Could not add %s to source %s" % (comp, repo))
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='ss', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def AddSuite(self, repo, suite, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
try:
source = repolib.Source()
source.load_from_file(filename='{}.sources'.format(repo))
if not suite in source.suites:
source.suites.append(suite)
source.suites.sort()
source.save_to_disk()
return 0
except:
raise AptException("Could not add %s to source %s" % (comp, repo))
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='ss', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def DelSuite(self, repo, suite, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
try:
source = repolib.Source()
source.load_from_file(filename='{}.sources'.format(repo))
if suite in source.suites:
source.suites.remove(suite)
source.suites.sort()
source.save_to_disk()
return 0
except:
raise AptException("Could not add %s to source %s" % (comp, repo))
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='sb', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def SetSource(self, repo, state, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
try:
source = repolib.Source()
source.load_from_file(filename='{}.sources'.format(repo))
source.set_source_enabled(state)
source.save_to_disk()
return 0
except:
raise AptException("Could not add %s to source %s" % (comp, repo))
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='sbbssss', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def SetModifiedRepo(self, name, enabled, source_code, uris, suites, components, filename, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
# PPA Modify code here
try:
source = repolib.Source(filename=filename)
source.load_from_file()
source.name=name
source.set_enabled(enabled)
source.uris=uris.split()
source.suites=suites.split()
source.components=components.split()
source.set_source_enabled(source_code)
source.save_to_disk()
return 0
except:
raise AptException("Could not modify the APT Source")
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='sb', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def SetCompEnabled(self, comp_name, is_enabled, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
# PPA Modify code here
return 0
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='sb', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def SetChildEnabled(self, child_name, is_enabled, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
# PPA Modify code here
return 0
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='b', out_signature='i',
sender_keyword='sender', connection_keyword='conn'
)
def SetSourceCodeEnabled(self, is_enabled, sender=None, conn=None):
self._check_polkit_privilege(
sender, conn, 'ro.santopiet.repoman.modppa'
)
return 0
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='', out_signature='',
sender_keyword='sender', connection_keyword='conn'
)
def RaiseException(self, sender=None, conn=None):
raise RepomanException('Error managing software sources!')
@dbus.service.method(
"ro.santopiet.repoman.Interface",
in_signature='', out_signature='',
sender_keyword='sender', connection_keyword='conn'
)
def Exit(self, sender=None, conn=None):
mainloop.quit()
@classmethod
def _log_in_file(klass, filename, string):
date = time.asctime(time.localtime())
ff = open(filename, "a")
ff.write("%s : %s\n" %(date,str(string)))
ff.close()
@classmethod
def _strip_source_line(self, source):
source = source.replace("#", "# ")
source = source.replace("[", "")
source = source.replace("]", "")
source = source.replace("'", "")
source = source.replace(" ", " ")
return source
def _check_polkit_privilege(self, sender, conn, privilege):
# from jockey
'''Verify that sender has a given PolicyKit privilege.
sender is the sender's (private) D-BUS name, such as ":1:42"
(sender_keyword in @dbus.service.methods). conn is
the dbus.Connection object (connection_keyword in
@dbus.service.methods). privilege is the PolicyKit privilege string.
This method returns if the caller is privileged, and otherwise throws a
PermissionDeniedByPolicy exception.
'''
if sender is None and conn is None:
# called locally, not through D-BUS
return
if not self.enforce_polkit:
# that happens for testing purposes when running on the session
# bus, and it does not make sense to restrict operations here
return
# get peer PID
if self.dbus_info is None:
self.dbus_info = dbus.Interface(conn.get_object('org.freedesktop.DBus',
'/org/freedesktop/DBus/Bus', False), 'org.freedesktop.DBus')
pid = self.dbus_info.GetConnectionUnixProcessID(sender)
# query PolicyKit
if self.polkit is None:
self.polkit = dbus.Interface(dbus.SystemBus().get_object(
'org.freedesktop.PolicyKit1',
'/org/freedesktop/PolicyKit1/Authority', False),
'org.freedesktop.PolicyKit1.Authority')
try:
# we don't need is_challenge return here, since we call with AllowUserInteraction
(is_auth, _, details) = self.polkit.CheckAuthorization(
('unix-process', {'pid': dbus.UInt32(pid, variant_level=1),
'start-time': dbus.UInt64(0, variant_level=1)}),
privilege, {'': ''}, dbus.UInt32(1), '', timeout=600)
except dbus.DBusException as e:
if e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown':
# polkitd timed out, connect again
self.polkit = None
return self._check_polkit_privilege(sender, conn, privilege)
else:
raise
if not is_auth:
PPAObject._log_in_file('/tmp/repoman.log','_check_polkit_privilege: sender %s on connection %s pid %i is not authorized for %s: %s' %
(sender, conn, pid, privilege, str(details)))
raise PermissionDeniedByPolicy(privilege)
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
name = dbus.service.BusName("ro.santopiet.repoman", bus)
object = PPAObject(bus, '/PPAObject')
mainloop = GObject.MainLoop()
mainloop.run()