37
36
dbus.service.Object.__init__(self, bus_name, '/')
38
37
apt_pkg.init_config()
40
def _authWithPolicyKit(self, sender, connection, priv):
41
#print "_authWithPolicyKit()"
42
system_bus = dbus.SystemBus()
43
obj = system_bus.get_object("org.freedesktop.PolicyKit1",
44
"/org/freedesktop/PolicyKit1/Authority",
45
"org.freedesktop.PolicyKit1.Authority")
46
policykit = dbus.Interface(obj, "org.freedesktop.PolicyKit1.Authority")
47
info = dbus.Interface(connection.get_object('org.freedesktop.DBus',
48
'/org/freedesktop/DBus/Bus',
50
'org.freedesktop.DBus')
51
pid = info.GetConnectionUnixProcessID(sender)
54
subject = ('unix-process',
55
{ 'pid' : dbus.UInt32(pid, variant_level=1),
56
'start-time' : dbus.UInt64(0),
60
flags = dbus.UInt32(1) # AllowUserInteraction = 0x00000001
62
(ok, notused, details) = policykit.CheckAuthorization(subject,
71
39
# proxy stuff ---------------------------------------------------
72
40
def _etc_environment_proxy(self, proxy_type):
73
41
" internal that returns the /etc/environment proxy "
236
204
modify /etc/environment
239
if not self._authWithPolicyKit(sender, conn,
240
"com.ubuntu.systemservice.setproxy"):
241
if not self._authWithPolicyKit(sender, conn,
242
"org.gnome.gconf.defaults.set-system"):
207
if not authWithPolicyKit(sender, conn,
208
"com.ubuntu.systemservice.setproxy"):
209
if not authWithPolicyKit(sender, conn,
210
"org.gnome.gconf.defaults.set-system"):
243
211
raise PermissionDeniedError, "Permission denied by policy"
245
213
# check if something supported is set
298
266
This function will modify /etc/environment
301
if not self._authWithPolicyKit(sender, conn,
302
"com.ubuntu.systemservice.setnoproxy"):
303
if not self._authWithPolicyKit(sender, conn,
304
"org.gnome.gconf.defaults.set-system"):
269
if not authWithPolicyKit(sender, conn,
270
"com.ubuntu.systemservice.setnoproxy"):
271
if not authWithPolicyKit(sender, conn,
272
"org.gnome.gconf.defaults.set-system"):
305
273
raise PermissionDeniedError, "Permission denied by policy"
314
282
# keyboard stuff ---------------------------------------------------
315
def _get_keyboard_from_etc(self):
317
helper that reads /etc/default/console-setup and gets the
318
keyboard settings there
324
for line in open(self.CONSOLE_SETUP_DEFAULT):
325
if line.startswith("XKBMODEL="):
326
model = line.split("=")[1].strip('"\n')
327
elif line.startswith("XKBLAYOUT="):
328
layout = line.split("=")[1].strip('"\n')
329
elif line.startswith("XKBVARIANT="):
330
variant = line.split("=")[1].strip('"\n')
331
elif line.startswith("XKBOPTIONS="):
332
options = line.split("=")[1].strip('"\n')
333
return (model, layout, variant, options)
335
283
@dbus.service.method(DBUS_INTERFACE_NAME,
337
285
out_signature='ssss',
350
298
It returns True on sucess
352
(model, layout, variant, options) = self._get_keyboard_from_etc()
300
(model, layout, variant, options) = get_keyboard_from_etc()
353
301
return (model, layout, variant, options)
355
def _set_keyboard_to_etc(self, model, layout, variant, options):
357
helper that writes /etc/default/console-setup
359
#print "set_keyboard_to_etc"
360
# FIXME: what to do if not os.path.exists(self.CONSOLE_SETUP_DEFAULT)
362
for line in open(self.CONSOLE_SETUP_DEFAULT):
363
if line.startswith("XKBMODEL="):
364
line = 'XKBMODEL="%s"\n' % model
365
elif line.startswith("XKBLAYOUT="):
366
line = 'XKBLAYOUT="%s"\n' % layout
367
elif line.startswith("XKBVARIANT="):
368
line = 'XKBVARIANT="%s"\n' % variant
369
elif line.startswith("XKBOPTIONS="):
370
line = 'XKBOPTIONS="%s"\n' % options
372
# if something changed, write
373
if content != open(self.CONSOLE_SETUP_DEFAULT).readlines():
374
#print "content changed, writing"
375
open(self.CONSOLE_SETUP_DEFAULT+".new","w").write("".join(content))
376
os.rename(self.CONSOLE_SETUP_DEFAULT+".new",
377
self.CONSOLE_SETUP_DEFAULT)
380
def _verify_keyboard_settings(self, model, layout, variant, options):
381
" helper that verfies the settings "
382
# check against char whitelist
383
allowed = "^[0-9a-zA-Z:,_]*$"
384
for s in (model, layout, variant, options):
385
if not re.match(allowed, s):
386
#print "illegal chars in '%s'" % s
388
# check if 'ckbcomp' can compile it
391
cmd += ["-model",model]
393
cmd += ["-layout", layout]
395
cmd += ["-variant", variant]
397
cmd += ["-option", options]
398
ret = subprocess.call(cmd, stdout=open(os.devnull))
401
def _run_setupcon(self):
403
helper that runs setupcon to activate the settings, taken from
404
oem-config (/usr/lib/oem-config/console/console-setup-apply)
406
ret = subprocess.call(["setupcon","--save-only"])
407
subprocess.Popen(["/usr/sbin/update-initramfs","-u"])
410
303
@dbus.service.method(DBUS_INTERFACE_NAME,
411
304
in_signature='ssss',
412
305
out_signature='b',
418
311
strings: (model, layout, variant, options)
420
313
#print "set_keyboard: ", model, layout, variant, options
421
if not self._authWithPolicyKit(sender, conn,
422
"com.ubuntu.systemservice.setkeyboard"):
423
if not self._authWithPolicyKit(sender, conn,
424
"org.gnome.gconf.defaults.set-system"):
314
if not authWithPolicyKit(sender, conn,
315
"com.ubuntu.systemservice.setkeyboard"):
316
if not authWithPolicyKit(sender, conn,
317
"org.gnome.gconf.defaults.set-system"):
426
319
raise PermissionDeniedError, "Permission denied by policy"
428
# if no keyboard model is set, try to guess one
429
# this is based on the "console-setup.config" code that
440
# verify the settings
441
if not self._verify_keyboard_settings(model, layout, variant, options):
442
#print "verify_keyboard failed"
443
raise InvalidKeyboardTypeError, "Invalid keyboard set"
446
if not self._set_keyboard_to_etc(model, layout, variant, options):
447
#print "could not write keyboard to /etc"
449
if not self._run_setupcon():
450
#print "setupcon failed"
322
if not set_keyboard_to_etc(model, layout, variant, options):
323
print "could not write keyboard to /etc"
498
371
Check if the package system is locked
500
373
#print "set_keyboard: ", model, layout, variant, options
501
if not self._authWithPolicyKit(sender, conn,
502
"com.ubuntu.systemservice.ispkgsystemlocked"):
503
raise PermissionDeniedError, "Permission denied by policy"
374
if not authWithPolicyKit(sender, conn,
375
"com.ubuntu.systemservice.ispkgsystemlocked"):
376
raise PermissionDeniedError, "Permission denied by policy"
504
377
return self._is_package_system_locked()