/usr/share/pyshared/UbuntuSystemService/systemd.py is in ubuntu-system-service 0.2.2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import socket
import dbus
import dbus.service
import dbus.mainloop.glib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
from UbuntuSystemService.utils import *
import os
import subprocess
class HostnameSystemdBackend(dbus.service.Object):
"""
the backend implementing the hostnamed systemd interfaces
"""
# some class properties
DBUS_HOSTNAME_NAME = "org.freedesktop.hostname1"
def __init__(self, bus=None):
if bus is None:
bus = dbus.SystemBus()
bus_name = dbus.service.BusName(self.DBUS_HOSTNAME_NAME, bus=bus)
dbus.service.Object.__init__(self, bus_name, "/org/freedesktop/hostname1")
# Initialize properties
try:
f = open("/etc/hostname", "r")
static_hostname = f.readline()
f.close()
except Exception:
# We couldn't read /etc/hostname, so ignore
static_hostname = socket.gethostname()
self.properties = {
"Hostname" : socket.gethostname(),
"StaticHostname" : static_hostname,
"PrettyHostname" : "",
"IconName" : ""
}
# hostnamed interface
@dbus.service.method(DBUS_HOSTNAME_NAME,
in_signature='sb',
out_signature='',
sender_keyword='sender',
connection_keyword='conn')
def SetHostname(self, name, user_interaction, sender=None, conn=None):
"""
Set the current hostname
"""
if not authWithPolicyKit(sender, conn,
"org.freedesktop.hostname1.set-hostname",
user_interaction):
raise PermissionDeniedError("Permission denied by policy")
if subprocess.call(["hostname", "-b", name]) != 0:
raise PermissionDeniedError("Could not run hostname command")
self.properties["Hostname"] = name
self.PropertiesChanged(self.DBUS_HOSTNAME_NAME,
{ "Hostname": name },
[])
return
@dbus.service.method(DBUS_HOSTNAME_NAME,
in_signature='sb',
out_signature='',
sender_keyword='sender',
connection_keyword='conn')
def SetStaticHostname(self, name, user_interaction, sender=None, conn=None):
"""
Set the static hostname
"""
if not authWithPolicyKit(sender, conn,
"org.freedesktop.hostname1.set-static-hostname",
user_interaction):
raise PermissionDeniedError("Permission denied by policy")
try:
f = open("/etc/hostname", "w")
f.write(name)
f.close()
self.properties["StaticHostname"] = name
self.PropertiesChanged(self.DBUS_HOSTNAME_NAME,
{ "StaticHostname": name },
[])
except Exception:
raise PermissionDeniedError("Can't write to /etc/hostname")
@dbus.service.method(DBUS_HOSTNAME_NAME,
in_signature='sb',
out_signature='',
sender_keyword='sender',
connection_keyword='conn')
def SetPrettyHostname(self, name, user_interaction, sender=None, conn=None):
"""
Set the pretty hostname
"""
# We don't have a standard place to have this information,
# so ignoring for now
pass
@dbus.service.method(DBUS_HOSTNAME_NAME,
in_signature='sb',
out_signature='',
sender_keyword='sender',
connection_keyword='conn')
def SetIconName(self, name, user_interaction, sender=None, conn=None):
"""
Set icon name for this host
"""
# We don't have a standard place to have this information,
# so ignoring for now
pass
# Properties
@dbus.service.method(dbus.PROPERTIES_IFACE,
in_signature='ss',
out_signature='v')
def Get(self, interface_name, property_name):
return self.properties[property_name]
@dbus.service.method(dbus.PROPERTIES_IFACE,
in_signature='s',
out_signature='a{sv}')
def GetAll(self, interface_name):
if interface_name == self.DBUS_HOSTNAME_NAME:
return self.properties
else:
raise dbus.exceptions.DBusException(
'com.example.UnknownInterface',
'The Foo object does not implement the %s interface'
% interface_name)
@dbus.service.method(dbus.PROPERTIES_IFACE,
in_signature='ssv')
def Set(self, interface_name, property_name, new_value):
# All our properties are read-only
pass
@dbus.service.signal(dbus.PROPERTIES_IFACE,
signature='sa{sv}as')
def PropertiesChanged(self, interface_name, changed_properties,
invalidated_properties):
pass
class LocaleSystemdBackend(dbus.service.Object):
"""
the backend implementing the localed systemd interfaces
"""
# some class properties
DBUS_LOCALE_NAME = "org.freedesktop.locale1"
def __init__(self, bus=None):
if bus is None:
bus = dbus.SystemBus()
bus_name = dbus.service.BusName(self.DBUS_LOCALE_NAME, bus=bus)
dbus.service.Object.__init__(self, bus_name, "/org/freedesktop/locale1")
# Initialize properties
locale = []
try:
f = open("/etc/default/locale")
for line in f:
if line.startswith("LANG") or line.startswith("LC_"):
locale.append(line.strip('\n'))
f.close()
except Exception:
pass
(model, layout, variant, options) = get_keyboard_from_etc()
self.properties = {
"Locale" : locale,
"VConsoleKeymap" : '',
"VConsoleKeymapToggle" : '',
"X11Layout" : layout,
"X11Model" : model,
"X11Variant" : variant,
"X11Options" : options
}
# localed interface
@dbus.service.method(DBUS_LOCALE_NAME,
in_signature='asb',
out_signature='',
sender_keyword='sender',
connection_keyword='conn')
def SetLocale(self, locale, user_interaction, sender=None, conn=None):
"""
Set the system locale
"""
if not authWithPolicyKit(sender, conn,
"org.freedesktop.locale1.set-locale",
user_interaction):
raise PermissionDeniedError("Permission denied by policy")
try:
f = open("/etc/default/locale", "w")
for line in locale:
f.write(line + "\n")
f.close();
self.properties["Locale"] = locale
self.PropertiesChanged(self.DBUS_LOCALE_NAME,
{ "Locale": locale },
[])
except Exception:
raise PermissionDeniedError("Could not save to /etc/default/locale")
@dbus.service.method(DBUS_LOCALE_NAME,
in_signature='ssbb',
out_signature='',
sender_keyword='sender',
connection_keyword='conn')
def SetVConsoleKeyboard(self, keymap, keymap_toggle, convert, user_interaction, sender=None, conn=None):
"""
Set vconsole keyboard
"""
if not authWithPolicyKit(sender, conn,
"org.freedesktop.locale1.set-keyboard",
user_interaction):
raise PermissionDeniedError("Permission denied by policy")
@dbus.service.method(DBUS_LOCALE_NAME,
in_signature='ssssbb',
out_signature='',
sender_keyword='sender',
connection_keyword='conn')
def SetX11Keyboard(self, layout, model, variant, options, convert, user_interaction, sender=None, conn=None):
"""
Set the keyboard for X11
"""
if not authWithPolicyKit(sender, conn,
"org.freedesktop.locale1.set-keyboard",
user_interaction):
raise PermissionDeniedError("Permission denied by policy")
if set_keyboard_to_etc(model, layout, variant, options):
self.properties["X11Layout"] = layout
self.properties["X11Model"] = model
self.properties["X11Variant"] = variant
self.properties["X11Options"] = options
self.PropertiesChanged(self.DBUS_LOCALE_NAME,
{ "X11Layout": layout,
"X11Model": model,
"X11Variant": variant,
"X11Options": options },
[])
return
# Properties
@dbus.service.method(dbus.PROPERTIES_IFACE,
in_signature='ss',
out_signature='v')
def Get(self, interface_name, property_name):
return self.properties[property_name]
@dbus.service.method(dbus.PROPERTIES_IFACE,
in_signature='s',
out_signature='a{sv}')
def GetAll(self, interface_name):
if interface_name == self.DBUS_LOCALE_NAME:
return self.properties
else:
raise dbus.exceptions.DBusException(
'com.example.UnknownInterface',
'The Foo object does not implement the %s interface'
% interface_name)
@dbus.service.method(dbus.PROPERTIES_IFACE,
in_signature='ssv')
def Set(self, interface_name, property_name, new_value):
# All our properties are read-only
pass
@dbus.service.signal(dbus.PROPERTIES_IFACE,
signature='sa{sv}as')
def PropertiesChanged(self, interface_name, changed_properties,
invalidated_properties):
pass
|