6
CONSOLE_SETUP_DEFAULT = "/etc/default/keyboard"
8
class UnknownProxyTypeError(dbus.DBusException):
9
" an unknown proxy type was passed "
11
class InvalidKeyboardTypeError(dbus.DBusException):
12
" an invalid keyboard was set "
14
class PermissionDeniedError(dbus.DBusException):
15
" permission denied by policy "
18
def authWithPolicyKit(sender, connection, priv, interactive=1):
19
#print "_authWithPolicyKit()"
20
system_bus = dbus.SystemBus()
21
obj = system_bus.get_object("org.freedesktop.PolicyKit1",
22
"/org/freedesktop/PolicyKit1/Authority",
23
"org.freedesktop.PolicyKit1.Authority")
24
policykit = dbus.Interface(obj, "org.freedesktop.PolicyKit1.Authority")
25
info = dbus.Interface(connection.get_object('org.freedesktop.DBus',
26
'/org/freedesktop/DBus/Bus',
28
'org.freedesktop.DBus')
29
pid = info.GetConnectionUnixProcessID(sender)
32
subject = ('unix-process',
33
{ 'pid' : dbus.UInt32(pid, variant_level=1),
34
'start-time' : dbus.UInt64(0),
38
flags = dbus.UInt32(interactive) # AllowUserInteraction = 0x00000001
40
(ok, notused, details) = policykit.CheckAuthorization(subject,
48
def get_keyboard_from_etc():
50
helper that reads /etc/default/console-setup and gets the
51
keyboard settings there
58
f = open(CONSOLE_SETUP_DEFAULT)
60
if line.startswith("XKBMODEL="):
61
model = line.split("=")[1].strip('"\n')
62
elif line.startswith("XKBLAYOUT="):
63
layout = line.split("=")[1].strip('"\n')
64
elif line.startswith("XKBVARIANT="):
65
variant = line.split("=")[1].strip('"\n')
66
elif line.startswith("XKBOPTIONS="):
67
options = line.split("=")[1].strip('"\n')
70
print (model, layout, variant, options)
72
print "Couldn't read ", CONSOLE_SETUP_DEFAULT
74
return (model, layout, variant, options)
78
helper that runs setupcon to activate the settings, taken from
79
oem-config (/usr/lib/oem-config/console/console-setup-apply)
81
ret = subprocess.call(["setupcon","--save-only"])
82
subprocess.Popen(["/usr/sbin/update-initramfs","-u"])
85
def set_keyboard_to_etc(model, layout, variant, options):
87
helper that writes /etc/default/console-setup
89
# if no keyboard model is set, try to guess one
90
# this is based on the "console-setup.config" code that
101
# verify the settings
102
if not verify_keyboard_settings(model, layout, variant, options):
103
#print "verify_keyboard failed"
104
raise InvalidKeyboardTypeError, "Invalid keyboard set"
106
# FIXME: what to do if not os.path.exists(CONSOLE_SETUP_DEFAULT)
108
for line in open(CONSOLE_SETUP_DEFAULT):
109
if line.startswith("XKBMODEL="):
110
line = 'XKBMODEL="%s"\n' % model
111
elif line.startswith("XKBLAYOUT="):
112
line = 'XKBLAYOUT="%s"\n' % layout
113
elif line.startswith("XKBVARIANT="):
114
line = 'XKBVARIANT="%s"\n' % variant
115
elif line.startswith("XKBOPTIONS="):
116
line = 'XKBOPTIONS="%s"\n' % options
118
# if something changed, write
119
if content != open(CONSOLE_SETUP_DEFAULT).readlines():
120
#print "content changed, writing"
121
open(CONSOLE_SETUP_DEFAULT+".new","w").write("".join(content))
122
os.rename(CONSOLE_SETUP_DEFAULT+".new",
123
CONSOLE_SETUP_DEFAULT)
125
if not run_setupcon():
126
#print "setupcon failed"
131
def verify_keyboard_settings(model, layout, variant, options):
132
" helper that verfies the settings "
133
# check against char whitelist
134
allowed = "^[0-9a-zA-Z:,_]*$"
135
for s in (model, layout, variant, options):
136
if not re.match(allowed, s):
137
#print "illegal chars in '%s'" % s
139
# check if 'ckbcomp' can compile it
142
cmd += ["-model",model]
144
cmd += ["-layout", layout]
146
cmd += ["-variant", variant]
148
cmd += ["-option", options]
149
ret = subprocess.call(cmd, stdout=open(os.devnull))
3
152
def verify_proxy(proxy_type, proxy):