40
37
urwid.curses_display.curses.A_UNDERLINE |= (
 
41
38
    urwid.curses_display.curses.A_BLINK)
 
 
40
def isoformat_to_datetime(iso):
 
 
41
    "Parse an ISO 8601 date string to a datetime.datetime()"
 
 
44
    d, t = iso.split(u"T", 1)
 
 
45
    year, month, day = d.split(u"-", 2)
 
 
46
    hour, minute, second = t.split(u":", 2)
 
 
47
    second, fraction = divmod(float(second), 1)
 
 
48
    return datetime.datetime(int(year),
 
 
53
                             int(second),           # Whole seconds
 
 
54
                             int(fraction*1000000)) # Microseconds
 
43
56
class MandosClientPropertyCache(object):
 
44
57
    """This wraps a Mandos Client D-Bus proxy object, caches the
 
45
58
    properties and calls a hook function when any of them are
 
 
57
70
        self.properties.update(
 
58
71
            self.proxy.GetAll(client_interface,
 
59
72
                              dbus_interface = dbus.PROPERTIES_IFACE))
 
61
 
        #XXX This break good super behaviour!
 
62
 
#        super(MandosClientPropertyCache, self).__init__(
 
 
73
        super(MandosClientPropertyCache, self).__init__(
 
 
74
            proxy_object=proxy_object, *args, **kwargs)
 
65
76
    def property_changed(self, property=None, value=None):
 
66
77
        """This is called whenever we get a PropertyChanged signal
 
 
107
121
                                     client_interface,
 
108
122
                                     byte_arrays=True)
 
