9
from landscape.ui.lib.polkit import PolicyKitMechanism, POLICY_NAME
12
SERVICE_NAME = "com.canonical.LandscapeClientRegistration"
14
"com.canonical.LandscapeClientRegistration.RegistrationInterface"
16
"/com/canonical/LandscapeClientRegistration/RegistrationInterface"
19
class PermissionDeniedByPolicy(dbus.DBusException):
21
"com.canonical.LandscapeClientRegistration.PermissionDeniedByPolicy"
24
class RegistrationError(dbus.DBusException):
26
"com.canonical.LandscapeClientRegistration.RegistrationError"
29
class RegistrationMechanism(PolicyKitMechanism):
31
L{RegistrationMechanism} is a mechanism for invoking and observing client
32
registration over DBus. It utilises PolicyKit to ensure that only
33
administrative users may use it.
36
def __init__(self, bus_name, bypass=False, conn=None):
37
super(RegistrationMechanism, self).__init__(
38
OBJECT_PATH, bus_name, PermissionDeniedByPolicy,
39
bypass=bypass, conn=conn)
41
self.message_queue = []
44
def _do_registration(self, config_path):
45
self.register_notify("Trying to register ...\n")
46
cmd = ["landscape-config", "--silent", "-c",
47
os.path.abspath(config_path)]
49
message = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
50
self.register_notify(message)
52
except subprocess.CalledProcessError, error:
53
wait_phrase = "Please wait... "
54
wait_phrase_index = error.output.find(wait_phrase)
55
if wait_phrase_index > -1:
56
message = error.output[wait_phrase_index + len(wait_phrase):]
58
message = "Landscape configuration failed.\n%s" % error.output
59
self.register_error(message)
62
@dbus.service.signal(dbus_interface=INTERFACE_NAME,
64
def register_notify(self, message):
66
L{register_notify} is a signal sent to subscribers. It is not
67
necessary for any actual work to occur in the method as it is called
68
for the effect of invoking its decorator.
71
@dbus.service.signal(dbus_interface=INTERFACE_NAME,
73
def register_error(self, message):
75
L{register_error} is a signal sent to subscribers. It is not
76
necessary for any actual work to occur in the method as it is called
77
for the effect of invoking its decorator.
80
@dbus.service.signal(dbus_interface=INTERFACE_NAME,
82
def register_succeed(self, message):
84
L{register_succeed} is a signal sent to subscribers. It is not
85
necessary for any actual work to occur in the method as it is called
86
for the effect of invoking its decorator.
89
@dbus.service.signal(dbus_interface=INTERFACE_NAME,
91
def register_fail(self, message):
93
L{register_fail} is a signal sent to subscribers. It is not
94
necessary for any actual work to occur in the method as it is called
95
for the effect of invoking its decorator.
98
@dbus.service.method(INTERFACE_NAME,
101
sender_keyword="sender",
102
connection_keyword="conn")
103
def challenge(self, sender=None, conn=None):
105
Safely check if we can escalate permissions.
108
return self._is_allowed_by_policy(sender, conn, POLICY_NAME)
109
except PermissionDeniedByPolicy:
112
@dbus.service.method(INTERFACE_NAME,
114
out_signature="(bs)",
115
sender_keyword="sender",
116
connection_keyword="conn")
117
def register(self, config_path, sender=None, conn=None):
118
if self._is_allowed_by_policy(sender, conn, POLICY_NAME):
119
succeed, message = self._do_registration(config_path)
121
message = "Registration message sent to Landscape server.\n"
122
self.register_succeed(message)
123
return (True, message)
125
self.register_fail(message)
126
return (False, message)
128
def _do_disabling(self):
129
cmd = ["landscape-config", "--disable"]
131
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
133
except subprocess.CalledProcessError:
136
@dbus.service.signal(dbus_interface=INTERFACE_NAME,
138
def disable_succeed(self):
140
L{disable_succeed} is a signal sent to subscribers. It is not
141
necessary for any actual work to occur in the method as it is called
142
for the effect of invoking its decorator.
145
@dbus.service.signal(dbus_interface=INTERFACE_NAME,
147
def disable_fail(self):
149
L{disable_fail} is a signal sent to subscribers. It is not
150
necessary for any actual work to occur in the method as it is called
151
for the effect of invoking its decorator.
154
@dbus.service.method(INTERFACE_NAME,
157
sender_keyword="sender",
158
connection_keyword="conn")
159
def disable(self, sender=None, conn=None):
160
if self._is_allowed_by_policy(sender, conn, POLICY_NAME):
161
if self._do_disabling():
162
self.disable_succeed()
168
@dbus.service.method(INTERFACE_NAME,
171
sender_keyword="sender",
172
connection_keyword="conn")
173
def exit(self, sender=None, conn=None):