~ubuntu-branches/ubuntu/lucid/landscape-client/lucid-updates

« back to all changes in this revision

Viewing changes to landscape/ui/model/registration/mechanism.py

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack
  • Date: 2012-04-10 14:28:48 UTC
  • mfrom: (1.1.27)
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: package-import@ubuntu.com-20120410142848-7xsy4g2xii7y7ntc
ImportĀ upstreamĀ versionĀ 12.04.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import subprocess
 
2
import sys
 
3
import os
 
4
 
 
5
import dbus
 
6
import dbus.service
 
7
import dbus.glib
 
8
 
 
9
from landscape.ui.lib.polkit import PolicyKitMechanism, POLICY_NAME
 
10
 
 
11
 
 
12
SERVICE_NAME = "com.canonical.LandscapeClientRegistration"
 
13
INTERFACE_NAME = \
 
14
    "com.canonical.LandscapeClientRegistration.RegistrationInterface"
 
15
OBJECT_PATH = \
 
16
    "/com/canonical/LandscapeClientRegistration/RegistrationInterface"
 
17
 
 
18
 
 
19
class PermissionDeniedByPolicy(dbus.DBusException):
 
20
    _dbus_error_name = \
 
21
        "com.canonical.LandscapeClientRegistration.PermissionDeniedByPolicy"
 
22
 
 
23
 
 
24
class RegistrationError(dbus.DBusException):
 
25
    _dbus_error_name = \
 
26
        "com.canonical.LandscapeClientRegistration.RegistrationError"
 
27
 
 
28
 
 
29
class RegistrationMechanism(PolicyKitMechanism):
 
30
    """
 
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.
 
34
    """
 
35
 
 
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)
 
40
        self.process = None
 
41
        self.message_queue = []
 
42
        self.error_queue = []
 
43
 
 
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)]
 
48
        try:
 
49
            message = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
 
50
            self.register_notify(message)
 
51
            return True, 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):]
 
57
            else:
 
58
                message = "Landscape configuration failed.\n%s" % error.output
 
59
            self.register_error(message)
 
60
            return False, message
 
61
 
 
62
    @dbus.service.signal(dbus_interface=INTERFACE_NAME,
 
63
                         signature='s')
 
64
    def register_notify(self, message):
 
65
        """
 
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.
 
69
        """
 
70
 
 
71
    @dbus.service.signal(dbus_interface=INTERFACE_NAME,
 
72
                         signature='s')
 
73
    def register_error(self, message):
 
74
        """
 
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.
 
78
        """
 
79
 
 
80
    @dbus.service.signal(dbus_interface=INTERFACE_NAME,
 
81
                         signature='s')
 
82
    def register_succeed(self, message):
 
83
        """
 
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.
 
87
        """
 
88
 
 
89
    @dbus.service.signal(dbus_interface=INTERFACE_NAME,
 
90
                         signature='s')
 
91
    def register_fail(self, message):
 
92
        """
 
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.
 
96
        """
 
97
 
 
98
    @dbus.service.method(INTERFACE_NAME,
 
99
                         in_signature="",
 
100
                         out_signature="b",
 
101
                         sender_keyword="sender",
 
102
                         connection_keyword="conn")
 
103
    def challenge(self, sender=None, conn=None):
 
104
        """
 
105
        Safely check if we can escalate permissions.
 
106
        """
 
107
        try:
 
108
            return self._is_allowed_by_policy(sender, conn, POLICY_NAME)
 
109
        except PermissionDeniedByPolicy:
 
110
            return False
 
111
 
 
112
    @dbus.service.method(INTERFACE_NAME,
 
113
                         in_signature="s",
 
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)
 
120
            if succeed:
 
121
                message = "Registration message sent to Landscape server.\n"
 
122
                self.register_succeed(message)
 
123
                return (True, message)
 
124
            else:
 
125
                self.register_fail(message)
 
126
                return (False, message)
 
127
 
 
128
    def _do_disabling(self):
 
129
        cmd = ["landscape-config", "--disable"]
 
130
        try:
 
131
            subprocess.check_output(cmd, stderr=subprocess.STDOUT)
 
132
            return True
 
133
        except subprocess.CalledProcessError:
 
134
            return False
 
135
 
 
136
    @dbus.service.signal(dbus_interface=INTERFACE_NAME,
 
137
                         signature='')
 
138
    def disable_succeed(self):
 
139
        """
 
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.
 
143
        """
 
144
 
 
145
    @dbus.service.signal(dbus_interface=INTERFACE_NAME,
 
146
                         signature='')
 
147
    def disable_fail(self):
 
148
        """
 
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.
 
152
        """
 
153
 
 
154
    @dbus.service.method(INTERFACE_NAME,
 
155
                         in_signature="",
 
156
                         out_signature="b",
 
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()
 
163
                return True
 
164
            else:
 
165
                self.disable_fail()
 
166
                return False
 
167
 
 
168
    @dbus.service.method(INTERFACE_NAME,
 
169
                         in_signature="",
 
170
                         out_signature="",
 
171
                         sender_keyword="sender",
 
172
                         connection_keyword="conn")
 
173
    def exit(self, sender=None, conn=None):
 
174
        """
 
175
        Exit this process.
 
176
        """
 
177
        sys.exit(0)