109
 
        self.proxy.connect_to_signal(u"NeedApproval",
 
113
123
        self.proxy.connect_to_signal(u"Rejected",
 
115
125
                                     client_interface,
 
116
126
                                     byte_arrays=True)
 
 
127
        last_checked_ok = isoformat_to_datetime(self.properties
 
 
129
        if last_checked_ok is None:
 
 
130
            self.last_checker_failed = True
 
 
132
            self.last_checker_failed = ((datetime.datetime.utcnow()
 
 
136
                                         self.properties["interval"]))
 
 
137
        if self.last_checker_failed:
 
 
138
            self._update_timer_callback_tag = (gobject.timeout_add
 
118
142
    def checker_completed(self, exitstatus, condition, command):
 
119
143
        if exitstatus == 0:
 
120
 
            #self.logger(u'Checker for client %s (command "%s")'
 
122
 
            #            % (self.properties[u"name"], command))
 
 
144
            if self.last_checker_failed:
 
 
145
                self.last_checker_failed = False
 
 
146
                gobject.source_remove(self._update_timer_callback_tag)
 
 
147
                self._update_timer_callback_tag = None
 
 
148
            self.logger(u'Checker for client %s (command "%s")'
 
 
150
                        % (self.properties[u"name"], command))
 
 
154
        if not self.last_checker_failed:
 
 
155
            self.last_checker_failed = True
 
 
156
            self._update_timer_callback_tag = (gobject.timeout_add
 
124
159
        if os.WIFEXITED(condition):
 
125
160
            self.logger(u'Checker for client %s (command "%s")'
 
126
161
                        u' failed with exit code %s'
 
127
162
                        % (self.properties[u"name"], command,
 
128
163
                           os.WEXITSTATUS(condition)))
 
130
 
        if os.WIFSIGNALED(condition):
 
 
164
        elif os.WIFSIGNALED(condition):
 
131
165
            self.logger(u'Checker for client %s (command "%s")'
 
132
166
                        u' was killed by signal %s'
 
133
167
                        % (self.properties[u"name"], command,
 
134
168
                           os.WTERMSIG(condition)))
 
136
 
        if os.WCOREDUMP(condition):
 
 
169
        elif os.WCOREDUMP(condition):
 
137
170
            self.logger(u'Checker for client %s (command "%s")'
 
139
172
                        % (self.properties[u"name"], command))
 
140
 
        self.logger(u'Checker for client %s completed mysteriously')
 
 
174
            self.logger(u'Checker for client %s completed mysteriously')
 
142
177
    def checker_started(self, command):
 
143
 
        #self.logger(u'Client %s started checker "%s"'
 
144
 
        #            % (self.properties[u"name"], unicode(command)))
 
 
178
        self.logger(u'Client %s started checker "%s"'
 
 
179
                    % (self.properties[u"name"], unicode(command)))
 
147
181
    def got_secret(self):
 
148
182
        self.logger(u'Client %s received its secret'
 
149
183
                    % self.properties[u"name"])
 
151
 
    def need_approval(self, timeout, default):
 
153
 
            message = u'Client %s needs approval within %s seconds'
 
155
 
            message = u'Client %s will get its secret in %s seconds'
 
157
 
                    % (self.properties[u"name"], timeout/1000))
 
159
 
    def rejected(self, reason):
 
160
 
        self.logger(u'Client %s was rejected; reason: %s'
 
161
 
                    % (self.properties[u"name"], reason))
 
 
186
        self.logger(u'Client %s was rejected'
 
 
187
                    % self.properties[u"name"])
 
163
189
    def selectable(self):
 
164
190
        """Make this a "selectable" widget.
 
 
186
212
                          u"bold-underline-blink":
 
187
213
                              u"bold-underline-blink-standout",
 
190
216
        # Rebuild focus and non-focus widgets using current properties
 
192
 
        # Base part of a client. Name!
 
193
 
        self._text = (u'%(name)s: '
 
194
 
                      % {u"name": self.properties[u"name"]})
 
196
 
        if self.properties[u"approved_pending"]:
 
197
 
            if self.properties[u"approved_by_default"]:
 
198
 
                self._text += u"Connection established to client. (d)eny?"
 
200
 
                self._text += u"Seeks approval to send secret. (a)pprove?"
 
202
 
            self._text += (u'%(enabled)s'
 
205
 
                                if self.properties[u"enabled"]
 
 
217
        self._text = (u'%(name)s: %(enabled)s%(timer)s'
 
 
218
                      % { u"name": self.properties[u"name"],
 
 
221
                               if self.properties[u"enabled"]
 
 
223
                          u"timer": (unicode(datetime.timedelta
 
 
229
                                                - isoformat_to_datetime
 
 
230
                                                (max((self.properties
 
 
235
                                                    self.properties[u"last_enabled"]))))
 
 
236
                                     if (self.last_checker_failed
 
207
240
        if not urwid.supports_unicode():
 
208
241
            self._text = self._text.encode("ascii", "replace")
 
209
242
        textlist = [(u"normal", self._text)]
 
 
233
274
    def keypress(self, (maxcol,), key):
 
235
276
        This overrides the method from urwid.FlowWidget"""
 
237
 
            self.proxy.Enable(dbus_interface = client_interface)
 
239
 
            self.proxy.Disable(dbus_interface = client_interface)
 
241
 
            self.proxy.Approve(dbus.Boolean(True, variant_level=1),
 
242
 
                               dbus_interface = client_interface)
 
244
 
            self.proxy.Approve(dbus.Boolean(False, variant_level=1),
 
245
 
                                  dbus_interface = client_interface)
 
 
277
        if key == u"e" or key == u"+":
 
 
279
        elif key == u"d" or key == u"-":
 
246
281
        elif key == u"r" or key == u"_" or key == u"ctrl k":
 
247
282
            self.server_proxy_object.RemoveClient(self.proxy
 
249
284
        elif key == u"s":
 
250
 
            self.proxy.StartChecker(dbus_interface = client_interface)
 
 
285
            self.proxy.StartChecker()
 
251
286
        elif key == u"S":
 
252
 
            self.proxy.StopChecker(dbus_interface = client_interface)
 
 
287
            self.proxy.StopChecker()
 
253
288
        elif key == u"C":
 
254
 
            self.proxy.CheckedOK(dbus_interface = client_interface)
 
 
289
            self.proxy.CheckedOK()
 
256
291
#         elif key == u"p" or key == "=":
 
257
292
#             self.proxy.pause()